2 * Store streams in xattrs
4 * Copyright (C) Volker Lendecke, 2008
6 * Partly based on James Peach's Darwin module, which is
8 * Copyright (C) James Peach 2006-2007
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "lib/util/tevent_unix.h"
28 #include "librpc/gen_ndr/ioctl.h"
29 #include "hash_inode.h"
32 #define DBGC_CLASS DBGC_VFS
34 struct streams_xattr_config {
37 bool store_stream_type;
45 vfs_handle_struct *handle;
48 static ssize_t get_xattr_size_fsp(struct files_struct *fsp,
49 const char *xattr_name)
55 status = get_ea_value(talloc_tos(),
61 if (!NT_STATUS_IS_OK(status)) {
65 result = ea.value.length-1;
66 TALLOC_FREE(ea.value.data);
71 * Given a stream name, populate xattr_name with the xattr name to use for
72 * accessing the stream.
74 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
76 const char *stream_name,
81 struct streams_xattr_config *config;
83 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
84 return NT_STATUS_UNSUCCESSFUL);
86 sname = talloc_strdup(ctx, stream_name + 1);
88 return NT_STATUS_NO_MEMORY;
92 * With vfs_fruit option "fruit:encoding = native" we're
93 * already converting stream names that contain illegal NTFS
94 * characters from their on-the-wire Unicode Private Range
95 * encoding to their native ASCII representation.
97 * As as result the name of xattrs storing the streams (via
98 * vfs_streams_xattr) may contain a colon, so we have to use
99 * strrchr_m() instead of strchr_m() for matching the stream
102 * In check_path_syntax() we've already ensured the streamname
103 * we got from the client is valid.
105 stype = strrchr_m(sname, ':');
109 * We only support one stream type: "$DATA"
111 if (strcasecmp_m(stype, ":$DATA") != 0) {
113 return NT_STATUS_INVALID_PARAMETER;
116 /* Split name and type */
120 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
123 config->store_stream_type ? ":$DATA" : "");
124 if (*xattr_name == NULL) {
126 return NT_STATUS_NO_MEMORY;
129 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
136 static bool streams_xattr_recheck(struct stream_io *sio)
139 char *xattr_name = NULL;
141 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
145 if (sio->fsp->fsp_name->stream_name == NULL) {
146 /* how can this happen */
151 status = streams_xattr_get_name(sio->handle, talloc_tos(),
152 sio->fsp->fsp_name->stream_name,
154 if (!NT_STATUS_IS_OK(status)) {
158 TALLOC_FREE(sio->xattr_name);
159 TALLOC_FREE(sio->base);
160 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
162 if (sio->xattr_name == NULL) {
163 DBG_DEBUG("sio->xattr_name==NULL\n");
166 TALLOC_FREE(xattr_name);
168 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
169 sio->fsp->fsp_name->base_name);
170 if (sio->base == NULL) {
171 DBG_DEBUG("sio->base==NULL\n");
175 sio->fsp_name_ptr = sio->fsp->fsp_name;
180 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
181 SMB_STRUCT_STAT *sbuf)
184 struct stream_io *io = (struct stream_io *)
185 VFS_FETCH_FSP_EXTENSION(handle, fsp);
187 if (io == NULL || fsp->base_fsp == NULL) {
188 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
191 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
193 if (!streams_xattr_recheck(io)) {
197 ret = SMB_VFS_NEXT_FSTAT(handle, fsp->base_fsp, sbuf);
202 sbuf->st_ex_size = get_xattr_size_fsp(fsp->base_fsp,
204 if (sbuf->st_ex_size == -1) {
205 SET_STAT_INVALID(*sbuf);
209 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
211 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
212 sbuf->st_ex_mode &= ~S_IFMT;
213 sbuf->st_ex_mode &= ~S_IFDIR;
214 sbuf->st_ex_mode |= S_IFREG;
215 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
220 static int streams_xattr_stat(vfs_handle_struct *handle,
221 struct smb_filename *smb_fname)
225 char *xattr_name = NULL;
226 char *tmp_stream_name = NULL;
227 struct smb_filename *smb_fname_cp = NULL;
228 struct files_struct *fsp = smb_fname->fsp;
230 if (!is_named_stream(smb_fname)) {
231 return SMB_VFS_NEXT_STAT(handle, smb_fname);
234 /* Note if lp_posix_paths() is true, we can never
235 * get here as is_named_stream() is
236 * always false. So we never need worry about
237 * not following links here. */
239 /* Populate the stat struct with info from the base file. */
240 tmp_stream_name = smb_fname->stream_name;
241 smb_fname->stream_name = NULL;
242 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
243 smb_fname->stream_name = tmp_stream_name;
249 /* Derive the xattr name to lookup. */
250 status = streams_xattr_get_name(handle, talloc_tos(),
251 smb_fname->stream_name, &xattr_name);
252 if (!NT_STATUS_IS_OK(status)) {
253 errno = map_errno_from_nt_status(status);
257 /* Augment the base file's stat information before returning. */
260 * openat_pathref_fsp() checks for the same
261 * filetype as the incoming stat info before allowing
262 * the open, so we must ensure it's correct here.
264 smb_fname->st.st_ex_mode &= ~S_IFMT;
265 smb_fname->st.st_ex_mode |= S_IFREG;
268 * openat_pathref_fsp() expects a talloc'ed
269 * smb_filename. stat can be passed a struct
270 * from the stack. Make a talloc'ed copy
271 * so openat_pathref_fsp() can add its
274 smb_fname_cp = cp_smb_filename(talloc_tos(),
276 if (smb_fname_cp == NULL) {
277 TALLOC_FREE(xattr_name);
281 status = openat_pathref_fsp(handle->conn->cwd_fsp,
283 if (!NT_STATUS_IS_OK(status)) {
284 DBG_DEBUG("openat_pathref_fsp for %s failed with %s\n",
285 smb_fname_str_dbg(smb_fname),
287 TALLOC_FREE(xattr_name);
288 TALLOC_FREE(smb_fname_cp);
289 SET_STAT_INVALID(smb_fname->st);
293 fsp = smb_fname_cp->fsp;
296 SMB_ASSERT(fsp->base_fsp != NULL);
297 smb_fname->st.st_ex_size = get_xattr_size_fsp(fsp->base_fsp,
299 if (smb_fname->st.st_ex_size == -1) {
300 TALLOC_FREE(xattr_name);
301 TALLOC_FREE(smb_fname_cp);
302 SET_STAT_INVALID(smb_fname->st);
307 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
308 smb_fname->st.st_ex_mode &= ~S_IFMT;
309 smb_fname->st.st_ex_mode |= S_IFREG;
310 smb_fname->st.st_ex_blocks =
311 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
313 TALLOC_FREE(xattr_name);
314 TALLOC_FREE(smb_fname_cp);
318 static int streams_xattr_lstat(vfs_handle_struct *handle,
319 struct smb_filename *smb_fname)
321 if (is_named_stream(smb_fname)) {
323 * There can never be EA's on a symlink.
324 * Windows will never see a symlink, and
325 * in SMB_FILENAME_POSIX_PATH mode we don't
326 * allow EA's on a symlink.
328 SET_STAT_INVALID(smb_fname->st);
332 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
335 static int streams_xattr_openat(struct vfs_handle_struct *handle,
336 const struct files_struct *dirfsp,
337 const struct smb_filename *smb_fname,
343 struct streams_xattr_config *config = NULL;
344 struct stream_io *sio = NULL;
346 char *xattr_name = NULL;
348 bool set_empty_xattr = false;
351 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
354 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
355 smb_fname_str_dbg(smb_fname), flags));
357 if (!is_named_stream(smb_fname)) {
358 return SMB_VFS_NEXT_OPENAT(handle,
367 * For now assert this, so the below SMB_VFS_SETXATTR() works.
369 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
371 status = streams_xattr_get_name(handle, talloc_tos(),
372 smb_fname->stream_name, &xattr_name);
373 if (!NT_STATUS_IS_OK(status)) {
374 errno = map_errno_from_nt_status(status);
378 status = get_ea_value(talloc_tos(),
385 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
387 if (!NT_STATUS_IS_OK(status)) {
388 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
390 * The base file is not there. This is an error even if
391 * we got O_CREAT, the higher levels should have created
392 * the base file for us.
394 DBG_DEBUG("streams_xattr_open: base file %s not around, "
395 "returning ENOENT\n", smb_fname->base_name);
400 if (!(flags & O_CREAT)) {
405 set_empty_xattr = true;
408 if (flags & O_TRUNC) {
409 set_empty_xattr = true;
412 if (set_empty_xattr) {
414 * The attribute does not exist or needs to be truncated
418 * Darn, xattrs need at least 1 byte
422 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
423 xattr_name, smb_fname->base_name));
425 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
428 flags & O_EXCL ? XATTR_CREATE : 0);
434 fakefd = vfs_fake_fd();
436 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
442 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
444 if (sio->xattr_name == NULL) {
450 * so->base needs to be a copy of fsp->fsp_name->base_name,
451 * making it identical to streams_xattr_recheck(). If the
452 * open is changing directories, fsp->fsp_name->base_name
453 * will be the full path from the share root, whilst
454 * smb_fname will be relative to the $cwd.
456 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
457 fsp->fsp_name->base_name);
458 if (sio->base == NULL) {
463 sio->fsp_name_ptr = fsp->fsp_name;
464 sio->handle = handle;
471 vfs_fake_fd_close(fakefd);
478 static int streams_xattr_close(vfs_handle_struct *handle,
484 fd = fsp_get_pathref_fd(fsp);
486 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
487 smb_fname_str_dbg(fsp->fsp_name), fd);
489 if (!is_named_stream(fsp->fsp_name)) {
490 return SMB_VFS_NEXT_CLOSE(handle, fsp);
493 ret = vfs_fake_fd_close(fd);
499 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
500 struct files_struct *dirfsp,
501 const struct smb_filename *smb_fname,
506 char *xattr_name = NULL;
508 if (!is_named_stream(smb_fname)) {
509 return SMB_VFS_NEXT_UNLINKAT(handle,
515 status = streams_xattr_get_name(handle, talloc_tos(),
516 smb_fname->stream_name, &xattr_name);
517 if (!NT_STATUS_IS_OK(status)) {
518 errno = map_errno_from_nt_status(status);
522 SMB_ASSERT(smb_fname->fsp != NULL);
523 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
525 ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
527 if ((ret == -1) && (errno == ENOATTR)) {
535 TALLOC_FREE(xattr_name);
539 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
540 struct files_struct *dirfsp,
541 const struct smb_filename *smb_fname,
545 if (flags & AT_REMOVEDIR) {
546 ret = SMB_VFS_NEXT_UNLINKAT(handle,
551 ret = streams_xattr_unlink_internal(handle,
559 static int streams_xattr_renameat(vfs_handle_struct *handle,
560 files_struct *srcfsp,
561 const struct smb_filename *smb_fname_src,
562 files_struct *dstfsp,
563 const struct smb_filename *smb_fname_dst)
567 char *src_xattr_name = NULL;
568 char *dst_xattr_name = NULL;
569 bool src_is_stream, dst_is_stream;
573 struct smb_filename *pathref_src = NULL;
574 struct smb_filename *pathref_dst = NULL;
575 struct smb_filename *full_src = NULL;
576 struct smb_filename *full_dst = NULL;
578 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
579 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
581 if (!src_is_stream && !dst_is_stream) {
582 return SMB_VFS_NEXT_RENAMEAT(handle,
589 /* For now don't allow renames from or to the default stream. */
590 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
591 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
596 /* Don't rename if the streams are identical. */
597 if (strcasecmp_m(smb_fname_src->stream_name,
598 smb_fname_dst->stream_name) == 0) {
602 /* Get the xattr names. */
603 status = streams_xattr_get_name(handle, talloc_tos(),
604 smb_fname_src->stream_name,
606 if (!NT_STATUS_IS_OK(status)) {
607 errno = map_errno_from_nt_status(status);
610 status = streams_xattr_get_name(handle, talloc_tos(),
611 smb_fname_dst->stream_name,
613 if (!NT_STATUS_IS_OK(status)) {
614 errno = map_errno_from_nt_status(status);
618 full_src = full_path_from_dirfsp_atname(talloc_tos(),
621 if (full_src == NULL) {
625 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
628 if (full_dst == NULL) {
633 /* Get a pathref for full_src (base file, no stream name). */
634 status = synthetic_pathref(talloc_tos(),
635 handle->conn->cwd_fsp,
642 if (!NT_STATUS_IS_OK(status)) {
647 /* Read the old stream from the base file fsp. */
648 status = get_ea_value(talloc_tos(),
654 if (!NT_STATUS_IS_OK(status)) {
655 errno = map_errno_from_nt_status(status);
659 /* Get a pathref for full_dst (base file, no stream name). */
660 status = synthetic_pathref(talloc_tos(),
661 handle->conn->cwd_fsp,
668 if (!NT_STATUS_IS_OK(status)) {
673 /* (Over)write the new stream on the base file fsp. */
674 nret = SMB_VFS_FSETXATTR(
681 if (errno == ENOATTR) {
688 * Remove the old stream from the base file fsp.
690 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
693 if (errno == ENOATTR) {
703 TALLOC_FREE(pathref_src);
704 TALLOC_FREE(pathref_dst);
705 TALLOC_FREE(full_src);
706 TALLOC_FREE(full_dst);
707 TALLOC_FREE(src_xattr_name);
708 TALLOC_FREE(dst_xattr_name);
712 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
714 const struct smb_filename *smb_fname,
715 bool (*fn)(struct ea_struct *ea,
722 struct streams_xattr_config *config;
724 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
725 return NT_STATUS_UNSUCCESSFUL);
727 status = get_ea_names_from_file(talloc_tos(),
731 if (!NT_STATUS_IS_OK(status)) {
735 for (i=0; i<num_names; i++) {
739 * We want to check with samba_private_attr_name()
740 * whether the xattr name is a private one,
741 * unfortunately it flags xattrs that begin with the
742 * default streams prefix as private.
744 * By only calling samba_private_attr_name() in case
745 * the xattr does NOT begin with the default prefix,
746 * we know that if it returns 'true' it definitely one
747 * of our internal xattr like "user.DOSATTRIB".
749 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
750 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
751 if (samba_private_attr_name(names[i])) {
756 if (strncmp(names[i], config->prefix,
757 config->prefix_len) != 0) {
761 status = get_ea_value(names,
767 if (!NT_STATUS_IS_OK(status)) {
768 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
770 smb_fname->base_name,
775 ea.name = talloc_asprintf(
776 ea.value.data, ":%s%s",
777 names[i] + config->prefix_len,
778 config->store_stream_type ? "" : ":$DATA");
779 if (ea.name == NULL) {
780 DEBUG(0, ("talloc failed\n"));
784 if (!fn(&ea, private_data)) {
785 TALLOC_FREE(ea.value.data);
789 TALLOC_FREE(ea.value.data);
796 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
797 struct stream_struct **streams,
798 const char *name, off_t size,
801 struct stream_struct *tmp;
803 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
809 tmp[*num_streams].name = talloc_strdup(tmp, name);
810 if (tmp[*num_streams].name == NULL) {
814 tmp[*num_streams].size = size;
815 tmp[*num_streams].alloc_size = alloc_size;
822 struct streaminfo_state {
824 vfs_handle_struct *handle;
825 unsigned int num_streams;
826 struct stream_struct *streams;
830 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
832 struct streaminfo_state *state =
833 (struct streaminfo_state *)private_data;
835 if (!add_one_stream(state->mem_ctx,
836 &state->num_streams, &state->streams,
837 ea->name, ea->value.length-1,
838 smb_roundup(state->handle->conn,
839 ea->value.length-1))) {
840 state->status = NT_STATUS_NO_MEMORY;
847 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
848 struct files_struct *fsp,
850 unsigned int *pnum_streams,
851 struct stream_struct **pstreams)
854 struct streaminfo_state state;
856 state.streams = *pstreams;
857 state.num_streams = *pnum_streams;
858 state.mem_ctx = mem_ctx;
859 state.handle = handle;
860 state.status = NT_STATUS_OK;
862 status = walk_xattr_streams(handle,
868 if (!NT_STATUS_IS_OK(status)) {
869 TALLOC_FREE(state.streams);
873 if (!NT_STATUS_IS_OK(state.status)) {
874 TALLOC_FREE(state.streams);
878 *pnum_streams = state.num_streams;
879 *pstreams = state.streams;
881 return SMB_VFS_NEXT_FSTREAMINFO(handle,
888 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
889 enum timestamp_set_resolution *p_ts_res)
891 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
894 static int streams_xattr_connect(vfs_handle_struct *handle,
895 const char *service, const char *user)
897 struct streams_xattr_config *config;
898 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
902 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
907 config = talloc_zero(handle->conn, struct streams_xattr_config);
908 if (config == NULL) {
909 DEBUG(1, ("talloc_zero() failed\n"));
914 prefix = lp_parm_const_string(SNUM(handle->conn),
915 "streams_xattr", "prefix",
917 config->prefix = talloc_strdup(config, prefix);
918 if (config->prefix == NULL) {
919 DEBUG(1, ("talloc_strdup() failed\n"));
923 config->prefix_len = strlen(config->prefix);
924 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
926 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
931 SMB_VFS_HANDLE_SET_DATA(handle, config,
932 NULL, struct stream_xattr_config,
938 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
939 files_struct *fsp, const void *data,
940 size_t n, off_t offset)
942 struct stream_io *sio =
943 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
948 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
951 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
954 if (!streams_xattr_recheck(sio)) {
958 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
960 * Requested write is beyond what can be read based on
961 * samba configuration.
962 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
963 * entire file to be skipped by File Explorer. VFAT returns
964 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
965 * to skip writing metadata, but copy data.
967 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
968 "supported extended attribute size. "
969 "Depending on filesystem type and operating system "
970 "(OS) specifics, this value may be increased using "
971 "the value of the parameter: "
972 "smbd max xattr size = <bytes>. Consult OS and "
973 "filesystem manpages prior to increasing this limit.\n",
974 sio->xattr_name, sio->base);
979 status = get_ea_value(talloc_tos(),
985 if (!NT_STATUS_IS_OK(status)) {
989 if ((offset + n) > ea.value.length-1) {
992 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
996 TALLOC_FREE(ea.value.data);
1000 ea.value.data = tmp;
1001 ea.value.length = offset + n + 1;
1002 ea.value.data[offset+n] = 0;
1005 memcpy(ea.value.data + offset, data, n);
1007 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1012 TALLOC_FREE(ea.value.data);
1021 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1022 files_struct *fsp, void *data,
1023 size_t n, off_t offset)
1025 struct stream_io *sio =
1026 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1027 struct ea_struct ea;
1029 size_t length, overlap;
1031 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1032 (int)offset, (int)n));
1035 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1038 if (!streams_xattr_recheck(sio)) {
1042 status = get_ea_value(talloc_tos(),
1048 if (!NT_STATUS_IS_OK(status)) {
1052 length = ea.value.length-1;
1054 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1057 /* Attempt to read past EOF. */
1058 if (length <= offset) {
1062 overlap = (offset + n) > length ? (length - offset) : n;
1063 memcpy(data, ea.value.data + offset, overlap);
1065 TALLOC_FREE(ea.value.data);
1069 struct streams_xattr_pread_state {
1071 struct vfs_aio_state vfs_aio_state;
1074 static void streams_xattr_pread_done(struct tevent_req *subreq);
1076 static struct tevent_req *streams_xattr_pread_send(
1077 struct vfs_handle_struct *handle,
1078 TALLOC_CTX *mem_ctx,
1079 struct tevent_context *ev,
1080 struct files_struct *fsp,
1082 size_t n, off_t offset)
1084 struct tevent_req *req = NULL;
1085 struct tevent_req *subreq = NULL;
1086 struct streams_xattr_pread_state *state = NULL;
1087 struct stream_io *sio =
1088 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1090 req = tevent_req_create(mem_ctx, &state,
1091 struct streams_xattr_pread_state);
1097 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1099 if (tevent_req_nomem(req, subreq)) {
1100 return tevent_req_post(req, ev);
1102 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1106 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1107 if (state->nread != n) {
1108 if (state->nread != -1) {
1111 tevent_req_error(req, errno);
1112 return tevent_req_post(req, ev);
1115 tevent_req_done(req);
1116 return tevent_req_post(req, ev);
1119 static void streams_xattr_pread_done(struct tevent_req *subreq)
1121 struct tevent_req *req = tevent_req_callback_data(
1122 subreq, struct tevent_req);
1123 struct streams_xattr_pread_state *state = tevent_req_data(
1124 req, struct streams_xattr_pread_state);
1126 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1127 TALLOC_FREE(subreq);
1129 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1132 tevent_req_done(req);
1135 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1136 struct vfs_aio_state *vfs_aio_state)
1138 struct streams_xattr_pread_state *state = tevent_req_data(
1139 req, struct streams_xattr_pread_state);
1141 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1145 *vfs_aio_state = state->vfs_aio_state;
1146 return state->nread;
1149 struct streams_xattr_pwrite_state {
1151 struct vfs_aio_state vfs_aio_state;
1154 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1156 static struct tevent_req *streams_xattr_pwrite_send(
1157 struct vfs_handle_struct *handle,
1158 TALLOC_CTX *mem_ctx,
1159 struct tevent_context *ev,
1160 struct files_struct *fsp,
1162 size_t n, off_t offset)
1164 struct tevent_req *req = NULL;
1165 struct tevent_req *subreq = NULL;
1166 struct streams_xattr_pwrite_state *state = NULL;
1167 struct stream_io *sio =
1168 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1170 req = tevent_req_create(mem_ctx, &state,
1171 struct streams_xattr_pwrite_state);
1177 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1179 if (tevent_req_nomem(req, subreq)) {
1180 return tevent_req_post(req, ev);
1182 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1186 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1187 if (state->nwritten != n) {
1188 if (state->nwritten != -1) {
1191 tevent_req_error(req, errno);
1192 return tevent_req_post(req, ev);
1195 tevent_req_done(req);
1196 return tevent_req_post(req, ev);
1199 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1201 struct tevent_req *req = tevent_req_callback_data(
1202 subreq, struct tevent_req);
1203 struct streams_xattr_pwrite_state *state = tevent_req_data(
1204 req, struct streams_xattr_pwrite_state);
1206 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1207 TALLOC_FREE(subreq);
1209 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1212 tevent_req_done(req);
1215 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1216 struct vfs_aio_state *vfs_aio_state)
1218 struct streams_xattr_pwrite_state *state = tevent_req_data(
1219 req, struct streams_xattr_pwrite_state);
1221 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1225 *vfs_aio_state = state->vfs_aio_state;
1226 return state->nwritten;
1229 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1230 struct files_struct *fsp,
1235 struct ea_struct ea;
1237 struct stream_io *sio =
1238 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1240 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1241 fsp_str_dbg(fsp), (double)offset));
1244 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1247 if (!streams_xattr_recheck(sio)) {
1251 status = get_ea_value(talloc_tos(),
1257 if (!NT_STATUS_IS_OK(status)) {
1261 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1265 TALLOC_FREE(ea.value.data);
1270 /* Did we expand ? */
1271 if (ea.value.length < offset + 1) {
1272 memset(&tmp[ea.value.length], '\0',
1273 offset + 1 - ea.value.length);
1276 ea.value.data = tmp;
1277 ea.value.length = offset + 1;
1278 ea.value.data[offset] = 0;
1280 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1286 TALLOC_FREE(ea.value.data);
1295 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1296 struct files_struct *fsp,
1301 struct stream_io *sio =
1302 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1304 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1306 fsp_str_dbg(fsp), (double)offset, (double)len));
1309 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1312 if (!streams_xattr_recheck(sio)) {
1316 /* Let the pwrite code path handle it. */
1321 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1322 uid_t uid, gid_t gid)
1324 struct stream_io *sio =
1325 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1328 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1334 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1338 struct stream_io *sio =
1339 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1342 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1348 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1349 struct files_struct *fsp,
1354 struct stream_io *sio =
1355 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1358 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1365 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1366 struct files_struct *fsp,
1370 struct stream_io *sio =
1371 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1374 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1381 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1382 struct files_struct *fsp,
1385 struct stream_io *sio =
1386 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1389 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1396 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1397 struct files_struct *fsp,
1403 struct stream_io *sio =
1404 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1407 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1415 struct streams_xattr_fsync_state {
1417 struct vfs_aio_state vfs_aio_state;
1420 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1422 static struct tevent_req *streams_xattr_fsync_send(
1423 struct vfs_handle_struct *handle,
1424 TALLOC_CTX *mem_ctx,
1425 struct tevent_context *ev,
1426 struct files_struct *fsp)
1428 struct tevent_req *req = NULL;
1429 struct tevent_req *subreq = NULL;
1430 struct streams_xattr_fsync_state *state = NULL;
1431 struct stream_io *sio =
1432 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1434 req = tevent_req_create(mem_ctx, &state,
1435 struct streams_xattr_fsync_state);
1441 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1442 if (tevent_req_nomem(req, subreq)) {
1443 return tevent_req_post(req, ev);
1445 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1450 * There's no pathname based sync variant and we don't have access to
1451 * the basefile handle, so we can't do anything here.
1454 tevent_req_done(req);
1455 return tevent_req_post(req, ev);
1458 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1460 struct tevent_req *req = tevent_req_callback_data(
1461 subreq, struct tevent_req);
1462 struct streams_xattr_fsync_state *state = tevent_req_data(
1463 req, struct streams_xattr_fsync_state);
1465 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1466 TALLOC_FREE(subreq);
1467 if (state->ret != 0) {
1468 tevent_req_error(req, errno);
1472 tevent_req_done(req);
1475 static int streams_xattr_fsync_recv(struct tevent_req *req,
1476 struct vfs_aio_state *vfs_aio_state)
1478 struct streams_xattr_fsync_state *state = tevent_req_data(
1479 req, struct streams_xattr_fsync_state);
1481 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1485 *vfs_aio_state = state->vfs_aio_state;
1489 static bool streams_xattr_lock(vfs_handle_struct *handle,
1496 struct stream_io *sio =
1497 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1500 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1506 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1513 struct stream_io *sio =
1514 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1517 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1518 pcount, ptype, ppid);
1525 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1527 uint32_t share_access,
1528 uint32_t access_mask)
1530 struct stream_io *sio =
1531 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1534 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1535 share_access, access_mask);
1541 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1545 struct stream_io *sio =
1546 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1549 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1555 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1557 struct lock_struct *plock)
1559 struct stream_io *sio =
1560 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1563 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1569 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1570 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1571 .connect_fn = streams_xattr_connect,
1572 .openat_fn = streams_xattr_openat,
1573 .close_fn = streams_xattr_close,
1574 .stat_fn = streams_xattr_stat,
1575 .fstat_fn = streams_xattr_fstat,
1576 .lstat_fn = streams_xattr_lstat,
1577 .pread_fn = streams_xattr_pread,
1578 .pwrite_fn = streams_xattr_pwrite,
1579 .pread_send_fn = streams_xattr_pread_send,
1580 .pread_recv_fn = streams_xattr_pread_recv,
1581 .pwrite_send_fn = streams_xattr_pwrite_send,
1582 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1583 .unlinkat_fn = streams_xattr_unlinkat,
1584 .renameat_fn = streams_xattr_renameat,
1585 .ftruncate_fn = streams_xattr_ftruncate,
1586 .fallocate_fn = streams_xattr_fallocate,
1587 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1589 .fsync_send_fn = streams_xattr_fsync_send,
1590 .fsync_recv_fn = streams_xattr_fsync_recv,
1592 .lock_fn = streams_xattr_lock,
1593 .getlock_fn = streams_xattr_getlock,
1594 .kernel_flock_fn = streams_xattr_kernel_flock,
1595 .linux_setlease_fn = streams_xattr_linux_setlease,
1596 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1598 .fchown_fn = streams_xattr_fchown,
1599 .fchmod_fn = streams_xattr_fchmod,
1601 .fgetxattr_fn = streams_xattr_fgetxattr,
1602 .flistxattr_fn = streams_xattr_flistxattr,
1603 .fremovexattr_fn = streams_xattr_fremovexattr,
1604 .fsetxattr_fn = streams_xattr_fsetxattr,
1608 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1610 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1611 &vfs_streams_xattr_fns);