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/>.
23 #define DBGC_CLASS DBGC_VFS
26 * Excerpt from a mail from tridge:
28 * Volker, what I'm thinking of is this:
29 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
30 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
32 * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
33 * is the fsid/inode. "namedstreamX" is a file named after the stream
37 static uint32_t hash_fn(DATA_BLOB key
)
39 uint32_t value
; /* Used to compute the hash value. */
40 uint32_t i
; /* Used to cycle through random values. */
42 /* Set the initial value from the key size. */
43 for (value
= 0x238F13AF * key
.length
, i
=0; i
< key
.length
; i
++)
44 value
= (value
+ (key
.data
[i
] << (i
*5 % 24)));
46 return (1103515243 * value
+ 12345);
50 * With the hashing scheme based on the inode we need to protect against
51 * streams showing up on files with re-used inodes. This can happen if we
52 * create a stream directory from within Samba, and a local process or NFS
53 * client deletes the file without deleting the streams directory. When the
54 * inode is re-used and the stream directory is still around, the streams in
55 * there would be show up as belonging to the new file.
57 * There are several workarounds for this, probably the easiest one is on
58 * systems which have a true birthtime stat element: When the file has a later
59 * birthtime than the streams directory, then we have to recreate the
62 * The other workaround is to somehow mark the file as generated by Samba with
63 * something that a NFS client would not do. The closest one is a special
64 * xattr value being set. On systems which do not support xattrs, it might be
65 * an option to put in a special ACL entry for a non-existing group.
68 #define SAMBA_XATTR_MARKER "user.SAMBA_STREAMS"
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_NEXT_GETXATTR(handle
, 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_NEXT_SETXATTR(handle
, path
, SAMBA_XATTR_MARKER
,
108 &buf
, sizeof(buf
), 0);
111 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno
)));
118 static char *stream_dir(vfs_handle_struct
*handle
, const char *base_path
,
119 const SMB_STRUCT_STAT
*base_sbuf
, bool create_it
)
123 SMB_STRUCT_STAT sbuf
;
124 uint8_t first
, second
;
132 check_valid
= lp_parm_bool(SNUM(handle
->conn
),
133 "streams_depot", "check_valid", true);
135 tmp
= talloc_asprintf(talloc_tos(), "%s/.streams", handle
->conn
->connectpath
);
142 rootdir
= lp_parm_const_string(
143 SNUM(handle
->conn
), "streams_depot", "directory",
146 if (base_sbuf
== NULL
) {
147 if (SMB_VFS_NEXT_STAT(handle
, base_path
, &sbuf
) == -1) {
149 * base file is not there
156 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, base_sbuf
);
158 push_file_id_16((char *)id_buf
, &id
);
160 hash
= hash_fn(data_blob_const(id_buf
, sizeof(id_buf
)));
163 second
= (hash
>> 8) & 0xff;
165 id_hex
= hex_encode_talloc(talloc_tos(), id_buf
, sizeof(id_buf
));
167 if (id_hex
== NULL
) {
172 result
= talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir
,
173 first
, second
, id_hex
);
177 if (result
== NULL
) {
182 if (SMB_VFS_NEXT_STAT(handle
, result
, &sbuf
) == 0) {
185 if (!S_ISDIR(sbuf
.st_mode
)) {
190 if (file_is_valid(handle
, base_path
, check_valid
)) {
195 * Someone has recreated a file under an existing inode
196 * without deleting the streams directory. For now, just move
201 newname
= talloc_asprintf(talloc_tos(), "lost-%lu", random());
202 if (newname
== NULL
) {
207 if (SMB_VFS_NEXT_RENAME(handle
, result
, newname
) == -1) {
208 if ((errno
== EEXIST
) || (errno
== ENOTEMPTY
)) {
209 TALLOC_FREE(newname
);
215 TALLOC_FREE(newname
);
223 if ((SMB_VFS_NEXT_MKDIR(handle
, rootdir
, 0755) != 0)
224 && (errno
!= EEXIST
)) {
228 tmp
= talloc_asprintf(result
, "%s/%2.2X", rootdir
, first
);
234 if ((SMB_VFS_NEXT_MKDIR(handle
, tmp
, 0755) != 0)
235 && (errno
!= EEXIST
)) {
241 tmp
= talloc_asprintf(result
, "%s/%2.2X/%2.2X", rootdir
, first
,
248 if ((SMB_VFS_NEXT_MKDIR(handle
, tmp
, 0755) != 0)
249 && (errno
!= EEXIST
)) {
255 if ((SMB_VFS_NEXT_MKDIR(handle
, result
, 0755) != 0)
256 && (errno
!= EEXIST
)) {
260 if (!mark_file_valid(handle
, base_path
, check_valid
)) {
271 static char *stream_name(vfs_handle_struct
*handle
, const char *fname
,
277 char *dirname
, *stream_fname
;
279 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname
,
281 DEBUG(10, ("split_ntfs_stream_name failed\n"));
286 /* if it's the ::$DATA stream just return the base file name */
291 dirname
= stream_dir(handle
, base
, NULL
, create_dir
);
293 if (dirname
== NULL
) {
297 stream_fname
= talloc_asprintf(talloc_tos(), "%s/:%s", dirname
, sname
);
299 if (stream_fname
== NULL
) {
304 DEBUG(10, ("stream filename = %s\n", stream_fname
));
313 DEBUG(5, ("stream_name failed: %s\n", strerror(errno
)));
320 static NTSTATUS
walk_streams(vfs_handle_struct
*handle
,
322 const SMB_STRUCT_STAT
*sbuf
,
324 bool (*fn
)(const char *dirname
,
330 SMB_STRUCT_DIR
*dirhandle
= NULL
;
333 dirname
= stream_dir(handle
, fname
, sbuf
, false);
335 if (dirname
== NULL
) {
336 if (errno
== ENOENT
) {
342 return map_nt_error_from_unix(errno
);
345 DEBUG(10, ("walk_streams: dirname=%s\n", dirname
));
347 dirhandle
= SMB_VFS_NEXT_OPENDIR(handle
, dirname
, NULL
, 0);
349 if (dirhandle
== NULL
) {
350 TALLOC_FREE(dirname
);
351 return map_nt_error_from_unix(errno
);
354 while ((dirent
= vfs_readdirname(handle
->conn
, dirhandle
, NULL
)) != NULL
) {
356 if (ISDOT(dirent
) || ISDOTDOT(dirent
)) {
360 DEBUG(10, ("walk_streams: dirent=%s\n", dirent
));
362 if (!fn(dirname
, dirent
, private_data
)) {
367 SMB_VFS_NEXT_CLOSEDIR(handle
, dirhandle
);
369 if (pdirname
!= NULL
) {
373 TALLOC_FREE(dirname
);
379 static int streams_depot_stat(vfs_handle_struct
*handle
, const char *fname
,
380 SMB_STRUCT_STAT
*sbuf
)
385 DEBUG(10, ("streams_depot_stat called for [%s]\n", fname
));
387 if (!is_ntfs_stream_name(fname
)) {
388 return SMB_VFS_NEXT_STAT(handle
, fname
, sbuf
);
391 stream_fname
= stream_name(handle
, fname
, false);
392 if (stream_fname
== NULL
) {
396 ret
= SMB_VFS_NEXT_STAT(handle
, stream_fname
, sbuf
);
399 TALLOC_FREE(stream_fname
);
403 static int streams_depot_lstat(vfs_handle_struct
*handle
, const char *fname
,
404 SMB_STRUCT_STAT
*sbuf
)
409 if (!is_ntfs_stream_name(fname
)) {
410 return SMB_VFS_NEXT_LSTAT(handle
, fname
, sbuf
);
413 stream_fname
= stream_name(handle
, fname
, false);
414 if (stream_fname
== NULL
) {
418 ret
= SMB_VFS_NEXT_LSTAT(handle
, stream_fname
, sbuf
);
421 TALLOC_FREE(stream_fname
);
425 static int streams_depot_open(vfs_handle_struct
*handle
, const char *fname
,
426 files_struct
*fsp
, int flags
, mode_t mode
)
431 SMB_STRUCT_STAT base_sbuf
;
435 if (!is_ntfs_stream_name(fname
)) {
436 return SMB_VFS_NEXT_OPEN(handle
, fname
, fsp
, flags
, mode
);
439 frame
= talloc_stackframe();
441 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname
,
448 ret
= SMB_VFS_NEXT_OPEN(handle
, base
, fsp
, flags
, mode
);
452 ret
= SMB_VFS_NEXT_STAT(handle
, base
, &base_sbuf
);
460 stream_fname
= stream_name(handle
, fname
, true);
461 if (stream_fname
== NULL
) {
465 ret
= SMB_VFS_NEXT_OPEN(handle
, stream_fname
, fsp
, flags
, mode
);
472 static int streams_depot_unlink(vfs_handle_struct
*handle
, const char *fname
)
475 SMB_STRUCT_STAT sbuf
;
477 DEBUG(10, ("streams_depot_unlink called for %s\n", fname
));
479 if (is_ntfs_stream_name(fname
)) {
482 stream_fname
= stream_name(handle
, fname
, false);
483 if (stream_fname
== NULL
) {
487 ret
= SMB_VFS_NEXT_UNLINK(handle
, stream_fname
);
489 TALLOC_FREE(stream_fname
);
494 * We potentially need to delete the per-inode streams directory
497 if (lp_posix_pathnames()) {
498 ret
= SMB_VFS_NEXT_LSTAT(handle
, fname
, &sbuf
);
500 ret
= SMB_VFS_NEXT_STAT(handle
, fname
, &sbuf
);
507 if (sbuf
.st_nlink
== 1) {
508 char *dirname
= stream_dir(handle
, fname
, &sbuf
, false);
510 if (dirname
!= NULL
) {
511 SMB_VFS_NEXT_RMDIR(handle
, dirname
);
513 TALLOC_FREE(dirname
);
516 return SMB_VFS_NEXT_UNLINK(handle
, fname
);
519 static int streams_depot_rename(vfs_handle_struct
*handle
,
523 TALLOC_CTX
*frame
= NULL
;
531 char *ostream_fname
= NULL
;
532 char *nstream_fname
= NULL
;
533 char *newname_full
= NULL
;
535 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
538 old_is_stream
= is_ntfs_stream_name(oldname
);
539 new_is_stream
= is_ntfs_stream_name(newname
);
541 if (!old_is_stream
&& !new_is_stream
) {
542 return SMB_VFS_NEXT_RENAME(handle
, oldname
, newname
);
545 frame
= talloc_stackframe();
547 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), oldname
,
553 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), newname
,
559 /* for now don't allow renames from or to the default stream */
560 if (!osname
|| !nsname
) {
565 ostream_fname
= stream_name(handle
, oldname
, false);
566 if (ostream_fname
== NULL
) {
571 * Handle passing in a stream name without the base file. This is
572 * exercised by the NTRENAME streams rename path.
574 if (StrCaseCmp(nbase
, "./") == 0) {
575 newname_full
= talloc_asprintf(talloc_tos(), "%s:%s", obase
,
577 if (newname_full
== NULL
) {
583 nstream_fname
= stream_name(handle
,
584 newname_full
? newname_full
: newname
,
586 if (nstream_fname
== NULL
) {
590 ret
= SMB_VFS_NEXT_RENAME(handle
, ostream_fname
, nstream_fname
);
597 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
598 struct stream_struct
**streams
,
599 const char *name
, SMB_OFF_T size
,
600 SMB_OFF_T alloc_size
)
602 struct stream_struct
*tmp
;
604 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
610 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
611 if (tmp
[*num_streams
].name
== NULL
) {
615 tmp
[*num_streams
].size
= size
;
616 tmp
[*num_streams
].alloc_size
= alloc_size
;
623 struct streaminfo_state
{
625 vfs_handle_struct
*handle
;
626 unsigned int num_streams
;
627 struct stream_struct
*streams
;
631 static bool collect_one_stream(const char *dirname
,
635 struct streaminfo_state
*state
=
636 (struct streaminfo_state
*)private_data
;
638 SMB_STRUCT_STAT sbuf
;
640 if (asprintf(&full_sname
, "%s/%s", dirname
, dirent
) == -1) {
641 state
->status
= NT_STATUS_NO_MEMORY
;
644 if (SMB_VFS_NEXT_STAT(state
->handle
, full_sname
, &sbuf
) == -1) {
645 DEBUG(10, ("Could not stat %s: %s\n", full_sname
,
647 SAFE_FREE(full_sname
);
651 SAFE_FREE(full_sname
);
653 if (!add_one_stream(state
->mem_ctx
,
654 &state
->num_streams
, &state
->streams
,
655 dirent
, sbuf
.st_size
,
656 SMB_VFS_GET_ALLOC_SIZE(state
->handle
->conn
, NULL
,
658 state
->status
= NT_STATUS_NO_MEMORY
;
665 static NTSTATUS
streams_depot_streaminfo(vfs_handle_struct
*handle
,
666 struct files_struct
*fsp
,
669 unsigned int *pnum_streams
,
670 struct stream_struct
**pstreams
)
672 SMB_STRUCT_STAT sbuf
;
675 struct streaminfo_state state
;
677 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
678 if (is_ntfs_stream_name(fsp
->fsp_name
)) {
679 return NT_STATUS_INVALID_PARAMETER
;
681 ret
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, &sbuf
);
684 if (is_ntfs_stream_name(fname
)) {
685 return NT_STATUS_INVALID_PARAMETER
;
687 if (lp_posix_pathnames()) {
688 ret
= SMB_VFS_NEXT_LSTAT(handle
, fname
, &sbuf
);
690 ret
= SMB_VFS_NEXT_STAT(handle
, fname
, &sbuf
);
695 return map_nt_error_from_unix(errno
);
698 state
.streams
= NULL
;
699 state
.num_streams
= 0;
701 if (!S_ISDIR(sbuf
.st_mode
)) {
702 if (!add_one_stream(mem_ctx
,
703 &state
.num_streams
, &state
.streams
,
704 "::$DATA", sbuf
.st_size
,
705 SMB_VFS_GET_ALLOC_SIZE(handle
->conn
, fsp
,
707 return NT_STATUS_NO_MEMORY
;
711 state
.mem_ctx
= mem_ctx
;
712 state
.handle
= handle
;
713 state
.status
= NT_STATUS_OK
;
715 status
= walk_streams(handle
, fname
, &sbuf
, NULL
, collect_one_stream
,
718 if (!NT_STATUS_IS_OK(status
)) {
719 TALLOC_FREE(state
.streams
);
723 if (!NT_STATUS_IS_OK(state
.status
)) {
724 TALLOC_FREE(state
.streams
);
728 *pnum_streams
= state
.num_streams
;
729 *pstreams
= state
.streams
;
733 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct
*handle
)
735 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
) | FILE_NAMED_STREAMS
;
738 /* VFS operations structure */
740 static vfs_op_tuple streams_depot_ops
[] = {
741 {SMB_VFS_OP(streams_depot_fs_capabilities
), SMB_VFS_OP_FS_CAPABILITIES
,
742 SMB_VFS_LAYER_TRANSPARENT
},
743 {SMB_VFS_OP(streams_depot_open
), SMB_VFS_OP_OPEN
,
744 SMB_VFS_LAYER_TRANSPARENT
},
745 {SMB_VFS_OP(streams_depot_stat
), SMB_VFS_OP_STAT
,
746 SMB_VFS_LAYER_TRANSPARENT
},
747 {SMB_VFS_OP(streams_depot_lstat
), SMB_VFS_OP_LSTAT
,
748 SMB_VFS_LAYER_TRANSPARENT
},
749 {SMB_VFS_OP(streams_depot_unlink
), SMB_VFS_OP_UNLINK
,
750 SMB_VFS_LAYER_TRANSPARENT
},
751 {SMB_VFS_OP(streams_depot_rename
), SMB_VFS_OP_RENAME
,
752 SMB_VFS_LAYER_TRANSPARENT
},
753 {SMB_VFS_OP(streams_depot_streaminfo
), SMB_VFS_OP_STREAMINFO
,
754 SMB_VFS_LAYER_OPAQUE
},
755 {SMB_VFS_OP(NULL
), SMB_VFS_OP_NOOP
, SMB_VFS_LAYER_NOOP
}
758 NTSTATUS
vfs_streams_depot_init(void);
759 NTSTATUS
vfs_streams_depot_init(void)
761 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_depot",