vfs: remove SMB_VFS_OPEN()
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blob2e4105fec496ee76d79e8135f694fb20f28b6f08
1 /*
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/>.
24 #include "includes.h"
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"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_VFS
34 struct streams_xattr_config {
35 const char *prefix;
36 size_t prefix_len;
37 bool store_stream_type;
40 struct stream_io {
41 char *base;
42 char *xattr_name;
43 void *fsp_name_ptr;
44 files_struct *fsp;
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)
52 NTSTATUS status;
53 struct ea_struct ea;
54 ssize_t result;
56 status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
57 xattr_name, &ea);
59 if (!NT_STATUS_IS_OK(status)) {
60 return -1;
63 result = ea.value.length-1;
64 TALLOC_FREE(ea.value.data);
65 return result;
68 /**
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,
73 TALLOC_CTX *ctx,
74 const char *stream_name,
75 char **xattr_name)
77 char *sname;
78 char *stype;
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);
85 if (sname == NULL) {
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
98 * type suffix.
100 * In check_path_syntax() we've already ensured the streamname
101 * we got from the client is valid.
103 stype = strrchr_m(sname, ':');
105 if (stype) {
107 * We only support one stream type: "$DATA"
109 if (strcasecmp_m(stype, ":$DATA") != 0) {
110 talloc_free(sname);
111 return NT_STATUS_INVALID_PARAMETER;
114 /* Split name and type */
115 stype[0] = '\0';
118 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
119 config->prefix,
120 sname,
121 config->store_stream_type ? ":$DATA" : "");
122 if (*xattr_name == NULL) {
123 talloc_free(sname);
124 return NT_STATUS_NO_MEMORY;
127 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
128 stream_name));
130 talloc_free(sname);
131 return NT_STATUS_OK;
134 static bool streams_xattr_recheck(struct stream_io *sio)
136 NTSTATUS status;
137 char *xattr_name = NULL;
139 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
140 return true;
143 if (sio->fsp->fsp_name->stream_name == NULL) {
144 /* how can this happen */
145 errno = EINVAL;
146 return false;
149 status = streams_xattr_get_name(sio->handle, talloc_tos(),
150 sio->fsp->fsp_name->stream_name,
151 &xattr_name);
152 if (!NT_STATUS_IS_OK(status)) {
153 return false;
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),
159 xattr_name);
160 if (sio->xattr_name == NULL) {
161 DBG_DEBUG("sio->xattr_name==NULL\n");
162 return false;
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");
170 return false;
173 sio->fsp_name_ptr = sio->fsp->fsp_name;
175 return true;
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,
183 bool follow_links)
185 char *tmp_stream_name;
186 int result;
188 tmp_stream_name = smb_fname->stream_name;
189 smb_fname->stream_name = NULL;
190 if (follow_links) {
191 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
192 } else {
193 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
195 smb_fname->stream_name = tmp_stream_name;
196 return result;
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;
203 int ret = -1;
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)) {
214 return -1;
217 /* Create an smb_filename with stream_name == NULL. */
218 smb_fname_base = synthetic_smb_fname(talloc_tos(),
219 io->base,
220 NULL,
221 NULL,
222 fsp->fsp_name->twrp,
223 fsp->fsp_name->flags);
224 if (smb_fname_base == NULL) {
225 errno = ENOMEM;
226 return -1;
229 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
230 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
231 } else {
232 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
234 *sbuf = smb_fname_base->st;
236 if (ret == -1) {
237 TALLOC_FREE(smb_fname_base);
238 return -1;
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);
246 return -1;
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);
258 return 0;
261 static int streams_xattr_stat(vfs_handle_struct *handle,
262 struct smb_filename *smb_fname)
264 NTSTATUS status;
265 int result = -1;
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) {
279 return -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);
287 return -1;
290 /* Augment the base file's stat information before returning. */
291 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
292 smb_fname,
293 xattr_name);
294 if (smb_fname->st.st_ex_size == -1) {
295 SET_STAT_INVALID(smb_fname->st);
296 errno = ENOENT;
297 result = -1;
298 goto fail;
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;
308 result = 0;
309 fail:
310 TALLOC_FREE(xattr_name);
311 return result;
314 static int streams_xattr_lstat(vfs_handle_struct *handle,
315 struct smb_filename *smb_fname)
317 NTSTATUS status;
318 int result = -1;
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) {
327 return -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);
335 return -1;
338 /* Augment the base file's stat information before returning. */
339 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
340 smb_fname,
341 xattr_name);
342 if (smb_fname->st.st_ex_size == -1) {
343 SET_STAT_INVALID(smb_fname->st);
344 errno = ENOENT;
345 result = -1;
346 goto fail;
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;
355 result = 0;
357 fail:
358 TALLOC_FREE(xattr_name);
359 return result;
362 static int streams_xattr_openat(struct vfs_handle_struct *handle,
363 const struct files_struct *dirfsp,
364 const struct smb_filename *smb_fname,
365 files_struct *fsp,
366 int flags,
367 mode_t mode)
369 NTSTATUS status;
370 struct streams_xattr_config *config = NULL;
371 struct stream_io *sio = NULL;
372 struct ea_struct ea;
373 char *xattr_name = NULL;
374 int pipe_fds[2];
375 int fakefd = -1;
376 bool set_empty_xattr = false;
377 int ret;
380 * For now assert this, so the below SMB_VFS_SETXATTR() works.
382 SMB_ASSERT(dirfsp->fh->fd == AT_FDCWD);
384 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
385 return -1);
387 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
388 smb_fname_str_dbg(smb_fname), flags));
390 if (!is_named_stream(smb_fname)) {
391 return SMB_VFS_NEXT_OPENAT(handle,
392 dirfsp,
393 smb_fname,
394 fsp,
395 flags,
396 mode);
399 status = streams_xattr_get_name(handle, talloc_tos(),
400 smb_fname->stream_name, &xattr_name);
401 if (!NT_STATUS_IS_OK(status)) {
402 errno = map_errno_from_nt_status(status);
403 goto fail;
406 status = get_ea_value(talloc_tos(), handle->conn, NULL,
407 smb_fname, xattr_name, &ea);
409 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
411 if (!NT_STATUS_IS_OK(status)) {
412 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
414 * The base file is not there. This is an error even if
415 * we got O_CREAT, the higher levels should have created
416 * the base file for us.
418 DBG_DEBUG("streams_xattr_open: base file %s not around, "
419 "returning ENOENT\n", smb_fname->base_name);
420 errno = ENOENT;
421 goto fail;
424 if (!(flags & O_CREAT)) {
425 errno = ENOATTR;
426 goto fail;
429 set_empty_xattr = true;
432 if (flags & O_TRUNC) {
433 set_empty_xattr = true;
436 if (set_empty_xattr) {
438 * The attribute does not exist or needs to be truncated
442 * Darn, xattrs need at least 1 byte
444 char null = '\0';
446 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
447 xattr_name, smb_fname->base_name));
449 ret = SMB_VFS_SETXATTR(fsp->conn,
450 smb_fname,
451 xattr_name,
452 &null, sizeof(null),
453 flags & O_EXCL ? XATTR_CREATE : 0);
454 if (ret != 0) {
455 goto fail;
460 * Return a valid fd, but ensure any attempt to use it returns an error
461 * (EPIPE).
463 ret = pipe(pipe_fds);
464 if (ret != 0) {
465 goto fail;
468 close(pipe_fds[1]);
469 pipe_fds[1] = -1;
470 fakefd = pipe_fds[0];
472 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
473 if (sio == NULL) {
474 errno = ENOMEM;
475 goto fail;
478 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
479 xattr_name);
480 if (sio->xattr_name == NULL) {
481 errno = ENOMEM;
482 goto fail;
486 * so->base needs to be a copy of fsp->fsp_name->base_name,
487 * making it identical to streams_xattr_recheck(). If the
488 * open is changing directories, fsp->fsp_name->base_name
489 * will be the full path from the share root, whilst
490 * smb_fname will be relative to the $cwd.
492 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
493 fsp->fsp_name->base_name);
494 if (sio->base == NULL) {
495 errno = ENOMEM;
496 goto fail;
499 sio->fsp_name_ptr = fsp->fsp_name;
500 sio->handle = handle;
501 sio->fsp = fsp;
503 return fakefd;
505 fail:
506 if (fakefd >= 0) {
507 close(fakefd);
508 fakefd = -1;
511 return -1;
514 static int streams_xattr_close(vfs_handle_struct *handle,
515 files_struct *fsp)
517 int ret;
518 int fd;
520 fd = fsp->fh->fd;
522 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
523 smb_fname_str_dbg(fsp->fsp_name), fd);
525 if (!is_named_stream(fsp->fsp_name)) {
526 return SMB_VFS_NEXT_CLOSE(handle, fsp);
529 ret = close(fd);
530 fsp->fh->fd = -1;
532 return ret;
535 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
536 struct files_struct *dirfsp,
537 const struct smb_filename *smb_fname,
538 int flags)
540 NTSTATUS status;
541 int ret = -1;
542 char *xattr_name = NULL;
544 if (!is_named_stream(smb_fname)) {
545 return SMB_VFS_NEXT_UNLINKAT(handle,
546 dirfsp,
547 smb_fname,
548 flags);
551 status = streams_xattr_get_name(handle, talloc_tos(),
552 smb_fname->stream_name, &xattr_name);
553 if (!NT_STATUS_IS_OK(status)) {
554 errno = map_errno_from_nt_status(status);
555 goto fail;
558 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
560 if ((ret == -1) && (errno == ENOATTR)) {
561 errno = ENOENT;
562 goto fail;
565 ret = 0;
567 fail:
568 TALLOC_FREE(xattr_name);
569 return ret;
572 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
573 struct files_struct *dirfsp,
574 const struct smb_filename *smb_fname,
575 int flags)
577 int ret;
578 if (flags & AT_REMOVEDIR) {
579 ret = SMB_VFS_NEXT_UNLINKAT(handle,
580 dirfsp,
581 smb_fname,
582 flags);
583 } else {
584 ret = streams_xattr_unlink_internal(handle,
585 dirfsp,
586 smb_fname,
587 flags);
589 return ret;
592 static int streams_xattr_renameat(vfs_handle_struct *handle,
593 files_struct *srcfsp,
594 const struct smb_filename *smb_fname_src,
595 files_struct *dstfsp,
596 const struct smb_filename *smb_fname_dst)
598 NTSTATUS status;
599 int ret = -1;
600 char *src_xattr_name = NULL;
601 char *dst_xattr_name = NULL;
602 bool src_is_stream, dst_is_stream;
603 ssize_t oret;
604 ssize_t nret;
605 struct ea_struct ea;
607 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
608 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
610 if (!src_is_stream && !dst_is_stream) {
611 return SMB_VFS_NEXT_RENAMEAT(handle,
612 srcfsp,
613 smb_fname_src,
614 dstfsp,
615 smb_fname_dst);
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)) {
621 errno = ENOSYS;
622 goto done;
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) {
628 goto done;
631 /* Get the xattr names. */
632 status = streams_xattr_get_name(handle, talloc_tos(),
633 smb_fname_src->stream_name,
634 &src_xattr_name);
635 if (!NT_STATUS_IS_OK(status)) {
636 errno = map_errno_from_nt_status(status);
637 goto fail;
639 status = streams_xattr_get_name(handle, talloc_tos(),
640 smb_fname_dst->stream_name,
641 &dst_xattr_name);
642 if (!NT_STATUS_IS_OK(status)) {
643 errno = map_errno_from_nt_status(status);
644 goto fail;
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)) {
651 errno = ENOENT;
652 goto fail;
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,
659 if (nret < 0) {
660 if (errno == ENOATTR) {
661 errno = ENOENT;
663 goto fail;
666 /* remove the old stream */
667 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
668 src_xattr_name);
669 if (oret < 0) {
670 if (errno == ENOATTR) {
671 errno = ENOENT;
673 goto fail;
676 done:
677 errno = 0;
678 ret = 0;
679 fail:
680 TALLOC_FREE(src_xattr_name);
681 TALLOC_FREE(dst_xattr_name);
682 return ret;
685 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
686 files_struct *fsp,
687 const struct smb_filename *smb_fname,
688 bool (*fn)(struct ea_struct *ea,
689 void *private_data),
690 void *private_data)
692 NTSTATUS status;
693 char **names;
694 size_t i, num_names;
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(),
701 handle->conn,
702 fsp,
703 smb_fname,
704 &names,
705 &num_names);
706 if (!NT_STATUS_IS_OK(status)) {
707 return status;
710 for (i=0; i<num_names; i++) {
711 struct ea_struct ea;
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])) {
727 continue;
731 if (strncmp(names[i], config->prefix,
732 config->prefix_len) != 0) {
733 continue;
736 status = get_ea_value(names,
737 handle->conn,
738 NULL,
739 smb_fname,
740 names[i],
741 &ea);
742 if (!NT_STATUS_IS_OK(status)) {
743 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
744 names[i],
745 smb_fname->base_name,
746 nt_errstr(status)));
747 continue;
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"));
756 continue;
759 if (!fn(&ea, private_data)) {
760 TALLOC_FREE(ea.value.data);
761 return NT_STATUS_OK;
764 TALLOC_FREE(ea.value.data);
767 TALLOC_FREE(names);
768 return NT_STATUS_OK;
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,
774 off_t alloc_size)
776 struct stream_struct *tmp;
778 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
779 (*num_streams)+1);
780 if (tmp == NULL) {
781 return false;
784 tmp[*num_streams].name = talloc_strdup(tmp, name);
785 if (tmp[*num_streams].name == NULL) {
786 return false;
789 tmp[*num_streams].size = size;
790 tmp[*num_streams].alloc_size = alloc_size;
792 *streams = tmp;
793 *num_streams += 1;
794 return true;
797 struct streaminfo_state {
798 TALLOC_CTX *mem_ctx;
799 vfs_handle_struct *handle;
800 unsigned int num_streams;
801 struct stream_struct *streams;
802 NTSTATUS status;
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;
816 return false;
819 return true;
822 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
823 struct files_struct *fsp,
824 const struct smb_filename *smb_fname,
825 TALLOC_CTX *mem_ctx,
826 unsigned int *pnum_streams,
827 struct stream_struct **pstreams)
829 SMB_STRUCT_STAT sbuf;
830 int ret;
831 NTSTATUS status;
832 struct streaminfo_state state;
834 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
835 if (ret == -1) {
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;
854 } else {
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);
861 return status;
864 if (!NT_STATUS_IS_OK(state.status)) {
865 TALLOC_FREE(state.streams);
866 return state.status;
869 *pnum_streams = state.num_streams;
870 *pstreams = state.streams;
872 return SMB_VFS_NEXT_STREAMINFO(handle,
873 fsp,
874 smb_fname,
875 mem_ctx,
876 pnum_streams,
877 pstreams);
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;
891 const char *prefix;
892 int rc;
894 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
895 if (rc != 0) {
896 return rc;
899 config = talloc_zero(handle->conn, struct streams_xattr_config);
900 if (config == NULL) {
901 DEBUG(1, ("talloc_zero() failed\n"));
902 errno = ENOMEM;
903 return -1;
906 prefix = lp_parm_const_string(SNUM(handle->conn),
907 "streams_xattr", "prefix",
908 default_prefix);
909 config->prefix = talloc_strdup(config, prefix);
910 if (config->prefix == NULL) {
911 DEBUG(1, ("talloc_strdup() failed\n"));
912 errno = ENOMEM;
913 return -1;
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),
919 "streams_xattr",
920 "store_stream_type",
921 true);
923 SMB_VFS_HANDLE_SET_DATA(handle, config,
924 NULL, struct stream_xattr_config,
925 return -1);
927 return 0;
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);
936 struct ea_struct ea;
937 NTSTATUS status;
938 struct smb_filename *smb_fname_base = NULL;
939 int ret;
941 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
943 if (sio == NULL) {
944 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
947 if (!streams_xattr_recheck(sio)) {
948 return -1;
951 /* Create an smb_filename with stream_name == NULL. */
952 smb_fname_base = synthetic_smb_fname(talloc_tos(),
953 sio->base,
954 NULL,
955 NULL,
956 fsp->fsp_name->twrp,
957 fsp->fsp_name->flags);
958 if (smb_fname_base == NULL) {
959 errno = ENOMEM;
960 return -1;
963 status = get_ea_value(talloc_tos(), handle->conn, NULL,
964 smb_fname_base, sio->xattr_name, &ea);
965 if (!NT_STATUS_IS_OK(status)) {
966 return -1;
969 if ((offset + n) > ea.value.length-1) {
970 uint8_t *tmp;
972 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
973 offset + n + 1);
975 if (tmp == NULL) {
976 TALLOC_FREE(ea.value.data);
977 errno = ENOMEM;
978 return -1;
980 ea.value.data = tmp;
981 ea.value.length = offset + n + 1;
982 ea.value.data[offset+n] = 0;
985 memcpy(ea.value.data + offset, data, n);
987 ret = SMB_VFS_SETXATTR(fsp->conn,
988 fsp->fsp_name,
989 sio->xattr_name,
990 ea.value.data, ea.value.length, 0);
991 TALLOC_FREE(ea.value.data);
993 if (ret == -1) {
994 return -1;
997 return n;
1000 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1001 files_struct *fsp, void *data,
1002 size_t n, off_t offset)
1004 struct stream_io *sio =
1005 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1006 struct ea_struct ea;
1007 NTSTATUS status;
1008 size_t length, overlap;
1009 struct smb_filename *smb_fname_base = NULL;
1011 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1012 (int)offset, (int)n));
1014 if (sio == NULL) {
1015 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1018 if (!streams_xattr_recheck(sio)) {
1019 return -1;
1022 /* Create an smb_filename with stream_name == NULL. */
1023 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1024 sio->base,
1025 NULL,
1026 NULL,
1027 fsp->fsp_name->twrp,
1028 fsp->fsp_name->flags);
1029 if (smb_fname_base == NULL) {
1030 errno = ENOMEM;
1031 return -1;
1034 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1035 smb_fname_base, sio->xattr_name, &ea);
1036 if (!NT_STATUS_IS_OK(status)) {
1037 return -1;
1040 length = ea.value.length-1;
1042 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1043 (int)length));
1045 /* Attempt to read past EOF. */
1046 if (length <= offset) {
1047 return 0;
1050 overlap = (offset + n) > length ? (length - offset) : n;
1051 memcpy(data, ea.value.data + offset, overlap);
1053 TALLOC_FREE(ea.value.data);
1054 return overlap;
1057 struct streams_xattr_pread_state {
1058 ssize_t nread;
1059 struct vfs_aio_state vfs_aio_state;
1062 static void streams_xattr_pread_done(struct tevent_req *subreq);
1064 static struct tevent_req *streams_xattr_pread_send(
1065 struct vfs_handle_struct *handle,
1066 TALLOC_CTX *mem_ctx,
1067 struct tevent_context *ev,
1068 struct files_struct *fsp,
1069 void *data,
1070 size_t n, off_t offset)
1072 struct tevent_req *req = NULL;
1073 struct tevent_req *subreq = NULL;
1074 struct streams_xattr_pread_state *state = NULL;
1075 struct stream_io *sio =
1076 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1078 req = tevent_req_create(mem_ctx, &state,
1079 struct streams_xattr_pread_state);
1080 if (req == NULL) {
1081 return NULL;
1084 if (sio == NULL) {
1085 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1086 data, n, offset);
1087 if (tevent_req_nomem(req, subreq)) {
1088 return tevent_req_post(req, ev);
1090 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1091 return req;
1094 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1095 if (state->nread != n) {
1096 if (state->nread != -1) {
1097 errno = EIO;
1099 tevent_req_error(req, errno);
1100 return tevent_req_post(req, ev);
1103 tevent_req_done(req);
1104 return tevent_req_post(req, ev);
1107 static void streams_xattr_pread_done(struct tevent_req *subreq)
1109 struct tevent_req *req = tevent_req_callback_data(
1110 subreq, struct tevent_req);
1111 struct streams_xattr_pread_state *state = tevent_req_data(
1112 req, struct streams_xattr_pread_state);
1114 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1115 TALLOC_FREE(subreq);
1117 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1118 return;
1120 tevent_req_done(req);
1123 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1124 struct vfs_aio_state *vfs_aio_state)
1126 struct streams_xattr_pread_state *state = tevent_req_data(
1127 req, struct streams_xattr_pread_state);
1129 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1130 return -1;
1133 *vfs_aio_state = state->vfs_aio_state;
1134 return state->nread;
1137 struct streams_xattr_pwrite_state {
1138 ssize_t nwritten;
1139 struct vfs_aio_state vfs_aio_state;
1142 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1144 static struct tevent_req *streams_xattr_pwrite_send(
1145 struct vfs_handle_struct *handle,
1146 TALLOC_CTX *mem_ctx,
1147 struct tevent_context *ev,
1148 struct files_struct *fsp,
1149 const void *data,
1150 size_t n, off_t offset)
1152 struct tevent_req *req = NULL;
1153 struct tevent_req *subreq = NULL;
1154 struct streams_xattr_pwrite_state *state = NULL;
1155 struct stream_io *sio =
1156 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1158 req = tevent_req_create(mem_ctx, &state,
1159 struct streams_xattr_pwrite_state);
1160 if (req == NULL) {
1161 return NULL;
1164 if (sio == NULL) {
1165 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1166 data, n, offset);
1167 if (tevent_req_nomem(req, subreq)) {
1168 return tevent_req_post(req, ev);
1170 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1171 return req;
1174 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1175 if (state->nwritten != n) {
1176 if (state->nwritten != -1) {
1177 errno = EIO;
1179 tevent_req_error(req, errno);
1180 return tevent_req_post(req, ev);
1183 tevent_req_done(req);
1184 return tevent_req_post(req, ev);
1187 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1189 struct tevent_req *req = tevent_req_callback_data(
1190 subreq, struct tevent_req);
1191 struct streams_xattr_pwrite_state *state = tevent_req_data(
1192 req, struct streams_xattr_pwrite_state);
1194 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1195 TALLOC_FREE(subreq);
1197 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1198 return;
1200 tevent_req_done(req);
1203 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1204 struct vfs_aio_state *vfs_aio_state)
1206 struct streams_xattr_pwrite_state *state = tevent_req_data(
1207 req, struct streams_xattr_pwrite_state);
1209 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1210 return -1;
1213 *vfs_aio_state = state->vfs_aio_state;
1214 return state->nwritten;
1217 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1218 struct files_struct *fsp,
1219 off_t offset)
1221 int ret;
1222 uint8_t *tmp;
1223 struct ea_struct ea;
1224 NTSTATUS status;
1225 struct stream_io *sio =
1226 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1227 struct smb_filename *smb_fname_base = NULL;
1229 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1230 fsp_str_dbg(fsp), (double)offset));
1232 if (sio == NULL) {
1233 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1236 if (!streams_xattr_recheck(sio)) {
1237 return -1;
1240 /* Create an smb_filename with stream_name == NULL. */
1241 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1242 sio->base,
1243 NULL,
1244 NULL,
1245 fsp->fsp_name->twrp,
1246 fsp->fsp_name->flags);
1247 if (smb_fname_base == NULL) {
1248 errno = ENOMEM;
1249 return -1;
1252 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1253 smb_fname_base, sio->xattr_name, &ea);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 return -1;
1258 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1259 offset + 1);
1261 if (tmp == NULL) {
1262 TALLOC_FREE(ea.value.data);
1263 errno = ENOMEM;
1264 return -1;
1267 /* Did we expand ? */
1268 if (ea.value.length < offset + 1) {
1269 memset(&tmp[ea.value.length], '\0',
1270 offset + 1 - ea.value.length);
1273 ea.value.data = tmp;
1274 ea.value.length = offset + 1;
1275 ea.value.data[offset] = 0;
1277 ret = SMB_VFS_SETXATTR(fsp->conn,
1278 fsp->fsp_name,
1279 sio->xattr_name,
1280 ea.value.data, ea.value.length, 0);
1281 TALLOC_FREE(ea.value.data);
1283 if (ret == -1) {
1284 return -1;
1287 return 0;
1290 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1291 struct files_struct *fsp,
1292 uint32_t mode,
1293 off_t offset,
1294 off_t len)
1296 struct stream_io *sio =
1297 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1299 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1300 "len = %.0f\n",
1301 fsp_str_dbg(fsp), (double)offset, (double)len));
1303 if (sio == NULL) {
1304 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1307 if (!streams_xattr_recheck(sio)) {
1308 return -1;
1311 /* Let the pwrite code path handle it. */
1312 errno = ENOSYS;
1313 return -1;
1316 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1317 uid_t uid, gid_t gid)
1319 struct stream_io *sio =
1320 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1322 if (sio == NULL) {
1323 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1326 return 0;
1329 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1330 files_struct *fsp,
1331 mode_t mode)
1333 struct stream_io *sio =
1334 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1336 if (sio == NULL) {
1337 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1340 return 0;
1343 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1344 struct files_struct *fsp,
1345 const char *name,
1346 void *value,
1347 size_t size)
1349 struct stream_io *sio =
1350 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1352 if (sio == NULL) {
1353 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1356 errno = ENOTSUP;
1357 return -1;
1360 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1361 struct files_struct *fsp,
1362 char *list,
1363 size_t size)
1365 struct stream_io *sio =
1366 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1368 if (sio == NULL) {
1369 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1372 errno = ENOTSUP;
1373 return -1;
1376 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1377 struct files_struct *fsp,
1378 const char *name)
1380 struct stream_io *sio =
1381 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1383 if (sio == NULL) {
1384 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1387 errno = ENOTSUP;
1388 return -1;
1391 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1392 struct files_struct *fsp,
1393 const char *name,
1394 const void *value,
1395 size_t size,
1396 int flags)
1398 struct stream_io *sio =
1399 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1401 if (sio == NULL) {
1402 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1403 size, flags);
1406 errno = ENOTSUP;
1407 return -1;
1410 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1411 files_struct *fsp,
1412 TALLOC_CTX *mem_ctx)
1414 struct stream_io *sio =
1415 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1417 if (sio == NULL) {
1418 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1421 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1422 handle, fsp->base_fsp->fsp_name,
1423 SMB_ACL_TYPE_ACCESS, mem_ctx);
1426 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1427 files_struct *fsp,
1428 SMB_ACL_T theacl)
1430 struct stream_io *sio =
1431 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1433 if (sio == NULL) {
1434 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1437 return 0;
1440 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1441 files_struct *fsp,
1442 TALLOC_CTX *mem_ctx,
1443 char **blob_description,
1444 DATA_BLOB *blob)
1446 struct stream_io *sio =
1447 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1449 if (sio == NULL) {
1450 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1451 blob_description, blob);
1454 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1455 handle, fsp->base_fsp->fsp_name, mem_ctx,
1456 blob_description, blob);
1459 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1460 files_struct *fsp,
1461 uint32_t security_info,
1462 TALLOC_CTX *mem_ctx,
1463 struct security_descriptor **ppdesc)
1465 struct stream_io *sio =
1466 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1468 if (sio == NULL) {
1469 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1470 mem_ctx, ppdesc);
1473 return SMB_VFS_NEXT_GET_NT_ACL_AT(handle,
1474 handle->conn->cwd_fsp,
1475 fsp->base_fsp->fsp_name,
1476 security_info,
1477 mem_ctx,
1478 ppdesc);
1481 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1482 files_struct *fsp,
1483 uint32_t security_info_sent,
1484 const struct security_descriptor *psd)
1486 struct stream_io *sio =
1487 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1489 if (sio == NULL) {
1490 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1491 security_info_sent, psd);
1494 return NT_STATUS_OK;
1497 struct streams_xattr_fsync_state {
1498 int ret;
1499 struct vfs_aio_state vfs_aio_state;
1502 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1504 static struct tevent_req *streams_xattr_fsync_send(
1505 struct vfs_handle_struct *handle,
1506 TALLOC_CTX *mem_ctx,
1507 struct tevent_context *ev,
1508 struct files_struct *fsp)
1510 struct tevent_req *req = NULL;
1511 struct tevent_req *subreq = NULL;
1512 struct streams_xattr_fsync_state *state = NULL;
1513 struct stream_io *sio =
1514 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1516 req = tevent_req_create(mem_ctx, &state,
1517 struct streams_xattr_fsync_state);
1518 if (req == NULL) {
1519 return NULL;
1522 if (sio == NULL) {
1523 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1524 if (tevent_req_nomem(req, subreq)) {
1525 return tevent_req_post(req, ev);
1527 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1528 return req;
1532 * There's no pathname based sync variant and we don't have access to
1533 * the basefile handle, so we can't do anything here.
1536 tevent_req_done(req);
1537 return tevent_req_post(req, ev);
1540 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1542 struct tevent_req *req = tevent_req_callback_data(
1543 subreq, struct tevent_req);
1544 struct streams_xattr_fsync_state *state = tevent_req_data(
1545 req, struct streams_xattr_fsync_state);
1547 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1548 TALLOC_FREE(subreq);
1549 if (state->ret != 0) {
1550 tevent_req_error(req, errno);
1551 return;
1554 tevent_req_done(req);
1557 static int streams_xattr_fsync_recv(struct tevent_req *req,
1558 struct vfs_aio_state *vfs_aio_state)
1560 struct streams_xattr_fsync_state *state = tevent_req_data(
1561 req, struct streams_xattr_fsync_state);
1563 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1564 return -1;
1567 *vfs_aio_state = state->vfs_aio_state;
1568 return state->ret;
1571 static bool streams_xattr_lock(vfs_handle_struct *handle,
1572 files_struct *fsp,
1573 int op,
1574 off_t offset,
1575 off_t count,
1576 int type)
1578 struct stream_io *sio =
1579 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1581 if (sio == NULL) {
1582 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1585 return true;
1588 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1589 files_struct *fsp,
1590 off_t *poffset,
1591 off_t *pcount,
1592 int *ptype,
1593 pid_t *ppid)
1595 struct stream_io *sio =
1596 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1598 if (sio == NULL) {
1599 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1600 pcount, ptype, ppid);
1603 errno = ENOTSUP;
1604 return false;
1607 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1608 files_struct *fsp,
1609 uint32_t share_access,
1610 uint32_t access_mask)
1612 struct stream_io *sio =
1613 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1615 if (sio == NULL) {
1616 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1617 share_access, access_mask);
1620 return 0;
1623 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1624 files_struct *fsp,
1625 int leasetype)
1627 struct stream_io *sio =
1628 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1630 if (sio == NULL) {
1631 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1634 return 0;
1637 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1638 files_struct *fsp,
1639 struct lock_struct *plock)
1641 struct stream_io *sio =
1642 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1644 if (sio == NULL) {
1645 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1648 return true;
1651 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1652 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1653 .connect_fn = streams_xattr_connect,
1654 .openat_fn = streams_xattr_openat,
1655 .close_fn = streams_xattr_close,
1656 .stat_fn = streams_xattr_stat,
1657 .fstat_fn = streams_xattr_fstat,
1658 .lstat_fn = streams_xattr_lstat,
1659 .pread_fn = streams_xattr_pread,
1660 .pwrite_fn = streams_xattr_pwrite,
1661 .pread_send_fn = streams_xattr_pread_send,
1662 .pread_recv_fn = streams_xattr_pread_recv,
1663 .pwrite_send_fn = streams_xattr_pwrite_send,
1664 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1665 .unlinkat_fn = streams_xattr_unlinkat,
1666 .renameat_fn = streams_xattr_renameat,
1667 .ftruncate_fn = streams_xattr_ftruncate,
1668 .fallocate_fn = streams_xattr_fallocate,
1669 .streaminfo_fn = streams_xattr_streaminfo,
1671 .fsync_send_fn = streams_xattr_fsync_send,
1672 .fsync_recv_fn = streams_xattr_fsync_recv,
1674 .lock_fn = streams_xattr_lock,
1675 .getlock_fn = streams_xattr_getlock,
1676 .kernel_flock_fn = streams_xattr_kernel_flock,
1677 .linux_setlease_fn = streams_xattr_linux_setlease,
1678 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1680 .fchown_fn = streams_xattr_fchown,
1681 .fchmod_fn = streams_xattr_fchmod,
1683 .fgetxattr_fn = streams_xattr_fgetxattr,
1684 .flistxattr_fn = streams_xattr_flistxattr,
1685 .fremovexattr_fn = streams_xattr_fremovexattr,
1686 .fsetxattr_fn = streams_xattr_fsetxattr,
1688 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1689 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1690 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1692 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1693 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1696 static_decl_vfs;
1697 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1699 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1700 &vfs_streams_xattr_fns);