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(),
222 fsp->fsp_name->flags);
223 if (smb_fname_base == NULL) {
228 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
229 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
231 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
233 *sbuf = smb_fname_base->st;
236 TALLOC_FREE(smb_fname_base);
240 sbuf->st_ex_size = get_xattr_size(handle->conn,
241 smb_fname_base, io->xattr_name);
242 if (sbuf->st_ex_size == -1) {
243 TALLOC_FREE(smb_fname_base);
244 SET_STAT_INVALID(*sbuf);
248 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
250 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
251 sbuf->st_ex_mode &= ~S_IFMT;
252 sbuf->st_ex_mode &= ~S_IFDIR;
253 sbuf->st_ex_mode |= S_IFREG;
254 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
256 TALLOC_FREE(smb_fname_base);
260 static int streams_xattr_stat(vfs_handle_struct *handle,
261 struct smb_filename *smb_fname)
265 char *xattr_name = NULL;
267 if (!is_ntfs_stream_smb_fname(smb_fname)) {
268 return SMB_VFS_NEXT_STAT(handle, smb_fname);
271 /* Note if lp_posix_paths() is true, we can never
272 * get here as is_ntfs_stream_smb_fname() is
273 * always false. So we never need worry about
274 * not following links here. */
276 /* If the default stream is requested, just stat the base file. */
277 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
278 return streams_xattr_stat_base(handle, smb_fname, true);
281 /* Populate the stat struct with info from the base file. */
282 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
286 /* Derive the xattr name to lookup. */
287 status = streams_xattr_get_name(handle, talloc_tos(),
288 smb_fname->stream_name, &xattr_name);
289 if (!NT_STATUS_IS_OK(status)) {
290 errno = map_errno_from_nt_status(status);
294 /* Augment the base file's stat information before returning. */
295 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
298 if (smb_fname->st.st_ex_size == -1) {
299 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_IFDIR;
308 smb_fname->st.st_ex_mode |= S_IFREG;
309 smb_fname->st.st_ex_blocks =
310 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
314 TALLOC_FREE(xattr_name);
318 static int streams_xattr_lstat(vfs_handle_struct *handle,
319 struct smb_filename *smb_fname)
323 char *xattr_name = NULL;
325 if (!is_ntfs_stream_smb_fname(smb_fname)) {
326 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
329 /* If the default stream is requested, just stat the base file. */
330 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
331 return streams_xattr_stat_base(handle, smb_fname, false);
334 /* Populate the stat struct with info from the base file. */
335 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
339 /* Derive the xattr name to lookup. */
340 status = streams_xattr_get_name(handle, talloc_tos(),
341 smb_fname->stream_name, &xattr_name);
342 if (!NT_STATUS_IS_OK(status)) {
343 errno = map_errno_from_nt_status(status);
347 /* Augment the base file's stat information before returning. */
348 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
351 if (smb_fname->st.st_ex_size == -1) {
352 SET_STAT_INVALID(smb_fname->st);
358 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
359 smb_fname->st.st_ex_mode &= ~S_IFMT;
360 smb_fname->st.st_ex_mode |= S_IFREG;
361 smb_fname->st.st_ex_blocks =
362 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
367 TALLOC_FREE(xattr_name);
371 static int streams_xattr_open(vfs_handle_struct *handle,
372 struct smb_filename *smb_fname,
373 files_struct *fsp, int flags, mode_t mode)
376 struct streams_xattr_config *config = NULL;
377 struct stream_io *sio = NULL;
379 char *xattr_name = NULL;
382 bool set_empty_xattr = false;
385 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
388 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
389 smb_fname_str_dbg(smb_fname), flags));
391 if (!is_ntfs_stream_smb_fname(smb_fname)) {
392 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
395 /* If the default stream is requested, just open the base file. */
396 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
397 char *tmp_stream_name;
399 tmp_stream_name = smb_fname->stream_name;
400 smb_fname->stream_name = NULL;
402 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
404 smb_fname->stream_name = tmp_stream_name;
409 status = streams_xattr_get_name(handle, talloc_tos(),
410 smb_fname->stream_name, &xattr_name);
411 if (!NT_STATUS_IS_OK(status)) {
412 errno = map_errno_from_nt_status(status);
416 status = get_ea_value(talloc_tos(), handle->conn, NULL,
417 smb_fname, xattr_name, &ea);
419 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
421 if (!NT_STATUS_IS_OK(status)) {
422 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
424 * The base file is not there. This is an error even if
425 * we got O_CREAT, the higher levels should have created
426 * the base file for us.
428 DBG_DEBUG("streams_xattr_open: base file %s not around, "
429 "returning ENOENT\n", smb_fname->base_name);
434 if (!(flags & O_CREAT)) {
439 set_empty_xattr = true;
442 if (flags & O_TRUNC) {
443 set_empty_xattr = true;
446 if (set_empty_xattr) {
448 * The attribute does not exist or needs to be truncated
452 * Darn, xattrs need at least 1 byte
456 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
457 xattr_name, smb_fname->base_name));
459 ret = SMB_VFS_SETXATTR(fsp->conn,
463 flags & O_EXCL ? XATTR_CREATE : 0);
470 * Return a valid fd, but ensure any attempt to use it returns an error
473 ret = pipe(pipe_fds);
480 fakefd = pipe_fds[0];
482 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
488 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
490 if (sio->xattr_name == NULL) {
496 * so->base needs to be a copy of fsp->fsp_name->base_name,
497 * making it identical to streams_xattr_recheck(). If the
498 * open is changing directories, fsp->fsp_name->base_name
499 * will be the full path from the share root, whilst
500 * smb_fname will be relative to the $cwd.
502 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
503 fsp->fsp_name->base_name);
504 if (sio->base == NULL) {
509 sio->fsp_name_ptr = fsp->fsp_name;
510 sio->handle = handle;
524 static int streams_xattr_close(vfs_handle_struct *handle,
532 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
533 smb_fname_str_dbg(fsp->fsp_name), fd);
535 if (!is_ntfs_stream_smb_fname(fsp->fsp_name)) {
536 return SMB_VFS_NEXT_CLOSE(handle, fsp);
539 if (is_ntfs_default_stream_smb_fname(fsp->fsp_name)) {
540 return SMB_VFS_NEXT_CLOSE(handle, fsp);
549 static int streams_xattr_unlink(vfs_handle_struct *handle,
550 const struct smb_filename *smb_fname)
554 char *xattr_name = NULL;
556 if (!is_ntfs_stream_smb_fname(smb_fname)) {
557 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
560 /* If the default stream is requested, just open the base file. */
561 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
562 struct smb_filename *smb_fname_base = NULL;
564 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
565 if (smb_fname_base == NULL) {
570 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
572 TALLOC_FREE(smb_fname_base);
576 status = streams_xattr_get_name(handle, talloc_tos(),
577 smb_fname->stream_name, &xattr_name);
578 if (!NT_STATUS_IS_OK(status)) {
579 errno = map_errno_from_nt_status(status);
583 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
585 if ((ret == -1) && (errno == ENOATTR)) {
593 TALLOC_FREE(xattr_name);
597 static int streams_xattr_rename(vfs_handle_struct *handle,
598 const struct smb_filename *smb_fname_src,
599 const struct smb_filename *smb_fname_dst)
603 char *src_xattr_name = NULL;
604 char *dst_xattr_name = NULL;
605 bool src_is_stream, dst_is_stream;
610 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
611 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
613 if (!src_is_stream && !dst_is_stream) {
614 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
618 /* For now don't allow renames from or to the default stream. */
619 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
620 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
625 /* Don't rename if the streams are identical. */
626 if (strcasecmp_m(smb_fname_src->stream_name,
627 smb_fname_dst->stream_name) == 0) {
631 /* Get the xattr names. */
632 status = streams_xattr_get_name(handle, talloc_tos(),
633 smb_fname_src->stream_name,
635 if (!NT_STATUS_IS_OK(status)) {
636 errno = map_errno_from_nt_status(status);
639 status = streams_xattr_get_name(handle, talloc_tos(),
640 smb_fname_dst->stream_name,
642 if (!NT_STATUS_IS_OK(status)) {
643 errno = map_errno_from_nt_status(status);
647 /* read the old stream */
648 status = get_ea_value(talloc_tos(), handle->conn, NULL,
649 smb_fname_src, src_xattr_name, &ea);
650 if (!NT_STATUS_IS_OK(status)) {
655 /* (over)write the new stream */
656 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
657 dst_xattr_name, ea.value.data, ea.value.length,
660 if (errno == ENOATTR) {
666 /* remove the old stream */
667 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
670 if (errno == ENOATTR) {
680 TALLOC_FREE(src_xattr_name);
681 TALLOC_FREE(dst_xattr_name);
685 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
687 const struct smb_filename *smb_fname,
688 bool (*fn)(struct ea_struct *ea,
695 struct streams_xattr_config *config;
697 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
698 return NT_STATUS_UNSUCCESSFUL);
700 status = get_ea_names_from_file(talloc_tos(),
706 if (!NT_STATUS_IS_OK(status)) {
710 for (i=0; i<num_names; i++) {
714 * We want to check with samba_private_attr_name()
715 * whether the xattr name is a private one,
716 * unfortunately it flags xattrs that begin with the
717 * default streams prefix as private.
719 * By only calling samba_private_attr_name() in case
720 * the xattr does NOT begin with the default prefix,
721 * we know that if it returns 'true' it definitely one
722 * of our internal xattr like "user.DOSATTRIB".
724 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
725 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
726 if (samba_private_attr_name(names[i])) {
731 if (strncmp(names[i], config->prefix,
732 config->prefix_len) != 0) {
736 status = get_ea_value(names,
742 if (!NT_STATUS_IS_OK(status)) {
743 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
745 smb_fname->base_name,
750 ea.name = talloc_asprintf(
751 ea.value.data, ":%s%s",
752 names[i] + config->prefix_len,
753 config->store_stream_type ? "" : ":$DATA");
754 if (ea.name == NULL) {
755 DEBUG(0, ("talloc failed\n"));
759 if (!fn(&ea, private_data)) {
760 TALLOC_FREE(ea.value.data);
764 TALLOC_FREE(ea.value.data);
771 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
772 struct stream_struct **streams,
773 const char *name, off_t size,
776 struct stream_struct *tmp;
778 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
784 tmp[*num_streams].name = talloc_strdup(tmp, name);
785 if (tmp[*num_streams].name == NULL) {
789 tmp[*num_streams].size = size;
790 tmp[*num_streams].alloc_size = alloc_size;
797 struct streaminfo_state {
799 vfs_handle_struct *handle;
800 unsigned int num_streams;
801 struct stream_struct *streams;
805 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
807 struct streaminfo_state *state =
808 (struct streaminfo_state *)private_data;
810 if (!add_one_stream(state->mem_ctx,
811 &state->num_streams, &state->streams,
812 ea->name, ea->value.length-1,
813 smb_roundup(state->handle->conn,
814 ea->value.length-1))) {
815 state->status = NT_STATUS_NO_MEMORY;
822 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
823 struct files_struct *fsp,
824 const struct smb_filename *smb_fname,
826 unsigned int *pnum_streams,
827 struct stream_struct **pstreams)
829 SMB_STRUCT_STAT sbuf;
832 struct streaminfo_state state;
834 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
836 return map_nt_error_from_unix(errno);
839 state.streams = *pstreams;
840 state.num_streams = *pnum_streams;
841 state.mem_ctx = mem_ctx;
842 state.handle = handle;
843 state.status = NT_STATUS_OK;
845 if (S_ISLNK(sbuf.st_ex_mode)) {
847 * Currently we do't have SMB_VFS_LLISTXATTR
848 * inside the VFS which means there's no way
849 * to cope with a symlink when lp_posix_pathnames().
850 * returns true. For now ignore links.
851 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
853 status = NT_STATUS_OK;
855 status = walk_xattr_streams(handle, fsp, smb_fname,
856 collect_one_stream, &state);
859 if (!NT_STATUS_IS_OK(status)) {
860 TALLOC_FREE(state.streams);
864 if (!NT_STATUS_IS_OK(state.status)) {
865 TALLOC_FREE(state.streams);
869 *pnum_streams = state.num_streams;
870 *pstreams = state.streams;
872 return SMB_VFS_NEXT_STREAMINFO(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);
938 struct smb_filename *smb_fname_base = NULL;
941 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
944 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
947 if (!streams_xattr_recheck(sio)) {
951 /* Create an smb_filename with stream_name == NULL. */
952 smb_fname_base = synthetic_smb_fname(talloc_tos(),
956 fsp->fsp_name->flags);
957 if (smb_fname_base == NULL) {
962 status = get_ea_value(talloc_tos(), handle->conn, NULL,
963 smb_fname_base, sio->xattr_name, &ea);
964 if (!NT_STATUS_IS_OK(status)) {
968 if ((offset + n) > ea.value.length-1) {
971 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
975 TALLOC_FREE(ea.value.data);
980 ea.value.length = offset + n + 1;
981 ea.value.data[offset+n] = 0;
984 memcpy(ea.value.data + offset, data, n);
986 ret = SMB_VFS_SETXATTR(fsp->conn,
989 ea.value.data, ea.value.length, 0);
990 TALLOC_FREE(ea.value.data);
999 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1000 files_struct *fsp, void *data,
1001 size_t n, off_t offset)
1003 struct stream_io *sio =
1004 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1005 struct ea_struct ea;
1007 size_t length, overlap;
1008 struct smb_filename *smb_fname_base = NULL;
1010 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1011 (int)offset, (int)n));
1014 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1017 if (!streams_xattr_recheck(sio)) {
1021 /* Create an smb_filename with stream_name == NULL. */
1022 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1026 fsp->fsp_name->flags);
1027 if (smb_fname_base == NULL) {
1032 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1033 smb_fname_base, sio->xattr_name, &ea);
1034 if (!NT_STATUS_IS_OK(status)) {
1038 length = ea.value.length-1;
1040 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1043 /* Attempt to read past EOF. */
1044 if (length <= offset) {
1048 overlap = (offset + n) > length ? (length - offset) : n;
1049 memcpy(data, ea.value.data + offset, overlap);
1051 TALLOC_FREE(ea.value.data);
1055 struct streams_xattr_pread_state {
1057 struct vfs_aio_state vfs_aio_state;
1060 static void streams_xattr_pread_done(struct tevent_req *subreq);
1062 static struct tevent_req *streams_xattr_pread_send(
1063 struct vfs_handle_struct *handle,
1064 TALLOC_CTX *mem_ctx,
1065 struct tevent_context *ev,
1066 struct files_struct *fsp,
1068 size_t n, off_t offset)
1070 struct tevent_req *req = NULL;
1071 struct tevent_req *subreq = NULL;
1072 struct streams_xattr_pread_state *state = NULL;
1073 struct stream_io *sio =
1074 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1076 req = tevent_req_create(mem_ctx, &state,
1077 struct streams_xattr_pread_state);
1083 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1085 if (tevent_req_nomem(req, subreq)) {
1086 return tevent_req_post(req, ev);
1088 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1092 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1093 if (state->nread != n) {
1094 if (state->nread != -1) {
1097 tevent_req_error(req, errno);
1098 return tevent_req_post(req, ev);
1101 tevent_req_done(req);
1102 return tevent_req_post(req, ev);
1105 static void streams_xattr_pread_done(struct tevent_req *subreq)
1107 struct tevent_req *req = tevent_req_callback_data(
1108 subreq, struct tevent_req);
1109 struct streams_xattr_pread_state *state = tevent_req_data(
1110 req, struct streams_xattr_pread_state);
1112 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1113 TALLOC_FREE(subreq);
1115 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1118 tevent_req_done(req);
1121 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1122 struct vfs_aio_state *vfs_aio_state)
1124 struct streams_xattr_pread_state *state = tevent_req_data(
1125 req, struct streams_xattr_pread_state);
1127 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1131 *vfs_aio_state = state->vfs_aio_state;
1132 return state->nread;
1135 struct streams_xattr_pwrite_state {
1137 struct vfs_aio_state vfs_aio_state;
1140 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1142 static struct tevent_req *streams_xattr_pwrite_send(
1143 struct vfs_handle_struct *handle,
1144 TALLOC_CTX *mem_ctx,
1145 struct tevent_context *ev,
1146 struct files_struct *fsp,
1148 size_t n, off_t offset)
1150 struct tevent_req *req = NULL;
1151 struct tevent_req *subreq = NULL;
1152 struct streams_xattr_pwrite_state *state = NULL;
1153 struct stream_io *sio =
1154 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1156 req = tevent_req_create(mem_ctx, &state,
1157 struct streams_xattr_pwrite_state);
1163 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1165 if (tevent_req_nomem(req, subreq)) {
1166 return tevent_req_post(req, ev);
1168 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1172 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1173 if (state->nwritten != n) {
1174 if (state->nwritten != -1) {
1177 tevent_req_error(req, errno);
1178 return tevent_req_post(req, ev);
1181 tevent_req_done(req);
1182 return tevent_req_post(req, ev);
1185 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1187 struct tevent_req *req = tevent_req_callback_data(
1188 subreq, struct tevent_req);
1189 struct streams_xattr_pwrite_state *state = tevent_req_data(
1190 req, struct streams_xattr_pwrite_state);
1192 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1193 TALLOC_FREE(subreq);
1195 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1198 tevent_req_done(req);
1201 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1202 struct vfs_aio_state *vfs_aio_state)
1204 struct streams_xattr_pwrite_state *state = tevent_req_data(
1205 req, struct streams_xattr_pwrite_state);
1207 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1211 *vfs_aio_state = state->vfs_aio_state;
1212 return state->nwritten;
1215 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1216 struct files_struct *fsp,
1221 struct ea_struct ea;
1223 struct stream_io *sio =
1224 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1225 struct smb_filename *smb_fname_base = NULL;
1227 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1228 fsp_str_dbg(fsp), (double)offset));
1231 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1234 if (!streams_xattr_recheck(sio)) {
1238 /* Create an smb_filename with stream_name == NULL. */
1239 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1243 fsp->fsp_name->flags);
1244 if (smb_fname_base == NULL) {
1249 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1250 smb_fname_base, sio->xattr_name, &ea);
1251 if (!NT_STATUS_IS_OK(status)) {
1255 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1259 TALLOC_FREE(ea.value.data);
1264 /* Did we expand ? */
1265 if (ea.value.length < offset + 1) {
1266 memset(&tmp[ea.value.length], '\0',
1267 offset + 1 - ea.value.length);
1270 ea.value.data = tmp;
1271 ea.value.length = offset + 1;
1272 ea.value.data[offset] = 0;
1274 ret = SMB_VFS_SETXATTR(fsp->conn,
1277 ea.value.data, ea.value.length, 0);
1278 TALLOC_FREE(ea.value.data);
1287 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1288 struct files_struct *fsp,
1293 struct stream_io *sio =
1294 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1296 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1298 fsp_str_dbg(fsp), (double)offset, (double)len));
1301 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1304 if (!streams_xattr_recheck(sio)) {
1308 /* Let the pwrite code path handle it. */
1313 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1314 uid_t uid, gid_t gid)
1316 struct stream_io *sio =
1317 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1320 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1326 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1330 struct stream_io *sio =
1331 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1334 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1340 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1341 struct files_struct *fsp,
1346 struct stream_io *sio =
1347 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1350 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1357 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1358 struct files_struct *fsp,
1362 struct stream_io *sio =
1363 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1366 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1373 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1374 struct files_struct *fsp,
1377 struct stream_io *sio =
1378 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1381 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1388 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1389 struct files_struct *fsp,
1395 struct stream_io *sio =
1396 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1399 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1407 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1409 TALLOC_CTX *mem_ctx)
1411 struct stream_io *sio =
1412 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1415 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1418 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1419 handle, fsp->base_fsp->fsp_name,
1420 SMB_ACL_TYPE_ACCESS, mem_ctx);
1423 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1427 struct stream_io *sio =
1428 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1431 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1437 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1439 TALLOC_CTX *mem_ctx,
1440 char **blob_description,
1443 struct stream_io *sio =
1444 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1447 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1448 blob_description, blob);
1451 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1452 handle, fsp->base_fsp->fsp_name, mem_ctx,
1453 blob_description, blob);
1456 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1458 uint32_t security_info,
1459 TALLOC_CTX *mem_ctx,
1460 struct security_descriptor **ppdesc)
1462 struct stream_io *sio =
1463 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1466 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1470 return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1471 security_info, mem_ctx, ppdesc);
1474 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1476 uint32_t security_info_sent,
1477 const struct security_descriptor *psd)
1479 struct stream_io *sio =
1480 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1483 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1484 security_info_sent, psd);
1487 return NT_STATUS_OK;
1490 struct streams_xattr_fsync_state {
1492 struct vfs_aio_state vfs_aio_state;
1495 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1497 static struct tevent_req *streams_xattr_fsync_send(
1498 struct vfs_handle_struct *handle,
1499 TALLOC_CTX *mem_ctx,
1500 struct tevent_context *ev,
1501 struct files_struct *fsp)
1503 struct tevent_req *req = NULL;
1504 struct tevent_req *subreq = NULL;
1505 struct streams_xattr_fsync_state *state = NULL;
1506 struct stream_io *sio =
1507 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1509 req = tevent_req_create(mem_ctx, &state,
1510 struct streams_xattr_fsync_state);
1516 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1517 if (tevent_req_nomem(req, subreq)) {
1518 return tevent_req_post(req, ev);
1520 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1525 * There's no pathname based sync variant and we don't have access to
1526 * the basefile handle, so we can't do anything here.
1529 tevent_req_done(req);
1530 return tevent_req_post(req, ev);
1533 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1535 struct tevent_req *req = tevent_req_callback_data(
1536 subreq, struct tevent_req);
1537 struct streams_xattr_fsync_state *state = tevent_req_data(
1538 req, struct streams_xattr_fsync_state);
1540 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1541 TALLOC_FREE(subreq);
1542 if (state->ret != 0) {
1543 tevent_req_error(req, errno);
1547 tevent_req_done(req);
1550 static int streams_xattr_fsync_recv(struct tevent_req *req,
1551 struct vfs_aio_state *vfs_aio_state)
1553 struct streams_xattr_fsync_state *state = tevent_req_data(
1554 req, struct streams_xattr_fsync_state);
1556 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1560 *vfs_aio_state = state->vfs_aio_state;
1564 static bool streams_xattr_lock(vfs_handle_struct *handle,
1571 struct stream_io *sio =
1572 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1575 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1581 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1588 struct stream_io *sio =
1589 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1592 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1593 pcount, ptype, ppid);
1600 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1602 uint32_t share_mode,
1603 uint32_t access_mask)
1605 struct stream_io *sio =
1606 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1609 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1610 share_mode, access_mask);
1616 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1620 struct stream_io *sio =
1621 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1624 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1630 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1632 struct lock_struct *plock)
1634 struct stream_io *sio =
1635 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1638 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1644 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1645 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1646 .connect_fn = streams_xattr_connect,
1647 .open_fn = streams_xattr_open,
1648 .close_fn = streams_xattr_close,
1649 .stat_fn = streams_xattr_stat,
1650 .fstat_fn = streams_xattr_fstat,
1651 .lstat_fn = streams_xattr_lstat,
1652 .pread_fn = streams_xattr_pread,
1653 .pwrite_fn = streams_xattr_pwrite,
1654 .pread_send_fn = streams_xattr_pread_send,
1655 .pread_recv_fn = streams_xattr_pread_recv,
1656 .pwrite_send_fn = streams_xattr_pwrite_send,
1657 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1658 .unlink_fn = streams_xattr_unlink,
1659 .rename_fn = streams_xattr_rename,
1660 .ftruncate_fn = streams_xattr_ftruncate,
1661 .fallocate_fn = streams_xattr_fallocate,
1662 .streaminfo_fn = streams_xattr_streaminfo,
1664 .fsync_send_fn = streams_xattr_fsync_send,
1665 .fsync_recv_fn = streams_xattr_fsync_recv,
1667 .lock_fn = streams_xattr_lock,
1668 .getlock_fn = streams_xattr_getlock,
1669 .kernel_flock_fn = streams_xattr_kernel_flock,
1670 .linux_setlease_fn = streams_xattr_linux_setlease,
1671 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1673 .fchown_fn = streams_xattr_fchown,
1674 .fchmod_fn = streams_xattr_fchmod,
1676 .fgetxattr_fn = streams_xattr_fgetxattr,
1677 .flistxattr_fn = streams_xattr_flistxattr,
1678 .fremovexattr_fn = streams_xattr_fremovexattr,
1679 .fsetxattr_fn = streams_xattr_fsetxattr,
1681 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1682 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1683 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1685 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1686 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1690 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1692 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1693 &vfs_streams_xattr_fns);