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"
29 #include "librpc/gen_ndr/ioctl.h"
32 #define DBGC_CLASS DBGC_VFS
34 struct streams_xattr_config {
37 bool store_stream_type;
45 vfs_handle_struct *handle;
48 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
51 unsigned char hash[16];
55 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
56 (unsigned long)sbuf->st_ex_dev,
57 (unsigned long)sbuf->st_ex_ino, sname));
59 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
60 SMB_ASSERT(upper_sname != NULL);
63 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
64 sizeof(sbuf->st_ex_dev));
65 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
66 sizeof(sbuf->st_ex_ino));
67 MD5Update(&ctx, (unsigned char *)upper_sname,
68 talloc_get_size(upper_sname)-1);
71 TALLOC_FREE(upper_sname);
73 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
74 memcpy(&result, hash, sizeof(result));
76 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
81 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, NULL, 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 if (io == NULL || fsp->base_fsp == NULL) {
236 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
239 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
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,
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_IFDIR;
281 sbuf->st_ex_mode |= S_IFREG;
282 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
284 TALLOC_FREE(smb_fname_base);
288 static int streams_xattr_stat(vfs_handle_struct *handle,
289 struct smb_filename *smb_fname)
293 char *xattr_name = NULL;
295 if (!is_ntfs_stream_smb_fname(smb_fname)) {
296 return SMB_VFS_NEXT_STAT(handle, smb_fname);
299 /* Note if lp_posix_paths() is true, we can never
300 * get here as is_ntfs_stream_smb_fname() is
301 * always false. So we never need worry about
302 * not following links here. */
304 /* If the default stream is requested, just stat the base file. */
305 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
306 return streams_xattr_stat_base(handle, smb_fname, true);
309 /* Populate the stat struct with info from the base file. */
310 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
314 /* Derive the xattr name to lookup. */
315 status = streams_xattr_get_name(handle, talloc_tos(),
316 smb_fname->stream_name, &xattr_name);
317 if (!NT_STATUS_IS_OK(status)) {
318 errno = map_errno_from_nt_status(status);
322 /* Augment the base file's stat information before returning. */
323 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
326 if (smb_fname->st.st_ex_size == -1) {
327 SET_STAT_INVALID(smb_fname->st);
333 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
334 smb_fname->st.st_ex_mode &= ~S_IFMT;
335 smb_fname->st.st_ex_mode &= ~S_IFDIR;
336 smb_fname->st.st_ex_mode |= S_IFREG;
337 smb_fname->st.st_ex_blocks =
338 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
342 TALLOC_FREE(xattr_name);
346 static int streams_xattr_lstat(vfs_handle_struct *handle,
347 struct smb_filename *smb_fname)
351 char *xattr_name = NULL;
353 if (!is_ntfs_stream_smb_fname(smb_fname)) {
354 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
357 /* If the default stream is requested, just stat the base file. */
358 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
359 return streams_xattr_stat_base(handle, smb_fname, false);
362 /* Populate the stat struct with info from the base file. */
363 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
367 /* Derive the xattr name to lookup. */
368 status = streams_xattr_get_name(handle, talloc_tos(),
369 smb_fname->stream_name, &xattr_name);
370 if (!NT_STATUS_IS_OK(status)) {
371 errno = map_errno_from_nt_status(status);
375 /* Augment the base file's stat information before returning. */
376 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
379 if (smb_fname->st.st_ex_size == -1) {
380 SET_STAT_INVALID(smb_fname->st);
386 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
387 smb_fname->st.st_ex_mode &= ~S_IFMT;
388 smb_fname->st.st_ex_mode |= S_IFREG;
389 smb_fname->st.st_ex_blocks =
390 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
395 TALLOC_FREE(xattr_name);
399 static int streams_xattr_open(vfs_handle_struct *handle,
400 struct smb_filename *smb_fname,
401 files_struct *fsp, int flags, mode_t mode)
404 struct streams_xattr_config *config = NULL;
405 struct stream_io *sio = NULL;
407 char *xattr_name = NULL;
412 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
415 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
416 smb_fname_str_dbg(smb_fname), flags));
418 if (!is_ntfs_stream_smb_fname(smb_fname)) {
419 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
422 /* If the default stream is requested, just open the base file. */
423 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
424 char *tmp_stream_name;
426 tmp_stream_name = smb_fname->stream_name;
427 smb_fname->stream_name = NULL;
429 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
431 smb_fname->stream_name = tmp_stream_name;
436 status = streams_xattr_get_name(handle, talloc_tos(),
437 smb_fname->stream_name, &xattr_name);
438 if (!NT_STATUS_IS_OK(status)) {
439 errno = map_errno_from_nt_status(status);
444 * Return a valid fd, but ensure any attempt to use it returns an error
447 ret = pipe(pipe_fds);
454 fakefd = pipe_fds[0];
456 status = get_ea_value(talloc_tos(), handle->conn, NULL,
457 smb_fname, xattr_name, &ea);
459 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
461 if (!NT_STATUS_IS_OK(status)
462 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
464 * The base file is not there. This is an error even if we got
465 * O_CREAT, the higher levels should have created the base
468 DEBUG(10, ("streams_xattr_open: base file %s not around, "
469 "returning ENOENT\n", smb_fname->base_name));
474 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
477 * The attribute does not exist or needs to be truncated
481 * Darn, xattrs need at least 1 byte
485 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
486 xattr_name, smb_fname->base_name));
488 ret = SMB_VFS_SETXATTR(fsp->conn,
492 flags & O_EXCL ? XATTR_CREATE : 0);
498 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
504 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
507 * so->base needs to be a copy of fsp->fsp_name->base_name,
508 * making it identical to streams_xattr_recheck(). If the
509 * open is changing directories, fsp->fsp_name->base_name
510 * will be the full path from the share root, whilst
511 * smb_fname will be relative to the $cwd.
513 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
514 fsp->fsp_name->base_name);
515 sio->fsp_name_ptr = fsp->fsp_name;
516 sio->handle = handle;
519 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
535 static int streams_xattr_unlink(vfs_handle_struct *handle,
536 const struct smb_filename *smb_fname)
540 char *xattr_name = NULL;
542 if (!is_ntfs_stream_smb_fname(smb_fname)) {
543 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
546 /* If the default stream is requested, just open the base file. */
547 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
548 struct smb_filename *smb_fname_base = NULL;
550 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
551 if (smb_fname_base == NULL) {
556 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
558 TALLOC_FREE(smb_fname_base);
562 status = streams_xattr_get_name(handle, talloc_tos(),
563 smb_fname->stream_name, &xattr_name);
564 if (!NT_STATUS_IS_OK(status)) {
565 errno = map_errno_from_nt_status(status);
569 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
571 if ((ret == -1) && (errno == ENOATTR)) {
579 TALLOC_FREE(xattr_name);
583 static int streams_xattr_rename(vfs_handle_struct *handle,
584 const struct smb_filename *smb_fname_src,
585 const struct smb_filename *smb_fname_dst)
589 char *src_xattr_name = NULL;
590 char *dst_xattr_name = NULL;
591 bool src_is_stream, dst_is_stream;
596 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
597 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
599 if (!src_is_stream && !dst_is_stream) {
600 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
604 /* For now don't allow renames from or to the default stream. */
605 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
606 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
611 /* Don't rename if the streams are identical. */
612 if (strcasecmp_m(smb_fname_src->stream_name,
613 smb_fname_dst->stream_name) == 0) {
617 /* Get the xattr names. */
618 status = streams_xattr_get_name(handle, talloc_tos(),
619 smb_fname_src->stream_name,
621 if (!NT_STATUS_IS_OK(status)) {
622 errno = map_errno_from_nt_status(status);
625 status = streams_xattr_get_name(handle, talloc_tos(),
626 smb_fname_dst->stream_name,
628 if (!NT_STATUS_IS_OK(status)) {
629 errno = map_errno_from_nt_status(status);
633 /* read the old stream */
634 status = get_ea_value(talloc_tos(), handle->conn, NULL,
635 smb_fname_src, src_xattr_name, &ea);
636 if (!NT_STATUS_IS_OK(status)) {
641 /* (over)write the new stream */
642 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
643 dst_xattr_name, ea.value.data, ea.value.length,
646 if (errno == ENOATTR) {
652 /* remove the old stream */
653 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
656 if (errno == ENOATTR) {
666 TALLOC_FREE(src_xattr_name);
667 TALLOC_FREE(dst_xattr_name);
671 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
673 const struct smb_filename *smb_fname,
674 bool (*fn)(struct ea_struct *ea,
681 struct streams_xattr_config *config;
683 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
684 return NT_STATUS_UNSUCCESSFUL);
686 status = get_ea_names_from_file(talloc_tos(),
692 if (!NT_STATUS_IS_OK(status)) {
696 for (i=0; i<num_names; i++) {
700 * We want to check with samba_private_attr_name()
701 * whether the xattr name is a private one,
702 * unfortunately it flags xattrs that begin with the
703 * default streams prefix as private.
705 * By only calling samba_private_attr_name() in case
706 * the xattr does NOT begin with the default prefix,
707 * we know that if it returns 'true' it definitely one
708 * of our internal xattr like "user.DOSATTRIB".
710 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
711 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
712 if (samba_private_attr_name(names[i])) {
717 if (strncmp(names[i], config->prefix,
718 config->prefix_len) != 0) {
722 status = get_ea_value(names,
728 if (!NT_STATUS_IS_OK(status)) {
729 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
731 smb_fname->base_name,
736 ea.name = talloc_asprintf(
737 ea.value.data, ":%s%s",
738 names[i] + config->prefix_len,
739 config->store_stream_type ? "" : ":$DATA");
740 if (ea.name == NULL) {
741 DEBUG(0, ("talloc failed\n"));
745 if (!fn(&ea, private_data)) {
746 TALLOC_FREE(ea.value.data);
750 TALLOC_FREE(ea.value.data);
757 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
758 struct stream_struct **streams,
759 const char *name, off_t size,
762 struct stream_struct *tmp;
764 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
770 tmp[*num_streams].name = talloc_strdup(tmp, name);
771 if (tmp[*num_streams].name == NULL) {
775 tmp[*num_streams].size = size;
776 tmp[*num_streams].alloc_size = alloc_size;
783 struct streaminfo_state {
785 vfs_handle_struct *handle;
786 unsigned int num_streams;
787 struct stream_struct *streams;
791 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
793 struct streaminfo_state *state =
794 (struct streaminfo_state *)private_data;
796 if (!add_one_stream(state->mem_ctx,
797 &state->num_streams, &state->streams,
798 ea->name, ea->value.length-1,
799 smb_roundup(state->handle->conn,
800 ea->value.length-1))) {
801 state->status = NT_STATUS_NO_MEMORY;
808 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
809 struct files_struct *fsp,
810 const struct smb_filename *smb_fname,
812 unsigned int *pnum_streams,
813 struct stream_struct **pstreams)
815 SMB_STRUCT_STAT sbuf;
818 struct streaminfo_state state;
820 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
822 return map_nt_error_from_unix(errno);
825 state.streams = *pstreams;
826 state.num_streams = *pnum_streams;
827 state.mem_ctx = mem_ctx;
828 state.handle = handle;
829 state.status = NT_STATUS_OK;
831 if (S_ISLNK(sbuf.st_ex_mode)) {
833 * Currently we do't have SMB_VFS_LLISTXATTR
834 * inside the VFS which means there's no way
835 * to cope with a symlink when lp_posix_pathnames().
836 * returns true. For now ignore links.
837 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
839 status = NT_STATUS_OK;
841 status = walk_xattr_streams(handle, fsp, smb_fname,
842 collect_one_stream, &state);
845 if (!NT_STATUS_IS_OK(status)) {
846 TALLOC_FREE(state.streams);
850 if (!NT_STATUS_IS_OK(state.status)) {
851 TALLOC_FREE(state.streams);
855 *pnum_streams = state.num_streams;
856 *pstreams = state.streams;
858 return SMB_VFS_NEXT_STREAMINFO(handle,
866 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
867 enum timestamp_set_resolution *p_ts_res)
869 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
872 static int streams_xattr_connect(vfs_handle_struct *handle,
873 const char *service, const char *user)
875 struct streams_xattr_config *config;
876 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
880 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
885 config = talloc_zero(handle->conn, struct streams_xattr_config);
886 if (config == NULL) {
887 DEBUG(1, ("talloc_zero() failed\n"));
892 prefix = lp_parm_const_string(SNUM(handle->conn),
893 "streams_xattr", "prefix",
895 config->prefix = talloc_strdup(config, prefix);
896 if (config->prefix == NULL) {
897 DEBUG(1, ("talloc_strdup() failed\n"));
901 config->prefix_len = strlen(config->prefix);
902 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
904 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
909 SMB_VFS_HANDLE_SET_DATA(handle, config,
910 NULL, struct stream_xattr_config,
916 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
917 files_struct *fsp, const void *data,
918 size_t n, off_t offset)
920 struct stream_io *sio =
921 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
924 struct smb_filename *smb_fname_base = NULL;
927 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
930 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
933 if (!streams_xattr_recheck(sio)) {
937 /* Create an smb_filename with stream_name == NULL. */
938 smb_fname_base = synthetic_smb_fname(talloc_tos(),
942 fsp->fsp_name->flags);
943 if (smb_fname_base == NULL) {
948 status = get_ea_value(talloc_tos(), handle->conn, NULL,
949 smb_fname_base, sio->xattr_name, &ea);
950 if (!NT_STATUS_IS_OK(status)) {
954 if ((offset + n) > ea.value.length-1) {
957 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
961 TALLOC_FREE(ea.value.data);
966 ea.value.length = offset + n + 1;
967 ea.value.data[offset+n] = 0;
970 memcpy(ea.value.data + offset, data, n);
972 ret = SMB_VFS_SETXATTR(fsp->conn,
975 ea.value.data, ea.value.length, 0);
976 TALLOC_FREE(ea.value.data);
985 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
986 files_struct *fsp, void *data,
987 size_t n, off_t offset)
989 struct stream_io *sio =
990 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
993 size_t length, overlap;
994 struct smb_filename *smb_fname_base = NULL;
996 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
997 (int)offset, (int)n));
1000 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1003 if (!streams_xattr_recheck(sio)) {
1007 /* Create an smb_filename with stream_name == NULL. */
1008 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1012 fsp->fsp_name->flags);
1013 if (smb_fname_base == NULL) {
1018 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1019 smb_fname_base, sio->xattr_name, &ea);
1020 if (!NT_STATUS_IS_OK(status)) {
1024 length = ea.value.length-1;
1026 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1029 /* Attempt to read past EOF. */
1030 if (length <= offset) {
1034 overlap = (offset + n) > length ? (length - offset) : n;
1035 memcpy(data, ea.value.data + offset, overlap);
1037 TALLOC_FREE(ea.value.data);
1041 struct streams_xattr_pread_state {
1043 struct vfs_aio_state vfs_aio_state;
1046 static void streams_xattr_pread_done(struct tevent_req *subreq);
1048 static struct tevent_req *streams_xattr_pread_send(
1049 struct vfs_handle_struct *handle,
1050 TALLOC_CTX *mem_ctx,
1051 struct tevent_context *ev,
1052 struct files_struct *fsp,
1054 size_t n, off_t offset)
1056 struct tevent_req *req = NULL;
1057 struct tevent_req *subreq = NULL;
1058 struct streams_xattr_pread_state *state = NULL;
1059 struct stream_io *sio =
1060 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1062 req = tevent_req_create(mem_ctx, &state,
1063 struct streams_xattr_pread_state);
1069 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1071 if (tevent_req_nomem(req, subreq)) {
1072 return tevent_req_post(req, ev);
1074 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1078 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1079 if (state->nread != n) {
1080 if (state->nread != -1) {
1083 tevent_req_error(req, errno);
1084 return tevent_req_post(req, ev);
1087 tevent_req_done(req);
1088 return tevent_req_post(req, ev);
1091 static void streams_xattr_pread_done(struct tevent_req *subreq)
1093 struct tevent_req *req = tevent_req_callback_data(
1094 subreq, struct tevent_req);
1095 struct streams_xattr_pread_state *state = tevent_req_data(
1096 req, struct streams_xattr_pread_state);
1098 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1099 TALLOC_FREE(subreq);
1101 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1104 tevent_req_done(req);
1107 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1108 struct vfs_aio_state *vfs_aio_state)
1110 struct streams_xattr_pread_state *state = tevent_req_data(
1111 req, struct streams_xattr_pread_state);
1113 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1117 *vfs_aio_state = state->vfs_aio_state;
1118 return state->nread;
1121 struct streams_xattr_pwrite_state {
1123 struct vfs_aio_state vfs_aio_state;
1126 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1128 static struct tevent_req *streams_xattr_pwrite_send(
1129 struct vfs_handle_struct *handle,
1130 TALLOC_CTX *mem_ctx,
1131 struct tevent_context *ev,
1132 struct files_struct *fsp,
1134 size_t n, off_t offset)
1136 struct tevent_req *req = NULL;
1137 struct tevent_req *subreq = NULL;
1138 struct streams_xattr_pwrite_state *state = NULL;
1139 struct stream_io *sio =
1140 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1142 req = tevent_req_create(mem_ctx, &state,
1143 struct streams_xattr_pwrite_state);
1149 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1151 if (tevent_req_nomem(req, subreq)) {
1152 return tevent_req_post(req, ev);
1154 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1158 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1159 if (state->nwritten != n) {
1160 if (state->nwritten != -1) {
1163 tevent_req_error(req, errno);
1164 return tevent_req_post(req, ev);
1167 tevent_req_done(req);
1168 return tevent_req_post(req, ev);
1171 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1173 struct tevent_req *req = tevent_req_callback_data(
1174 subreq, struct tevent_req);
1175 struct streams_xattr_pwrite_state *state = tevent_req_data(
1176 req, struct streams_xattr_pwrite_state);
1178 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1179 TALLOC_FREE(subreq);
1181 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1184 tevent_req_done(req);
1187 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1188 struct vfs_aio_state *vfs_aio_state)
1190 struct streams_xattr_pwrite_state *state = tevent_req_data(
1191 req, struct streams_xattr_pwrite_state);
1193 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1197 *vfs_aio_state = state->vfs_aio_state;
1198 return state->nwritten;
1201 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1202 struct files_struct *fsp,
1207 struct ea_struct ea;
1209 struct stream_io *sio =
1210 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1211 struct smb_filename *smb_fname_base = NULL;
1213 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1214 fsp_str_dbg(fsp), (double)offset));
1217 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1220 if (!streams_xattr_recheck(sio)) {
1224 /* Create an smb_filename with stream_name == NULL. */
1225 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1229 fsp->fsp_name->flags);
1230 if (smb_fname_base == NULL) {
1235 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1236 smb_fname_base, sio->xattr_name, &ea);
1237 if (!NT_STATUS_IS_OK(status)) {
1241 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1245 TALLOC_FREE(ea.value.data);
1250 /* Did we expand ? */
1251 if (ea.value.length < offset + 1) {
1252 memset(&tmp[ea.value.length], '\0',
1253 offset + 1 - ea.value.length);
1256 ea.value.data = tmp;
1257 ea.value.length = offset + 1;
1258 ea.value.data[offset] = 0;
1260 ret = SMB_VFS_SETXATTR(fsp->conn,
1263 ea.value.data, ea.value.length, 0);
1264 TALLOC_FREE(ea.value.data);
1273 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1274 struct files_struct *fsp,
1279 struct stream_io *sio =
1280 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1282 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1284 fsp_str_dbg(fsp), (double)offset, (double)len));
1287 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1290 if (!streams_xattr_recheck(sio)) {
1294 /* Let the pwrite code path handle it. */
1299 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1300 uid_t uid, gid_t gid)
1302 struct stream_io *sio =
1303 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1306 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1312 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1316 struct stream_io *sio =
1317 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1320 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1326 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1327 struct files_struct *fsp,
1332 struct stream_io *sio =
1333 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1336 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1343 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1344 struct files_struct *fsp,
1348 struct stream_io *sio =
1349 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1352 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1359 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1360 struct files_struct *fsp,
1363 struct stream_io *sio =
1364 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1367 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1374 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1375 struct files_struct *fsp,
1381 struct stream_io *sio =
1382 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1385 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1393 static int streams_xattr_fchmod_acl(vfs_handle_struct *handle,
1397 struct stream_io *sio =
1398 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1401 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
1407 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1409 TALLOC_CTX *mem_ctx)
1411 struct stream_io *sio =
1412 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1415 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1418 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1419 handle, fsp->base_fsp->fsp_name,
1420 SMB_ACL_TYPE_ACCESS, mem_ctx);
1423 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1427 struct stream_io *sio =
1428 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1431 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1437 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1439 TALLOC_CTX *mem_ctx,
1440 char **blob_description,
1443 struct stream_io *sio =
1444 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1447 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1448 blob_description, blob);
1451 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1452 handle, fsp->base_fsp->fsp_name, mem_ctx,
1453 blob_description, blob);
1456 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1458 uint32_t security_info,
1459 TALLOC_CTX *mem_ctx,
1460 struct security_descriptor **ppdesc)
1462 struct stream_io *sio =
1463 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1466 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1470 return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1471 security_info, mem_ctx, ppdesc);
1474 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1476 uint32_t security_info_sent,
1477 const struct security_descriptor *psd)
1479 struct stream_io *sio =
1480 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1483 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1484 security_info_sent, psd);
1487 return NT_STATUS_OK;
1490 struct streams_xattr_fsync_state {
1492 struct vfs_aio_state vfs_aio_state;
1495 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1497 static struct tevent_req *streams_xattr_fsync_send(
1498 struct vfs_handle_struct *handle,
1499 TALLOC_CTX *mem_ctx,
1500 struct tevent_context *ev,
1501 struct files_struct *fsp)
1503 struct tevent_req *req = NULL;
1504 struct tevent_req *subreq = NULL;
1505 struct streams_xattr_fsync_state *state = NULL;
1506 struct stream_io *sio =
1507 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1509 req = tevent_req_create(mem_ctx, &state,
1510 struct streams_xattr_fsync_state);
1516 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1517 if (tevent_req_nomem(req, subreq)) {
1518 return tevent_req_post(req, ev);
1520 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1525 * There's no pathname based sync variant and we don't have access to
1526 * the basefile handle, so we can't do anything here.
1529 tevent_req_done(req);
1530 return tevent_req_post(req, ev);
1533 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1535 struct tevent_req *req = tevent_req_callback_data(
1536 subreq, struct tevent_req);
1537 struct streams_xattr_fsync_state *state = tevent_req_data(
1538 req, struct streams_xattr_fsync_state);
1540 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1541 TALLOC_FREE(subreq);
1542 if (state->ret != 0) {
1543 tevent_req_error(req, errno);
1547 tevent_req_done(req);
1550 static int streams_xattr_fsync_recv(struct tevent_req *req,
1551 struct vfs_aio_state *vfs_aio_state)
1553 struct streams_xattr_fsync_state *state = tevent_req_data(
1554 req, struct streams_xattr_fsync_state);
1556 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1560 *vfs_aio_state = state->vfs_aio_state;
1564 static bool streams_xattr_lock(vfs_handle_struct *handle,
1571 struct stream_io *sio =
1572 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1575 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1581 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1588 struct stream_io *sio =
1589 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1592 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1593 pcount, ptype, ppid);
1600 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1602 uint32_t share_mode,
1603 uint32_t access_mask)
1605 struct stream_io *sio =
1606 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1609 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1610 share_mode, access_mask);
1616 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1620 struct stream_io *sio =
1621 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1624 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1630 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1632 struct lock_struct *plock)
1634 struct stream_io *sio =
1635 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1638 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1644 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1645 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1646 .connect_fn = streams_xattr_connect,
1647 .open_fn = streams_xattr_open,
1648 .stat_fn = streams_xattr_stat,
1649 .fstat_fn = streams_xattr_fstat,
1650 .lstat_fn = streams_xattr_lstat,
1651 .pread_fn = streams_xattr_pread,
1652 .pwrite_fn = streams_xattr_pwrite,
1653 .pread_send_fn = streams_xattr_pread_send,
1654 .pread_recv_fn = streams_xattr_pread_recv,
1655 .pwrite_send_fn = streams_xattr_pwrite_send,
1656 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1657 .unlink_fn = streams_xattr_unlink,
1658 .rename_fn = streams_xattr_rename,
1659 .ftruncate_fn = streams_xattr_ftruncate,
1660 .fallocate_fn = streams_xattr_fallocate,
1661 .streaminfo_fn = streams_xattr_streaminfo,
1663 .fsync_send_fn = streams_xattr_fsync_send,
1664 .fsync_recv_fn = streams_xattr_fsync_recv,
1666 .lock_fn = streams_xattr_lock,
1667 .getlock_fn = streams_xattr_getlock,
1668 .kernel_flock_fn = streams_xattr_kernel_flock,
1669 .linux_setlease_fn = streams_xattr_linux_setlease,
1670 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1672 .fchown_fn = streams_xattr_fchown,
1673 .fchmod_fn = streams_xattr_fchmod,
1675 .fgetxattr_fn = streams_xattr_fgetxattr,
1676 .flistxattr_fn = streams_xattr_flistxattr,
1677 .fremovexattr_fn = streams_xattr_fremovexattr,
1678 .fsetxattr_fn = streams_xattr_fsetxattr,
1680 .fchmod_acl_fn = streams_xattr_fchmod_acl,
1682 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1683 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1684 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1686 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1687 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1691 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1693 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1694 &vfs_streams_xattr_fns);