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/util/tevent_unix.h"
28 #include "librpc/gen_ndr/ioctl.h"
29 #include "hash_inode.h"
32 #define DBGC_CLASS DBGC_VFS
34 struct streams_xattr_config {
37 bool store_stream_type;
45 vfs_handle_struct *handle;
48 static ssize_t get_xattr_size_fsp(struct files_struct *fsp,
49 const char *xattr_name)
55 status = get_ea_value_fsp(talloc_tos(),
59 if (!NT_STATUS_IS_OK(status)) {
63 result = ea.value.length-1;
64 TALLOC_FREE(ea.value.data);
69 * Given a stream name, populate xattr_name with the xattr name to use for
70 * accessing the stream.
72 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
74 const char *stream_name,
79 struct streams_xattr_config *config;
81 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
82 return NT_STATUS_UNSUCCESSFUL);
84 sname = talloc_strdup(ctx, stream_name + 1);
86 return NT_STATUS_NO_MEMORY;
90 * With vfs_fruit option "fruit:encoding = native" we're
91 * already converting stream names that contain illegal NTFS
92 * characters from their on-the-wire Unicode Private Range
93 * encoding to their native ASCII representation.
95 * As as result the name of xattrs storing the streams (via
96 * vfs_streams_xattr) may contain a colon, so we have to use
97 * strrchr_m() instead of strchr_m() for matching the stream
100 * In check_path_syntax() we've already ensured the streamname
101 * we got from the client is valid.
103 stype = strrchr_m(sname, ':');
107 * We only support one stream type: "$DATA"
109 if (strcasecmp_m(stype, ":$DATA") != 0) {
111 return NT_STATUS_INVALID_PARAMETER;
114 /* Split name and type */
118 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
121 config->store_stream_type ? ":$DATA" : "");
122 if (*xattr_name == NULL) {
124 return NT_STATUS_NO_MEMORY;
127 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
134 static bool streams_xattr_recheck(struct stream_io *sio)
137 char *xattr_name = NULL;
139 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
143 if (sio->fsp->fsp_name->stream_name == NULL) {
144 /* how can this happen */
149 status = streams_xattr_get_name(sio->handle, talloc_tos(),
150 sio->fsp->fsp_name->stream_name,
152 if (!NT_STATUS_IS_OK(status)) {
156 TALLOC_FREE(sio->xattr_name);
157 TALLOC_FREE(sio->base);
158 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
160 if (sio->xattr_name == NULL) {
161 DBG_DEBUG("sio->xattr_name==NULL\n");
164 TALLOC_FREE(xattr_name);
166 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
167 sio->fsp->fsp_name->base_name);
168 if (sio->base == NULL) {
169 DBG_DEBUG("sio->base==NULL\n");
173 sio->fsp_name_ptr = sio->fsp->fsp_name;
178 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
179 SMB_STRUCT_STAT *sbuf)
182 struct stream_io *io = (struct stream_io *)
183 VFS_FETCH_FSP_EXTENSION(handle, fsp);
185 if (io == NULL || fsp->base_fsp == NULL) {
186 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
189 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
191 if (!streams_xattr_recheck(io)) {
195 ret = SMB_VFS_NEXT_FSTAT(handle, fsp->base_fsp, sbuf);
200 sbuf->st_ex_size = get_xattr_size_fsp(fsp->base_fsp,
202 if (sbuf->st_ex_size == -1) {
203 SET_STAT_INVALID(*sbuf);
207 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
209 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
210 sbuf->st_ex_mode &= ~S_IFMT;
211 sbuf->st_ex_mode &= ~S_IFDIR;
212 sbuf->st_ex_mode |= S_IFREG;
213 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
218 static int streams_xattr_stat(vfs_handle_struct *handle,
219 struct smb_filename *smb_fname)
223 char *xattr_name = NULL;
224 char *tmp_stream_name = NULL;
225 struct smb_filename *pathref = NULL;
226 struct files_struct *fsp = smb_fname->fsp;
228 if (!is_named_stream(smb_fname)) {
229 return SMB_VFS_NEXT_STAT(handle, smb_fname);
232 /* Note if lp_posix_paths() is true, we can never
233 * get here as is_named_stream() is
234 * always false. So we never need worry about
235 * not following links here. */
237 /* Populate the stat struct with info from the base file. */
238 tmp_stream_name = smb_fname->stream_name;
239 smb_fname->stream_name = NULL;
240 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
241 smb_fname->stream_name = tmp_stream_name;
247 /* Derive the xattr name to lookup. */
248 status = streams_xattr_get_name(handle, talloc_tos(),
249 smb_fname->stream_name, &xattr_name);
250 if (!NT_STATUS_IS_OK(status)) {
251 errno = map_errno_from_nt_status(status);
255 /* Augment the base file's stat information before returning. */
257 status = synthetic_pathref(talloc_tos(),
258 handle->conn->cwd_fsp,
259 smb_fname->base_name,
265 if (!NT_STATUS_IS_OK(status)) {
266 TALLOC_FREE(xattr_name);
267 SET_STAT_INVALID(smb_fname->st);
276 smb_fname->st.st_ex_size = get_xattr_size_fsp(fsp,
278 if (smb_fname->st.st_ex_size == -1) {
279 TALLOC_FREE(xattr_name);
280 TALLOC_FREE(pathref);
281 SET_STAT_INVALID(smb_fname->st);
286 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
287 smb_fname->st.st_ex_mode &= ~S_IFMT;
288 smb_fname->st.st_ex_mode |= S_IFREG;
289 smb_fname->st.st_ex_blocks =
290 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
292 TALLOC_FREE(xattr_name);
293 TALLOC_FREE(pathref);
297 static int streams_xattr_lstat(vfs_handle_struct *handle,
298 struct smb_filename *smb_fname)
300 if (is_named_stream(smb_fname)) {
302 * There can never be EA's on a symlink.
303 * Windows will never see a symlink, and
304 * in SMB_FILENAME_POSIX_PATH mode we don't
305 * allow EA's on a symlink.
307 SET_STAT_INVALID(smb_fname->st);
311 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
314 static int streams_xattr_openat(struct vfs_handle_struct *handle,
315 const struct files_struct *dirfsp,
316 const struct smb_filename *smb_fname,
322 struct streams_xattr_config *config = NULL;
323 struct stream_io *sio = NULL;
325 char *xattr_name = NULL;
327 bool set_empty_xattr = false;
330 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
333 DBG_DEBUG("called for %s with flags 0x%x\n",
334 smb_fname_str_dbg(smb_fname),
337 if (!is_named_stream(smb_fname)) {
338 return SMB_VFS_NEXT_OPENAT(handle,
347 * For now assert this, so the below SMB_VFS_SETXATTR() works.
349 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
351 status = streams_xattr_get_name(handle, talloc_tos(),
352 smb_fname->stream_name, &xattr_name);
353 if (!NT_STATUS_IS_OK(status)) {
354 errno = map_errno_from_nt_status(status);
358 status = get_ea_value_fsp(talloc_tos(),
363 DBG_DEBUG("get_ea_value_fsp returned %s\n", nt_errstr(status));
365 if (!NT_STATUS_IS_OK(status)) {
366 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
368 * The base file is not there. This is an error even if
369 * we got O_CREAT, the higher levels should have created
370 * the base file for us.
372 DBG_DEBUG("streams_xattr_open: base file %s not around, "
373 "returning ENOENT\n", smb_fname->base_name);
378 if (!(flags & O_CREAT)) {
383 set_empty_xattr = true;
386 if (flags & O_TRUNC) {
387 set_empty_xattr = true;
390 if (set_empty_xattr) {
392 * The attribute does not exist or needs to be truncated
396 * Darn, xattrs need at least 1 byte
400 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
401 xattr_name, smb_fname->base_name));
403 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
406 flags & O_EXCL ? XATTR_CREATE : 0);
412 fakefd = vfs_fake_fd();
414 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
420 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
422 if (sio->xattr_name == NULL) {
428 * so->base needs to be a copy of fsp->fsp_name->base_name,
429 * making it identical to streams_xattr_recheck(). If the
430 * open is changing directories, fsp->fsp_name->base_name
431 * will be the full path from the share root, whilst
432 * smb_fname will be relative to the $cwd.
434 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
435 fsp->fsp_name->base_name);
436 if (sio->base == NULL) {
441 sio->fsp_name_ptr = fsp->fsp_name;
442 sio->handle = handle;
449 vfs_fake_fd_close(fakefd);
456 static int streams_xattr_close(vfs_handle_struct *handle,
462 fd = fsp_get_pathref_fd(fsp);
464 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
465 smb_fname_str_dbg(fsp->fsp_name), fd);
467 if (!is_named_stream(fsp->fsp_name)) {
468 return SMB_VFS_NEXT_CLOSE(handle, fsp);
471 ret = vfs_fake_fd_close(fd);
477 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
478 struct files_struct *dirfsp,
479 const struct smb_filename *smb_fname,
484 char *xattr_name = NULL;
486 if (!is_named_stream(smb_fname)) {
487 return SMB_VFS_NEXT_UNLINKAT(handle,
493 status = streams_xattr_get_name(handle, talloc_tos(),
494 smb_fname->stream_name, &xattr_name);
495 if (!NT_STATUS_IS_OK(status)) {
496 errno = map_errno_from_nt_status(status);
500 SMB_ASSERT(smb_fname->fsp != NULL);
501 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
503 ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
505 if ((ret == -1) && (errno == ENOATTR)) {
513 TALLOC_FREE(xattr_name);
517 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
518 struct files_struct *dirfsp,
519 const struct smb_filename *smb_fname,
523 if (flags & AT_REMOVEDIR) {
524 ret = SMB_VFS_NEXT_UNLINKAT(handle,
529 ret = streams_xattr_unlink_internal(handle,
537 static int streams_xattr_renameat(vfs_handle_struct *handle,
538 files_struct *srcfsp,
539 const struct smb_filename *smb_fname_src,
540 files_struct *dstfsp,
541 const struct smb_filename *smb_fname_dst)
545 char *src_xattr_name = NULL;
546 char *dst_xattr_name = NULL;
547 bool src_is_stream, dst_is_stream;
551 struct smb_filename *pathref_src = NULL;
552 struct smb_filename *pathref_dst = NULL;
553 struct smb_filename *full_src = NULL;
554 struct smb_filename *full_dst = NULL;
556 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
557 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
559 if (!src_is_stream && !dst_is_stream) {
560 return SMB_VFS_NEXT_RENAMEAT(handle,
567 /* For now don't allow renames from or to the default stream. */
568 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
569 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
574 /* Don't rename if the streams are identical. */
575 if (strcasecmp_m(smb_fname_src->stream_name,
576 smb_fname_dst->stream_name) == 0) {
580 /* Get the xattr names. */
581 status = streams_xattr_get_name(handle, talloc_tos(),
582 smb_fname_src->stream_name,
584 if (!NT_STATUS_IS_OK(status)) {
585 errno = map_errno_from_nt_status(status);
588 status = streams_xattr_get_name(handle, talloc_tos(),
589 smb_fname_dst->stream_name,
591 if (!NT_STATUS_IS_OK(status)) {
592 errno = map_errno_from_nt_status(status);
596 full_src = full_path_from_dirfsp_atname(talloc_tos(),
599 if (full_src == NULL) {
603 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
606 if (full_dst == NULL) {
611 /* Get a pathref for full_src (base file, no stream name). */
612 status = synthetic_pathref(talloc_tos(),
613 handle->conn->cwd_fsp,
620 if (!NT_STATUS_IS_OK(status)) {
625 /* Read the old stream from the base file fsp. */
626 status = get_ea_value_fsp(talloc_tos(),
630 if (!NT_STATUS_IS_OK(status)) {
631 errno = map_errno_from_nt_status(status);
635 /* Get a pathref for full_dst (base file, no stream name). */
636 status = synthetic_pathref(talloc_tos(),
637 handle->conn->cwd_fsp,
644 if (!NT_STATUS_IS_OK(status)) {
649 /* (Over)write the new stream on the base file fsp. */
650 nret = SMB_VFS_FSETXATTR(
657 if (errno == ENOATTR) {
664 * Remove the old stream from the base file fsp.
666 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
669 if (errno == ENOATTR) {
679 TALLOC_FREE(pathref_src);
680 TALLOC_FREE(pathref_dst);
681 TALLOC_FREE(full_src);
682 TALLOC_FREE(full_dst);
683 TALLOC_FREE(src_xattr_name);
684 TALLOC_FREE(dst_xattr_name);
688 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
690 const struct smb_filename *smb_fname,
691 bool (*fn)(struct ea_struct *ea,
698 struct streams_xattr_config *config;
700 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
701 return NT_STATUS_UNSUCCESSFUL);
703 status = get_ea_names_from_fsp(talloc_tos(),
707 if (!NT_STATUS_IS_OK(status)) {
711 for (i=0; i<num_names; i++) {
715 * We want to check with samba_private_attr_name()
716 * whether the xattr name is a private one,
717 * unfortunately it flags xattrs that begin with the
718 * default streams prefix as private.
720 * By only calling samba_private_attr_name() in case
721 * the xattr does NOT begin with the default prefix,
722 * we know that if it returns 'true' it definitely one
723 * of our internal xattr like "user.DOSATTRIB".
725 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
726 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
727 if (samba_private_attr_name(names[i])) {
732 if (strncmp(names[i], config->prefix,
733 config->prefix_len) != 0) {
737 status = get_ea_value_fsp(names,
741 if (!NT_STATUS_IS_OK(status)) {
742 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
744 smb_fname->base_name,
749 ea.name = talloc_asprintf(
750 ea.value.data, ":%s%s",
751 names[i] + config->prefix_len,
752 config->store_stream_type ? "" : ":$DATA");
753 if (ea.name == NULL) {
754 DEBUG(0, ("talloc failed\n"));
758 if (!fn(&ea, private_data)) {
759 TALLOC_FREE(ea.value.data);
763 TALLOC_FREE(ea.value.data);
770 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
771 struct stream_struct **streams,
772 const char *name, off_t size,
775 struct stream_struct *tmp;
777 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
783 tmp[*num_streams].name = talloc_strdup(tmp, name);
784 if (tmp[*num_streams].name == NULL) {
788 tmp[*num_streams].size = size;
789 tmp[*num_streams].alloc_size = alloc_size;
796 struct streaminfo_state {
798 vfs_handle_struct *handle;
799 unsigned int num_streams;
800 struct stream_struct *streams;
804 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
806 struct streaminfo_state *state =
807 (struct streaminfo_state *)private_data;
809 if (!add_one_stream(state->mem_ctx,
810 &state->num_streams, &state->streams,
811 ea->name, ea->value.length-1,
812 smb_roundup(state->handle->conn,
813 ea->value.length-1))) {
814 state->status = NT_STATUS_NO_MEMORY;
821 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
822 struct files_struct *fsp,
824 unsigned int *pnum_streams,
825 struct stream_struct **pstreams)
828 struct streaminfo_state state;
830 state.streams = *pstreams;
831 state.num_streams = *pnum_streams;
832 state.mem_ctx = mem_ctx;
833 state.handle = handle;
834 state.status = NT_STATUS_OK;
836 status = walk_xattr_streams(handle,
842 if (!NT_STATUS_IS_OK(status)) {
843 TALLOC_FREE(state.streams);
847 if (!NT_STATUS_IS_OK(state.status)) {
848 TALLOC_FREE(state.streams);
852 *pnum_streams = state.num_streams;
853 *pstreams = state.streams;
855 return SMB_VFS_NEXT_FSTREAMINFO(handle,
862 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
863 enum timestamp_set_resolution *p_ts_res)
865 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
868 static int streams_xattr_connect(vfs_handle_struct *handle,
869 const char *service, const char *user)
871 struct streams_xattr_config *config;
872 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
876 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
881 config = talloc_zero(handle->conn, struct streams_xattr_config);
882 if (config == NULL) {
883 DEBUG(1, ("talloc_zero() failed\n"));
888 prefix = lp_parm_const_string(SNUM(handle->conn),
889 "streams_xattr", "prefix",
891 config->prefix = talloc_strdup(config, prefix);
892 if (config->prefix == NULL) {
893 DEBUG(1, ("talloc_strdup() failed\n"));
897 config->prefix_len = strlen(config->prefix);
898 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
900 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
905 SMB_VFS_HANDLE_SET_DATA(handle, config,
906 NULL, struct stream_xattr_config,
912 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
913 files_struct *fsp, const void *data,
914 size_t n, off_t offset)
916 struct stream_io *sio =
917 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
922 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
925 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
928 if (!streams_xattr_recheck(sio)) {
932 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
934 * Requested write is beyond what can be read based on
935 * samba configuration.
936 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
937 * entire file to be skipped by File Explorer. VFAT returns
938 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
939 * to skip writing metadata, but copy data.
941 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
942 "supported extended attribute size. "
943 "Depending on filesystem type and operating system "
944 "(OS) specifics, this value may be increased using "
945 "the value of the parameter: "
946 "smbd max xattr size = <bytes>. Consult OS and "
947 "filesystem manpages prior to increasing this limit.\n",
948 sio->xattr_name, sio->base);
953 status = get_ea_value_fsp(talloc_tos(),
957 if (!NT_STATUS_IS_OK(status)) {
961 if ((offset + n) > ea.value.length-1) {
964 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
968 TALLOC_FREE(ea.value.data);
973 ea.value.length = offset + n + 1;
974 ea.value.data[offset+n] = 0;
977 memcpy(ea.value.data + offset, data, n);
979 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
984 TALLOC_FREE(ea.value.data);
993 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
994 files_struct *fsp, void *data,
995 size_t n, off_t offset)
997 struct stream_io *sio =
998 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1001 size_t length, overlap;
1003 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1004 (int)offset, (int)n));
1007 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1010 if (!streams_xattr_recheck(sio)) {
1014 status = get_ea_value_fsp(talloc_tos(),
1018 if (!NT_STATUS_IS_OK(status)) {
1022 length = ea.value.length-1;
1024 DBG_DEBUG("get_ea_value_fsp returned %d bytes\n",
1027 /* Attempt to read past EOF. */
1028 if (length <= offset) {
1032 overlap = (offset + n) > length ? (length - offset) : n;
1033 memcpy(data, ea.value.data + offset, overlap);
1035 TALLOC_FREE(ea.value.data);
1039 struct streams_xattr_pread_state {
1041 struct vfs_aio_state vfs_aio_state;
1044 static void streams_xattr_pread_done(struct tevent_req *subreq);
1046 static struct tevent_req *streams_xattr_pread_send(
1047 struct vfs_handle_struct *handle,
1048 TALLOC_CTX *mem_ctx,
1049 struct tevent_context *ev,
1050 struct files_struct *fsp,
1052 size_t n, off_t offset)
1054 struct tevent_req *req = NULL;
1055 struct tevent_req *subreq = NULL;
1056 struct streams_xattr_pread_state *state = NULL;
1057 struct stream_io *sio =
1058 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1060 req = tevent_req_create(mem_ctx, &state,
1061 struct streams_xattr_pread_state);
1067 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1069 if (tevent_req_nomem(req, subreq)) {
1070 return tevent_req_post(req, ev);
1072 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1076 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1077 if (state->nread != n) {
1078 if (state->nread != -1) {
1081 tevent_req_error(req, errno);
1082 return tevent_req_post(req, ev);
1085 tevent_req_done(req);
1086 return tevent_req_post(req, ev);
1089 static void streams_xattr_pread_done(struct tevent_req *subreq)
1091 struct tevent_req *req = tevent_req_callback_data(
1092 subreq, struct tevent_req);
1093 struct streams_xattr_pread_state *state = tevent_req_data(
1094 req, struct streams_xattr_pread_state);
1096 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1097 TALLOC_FREE(subreq);
1099 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1102 tevent_req_done(req);
1105 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1106 struct vfs_aio_state *vfs_aio_state)
1108 struct streams_xattr_pread_state *state = tevent_req_data(
1109 req, struct streams_xattr_pread_state);
1111 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1115 *vfs_aio_state = state->vfs_aio_state;
1116 return state->nread;
1119 struct streams_xattr_pwrite_state {
1121 struct vfs_aio_state vfs_aio_state;
1124 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1126 static struct tevent_req *streams_xattr_pwrite_send(
1127 struct vfs_handle_struct *handle,
1128 TALLOC_CTX *mem_ctx,
1129 struct tevent_context *ev,
1130 struct files_struct *fsp,
1132 size_t n, off_t offset)
1134 struct tevent_req *req = NULL;
1135 struct tevent_req *subreq = NULL;
1136 struct streams_xattr_pwrite_state *state = NULL;
1137 struct stream_io *sio =
1138 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1140 req = tevent_req_create(mem_ctx, &state,
1141 struct streams_xattr_pwrite_state);
1147 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1149 if (tevent_req_nomem(req, subreq)) {
1150 return tevent_req_post(req, ev);
1152 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1156 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1157 if (state->nwritten != n) {
1158 if (state->nwritten != -1) {
1161 tevent_req_error(req, errno);
1162 return tevent_req_post(req, ev);
1165 tevent_req_done(req);
1166 return tevent_req_post(req, ev);
1169 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1171 struct tevent_req *req = tevent_req_callback_data(
1172 subreq, struct tevent_req);
1173 struct streams_xattr_pwrite_state *state = tevent_req_data(
1174 req, struct streams_xattr_pwrite_state);
1176 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1177 TALLOC_FREE(subreq);
1179 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1182 tevent_req_done(req);
1185 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1186 struct vfs_aio_state *vfs_aio_state)
1188 struct streams_xattr_pwrite_state *state = tevent_req_data(
1189 req, struct streams_xattr_pwrite_state);
1191 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1195 *vfs_aio_state = state->vfs_aio_state;
1196 return state->nwritten;
1199 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1200 struct files_struct *fsp,
1205 struct ea_struct ea;
1207 struct stream_io *sio =
1208 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1210 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1211 fsp_str_dbg(fsp), (double)offset));
1214 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1217 if (!streams_xattr_recheck(sio)) {
1221 status = get_ea_value_fsp(talloc_tos(),
1225 if (!NT_STATUS_IS_OK(status)) {
1229 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1233 TALLOC_FREE(ea.value.data);
1238 /* Did we expand ? */
1239 if (ea.value.length < offset + 1) {
1240 memset(&tmp[ea.value.length], '\0',
1241 offset + 1 - ea.value.length);
1244 ea.value.data = tmp;
1245 ea.value.length = offset + 1;
1246 ea.value.data[offset] = 0;
1248 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1254 TALLOC_FREE(ea.value.data);
1263 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1264 struct files_struct *fsp,
1269 struct stream_io *sio =
1270 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1272 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1274 fsp_str_dbg(fsp), (double)offset, (double)len));
1277 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1280 if (!streams_xattr_recheck(sio)) {
1284 /* Let the pwrite code path handle it. */
1289 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1290 uid_t uid, gid_t gid)
1292 struct stream_io *sio =
1293 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1296 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1302 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1306 struct stream_io *sio =
1307 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1310 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1316 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1317 struct files_struct *fsp,
1322 struct stream_io *sio =
1323 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1326 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1333 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1334 struct files_struct *fsp,
1338 struct stream_io *sio =
1339 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1342 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1349 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1350 struct files_struct *fsp,
1353 struct stream_io *sio =
1354 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1357 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1364 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1365 struct files_struct *fsp,
1371 struct stream_io *sio =
1372 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1375 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1383 struct streams_xattr_fsync_state {
1385 struct vfs_aio_state vfs_aio_state;
1388 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1390 static struct tevent_req *streams_xattr_fsync_send(
1391 struct vfs_handle_struct *handle,
1392 TALLOC_CTX *mem_ctx,
1393 struct tevent_context *ev,
1394 struct files_struct *fsp)
1396 struct tevent_req *req = NULL;
1397 struct tevent_req *subreq = NULL;
1398 struct streams_xattr_fsync_state *state = NULL;
1399 struct stream_io *sio =
1400 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1402 req = tevent_req_create(mem_ctx, &state,
1403 struct streams_xattr_fsync_state);
1409 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1410 if (tevent_req_nomem(req, subreq)) {
1411 return tevent_req_post(req, ev);
1413 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1418 * There's no pathname based sync variant and we don't have access to
1419 * the basefile handle, so we can't do anything here.
1422 tevent_req_done(req);
1423 return tevent_req_post(req, ev);
1426 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1428 struct tevent_req *req = tevent_req_callback_data(
1429 subreq, struct tevent_req);
1430 struct streams_xattr_fsync_state *state = tevent_req_data(
1431 req, struct streams_xattr_fsync_state);
1433 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1434 TALLOC_FREE(subreq);
1435 if (state->ret != 0) {
1436 tevent_req_error(req, errno);
1440 tevent_req_done(req);
1443 static int streams_xattr_fsync_recv(struct tevent_req *req,
1444 struct vfs_aio_state *vfs_aio_state)
1446 struct streams_xattr_fsync_state *state = tevent_req_data(
1447 req, struct streams_xattr_fsync_state);
1449 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1453 *vfs_aio_state = state->vfs_aio_state;
1457 static bool streams_xattr_lock(vfs_handle_struct *handle,
1464 struct stream_io *sio =
1465 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1468 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1474 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1481 struct stream_io *sio =
1482 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1485 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1486 pcount, ptype, ppid);
1493 static int streams_xattr_filesystem_sharemode(vfs_handle_struct *handle,
1495 uint32_t share_access,
1496 uint32_t access_mask)
1498 struct stream_io *sio =
1499 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1502 return SMB_VFS_NEXT_FILESYSTEM_SHAREMODE(handle,
1511 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1515 struct stream_io *sio =
1516 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1519 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1525 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1527 struct lock_struct *plock)
1529 struct stream_io *sio =
1530 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1533 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1539 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1540 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1541 .connect_fn = streams_xattr_connect,
1542 .openat_fn = streams_xattr_openat,
1543 .close_fn = streams_xattr_close,
1544 .stat_fn = streams_xattr_stat,
1545 .fstat_fn = streams_xattr_fstat,
1546 .lstat_fn = streams_xattr_lstat,
1547 .pread_fn = streams_xattr_pread,
1548 .pwrite_fn = streams_xattr_pwrite,
1549 .pread_send_fn = streams_xattr_pread_send,
1550 .pread_recv_fn = streams_xattr_pread_recv,
1551 .pwrite_send_fn = streams_xattr_pwrite_send,
1552 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1553 .unlinkat_fn = streams_xattr_unlinkat,
1554 .renameat_fn = streams_xattr_renameat,
1555 .ftruncate_fn = streams_xattr_ftruncate,
1556 .fallocate_fn = streams_xattr_fallocate,
1557 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1559 .fsync_send_fn = streams_xattr_fsync_send,
1560 .fsync_recv_fn = streams_xattr_fsync_recv,
1562 .lock_fn = streams_xattr_lock,
1563 .getlock_fn = streams_xattr_getlock,
1564 .filesystem_sharemode_fn = streams_xattr_filesystem_sharemode,
1565 .linux_setlease_fn = streams_xattr_linux_setlease,
1566 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1568 .fchown_fn = streams_xattr_fchown,
1569 .fchmod_fn = streams_xattr_fchmod,
1571 .fgetxattr_fn = streams_xattr_fgetxattr,
1572 .flistxattr_fn = streams_xattr_flistxattr,
1573 .fremovexattr_fn = streams_xattr_fremovexattr,
1574 .fsetxattr_fn = streams_xattr_fsetxattr,
1578 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1580 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1581 &vfs_streams_xattr_fns);