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_unlinkat(vfs_handle_struct *handle,
598 struct files_struct *dirfsp,
599 const struct smb_filename *smb_fname,
603 if (flags & AT_REMOVEDIR) {
604 ret = SMB_VFS_NEXT_UNLINKAT(handle,
609 ret = streams_xattr_unlink(handle,
615 static int streams_xattr_renameat(vfs_handle_struct *handle,
616 files_struct *srcfsp,
617 const struct smb_filename *smb_fname_src,
618 files_struct *dstfsp,
619 const struct smb_filename *smb_fname_dst)
623 char *src_xattr_name = NULL;
624 char *dst_xattr_name = NULL;
625 bool src_is_stream, dst_is_stream;
630 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
631 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
633 if (!src_is_stream && !dst_is_stream) {
634 return SMB_VFS_NEXT_RENAMEAT(handle,
641 /* For now don't allow renames from or to the default stream. */
642 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
643 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
648 /* Don't rename if the streams are identical. */
649 if (strcasecmp_m(smb_fname_src->stream_name,
650 smb_fname_dst->stream_name) == 0) {
654 /* Get the xattr names. */
655 status = streams_xattr_get_name(handle, talloc_tos(),
656 smb_fname_src->stream_name,
658 if (!NT_STATUS_IS_OK(status)) {
659 errno = map_errno_from_nt_status(status);
662 status = streams_xattr_get_name(handle, talloc_tos(),
663 smb_fname_dst->stream_name,
665 if (!NT_STATUS_IS_OK(status)) {
666 errno = map_errno_from_nt_status(status);
670 /* read the old stream */
671 status = get_ea_value(talloc_tos(), handle->conn, NULL,
672 smb_fname_src, src_xattr_name, &ea);
673 if (!NT_STATUS_IS_OK(status)) {
678 /* (over)write the new stream */
679 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
680 dst_xattr_name, ea.value.data, ea.value.length,
683 if (errno == ENOATTR) {
689 /* remove the old stream */
690 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
693 if (errno == ENOATTR) {
703 TALLOC_FREE(src_xattr_name);
704 TALLOC_FREE(dst_xattr_name);
708 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
710 const struct smb_filename *smb_fname,
711 bool (*fn)(struct ea_struct *ea,
718 struct streams_xattr_config *config;
720 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
721 return NT_STATUS_UNSUCCESSFUL);
723 status = get_ea_names_from_file(talloc_tos(),
729 if (!NT_STATUS_IS_OK(status)) {
733 for (i=0; i<num_names; i++) {
737 * We want to check with samba_private_attr_name()
738 * whether the xattr name is a private one,
739 * unfortunately it flags xattrs that begin with the
740 * default streams prefix as private.
742 * By only calling samba_private_attr_name() in case
743 * the xattr does NOT begin with the default prefix,
744 * we know that if it returns 'true' it definitely one
745 * of our internal xattr like "user.DOSATTRIB".
747 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
748 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
749 if (samba_private_attr_name(names[i])) {
754 if (strncmp(names[i], config->prefix,
755 config->prefix_len) != 0) {
759 status = get_ea_value(names,
765 if (!NT_STATUS_IS_OK(status)) {
766 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
768 smb_fname->base_name,
773 ea.name = talloc_asprintf(
774 ea.value.data, ":%s%s",
775 names[i] + config->prefix_len,
776 config->store_stream_type ? "" : ":$DATA");
777 if (ea.name == NULL) {
778 DEBUG(0, ("talloc failed\n"));
782 if (!fn(&ea, private_data)) {
783 TALLOC_FREE(ea.value.data);
787 TALLOC_FREE(ea.value.data);
794 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
795 struct stream_struct **streams,
796 const char *name, off_t size,
799 struct stream_struct *tmp;
801 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
807 tmp[*num_streams].name = talloc_strdup(tmp, name);
808 if (tmp[*num_streams].name == NULL) {
812 tmp[*num_streams].size = size;
813 tmp[*num_streams].alloc_size = alloc_size;
820 struct streaminfo_state {
822 vfs_handle_struct *handle;
823 unsigned int num_streams;
824 struct stream_struct *streams;
828 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
830 struct streaminfo_state *state =
831 (struct streaminfo_state *)private_data;
833 if (!add_one_stream(state->mem_ctx,
834 &state->num_streams, &state->streams,
835 ea->name, ea->value.length-1,
836 smb_roundup(state->handle->conn,
837 ea->value.length-1))) {
838 state->status = NT_STATUS_NO_MEMORY;
845 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
846 struct files_struct *fsp,
847 const struct smb_filename *smb_fname,
849 unsigned int *pnum_streams,
850 struct stream_struct **pstreams)
852 SMB_STRUCT_STAT sbuf;
855 struct streaminfo_state state;
857 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
859 return map_nt_error_from_unix(errno);
862 state.streams = *pstreams;
863 state.num_streams = *pnum_streams;
864 state.mem_ctx = mem_ctx;
865 state.handle = handle;
866 state.status = NT_STATUS_OK;
868 if (S_ISLNK(sbuf.st_ex_mode)) {
870 * Currently we do't have SMB_VFS_LLISTXATTR
871 * inside the VFS which means there's no way
872 * to cope with a symlink when lp_posix_pathnames().
873 * returns true. For now ignore links.
874 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
876 status = NT_STATUS_OK;
878 status = walk_xattr_streams(handle, fsp, smb_fname,
879 collect_one_stream, &state);
882 if (!NT_STATUS_IS_OK(status)) {
883 TALLOC_FREE(state.streams);
887 if (!NT_STATUS_IS_OK(state.status)) {
888 TALLOC_FREE(state.streams);
892 *pnum_streams = state.num_streams;
893 *pstreams = state.streams;
895 return SMB_VFS_NEXT_STREAMINFO(handle,
903 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
904 enum timestamp_set_resolution *p_ts_res)
906 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
909 static int streams_xattr_connect(vfs_handle_struct *handle,
910 const char *service, const char *user)
912 struct streams_xattr_config *config;
913 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
917 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
922 config = talloc_zero(handle->conn, struct streams_xattr_config);
923 if (config == NULL) {
924 DEBUG(1, ("talloc_zero() failed\n"));
929 prefix = lp_parm_const_string(SNUM(handle->conn),
930 "streams_xattr", "prefix",
932 config->prefix = talloc_strdup(config, prefix);
933 if (config->prefix == NULL) {
934 DEBUG(1, ("talloc_strdup() failed\n"));
938 config->prefix_len = strlen(config->prefix);
939 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
941 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
946 SMB_VFS_HANDLE_SET_DATA(handle, config,
947 NULL, struct stream_xattr_config,
953 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
954 files_struct *fsp, const void *data,
955 size_t n, off_t offset)
957 struct stream_io *sio =
958 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
961 struct smb_filename *smb_fname_base = NULL;
964 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
967 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
970 if (!streams_xattr_recheck(sio)) {
974 /* Create an smb_filename with stream_name == NULL. */
975 smb_fname_base = synthetic_smb_fname(talloc_tos(),
979 fsp->fsp_name->flags);
980 if (smb_fname_base == NULL) {
985 status = get_ea_value(talloc_tos(), handle->conn, NULL,
986 smb_fname_base, sio->xattr_name, &ea);
987 if (!NT_STATUS_IS_OK(status)) {
991 if ((offset + n) > ea.value.length-1) {
994 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
998 TALLOC_FREE(ea.value.data);
1002 ea.value.data = tmp;
1003 ea.value.length = offset + n + 1;
1004 ea.value.data[offset+n] = 0;
1007 memcpy(ea.value.data + offset, data, n);
1009 ret = SMB_VFS_SETXATTR(fsp->conn,
1012 ea.value.data, ea.value.length, 0);
1013 TALLOC_FREE(ea.value.data);
1022 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1023 files_struct *fsp, void *data,
1024 size_t n, off_t offset)
1026 struct stream_io *sio =
1027 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1028 struct ea_struct ea;
1030 size_t length, overlap;
1031 struct smb_filename *smb_fname_base = NULL;
1033 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1034 (int)offset, (int)n));
1037 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1040 if (!streams_xattr_recheck(sio)) {
1044 /* Create an smb_filename with stream_name == NULL. */
1045 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1049 fsp->fsp_name->flags);
1050 if (smb_fname_base == NULL) {
1055 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1056 smb_fname_base, sio->xattr_name, &ea);
1057 if (!NT_STATUS_IS_OK(status)) {
1061 length = ea.value.length-1;
1063 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1066 /* Attempt to read past EOF. */
1067 if (length <= offset) {
1071 overlap = (offset + n) > length ? (length - offset) : n;
1072 memcpy(data, ea.value.data + offset, overlap);
1074 TALLOC_FREE(ea.value.data);
1078 struct streams_xattr_pread_state {
1080 struct vfs_aio_state vfs_aio_state;
1083 static void streams_xattr_pread_done(struct tevent_req *subreq);
1085 static struct tevent_req *streams_xattr_pread_send(
1086 struct vfs_handle_struct *handle,
1087 TALLOC_CTX *mem_ctx,
1088 struct tevent_context *ev,
1089 struct files_struct *fsp,
1091 size_t n, off_t offset)
1093 struct tevent_req *req = NULL;
1094 struct tevent_req *subreq = NULL;
1095 struct streams_xattr_pread_state *state = NULL;
1096 struct stream_io *sio =
1097 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1099 req = tevent_req_create(mem_ctx, &state,
1100 struct streams_xattr_pread_state);
1106 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1108 if (tevent_req_nomem(req, subreq)) {
1109 return tevent_req_post(req, ev);
1111 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1115 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1116 if (state->nread != n) {
1117 if (state->nread != -1) {
1120 tevent_req_error(req, errno);
1121 return tevent_req_post(req, ev);
1124 tevent_req_done(req);
1125 return tevent_req_post(req, ev);
1128 static void streams_xattr_pread_done(struct tevent_req *subreq)
1130 struct tevent_req *req = tevent_req_callback_data(
1131 subreq, struct tevent_req);
1132 struct streams_xattr_pread_state *state = tevent_req_data(
1133 req, struct streams_xattr_pread_state);
1135 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1136 TALLOC_FREE(subreq);
1138 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1141 tevent_req_done(req);
1144 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1145 struct vfs_aio_state *vfs_aio_state)
1147 struct streams_xattr_pread_state *state = tevent_req_data(
1148 req, struct streams_xattr_pread_state);
1150 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1154 *vfs_aio_state = state->vfs_aio_state;
1155 return state->nread;
1158 struct streams_xattr_pwrite_state {
1160 struct vfs_aio_state vfs_aio_state;
1163 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1165 static struct tevent_req *streams_xattr_pwrite_send(
1166 struct vfs_handle_struct *handle,
1167 TALLOC_CTX *mem_ctx,
1168 struct tevent_context *ev,
1169 struct files_struct *fsp,
1171 size_t n, off_t offset)
1173 struct tevent_req *req = NULL;
1174 struct tevent_req *subreq = NULL;
1175 struct streams_xattr_pwrite_state *state = NULL;
1176 struct stream_io *sio =
1177 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1179 req = tevent_req_create(mem_ctx, &state,
1180 struct streams_xattr_pwrite_state);
1186 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1188 if (tevent_req_nomem(req, subreq)) {
1189 return tevent_req_post(req, ev);
1191 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1195 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1196 if (state->nwritten != n) {
1197 if (state->nwritten != -1) {
1200 tevent_req_error(req, errno);
1201 return tevent_req_post(req, ev);
1204 tevent_req_done(req);
1205 return tevent_req_post(req, ev);
1208 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1210 struct tevent_req *req = tevent_req_callback_data(
1211 subreq, struct tevent_req);
1212 struct streams_xattr_pwrite_state *state = tevent_req_data(
1213 req, struct streams_xattr_pwrite_state);
1215 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1216 TALLOC_FREE(subreq);
1218 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1221 tevent_req_done(req);
1224 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1225 struct vfs_aio_state *vfs_aio_state)
1227 struct streams_xattr_pwrite_state *state = tevent_req_data(
1228 req, struct streams_xattr_pwrite_state);
1230 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1234 *vfs_aio_state = state->vfs_aio_state;
1235 return state->nwritten;
1238 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1239 struct files_struct *fsp,
1244 struct ea_struct ea;
1246 struct stream_io *sio =
1247 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1248 struct smb_filename *smb_fname_base = NULL;
1250 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1251 fsp_str_dbg(fsp), (double)offset));
1254 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1257 if (!streams_xattr_recheck(sio)) {
1261 /* Create an smb_filename with stream_name == NULL. */
1262 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1266 fsp->fsp_name->flags);
1267 if (smb_fname_base == NULL) {
1272 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1273 smb_fname_base, sio->xattr_name, &ea);
1274 if (!NT_STATUS_IS_OK(status)) {
1278 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1282 TALLOC_FREE(ea.value.data);
1287 /* Did we expand ? */
1288 if (ea.value.length < offset + 1) {
1289 memset(&tmp[ea.value.length], '\0',
1290 offset + 1 - ea.value.length);
1293 ea.value.data = tmp;
1294 ea.value.length = offset + 1;
1295 ea.value.data[offset] = 0;
1297 ret = SMB_VFS_SETXATTR(fsp->conn,
1300 ea.value.data, ea.value.length, 0);
1301 TALLOC_FREE(ea.value.data);
1310 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1311 struct files_struct *fsp,
1316 struct stream_io *sio =
1317 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1319 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1321 fsp_str_dbg(fsp), (double)offset, (double)len));
1324 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1327 if (!streams_xattr_recheck(sio)) {
1331 /* Let the pwrite code path handle it. */
1336 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1337 uid_t uid, gid_t gid)
1339 struct stream_io *sio =
1340 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1343 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1349 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1353 struct stream_io *sio =
1354 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1357 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1363 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1364 struct files_struct *fsp,
1369 struct stream_io *sio =
1370 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1373 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1380 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1381 struct files_struct *fsp,
1385 struct stream_io *sio =
1386 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1389 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1396 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1397 struct files_struct *fsp,
1400 struct stream_io *sio =
1401 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1404 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1411 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1412 struct files_struct *fsp,
1418 struct stream_io *sio =
1419 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1422 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1430 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1432 TALLOC_CTX *mem_ctx)
1434 struct stream_io *sio =
1435 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1438 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1441 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1442 handle, fsp->base_fsp->fsp_name,
1443 SMB_ACL_TYPE_ACCESS, mem_ctx);
1446 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1450 struct stream_io *sio =
1451 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1454 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1460 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1462 TALLOC_CTX *mem_ctx,
1463 char **blob_description,
1466 struct stream_io *sio =
1467 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1470 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1471 blob_description, blob);
1474 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1475 handle, fsp->base_fsp->fsp_name, mem_ctx,
1476 blob_description, blob);
1479 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1481 uint32_t security_info,
1482 TALLOC_CTX *mem_ctx,
1483 struct security_descriptor **ppdesc)
1485 struct stream_io *sio =
1486 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1489 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1493 return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1494 security_info, mem_ctx, ppdesc);
1497 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1499 uint32_t security_info_sent,
1500 const struct security_descriptor *psd)
1502 struct stream_io *sio =
1503 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1506 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1507 security_info_sent, psd);
1510 return NT_STATUS_OK;
1513 struct streams_xattr_fsync_state {
1515 struct vfs_aio_state vfs_aio_state;
1518 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1520 static struct tevent_req *streams_xattr_fsync_send(
1521 struct vfs_handle_struct *handle,
1522 TALLOC_CTX *mem_ctx,
1523 struct tevent_context *ev,
1524 struct files_struct *fsp)
1526 struct tevent_req *req = NULL;
1527 struct tevent_req *subreq = NULL;
1528 struct streams_xattr_fsync_state *state = NULL;
1529 struct stream_io *sio =
1530 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1532 req = tevent_req_create(mem_ctx, &state,
1533 struct streams_xattr_fsync_state);
1539 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1540 if (tevent_req_nomem(req, subreq)) {
1541 return tevent_req_post(req, ev);
1543 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1548 * There's no pathname based sync variant and we don't have access to
1549 * the basefile handle, so we can't do anything here.
1552 tevent_req_done(req);
1553 return tevent_req_post(req, ev);
1556 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1558 struct tevent_req *req = tevent_req_callback_data(
1559 subreq, struct tevent_req);
1560 struct streams_xattr_fsync_state *state = tevent_req_data(
1561 req, struct streams_xattr_fsync_state);
1563 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1564 TALLOC_FREE(subreq);
1565 if (state->ret != 0) {
1566 tevent_req_error(req, errno);
1570 tevent_req_done(req);
1573 static int streams_xattr_fsync_recv(struct tevent_req *req,
1574 struct vfs_aio_state *vfs_aio_state)
1576 struct streams_xattr_fsync_state *state = tevent_req_data(
1577 req, struct streams_xattr_fsync_state);
1579 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1583 *vfs_aio_state = state->vfs_aio_state;
1587 static bool streams_xattr_lock(vfs_handle_struct *handle,
1594 struct stream_io *sio =
1595 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1598 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1604 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1611 struct stream_io *sio =
1612 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1615 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1616 pcount, ptype, ppid);
1623 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1625 uint32_t share_mode,
1626 uint32_t access_mask)
1628 struct stream_io *sio =
1629 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1632 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1633 share_mode, access_mask);
1639 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1643 struct stream_io *sio =
1644 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1647 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1653 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1655 struct lock_struct *plock)
1657 struct stream_io *sio =
1658 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1661 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1667 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1668 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1669 .connect_fn = streams_xattr_connect,
1670 .open_fn = streams_xattr_open,
1671 .close_fn = streams_xattr_close,
1672 .stat_fn = streams_xattr_stat,
1673 .fstat_fn = streams_xattr_fstat,
1674 .lstat_fn = streams_xattr_lstat,
1675 .pread_fn = streams_xattr_pread,
1676 .pwrite_fn = streams_xattr_pwrite,
1677 .pread_send_fn = streams_xattr_pread_send,
1678 .pread_recv_fn = streams_xattr_pread_recv,
1679 .pwrite_send_fn = streams_xattr_pwrite_send,
1680 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1681 .unlink_fn = streams_xattr_unlink,
1682 .unlinkat_fn = streams_xattr_unlinkat,
1683 .renameat_fn = streams_xattr_renameat,
1684 .ftruncate_fn = streams_xattr_ftruncate,
1685 .fallocate_fn = streams_xattr_fallocate,
1686 .streaminfo_fn = streams_xattr_streaminfo,
1688 .fsync_send_fn = streams_xattr_fsync_send,
1689 .fsync_recv_fn = streams_xattr_fsync_recv,
1691 .lock_fn = streams_xattr_lock,
1692 .getlock_fn = streams_xattr_getlock,
1693 .kernel_flock_fn = streams_xattr_kernel_flock,
1694 .linux_setlease_fn = streams_xattr_linux_setlease,
1695 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1697 .fchown_fn = streams_xattr_fchown,
1698 .fchmod_fn = streams_xattr_fchmod,
1700 .fgetxattr_fn = streams_xattr_fgetxattr,
1701 .flistxattr_fn = streams_xattr_flistxattr,
1702 .fremovexattr_fn = streams_xattr_fremovexattr,
1703 .fsetxattr_fn = streams_xattr_fsetxattr,
1705 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1706 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1707 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1709 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1710 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1714 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1716 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1717 &vfs_streams_xattr_fns);