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_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(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;
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)) != NULL) {
413 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
417 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
419 if (!fn(dirname, dirent, private_data)) {
424 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
426 if (pdirname != NULL) {
430 TALLOC_FREE(dirname);
437 * Helper to stat/lstat the base file of an smb_fname. This will actually
438 * fills in the stat struct in smb_filename.
440 static int streams_depot_stat_base(vfs_handle_struct *handle,
441 struct smb_filename *smb_fname,
444 char *tmp_stream_name;
447 tmp_stream_name = smb_fname->stream_name;
448 smb_fname->stream_name = NULL;
450 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
452 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
454 smb_fname->stream_name = tmp_stream_name;
458 static int streams_depot_stat(vfs_handle_struct *handle,
459 struct smb_filename *smb_fname)
461 struct smb_filename *smb_fname_stream = NULL;
465 DEBUG(10, ("streams_depot_stat called for [%s]\n",
466 smb_fname_str_dbg(smb_fname)));
468 if (!is_ntfs_stream_smb_fname(smb_fname)) {
469 return SMB_VFS_NEXT_STAT(handle, smb_fname);
472 /* If the default stream is requested, just stat the base file. */
473 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
474 return streams_depot_stat_base(handle, smb_fname, true);
477 /* Stat the actual stream now. */
478 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
480 if (!NT_STATUS_IS_OK(status)) {
482 errno = map_errno_from_nt_status(status);
486 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
488 /* Update the original smb_fname with the stat info. */
489 smb_fname->st = smb_fname_stream->st;
491 TALLOC_FREE(smb_fname_stream);
497 static int streams_depot_lstat(vfs_handle_struct *handle,
498 struct smb_filename *smb_fname)
500 struct smb_filename *smb_fname_stream = NULL;
504 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
505 smb_fname_str_dbg(smb_fname)));
507 if (!is_ntfs_stream_smb_fname(smb_fname)) {
508 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
511 /* If the default stream is requested, just stat the base file. */
512 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
513 return streams_depot_stat_base(handle, smb_fname, false);
516 /* Stat the actual stream now. */
517 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
519 if (!NT_STATUS_IS_OK(status)) {
521 errno = map_errno_from_nt_status(status);
525 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
528 TALLOC_FREE(smb_fname_stream);
532 static int streams_depot_open(vfs_handle_struct *handle,
533 struct smb_filename *smb_fname,
534 files_struct *fsp, int flags, mode_t mode)
536 struct smb_filename *smb_fname_stream = NULL;
537 struct smb_filename *smb_fname_base = NULL;
541 if (!is_ntfs_stream_smb_fname(smb_fname)) {
542 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
545 /* If the default stream is requested, just open the base file. */
546 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
547 char *tmp_stream_name;
549 tmp_stream_name = smb_fname->stream_name;
550 smb_fname->stream_name = NULL;
551 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
552 smb_fname->stream_name = tmp_stream_name;
557 /* Ensure the base file still exists. */
558 status = create_synthetic_smb_fname(talloc_tos(),
559 smb_fname->base_name,
562 if (!NT_STATUS_IS_OK(status)) {
564 errno = map_errno_from_nt_status(status);
568 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
573 /* Determine the stream name, and then open it. */
574 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
575 if (!NT_STATUS_IS_OK(status)) {
577 errno = map_errno_from_nt_status(status);
581 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
584 TALLOC_FREE(smb_fname_stream);
585 TALLOC_FREE(smb_fname_base);
589 static int streams_depot_unlink(vfs_handle_struct *handle,
590 const struct smb_filename *smb_fname)
592 struct smb_filename *smb_fname_base = NULL;
596 DEBUG(10, ("streams_depot_unlink called for %s\n",
597 smb_fname_str_dbg(smb_fname)));
599 /* If there is a valid stream, just unlink the stream and return. */
600 if (is_ntfs_stream_smb_fname(smb_fname) &&
601 !is_ntfs_default_stream_smb_fname(smb_fname)) {
602 struct smb_filename *smb_fname_stream = NULL;
604 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
606 if (!NT_STATUS_IS_OK(status)) {
607 errno = map_errno_from_nt_status(status);
611 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
613 TALLOC_FREE(smb_fname_stream);
618 * We potentially need to delete the per-inode streams directory
621 status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
622 NULL, NULL, &smb_fname_base);
623 if (!NT_STATUS_IS_OK(status)) {
624 errno = map_errno_from_nt_status(status);
628 if (lp_posix_pathnames()) {
629 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
631 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
635 TALLOC_FREE(smb_fname_base);
639 if (smb_fname_base->st.st_ex_nlink == 1) {
640 char *dirname = stream_dir(handle, smb_fname_base,
641 &smb_fname_base->st, false);
643 if (dirname != NULL) {
644 SMB_VFS_NEXT_RMDIR(handle, dirname);
646 TALLOC_FREE(dirname);
649 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
651 TALLOC_FREE(smb_fname_base);
655 static int streams_depot_rename(vfs_handle_struct *handle,
656 const struct smb_filename *smb_fname_src,
657 const struct smb_filename *smb_fname_dst)
659 struct smb_filename *smb_fname_src_stream = NULL;
660 struct smb_filename *smb_fname_dst_stream = NULL;
661 struct smb_filename *smb_fname_dst_mod = NULL;
662 bool src_is_stream, dst_is_stream;
666 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
667 smb_fname_str_dbg(smb_fname_src),
668 smb_fname_str_dbg(smb_fname_dst)));
670 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
671 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
673 if (!src_is_stream && !dst_is_stream) {
674 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
678 /* for now don't allow renames from or to the default stream */
679 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
680 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
685 status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
687 if (!NT_STATUS_IS_OK(status)) {
688 errno = map_errno_from_nt_status(status);
693 * Handle passing in a stream name without the base file. This is
694 * exercised by the NTRENAME streams rename path.
696 if (StrCaseCmp(smb_fname_dst->base_name, "./") == 0) {
697 status = create_synthetic_smb_fname(talloc_tos(),
698 smb_fname_src->base_name,
699 smb_fname_dst->stream_name,
700 NULL, &smb_fname_dst_mod);
701 if (!NT_STATUS_IS_OK(status)) {
702 errno = map_errno_from_nt_status(status);
707 status = stream_smb_fname(handle, (smb_fname_dst_mod ?
708 smb_fname_dst_mod : smb_fname_dst),
709 &smb_fname_dst_stream, false);
710 if (!NT_STATUS_IS_OK(status)) {
711 errno = map_errno_from_nt_status(status);
715 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
716 smb_fname_dst_stream);
719 TALLOC_FREE(smb_fname_src_stream);
720 TALLOC_FREE(smb_fname_dst_stream);
721 TALLOC_FREE(smb_fname_dst_mod);
725 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
726 struct stream_struct **streams,
727 const char *name, SMB_OFF_T size,
728 SMB_OFF_T alloc_size)
730 struct stream_struct *tmp;
732 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
738 tmp[*num_streams].name = talloc_strdup(tmp, name);
739 if (tmp[*num_streams].name == NULL) {
743 tmp[*num_streams].size = size;
744 tmp[*num_streams].alloc_size = alloc_size;
751 struct streaminfo_state {
753 vfs_handle_struct *handle;
754 unsigned int num_streams;
755 struct stream_struct *streams;
759 static bool collect_one_stream(const char *dirname,
763 struct streaminfo_state *state =
764 (struct streaminfo_state *)private_data;
765 struct smb_filename *smb_fname = NULL;
770 sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
772 state->status = NT_STATUS_NO_MEMORY;
777 status = create_synthetic_smb_fname(talloc_tos(), sname, NULL,
779 if (!NT_STATUS_IS_OK(status)) {
780 state->status = status;
785 if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
786 DEBUG(10, ("Could not stat %s: %s\n", sname,
792 if (!add_one_stream(state->mem_ctx,
793 &state->num_streams, &state->streams,
794 dirent, smb_fname->st.st_ex_size,
795 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
797 state->status = NT_STATUS_NO_MEMORY;
805 TALLOC_FREE(smb_fname);
809 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
810 struct files_struct *fsp,
813 unsigned int *pnum_streams,
814 struct stream_struct **pstreams)
816 struct smb_filename *smb_fname_base = NULL;
819 struct streaminfo_state state;
821 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
823 if (!NT_STATUS_IS_OK(status)) {
827 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
828 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
831 if (lp_posix_pathnames()) {
832 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
834 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
839 status = map_nt_error_from_unix(errno);
843 state.streams = NULL;
844 state.num_streams = 0;
846 if (!S_ISDIR(smb_fname_base->st.st_ex_mode)) {
847 if (!add_one_stream(mem_ctx,
848 &state.num_streams, &state.streams,
849 "::$DATA", smb_fname_base->st.st_ex_size,
850 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
851 &smb_fname_base->st))) {
852 status = NT_STATUS_NO_MEMORY;
857 state.mem_ctx = mem_ctx;
858 state.handle = handle;
859 state.status = NT_STATUS_OK;
861 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
864 if (!NT_STATUS_IS_OK(status)) {
865 TALLOC_FREE(state.streams);
869 if (!NT_STATUS_IS_OK(state.status)) {
870 TALLOC_FREE(state.streams);
871 status = state.status;
875 *pnum_streams = state.num_streams;
876 *pstreams = state.streams;
877 status = NT_STATUS_OK;
880 TALLOC_FREE(smb_fname_base);
884 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle)
886 return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
889 static struct vfs_fn_pointers vfs_streams_depot_fns = {
890 .fs_capabilities = streams_depot_fs_capabilities,
891 .open = streams_depot_open,
892 .stat = streams_depot_stat,
893 .lstat = streams_depot_lstat,
894 .unlink = streams_depot_unlink,
895 .rename = streams_depot_rename,
896 .streaminfo = streams_depot_streaminfo,
899 NTSTATUS vfs_streams_depot_init(void);
900 NTSTATUS vfs_streams_depot_init(void)
902 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
903 &vfs_streams_depot_fns);