2 * Store streams in a separate subdirectory
4 * Copyright (C) Volker Lendecke, 2007
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "smbd/smbd.h"
22 #include "system/filesys.h"
25 #define DBGC_CLASS DBGC_VFS
28 * Excerpt from a mail from tridge:
30 * Volker, what I'm thinking of is this:
31 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
32 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
34 * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
35 * is the fsid/inode. "namedstreamX" is a file named after the stream
39 static uint32_t hash_fn(DATA_BLOB key
)
41 uint32_t value
; /* Used to compute the hash value. */
42 uint32_t i
; /* Used to cycle through random values. */
44 /* Set the initial value from the key size. */
45 for (value
= 0x238F13AF * key
.length
, i
=0; i
< key
.length
; i
++)
46 value
= (value
+ (key
.data
[i
] << (i
*5 % 24)));
48 return (1103515243 * value
+ 12345);
52 * With the hashing scheme based on the inode we need to protect against
53 * streams showing up on files with re-used inodes. This can happen if we
54 * create a stream directory from within Samba, and a local process or NFS
55 * client deletes the file without deleting the streams directory. When the
56 * inode is re-used and the stream directory is still around, the streams in
57 * there would be show up as belonging to the new file.
59 * There are several workarounds for this, probably the easiest one is on
60 * systems which have a true birthtime stat element: When the file has a later
61 * birthtime than the streams directory, then we have to recreate the
64 * The other workaround is to somehow mark the file as generated by Samba with
65 * something that a NFS client would not do. The closest one is a special
66 * xattr value being set. On systems which do not support xattrs, it might be
67 * an option to put in a special ACL entry for a non-existing group.
70 static bool file_is_valid(vfs_handle_struct
*handle
, const char *path
,
79 DEBUG(10, ("file_is_valid (%s) called\n", path
));
81 if (SMB_VFS_GETXATTR(handle
->conn
, path
, SAMBA_XATTR_MARKER
,
82 &buf
, sizeof(buf
)) != sizeof(buf
)) {
83 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno
)));
88 DEBUG(10, ("got wrong buffer content: '%c'\n", buf
));
95 static bool mark_file_valid(vfs_handle_struct
*handle
, const char *path
,
105 DEBUG(10, ("marking file %s as valid\n", path
));
107 ret
= SMB_VFS_SETXATTR(handle
->conn
, path
, SAMBA_XATTR_MARKER
,
108 &buf
, sizeof(buf
), 0);
111 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno
)));
119 * Given an smb_filename, determine the stream directory using the file's
122 static char *stream_dir(vfs_handle_struct
*handle
,
123 const struct smb_filename
*smb_fname
,
124 const SMB_STRUCT_STAT
*base_sbuf
, bool create_it
)
127 struct smb_filename
*smb_fname_hash
= NULL
;
129 SMB_STRUCT_STAT base_sbuf_tmp
;
130 uint8_t first
, second
;
139 check_valid
= lp_parm_bool(SNUM(handle
->conn
),
140 "streams_depot", "check_valid", true);
142 tmp
= talloc_asprintf(talloc_tos(), "%s/.streams", handle
->conn
->connectpath
);
149 rootdir
= lp_parm_const_string(
150 SNUM(handle
->conn
), "streams_depot", "directory",
153 /* Stat the base file if it hasn't already been done. */
154 if (base_sbuf
== NULL
) {
155 struct smb_filename
*smb_fname_base
= NULL
;
157 status
= create_synthetic_smb_fname(talloc_tos(),
158 smb_fname
->base_name
,
161 if (!NT_STATUS_IS_OK(status
)) {
162 errno
= map_errno_from_nt_status(status
);
165 if (SMB_VFS_NEXT_STAT(handle
, smb_fname_base
) == -1) {
166 TALLOC_FREE(smb_fname_base
);
169 base_sbuf_tmp
= smb_fname_base
->st
;
170 TALLOC_FREE(smb_fname_base
);
172 base_sbuf_tmp
= *base_sbuf
;
175 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &base_sbuf_tmp
);
177 push_file_id_16((char *)id_buf
, &id
);
179 hash
= hash_fn(data_blob_const(id_buf
, sizeof(id_buf
)));
182 second
= (hash
>> 8) & 0xff;
184 id_hex
= hex_encode_talloc(talloc_tos(), id_buf
, sizeof(id_buf
));
186 if (id_hex
== NULL
) {
191 result
= talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir
,
192 first
, second
, id_hex
);
196 if (result
== NULL
) {
201 status
= create_synthetic_smb_fname(talloc_tos(), result
, NULL
, NULL
,
203 if (!NT_STATUS_IS_OK(status
)) {
204 errno
= map_errno_from_nt_status(status
);
208 if (SMB_VFS_NEXT_STAT(handle
, smb_fname_hash
) == 0) {
209 struct smb_filename
*smb_fname_new
= NULL
;
212 if (!S_ISDIR(smb_fname_hash
->st
.st_ex_mode
)) {
217 if (file_is_valid(handle
, smb_fname
->base_name
, check_valid
)) {
222 * Someone has recreated a file under an existing inode
223 * without deleting the streams directory. For now, just move
228 newname
= talloc_asprintf(talloc_tos(), "lost-%lu", random());
229 if (newname
== NULL
) {
234 status
= create_synthetic_smb_fname(talloc_tos(), newname
,
237 TALLOC_FREE(newname
);
238 if (!NT_STATUS_IS_OK(status
)) {
239 errno
= map_errno_from_nt_status(status
);
243 if (SMB_VFS_NEXT_RENAME(handle
, smb_fname_hash
,
244 smb_fname_new
) == -1) {
245 TALLOC_FREE(smb_fname_new
);
246 if ((errno
== EEXIST
) || (errno
== ENOTEMPTY
)) {
252 TALLOC_FREE(smb_fname_new
);
260 if ((SMB_VFS_NEXT_MKDIR(handle
, rootdir
, 0755) != 0)
261 && (errno
!= EEXIST
)) {
265 tmp
= talloc_asprintf(result
, "%s/%2.2X", rootdir
, first
);
271 if ((SMB_VFS_NEXT_MKDIR(handle
, tmp
, 0755) != 0)
272 && (errno
!= EEXIST
)) {
278 tmp
= talloc_asprintf(result
, "%s/%2.2X/%2.2X", rootdir
, first
,
285 if ((SMB_VFS_NEXT_MKDIR(handle
, tmp
, 0755) != 0)
286 && (errno
!= EEXIST
)) {
292 if ((SMB_VFS_NEXT_MKDIR(handle
, result
, 0755) != 0)
293 && (errno
!= EEXIST
)) {
297 if (!mark_file_valid(handle
, smb_fname
->base_name
, check_valid
)) {
301 TALLOC_FREE(smb_fname_hash
);
305 TALLOC_FREE(smb_fname_hash
);
310 * Given a stream name, populate smb_fname_out with the actual location of the
313 static NTSTATUS
stream_smb_fname(vfs_handle_struct
*handle
,
314 const struct smb_filename
*smb_fname
,
315 struct smb_filename
**smb_fname_out
,
318 char *dirname
, *stream_fname
;
322 *smb_fname_out
= NULL
;
324 stype
= strchr_m(smb_fname
->stream_name
+ 1, ':');
327 if (strcasecmp_m(stype
, ":$DATA") != 0) {
328 return NT_STATUS_INVALID_PARAMETER
;
332 dirname
= stream_dir(handle
, smb_fname
, NULL
, create_dir
);
334 if (dirname
== NULL
) {
335 status
= map_nt_error_from_unix(errno
);
339 stream_fname
= talloc_asprintf(talloc_tos(), "%s/%s", dirname
,
340 smb_fname
->stream_name
);
342 if (stream_fname
== NULL
) {
343 status
= NT_STATUS_NO_MEMORY
;
348 /* Append an explicit stream type if one wasn't specified. */
349 stream_fname
= talloc_asprintf(talloc_tos(), "%s:$DATA",
351 if (stream_fname
== NULL
) {
352 status
= NT_STATUS_NO_MEMORY
;
356 /* Normalize the stream type to upercase. */
357 strupper_m(strrchr_m(stream_fname
, ':') + 1);
360 DEBUG(10, ("stream filename = %s\n", stream_fname
));
362 /* Create an smb_filename with stream_name == NULL. */
363 status
= create_synthetic_smb_fname(talloc_tos(), stream_fname
, NULL
,
364 NULL
, smb_fname_out
);
365 if (!NT_STATUS_IS_OK(status
)) {
372 DEBUG(5, ("stream_name failed: %s\n", strerror(errno
)));
373 TALLOC_FREE(*smb_fname_out
);
377 static NTSTATUS
walk_streams(vfs_handle_struct
*handle
,
378 struct smb_filename
*smb_fname_base
,
380 bool (*fn
)(const char *dirname
,
386 SMB_STRUCT_DIR
*dirhandle
= NULL
;
387 const char *dirent
= NULL
;
388 char *talloced
= NULL
;
390 dirname
= stream_dir(handle
, smb_fname_base
, &smb_fname_base
->st
,
393 if (dirname
== NULL
) {
394 if (errno
== ENOENT
) {
400 return map_nt_error_from_unix(errno
);
403 DEBUG(10, ("walk_streams: dirname=%s\n", dirname
));
405 dirhandle
= SMB_VFS_NEXT_OPENDIR(handle
, dirname
, NULL
, 0);
407 if (dirhandle
== NULL
) {
408 TALLOC_FREE(dirname
);
409 return map_nt_error_from_unix(errno
);
412 while ((dirent
= vfs_readdirname(handle
->conn
, dirhandle
, NULL
,
413 &talloced
)) != NULL
) {
415 if (ISDOT(dirent
) || ISDOTDOT(dirent
)) {
416 TALLOC_FREE(talloced
);
420 DEBUG(10, ("walk_streams: dirent=%s\n", dirent
));
422 if (!fn(dirname
, dirent
, private_data
)) {
423 TALLOC_FREE(talloced
);
426 TALLOC_FREE(talloced
);
429 SMB_VFS_NEXT_CLOSEDIR(handle
, dirhandle
);
431 if (pdirname
!= NULL
) {
435 TALLOC_FREE(dirname
);
442 * Helper to stat/lstat the base file of an smb_fname. This will actually
443 * fills in the stat struct in smb_filename.
445 static int streams_depot_stat_base(vfs_handle_struct
*handle
,
446 struct smb_filename
*smb_fname
,
449 char *tmp_stream_name
;
452 tmp_stream_name
= smb_fname
->stream_name
;
453 smb_fname
->stream_name
= NULL
;
455 result
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
457 result
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
459 smb_fname
->stream_name
= tmp_stream_name
;
463 static int streams_depot_stat(vfs_handle_struct
*handle
,
464 struct smb_filename
*smb_fname
)
466 struct smb_filename
*smb_fname_stream
= NULL
;
470 DEBUG(10, ("streams_depot_stat called for [%s]\n",
471 smb_fname_str_dbg(smb_fname
)));
473 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
474 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
477 /* If the default stream is requested, just stat the base file. */
478 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
479 return streams_depot_stat_base(handle
, smb_fname
, true);
482 /* Stat the actual stream now. */
483 status
= stream_smb_fname(handle
, smb_fname
, &smb_fname_stream
,
485 if (!NT_STATUS_IS_OK(status
)) {
487 errno
= map_errno_from_nt_status(status
);
491 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_stream
);
493 /* Update the original smb_fname with the stat info. */
494 smb_fname
->st
= smb_fname_stream
->st
;
496 TALLOC_FREE(smb_fname_stream
);
502 static int streams_depot_lstat(vfs_handle_struct
*handle
,
503 struct smb_filename
*smb_fname
)
505 struct smb_filename
*smb_fname_stream
= NULL
;
509 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
510 smb_fname_str_dbg(smb_fname
)));
512 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
513 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
516 /* If the default stream is requested, just stat the base file. */
517 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
518 return streams_depot_stat_base(handle
, smb_fname
, false);
521 /* Stat the actual stream now. */
522 status
= stream_smb_fname(handle
, smb_fname
, &smb_fname_stream
,
524 if (!NT_STATUS_IS_OK(status
)) {
526 errno
= map_errno_from_nt_status(status
);
530 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname_stream
);
533 TALLOC_FREE(smb_fname_stream
);
537 static int streams_depot_open(vfs_handle_struct
*handle
,
538 struct smb_filename
*smb_fname
,
539 files_struct
*fsp
, int flags
, mode_t mode
)
541 struct smb_filename
*smb_fname_stream
= NULL
;
542 struct smb_filename
*smb_fname_base
= NULL
;
546 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
547 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
550 /* If the default stream is requested, just open the base file. */
551 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
552 char *tmp_stream_name
;
554 tmp_stream_name
= smb_fname
->stream_name
;
555 smb_fname
->stream_name
= NULL
;
556 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
557 smb_fname
->stream_name
= tmp_stream_name
;
562 /* Ensure the base file still exists. */
563 status
= create_synthetic_smb_fname(talloc_tos(),
564 smb_fname
->base_name
,
567 if (!NT_STATUS_IS_OK(status
)) {
569 errno
= map_errno_from_nt_status(status
);
573 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_base
);
578 /* Determine the stream name, and then open it. */
579 status
= stream_smb_fname(handle
, smb_fname
, &smb_fname_stream
, true);
580 if (!NT_STATUS_IS_OK(status
)) {
582 errno
= map_errno_from_nt_status(status
);
586 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname_stream
, fsp
, flags
, mode
);
589 TALLOC_FREE(smb_fname_stream
);
590 TALLOC_FREE(smb_fname_base
);
594 static int streams_depot_unlink(vfs_handle_struct
*handle
,
595 const struct smb_filename
*smb_fname
)
597 struct smb_filename
*smb_fname_base
= NULL
;
601 DEBUG(10, ("streams_depot_unlink called for %s\n",
602 smb_fname_str_dbg(smb_fname
)));
604 /* If there is a valid stream, just unlink the stream and return. */
605 if (is_ntfs_stream_smb_fname(smb_fname
) &&
606 !is_ntfs_default_stream_smb_fname(smb_fname
)) {
607 struct smb_filename
*smb_fname_stream
= NULL
;
609 status
= stream_smb_fname(handle
, smb_fname
, &smb_fname_stream
,
611 if (!NT_STATUS_IS_OK(status
)) {
612 errno
= map_errno_from_nt_status(status
);
616 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_stream
);
618 TALLOC_FREE(smb_fname_stream
);
623 * We potentially need to delete the per-inode streams directory
626 status
= create_synthetic_smb_fname(talloc_tos(), smb_fname
->base_name
,
627 NULL
, NULL
, &smb_fname_base
);
628 if (!NT_STATUS_IS_OK(status
)) {
629 errno
= map_errno_from_nt_status(status
);
633 if (lp_posix_pathnames()) {
634 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname_base
);
636 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_base
);
640 TALLOC_FREE(smb_fname_base
);
644 if (smb_fname_base
->st
.st_ex_nlink
== 1) {
645 char *dirname
= stream_dir(handle
, smb_fname_base
,
646 &smb_fname_base
->st
, false);
648 if (dirname
!= NULL
) {
649 SMB_VFS_NEXT_RMDIR(handle
, dirname
);
651 TALLOC_FREE(dirname
);
654 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
656 TALLOC_FREE(smb_fname_base
);
660 static int streams_depot_rename(vfs_handle_struct
*handle
,
661 const struct smb_filename
*smb_fname_src
,
662 const struct smb_filename
*smb_fname_dst
)
664 struct smb_filename
*smb_fname_src_stream
= NULL
;
665 struct smb_filename
*smb_fname_dst_stream
= NULL
;
666 bool src_is_stream
, dst_is_stream
;
670 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
671 smb_fname_str_dbg(smb_fname_src
),
672 smb_fname_str_dbg(smb_fname_dst
)));
674 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
675 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
677 if (!src_is_stream
&& !dst_is_stream
) {
678 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
682 /* for now don't allow renames from or to the default stream */
683 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
684 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
689 status
= stream_smb_fname(handle
, smb_fname_src
, &smb_fname_src_stream
,
691 if (!NT_STATUS_IS_OK(status
)) {
692 errno
= map_errno_from_nt_status(status
);
696 status
= stream_smb_fname(handle
, smb_fname_dst
,
697 &smb_fname_dst_stream
, false);
698 if (!NT_STATUS_IS_OK(status
)) {
699 errno
= map_errno_from_nt_status(status
);
703 ret
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src_stream
,
704 smb_fname_dst_stream
);
707 TALLOC_FREE(smb_fname_src_stream
);
708 TALLOC_FREE(smb_fname_dst_stream
);
712 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
713 struct stream_struct
**streams
,
714 const char *name
, SMB_OFF_T size
,
715 SMB_OFF_T alloc_size
)
717 struct stream_struct
*tmp
;
719 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
725 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
726 if (tmp
[*num_streams
].name
== NULL
) {
730 tmp
[*num_streams
].size
= size
;
731 tmp
[*num_streams
].alloc_size
= alloc_size
;
738 struct streaminfo_state
{
740 vfs_handle_struct
*handle
;
741 unsigned int num_streams
;
742 struct stream_struct
*streams
;
746 static bool collect_one_stream(const char *dirname
,
750 struct streaminfo_state
*state
=
751 (struct streaminfo_state
*)private_data
;
752 struct smb_filename
*smb_fname
= NULL
;
757 sname
= talloc_asprintf(talloc_tos(), "%s/%s", dirname
, dirent
);
759 state
->status
= NT_STATUS_NO_MEMORY
;
764 status
= create_synthetic_smb_fname(talloc_tos(), sname
, NULL
,
766 if (!NT_STATUS_IS_OK(status
)) {
767 state
->status
= status
;
772 if (SMB_VFS_NEXT_STAT(state
->handle
, smb_fname
) == -1) {
773 DEBUG(10, ("Could not stat %s: %s\n", sname
,
779 if (!add_one_stream(state
->mem_ctx
,
780 &state
->num_streams
, &state
->streams
,
781 dirent
, smb_fname
->st
.st_ex_size
,
782 SMB_VFS_GET_ALLOC_SIZE(state
->handle
->conn
, NULL
,
784 state
->status
= NT_STATUS_NO_MEMORY
;
792 TALLOC_FREE(smb_fname
);
796 static NTSTATUS
streams_depot_streaminfo(vfs_handle_struct
*handle
,
797 struct files_struct
*fsp
,
800 unsigned int *pnum_streams
,
801 struct stream_struct
**pstreams
)
803 struct smb_filename
*smb_fname_base
= NULL
;
806 struct streaminfo_state state
;
808 status
= create_synthetic_smb_fname(talloc_tos(), fname
, NULL
, NULL
,
810 if (!NT_STATUS_IS_OK(status
)) {
814 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
815 ret
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, &smb_fname_base
->st
);
818 if (lp_posix_pathnames()) {
819 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname_base
);
821 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_base
);
826 status
= map_nt_error_from_unix(errno
);
830 state
.streams
= NULL
;
831 state
.num_streams
= 0;
833 if (!S_ISDIR(smb_fname_base
->st
.st_ex_mode
)) {
834 if (!add_one_stream(mem_ctx
,
835 &state
.num_streams
, &state
.streams
,
836 "::$DATA", smb_fname_base
->st
.st_ex_size
,
837 SMB_VFS_GET_ALLOC_SIZE(handle
->conn
, fsp
,
838 &smb_fname_base
->st
))) {
839 status
= NT_STATUS_NO_MEMORY
;
844 state
.mem_ctx
= mem_ctx
;
845 state
.handle
= handle
;
846 state
.status
= NT_STATUS_OK
;
848 status
= walk_streams(handle
, smb_fname_base
, NULL
, collect_one_stream
,
851 if (!NT_STATUS_IS_OK(status
)) {
852 TALLOC_FREE(state
.streams
);
856 if (!NT_STATUS_IS_OK(state
.status
)) {
857 TALLOC_FREE(state
.streams
);
858 status
= state
.status
;
862 *pnum_streams
= state
.num_streams
;
863 *pstreams
= state
.streams
;
864 status
= NT_STATUS_OK
;
867 TALLOC_FREE(smb_fname_base
);
871 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct
*handle
,
872 enum timestamp_set_resolution
*p_ts_res
)
874 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
877 static struct vfs_fn_pointers vfs_streams_depot_fns
= {
878 .fs_capabilities
= streams_depot_fs_capabilities
,
879 .open_fn
= streams_depot_open
,
880 .stat
= streams_depot_stat
,
881 .lstat
= streams_depot_lstat
,
882 .unlink
= streams_depot_unlink
,
883 .rename
= streams_depot_rename
,
884 .streaminfo
= streams_depot_streaminfo
,
887 NTSTATUS
vfs_streams_depot_init(void);
888 NTSTATUS
vfs_streams_depot_init(void)
890 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_depot",
891 &vfs_streams_depot_fns
);