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;
192 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
193 SMB_STRUCT_STAT *sbuf)
196 struct stream_io *io = (struct stream_io *)
197 VFS_FETCH_FSP_EXTENSION(handle, fsp);
199 if (io == NULL || fsp->base_fsp == NULL) {
200 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
203 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
205 if (!streams_xattr_recheck(io)) {
209 ret = SMB_VFS_NEXT_FSTAT(handle, fsp->base_fsp, sbuf);
214 sbuf->st_ex_size = get_xattr_size(handle->conn,
218 if (sbuf->st_ex_size == -1) {
219 SET_STAT_INVALID(*sbuf);
223 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
225 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
226 sbuf->st_ex_mode &= ~S_IFMT;
227 sbuf->st_ex_mode &= ~S_IFDIR;
228 sbuf->st_ex_mode |= S_IFREG;
229 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
234 static int streams_xattr_stat(vfs_handle_struct *handle,
235 struct smb_filename *smb_fname)
239 char *xattr_name = NULL;
240 char *tmp_stream_name = NULL;
241 struct smb_filename *smb_fname_cp = NULL;
242 struct files_struct *fsp = smb_fname->fsp;
244 if (!is_named_stream(smb_fname)) {
245 return SMB_VFS_NEXT_STAT(handle, smb_fname);
248 /* Note if lp_posix_paths() is true, we can never
249 * get here as is_named_stream() is
250 * always false. So we never need worry about
251 * not following links here. */
253 /* Populate the stat struct with info from the base file. */
254 tmp_stream_name = smb_fname->stream_name;
255 smb_fname->stream_name = NULL;
256 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
257 smb_fname->stream_name = tmp_stream_name;
263 /* Derive the xattr name to lookup. */
264 status = streams_xattr_get_name(handle, talloc_tos(),
265 smb_fname->stream_name, &xattr_name);
266 if (!NT_STATUS_IS_OK(status)) {
267 errno = map_errno_from_nt_status(status);
271 /* Augment the base file's stat information before returning. */
274 * openat_pathref_fsp() checks for the same
275 * filetype as the incoming stat info before allowing
276 * the open, so we must ensure it's correct here.
278 smb_fname->st.st_ex_mode &= ~S_IFMT;
279 smb_fname->st.st_ex_mode |= S_IFREG;
282 * openat_pathref_fsp() expects a talloc'ed
283 * smb_filename. stat can be passed a struct
284 * from the stack. Make a talloc'ed copy
285 * so openat_pathref_fsp() can add its
288 smb_fname_cp = cp_smb_filename(talloc_tos(),
290 if (smb_fname_cp == NULL) {
291 TALLOC_FREE(xattr_name);
295 status = openat_pathref_fsp(handle->conn->cwd_fsp,
297 if (!NT_STATUS_IS_OK(status)) {
298 DBG_DEBUG("openat_pathref_fsp for %s failed with %s\n",
299 smb_fname_str_dbg(smb_fname),
301 TALLOC_FREE(xattr_name);
302 TALLOC_FREE(smb_fname_cp);
303 SET_STAT_INVALID(smb_fname->st);
307 fsp = smb_fname_cp->fsp;
310 SMB_ASSERT(fsp->base_fsp != NULL);
311 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
315 if (smb_fname->st.st_ex_size == -1) {
316 TALLOC_FREE(xattr_name);
317 TALLOC_FREE(smb_fname_cp);
318 SET_STAT_INVALID(smb_fname->st);
323 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
324 smb_fname->st.st_ex_mode &= ~S_IFMT;
325 smb_fname->st.st_ex_mode &= ~S_IFDIR;
326 smb_fname->st.st_ex_mode |= S_IFREG;
327 smb_fname->st.st_ex_blocks =
328 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
330 TALLOC_FREE(xattr_name);
331 TALLOC_FREE(smb_fname_cp);
335 static int streams_xattr_lstat(vfs_handle_struct *handle,
336 struct smb_filename *smb_fname)
338 if (is_named_stream(smb_fname)) {
340 * There can never be EA's on a symlink.
341 * Windows will never see a symlink, and
342 * in SMB_FILENAME_POSIX_PATH mode we don't
343 * allow EA's on a symlink.
345 SET_STAT_INVALID(smb_fname->st);
349 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
352 static int streams_xattr_openat(struct vfs_handle_struct *handle,
353 const struct files_struct *dirfsp,
354 const struct smb_filename *smb_fname,
360 struct streams_xattr_config *config = NULL;
361 struct stream_io *sio = NULL;
363 char *xattr_name = NULL;
365 bool set_empty_xattr = false;
368 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
371 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
372 smb_fname_str_dbg(smb_fname), flags));
374 if (!is_named_stream(smb_fname)) {
375 return SMB_VFS_NEXT_OPENAT(handle,
384 * For now assert this, so the below SMB_VFS_SETXATTR() works.
386 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
388 status = streams_xattr_get_name(handle, talloc_tos(),
389 smb_fname->stream_name, &xattr_name);
390 if (!NT_STATUS_IS_OK(status)) {
391 errno = map_errno_from_nt_status(status);
395 status = get_ea_value(talloc_tos(),
402 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
404 if (!NT_STATUS_IS_OK(status)) {
405 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
407 * The base file is not there. This is an error even if
408 * we got O_CREAT, the higher levels should have created
409 * the base file for us.
411 DBG_DEBUG("streams_xattr_open: base file %s not around, "
412 "returning ENOENT\n", smb_fname->base_name);
417 if (!(flags & O_CREAT)) {
422 set_empty_xattr = true;
425 if (flags & O_TRUNC) {
426 set_empty_xattr = true;
429 if (set_empty_xattr) {
431 * The attribute does not exist or needs to be truncated
435 * Darn, xattrs need at least 1 byte
439 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
440 xattr_name, smb_fname->base_name));
442 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
445 flags & O_EXCL ? XATTR_CREATE : 0);
451 fakefd = vfs_fake_fd();
453 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
459 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
461 if (sio->xattr_name == NULL) {
467 * so->base needs to be a copy of fsp->fsp_name->base_name,
468 * making it identical to streams_xattr_recheck(). If the
469 * open is changing directories, fsp->fsp_name->base_name
470 * will be the full path from the share root, whilst
471 * smb_fname will be relative to the $cwd.
473 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
474 fsp->fsp_name->base_name);
475 if (sio->base == NULL) {
480 sio->fsp_name_ptr = fsp->fsp_name;
481 sio->handle = handle;
488 vfs_fake_fd_close(fakefd);
495 static int streams_xattr_close(vfs_handle_struct *handle,
501 fd = fsp_get_pathref_fd(fsp);
503 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
504 smb_fname_str_dbg(fsp->fsp_name), fd);
506 if (!is_named_stream(fsp->fsp_name)) {
507 return SMB_VFS_NEXT_CLOSE(handle, fsp);
510 ret = vfs_fake_fd_close(fd);
516 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
517 struct files_struct *dirfsp,
518 const struct smb_filename *smb_fname,
523 char *xattr_name = NULL;
525 if (!is_named_stream(smb_fname)) {
526 return SMB_VFS_NEXT_UNLINKAT(handle,
532 status = streams_xattr_get_name(handle, talloc_tos(),
533 smb_fname->stream_name, &xattr_name);
534 if (!NT_STATUS_IS_OK(status)) {
535 errno = map_errno_from_nt_status(status);
539 SMB_ASSERT(smb_fname->fsp != NULL);
540 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
542 ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
544 if ((ret == -1) && (errno == ENOATTR)) {
552 TALLOC_FREE(xattr_name);
556 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
557 struct files_struct *dirfsp,
558 const struct smb_filename *smb_fname,
562 if (flags & AT_REMOVEDIR) {
563 ret = SMB_VFS_NEXT_UNLINKAT(handle,
568 ret = streams_xattr_unlink_internal(handle,
576 static int streams_xattr_renameat(vfs_handle_struct *handle,
577 files_struct *srcfsp,
578 const struct smb_filename *smb_fname_src,
579 files_struct *dstfsp,
580 const struct smb_filename *smb_fname_dst)
584 char *src_xattr_name = NULL;
585 char *dst_xattr_name = NULL;
586 bool src_is_stream, dst_is_stream;
590 struct smb_filename *pathref_src = NULL;
591 struct smb_filename *pathref_dst = NULL;
592 struct smb_filename *full_src = NULL;
593 struct smb_filename *full_dst = NULL;
595 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
596 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
598 if (!src_is_stream && !dst_is_stream) {
599 return SMB_VFS_NEXT_RENAMEAT(handle,
606 /* For now don't allow renames from or to the default stream. */
607 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
608 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
613 /* Don't rename if the streams are identical. */
614 if (strcasecmp_m(smb_fname_src->stream_name,
615 smb_fname_dst->stream_name) == 0) {
619 /* Get the xattr names. */
620 status = streams_xattr_get_name(handle, talloc_tos(),
621 smb_fname_src->stream_name,
623 if (!NT_STATUS_IS_OK(status)) {
624 errno = map_errno_from_nt_status(status);
627 status = streams_xattr_get_name(handle, talloc_tos(),
628 smb_fname_dst->stream_name,
630 if (!NT_STATUS_IS_OK(status)) {
631 errno = map_errno_from_nt_status(status);
635 full_src = full_path_from_dirfsp_atname(talloc_tos(),
638 if (full_src == NULL) {
642 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
645 if (full_dst == NULL) {
650 /* Get a pathref for full_src (base file, no stream name). */
651 status = synthetic_pathref(talloc_tos(),
652 handle->conn->cwd_fsp,
659 if (!NT_STATUS_IS_OK(status)) {
664 /* Read the old stream from the base file fsp. */
665 status = get_ea_value(talloc_tos(),
671 if (!NT_STATUS_IS_OK(status)) {
672 errno = map_errno_from_nt_status(status);
676 /* Get a pathref for full_dst (base file, no stream name). */
677 status = synthetic_pathref(talloc_tos(),
678 handle->conn->cwd_fsp,
685 if (!NT_STATUS_IS_OK(status)) {
690 /* (Over)write the new stream on the base file fsp. */
691 nret = SMB_VFS_FSETXATTR(
698 if (errno == ENOATTR) {
705 * Remove the old stream from the base file fsp.
707 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
710 if (errno == ENOATTR) {
720 TALLOC_FREE(pathref_src);
721 TALLOC_FREE(pathref_dst);
722 TALLOC_FREE(full_src);
723 TALLOC_FREE(full_dst);
724 TALLOC_FREE(src_xattr_name);
725 TALLOC_FREE(dst_xattr_name);
729 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
731 const struct smb_filename *smb_fname,
732 bool (*fn)(struct ea_struct *ea,
739 struct streams_xattr_config *config;
741 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
742 return NT_STATUS_UNSUCCESSFUL);
744 status = get_ea_names_from_file(talloc_tos(),
748 if (!NT_STATUS_IS_OK(status)) {
752 for (i=0; i<num_names; i++) {
756 * We want to check with samba_private_attr_name()
757 * whether the xattr name is a private one,
758 * unfortunately it flags xattrs that begin with the
759 * default streams prefix as private.
761 * By only calling samba_private_attr_name() in case
762 * the xattr does NOT begin with the default prefix,
763 * we know that if it returns 'true' it definitely one
764 * of our internal xattr like "user.DOSATTRIB".
766 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
767 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
768 if (samba_private_attr_name(names[i])) {
773 if (strncmp(names[i], config->prefix,
774 config->prefix_len) != 0) {
778 status = get_ea_value(names,
784 if (!NT_STATUS_IS_OK(status)) {
785 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
787 smb_fname->base_name,
792 ea.name = talloc_asprintf(
793 ea.value.data, ":%s%s",
794 names[i] + config->prefix_len,
795 config->store_stream_type ? "" : ":$DATA");
796 if (ea.name == NULL) {
797 DEBUG(0, ("talloc failed\n"));
801 if (!fn(&ea, private_data)) {
802 TALLOC_FREE(ea.value.data);
806 TALLOC_FREE(ea.value.data);
813 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
814 struct stream_struct **streams,
815 const char *name, off_t size,
818 struct stream_struct *tmp;
820 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
826 tmp[*num_streams].name = talloc_strdup(tmp, name);
827 if (tmp[*num_streams].name == NULL) {
831 tmp[*num_streams].size = size;
832 tmp[*num_streams].alloc_size = alloc_size;
839 struct streaminfo_state {
841 vfs_handle_struct *handle;
842 unsigned int num_streams;
843 struct stream_struct *streams;
847 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
849 struct streaminfo_state *state =
850 (struct streaminfo_state *)private_data;
852 if (!add_one_stream(state->mem_ctx,
853 &state->num_streams, &state->streams,
854 ea->name, ea->value.length-1,
855 smb_roundup(state->handle->conn,
856 ea->value.length-1))) {
857 state->status = NT_STATUS_NO_MEMORY;
864 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
865 struct files_struct *fsp,
867 unsigned int *pnum_streams,
868 struct stream_struct **pstreams)
871 struct streaminfo_state state;
873 state.streams = *pstreams;
874 state.num_streams = *pnum_streams;
875 state.mem_ctx = mem_ctx;
876 state.handle = handle;
877 state.status = NT_STATUS_OK;
879 status = walk_xattr_streams(handle,
885 if (!NT_STATUS_IS_OK(status)) {
886 TALLOC_FREE(state.streams);
890 if (!NT_STATUS_IS_OK(state.status)) {
891 TALLOC_FREE(state.streams);
895 *pnum_streams = state.num_streams;
896 *pstreams = state.streams;
898 return SMB_VFS_NEXT_FSTREAMINFO(handle,
905 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
906 enum timestamp_set_resolution *p_ts_res)
908 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
911 static int streams_xattr_connect(vfs_handle_struct *handle,
912 const char *service, const char *user)
914 struct streams_xattr_config *config;
915 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
919 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
924 config = talloc_zero(handle->conn, struct streams_xattr_config);
925 if (config == NULL) {
926 DEBUG(1, ("talloc_zero() failed\n"));
931 prefix = lp_parm_const_string(SNUM(handle->conn),
932 "streams_xattr", "prefix",
934 config->prefix = talloc_strdup(config, prefix);
935 if (config->prefix == NULL) {
936 DEBUG(1, ("talloc_strdup() failed\n"));
940 config->prefix_len = strlen(config->prefix);
941 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
943 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
948 SMB_VFS_HANDLE_SET_DATA(handle, config,
949 NULL, struct stream_xattr_config,
955 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
956 files_struct *fsp, const void *data,
957 size_t n, off_t offset)
959 struct stream_io *sio =
960 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
965 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
968 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
971 if (!streams_xattr_recheck(sio)) {
975 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
977 * Requested write is beyond what can be read based on
978 * samba configuration.
979 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
980 * entire file to be skipped by File Explorer. VFAT returns
981 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
982 * to skip writing metadata, but copy data.
984 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
985 "supported extended attribute size. "
986 "Depending on filesystem type and operating system "
987 "(OS) specifics, this value may be increased using "
988 "the value of the parameter: "
989 "smbd max xattr size = <bytes>. Consult OS and "
990 "filesystem manpages prior to increasing this limit.\n",
991 sio->xattr_name, sio->base);
996 status = get_ea_value(talloc_tos(),
1002 if (!NT_STATUS_IS_OK(status)) {
1006 if ((offset + n) > ea.value.length-1) {
1009 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1013 TALLOC_FREE(ea.value.data);
1017 ea.value.data = tmp;
1018 ea.value.length = offset + n + 1;
1019 ea.value.data[offset+n] = 0;
1022 memcpy(ea.value.data + offset, data, n);
1024 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1029 TALLOC_FREE(ea.value.data);
1038 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1039 files_struct *fsp, void *data,
1040 size_t n, off_t offset)
1042 struct stream_io *sio =
1043 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1044 struct ea_struct ea;
1046 size_t length, overlap;
1048 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1049 (int)offset, (int)n));
1052 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1055 if (!streams_xattr_recheck(sio)) {
1059 status = get_ea_value(talloc_tos(),
1065 if (!NT_STATUS_IS_OK(status)) {
1069 length = ea.value.length-1;
1071 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1074 /* Attempt to read past EOF. */
1075 if (length <= offset) {
1079 overlap = (offset + n) > length ? (length - offset) : n;
1080 memcpy(data, ea.value.data + offset, overlap);
1082 TALLOC_FREE(ea.value.data);
1086 struct streams_xattr_pread_state {
1088 struct vfs_aio_state vfs_aio_state;
1091 static void streams_xattr_pread_done(struct tevent_req *subreq);
1093 static struct tevent_req *streams_xattr_pread_send(
1094 struct vfs_handle_struct *handle,
1095 TALLOC_CTX *mem_ctx,
1096 struct tevent_context *ev,
1097 struct files_struct *fsp,
1099 size_t n, off_t offset)
1101 struct tevent_req *req = NULL;
1102 struct tevent_req *subreq = NULL;
1103 struct streams_xattr_pread_state *state = NULL;
1104 struct stream_io *sio =
1105 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1107 req = tevent_req_create(mem_ctx, &state,
1108 struct streams_xattr_pread_state);
1114 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1116 if (tevent_req_nomem(req, subreq)) {
1117 return tevent_req_post(req, ev);
1119 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1123 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1124 if (state->nread != n) {
1125 if (state->nread != -1) {
1128 tevent_req_error(req, errno);
1129 return tevent_req_post(req, ev);
1132 tevent_req_done(req);
1133 return tevent_req_post(req, ev);
1136 static void streams_xattr_pread_done(struct tevent_req *subreq)
1138 struct tevent_req *req = tevent_req_callback_data(
1139 subreq, struct tevent_req);
1140 struct streams_xattr_pread_state *state = tevent_req_data(
1141 req, struct streams_xattr_pread_state);
1143 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1144 TALLOC_FREE(subreq);
1146 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1149 tevent_req_done(req);
1152 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1153 struct vfs_aio_state *vfs_aio_state)
1155 struct streams_xattr_pread_state *state = tevent_req_data(
1156 req, struct streams_xattr_pread_state);
1158 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1162 *vfs_aio_state = state->vfs_aio_state;
1163 return state->nread;
1166 struct streams_xattr_pwrite_state {
1168 struct vfs_aio_state vfs_aio_state;
1171 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1173 static struct tevent_req *streams_xattr_pwrite_send(
1174 struct vfs_handle_struct *handle,
1175 TALLOC_CTX *mem_ctx,
1176 struct tevent_context *ev,
1177 struct files_struct *fsp,
1179 size_t n, off_t offset)
1181 struct tevent_req *req = NULL;
1182 struct tevent_req *subreq = NULL;
1183 struct streams_xattr_pwrite_state *state = NULL;
1184 struct stream_io *sio =
1185 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1187 req = tevent_req_create(mem_ctx, &state,
1188 struct streams_xattr_pwrite_state);
1194 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1196 if (tevent_req_nomem(req, subreq)) {
1197 return tevent_req_post(req, ev);
1199 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1203 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1204 if (state->nwritten != n) {
1205 if (state->nwritten != -1) {
1208 tevent_req_error(req, errno);
1209 return tevent_req_post(req, ev);
1212 tevent_req_done(req);
1213 return tevent_req_post(req, ev);
1216 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1218 struct tevent_req *req = tevent_req_callback_data(
1219 subreq, struct tevent_req);
1220 struct streams_xattr_pwrite_state *state = tevent_req_data(
1221 req, struct streams_xattr_pwrite_state);
1223 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1224 TALLOC_FREE(subreq);
1226 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1229 tevent_req_done(req);
1232 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1233 struct vfs_aio_state *vfs_aio_state)
1235 struct streams_xattr_pwrite_state *state = tevent_req_data(
1236 req, struct streams_xattr_pwrite_state);
1238 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1242 *vfs_aio_state = state->vfs_aio_state;
1243 return state->nwritten;
1246 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1247 struct files_struct *fsp,
1252 struct ea_struct ea;
1254 struct stream_io *sio =
1255 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1257 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1258 fsp_str_dbg(fsp), (double)offset));
1261 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1264 if (!streams_xattr_recheck(sio)) {
1268 status = get_ea_value(talloc_tos(),
1274 if (!NT_STATUS_IS_OK(status)) {
1278 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1282 TALLOC_FREE(ea.value.data);
1287 /* Did we expand ? */
1288 if (ea.value.length < offset + 1) {
1289 memset(&tmp[ea.value.length], '\0',
1290 offset + 1 - ea.value.length);
1293 ea.value.data = tmp;
1294 ea.value.length = offset + 1;
1295 ea.value.data[offset] = 0;
1297 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1303 TALLOC_FREE(ea.value.data);
1312 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1313 struct files_struct *fsp,
1318 struct stream_io *sio =
1319 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1321 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1323 fsp_str_dbg(fsp), (double)offset, (double)len));
1326 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1329 if (!streams_xattr_recheck(sio)) {
1333 /* Let the pwrite code path handle it. */
1338 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1339 uid_t uid, gid_t gid)
1341 struct stream_io *sio =
1342 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1345 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1351 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1355 struct stream_io *sio =
1356 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1359 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1365 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1366 struct files_struct *fsp,
1371 struct stream_io *sio =
1372 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1375 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1382 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1383 struct files_struct *fsp,
1387 struct stream_io *sio =
1388 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1391 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1398 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1399 struct files_struct *fsp,
1402 struct stream_io *sio =
1403 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1406 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1413 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1414 struct files_struct *fsp,
1420 struct stream_io *sio =
1421 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1424 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1432 struct streams_xattr_fsync_state {
1434 struct vfs_aio_state vfs_aio_state;
1437 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1439 static struct tevent_req *streams_xattr_fsync_send(
1440 struct vfs_handle_struct *handle,
1441 TALLOC_CTX *mem_ctx,
1442 struct tevent_context *ev,
1443 struct files_struct *fsp)
1445 struct tevent_req *req = NULL;
1446 struct tevent_req *subreq = NULL;
1447 struct streams_xattr_fsync_state *state = NULL;
1448 struct stream_io *sio =
1449 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1451 req = tevent_req_create(mem_ctx, &state,
1452 struct streams_xattr_fsync_state);
1458 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1459 if (tevent_req_nomem(req, subreq)) {
1460 return tevent_req_post(req, ev);
1462 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1467 * There's no pathname based sync variant and we don't have access to
1468 * the basefile handle, so we can't do anything here.
1471 tevent_req_done(req);
1472 return tevent_req_post(req, ev);
1475 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1477 struct tevent_req *req = tevent_req_callback_data(
1478 subreq, struct tevent_req);
1479 struct streams_xattr_fsync_state *state = tevent_req_data(
1480 req, struct streams_xattr_fsync_state);
1482 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1483 TALLOC_FREE(subreq);
1484 if (state->ret != 0) {
1485 tevent_req_error(req, errno);
1489 tevent_req_done(req);
1492 static int streams_xattr_fsync_recv(struct tevent_req *req,
1493 struct vfs_aio_state *vfs_aio_state)
1495 struct streams_xattr_fsync_state *state = tevent_req_data(
1496 req, struct streams_xattr_fsync_state);
1498 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1502 *vfs_aio_state = state->vfs_aio_state;
1506 static bool streams_xattr_lock(vfs_handle_struct *handle,
1513 struct stream_io *sio =
1514 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1517 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1523 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1530 struct stream_io *sio =
1531 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1534 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1535 pcount, ptype, ppid);
1542 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1544 uint32_t share_access,
1545 uint32_t access_mask)
1547 struct stream_io *sio =
1548 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1551 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1552 share_access, access_mask);
1558 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1562 struct stream_io *sio =
1563 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1566 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1572 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1574 struct lock_struct *plock)
1576 struct stream_io *sio =
1577 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1580 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1586 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1587 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1588 .connect_fn = streams_xattr_connect,
1589 .openat_fn = streams_xattr_openat,
1590 .close_fn = streams_xattr_close,
1591 .stat_fn = streams_xattr_stat,
1592 .fstat_fn = streams_xattr_fstat,
1593 .lstat_fn = streams_xattr_lstat,
1594 .pread_fn = streams_xattr_pread,
1595 .pwrite_fn = streams_xattr_pwrite,
1596 .pread_send_fn = streams_xattr_pread_send,
1597 .pread_recv_fn = streams_xattr_pread_recv,
1598 .pwrite_send_fn = streams_xattr_pwrite_send,
1599 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1600 .unlinkat_fn = streams_xattr_unlinkat,
1601 .renameat_fn = streams_xattr_renameat,
1602 .ftruncate_fn = streams_xattr_ftruncate,
1603 .fallocate_fn = streams_xattr_fallocate,
1604 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1606 .fsync_send_fn = streams_xattr_fsync_send,
1607 .fsync_recv_fn = streams_xattr_fsync_recv,
1609 .lock_fn = streams_xattr_lock,
1610 .getlock_fn = streams_xattr_getlock,
1611 .kernel_flock_fn = streams_xattr_kernel_flock,
1612 .linux_setlease_fn = streams_xattr_linux_setlease,
1613 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1615 .fchown_fn = streams_xattr_fchown,
1616 .fchmod_fn = streams_xattr_fchmod,
1618 .fgetxattr_fn = streams_xattr_fgetxattr,
1619 .flistxattr_fn = streams_xattr_flistxattr,
1620 .fremovexattr_fn = streams_xattr_fremovexattr,
1621 .fsetxattr_fn = streams_xattr_fsetxattr,
1625 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1627 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1628 &vfs_streams_xattr_fns);