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 if (smb_fname_base
->flags
& SMB_FILENAME_POSIX_PATH
) {
230 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
232 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
234 *sbuf
= smb_fname_base
->st
;
237 TALLOC_FREE(smb_fname_base
);
241 sbuf
->st_ex_size
= get_xattr_size(handle
->conn
,
242 smb_fname_base
, io
->xattr_name
);
243 if (sbuf
->st_ex_size
== -1) {
244 TALLOC_FREE(smb_fname_base
);
245 SET_STAT_INVALID(*sbuf
);
249 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf
->st_ex_size
));
251 sbuf
->st_ex_ino
= hash_inode(sbuf
, io
->xattr_name
);
252 sbuf
->st_ex_mode
&= ~S_IFMT
;
253 sbuf
->st_ex_mode
&= ~S_IFDIR
;
254 sbuf
->st_ex_mode
|= S_IFREG
;
255 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
257 TALLOC_FREE(smb_fname_base
);
261 static int streams_xattr_stat(vfs_handle_struct
*handle
,
262 struct smb_filename
*smb_fname
)
266 char *xattr_name
= NULL
;
268 if (!is_named_stream(smb_fname
)) {
269 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
272 /* Note if lp_posix_paths() is true, we can never
273 * get here as is_named_stream() is
274 * always false. So we never need worry about
275 * not following links here. */
277 /* Populate the stat struct with info from the base file. */
278 if (streams_xattr_stat_base(handle
, smb_fname
, true) == -1) {
282 /* Derive the xattr name to lookup. */
283 status
= streams_xattr_get_name(handle
, talloc_tos(),
284 smb_fname
->stream_name
, &xattr_name
);
285 if (!NT_STATUS_IS_OK(status
)) {
286 errno
= map_errno_from_nt_status(status
);
290 /* Augment the base file's stat information before returning. */
291 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
,
294 if (smb_fname
->st
.st_ex_size
== -1) {
295 SET_STAT_INVALID(smb_fname
->st
);
301 smb_fname
->st
.st_ex_ino
= hash_inode(&smb_fname
->st
, xattr_name
);
302 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
303 smb_fname
->st
.st_ex_mode
&= ~S_IFDIR
;
304 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
305 smb_fname
->st
.st_ex_blocks
=
306 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
310 TALLOC_FREE(xattr_name
);
314 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
315 struct smb_filename
*smb_fname
)
319 char *xattr_name
= NULL
;
321 if (!is_named_stream(smb_fname
)) {
322 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
325 /* Populate the stat struct with info from the base file. */
326 if (streams_xattr_stat_base(handle
, smb_fname
, false) == -1) {
330 /* Derive the xattr name to lookup. */
331 status
= streams_xattr_get_name(handle
, talloc_tos(),
332 smb_fname
->stream_name
, &xattr_name
);
333 if (!NT_STATUS_IS_OK(status
)) {
334 errno
= map_errno_from_nt_status(status
);
338 /* Augment the base file's stat information before returning. */
339 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
,
342 if (smb_fname
->st
.st_ex_size
== -1) {
343 SET_STAT_INVALID(smb_fname
->st
);
349 smb_fname
->st
.st_ex_ino
= hash_inode(&smb_fname
->st
, xattr_name
);
350 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
351 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
352 smb_fname
->st
.st_ex_blocks
=
353 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
358 TALLOC_FREE(xattr_name
);
362 static int streams_xattr_openat(struct vfs_handle_struct
*handle
,
363 const struct files_struct
*dirfsp
,
364 const struct smb_filename
*smb_fname
,
370 struct streams_xattr_config
*config
= NULL
;
371 struct stream_io
*sio
= NULL
;
373 char *xattr_name
= NULL
;
376 bool set_empty_xattr
= false;
380 * For now assert this, so the below SMB_VFS_SETXATTR() works.
382 SMB_ASSERT(dirfsp
->fh
->fd
== AT_FDCWD
);
384 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
387 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
388 smb_fname_str_dbg(smb_fname
), flags
));
390 if (!is_named_stream(smb_fname
)) {
391 return SMB_VFS_NEXT_OPENAT(handle
,
399 status
= streams_xattr_get_name(handle
, talloc_tos(),
400 smb_fname
->stream_name
, &xattr_name
);
401 if (!NT_STATUS_IS_OK(status
)) {
402 errno
= map_errno_from_nt_status(status
);
406 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
407 smb_fname
, xattr_name
, &ea
);
409 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
411 if (!NT_STATUS_IS_OK(status
)) {
412 if (!NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
414 * The base file is not there. This is an error even if
415 * we got O_CREAT, the higher levels should have created
416 * the base file for us.
418 DBG_DEBUG("streams_xattr_open: base file %s not around, "
419 "returning ENOENT\n", smb_fname
->base_name
);
424 if (!(flags
& O_CREAT
)) {
429 set_empty_xattr
= true;
432 if (flags
& O_TRUNC
) {
433 set_empty_xattr
= true;
436 if (set_empty_xattr
) {
438 * The attribute does not exist or needs to be truncated
442 * Darn, xattrs need at least 1 byte
446 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
447 xattr_name
, smb_fname
->base_name
));
449 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
453 flags
& O_EXCL
? XATTR_CREATE
: 0);
460 * Return a valid fd, but ensure any attempt to use it returns an error
463 ret
= pipe(pipe_fds
);
470 fakefd
= pipe_fds
[0];
472 sio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct stream_io
, NULL
);
478 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
480 if (sio
->xattr_name
== NULL
) {
486 * so->base needs to be a copy of fsp->fsp_name->base_name,
487 * making it identical to streams_xattr_recheck(). If the
488 * open is changing directories, fsp->fsp_name->base_name
489 * will be the full path from the share root, whilst
490 * smb_fname will be relative to the $cwd.
492 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
493 fsp
->fsp_name
->base_name
);
494 if (sio
->base
== NULL
) {
499 sio
->fsp_name_ptr
= fsp
->fsp_name
;
500 sio
->handle
= handle
;
514 static int streams_xattr_close(vfs_handle_struct
*handle
,
522 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
523 smb_fname_str_dbg(fsp
->fsp_name
), fd
);
525 if (!is_named_stream(fsp
->fsp_name
)) {
526 return SMB_VFS_NEXT_CLOSE(handle
, fsp
);
535 static int streams_xattr_unlink_internal(vfs_handle_struct
*handle
,
536 struct files_struct
*dirfsp
,
537 const struct smb_filename
*smb_fname
,
542 char *xattr_name
= NULL
;
544 if (!is_named_stream(smb_fname
)) {
545 return SMB_VFS_NEXT_UNLINKAT(handle
,
551 status
= streams_xattr_get_name(handle
, talloc_tos(),
552 smb_fname
->stream_name
, &xattr_name
);
553 if (!NT_STATUS_IS_OK(status
)) {
554 errno
= map_errno_from_nt_status(status
);
558 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
, xattr_name
);
560 if ((ret
== -1) && (errno
== ENOATTR
)) {
568 TALLOC_FREE(xattr_name
);
572 static int streams_xattr_unlinkat(vfs_handle_struct
*handle
,
573 struct files_struct
*dirfsp
,
574 const struct smb_filename
*smb_fname
,
578 if (flags
& AT_REMOVEDIR
) {
579 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
584 ret
= streams_xattr_unlink_internal(handle
,
592 static int streams_xattr_renameat(vfs_handle_struct
*handle
,
593 files_struct
*srcfsp
,
594 const struct smb_filename
*smb_fname_src
,
595 files_struct
*dstfsp
,
596 const struct smb_filename
*smb_fname_dst
)
600 char *src_xattr_name
= NULL
;
601 char *dst_xattr_name
= NULL
;
602 bool src_is_stream
, dst_is_stream
;
607 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
608 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
610 if (!src_is_stream
&& !dst_is_stream
) {
611 return SMB_VFS_NEXT_RENAMEAT(handle
,
618 /* For now don't allow renames from or to the default stream. */
619 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
620 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
625 /* Don't rename if the streams are identical. */
626 if (strcasecmp_m(smb_fname_src
->stream_name
,
627 smb_fname_dst
->stream_name
) == 0) {
631 /* Get the xattr names. */
632 status
= streams_xattr_get_name(handle
, talloc_tos(),
633 smb_fname_src
->stream_name
,
635 if (!NT_STATUS_IS_OK(status
)) {
636 errno
= map_errno_from_nt_status(status
);
639 status
= streams_xattr_get_name(handle
, talloc_tos(),
640 smb_fname_dst
->stream_name
,
642 if (!NT_STATUS_IS_OK(status
)) {
643 errno
= map_errno_from_nt_status(status
);
647 /* read the old stream */
648 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
649 smb_fname_src
, src_xattr_name
, &ea
);
650 if (!NT_STATUS_IS_OK(status
)) {
655 /* (over)write the new stream */
656 nret
= SMB_VFS_SETXATTR(handle
->conn
, smb_fname_src
,
657 dst_xattr_name
, ea
.value
.data
, ea
.value
.length
,
660 if (errno
== ENOATTR
) {
666 /* remove the old stream */
667 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
,
670 if (errno
== ENOATTR
) {
680 TALLOC_FREE(src_xattr_name
);
681 TALLOC_FREE(dst_xattr_name
);
685 static NTSTATUS
walk_xattr_streams(vfs_handle_struct
*handle
,
687 const struct smb_filename
*smb_fname
,
688 bool (*fn
)(struct ea_struct
*ea
,
695 struct streams_xattr_config
*config
;
697 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
698 return NT_STATUS_UNSUCCESSFUL
);
700 status
= get_ea_names_from_file(talloc_tos(),
706 if (!NT_STATUS_IS_OK(status
)) {
710 for (i
=0; i
<num_names
; i
++) {
714 * We want to check with samba_private_attr_name()
715 * whether the xattr name is a private one,
716 * unfortunately it flags xattrs that begin with the
717 * default streams prefix as private.
719 * By only calling samba_private_attr_name() in case
720 * the xattr does NOT begin with the default prefix,
721 * we know that if it returns 'true' it definitely one
722 * of our internal xattr like "user.DOSATTRIB".
724 if (strncasecmp_m(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
725 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
)) != 0) {
726 if (samba_private_attr_name(names
[i
])) {
731 if (strncmp(names
[i
], config
->prefix
,
732 config
->prefix_len
) != 0) {
736 status
= get_ea_value(names
,
742 if (!NT_STATUS_IS_OK(status
)) {
743 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
745 smb_fname
->base_name
,
750 ea
.name
= talloc_asprintf(
751 ea
.value
.data
, ":%s%s",
752 names
[i
] + config
->prefix_len
,
753 config
->store_stream_type
? "" : ":$DATA");
754 if (ea
.name
== NULL
) {
755 DEBUG(0, ("talloc failed\n"));
759 if (!fn(&ea
, private_data
)) {
760 TALLOC_FREE(ea
.value
.data
);
764 TALLOC_FREE(ea
.value
.data
);
771 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
772 struct stream_struct
**streams
,
773 const char *name
, off_t size
,
776 struct stream_struct
*tmp
;
778 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
784 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
785 if (tmp
[*num_streams
].name
== NULL
) {
789 tmp
[*num_streams
].size
= size
;
790 tmp
[*num_streams
].alloc_size
= alloc_size
;
797 struct streaminfo_state
{
799 vfs_handle_struct
*handle
;
800 unsigned int num_streams
;
801 struct stream_struct
*streams
;
805 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
807 struct streaminfo_state
*state
=
808 (struct streaminfo_state
*)private_data
;
810 if (!add_one_stream(state
->mem_ctx
,
811 &state
->num_streams
, &state
->streams
,
812 ea
->name
, ea
->value
.length
-1,
813 smb_roundup(state
->handle
->conn
,
814 ea
->value
.length
-1))) {
815 state
->status
= NT_STATUS_NO_MEMORY
;
822 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
823 struct files_struct
*fsp
,
824 const struct smb_filename
*smb_fname
,
826 unsigned int *pnum_streams
,
827 struct stream_struct
**pstreams
)
829 SMB_STRUCT_STAT sbuf
;
832 struct streaminfo_state state
;
834 ret
= vfs_stat_smb_basename(handle
->conn
, smb_fname
, &sbuf
);
836 return map_nt_error_from_unix(errno
);
839 state
.streams
= *pstreams
;
840 state
.num_streams
= *pnum_streams
;
841 state
.mem_ctx
= mem_ctx
;
842 state
.handle
= handle
;
843 state
.status
= NT_STATUS_OK
;
845 if (S_ISLNK(sbuf
.st_ex_mode
)) {
847 * Currently we do't have SMB_VFS_LLISTXATTR
848 * inside the VFS which means there's no way
849 * to cope with a symlink when lp_posix_pathnames().
850 * returns true. For now ignore links.
851 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
853 status
= NT_STATUS_OK
;
855 status
= walk_xattr_streams(handle
, fsp
, smb_fname
,
856 collect_one_stream
, &state
);
859 if (!NT_STATUS_IS_OK(status
)) {
860 TALLOC_FREE(state
.streams
);
864 if (!NT_STATUS_IS_OK(state
.status
)) {
865 TALLOC_FREE(state
.streams
);
869 *pnum_streams
= state
.num_streams
;
870 *pstreams
= state
.streams
;
872 return SMB_VFS_NEXT_STREAMINFO(handle
,
880 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
,
881 enum timestamp_set_resolution
*p_ts_res
)
883 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
886 static int streams_xattr_connect(vfs_handle_struct
*handle
,
887 const char *service
, const char *user
)
889 struct streams_xattr_config
*config
;
890 const char *default_prefix
= SAMBA_XATTR_DOSSTREAM_PREFIX
;
894 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
899 config
= talloc_zero(handle
->conn
, struct streams_xattr_config
);
900 if (config
== NULL
) {
901 DEBUG(1, ("talloc_zero() failed\n"));
906 prefix
= lp_parm_const_string(SNUM(handle
->conn
),
907 "streams_xattr", "prefix",
909 config
->prefix
= talloc_strdup(config
, prefix
);
910 if (config
->prefix
== NULL
) {
911 DEBUG(1, ("talloc_strdup() failed\n"));
915 config
->prefix_len
= strlen(config
->prefix
);
916 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config
->prefix
));
918 config
->store_stream_type
= lp_parm_bool(SNUM(handle
->conn
),
923 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
924 NULL
, struct stream_xattr_config
,
930 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
931 files_struct
*fsp
, const void *data
,
932 size_t n
, off_t offset
)
934 struct stream_io
*sio
=
935 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
938 struct smb_filename
*smb_fname_base
= NULL
;
941 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
944 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
947 if (!streams_xattr_recheck(sio
)) {
951 /* Create an smb_filename with stream_name == NULL. */
952 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
957 fsp
->fsp_name
->flags
);
958 if (smb_fname_base
== NULL
) {
963 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
964 smb_fname_base
, sio
->xattr_name
, &ea
);
965 if (!NT_STATUS_IS_OK(status
)) {
969 if ((offset
+ n
) > ea
.value
.length
-1) {
972 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8_t,
976 TALLOC_FREE(ea
.value
.data
);
981 ea
.value
.length
= offset
+ n
+ 1;
982 ea
.value
.data
[offset
+n
] = 0;
985 memcpy(ea
.value
.data
+ offset
, data
, n
);
987 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
990 ea
.value
.data
, ea
.value
.length
, 0);
991 TALLOC_FREE(ea
.value
.data
);
1000 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
1001 files_struct
*fsp
, void *data
,
1002 size_t n
, off_t offset
)
1004 struct stream_io
*sio
=
1005 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1006 struct ea_struct ea
;
1008 size_t length
, overlap
;
1009 struct smb_filename
*smb_fname_base
= NULL
;
1011 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1012 (int)offset
, (int)n
));
1015 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
1018 if (!streams_xattr_recheck(sio
)) {
1022 /* Create an smb_filename with stream_name == NULL. */
1023 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
1027 fsp
->fsp_name
->twrp
,
1028 fsp
->fsp_name
->flags
);
1029 if (smb_fname_base
== NULL
) {
1034 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
1035 smb_fname_base
, sio
->xattr_name
, &ea
);
1036 if (!NT_STATUS_IS_OK(status
)) {
1040 length
= ea
.value
.length
-1;
1042 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1045 /* Attempt to read past EOF. */
1046 if (length
<= offset
) {
1050 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
1051 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
1053 TALLOC_FREE(ea
.value
.data
);
1057 struct streams_xattr_pread_state
{
1059 struct vfs_aio_state vfs_aio_state
;
1062 static void streams_xattr_pread_done(struct tevent_req
*subreq
);
1064 static struct tevent_req
*streams_xattr_pread_send(
1065 struct vfs_handle_struct
*handle
,
1066 TALLOC_CTX
*mem_ctx
,
1067 struct tevent_context
*ev
,
1068 struct files_struct
*fsp
,
1070 size_t n
, off_t offset
)
1072 struct tevent_req
*req
= NULL
;
1073 struct tevent_req
*subreq
= NULL
;
1074 struct streams_xattr_pread_state
*state
= NULL
;
1075 struct stream_io
*sio
=
1076 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1078 req
= tevent_req_create(mem_ctx
, &state
,
1079 struct streams_xattr_pread_state
);
1085 subreq
= SMB_VFS_NEXT_PREAD_SEND(state
, ev
, handle
, fsp
,
1087 if (tevent_req_nomem(req
, subreq
)) {
1088 return tevent_req_post(req
, ev
);
1090 tevent_req_set_callback(subreq
, streams_xattr_pread_done
, req
);
1094 state
->nread
= SMB_VFS_PREAD(fsp
, data
, n
, offset
);
1095 if (state
->nread
!= n
) {
1096 if (state
->nread
!= -1) {
1099 tevent_req_error(req
, errno
);
1100 return tevent_req_post(req
, ev
);
1103 tevent_req_done(req
);
1104 return tevent_req_post(req
, ev
);
1107 static void streams_xattr_pread_done(struct tevent_req
*subreq
)
1109 struct tevent_req
*req
= tevent_req_callback_data(
1110 subreq
, struct tevent_req
);
1111 struct streams_xattr_pread_state
*state
= tevent_req_data(
1112 req
, struct streams_xattr_pread_state
);
1114 state
->nread
= SMB_VFS_PREAD_RECV(subreq
, &state
->vfs_aio_state
);
1115 TALLOC_FREE(subreq
);
1117 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
1120 tevent_req_done(req
);
1123 static ssize_t
streams_xattr_pread_recv(struct tevent_req
*req
,
1124 struct vfs_aio_state
*vfs_aio_state
)
1126 struct streams_xattr_pread_state
*state
= tevent_req_data(
1127 req
, struct streams_xattr_pread_state
);
1129 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
1133 *vfs_aio_state
= state
->vfs_aio_state
;
1134 return state
->nread
;
1137 struct streams_xattr_pwrite_state
{
1139 struct vfs_aio_state vfs_aio_state
;
1142 static void streams_xattr_pwrite_done(struct tevent_req
*subreq
);
1144 static struct tevent_req
*streams_xattr_pwrite_send(
1145 struct vfs_handle_struct
*handle
,
1146 TALLOC_CTX
*mem_ctx
,
1147 struct tevent_context
*ev
,
1148 struct files_struct
*fsp
,
1150 size_t n
, off_t offset
)
1152 struct tevent_req
*req
= NULL
;
1153 struct tevent_req
*subreq
= NULL
;
1154 struct streams_xattr_pwrite_state
*state
= NULL
;
1155 struct stream_io
*sio
=
1156 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1158 req
= tevent_req_create(mem_ctx
, &state
,
1159 struct streams_xattr_pwrite_state
);
1165 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
,
1167 if (tevent_req_nomem(req
, subreq
)) {
1168 return tevent_req_post(req
, ev
);
1170 tevent_req_set_callback(subreq
, streams_xattr_pwrite_done
, req
);
1174 state
->nwritten
= SMB_VFS_PWRITE(fsp
, data
, n
, offset
);
1175 if (state
->nwritten
!= n
) {
1176 if (state
->nwritten
!= -1) {
1179 tevent_req_error(req
, errno
);
1180 return tevent_req_post(req
, ev
);
1183 tevent_req_done(req
);
1184 return tevent_req_post(req
, ev
);
1187 static void streams_xattr_pwrite_done(struct tevent_req
*subreq
)
1189 struct tevent_req
*req
= tevent_req_callback_data(
1190 subreq
, struct tevent_req
);
1191 struct streams_xattr_pwrite_state
*state
= tevent_req_data(
1192 req
, struct streams_xattr_pwrite_state
);
1194 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
1195 TALLOC_FREE(subreq
);
1197 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
1200 tevent_req_done(req
);
1203 static ssize_t
streams_xattr_pwrite_recv(struct tevent_req
*req
,
1204 struct vfs_aio_state
*vfs_aio_state
)
1206 struct streams_xattr_pwrite_state
*state
= tevent_req_data(
1207 req
, struct streams_xattr_pwrite_state
);
1209 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
1213 *vfs_aio_state
= state
->vfs_aio_state
;
1214 return state
->nwritten
;
1217 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
1218 struct files_struct
*fsp
,
1223 struct ea_struct ea
;
1225 struct stream_io
*sio
=
1226 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1227 struct smb_filename
*smb_fname_base
= NULL
;
1229 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1230 fsp_str_dbg(fsp
), (double)offset
));
1233 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
1236 if (!streams_xattr_recheck(sio
)) {
1240 /* Create an smb_filename with stream_name == NULL. */
1241 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
1245 fsp
->fsp_name
->twrp
,
1246 fsp
->fsp_name
->flags
);
1247 if (smb_fname_base
== NULL
) {
1252 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
1253 smb_fname_base
, sio
->xattr_name
, &ea
);
1254 if (!NT_STATUS_IS_OK(status
)) {
1258 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8_t,
1262 TALLOC_FREE(ea
.value
.data
);
1267 /* Did we expand ? */
1268 if (ea
.value
.length
< offset
+ 1) {
1269 memset(&tmp
[ea
.value
.length
], '\0',
1270 offset
+ 1 - ea
.value
.length
);
1273 ea
.value
.data
= tmp
;
1274 ea
.value
.length
= offset
+ 1;
1275 ea
.value
.data
[offset
] = 0;
1277 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1280 ea
.value
.data
, ea
.value
.length
, 0);
1281 TALLOC_FREE(ea
.value
.data
);
1290 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
1291 struct files_struct
*fsp
,
1296 struct stream_io
*sio
=
1297 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1299 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1301 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1304 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1307 if (!streams_xattr_recheck(sio
)) {
1311 /* Let the pwrite code path handle it. */
1316 static int streams_xattr_fchown(vfs_handle_struct
*handle
, files_struct
*fsp
,
1317 uid_t uid
, gid_t gid
)
1319 struct stream_io
*sio
=
1320 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1323 return SMB_VFS_NEXT_FCHOWN(handle
, fsp
, uid
, gid
);
1329 static int streams_xattr_fchmod(vfs_handle_struct
*handle
,
1333 struct stream_io
*sio
=
1334 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1337 return SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);
1343 static ssize_t
streams_xattr_fgetxattr(struct vfs_handle_struct
*handle
,
1344 struct files_struct
*fsp
,
1349 struct stream_io
*sio
=
1350 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1353 return SMB_VFS_NEXT_FGETXATTR(handle
, fsp
, name
, value
, size
);
1360 static ssize_t
streams_xattr_flistxattr(struct vfs_handle_struct
*handle
,
1361 struct files_struct
*fsp
,
1365 struct stream_io
*sio
=
1366 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1369 return SMB_VFS_NEXT_FLISTXATTR(handle
, fsp
, list
, size
);
1376 static int streams_xattr_fremovexattr(struct vfs_handle_struct
*handle
,
1377 struct files_struct
*fsp
,
1380 struct stream_io
*sio
=
1381 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1384 return SMB_VFS_NEXT_FREMOVEXATTR(handle
, fsp
, name
);
1391 static int streams_xattr_fsetxattr(struct vfs_handle_struct
*handle
,
1392 struct files_struct
*fsp
,
1398 struct stream_io
*sio
=
1399 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1402 return SMB_VFS_NEXT_FSETXATTR(handle
, fsp
, name
, value
,
1410 static SMB_ACL_T
streams_xattr_sys_acl_get_fd(vfs_handle_struct
*handle
,
1412 TALLOC_CTX
*mem_ctx
)
1414 struct stream_io
*sio
=
1415 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1418 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle
, fsp
, mem_ctx
);
1421 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1422 handle
, fsp
->base_fsp
->fsp_name
,
1423 SMB_ACL_TYPE_ACCESS
, mem_ctx
);
1426 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct
*handle
,
1430 struct stream_io
*sio
=
1431 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1434 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle
, fsp
, theacl
);
1440 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct
*handle
,
1442 TALLOC_CTX
*mem_ctx
,
1443 char **blob_description
,
1446 struct stream_io
*sio
=
1447 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1450 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle
, fsp
, mem_ctx
,
1451 blob_description
, blob
);
1454 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1455 handle
, fsp
->base_fsp
->fsp_name
, mem_ctx
,
1456 blob_description
, blob
);
1459 static NTSTATUS
streams_xattr_fget_nt_acl(vfs_handle_struct
*handle
,
1461 uint32_t security_info
,
1462 TALLOC_CTX
*mem_ctx
,
1463 struct security_descriptor
**ppdesc
)
1465 struct stream_io
*sio
=
1466 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1469 return SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
1473 return SMB_VFS_NEXT_GET_NT_ACL_AT(handle
,
1474 handle
->conn
->cwd_fsp
,
1475 fsp
->base_fsp
->fsp_name
,
1481 static NTSTATUS
streams_xattr_fset_nt_acl(vfs_handle_struct
*handle
,
1483 uint32_t security_info_sent
,
1484 const struct security_descriptor
*psd
)
1486 struct stream_io
*sio
=
1487 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1490 return SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
,
1491 security_info_sent
, psd
);
1494 return NT_STATUS_OK
;
1497 struct streams_xattr_fsync_state
{
1499 struct vfs_aio_state vfs_aio_state
;
1502 static void streams_xattr_fsync_done(struct tevent_req
*subreq
);
1504 static struct tevent_req
*streams_xattr_fsync_send(
1505 struct vfs_handle_struct
*handle
,
1506 TALLOC_CTX
*mem_ctx
,
1507 struct tevent_context
*ev
,
1508 struct files_struct
*fsp
)
1510 struct tevent_req
*req
= NULL
;
1511 struct tevent_req
*subreq
= NULL
;
1512 struct streams_xattr_fsync_state
*state
= NULL
;
1513 struct stream_io
*sio
=
1514 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1516 req
= tevent_req_create(mem_ctx
, &state
,
1517 struct streams_xattr_fsync_state
);
1523 subreq
= SMB_VFS_NEXT_FSYNC_SEND(state
, ev
, handle
, fsp
);
1524 if (tevent_req_nomem(req
, subreq
)) {
1525 return tevent_req_post(req
, ev
);
1527 tevent_req_set_callback(subreq
, streams_xattr_fsync_done
, req
);
1532 * There's no pathname based sync variant and we don't have access to
1533 * the basefile handle, so we can't do anything here.
1536 tevent_req_done(req
);
1537 return tevent_req_post(req
, ev
);
1540 static void streams_xattr_fsync_done(struct tevent_req
*subreq
)
1542 struct tevent_req
*req
= tevent_req_callback_data(
1543 subreq
, struct tevent_req
);
1544 struct streams_xattr_fsync_state
*state
= tevent_req_data(
1545 req
, struct streams_xattr_fsync_state
);
1547 state
->ret
= SMB_VFS_FSYNC_RECV(subreq
, &state
->vfs_aio_state
);
1548 TALLOC_FREE(subreq
);
1549 if (state
->ret
!= 0) {
1550 tevent_req_error(req
, errno
);
1554 tevent_req_done(req
);
1557 static int streams_xattr_fsync_recv(struct tevent_req
*req
,
1558 struct vfs_aio_state
*vfs_aio_state
)
1560 struct streams_xattr_fsync_state
*state
= tevent_req_data(
1561 req
, struct streams_xattr_fsync_state
);
1563 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
1567 *vfs_aio_state
= state
->vfs_aio_state
;
1571 static bool streams_xattr_lock(vfs_handle_struct
*handle
,
1578 struct stream_io
*sio
=
1579 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1582 return SMB_VFS_NEXT_LOCK(handle
, fsp
, op
, offset
, count
, type
);
1588 static bool streams_xattr_getlock(vfs_handle_struct
*handle
,
1595 struct stream_io
*sio
=
1596 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1599 return SMB_VFS_NEXT_GETLOCK(handle
, fsp
, poffset
,
1600 pcount
, ptype
, ppid
);
1607 static int streams_xattr_kernel_flock(vfs_handle_struct
*handle
,
1609 uint32_t share_access
,
1610 uint32_t access_mask
)
1612 struct stream_io
*sio
=
1613 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1616 return SMB_VFS_NEXT_KERNEL_FLOCK(handle
, fsp
,
1617 share_access
, access_mask
);
1623 static int streams_xattr_linux_setlease(vfs_handle_struct
*handle
,
1627 struct stream_io
*sio
=
1628 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1631 return SMB_VFS_NEXT_LINUX_SETLEASE(handle
, fsp
, leasetype
);
1637 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct
*handle
,
1639 struct lock_struct
*plock
)
1641 struct stream_io
*sio
=
1642 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1645 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle
, fsp
, plock
);
1651 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1652 .fs_capabilities_fn
= streams_xattr_fs_capabilities
,
1653 .connect_fn
= streams_xattr_connect
,
1654 .openat_fn
= streams_xattr_openat
,
1655 .close_fn
= streams_xattr_close
,
1656 .stat_fn
= streams_xattr_stat
,
1657 .fstat_fn
= streams_xattr_fstat
,
1658 .lstat_fn
= streams_xattr_lstat
,
1659 .pread_fn
= streams_xattr_pread
,
1660 .pwrite_fn
= streams_xattr_pwrite
,
1661 .pread_send_fn
= streams_xattr_pread_send
,
1662 .pread_recv_fn
= streams_xattr_pread_recv
,
1663 .pwrite_send_fn
= streams_xattr_pwrite_send
,
1664 .pwrite_recv_fn
= streams_xattr_pwrite_recv
,
1665 .unlinkat_fn
= streams_xattr_unlinkat
,
1666 .renameat_fn
= streams_xattr_renameat
,
1667 .ftruncate_fn
= streams_xattr_ftruncate
,
1668 .fallocate_fn
= streams_xattr_fallocate
,
1669 .streaminfo_fn
= streams_xattr_streaminfo
,
1671 .fsync_send_fn
= streams_xattr_fsync_send
,
1672 .fsync_recv_fn
= streams_xattr_fsync_recv
,
1674 .lock_fn
= streams_xattr_lock
,
1675 .getlock_fn
= streams_xattr_getlock
,
1676 .kernel_flock_fn
= streams_xattr_kernel_flock
,
1677 .linux_setlease_fn
= streams_xattr_linux_setlease
,
1678 .strict_lock_check_fn
= streams_xattr_strict_lock_check
,
1680 .fchown_fn
= streams_xattr_fchown
,
1681 .fchmod_fn
= streams_xattr_fchmod
,
1683 .fgetxattr_fn
= streams_xattr_fgetxattr
,
1684 .flistxattr_fn
= streams_xattr_flistxattr
,
1685 .fremovexattr_fn
= streams_xattr_fremovexattr
,
1686 .fsetxattr_fn
= streams_xattr_fsetxattr
,
1688 .sys_acl_get_fd_fn
= streams_xattr_sys_acl_get_fd
,
1689 .sys_acl_blob_get_fd_fn
= streams_xattr_sys_acl_blob_get_fd
,
1690 .sys_acl_set_fd_fn
= streams_xattr_sys_acl_set_fd
,
1692 .fget_nt_acl_fn
= streams_xattr_fget_nt_acl
,
1693 .fset_nt_acl_fn
= streams_xattr_fset_nt_acl
,
1697 NTSTATUS
vfs_streams_xattr_init(TALLOC_CTX
*ctx
)
1699 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1700 &vfs_streams_xattr_fns
);