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"
30 #define DBGC_CLASS DBGC_VFS
32 struct streams_xattr_config
{
35 bool store_stream_type
;
43 vfs_handle_struct
*handle
;
46 static SMB_INO_T
stream_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
49 unsigned char hash
[16];
53 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
54 (unsigned long)sbuf
->st_ex_dev
,
55 (unsigned long)sbuf
->st_ex_ino
, sname
));
57 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
58 SMB_ASSERT(upper_sname
!= NULL
);
61 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_dev
),
62 sizeof(sbuf
->st_ex_dev
));
63 MD5Update(&ctx
, (const unsigned char *)&(sbuf
->st_ex_ino
),
64 sizeof(sbuf
->st_ex_ino
));
65 MD5Update(&ctx
, (unsigned char *)upper_sname
,
66 talloc_get_size(upper_sname
)-1);
69 TALLOC_FREE(upper_sname
);
71 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
72 memcpy(&result
, hash
, sizeof(result
));
74 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result
));
79 static ssize_t
get_xattr_size(connection_struct
*conn
,
82 const char *xattr_name
)
88 status
= get_ea_value(talloc_tos(), conn
, fsp
, 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
,
110 struct streams_xattr_config
*config
;
112 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
113 return NT_STATUS_UNSUCCESSFUL
);
116 * With vfs_fruit option "fruit:encoding = native" we're
117 * already converting stream names that contain illegal NTFS
118 * characters from their on-the-wire Unicode Private Range
119 * encoding to their native ASCII representation.
121 * As as result the name of xattrs storing the streams (via
122 * vfs_streams_xattr) may contain a colon, so we have to use
123 * strrchr_m() instead of strchr_m() for matching the stream
126 * In check_path_syntax() we've already ensured the streamname
127 * we got from the client is valid.
129 stype
= strrchr_m(stream_name
+ 1, ':');
132 if (strcasecmp_m(stype
, ":$DATA") != 0) {
133 return NT_STATUS_INVALID_PARAMETER
;
137 *xattr_name
= talloc_asprintf(ctx
, "%s%s",
140 if (*xattr_name
== NULL
) {
141 return NT_STATUS_NO_MEMORY
;
145 /* Normalize the stream type to upercase. */
146 if (!strupper_m(strrchr_m(*xattr_name
, ':') + 1)) {
147 return NT_STATUS_INVALID_PARAMETER
;
149 } else if (config
->store_stream_type
) {
151 * Append an explicit stream type if one wasn't
154 *xattr_name
= talloc_asprintf(ctx
, "%s%s",
155 *xattr_name
, ":$DATA");
156 if (*xattr_name
== NULL
) {
157 return NT_STATUS_NO_MEMORY
;
161 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 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp
->fh
->fd
));
237 if (io
== NULL
|| fsp
->base_fsp
== NULL
) {
238 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
241 if (!streams_xattr_recheck(io
)) {
245 /* Create an smb_filename with stream_name == NULL. */
246 smb_fname_base
= synthetic_smb_fname(talloc_tos(), io
->base
,
248 if (smb_fname_base
== NULL
) {
253 if (lp_posix_pathnames()) {
254 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
256 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
258 *sbuf
= smb_fname_base
->st
;
259 TALLOC_FREE(smb_fname_base
);
265 sbuf
->st_ex_size
= get_xattr_size(handle
->conn
, fsp
->base_fsp
,
266 io
->base
, io
->xattr_name
);
267 if (sbuf
->st_ex_size
== -1) {
271 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf
->st_ex_size
));
273 sbuf
->st_ex_ino
= stream_inode(sbuf
, io
->xattr_name
);
274 sbuf
->st_ex_mode
&= ~S_IFMT
;
275 sbuf
->st_ex_mode
|= S_IFREG
;
276 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
281 static int streams_xattr_stat(vfs_handle_struct
*handle
,
282 struct smb_filename
*smb_fname
)
286 char *xattr_name
= NULL
;
288 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
289 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
292 /* Note if lp_posix_paths() is true, we can never
293 * get here as is_ntfs_stream_smb_fname() is
294 * always false. So we never need worry about
295 * not following links here. */
297 /* If the default stream is requested, just stat the base file. */
298 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
299 return streams_xattr_stat_base(handle
, smb_fname
, true);
302 /* Populate the stat struct with info from the base file. */
303 if (streams_xattr_stat_base(handle
, smb_fname
, true) == -1) {
307 /* Derive the xattr name to lookup. */
308 status
= streams_xattr_get_name(handle
, talloc_tos(),
309 smb_fname
->stream_name
, &xattr_name
);
310 if (!NT_STATUS_IS_OK(status
)) {
311 errno
= map_errno_from_nt_status(status
);
315 /* Augment the base file's stat information before returning. */
316 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
317 smb_fname
->base_name
,
319 if (smb_fname
->st
.st_ex_size
== -1) {
325 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
326 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
327 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
328 smb_fname
->st
.st_ex_blocks
=
329 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
333 TALLOC_FREE(xattr_name
);
337 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
338 struct smb_filename
*smb_fname
)
342 char *xattr_name
= NULL
;
344 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
345 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
348 /* If the default stream is requested, just stat the base file. */
349 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
350 return streams_xattr_stat_base(handle
, smb_fname
, false);
353 /* Populate the stat struct with info from the base file. */
354 if (streams_xattr_stat_base(handle
, smb_fname
, false) == -1) {
358 /* Derive the xattr name to lookup. */
359 status
= streams_xattr_get_name(handle
, talloc_tos(),
360 smb_fname
->stream_name
, &xattr_name
);
361 if (!NT_STATUS_IS_OK(status
)) {
362 errno
= map_errno_from_nt_status(status
);
366 /* Augment the base file's stat information before returning. */
367 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
368 smb_fname
->base_name
,
370 if (smb_fname
->st
.st_ex_size
== -1) {
376 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
377 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
378 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
379 smb_fname
->st
.st_ex_blocks
=
380 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
385 TALLOC_FREE(xattr_name
);
389 static int streams_xattr_open(vfs_handle_struct
*handle
,
390 struct smb_filename
*smb_fname
,
391 files_struct
*fsp
, int flags
, mode_t mode
)
394 struct smb_filename
*smb_fname_base
= NULL
;
395 struct stream_io
*sio
;
397 char *xattr_name
= NULL
;
401 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
402 smb_fname_str_dbg(smb_fname
), flags
));
404 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
405 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
408 /* If the default stream is requested, just open the base file. */
409 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
410 char *tmp_stream_name
;
413 tmp_stream_name
= smb_fname
->stream_name
;
414 smb_fname
->stream_name
= NULL
;
416 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
418 smb_fname
->stream_name
= tmp_stream_name
;
423 status
= streams_xattr_get_name(handle
, talloc_tos(),
424 smb_fname
->stream_name
, &xattr_name
);
425 if (!NT_STATUS_IS_OK(status
)) {
426 errno
= map_errno_from_nt_status(status
);
430 /* Create an smb_filename with stream_name == NULL. */
431 smb_fname_base
= synthetic_smb_fname(
432 talloc_tos(), smb_fname
->base_name
, NULL
, NULL
);
433 if (smb_fname_base
== NULL
) {
439 * We use baseflags to turn off nasty side-effects when opening the
443 baseflags
&= ~O_TRUNC
;
444 baseflags
&= ~O_EXCL
;
445 baseflags
&= ~O_CREAT
;
447 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
450 TALLOC_FREE(smb_fname_base
);
452 /* It is legit to open a stream on a directory, but the base
453 * fd has to be read-only.
455 if ((hostfd
== -1) && (errno
== EISDIR
)) {
456 baseflags
&= ~O_ACCMODE
;
457 baseflags
|= O_RDONLY
;
458 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname
, fsp
, baseflags
,
466 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
467 smb_fname
->base_name
, xattr_name
, &ea
);
469 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
471 if (!NT_STATUS_IS_OK(status
)
472 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
474 * The base file is not there. This is an error even if we got
475 * O_CREAT, the higher levels should have created the base
478 DEBUG(10, ("streams_xattr_open: base file %s not around, "
479 "returning ENOENT\n", smb_fname
->base_name
));
484 if ((!NT_STATUS_IS_OK(status
) && (flags
& O_CREAT
)) ||
487 * The attribute does not exist or needs to be truncated
491 * Darn, xattrs need at least 1 byte
495 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
496 xattr_name
, smb_fname
->base_name
));
498 if (fsp
->base_fsp
->fh
->fd
!= -1) {
499 if (SMB_VFS_FSETXATTR(
500 fsp
->base_fsp
, xattr_name
,
502 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
506 if (SMB_VFS_SETXATTR(
507 handle
->conn
, smb_fname
->base_name
,
508 xattr_name
, &null
, sizeof(null
),
509 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
515 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
523 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
525 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
526 smb_fname
->base_name
);
527 sio
->fsp_name_ptr
= fsp
->fsp_name
;
528 sio
->handle
= handle
;
531 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
541 * BUGBUGBUG -- we would need to call fd_close_posix here, but
542 * we don't have a full fsp yet
544 fsp
->fh
->fd
= hostfd
;
551 static int streams_xattr_unlink(vfs_handle_struct
*handle
,
552 const struct smb_filename
*smb_fname
)
556 char *xattr_name
= NULL
;
558 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
559 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
562 /* If the default stream is requested, just open the base file. */
563 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
564 struct smb_filename
*smb_fname_base
= NULL
;
566 smb_fname_base
= cp_smb_filename(talloc_tos(), smb_fname
);
567 if (smb_fname_base
== NULL
) {
572 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
574 TALLOC_FREE(smb_fname_base
);
578 status
= streams_xattr_get_name(handle
, talloc_tos(),
579 smb_fname
->stream_name
, &xattr_name
);
580 if (!NT_STATUS_IS_OK(status
)) {
581 errno
= map_errno_from_nt_status(status
);
585 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
->base_name
, xattr_name
);
587 if ((ret
== -1) && (errno
== ENOATTR
)) {
595 TALLOC_FREE(xattr_name
);
599 static int streams_xattr_rename(vfs_handle_struct
*handle
,
600 const struct smb_filename
*smb_fname_src
,
601 const struct smb_filename
*smb_fname_dst
)
605 char *src_xattr_name
= NULL
;
606 char *dst_xattr_name
= NULL
;
607 bool src_is_stream
, dst_is_stream
;
612 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
613 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
615 if (!src_is_stream
&& !dst_is_stream
) {
616 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
620 /* For now don't allow renames from or to the default stream. */
621 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
622 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
627 /* Don't rename if the streams are identical. */
628 if (strcasecmp_m(smb_fname_src
->stream_name
,
629 smb_fname_dst
->stream_name
) == 0) {
633 /* Get the xattr names. */
634 status
= streams_xattr_get_name(handle
, talloc_tos(),
635 smb_fname_src
->stream_name
,
637 if (!NT_STATUS_IS_OK(status
)) {
638 errno
= map_errno_from_nt_status(status
);
641 status
= streams_xattr_get_name(handle
, talloc_tos(),
642 smb_fname_dst
->stream_name
,
644 if (!NT_STATUS_IS_OK(status
)) {
645 errno
= map_errno_from_nt_status(status
);
649 /* read the old stream */
650 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
651 smb_fname_src
->base_name
, src_xattr_name
, &ea
);
652 if (!NT_STATUS_IS_OK(status
)) {
657 /* (over)write the new stream */
658 nret
= SMB_VFS_SETXATTR(handle
->conn
, smb_fname_src
->base_name
,
659 dst_xattr_name
, ea
.value
.data
, ea
.value
.length
,
662 if (errno
== ENOATTR
) {
668 /* remove the old stream */
669 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
->base_name
,
672 if (errno
== ENOATTR
) {
682 TALLOC_FREE(src_xattr_name
);
683 TALLOC_FREE(dst_xattr_name
);
687 static NTSTATUS
walk_xattr_streams(vfs_handle_struct
*handle
, files_struct
*fsp
,
689 bool (*fn
)(struct ea_struct
*ea
,
696 struct streams_xattr_config
*config
;
698 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
699 return NT_STATUS_UNSUCCESSFUL
);
701 status
= get_ea_names_from_file(talloc_tos(), handle
->conn
, fsp
, fname
,
703 if (!NT_STATUS_IS_OK(status
)) {
707 for (i
=0; i
<num_names
; i
++) {
711 * We want to check with samba_private_attr_name()
712 * whether the xattr name is a private one,
713 * unfortunately it flags xattrs that begin with the
714 * default streams prefix as private.
716 * By only calling samba_private_attr_name() in case
717 * the xattr does NOT begin with the default prefix,
718 * we know that if it returns 'true' it definitely one
719 * of our internal xattr like "user.DOSATTRIB".
721 if (strncasecmp_m(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
722 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
)) != 0) {
723 if (samba_private_attr_name(names
[i
])) {
728 if (strncmp(names
[i
], config
->prefix
,
729 config
->prefix_len
) != 0) {
733 status
= get_ea_value(names
, handle
->conn
, fsp
, fname
,
735 if (!NT_STATUS_IS_OK(status
)) {
736 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
737 names
[i
], fname
, nt_errstr(status
)));
741 ea
.name
= talloc_asprintf(
742 ea
.value
.data
, ":%s%s",
743 names
[i
] + config
->prefix_len
,
744 config
->store_stream_type
? "" : ":$DATA");
745 if (ea
.name
== NULL
) {
746 DEBUG(0, ("talloc failed\n"));
750 if (!fn(&ea
, private_data
)) {
751 TALLOC_FREE(ea
.value
.data
);
755 TALLOC_FREE(ea
.value
.data
);
762 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
763 struct stream_struct
**streams
,
764 const char *name
, off_t size
,
767 struct stream_struct
*tmp
;
769 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
775 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
776 if (tmp
[*num_streams
].name
== NULL
) {
780 tmp
[*num_streams
].size
= size
;
781 tmp
[*num_streams
].alloc_size
= alloc_size
;
788 struct streaminfo_state
{
790 vfs_handle_struct
*handle
;
791 unsigned int num_streams
;
792 struct stream_struct
*streams
;
796 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
798 struct streaminfo_state
*state
=
799 (struct streaminfo_state
*)private_data
;
801 if (!add_one_stream(state
->mem_ctx
,
802 &state
->num_streams
, &state
->streams
,
803 ea
->name
, ea
->value
.length
-1,
804 smb_roundup(state
->handle
->conn
,
805 ea
->value
.length
-1))) {
806 state
->status
= NT_STATUS_NO_MEMORY
;
813 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
814 struct files_struct
*fsp
,
817 unsigned int *pnum_streams
,
818 struct stream_struct
**pstreams
)
820 SMB_STRUCT_STAT sbuf
;
823 struct streaminfo_state state
;
825 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
826 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
829 struct smb_filename
*smb_fname
= NULL
;
830 smb_fname
= synthetic_smb_fname(talloc_tos(), fname
, NULL
,
832 if (smb_fname
== NULL
) {
833 return NT_STATUS_NO_MEMORY
;
835 if (lp_posix_pathnames()) {
836 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname
);
838 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
840 sbuf
= smb_fname
->st
;
841 TALLOC_FREE(smb_fname
);
845 return map_nt_error_from_unix(errno
);
848 state
.streams
= *pstreams
;
849 state
.num_streams
= *pnum_streams
;
850 state
.mem_ctx
= mem_ctx
;
851 state
.handle
= handle
;
852 state
.status
= NT_STATUS_OK
;
854 if (S_ISLNK(sbuf
.st_ex_mode
)) {
856 * Currently we do't have SMB_VFS_LLISTXATTR
857 * inside the VFS which means there's no way
858 * to cope with a symlink when lp_posix_pathnames().
859 * returns true. For now ignore links.
860 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
862 status
= NT_STATUS_OK
;
864 status
= walk_xattr_streams(handle
, fsp
, fname
,
865 collect_one_stream
, &state
);
868 if (!NT_STATUS_IS_OK(status
)) {
869 TALLOC_FREE(state
.streams
);
873 if (!NT_STATUS_IS_OK(state
.status
)) {
874 TALLOC_FREE(state
.streams
);
878 *pnum_streams
= state
.num_streams
;
879 *pstreams
= state
.streams
;
881 return SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, fname
, mem_ctx
, pnum_streams
, pstreams
);
884 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
,
885 enum timestamp_set_resolution
*p_ts_res
)
887 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
890 static int streams_xattr_connect(vfs_handle_struct
*handle
,
891 const char *service
, const char *user
)
893 struct streams_xattr_config
*config
;
894 const char *default_prefix
= SAMBA_XATTR_DOSSTREAM_PREFIX
;
898 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
903 config
= talloc_zero(handle
->conn
, struct streams_xattr_config
);
904 if (config
== NULL
) {
905 DEBUG(1, ("talloc_zero() failed\n"));
910 prefix
= lp_parm_const_string(SNUM(handle
->conn
),
911 "streams_xattr", "prefix",
913 config
->prefix
= talloc_strdup(config
, prefix
);
914 if (config
->prefix
== NULL
) {
915 DEBUG(1, ("talloc_strdup() failed\n"));
919 config
->prefix_len
= strlen(config
->prefix
);
920 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config
->prefix
));
922 config
->store_stream_type
= lp_parm_bool(SNUM(handle
->conn
),
927 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
928 NULL
, struct stream_xattr_config
,
934 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
935 files_struct
*fsp
, const void *data
,
936 size_t n
, off_t offset
)
938 struct stream_io
*sio
=
939 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
944 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
947 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
950 if (!streams_xattr_recheck(sio
)) {
954 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
955 sio
->base
, sio
->xattr_name
, &ea
);
956 if (!NT_STATUS_IS_OK(status
)) {
960 if ((offset
+ n
) > ea
.value
.length
-1) {
963 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8_t,
967 TALLOC_FREE(ea
.value
.data
);
972 ea
.value
.length
= offset
+ n
+ 1;
973 ea
.value
.data
[offset
+n
] = 0;
976 memcpy(ea
.value
.data
+ offset
, data
, n
);
978 if (fsp
->base_fsp
->fh
->fd
!= -1) {
979 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
981 ea
.value
.data
, ea
.value
.length
, 0);
983 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
984 fsp
->base_fsp
->fsp_name
->base_name
,
986 ea
.value
.data
, ea
.value
.length
, 0);
988 TALLOC_FREE(ea
.value
.data
);
997 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
998 files_struct
*fsp
, void *data
,
999 size_t n
, off_t offset
)
1001 struct stream_io
*sio
=
1002 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1003 struct ea_struct ea
;
1005 size_t length
, overlap
;
1007 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1008 (int)offset
, (int)n
));
1011 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
1014 if (!streams_xattr_recheck(sio
)) {
1018 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
1019 sio
->base
, sio
->xattr_name
, &ea
);
1020 if (!NT_STATUS_IS_OK(status
)) {
1024 length
= ea
.value
.length
-1;
1026 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1029 /* Attempt to read past EOF. */
1030 if (length
<= offset
) {
1034 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
1035 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
1037 TALLOC_FREE(ea
.value
.data
);
1041 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
1042 struct files_struct
*fsp
,
1047 struct ea_struct ea
;
1049 struct stream_io
*sio
=
1050 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1052 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1053 fsp_str_dbg(fsp
), (double)offset
));
1056 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
1059 if (!streams_xattr_recheck(sio
)) {
1063 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
1064 sio
->base
, sio
->xattr_name
, &ea
);
1065 if (!NT_STATUS_IS_OK(status
)) {
1069 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8_t,
1073 TALLOC_FREE(ea
.value
.data
);
1078 /* Did we expand ? */
1079 if (ea
.value
.length
< offset
+ 1) {
1080 memset(&tmp
[ea
.value
.length
], '\0',
1081 offset
+ 1 - ea
.value
.length
);
1084 ea
.value
.data
= tmp
;
1085 ea
.value
.length
= offset
+ 1;
1086 ea
.value
.data
[offset
] = 0;
1088 if (fsp
->base_fsp
->fh
->fd
!= -1) {
1089 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
1091 ea
.value
.data
, ea
.value
.length
, 0);
1093 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1094 fsp
->base_fsp
->fsp_name
->base_name
,
1096 ea
.value
.data
, ea
.value
.length
, 0);
1099 TALLOC_FREE(ea
.value
.data
);
1108 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
1109 struct files_struct
*fsp
,
1114 struct stream_io
*sio
=
1115 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1117 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1119 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1122 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1125 if (!streams_xattr_recheck(sio
)) {
1129 /* Let the pwrite code path handle it. */
1135 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1136 .fs_capabilities_fn
= streams_xattr_fs_capabilities
,
1137 .connect_fn
= streams_xattr_connect
,
1138 .open_fn
= streams_xattr_open
,
1139 .stat_fn
= streams_xattr_stat
,
1140 .fstat_fn
= streams_xattr_fstat
,
1141 .lstat_fn
= streams_xattr_lstat
,
1142 .pread_fn
= streams_xattr_pread
,
1143 .pwrite_fn
= streams_xattr_pwrite
,
1144 .unlink_fn
= streams_xattr_unlink
,
1145 .rename_fn
= streams_xattr_rename
,
1146 .ftruncate_fn
= streams_xattr_ftruncate
,
1147 .fallocate_fn
= streams_xattr_fallocate
,
1148 .streaminfo_fn
= streams_xattr_streaminfo
,
1151 NTSTATUS
vfs_streams_xattr_init(void);
1152 NTSTATUS
vfs_streams_xattr_init(void)
1154 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1155 &vfs_streams_xattr_fns
);