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 *smb_fname_cp = 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. */
258 * openat_pathref_fsp() checks for the same
259 * filetype as the incoming stat info before allowing
260 * the open, so we must ensure it's correct here.
262 smb_fname->st.st_ex_mode &= ~S_IFMT;
263 smb_fname->st.st_ex_mode |= S_IFREG;
266 * openat_pathref_fsp() expects a talloc'ed
267 * smb_filename. stat can be passed a struct
268 * from the stack. Make a talloc'ed copy
269 * so openat_pathref_fsp() can add its
272 smb_fname_cp = cp_smb_filename(talloc_tos(),
274 if (smb_fname_cp == NULL) {
275 TALLOC_FREE(xattr_name);
279 status = openat_pathref_fsp(handle->conn->cwd_fsp,
281 if (!NT_STATUS_IS_OK(status)) {
282 DBG_DEBUG("openat_pathref_fsp for %s failed with %s\n",
283 smb_fname_str_dbg(smb_fname),
285 TALLOC_FREE(xattr_name);
286 TALLOC_FREE(smb_fname_cp);
287 SET_STAT_INVALID(smb_fname->st);
291 fsp = smb_fname_cp->fsp;
294 SMB_ASSERT(fsp->base_fsp != NULL);
295 smb_fname->st.st_ex_size = get_xattr_size_fsp(fsp->base_fsp,
297 if (smb_fname->st.st_ex_size == -1) {
298 TALLOC_FREE(xattr_name);
299 TALLOC_FREE(smb_fname_cp);
300 SET_STAT_INVALID(smb_fname->st);
305 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
306 smb_fname->st.st_ex_mode &= ~S_IFMT;
307 smb_fname->st.st_ex_mode |= S_IFREG;
308 smb_fname->st.st_ex_blocks =
309 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
311 TALLOC_FREE(xattr_name);
312 TALLOC_FREE(smb_fname_cp);
316 static int streams_xattr_lstat(vfs_handle_struct *handle,
317 struct smb_filename *smb_fname)
319 if (is_named_stream(smb_fname)) {
321 * There can never be EA's on a symlink.
322 * Windows will never see a symlink, and
323 * in SMB_FILENAME_POSIX_PATH mode we don't
324 * allow EA's on a symlink.
326 SET_STAT_INVALID(smb_fname->st);
330 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
333 static int streams_xattr_openat(struct vfs_handle_struct *handle,
334 const struct files_struct *dirfsp,
335 const struct smb_filename *smb_fname,
341 struct streams_xattr_config *config = NULL;
342 struct stream_io *sio = NULL;
344 char *xattr_name = NULL;
346 bool set_empty_xattr = false;
349 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
352 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
353 smb_fname_str_dbg(smb_fname), flags));
355 if (!is_named_stream(smb_fname)) {
356 return SMB_VFS_NEXT_OPENAT(handle,
365 * For now assert this, so the below SMB_VFS_SETXATTR() works.
367 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
369 status = streams_xattr_get_name(handle, talloc_tos(),
370 smb_fname->stream_name, &xattr_name);
371 if (!NT_STATUS_IS_OK(status)) {
372 errno = map_errno_from_nt_status(status);
376 status = get_ea_value_fsp(talloc_tos(),
381 DBG_DEBUG("get_ea_value_fsp returned %s\n", nt_errstr(status));
383 if (!NT_STATUS_IS_OK(status)) {
384 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
386 * The base file is not there. This is an error even if
387 * we got O_CREAT, the higher levels should have created
388 * the base file for us.
390 DBG_DEBUG("streams_xattr_open: base file %s not around, "
391 "returning ENOENT\n", smb_fname->base_name);
396 if (!(flags & O_CREAT)) {
401 set_empty_xattr = true;
404 if (flags & O_TRUNC) {
405 set_empty_xattr = true;
408 if (set_empty_xattr) {
410 * The attribute does not exist or needs to be truncated
414 * Darn, xattrs need at least 1 byte
418 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
419 xattr_name, smb_fname->base_name));
421 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
424 flags & O_EXCL ? XATTR_CREATE : 0);
430 fakefd = vfs_fake_fd();
432 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
438 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
440 if (sio->xattr_name == NULL) {
446 * so->base needs to be a copy of fsp->fsp_name->base_name,
447 * making it identical to streams_xattr_recheck(). If the
448 * open is changing directories, fsp->fsp_name->base_name
449 * will be the full path from the share root, whilst
450 * smb_fname will be relative to the $cwd.
452 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
453 fsp->fsp_name->base_name);
454 if (sio->base == NULL) {
459 sio->fsp_name_ptr = fsp->fsp_name;
460 sio->handle = handle;
467 vfs_fake_fd_close(fakefd);
474 static int streams_xattr_close(vfs_handle_struct *handle,
480 fd = fsp_get_pathref_fd(fsp);
482 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
483 smb_fname_str_dbg(fsp->fsp_name), fd);
485 if (!is_named_stream(fsp->fsp_name)) {
486 return SMB_VFS_NEXT_CLOSE(handle, fsp);
489 ret = vfs_fake_fd_close(fd);
495 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
496 struct files_struct *dirfsp,
497 const struct smb_filename *smb_fname,
502 char *xattr_name = NULL;
504 if (!is_named_stream(smb_fname)) {
505 return SMB_VFS_NEXT_UNLINKAT(handle,
511 status = streams_xattr_get_name(handle, talloc_tos(),
512 smb_fname->stream_name, &xattr_name);
513 if (!NT_STATUS_IS_OK(status)) {
514 errno = map_errno_from_nt_status(status);
518 SMB_ASSERT(smb_fname->fsp != NULL);
519 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
521 ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
523 if ((ret == -1) && (errno == ENOATTR)) {
531 TALLOC_FREE(xattr_name);
535 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
536 struct files_struct *dirfsp,
537 const struct smb_filename *smb_fname,
541 if (flags & AT_REMOVEDIR) {
542 ret = SMB_VFS_NEXT_UNLINKAT(handle,
547 ret = streams_xattr_unlink_internal(handle,
555 static int streams_xattr_renameat(vfs_handle_struct *handle,
556 files_struct *srcfsp,
557 const struct smb_filename *smb_fname_src,
558 files_struct *dstfsp,
559 const struct smb_filename *smb_fname_dst)
563 char *src_xattr_name = NULL;
564 char *dst_xattr_name = NULL;
565 bool src_is_stream, dst_is_stream;
569 struct smb_filename *pathref_src = NULL;
570 struct smb_filename *pathref_dst = NULL;
571 struct smb_filename *full_src = NULL;
572 struct smb_filename *full_dst = NULL;
574 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
575 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
577 if (!src_is_stream && !dst_is_stream) {
578 return SMB_VFS_NEXT_RENAMEAT(handle,
585 /* For now don't allow renames from or to the default stream. */
586 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
587 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
592 /* Don't rename if the streams are identical. */
593 if (strcasecmp_m(smb_fname_src->stream_name,
594 smb_fname_dst->stream_name) == 0) {
598 /* Get the xattr names. */
599 status = streams_xattr_get_name(handle, talloc_tos(),
600 smb_fname_src->stream_name,
602 if (!NT_STATUS_IS_OK(status)) {
603 errno = map_errno_from_nt_status(status);
606 status = streams_xattr_get_name(handle, talloc_tos(),
607 smb_fname_dst->stream_name,
609 if (!NT_STATUS_IS_OK(status)) {
610 errno = map_errno_from_nt_status(status);
614 full_src = full_path_from_dirfsp_atname(talloc_tos(),
617 if (full_src == NULL) {
621 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
624 if (full_dst == NULL) {
629 /* Get a pathref for full_src (base file, no stream name). */
630 status = synthetic_pathref(talloc_tos(),
631 handle->conn->cwd_fsp,
638 if (!NT_STATUS_IS_OK(status)) {
643 /* Read the old stream from the base file fsp. */
644 status = get_ea_value_fsp(talloc_tos(),
648 if (!NT_STATUS_IS_OK(status)) {
649 errno = map_errno_from_nt_status(status);
653 /* Get a pathref for full_dst (base file, no stream name). */
654 status = synthetic_pathref(talloc_tos(),
655 handle->conn->cwd_fsp,
662 if (!NT_STATUS_IS_OK(status)) {
667 /* (Over)write the new stream on the base file fsp. */
668 nret = SMB_VFS_FSETXATTR(
675 if (errno == ENOATTR) {
682 * Remove the old stream from the base file fsp.
684 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
687 if (errno == ENOATTR) {
697 TALLOC_FREE(pathref_src);
698 TALLOC_FREE(pathref_dst);
699 TALLOC_FREE(full_src);
700 TALLOC_FREE(full_dst);
701 TALLOC_FREE(src_xattr_name);
702 TALLOC_FREE(dst_xattr_name);
706 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
708 const struct smb_filename *smb_fname,
709 bool (*fn)(struct ea_struct *ea,
716 struct streams_xattr_config *config;
718 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
719 return NT_STATUS_UNSUCCESSFUL);
721 status = get_ea_names_from_file(talloc_tos(),
725 if (!NT_STATUS_IS_OK(status)) {
729 for (i=0; i<num_names; i++) {
733 * We want to check with samba_private_attr_name()
734 * whether the xattr name is a private one,
735 * unfortunately it flags xattrs that begin with the
736 * default streams prefix as private.
738 * By only calling samba_private_attr_name() in case
739 * the xattr does NOT begin with the default prefix,
740 * we know that if it returns 'true' it definitely one
741 * of our internal xattr like "user.DOSATTRIB".
743 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
744 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
745 if (samba_private_attr_name(names[i])) {
750 if (strncmp(names[i], config->prefix,
751 config->prefix_len) != 0) {
755 status = get_ea_value_fsp(names,
759 if (!NT_STATUS_IS_OK(status)) {
760 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
762 smb_fname->base_name,
767 ea.name = talloc_asprintf(
768 ea.value.data, ":%s%s",
769 names[i] + config->prefix_len,
770 config->store_stream_type ? "" : ":$DATA");
771 if (ea.name == NULL) {
772 DEBUG(0, ("talloc failed\n"));
776 if (!fn(&ea, private_data)) {
777 TALLOC_FREE(ea.value.data);
781 TALLOC_FREE(ea.value.data);
788 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
789 struct stream_struct **streams,
790 const char *name, off_t size,
793 struct stream_struct *tmp;
795 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
801 tmp[*num_streams].name = talloc_strdup(tmp, name);
802 if (tmp[*num_streams].name == NULL) {
806 tmp[*num_streams].size = size;
807 tmp[*num_streams].alloc_size = alloc_size;
814 struct streaminfo_state {
816 vfs_handle_struct *handle;
817 unsigned int num_streams;
818 struct stream_struct *streams;
822 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
824 struct streaminfo_state *state =
825 (struct streaminfo_state *)private_data;
827 if (!add_one_stream(state->mem_ctx,
828 &state->num_streams, &state->streams,
829 ea->name, ea->value.length-1,
830 smb_roundup(state->handle->conn,
831 ea->value.length-1))) {
832 state->status = NT_STATUS_NO_MEMORY;
839 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
840 struct files_struct *fsp,
842 unsigned int *pnum_streams,
843 struct stream_struct **pstreams)
846 struct streaminfo_state state;
848 state.streams = *pstreams;
849 state.num_streams = *pnum_streams;
850 state.mem_ctx = mem_ctx;
851 state.handle = handle;
852 state.status = NT_STATUS_OK;
854 status = walk_xattr_streams(handle,
860 if (!NT_STATUS_IS_OK(status)) {
861 TALLOC_FREE(state.streams);
865 if (!NT_STATUS_IS_OK(state.status)) {
866 TALLOC_FREE(state.streams);
870 *pnum_streams = state.num_streams;
871 *pstreams = state.streams;
873 return SMB_VFS_NEXT_FSTREAMINFO(handle,
880 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
881 enum timestamp_set_resolution *p_ts_res)
883 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
886 static int streams_xattr_connect(vfs_handle_struct *handle,
887 const char *service, const char *user)
889 struct streams_xattr_config *config;
890 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
894 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
899 config = talloc_zero(handle->conn, struct streams_xattr_config);
900 if (config == NULL) {
901 DEBUG(1, ("talloc_zero() failed\n"));
906 prefix = lp_parm_const_string(SNUM(handle->conn),
907 "streams_xattr", "prefix",
909 config->prefix = talloc_strdup(config, prefix);
910 if (config->prefix == NULL) {
911 DEBUG(1, ("talloc_strdup() failed\n"));
915 config->prefix_len = strlen(config->prefix);
916 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
918 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
923 SMB_VFS_HANDLE_SET_DATA(handle, config,
924 NULL, struct stream_xattr_config,
930 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
931 files_struct *fsp, const void *data,
932 size_t n, off_t offset)
934 struct stream_io *sio =
935 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
940 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
943 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
946 if (!streams_xattr_recheck(sio)) {
950 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
952 * Requested write is beyond what can be read based on
953 * samba configuration.
954 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
955 * entire file to be skipped by File Explorer. VFAT returns
956 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
957 * to skip writing metadata, but copy data.
959 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
960 "supported extended attribute size. "
961 "Depending on filesystem type and operating system "
962 "(OS) specifics, this value may be increased using "
963 "the value of the parameter: "
964 "smbd max xattr size = <bytes>. Consult OS and "
965 "filesystem manpages prior to increasing this limit.\n",
966 sio->xattr_name, sio->base);
971 status = get_ea_value_fsp(talloc_tos(),
975 if (!NT_STATUS_IS_OK(status)) {
979 if ((offset + n) > ea.value.length-1) {
982 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
986 TALLOC_FREE(ea.value.data);
991 ea.value.length = offset + n + 1;
992 ea.value.data[offset+n] = 0;
995 memcpy(ea.value.data + offset, data, n);
997 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1002 TALLOC_FREE(ea.value.data);
1011 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1012 files_struct *fsp, void *data,
1013 size_t n, off_t offset)
1015 struct stream_io *sio =
1016 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1017 struct ea_struct ea;
1019 size_t length, overlap;
1021 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1022 (int)offset, (int)n));
1025 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1028 if (!streams_xattr_recheck(sio)) {
1032 status = get_ea_value_fsp(talloc_tos(),
1036 if (!NT_STATUS_IS_OK(status)) {
1040 length = ea.value.length-1;
1042 DBG_DEBUG("get_ea_value_fsp returned %d bytes\n",
1045 /* Attempt to read past EOF. */
1046 if (length <= offset) {
1050 overlap = (offset + n) > length ? (length - offset) : n;
1051 memcpy(data, ea.value.data + offset, overlap);
1053 TALLOC_FREE(ea.value.data);
1057 struct streams_xattr_pread_state {
1059 struct vfs_aio_state vfs_aio_state;
1062 static void streams_xattr_pread_done(struct tevent_req *subreq);
1064 static struct tevent_req *streams_xattr_pread_send(
1065 struct vfs_handle_struct *handle,
1066 TALLOC_CTX *mem_ctx,
1067 struct tevent_context *ev,
1068 struct files_struct *fsp,
1070 size_t n, off_t offset)
1072 struct tevent_req *req = NULL;
1073 struct tevent_req *subreq = NULL;
1074 struct streams_xattr_pread_state *state = NULL;
1075 struct stream_io *sio =
1076 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1078 req = tevent_req_create(mem_ctx, &state,
1079 struct streams_xattr_pread_state);
1085 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1087 if (tevent_req_nomem(req, subreq)) {
1088 return tevent_req_post(req, ev);
1090 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1094 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1095 if (state->nread != n) {
1096 if (state->nread != -1) {
1099 tevent_req_error(req, errno);
1100 return tevent_req_post(req, ev);
1103 tevent_req_done(req);
1104 return tevent_req_post(req, ev);
1107 static void streams_xattr_pread_done(struct tevent_req *subreq)
1109 struct tevent_req *req = tevent_req_callback_data(
1110 subreq, struct tevent_req);
1111 struct streams_xattr_pread_state *state = tevent_req_data(
1112 req, struct streams_xattr_pread_state);
1114 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1115 TALLOC_FREE(subreq);
1117 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1120 tevent_req_done(req);
1123 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1124 struct vfs_aio_state *vfs_aio_state)
1126 struct streams_xattr_pread_state *state = tevent_req_data(
1127 req, struct streams_xattr_pread_state);
1129 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1133 *vfs_aio_state = state->vfs_aio_state;
1134 return state->nread;
1137 struct streams_xattr_pwrite_state {
1139 struct vfs_aio_state vfs_aio_state;
1142 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1144 static struct tevent_req *streams_xattr_pwrite_send(
1145 struct vfs_handle_struct *handle,
1146 TALLOC_CTX *mem_ctx,
1147 struct tevent_context *ev,
1148 struct files_struct *fsp,
1150 size_t n, off_t offset)
1152 struct tevent_req *req = NULL;
1153 struct tevent_req *subreq = NULL;
1154 struct streams_xattr_pwrite_state *state = NULL;
1155 struct stream_io *sio =
1156 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1158 req = tevent_req_create(mem_ctx, &state,
1159 struct streams_xattr_pwrite_state);
1165 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1167 if (tevent_req_nomem(req, subreq)) {
1168 return tevent_req_post(req, ev);
1170 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1174 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1175 if (state->nwritten != n) {
1176 if (state->nwritten != -1) {
1179 tevent_req_error(req, errno);
1180 return tevent_req_post(req, ev);
1183 tevent_req_done(req);
1184 return tevent_req_post(req, ev);
1187 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1189 struct tevent_req *req = tevent_req_callback_data(
1190 subreq, struct tevent_req);
1191 struct streams_xattr_pwrite_state *state = tevent_req_data(
1192 req, struct streams_xattr_pwrite_state);
1194 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1195 TALLOC_FREE(subreq);
1197 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1200 tevent_req_done(req);
1203 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1204 struct vfs_aio_state *vfs_aio_state)
1206 struct streams_xattr_pwrite_state *state = tevent_req_data(
1207 req, struct streams_xattr_pwrite_state);
1209 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1213 *vfs_aio_state = state->vfs_aio_state;
1214 return state->nwritten;
1217 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1218 struct files_struct *fsp,
1223 struct ea_struct ea;
1225 struct stream_io *sio =
1226 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1228 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1229 fsp_str_dbg(fsp), (double)offset));
1232 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1235 if (!streams_xattr_recheck(sio)) {
1239 status = get_ea_value_fsp(talloc_tos(),
1243 if (!NT_STATUS_IS_OK(status)) {
1247 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1251 TALLOC_FREE(ea.value.data);
1256 /* Did we expand ? */
1257 if (ea.value.length < offset + 1) {
1258 memset(&tmp[ea.value.length], '\0',
1259 offset + 1 - ea.value.length);
1262 ea.value.data = tmp;
1263 ea.value.length = offset + 1;
1264 ea.value.data[offset] = 0;
1266 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1272 TALLOC_FREE(ea.value.data);
1281 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1282 struct files_struct *fsp,
1287 struct stream_io *sio =
1288 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1290 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1292 fsp_str_dbg(fsp), (double)offset, (double)len));
1295 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1298 if (!streams_xattr_recheck(sio)) {
1302 /* Let the pwrite code path handle it. */
1307 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1308 uid_t uid, gid_t gid)
1310 struct stream_io *sio =
1311 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1314 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1320 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1324 struct stream_io *sio =
1325 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1328 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1334 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1335 struct files_struct *fsp,
1340 struct stream_io *sio =
1341 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1344 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1351 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1352 struct files_struct *fsp,
1356 struct stream_io *sio =
1357 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1360 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1367 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1368 struct files_struct *fsp,
1371 struct stream_io *sio =
1372 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1375 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1382 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1383 struct files_struct *fsp,
1389 struct stream_io *sio =
1390 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1393 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1401 struct streams_xattr_fsync_state {
1403 struct vfs_aio_state vfs_aio_state;
1406 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1408 static struct tevent_req *streams_xattr_fsync_send(
1409 struct vfs_handle_struct *handle,
1410 TALLOC_CTX *mem_ctx,
1411 struct tevent_context *ev,
1412 struct files_struct *fsp)
1414 struct tevent_req *req = NULL;
1415 struct tevent_req *subreq = NULL;
1416 struct streams_xattr_fsync_state *state = NULL;
1417 struct stream_io *sio =
1418 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1420 req = tevent_req_create(mem_ctx, &state,
1421 struct streams_xattr_fsync_state);
1427 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1428 if (tevent_req_nomem(req, subreq)) {
1429 return tevent_req_post(req, ev);
1431 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1436 * There's no pathname based sync variant and we don't have access to
1437 * the basefile handle, so we can't do anything here.
1440 tevent_req_done(req);
1441 return tevent_req_post(req, ev);
1444 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1446 struct tevent_req *req = tevent_req_callback_data(
1447 subreq, struct tevent_req);
1448 struct streams_xattr_fsync_state *state = tevent_req_data(
1449 req, struct streams_xattr_fsync_state);
1451 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1452 TALLOC_FREE(subreq);
1453 if (state->ret != 0) {
1454 tevent_req_error(req, errno);
1458 tevent_req_done(req);
1461 static int streams_xattr_fsync_recv(struct tevent_req *req,
1462 struct vfs_aio_state *vfs_aio_state)
1464 struct streams_xattr_fsync_state *state = tevent_req_data(
1465 req, struct streams_xattr_fsync_state);
1467 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1471 *vfs_aio_state = state->vfs_aio_state;
1475 static bool streams_xattr_lock(vfs_handle_struct *handle,
1482 struct stream_io *sio =
1483 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1486 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1492 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1499 struct stream_io *sio =
1500 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1503 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1504 pcount, ptype, ppid);
1511 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1513 uint32_t share_access,
1514 uint32_t access_mask)
1516 struct stream_io *sio =
1517 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1520 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1521 share_access, access_mask);
1527 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1531 struct stream_io *sio =
1532 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1535 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1541 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1543 struct lock_struct *plock)
1545 struct stream_io *sio =
1546 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1549 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1555 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1556 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1557 .connect_fn = streams_xattr_connect,
1558 .openat_fn = streams_xattr_openat,
1559 .close_fn = streams_xattr_close,
1560 .stat_fn = streams_xattr_stat,
1561 .fstat_fn = streams_xattr_fstat,
1562 .lstat_fn = streams_xattr_lstat,
1563 .pread_fn = streams_xattr_pread,
1564 .pwrite_fn = streams_xattr_pwrite,
1565 .pread_send_fn = streams_xattr_pread_send,
1566 .pread_recv_fn = streams_xattr_pread_recv,
1567 .pwrite_send_fn = streams_xattr_pwrite_send,
1568 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1569 .unlinkat_fn = streams_xattr_unlinkat,
1570 .renameat_fn = streams_xattr_renameat,
1571 .ftruncate_fn = streams_xattr_ftruncate,
1572 .fallocate_fn = streams_xattr_fallocate,
1573 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1575 .fsync_send_fn = streams_xattr_fsync_send,
1576 .fsync_recv_fn = streams_xattr_fsync_recv,
1578 .lock_fn = streams_xattr_lock,
1579 .getlock_fn = streams_xattr_getlock,
1580 .kernel_flock_fn = streams_xattr_kernel_flock,
1581 .linux_setlease_fn = streams_xattr_linux_setlease,
1582 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1584 .fchown_fn = streams_xattr_fchown,
1585 .fchmod_fn = streams_xattr_fchmod,
1587 .fgetxattr_fn = streams_xattr_fgetxattr,
1588 .flistxattr_fn = streams_xattr_flistxattr,
1589 .fremovexattr_fn = streams_xattr_fremovexattr,
1590 .fsetxattr_fn = streams_xattr_fsetxattr,
1594 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1596 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1597 &vfs_streams_xattr_fns);