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
, const char *fname
,
68 const char *xattr_name
)
74 status
= get_ea_value(talloc_tos(), conn
, NULL
, fname
,
77 if (!NT_STATUS_IS_OK(status
)) {
81 result
= ea
.value
.length
-1;
82 TALLOC_FREE(ea
.value
.data
);
87 static int streams_xattr_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
88 SMB_STRUCT_STAT
*sbuf
)
90 struct stream_io
*io
= (struct stream_io
*)
91 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
93 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp
->fh
->fd
));
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 DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf
->st_size
));
110 sbuf
->st_ino
= stream_inode(sbuf
, io
->xattr_name
);
111 sbuf
->st_mode
&= ~S_IFMT
;
112 sbuf
->st_mode
|= S_IFREG
;
113 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
118 static int streams_xattr_stat(vfs_handle_struct
*handle
, const char *fname
,
119 SMB_STRUCT_STAT
*sbuf
)
122 char *base
= NULL
, *sname
= NULL
;
126 if (!is_ntfs_stream_name(fname
)) {
127 return SMB_VFS_NEXT_STAT(handle
, fname
, sbuf
);
130 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
131 if (!NT_STATUS_IS_OK(status
)) {
136 if (SMB_VFS_STAT(handle
->conn
, base
, sbuf
) == -1) {
140 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
141 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
142 if (xattr_name
== NULL
) {
147 sbuf
->st_size
= get_xattr_size(handle
->conn
, base
, xattr_name
);
148 if (sbuf
->st_size
== -1) {
153 sbuf
->st_ino
= stream_inode(sbuf
, xattr_name
);
154 sbuf
->st_mode
&= ~S_IFMT
;
155 sbuf
->st_mode
|= S_IFREG
;
156 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
165 static int streams_xattr_lstat(vfs_handle_struct
*handle
, const char *fname
,
166 SMB_STRUCT_STAT
*sbuf
)
173 if (!is_ntfs_stream_name(fname
)) {
174 return SMB_VFS_NEXT_LSTAT(handle
, fname
, sbuf
);
177 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
178 if (!NT_STATUS_IS_OK(status
)) {
183 if (SMB_VFS_LSTAT(handle
->conn
, base
, sbuf
) == -1) {
187 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
188 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
189 if (xattr_name
== NULL
) {
194 sbuf
->st_size
= get_xattr_size(handle
->conn
, base
, xattr_name
);
195 if (sbuf
->st_size
== -1) {
200 sbuf
->st_ino
= stream_inode(sbuf
, xattr_name
);
201 sbuf
->st_mode
&= ~S_IFMT
;
202 sbuf
->st_mode
|= S_IFREG
;
203 sbuf
->st_blocks
= sbuf
->st_size
% STAT_ST_BLOCKSIZE
+ 1;
212 static int streams_xattr_open(vfs_handle_struct
*handle
, const char *fname
,
213 files_struct
*fsp
, int flags
, mode_t mode
)
217 struct stream_io
*sio
;
224 DEBUG(10, ("streams_xattr_open called for %s\n", fname
));
226 if (!is_ntfs_stream_name(fname
)) {
227 return SMB_VFS_NEXT_OPEN(handle
, fname
, fsp
, flags
, mode
);
230 frame
= talloc_stackframe();
232 status
= split_ntfs_stream_name(talloc_tos(), fname
,
234 if (!NT_STATUS_IS_OK(status
)) {
239 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
240 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
241 if (xattr_name
== NULL
) {
247 * We use baseflags to turn off nasty side-effects when opening the
251 baseflags
&= ~O_TRUNC
;
252 baseflags
&= ~O_EXCL
;
253 baseflags
&= ~O_CREAT
;
255 hostfd
= SMB_VFS_OPEN(handle
->conn
, base
, fsp
, baseflags
, mode
);
257 /* It is legit to open a stream on a directory, but the base
258 * fd has to be read-only.
260 if ((hostfd
== -1) && (errno
== EISDIR
)) {
261 baseflags
&= ~O_ACCMODE
;
262 baseflags
|= O_RDONLY
;
263 hostfd
= SMB_VFS_OPEN(handle
->conn
, fname
, fsp
, baseflags
,
271 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
, base
,
274 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
276 if (!NT_STATUS_IS_OK(status
)
277 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
279 * The base file is not there. This is an error even if we got
280 * O_CREAT, the higher levels should have created the base
283 DEBUG(10, ("streams_xattr_open: base file %s not around, "
284 "returning ENOENT\n", base
));
289 if (!NT_STATUS_IS_OK(status
)) {
291 * The attribute does not exist
294 if (flags
& O_CREAT
) {
296 * Darn, xattrs need at least 1 byte
300 DEBUG(10, ("creating attribute %s on file %s\n",
303 if (SMB_VFS_SETXATTR(
304 handle
->conn
, base
, xattr_name
,
306 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
312 if (flags
& O_TRUNC
) {
314 if (SMB_VFS_SETXATTR(
315 handle
->conn
, base
, xattr_name
,
317 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
322 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
329 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
331 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
334 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
345 * BUGBUGBUG -- we would need to call fd_close_posix here, but
346 * we don't have a full fsp yet
355 static int streams_xattr_unlink(vfs_handle_struct
*handle
, const char *fname
)
363 if (!is_ntfs_stream_name(fname
)) {
364 return SMB_VFS_NEXT_UNLINK(handle
, fname
);
367 status
= split_ntfs_stream_name(talloc_tos(), fname
, &base
, &sname
);
368 if (!NT_STATUS_IS_OK(status
)) {
373 xattr_name
= talloc_asprintf(talloc_tos(), "%s%s",
374 SAMBA_XATTR_DOSSTREAM_PREFIX
, sname
);
375 if (xattr_name
== NULL
) {
380 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, base
, xattr_name
);
382 if ((ret
== -1) && (errno
== ENOATTR
)) {
395 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
397 bool (*fn
)(struct ea_struct
*ea
,
404 size_t prefix_len
= strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
);
406 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
408 if (!NT_STATUS_IS_OK(status
)) {
412 for (i
=0; i
<num_names
; i
++) {
415 if (strncmp(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
420 status
= get_ea_value(names
, conn
, fsp
, fname
, names
[i
], &ea
);
421 if (!NT_STATUS_IS_OK(status
)) {
422 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
423 names
[i
], fname
, nt_errstr(status
)));
427 ea
.name
= talloc_asprintf(ea
.value
.data
, ":%s",
428 names
[i
] + prefix_len
);
429 if (ea
.name
== NULL
) {
430 DEBUG(0, ("talloc failed\n"));
434 if (!fn(&ea
, private_data
)) {
435 TALLOC_FREE(ea
.value
.data
);
439 TALLOC_FREE(ea
.value
.data
);
446 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
447 struct stream_struct
**streams
,
448 const char *name
, SMB_OFF_T size
,
449 SMB_OFF_T alloc_size
)
451 struct stream_struct
*tmp
;
453 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
459 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
460 if (tmp
[*num_streams
].name
== NULL
) {
464 tmp
[*num_streams
].size
= size
;
465 tmp
[*num_streams
].alloc_size
= alloc_size
;
472 struct streaminfo_state
{
474 vfs_handle_struct
*handle
;
475 unsigned int num_streams
;
476 struct stream_struct
*streams
;
480 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
482 struct streaminfo_state
*state
=
483 (struct streaminfo_state
*)private_data
;
485 if (!add_one_stream(state
->mem_ctx
,
486 &state
->num_streams
, &state
->streams
,
487 ea
->name
, ea
->value
.length
-1,
488 smb_roundup(state
->handle
->conn
,
489 ea
->value
.length
-1))) {
490 state
->status
= NT_STATUS_NO_MEMORY
;
497 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
498 struct files_struct
*fsp
,
501 unsigned int *pnum_streams
,
502 struct stream_struct
**pstreams
)
504 SMB_STRUCT_STAT sbuf
;
507 struct streaminfo_state state
;
509 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
510 if (is_ntfs_stream_name(fsp
->fsp_name
)) {
511 return NT_STATUS_INVALID_PARAMETER
;
513 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
516 if (is_ntfs_stream_name(fname
)) {
517 return NT_STATUS_INVALID_PARAMETER
;
519 ret
= SMB_VFS_STAT(handle
->conn
, fname
, &sbuf
);
523 return map_nt_error_from_unix(errno
);
526 state
.streams
= NULL
;
527 state
.num_streams
= 0;
529 if (!S_ISDIR(sbuf
.st_mode
)) {
530 if (!add_one_stream(mem_ctx
,
531 &state
.num_streams
, &state
.streams
,
532 "::$DATA", sbuf
.st_size
,
533 get_allocation_size(handle
->conn
, fsp
,
535 return NT_STATUS_NO_MEMORY
;
539 state
.mem_ctx
= mem_ctx
;
540 state
.handle
= handle
;
541 state
.status
= NT_STATUS_OK
;
543 status
= walk_xattr_streams(handle
->conn
, fsp
, fname
,
544 collect_one_stream
, &state
);
546 if (!NT_STATUS_IS_OK(status
)) {
547 TALLOC_FREE(state
.streams
);
551 if (!NT_STATUS_IS_OK(state
.status
)) {
552 TALLOC_FREE(state
.streams
);
556 *pnum_streams
= state
.num_streams
;
557 *pstreams
= state
.streams
;
561 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
)
563 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
) | FILE_NAMED_STREAMS
;
566 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
567 files_struct
*fsp
, const void *data
,
568 size_t n
, SMB_OFF_T offset
)
570 struct stream_io
*sio
=
571 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
576 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
579 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
582 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
583 sio
->base
, sio
->xattr_name
, &ea
);
584 if (!NT_STATUS_IS_OK(status
)) {
588 if ((offset
+ n
) > ea
.value
.length
-1) {
591 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
595 TALLOC_FREE(ea
.value
.data
);
600 ea
.value
.length
= offset
+ n
+ 1;
601 ea
.value
.data
[offset
+n
] = 0;
604 memcpy(ea
.value
.data
+ offset
, data
, n
);
606 ret
= SMB_VFS_SETXATTR(fsp
->conn
, fsp
->base_fsp
->fsp_name
,
608 ea
.value
.data
, ea
.value
.length
, 0);
610 TALLOC_FREE(ea
.value
.data
);
619 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
620 files_struct
*fsp
, void *data
,
621 size_t n
, SMB_OFF_T offset
)
623 struct stream_io
*sio
=
624 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
627 size_t length
, overlap
;
630 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
633 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
634 sio
->base
, sio
->xattr_name
, &ea
);
635 if (!NT_STATUS_IS_OK(status
)) {
639 length
= ea
.value
.length
-1;
641 /* Attempt to read past EOF. */
642 if (length
<= offset
) {
647 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
648 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
650 TALLOC_FREE(ea
.value
.data
);
654 /* VFS operations structure */
656 static vfs_op_tuple streams_xattr_ops
[] = {
657 {SMB_VFS_OP(streams_xattr_fs_capabilities
), SMB_VFS_OP_FS_CAPABILITIES
,
658 SMB_VFS_LAYER_TRANSPARENT
},
659 {SMB_VFS_OP(streams_xattr_open
), SMB_VFS_OP_OPEN
,
660 SMB_VFS_LAYER_TRANSPARENT
},
661 {SMB_VFS_OP(streams_xattr_stat
), SMB_VFS_OP_STAT
,
662 SMB_VFS_LAYER_TRANSPARENT
},
663 {SMB_VFS_OP(streams_xattr_fstat
), SMB_VFS_OP_FSTAT
,
664 SMB_VFS_LAYER_TRANSPARENT
},
665 {SMB_VFS_OP(streams_xattr_lstat
), SMB_VFS_OP_LSTAT
,
666 SMB_VFS_LAYER_TRANSPARENT
},
667 {SMB_VFS_OP(streams_xattr_pread
), SMB_VFS_OP_PREAD
,
668 SMB_VFS_LAYER_TRANSPARENT
},
669 {SMB_VFS_OP(streams_xattr_pwrite
), SMB_VFS_OP_PWRITE
,
670 SMB_VFS_LAYER_TRANSPARENT
},
671 {SMB_VFS_OP(streams_xattr_lstat
), SMB_VFS_OP_LSTAT
,
672 SMB_VFS_LAYER_TRANSPARENT
},
673 {SMB_VFS_OP(streams_xattr_unlink
), SMB_VFS_OP_UNLINK
,
674 SMB_VFS_LAYER_TRANSPARENT
},
675 {SMB_VFS_OP(streams_xattr_streaminfo
), SMB_VFS_OP_STREAMINFO
,
676 SMB_VFS_LAYER_OPAQUE
},
677 {SMB_VFS_OP(NULL
), SMB_VFS_OP_NOOP
, SMB_VFS_LAYER_NOOP
}
680 NTSTATUS
vfs_streams_xattr_init(void);
681 NTSTATUS
vfs_streams_xattr_init(void)
683 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",