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/>.
27 #define DBGC_CLASS DBGC_VFS
34 static SMB_INO_T
stream_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
36 struct MD5Context ctx
;
37 unsigned char hash
[16];
41 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
42 (unsigned long)sbuf
->st_dev
,
43 (unsigned long)sbuf
->st_ino
, sname
));
45 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
46 SMB_ASSERT(upper_sname
!= NULL
);
49 MD5Update(&ctx
, (unsigned char *)&(sbuf
->st_dev
),
50 sizeof(sbuf
->st_dev
));
51 MD5Update(&ctx
, (unsigned char *)&(sbuf
->st_ino
),
52 sizeof(sbuf
->st_ino
));
53 MD5Update(&ctx
, (unsigned char *)upper_sname
,
54 talloc_get_size(upper_sname
)-1);
57 TALLOC_FREE(upper_sname
);
59 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
60 memcpy(&result
, hash
, sizeof(result
));
62 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result
));
67 static ssize_t
get_xattr_size(connection_struct
*conn
,
70 const char *xattr_name
)
76 status
= get_ea_value(talloc_tos(), conn
, fsp
, fname
,
79 if (!NT_STATUS_IS_OK(status
)) {
83 result
= ea
.value
.length
-1;
84 TALLOC_FREE(ea
.value
.data
);
89 static int streams_xattr_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
90 SMB_STRUCT_STAT
*sbuf
)
92 struct stream_io
*io
= (struct stream_io
*)
93 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
95 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp
->fh
->fd
));
97 if (io
== NULL
|| fsp
->base_fsp
== NULL
) {
98 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
101 if (SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
) == -1) {
105 sbuf
->st_size
= get_xattr_size(handle
->conn
, fsp
->base_fsp
,
106 io
->base
, io
->xattr_name
);
107 if (sbuf
->st_size
== -1) {
111 DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf
->st_size
));
113 sbuf
->st_ino
= stream_inode(sbuf
, io
->xattr_name
);
114 sbuf
->st_mode
&= ~S_IFMT
;
115 sbuf
->st_mode
|= S_IFREG
;
116 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
121 static int streams_xattr_stat(vfs_handle_struct
*handle
, const char *fname
,
122 SMB_STRUCT_STAT
*sbuf
)
125 char *base
= NULL
, *sname
= NULL
;
129 if (!is_ntfs_stream_name(fname
)) {
130 return SMB_VFS_NEXT_STAT(handle
, fname
, sbuf
);
133 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
134 if (!NT_STATUS_IS_OK(status
)) {
140 return SMB_VFS_NEXT_STAT(handle
, base
, sbuf
);
143 if (SMB_VFS_STAT(handle
->conn
, base
, sbuf
) == -1) {
147 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
148 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
149 if (xattr_name
== NULL
) {
154 sbuf
->st_size
= get_xattr_size(handle
->conn
, NULL
, base
, xattr_name
);
155 if (sbuf
->st_size
== -1) {
160 sbuf
->st_ino
= stream_inode(sbuf
, xattr_name
);
161 sbuf
->st_mode
&= ~S_IFMT
;
162 sbuf
->st_mode
|= S_IFREG
;
163 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
172 static int streams_xattr_lstat(vfs_handle_struct
*handle
, const char *fname
,
173 SMB_STRUCT_STAT
*sbuf
)
180 if (!is_ntfs_stream_name(fname
)) {
181 return SMB_VFS_NEXT_LSTAT(handle
, fname
, sbuf
);
184 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
185 if (!NT_STATUS_IS_OK(status
)) {
191 return SMB_VFS_NEXT_LSTAT(handle
, base
, sbuf
);
194 if (SMB_VFS_LSTAT(handle
->conn
, base
, sbuf
) == -1) {
198 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
199 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
200 if (xattr_name
== NULL
) {
205 sbuf
->st_size
= get_xattr_size(handle
->conn
, NULL
, base
, xattr_name
);
206 if (sbuf
->st_size
== -1) {
211 sbuf
->st_ino
= stream_inode(sbuf
, xattr_name
);
212 sbuf
->st_mode
&= ~S_IFMT
;
213 sbuf
->st_mode
|= S_IFREG
;
214 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
223 static int streams_xattr_open(vfs_handle_struct
*handle
, const char *fname
,
224 files_struct
*fsp
, int flags
, mode_t mode
)
228 struct stream_io
*sio
;
235 DEBUG(10, ("streams_xattr_open called for %s\n", fname
));
237 if (!is_ntfs_stream_name(fname
)) {
238 return SMB_VFS_NEXT_OPEN(handle
, fname
, fsp
, flags
, mode
);
241 frame
= talloc_stackframe();
243 status
= split_ntfs_stream_name(talloc_tos(), fname
,
245 if (!NT_STATUS_IS_OK(status
)) {
251 hostfd
= SMB_VFS_NEXT_OPEN(handle
, base
, fsp
, flags
, mode
);
256 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
257 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
258 if (xattr_name
== NULL
) {
264 * We use baseflags to turn off nasty side-effects when opening the
268 baseflags
&= ~O_TRUNC
;
269 baseflags
&= ~O_EXCL
;
270 baseflags
&= ~O_CREAT
;
272 hostfd
= SMB_VFS_OPEN(handle
->conn
, base
, fsp
, baseflags
, mode
);
274 /* It is legit to open a stream on a directory, but the base
275 * fd has to be read-only.
277 if ((hostfd
== -1) && (errno
== EISDIR
)) {
278 baseflags
&= ~O_ACCMODE
;
279 baseflags
|= O_RDONLY
;
280 hostfd
= SMB_VFS_OPEN(handle
->conn
, fname
, fsp
, baseflags
,
288 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
, base
,
291 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
293 if (!NT_STATUS_IS_OK(status
)
294 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
296 * The base file is not there. This is an error even if we got
297 * O_CREAT, the higher levels should have created the base
300 DEBUG(10, ("streams_xattr_open: base file %s not around, "
301 "returning ENOENT\n", base
));
306 if (!NT_STATUS_IS_OK(status
)) {
308 * The attribute does not exist
311 if (flags
& O_CREAT
) {
313 * Darn, xattrs need at least 1 byte
317 DEBUG(10, ("creating attribute %s on file %s\n",
320 if (fsp
->base_fsp
->fh
->fd
!= -1) {
321 if (SMB_VFS_FSETXATTR(
322 fsp
->base_fsp
, xattr_name
,
324 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
328 if (SMB_VFS_SETXATTR(
329 handle
->conn
, base
, xattr_name
,
331 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
338 if (flags
& O_TRUNC
) {
340 if (fsp
->base_fsp
->fh
->fd
!= -1) {
341 if (SMB_VFS_FSETXATTR(
342 fsp
->base_fsp
, xattr_name
,
344 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
348 if (SMB_VFS_SETXATTR(
349 handle
->conn
, base
, xattr_name
,
351 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
357 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
364 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
366 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
369 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
380 * BUGBUGBUG -- we would need to call fd_close_posix here, but
381 * we don't have a full fsp yet
390 static int streams_xattr_unlink(vfs_handle_struct
*handle
, const char *fname
)
398 if (!is_ntfs_stream_name(fname
)) {
399 return SMB_VFS_NEXT_UNLINK(handle
, fname
);
402 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
403 if (!NT_STATUS_IS_OK(status
)) {
409 return SMB_VFS_NEXT_UNLINK(handle
, base
);
412 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
413 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
414 if (xattr_name
== NULL
) {
419 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, base
, xattr_name
);
421 if ((ret
== -1) && (errno
== ENOATTR
)) {
434 static int streams_xattr_rename(vfs_handle_struct
*handle
,
439 TALLOC_CTX
*frame
= NULL
;
454 o_is_stream
= is_ntfs_stream_name(oldname
);
455 n_is_stream
= is_ntfs_stream_name(newname
);
457 if (!o_is_stream
&& !n_is_stream
) {
458 return SMB_VFS_NEXT_RENAME(handle
, oldname
, newname
);
461 if (!(o_is_stream
&& n_is_stream
)) {
466 frame
= talloc_stackframe();
471 status
= split_ntfs_stream_name(talloc_tos(), oldname
, &obase
, &ostream
);
472 if (!NT_STATUS_IS_OK(status
)) {
477 status
= split_ntfs_stream_name(talloc_tos(), newname
, &nbase
, &nstream
);
478 if (!NT_STATUS_IS_OK(status
)) {
483 /*TODO: maybe call SMB_VFS_NEXT_RENAME() both streams are NULL (::$DATA) */
484 if (ostream
== NULL
) {
489 if (nstream
== NULL
) {
494 /* the new base should be empty */
495 if (StrCaseCmp(obase
, nbase
) != 0) {
500 if (StrCaseCmp(ostream
, nstream
) == 0) {
506 oxattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
507 SAMBA_XATTR_DOSSTREAM_PREFIX
, ostream
);
508 if (oxattr_name
== NULL
) {
513 nxattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
514 SAMBA_XATTR_DOSSTREAM_PREFIX
, nstream
);
515 if (nxattr_name
== NULL
) {
520 /* read the old stream */
521 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
522 base
, oxattr_name
, &ea
);
523 if (!NT_STATUS_IS_OK(status
)) {
528 /* (over)write the new stream */
529 nret
= SMB_VFS_SETXATTR(handle
->conn
, base
, nxattr_name
,
530 ea
.value
.data
, ea
.value
.length
, 0);
532 if (errno
== ENOATTR
) {
538 /* remove the old stream */
539 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, base
, oxattr_name
);
541 if (errno
== ENOATTR
) {
555 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
557 bool (*fn
)(struct ea_struct
*ea
,
564 size_t prefix_len
= strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
);
566 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
568 if (!NT_STATUS_IS_OK(status
)) {
572 for (i
=0; i
<num_names
; i
++) {
575 if (strncmp(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
580 status
= get_ea_value(names
, conn
, fsp
, fname
, names
[i
], &ea
);
581 if (!NT_STATUS_IS_OK(status
)) {
582 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
583 names
[i
], fname
, nt_errstr(status
)));
587 ea
.name
= talloc_asprintf(ea
.value
.data
, ":%s",
588 names
[i
] + prefix_len
);
589 if (ea
.name
== NULL
) {
590 DEBUG(0, ("talloc failed\n"));
594 if (!fn(&ea
, private_data
)) {
595 TALLOC_FREE(ea
.value
.data
);
599 TALLOC_FREE(ea
.value
.data
);
606 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
607 struct stream_struct
**streams
,
608 const char *name
, SMB_OFF_T size
,
609 SMB_OFF_T alloc_size
)
611 struct stream_struct
*tmp
;
613 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
619 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
620 if (tmp
[*num_streams
].name
== NULL
) {
624 tmp
[*num_streams
].size
= size
;
625 tmp
[*num_streams
].alloc_size
= alloc_size
;
632 struct streaminfo_state
{
634 vfs_handle_struct
*handle
;
635 unsigned int num_streams
;
636 struct stream_struct
*streams
;
640 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
642 struct streaminfo_state
*state
=
643 (struct streaminfo_state
*)private_data
;
645 if (!add_one_stream(state
->mem_ctx
,
646 &state
->num_streams
, &state
->streams
,
647 ea
->name
, ea
->value
.length
-1,
648 smb_roundup(state
->handle
->conn
,
649 ea
->value
.length
-1))) {
650 state
->status
= NT_STATUS_NO_MEMORY
;
657 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
658 struct files_struct
*fsp
,
661 unsigned int *pnum_streams
,
662 struct stream_struct
**pstreams
)
664 SMB_STRUCT_STAT sbuf
;
667 struct streaminfo_state state
;
669 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
670 if (is_ntfs_stream_name(fsp
->fsp_name
)) {
671 return NT_STATUS_INVALID_PARAMETER
;
673 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
676 if (is_ntfs_stream_name(fname
)) {
677 return NT_STATUS_INVALID_PARAMETER
;
679 ret
= SMB_VFS_STAT(handle
->conn
, fname
, &sbuf
);
683 return map_nt_error_from_unix(errno
);
686 state
.streams
= NULL
;
687 state
.num_streams
= 0;
689 if (!S_ISDIR(sbuf
.st_mode
)) {
690 if (!add_one_stream(mem_ctx
,
691 &state
.num_streams
, &state
.streams
,
692 "::$DATA", sbuf
.st_size
,
693 get_allocation_size(handle
->conn
, fsp
,
695 return NT_STATUS_NO_MEMORY
;
699 state
.mem_ctx
= mem_ctx
;
700 state
.handle
= handle
;
701 state
.status
= NT_STATUS_OK
;
703 status
= walk_xattr_streams(handle
->conn
, fsp
, fname
,
704 collect_one_stream
, &state
);
706 if (!NT_STATUS_IS_OK(status
)) {
707 TALLOC_FREE(state
.streams
);
711 if (!NT_STATUS_IS_OK(state
.status
)) {
712 TALLOC_FREE(state
.streams
);
716 *pnum_streams
= state
.num_streams
;
717 *pstreams
= state
.streams
;
721 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
)
723 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
) | FILE_NAMED_STREAMS
;
726 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
727 files_struct
*fsp
, const void *data
,
728 size_t n
, SMB_OFF_T offset
)
730 struct stream_io
*sio
=
731 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
736 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
739 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
742 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
743 sio
->base
, sio
->xattr_name
, &ea
);
744 if (!NT_STATUS_IS_OK(status
)) {
748 if ((offset
+ n
) > ea
.value
.length
-1) {
751 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
755 TALLOC_FREE(ea
.value
.data
);
760 ea
.value
.length
= offset
+ n
+ 1;
761 ea
.value
.data
[offset
+n
] = 0;
764 memcpy(ea
.value
.data
+ offset
, data
, n
);
766 if (fsp
->base_fsp
->fh
->fd
!= -1) {
767 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
769 ea
.value
.data
, ea
.value
.length
, 0);
771 ret
= SMB_VFS_SETXATTR(fsp
->conn
, fsp
->base_fsp
->fsp_name
,
773 ea
.value
.data
, ea
.value
.length
, 0);
775 TALLOC_FREE(ea
.value
.data
);
784 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
785 files_struct
*fsp
, void *data
,
786 size_t n
, SMB_OFF_T offset
)
788 struct stream_io
*sio
=
789 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
792 size_t length
, overlap
;
795 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
798 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
799 sio
->base
, sio
->xattr_name
, &ea
);
800 if (!NT_STATUS_IS_OK(status
)) {
804 length
= ea
.value
.length
-1;
806 /* Attempt to read past EOF. */
807 if (length
<= offset
) {
812 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
813 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
815 TALLOC_FREE(ea
.value
.data
);
819 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
820 struct files_struct
*fsp
,
827 struct stream_io
*sio
=
828 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
830 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
835 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
838 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
839 sio
->base
, sio
->xattr_name
, &ea
);
840 if (!NT_STATUS_IS_OK(status
)) {
844 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
848 TALLOC_FREE(ea
.value
.data
);
853 /* Did we expand ? */
854 if (ea
.value
.length
< offset
+ 1) {
855 memset(&tmp
[ea
.value
.length
], '\0',
856 offset
+ 1 - ea
.value
.length
);
860 ea
.value
.length
= offset
+ 1;
861 ea
.value
.data
[offset
] = 0;
863 if (fsp
->base_fsp
->fh
->fd
!= -1) {
864 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
866 ea
.value
.data
, ea
.value
.length
, 0);
868 ret
= SMB_VFS_SETXATTR(fsp
->conn
, fsp
->base_fsp
->fsp_name
,
870 ea
.value
.data
, ea
.value
.length
, 0);
873 TALLOC_FREE(ea
.value
.data
);
882 /* VFS operations structure */
884 static vfs_op_tuple streams_xattr_ops
[] = {
885 {SMB_VFS_OP(streams_xattr_fs_capabilities
), SMB_VFS_OP_FS_CAPABILITIES
,
886 SMB_VFS_LAYER_TRANSPARENT
},
887 {SMB_VFS_OP(streams_xattr_open
), SMB_VFS_OP_OPEN
,
888 SMB_VFS_LAYER_TRANSPARENT
},
889 {SMB_VFS_OP(streams_xattr_stat
), SMB_VFS_OP_STAT
,
890 SMB_VFS_LAYER_TRANSPARENT
},
891 {SMB_VFS_OP(streams_xattr_fstat
), SMB_VFS_OP_FSTAT
,
892 SMB_VFS_LAYER_TRANSPARENT
},
893 {SMB_VFS_OP(streams_xattr_lstat
), SMB_VFS_OP_LSTAT
,
894 SMB_VFS_LAYER_TRANSPARENT
},
895 {SMB_VFS_OP(streams_xattr_pread
), SMB_VFS_OP_PREAD
,
896 SMB_VFS_LAYER_TRANSPARENT
},
897 {SMB_VFS_OP(streams_xattr_pwrite
), SMB_VFS_OP_PWRITE
,
898 SMB_VFS_LAYER_TRANSPARENT
},
899 {SMB_VFS_OP(streams_xattr_lstat
), SMB_VFS_OP_LSTAT
,
900 SMB_VFS_LAYER_TRANSPARENT
},
901 {SMB_VFS_OP(streams_xattr_unlink
), SMB_VFS_OP_UNLINK
,
902 SMB_VFS_LAYER_TRANSPARENT
},
903 {SMB_VFS_OP(streams_xattr_rename
), SMB_VFS_OP_RENAME
,
904 SMB_VFS_LAYER_TRANSPARENT
},
905 {SMB_VFS_OP(streams_xattr_ftruncate
), SMB_VFS_OP_FTRUNCATE
,
906 SMB_VFS_LAYER_TRANSPARENT
},
907 {SMB_VFS_OP(streams_xattr_streaminfo
), SMB_VFS_OP_STREAMINFO
,
908 SMB_VFS_LAYER_OPAQUE
},
909 {SMB_VFS_OP(NULL
), SMB_VFS_OP_NOOP
, SMB_VFS_LAYER_NOOP
}
912 NTSTATUS
vfs_streams_xattr_init(void);
913 NTSTATUS
vfs_streams_xattr_init(void)
915 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",