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_is_alternate_stream(fsp)) {
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,
318 const struct vfs_open_how *how)
321 struct streams_xattr_config *config = NULL;
322 struct stream_io *sio = NULL;
324 char *xattr_name = NULL;
326 bool set_empty_xattr = false;
329 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
332 DBG_DEBUG("called for %s with flags 0x%x\n",
333 smb_fname_str_dbg(smb_fname),
336 if (!is_named_stream(smb_fname)) {
337 return SMB_VFS_NEXT_OPENAT(handle,
344 if (how->resolve != 0) {
349 SMB_ASSERT(fsp_is_alternate_stream(fsp));
350 SMB_ASSERT(dirfsp == NULL);
352 status = streams_xattr_get_name(handle, talloc_tos(),
353 smb_fname->stream_name, &xattr_name);
354 if (!NT_STATUS_IS_OK(status)) {
355 errno = map_errno_from_nt_status(status);
359 status = get_ea_value_fsp(talloc_tos(),
364 DBG_DEBUG("get_ea_value_fsp returned %s\n", nt_errstr(status));
366 if (!NT_STATUS_IS_OK(status)) {
367 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
369 * The base file is not there. This is an error even if
370 * we got O_CREAT, the higher levels should have created
371 * the base file for us.
373 DBG_DEBUG("streams_xattr_open: base file %s not around, "
374 "returning ENOENT\n", smb_fname->base_name);
379 if (!(how->flags & O_CREAT)) {
384 set_empty_xattr = true;
387 if (how->flags & O_TRUNC) {
388 set_empty_xattr = true;
391 if (set_empty_xattr) {
393 * The attribute does not exist or needs to be truncated
397 * Darn, xattrs need at least 1 byte
401 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
402 xattr_name, smb_fname->base_name));
404 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
407 how->flags & O_EXCL ? XATTR_CREATE : 0);
413 fakefd = vfs_fake_fd();
415 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
421 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
423 if (sio->xattr_name == NULL) {
429 * so->base needs to be a copy of fsp->fsp_name->base_name,
430 * making it identical to streams_xattr_recheck(). If the
431 * open is changing directories, fsp->fsp_name->base_name
432 * will be the full path from the share root, whilst
433 * smb_fname will be relative to the $cwd.
435 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
436 fsp->fsp_name->base_name);
437 if (sio->base == NULL) {
442 sio->fsp_name_ptr = fsp->fsp_name;
443 sio->handle = handle;
450 vfs_fake_fd_close(fakefd);
457 static int streams_xattr_close(vfs_handle_struct *handle,
463 fd = fsp_get_pathref_fd(fsp);
465 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
466 smb_fname_str_dbg(fsp->fsp_name), fd);
468 if (!fsp_is_alternate_stream(fsp)) {
469 return SMB_VFS_NEXT_CLOSE(handle, fsp);
472 ret = vfs_fake_fd_close(fd);
478 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
479 struct files_struct *dirfsp,
480 const struct smb_filename *smb_fname,
485 char *xattr_name = NULL;
486 struct smb_filename *pathref = NULL;
487 struct files_struct *fsp = smb_fname->fsp;
489 if (!is_named_stream(smb_fname)) {
490 return SMB_VFS_NEXT_UNLINKAT(handle,
496 /* A stream can never be rmdir'ed */
497 SMB_ASSERT((flags & AT_REMOVEDIR) == 0);
499 status = streams_xattr_get_name(handle, talloc_tos(),
500 smb_fname->stream_name, &xattr_name);
501 if (!NT_STATUS_IS_OK(status)) {
502 errno = map_errno_from_nt_status(status);
507 status = synthetic_pathref(talloc_tos(),
508 handle->conn->cwd_fsp,
509 smb_fname->base_name,
515 if (!NT_STATUS_IS_OK(status)) {
521 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
525 ret = SMB_VFS_FREMOVEXATTR(fsp, xattr_name);
527 if ((ret == -1) && (errno == ENOATTR)) {
535 TALLOC_FREE(xattr_name);
536 TALLOC_FREE(pathref);
540 static int streams_xattr_renameat(vfs_handle_struct *handle,
541 files_struct *srcfsp,
542 const struct smb_filename *smb_fname_src,
543 files_struct *dstfsp,
544 const struct smb_filename *smb_fname_dst)
548 char *src_xattr_name = NULL;
549 char *dst_xattr_name = NULL;
550 bool src_is_stream, dst_is_stream;
554 struct smb_filename *pathref_src = NULL;
555 struct smb_filename *pathref_dst = NULL;
556 struct smb_filename *full_src = NULL;
557 struct smb_filename *full_dst = NULL;
559 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
560 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
562 if (!src_is_stream && !dst_is_stream) {
563 return SMB_VFS_NEXT_RENAMEAT(handle,
570 /* For now don't allow renames from or to the default stream. */
571 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
572 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
577 /* Don't rename if the streams are identical. */
578 if (strcasecmp_m(smb_fname_src->stream_name,
579 smb_fname_dst->stream_name) == 0) {
583 /* Get the xattr names. */
584 status = streams_xattr_get_name(handle, talloc_tos(),
585 smb_fname_src->stream_name,
587 if (!NT_STATUS_IS_OK(status)) {
588 errno = map_errno_from_nt_status(status);
591 status = streams_xattr_get_name(handle, talloc_tos(),
592 smb_fname_dst->stream_name,
594 if (!NT_STATUS_IS_OK(status)) {
595 errno = map_errno_from_nt_status(status);
599 full_src = full_path_from_dirfsp_atname(talloc_tos(),
602 if (full_src == NULL) {
606 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
609 if (full_dst == NULL) {
614 /* Get a pathref for full_src (base file, no stream name). */
615 status = synthetic_pathref(talloc_tos(),
616 handle->conn->cwd_fsp,
623 if (!NT_STATUS_IS_OK(status)) {
628 /* Read the old stream from the base file fsp. */
629 status = get_ea_value_fsp(talloc_tos(),
633 if (!NT_STATUS_IS_OK(status)) {
634 errno = map_errno_from_nt_status(status);
638 /* Get a pathref for full_dst (base file, no stream name). */
639 status = synthetic_pathref(talloc_tos(),
640 handle->conn->cwd_fsp,
647 if (!NT_STATUS_IS_OK(status)) {
652 /* (Over)write the new stream on the base file fsp. */
653 nret = SMB_VFS_FSETXATTR(
660 if (errno == ENOATTR) {
667 * Remove the old stream from the base file fsp.
669 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
672 if (errno == ENOATTR) {
682 TALLOC_FREE(pathref_src);
683 TALLOC_FREE(pathref_dst);
684 TALLOC_FREE(full_src);
685 TALLOC_FREE(full_dst);
686 TALLOC_FREE(src_xattr_name);
687 TALLOC_FREE(dst_xattr_name);
691 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
693 const struct smb_filename *smb_fname,
694 bool (*fn)(struct ea_struct *ea,
701 struct streams_xattr_config *config;
703 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
704 return NT_STATUS_UNSUCCESSFUL);
706 status = get_ea_names_from_fsp(talloc_tos(),
710 if (!NT_STATUS_IS_OK(status)) {
714 for (i=0; i<num_names; i++) {
718 * We want to check with samba_private_attr_name()
719 * whether the xattr name is a private one,
720 * unfortunately it flags xattrs that begin with the
721 * default streams prefix as private.
723 * By only calling samba_private_attr_name() in case
724 * the xattr does NOT begin with the default prefix,
725 * we know that if it returns 'true' it definitely one
726 * of our internal xattr like "user.DOSATTRIB".
728 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
729 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
730 if (samba_private_attr_name(names[i])) {
735 if (strncmp(names[i], config->prefix,
736 config->prefix_len) != 0) {
740 status = get_ea_value_fsp(names,
744 if (!NT_STATUS_IS_OK(status)) {
745 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
747 smb_fname->base_name,
752 ea.name = talloc_asprintf(
753 ea.value.data, ":%s%s",
754 names[i] + config->prefix_len,
755 config->store_stream_type ? "" : ":$DATA");
756 if (ea.name == NULL) {
757 DEBUG(0, ("talloc failed\n"));
761 if (!fn(&ea, private_data)) {
762 TALLOC_FREE(ea.value.data);
766 TALLOC_FREE(ea.value.data);
773 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
774 struct stream_struct **streams,
775 const char *name, off_t size,
778 struct stream_struct *tmp;
780 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
786 tmp[*num_streams].name = talloc_strdup(tmp, name);
787 if (tmp[*num_streams].name == NULL) {
791 tmp[*num_streams].size = size;
792 tmp[*num_streams].alloc_size = alloc_size;
799 struct streaminfo_state {
801 vfs_handle_struct *handle;
802 unsigned int num_streams;
803 struct stream_struct *streams;
807 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
809 struct streaminfo_state *state =
810 (struct streaminfo_state *)private_data;
812 if (!add_one_stream(state->mem_ctx,
813 &state->num_streams, &state->streams,
814 ea->name, ea->value.length-1,
815 smb_roundup(state->handle->conn,
816 ea->value.length-1))) {
817 state->status = NT_STATUS_NO_MEMORY;
824 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
825 struct files_struct *fsp,
827 unsigned int *pnum_streams,
828 struct stream_struct **pstreams)
831 struct streaminfo_state state;
833 state.streams = *pstreams;
834 state.num_streams = *pnum_streams;
835 state.mem_ctx = mem_ctx;
836 state.handle = handle;
837 state.status = NT_STATUS_OK;
839 status = walk_xattr_streams(handle,
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_FSTREAMINFO(handle,
865 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
866 enum timestamp_set_resolution *p_ts_res)
868 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
871 static int streams_xattr_connect(vfs_handle_struct *handle,
872 const char *service, const char *user)
874 struct streams_xattr_config *config;
875 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
879 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
884 config = talloc_zero(handle->conn, struct streams_xattr_config);
885 if (config == NULL) {
886 DEBUG(1, ("talloc_zero() failed\n"));
891 prefix = lp_parm_const_string(SNUM(handle->conn),
892 "streams_xattr", "prefix",
894 config->prefix = talloc_strdup(config, prefix);
895 if (config->prefix == NULL) {
896 DEBUG(1, ("talloc_strdup() failed\n"));
900 config->prefix_len = strlen(config->prefix);
901 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
903 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
908 SMB_VFS_HANDLE_SET_DATA(handle, config,
909 NULL, struct stream_xattr_config,
915 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
916 files_struct *fsp, const void *data,
917 size_t n, off_t offset)
919 struct stream_io *sio =
920 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
925 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
928 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
931 if (!streams_xattr_recheck(sio)) {
935 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
937 * Requested write is beyond what can be read based on
938 * samba configuration.
939 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
940 * entire file to be skipped by File Explorer. VFAT returns
941 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
942 * to skip writing metadata, but copy data.
944 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
945 "supported extended attribute size. "
946 "Depending on filesystem type and operating system "
947 "(OS) specifics, this value may be increased using "
948 "the value of the parameter: "
949 "smbd max xattr size = <bytes>. Consult OS and "
950 "filesystem manpages prior to increasing this limit.\n",
951 sio->xattr_name, sio->base);
956 status = get_ea_value_fsp(talloc_tos(),
960 if (!NT_STATUS_IS_OK(status)) {
964 if ((offset + n) > ea.value.length-1) {
967 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
971 TALLOC_FREE(ea.value.data);
976 ea.value.length = offset + n + 1;
977 ea.value.data[offset+n] = 0;
980 memcpy(ea.value.data + offset, data, n);
982 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
987 TALLOC_FREE(ea.value.data);
996 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
997 files_struct *fsp, void *data,
998 size_t n, off_t offset)
1000 struct stream_io *sio =
1001 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1002 struct ea_struct ea;
1004 size_t length, overlap;
1006 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1007 (int)offset, (int)n));
1010 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1013 if (!streams_xattr_recheck(sio)) {
1017 status = get_ea_value_fsp(talloc_tos(),
1021 if (!NT_STATUS_IS_OK(status)) {
1025 length = ea.value.length-1;
1027 DBG_DEBUG("get_ea_value_fsp returned %d bytes\n",
1030 /* Attempt to read past EOF. */
1031 if (length <= offset) {
1035 overlap = (offset + n) > length ? (length - offset) : n;
1036 memcpy(data, ea.value.data + offset, overlap);
1038 TALLOC_FREE(ea.value.data);
1042 struct streams_xattr_pread_state {
1044 struct vfs_aio_state vfs_aio_state;
1047 static void streams_xattr_pread_done(struct tevent_req *subreq);
1049 static struct tevent_req *streams_xattr_pread_send(
1050 struct vfs_handle_struct *handle,
1051 TALLOC_CTX *mem_ctx,
1052 struct tevent_context *ev,
1053 struct files_struct *fsp,
1055 size_t n, off_t offset)
1057 struct tevent_req *req = NULL;
1058 struct tevent_req *subreq = NULL;
1059 struct streams_xattr_pread_state *state = NULL;
1060 struct stream_io *sio =
1061 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1063 req = tevent_req_create(mem_ctx, &state,
1064 struct streams_xattr_pread_state);
1070 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1072 if (tevent_req_nomem(req, subreq)) {
1073 return tevent_req_post(req, ev);
1075 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1079 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1080 if (state->nread != n) {
1081 if (state->nread != -1) {
1084 tevent_req_error(req, errno);
1085 return tevent_req_post(req, ev);
1088 tevent_req_done(req);
1089 return tevent_req_post(req, ev);
1092 static void streams_xattr_pread_done(struct tevent_req *subreq)
1094 struct tevent_req *req = tevent_req_callback_data(
1095 subreq, struct tevent_req);
1096 struct streams_xattr_pread_state *state = tevent_req_data(
1097 req, struct streams_xattr_pread_state);
1099 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1100 TALLOC_FREE(subreq);
1102 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1105 tevent_req_done(req);
1108 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1109 struct vfs_aio_state *vfs_aio_state)
1111 struct streams_xattr_pread_state *state = tevent_req_data(
1112 req, struct streams_xattr_pread_state);
1114 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1118 *vfs_aio_state = state->vfs_aio_state;
1119 return state->nread;
1122 struct streams_xattr_pwrite_state {
1124 struct vfs_aio_state vfs_aio_state;
1127 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1129 static struct tevent_req *streams_xattr_pwrite_send(
1130 struct vfs_handle_struct *handle,
1131 TALLOC_CTX *mem_ctx,
1132 struct tevent_context *ev,
1133 struct files_struct *fsp,
1135 size_t n, off_t offset)
1137 struct tevent_req *req = NULL;
1138 struct tevent_req *subreq = NULL;
1139 struct streams_xattr_pwrite_state *state = NULL;
1140 struct stream_io *sio =
1141 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1143 req = tevent_req_create(mem_ctx, &state,
1144 struct streams_xattr_pwrite_state);
1150 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1152 if (tevent_req_nomem(req, subreq)) {
1153 return tevent_req_post(req, ev);
1155 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1159 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1160 if (state->nwritten != n) {
1161 if (state->nwritten != -1) {
1164 tevent_req_error(req, errno);
1165 return tevent_req_post(req, ev);
1168 tevent_req_done(req);
1169 return tevent_req_post(req, ev);
1172 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1174 struct tevent_req *req = tevent_req_callback_data(
1175 subreq, struct tevent_req);
1176 struct streams_xattr_pwrite_state *state = tevent_req_data(
1177 req, struct streams_xattr_pwrite_state);
1179 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1180 TALLOC_FREE(subreq);
1182 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1185 tevent_req_done(req);
1188 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1189 struct vfs_aio_state *vfs_aio_state)
1191 struct streams_xattr_pwrite_state *state = tevent_req_data(
1192 req, struct streams_xattr_pwrite_state);
1194 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1198 *vfs_aio_state = state->vfs_aio_state;
1199 return state->nwritten;
1202 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1203 struct files_struct *fsp,
1208 struct ea_struct ea;
1210 struct stream_io *sio =
1211 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
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 status = get_ea_value_fsp(talloc_tos(),
1228 if (!NT_STATUS_IS_OK(status)) {
1232 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1236 TALLOC_FREE(ea.value.data);
1241 /* Did we expand ? */
1242 if (ea.value.length < offset + 1) {
1243 memset(&tmp[ea.value.length], '\0',
1244 offset + 1 - ea.value.length);
1247 ea.value.data = tmp;
1248 ea.value.length = offset + 1;
1249 ea.value.data[offset] = 0;
1251 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1257 TALLOC_FREE(ea.value.data);
1266 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1267 struct files_struct *fsp,
1272 struct stream_io *sio =
1273 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1275 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1277 fsp_str_dbg(fsp), (double)offset, (double)len));
1280 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1283 if (!streams_xattr_recheck(sio)) {
1287 /* Let the pwrite code path handle it. */
1292 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1293 uid_t uid, gid_t gid)
1295 struct stream_io *sio =
1296 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1299 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1305 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1309 struct stream_io *sio =
1310 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1313 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1319 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1320 struct files_struct *fsp,
1325 struct stream_io *sio =
1326 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1329 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1336 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1337 struct files_struct *fsp,
1341 struct stream_io *sio =
1342 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1345 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1352 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1353 struct files_struct *fsp,
1356 struct stream_io *sio =
1357 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1360 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1367 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1368 struct files_struct *fsp,
1374 struct stream_io *sio =
1375 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1378 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1386 struct streams_xattr_fsync_state {
1388 struct vfs_aio_state vfs_aio_state;
1391 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1393 static struct tevent_req *streams_xattr_fsync_send(
1394 struct vfs_handle_struct *handle,
1395 TALLOC_CTX *mem_ctx,
1396 struct tevent_context *ev,
1397 struct files_struct *fsp)
1399 struct tevent_req *req = NULL;
1400 struct tevent_req *subreq = NULL;
1401 struct streams_xattr_fsync_state *state = NULL;
1402 struct stream_io *sio =
1403 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1405 req = tevent_req_create(mem_ctx, &state,
1406 struct streams_xattr_fsync_state);
1412 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1413 if (tevent_req_nomem(req, subreq)) {
1414 return tevent_req_post(req, ev);
1416 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1421 * There's no pathname based sync variant and we don't have access to
1422 * the basefile handle, so we can't do anything here.
1425 tevent_req_done(req);
1426 return tevent_req_post(req, ev);
1429 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1431 struct tevent_req *req = tevent_req_callback_data(
1432 subreq, struct tevent_req);
1433 struct streams_xattr_fsync_state *state = tevent_req_data(
1434 req, struct streams_xattr_fsync_state);
1436 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1437 TALLOC_FREE(subreq);
1438 if (state->ret != 0) {
1439 tevent_req_error(req, errno);
1443 tevent_req_done(req);
1446 static int streams_xattr_fsync_recv(struct tevent_req *req,
1447 struct vfs_aio_state *vfs_aio_state)
1449 struct streams_xattr_fsync_state *state = tevent_req_data(
1450 req, struct streams_xattr_fsync_state);
1452 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1456 *vfs_aio_state = state->vfs_aio_state;
1460 static bool streams_xattr_lock(vfs_handle_struct *handle,
1467 struct stream_io *sio =
1468 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1471 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1477 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1484 struct stream_io *sio =
1485 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1488 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1489 pcount, ptype, ppid);
1496 static int streams_xattr_filesystem_sharemode(vfs_handle_struct *handle,
1498 uint32_t share_access,
1499 uint32_t access_mask)
1501 struct stream_io *sio =
1502 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1505 return SMB_VFS_NEXT_FILESYSTEM_SHAREMODE(handle,
1514 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1518 struct stream_io *sio =
1519 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1522 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1528 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1530 struct lock_struct *plock)
1532 struct stream_io *sio =
1533 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1536 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1542 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1543 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1544 .connect_fn = streams_xattr_connect,
1545 .openat_fn = streams_xattr_openat,
1546 .close_fn = streams_xattr_close,
1547 .stat_fn = streams_xattr_stat,
1548 .fstat_fn = streams_xattr_fstat,
1549 .lstat_fn = streams_xattr_lstat,
1550 .pread_fn = streams_xattr_pread,
1551 .pwrite_fn = streams_xattr_pwrite,
1552 .pread_send_fn = streams_xattr_pread_send,
1553 .pread_recv_fn = streams_xattr_pread_recv,
1554 .pwrite_send_fn = streams_xattr_pwrite_send,
1555 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1556 .unlinkat_fn = streams_xattr_unlinkat,
1557 .renameat_fn = streams_xattr_renameat,
1558 .ftruncate_fn = streams_xattr_ftruncate,
1559 .fallocate_fn = streams_xattr_fallocate,
1560 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1562 .fsync_send_fn = streams_xattr_fsync_send,
1563 .fsync_recv_fn = streams_xattr_fsync_recv,
1565 .lock_fn = streams_xattr_lock,
1566 .getlock_fn = streams_xattr_getlock,
1567 .filesystem_sharemode_fn = streams_xattr_filesystem_sharemode,
1568 .linux_setlease_fn = streams_xattr_linux_setlease,
1569 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1571 .fchown_fn = streams_xattr_fchown,
1572 .fchmod_fn = streams_xattr_fchmod,
1574 .fgetxattr_fn = streams_xattr_fgetxattr,
1575 .flistxattr_fn = streams_xattr_flistxattr,
1576 .fremovexattr_fn = streams_xattr_fremovexattr,
1577 .fsetxattr_fn = streams_xattr_fsetxattr,
1581 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1583 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1584 &vfs_streams_xattr_fns);