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_fsp(talloc_tos(),
59 if (!NT_STATUS_IS_OK(status)) {
63 result = ea.value.length-1;
64 TALLOC_FREE(ea.value.data);
69 * Given a stream name, populate xattr_name with the xattr name to use for
70 * accessing the stream.
72 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
74 const char *stream_name,
77 size_t stream_name_len = strlen(stream_name);
79 struct streams_xattr_config *config;
81 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
82 return NT_STATUS_UNSUCCESSFUL);
84 SMB_ASSERT(stream_name[0] == ':');
88 * With vfs_fruit option "fruit:encoding = native" we're
89 * already converting stream names that contain illegal NTFS
90 * characters from their on-the-wire Unicode Private Range
91 * encoding to their native ASCII representation.
93 * As as result the name of xattrs storing the streams (via
94 * vfs_streams_xattr) may contain a colon, so we have to use
95 * strrchr_m() instead of strchr_m() for matching the stream
98 * In check_path_syntax() we've already ensured the streamname
99 * we got from the client is valid.
101 stype = strrchr_m(stream_name, ':');
105 * We only support one stream type: "$DATA"
107 if (strcasecmp_m(stype, ":$DATA") != 0) {
108 return NT_STATUS_INVALID_PARAMETER;
111 /* Split name and type */
112 stream_name_len = (stype - stream_name);
115 *xattr_name = talloc_asprintf(ctx, "%s%.*s%s",
117 (int)stream_name_len,
119 config->store_stream_type ? ":$DATA" : "");
120 if (*xattr_name == NULL) {
121 return NT_STATUS_NO_MEMORY;
124 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
130 static bool streams_xattr_recheck(struct stream_io *sio)
133 char *xattr_name = NULL;
135 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
139 if (sio->fsp->fsp_name->stream_name == NULL) {
140 /* how can this happen */
145 status = streams_xattr_get_name(sio->handle, talloc_tos(),
146 sio->fsp->fsp_name->stream_name,
148 if (!NT_STATUS_IS_OK(status)) {
152 TALLOC_FREE(sio->xattr_name);
153 TALLOC_FREE(sio->base);
154 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
156 if (sio->xattr_name == NULL) {
157 DBG_DEBUG("sio->xattr_name==NULL\n");
160 TALLOC_FREE(xattr_name);
162 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
163 sio->fsp->fsp_name->base_name);
164 if (sio->base == NULL) {
165 DBG_DEBUG("sio->base==NULL\n");
169 sio->fsp_name_ptr = sio->fsp->fsp_name;
174 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
175 SMB_STRUCT_STAT *sbuf)
178 struct stream_io *io = (struct stream_io *)
179 VFS_FETCH_FSP_EXTENSION(handle, fsp);
181 if (io == NULL || !fsp_is_alternate_stream(fsp)) {
182 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
185 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
187 if (!streams_xattr_recheck(io)) {
191 ret = SMB_VFS_NEXT_FSTAT(handle, fsp->base_fsp, sbuf);
196 sbuf->st_ex_size = get_xattr_size_fsp(fsp->base_fsp,
198 if (sbuf->st_ex_size == -1) {
199 SET_STAT_INVALID(*sbuf);
203 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
205 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
206 sbuf->st_ex_mode &= ~S_IFMT;
207 sbuf->st_ex_mode &= ~S_IFDIR;
208 sbuf->st_ex_mode |= S_IFREG;
209 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
214 static int streams_xattr_stat(vfs_handle_struct *handle,
215 struct smb_filename *smb_fname)
219 char *xattr_name = NULL;
220 char *tmp_stream_name = NULL;
221 struct smb_filename *pathref = NULL;
222 struct files_struct *fsp = smb_fname->fsp;
224 if (!is_named_stream(smb_fname)) {
225 return SMB_VFS_NEXT_STAT(handle, smb_fname);
228 /* Note if lp_posix_paths() is true, we can never
229 * get here as is_named_stream() is
230 * always false. So we never need worry about
231 * not following links here. */
233 /* Populate the stat struct with info from the base file. */
234 tmp_stream_name = smb_fname->stream_name;
235 smb_fname->stream_name = NULL;
236 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
237 smb_fname->stream_name = tmp_stream_name;
243 /* Derive the xattr name to lookup. */
244 status = streams_xattr_get_name(handle, talloc_tos(),
245 smb_fname->stream_name, &xattr_name);
246 if (!NT_STATUS_IS_OK(status)) {
247 errno = map_errno_from_nt_status(status);
251 /* Augment the base file's stat information before returning. */
253 status = synthetic_pathref(talloc_tos(),
254 handle->conn->cwd_fsp,
255 smb_fname->base_name,
261 if (!NT_STATUS_IS_OK(status)) {
262 TALLOC_FREE(xattr_name);
263 SET_STAT_INVALID(smb_fname->st);
272 smb_fname->st.st_ex_size = get_xattr_size_fsp(fsp,
274 if (smb_fname->st.st_ex_size == -1) {
275 TALLOC_FREE(xattr_name);
276 TALLOC_FREE(pathref);
277 SET_STAT_INVALID(smb_fname->st);
282 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
283 smb_fname->st.st_ex_mode &= ~S_IFMT;
284 smb_fname->st.st_ex_mode |= S_IFREG;
285 smb_fname->st.st_ex_blocks =
286 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
288 TALLOC_FREE(xattr_name);
289 TALLOC_FREE(pathref);
293 static int streams_xattr_lstat(vfs_handle_struct *handle,
294 struct smb_filename *smb_fname)
296 if (is_named_stream(smb_fname)) {
298 * There can never be EA's on a symlink.
299 * Windows will never see a symlink, and
300 * in SMB_FILENAME_POSIX_PATH mode we don't
301 * allow EA's on a symlink.
303 SET_STAT_INVALID(smb_fname->st);
307 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
310 static int streams_xattr_openat(struct vfs_handle_struct *handle,
311 const struct files_struct *dirfsp,
312 const struct smb_filename *smb_fname,
314 const struct vfs_open_how *how)
317 struct streams_xattr_config *config = NULL;
318 struct stream_io *sio = NULL;
320 char *xattr_name = NULL;
322 bool set_empty_xattr = false;
325 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
328 DBG_DEBUG("called for %s with flags 0x%x\n",
329 smb_fname_str_dbg(smb_fname),
332 if (!is_named_stream(smb_fname)) {
333 return SMB_VFS_NEXT_OPENAT(handle,
340 if (how->resolve != 0) {
345 SMB_ASSERT(fsp_is_alternate_stream(fsp));
346 SMB_ASSERT(dirfsp == NULL);
348 status = streams_xattr_get_name(handle, talloc_tos(),
349 smb_fname->stream_name, &xattr_name);
350 if (!NT_STATUS_IS_OK(status)) {
351 errno = map_errno_from_nt_status(status);
355 status = get_ea_value_fsp(talloc_tos(),
360 DBG_DEBUG("get_ea_value_fsp returned %s\n", nt_errstr(status));
362 if (!NT_STATUS_IS_OK(status)) {
363 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
365 * The base file is not there. This is an error even if
366 * we got O_CREAT, the higher levels should have created
367 * the base file for us.
369 DBG_DEBUG("streams_xattr_open: base file %s not around, "
370 "returning ENOENT\n", smb_fname->base_name);
375 if (!(how->flags & O_CREAT)) {
380 set_empty_xattr = true;
383 if (how->flags & O_TRUNC) {
384 set_empty_xattr = true;
387 if (set_empty_xattr) {
389 * The attribute does not exist or needs to be truncated
393 * Darn, xattrs need at least 1 byte
397 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
398 xattr_name, smb_fname->base_name));
400 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
403 how->flags & O_EXCL ? XATTR_CREATE : 0);
409 fakefd = vfs_fake_fd();
411 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
417 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
419 if (sio->xattr_name == NULL) {
425 * so->base needs to be a copy of fsp->fsp_name->base_name,
426 * making it identical to streams_xattr_recheck(). If the
427 * open is changing directories, fsp->fsp_name->base_name
428 * will be the full path from the share root, whilst
429 * smb_fname will be relative to the $cwd.
431 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
432 fsp->fsp_name->base_name);
433 if (sio->base == NULL) {
438 sio->fsp_name_ptr = fsp->fsp_name;
439 sio->handle = handle;
446 vfs_fake_fd_close(fakefd);
453 static int streams_xattr_close(vfs_handle_struct *handle,
459 fd = fsp_get_pathref_fd(fsp);
461 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
462 smb_fname_str_dbg(fsp->fsp_name), fd);
464 if (!fsp_is_alternate_stream(fsp)) {
465 return SMB_VFS_NEXT_CLOSE(handle, fsp);
468 ret = vfs_fake_fd_close(fd);
474 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
475 struct files_struct *dirfsp,
476 const struct smb_filename *smb_fname,
481 char *xattr_name = NULL;
482 struct smb_filename *pathref = NULL;
483 struct files_struct *fsp = smb_fname->fsp;
485 if (!is_named_stream(smb_fname)) {
486 return SMB_VFS_NEXT_UNLINKAT(handle,
492 /* A stream can never be rmdir'ed */
493 SMB_ASSERT((flags & AT_REMOVEDIR) == 0);
495 status = streams_xattr_get_name(handle, talloc_tos(),
496 smb_fname->stream_name, &xattr_name);
497 if (!NT_STATUS_IS_OK(status)) {
498 errno = map_errno_from_nt_status(status);
503 status = synthetic_pathref(talloc_tos(),
504 handle->conn->cwd_fsp,
505 smb_fname->base_name,
511 if (!NT_STATUS_IS_OK(status)) {
517 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
521 ret = SMB_VFS_FREMOVEXATTR(fsp, xattr_name);
523 if ((ret == -1) && (errno == ENOATTR)) {
531 TALLOC_FREE(xattr_name);
532 TALLOC_FREE(pathref);
536 static int streams_xattr_renameat(vfs_handle_struct *handle,
537 files_struct *srcfsp,
538 const struct smb_filename *smb_fname_src,
539 files_struct *dstfsp,
540 const struct smb_filename *smb_fname_dst)
544 char *src_xattr_name = NULL;
545 char *dst_xattr_name = NULL;
546 bool src_is_stream, dst_is_stream;
550 struct smb_filename *pathref_src = NULL;
551 struct smb_filename *pathref_dst = NULL;
552 struct smb_filename *full_src = NULL;
553 struct smb_filename *full_dst = NULL;
555 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
556 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
558 if (!src_is_stream && !dst_is_stream) {
559 return SMB_VFS_NEXT_RENAMEAT(handle,
566 /* For now don't allow renames from or to the default stream. */
567 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
568 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
573 /* Don't rename if the streams are identical. */
574 if (strcasecmp_m(smb_fname_src->stream_name,
575 smb_fname_dst->stream_name) == 0) {
579 /* Get the xattr names. */
580 status = streams_xattr_get_name(handle, talloc_tos(),
581 smb_fname_src->stream_name,
583 if (!NT_STATUS_IS_OK(status)) {
584 errno = map_errno_from_nt_status(status);
587 status = streams_xattr_get_name(handle, talloc_tos(),
588 smb_fname_dst->stream_name,
590 if (!NT_STATUS_IS_OK(status)) {
591 errno = map_errno_from_nt_status(status);
595 full_src = full_path_from_dirfsp_atname(talloc_tos(),
598 if (full_src == NULL) {
602 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
605 if (full_dst == NULL) {
610 /* Get a pathref for full_src (base file, no stream name). */
611 status = synthetic_pathref(talloc_tos(),
612 handle->conn->cwd_fsp,
619 if (!NT_STATUS_IS_OK(status)) {
624 /* Read the old stream from the base file fsp. */
625 status = get_ea_value_fsp(talloc_tos(),
629 if (!NT_STATUS_IS_OK(status)) {
630 errno = map_errno_from_nt_status(status);
634 /* Get a pathref for full_dst (base file, no stream name). */
635 status = synthetic_pathref(talloc_tos(),
636 handle->conn->cwd_fsp,
643 if (!NT_STATUS_IS_OK(status)) {
648 /* (Over)write the new stream on the base file fsp. */
649 nret = SMB_VFS_FSETXATTR(
656 if (errno == ENOATTR) {
663 * Remove the old stream from the base file fsp.
665 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
668 if (errno == ENOATTR) {
678 TALLOC_FREE(pathref_src);
679 TALLOC_FREE(pathref_dst);
680 TALLOC_FREE(full_src);
681 TALLOC_FREE(full_dst);
682 TALLOC_FREE(src_xattr_name);
683 TALLOC_FREE(dst_xattr_name);
687 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
689 const struct smb_filename *smb_fname,
690 bool (*fn)(struct ea_struct *ea,
697 struct streams_xattr_config *config;
699 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
700 return NT_STATUS_UNSUCCESSFUL);
702 status = get_ea_names_from_fsp(talloc_tos(),
706 if (!NT_STATUS_IS_OK(status)) {
710 for (i=0; i<num_names; i++) {
714 * We want to check with samba_private_attr_name()
715 * whether the xattr name is a private one,
716 * unfortunately it flags xattrs that begin with the
717 * default streams prefix as private.
719 * By only calling samba_private_attr_name() in case
720 * the xattr does NOT begin with the default prefix,
721 * we know that if it returns 'true' it definitely one
722 * of our internal xattr like "user.DOSATTRIB".
724 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
725 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
726 if (samba_private_attr_name(names[i])) {
731 if (strncmp(names[i], config->prefix,
732 config->prefix_len) != 0) {
736 status = get_ea_value_fsp(names,
740 if (!NT_STATUS_IS_OK(status)) {
741 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
743 smb_fname->base_name,
748 ea.name = talloc_asprintf(
749 ea.value.data, ":%s%s",
750 names[i] + config->prefix_len,
751 config->store_stream_type ? "" : ":$DATA");
752 if (ea.name == NULL) {
753 DEBUG(0, ("talloc failed\n"));
757 if (!fn(&ea, private_data)) {
758 TALLOC_FREE(ea.value.data);
762 TALLOC_FREE(ea.value.data);
769 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
770 struct stream_struct **streams,
771 const char *name, off_t size,
774 struct stream_struct *tmp;
776 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
782 tmp[*num_streams].name = talloc_strdup(tmp, name);
783 if (tmp[*num_streams].name == NULL) {
787 tmp[*num_streams].size = size;
788 tmp[*num_streams].alloc_size = alloc_size;
795 struct streaminfo_state {
797 vfs_handle_struct *handle;
798 unsigned int num_streams;
799 struct stream_struct *streams;
803 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
805 struct streaminfo_state *state =
806 (struct streaminfo_state *)private_data;
808 if (!add_one_stream(state->mem_ctx,
809 &state->num_streams, &state->streams,
810 ea->name, ea->value.length-1,
811 smb_roundup(state->handle->conn,
812 ea->value.length-1))) {
813 state->status = NT_STATUS_NO_MEMORY;
820 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
821 struct files_struct *fsp,
823 unsigned int *pnum_streams,
824 struct stream_struct **pstreams)
827 struct streaminfo_state state;
829 state.streams = *pstreams;
830 state.num_streams = *pnum_streams;
831 state.mem_ctx = mem_ctx;
832 state.handle = handle;
833 state.status = NT_STATUS_OK;
835 status = walk_xattr_streams(handle,
841 if (!NT_STATUS_IS_OK(status)) {
842 TALLOC_FREE(state.streams);
846 if (!NT_STATUS_IS_OK(state.status)) {
847 TALLOC_FREE(state.streams);
851 *pnum_streams = state.num_streams;
852 *pstreams = state.streams;
854 return SMB_VFS_NEXT_FSTREAMINFO(handle,
861 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
862 enum timestamp_set_resolution *p_ts_res)
864 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
867 static int streams_xattr_connect(vfs_handle_struct *handle,
868 const char *service, const char *user)
870 struct streams_xattr_config *config;
871 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
875 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
880 config = talloc_zero(handle->conn, struct streams_xattr_config);
881 if (config == NULL) {
882 DEBUG(1, ("talloc_zero() failed\n"));
887 prefix = lp_parm_const_string(SNUM(handle->conn),
888 "streams_xattr", "prefix",
890 config->prefix = talloc_strdup(config, prefix);
891 if (config->prefix == NULL) {
892 DEBUG(1, ("talloc_strdup() failed\n"));
896 config->prefix_len = strlen(config->prefix);
897 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
899 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
904 SMB_VFS_HANDLE_SET_DATA(handle, config,
905 NULL, struct stream_xattr_config,
911 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
912 files_struct *fsp, const void *data,
913 size_t n, off_t offset)
915 struct stream_io *sio =
916 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
921 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
924 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
927 if (!streams_xattr_recheck(sio)) {
931 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
933 * Requested write is beyond what can be read based on
934 * samba configuration.
935 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
936 * entire file to be skipped by File Explorer. VFAT returns
937 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
938 * to skip writing metadata, but copy data.
940 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
941 "supported extended attribute size. "
942 "Depending on filesystem type and operating system "
943 "(OS) specifics, this value may be increased using "
944 "the value of the parameter: "
945 "smbd max xattr size = <bytes>. Consult OS and "
946 "filesystem manpages prior to increasing this limit.\n",
947 sio->xattr_name, sio->base);
952 status = get_ea_value_fsp(talloc_tos(),
956 if (!NT_STATUS_IS_OK(status)) {
960 if ((offset + n) > ea.value.length-1) {
963 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
967 TALLOC_FREE(ea.value.data);
972 ea.value.length = offset + n + 1;
973 ea.value.data[offset+n] = 0;
976 memcpy(ea.value.data + offset, data, n);
978 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
983 TALLOC_FREE(ea.value.data);
992 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
993 files_struct *fsp, void *data,
994 size_t n, off_t offset)
996 struct stream_io *sio =
997 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1000 size_t length, overlap;
1002 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1003 (int)offset, (int)n));
1006 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1009 if (!streams_xattr_recheck(sio)) {
1013 status = get_ea_value_fsp(talloc_tos(),
1017 if (!NT_STATUS_IS_OK(status)) {
1021 length = ea.value.length-1;
1023 DBG_DEBUG("get_ea_value_fsp returned %d bytes\n",
1026 /* Attempt to read past EOF. */
1027 if (length <= offset) {
1031 overlap = (offset + n) > length ? (length - offset) : n;
1032 memcpy(data, ea.value.data + offset, overlap);
1034 TALLOC_FREE(ea.value.data);
1038 struct streams_xattr_pread_state {
1040 struct vfs_aio_state vfs_aio_state;
1043 static void streams_xattr_pread_done(struct tevent_req *subreq);
1045 static struct tevent_req *streams_xattr_pread_send(
1046 struct vfs_handle_struct *handle,
1047 TALLOC_CTX *mem_ctx,
1048 struct tevent_context *ev,
1049 struct files_struct *fsp,
1051 size_t n, off_t offset)
1053 struct tevent_req *req = NULL;
1054 struct tevent_req *subreq = NULL;
1055 struct streams_xattr_pread_state *state = NULL;
1056 struct stream_io *sio =
1057 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1059 req = tevent_req_create(mem_ctx, &state,
1060 struct streams_xattr_pread_state);
1066 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1068 if (tevent_req_nomem(req, subreq)) {
1069 return tevent_req_post(req, ev);
1071 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1075 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1076 if (state->nread != n) {
1077 if (state->nread != -1) {
1080 tevent_req_error(req, errno);
1081 return tevent_req_post(req, ev);
1084 tevent_req_done(req);
1085 return tevent_req_post(req, ev);
1088 static void streams_xattr_pread_done(struct tevent_req *subreq)
1090 struct tevent_req *req = tevent_req_callback_data(
1091 subreq, struct tevent_req);
1092 struct streams_xattr_pread_state *state = tevent_req_data(
1093 req, struct streams_xattr_pread_state);
1095 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1096 TALLOC_FREE(subreq);
1098 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1101 tevent_req_done(req);
1104 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1105 struct vfs_aio_state *vfs_aio_state)
1107 struct streams_xattr_pread_state *state = tevent_req_data(
1108 req, struct streams_xattr_pread_state);
1110 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1114 *vfs_aio_state = state->vfs_aio_state;
1115 return state->nread;
1118 struct streams_xattr_pwrite_state {
1120 struct vfs_aio_state vfs_aio_state;
1123 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1125 static struct tevent_req *streams_xattr_pwrite_send(
1126 struct vfs_handle_struct *handle,
1127 TALLOC_CTX *mem_ctx,
1128 struct tevent_context *ev,
1129 struct files_struct *fsp,
1131 size_t n, off_t offset)
1133 struct tevent_req *req = NULL;
1134 struct tevent_req *subreq = NULL;
1135 struct streams_xattr_pwrite_state *state = NULL;
1136 struct stream_io *sio =
1137 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1139 req = tevent_req_create(mem_ctx, &state,
1140 struct streams_xattr_pwrite_state);
1146 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1148 if (tevent_req_nomem(req, subreq)) {
1149 return tevent_req_post(req, ev);
1151 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1155 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1156 if (state->nwritten != n) {
1157 if (state->nwritten != -1) {
1160 tevent_req_error(req, errno);
1161 return tevent_req_post(req, ev);
1164 tevent_req_done(req);
1165 return tevent_req_post(req, ev);
1168 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1170 struct tevent_req *req = tevent_req_callback_data(
1171 subreq, struct tevent_req);
1172 struct streams_xattr_pwrite_state *state = tevent_req_data(
1173 req, struct streams_xattr_pwrite_state);
1175 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1176 TALLOC_FREE(subreq);
1178 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1181 tevent_req_done(req);
1184 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1185 struct vfs_aio_state *vfs_aio_state)
1187 struct streams_xattr_pwrite_state *state = tevent_req_data(
1188 req, struct streams_xattr_pwrite_state);
1190 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1194 *vfs_aio_state = state->vfs_aio_state;
1195 return state->nwritten;
1198 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1199 struct files_struct *fsp,
1204 struct ea_struct ea;
1206 struct stream_io *sio =
1207 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1209 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1210 fsp_str_dbg(fsp), (double)offset));
1213 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1216 if (!streams_xattr_recheck(sio)) {
1220 status = get_ea_value_fsp(talloc_tos(),
1224 if (!NT_STATUS_IS_OK(status)) {
1228 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1232 TALLOC_FREE(ea.value.data);
1237 /* Did we expand ? */
1238 if (ea.value.length < offset + 1) {
1239 memset(&tmp[ea.value.length], '\0',
1240 offset + 1 - ea.value.length);
1243 ea.value.data = tmp;
1244 ea.value.length = offset + 1;
1245 ea.value.data[offset] = 0;
1247 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1253 TALLOC_FREE(ea.value.data);
1262 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1263 struct files_struct *fsp,
1268 struct stream_io *sio =
1269 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1271 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1273 fsp_str_dbg(fsp), (double)offset, (double)len));
1276 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1279 if (!streams_xattr_recheck(sio)) {
1283 /* Let the pwrite code path handle it. */
1288 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1289 uid_t uid, gid_t gid)
1291 struct stream_io *sio =
1292 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1295 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1301 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1305 struct stream_io *sio =
1306 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1309 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1315 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1316 struct files_struct *fsp,
1321 struct stream_io *sio =
1322 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1325 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1332 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1333 struct files_struct *fsp,
1337 struct stream_io *sio =
1338 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1341 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1348 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1349 struct files_struct *fsp,
1352 struct stream_io *sio =
1353 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1356 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1363 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1364 struct files_struct *fsp,
1370 struct stream_io *sio =
1371 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1374 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1382 struct streams_xattr_fsync_state {
1384 struct vfs_aio_state vfs_aio_state;
1387 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1389 static struct tevent_req *streams_xattr_fsync_send(
1390 struct vfs_handle_struct *handle,
1391 TALLOC_CTX *mem_ctx,
1392 struct tevent_context *ev,
1393 struct files_struct *fsp)
1395 struct tevent_req *req = NULL;
1396 struct tevent_req *subreq = NULL;
1397 struct streams_xattr_fsync_state *state = NULL;
1398 struct stream_io *sio =
1399 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1401 req = tevent_req_create(mem_ctx, &state,
1402 struct streams_xattr_fsync_state);
1408 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1409 if (tevent_req_nomem(req, subreq)) {
1410 return tevent_req_post(req, ev);
1412 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1417 * There's no pathname based sync variant and we don't have access to
1418 * the basefile handle, so we can't do anything here.
1421 tevent_req_done(req);
1422 return tevent_req_post(req, ev);
1425 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1427 struct tevent_req *req = tevent_req_callback_data(
1428 subreq, struct tevent_req);
1429 struct streams_xattr_fsync_state *state = tevent_req_data(
1430 req, struct streams_xattr_fsync_state);
1432 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1433 TALLOC_FREE(subreq);
1434 if (state->ret != 0) {
1435 tevent_req_error(req, errno);
1439 tevent_req_done(req);
1442 static int streams_xattr_fsync_recv(struct tevent_req *req,
1443 struct vfs_aio_state *vfs_aio_state)
1445 struct streams_xattr_fsync_state *state = tevent_req_data(
1446 req, struct streams_xattr_fsync_state);
1448 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1452 *vfs_aio_state = state->vfs_aio_state;
1456 static bool streams_xattr_lock(vfs_handle_struct *handle,
1463 struct stream_io *sio =
1464 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1467 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1473 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1480 struct stream_io *sio =
1481 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1484 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1485 pcount, ptype, ppid);
1492 static int streams_xattr_filesystem_sharemode(vfs_handle_struct *handle,
1494 uint32_t share_access,
1495 uint32_t access_mask)
1497 struct stream_io *sio =
1498 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1501 return SMB_VFS_NEXT_FILESYSTEM_SHAREMODE(handle,
1510 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1514 struct stream_io *sio =
1515 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1518 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1524 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1526 struct lock_struct *plock)
1528 struct stream_io *sio =
1529 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1532 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1538 static int streams_xattr_fcntl(vfs_handle_struct *handle,
1543 va_list dup_cmd_arg;
1547 if (fsp_is_alternate_stream(fsp)) {
1553 DBG_ERR("Unsupported fcntl() cmd [%d] on [%s]\n",
1554 cmd, fsp_str_dbg(fsp));
1560 va_copy(dup_cmd_arg, cmd_arg);
1561 arg = va_arg(dup_cmd_arg, void *);
1563 ret = SMB_VFS_NEXT_FCNTL(handle, fsp, cmd, arg);
1565 va_end(dup_cmd_arg);
1570 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1571 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1572 .connect_fn = streams_xattr_connect,
1573 .openat_fn = streams_xattr_openat,
1574 .close_fn = streams_xattr_close,
1575 .stat_fn = streams_xattr_stat,
1576 .fstat_fn = streams_xattr_fstat,
1577 .lstat_fn = streams_xattr_lstat,
1578 .pread_fn = streams_xattr_pread,
1579 .pwrite_fn = streams_xattr_pwrite,
1580 .pread_send_fn = streams_xattr_pread_send,
1581 .pread_recv_fn = streams_xattr_pread_recv,
1582 .pwrite_send_fn = streams_xattr_pwrite_send,
1583 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1584 .unlinkat_fn = streams_xattr_unlinkat,
1585 .renameat_fn = streams_xattr_renameat,
1586 .ftruncate_fn = streams_xattr_ftruncate,
1587 .fallocate_fn = streams_xattr_fallocate,
1588 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1590 .fsync_send_fn = streams_xattr_fsync_send,
1591 .fsync_recv_fn = streams_xattr_fsync_recv,
1593 .lock_fn = streams_xattr_lock,
1594 .getlock_fn = streams_xattr_getlock,
1595 .filesystem_sharemode_fn = streams_xattr_filesystem_sharemode,
1596 .linux_setlease_fn = streams_xattr_linux_setlease,
1597 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1598 .fcntl_fn = streams_xattr_fcntl,
1600 .fchown_fn = streams_xattr_fchown,
1601 .fchmod_fn = streams_xattr_fchmod,
1603 .fgetxattr_fn = streams_xattr_fgetxattr,
1604 .flistxattr_fn = streams_xattr_flistxattr,
1605 .fremovexattr_fn = streams_xattr_fremovexattr,
1606 .fsetxattr_fn = streams_xattr_fsetxattr,
1610 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1612 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1613 &vfs_streams_xattr_fns);