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
34 vfs_handle_struct *handle;
37 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
39 struct MD5Context ctx;
40 unsigned char hash[16];
44 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
45 (unsigned long)sbuf->st_dev,
46 (unsigned long)sbuf->st_ino, sname));
48 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
49 SMB_ASSERT(upper_sname != NULL);
52 MD5Update(&ctx, (unsigned char *)&(sbuf->st_dev),
53 sizeof(sbuf->st_dev));
54 MD5Update(&ctx, (unsigned char *)&(sbuf->st_ino),
55 sizeof(sbuf->st_ino));
56 MD5Update(&ctx, (unsigned char *)upper_sname,
57 talloc_get_size(upper_sname)-1);
60 TALLOC_FREE(upper_sname);
62 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
63 memcpy(&result, hash, sizeof(result));
65 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
70 static ssize_t get_xattr_size(connection_struct *conn,
73 const char *xattr_name)
79 status = get_ea_value(talloc_tos(), conn, fsp, fname,
82 if (!NT_STATUS_IS_OK(status)) {
86 result = ea.value.length-1;
87 TALLOC_FREE(ea.value.data);
91 static bool streams_xattr_recheck(struct stream_io *sio)
96 char *xattr_name = NULL;
98 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
102 status = split_ntfs_stream_name(talloc_tos(), sio->fsp->fsp_name,
104 if (!NT_STATUS_IS_OK(status)) {
109 /* how can this happen */
114 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
115 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
116 if (xattr_name == NULL) {
120 TALLOC_FREE(sio->xattr_name);
121 TALLOC_FREE(sio->base);
122 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
124 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
126 sio->fsp_name_ptr = sio->fsp->fsp_name;
128 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
135 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
136 SMB_STRUCT_STAT *sbuf)
138 struct stream_io *io = (struct stream_io *)
139 VFS_FETCH_FSP_EXTENSION(handle, fsp);
141 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
143 if (io == NULL || fsp->base_fsp == NULL) {
144 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
147 if (!streams_xattr_recheck(io)) {
151 if (SMB_VFS_STAT(handle->conn, io->base, sbuf) == -1) {
155 sbuf->st_size = get_xattr_size(handle->conn, fsp->base_fsp,
156 io->base, io->xattr_name);
157 if (sbuf->st_size == -1) {
161 DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf->st_size));
163 sbuf->st_ino = stream_inode(sbuf, io->xattr_name);
164 sbuf->st_mode &= ~S_IFMT;
165 sbuf->st_mode |= S_IFREG;
166 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
171 static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
172 SMB_STRUCT_STAT *sbuf)
175 char *base = NULL, *sname = NULL;
179 if (!is_ntfs_stream_name(fname)) {
180 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
183 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
184 if (!NT_STATUS_IS_OK(status)) {
190 return SMB_VFS_NEXT_STAT(handle, base, sbuf);
193 if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
197 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
198 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
199 if (xattr_name == NULL) {
204 sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
205 if (sbuf->st_size == -1) {
210 sbuf->st_ino = stream_inode(sbuf, xattr_name);
211 sbuf->st_mode &= ~S_IFMT;
212 sbuf->st_mode |= S_IFREG;
213 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
222 static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
223 SMB_STRUCT_STAT *sbuf)
230 if (!is_ntfs_stream_name(fname)) {
231 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
234 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
235 if (!NT_STATUS_IS_OK(status)) {
241 return SMB_VFS_NEXT_LSTAT(handle, base, sbuf);
244 if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
248 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
249 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
250 if (xattr_name == NULL) {
255 sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
256 if (sbuf->st_size == -1) {
261 sbuf->st_ino = stream_inode(sbuf, xattr_name);
262 sbuf->st_mode &= ~S_IFMT;
263 sbuf->st_mode |= S_IFREG;
264 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
273 static int streams_xattr_open(vfs_handle_struct *handle, const char *fname,
274 files_struct *fsp, int flags, mode_t mode)
278 struct stream_io *sio;
285 DEBUG(10, ("streams_xattr_open called for %s\n", fname));
287 if (!is_ntfs_stream_name(fname)) {
288 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
291 frame = talloc_stackframe();
293 status = split_ntfs_stream_name(talloc_tos(), fname,
295 if (!NT_STATUS_IS_OK(status)) {
301 hostfd = SMB_VFS_NEXT_OPEN(handle, base, fsp, flags, mode);
306 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
307 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
308 if (xattr_name == NULL) {
314 * We use baseflags to turn off nasty side-effects when opening the
318 baseflags &= ~O_TRUNC;
319 baseflags &= ~O_EXCL;
320 baseflags &= ~O_CREAT;
322 hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
324 /* It is legit to open a stream on a directory, but the base
325 * fd has to be read-only.
327 if ((hostfd == -1) && (errno == EISDIR)) {
328 baseflags &= ~O_ACCMODE;
329 baseflags |= O_RDONLY;
330 hostfd = SMB_VFS_OPEN(handle->conn, fname, fsp, baseflags,
338 status = get_ea_value(talloc_tos(), handle->conn, NULL, base,
341 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
343 if (!NT_STATUS_IS_OK(status)
344 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
346 * The base file is not there. This is an error even if we got
347 * O_CREAT, the higher levels should have created the base
350 DEBUG(10, ("streams_xattr_open: base file %s not around, "
351 "returning ENOENT\n", base));
356 if (!NT_STATUS_IS_OK(status)) {
358 * The attribute does not exist
361 if (flags & O_CREAT) {
363 * Darn, xattrs need at least 1 byte
367 DEBUG(10, ("creating attribute %s on file %s\n",
370 if (fsp->base_fsp->fh->fd != -1) {
371 if (SMB_VFS_FSETXATTR(
372 fsp->base_fsp, xattr_name,
374 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
378 if (SMB_VFS_SETXATTR(
379 handle->conn, base, xattr_name,
381 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
388 if (flags & O_TRUNC) {
390 if (fsp->base_fsp->fh->fd != -1) {
391 if (SMB_VFS_FSETXATTR(
392 fsp->base_fsp, xattr_name,
394 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
398 if (SMB_VFS_SETXATTR(
399 handle->conn, base, xattr_name,
401 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
407 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
414 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
416 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
418 sio->fsp_name_ptr = fsp->fsp_name;
419 sio->handle = handle;
422 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
433 * BUGBUGBUG -- we would need to call fd_close_posix here, but
434 * we don't have a full fsp yet
443 static int streams_xattr_unlink(vfs_handle_struct *handle, const char *fname)
451 if (!is_ntfs_stream_name(fname)) {
452 return SMB_VFS_NEXT_UNLINK(handle, fname);
455 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
456 if (!NT_STATUS_IS_OK(status)) {
462 return SMB_VFS_NEXT_UNLINK(handle, base);
465 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
466 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
467 if (xattr_name == NULL) {
472 ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
474 if ((ret == -1) && (errno == ENOATTR)) {
487 static int streams_xattr_rename(vfs_handle_struct *handle,
492 TALLOC_CTX *frame = NULL;
507 o_is_stream = is_ntfs_stream_name(oldname);
508 n_is_stream = is_ntfs_stream_name(newname);
510 if (!o_is_stream && !n_is_stream) {
511 return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
514 frame = talloc_stackframe();
519 status = split_ntfs_stream_name(talloc_tos(), oldname, &obase, &ostream);
520 if (!NT_STATUS_IS_OK(status)) {
525 status = split_ntfs_stream_name(talloc_tos(), newname, &nbase, &nstream);
526 if (!NT_STATUS_IS_OK(status)) {
531 /*TODO: maybe call SMB_VFS_NEXT_RENAME() both streams are NULL (::$DATA) */
532 if (ostream == NULL) {
537 if (nstream == NULL) {
542 if (StrCaseCmp(ostream, nstream) == 0) {
548 oxattr_name = talloc_asprintf(talloc_tos(), "%s%s",
549 SAMBA_XATTR_DOSSTREAM_PREFIX, ostream);
550 if (oxattr_name == NULL) {
555 nxattr_name = talloc_asprintf(talloc_tos(), "%s%s",
556 SAMBA_XATTR_DOSSTREAM_PREFIX, nstream);
557 if (nxattr_name == NULL) {
562 /* read the old stream */
563 status = get_ea_value(talloc_tos(), handle->conn, NULL,
564 base, oxattr_name, &ea);
565 if (!NT_STATUS_IS_OK(status)) {
570 /* (over)write the new stream */
571 nret = SMB_VFS_SETXATTR(handle->conn, base, nxattr_name,
572 ea.value.data, ea.value.length, 0);
574 if (errno == ENOATTR) {
580 /* remove the old stream */
581 oret = SMB_VFS_REMOVEXATTR(handle->conn, base, oxattr_name);
583 if (errno == ENOATTR) {
597 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
599 bool (*fn)(struct ea_struct *ea,
606 size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
608 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
610 if (!NT_STATUS_IS_OK(status)) {
614 for (i=0; i<num_names; i++) {
617 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
622 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
623 if (!NT_STATUS_IS_OK(status)) {
624 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
625 names[i], fname, nt_errstr(status)));
629 ea.name = talloc_asprintf(ea.value.data, ":%s",
630 names[i] + prefix_len);
631 if (ea.name == NULL) {
632 DEBUG(0, ("talloc failed\n"));
636 if (!fn(&ea, private_data)) {
637 TALLOC_FREE(ea.value.data);
641 TALLOC_FREE(ea.value.data);
648 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
649 struct stream_struct **streams,
650 const char *name, SMB_OFF_T size,
651 SMB_OFF_T alloc_size)
653 struct stream_struct *tmp;
655 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
661 tmp[*num_streams].name = talloc_strdup(tmp, name);
662 if (tmp[*num_streams].name == NULL) {
666 tmp[*num_streams].size = size;
667 tmp[*num_streams].alloc_size = alloc_size;
674 struct streaminfo_state {
676 vfs_handle_struct *handle;
677 unsigned int num_streams;
678 struct stream_struct *streams;
682 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
684 struct streaminfo_state *state =
685 (struct streaminfo_state *)private_data;
687 if (!add_one_stream(state->mem_ctx,
688 &state->num_streams, &state->streams,
689 ea->name, ea->value.length-1,
690 smb_roundup(state->handle->conn,
691 ea->value.length-1))) {
692 state->status = NT_STATUS_NO_MEMORY;
699 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
700 struct files_struct *fsp,
703 unsigned int *pnum_streams,
704 struct stream_struct **pstreams)
706 SMB_STRUCT_STAT sbuf;
709 struct streaminfo_state state;
711 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
712 if (is_ntfs_stream_name(fsp->fsp_name)) {
713 return NT_STATUS_INVALID_PARAMETER;
715 ret = SMB_VFS_FSTAT(fsp, &sbuf);
718 if (is_ntfs_stream_name(fname)) {
719 return NT_STATUS_INVALID_PARAMETER;
721 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
725 return map_nt_error_from_unix(errno);
728 state.streams = NULL;
729 state.num_streams = 0;
731 if (!S_ISDIR(sbuf.st_mode)) {
732 if (!add_one_stream(mem_ctx,
733 &state.num_streams, &state.streams,
734 "::$DATA", sbuf.st_size,
735 get_allocation_size(handle->conn, fsp,
737 return NT_STATUS_NO_MEMORY;
741 state.mem_ctx = mem_ctx;
742 state.handle = handle;
743 state.status = NT_STATUS_OK;
745 status = walk_xattr_streams(handle->conn, fsp, fname,
746 collect_one_stream, &state);
748 if (!NT_STATUS_IS_OK(status)) {
749 TALLOC_FREE(state.streams);
753 if (!NT_STATUS_IS_OK(state.status)) {
754 TALLOC_FREE(state.streams);
758 *pnum_streams = state.num_streams;
759 *pstreams = state.streams;
763 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle)
765 return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
768 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
769 files_struct *fsp, const void *data,
770 size_t n, SMB_OFF_T offset)
772 struct stream_io *sio =
773 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
778 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
781 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
784 if (!streams_xattr_recheck(sio)) {
788 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
789 sio->base, sio->xattr_name, &ea);
790 if (!NT_STATUS_IS_OK(status)) {
794 if ((offset + n) > ea.value.length-1) {
797 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
801 TALLOC_FREE(ea.value.data);
806 ea.value.length = offset + n + 1;
807 ea.value.data[offset+n] = 0;
810 memcpy(ea.value.data + offset, data, n);
812 if (fsp->base_fsp->fh->fd != -1) {
813 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
815 ea.value.data, ea.value.length, 0);
817 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
819 ea.value.data, ea.value.length, 0);
821 TALLOC_FREE(ea.value.data);
830 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
831 files_struct *fsp, void *data,
832 size_t n, SMB_OFF_T offset)
834 struct stream_io *sio =
835 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
838 size_t length, overlap;
841 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
844 if (!streams_xattr_recheck(sio)) {
848 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
849 sio->base, sio->xattr_name, &ea);
850 if (!NT_STATUS_IS_OK(status)) {
854 length = ea.value.length-1;
856 /* Attempt to read past EOF. */
857 if (length <= offset) {
862 overlap = (offset + n) > length ? (length - offset) : n;
863 memcpy(data, ea.value.data + offset, overlap);
865 TALLOC_FREE(ea.value.data);
869 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
870 struct files_struct *fsp,
877 struct stream_io *sio =
878 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
880 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
885 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
888 if (!streams_xattr_recheck(sio)) {
892 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
893 sio->base, sio->xattr_name, &ea);
894 if (!NT_STATUS_IS_OK(status)) {
898 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
902 TALLOC_FREE(ea.value.data);
907 /* Did we expand ? */
908 if (ea.value.length < offset + 1) {
909 memset(&tmp[ea.value.length], '\0',
910 offset + 1 - ea.value.length);
914 ea.value.length = offset + 1;
915 ea.value.data[offset] = 0;
917 if (fsp->base_fsp->fh->fd != -1) {
918 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
920 ea.value.data, ea.value.length, 0);
922 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
924 ea.value.data, ea.value.length, 0);
927 TALLOC_FREE(ea.value.data);
936 /* VFS operations structure */
938 static vfs_op_tuple streams_xattr_ops[] = {
939 {SMB_VFS_OP(streams_xattr_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
940 SMB_VFS_LAYER_TRANSPARENT},
941 {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
942 SMB_VFS_LAYER_TRANSPARENT},
943 {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
944 SMB_VFS_LAYER_TRANSPARENT},
945 {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
946 SMB_VFS_LAYER_TRANSPARENT},
947 {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
948 SMB_VFS_LAYER_TRANSPARENT},
949 {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
950 SMB_VFS_LAYER_TRANSPARENT},
951 {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
952 SMB_VFS_LAYER_TRANSPARENT},
953 {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
954 SMB_VFS_LAYER_TRANSPARENT},
955 {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
956 SMB_VFS_LAYER_TRANSPARENT},
957 {SMB_VFS_OP(streams_xattr_rename), SMB_VFS_OP_RENAME,
958 SMB_VFS_LAYER_TRANSPARENT},
959 {SMB_VFS_OP(streams_xattr_ftruncate), SMB_VFS_OP_FTRUNCATE,
960 SMB_VFS_LAYER_TRANSPARENT},
961 {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
962 SMB_VFS_LAYER_OPAQUE},
963 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
966 NTSTATUS vfs_streams_xattr_init(void);
967 NTSTATUS vfs_streams_xattr_init(void)
969 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",