Revert c76e4791d3ae4f180fba209c29bd3774c00858cf - "Fix the logic bug that caused...
[Samba.git] / source / modules / vfs_streams_depot.c
blobc530d2adf064f2367fe7785a9615f705bb4758ee
1 /*
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/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
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
34 * name.
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
60 * directory.
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)
72 char buf;
74 DEBUG(10, ("file_is_valid (%s) called\n", path));
76 if (SMB_VFS_NEXT_GETXATTR(handle, path, SAMBA_XATTR_MARKER,
77 &buf, sizeof(buf)) != sizeof(buf)) {
78 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
79 return false;
82 if (buf != '1') {
83 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
84 return false;
87 return true;
90 static bool mark_file_valid(vfs_handle_struct *handle, const char *path)
92 char buf = '1';
93 int ret;
95 DEBUG(10, ("marking file %s as valid\n", path));
97 ret = SMB_VFS_NEXT_SETXATTR(handle, path, SAMBA_XATTR_MARKER,
98 &buf, sizeof(buf), 0);
100 if (ret == -1) {
101 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
102 return false;
105 return true;
108 static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
109 const SMB_STRUCT_STAT *base_sbuf, bool create_it)
111 uint32_t hash;
112 char *result = NULL;
113 SMB_STRUCT_STAT sbuf;
114 uint8_t first, second;
115 char *tmp;
116 char *id_hex;
117 struct file_id id;
118 uint8 id_buf[16];
119 const char *rootdir;
121 tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
123 if (tmp == NULL) {
124 errno = ENOMEM;
125 goto fail;
128 rootdir = lp_parm_const_string(
129 SNUM(handle->conn), "streams_depot", "directory",
130 tmp);
131 TALLOC_FREE(tmp);
133 if (base_sbuf == NULL) {
134 if (SMB_VFS_NEXT_STAT(handle, base_path, &sbuf) == -1) {
136 * base file is not there
138 goto fail;
140 base_sbuf = &sbuf;
143 id = SMB_VFS_FILE_ID_CREATE(handle->conn, base_sbuf->st_dev,
144 base_sbuf->st_ino);
146 push_file_id_16((char *)id_buf, &id);
148 hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
150 first = hash & 0xff;
151 second = (hash >> 8) & 0xff;
153 id_hex = hex_encode(talloc_tos(), id_buf, sizeof(id_buf));
155 if (id_hex == NULL) {
156 errno = ENOMEM;
157 goto fail;
160 result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
161 first, second, id_hex);
163 TALLOC_FREE(id_hex);
165 if (result == NULL) {
166 errno = ENOMEM;
167 return NULL;
170 if (SMB_VFS_NEXT_STAT(handle, result, &sbuf) == 0) {
171 char *newname;
173 if (!S_ISDIR(sbuf.st_mode)) {
174 errno = EINVAL;
175 goto fail;
178 if (file_is_valid(handle, base_path)) {
179 return result;
183 * Someone has recreated a file under an existing inode
184 * without deleting the streams directory. For now, just move
185 * it away.
188 again:
189 newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
190 if (newname == NULL) {
191 errno = ENOMEM;
192 goto fail;
195 if (SMB_VFS_NEXT_RENAME(handle, result, newname) == -1) {
196 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
197 TALLOC_FREE(newname);
198 goto again;
200 goto fail;
203 TALLOC_FREE(newname);
206 if (!create_it) {
207 errno = ENOENT;
208 goto fail;
211 if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
212 && (errno != EEXIST)) {
213 goto fail;
216 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
217 if (tmp == NULL) {
218 errno = ENOMEM;
219 goto fail;
222 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
223 && (errno != EEXIST)) {
224 goto fail;
227 TALLOC_FREE(tmp);
229 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
230 second);
231 if (tmp == NULL) {
232 errno = ENOMEM;
233 goto fail;
236 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
237 && (errno != EEXIST)) {
238 goto fail;
241 TALLOC_FREE(tmp);
243 if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
244 && (errno != EEXIST)) {
245 goto fail;
248 if (!mark_file_valid(handle, base_path)) {
249 goto fail;
252 return result;
254 fail:
255 TALLOC_FREE(result);
256 return NULL;
259 static char *stream_name(vfs_handle_struct *handle, const char *fname,
260 bool create_dir)
262 char *base = NULL;
263 char *sname = NULL;
264 char *id_hex = NULL;
265 char *dirname, *stream_fname;
267 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
268 &base, &sname))) {
269 DEBUG(10, ("split_ntfs_stream_name failed\n"));
270 errno = ENOMEM;
271 goto fail;
274 dirname = stream_dir(handle, base, NULL, create_dir);
276 if (dirname == NULL) {
277 goto fail;
280 stream_fname = talloc_asprintf(talloc_tos(), "%s/:%s", dirname, sname);
282 if (stream_fname == NULL) {
283 errno = ENOMEM;
284 goto fail;
287 DEBUG(10, ("stream filename = %s\n", stream_fname));
289 TALLOC_FREE(base);
290 TALLOC_FREE(sname);
291 TALLOC_FREE(id_hex);
293 return stream_fname;
295 fail:
296 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
297 TALLOC_FREE(base);
298 TALLOC_FREE(sname);
299 TALLOC_FREE(id_hex);
300 return NULL;
303 static NTSTATUS walk_streams(vfs_handle_struct *handle,
304 const char *fname,
305 const SMB_STRUCT_STAT *sbuf,
306 char **pdirname,
307 bool (*fn)(const char *dirname,
308 const char *dirent,
309 void *private_data),
310 void *private_data)
312 char *dirname;
313 SMB_STRUCT_DIR *dirhandle = NULL;
314 char *dirent;
316 dirname = stream_dir(handle, fname, sbuf, false);
318 if (dirname == NULL) {
319 if (errno == ENOENT) {
321 * no stream around
323 return NT_STATUS_OK;
325 return map_nt_error_from_unix(errno);
328 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
330 dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
332 if (dirhandle == NULL) {
333 TALLOC_FREE(dirname);
334 return map_nt_error_from_unix(errno);
337 while ((dirent = vfs_readdirname(handle->conn, dirhandle)) != NULL) {
339 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
340 continue;
343 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
345 if (!fn(dirname, dirent, private_data)) {
346 break;
350 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
352 if (pdirname != NULL) {
353 *pdirname = dirname;
355 else {
356 TALLOC_FREE(dirname);
359 return NT_STATUS_OK;
362 static int streams_depot_stat(vfs_handle_struct *handle, const char *fname,
363 SMB_STRUCT_STAT *sbuf)
365 char *stream_fname;
366 int ret = -1;
368 DEBUG(10, ("streams_depot_stat called for [%s]\n", fname));
370 if (!is_ntfs_stream_name(fname)) {
371 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
374 stream_fname = stream_name(handle, fname, false);
375 if (stream_fname == NULL) {
376 goto done;
379 ret = SMB_VFS_NEXT_STAT(handle, stream_fname, sbuf);
381 done:
382 TALLOC_FREE(stream_fname);
383 return ret;
386 static int streams_depot_lstat(vfs_handle_struct *handle, const char *fname,
387 SMB_STRUCT_STAT *sbuf)
389 char *stream_fname;
390 int ret = -1;
392 if (!is_ntfs_stream_name(fname)) {
393 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
396 stream_fname = stream_name(handle, fname, false);
397 if (stream_fname == NULL) {
398 goto done;
401 ret = SMB_VFS_NEXT_LSTAT(handle, stream_fname, sbuf);
403 done:
404 TALLOC_FREE(stream_fname);
405 return ret;
408 static int streams_depot_open(vfs_handle_struct *handle, const char *fname,
409 files_struct *fsp, int flags, mode_t mode)
411 TALLOC_CTX *frame;
412 char *base = NULL;
413 SMB_STRUCT_STAT base_sbuf;
414 char *stream_fname;
415 int ret = -1;
417 if (!is_ntfs_stream_name(fname)) {
418 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
421 frame = talloc_stackframe();
423 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
424 &base, NULL))) {
425 errno = ENOMEM;
426 goto done;
429 ret = SMB_VFS_NEXT_STAT(handle, base, &base_sbuf);
431 if (ret == -1) {
432 goto done;
435 TALLOC_FREE(base);
437 stream_fname = stream_name(handle, fname, true);
438 if (stream_fname == NULL) {
439 goto done;
442 ret = SMB_VFS_NEXT_OPEN(handle, stream_fname, fsp, flags, mode);
444 done:
445 TALLOC_FREE(frame);
446 return ret;
449 static int streams_depot_unlink(vfs_handle_struct *handle, const char *fname)
451 int ret = -1;
452 SMB_STRUCT_STAT sbuf;
454 DEBUG(10, ("streams_depot_unlink called for %s\n", fname));
456 if (is_ntfs_stream_name(fname)) {
457 char *stream_fname;
459 stream_fname = stream_name(handle, fname, false);
460 if (stream_fname == NULL) {
461 return -1;
464 ret = SMB_VFS_NEXT_UNLINK(handle, stream_fname);
466 TALLOC_FREE(stream_fname);
467 return ret;
471 * We potentially need to delete the per-inode streams directory
474 if (SMB_VFS_NEXT_STAT(handle, fname, &sbuf) == -1) {
475 return -1;
478 if (sbuf.st_nlink == 1) {
479 char *dirname = stream_dir(handle, fname, &sbuf, false);
481 if (dirname != NULL) {
482 SMB_VFS_NEXT_RMDIR(handle, dirname);
484 TALLOC_FREE(dirname);
487 return SMB_VFS_NEXT_UNLINK(handle, fname);
490 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
491 struct stream_struct **streams,
492 const char *name, SMB_OFF_T size,
493 SMB_OFF_T alloc_size)
495 struct stream_struct *tmp;
497 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
498 (*num_streams)+1);
499 if (tmp == NULL) {
500 return false;
503 tmp[*num_streams].name = talloc_strdup(tmp, name);
504 if (tmp[*num_streams].name == NULL) {
505 return false;
508 tmp[*num_streams].size = size;
509 tmp[*num_streams].alloc_size = alloc_size;
511 *streams = tmp;
512 *num_streams += 1;
513 return true;
516 struct streaminfo_state {
517 TALLOC_CTX *mem_ctx;
518 vfs_handle_struct *handle;
519 unsigned int num_streams;
520 struct stream_struct *streams;
521 NTSTATUS status;
524 static bool collect_one_stream(const char *dirname,
525 const char *dirent,
526 void *private_data)
528 struct streaminfo_state *state =
529 (struct streaminfo_state *)private_data;
530 char *full_sname;
531 SMB_STRUCT_STAT sbuf;
533 if (asprintf(&full_sname, "%s/%s", dirname, dirent) == -1) {
534 state->status = NT_STATUS_NO_MEMORY;
535 return false;
537 if (SMB_VFS_NEXT_STAT(state->handle, full_sname, &sbuf) == -1) {
538 DEBUG(10, ("Could not stat %s: %s\n", full_sname,
539 strerror(errno)));
540 SAFE_FREE(full_sname);
541 return true;
544 SAFE_FREE(full_sname);
546 if (!add_one_stream(state->mem_ctx,
547 &state->num_streams, &state->streams,
548 dirent, sbuf.st_size,
549 get_allocation_size(
550 state->handle->conn, NULL, &sbuf))) {
551 state->status = NT_STATUS_NO_MEMORY;
552 return false;
555 return true;
558 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
559 struct files_struct *fsp,
560 const char *fname,
561 TALLOC_CTX *mem_ctx,
562 unsigned int *pnum_streams,
563 struct stream_struct **pstreams)
565 SMB_STRUCT_STAT sbuf;
566 int ret;
567 NTSTATUS status;
568 struct streaminfo_state state;
570 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
571 if (is_ntfs_stream_name(fsp->fsp_name)) {
572 return NT_STATUS_INVALID_PARAMETER;
574 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf);
576 else {
577 if (is_ntfs_stream_name(fname)) {
578 return NT_STATUS_INVALID_PARAMETER;
580 ret = SMB_VFS_NEXT_STAT(handle, fname, &sbuf);
583 if (ret == -1) {
584 return map_nt_error_from_unix(errno);
587 state.streams = NULL;
588 state.num_streams = 0;
590 if (!S_ISDIR(sbuf.st_mode)) {
591 if (!add_one_stream(mem_ctx,
592 &state.num_streams, &state.streams,
593 "::$DATA", sbuf.st_size,
594 get_allocation_size(handle->conn, fsp,
595 &sbuf))) {
596 return NT_STATUS_NO_MEMORY;
600 state.mem_ctx = mem_ctx;
601 state.handle = handle;
602 state.status = NT_STATUS_OK;
604 status = walk_streams(handle, fname, &sbuf, NULL, collect_one_stream,
605 &state);
607 if (!NT_STATUS_IS_OK(status)) {
608 TALLOC_FREE(state.streams);
609 return status;
612 if (!NT_STATUS_IS_OK(state.status)) {
613 TALLOC_FREE(state.streams);
614 return state.status;
617 *pnum_streams = state.num_streams;
618 *pstreams = state.streams;
619 return NT_STATUS_OK;
622 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle)
624 return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
627 /* VFS operations structure */
629 static vfs_op_tuple streams_depot_ops[] = {
630 {SMB_VFS_OP(streams_depot_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
631 SMB_VFS_LAYER_TRANSPARENT},
632 {SMB_VFS_OP(streams_depot_open), SMB_VFS_OP_OPEN,
633 SMB_VFS_LAYER_TRANSPARENT},
634 {SMB_VFS_OP(streams_depot_stat), SMB_VFS_OP_STAT,
635 SMB_VFS_LAYER_TRANSPARENT},
636 {SMB_VFS_OP(streams_depot_lstat), SMB_VFS_OP_LSTAT,
637 SMB_VFS_LAYER_TRANSPARENT},
638 {SMB_VFS_OP(streams_depot_unlink), SMB_VFS_OP_UNLINK,
639 SMB_VFS_LAYER_TRANSPARENT},
640 {SMB_VFS_OP(streams_depot_streaminfo), SMB_VFS_OP_STREAMINFO,
641 SMB_VFS_LAYER_OPAQUE},
642 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
645 NTSTATUS vfs_streams_depot_init(void);
646 NTSTATUS vfs_streams_depot_init(void)
648 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
649 streams_depot_ops);