2 * Store streams in xattrs
4 * Copyright (C) Volker Lendecke, 2008
6 * Partly based on James Peach's Darwin module, which is
8 * Copyright (C) James Peach 2006-2007
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #define DBGC_CLASS DBGC_VFS
29 #define XATTR_DOSSTREAM_PREFIX "user.DosStream."
36 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
38 struct MD5Context ctx;
39 unsigned char hash[16];
43 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
44 (unsigned long)sbuf->st_dev,
45 (unsigned long)sbuf->st_ino, sname));
47 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
48 SMB_ASSERT(upper_sname != NULL);
51 MD5Update(&ctx, (unsigned char *)&(sbuf->st_dev),
52 sizeof(sbuf->st_dev));
53 MD5Update(&ctx, (unsigned char *)&(sbuf->st_ino),
54 sizeof(sbuf->st_ino));
55 MD5Update(&ctx, (unsigned char *)upper_sname,
56 talloc_get_size(upper_sname)-1);
59 TALLOC_FREE(upper_sname);
61 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
62 memcpy(&result, hash, sizeof(result));
64 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
69 static ssize_t get_xattr_size(connection_struct *conn, const char *fname,
70 const char *xattr_name)
76 status = get_ea_value(talloc_tos(), conn, NULL, fname,
79 if (!NT_STATUS_IS_OK(status)) {
83 result = ea.value.length-1;
84 TALLOC_FREE(ea.value.data);
89 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
90 SMB_STRUCT_STAT *sbuf)
92 struct stream_io *io = (struct stream_io *)
93 VFS_FETCH_FSP_EXTENSION(handle, fsp);
95 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
98 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
101 if (SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf) == -1) {
105 sbuf->st_size = get_xattr_size(handle->conn, io->base, io->xattr_name);
106 if (sbuf->st_size == -1) {
110 DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf->st_size));
112 sbuf->st_ino = stream_inode(sbuf, io->xattr_name);
113 sbuf->st_mode &= ~S_IFMT;
114 sbuf->st_mode |= S_IFREG;
115 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
120 static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
121 SMB_STRUCT_STAT *sbuf)
128 if (!is_ntfs_stream_name(fname)) {
129 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
132 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
133 if (!NT_STATUS_IS_OK(status)) {
138 if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
142 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
143 XATTR_DOSSTREAM_PREFIX, sname);
144 if (xattr_name == NULL) {
149 sbuf->st_size = get_xattr_size(handle->conn, base, xattr_name);
150 if (sbuf->st_size == -1) {
155 sbuf->st_ino = stream_inode(sbuf, xattr_name);
156 sbuf->st_mode &= ~S_IFMT;
157 sbuf->st_mode |= S_IFREG;
158 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
167 static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
168 SMB_STRUCT_STAT *sbuf)
175 if (!is_ntfs_stream_name(fname)) {
176 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
179 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
180 if (!NT_STATUS_IS_OK(status)) {
185 if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
189 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
190 XATTR_DOSSTREAM_PREFIX, sname);
191 if (xattr_name == NULL) {
196 sbuf->st_size = get_xattr_size(handle->conn, base, xattr_name);
197 if (sbuf->st_size == -1) {
202 sbuf->st_ino = stream_inode(sbuf, xattr_name);
203 sbuf->st_mode &= ~S_IFMT;
204 sbuf->st_mode |= S_IFREG;
205 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
214 static int streams_xattr_open(vfs_handle_struct *handle, const char *fname,
215 files_struct *fsp, int flags, mode_t mode)
219 struct stream_io *sio;
226 DEBUG(10, ("streams_xattr_open called for %s\n", fname));
228 if (!is_ntfs_stream_name(fname)) {
229 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
232 frame = talloc_stackframe();
234 status = split_ntfs_stream_name(talloc_tos(), fname,
236 if (!NT_STATUS_IS_OK(status)) {
241 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
242 XATTR_DOSSTREAM_PREFIX, sname);
243 if (xattr_name == NULL) {
249 * We use baseflags to turn off nasty side-effects when opening the
253 baseflags &= ~O_TRUNC;
254 baseflags &= ~O_EXCL;
255 baseflags &= ~O_CREAT;
257 hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
259 /* It is legit to open a stream on a directory, but the base
260 * fd has to be read-only.
262 if ((hostfd == -1) && (errno == EISDIR)) {
263 baseflags &= ~O_ACCMODE;
264 baseflags |= O_RDONLY;
265 hostfd = SMB_VFS_OPEN(handle->conn, fname, fsp, baseflags,
273 status = get_ea_value(talloc_tos(), handle->conn, NULL, base,
276 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
278 if (!NT_STATUS_IS_OK(status)
279 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
281 * The base file is not there. This is an error even if we got
282 * O_CREAT, the higher levels should have created the base
285 DEBUG(10, ("streams_xattr_open: base file %s not around, "
286 "returning ENOENT\n", base));
291 if (!NT_STATUS_IS_OK(status)) {
293 * The attribute does not exist
296 if (flags & O_CREAT) {
298 * Darn, xattrs need at least 1 byte
302 DEBUG(10, ("creating attribute %s on file %s\n",
305 if (SMB_VFS_SETXATTR(
306 handle->conn, base, xattr_name,
308 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
314 if (flags & O_TRUNC) {
316 if (SMB_VFS_SETXATTR(
317 handle->conn, base, xattr_name,
319 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
324 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
331 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
333 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
336 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
347 * BUGBUGBUG -- we would need to call fd_close_posix here, but
348 * we don't have a full fsp yet
350 SMB_VFS_CLOSE(fsp, hostfd);
357 static int streams_xattr_unlink(vfs_handle_struct *handle, const char *fname)
365 if (!is_ntfs_stream_name(fname)) {
366 return SMB_VFS_NEXT_UNLINK(handle, fname);
369 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
370 if (!NT_STATUS_IS_OK(status)) {
375 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
376 XATTR_DOSSTREAM_PREFIX, sname);
377 if (xattr_name == NULL) {
382 ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
384 if ((ret == -1) && (errno == ENOATTR)) {
397 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
399 bool (*fn)(struct ea_struct *ea,
406 size_t prefix_len = strlen(XATTR_DOSSTREAM_PREFIX);
408 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
410 if (!NT_STATUS_IS_OK(status)) {
414 for (i=0; i<num_names; i++) {
417 if (strncmp(names[i], XATTR_DOSSTREAM_PREFIX,
422 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
423 if (!NT_STATUS_IS_OK(status)) {
424 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
425 names[i], fname, nt_errstr(status)));
429 ea.name = talloc_asprintf(ea.value.data, ":%s",
430 names[i] + prefix_len);
431 if (ea.name == NULL) {
432 DEBUG(0, ("talloc failed\n"));
436 if (!fn(&ea, private_data)) {
437 TALLOC_FREE(ea.value.data);
441 TALLOC_FREE(ea.value.data);
448 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
449 struct stream_struct **streams,
450 const char *name, SMB_OFF_T size,
451 SMB_OFF_T alloc_size)
453 struct stream_struct *tmp;
455 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
461 tmp[*num_streams].name = talloc_strdup(tmp, name);
462 if (tmp[*num_streams].name == NULL) {
466 tmp[*num_streams].size = size;
467 tmp[*num_streams].alloc_size = alloc_size;
474 struct streaminfo_state {
476 vfs_handle_struct *handle;
477 unsigned int num_streams;
478 struct stream_struct *streams;
482 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
484 struct streaminfo_state *state =
485 (struct streaminfo_state *)private_data;
487 if (!add_one_stream(state->mem_ctx,
488 &state->num_streams, &state->streams,
489 ea->name, ea->value.length-1,
490 smb_roundup(state->handle->conn,
491 ea->value.length-1))) {
492 state->status = NT_STATUS_NO_MEMORY;
499 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
500 struct files_struct *fsp,
503 unsigned int *pnum_streams,
504 struct stream_struct **pstreams)
506 SMB_STRUCT_STAT sbuf;
509 struct streaminfo_state state;
511 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
512 if (is_ntfs_stream_name(fsp->fsp_name)) {
513 return NT_STATUS_INVALID_PARAMETER;
515 ret = SMB_VFS_FSTAT(fsp, &sbuf);
518 if (is_ntfs_stream_name(fname)) {
519 return NT_STATUS_INVALID_PARAMETER;
521 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
525 return map_nt_error_from_unix(errno);
528 state.streams = NULL;
529 state.num_streams = 0;
531 if (!S_ISDIR(sbuf.st_mode)) {
532 if (!add_one_stream(mem_ctx,
533 &state.num_streams, &state.streams,
534 "::$DATA", sbuf.st_size,
535 get_allocation_size(handle->conn, fsp,
537 return NT_STATUS_NO_MEMORY;
541 state.mem_ctx = mem_ctx;
542 state.handle = handle;
543 state.status = NT_STATUS_OK;
545 status = walk_xattr_streams(handle->conn, fsp, fname,
546 collect_one_stream, &state);
548 if (!NT_STATUS_IS_OK(status)) {
549 TALLOC_FREE(state.streams);
553 if (!NT_STATUS_IS_OK(state.status)) {
554 TALLOC_FREE(state.streams);
558 *pnum_streams = state.num_streams;
559 *pstreams = state.streams;
563 static int streams_xattr_statvfs(struct vfs_handle_struct *handle,
565 struct vfs_statvfs_struct *statbuf)
569 ret = SMB_VFS_NEXT_STATVFS(handle, path, statbuf);
570 statbuf->FsCapabilities |= FILE_NAMED_STREAMS;
575 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
576 files_struct *fsp, const void *data,
577 size_t n, SMB_OFF_T offset)
579 struct stream_io *sio =
580 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
585 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
588 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
591 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
592 sio->base, sio->xattr_name, &ea);
593 if (!NT_STATUS_IS_OK(status)) {
597 if ((offset + n) > ea.value.length-1) {
600 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
604 TALLOC_FREE(ea.value.data);
609 ea.value.length = offset + n + 1;
610 ea.value.data[offset+n] = 0;
613 memcpy(ea.value.data + offset, data, n);
615 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
617 ea.value.data, ea.value.length, 0);
619 TALLOC_FREE(ea.value.data);
624 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
625 files_struct *fsp, void *data,
626 size_t n, SMB_OFF_T offset)
628 struct stream_io *sio =
629 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
632 size_t length, overlap;
635 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
638 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
639 sio->base, sio->xattr_name, &ea);
640 if (!NT_STATUS_IS_OK(status)) {
644 length = ea.value.length-1;
646 /* Attempt to read past EOF. */
647 if (length <= offset) {
652 overlap = (offset + n) > length ? (length - offset) : n;
653 memcpy(data, ea.value.data + offset, overlap);
655 TALLOC_FREE(ea.value.data);
659 /* VFS operations structure */
661 static vfs_op_tuple streams_xattr_ops[] = {
662 {SMB_VFS_OP(streams_xattr_statvfs), SMB_VFS_OP_STATVFS,
663 SMB_VFS_LAYER_TRANSPARENT},
664 {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
665 SMB_VFS_LAYER_TRANSPARENT},
666 {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
667 SMB_VFS_LAYER_TRANSPARENT},
668 {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
669 SMB_VFS_LAYER_TRANSPARENT},
670 {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
671 SMB_VFS_LAYER_TRANSPARENT},
672 {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
673 SMB_VFS_LAYER_TRANSPARENT},
674 {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
675 SMB_VFS_LAYER_TRANSPARENT},
676 {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
677 SMB_VFS_LAYER_TRANSPARENT},
678 {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
679 SMB_VFS_LAYER_TRANSPARENT},
680 {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
681 SMB_VFS_LAYER_OPAQUE},
682 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
685 NTSTATUS vfs_streams_xattr_init(void);
686 NTSTATUS vfs_streams_xattr_init(void)
688 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",