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/>.
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "../lib/crypto/md5.h"
28 #include "lib/util/tevent_unix.h"
31 #define DBGC_CLASS DBGC_VFS
33 struct streams_xattr_config {
36 bool store_stream_type;
44 vfs_handle_struct *handle;
47 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
50 unsigned char hash[16];
54 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
55 (unsigned long)sbuf->st_ex_dev,
56 (unsigned long)sbuf->st_ex_ino, sname));
58 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
59 SMB_ASSERT(upper_sname != NULL);
62 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
63 sizeof(sbuf->st_ex_dev));
64 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
65 sizeof(sbuf->st_ex_ino));
66 MD5Update(&ctx, (unsigned char *)upper_sname,
67 talloc_get_size(upper_sname)-1);
70 TALLOC_FREE(upper_sname);
72 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
73 memcpy(&result, hash, sizeof(result));
75 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
80 static ssize_t get_xattr_size(connection_struct *conn,
82 const struct smb_filename *smb_fname,
83 const char *xattr_name)
89 status = get_ea_value(talloc_tos(), conn, fsp, smb_fname,
92 if (!NT_STATUS_IS_OK(status)) {
96 result = ea.value.length-1;
97 TALLOC_FREE(ea.value.data);
102 * Given a stream name, populate xattr_name with the xattr name to use for
103 * accessing the stream.
105 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
107 const char *stream_name,
112 struct streams_xattr_config *config;
114 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
115 return NT_STATUS_UNSUCCESSFUL);
117 sname = talloc_strdup(ctx, stream_name + 1);
119 return NT_STATUS_NO_MEMORY;
123 * With vfs_fruit option "fruit:encoding = native" we're
124 * already converting stream names that contain illegal NTFS
125 * characters from their on-the-wire Unicode Private Range
126 * encoding to their native ASCII representation.
128 * As as result the name of xattrs storing the streams (via
129 * vfs_streams_xattr) may contain a colon, so we have to use
130 * strrchr_m() instead of strchr_m() for matching the stream
133 * In check_path_syntax() we've already ensured the streamname
134 * we got from the client is valid.
136 stype = strrchr_m(sname, ':');
140 * We only support one stream type: "$DATA"
142 if (strcasecmp_m(stype, ":$DATA") != 0) {
144 return NT_STATUS_INVALID_PARAMETER;
147 /* Split name and type */
151 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
154 config->store_stream_type ? ":$DATA" : "");
155 if (*xattr_name == NULL) {
157 return NT_STATUS_NO_MEMORY;
160 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
167 static bool streams_xattr_recheck(struct stream_io *sio)
170 char *xattr_name = NULL;
172 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
176 if (sio->fsp->fsp_name->stream_name == NULL) {
177 /* how can this happen */
182 status = streams_xattr_get_name(sio->handle, talloc_tos(),
183 sio->fsp->fsp_name->stream_name,
185 if (!NT_STATUS_IS_OK(status)) {
189 TALLOC_FREE(sio->xattr_name);
190 TALLOC_FREE(sio->base);
191 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
193 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
194 sio->fsp->fsp_name->base_name);
195 sio->fsp_name_ptr = sio->fsp->fsp_name;
197 TALLOC_FREE(xattr_name);
199 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
207 * Helper to stat/lstat the base file of an smb_fname.
209 static int streams_xattr_stat_base(vfs_handle_struct *handle,
210 struct smb_filename *smb_fname,
213 char *tmp_stream_name;
216 tmp_stream_name = smb_fname->stream_name;
217 smb_fname->stream_name = NULL;
219 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
221 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
223 smb_fname->stream_name = tmp_stream_name;
227 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
228 SMB_STRUCT_STAT *sbuf)
230 struct smb_filename *smb_fname_base = NULL;
232 struct stream_io *io = (struct stream_io *)
233 VFS_FETCH_FSP_EXTENSION(handle, fsp);
235 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
237 if (io == NULL || fsp->base_fsp == NULL) {
238 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
241 if (!streams_xattr_recheck(io)) {
245 /* Create an smb_filename with stream_name == NULL. */
246 smb_fname_base = synthetic_smb_fname(talloc_tos(),
250 fsp->fsp_name->flags);
251 if (smb_fname_base == NULL) {
256 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
257 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
259 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
261 *sbuf = smb_fname_base->st;
264 TALLOC_FREE(smb_fname_base);
268 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp,
269 smb_fname_base, io->xattr_name);
270 if (sbuf->st_ex_size == -1) {
271 TALLOC_FREE(smb_fname_base);
272 SET_STAT_INVALID(*sbuf);
276 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
278 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
279 sbuf->st_ex_mode &= ~S_IFMT;
280 sbuf->st_ex_mode |= S_IFREG;
281 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
283 TALLOC_FREE(smb_fname_base);
287 static int streams_xattr_stat(vfs_handle_struct *handle,
288 struct smb_filename *smb_fname)
292 char *xattr_name = NULL;
294 if (!is_ntfs_stream_smb_fname(smb_fname)) {
295 return SMB_VFS_NEXT_STAT(handle, smb_fname);
298 /* Note if lp_posix_paths() is true, we can never
299 * get here as is_ntfs_stream_smb_fname() is
300 * always false. So we never need worry about
301 * not following links here. */
303 /* If the default stream is requested, just stat the base file. */
304 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
305 return streams_xattr_stat_base(handle, smb_fname, true);
308 /* Populate the stat struct with info from the base file. */
309 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
313 /* Derive the xattr name to lookup. */
314 status = streams_xattr_get_name(handle, talloc_tos(),
315 smb_fname->stream_name, &xattr_name);
316 if (!NT_STATUS_IS_OK(status)) {
317 errno = map_errno_from_nt_status(status);
321 /* Augment the base file's stat information before returning. */
322 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
325 if (smb_fname->st.st_ex_size == -1) {
326 SET_STAT_INVALID(smb_fname->st);
332 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
333 smb_fname->st.st_ex_mode &= ~S_IFMT;
334 smb_fname->st.st_ex_mode |= S_IFREG;
335 smb_fname->st.st_ex_blocks =
336 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
340 TALLOC_FREE(xattr_name);
344 static int streams_xattr_lstat(vfs_handle_struct *handle,
345 struct smb_filename *smb_fname)
349 char *xattr_name = NULL;
351 if (!is_ntfs_stream_smb_fname(smb_fname)) {
352 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
355 /* If the default stream is requested, just stat the base file. */
356 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
357 return streams_xattr_stat_base(handle, smb_fname, false);
360 /* Populate the stat struct with info from the base file. */
361 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
365 /* Derive the xattr name to lookup. */
366 status = streams_xattr_get_name(handle, talloc_tos(),
367 smb_fname->stream_name, &xattr_name);
368 if (!NT_STATUS_IS_OK(status)) {
369 errno = map_errno_from_nt_status(status);
373 /* Augment the base file's stat information before returning. */
374 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
377 if (smb_fname->st.st_ex_size == -1) {
378 SET_STAT_INVALID(smb_fname->st);
384 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
385 smb_fname->st.st_ex_mode &= ~S_IFMT;
386 smb_fname->st.st_ex_mode |= S_IFREG;
387 smb_fname->st.st_ex_blocks =
388 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
393 TALLOC_FREE(xattr_name);
397 static int streams_xattr_open(vfs_handle_struct *handle,
398 struct smb_filename *smb_fname,
399 files_struct *fsp, int flags, mode_t mode)
402 struct smb_filename *smb_fname_base = NULL;
403 struct stream_io *sio;
405 char *xattr_name = NULL;
410 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
411 smb_fname_str_dbg(smb_fname), flags));
413 if (!is_ntfs_stream_smb_fname(smb_fname)) {
414 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
417 /* If the default stream is requested, just open the base file. */
418 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
419 char *tmp_stream_name;
421 tmp_stream_name = smb_fname->stream_name;
422 smb_fname->stream_name = NULL;
424 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
426 smb_fname->stream_name = tmp_stream_name;
431 status = streams_xattr_get_name(handle, talloc_tos(),
432 smb_fname->stream_name, &xattr_name);
433 if (!NT_STATUS_IS_OK(status)) {
434 errno = map_errno_from_nt_status(status);
438 /* Create an smb_filename with stream_name == NULL. */
439 smb_fname_base = synthetic_smb_fname(talloc_tos(),
440 smb_fname->base_name,
444 if (smb_fname_base == NULL) {
450 * We use baseflags to turn off nasty side-effects when opening the
454 baseflags &= ~O_TRUNC;
455 baseflags &= ~O_EXCL;
456 baseflags &= ~O_CREAT;
458 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
461 /* It is legit to open a stream on a directory, but the base
462 * fd has to be read-only.
464 if ((hostfd == -1) && (errno == EISDIR)) {
465 baseflags &= ~O_ACCMODE;
466 baseflags |= O_RDONLY;
467 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp, baseflags,
471 TALLOC_FREE(smb_fname_base);
477 status = get_ea_value(talloc_tos(), handle->conn, NULL,
478 smb_fname, xattr_name, &ea);
480 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
482 if (!NT_STATUS_IS_OK(status)
483 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
485 * The base file is not there. This is an error even if we got
486 * O_CREAT, the higher levels should have created the base
489 DEBUG(10, ("streams_xattr_open: base file %s not around, "
490 "returning ENOENT\n", smb_fname->base_name));
495 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
498 * The attribute does not exist or needs to be truncated
502 * Darn, xattrs need at least 1 byte
506 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
507 xattr_name, smb_fname->base_name));
509 ret = SMB_VFS_SETXATTR(fsp->conn,
513 flags & O_EXCL ? XATTR_CREATE : 0);
519 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
527 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
530 * so->base needs to be a copy of fsp->fsp_name->base_name,
531 * making it identical to streams_xattr_recheck(). If the
532 * open is changing directories, fsp->fsp_name->base_name
533 * will be the full path from the share root, whilst
534 * smb_fname will be relative to the $cwd.
536 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
537 fsp->fsp_name->base_name);
538 sio->fsp_name_ptr = fsp->fsp_name;
539 sio->handle = handle;
542 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
552 * BUGBUGBUG -- we would need to call fd_close_posix here, but
553 * we don't have a full fsp yet
555 fsp->fh->fd = hostfd;
556 SMB_VFS_NEXT_CLOSE(handle, fsp);
562 static int streams_xattr_unlink(vfs_handle_struct *handle,
563 const struct smb_filename *smb_fname)
567 char *xattr_name = NULL;
569 if (!is_ntfs_stream_smb_fname(smb_fname)) {
570 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
573 /* If the default stream is requested, just open the base file. */
574 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
575 struct smb_filename *smb_fname_base = NULL;
577 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
578 if (smb_fname_base == NULL) {
583 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
585 TALLOC_FREE(smb_fname_base);
589 status = streams_xattr_get_name(handle, talloc_tos(),
590 smb_fname->stream_name, &xattr_name);
591 if (!NT_STATUS_IS_OK(status)) {
592 errno = map_errno_from_nt_status(status);
596 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
598 if ((ret == -1) && (errno == ENOATTR)) {
606 TALLOC_FREE(xattr_name);
610 static int streams_xattr_rename(vfs_handle_struct *handle,
611 const struct smb_filename *smb_fname_src,
612 const struct smb_filename *smb_fname_dst)
616 char *src_xattr_name = NULL;
617 char *dst_xattr_name = NULL;
618 bool src_is_stream, dst_is_stream;
623 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
624 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
626 if (!src_is_stream && !dst_is_stream) {
627 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
631 /* For now don't allow renames from or to the default stream. */
632 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
633 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
638 /* Don't rename if the streams are identical. */
639 if (strcasecmp_m(smb_fname_src->stream_name,
640 smb_fname_dst->stream_name) == 0) {
644 /* Get the xattr names. */
645 status = streams_xattr_get_name(handle, talloc_tos(),
646 smb_fname_src->stream_name,
648 if (!NT_STATUS_IS_OK(status)) {
649 errno = map_errno_from_nt_status(status);
652 status = streams_xattr_get_name(handle, talloc_tos(),
653 smb_fname_dst->stream_name,
655 if (!NT_STATUS_IS_OK(status)) {
656 errno = map_errno_from_nt_status(status);
660 /* read the old stream */
661 status = get_ea_value(talloc_tos(), handle->conn, NULL,
662 smb_fname_src, src_xattr_name, &ea);
663 if (!NT_STATUS_IS_OK(status)) {
668 /* (over)write the new stream */
669 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
670 dst_xattr_name, ea.value.data, ea.value.length,
673 if (errno == ENOATTR) {
679 /* remove the old stream */
680 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
683 if (errno == ENOATTR) {
693 TALLOC_FREE(src_xattr_name);
694 TALLOC_FREE(dst_xattr_name);
698 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
700 const struct smb_filename *smb_fname,
701 bool (*fn)(struct ea_struct *ea,
708 struct streams_xattr_config *config;
710 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
711 return NT_STATUS_UNSUCCESSFUL);
713 status = get_ea_names_from_file(talloc_tos(),
719 if (!NT_STATUS_IS_OK(status)) {
723 for (i=0; i<num_names; i++) {
727 * We want to check with samba_private_attr_name()
728 * whether the xattr name is a private one,
729 * unfortunately it flags xattrs that begin with the
730 * default streams prefix as private.
732 * By only calling samba_private_attr_name() in case
733 * the xattr does NOT begin with the default prefix,
734 * we know that if it returns 'true' it definitely one
735 * of our internal xattr like "user.DOSATTRIB".
737 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
738 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
739 if (samba_private_attr_name(names[i])) {
744 if (strncmp(names[i], config->prefix,
745 config->prefix_len) != 0) {
749 status = get_ea_value(names,
755 if (!NT_STATUS_IS_OK(status)) {
756 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
758 smb_fname->base_name,
763 ea.name = talloc_asprintf(
764 ea.value.data, ":%s%s",
765 names[i] + config->prefix_len,
766 config->store_stream_type ? "" : ":$DATA");
767 if (ea.name == NULL) {
768 DEBUG(0, ("talloc failed\n"));
772 if (!fn(&ea, private_data)) {
773 TALLOC_FREE(ea.value.data);
777 TALLOC_FREE(ea.value.data);
784 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
785 struct stream_struct **streams,
786 const char *name, off_t size,
789 struct stream_struct *tmp;
791 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
797 tmp[*num_streams].name = talloc_strdup(tmp, name);
798 if (tmp[*num_streams].name == NULL) {
802 tmp[*num_streams].size = size;
803 tmp[*num_streams].alloc_size = alloc_size;
810 struct streaminfo_state {
812 vfs_handle_struct *handle;
813 unsigned int num_streams;
814 struct stream_struct *streams;
818 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
820 struct streaminfo_state *state =
821 (struct streaminfo_state *)private_data;
823 if (!add_one_stream(state->mem_ctx,
824 &state->num_streams, &state->streams,
825 ea->name, ea->value.length-1,
826 smb_roundup(state->handle->conn,
827 ea->value.length-1))) {
828 state->status = NT_STATUS_NO_MEMORY;
835 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
836 struct files_struct *fsp,
837 const struct smb_filename *smb_fname,
839 unsigned int *pnum_streams,
840 struct stream_struct **pstreams)
842 SMB_STRUCT_STAT sbuf;
845 struct streaminfo_state state;
847 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
849 return map_nt_error_from_unix(errno);
852 state.streams = *pstreams;
853 state.num_streams = *pnum_streams;
854 state.mem_ctx = mem_ctx;
855 state.handle = handle;
856 state.status = NT_STATUS_OK;
858 if (S_ISLNK(sbuf.st_ex_mode)) {
860 * Currently we do't have SMB_VFS_LLISTXATTR
861 * inside the VFS which means there's no way
862 * to cope with a symlink when lp_posix_pathnames().
863 * returns true. For now ignore links.
864 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
866 status = NT_STATUS_OK;
868 status = walk_xattr_streams(handle, fsp, smb_fname,
869 collect_one_stream, &state);
872 if (!NT_STATUS_IS_OK(status)) {
873 TALLOC_FREE(state.streams);
877 if (!NT_STATUS_IS_OK(state.status)) {
878 TALLOC_FREE(state.streams);
882 *pnum_streams = state.num_streams;
883 *pstreams = state.streams;
885 return SMB_VFS_NEXT_STREAMINFO(handle,
893 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
894 enum timestamp_set_resolution *p_ts_res)
896 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
899 static int streams_xattr_connect(vfs_handle_struct *handle,
900 const char *service, const char *user)
902 struct streams_xattr_config *config;
903 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
907 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
912 config = talloc_zero(handle->conn, struct streams_xattr_config);
913 if (config == NULL) {
914 DEBUG(1, ("talloc_zero() failed\n"));
919 prefix = lp_parm_const_string(SNUM(handle->conn),
920 "streams_xattr", "prefix",
922 config->prefix = talloc_strdup(config, prefix);
923 if (config->prefix == NULL) {
924 DEBUG(1, ("talloc_strdup() failed\n"));
928 config->prefix_len = strlen(config->prefix);
929 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
931 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
936 SMB_VFS_HANDLE_SET_DATA(handle, config,
937 NULL, struct stream_xattr_config,
943 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
944 files_struct *fsp, const void *data,
945 size_t n, off_t offset)
947 struct stream_io *sio =
948 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
951 struct smb_filename *smb_fname_base = NULL;
954 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
957 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
960 if (!streams_xattr_recheck(sio)) {
964 /* Create an smb_filename with stream_name == NULL. */
965 smb_fname_base = synthetic_smb_fname(talloc_tos(),
969 fsp->fsp_name->flags);
970 if (smb_fname_base == NULL) {
975 status = get_ea_value(talloc_tos(), handle->conn, fsp,
976 smb_fname_base, sio->xattr_name, &ea);
977 if (!NT_STATUS_IS_OK(status)) {
981 if ((offset + n) > ea.value.length-1) {
984 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
988 TALLOC_FREE(ea.value.data);
993 ea.value.length = offset + n + 1;
994 ea.value.data[offset+n] = 0;
997 memcpy(ea.value.data + offset, data, n);
999 ret = SMB_VFS_SETXATTR(fsp->conn,
1002 ea.value.data, ea.value.length, 0);
1003 TALLOC_FREE(ea.value.data);
1012 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1013 files_struct *fsp, void *data,
1014 size_t n, off_t offset)
1016 struct stream_io *sio =
1017 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1018 struct ea_struct ea;
1020 size_t length, overlap;
1021 struct smb_filename *smb_fname_base = NULL;
1023 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1024 (int)offset, (int)n));
1027 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1030 if (!streams_xattr_recheck(sio)) {
1034 /* Create an smb_filename with stream_name == NULL. */
1035 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1039 fsp->fsp_name->flags);
1040 if (smb_fname_base == NULL) {
1045 status = get_ea_value(talloc_tos(), handle->conn, fsp,
1046 smb_fname_base, sio->xattr_name, &ea);
1047 if (!NT_STATUS_IS_OK(status)) {
1051 length = ea.value.length-1;
1053 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1056 /* Attempt to read past EOF. */
1057 if (length <= offset) {
1061 overlap = (offset + n) > length ? (length - offset) : n;
1062 memcpy(data, ea.value.data + offset, overlap);
1064 TALLOC_FREE(ea.value.data);
1068 struct streams_xattr_pread_state {
1070 struct vfs_aio_state vfs_aio_state;
1073 static void streams_xattr_pread_done(struct tevent_req *subreq);
1075 static struct tevent_req *streams_xattr_pread_send(
1076 struct vfs_handle_struct *handle,
1077 TALLOC_CTX *mem_ctx,
1078 struct tevent_context *ev,
1079 struct files_struct *fsp,
1081 size_t n, off_t offset)
1083 struct tevent_req *req = NULL;
1084 struct tevent_req *subreq = NULL;
1085 struct streams_xattr_pread_state *state = NULL;
1086 struct stream_io *sio =
1087 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1089 req = tevent_req_create(mem_ctx, &state,
1090 struct streams_xattr_pread_state);
1096 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1098 if (tevent_req_nomem(req, subreq)) {
1099 return tevent_req_post(req, ev);
1101 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1105 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1106 if (state->nread != n) {
1107 if (state->nread != -1) {
1110 tevent_req_error(req, errno);
1111 return tevent_req_post(req, ev);
1114 tevent_req_done(req);
1115 return tevent_req_post(req, ev);
1118 static void streams_xattr_pread_done(struct tevent_req *subreq)
1120 struct tevent_req *req = tevent_req_callback_data(
1121 subreq, struct tevent_req);
1122 struct streams_xattr_pread_state *state = tevent_req_data(
1123 req, struct streams_xattr_pread_state);
1125 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1126 TALLOC_FREE(subreq);
1128 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1131 tevent_req_done(req);
1134 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1135 struct vfs_aio_state *vfs_aio_state)
1137 struct streams_xattr_pread_state *state = tevent_req_data(
1138 req, struct streams_xattr_pread_state);
1140 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1144 *vfs_aio_state = state->vfs_aio_state;
1145 return state->nread;
1148 struct streams_xattr_pwrite_state {
1150 struct vfs_aio_state vfs_aio_state;
1153 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1155 static struct tevent_req *streams_xattr_pwrite_send(
1156 struct vfs_handle_struct *handle,
1157 TALLOC_CTX *mem_ctx,
1158 struct tevent_context *ev,
1159 struct files_struct *fsp,
1161 size_t n, off_t offset)
1163 struct tevent_req *req = NULL;
1164 struct tevent_req *subreq = NULL;
1165 struct streams_xattr_pwrite_state *state = NULL;
1166 struct stream_io *sio =
1167 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1169 req = tevent_req_create(mem_ctx, &state,
1170 struct streams_xattr_pwrite_state);
1176 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1178 if (tevent_req_nomem(req, subreq)) {
1179 return tevent_req_post(req, ev);
1181 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1185 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1186 if (state->nwritten != n) {
1187 if (state->nwritten != -1) {
1190 tevent_req_error(req, errno);
1191 return tevent_req_post(req, ev);
1194 tevent_req_done(req);
1195 return tevent_req_post(req, ev);
1198 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1200 struct tevent_req *req = tevent_req_callback_data(
1201 subreq, struct tevent_req);
1202 struct streams_xattr_pwrite_state *state = tevent_req_data(
1203 req, struct streams_xattr_pwrite_state);
1205 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1206 TALLOC_FREE(subreq);
1208 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1211 tevent_req_done(req);
1214 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1215 struct vfs_aio_state *vfs_aio_state)
1217 struct streams_xattr_pwrite_state *state = tevent_req_data(
1218 req, struct streams_xattr_pwrite_state);
1220 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1224 *vfs_aio_state = state->vfs_aio_state;
1225 return state->nwritten;
1228 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1229 struct files_struct *fsp,
1234 struct ea_struct ea;
1236 struct stream_io *sio =
1237 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1238 struct smb_filename *smb_fname_base = NULL;
1240 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1241 fsp_str_dbg(fsp), (double)offset));
1244 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1247 if (!streams_xattr_recheck(sio)) {
1251 /* Create an smb_filename with stream_name == NULL. */
1252 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1256 fsp->fsp_name->flags);
1257 if (smb_fname_base == NULL) {
1262 status = get_ea_value(talloc_tos(), handle->conn, fsp,
1263 smb_fname_base, sio->xattr_name, &ea);
1264 if (!NT_STATUS_IS_OK(status)) {
1268 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1272 TALLOC_FREE(ea.value.data);
1277 /* Did we expand ? */
1278 if (ea.value.length < offset + 1) {
1279 memset(&tmp[ea.value.length], '\0',
1280 offset + 1 - ea.value.length);
1283 ea.value.data = tmp;
1284 ea.value.length = offset + 1;
1285 ea.value.data[offset] = 0;
1287 ret = SMB_VFS_SETXATTR(fsp->conn,
1290 ea.value.data, ea.value.length, 0);
1291 TALLOC_FREE(ea.value.data);
1300 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1301 struct files_struct *fsp,
1306 struct stream_io *sio =
1307 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1309 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1311 fsp_str_dbg(fsp), (double)offset, (double)len));
1314 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1317 if (!streams_xattr_recheck(sio)) {
1321 /* Let the pwrite code path handle it. */
1327 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1328 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1329 .connect_fn = streams_xattr_connect,
1330 .open_fn = streams_xattr_open,
1331 .stat_fn = streams_xattr_stat,
1332 .fstat_fn = streams_xattr_fstat,
1333 .lstat_fn = streams_xattr_lstat,
1334 .pread_fn = streams_xattr_pread,
1335 .pwrite_fn = streams_xattr_pwrite,
1336 .pread_send_fn = streams_xattr_pread_send,
1337 .pread_recv_fn = streams_xattr_pread_recv,
1338 .pwrite_send_fn = streams_xattr_pwrite_send,
1339 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1340 .unlink_fn = streams_xattr_unlink,
1341 .rename_fn = streams_xattr_rename,
1342 .ftruncate_fn = streams_xattr_ftruncate,
1343 .fallocate_fn = streams_xattr_fallocate,
1344 .streaminfo_fn = streams_xattr_streaminfo,
1347 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *);
1348 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1350 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1351 &vfs_streams_xattr_fns);