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(connection_struct *conn,
49 const struct smb_filename *smb_fname,
50 const char *xattr_name)
56 status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
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;
179 * Helper to stat/lstat the base file of an smb_fname.
181 static int streams_xattr_stat_base(vfs_handle_struct *handle,
182 struct smb_filename *smb_fname,
185 char *tmp_stream_name;
188 tmp_stream_name = smb_fname->stream_name;
189 smb_fname->stream_name = NULL;
191 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
193 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
195 smb_fname->stream_name = tmp_stream_name;
199 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
200 SMB_STRUCT_STAT *sbuf)
202 struct smb_filename *smb_fname_base = NULL;
204 struct stream_io *io = (struct stream_io *)
205 VFS_FETCH_FSP_EXTENSION(handle, fsp);
207 if (io == NULL || fsp->base_fsp == NULL) {
208 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
211 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
213 if (!streams_xattr_recheck(io)) {
217 /* Create an smb_filename with stream_name == NULL. */
218 smb_fname_base = synthetic_smb_fname(talloc_tos(),
223 fsp->fsp_name->flags);
224 if (smb_fname_base == NULL) {
229 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
230 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
232 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
234 *sbuf = smb_fname_base->st;
237 TALLOC_FREE(smb_fname_base);
241 sbuf->st_ex_size = get_xattr_size(handle->conn,
242 smb_fname_base, io->xattr_name);
243 if (sbuf->st_ex_size == -1) {
244 TALLOC_FREE(smb_fname_base);
245 SET_STAT_INVALID(*sbuf);
249 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
251 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
252 sbuf->st_ex_mode &= ~S_IFMT;
253 sbuf->st_ex_mode &= ~S_IFDIR;
254 sbuf->st_ex_mode |= S_IFREG;
255 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
257 TALLOC_FREE(smb_fname_base);
261 static int streams_xattr_stat(vfs_handle_struct *handle,
262 struct smb_filename *smb_fname)
266 char *xattr_name = NULL;
268 if (!is_named_stream(smb_fname)) {
269 return SMB_VFS_NEXT_STAT(handle, smb_fname);
272 /* Note if lp_posix_paths() is true, we can never
273 * get here as is_named_stream() is
274 * always false. So we never need worry about
275 * not following links here. */
277 /* Populate the stat struct with info from the base file. */
278 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
282 /* Derive the xattr name to lookup. */
283 status = streams_xattr_get_name(handle, talloc_tos(),
284 smb_fname->stream_name, &xattr_name);
285 if (!NT_STATUS_IS_OK(status)) {
286 errno = map_errno_from_nt_status(status);
290 /* Augment the base file's stat information before returning. */
291 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
294 if (smb_fname->st.st_ex_size == -1) {
295 SET_STAT_INVALID(smb_fname->st);
301 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
302 smb_fname->st.st_ex_mode &= ~S_IFMT;
303 smb_fname->st.st_ex_mode &= ~S_IFDIR;
304 smb_fname->st.st_ex_mode |= S_IFREG;
305 smb_fname->st.st_ex_blocks =
306 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
310 TALLOC_FREE(xattr_name);
314 static int streams_xattr_lstat(vfs_handle_struct *handle,
315 struct smb_filename *smb_fname)
319 char *xattr_name = NULL;
321 if (!is_named_stream(smb_fname)) {
322 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
325 /* Populate the stat struct with info from the base file. */
326 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
330 /* Derive the xattr name to lookup. */
331 status = streams_xattr_get_name(handle, talloc_tos(),
332 smb_fname->stream_name, &xattr_name);
333 if (!NT_STATUS_IS_OK(status)) {
334 errno = map_errno_from_nt_status(status);
338 /* Augment the base file's stat information before returning. */
339 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
342 if (smb_fname->st.st_ex_size == -1) {
343 SET_STAT_INVALID(smb_fname->st);
349 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
350 smb_fname->st.st_ex_mode &= ~S_IFMT;
351 smb_fname->st.st_ex_mode |= S_IFREG;
352 smb_fname->st.st_ex_blocks =
353 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
358 TALLOC_FREE(xattr_name);
362 static int streams_xattr_open(vfs_handle_struct *handle,
363 struct smb_filename *smb_fname,
364 files_struct *fsp, int flags, mode_t mode)
367 struct streams_xattr_config *config = NULL;
368 struct stream_io *sio = NULL;
370 char *xattr_name = NULL;
373 bool set_empty_xattr = false;
376 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
379 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
380 smb_fname_str_dbg(smb_fname), flags));
382 if (!is_named_stream(smb_fname)) {
383 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
386 status = streams_xattr_get_name(handle, talloc_tos(),
387 smb_fname->stream_name, &xattr_name);
388 if (!NT_STATUS_IS_OK(status)) {
389 errno = map_errno_from_nt_status(status);
393 status = get_ea_value(talloc_tos(), handle->conn, NULL,
394 smb_fname, xattr_name, &ea);
396 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
398 if (!NT_STATUS_IS_OK(status)) {
399 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
401 * The base file is not there. This is an error even if
402 * we got O_CREAT, the higher levels should have created
403 * the base file for us.
405 DBG_DEBUG("streams_xattr_open: base file %s not around, "
406 "returning ENOENT\n", smb_fname->base_name);
411 if (!(flags & O_CREAT)) {
416 set_empty_xattr = true;
419 if (flags & O_TRUNC) {
420 set_empty_xattr = true;
423 if (set_empty_xattr) {
425 * The attribute does not exist or needs to be truncated
429 * Darn, xattrs need at least 1 byte
433 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
434 xattr_name, smb_fname->base_name));
436 ret = SMB_VFS_SETXATTR(fsp->conn,
440 flags & O_EXCL ? XATTR_CREATE : 0);
447 * Return a valid fd, but ensure any attempt to use it returns an error
450 ret = pipe(pipe_fds);
457 fakefd = pipe_fds[0];
459 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
465 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
467 if (sio->xattr_name == NULL) {
473 * so->base needs to be a copy of fsp->fsp_name->base_name,
474 * making it identical to streams_xattr_recheck(). If the
475 * open is changing directories, fsp->fsp_name->base_name
476 * will be the full path from the share root, whilst
477 * smb_fname will be relative to the $cwd.
479 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
480 fsp->fsp_name->base_name);
481 if (sio->base == NULL) {
486 sio->fsp_name_ptr = fsp->fsp_name;
487 sio->handle = handle;
501 static int streams_xattr_close(vfs_handle_struct *handle,
509 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
510 smb_fname_str_dbg(fsp->fsp_name), fd);
512 if (!is_named_stream(fsp->fsp_name)) {
513 return SMB_VFS_NEXT_CLOSE(handle, fsp);
522 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
523 struct files_struct *dirfsp,
524 const struct smb_filename *smb_fname,
529 char *xattr_name = NULL;
531 if (!is_named_stream(smb_fname)) {
532 return SMB_VFS_NEXT_UNLINKAT(handle,
538 status = streams_xattr_get_name(handle, talloc_tos(),
539 smb_fname->stream_name, &xattr_name);
540 if (!NT_STATUS_IS_OK(status)) {
541 errno = map_errno_from_nt_status(status);
545 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
547 if ((ret == -1) && (errno == ENOATTR)) {
555 TALLOC_FREE(xattr_name);
559 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
560 struct files_struct *dirfsp,
561 const struct smb_filename *smb_fname,
565 if (flags & AT_REMOVEDIR) {
566 ret = SMB_VFS_NEXT_UNLINKAT(handle,
571 ret = streams_xattr_unlink_internal(handle,
579 static int streams_xattr_renameat(vfs_handle_struct *handle,
580 files_struct *srcfsp,
581 const struct smb_filename *smb_fname_src,
582 files_struct *dstfsp,
583 const struct smb_filename *smb_fname_dst)
587 char *src_xattr_name = NULL;
588 char *dst_xattr_name = NULL;
589 bool src_is_stream, dst_is_stream;
594 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
595 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
597 if (!src_is_stream && !dst_is_stream) {
598 return SMB_VFS_NEXT_RENAMEAT(handle,
605 /* For now don't allow renames from or to the default stream. */
606 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
607 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
612 /* Don't rename if the streams are identical. */
613 if (strcasecmp_m(smb_fname_src->stream_name,
614 smb_fname_dst->stream_name) == 0) {
618 /* Get the xattr names. */
619 status = streams_xattr_get_name(handle, talloc_tos(),
620 smb_fname_src->stream_name,
622 if (!NT_STATUS_IS_OK(status)) {
623 errno = map_errno_from_nt_status(status);
626 status = streams_xattr_get_name(handle, talloc_tos(),
627 smb_fname_dst->stream_name,
629 if (!NT_STATUS_IS_OK(status)) {
630 errno = map_errno_from_nt_status(status);
634 /* read the old stream */
635 status = get_ea_value(talloc_tos(), handle->conn, NULL,
636 smb_fname_src, src_xattr_name, &ea);
637 if (!NT_STATUS_IS_OK(status)) {
642 /* (over)write the new stream */
643 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
644 dst_xattr_name, ea.value.data, ea.value.length,
647 if (errno == ENOATTR) {
653 /* remove the old stream */
654 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
657 if (errno == ENOATTR) {
667 TALLOC_FREE(src_xattr_name);
668 TALLOC_FREE(dst_xattr_name);
672 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
674 const struct smb_filename *smb_fname,
675 bool (*fn)(struct ea_struct *ea,
682 struct streams_xattr_config *config;
684 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
685 return NT_STATUS_UNSUCCESSFUL);
687 status = get_ea_names_from_file(talloc_tos(),
693 if (!NT_STATUS_IS_OK(status)) {
697 for (i=0; i<num_names; i++) {
701 * We want to check with samba_private_attr_name()
702 * whether the xattr name is a private one,
703 * unfortunately it flags xattrs that begin with the
704 * default streams prefix as private.
706 * By only calling samba_private_attr_name() in case
707 * the xattr does NOT begin with the default prefix,
708 * we know that if it returns 'true' it definitely one
709 * of our internal xattr like "user.DOSATTRIB".
711 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
712 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
713 if (samba_private_attr_name(names[i])) {
718 if (strncmp(names[i], config->prefix,
719 config->prefix_len) != 0) {
723 status = get_ea_value(names,
729 if (!NT_STATUS_IS_OK(status)) {
730 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
732 smb_fname->base_name,
737 ea.name = talloc_asprintf(
738 ea.value.data, ":%s%s",
739 names[i] + config->prefix_len,
740 config->store_stream_type ? "" : ":$DATA");
741 if (ea.name == NULL) {
742 DEBUG(0, ("talloc failed\n"));
746 if (!fn(&ea, private_data)) {
747 TALLOC_FREE(ea.value.data);
751 TALLOC_FREE(ea.value.data);
758 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
759 struct stream_struct **streams,
760 const char *name, off_t size,
763 struct stream_struct *tmp;
765 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
771 tmp[*num_streams].name = talloc_strdup(tmp, name);
772 if (tmp[*num_streams].name == NULL) {
776 tmp[*num_streams].size = size;
777 tmp[*num_streams].alloc_size = alloc_size;
784 struct streaminfo_state {
786 vfs_handle_struct *handle;
787 unsigned int num_streams;
788 struct stream_struct *streams;
792 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
794 struct streaminfo_state *state =
795 (struct streaminfo_state *)private_data;
797 if (!add_one_stream(state->mem_ctx,
798 &state->num_streams, &state->streams,
799 ea->name, ea->value.length-1,
800 smb_roundup(state->handle->conn,
801 ea->value.length-1))) {
802 state->status = NT_STATUS_NO_MEMORY;
809 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
810 struct files_struct *fsp,
811 const struct smb_filename *smb_fname,
813 unsigned int *pnum_streams,
814 struct stream_struct **pstreams)
816 SMB_STRUCT_STAT sbuf;
819 struct streaminfo_state state;
821 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
823 return map_nt_error_from_unix(errno);
826 state.streams = *pstreams;
827 state.num_streams = *pnum_streams;
828 state.mem_ctx = mem_ctx;
829 state.handle = handle;
830 state.status = NT_STATUS_OK;
832 if (S_ISLNK(sbuf.st_ex_mode)) {
834 * Currently we do't have SMB_VFS_LLISTXATTR
835 * inside the VFS which means there's no way
836 * to cope with a symlink when lp_posix_pathnames().
837 * returns true. For now ignore links.
838 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
840 status = NT_STATUS_OK;
842 status = walk_xattr_streams(handle, fsp, smb_fname,
843 collect_one_stream, &state);
846 if (!NT_STATUS_IS_OK(status)) {
847 TALLOC_FREE(state.streams);
851 if (!NT_STATUS_IS_OK(state.status)) {
852 TALLOC_FREE(state.streams);
856 *pnum_streams = state.num_streams;
857 *pstreams = state.streams;
859 return SMB_VFS_NEXT_STREAMINFO(handle,
867 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
868 enum timestamp_set_resolution *p_ts_res)
870 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
873 static int streams_xattr_connect(vfs_handle_struct *handle,
874 const char *service, const char *user)
876 struct streams_xattr_config *config;
877 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
881 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
886 config = talloc_zero(handle->conn, struct streams_xattr_config);
887 if (config == NULL) {
888 DEBUG(1, ("talloc_zero() failed\n"));
893 prefix = lp_parm_const_string(SNUM(handle->conn),
894 "streams_xattr", "prefix",
896 config->prefix = talloc_strdup(config, prefix);
897 if (config->prefix == NULL) {
898 DEBUG(1, ("talloc_strdup() failed\n"));
902 config->prefix_len = strlen(config->prefix);
903 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
905 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
910 SMB_VFS_HANDLE_SET_DATA(handle, config,
911 NULL, struct stream_xattr_config,
917 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
918 files_struct *fsp, const void *data,
919 size_t n, off_t offset)
921 struct stream_io *sio =
922 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
925 struct smb_filename *smb_fname_base = NULL;
928 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
931 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
934 if (!streams_xattr_recheck(sio)) {
938 /* Create an smb_filename with stream_name == NULL. */
939 smb_fname_base = synthetic_smb_fname(talloc_tos(),
944 fsp->fsp_name->flags);
945 if (smb_fname_base == NULL) {
950 status = get_ea_value(talloc_tos(), handle->conn, NULL,
951 smb_fname_base, sio->xattr_name, &ea);
952 if (!NT_STATUS_IS_OK(status)) {
956 if ((offset + n) > ea.value.length-1) {
959 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
963 TALLOC_FREE(ea.value.data);
968 ea.value.length = offset + n + 1;
969 ea.value.data[offset+n] = 0;
972 memcpy(ea.value.data + offset, data, n);
974 ret = SMB_VFS_SETXATTR(fsp->conn,
977 ea.value.data, ea.value.length, 0);
978 TALLOC_FREE(ea.value.data);
987 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
988 files_struct *fsp, void *data,
989 size_t n, off_t offset)
991 struct stream_io *sio =
992 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
995 size_t length, overlap;
996 struct smb_filename *smb_fname_base = NULL;
998 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
999 (int)offset, (int)n));
1002 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1005 if (!streams_xattr_recheck(sio)) {
1009 /* Create an smb_filename with stream_name == NULL. */
1010 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1014 fsp->fsp_name->twrp,
1015 fsp->fsp_name->flags);
1016 if (smb_fname_base == NULL) {
1021 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1022 smb_fname_base, sio->xattr_name, &ea);
1023 if (!NT_STATUS_IS_OK(status)) {
1027 length = ea.value.length-1;
1029 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1032 /* Attempt to read past EOF. */
1033 if (length <= offset) {
1037 overlap = (offset + n) > length ? (length - offset) : n;
1038 memcpy(data, ea.value.data + offset, overlap);
1040 TALLOC_FREE(ea.value.data);
1044 struct streams_xattr_pread_state {
1046 struct vfs_aio_state vfs_aio_state;
1049 static void streams_xattr_pread_done(struct tevent_req *subreq);
1051 static struct tevent_req *streams_xattr_pread_send(
1052 struct vfs_handle_struct *handle,
1053 TALLOC_CTX *mem_ctx,
1054 struct tevent_context *ev,
1055 struct files_struct *fsp,
1057 size_t n, off_t offset)
1059 struct tevent_req *req = NULL;
1060 struct tevent_req *subreq = NULL;
1061 struct streams_xattr_pread_state *state = NULL;
1062 struct stream_io *sio =
1063 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1065 req = tevent_req_create(mem_ctx, &state,
1066 struct streams_xattr_pread_state);
1072 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1074 if (tevent_req_nomem(req, subreq)) {
1075 return tevent_req_post(req, ev);
1077 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1081 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1082 if (state->nread != n) {
1083 if (state->nread != -1) {
1086 tevent_req_error(req, errno);
1087 return tevent_req_post(req, ev);
1090 tevent_req_done(req);
1091 return tevent_req_post(req, ev);
1094 static void streams_xattr_pread_done(struct tevent_req *subreq)
1096 struct tevent_req *req = tevent_req_callback_data(
1097 subreq, struct tevent_req);
1098 struct streams_xattr_pread_state *state = tevent_req_data(
1099 req, struct streams_xattr_pread_state);
1101 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1102 TALLOC_FREE(subreq);
1104 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1107 tevent_req_done(req);
1110 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1111 struct vfs_aio_state *vfs_aio_state)
1113 struct streams_xattr_pread_state *state = tevent_req_data(
1114 req, struct streams_xattr_pread_state);
1116 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1120 *vfs_aio_state = state->vfs_aio_state;
1121 return state->nread;
1124 struct streams_xattr_pwrite_state {
1126 struct vfs_aio_state vfs_aio_state;
1129 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1131 static struct tevent_req *streams_xattr_pwrite_send(
1132 struct vfs_handle_struct *handle,
1133 TALLOC_CTX *mem_ctx,
1134 struct tevent_context *ev,
1135 struct files_struct *fsp,
1137 size_t n, off_t offset)
1139 struct tevent_req *req = NULL;
1140 struct tevent_req *subreq = NULL;
1141 struct streams_xattr_pwrite_state *state = NULL;
1142 struct stream_io *sio =
1143 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1145 req = tevent_req_create(mem_ctx, &state,
1146 struct streams_xattr_pwrite_state);
1152 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1154 if (tevent_req_nomem(req, subreq)) {
1155 return tevent_req_post(req, ev);
1157 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1161 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1162 if (state->nwritten != n) {
1163 if (state->nwritten != -1) {
1166 tevent_req_error(req, errno);
1167 return tevent_req_post(req, ev);
1170 tevent_req_done(req);
1171 return tevent_req_post(req, ev);
1174 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1176 struct tevent_req *req = tevent_req_callback_data(
1177 subreq, struct tevent_req);
1178 struct streams_xattr_pwrite_state *state = tevent_req_data(
1179 req, struct streams_xattr_pwrite_state);
1181 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1182 TALLOC_FREE(subreq);
1184 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1187 tevent_req_done(req);
1190 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1191 struct vfs_aio_state *vfs_aio_state)
1193 struct streams_xattr_pwrite_state *state = tevent_req_data(
1194 req, struct streams_xattr_pwrite_state);
1196 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1200 *vfs_aio_state = state->vfs_aio_state;
1201 return state->nwritten;
1204 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1205 struct files_struct *fsp,
1210 struct ea_struct ea;
1212 struct stream_io *sio =
1213 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1214 struct smb_filename *smb_fname_base = NULL;
1216 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1217 fsp_str_dbg(fsp), (double)offset));
1220 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1223 if (!streams_xattr_recheck(sio)) {
1227 /* Create an smb_filename with stream_name == NULL. */
1228 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1232 fsp->fsp_name->twrp,
1233 fsp->fsp_name->flags);
1234 if (smb_fname_base == NULL) {
1239 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1240 smb_fname_base, sio->xattr_name, &ea);
1241 if (!NT_STATUS_IS_OK(status)) {
1245 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1249 TALLOC_FREE(ea.value.data);
1254 /* Did we expand ? */
1255 if (ea.value.length < offset + 1) {
1256 memset(&tmp[ea.value.length], '\0',
1257 offset + 1 - ea.value.length);
1260 ea.value.data = tmp;
1261 ea.value.length = offset + 1;
1262 ea.value.data[offset] = 0;
1264 ret = SMB_VFS_SETXATTR(fsp->conn,
1267 ea.value.data, ea.value.length, 0);
1268 TALLOC_FREE(ea.value.data);
1277 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1278 struct files_struct *fsp,
1283 struct stream_io *sio =
1284 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1286 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1288 fsp_str_dbg(fsp), (double)offset, (double)len));
1291 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1294 if (!streams_xattr_recheck(sio)) {
1298 /* Let the pwrite code path handle it. */
1303 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1304 uid_t uid, gid_t gid)
1306 struct stream_io *sio =
1307 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1310 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1316 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1320 struct stream_io *sio =
1321 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1324 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1330 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1331 struct files_struct *fsp,
1336 struct stream_io *sio =
1337 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1340 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1347 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1348 struct files_struct *fsp,
1352 struct stream_io *sio =
1353 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1356 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1363 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1364 struct files_struct *fsp,
1367 struct stream_io *sio =
1368 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1371 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1378 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1379 struct files_struct *fsp,
1385 struct stream_io *sio =
1386 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1389 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1397 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1399 TALLOC_CTX *mem_ctx)
1401 struct stream_io *sio =
1402 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1405 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1408 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1409 handle, fsp->base_fsp->fsp_name,
1410 SMB_ACL_TYPE_ACCESS, mem_ctx);
1413 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1417 struct stream_io *sio =
1418 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1421 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1427 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1429 TALLOC_CTX *mem_ctx,
1430 char **blob_description,
1433 struct stream_io *sio =
1434 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1437 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1438 blob_description, blob);
1441 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1442 handle, fsp->base_fsp->fsp_name, mem_ctx,
1443 blob_description, blob);
1446 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1448 uint32_t security_info,
1449 TALLOC_CTX *mem_ctx,
1450 struct security_descriptor **ppdesc)
1452 struct stream_io *sio =
1453 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1456 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1460 return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1461 security_info, mem_ctx, ppdesc);
1464 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1466 uint32_t security_info_sent,
1467 const struct security_descriptor *psd)
1469 struct stream_io *sio =
1470 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1473 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1474 security_info_sent, psd);
1477 return NT_STATUS_OK;
1480 struct streams_xattr_fsync_state {
1482 struct vfs_aio_state vfs_aio_state;
1485 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1487 static struct tevent_req *streams_xattr_fsync_send(
1488 struct vfs_handle_struct *handle,
1489 TALLOC_CTX *mem_ctx,
1490 struct tevent_context *ev,
1491 struct files_struct *fsp)
1493 struct tevent_req *req = NULL;
1494 struct tevent_req *subreq = NULL;
1495 struct streams_xattr_fsync_state *state = NULL;
1496 struct stream_io *sio =
1497 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1499 req = tevent_req_create(mem_ctx, &state,
1500 struct streams_xattr_fsync_state);
1506 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1507 if (tevent_req_nomem(req, subreq)) {
1508 return tevent_req_post(req, ev);
1510 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1515 * There's no pathname based sync variant and we don't have access to
1516 * the basefile handle, so we can't do anything here.
1519 tevent_req_done(req);
1520 return tevent_req_post(req, ev);
1523 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1525 struct tevent_req *req = tevent_req_callback_data(
1526 subreq, struct tevent_req);
1527 struct streams_xattr_fsync_state *state = tevent_req_data(
1528 req, struct streams_xattr_fsync_state);
1530 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1531 TALLOC_FREE(subreq);
1532 if (state->ret != 0) {
1533 tevent_req_error(req, errno);
1537 tevent_req_done(req);
1540 static int streams_xattr_fsync_recv(struct tevent_req *req,
1541 struct vfs_aio_state *vfs_aio_state)
1543 struct streams_xattr_fsync_state *state = tevent_req_data(
1544 req, struct streams_xattr_fsync_state);
1546 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1550 *vfs_aio_state = state->vfs_aio_state;
1554 static bool streams_xattr_lock(vfs_handle_struct *handle,
1561 struct stream_io *sio =
1562 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1565 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1571 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1578 struct stream_io *sio =
1579 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1582 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1583 pcount, ptype, ppid);
1590 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1592 uint32_t share_access,
1593 uint32_t access_mask)
1595 struct stream_io *sio =
1596 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1599 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1600 share_access, access_mask);
1606 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1610 struct stream_io *sio =
1611 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1614 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1620 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1622 struct lock_struct *plock)
1624 struct stream_io *sio =
1625 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1628 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1634 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1635 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1636 .connect_fn = streams_xattr_connect,
1637 .open_fn = streams_xattr_open,
1638 .close_fn = streams_xattr_close,
1639 .stat_fn = streams_xattr_stat,
1640 .fstat_fn = streams_xattr_fstat,
1641 .lstat_fn = streams_xattr_lstat,
1642 .pread_fn = streams_xattr_pread,
1643 .pwrite_fn = streams_xattr_pwrite,
1644 .pread_send_fn = streams_xattr_pread_send,
1645 .pread_recv_fn = streams_xattr_pread_recv,
1646 .pwrite_send_fn = streams_xattr_pwrite_send,
1647 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1648 .unlinkat_fn = streams_xattr_unlinkat,
1649 .renameat_fn = streams_xattr_renameat,
1650 .ftruncate_fn = streams_xattr_ftruncate,
1651 .fallocate_fn = streams_xattr_fallocate,
1652 .streaminfo_fn = streams_xattr_streaminfo,
1654 .fsync_send_fn = streams_xattr_fsync_send,
1655 .fsync_recv_fn = streams_xattr_fsync_recv,
1657 .lock_fn = streams_xattr_lock,
1658 .getlock_fn = streams_xattr_getlock,
1659 .kernel_flock_fn = streams_xattr_kernel_flock,
1660 .linux_setlease_fn = streams_xattr_linux_setlease,
1661 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1663 .fchown_fn = streams_xattr_fchown,
1664 .fchmod_fn = streams_xattr_fchmod,
1666 .fgetxattr_fn = streams_xattr_fgetxattr,
1667 .flistxattr_fn = streams_xattr_flistxattr,
1668 .fremovexattr_fn = streams_xattr_fremovexattr,
1669 .fsetxattr_fn = streams_xattr_fsetxattr,
1671 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1672 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1673 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1675 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1676 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1680 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1682 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1683 &vfs_streams_xattr_fns);