s3/smbd: modify get_ea_names_from_file signature fn to take fsp alone
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blobca85ad86705550a3fe0bb65d22e489d3456b2287
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 ret = vfs_stat(handle->conn, smb_fname_base);
230 *sbuf = smb_fname_base->st;
232 if (ret == -1) {
233 TALLOC_FREE(smb_fname_base);
234 return -1;
237 sbuf->st_ex_size = get_xattr_size(handle->conn,
238 smb_fname_base, io->xattr_name);
239 if (sbuf->st_ex_size == -1) {
240 TALLOC_FREE(smb_fname_base);
241 SET_STAT_INVALID(*sbuf);
242 return -1;
245 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
247 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
248 sbuf->st_ex_mode &= ~S_IFMT;
249 sbuf->st_ex_mode &= ~S_IFDIR;
250 sbuf->st_ex_mode |= S_IFREG;
251 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
253 TALLOC_FREE(smb_fname_base);
254 return 0;
257 static int streams_xattr_stat(vfs_handle_struct *handle,
258 struct smb_filename *smb_fname)
260 NTSTATUS status;
261 int result = -1;
262 char *xattr_name = NULL;
264 if (!is_named_stream(smb_fname)) {
265 return SMB_VFS_NEXT_STAT(handle, smb_fname);
268 /* Note if lp_posix_paths() is true, we can never
269 * get here as is_named_stream() is
270 * always false. So we never need worry about
271 * not following links here. */
273 /* Populate the stat struct with info from the base file. */
274 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
275 return -1;
278 /* Derive the xattr name to lookup. */
279 status = streams_xattr_get_name(handle, talloc_tos(),
280 smb_fname->stream_name, &xattr_name);
281 if (!NT_STATUS_IS_OK(status)) {
282 errno = map_errno_from_nt_status(status);
283 return -1;
286 /* Augment the base file's stat information before returning. */
287 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
288 smb_fname,
289 xattr_name);
290 if (smb_fname->st.st_ex_size == -1) {
291 SET_STAT_INVALID(smb_fname->st);
292 errno = ENOENT;
293 result = -1;
294 goto fail;
297 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
298 smb_fname->st.st_ex_mode &= ~S_IFMT;
299 smb_fname->st.st_ex_mode &= ~S_IFDIR;
300 smb_fname->st.st_ex_mode |= S_IFREG;
301 smb_fname->st.st_ex_blocks =
302 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
304 result = 0;
305 fail:
306 TALLOC_FREE(xattr_name);
307 return result;
310 static int streams_xattr_lstat(vfs_handle_struct *handle,
311 struct smb_filename *smb_fname)
313 NTSTATUS status;
314 int result = -1;
315 char *xattr_name = NULL;
317 if (!is_named_stream(smb_fname)) {
318 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
321 /* Populate the stat struct with info from the base file. */
322 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
323 return -1;
326 /* Derive the xattr name to lookup. */
327 status = streams_xattr_get_name(handle, talloc_tos(),
328 smb_fname->stream_name, &xattr_name);
329 if (!NT_STATUS_IS_OK(status)) {
330 errno = map_errno_from_nt_status(status);
331 return -1;
334 /* Augment the base file's stat information before returning. */
335 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
336 smb_fname,
337 xattr_name);
338 if (smb_fname->st.st_ex_size == -1) {
339 SET_STAT_INVALID(smb_fname->st);
340 errno = ENOENT;
341 result = -1;
342 goto fail;
345 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
346 smb_fname->st.st_ex_mode &= ~S_IFMT;
347 smb_fname->st.st_ex_mode |= S_IFREG;
348 smb_fname->st.st_ex_blocks =
349 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
351 result = 0;
353 fail:
354 TALLOC_FREE(xattr_name);
355 return result;
358 static int streams_xattr_openat(struct vfs_handle_struct *handle,
359 const struct files_struct *dirfsp,
360 const struct smb_filename *smb_fname,
361 files_struct *fsp,
362 int flags,
363 mode_t mode)
365 NTSTATUS status;
366 struct streams_xattr_config *config = NULL;
367 struct stream_io *sio = NULL;
368 struct ea_struct ea;
369 char *xattr_name = NULL;
370 int fakefd = -1;
371 bool set_empty_xattr = false;
372 int ret;
374 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
375 return -1);
377 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
378 smb_fname_str_dbg(smb_fname), flags));
380 if (!is_named_stream(smb_fname)) {
381 return SMB_VFS_NEXT_OPENAT(handle,
382 dirfsp,
383 smb_fname,
384 fsp,
385 flags,
386 mode);
390 * For now assert this, so the below SMB_VFS_SETXATTR() works.
392 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
394 status = streams_xattr_get_name(handle, talloc_tos(),
395 smb_fname->stream_name, &xattr_name);
396 if (!NT_STATUS_IS_OK(status)) {
397 errno = map_errno_from_nt_status(status);
398 goto fail;
401 status = get_ea_value(talloc_tos(), handle->conn, NULL,
402 smb_fname, xattr_name, &ea);
404 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
406 if (!NT_STATUS_IS_OK(status)) {
407 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
409 * The base file is not there. This is an error even if
410 * we got O_CREAT, the higher levels should have created
411 * the base file for us.
413 DBG_DEBUG("streams_xattr_open: base file %s not around, "
414 "returning ENOENT\n", smb_fname->base_name);
415 errno = ENOENT;
416 goto fail;
419 if (!(flags & O_CREAT)) {
420 errno = ENOATTR;
421 goto fail;
424 set_empty_xattr = true;
427 if (flags & O_TRUNC) {
428 set_empty_xattr = true;
431 if (set_empty_xattr) {
433 * The attribute does not exist or needs to be truncated
437 * Darn, xattrs need at least 1 byte
439 char null = '\0';
441 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
442 xattr_name, smb_fname->base_name));
444 ret = SMB_VFS_SETXATTR(fsp->conn,
445 smb_fname,
446 xattr_name,
447 &null, sizeof(null),
448 flags & O_EXCL ? XATTR_CREATE : 0);
449 if (ret != 0) {
450 goto fail;
454 fakefd = vfs_fake_fd();
456 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
457 if (sio == NULL) {
458 errno = ENOMEM;
459 goto fail;
462 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
463 xattr_name);
464 if (sio->xattr_name == NULL) {
465 errno = ENOMEM;
466 goto fail;
470 * so->base needs to be a copy of fsp->fsp_name->base_name,
471 * making it identical to streams_xattr_recheck(). If the
472 * open is changing directories, fsp->fsp_name->base_name
473 * will be the full path from the share root, whilst
474 * smb_fname will be relative to the $cwd.
476 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
477 fsp->fsp_name->base_name);
478 if (sio->base == NULL) {
479 errno = ENOMEM;
480 goto fail;
483 sio->fsp_name_ptr = fsp->fsp_name;
484 sio->handle = handle;
485 sio->fsp = fsp;
487 return fakefd;
489 fail:
490 if (fakefd >= 0) {
491 vfs_fake_fd_close(fakefd);
492 fakefd = -1;
495 return -1;
498 static int streams_xattr_close(vfs_handle_struct *handle,
499 files_struct *fsp)
501 int ret;
502 int fd;
504 fd = fsp_get_pathref_fd(fsp);
506 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
507 smb_fname_str_dbg(fsp->fsp_name), fd);
509 if (!is_named_stream(fsp->fsp_name)) {
510 return SMB_VFS_NEXT_CLOSE(handle, fsp);
513 ret = vfs_fake_fd_close(fd);
514 fsp_set_fd(fsp, -1);
516 return ret;
519 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
520 struct files_struct *dirfsp,
521 const struct smb_filename *smb_fname,
522 int flags)
524 NTSTATUS status;
525 int ret = -1;
526 char *xattr_name = NULL;
528 if (!is_named_stream(smb_fname)) {
529 return SMB_VFS_NEXT_UNLINKAT(handle,
530 dirfsp,
531 smb_fname,
532 flags);
535 status = streams_xattr_get_name(handle, talloc_tos(),
536 smb_fname->stream_name, &xattr_name);
537 if (!NT_STATUS_IS_OK(status)) {
538 errno = map_errno_from_nt_status(status);
539 goto fail;
542 SMB_ASSERT(smb_fname->fsp != NULL);
543 SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
545 ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
547 if ((ret == -1) && (errno == ENOATTR)) {
548 errno = ENOENT;
549 goto fail;
552 ret = 0;
554 fail:
555 TALLOC_FREE(xattr_name);
556 return ret;
559 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
560 struct files_struct *dirfsp,
561 const struct smb_filename *smb_fname,
562 int flags)
564 int ret;
565 if (flags & AT_REMOVEDIR) {
566 ret = SMB_VFS_NEXT_UNLINKAT(handle,
567 dirfsp,
568 smb_fname,
569 flags);
570 } else {
571 ret = streams_xattr_unlink_internal(handle,
572 dirfsp,
573 smb_fname,
574 flags);
576 return ret;
579 static int streams_xattr_renameat(vfs_handle_struct *handle,
580 files_struct *srcfsp,
581 const struct smb_filename *smb_fname_src,
582 files_struct *dstfsp,
583 const struct smb_filename *smb_fname_dst)
585 NTSTATUS status;
586 int ret = -1;
587 char *src_xattr_name = NULL;
588 char *dst_xattr_name = NULL;
589 bool src_is_stream, dst_is_stream;
590 ssize_t oret;
591 ssize_t nret;
592 struct ea_struct ea;
594 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
595 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
597 if (!src_is_stream && !dst_is_stream) {
598 return SMB_VFS_NEXT_RENAMEAT(handle,
599 srcfsp,
600 smb_fname_src,
601 dstfsp,
602 smb_fname_dst);
605 /* For now don't allow renames from or to the default stream. */
606 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
607 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
608 errno = ENOSYS;
609 goto done;
612 /* Don't rename if the streams are identical. */
613 if (strcasecmp_m(smb_fname_src->stream_name,
614 smb_fname_dst->stream_name) == 0) {
615 goto done;
618 /* Get the xattr names. */
619 status = streams_xattr_get_name(handle, talloc_tos(),
620 smb_fname_src->stream_name,
621 &src_xattr_name);
622 if (!NT_STATUS_IS_OK(status)) {
623 errno = map_errno_from_nt_status(status);
624 goto fail;
626 status = streams_xattr_get_name(handle, talloc_tos(),
627 smb_fname_dst->stream_name,
628 &dst_xattr_name);
629 if (!NT_STATUS_IS_OK(status)) {
630 errno = map_errno_from_nt_status(status);
631 goto fail;
634 /* read the old stream */
635 status = get_ea_value(talloc_tos(), handle->conn, NULL,
636 smb_fname_src, src_xattr_name, &ea);
637 if (!NT_STATUS_IS_OK(status)) {
638 errno = ENOENT;
639 goto fail;
642 /* (over)write the new stream */
643 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
644 dst_xattr_name, ea.value.data, ea.value.length,
646 if (nret < 0) {
647 if (errno == ENOATTR) {
648 errno = ENOENT;
650 goto fail;
653 /* remove the old stream */
654 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
655 src_xattr_name);
656 if (oret < 0) {
657 if (errno == ENOATTR) {
658 errno = ENOENT;
660 goto fail;
663 done:
664 errno = 0;
665 ret = 0;
666 fail:
667 TALLOC_FREE(src_xattr_name);
668 TALLOC_FREE(dst_xattr_name);
669 return ret;
672 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
673 files_struct *fsp,
674 const struct smb_filename *smb_fname,
675 bool (*fn)(struct ea_struct *ea,
676 void *private_data),
677 void *private_data)
679 NTSTATUS status;
680 char **names;
681 size_t i, num_names;
682 struct streams_xattr_config *config;
684 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
685 return NT_STATUS_UNSUCCESSFUL);
687 status = get_ea_names_from_file(talloc_tos(),
688 handle->conn,
689 smb_fname->fsp,
690 &names,
691 &num_names);
692 if (!NT_STATUS_IS_OK(status)) {
693 return status;
696 for (i=0; i<num_names; i++) {
697 struct ea_struct ea;
700 * We want to check with samba_private_attr_name()
701 * whether the xattr name is a private one,
702 * unfortunately it flags xattrs that begin with the
703 * default streams prefix as private.
705 * By only calling samba_private_attr_name() in case
706 * the xattr does NOT begin with the default prefix,
707 * we know that if it returns 'true' it definitely one
708 * of our internal xattr like "user.DOSATTRIB".
710 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
711 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
712 if (samba_private_attr_name(names[i])) {
713 continue;
717 if (strncmp(names[i], config->prefix,
718 config->prefix_len) != 0) {
719 continue;
722 status = get_ea_value(names,
723 handle->conn,
724 NULL,
725 smb_fname,
726 names[i],
727 &ea);
728 if (!NT_STATUS_IS_OK(status)) {
729 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
730 names[i],
731 smb_fname->base_name,
732 nt_errstr(status)));
733 continue;
736 ea.name = talloc_asprintf(
737 ea.value.data, ":%s%s",
738 names[i] + config->prefix_len,
739 config->store_stream_type ? "" : ":$DATA");
740 if (ea.name == NULL) {
741 DEBUG(0, ("talloc failed\n"));
742 continue;
745 if (!fn(&ea, private_data)) {
746 TALLOC_FREE(ea.value.data);
747 return NT_STATUS_OK;
750 TALLOC_FREE(ea.value.data);
753 TALLOC_FREE(names);
754 return NT_STATUS_OK;
757 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
758 struct stream_struct **streams,
759 const char *name, off_t size,
760 off_t alloc_size)
762 struct stream_struct *tmp;
764 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
765 (*num_streams)+1);
766 if (tmp == NULL) {
767 return false;
770 tmp[*num_streams].name = talloc_strdup(tmp, name);
771 if (tmp[*num_streams].name == NULL) {
772 return false;
775 tmp[*num_streams].size = size;
776 tmp[*num_streams].alloc_size = alloc_size;
778 *streams = tmp;
779 *num_streams += 1;
780 return true;
783 struct streaminfo_state {
784 TALLOC_CTX *mem_ctx;
785 vfs_handle_struct *handle;
786 unsigned int num_streams;
787 struct stream_struct *streams;
788 NTSTATUS status;
791 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
793 struct streaminfo_state *state =
794 (struct streaminfo_state *)private_data;
796 if (!add_one_stream(state->mem_ctx,
797 &state->num_streams, &state->streams,
798 ea->name, ea->value.length-1,
799 smb_roundup(state->handle->conn,
800 ea->value.length-1))) {
801 state->status = NT_STATUS_NO_MEMORY;
802 return false;
805 return true;
808 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
809 struct files_struct *fsp,
810 const struct smb_filename *smb_fname,
811 TALLOC_CTX *mem_ctx,
812 unsigned int *pnum_streams,
813 struct stream_struct **pstreams)
815 SMB_STRUCT_STAT sbuf;
816 int ret;
817 NTSTATUS status;
818 struct streaminfo_state state;
820 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
821 if (ret == -1) {
822 return map_nt_error_from_unix(errno);
825 state.streams = *pstreams;
826 state.num_streams = *pnum_streams;
827 state.mem_ctx = mem_ctx;
828 state.handle = handle;
829 state.status = NT_STATUS_OK;
831 if (S_ISLNK(sbuf.st_ex_mode)) {
833 * Currently we do't have SMB_VFS_LLISTXATTR
834 * inside the VFS which means there's no way
835 * to cope with a symlink when lp_posix_pathnames().
836 * returns true. For now ignore links.
837 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
839 status = NT_STATUS_OK;
840 } else {
841 status = walk_xattr_streams(handle, fsp, smb_fname,
842 collect_one_stream, &state);
845 if (!NT_STATUS_IS_OK(status)) {
846 TALLOC_FREE(state.streams);
847 return status;
850 if (!NT_STATUS_IS_OK(state.status)) {
851 TALLOC_FREE(state.streams);
852 return state.status;
855 *pnum_streams = state.num_streams;
856 *pstreams = state.streams;
858 return SMB_VFS_NEXT_STREAMINFO(handle,
859 fsp,
860 smb_fname,
861 mem_ctx,
862 pnum_streams,
863 pstreams);
866 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
867 enum timestamp_set_resolution *p_ts_res)
869 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
872 static int streams_xattr_connect(vfs_handle_struct *handle,
873 const char *service, const char *user)
875 struct streams_xattr_config *config;
876 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
877 const char *prefix;
878 int rc;
880 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
881 if (rc != 0) {
882 return rc;
885 config = talloc_zero(handle->conn, struct streams_xattr_config);
886 if (config == NULL) {
887 DEBUG(1, ("talloc_zero() failed\n"));
888 errno = ENOMEM;
889 return -1;
892 prefix = lp_parm_const_string(SNUM(handle->conn),
893 "streams_xattr", "prefix",
894 default_prefix);
895 config->prefix = talloc_strdup(config, prefix);
896 if (config->prefix == NULL) {
897 DEBUG(1, ("talloc_strdup() failed\n"));
898 errno = ENOMEM;
899 return -1;
901 config->prefix_len = strlen(config->prefix);
902 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
904 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
905 "streams_xattr",
906 "store_stream_type",
907 true);
909 SMB_VFS_HANDLE_SET_DATA(handle, config,
910 NULL, struct stream_xattr_config,
911 return -1);
913 return 0;
916 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
917 files_struct *fsp, const void *data,
918 size_t n, off_t offset)
920 struct stream_io *sio =
921 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
922 struct ea_struct ea;
923 NTSTATUS status;
924 struct smb_filename *smb_fname_base = NULL;
925 int ret;
927 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
929 if (sio == NULL) {
930 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
933 if (!streams_xattr_recheck(sio)) {
934 return -1;
937 /* Create an smb_filename with stream_name == NULL. */
938 smb_fname_base = synthetic_smb_fname(talloc_tos(),
939 sio->base,
940 NULL,
941 NULL,
942 fsp->fsp_name->twrp,
943 fsp->fsp_name->flags);
944 if (smb_fname_base == NULL) {
945 errno = ENOMEM;
946 return -1;
949 status = get_ea_value(talloc_tos(), handle->conn, NULL,
950 smb_fname_base, sio->xattr_name, &ea);
951 if (!NT_STATUS_IS_OK(status)) {
952 return -1;
955 if ((offset + n) > ea.value.length-1) {
956 uint8_t *tmp;
958 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
959 offset + n + 1);
961 if (tmp == NULL) {
962 TALLOC_FREE(ea.value.data);
963 errno = ENOMEM;
964 return -1;
966 ea.value.data = tmp;
967 ea.value.length = offset + n + 1;
968 ea.value.data[offset+n] = 0;
971 memcpy(ea.value.data + offset, data, n);
973 ret = SMB_VFS_SETXATTR(fsp->conn,
974 fsp->fsp_name,
975 sio->xattr_name,
976 ea.value.data, ea.value.length, 0);
977 TALLOC_FREE(ea.value.data);
979 if (ret == -1) {
980 return -1;
983 return n;
986 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
987 files_struct *fsp, void *data,
988 size_t n, off_t offset)
990 struct stream_io *sio =
991 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
992 struct ea_struct ea;
993 NTSTATUS status;
994 size_t length, overlap;
995 struct smb_filename *smb_fname_base = NULL;
997 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
998 (int)offset, (int)n));
1000 if (sio == NULL) {
1001 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1004 if (!streams_xattr_recheck(sio)) {
1005 return -1;
1008 /* Create an smb_filename with stream_name == NULL. */
1009 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1010 sio->base,
1011 NULL,
1012 NULL,
1013 fsp->fsp_name->twrp,
1014 fsp->fsp_name->flags);
1015 if (smb_fname_base == NULL) {
1016 errno = ENOMEM;
1017 return -1;
1020 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1021 smb_fname_base, sio->xattr_name, &ea);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 return -1;
1026 length = ea.value.length-1;
1028 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1029 (int)length));
1031 /* Attempt to read past EOF. */
1032 if (length <= offset) {
1033 return 0;
1036 overlap = (offset + n) > length ? (length - offset) : n;
1037 memcpy(data, ea.value.data + offset, overlap);
1039 TALLOC_FREE(ea.value.data);
1040 return overlap;
1043 struct streams_xattr_pread_state {
1044 ssize_t nread;
1045 struct vfs_aio_state vfs_aio_state;
1048 static void streams_xattr_pread_done(struct tevent_req *subreq);
1050 static struct tevent_req *streams_xattr_pread_send(
1051 struct vfs_handle_struct *handle,
1052 TALLOC_CTX *mem_ctx,
1053 struct tevent_context *ev,
1054 struct files_struct *fsp,
1055 void *data,
1056 size_t n, off_t offset)
1058 struct tevent_req *req = NULL;
1059 struct tevent_req *subreq = NULL;
1060 struct streams_xattr_pread_state *state = NULL;
1061 struct stream_io *sio =
1062 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1064 req = tevent_req_create(mem_ctx, &state,
1065 struct streams_xattr_pread_state);
1066 if (req == NULL) {
1067 return NULL;
1070 if (sio == NULL) {
1071 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1072 data, n, offset);
1073 if (tevent_req_nomem(req, subreq)) {
1074 return tevent_req_post(req, ev);
1076 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1077 return req;
1080 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1081 if (state->nread != n) {
1082 if (state->nread != -1) {
1083 errno = EIO;
1085 tevent_req_error(req, errno);
1086 return tevent_req_post(req, ev);
1089 tevent_req_done(req);
1090 return tevent_req_post(req, ev);
1093 static void streams_xattr_pread_done(struct tevent_req *subreq)
1095 struct tevent_req *req = tevent_req_callback_data(
1096 subreq, struct tevent_req);
1097 struct streams_xattr_pread_state *state = tevent_req_data(
1098 req, struct streams_xattr_pread_state);
1100 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1101 TALLOC_FREE(subreq);
1103 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1104 return;
1106 tevent_req_done(req);
1109 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1110 struct vfs_aio_state *vfs_aio_state)
1112 struct streams_xattr_pread_state *state = tevent_req_data(
1113 req, struct streams_xattr_pread_state);
1115 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1116 return -1;
1119 *vfs_aio_state = state->vfs_aio_state;
1120 return state->nread;
1123 struct streams_xattr_pwrite_state {
1124 ssize_t nwritten;
1125 struct vfs_aio_state vfs_aio_state;
1128 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1130 static struct tevent_req *streams_xattr_pwrite_send(
1131 struct vfs_handle_struct *handle,
1132 TALLOC_CTX *mem_ctx,
1133 struct tevent_context *ev,
1134 struct files_struct *fsp,
1135 const void *data,
1136 size_t n, off_t offset)
1138 struct tevent_req *req = NULL;
1139 struct tevent_req *subreq = NULL;
1140 struct streams_xattr_pwrite_state *state = NULL;
1141 struct stream_io *sio =
1142 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1144 req = tevent_req_create(mem_ctx, &state,
1145 struct streams_xattr_pwrite_state);
1146 if (req == NULL) {
1147 return NULL;
1150 if (sio == NULL) {
1151 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1152 data, n, offset);
1153 if (tevent_req_nomem(req, subreq)) {
1154 return tevent_req_post(req, ev);
1156 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1157 return req;
1160 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1161 if (state->nwritten != n) {
1162 if (state->nwritten != -1) {
1163 errno = EIO;
1165 tevent_req_error(req, errno);
1166 return tevent_req_post(req, ev);
1169 tevent_req_done(req);
1170 return tevent_req_post(req, ev);
1173 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1175 struct tevent_req *req = tevent_req_callback_data(
1176 subreq, struct tevent_req);
1177 struct streams_xattr_pwrite_state *state = tevent_req_data(
1178 req, struct streams_xattr_pwrite_state);
1180 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1181 TALLOC_FREE(subreq);
1183 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1184 return;
1186 tevent_req_done(req);
1189 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1190 struct vfs_aio_state *vfs_aio_state)
1192 struct streams_xattr_pwrite_state *state = tevent_req_data(
1193 req, struct streams_xattr_pwrite_state);
1195 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1196 return -1;
1199 *vfs_aio_state = state->vfs_aio_state;
1200 return state->nwritten;
1203 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1204 struct files_struct *fsp,
1205 off_t offset)
1207 int ret;
1208 uint8_t *tmp;
1209 struct ea_struct ea;
1210 NTSTATUS status;
1211 struct stream_io *sio =
1212 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1213 struct smb_filename *smb_fname_base = NULL;
1215 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1216 fsp_str_dbg(fsp), (double)offset));
1218 if (sio == NULL) {
1219 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1222 if (!streams_xattr_recheck(sio)) {
1223 return -1;
1226 /* Create an smb_filename with stream_name == NULL. */
1227 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1228 sio->base,
1229 NULL,
1230 NULL,
1231 fsp->fsp_name->twrp,
1232 fsp->fsp_name->flags);
1233 if (smb_fname_base == NULL) {
1234 errno = ENOMEM;
1235 return -1;
1238 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1239 smb_fname_base, sio->xattr_name, &ea);
1240 if (!NT_STATUS_IS_OK(status)) {
1241 return -1;
1244 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1245 offset + 1);
1247 if (tmp == NULL) {
1248 TALLOC_FREE(ea.value.data);
1249 errno = ENOMEM;
1250 return -1;
1253 /* Did we expand ? */
1254 if (ea.value.length < offset + 1) {
1255 memset(&tmp[ea.value.length], '\0',
1256 offset + 1 - ea.value.length);
1259 ea.value.data = tmp;
1260 ea.value.length = offset + 1;
1261 ea.value.data[offset] = 0;
1263 ret = SMB_VFS_SETXATTR(fsp->conn,
1264 fsp->fsp_name,
1265 sio->xattr_name,
1266 ea.value.data, ea.value.length, 0);
1267 TALLOC_FREE(ea.value.data);
1269 if (ret == -1) {
1270 return -1;
1273 return 0;
1276 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1277 struct files_struct *fsp,
1278 uint32_t mode,
1279 off_t offset,
1280 off_t len)
1282 struct stream_io *sio =
1283 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1285 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1286 "len = %.0f\n",
1287 fsp_str_dbg(fsp), (double)offset, (double)len));
1289 if (sio == NULL) {
1290 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1293 if (!streams_xattr_recheck(sio)) {
1294 return -1;
1297 /* Let the pwrite code path handle it. */
1298 errno = ENOSYS;
1299 return -1;
1302 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1303 uid_t uid, gid_t gid)
1305 struct stream_io *sio =
1306 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1308 if (sio == NULL) {
1309 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1312 return 0;
1315 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1316 files_struct *fsp,
1317 mode_t mode)
1319 struct stream_io *sio =
1320 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1322 if (sio == NULL) {
1323 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1326 return 0;
1329 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1330 struct files_struct *fsp,
1331 const char *name,
1332 void *value,
1333 size_t size)
1335 struct stream_io *sio =
1336 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1338 if (sio == NULL) {
1339 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1342 errno = ENOTSUP;
1343 return -1;
1346 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1347 struct files_struct *fsp,
1348 char *list,
1349 size_t size)
1351 struct stream_io *sio =
1352 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1354 if (sio == NULL) {
1355 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1358 errno = ENOTSUP;
1359 return -1;
1362 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1363 struct files_struct *fsp,
1364 const char *name)
1366 struct stream_io *sio =
1367 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1369 if (sio == NULL) {
1370 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1373 errno = ENOTSUP;
1374 return -1;
1377 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1378 struct files_struct *fsp,
1379 const char *name,
1380 const void *value,
1381 size_t size,
1382 int flags)
1384 struct stream_io *sio =
1385 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1387 if (sio == NULL) {
1388 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1389 size, flags);
1392 errno = ENOTSUP;
1393 return -1;
1396 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1397 files_struct *fsp,
1398 TALLOC_CTX *mem_ctx)
1400 struct stream_io *sio =
1401 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1403 if (sio == NULL) {
1404 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1407 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1408 handle, fsp->base_fsp->fsp_name,
1409 SMB_ACL_TYPE_ACCESS, mem_ctx);
1412 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1413 files_struct *fsp,
1414 SMB_ACL_TYPE_T type,
1415 SMB_ACL_T theacl)
1417 struct stream_io *sio =
1418 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1420 if (sio == NULL) {
1421 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, type, theacl);
1424 return 0;
1427 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1428 files_struct *fsp,
1429 TALLOC_CTX *mem_ctx,
1430 char **blob_description,
1431 DATA_BLOB *blob)
1433 struct stream_io *sio =
1434 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1436 if (sio == NULL) {
1437 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1438 blob_description, blob);
1441 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1442 handle, fsp->base_fsp->fsp_name, mem_ctx,
1443 blob_description, blob);
1446 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1447 files_struct *fsp,
1448 uint32_t security_info,
1449 TALLOC_CTX *mem_ctx,
1450 struct security_descriptor **ppdesc)
1452 struct stream_io *sio =
1453 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1455 if (sio == NULL) {
1456 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1457 mem_ctx, ppdesc);
1460 return SMB_VFS_NEXT_GET_NT_ACL_AT(handle,
1461 handle->conn->cwd_fsp,
1462 fsp->base_fsp->fsp_name,
1463 security_info,
1464 mem_ctx,
1465 ppdesc);
1468 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1469 files_struct *fsp,
1470 uint32_t security_info_sent,
1471 const struct security_descriptor *psd)
1473 struct stream_io *sio =
1474 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1476 if (sio == NULL) {
1477 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1478 security_info_sent, psd);
1481 return NT_STATUS_OK;
1484 struct streams_xattr_fsync_state {
1485 int ret;
1486 struct vfs_aio_state vfs_aio_state;
1489 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1491 static struct tevent_req *streams_xattr_fsync_send(
1492 struct vfs_handle_struct *handle,
1493 TALLOC_CTX *mem_ctx,
1494 struct tevent_context *ev,
1495 struct files_struct *fsp)
1497 struct tevent_req *req = NULL;
1498 struct tevent_req *subreq = NULL;
1499 struct streams_xattr_fsync_state *state = NULL;
1500 struct stream_io *sio =
1501 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1503 req = tevent_req_create(mem_ctx, &state,
1504 struct streams_xattr_fsync_state);
1505 if (req == NULL) {
1506 return NULL;
1509 if (sio == NULL) {
1510 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1511 if (tevent_req_nomem(req, subreq)) {
1512 return tevent_req_post(req, ev);
1514 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1515 return req;
1519 * There's no pathname based sync variant and we don't have access to
1520 * the basefile handle, so we can't do anything here.
1523 tevent_req_done(req);
1524 return tevent_req_post(req, ev);
1527 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1529 struct tevent_req *req = tevent_req_callback_data(
1530 subreq, struct tevent_req);
1531 struct streams_xattr_fsync_state *state = tevent_req_data(
1532 req, struct streams_xattr_fsync_state);
1534 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1535 TALLOC_FREE(subreq);
1536 if (state->ret != 0) {
1537 tevent_req_error(req, errno);
1538 return;
1541 tevent_req_done(req);
1544 static int streams_xattr_fsync_recv(struct tevent_req *req,
1545 struct vfs_aio_state *vfs_aio_state)
1547 struct streams_xattr_fsync_state *state = tevent_req_data(
1548 req, struct streams_xattr_fsync_state);
1550 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1551 return -1;
1554 *vfs_aio_state = state->vfs_aio_state;
1555 return state->ret;
1558 static bool streams_xattr_lock(vfs_handle_struct *handle,
1559 files_struct *fsp,
1560 int op,
1561 off_t offset,
1562 off_t count,
1563 int type)
1565 struct stream_io *sio =
1566 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1568 if (sio == NULL) {
1569 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1572 return true;
1575 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1576 files_struct *fsp,
1577 off_t *poffset,
1578 off_t *pcount,
1579 int *ptype,
1580 pid_t *ppid)
1582 struct stream_io *sio =
1583 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1585 if (sio == NULL) {
1586 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1587 pcount, ptype, ppid);
1590 errno = ENOTSUP;
1591 return false;
1594 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1595 files_struct *fsp,
1596 uint32_t share_access,
1597 uint32_t access_mask)
1599 struct stream_io *sio =
1600 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1602 if (sio == NULL) {
1603 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1604 share_access, access_mask);
1607 return 0;
1610 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1611 files_struct *fsp,
1612 int leasetype)
1614 struct stream_io *sio =
1615 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1617 if (sio == NULL) {
1618 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1621 return 0;
1624 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1625 files_struct *fsp,
1626 struct lock_struct *plock)
1628 struct stream_io *sio =
1629 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1631 if (sio == NULL) {
1632 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1635 return true;
1638 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1639 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1640 .connect_fn = streams_xattr_connect,
1641 .openat_fn = streams_xattr_openat,
1642 .close_fn = streams_xattr_close,
1643 .stat_fn = streams_xattr_stat,
1644 .fstat_fn = streams_xattr_fstat,
1645 .lstat_fn = streams_xattr_lstat,
1646 .pread_fn = streams_xattr_pread,
1647 .pwrite_fn = streams_xattr_pwrite,
1648 .pread_send_fn = streams_xattr_pread_send,
1649 .pread_recv_fn = streams_xattr_pread_recv,
1650 .pwrite_send_fn = streams_xattr_pwrite_send,
1651 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1652 .unlinkat_fn = streams_xattr_unlinkat,
1653 .renameat_fn = streams_xattr_renameat,
1654 .ftruncate_fn = streams_xattr_ftruncate,
1655 .fallocate_fn = streams_xattr_fallocate,
1656 .streaminfo_fn = streams_xattr_streaminfo,
1658 .fsync_send_fn = streams_xattr_fsync_send,
1659 .fsync_recv_fn = streams_xattr_fsync_recv,
1661 .lock_fn = streams_xattr_lock,
1662 .getlock_fn = streams_xattr_getlock,
1663 .kernel_flock_fn = streams_xattr_kernel_flock,
1664 .linux_setlease_fn = streams_xattr_linux_setlease,
1665 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1667 .fchown_fn = streams_xattr_fchown,
1668 .fchmod_fn = streams_xattr_fchmod,
1670 .fgetxattr_fn = streams_xattr_fgetxattr,
1671 .flistxattr_fn = streams_xattr_flistxattr,
1672 .fremovexattr_fn = streams_xattr_fremovexattr,
1673 .fsetxattr_fn = streams_xattr_fsetxattr,
1675 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1676 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1677 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1679 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1680 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1683 static_decl_vfs;
1684 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1686 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1687 &vfs_streams_xattr_fns);