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(connection_struct *conn,
49 struct files_struct *fsp,
50 const struct smb_filename *smb_fname,
51 const char *xattr_name)
58 status = get_ea_value(talloc_tos(),
65 status = get_ea_value(talloc_tos(),
73 if (!NT_STATUS_IS_OK(status)) {
77 result = ea.value.length-1;
78 TALLOC_FREE(ea.value.data);
83 * Given a stream name, populate xattr_name with the xattr name to use for
84 * accessing the stream.
86 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
88 const char *stream_name,
93 struct streams_xattr_config *config;
95 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
96 return NT_STATUS_UNSUCCESSFUL);
98 sname = talloc_strdup(ctx, stream_name + 1);
100 return NT_STATUS_NO_MEMORY;
104 * With vfs_fruit option "fruit:encoding = native" we're
105 * already converting stream names that contain illegal NTFS
106 * characters from their on-the-wire Unicode Private Range
107 * encoding to their native ASCII representation.
109 * As as result the name of xattrs storing the streams (via
110 * vfs_streams_xattr) may contain a colon, so we have to use
111 * strrchr_m() instead of strchr_m() for matching the stream
114 * In check_path_syntax() we've already ensured the streamname
115 * we got from the client is valid.
117 stype = strrchr_m(sname, ':');
121 * We only support one stream type: "$DATA"
123 if (strcasecmp_m(stype, ":$DATA") != 0) {
125 return NT_STATUS_INVALID_PARAMETER;
128 /* Split name and type */
132 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
135 config->store_stream_type ? ":$DATA" : "");
136 if (*xattr_name == NULL) {
138 return NT_STATUS_NO_MEMORY;
141 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
148 static bool streams_xattr_recheck(struct stream_io *sio)
151 char *xattr_name = NULL;
153 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
157 if (sio->fsp->fsp_name->stream_name == NULL) {
158 /* how can this happen */
163 status = streams_xattr_get_name(sio->handle, talloc_tos(),
164 sio->fsp->fsp_name->stream_name,
166 if (!NT_STATUS_IS_OK(status)) {
170 TALLOC_FREE(sio->xattr_name);
171 TALLOC_FREE(sio->base);
172 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
174 if (sio->xattr_name == NULL) {
175 DBG_DEBUG("sio->xattr_name==NULL\n");
178 TALLOC_FREE(xattr_name);
180 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
181 sio->fsp->fsp_name->base_name);
182 if (sio->base == NULL) {
183 DBG_DEBUG("sio->base==NULL\n");
187 sio->fsp_name_ptr = sio->fsp->fsp_name;
193 * Helper to stat/lstat the base file of an smb_fname.
195 static int streams_xattr_stat_base(vfs_handle_struct *handle,
196 struct smb_filename *smb_fname,
199 char *tmp_stream_name;
202 tmp_stream_name = smb_fname->stream_name;
203 smb_fname->stream_name = NULL;
205 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
207 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
209 smb_fname->stream_name = tmp_stream_name;
213 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
214 SMB_STRUCT_STAT *sbuf)
216 struct smb_filename *smb_fname_base = NULL;
218 struct stream_io *io = (struct stream_io *)
219 VFS_FETCH_FSP_EXTENSION(handle, fsp);
221 if (io == NULL || fsp->base_fsp == NULL) {
222 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
225 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
227 if (!streams_xattr_recheck(io)) {
231 /* Create an smb_filename with stream_name == NULL. */
232 smb_fname_base = synthetic_smb_fname(talloc_tos(),
237 fsp->fsp_name->flags);
238 if (smb_fname_base == NULL) {
243 ret = vfs_stat(handle->conn, smb_fname_base);
244 *sbuf = smb_fname_base->st;
247 TALLOC_FREE(smb_fname_base);
251 sbuf->st_ex_size = get_xattr_size(handle->conn,
255 if (sbuf->st_ex_size == -1) {
256 TALLOC_FREE(smb_fname_base);
257 SET_STAT_INVALID(*sbuf);
261 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
263 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
264 sbuf->st_ex_mode &= ~S_IFMT;
265 sbuf->st_ex_mode &= ~S_IFDIR;
266 sbuf->st_ex_mode |= S_IFREG;
267 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
269 TALLOC_FREE(smb_fname_base);
273 static int streams_xattr_stat(vfs_handle_struct *handle,
274 struct smb_filename *smb_fname)
278 char *xattr_name = NULL;
280 if (!is_named_stream(smb_fname)) {
281 return SMB_VFS_NEXT_STAT(handle, smb_fname);
284 /* Note if lp_posix_paths() is true, we can never
285 * get here as is_named_stream() is
286 * always false. So we never need worry about
287 * not following links here. */
289 /* Populate the stat struct with info from the base file. */
290 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
294 /* Derive the xattr name to lookup. */
295 status = streams_xattr_get_name(handle, talloc_tos(),
296 smb_fname->stream_name, &xattr_name);
297 if (!NT_STATUS_IS_OK(status)) {
298 errno = map_errno_from_nt_status(status);
302 /* Augment the base file's stat information before returning. */
303 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
307 if (smb_fname->st.st_ex_size == -1) {
308 SET_STAT_INVALID(smb_fname->st);
314 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
315 smb_fname->st.st_ex_mode &= ~S_IFMT;
316 smb_fname->st.st_ex_mode &= ~S_IFDIR;
317 smb_fname->st.st_ex_mode |= S_IFREG;
318 smb_fname->st.st_ex_blocks =
319 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
323 TALLOC_FREE(xattr_name);
327 static int streams_xattr_lstat(vfs_handle_struct *handle,
328 struct smb_filename *smb_fname)
332 char *xattr_name = NULL;
334 if (!is_named_stream(smb_fname)) {
335 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
338 /* Populate the stat struct with info from the base file. */
339 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
343 /* Derive the xattr name to lookup. */
344 status = streams_xattr_get_name(handle, talloc_tos(),
345 smb_fname->stream_name, &xattr_name);
346 if (!NT_STATUS_IS_OK(status)) {
347 errno = map_errno_from_nt_status(status);
351 /* Augment the base file's stat information before returning. */
352 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
356 if (smb_fname->st.st_ex_size == -1) {
357 SET_STAT_INVALID(smb_fname->st);
363 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
364 smb_fname->st.st_ex_mode &= ~S_IFMT;
365 smb_fname->st.st_ex_mode |= S_IFREG;
366 smb_fname->st.st_ex_blocks =
367 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
372 TALLOC_FREE(xattr_name);
376 static int streams_xattr_openat(struct vfs_handle_struct *handle,
377 const struct files_struct *dirfsp,
378 const struct smb_filename *smb_fname,
384 struct streams_xattr_config *config = NULL;
385 struct stream_io *sio = NULL;
387 char *xattr_name = NULL;
389 bool set_empty_xattr = false;
392 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
395 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
396 smb_fname_str_dbg(smb_fname), flags));
398 if (!is_named_stream(smb_fname)) {
399 return SMB_VFS_NEXT_OPENAT(handle,
408 * For now assert this, so the below SMB_VFS_SETXATTR() works.
410 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
412 status = streams_xattr_get_name(handle, talloc_tos(),
413 smb_fname->stream_name, &xattr_name);
414 if (!NT_STATUS_IS_OK(status)) {
415 errno = map_errno_from_nt_status(status);
419 status = get_ea_value(talloc_tos(),
426 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
428 if (!NT_STATUS_IS_OK(status)) {
429 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
431 * The base file is not there. This is an error even if
432 * we got O_CREAT, the higher levels should have created
433 * the base file for us.
435 DBG_DEBUG("streams_xattr_open: base file %s not around, "
436 "returning ENOENT\n", smb_fname->base_name);
441 if (!(flags & O_CREAT)) {
446 set_empty_xattr = true;
449 if (flags & O_TRUNC) {
450 set_empty_xattr = true;
453 if (set_empty_xattr) {
455 * The attribute does not exist or needs to be truncated
459 * Darn, xattrs need at least 1 byte
463 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
464 xattr_name, smb_fname->base_name));
466 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
469 flags & O_EXCL ? XATTR_CREATE : 0);
475 fakefd = vfs_fake_fd();
477 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
483 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
485 if (sio->xattr_name == NULL) {
491 * so->base needs to be a copy of fsp->fsp_name->base_name,
492 * making it identical to streams_xattr_recheck(). If the
493 * open is changing directories, fsp->fsp_name->base_name
494 * will be the full path from the share root, whilst
495 * smb_fname will be relative to the $cwd.
497 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
498 fsp->fsp_name->base_name);
499 if (sio->base == NULL) {
504 sio->fsp_name_ptr = fsp->fsp_name;
505 sio->handle = handle;
512 vfs_fake_fd_close(fakefd);
519 static int streams_xattr_close(vfs_handle_struct *handle,
525 fd = fsp_get_pathref_fd(fsp);
527 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
528 smb_fname_str_dbg(fsp->fsp_name), fd);
530 if (!is_named_stream(fsp->fsp_name)) {
531 return SMB_VFS_NEXT_CLOSE(handle, fsp);
534 ret = vfs_fake_fd_close(fd);
540 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
541 struct files_struct *dirfsp,
542 const struct smb_filename *smb_fname,
547 char *xattr_name = NULL;
549 if (!is_named_stream(smb_fname)) {
550 return SMB_VFS_NEXT_UNLINKAT(handle,
556 status = streams_xattr_get_name(handle, talloc_tos(),
557 smb_fname->stream_name, &xattr_name);
558 if (!NT_STATUS_IS_OK(status)) {
559 errno = map_errno_from_nt_status(status);
563 SMB_ASSERT(smb_fname->fsp != NULL);
564 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
566 ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
568 if ((ret == -1) && (errno == ENOATTR)) {
576 TALLOC_FREE(xattr_name);
580 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
581 struct files_struct *dirfsp,
582 const struct smb_filename *smb_fname,
586 if (flags & AT_REMOVEDIR) {
587 ret = SMB_VFS_NEXT_UNLINKAT(handle,
592 ret = streams_xattr_unlink_internal(handle,
600 static int streams_xattr_renameat(vfs_handle_struct *handle,
601 files_struct *srcfsp,
602 const struct smb_filename *smb_fname_src,
603 files_struct *dstfsp,
604 const struct smb_filename *smb_fname_dst)
608 char *src_xattr_name = NULL;
609 char *dst_xattr_name = NULL;
610 bool src_is_stream, dst_is_stream;
614 struct smb_filename *pathref_src = NULL;
615 struct smb_filename *pathref_dst = NULL;
616 struct smb_filename *full_src = NULL;
617 struct smb_filename *full_dst = NULL;
619 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
620 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
622 if (!src_is_stream && !dst_is_stream) {
623 return SMB_VFS_NEXT_RENAMEAT(handle,
630 /* For now don't allow renames from or to the default stream. */
631 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
632 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
637 /* Don't rename if the streams are identical. */
638 if (strcasecmp_m(smb_fname_src->stream_name,
639 smb_fname_dst->stream_name) == 0) {
643 /* Get the xattr names. */
644 status = streams_xattr_get_name(handle, talloc_tos(),
645 smb_fname_src->stream_name,
647 if (!NT_STATUS_IS_OK(status)) {
648 errno = map_errno_from_nt_status(status);
651 status = streams_xattr_get_name(handle, talloc_tos(),
652 smb_fname_dst->stream_name,
654 if (!NT_STATUS_IS_OK(status)) {
655 errno = map_errno_from_nt_status(status);
659 full_src = full_path_from_dirfsp_atname(talloc_tos(),
662 if (full_src == NULL) {
666 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
669 if (full_dst == NULL) {
674 /* Get a pathref for full_src (base file, no stream name). */
675 status = synthetic_pathref(talloc_tos(),
676 handle->conn->cwd_fsp,
683 if (!NT_STATUS_IS_OK(status)) {
688 /* Read the old stream from the base file fsp. */
689 status = get_ea_value(talloc_tos(),
695 if (!NT_STATUS_IS_OK(status)) {
696 errno = map_errno_from_nt_status(status);
700 /* Get a pathref for full_dst (base file, no stream name). */
701 status = synthetic_pathref(talloc_tos(),
702 handle->conn->cwd_fsp,
709 if (!NT_STATUS_IS_OK(status)) {
714 /* (Over)write the new stream on the base file fsp. */
715 nret = SMB_VFS_FSETXATTR(
722 if (errno == ENOATTR) {
729 * Remove the old stream from the base file fsp.
731 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
734 if (errno == ENOATTR) {
744 TALLOC_FREE(pathref_src);
745 TALLOC_FREE(pathref_dst);
746 TALLOC_FREE(full_src);
747 TALLOC_FREE(full_dst);
748 TALLOC_FREE(src_xattr_name);
749 TALLOC_FREE(dst_xattr_name);
753 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
755 const struct smb_filename *smb_fname,
756 bool (*fn)(struct ea_struct *ea,
763 struct streams_xattr_config *config;
765 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
766 return NT_STATUS_UNSUCCESSFUL);
768 status = get_ea_names_from_file(talloc_tos(),
772 if (!NT_STATUS_IS_OK(status)) {
776 for (i=0; i<num_names; i++) {
780 * We want to check with samba_private_attr_name()
781 * whether the xattr name is a private one,
782 * unfortunately it flags xattrs that begin with the
783 * default streams prefix as private.
785 * By only calling samba_private_attr_name() in case
786 * the xattr does NOT begin with the default prefix,
787 * we know that if it returns 'true' it definitely one
788 * of our internal xattr like "user.DOSATTRIB".
790 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
791 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
792 if (samba_private_attr_name(names[i])) {
797 if (strncmp(names[i], config->prefix,
798 config->prefix_len) != 0) {
802 status = get_ea_value(names,
808 if (!NT_STATUS_IS_OK(status)) {
809 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
811 smb_fname->base_name,
816 ea.name = talloc_asprintf(
817 ea.value.data, ":%s%s",
818 names[i] + config->prefix_len,
819 config->store_stream_type ? "" : ":$DATA");
820 if (ea.name == NULL) {
821 DEBUG(0, ("talloc failed\n"));
825 if (!fn(&ea, private_data)) {
826 TALLOC_FREE(ea.value.data);
830 TALLOC_FREE(ea.value.data);
837 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
838 struct stream_struct **streams,
839 const char *name, off_t size,
842 struct stream_struct *tmp;
844 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
850 tmp[*num_streams].name = talloc_strdup(tmp, name);
851 if (tmp[*num_streams].name == NULL) {
855 tmp[*num_streams].size = size;
856 tmp[*num_streams].alloc_size = alloc_size;
863 struct streaminfo_state {
865 vfs_handle_struct *handle;
866 unsigned int num_streams;
867 struct stream_struct *streams;
871 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
873 struct streaminfo_state *state =
874 (struct streaminfo_state *)private_data;
876 if (!add_one_stream(state->mem_ctx,
877 &state->num_streams, &state->streams,
878 ea->name, ea->value.length-1,
879 smb_roundup(state->handle->conn,
880 ea->value.length-1))) {
881 state->status = NT_STATUS_NO_MEMORY;
888 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
889 struct files_struct *fsp,
891 unsigned int *pnum_streams,
892 struct stream_struct **pstreams)
895 struct streaminfo_state state;
897 state.streams = *pstreams;
898 state.num_streams = *pnum_streams;
899 state.mem_ctx = mem_ctx;
900 state.handle = handle;
901 state.status = NT_STATUS_OK;
903 status = walk_xattr_streams(handle,
909 if (!NT_STATUS_IS_OK(status)) {
910 TALLOC_FREE(state.streams);
914 if (!NT_STATUS_IS_OK(state.status)) {
915 TALLOC_FREE(state.streams);
919 *pnum_streams = state.num_streams;
920 *pstreams = state.streams;
922 return SMB_VFS_NEXT_FSTREAMINFO(handle,
929 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
930 enum timestamp_set_resolution *p_ts_res)
932 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
935 static int streams_xattr_connect(vfs_handle_struct *handle,
936 const char *service, const char *user)
938 struct streams_xattr_config *config;
939 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
943 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
948 config = talloc_zero(handle->conn, struct streams_xattr_config);
949 if (config == NULL) {
950 DEBUG(1, ("talloc_zero() failed\n"));
955 prefix = lp_parm_const_string(SNUM(handle->conn),
956 "streams_xattr", "prefix",
958 config->prefix = talloc_strdup(config, prefix);
959 if (config->prefix == NULL) {
960 DEBUG(1, ("talloc_strdup() failed\n"));
964 config->prefix_len = strlen(config->prefix);
965 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
967 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
972 SMB_VFS_HANDLE_SET_DATA(handle, config,
973 NULL, struct stream_xattr_config,
979 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
980 files_struct *fsp, const void *data,
981 size_t n, off_t offset)
983 struct stream_io *sio =
984 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
989 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
992 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
995 if (!streams_xattr_recheck(sio)) {
999 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
1001 * Requested write is beyond what can be read based on
1002 * samba configuration.
1003 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
1004 * entire file to be skipped by File Explorer. VFAT returns
1005 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
1006 * to skip writing metadata, but copy data.
1008 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
1009 "supported extended attribute size. "
1010 "Depending on filesystem type and operating system "
1011 "(OS) specifics, this value may be increased using "
1012 "the value of the parameter: "
1013 "smbd max xattr size = <bytes>. Consult OS and "
1014 "filesystem manpages prior to increasing this limit.\n",
1015 sio->xattr_name, sio->base);
1020 status = get_ea_value(talloc_tos(),
1026 if (!NT_STATUS_IS_OK(status)) {
1030 if ((offset + n) > ea.value.length-1) {
1033 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1037 TALLOC_FREE(ea.value.data);
1041 ea.value.data = tmp;
1042 ea.value.length = offset + n + 1;
1043 ea.value.data[offset+n] = 0;
1046 memcpy(ea.value.data + offset, data, n);
1048 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1053 TALLOC_FREE(ea.value.data);
1062 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1063 files_struct *fsp, void *data,
1064 size_t n, off_t offset)
1066 struct stream_io *sio =
1067 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1068 struct ea_struct ea;
1070 size_t length, overlap;
1072 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1073 (int)offset, (int)n));
1076 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1079 if (!streams_xattr_recheck(sio)) {
1083 status = get_ea_value(talloc_tos(),
1089 if (!NT_STATUS_IS_OK(status)) {
1093 length = ea.value.length-1;
1095 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1098 /* Attempt to read past EOF. */
1099 if (length <= offset) {
1103 overlap = (offset + n) > length ? (length - offset) : n;
1104 memcpy(data, ea.value.data + offset, overlap);
1106 TALLOC_FREE(ea.value.data);
1110 struct streams_xattr_pread_state {
1112 struct vfs_aio_state vfs_aio_state;
1115 static void streams_xattr_pread_done(struct tevent_req *subreq);
1117 static struct tevent_req *streams_xattr_pread_send(
1118 struct vfs_handle_struct *handle,
1119 TALLOC_CTX *mem_ctx,
1120 struct tevent_context *ev,
1121 struct files_struct *fsp,
1123 size_t n, off_t offset)
1125 struct tevent_req *req = NULL;
1126 struct tevent_req *subreq = NULL;
1127 struct streams_xattr_pread_state *state = NULL;
1128 struct stream_io *sio =
1129 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1131 req = tevent_req_create(mem_ctx, &state,
1132 struct streams_xattr_pread_state);
1138 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1140 if (tevent_req_nomem(req, subreq)) {
1141 return tevent_req_post(req, ev);
1143 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1147 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1148 if (state->nread != n) {
1149 if (state->nread != -1) {
1152 tevent_req_error(req, errno);
1153 return tevent_req_post(req, ev);
1156 tevent_req_done(req);
1157 return tevent_req_post(req, ev);
1160 static void streams_xattr_pread_done(struct tevent_req *subreq)
1162 struct tevent_req *req = tevent_req_callback_data(
1163 subreq, struct tevent_req);
1164 struct streams_xattr_pread_state *state = tevent_req_data(
1165 req, struct streams_xattr_pread_state);
1167 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1168 TALLOC_FREE(subreq);
1170 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1173 tevent_req_done(req);
1176 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1177 struct vfs_aio_state *vfs_aio_state)
1179 struct streams_xattr_pread_state *state = tevent_req_data(
1180 req, struct streams_xattr_pread_state);
1182 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1186 *vfs_aio_state = state->vfs_aio_state;
1187 return state->nread;
1190 struct streams_xattr_pwrite_state {
1192 struct vfs_aio_state vfs_aio_state;
1195 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1197 static struct tevent_req *streams_xattr_pwrite_send(
1198 struct vfs_handle_struct *handle,
1199 TALLOC_CTX *mem_ctx,
1200 struct tevent_context *ev,
1201 struct files_struct *fsp,
1203 size_t n, off_t offset)
1205 struct tevent_req *req = NULL;
1206 struct tevent_req *subreq = NULL;
1207 struct streams_xattr_pwrite_state *state = NULL;
1208 struct stream_io *sio =
1209 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1211 req = tevent_req_create(mem_ctx, &state,
1212 struct streams_xattr_pwrite_state);
1218 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1220 if (tevent_req_nomem(req, subreq)) {
1221 return tevent_req_post(req, ev);
1223 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1227 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1228 if (state->nwritten != n) {
1229 if (state->nwritten != -1) {
1232 tevent_req_error(req, errno);
1233 return tevent_req_post(req, ev);
1236 tevent_req_done(req);
1237 return tevent_req_post(req, ev);
1240 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1242 struct tevent_req *req = tevent_req_callback_data(
1243 subreq, struct tevent_req);
1244 struct streams_xattr_pwrite_state *state = tevent_req_data(
1245 req, struct streams_xattr_pwrite_state);
1247 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1248 TALLOC_FREE(subreq);
1250 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1253 tevent_req_done(req);
1256 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1257 struct vfs_aio_state *vfs_aio_state)
1259 struct streams_xattr_pwrite_state *state = tevent_req_data(
1260 req, struct streams_xattr_pwrite_state);
1262 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1266 *vfs_aio_state = state->vfs_aio_state;
1267 return state->nwritten;
1270 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1271 struct files_struct *fsp,
1276 struct ea_struct ea;
1278 struct stream_io *sio =
1279 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1281 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1282 fsp_str_dbg(fsp), (double)offset));
1285 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1288 if (!streams_xattr_recheck(sio)) {
1292 status = get_ea_value(talloc_tos(),
1298 if (!NT_STATUS_IS_OK(status)) {
1302 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1306 TALLOC_FREE(ea.value.data);
1311 /* Did we expand ? */
1312 if (ea.value.length < offset + 1) {
1313 memset(&tmp[ea.value.length], '\0',
1314 offset + 1 - ea.value.length);
1317 ea.value.data = tmp;
1318 ea.value.length = offset + 1;
1319 ea.value.data[offset] = 0;
1321 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1327 TALLOC_FREE(ea.value.data);
1336 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1337 struct files_struct *fsp,
1342 struct stream_io *sio =
1343 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1345 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1347 fsp_str_dbg(fsp), (double)offset, (double)len));
1350 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1353 if (!streams_xattr_recheck(sio)) {
1357 /* Let the pwrite code path handle it. */
1362 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1363 uid_t uid, gid_t gid)
1365 struct stream_io *sio =
1366 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1369 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1375 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1379 struct stream_io *sio =
1380 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1383 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1389 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1390 struct files_struct *fsp,
1395 struct stream_io *sio =
1396 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1399 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1406 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1407 struct files_struct *fsp,
1411 struct stream_io *sio =
1412 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1415 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1422 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1423 struct files_struct *fsp,
1426 struct stream_io *sio =
1427 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1430 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1437 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1438 struct files_struct *fsp,
1444 struct stream_io *sio =
1445 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1448 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1456 struct streams_xattr_fsync_state {
1458 struct vfs_aio_state vfs_aio_state;
1461 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1463 static struct tevent_req *streams_xattr_fsync_send(
1464 struct vfs_handle_struct *handle,
1465 TALLOC_CTX *mem_ctx,
1466 struct tevent_context *ev,
1467 struct files_struct *fsp)
1469 struct tevent_req *req = NULL;
1470 struct tevent_req *subreq = NULL;
1471 struct streams_xattr_fsync_state *state = NULL;
1472 struct stream_io *sio =
1473 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1475 req = tevent_req_create(mem_ctx, &state,
1476 struct streams_xattr_fsync_state);
1482 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1483 if (tevent_req_nomem(req, subreq)) {
1484 return tevent_req_post(req, ev);
1486 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1491 * There's no pathname based sync variant and we don't have access to
1492 * the basefile handle, so we can't do anything here.
1495 tevent_req_done(req);
1496 return tevent_req_post(req, ev);
1499 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1501 struct tevent_req *req = tevent_req_callback_data(
1502 subreq, struct tevent_req);
1503 struct streams_xattr_fsync_state *state = tevent_req_data(
1504 req, struct streams_xattr_fsync_state);
1506 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1507 TALLOC_FREE(subreq);
1508 if (state->ret != 0) {
1509 tevent_req_error(req, errno);
1513 tevent_req_done(req);
1516 static int streams_xattr_fsync_recv(struct tevent_req *req,
1517 struct vfs_aio_state *vfs_aio_state)
1519 struct streams_xattr_fsync_state *state = tevent_req_data(
1520 req, struct streams_xattr_fsync_state);
1522 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1526 *vfs_aio_state = state->vfs_aio_state;
1530 static bool streams_xattr_lock(vfs_handle_struct *handle,
1537 struct stream_io *sio =
1538 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1541 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1547 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1554 struct stream_io *sio =
1555 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1558 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1559 pcount, ptype, ppid);
1566 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1568 uint32_t share_access,
1569 uint32_t access_mask)
1571 struct stream_io *sio =
1572 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1575 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1576 share_access, access_mask);
1582 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1586 struct stream_io *sio =
1587 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1590 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1596 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1598 struct lock_struct *plock)
1600 struct stream_io *sio =
1601 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1604 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1610 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1611 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1612 .connect_fn = streams_xattr_connect,
1613 .openat_fn = streams_xattr_openat,
1614 .close_fn = streams_xattr_close,
1615 .stat_fn = streams_xattr_stat,
1616 .fstat_fn = streams_xattr_fstat,
1617 .lstat_fn = streams_xattr_lstat,
1618 .pread_fn = streams_xattr_pread,
1619 .pwrite_fn = streams_xattr_pwrite,
1620 .pread_send_fn = streams_xattr_pread_send,
1621 .pread_recv_fn = streams_xattr_pread_recv,
1622 .pwrite_send_fn = streams_xattr_pwrite_send,
1623 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1624 .unlinkat_fn = streams_xattr_unlinkat,
1625 .renameat_fn = streams_xattr_renameat,
1626 .ftruncate_fn = streams_xattr_ftruncate,
1627 .fallocate_fn = streams_xattr_fallocate,
1628 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1630 .fsync_send_fn = streams_xattr_fsync_send,
1631 .fsync_recv_fn = streams_xattr_fsync_recv,
1633 .lock_fn = streams_xattr_lock,
1634 .getlock_fn = streams_xattr_getlock,
1635 .kernel_flock_fn = streams_xattr_kernel_flock,
1636 .linux_setlease_fn = streams_xattr_linux_setlease,
1637 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1639 .fchown_fn = streams_xattr_fchown,
1640 .fchmod_fn = streams_xattr_fchmod,
1642 .fgetxattr_fn = streams_xattr_fgetxattr,
1643 .flistxattr_fn = streams_xattr_flistxattr,
1644 .fremovexattr_fn = streams_xattr_fremovexattr,
1645 .fsetxattr_fn = streams_xattr_fsetxattr,
1649 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1651 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1652 &vfs_streams_xattr_fns);