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
);
115 stype
= strchr_m(stream_name
+ 1, ':');
118 if (strcasecmp_m(stype
, ":$DATA") != 0) {
119 return NT_STATUS_INVALID_PARAMETER
;
123 *xattr_name
= talloc_asprintf(ctx
, "%s%s",
126 if (*xattr_name
== NULL
) {
127 return NT_STATUS_NO_MEMORY
;
131 /* Normalize the stream type to upercase. */
132 if (!strupper_m(strrchr_m(*xattr_name
, ':') + 1)) {
133 return NT_STATUS_INVALID_PARAMETER
;
135 } else if (config
->store_stream_type
) {
137 * Append an explicit stream type if one wasn't
140 *xattr_name
= talloc_asprintf(ctx
, "%s%s",
141 *xattr_name
, ":$DATA");
142 if (*xattr_name
== NULL
) {
143 return NT_STATUS_NO_MEMORY
;
147 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name
,
153 static bool streams_xattr_recheck(struct stream_io
*sio
)
156 char *xattr_name
= NULL
;
158 if (sio
->fsp
->fsp_name
== sio
->fsp_name_ptr
) {
162 if (sio
->fsp
->fsp_name
->stream_name
== NULL
) {
163 /* how can this happen */
168 status
= streams_xattr_get_name(sio
->handle
, talloc_tos(),
169 sio
->fsp
->fsp_name
->stream_name
,
171 if (!NT_STATUS_IS_OK(status
)) {
175 TALLOC_FREE(sio
->xattr_name
);
176 TALLOC_FREE(sio
->base
);
177 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
179 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
180 sio
->fsp
->fsp_name
->base_name
);
181 sio
->fsp_name_ptr
= sio
->fsp
->fsp_name
;
183 TALLOC_FREE(xattr_name
);
185 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
193 * Helper to stat/lstat the base file of an smb_fname.
195 static int streams_xattr_stat_base(vfs_handle_struct
*handle
,
196 struct smb_filename
*smb_fname
,
199 char *tmp_stream_name
;
202 tmp_stream_name
= smb_fname
->stream_name
;
203 smb_fname
->stream_name
= NULL
;
205 result
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
207 result
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
209 smb_fname
->stream_name
= tmp_stream_name
;
213 static int streams_xattr_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
214 SMB_STRUCT_STAT
*sbuf
)
216 struct smb_filename
*smb_fname_base
= NULL
;
218 struct stream_io
*io
= (struct stream_io
*)
219 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
221 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp
->fh
->fd
));
223 if (io
== NULL
|| fsp
->base_fsp
== NULL
) {
224 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
227 if (!streams_xattr_recheck(io
)) {
231 /* Create an smb_filename with stream_name == NULL. */
232 smb_fname_base
= synthetic_smb_fname(talloc_tos(), io
->base
,
234 if (smb_fname_base
== NULL
) {
239 if (lp_posix_pathnames()) {
240 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
242 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
244 *sbuf
= smb_fname_base
->st
;
245 TALLOC_FREE(smb_fname_base
);
251 sbuf
->st_ex_size
= get_xattr_size(handle
->conn
, fsp
->base_fsp
,
252 io
->base
, io
->xattr_name
);
253 if (sbuf
->st_ex_size
== -1) {
257 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf
->st_ex_size
));
259 sbuf
->st_ex_ino
= stream_inode(sbuf
, io
->xattr_name
);
260 sbuf
->st_ex_mode
&= ~S_IFMT
;
261 sbuf
->st_ex_mode
|= S_IFREG
;
262 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
267 static int streams_xattr_stat(vfs_handle_struct
*handle
,
268 struct smb_filename
*smb_fname
)
272 char *xattr_name
= NULL
;
274 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
275 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
278 /* Note if lp_posix_paths() is true, we can never
279 * get here as is_ntfs_stream_smb_fname() is
280 * always false. So we never need worry about
281 * not following links here. */
283 /* If the default stream is requested, just stat the base file. */
284 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
285 return streams_xattr_stat_base(handle
, smb_fname
, true);
288 /* Populate the stat struct with info from the base file. */
289 if (streams_xattr_stat_base(handle
, smb_fname
, true) == -1) {
293 /* Derive the xattr name to lookup. */
294 status
= streams_xattr_get_name(handle
, talloc_tos(),
295 smb_fname
->stream_name
, &xattr_name
);
296 if (!NT_STATUS_IS_OK(status
)) {
297 errno
= map_errno_from_nt_status(status
);
301 /* Augment the base file's stat information before returning. */
302 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
303 smb_fname
->base_name
,
305 if (smb_fname
->st
.st_ex_size
== -1) {
311 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
312 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
313 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
314 smb_fname
->st
.st_ex_blocks
=
315 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
319 TALLOC_FREE(xattr_name
);
323 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
324 struct smb_filename
*smb_fname
)
328 char *xattr_name
= NULL
;
330 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
331 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
334 /* If the default stream is requested, just stat the base file. */
335 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
336 return streams_xattr_stat_base(handle
, smb_fname
, false);
339 /* Populate the stat struct with info from the base file. */
340 if (streams_xattr_stat_base(handle
, smb_fname
, false) == -1) {
344 /* Derive the xattr name to lookup. */
345 status
= streams_xattr_get_name(handle
, talloc_tos(),
346 smb_fname
->stream_name
, &xattr_name
);
347 if (!NT_STATUS_IS_OK(status
)) {
348 errno
= map_errno_from_nt_status(status
);
352 /* Augment the base file's stat information before returning. */
353 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
354 smb_fname
->base_name
,
356 if (smb_fname
->st
.st_ex_size
== -1) {
362 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
363 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
364 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
365 smb_fname
->st
.st_ex_blocks
=
366 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
371 TALLOC_FREE(xattr_name
);
375 static int streams_xattr_open(vfs_handle_struct
*handle
,
376 struct smb_filename
*smb_fname
,
377 files_struct
*fsp
, int flags
, mode_t mode
)
380 struct smb_filename
*smb_fname_base
= NULL
;
381 struct stream_io
*sio
;
383 char *xattr_name
= NULL
;
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_ntfs_stream_smb_fname(smb_fname
)) {
391 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
394 /* If the default stream is requested, just open the base file. */
395 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
396 char *tmp_stream_name
;
399 tmp_stream_name
= smb_fname
->stream_name
;
400 smb_fname
->stream_name
= NULL
;
402 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
404 smb_fname
->stream_name
= tmp_stream_name
;
409 status
= streams_xattr_get_name(handle
, talloc_tos(),
410 smb_fname
->stream_name
, &xattr_name
);
411 if (!NT_STATUS_IS_OK(status
)) {
412 errno
= map_errno_from_nt_status(status
);
416 /* Create an smb_filename with stream_name == NULL. */
417 smb_fname_base
= synthetic_smb_fname(
418 talloc_tos(), smb_fname
->base_name
, NULL
, NULL
);
419 if (smb_fname_base
== NULL
) {
425 * We use baseflags to turn off nasty side-effects when opening the
429 baseflags
&= ~O_TRUNC
;
430 baseflags
&= ~O_EXCL
;
431 baseflags
&= ~O_CREAT
;
433 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
436 TALLOC_FREE(smb_fname_base
);
438 /* It is legit to open a stream on a directory, but the base
439 * fd has to be read-only.
441 if ((hostfd
== -1) && (errno
== EISDIR
)) {
442 baseflags
&= ~O_ACCMODE
;
443 baseflags
|= O_RDONLY
;
444 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname
, fsp
, baseflags
,
452 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
453 smb_fname
->base_name
, xattr_name
, &ea
);
455 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
457 if (!NT_STATUS_IS_OK(status
)
458 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
460 * The base file is not there. This is an error even if we got
461 * O_CREAT, the higher levels should have created the base
464 DEBUG(10, ("streams_xattr_open: base file %s not around, "
465 "returning ENOENT\n", smb_fname
->base_name
));
470 if ((!NT_STATUS_IS_OK(status
) && (flags
& O_CREAT
)) ||
473 * The attribute does not exist or needs to be truncated
477 * Darn, xattrs need at least 1 byte
481 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
482 xattr_name
, smb_fname
->base_name
));
484 if (fsp
->base_fsp
->fh
->fd
!= -1) {
485 if (SMB_VFS_FSETXATTR(
486 fsp
->base_fsp
, xattr_name
,
488 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
492 if (SMB_VFS_SETXATTR(
493 handle
->conn
, smb_fname
->base_name
,
494 xattr_name
, &null
, sizeof(null
),
495 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
501 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
509 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
511 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
512 smb_fname
->base_name
);
513 sio
->fsp_name_ptr
= fsp
->fsp_name
;
514 sio
->handle
= handle
;
517 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
527 * BUGBUGBUG -- we would need to call fd_close_posix here, but
528 * we don't have a full fsp yet
530 fsp
->fh
->fd
= hostfd
;
537 static int streams_xattr_unlink(vfs_handle_struct
*handle
,
538 const struct smb_filename
*smb_fname
)
542 char *xattr_name
= NULL
;
544 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
545 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
548 /* If the default stream is requested, just open the base file. */
549 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
550 struct smb_filename
*smb_fname_base
= NULL
;
552 smb_fname_base
= cp_smb_filename(talloc_tos(), smb_fname
);
553 if (smb_fname_base
== NULL
) {
558 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
560 TALLOC_FREE(smb_fname_base
);
564 status
= streams_xattr_get_name(handle
, talloc_tos(),
565 smb_fname
->stream_name
, &xattr_name
);
566 if (!NT_STATUS_IS_OK(status
)) {
567 errno
= map_errno_from_nt_status(status
);
571 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
->base_name
, xattr_name
);
573 if ((ret
== -1) && (errno
== ENOATTR
)) {
581 TALLOC_FREE(xattr_name
);
585 static int streams_xattr_rename(vfs_handle_struct
*handle
,
586 const struct smb_filename
*smb_fname_src
,
587 const struct smb_filename
*smb_fname_dst
)
591 char *src_xattr_name
= NULL
;
592 char *dst_xattr_name
= NULL
;
593 bool src_is_stream
, dst_is_stream
;
598 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
599 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
601 if (!src_is_stream
&& !dst_is_stream
) {
602 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
606 /* For now don't allow renames from or to the default stream. */
607 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
608 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
613 /* Don't rename if the streams are identical. */
614 if (strcasecmp_m(smb_fname_src
->stream_name
,
615 smb_fname_dst
->stream_name
) == 0) {
619 /* Get the xattr names. */
620 status
= streams_xattr_get_name(handle
, talloc_tos(),
621 smb_fname_src
->stream_name
,
623 if (!NT_STATUS_IS_OK(status
)) {
624 errno
= map_errno_from_nt_status(status
);
627 status
= streams_xattr_get_name(handle
, talloc_tos(),
628 smb_fname_dst
->stream_name
,
630 if (!NT_STATUS_IS_OK(status
)) {
631 errno
= map_errno_from_nt_status(status
);
635 /* read the old stream */
636 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
637 smb_fname_src
->base_name
, src_xattr_name
, &ea
);
638 if (!NT_STATUS_IS_OK(status
)) {
643 /* (over)write the new stream */
644 nret
= SMB_VFS_SETXATTR(handle
->conn
, smb_fname_src
->base_name
,
645 dst_xattr_name
, ea
.value
.data
, ea
.value
.length
,
648 if (errno
== ENOATTR
) {
654 /* remove the old stream */
655 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
->base_name
,
658 if (errno
== ENOATTR
) {
668 TALLOC_FREE(src_xattr_name
);
669 TALLOC_FREE(dst_xattr_name
);
673 static NTSTATUS
walk_xattr_streams(vfs_handle_struct
*handle
, files_struct
*fsp
,
675 bool (*fn
)(struct ea_struct
*ea
,
682 struct streams_xattr_config
*config
;
684 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct streams_xattr_config
,
685 return NT_STATUS_UNSUCCESSFUL
);
687 status
= get_ea_names_from_file(talloc_tos(), handle
->conn
, fsp
, fname
,
689 if (!NT_STATUS_IS_OK(status
)) {
693 for (i
=0; i
<num_names
; i
++) {
697 * We want to check with samba_private_attr_name()
698 * whether the xattr name is a private one,
699 * unfortunately it flags xattrs that begin with the
700 * default streams prefix as private.
702 * By only calling samba_private_attr_name() in case
703 * the xattr does NOT begin with the default prefix,
704 * we know that if it returns 'true' it definitely one
705 * of our internal xattr like "user.DOSATTRIB".
707 if (strncasecmp_m(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
708 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
)) != 0) {
709 if (samba_private_attr_name(names
[i
])) {
714 if (strncmp(names
[i
], config
->prefix
,
715 config
->prefix_len
) != 0) {
719 status
= get_ea_value(names
, handle
->conn
, fsp
, fname
,
721 if (!NT_STATUS_IS_OK(status
)) {
722 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
723 names
[i
], fname
, nt_errstr(status
)));
727 ea
.name
= talloc_asprintf(
728 ea
.value
.data
, ":%s%s",
729 names
[i
] + config
->prefix_len
,
730 config
->store_stream_type
? "" : ":$DATA");
731 if (ea
.name
== NULL
) {
732 DEBUG(0, ("talloc failed\n"));
736 if (!fn(&ea
, private_data
)) {
737 TALLOC_FREE(ea
.value
.data
);
741 TALLOC_FREE(ea
.value
.data
);
748 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
749 struct stream_struct
**streams
,
750 const char *name
, off_t size
,
753 struct stream_struct
*tmp
;
755 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
761 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
762 if (tmp
[*num_streams
].name
== NULL
) {
766 tmp
[*num_streams
].size
= size
;
767 tmp
[*num_streams
].alloc_size
= alloc_size
;
774 struct streaminfo_state
{
776 vfs_handle_struct
*handle
;
777 unsigned int num_streams
;
778 struct stream_struct
*streams
;
782 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
784 struct streaminfo_state
*state
=
785 (struct streaminfo_state
*)private_data
;
787 if (!add_one_stream(state
->mem_ctx
,
788 &state
->num_streams
, &state
->streams
,
789 ea
->name
, ea
->value
.length
-1,
790 smb_roundup(state
->handle
->conn
,
791 ea
->value
.length
-1))) {
792 state
->status
= NT_STATUS_NO_MEMORY
;
799 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
800 struct files_struct
*fsp
,
803 unsigned int *pnum_streams
,
804 struct stream_struct
**pstreams
)
806 SMB_STRUCT_STAT sbuf
;
809 struct streaminfo_state state
;
811 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
812 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
815 struct smb_filename
*smb_fname
= NULL
;
816 smb_fname
= synthetic_smb_fname(talloc_tos(), fname
, NULL
,
818 if (smb_fname
== NULL
) {
819 return NT_STATUS_NO_MEMORY
;
821 if (lp_posix_pathnames()) {
822 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname
);
824 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
826 sbuf
= smb_fname
->st
;
827 TALLOC_FREE(smb_fname
);
831 return map_nt_error_from_unix(errno
);
834 state
.streams
= *pstreams
;
835 state
.num_streams
= *pnum_streams
;
836 state
.mem_ctx
= mem_ctx
;
837 state
.handle
= handle
;
838 state
.status
= NT_STATUS_OK
;
840 if (S_ISLNK(sbuf
.st_ex_mode
)) {
842 * Currently we do't have SMB_VFS_LLISTXATTR
843 * inside the VFS which means there's no way
844 * to cope with a symlink when lp_posix_pathnames().
845 * returns true. For now ignore links.
846 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
848 status
= NT_STATUS_OK
;
850 status
= walk_xattr_streams(handle
, fsp
, fname
,
851 collect_one_stream
, &state
);
854 if (!NT_STATUS_IS_OK(status
)) {
855 TALLOC_FREE(state
.streams
);
859 if (!NT_STATUS_IS_OK(state
.status
)) {
860 TALLOC_FREE(state
.streams
);
864 *pnum_streams
= state
.num_streams
;
865 *pstreams
= state
.streams
;
867 return SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, fname
, mem_ctx
, pnum_streams
, pstreams
);
870 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
,
871 enum timestamp_set_resolution
*p_ts_res
)
873 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
876 static int streams_xattr_connect(vfs_handle_struct
*handle
,
877 const char *service
, const char *user
)
879 struct streams_xattr_config
*config
;
880 const char *default_prefix
= SAMBA_XATTR_DOSSTREAM_PREFIX
;
884 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
889 config
= talloc_zero(handle
->conn
, struct streams_xattr_config
);
890 if (config
== NULL
) {
891 DEBUG(1, ("talloc_zero() failed\n"));
896 prefix
= lp_parm_const_string(SNUM(handle
->conn
),
897 "streams_xattr", "prefix",
899 config
->prefix
= talloc_strdup(config
, prefix
);
900 if (config
->prefix
== NULL
) {
901 DEBUG(1, ("talloc_strdup() failed\n"));
905 config
->prefix_len
= strlen(config
->prefix
);
906 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config
->prefix
));
908 config
->store_stream_type
= lp_parm_bool(SNUM(handle
->conn
),
913 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
914 NULL
, struct stream_xattr_config
,
920 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
921 files_struct
*fsp
, const void *data
,
922 size_t n
, off_t offset
)
924 struct stream_io
*sio
=
925 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
930 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
933 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
936 if (!streams_xattr_recheck(sio
)) {
940 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
941 sio
->base
, sio
->xattr_name
, &ea
);
942 if (!NT_STATUS_IS_OK(status
)) {
946 if ((offset
+ n
) > ea
.value
.length
-1) {
949 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8
,
953 TALLOC_FREE(ea
.value
.data
);
958 ea
.value
.length
= offset
+ n
+ 1;
959 ea
.value
.data
[offset
+n
] = 0;
962 memcpy(ea
.value
.data
+ offset
, data
, n
);
964 if (fsp
->base_fsp
->fh
->fd
!= -1) {
965 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
967 ea
.value
.data
, ea
.value
.length
, 0);
969 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
970 fsp
->base_fsp
->fsp_name
->base_name
,
972 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
;
993 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
994 (int)offset
, (int)n
));
997 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
1000 if (!streams_xattr_recheck(sio
)) {
1004 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
1005 sio
->base
, sio
->xattr_name
, &ea
);
1006 if (!NT_STATUS_IS_OK(status
)) {
1010 length
= ea
.value
.length
-1;
1012 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1015 /* Attempt to read past EOF. */
1016 if (length
<= offset
) {
1020 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
1021 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
1023 TALLOC_FREE(ea
.value
.data
);
1027 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
1028 struct files_struct
*fsp
,
1033 struct ea_struct ea
;
1035 struct stream_io
*sio
=
1036 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1038 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1039 fsp_str_dbg(fsp
), (double)offset
));
1042 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
1045 if (!streams_xattr_recheck(sio
)) {
1049 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
1050 sio
->base
, sio
->xattr_name
, &ea
);
1051 if (!NT_STATUS_IS_OK(status
)) {
1055 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8
,
1059 TALLOC_FREE(ea
.value
.data
);
1064 /* Did we expand ? */
1065 if (ea
.value
.length
< offset
+ 1) {
1066 memset(&tmp
[ea
.value
.length
], '\0',
1067 offset
+ 1 - ea
.value
.length
);
1070 ea
.value
.data
= tmp
;
1071 ea
.value
.length
= offset
+ 1;
1072 ea
.value
.data
[offset
] = 0;
1074 if (fsp
->base_fsp
->fh
->fd
!= -1) {
1075 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
1077 ea
.value
.data
, ea
.value
.length
, 0);
1079 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1080 fsp
->base_fsp
->fsp_name
->base_name
,
1082 ea
.value
.data
, ea
.value
.length
, 0);
1085 TALLOC_FREE(ea
.value
.data
);
1094 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
1095 struct files_struct
*fsp
,
1096 enum vfs_fallocate_mode mode
,
1100 struct stream_io
*sio
=
1101 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1103 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1105 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1108 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1111 if (!streams_xattr_recheck(sio
)) {
1115 /* Let the pwrite code path handle it. */
1121 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1122 .fs_capabilities_fn
= streams_xattr_fs_capabilities
,
1123 .connect_fn
= streams_xattr_connect
,
1124 .open_fn
= streams_xattr_open
,
1125 .stat_fn
= streams_xattr_stat
,
1126 .fstat_fn
= streams_xattr_fstat
,
1127 .lstat_fn
= streams_xattr_lstat
,
1128 .pread_fn
= streams_xattr_pread
,
1129 .pwrite_fn
= streams_xattr_pwrite
,
1130 .unlink_fn
= streams_xattr_unlink
,
1131 .rename_fn
= streams_xattr_rename
,
1132 .ftruncate_fn
= streams_xattr_ftruncate
,
1133 .fallocate_fn
= streams_xattr_fallocate
,
1134 .streaminfo_fn
= streams_xattr_streaminfo
,
1137 NTSTATUS
vfs_streams_xattr_init(void);
1138 NTSTATUS
vfs_streams_xattr_init(void)
1140 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1141 &vfs_streams_xattr_fns
);