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 static SMB_INO_T
stream_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
36 struct MD5Context ctx
;
37 unsigned char hash
[16];
41 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
42 (unsigned long)sbuf
->st_dev
,
43 (unsigned long)sbuf
->st_ino
, sname
));
45 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
46 SMB_ASSERT(upper_sname
!= NULL
);
49 MD5Update(&ctx
, (unsigned char *)&(sbuf
->st_dev
),
50 sizeof(sbuf
->st_dev
));
51 MD5Update(&ctx
, (unsigned char *)&(sbuf
->st_ino
),
52 sizeof(sbuf
->st_ino
));
53 MD5Update(&ctx
, (unsigned char *)upper_sname
,
54 talloc_get_size(upper_sname
)-1);
57 TALLOC_FREE(upper_sname
);
59 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
60 memcpy(&result
, hash
, sizeof(result
));
62 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result
));
67 static ssize_t
get_xattr_size(connection_struct
*conn
,
70 const char *xattr_name
)
76 status
= get_ea_value(talloc_tos(), conn
, fsp
, 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
, fsp
->base_fsp
,
106 io
->base
, io
->xattr_name
);
107 if (sbuf
->st_size
== -1) {
111 DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf
->st_size
));
113 sbuf
->st_ino
= stream_inode(sbuf
, io
->xattr_name
);
114 sbuf
->st_mode
&= ~S_IFMT
;
115 sbuf
->st_mode
|= S_IFREG
;
116 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
121 static int streams_xattr_stat(vfs_handle_struct
*handle
, const char *fname
,
122 SMB_STRUCT_STAT
*sbuf
)
125 char *base
= NULL
, *sname
= NULL
;
129 if (!is_ntfs_stream_name(fname
)) {
130 return SMB_VFS_NEXT_STAT(handle
, fname
, sbuf
);
133 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
134 if (!NT_STATUS_IS_OK(status
)) {
139 if (SMB_VFS_STAT(handle
->conn
, base
, sbuf
) == -1) {
143 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
144 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
145 if (xattr_name
== NULL
) {
150 sbuf
->st_size
= get_xattr_size(handle
->conn
, NULL
, base
, xattr_name
);
151 if (sbuf
->st_size
== -1) {
156 sbuf
->st_ino
= stream_inode(sbuf
, xattr_name
);
157 sbuf
->st_mode
&= ~S_IFMT
;
158 sbuf
->st_mode
|= S_IFREG
;
159 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
168 static int streams_xattr_lstat(vfs_handle_struct
*handle
, const char *fname
,
169 SMB_STRUCT_STAT
*sbuf
)
176 if (!is_ntfs_stream_name(fname
)) {
177 return SMB_VFS_NEXT_LSTAT(handle
, fname
, sbuf
);
180 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
181 if (!NT_STATUS_IS_OK(status
)) {
186 if (SMB_VFS_LSTAT(handle
->conn
, base
, sbuf
) == -1) {
190 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
191 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
192 if (xattr_name
== NULL
) {
197 sbuf
->st_size
= get_xattr_size(handle
->conn
, NULL
, base
, xattr_name
);
198 if (sbuf
->st_size
== -1) {
203 sbuf
->st_ino
= stream_inode(sbuf
, xattr_name
);
204 sbuf
->st_mode
&= ~S_IFMT
;
205 sbuf
->st_mode
|= S_IFREG
;
206 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
215 static int streams_xattr_open(vfs_handle_struct
*handle
, const char *fname
,
216 files_struct
*fsp
, int flags
, mode_t mode
)
220 struct stream_io
*sio
;
227 DEBUG(10, ("streams_xattr_open called for %s\n", fname
));
229 if (!is_ntfs_stream_name(fname
)) {
230 return SMB_VFS_NEXT_OPEN(handle
, fname
, fsp
, flags
, mode
);
233 frame
= talloc_stackframe();
235 status
= split_ntfs_stream_name(talloc_tos(), fname
,
237 if (!NT_STATUS_IS_OK(status
)) {
242 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
243 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
244 if (xattr_name
== NULL
) {
250 * We use baseflags to turn off nasty side-effects when opening the
254 baseflags
&= ~O_TRUNC
;
255 baseflags
&= ~O_EXCL
;
256 baseflags
&= ~O_CREAT
;
258 hostfd
= SMB_VFS_OPEN(handle
->conn
, base
, fsp
, baseflags
, mode
);
260 /* It is legit to open a stream on a directory, but the base
261 * fd has to be read-only.
263 if ((hostfd
== -1) && (errno
== EISDIR
)) {
264 baseflags
&= ~O_ACCMODE
;
265 baseflags
|= O_RDONLY
;
266 hostfd
= SMB_VFS_OPEN(handle
->conn
, fname
, fsp
, baseflags
,
274 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
, base
,
277 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
279 if (!NT_STATUS_IS_OK(status
)
280 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
282 * The base file is not there. This is an error even if we got
283 * O_CREAT, the higher levels should have created the base
286 DEBUG(10, ("streams_xattr_open: base file %s not around, "
287 "returning ENOENT\n", base
));
292 if (!NT_STATUS_IS_OK(status
)) {
294 * The attribute does not exist
297 if (flags
& O_CREAT
) {
299 * Darn, xattrs need at least 1 byte
303 DEBUG(10, ("creating attribute %s on file %s\n",
306 if (fsp
->base_fsp
->fh
->fd
!= -1) {
307 if (SMB_VFS_FSETXATTR(
308 fsp
->base_fsp
, xattr_name
,
310 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
314 if (SMB_VFS_SETXATTR(
315 handle
->conn
, base
, xattr_name
,
317 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
324 if (flags
& O_TRUNC
) {
326 if (fsp
->base_fsp
->fh
->fd
!= -1) {
327 if (SMB_VFS_FSETXATTR(
328 fsp
->base_fsp
, xattr_name
,
330 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
334 if (SMB_VFS_SETXATTR(
335 handle
->conn
, base
, xattr_name
,
337 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
343 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
350 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
352 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
355 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
366 * BUGBUGBUG -- we would need to call fd_close_posix here, but
367 * we don't have a full fsp yet
376 static int streams_xattr_unlink(vfs_handle_struct
*handle
, const char *fname
)
384 if (!is_ntfs_stream_name(fname
)) {
385 return SMB_VFS_NEXT_UNLINK(handle
, fname
);
388 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
389 if (!NT_STATUS_IS_OK(status
)) {
394 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
395 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
396 if (xattr_name
== NULL
) {
401 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, base
, xattr_name
);
403 if ((ret
== -1) && (errno
== ENOATTR
)) {
416 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
418 bool (*fn
)(struct ea_struct
*ea
,
425 size_t prefix_len
= strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
);
427 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
429 if (!NT_STATUS_IS_OK(status
)) {
433 for (i
=0; i
<num_names
; i
++) {
436 if (strncmp(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
441 status
= get_ea_value(names
, conn
, fsp
, fname
, names
[i
], &ea
);
442 if (!NT_STATUS_IS_OK(status
)) {
443 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
444 names
[i
], fname
, nt_errstr(status
)));
448 ea
.name
= talloc_asprintf(ea
.value
.data
, ":%s",
449 names
[i
] + prefix_len
);
450 if (ea
.name
== NULL
) {
451 DEBUG(0, ("talloc failed\n"));
455 if (!fn(&ea
, private_data
)) {
456 TALLOC_FREE(ea
.value
.data
);
460 TALLOC_FREE(ea
.value
.data
);
467 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
468 struct stream_struct
**streams
,
469 const char *name
, SMB_OFF_T size
,
470 SMB_OFF_T alloc_size
)
472 struct stream_struct
*tmp
;
474 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
480 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
481 if (tmp
[*num_streams
].name
== NULL
) {
485 tmp
[*num_streams
].size
= size
;
486 tmp
[*num_streams
].alloc_size
= alloc_size
;
493 struct streaminfo_state
{
495 vfs_handle_struct
*handle
;
496 unsigned int num_streams
;
497 struct stream_struct
*streams
;
501 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
503 struct streaminfo_state
*state
=
504 (struct streaminfo_state
*)private_data
;
506 if (!add_one_stream(state
->mem_ctx
,
507 &state
->num_streams
, &state
->streams
,
508 ea
->name
, ea
->value
.length
-1,
509 smb_roundup(state
->handle
->conn
,
510 ea
->value
.length
-1))) {
511 state
->status
= NT_STATUS_NO_MEMORY
;
518 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
519 struct files_struct
*fsp
,
522 unsigned int *pnum_streams
,
523 struct stream_struct
**pstreams
)
525 SMB_STRUCT_STAT sbuf
;
528 struct streaminfo_state state
;
530 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
531 if (is_ntfs_stream_name(fsp
->fsp_name
)) {
532 return NT_STATUS_INVALID_PARAMETER
;
534 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
537 if (is_ntfs_stream_name(fname
)) {
538 return NT_STATUS_INVALID_PARAMETER
;
540 ret
= SMB_VFS_STAT(handle
->conn
, fname
, &sbuf
);
544 return map_nt_error_from_unix(errno
);
547 state
.streams
= NULL
;
548 state
.num_streams
= 0;
550 if (!S_ISDIR(sbuf
.st_mode
)) {
551 if (!add_one_stream(mem_ctx
,
552 &state
.num_streams
, &state
.streams
,
553 "::$DATA", sbuf
.st_size
,
554 get_allocation_size(handle
->conn
, fsp
,
556 return NT_STATUS_NO_MEMORY
;
560 state
.mem_ctx
= mem_ctx
;
561 state
.handle
= handle
;
562 state
.status
= NT_STATUS_OK
;
564 status
= walk_xattr_streams(handle
->conn
, fsp
, fname
,
565 collect_one_stream
, &state
);
567 if (!NT_STATUS_IS_OK(status
)) {
568 TALLOC_FREE(state
.streams
);
572 if (!NT_STATUS_IS_OK(state
.status
)) {
573 TALLOC_FREE(state
.streams
);
577 *pnum_streams
= state
.num_streams
;
578 *pstreams
= state
.streams
;
582 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
)
584 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
) | FILE_NAMED_STREAMS
;
587 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
588 files_struct
*fsp
, const void *data
,
589 size_t n
, SMB_OFF_T offset
)
591 struct stream_io
*sio
=
592 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
597 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
600 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
603 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
604 sio
->base
, sio
->xattr_name
, &ea
);
605 if (!NT_STATUS_IS_OK(status
)) {
609 if ((offset
+ n
) > ea
.value
.length
-1) {
612 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
616 TALLOC_FREE(ea
.value
.data
);
621 ea
.value
.length
= offset
+ n
+ 1;
622 ea
.value
.data
[offset
+n
] = 0;
625 memcpy(ea
.value
.data
+ offset
, data
, n
);
627 if (fsp
->base_fsp
->fh
->fd
!= -1) {
628 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
630 ea
.value
.data
, ea
.value
.length
, 0);
632 ret
= SMB_VFS_SETXATTR(fsp
->conn
, fsp
->base_fsp
->fsp_name
,
634 ea
.value
.data
, ea
.value
.length
, 0);
636 TALLOC_FREE(ea
.value
.data
);
645 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
646 files_struct
*fsp
, void *data
,
647 size_t n
, SMB_OFF_T offset
)
649 struct stream_io
*sio
=
650 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
653 size_t length
, overlap
;
656 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
659 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
660 sio
->base
, sio
->xattr_name
, &ea
);
661 if (!NT_STATUS_IS_OK(status
)) {
665 length
= ea
.value
.length
-1;
667 /* Attempt to read past EOF. */
668 if (length
<= offset
) {
673 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
674 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
676 TALLOC_FREE(ea
.value
.data
);
680 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
681 struct files_struct
*fsp
,
688 struct stream_io
*sio
=
689 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
691 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
696 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
699 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
700 sio
->base
, sio
->xattr_name
, &ea
);
701 if (!NT_STATUS_IS_OK(status
)) {
705 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
709 TALLOC_FREE(ea
.value
.data
);
714 /* Did we expand ? */
715 if (ea
.value
.length
< offset
+ 1) {
716 memset(&tmp
[ea
.value
.length
], '\0',
717 offset
+ 1 - ea
.value
.length
);
721 ea
.value
.length
= offset
+ 1;
722 ea
.value
.data
[offset
] = 0;
724 if (fsp
->base_fsp
->fh
->fd
!= -1) {
725 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
727 ea
.value
.data
, ea
.value
.length
, 0);
729 ret
= SMB_VFS_SETXATTR(fsp
->conn
, fsp
->base_fsp
->fsp_name
,
731 ea
.value
.data
, ea
.value
.length
, 0);
734 TALLOC_FREE(ea
.value
.data
);
743 /* VFS operations structure */
745 static vfs_op_tuple streams_xattr_ops
[] = {
746 {SMB_VFS_OP(streams_xattr_fs_capabilities
), SMB_VFS_OP_FS_CAPABILITIES
,
747 SMB_VFS_LAYER_TRANSPARENT
},
748 {SMB_VFS_OP(streams_xattr_open
), SMB_VFS_OP_OPEN
,
749 SMB_VFS_LAYER_TRANSPARENT
},
750 {SMB_VFS_OP(streams_xattr_stat
), SMB_VFS_OP_STAT
,
751 SMB_VFS_LAYER_TRANSPARENT
},
752 {SMB_VFS_OP(streams_xattr_fstat
), SMB_VFS_OP_FSTAT
,
753 SMB_VFS_LAYER_TRANSPARENT
},
754 {SMB_VFS_OP(streams_xattr_lstat
), SMB_VFS_OP_LSTAT
,
755 SMB_VFS_LAYER_TRANSPARENT
},
756 {SMB_VFS_OP(streams_xattr_pread
), SMB_VFS_OP_PREAD
,
757 SMB_VFS_LAYER_TRANSPARENT
},
758 {SMB_VFS_OP(streams_xattr_pwrite
), SMB_VFS_OP_PWRITE
,
759 SMB_VFS_LAYER_TRANSPARENT
},
760 {SMB_VFS_OP(streams_xattr_lstat
), SMB_VFS_OP_LSTAT
,
761 SMB_VFS_LAYER_TRANSPARENT
},
762 {SMB_VFS_OP(streams_xattr_unlink
), SMB_VFS_OP_UNLINK
,
763 SMB_VFS_LAYER_TRANSPARENT
},
764 {SMB_VFS_OP(streams_xattr_ftruncate
), SMB_VFS_OP_FTRUNCATE
,
765 SMB_VFS_LAYER_TRANSPARENT
},
766 {SMB_VFS_OP(streams_xattr_streaminfo
), SMB_VFS_OP_STREAMINFO
,
767 SMB_VFS_LAYER_OPAQUE
},
768 {SMB_VFS_OP(NULL
), SMB_VFS_OP_NOOP
, SMB_VFS_LAYER_NOOP
}
771 NTSTATUS
vfs_streams_xattr_init(void);
772 NTSTATUS
vfs_streams_xattr_init(void)
774 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",