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"
31 #define DBGC_CLASS DBGC_VFS
33 struct streams_xattr_config
{
36 bool store_stream_type
;
44 vfs_handle_struct
*handle
;
47 static SMB_INO_T
stream_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
50 unsigned char hash
[16];
54 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
55 (unsigned long)sbuf
->st_ex_dev
,
56 (unsigned long)sbuf
->st_ex_ino
, sname
));
58 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
59 SMB_ASSERT(upper_sname
!= NULL
);
62 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_dev
),
63 sizeof(sbuf
->st_ex_dev
));
64 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_ino
),
65 sizeof(sbuf
->st_ex_ino
));
66 MD5Update(&ctx
, (unsigned char *)upper_sname
,
67 talloc_get_size(upper_sname
)-1);
70 TALLOC_FREE(upper_sname
);
72 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
73 memcpy(&result
, hash
, sizeof(result
));
75 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result
));
80 static ssize_t
get_xattr_size(connection_struct
*conn
,
81 const struct smb_filename
*smb_fname
,
82 const char *xattr_name
)
88 status
= get_ea_value(talloc_tos(), conn
, NULL
, smb_fname
,
91 if (!NT_STATUS_IS_OK(status
)) {
95 result
= ea
.value
.length
-1;
96 TALLOC_FREE(ea
.value
.data
);
101 * Given a stream name, populate xattr_name with the xattr name to use for
102 * accessing the stream.
104 static NTSTATUS
streams_xattr_get_name(vfs_handle_struct
*handle
,
106 const char *stream_name
,
111 struct streams_xattr_config
*config
;
113 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
114 return NT_STATUS_UNSUCCESSFUL
);
116 sname
= talloc_strdup(ctx
, stream_name
+ 1);
118 return NT_STATUS_NO_MEMORY
;
122 * With vfs_fruit option "fruit:encoding = native" we're
123 * already converting stream names that contain illegal NTFS
124 * characters from their on-the-wire Unicode Private Range
125 * encoding to their native ASCII representation.
127 * As as result the name of xattrs storing the streams (via
128 * vfs_streams_xattr) may contain a colon, so we have to use
129 * strrchr_m() instead of strchr_m() for matching the stream
132 * In check_path_syntax() we've already ensured the streamname
133 * we got from the client is valid.
135 stype
= strrchr_m(sname
, ':');
139 * We only support one stream type: "$DATA"
141 if (strcasecmp_m(stype
, ":$DATA") != 0) {
143 return NT_STATUS_INVALID_PARAMETER
;
146 /* Split name and type */
150 *xattr_name
= talloc_asprintf(ctx
, "%s%s%s",
153 config
->store_stream_type
? ":$DATA" : "");
154 if (*xattr_name
== NULL
) {
156 return NT_STATUS_NO_MEMORY
;
159 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name
,
166 static bool streams_xattr_recheck(struct stream_io
*sio
)
169 char *xattr_name
= NULL
;
171 if (sio
->fsp
->fsp_name
== sio
->fsp_name_ptr
) {
175 if (sio
->fsp
->fsp_name
->stream_name
== NULL
) {
176 /* how can this happen */
181 status
= streams_xattr_get_name(sio
->handle
, talloc_tos(),
182 sio
->fsp
->fsp_name
->stream_name
,
184 if (!NT_STATUS_IS_OK(status
)) {
188 TALLOC_FREE(sio
->xattr_name
);
189 TALLOC_FREE(sio
->base
);
190 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
192 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
193 sio
->fsp
->fsp_name
->base_name
);
194 sio
->fsp_name_ptr
= sio
->fsp
->fsp_name
;
196 TALLOC_FREE(xattr_name
);
198 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
206 * Helper to stat/lstat the base file of an smb_fname.
208 static int streams_xattr_stat_base(vfs_handle_struct
*handle
,
209 struct smb_filename
*smb_fname
,
212 char *tmp_stream_name
;
215 tmp_stream_name
= smb_fname
->stream_name
;
216 smb_fname
->stream_name
= NULL
;
218 result
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
220 result
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
222 smb_fname
->stream_name
= tmp_stream_name
;
226 static int streams_xattr_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
227 SMB_STRUCT_STAT
*sbuf
)
229 struct smb_filename
*smb_fname_base
= NULL
;
231 struct stream_io
*io
= (struct stream_io
*)
232 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
234 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io
->fsp
));
236 if (io
== NULL
|| fsp
->base_fsp
== NULL
) {
237 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
240 if (!streams_xattr_recheck(io
)) {
244 /* Create an smb_filename with stream_name == NULL. */
245 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
249 fsp
->fsp_name
->flags
);
250 if (smb_fname_base
== NULL
) {
255 if (smb_fname_base
->flags
& SMB_FILENAME_POSIX_PATH
) {
256 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
258 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
260 *sbuf
= smb_fname_base
->st
;
263 TALLOC_FREE(smb_fname_base
);
267 sbuf
->st_ex_size
= get_xattr_size(handle
->conn
,
268 smb_fname_base
, io
->xattr_name
);
269 if (sbuf
->st_ex_size
== -1) {
270 TALLOC_FREE(smb_fname_base
);
271 SET_STAT_INVALID(*sbuf
);
275 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf
->st_ex_size
));
277 sbuf
->st_ex_ino
= stream_inode(sbuf
, io
->xattr_name
);
278 sbuf
->st_ex_mode
&= ~S_IFMT
;
279 sbuf
->st_ex_mode
|= S_IFREG
;
280 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
282 TALLOC_FREE(smb_fname_base
);
286 static int streams_xattr_stat(vfs_handle_struct
*handle
,
287 struct smb_filename
*smb_fname
)
291 char *xattr_name
= NULL
;
293 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
294 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
297 /* Note if lp_posix_paths() is true, we can never
298 * get here as is_ntfs_stream_smb_fname() is
299 * always false. So we never need worry about
300 * not following links here. */
302 /* If the default stream is requested, just stat the base file. */
303 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
304 return streams_xattr_stat_base(handle
, smb_fname
, true);
307 /* Populate the stat struct with info from the base file. */
308 if (streams_xattr_stat_base(handle
, smb_fname
, true) == -1) {
312 /* Derive the xattr name to lookup. */
313 status
= streams_xattr_get_name(handle
, talloc_tos(),
314 smb_fname
->stream_name
, &xattr_name
);
315 if (!NT_STATUS_IS_OK(status
)) {
316 errno
= map_errno_from_nt_status(status
);
320 /* Augment the base file's stat information before returning. */
321 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
,
324 if (smb_fname
->st
.st_ex_size
== -1) {
325 SET_STAT_INVALID(smb_fname
->st
);
331 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
332 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
333 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
334 smb_fname
->st
.st_ex_blocks
=
335 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
339 TALLOC_FREE(xattr_name
);
343 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
344 struct smb_filename
*smb_fname
)
348 char *xattr_name
= NULL
;
350 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
351 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
354 /* If the default stream is requested, just stat the base file. */
355 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
356 return streams_xattr_stat_base(handle
, smb_fname
, false);
359 /* Populate the stat struct with info from the base file. */
360 if (streams_xattr_stat_base(handle
, smb_fname
, false) == -1) {
364 /* Derive the xattr name to lookup. */
365 status
= streams_xattr_get_name(handle
, talloc_tos(),
366 smb_fname
->stream_name
, &xattr_name
);
367 if (!NT_STATUS_IS_OK(status
)) {
368 errno
= map_errno_from_nt_status(status
);
372 /* Augment the base file's stat information before returning. */
373 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
,
376 if (smb_fname
->st
.st_ex_size
== -1) {
377 SET_STAT_INVALID(smb_fname
->st
);
383 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
384 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
385 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
386 smb_fname
->st
.st_ex_blocks
=
387 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
392 TALLOC_FREE(xattr_name
);
396 static int streams_xattr_open(vfs_handle_struct
*handle
,
397 struct smb_filename
*smb_fname
,
398 files_struct
*fsp
, int flags
, mode_t mode
)
401 struct smb_filename
*smb_fname_base
= NULL
;
402 struct stream_io
*sio
;
404 char *xattr_name
= NULL
;
409 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
410 smb_fname_str_dbg(smb_fname
), flags
));
412 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
413 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
416 /* If the default stream is requested, just open the base file. */
417 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
418 char *tmp_stream_name
;
420 tmp_stream_name
= smb_fname
->stream_name
;
421 smb_fname
->stream_name
= NULL
;
423 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
425 smb_fname
->stream_name
= tmp_stream_name
;
430 status
= streams_xattr_get_name(handle
, talloc_tos(),
431 smb_fname
->stream_name
, &xattr_name
);
432 if (!NT_STATUS_IS_OK(status
)) {
433 errno
= map_errno_from_nt_status(status
);
437 /* Create an smb_filename with stream_name == NULL. */
438 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
439 smb_fname
->base_name
,
443 if (smb_fname_base
== NULL
) {
449 * We use baseflags to turn off nasty side-effects when opening the
453 baseflags
&= ~O_TRUNC
;
454 baseflags
&= ~O_EXCL
;
455 baseflags
&= ~O_CREAT
;
457 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname_base
, fsp
,
460 /* It is legit to open a stream on a directory, but the base
461 * fd has to be read-only.
463 if ((hostfd
== -1) && (errno
== EISDIR
)) {
464 baseflags
&= ~O_ACCMODE
;
465 baseflags
|= O_RDONLY
;
466 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname_base
, fsp
, baseflags
,
470 TALLOC_FREE(smb_fname_base
);
476 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
477 smb_fname
, xattr_name
, &ea
);
479 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
481 if (!NT_STATUS_IS_OK(status
)
482 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
484 * The base file is not there. This is an error even if we got
485 * O_CREAT, the higher levels should have created the base
488 DEBUG(10, ("streams_xattr_open: base file %s not around, "
489 "returning ENOENT\n", smb_fname
->base_name
));
494 if ((!NT_STATUS_IS_OK(status
) && (flags
& O_CREAT
)) ||
497 * The attribute does not exist or needs to be truncated
501 * Darn, xattrs need at least 1 byte
505 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
506 xattr_name
, smb_fname
->base_name
));
508 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
512 flags
& O_EXCL
? XATTR_CREATE
: 0);
518 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
526 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
529 * so->base needs to be a copy of fsp->fsp_name->base_name,
530 * making it identical to streams_xattr_recheck(). If the
531 * open is changing directories, fsp->fsp_name->base_name
532 * will be the full path from the share root, whilst
533 * smb_fname will be relative to the $cwd.
535 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
536 fsp
->fsp_name
->base_name
);
537 sio
->fsp_name_ptr
= fsp
->fsp_name
;
538 sio
->handle
= handle
;
541 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
551 * BUGBUGBUG -- we would need to call fd_close_posix here, but
552 * we don't have a full fsp yet
554 fsp
->fh
->fd
= hostfd
;
555 SMB_VFS_NEXT_CLOSE(handle
, fsp
);
561 static int streams_xattr_unlink(vfs_handle_struct
*handle
,
562 const struct smb_filename
*smb_fname
)
566 char *xattr_name
= NULL
;
568 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
569 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
572 /* If the default stream is requested, just open the base file. */
573 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
574 struct smb_filename
*smb_fname_base
= NULL
;
576 smb_fname_base
= cp_smb_filename(talloc_tos(), smb_fname
);
577 if (smb_fname_base
== NULL
) {
582 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
584 TALLOC_FREE(smb_fname_base
);
588 status
= streams_xattr_get_name(handle
, talloc_tos(),
589 smb_fname
->stream_name
, &xattr_name
);
590 if (!NT_STATUS_IS_OK(status
)) {
591 errno
= map_errno_from_nt_status(status
);
595 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
, xattr_name
);
597 if ((ret
== -1) && (errno
== ENOATTR
)) {
605 TALLOC_FREE(xattr_name
);
609 static int streams_xattr_rename(vfs_handle_struct
*handle
,
610 const struct smb_filename
*smb_fname_src
,
611 const struct smb_filename
*smb_fname_dst
)
615 char *src_xattr_name
= NULL
;
616 char *dst_xattr_name
= NULL
;
617 bool src_is_stream
, dst_is_stream
;
622 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
623 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
625 if (!src_is_stream
&& !dst_is_stream
) {
626 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
630 /* For now don't allow renames from or to the default stream. */
631 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
632 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
637 /* Don't rename if the streams are identical. */
638 if (strcasecmp_m(smb_fname_src
->stream_name
,
639 smb_fname_dst
->stream_name
) == 0) {
643 /* Get the xattr names. */
644 status
= streams_xattr_get_name(handle
, talloc_tos(),
645 smb_fname_src
->stream_name
,
647 if (!NT_STATUS_IS_OK(status
)) {
648 errno
= map_errno_from_nt_status(status
);
651 status
= streams_xattr_get_name(handle
, talloc_tos(),
652 smb_fname_dst
->stream_name
,
654 if (!NT_STATUS_IS_OK(status
)) {
655 errno
= map_errno_from_nt_status(status
);
659 /* read the old stream */
660 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
661 smb_fname_src
, src_xattr_name
, &ea
);
662 if (!NT_STATUS_IS_OK(status
)) {
667 /* (over)write the new stream */
668 nret
= SMB_VFS_SETXATTR(handle
->conn
, smb_fname_src
,
669 dst_xattr_name
, ea
.value
.data
, ea
.value
.length
,
672 if (errno
== ENOATTR
) {
678 /* remove the old stream */
679 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
,
682 if (errno
== ENOATTR
) {
692 TALLOC_FREE(src_xattr_name
);
693 TALLOC_FREE(dst_xattr_name
);
697 static NTSTATUS
walk_xattr_streams(vfs_handle_struct
*handle
,
699 const struct smb_filename
*smb_fname
,
700 bool (*fn
)(struct ea_struct
*ea
,
707 struct streams_xattr_config
*config
;
709 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
710 return NT_STATUS_UNSUCCESSFUL
);
712 status
= get_ea_names_from_file(talloc_tos(),
718 if (!NT_STATUS_IS_OK(status
)) {
722 for (i
=0; i
<num_names
; i
++) {
726 * We want to check with samba_private_attr_name()
727 * whether the xattr name is a private one,
728 * unfortunately it flags xattrs that begin with the
729 * default streams prefix as private.
731 * By only calling samba_private_attr_name() in case
732 * the xattr does NOT begin with the default prefix,
733 * we know that if it returns 'true' it definitely one
734 * of our internal xattr like "user.DOSATTRIB".
736 if (strncasecmp_m(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
737 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
)) != 0) {
738 if (samba_private_attr_name(names
[i
])) {
743 if (strncmp(names
[i
], config
->prefix
,
744 config
->prefix_len
) != 0) {
748 status
= get_ea_value(names
,
754 if (!NT_STATUS_IS_OK(status
)) {
755 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
757 smb_fname
->base_name
,
762 ea
.name
= talloc_asprintf(
763 ea
.value
.data
, ":%s%s",
764 names
[i
] + config
->prefix_len
,
765 config
->store_stream_type
? "" : ":$DATA");
766 if (ea
.name
== NULL
) {
767 DEBUG(0, ("talloc failed\n"));
771 if (!fn(&ea
, private_data
)) {
772 TALLOC_FREE(ea
.value
.data
);
776 TALLOC_FREE(ea
.value
.data
);
783 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
784 struct stream_struct
**streams
,
785 const char *name
, off_t size
,
788 struct stream_struct
*tmp
;
790 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
796 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
797 if (tmp
[*num_streams
].name
== NULL
) {
801 tmp
[*num_streams
].size
= size
;
802 tmp
[*num_streams
].alloc_size
= alloc_size
;
809 struct streaminfo_state
{
811 vfs_handle_struct
*handle
;
812 unsigned int num_streams
;
813 struct stream_struct
*streams
;
817 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
819 struct streaminfo_state
*state
=
820 (struct streaminfo_state
*)private_data
;
822 if (!add_one_stream(state
->mem_ctx
,
823 &state
->num_streams
, &state
->streams
,
824 ea
->name
, ea
->value
.length
-1,
825 smb_roundup(state
->handle
->conn
,
826 ea
->value
.length
-1))) {
827 state
->status
= NT_STATUS_NO_MEMORY
;
834 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
835 struct files_struct
*fsp
,
836 const struct smb_filename
*smb_fname
,
838 unsigned int *pnum_streams
,
839 struct stream_struct
**pstreams
)
841 SMB_STRUCT_STAT sbuf
;
844 struct streaminfo_state state
;
846 ret
= vfs_stat_smb_basename(handle
->conn
, smb_fname
, &sbuf
);
848 return map_nt_error_from_unix(errno
);
851 state
.streams
= *pstreams
;
852 state
.num_streams
= *pnum_streams
;
853 state
.mem_ctx
= mem_ctx
;
854 state
.handle
= handle
;
855 state
.status
= NT_STATUS_OK
;
857 if (S_ISLNK(sbuf
.st_ex_mode
)) {
859 * Currently we do't have SMB_VFS_LLISTXATTR
860 * inside the VFS which means there's no way
861 * to cope with a symlink when lp_posix_pathnames().
862 * returns true. For now ignore links.
863 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
865 status
= NT_STATUS_OK
;
867 status
= walk_xattr_streams(handle
, fsp
, smb_fname
,
868 collect_one_stream
, &state
);
871 if (!NT_STATUS_IS_OK(status
)) {
872 TALLOC_FREE(state
.streams
);
876 if (!NT_STATUS_IS_OK(state
.status
)) {
877 TALLOC_FREE(state
.streams
);
881 *pnum_streams
= state
.num_streams
;
882 *pstreams
= state
.streams
;
884 return SMB_VFS_NEXT_STREAMINFO(handle
,
892 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
,
893 enum timestamp_set_resolution
*p_ts_res
)
895 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
898 static int streams_xattr_connect(vfs_handle_struct
*handle
,
899 const char *service
, const char *user
)
901 struct streams_xattr_config
*config
;
902 const char *default_prefix
= SAMBA_XATTR_DOSSTREAM_PREFIX
;
906 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
911 config
= talloc_zero(handle
->conn
, struct streams_xattr_config
);
912 if (config
== NULL
) {
913 DEBUG(1, ("talloc_zero() failed\n"));
918 prefix
= lp_parm_const_string(SNUM(handle
->conn
),
919 "streams_xattr", "prefix",
921 config
->prefix
= talloc_strdup(config
, prefix
);
922 if (config
->prefix
== NULL
) {
923 DEBUG(1, ("talloc_strdup() failed\n"));
927 config
->prefix_len
= strlen(config
->prefix
);
928 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config
->prefix
));
930 config
->store_stream_type
= lp_parm_bool(SNUM(handle
->conn
),
935 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
936 NULL
, struct stream_xattr_config
,
942 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
943 files_struct
*fsp
, const void *data
,
944 size_t n
, off_t offset
)
946 struct stream_io
*sio
=
947 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
950 struct smb_filename
*smb_fname_base
= NULL
;
953 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
956 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
959 if (!streams_xattr_recheck(sio
)) {
963 /* Create an smb_filename with stream_name == NULL. */
964 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
968 fsp
->fsp_name
->flags
);
969 if (smb_fname_base
== NULL
) {
974 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
975 smb_fname_base
, sio
->xattr_name
, &ea
);
976 if (!NT_STATUS_IS_OK(status
)) {
980 if ((offset
+ n
) > ea
.value
.length
-1) {
983 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8_t,
987 TALLOC_FREE(ea
.value
.data
);
992 ea
.value
.length
= offset
+ n
+ 1;
993 ea
.value
.data
[offset
+n
] = 0;
996 memcpy(ea
.value
.data
+ offset
, data
, n
);
998 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1001 ea
.value
.data
, ea
.value
.length
, 0);
1002 TALLOC_FREE(ea
.value
.data
);
1011 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
1012 files_struct
*fsp
, void *data
,
1013 size_t n
, off_t offset
)
1015 struct stream_io
*sio
=
1016 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1017 struct ea_struct ea
;
1019 size_t length
, overlap
;
1020 struct smb_filename
*smb_fname_base
= NULL
;
1022 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1023 (int)offset
, (int)n
));
1026 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
1029 if (!streams_xattr_recheck(sio
)) {
1033 /* Create an smb_filename with stream_name == NULL. */
1034 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
1038 fsp
->fsp_name
->flags
);
1039 if (smb_fname_base
== NULL
) {
1044 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
1045 smb_fname_base
, sio
->xattr_name
, &ea
);
1046 if (!NT_STATUS_IS_OK(status
)) {
1050 length
= ea
.value
.length
-1;
1052 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1055 /* Attempt to read past EOF. */
1056 if (length
<= offset
) {
1060 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
1061 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
1063 TALLOC_FREE(ea
.value
.data
);
1067 struct streams_xattr_pread_state
{
1069 struct vfs_aio_state vfs_aio_state
;
1072 static void streams_xattr_pread_done(struct tevent_req
*subreq
);
1074 static struct tevent_req
*streams_xattr_pread_send(
1075 struct vfs_handle_struct
*handle
,
1076 TALLOC_CTX
*mem_ctx
,
1077 struct tevent_context
*ev
,
1078 struct files_struct
*fsp
,
1080 size_t n
, off_t offset
)
1082 struct tevent_req
*req
= NULL
;
1083 struct tevent_req
*subreq
= NULL
;
1084 struct streams_xattr_pread_state
*state
= NULL
;
1085 struct stream_io
*sio
=
1086 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1088 req
= tevent_req_create(mem_ctx
, &state
,
1089 struct streams_xattr_pread_state
);
1095 subreq
= SMB_VFS_NEXT_PREAD_SEND(state
, ev
, handle
, fsp
,
1097 if (tevent_req_nomem(req
, subreq
)) {
1098 return tevent_req_post(req
, ev
);
1100 tevent_req_set_callback(subreq
, streams_xattr_pread_done
, req
);
1104 state
->nread
= SMB_VFS_PREAD(fsp
, data
, n
, offset
);
1105 if (state
->nread
!= n
) {
1106 if (state
->nread
!= -1) {
1109 tevent_req_error(req
, errno
);
1110 return tevent_req_post(req
, ev
);
1113 tevent_req_done(req
);
1114 return tevent_req_post(req
, ev
);
1117 static void streams_xattr_pread_done(struct tevent_req
*subreq
)
1119 struct tevent_req
*req
= tevent_req_callback_data(
1120 subreq
, struct tevent_req
);
1121 struct streams_xattr_pread_state
*state
= tevent_req_data(
1122 req
, struct streams_xattr_pread_state
);
1124 state
->nread
= SMB_VFS_PREAD_RECV(subreq
, &state
->vfs_aio_state
);
1125 TALLOC_FREE(subreq
);
1127 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
1130 tevent_req_done(req
);
1133 static ssize_t
streams_xattr_pread_recv(struct tevent_req
*req
,
1134 struct vfs_aio_state
*vfs_aio_state
)
1136 struct streams_xattr_pread_state
*state
= tevent_req_data(
1137 req
, struct streams_xattr_pread_state
);
1139 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
1143 *vfs_aio_state
= state
->vfs_aio_state
;
1144 return state
->nread
;
1147 struct streams_xattr_pwrite_state
{
1149 struct vfs_aio_state vfs_aio_state
;
1152 static void streams_xattr_pwrite_done(struct tevent_req
*subreq
);
1154 static struct tevent_req
*streams_xattr_pwrite_send(
1155 struct vfs_handle_struct
*handle
,
1156 TALLOC_CTX
*mem_ctx
,
1157 struct tevent_context
*ev
,
1158 struct files_struct
*fsp
,
1160 size_t n
, off_t offset
)
1162 struct tevent_req
*req
= NULL
;
1163 struct tevent_req
*subreq
= NULL
;
1164 struct streams_xattr_pwrite_state
*state
= NULL
;
1165 struct stream_io
*sio
=
1166 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1168 req
= tevent_req_create(mem_ctx
, &state
,
1169 struct streams_xattr_pwrite_state
);
1175 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
,
1177 if (tevent_req_nomem(req
, subreq
)) {
1178 return tevent_req_post(req
, ev
);
1180 tevent_req_set_callback(subreq
, streams_xattr_pwrite_done
, req
);
1184 state
->nwritten
= SMB_VFS_PWRITE(fsp
, data
, n
, offset
);
1185 if (state
->nwritten
!= n
) {
1186 if (state
->nwritten
!= -1) {
1189 tevent_req_error(req
, errno
);
1190 return tevent_req_post(req
, ev
);
1193 tevent_req_done(req
);
1194 return tevent_req_post(req
, ev
);
1197 static void streams_xattr_pwrite_done(struct tevent_req
*subreq
)
1199 struct tevent_req
*req
= tevent_req_callback_data(
1200 subreq
, struct tevent_req
);
1201 struct streams_xattr_pwrite_state
*state
= tevent_req_data(
1202 req
, struct streams_xattr_pwrite_state
);
1204 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
1205 TALLOC_FREE(subreq
);
1207 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
1210 tevent_req_done(req
);
1213 static ssize_t
streams_xattr_pwrite_recv(struct tevent_req
*req
,
1214 struct vfs_aio_state
*vfs_aio_state
)
1216 struct streams_xattr_pwrite_state
*state
= tevent_req_data(
1217 req
, struct streams_xattr_pwrite_state
);
1219 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
1223 *vfs_aio_state
= state
->vfs_aio_state
;
1224 return state
->nwritten
;
1227 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
1228 struct files_struct
*fsp
,
1233 struct ea_struct ea
;
1235 struct stream_io
*sio
=
1236 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1237 struct smb_filename
*smb_fname_base
= NULL
;
1239 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1240 fsp_str_dbg(fsp
), (double)offset
));
1243 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
1246 if (!streams_xattr_recheck(sio
)) {
1250 /* Create an smb_filename with stream_name == NULL. */
1251 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
1255 fsp
->fsp_name
->flags
);
1256 if (smb_fname_base
== NULL
) {
1261 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
1262 smb_fname_base
, sio
->xattr_name
, &ea
);
1263 if (!NT_STATUS_IS_OK(status
)) {
1267 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8_t,
1271 TALLOC_FREE(ea
.value
.data
);
1276 /* Did we expand ? */
1277 if (ea
.value
.length
< offset
+ 1) {
1278 memset(&tmp
[ea
.value
.length
], '\0',
1279 offset
+ 1 - ea
.value
.length
);
1282 ea
.value
.data
= tmp
;
1283 ea
.value
.length
= offset
+ 1;
1284 ea
.value
.data
[offset
] = 0;
1286 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1289 ea
.value
.data
, ea
.value
.length
, 0);
1290 TALLOC_FREE(ea
.value
.data
);
1299 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
1300 struct files_struct
*fsp
,
1305 struct stream_io
*sio
=
1306 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1308 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1310 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1313 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1316 if (!streams_xattr_recheck(sio
)) {
1320 /* Let the pwrite code path handle it. */
1326 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1327 .fs_capabilities_fn
= streams_xattr_fs_capabilities
,
1328 .connect_fn
= streams_xattr_connect
,
1329 .open_fn
= streams_xattr_open
,
1330 .stat_fn
= streams_xattr_stat
,
1331 .fstat_fn
= streams_xattr_fstat
,
1332 .lstat_fn
= streams_xattr_lstat
,
1333 .pread_fn
= streams_xattr_pread
,
1334 .pwrite_fn
= streams_xattr_pwrite
,
1335 .pread_send_fn
= streams_xattr_pread_send
,
1336 .pread_recv_fn
= streams_xattr_pread_recv
,
1337 .pwrite_send_fn
= streams_xattr_pwrite_send
,
1338 .pwrite_recv_fn
= streams_xattr_pwrite_recv
,
1339 .unlink_fn
= streams_xattr_unlink
,
1340 .rename_fn
= streams_xattr_rename
,
1341 .ftruncate_fn
= streams_xattr_ftruncate
,
1342 .fallocate_fn
= streams_xattr_fallocate
,
1343 .streaminfo_fn
= streams_xattr_streaminfo
,
1346 NTSTATUS
vfs_streams_xattr_init(TALLOC_CTX
*);
1347 NTSTATUS
vfs_streams_xattr_init(TALLOC_CTX
*ctx
)
1349 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1350 &vfs_streams_xattr_fns
);