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/crypto/md5.h"
28 #include "lib/util/tevent_unix.h"
29 #include "librpc/gen_ndr/ioctl.h"
32 #define DBGC_CLASS DBGC_VFS
34 struct streams_xattr_config
{
37 bool store_stream_type
;
45 vfs_handle_struct
*handle
;
48 static SMB_INO_T
stream_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
51 unsigned char hash
[16];
55 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
56 (unsigned long)sbuf
->st_ex_dev
,
57 (unsigned long)sbuf
->st_ex_ino
, sname
));
59 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
60 SMB_ASSERT(upper_sname
!= NULL
);
63 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_dev
),
64 sizeof(sbuf
->st_ex_dev
));
65 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_ino
),
66 sizeof(sbuf
->st_ex_ino
));
67 MD5Update(&ctx
, (unsigned char *)upper_sname
,
68 talloc_get_size(upper_sname
)-1);
71 TALLOC_FREE(upper_sname
);
73 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
74 memcpy(&result
, hash
, sizeof(result
));
76 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result
));
81 static ssize_t
get_xattr_size(connection_struct
*conn
,
82 const struct smb_filename
*smb_fname
,
83 const char *xattr_name
)
89 status
= get_ea_value(talloc_tos(), conn
, NULL
, smb_fname
,
92 if (!NT_STATUS_IS_OK(status
)) {
96 result
= ea
.value
.length
-1;
97 TALLOC_FREE(ea
.value
.data
);
102 * Given a stream name, populate xattr_name with the xattr name to use for
103 * accessing the stream.
105 static NTSTATUS
streams_xattr_get_name(vfs_handle_struct
*handle
,
107 const char *stream_name
,
112 struct streams_xattr_config
*config
;
114 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
115 return NT_STATUS_UNSUCCESSFUL
);
117 sname
= talloc_strdup(ctx
, stream_name
+ 1);
119 return NT_STATUS_NO_MEMORY
;
123 * With vfs_fruit option "fruit:encoding = native" we're
124 * already converting stream names that contain illegal NTFS
125 * characters from their on-the-wire Unicode Private Range
126 * encoding to their native ASCII representation.
128 * As as result the name of xattrs storing the streams (via
129 * vfs_streams_xattr) may contain a colon, so we have to use
130 * strrchr_m() instead of strchr_m() for matching the stream
133 * In check_path_syntax() we've already ensured the streamname
134 * we got from the client is valid.
136 stype
= strrchr_m(sname
, ':');
140 * We only support one stream type: "$DATA"
142 if (strcasecmp_m(stype
, ":$DATA") != 0) {
144 return NT_STATUS_INVALID_PARAMETER
;
147 /* Split name and type */
151 *xattr_name
= talloc_asprintf(ctx
, "%s%s%s",
154 config
->store_stream_type
? ":$DATA" : "");
155 if (*xattr_name
== NULL
) {
157 return NT_STATUS_NO_MEMORY
;
160 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name
,
167 static bool streams_xattr_recheck(struct stream_io
*sio
)
170 char *xattr_name
= NULL
;
172 if (sio
->fsp
->fsp_name
== sio
->fsp_name_ptr
) {
176 if (sio
->fsp
->fsp_name
->stream_name
== NULL
) {
177 /* how can this happen */
182 status
= streams_xattr_get_name(sio
->handle
, talloc_tos(),
183 sio
->fsp
->fsp_name
->stream_name
,
185 if (!NT_STATUS_IS_OK(status
)) {
189 TALLOC_FREE(sio
->xattr_name
);
190 TALLOC_FREE(sio
->base
);
191 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
193 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
194 sio
->fsp
->fsp_name
->base_name
);
195 sio
->fsp_name_ptr
= sio
->fsp
->fsp_name
;
197 TALLOC_FREE(xattr_name
);
199 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
207 * Helper to stat/lstat the base file of an smb_fname.
209 static int streams_xattr_stat_base(vfs_handle_struct
*handle
,
210 struct smb_filename
*smb_fname
,
213 char *tmp_stream_name
;
216 tmp_stream_name
= smb_fname
->stream_name
;
217 smb_fname
->stream_name
= NULL
;
219 result
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
221 result
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
223 smb_fname
->stream_name
= tmp_stream_name
;
227 static int streams_xattr_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
228 SMB_STRUCT_STAT
*sbuf
)
230 struct smb_filename
*smb_fname_base
= NULL
;
232 struct stream_io
*io
= (struct stream_io
*)
233 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
235 if (io
== NULL
|| fsp
->base_fsp
== NULL
) {
236 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
239 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io
->fsp
));
241 if (!streams_xattr_recheck(io
)) {
245 /* Create an smb_filename with stream_name == NULL. */
246 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
250 fsp
->fsp_name
->flags
);
251 if (smb_fname_base
== NULL
) {
256 if (smb_fname_base
->flags
& SMB_FILENAME_POSIX_PATH
) {
257 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
259 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
261 *sbuf
= smb_fname_base
->st
;
264 TALLOC_FREE(smb_fname_base
);
268 sbuf
->st_ex_size
= get_xattr_size(handle
->conn
,
269 smb_fname_base
, io
->xattr_name
);
270 if (sbuf
->st_ex_size
== -1) {
271 TALLOC_FREE(smb_fname_base
);
272 SET_STAT_INVALID(*sbuf
);
276 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf
->st_ex_size
));
278 sbuf
->st_ex_ino
= stream_inode(sbuf
, io
->xattr_name
);
279 sbuf
->st_ex_mode
&= ~S_IFMT
;
280 sbuf
->st_ex_mode
|= S_IFREG
;
281 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
283 TALLOC_FREE(smb_fname_base
);
287 static int streams_xattr_stat(vfs_handle_struct
*handle
,
288 struct smb_filename
*smb_fname
)
292 char *xattr_name
= NULL
;
294 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
295 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
298 /* Note if lp_posix_paths() is true, we can never
299 * get here as is_ntfs_stream_smb_fname() is
300 * always false. So we never need worry about
301 * not following links here. */
303 /* If the default stream is requested, just stat the base file. */
304 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
305 return streams_xattr_stat_base(handle
, smb_fname
, true);
308 /* Populate the stat struct with info from the base file. */
309 if (streams_xattr_stat_base(handle
, smb_fname
, true) == -1) {
313 /* Derive the xattr name to lookup. */
314 status
= streams_xattr_get_name(handle
, talloc_tos(),
315 smb_fname
->stream_name
, &xattr_name
);
316 if (!NT_STATUS_IS_OK(status
)) {
317 errno
= map_errno_from_nt_status(status
);
321 /* Augment the base file's stat information before returning. */
322 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
,
325 if (smb_fname
->st
.st_ex_size
== -1) {
326 SET_STAT_INVALID(smb_fname
->st
);
332 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
333 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
334 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
335 smb_fname
->st
.st_ex_blocks
=
336 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
340 TALLOC_FREE(xattr_name
);
344 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
345 struct smb_filename
*smb_fname
)
349 char *xattr_name
= NULL
;
351 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
352 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
355 /* If the default stream is requested, just stat the base file. */
356 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
357 return streams_xattr_stat_base(handle
, smb_fname
, false);
360 /* Populate the stat struct with info from the base file. */
361 if (streams_xattr_stat_base(handle
, smb_fname
, false) == -1) {
365 /* Derive the xattr name to lookup. */
366 status
= streams_xattr_get_name(handle
, talloc_tos(),
367 smb_fname
->stream_name
, &xattr_name
);
368 if (!NT_STATUS_IS_OK(status
)) {
369 errno
= map_errno_from_nt_status(status
);
373 /* Augment the base file's stat information before returning. */
374 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
,
377 if (smb_fname
->st
.st_ex_size
== -1) {
378 SET_STAT_INVALID(smb_fname
->st
);
384 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
385 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
386 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
387 smb_fname
->st
.st_ex_blocks
=
388 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
393 TALLOC_FREE(xattr_name
);
397 static int streams_xattr_open(vfs_handle_struct
*handle
,
398 struct smb_filename
*smb_fname
,
399 files_struct
*fsp
, int flags
, mode_t mode
)
402 struct streams_xattr_config
*config
= NULL
;
403 struct stream_io
*sio
= NULL
;
405 char *xattr_name
= NULL
;
410 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
413 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
414 smb_fname_str_dbg(smb_fname
), flags
));
416 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
417 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
420 /* If the default stream is requested, just open the base file. */
421 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
422 char *tmp_stream_name
;
424 tmp_stream_name
= smb_fname
->stream_name
;
425 smb_fname
->stream_name
= NULL
;
427 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
429 smb_fname
->stream_name
= tmp_stream_name
;
434 status
= streams_xattr_get_name(handle
, talloc_tos(),
435 smb_fname
->stream_name
, &xattr_name
);
436 if (!NT_STATUS_IS_OK(status
)) {
437 errno
= map_errno_from_nt_status(status
);
442 * Return a valid fd, but ensure any attempt to use it returns an error
445 ret
= pipe(pipe_fds
);
452 fakefd
= pipe_fds
[0];
454 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
455 smb_fname
, xattr_name
, &ea
);
457 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
459 if (!NT_STATUS_IS_OK(status
)
460 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
462 * The base file is not there. This is an error even if we got
463 * O_CREAT, the higher levels should have created the base
466 DEBUG(10, ("streams_xattr_open: base file %s not around, "
467 "returning ENOENT\n", smb_fname
->base_name
));
472 if ((!NT_STATUS_IS_OK(status
) && (flags
& O_CREAT
)) ||
475 * The attribute does not exist or needs to be truncated
479 * Darn, xattrs need at least 1 byte
483 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
484 xattr_name
, smb_fname
->base_name
));
486 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
490 flags
& O_EXCL
? XATTR_CREATE
: 0);
496 sio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct stream_io
, NULL
);
502 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
505 * so->base needs to be a copy of fsp->fsp_name->base_name,
506 * making it identical to streams_xattr_recheck(). If the
507 * open is changing directories, fsp->fsp_name->base_name
508 * will be the full path from the share root, whilst
509 * smb_fname will be relative to the $cwd.
511 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
512 fsp
->fsp_name
->base_name
);
513 sio
->fsp_name_ptr
= fsp
->fsp_name
;
514 sio
->handle
= handle
;
517 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
533 static int streams_xattr_unlink(vfs_handle_struct
*handle
,
534 const struct smb_filename
*smb_fname
)
538 char *xattr_name
= NULL
;
540 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
541 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
544 /* If the default stream is requested, just open the base file. */
545 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
546 struct smb_filename
*smb_fname_base
= NULL
;
548 smb_fname_base
= cp_smb_filename(talloc_tos(), smb_fname
);
549 if (smb_fname_base
== NULL
) {
554 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
556 TALLOC_FREE(smb_fname_base
);
560 status
= streams_xattr_get_name(handle
, talloc_tos(),
561 smb_fname
->stream_name
, &xattr_name
);
562 if (!NT_STATUS_IS_OK(status
)) {
563 errno
= map_errno_from_nt_status(status
);
567 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
, xattr_name
);
569 if ((ret
== -1) && (errno
== ENOATTR
)) {
577 TALLOC_FREE(xattr_name
);
581 static int streams_xattr_rename(vfs_handle_struct
*handle
,
582 const struct smb_filename
*smb_fname_src
,
583 const struct smb_filename
*smb_fname_dst
)
587 char *src_xattr_name
= NULL
;
588 char *dst_xattr_name
= NULL
;
589 bool src_is_stream
, dst_is_stream
;
594 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
595 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
597 if (!src_is_stream
&& !dst_is_stream
) {
598 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
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(),
940 fsp
->fsp_name
->flags
);
941 if (smb_fname_base
== NULL
) {
946 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
947 smb_fname_base
, sio
->xattr_name
, &ea
);
948 if (!NT_STATUS_IS_OK(status
)) {
952 if ((offset
+ n
) > ea
.value
.length
-1) {
955 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8_t,
959 TALLOC_FREE(ea
.value
.data
);
964 ea
.value
.length
= offset
+ n
+ 1;
965 ea
.value
.data
[offset
+n
] = 0;
968 memcpy(ea
.value
.data
+ offset
, data
, n
);
970 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
973 ea
.value
.data
, ea
.value
.length
, 0);
974 TALLOC_FREE(ea
.value
.data
);
983 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
984 files_struct
*fsp
, void *data
,
985 size_t n
, off_t offset
)
987 struct stream_io
*sio
=
988 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
991 size_t length
, overlap
;
992 struct smb_filename
*smb_fname_base
= NULL
;
994 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
995 (int)offset
, (int)n
));
998 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
1001 if (!streams_xattr_recheck(sio
)) {
1005 /* Create an smb_filename with stream_name == NULL. */
1006 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
1010 fsp
->fsp_name
->flags
);
1011 if (smb_fname_base
== NULL
) {
1016 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
1017 smb_fname_base
, sio
->xattr_name
, &ea
);
1018 if (!NT_STATUS_IS_OK(status
)) {
1022 length
= ea
.value
.length
-1;
1024 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1027 /* Attempt to read past EOF. */
1028 if (length
<= offset
) {
1032 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
1033 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
1035 TALLOC_FREE(ea
.value
.data
);
1039 struct streams_xattr_pread_state
{
1041 struct vfs_aio_state vfs_aio_state
;
1044 static void streams_xattr_pread_done(struct tevent_req
*subreq
);
1046 static struct tevent_req
*streams_xattr_pread_send(
1047 struct vfs_handle_struct
*handle
,
1048 TALLOC_CTX
*mem_ctx
,
1049 struct tevent_context
*ev
,
1050 struct files_struct
*fsp
,
1052 size_t n
, off_t offset
)
1054 struct tevent_req
*req
= NULL
;
1055 struct tevent_req
*subreq
= NULL
;
1056 struct streams_xattr_pread_state
*state
= NULL
;
1057 struct stream_io
*sio
=
1058 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1060 req
= tevent_req_create(mem_ctx
, &state
,
1061 struct streams_xattr_pread_state
);
1067 subreq
= SMB_VFS_NEXT_PREAD_SEND(state
, ev
, handle
, fsp
,
1069 if (tevent_req_nomem(req
, subreq
)) {
1070 return tevent_req_post(req
, ev
);
1072 tevent_req_set_callback(subreq
, streams_xattr_pread_done
, req
);
1076 state
->nread
= SMB_VFS_PREAD(fsp
, data
, n
, offset
);
1077 if (state
->nread
!= n
) {
1078 if (state
->nread
!= -1) {
1081 tevent_req_error(req
, errno
);
1082 return tevent_req_post(req
, ev
);
1085 tevent_req_done(req
);
1086 return tevent_req_post(req
, ev
);
1089 static void streams_xattr_pread_done(struct tevent_req
*subreq
)
1091 struct tevent_req
*req
= tevent_req_callback_data(
1092 subreq
, struct tevent_req
);
1093 struct streams_xattr_pread_state
*state
= tevent_req_data(
1094 req
, struct streams_xattr_pread_state
);
1096 state
->nread
= SMB_VFS_PREAD_RECV(subreq
, &state
->vfs_aio_state
);
1097 TALLOC_FREE(subreq
);
1099 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
1102 tevent_req_done(req
);
1105 static ssize_t
streams_xattr_pread_recv(struct tevent_req
*req
,
1106 struct vfs_aio_state
*vfs_aio_state
)
1108 struct streams_xattr_pread_state
*state
= tevent_req_data(
1109 req
, struct streams_xattr_pread_state
);
1111 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
1115 *vfs_aio_state
= state
->vfs_aio_state
;
1116 return state
->nread
;
1119 struct streams_xattr_pwrite_state
{
1121 struct vfs_aio_state vfs_aio_state
;
1124 static void streams_xattr_pwrite_done(struct tevent_req
*subreq
);
1126 static struct tevent_req
*streams_xattr_pwrite_send(
1127 struct vfs_handle_struct
*handle
,
1128 TALLOC_CTX
*mem_ctx
,
1129 struct tevent_context
*ev
,
1130 struct files_struct
*fsp
,
1132 size_t n
, off_t offset
)
1134 struct tevent_req
*req
= NULL
;
1135 struct tevent_req
*subreq
= NULL
;
1136 struct streams_xattr_pwrite_state
*state
= NULL
;
1137 struct stream_io
*sio
=
1138 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1140 req
= tevent_req_create(mem_ctx
, &state
,
1141 struct streams_xattr_pwrite_state
);
1147 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
,
1149 if (tevent_req_nomem(req
, subreq
)) {
1150 return tevent_req_post(req
, ev
);
1152 tevent_req_set_callback(subreq
, streams_xattr_pwrite_done
, req
);
1156 state
->nwritten
= SMB_VFS_PWRITE(fsp
, data
, n
, offset
);
1157 if (state
->nwritten
!= n
) {
1158 if (state
->nwritten
!= -1) {
1161 tevent_req_error(req
, errno
);
1162 return tevent_req_post(req
, ev
);
1165 tevent_req_done(req
);
1166 return tevent_req_post(req
, ev
);
1169 static void streams_xattr_pwrite_done(struct tevent_req
*subreq
)
1171 struct tevent_req
*req
= tevent_req_callback_data(
1172 subreq
, struct tevent_req
);
1173 struct streams_xattr_pwrite_state
*state
= tevent_req_data(
1174 req
, struct streams_xattr_pwrite_state
);
1176 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
1177 TALLOC_FREE(subreq
);
1179 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
1182 tevent_req_done(req
);
1185 static ssize_t
streams_xattr_pwrite_recv(struct tevent_req
*req
,
1186 struct vfs_aio_state
*vfs_aio_state
)
1188 struct streams_xattr_pwrite_state
*state
= tevent_req_data(
1189 req
, struct streams_xattr_pwrite_state
);
1191 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
1195 *vfs_aio_state
= state
->vfs_aio_state
;
1196 return state
->nwritten
;
1199 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
1200 struct files_struct
*fsp
,
1205 struct ea_struct ea
;
1207 struct stream_io
*sio
=
1208 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1209 struct smb_filename
*smb_fname_base
= NULL
;
1211 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1212 fsp_str_dbg(fsp
), (double)offset
));
1215 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
1218 if (!streams_xattr_recheck(sio
)) {
1222 /* Create an smb_filename with stream_name == NULL. */
1223 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
1227 fsp
->fsp_name
->flags
);
1228 if (smb_fname_base
== NULL
) {
1233 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
1234 smb_fname_base
, sio
->xattr_name
, &ea
);
1235 if (!NT_STATUS_IS_OK(status
)) {
1239 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8_t,
1243 TALLOC_FREE(ea
.value
.data
);
1248 /* Did we expand ? */
1249 if (ea
.value
.length
< offset
+ 1) {
1250 memset(&tmp
[ea
.value
.length
], '\0',
1251 offset
+ 1 - ea
.value
.length
);
1254 ea
.value
.data
= tmp
;
1255 ea
.value
.length
= offset
+ 1;
1256 ea
.value
.data
[offset
] = 0;
1258 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1261 ea
.value
.data
, ea
.value
.length
, 0);
1262 TALLOC_FREE(ea
.value
.data
);
1271 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
1272 struct files_struct
*fsp
,
1277 struct stream_io
*sio
=
1278 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1280 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1282 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1285 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1288 if (!streams_xattr_recheck(sio
)) {
1292 /* Let the pwrite code path handle it. */
1297 static int streams_xattr_fchown(vfs_handle_struct
*handle
, files_struct
*fsp
,
1298 uid_t uid
, gid_t gid
)
1300 struct stream_io
*sio
=
1301 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1304 return SMB_VFS_NEXT_FCHOWN(handle
, fsp
, uid
, gid
);
1310 static int streams_xattr_fchmod(vfs_handle_struct
*handle
,
1314 struct stream_io
*sio
=
1315 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1318 return SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);
1324 static int streams_xattr_fsync(vfs_handle_struct
*handle
, files_struct
*fsp
)
1326 struct stream_io
*sio
=
1327 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1330 return SMB_VFS_NEXT_FSYNC(handle
, fsp
);
1336 static ssize_t
streams_xattr_fgetxattr(struct vfs_handle_struct
*handle
,
1337 struct files_struct
*fsp
,
1342 struct stream_io
*sio
=
1343 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1346 return SMB_VFS_NEXT_FGETXATTR(handle
, fsp
, name
, value
, size
);
1353 static ssize_t
streams_xattr_flistxattr(struct vfs_handle_struct
*handle
,
1354 struct files_struct
*fsp
,
1358 struct stream_io
*sio
=
1359 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1362 return SMB_VFS_NEXT_FLISTXATTR(handle
, fsp
, list
, size
);
1369 static int streams_xattr_fremovexattr(struct vfs_handle_struct
*handle
,
1370 struct files_struct
*fsp
,
1373 struct stream_io
*sio
=
1374 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1377 return SMB_VFS_NEXT_FREMOVEXATTR(handle
, fsp
, name
);
1384 static int streams_xattr_fsetxattr(struct vfs_handle_struct
*handle
,
1385 struct files_struct
*fsp
,
1391 struct stream_io
*sio
=
1392 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1395 return SMB_VFS_NEXT_FSETXATTR(handle
, fsp
, name
, value
,
1403 static int streams_xattr_fchmod_acl(vfs_handle_struct
*handle
,
1407 struct stream_io
*sio
=
1408 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1411 return SMB_VFS_NEXT_FCHMOD_ACL(handle
, fsp
, mode
);
1417 static SMB_ACL_T
streams_xattr_sys_acl_get_fd(vfs_handle_struct
*handle
,
1419 TALLOC_CTX
*mem_ctx
)
1421 struct stream_io
*sio
=
1422 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1425 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle
, fsp
, mem_ctx
);
1428 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1429 handle
, fsp
->base_fsp
->fsp_name
,
1430 SMB_ACL_TYPE_ACCESS
, mem_ctx
);
1433 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct
*handle
,
1437 struct stream_io
*sio
=
1438 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1441 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle
, fsp
, theacl
);
1447 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct
*handle
,
1449 TALLOC_CTX
*mem_ctx
,
1450 char **blob_description
,
1453 struct stream_io
*sio
=
1454 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1457 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle
, fsp
, mem_ctx
,
1458 blob_description
, blob
);
1461 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1462 handle
, fsp
->base_fsp
->fsp_name
, mem_ctx
,
1463 blob_description
, blob
);
1466 static NTSTATUS
streams_xattr_fget_nt_acl(vfs_handle_struct
*handle
,
1468 uint32_t security_info
,
1469 TALLOC_CTX
*mem_ctx
,
1470 struct security_descriptor
**ppdesc
)
1472 struct stream_io
*sio
=
1473 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1476 return SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
1480 return SMB_VFS_NEXT_GET_NT_ACL(handle
, fsp
->base_fsp
->fsp_name
,
1481 security_info
, mem_ctx
, ppdesc
);
1484 static NTSTATUS
streams_xattr_fset_nt_acl(vfs_handle_struct
*handle
,
1486 uint32_t security_info_sent
,
1487 const struct security_descriptor
*psd
)
1489 struct stream_io
*sio
=
1490 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1493 return SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
,
1494 security_info_sent
, psd
);
1497 return NT_STATUS_OK
;
1500 struct streams_xattr_fsync_state
{
1502 struct vfs_aio_state vfs_aio_state
;
1505 static void streams_xattr_fsync_done(struct tevent_req
*subreq
);
1507 static struct tevent_req
*streams_xattr_fsync_send(
1508 struct vfs_handle_struct
*handle
,
1509 TALLOC_CTX
*mem_ctx
,
1510 struct tevent_context
*ev
,
1511 struct files_struct
*fsp
)
1513 struct tevent_req
*req
= NULL
;
1514 struct tevent_req
*subreq
= NULL
;
1515 struct streams_xattr_fsync_state
*state
= NULL
;
1516 struct stream_io
*sio
=
1517 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1519 req
= tevent_req_create(mem_ctx
, &state
,
1520 struct streams_xattr_fsync_state
);
1526 subreq
= SMB_VFS_NEXT_FSYNC_SEND(state
, ev
, handle
, fsp
);
1527 if (tevent_req_nomem(req
, subreq
)) {
1528 return tevent_req_post(req
, ev
);
1530 tevent_req_set_callback(subreq
, streams_xattr_fsync_done
, req
);
1535 * There's no pathname based sync variant and we don't have access to
1536 * the basefile handle, so we can't do anything here.
1539 tevent_req_done(req
);
1540 return tevent_req_post(req
, ev
);
1543 static void streams_xattr_fsync_done(struct tevent_req
*subreq
)
1545 struct tevent_req
*req
= tevent_req_callback_data(
1546 subreq
, struct tevent_req
);
1547 struct streams_xattr_fsync_state
*state
= tevent_req_data(
1548 req
, struct streams_xattr_fsync_state
);
1550 state
->ret
= SMB_VFS_FSYNC_RECV(subreq
, &state
->vfs_aio_state
);
1551 TALLOC_FREE(subreq
);
1552 if (state
->ret
!= 0) {
1553 tevent_req_error(req
, errno
);
1557 tevent_req_done(req
);
1560 static int streams_xattr_fsync_recv(struct tevent_req
*req
,
1561 struct vfs_aio_state
*vfs_aio_state
)
1563 struct streams_xattr_fsync_state
*state
= tevent_req_data(
1564 req
, struct streams_xattr_fsync_state
);
1566 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
1570 *vfs_aio_state
= state
->vfs_aio_state
;
1574 static bool streams_xattr_lock(vfs_handle_struct
*handle
,
1581 struct stream_io
*sio
=
1582 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1585 return SMB_VFS_NEXT_LOCK(handle
, fsp
, op
, offset
, count
, type
);
1591 static bool streams_xattr_getlock(vfs_handle_struct
*handle
,
1598 struct stream_io
*sio
=
1599 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1602 return SMB_VFS_NEXT_GETLOCK(handle
, fsp
, poffset
,
1603 pcount
, ptype
, ppid
);
1610 static int streams_xattr_kernel_flock(vfs_handle_struct
*handle
,
1612 uint32_t share_mode
,
1613 uint32_t access_mask
)
1615 struct stream_io
*sio
=
1616 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1619 return SMB_VFS_NEXT_KERNEL_FLOCK(handle
, fsp
,
1620 share_mode
, access_mask
);
1626 static int streams_xattr_linux_setlease(vfs_handle_struct
*handle
,
1630 struct stream_io
*sio
=
1631 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1634 return SMB_VFS_NEXT_LINUX_SETLEASE(handle
, fsp
, leasetype
);
1640 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct
*handle
,
1642 struct lock_struct
*plock
)
1644 struct stream_io
*sio
=
1645 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1648 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle
, fsp
, plock
);
1654 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1655 .fs_capabilities_fn
= streams_xattr_fs_capabilities
,
1656 .connect_fn
= streams_xattr_connect
,
1657 .open_fn
= streams_xattr_open
,
1658 .stat_fn
= streams_xattr_stat
,
1659 .fstat_fn
= streams_xattr_fstat
,
1660 .lstat_fn
= streams_xattr_lstat
,
1661 .pread_fn
= streams_xattr_pread
,
1662 .pwrite_fn
= streams_xattr_pwrite
,
1663 .pread_send_fn
= streams_xattr_pread_send
,
1664 .pread_recv_fn
= streams_xattr_pread_recv
,
1665 .pwrite_send_fn
= streams_xattr_pwrite_send
,
1666 .pwrite_recv_fn
= streams_xattr_pwrite_recv
,
1667 .unlink_fn
= streams_xattr_unlink
,
1668 .rename_fn
= streams_xattr_rename
,
1669 .ftruncate_fn
= streams_xattr_ftruncate
,
1670 .fallocate_fn
= streams_xattr_fallocate
,
1671 .streaminfo_fn
= streams_xattr_streaminfo
,
1673 .fsync_send_fn
= streams_xattr_fsync_send
,
1674 .fsync_recv_fn
= streams_xattr_fsync_recv
,
1676 .lock_fn
= streams_xattr_lock
,
1677 .getlock_fn
= streams_xattr_getlock
,
1678 .kernel_flock_fn
= streams_xattr_kernel_flock
,
1679 .linux_setlease_fn
= streams_xattr_linux_setlease
,
1680 .strict_lock_check_fn
= streams_xattr_strict_lock_check
,
1682 .fchown_fn
= streams_xattr_fchown
,
1683 .fchmod_fn
= streams_xattr_fchmod
,
1684 .fsync_fn
= streams_xattr_fsync
,
1686 .fgetxattr_fn
= streams_xattr_fgetxattr
,
1687 .flistxattr_fn
= streams_xattr_flistxattr
,
1688 .fremovexattr_fn
= streams_xattr_fremovexattr
,
1689 .fsetxattr_fn
= streams_xattr_fsetxattr
,
1691 .fchmod_acl_fn
= streams_xattr_fchmod_acl
,
1693 .sys_acl_get_fd_fn
= streams_xattr_sys_acl_get_fd
,
1694 .sys_acl_blob_get_fd_fn
= streams_xattr_sys_acl_blob_get_fd
,
1695 .sys_acl_set_fd_fn
= streams_xattr_sys_acl_set_fd
,
1697 .fget_nt_acl_fn
= streams_xattr_fget_nt_acl
,
1698 .fset_nt_acl_fn
= streams_xattr_fset_nt_acl
,
1701 NTSTATUS
vfs_streams_xattr_init(TALLOC_CTX
*);
1702 NTSTATUS
vfs_streams_xattr_init(TALLOC_CTX
*ctx
)
1704 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1705 &vfs_streams_xattr_fns
);