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_SETXATTR(fsp->conn,
448 flags & O_EXCL ? XATTR_CREATE : 0);
454 fakefd = vfs_fake_fd();
456 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
462 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
464 if (sio->xattr_name == NULL) {
470 * so->base needs to be a copy of fsp->fsp_name->base_name,
471 * making it identical to streams_xattr_recheck(). If the
472 * open is changing directories, fsp->fsp_name->base_name
473 * will be the full path from the share root, whilst
474 * smb_fname will be relative to the $cwd.
476 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
477 fsp->fsp_name->base_name);
478 if (sio->base == NULL) {
483 sio->fsp_name_ptr = fsp->fsp_name;
484 sio->handle = handle;
498 static int streams_xattr_close(vfs_handle_struct *handle,
504 fd = fsp_get_pathref_fd(fsp);
506 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
507 smb_fname_str_dbg(fsp->fsp_name), fd);
509 if (!is_named_stream(fsp->fsp_name)) {
510 return SMB_VFS_NEXT_CLOSE(handle, fsp);
519 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
520 struct files_struct *dirfsp,
521 const struct smb_filename *smb_fname,
526 char *xattr_name = NULL;
528 if (!is_named_stream(smb_fname)) {
529 return SMB_VFS_NEXT_UNLINKAT(handle,
535 status = streams_xattr_get_name(handle, talloc_tos(),
536 smb_fname->stream_name, &xattr_name);
537 if (!NT_STATUS_IS_OK(status)) {
538 errno = map_errno_from_nt_status(status);
542 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, 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;
591 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
592 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
594 if (!src_is_stream && !dst_is_stream) {
595 return SMB_VFS_NEXT_RENAMEAT(handle,
602 /* For now don't allow renames from or to the default stream. */
603 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
604 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
609 /* Don't rename if the streams are identical. */
610 if (strcasecmp_m(smb_fname_src->stream_name,
611 smb_fname_dst->stream_name) == 0) {
615 /* Get the xattr names. */
616 status = streams_xattr_get_name(handle, talloc_tos(),
617 smb_fname_src->stream_name,
619 if (!NT_STATUS_IS_OK(status)) {
620 errno = map_errno_from_nt_status(status);
623 status = streams_xattr_get_name(handle, talloc_tos(),
624 smb_fname_dst->stream_name,
626 if (!NT_STATUS_IS_OK(status)) {
627 errno = map_errno_from_nt_status(status);
631 /* read the old stream */
632 status = get_ea_value(talloc_tos(), handle->conn, NULL,
633 smb_fname_src, src_xattr_name, &ea);
634 if (!NT_STATUS_IS_OK(status)) {
639 /* (over)write the new stream */
640 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
641 dst_xattr_name, ea.value.data, ea.value.length,
644 if (errno == ENOATTR) {
650 /* remove the old stream */
651 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
654 if (errno == ENOATTR) {
664 TALLOC_FREE(src_xattr_name);
665 TALLOC_FREE(dst_xattr_name);
669 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
671 const struct smb_filename *smb_fname,
672 bool (*fn)(struct ea_struct *ea,
679 struct streams_xattr_config *config;
681 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
682 return NT_STATUS_UNSUCCESSFUL);
684 status = get_ea_names_from_file(talloc_tos(),
690 if (!NT_STATUS_IS_OK(status)) {
694 for (i=0; i<num_names; i++) {
698 * We want to check with samba_private_attr_name()
699 * whether the xattr name is a private one,
700 * unfortunately it flags xattrs that begin with the
701 * default streams prefix as private.
703 * By only calling samba_private_attr_name() in case
704 * the xattr does NOT begin with the default prefix,
705 * we know that if it returns 'true' it definitely one
706 * of our internal xattr like "user.DOSATTRIB".
708 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
709 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
710 if (samba_private_attr_name(names[i])) {
715 if (strncmp(names[i], config->prefix,
716 config->prefix_len) != 0) {
720 status = get_ea_value(names,
726 if (!NT_STATUS_IS_OK(status)) {
727 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
729 smb_fname->base_name,
734 ea.name = talloc_asprintf(
735 ea.value.data, ":%s%s",
736 names[i] + config->prefix_len,
737 config->store_stream_type ? "" : ":$DATA");
738 if (ea.name == NULL) {
739 DEBUG(0, ("talloc failed\n"));
743 if (!fn(&ea, private_data)) {
744 TALLOC_FREE(ea.value.data);
748 TALLOC_FREE(ea.value.data);
755 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
756 struct stream_struct **streams,
757 const char *name, off_t size,
760 struct stream_struct *tmp;
762 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
768 tmp[*num_streams].name = talloc_strdup(tmp, name);
769 if (tmp[*num_streams].name == NULL) {
773 tmp[*num_streams].size = size;
774 tmp[*num_streams].alloc_size = alloc_size;
781 struct streaminfo_state {
783 vfs_handle_struct *handle;
784 unsigned int num_streams;
785 struct stream_struct *streams;
789 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
791 struct streaminfo_state *state =
792 (struct streaminfo_state *)private_data;
794 if (!add_one_stream(state->mem_ctx,
795 &state->num_streams, &state->streams,
796 ea->name, ea->value.length-1,
797 smb_roundup(state->handle->conn,
798 ea->value.length-1))) {
799 state->status = NT_STATUS_NO_MEMORY;
806 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
807 struct files_struct *fsp,
808 const struct smb_filename *smb_fname,
810 unsigned int *pnum_streams,
811 struct stream_struct **pstreams)
813 SMB_STRUCT_STAT sbuf;
816 struct streaminfo_state state;
818 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
820 return map_nt_error_from_unix(errno);
823 state.streams = *pstreams;
824 state.num_streams = *pnum_streams;
825 state.mem_ctx = mem_ctx;
826 state.handle = handle;
827 state.status = NT_STATUS_OK;
829 if (S_ISLNK(sbuf.st_ex_mode)) {
831 * Currently we do't have SMB_VFS_LLISTXATTR
832 * inside the VFS which means there's no way
833 * to cope with a symlink when lp_posix_pathnames().
834 * returns true. For now ignore links.
835 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
837 status = NT_STATUS_OK;
839 status = walk_xattr_streams(handle, fsp, smb_fname,
840 collect_one_stream, &state);
843 if (!NT_STATUS_IS_OK(status)) {
844 TALLOC_FREE(state.streams);
848 if (!NT_STATUS_IS_OK(state.status)) {
849 TALLOC_FREE(state.streams);
853 *pnum_streams = state.num_streams;
854 *pstreams = state.streams;
856 return SMB_VFS_NEXT_STREAMINFO(handle,
864 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
865 enum timestamp_set_resolution *p_ts_res)
867 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
870 static int streams_xattr_connect(vfs_handle_struct *handle,
871 const char *service, const char *user)
873 struct streams_xattr_config *config;
874 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
878 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
883 config = talloc_zero(handle->conn, struct streams_xattr_config);
884 if (config == NULL) {
885 DEBUG(1, ("talloc_zero() failed\n"));
890 prefix = lp_parm_const_string(SNUM(handle->conn),
891 "streams_xattr", "prefix",
893 config->prefix = talloc_strdup(config, prefix);
894 if (config->prefix == NULL) {
895 DEBUG(1, ("talloc_strdup() failed\n"));
899 config->prefix_len = strlen(config->prefix);
900 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
902 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
907 SMB_VFS_HANDLE_SET_DATA(handle, config,
908 NULL, struct stream_xattr_config,
914 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
915 files_struct *fsp, const void *data,
916 size_t n, off_t offset)
918 struct stream_io *sio =
919 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
922 struct smb_filename *smb_fname_base = NULL;
925 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
928 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
931 if (!streams_xattr_recheck(sio)) {
935 /* Create an smb_filename with stream_name == NULL. */
936 smb_fname_base = synthetic_smb_fname(talloc_tos(),
941 fsp->fsp_name->flags);
942 if (smb_fname_base == NULL) {
947 status = get_ea_value(talloc_tos(), handle->conn, NULL,
948 smb_fname_base, sio->xattr_name, &ea);
949 if (!NT_STATUS_IS_OK(status)) {
953 if ((offset + n) > ea.value.length-1) {
956 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
960 TALLOC_FREE(ea.value.data);
965 ea.value.length = offset + n + 1;
966 ea.value.data[offset+n] = 0;
969 memcpy(ea.value.data + offset, data, n);
971 ret = SMB_VFS_SETXATTR(fsp->conn,
974 ea.value.data, ea.value.length, 0);
975 TALLOC_FREE(ea.value.data);
984 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
985 files_struct *fsp, void *data,
986 size_t n, off_t offset)
988 struct stream_io *sio =
989 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
992 size_t length, overlap;
993 struct smb_filename *smb_fname_base = NULL;
995 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
996 (int)offset, (int)n));
999 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1002 if (!streams_xattr_recheck(sio)) {
1006 /* Create an smb_filename with stream_name == NULL. */
1007 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1011 fsp->fsp_name->twrp,
1012 fsp->fsp_name->flags);
1013 if (smb_fname_base == NULL) {
1018 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1019 smb_fname_base, sio->xattr_name, &ea);
1020 if (!NT_STATUS_IS_OK(status)) {
1024 length = ea.value.length-1;
1026 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1029 /* Attempt to read past EOF. */
1030 if (length <= offset) {
1034 overlap = (offset + n) > length ? (length - offset) : n;
1035 memcpy(data, ea.value.data + offset, overlap);
1037 TALLOC_FREE(ea.value.data);
1041 struct streams_xattr_pread_state {
1043 struct vfs_aio_state vfs_aio_state;
1046 static void streams_xattr_pread_done(struct tevent_req *subreq);
1048 static struct tevent_req *streams_xattr_pread_send(
1049 struct vfs_handle_struct *handle,
1050 TALLOC_CTX *mem_ctx,
1051 struct tevent_context *ev,
1052 struct files_struct *fsp,
1054 size_t n, off_t offset)
1056 struct tevent_req *req = NULL;
1057 struct tevent_req *subreq = NULL;
1058 struct streams_xattr_pread_state *state = NULL;
1059 struct stream_io *sio =
1060 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1062 req = tevent_req_create(mem_ctx, &state,
1063 struct streams_xattr_pread_state);
1069 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1071 if (tevent_req_nomem(req, subreq)) {
1072 return tevent_req_post(req, ev);
1074 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1078 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1079 if (state->nread != n) {
1080 if (state->nread != -1) {
1083 tevent_req_error(req, errno);
1084 return tevent_req_post(req, ev);
1087 tevent_req_done(req);
1088 return tevent_req_post(req, ev);
1091 static void streams_xattr_pread_done(struct tevent_req *subreq)
1093 struct tevent_req *req = tevent_req_callback_data(
1094 subreq, struct tevent_req);
1095 struct streams_xattr_pread_state *state = tevent_req_data(
1096 req, struct streams_xattr_pread_state);
1098 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1099 TALLOC_FREE(subreq);
1101 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1104 tevent_req_done(req);
1107 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1108 struct vfs_aio_state *vfs_aio_state)
1110 struct streams_xattr_pread_state *state = tevent_req_data(
1111 req, struct streams_xattr_pread_state);
1113 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1117 *vfs_aio_state = state->vfs_aio_state;
1118 return state->nread;
1121 struct streams_xattr_pwrite_state {
1123 struct vfs_aio_state vfs_aio_state;
1126 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1128 static struct tevent_req *streams_xattr_pwrite_send(
1129 struct vfs_handle_struct *handle,
1130 TALLOC_CTX *mem_ctx,
1131 struct tevent_context *ev,
1132 struct files_struct *fsp,
1134 size_t n, off_t offset)
1136 struct tevent_req *req = NULL;
1137 struct tevent_req *subreq = NULL;
1138 struct streams_xattr_pwrite_state *state = NULL;
1139 struct stream_io *sio =
1140 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1142 req = tevent_req_create(mem_ctx, &state,
1143 struct streams_xattr_pwrite_state);
1149 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1151 if (tevent_req_nomem(req, subreq)) {
1152 return tevent_req_post(req, ev);
1154 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1158 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1159 if (state->nwritten != n) {
1160 if (state->nwritten != -1) {
1163 tevent_req_error(req, errno);
1164 return tevent_req_post(req, ev);
1167 tevent_req_done(req);
1168 return tevent_req_post(req, ev);
1171 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1173 struct tevent_req *req = tevent_req_callback_data(
1174 subreq, struct tevent_req);
1175 struct streams_xattr_pwrite_state *state = tevent_req_data(
1176 req, struct streams_xattr_pwrite_state);
1178 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1179 TALLOC_FREE(subreq);
1181 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1184 tevent_req_done(req);
1187 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1188 struct vfs_aio_state *vfs_aio_state)
1190 struct streams_xattr_pwrite_state *state = tevent_req_data(
1191 req, struct streams_xattr_pwrite_state);
1193 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1197 *vfs_aio_state = state->vfs_aio_state;
1198 return state->nwritten;
1201 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1202 struct files_struct *fsp,
1207 struct ea_struct ea;
1209 struct stream_io *sio =
1210 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1211 struct smb_filename *smb_fname_base = NULL;
1213 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1214 fsp_str_dbg(fsp), (double)offset));
1217 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1220 if (!streams_xattr_recheck(sio)) {
1224 /* Create an smb_filename with stream_name == NULL. */
1225 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1229 fsp->fsp_name->twrp,
1230 fsp->fsp_name->flags);
1231 if (smb_fname_base == NULL) {
1236 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1237 smb_fname_base, sio->xattr_name, &ea);
1238 if (!NT_STATUS_IS_OK(status)) {
1242 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1246 TALLOC_FREE(ea.value.data);
1251 /* Did we expand ? */
1252 if (ea.value.length < offset + 1) {
1253 memset(&tmp[ea.value.length], '\0',
1254 offset + 1 - ea.value.length);
1257 ea.value.data = tmp;
1258 ea.value.length = offset + 1;
1259 ea.value.data[offset] = 0;
1261 ret = SMB_VFS_SETXATTR(fsp->conn,
1264 ea.value.data, ea.value.length, 0);
1265 TALLOC_FREE(ea.value.data);
1274 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1275 struct files_struct *fsp,
1280 struct stream_io *sio =
1281 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1283 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1285 fsp_str_dbg(fsp), (double)offset, (double)len));
1288 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1291 if (!streams_xattr_recheck(sio)) {
1295 /* Let the pwrite code path handle it. */
1300 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1301 uid_t uid, gid_t gid)
1303 struct stream_io *sio =
1304 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1307 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1313 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1317 struct stream_io *sio =
1318 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1321 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1327 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1328 struct files_struct *fsp,
1333 struct stream_io *sio =
1334 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1337 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1344 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1345 struct files_struct *fsp,
1349 struct stream_io *sio =
1350 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1353 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1360 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1361 struct files_struct *fsp,
1364 struct stream_io *sio =
1365 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1368 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1375 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1376 struct files_struct *fsp,
1382 struct stream_io *sio =
1383 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1386 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1394 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1396 TALLOC_CTX *mem_ctx)
1398 struct stream_io *sio =
1399 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1402 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1405 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1406 handle, fsp->base_fsp->fsp_name,
1407 SMB_ACL_TYPE_ACCESS, mem_ctx);
1410 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1414 struct stream_io *sio =
1415 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1418 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1424 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1426 TALLOC_CTX *mem_ctx,
1427 char **blob_description,
1430 struct stream_io *sio =
1431 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1434 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1435 blob_description, blob);
1438 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1439 handle, fsp->base_fsp->fsp_name, mem_ctx,
1440 blob_description, blob);
1443 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1445 uint32_t security_info,
1446 TALLOC_CTX *mem_ctx,
1447 struct security_descriptor **ppdesc)
1449 struct stream_io *sio =
1450 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1453 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1457 return SMB_VFS_NEXT_GET_NT_ACL_AT(handle,
1458 handle->conn->cwd_fsp,
1459 fsp->base_fsp->fsp_name,
1465 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1467 uint32_t security_info_sent,
1468 const struct security_descriptor *psd)
1470 struct stream_io *sio =
1471 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1474 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1475 security_info_sent, psd);
1478 return NT_STATUS_OK;
1481 struct streams_xattr_fsync_state {
1483 struct vfs_aio_state vfs_aio_state;
1486 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1488 static struct tevent_req *streams_xattr_fsync_send(
1489 struct vfs_handle_struct *handle,
1490 TALLOC_CTX *mem_ctx,
1491 struct tevent_context *ev,
1492 struct files_struct *fsp)
1494 struct tevent_req *req = NULL;
1495 struct tevent_req *subreq = NULL;
1496 struct streams_xattr_fsync_state *state = NULL;
1497 struct stream_io *sio =
1498 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1500 req = tevent_req_create(mem_ctx, &state,
1501 struct streams_xattr_fsync_state);
1507 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1508 if (tevent_req_nomem(req, subreq)) {
1509 return tevent_req_post(req, ev);
1511 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1516 * There's no pathname based sync variant and we don't have access to
1517 * the basefile handle, so we can't do anything here.
1520 tevent_req_done(req);
1521 return tevent_req_post(req, ev);
1524 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1526 struct tevent_req *req = tevent_req_callback_data(
1527 subreq, struct tevent_req);
1528 struct streams_xattr_fsync_state *state = tevent_req_data(
1529 req, struct streams_xattr_fsync_state);
1531 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1532 TALLOC_FREE(subreq);
1533 if (state->ret != 0) {
1534 tevent_req_error(req, errno);
1538 tevent_req_done(req);
1541 static int streams_xattr_fsync_recv(struct tevent_req *req,
1542 struct vfs_aio_state *vfs_aio_state)
1544 struct streams_xattr_fsync_state *state = tevent_req_data(
1545 req, struct streams_xattr_fsync_state);
1547 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1551 *vfs_aio_state = state->vfs_aio_state;
1555 static bool streams_xattr_lock(vfs_handle_struct *handle,
1562 struct stream_io *sio =
1563 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1566 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1572 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1579 struct stream_io *sio =
1580 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1583 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1584 pcount, ptype, ppid);
1591 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1593 uint32_t share_access,
1594 uint32_t access_mask)
1596 struct stream_io *sio =
1597 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1600 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1601 share_access, access_mask);
1607 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1611 struct stream_io *sio =
1612 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1615 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1621 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1623 struct lock_struct *plock)
1625 struct stream_io *sio =
1626 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1629 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1635 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1636 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1637 .connect_fn = streams_xattr_connect,
1638 .openat_fn = streams_xattr_openat,
1639 .close_fn = streams_xattr_close,
1640 .stat_fn = streams_xattr_stat,
1641 .fstat_fn = streams_xattr_fstat,
1642 .lstat_fn = streams_xattr_lstat,
1643 .pread_fn = streams_xattr_pread,
1644 .pwrite_fn = streams_xattr_pwrite,
1645 .pread_send_fn = streams_xattr_pread_send,
1646 .pread_recv_fn = streams_xattr_pread_recv,
1647 .pwrite_send_fn = streams_xattr_pwrite_send,
1648 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1649 .unlinkat_fn = streams_xattr_unlinkat,
1650 .renameat_fn = streams_xattr_renameat,
1651 .ftruncate_fn = streams_xattr_ftruncate,
1652 .fallocate_fn = streams_xattr_fallocate,
1653 .streaminfo_fn = streams_xattr_streaminfo,
1655 .fsync_send_fn = streams_xattr_fsync_send,
1656 .fsync_recv_fn = streams_xattr_fsync_recv,
1658 .lock_fn = streams_xattr_lock,
1659 .getlock_fn = streams_xattr_getlock,
1660 .kernel_flock_fn = streams_xattr_kernel_flock,
1661 .linux_setlease_fn = streams_xattr_linux_setlease,
1662 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1664 .fchown_fn = streams_xattr_fchown,
1665 .fchmod_fn = streams_xattr_fchmod,
1667 .fgetxattr_fn = streams_xattr_fgetxattr,
1668 .flistxattr_fn = streams_xattr_flistxattr,
1669 .fremovexattr_fn = streams_xattr_fremovexattr,
1670 .fsetxattr_fn = streams_xattr_fsetxattr,
1672 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1673 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1674 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1676 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1677 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1681 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1683 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1684 &vfs_streams_xattr_fns);