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 const struct smb_filename *smb_fname,
50 const char *xattr_name)
56 status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
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,
79 struct streams_xattr_config *config;
81 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
82 return NT_STATUS_UNSUCCESSFUL);
84 sname = talloc_strdup(ctx, stream_name + 1);
86 return NT_STATUS_NO_MEMORY;
90 * With vfs_fruit option "fruit:encoding = native" we're
91 * already converting stream names that contain illegal NTFS
92 * characters from their on-the-wire Unicode Private Range
93 * encoding to their native ASCII representation.
95 * As as result the name of xattrs storing the streams (via
96 * vfs_streams_xattr) may contain a colon, so we have to use
97 * strrchr_m() instead of strchr_m() for matching the stream
100 * In check_path_syntax() we've already ensured the streamname
101 * we got from the client is valid.
103 stype = strrchr_m(sname, ':');
107 * We only support one stream type: "$DATA"
109 if (strcasecmp_m(stype, ":$DATA") != 0) {
111 return NT_STATUS_INVALID_PARAMETER;
114 /* Split name and type */
118 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
121 config->store_stream_type ? ":$DATA" : "");
122 if (*xattr_name == NULL) {
124 return NT_STATUS_NO_MEMORY;
127 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
134 static bool streams_xattr_recheck(struct stream_io *sio)
137 char *xattr_name = NULL;
139 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
143 if (sio->fsp->fsp_name->stream_name == NULL) {
144 /* how can this happen */
149 status = streams_xattr_get_name(sio->handle, talloc_tos(),
150 sio->fsp->fsp_name->stream_name,
152 if (!NT_STATUS_IS_OK(status)) {
156 TALLOC_FREE(sio->xattr_name);
157 TALLOC_FREE(sio->base);
158 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
160 if (sio->xattr_name == NULL) {
161 DBG_DEBUG("sio->xattr_name==NULL\n");
164 TALLOC_FREE(xattr_name);
166 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
167 sio->fsp->fsp_name->base_name);
168 if (sio->base == NULL) {
169 DBG_DEBUG("sio->base==NULL\n");
173 sio->fsp_name_ptr = sio->fsp->fsp_name;
179 * Helper to stat/lstat the base file of an smb_fname.
181 static int streams_xattr_stat_base(vfs_handle_struct *handle,
182 struct smb_filename *smb_fname,
185 char *tmp_stream_name;
188 tmp_stream_name = smb_fname->stream_name;
189 smb_fname->stream_name = NULL;
191 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
193 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
195 smb_fname->stream_name = tmp_stream_name;
199 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
200 SMB_STRUCT_STAT *sbuf)
202 struct smb_filename *smb_fname_base = NULL;
204 struct stream_io *io = (struct stream_io *)
205 VFS_FETCH_FSP_EXTENSION(handle, fsp);
207 if (io == NULL || fsp->base_fsp == NULL) {
208 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
211 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
213 if (!streams_xattr_recheck(io)) {
217 /* Create an smb_filename with stream_name == NULL. */
218 smb_fname_base = synthetic_smb_fname(talloc_tos(),
223 fsp->fsp_name->flags);
224 if (smb_fname_base == NULL) {
229 ret = vfs_stat(handle->conn, smb_fname_base);
230 *sbuf = smb_fname_base->st;
233 TALLOC_FREE(smb_fname_base);
237 sbuf->st_ex_size = get_xattr_size(handle->conn,
238 smb_fname_base, io->xattr_name);
239 if (sbuf->st_ex_size == -1) {
240 TALLOC_FREE(smb_fname_base);
241 SET_STAT_INVALID(*sbuf);
245 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
247 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
248 sbuf->st_ex_mode &= ~S_IFMT;
249 sbuf->st_ex_mode &= ~S_IFDIR;
250 sbuf->st_ex_mode |= S_IFREG;
251 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
253 TALLOC_FREE(smb_fname_base);
257 static int streams_xattr_stat(vfs_handle_struct *handle,
258 struct smb_filename *smb_fname)
262 char *xattr_name = NULL;
264 if (!is_named_stream(smb_fname)) {
265 return SMB_VFS_NEXT_STAT(handle, smb_fname);
268 /* Note if lp_posix_paths() is true, we can never
269 * get here as is_named_stream() is
270 * always false. So we never need worry about
271 * not following links here. */
273 /* Populate the stat struct with info from the base file. */
274 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
278 /* Derive the xattr name to lookup. */
279 status = streams_xattr_get_name(handle, talloc_tos(),
280 smb_fname->stream_name, &xattr_name);
281 if (!NT_STATUS_IS_OK(status)) {
282 errno = map_errno_from_nt_status(status);
286 /* Augment the base file's stat information before returning. */
287 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
290 if (smb_fname->st.st_ex_size == -1) {
291 SET_STAT_INVALID(smb_fname->st);
297 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
298 smb_fname->st.st_ex_mode &= ~S_IFMT;
299 smb_fname->st.st_ex_mode &= ~S_IFDIR;
300 smb_fname->st.st_ex_mode |= S_IFREG;
301 smb_fname->st.st_ex_blocks =
302 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
306 TALLOC_FREE(xattr_name);
310 static int streams_xattr_lstat(vfs_handle_struct *handle,
311 struct smb_filename *smb_fname)
315 char *xattr_name = NULL;
317 if (!is_named_stream(smb_fname)) {
318 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
321 /* Populate the stat struct with info from the base file. */
322 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
326 /* Derive the xattr name to lookup. */
327 status = streams_xattr_get_name(handle, talloc_tos(),
328 smb_fname->stream_name, &xattr_name);
329 if (!NT_STATUS_IS_OK(status)) {
330 errno = map_errno_from_nt_status(status);
334 /* Augment the base file's stat information before returning. */
335 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
338 if (smb_fname->st.st_ex_size == -1) {
339 SET_STAT_INVALID(smb_fname->st);
345 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
346 smb_fname->st.st_ex_mode &= ~S_IFMT;
347 smb_fname->st.st_ex_mode |= S_IFREG;
348 smb_fname->st.st_ex_blocks =
349 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
354 TALLOC_FREE(xattr_name);
358 static int streams_xattr_openat(struct vfs_handle_struct *handle,
359 const struct files_struct *dirfsp,
360 const struct smb_filename *smb_fname,
366 struct streams_xattr_config *config = NULL;
367 struct stream_io *sio = NULL;
369 char *xattr_name = NULL;
371 bool set_empty_xattr = false;
374 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
377 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
378 smb_fname_str_dbg(smb_fname), flags));
380 if (!is_named_stream(smb_fname)) {
381 return SMB_VFS_NEXT_OPENAT(handle,
390 * For now assert this, so the below SMB_VFS_SETXATTR() works.
392 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
394 status = streams_xattr_get_name(handle, talloc_tos(),
395 smb_fname->stream_name, &xattr_name);
396 if (!NT_STATUS_IS_OK(status)) {
397 errno = map_errno_from_nt_status(status);
401 status = get_ea_value(talloc_tos(), handle->conn, NULL,
402 smb_fname, xattr_name, &ea);
404 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
406 if (!NT_STATUS_IS_OK(status)) {
407 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
409 * The base file is not there. This is an error even if
410 * we got O_CREAT, the higher levels should have created
411 * the base file for us.
413 DBG_DEBUG("streams_xattr_open: base file %s not around, "
414 "returning ENOENT\n", smb_fname->base_name);
419 if (!(flags & O_CREAT)) {
424 set_empty_xattr = true;
427 if (flags & O_TRUNC) {
428 set_empty_xattr = true;
431 if (set_empty_xattr) {
433 * The attribute does not exist or needs to be truncated
437 * Darn, xattrs need at least 1 byte
441 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
442 xattr_name, smb_fname->base_name));
444 ret = SMB_VFS_FSETXATTR(fsp->base_fsp ? fsp->base_fsp : fsp,
447 flags & O_EXCL ? XATTR_CREATE : 0);
453 fakefd = vfs_fake_fd();
455 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
461 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
463 if (sio->xattr_name == NULL) {
469 * so->base needs to be a copy of fsp->fsp_name->base_name,
470 * making it identical to streams_xattr_recheck(). If the
471 * open is changing directories, fsp->fsp_name->base_name
472 * will be the full path from the share root, whilst
473 * smb_fname will be relative to the $cwd.
475 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
476 fsp->fsp_name->base_name);
477 if (sio->base == NULL) {
482 sio->fsp_name_ptr = fsp->fsp_name;
483 sio->handle = handle;
490 vfs_fake_fd_close(fakefd);
497 static int streams_xattr_close(vfs_handle_struct *handle,
503 fd = fsp_get_pathref_fd(fsp);
505 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
506 smb_fname_str_dbg(fsp->fsp_name), fd);
508 if (!is_named_stream(fsp->fsp_name)) {
509 return SMB_VFS_NEXT_CLOSE(handle, fsp);
512 ret = vfs_fake_fd_close(fd);
518 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
519 struct files_struct *dirfsp,
520 const struct smb_filename *smb_fname,
525 char *xattr_name = NULL;
527 if (!is_named_stream(smb_fname)) {
528 return SMB_VFS_NEXT_UNLINKAT(handle,
534 status = streams_xattr_get_name(handle, talloc_tos(),
535 smb_fname->stream_name, &xattr_name);
536 if (!NT_STATUS_IS_OK(status)) {
537 errno = map_errno_from_nt_status(status);
541 SMB_ASSERT(smb_fname->fsp != NULL);
542 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
544 ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
546 if ((ret == -1) && (errno == ENOATTR)) {
554 TALLOC_FREE(xattr_name);
558 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
559 struct files_struct *dirfsp,
560 const struct smb_filename *smb_fname,
564 if (flags & AT_REMOVEDIR) {
565 ret = SMB_VFS_NEXT_UNLINKAT(handle,
570 ret = streams_xattr_unlink_internal(handle,
578 static int streams_xattr_renameat(vfs_handle_struct *handle,
579 files_struct *srcfsp,
580 const struct smb_filename *smb_fname_src,
581 files_struct *dstfsp,
582 const struct smb_filename *smb_fname_dst)
586 char *src_xattr_name = NULL;
587 char *dst_xattr_name = NULL;
588 bool src_is_stream, dst_is_stream;
592 struct smb_filename *pathref_src = NULL;
593 struct smb_filename *pathref_dst = NULL;
594 struct smb_filename *full_src = NULL;
595 struct smb_filename *full_dst = NULL;
597 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
598 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
600 if (!src_is_stream && !dst_is_stream) {
601 return SMB_VFS_NEXT_RENAMEAT(handle,
608 /* For now don't allow renames from or to the default stream. */
609 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
610 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
615 /* Don't rename if the streams are identical. */
616 if (strcasecmp_m(smb_fname_src->stream_name,
617 smb_fname_dst->stream_name) == 0) {
621 /* Get the xattr names. */
622 status = streams_xattr_get_name(handle, talloc_tos(),
623 smb_fname_src->stream_name,
625 if (!NT_STATUS_IS_OK(status)) {
626 errno = map_errno_from_nt_status(status);
629 status = streams_xattr_get_name(handle, talloc_tos(),
630 smb_fname_dst->stream_name,
632 if (!NT_STATUS_IS_OK(status)) {
633 errno = map_errno_from_nt_status(status);
637 full_src = full_path_from_dirfsp_atname(talloc_tos(),
640 if (full_src == NULL) {
644 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
647 if (full_dst == NULL) {
652 /* Get a pathref for full_src (base file, no stream name). */
653 status = synthetic_pathref(talloc_tos(),
654 handle->conn->cwd_fsp,
661 if (!NT_STATUS_IS_OK(status)) {
666 /* Read the old stream from the base file fsp. */
667 status = get_ea_value(talloc_tos(),
673 if (!NT_STATUS_IS_OK(status)) {
674 errno = map_errno_from_nt_status(status);
678 /* Get a pathref for full_dst (base file, no stream name). */
679 status = synthetic_pathref(talloc_tos(),
680 handle->conn->cwd_fsp,
687 if (!NT_STATUS_IS_OK(status)) {
692 /* (Over)write the new stream on the base file fsp. */
693 nret = SMB_VFS_FSETXATTR(
700 if (errno == ENOATTR) {
707 * Remove the old stream from the base file fsp.
709 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
712 if (errno == ENOATTR) {
722 TALLOC_FREE(pathref_src);
723 TALLOC_FREE(pathref_dst);
724 TALLOC_FREE(full_src);
725 TALLOC_FREE(full_dst);
726 TALLOC_FREE(src_xattr_name);
727 TALLOC_FREE(dst_xattr_name);
731 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
733 const struct smb_filename *smb_fname,
734 bool (*fn)(struct ea_struct *ea,
741 struct streams_xattr_config *config;
743 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
744 return NT_STATUS_UNSUCCESSFUL);
746 status = get_ea_names_from_file(talloc_tos(),
750 if (!NT_STATUS_IS_OK(status)) {
754 for (i=0; i<num_names; i++) {
758 * We want to check with samba_private_attr_name()
759 * whether the xattr name is a private one,
760 * unfortunately it flags xattrs that begin with the
761 * default streams prefix as private.
763 * By only calling samba_private_attr_name() in case
764 * the xattr does NOT begin with the default prefix,
765 * we know that if it returns 'true' it definitely one
766 * of our internal xattr like "user.DOSATTRIB".
768 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
769 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
770 if (samba_private_attr_name(names[i])) {
775 if (strncmp(names[i], config->prefix,
776 config->prefix_len) != 0) {
780 status = get_ea_value(names,
786 if (!NT_STATUS_IS_OK(status)) {
787 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
789 smb_fname->base_name,
794 ea.name = talloc_asprintf(
795 ea.value.data, ":%s%s",
796 names[i] + config->prefix_len,
797 config->store_stream_type ? "" : ":$DATA");
798 if (ea.name == NULL) {
799 DEBUG(0, ("talloc failed\n"));
803 if (!fn(&ea, private_data)) {
804 TALLOC_FREE(ea.value.data);
808 TALLOC_FREE(ea.value.data);
815 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
816 struct stream_struct **streams,
817 const char *name, off_t size,
820 struct stream_struct *tmp;
822 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
828 tmp[*num_streams].name = talloc_strdup(tmp, name);
829 if (tmp[*num_streams].name == NULL) {
833 tmp[*num_streams].size = size;
834 tmp[*num_streams].alloc_size = alloc_size;
841 struct streaminfo_state {
843 vfs_handle_struct *handle;
844 unsigned int num_streams;
845 struct stream_struct *streams;
849 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
851 struct streaminfo_state *state =
852 (struct streaminfo_state *)private_data;
854 if (!add_one_stream(state->mem_ctx,
855 &state->num_streams, &state->streams,
856 ea->name, ea->value.length-1,
857 smb_roundup(state->handle->conn,
858 ea->value.length-1))) {
859 state->status = NT_STATUS_NO_MEMORY;
866 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
867 struct files_struct *fsp,
869 unsigned int *pnum_streams,
870 struct stream_struct **pstreams)
873 struct streaminfo_state state;
875 state.streams = *pstreams;
876 state.num_streams = *pnum_streams;
877 state.mem_ctx = mem_ctx;
878 state.handle = handle;
879 state.status = NT_STATUS_OK;
881 status = walk_xattr_streams(handle,
887 if (!NT_STATUS_IS_OK(status)) {
888 TALLOC_FREE(state.streams);
892 if (!NT_STATUS_IS_OK(state.status)) {
893 TALLOC_FREE(state.streams);
897 *pnum_streams = state.num_streams;
898 *pstreams = state.streams;
900 return SMB_VFS_NEXT_FSTREAMINFO(handle,
907 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
908 enum timestamp_set_resolution *p_ts_res)
910 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
913 static int streams_xattr_connect(vfs_handle_struct *handle,
914 const char *service, const char *user)
916 struct streams_xattr_config *config;
917 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
921 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
926 config = talloc_zero(handle->conn, struct streams_xattr_config);
927 if (config == NULL) {
928 DEBUG(1, ("talloc_zero() failed\n"));
933 prefix = lp_parm_const_string(SNUM(handle->conn),
934 "streams_xattr", "prefix",
936 config->prefix = talloc_strdup(config, prefix);
937 if (config->prefix == NULL) {
938 DEBUG(1, ("talloc_strdup() failed\n"));
942 config->prefix_len = strlen(config->prefix);
943 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
945 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
950 SMB_VFS_HANDLE_SET_DATA(handle, config,
951 NULL, struct stream_xattr_config,
957 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
958 files_struct *fsp, const void *data,
959 size_t n, off_t offset)
961 struct stream_io *sio =
962 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
967 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
970 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
973 if (!streams_xattr_recheck(sio)) {
977 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
979 * Requested write is beyond what can be read based on
980 * samba configuration.
981 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
982 * entire file to be skipped by File Explorer. VFAT returns
983 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
984 * to skip writing metadata, but copy data.
986 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
987 "supported extended attribute size. "
988 "Depending on filesystem type and operating system "
989 "(OS) specifics, this value may be increased using "
990 "the value of the parameter: "
991 "smbd max xattr size = <bytes>. Consult OS and "
992 "filesystem manpages prior to increasing this limit.\n",
993 sio->xattr_name, sio->base);
998 status = get_ea_value(talloc_tos(),
1004 if (!NT_STATUS_IS_OK(status)) {
1008 if ((offset + n) > ea.value.length-1) {
1011 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1015 TALLOC_FREE(ea.value.data);
1019 ea.value.data = tmp;
1020 ea.value.length = offset + n + 1;
1021 ea.value.data[offset+n] = 0;
1024 memcpy(ea.value.data + offset, data, n);
1026 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1031 TALLOC_FREE(ea.value.data);
1040 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1041 files_struct *fsp, void *data,
1042 size_t n, off_t offset)
1044 struct stream_io *sio =
1045 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1046 struct ea_struct ea;
1048 size_t length, overlap;
1049 struct smb_filename *smb_fname_base = NULL;
1051 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1052 (int)offset, (int)n));
1055 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1058 if (!streams_xattr_recheck(sio)) {
1062 /* Create an smb_filename with stream_name == NULL. */
1063 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1067 fsp->fsp_name->twrp,
1068 fsp->fsp_name->flags);
1069 if (smb_fname_base == NULL) {
1074 status = get_ea_value(talloc_tos(),
1080 if (!NT_STATUS_IS_OK(status)) {
1084 length = ea.value.length-1;
1086 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1089 /* Attempt to read past EOF. */
1090 if (length <= offset) {
1094 overlap = (offset + n) > length ? (length - offset) : n;
1095 memcpy(data, ea.value.data + offset, overlap);
1097 TALLOC_FREE(ea.value.data);
1101 struct streams_xattr_pread_state {
1103 struct vfs_aio_state vfs_aio_state;
1106 static void streams_xattr_pread_done(struct tevent_req *subreq);
1108 static struct tevent_req *streams_xattr_pread_send(
1109 struct vfs_handle_struct *handle,
1110 TALLOC_CTX *mem_ctx,
1111 struct tevent_context *ev,
1112 struct files_struct *fsp,
1114 size_t n, off_t offset)
1116 struct tevent_req *req = NULL;
1117 struct tevent_req *subreq = NULL;
1118 struct streams_xattr_pread_state *state = NULL;
1119 struct stream_io *sio =
1120 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1122 req = tevent_req_create(mem_ctx, &state,
1123 struct streams_xattr_pread_state);
1129 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1131 if (tevent_req_nomem(req, subreq)) {
1132 return tevent_req_post(req, ev);
1134 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1138 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1139 if (state->nread != n) {
1140 if (state->nread != -1) {
1143 tevent_req_error(req, errno);
1144 return tevent_req_post(req, ev);
1147 tevent_req_done(req);
1148 return tevent_req_post(req, ev);
1151 static void streams_xattr_pread_done(struct tevent_req *subreq)
1153 struct tevent_req *req = tevent_req_callback_data(
1154 subreq, struct tevent_req);
1155 struct streams_xattr_pread_state *state = tevent_req_data(
1156 req, struct streams_xattr_pread_state);
1158 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1159 TALLOC_FREE(subreq);
1161 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1164 tevent_req_done(req);
1167 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1168 struct vfs_aio_state *vfs_aio_state)
1170 struct streams_xattr_pread_state *state = tevent_req_data(
1171 req, struct streams_xattr_pread_state);
1173 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1177 *vfs_aio_state = state->vfs_aio_state;
1178 return state->nread;
1181 struct streams_xattr_pwrite_state {
1183 struct vfs_aio_state vfs_aio_state;
1186 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1188 static struct tevent_req *streams_xattr_pwrite_send(
1189 struct vfs_handle_struct *handle,
1190 TALLOC_CTX *mem_ctx,
1191 struct tevent_context *ev,
1192 struct files_struct *fsp,
1194 size_t n, off_t offset)
1196 struct tevent_req *req = NULL;
1197 struct tevent_req *subreq = NULL;
1198 struct streams_xattr_pwrite_state *state = NULL;
1199 struct stream_io *sio =
1200 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1202 req = tevent_req_create(mem_ctx, &state,
1203 struct streams_xattr_pwrite_state);
1209 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1211 if (tevent_req_nomem(req, subreq)) {
1212 return tevent_req_post(req, ev);
1214 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1218 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1219 if (state->nwritten != n) {
1220 if (state->nwritten != -1) {
1223 tevent_req_error(req, errno);
1224 return tevent_req_post(req, ev);
1227 tevent_req_done(req);
1228 return tevent_req_post(req, ev);
1231 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1233 struct tevent_req *req = tevent_req_callback_data(
1234 subreq, struct tevent_req);
1235 struct streams_xattr_pwrite_state *state = tevent_req_data(
1236 req, struct streams_xattr_pwrite_state);
1238 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1239 TALLOC_FREE(subreq);
1241 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1244 tevent_req_done(req);
1247 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1248 struct vfs_aio_state *vfs_aio_state)
1250 struct streams_xattr_pwrite_state *state = tevent_req_data(
1251 req, struct streams_xattr_pwrite_state);
1253 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1257 *vfs_aio_state = state->vfs_aio_state;
1258 return state->nwritten;
1261 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1262 struct files_struct *fsp,
1267 struct ea_struct ea;
1269 struct stream_io *sio =
1270 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1271 struct smb_filename *smb_fname_base = NULL;
1273 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1274 fsp_str_dbg(fsp), (double)offset));
1277 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1280 if (!streams_xattr_recheck(sio)) {
1284 /* Create an smb_filename with stream_name == NULL. */
1285 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1289 fsp->fsp_name->twrp,
1290 fsp->fsp_name->flags);
1291 if (smb_fname_base == NULL) {
1296 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1297 smb_fname_base, sio->xattr_name, &ea);
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 ? fsp->base_fsp : fsp,
1323 ea.value.data, ea.value.length, 0);
1325 TALLOC_FREE(ea.value.data);
1334 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1335 struct files_struct *fsp,
1340 struct stream_io *sio =
1341 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1343 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1345 fsp_str_dbg(fsp), (double)offset, (double)len));
1348 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1351 if (!streams_xattr_recheck(sio)) {
1355 /* Let the pwrite code path handle it. */
1360 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1361 uid_t uid, gid_t gid)
1363 struct stream_io *sio =
1364 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1367 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1373 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1377 struct stream_io *sio =
1378 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1381 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1387 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1388 struct files_struct *fsp,
1393 struct stream_io *sio =
1394 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1397 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1404 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1405 struct files_struct *fsp,
1409 struct stream_io *sio =
1410 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1413 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1420 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1421 struct files_struct *fsp,
1424 struct stream_io *sio =
1425 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1428 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1435 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1436 struct files_struct *fsp,
1442 struct stream_io *sio =
1443 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1446 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1454 struct streams_xattr_fsync_state {
1456 struct vfs_aio_state vfs_aio_state;
1459 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1461 static struct tevent_req *streams_xattr_fsync_send(
1462 struct vfs_handle_struct *handle,
1463 TALLOC_CTX *mem_ctx,
1464 struct tevent_context *ev,
1465 struct files_struct *fsp)
1467 struct tevent_req *req = NULL;
1468 struct tevent_req *subreq = NULL;
1469 struct streams_xattr_fsync_state *state = NULL;
1470 struct stream_io *sio =
1471 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1473 req = tevent_req_create(mem_ctx, &state,
1474 struct streams_xattr_fsync_state);
1480 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1481 if (tevent_req_nomem(req, subreq)) {
1482 return tevent_req_post(req, ev);
1484 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1489 * There's no pathname based sync variant and we don't have access to
1490 * the basefile handle, so we can't do anything here.
1493 tevent_req_done(req);
1494 return tevent_req_post(req, ev);
1497 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1499 struct tevent_req *req = tevent_req_callback_data(
1500 subreq, struct tevent_req);
1501 struct streams_xattr_fsync_state *state = tevent_req_data(
1502 req, struct streams_xattr_fsync_state);
1504 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1505 TALLOC_FREE(subreq);
1506 if (state->ret != 0) {
1507 tevent_req_error(req, errno);
1511 tevent_req_done(req);
1514 static int streams_xattr_fsync_recv(struct tevent_req *req,
1515 struct vfs_aio_state *vfs_aio_state)
1517 struct streams_xattr_fsync_state *state = tevent_req_data(
1518 req, struct streams_xattr_fsync_state);
1520 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1524 *vfs_aio_state = state->vfs_aio_state;
1528 static bool streams_xattr_lock(vfs_handle_struct *handle,
1535 struct stream_io *sio =
1536 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1539 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1545 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1552 struct stream_io *sio =
1553 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1556 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1557 pcount, ptype, ppid);
1564 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1566 uint32_t share_access,
1567 uint32_t access_mask)
1569 struct stream_io *sio =
1570 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1573 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1574 share_access, access_mask);
1580 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1584 struct stream_io *sio =
1585 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1588 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1594 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1596 struct lock_struct *plock)
1598 struct stream_io *sio =
1599 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1602 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1608 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1609 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1610 .connect_fn = streams_xattr_connect,
1611 .openat_fn = streams_xattr_openat,
1612 .close_fn = streams_xattr_close,
1613 .stat_fn = streams_xattr_stat,
1614 .fstat_fn = streams_xattr_fstat,
1615 .lstat_fn = streams_xattr_lstat,
1616 .pread_fn = streams_xattr_pread,
1617 .pwrite_fn = streams_xattr_pwrite,
1618 .pread_send_fn = streams_xattr_pread_send,
1619 .pread_recv_fn = streams_xattr_pread_recv,
1620 .pwrite_send_fn = streams_xattr_pwrite_send,
1621 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1622 .unlinkat_fn = streams_xattr_unlinkat,
1623 .renameat_fn = streams_xattr_renameat,
1624 .ftruncate_fn = streams_xattr_ftruncate,
1625 .fallocate_fn = streams_xattr_fallocate,
1626 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1628 .fsync_send_fn = streams_xattr_fsync_send,
1629 .fsync_recv_fn = streams_xattr_fsync_recv,
1631 .lock_fn = streams_xattr_lock,
1632 .getlock_fn = streams_xattr_getlock,
1633 .kernel_flock_fn = streams_xattr_kernel_flock,
1634 .linux_setlease_fn = streams_xattr_linux_setlease,
1635 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1637 .fchown_fn = streams_xattr_fchown,
1638 .fchmod_fn = streams_xattr_fchmod,
1640 .fgetxattr_fn = streams_xattr_fgetxattr,
1641 .flistxattr_fn = streams_xattr_flistxattr,
1642 .fremovexattr_fn = streams_xattr_fremovexattr,
1643 .fsetxattr_fn = streams_xattr_fsetxattr,
1647 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1649 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1650 &vfs_streams_xattr_fns);