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 "system/filesys.h"
24 #define DBGC_CLASS DBGC_VFS
27 * Excerpt from a mail from tridge:
29 * Volker, what I'm thinking of is this:
30 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
31 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
33 * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
34 * is the fsid/inode. "namedstreamX" is a file named after the stream
38 static uint32_t hash_fn(DATA_BLOB key)
40 uint32_t value; /* Used to compute the hash value. */
41 uint32_t i; /* Used to cycle through random values. */
43 /* Set the initial value from the key size. */
44 for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
45 value = (value + (key.data[i] << (i*5 % 24)));
47 return (1103515243 * value + 12345);
51 * With the hashing scheme based on the inode we need to protect against
52 * streams showing up on files with re-used inodes. This can happen if we
53 * create a stream directory from within Samba, and a local process or NFS
54 * client deletes the file without deleting the streams directory. When the
55 * inode is re-used and the stream directory is still around, the streams in
56 * there would be show up as belonging to the new file.
58 * There are several workarounds for this, probably the easiest one is on
59 * systems which have a true birthtime stat element: When the file has a later
60 * birthtime than the streams directory, then we have to recreate the
63 * The other workaround is to somehow mark the file as generated by Samba with
64 * something that a NFS client would not do. The closest one is a special
65 * xattr value being set. On systems which do not support xattrs, it might be
66 * an option to put in a special ACL entry for a non-existing group.
69 static bool file_is_valid(vfs_handle_struct *handle, const char *path,
78 DEBUG(10, ("file_is_valid (%s) called\n", path));
80 if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
81 &buf, sizeof(buf)) != sizeof(buf)) {
82 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
87 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
94 static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
104 DEBUG(10, ("marking file %s as valid\n", path));
106 ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
107 &buf, sizeof(buf), 0);
110 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
118 * Given an smb_filename, determine the stream directory using the file's
121 static char *stream_dir(vfs_handle_struct *handle,
122 const struct smb_filename *smb_fname,
123 const SMB_STRUCT_STAT *base_sbuf, bool create_it)
126 struct smb_filename *smb_fname_hash = NULL;
128 SMB_STRUCT_STAT base_sbuf_tmp;
129 uint8_t first, second;
138 check_valid = lp_parm_bool(SNUM(handle->conn),
139 "streams_depot", "check_valid", true);
141 tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
148 rootdir = lp_parm_const_string(
149 SNUM(handle->conn), "streams_depot", "directory",
152 /* Stat the base file if it hasn't already been done. */
153 if (base_sbuf == NULL) {
154 struct smb_filename *smb_fname_base = NULL;
156 status = create_synthetic_smb_fname(talloc_tos(),
157 smb_fname->base_name,
160 if (!NT_STATUS_IS_OK(status)) {
161 errno = map_errno_from_nt_status(status);
164 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
165 TALLOC_FREE(smb_fname_base);
168 base_sbuf_tmp = smb_fname_base->st;
169 TALLOC_FREE(smb_fname_base);
171 base_sbuf_tmp = *base_sbuf;
174 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
176 push_file_id_16((char *)id_buf, &id);
178 hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
181 second = (hash >> 8) & 0xff;
183 id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
185 if (id_hex == NULL) {
190 result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
191 first, second, id_hex);
195 if (result == NULL) {
200 status = create_synthetic_smb_fname(talloc_tos(), result, NULL, NULL,
202 if (!NT_STATUS_IS_OK(status)) {
203 errno = map_errno_from_nt_status(status);
207 if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
208 struct smb_filename *smb_fname_new = NULL;
211 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
216 if (file_is_valid(handle, smb_fname->base_name, check_valid)) {
221 * Someone has recreated a file under an existing inode
222 * without deleting the streams directory. For now, just move
227 newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
228 if (newname == NULL) {
233 status = create_synthetic_smb_fname(talloc_tos(), newname,
236 TALLOC_FREE(newname);
237 if (!NT_STATUS_IS_OK(status)) {
238 errno = map_errno_from_nt_status(status);
242 if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
243 smb_fname_new) == -1) {
244 TALLOC_FREE(smb_fname_new);
245 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
251 TALLOC_FREE(smb_fname_new);
259 if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
260 && (errno != EEXIST)) {
264 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
270 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
271 && (errno != EEXIST)) {
277 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
284 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
285 && (errno != EEXIST)) {
291 if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
292 && (errno != EEXIST)) {
296 if (!mark_file_valid(handle, smb_fname->base_name, check_valid)) {
300 TALLOC_FREE(smb_fname_hash);
304 TALLOC_FREE(smb_fname_hash);
309 * Given a stream name, populate smb_fname_out with the actual location of the
312 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
313 const struct smb_filename *smb_fname,
314 struct smb_filename **smb_fname_out,
317 char *dirname, *stream_fname;
321 *smb_fname_out = NULL;
323 stype = strchr_m(smb_fname->stream_name + 1, ':');
326 if (StrCaseCmp(stype, ":$DATA") != 0) {
327 return NT_STATUS_INVALID_PARAMETER;
331 dirname = stream_dir(handle, smb_fname, NULL, create_dir);
333 if (dirname == NULL) {
334 status = map_nt_error_from_unix(errno);
338 stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
339 smb_fname->stream_name);
341 if (stream_fname == NULL) {
342 status = NT_STATUS_NO_MEMORY;
347 /* Append an explicit stream type if one wasn't specified. */
348 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
350 if (stream_fname == NULL) {
351 status = NT_STATUS_NO_MEMORY;
355 /* Normalize the stream type to upercase. */
356 strupper_m(strrchr_m(stream_fname, ':') + 1);
359 DEBUG(10, ("stream filename = %s\n", stream_fname));
361 /* Create an smb_filename with stream_name == NULL. */
362 status = create_synthetic_smb_fname(talloc_tos(), stream_fname, NULL,
363 NULL, smb_fname_out);
364 if (!NT_STATUS_IS_OK(status)) {
371 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
372 TALLOC_FREE(*smb_fname_out);
376 static NTSTATUS walk_streams(vfs_handle_struct *handle,
377 struct smb_filename *smb_fname_base,
379 bool (*fn)(const char *dirname,
385 SMB_STRUCT_DIR *dirhandle = NULL;
386 const char *dirent = NULL;
387 char *talloced = NULL;
389 dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
392 if (dirname == NULL) {
393 if (errno == ENOENT) {
399 return map_nt_error_from_unix(errno);
402 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
404 dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
406 if (dirhandle == NULL) {
407 TALLOC_FREE(dirname);
408 return map_nt_error_from_unix(errno);
411 while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL,
412 &talloced)) != NULL) {
414 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
415 TALLOC_FREE(talloced);
419 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
421 if (!fn(dirname, dirent, private_data)) {
422 TALLOC_FREE(talloced);
425 TALLOC_FREE(talloced);
428 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
430 if (pdirname != NULL) {
434 TALLOC_FREE(dirname);
441 * Helper to stat/lstat the base file of an smb_fname. This will actually
442 * fills in the stat struct in smb_filename.
444 static int streams_depot_stat_base(vfs_handle_struct *handle,
445 struct smb_filename *smb_fname,
448 char *tmp_stream_name;
451 tmp_stream_name = smb_fname->stream_name;
452 smb_fname->stream_name = NULL;
454 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
456 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
458 smb_fname->stream_name = tmp_stream_name;
462 static int streams_depot_stat(vfs_handle_struct *handle,
463 struct smb_filename *smb_fname)
465 struct smb_filename *smb_fname_stream = NULL;
469 DEBUG(10, ("streams_depot_stat called for [%s]\n",
470 smb_fname_str_dbg(smb_fname)));
472 if (!is_ntfs_stream_smb_fname(smb_fname)) {
473 return SMB_VFS_NEXT_STAT(handle, smb_fname);
476 /* If the default stream is requested, just stat the base file. */
477 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
478 return streams_depot_stat_base(handle, smb_fname, true);
481 /* Stat the actual stream now. */
482 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
484 if (!NT_STATUS_IS_OK(status)) {
486 errno = map_errno_from_nt_status(status);
490 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
492 /* Update the original smb_fname with the stat info. */
493 smb_fname->st = smb_fname_stream->st;
495 TALLOC_FREE(smb_fname_stream);
501 static int streams_depot_lstat(vfs_handle_struct *handle,
502 struct smb_filename *smb_fname)
504 struct smb_filename *smb_fname_stream = NULL;
508 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
509 smb_fname_str_dbg(smb_fname)));
511 if (!is_ntfs_stream_smb_fname(smb_fname)) {
512 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
515 /* If the default stream is requested, just stat the base file. */
516 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
517 return streams_depot_stat_base(handle, smb_fname, false);
520 /* Stat the actual stream now. */
521 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
523 if (!NT_STATUS_IS_OK(status)) {
525 errno = map_errno_from_nt_status(status);
529 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
532 TALLOC_FREE(smb_fname_stream);
536 static int streams_depot_open(vfs_handle_struct *handle,
537 struct smb_filename *smb_fname,
538 files_struct *fsp, int flags, mode_t mode)
540 struct smb_filename *smb_fname_stream = NULL;
541 struct smb_filename *smb_fname_base = NULL;
545 if (!is_ntfs_stream_smb_fname(smb_fname)) {
546 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
549 /* If the default stream is requested, just open the base file. */
550 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
551 char *tmp_stream_name;
553 tmp_stream_name = smb_fname->stream_name;
554 smb_fname->stream_name = NULL;
555 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
556 smb_fname->stream_name = tmp_stream_name;
561 /* Ensure the base file still exists. */
562 status = create_synthetic_smb_fname(talloc_tos(),
563 smb_fname->base_name,
566 if (!NT_STATUS_IS_OK(status)) {
568 errno = map_errno_from_nt_status(status);
572 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
577 /* Determine the stream name, and then open it. */
578 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
579 if (!NT_STATUS_IS_OK(status)) {
581 errno = map_errno_from_nt_status(status);
585 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
588 TALLOC_FREE(smb_fname_stream);
589 TALLOC_FREE(smb_fname_base);
593 static int streams_depot_unlink(vfs_handle_struct *handle,
594 const struct smb_filename *smb_fname)
596 struct smb_filename *smb_fname_base = NULL;
600 DEBUG(10, ("streams_depot_unlink called for %s\n",
601 smb_fname_str_dbg(smb_fname)));
603 /* If there is a valid stream, just unlink the stream and return. */
604 if (is_ntfs_stream_smb_fname(smb_fname) &&
605 !is_ntfs_default_stream_smb_fname(smb_fname)) {
606 struct smb_filename *smb_fname_stream = NULL;
608 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
610 if (!NT_STATUS_IS_OK(status)) {
611 errno = map_errno_from_nt_status(status);
615 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
617 TALLOC_FREE(smb_fname_stream);
622 * We potentially need to delete the per-inode streams directory
625 status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
626 NULL, NULL, &smb_fname_base);
627 if (!NT_STATUS_IS_OK(status)) {
628 errno = map_errno_from_nt_status(status);
632 if (lp_posix_pathnames()) {
633 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
635 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
639 TALLOC_FREE(smb_fname_base);
643 if (smb_fname_base->st.st_ex_nlink == 1) {
644 char *dirname = stream_dir(handle, smb_fname_base,
645 &smb_fname_base->st, false);
647 if (dirname != NULL) {
648 SMB_VFS_NEXT_RMDIR(handle, dirname);
650 TALLOC_FREE(dirname);
653 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
655 TALLOC_FREE(smb_fname_base);
659 static int streams_depot_rename(vfs_handle_struct *handle,
660 const struct smb_filename *smb_fname_src,
661 const struct smb_filename *smb_fname_dst)
663 struct smb_filename *smb_fname_src_stream = NULL;
664 struct smb_filename *smb_fname_dst_stream = NULL;
665 bool src_is_stream, dst_is_stream;
669 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
670 smb_fname_str_dbg(smb_fname_src),
671 smb_fname_str_dbg(smb_fname_dst)));
673 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
674 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
676 if (!src_is_stream && !dst_is_stream) {
677 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
681 /* for now don't allow renames from or to the default stream */
682 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
683 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
688 status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
690 if (!NT_STATUS_IS_OK(status)) {
691 errno = map_errno_from_nt_status(status);
695 status = stream_smb_fname(handle, smb_fname_dst,
696 &smb_fname_dst_stream, false);
697 if (!NT_STATUS_IS_OK(status)) {
698 errno = map_errno_from_nt_status(status);
702 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
703 smb_fname_dst_stream);
706 TALLOC_FREE(smb_fname_src_stream);
707 TALLOC_FREE(smb_fname_dst_stream);
711 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
712 struct stream_struct **streams,
713 const char *name, SMB_OFF_T size,
714 SMB_OFF_T alloc_size)
716 struct stream_struct *tmp;
718 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
724 tmp[*num_streams].name = talloc_strdup(tmp, name);
725 if (tmp[*num_streams].name == NULL) {
729 tmp[*num_streams].size = size;
730 tmp[*num_streams].alloc_size = alloc_size;
737 struct streaminfo_state {
739 vfs_handle_struct *handle;
740 unsigned int num_streams;
741 struct stream_struct *streams;
745 static bool collect_one_stream(const char *dirname,
749 struct streaminfo_state *state =
750 (struct streaminfo_state *)private_data;
751 struct smb_filename *smb_fname = NULL;
756 sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
758 state->status = NT_STATUS_NO_MEMORY;
763 status = create_synthetic_smb_fname(talloc_tos(), sname, NULL,
765 if (!NT_STATUS_IS_OK(status)) {
766 state->status = status;
771 if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
772 DEBUG(10, ("Could not stat %s: %s\n", sname,
778 if (!add_one_stream(state->mem_ctx,
779 &state->num_streams, &state->streams,
780 dirent, smb_fname->st.st_ex_size,
781 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
783 state->status = NT_STATUS_NO_MEMORY;
791 TALLOC_FREE(smb_fname);
795 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
796 struct files_struct *fsp,
799 unsigned int *pnum_streams,
800 struct stream_struct **pstreams)
802 struct smb_filename *smb_fname_base = NULL;
805 struct streaminfo_state state;
807 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
809 if (!NT_STATUS_IS_OK(status)) {
813 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
814 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
817 if (lp_posix_pathnames()) {
818 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
820 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
825 status = map_nt_error_from_unix(errno);
829 state.streams = NULL;
830 state.num_streams = 0;
832 if (!S_ISDIR(smb_fname_base->st.st_ex_mode)) {
833 if (!add_one_stream(mem_ctx,
834 &state.num_streams, &state.streams,
835 "::$DATA", smb_fname_base->st.st_ex_size,
836 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
837 &smb_fname_base->st))) {
838 status = NT_STATUS_NO_MEMORY;
843 state.mem_ctx = mem_ctx;
844 state.handle = handle;
845 state.status = NT_STATUS_OK;
847 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
850 if (!NT_STATUS_IS_OK(status)) {
851 TALLOC_FREE(state.streams);
855 if (!NT_STATUS_IS_OK(state.status)) {
856 TALLOC_FREE(state.streams);
857 status = state.status;
861 *pnum_streams = state.num_streams;
862 *pstreams = state.streams;
863 status = NT_STATUS_OK;
866 TALLOC_FREE(smb_fname_base);
870 static uint32_t streams_depot_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 struct vfs_fn_pointers vfs_streams_depot_fns = {
877 .fs_capabilities = streams_depot_fs_capabilities,
878 .open = streams_depot_open,
879 .stat = streams_depot_stat,
880 .lstat = streams_depot_lstat,
881 .unlink = streams_depot_unlink,
882 .rename = streams_depot_rename,
883 .streaminfo = streams_depot_streaminfo,
886 NTSTATUS vfs_streams_depot_init(void);
887 NTSTATUS vfs_streams_depot_init(void)
889 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
890 &vfs_streams_depot_fns);