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
);
96 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
99 if (SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
) == -1) {
103 sbuf
->st_size
= get_xattr_size(handle
->conn
, io
->base
, io
->xattr_name
);
104 if (sbuf
->st_size
== -1) {
108 sbuf
->st_ino
= stream_inode(sbuf
, io
->xattr_name
);
109 sbuf
->st_mode
&= ~S_IFMT
;
110 sbuf
->st_mode
|= S_IFREG
;
111 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
116 static int streams_xattr_stat(vfs_handle_struct
*handle
, const char *fname
,
117 SMB_STRUCT_STAT
*sbuf
)
124 if (!is_ntfs_stream_name(fname
)) {
125 return SMB_VFS_NEXT_STAT(handle
, fname
, sbuf
);
128 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
129 if (!NT_STATUS_IS_OK(status
)) {
134 if (SMB_VFS_STAT(handle
->conn
, base
, sbuf
) == -1) {
138 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
139 XATTR_DOSSTREAM_PREFIX
, sname
);
140 if (xattr_name
== NULL
) {
145 sbuf
->st_size
= get_xattr_size(handle
->conn
, base
, xattr_name
);
146 if (sbuf
->st_size
== -1) {
151 sbuf
->st_ino
= stream_inode(sbuf
, xattr_name
);
152 sbuf
->st_mode
&= ~S_IFMT
;
153 sbuf
->st_mode
|= S_IFREG
;
154 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
163 static int streams_xattr_lstat(vfs_handle_struct
*handle
, const char *fname
,
164 SMB_STRUCT_STAT
*sbuf
)
171 if (!is_ntfs_stream_name(fname
)) {
172 return SMB_VFS_NEXT_LSTAT(handle
, fname
, sbuf
);
175 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
176 if (!NT_STATUS_IS_OK(status
)) {
181 if (SMB_VFS_LSTAT(handle
->conn
, base
, sbuf
) == -1) {
185 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
186 XATTR_DOSSTREAM_PREFIX
, sname
);
187 if (xattr_name
== NULL
) {
192 sbuf
->st_size
= get_xattr_size(handle
->conn
, base
, xattr_name
);
193 if (sbuf
->st_size
== -1) {
198 sbuf
->st_ino
= stream_inode(sbuf
, xattr_name
);
199 sbuf
->st_mode
&= ~S_IFMT
;
200 sbuf
->st_mode
|= S_IFREG
;
201 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
210 static int streams_xattr_open(vfs_handle_struct
*handle
, const char *fname
,
211 files_struct
*fsp
, int flags
, mode_t mode
)
215 struct stream_io
*sio
;
222 if (!is_ntfs_stream_name(fname
)) {
223 return SMB_VFS_NEXT_OPEN(handle
, fname
, fsp
, flags
, mode
);
226 frame
= talloc_stackframe();
228 status
= split_ntfs_stream_name(talloc_tos(), fname
,
230 if (!NT_STATUS_IS_OK(status
)) {
235 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
236 XATTR_DOSSTREAM_PREFIX
, sname
);
237 if (xattr_name
== NULL
) {
243 * We use baseflags to turn off nasty side-effects when opening the
247 baseflags
&= ~O_TRUNC
;
248 baseflags
&= ~O_EXCL
;
249 baseflags
&= ~O_CREAT
;
251 hostfd
= SMB_VFS_OPEN(handle
->conn
, base
, fsp
, baseflags
, mode
);
253 /* It is legit to open a stream on a directory, but the base
254 * fd has to be read-only.
256 if ((hostfd
== -1) && (errno
== EISDIR
)) {
257 baseflags
&= ~O_ACCMODE
;
258 baseflags
|= O_RDONLY
;
259 hostfd
= SMB_VFS_OPEN(handle
->conn
, fname
, fsp
, baseflags
,
267 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
, base
,
270 if (!NT_STATUS_IS_OK(status
)
271 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
273 * The base file is not there. This is an error even if we got
274 * O_CREAT, the higher levels should have created the base
281 if (!NT_STATUS_IS_OK(status
)) {
283 * The attribute does not exist
286 if (flags
& O_CREAT
) {
288 * Darn, xattrs need at least 1 byte
292 DEBUG(10, ("creating attribute %s on file %s\n",
295 if (SMB_VFS_SETXATTR(
296 handle
->conn
, base
, xattr_name
,
298 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
304 if (flags
& O_TRUNC
) {
306 if (SMB_VFS_SETXATTR(
307 handle
->conn
, base
, xattr_name
,
309 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
314 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
321 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
323 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
326 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
337 * BUGBUGBUG -- we would need to call fd_close_posix here, but
338 * we don't have a full fsp yet
340 SMB_VFS_CLOSE(fsp
, hostfd
);
347 static int streams_xattr_unlink(vfs_handle_struct
*handle
, const char *fname
)
355 if (!is_ntfs_stream_name(fname
)) {
356 return SMB_VFS_NEXT_UNLINK(handle
, fname
);
359 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
360 if (!NT_STATUS_IS_OK(status
)) {
365 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
366 XATTR_DOSSTREAM_PREFIX
, sname
);
367 if (xattr_name
== NULL
) {
372 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, base
, xattr_name
);
374 if ((ret
== -1) && (errno
== ENOATTR
)) {
387 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
389 bool (*fn
)(struct ea_struct
*ea
,
396 size_t prefix_len
= strlen(XATTR_DOSSTREAM_PREFIX
);
398 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
400 if (!NT_STATUS_IS_OK(status
)) {
404 for (i
=0; i
<num_names
; i
++) {
407 if (strncmp(names
[i
], XATTR_DOSSTREAM_PREFIX
,
412 status
= get_ea_value(names
, conn
, fsp
, fname
, names
[i
], &ea
);
413 if (!NT_STATUS_IS_OK(status
)) {
414 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
415 names
[i
], fname
, nt_errstr(status
)));
419 ea
.name
= talloc_asprintf(ea
.value
.data
, ":%s",
420 names
[i
] + prefix_len
);
421 if (ea
.name
== NULL
) {
422 DEBUG(0, ("talloc failed\n"));
426 if (!fn(&ea
, private_data
)) {
427 TALLOC_FREE(ea
.value
.data
);
431 TALLOC_FREE(ea
.value
.data
);
438 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
439 struct stream_struct
**streams
,
440 const char *name
, SMB_OFF_T size
,
441 SMB_OFF_T alloc_size
)
443 struct stream_struct
*tmp
;
445 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
451 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
452 if (tmp
[*num_streams
].name
== NULL
) {
456 tmp
[*num_streams
].size
= size
;
457 tmp
[*num_streams
].alloc_size
= alloc_size
;
464 struct streaminfo_state
{
466 vfs_handle_struct
*handle
;
467 unsigned int num_streams
;
468 struct stream_struct
*streams
;
472 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
474 struct streaminfo_state
*state
=
475 (struct streaminfo_state
*)private_data
;
477 if (!add_one_stream(state
->mem_ctx
,
478 &state
->num_streams
, &state
->streams
,
479 ea
->name
, ea
->value
.length
-1,
480 smb_roundup(state
->handle
->conn
,
481 ea
->value
.length
-1))) {
482 state
->status
= NT_STATUS_NO_MEMORY
;
489 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
490 struct files_struct
*fsp
,
493 unsigned int *pnum_streams
,
494 struct stream_struct
**pstreams
)
496 SMB_STRUCT_STAT sbuf
;
499 struct streaminfo_state state
;
501 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
502 if (is_ntfs_stream_name(fsp
->fsp_name
)) {
503 return NT_STATUS_INVALID_PARAMETER
;
505 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
508 if (is_ntfs_stream_name(fname
)) {
509 return NT_STATUS_INVALID_PARAMETER
;
511 ret
= SMB_VFS_STAT(handle
->conn
, fname
, &sbuf
);
515 return map_nt_error_from_unix(errno
);
518 state
.streams
= NULL
;
519 state
.num_streams
= 0;
521 if (!S_ISDIR(sbuf
.st_mode
)) {
522 if (!add_one_stream(mem_ctx
,
523 &state
.num_streams
, &state
.streams
,
524 "::$DATA", sbuf
.st_size
,
525 get_allocation_size(handle
->conn
, fsp
,
527 return NT_STATUS_NO_MEMORY
;
531 state
.mem_ctx
= mem_ctx
;
532 state
.handle
= handle
;
533 state
.status
= NT_STATUS_OK
;
535 status
= walk_xattr_streams(handle
->conn
, fsp
, fname
,
536 collect_one_stream
, &state
);
538 if (!NT_STATUS_IS_OK(status
)) {
539 TALLOC_FREE(state
.streams
);
543 if (!NT_STATUS_IS_OK(state
.status
)) {
544 TALLOC_FREE(state
.streams
);
548 *pnum_streams
= state
.num_streams
;
549 *pstreams
= state
.streams
;
553 static int streams_xattr_statvfs(struct vfs_handle_struct
*handle
,
555 struct vfs_statvfs_struct
*statbuf
)
559 ret
= SMB_VFS_NEXT_STATVFS(handle
, path
, statbuf
);
560 statbuf
->FsCapabilities
|= FILE_NAMED_STREAMS
;
565 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
566 files_struct
*fsp
, const void *data
,
567 size_t n
, SMB_OFF_T offset
)
569 struct stream_io
*sio
=
570 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
576 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
579 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
580 sio
->base
, sio
->xattr_name
, &ea
);
581 if (!NT_STATUS_IS_OK(status
)) {
585 if ((offset
+ n
) > ea
.value
.length
-1) {
588 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
592 TALLOC_FREE(ea
.value
.data
);
597 ea
.value
.length
= offset
+ n
+ 1;
598 ea
.value
.data
[offset
+n
] = 0;
601 memcpy(ea
.value
.data
+ offset
, data
, n
);
603 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
, sio
->xattr_name
,
604 ea
.value
.data
, ea
.value
.length
, 0);
606 TALLOC_FREE(ea
.value
.data
);
611 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
612 files_struct
*fsp
, void *data
,
613 size_t n
, SMB_OFF_T offset
)
615 struct stream_io
*sio
=
616 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
619 size_t length
, overlap
;
622 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
625 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
626 sio
->base
, sio
->xattr_name
, &ea
);
627 if (!NT_STATUS_IS_OK(status
)) {
631 length
= ea
.value
.length
-1;
633 /* Attempt to read past EOF. */
634 if (length
<= offset
) {
639 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
640 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
642 TALLOC_FREE(ea
.value
.data
);
646 /* VFS operations structure */
648 static vfs_op_tuple streams_xattr_ops
[] = {
649 {SMB_VFS_OP(streams_xattr_statvfs
), SMB_VFS_OP_STATVFS
,
650 SMB_VFS_LAYER_TRANSPARENT
},
651 {SMB_VFS_OP(streams_xattr_open
), SMB_VFS_OP_OPEN
,
652 SMB_VFS_LAYER_TRANSPARENT
},
653 {SMB_VFS_OP(streams_xattr_stat
), SMB_VFS_OP_STAT
,
654 SMB_VFS_LAYER_TRANSPARENT
},
655 {SMB_VFS_OP(streams_xattr_fstat
), SMB_VFS_OP_FSTAT
,
656 SMB_VFS_LAYER_TRANSPARENT
},
657 {SMB_VFS_OP(streams_xattr_lstat
), SMB_VFS_OP_LSTAT
,
658 SMB_VFS_LAYER_TRANSPARENT
},
659 {SMB_VFS_OP(streams_xattr_pread
), SMB_VFS_OP_PREAD
,
660 SMB_VFS_LAYER_TRANSPARENT
},
661 {SMB_VFS_OP(streams_xattr_pwrite
), SMB_VFS_OP_PWRITE
,
662 SMB_VFS_LAYER_TRANSPARENT
},
663 {SMB_VFS_OP(streams_xattr_lstat
), SMB_VFS_OP_LSTAT
,
664 SMB_VFS_LAYER_TRANSPARENT
},
665 {SMB_VFS_OP(streams_xattr_unlink
), SMB_VFS_OP_UNLINK
,
666 SMB_VFS_LAYER_TRANSPARENT
},
667 {SMB_VFS_OP(streams_xattr_streaminfo
), SMB_VFS_OP_STREAMINFO
,
668 SMB_VFS_LAYER_OPAQUE
},
669 {SMB_VFS_OP(NULL
), SMB_VFS_OP_NOOP
, SMB_VFS_LAYER_NOOP
}
672 NTSTATUS
vfs_streams_xattr_init(void);
673 NTSTATUS
vfs_streams_xattr_init(void)
675 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",