selftest: Add test for failing chdir call in smbd
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blobc126a483f70182890163766c09d0be90b6fd995d
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/crypto/md5.h"
28 #include "lib/util/tevent_unix.h"
29 #include "librpc/gen_ndr/ioctl.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 SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
50 MD5_CTX ctx;
51 unsigned char hash[16];
52 SMB_INO_T result;
53 char *upper_sname;
55 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
56 (unsigned long)sbuf->st_ex_dev,
57 (unsigned long)sbuf->st_ex_ino, sname));
59 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
60 SMB_ASSERT(upper_sname != NULL);
62 MD5Init(&ctx);
63 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
64 sizeof(sbuf->st_ex_dev));
65 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
66 sizeof(sbuf->st_ex_ino));
67 MD5Update(&ctx, (unsigned char *)upper_sname,
68 talloc_get_size(upper_sname)-1);
69 MD5Final(hash, &ctx);
71 TALLOC_FREE(upper_sname);
73 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
74 memcpy(&result, hash, sizeof(result));
76 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
78 return result;
81 static ssize_t get_xattr_size(connection_struct *conn,
82 const struct smb_filename *smb_fname,
83 const char *xattr_name)
85 NTSTATUS status;
86 struct ea_struct ea;
87 ssize_t result;
89 status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
90 xattr_name, &ea);
92 if (!NT_STATUS_IS_OK(status)) {
93 return -1;
96 result = ea.value.length-1;
97 TALLOC_FREE(ea.value.data);
98 return result;
102 * Given a stream name, populate xattr_name with the xattr name to use for
103 * accessing the stream.
105 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
106 TALLOC_CTX *ctx,
107 const char *stream_name,
108 char **xattr_name)
110 char *sname;
111 char *stype;
112 struct streams_xattr_config *config;
114 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
115 return NT_STATUS_UNSUCCESSFUL);
117 sname = talloc_strdup(ctx, stream_name + 1);
118 if (sname == NULL) {
119 return NT_STATUS_NO_MEMORY;
123 * With vfs_fruit option "fruit:encoding = native" we're
124 * already converting stream names that contain illegal NTFS
125 * characters from their on-the-wire Unicode Private Range
126 * encoding to their native ASCII representation.
128 * As as result the name of xattrs storing the streams (via
129 * vfs_streams_xattr) may contain a colon, so we have to use
130 * strrchr_m() instead of strchr_m() for matching the stream
131 * type suffix.
133 * In check_path_syntax() we've already ensured the streamname
134 * we got from the client is valid.
136 stype = strrchr_m(sname, ':');
138 if (stype) {
140 * We only support one stream type: "$DATA"
142 if (strcasecmp_m(stype, ":$DATA") != 0) {
143 talloc_free(sname);
144 return NT_STATUS_INVALID_PARAMETER;
147 /* Split name and type */
148 stype[0] = '\0';
151 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
152 config->prefix,
153 sname,
154 config->store_stream_type ? ":$DATA" : "");
155 if (*xattr_name == NULL) {
156 talloc_free(sname);
157 return NT_STATUS_NO_MEMORY;
160 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
161 stream_name));
163 talloc_free(sname);
164 return NT_STATUS_OK;
167 static bool streams_xattr_recheck(struct stream_io *sio)
169 NTSTATUS status;
170 char *xattr_name = NULL;
172 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
173 return true;
176 if (sio->fsp->fsp_name->stream_name == NULL) {
177 /* how can this happen */
178 errno = EINVAL;
179 return false;
182 status = streams_xattr_get_name(sio->handle, talloc_tos(),
183 sio->fsp->fsp_name->stream_name,
184 &xattr_name);
185 if (!NT_STATUS_IS_OK(status)) {
186 return false;
189 TALLOC_FREE(sio->xattr_name);
190 TALLOC_FREE(sio->base);
191 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
192 xattr_name);
193 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
194 sio->fsp->fsp_name->base_name);
195 sio->fsp_name_ptr = sio->fsp->fsp_name;
197 TALLOC_FREE(xattr_name);
199 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
200 return false;
203 return true;
207 * Helper to stat/lstat the base file of an smb_fname.
209 static int streams_xattr_stat_base(vfs_handle_struct *handle,
210 struct smb_filename *smb_fname,
211 bool follow_links)
213 char *tmp_stream_name;
214 int result;
216 tmp_stream_name = smb_fname->stream_name;
217 smb_fname->stream_name = NULL;
218 if (follow_links) {
219 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
220 } else {
221 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
223 smb_fname->stream_name = tmp_stream_name;
224 return result;
227 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
228 SMB_STRUCT_STAT *sbuf)
230 struct smb_filename *smb_fname_base = NULL;
231 int ret = -1;
232 struct stream_io *io = (struct stream_io *)
233 VFS_FETCH_FSP_EXTENSION(handle, fsp);
235 if (io == NULL || fsp->base_fsp == NULL) {
236 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
239 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
241 if (!streams_xattr_recheck(io)) {
242 return -1;
245 /* Create an smb_filename with stream_name == NULL. */
246 smb_fname_base = synthetic_smb_fname(talloc_tos(),
247 io->base,
248 NULL,
249 NULL,
250 fsp->fsp_name->flags);
251 if (smb_fname_base == NULL) {
252 errno = ENOMEM;
253 return -1;
256 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
257 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
258 } else {
259 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
261 *sbuf = smb_fname_base->st;
263 if (ret == -1) {
264 TALLOC_FREE(smb_fname_base);
265 return -1;
268 sbuf->st_ex_size = get_xattr_size(handle->conn,
269 smb_fname_base, io->xattr_name);
270 if (sbuf->st_ex_size == -1) {
271 TALLOC_FREE(smb_fname_base);
272 SET_STAT_INVALID(*sbuf);
273 return -1;
276 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
278 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
279 sbuf->st_ex_mode &= ~S_IFMT;
280 sbuf->st_ex_mode |= S_IFREG;
281 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
283 TALLOC_FREE(smb_fname_base);
284 return 0;
287 static int streams_xattr_stat(vfs_handle_struct *handle,
288 struct smb_filename *smb_fname)
290 NTSTATUS status;
291 int result = -1;
292 char *xattr_name = NULL;
294 if (!is_ntfs_stream_smb_fname(smb_fname)) {
295 return SMB_VFS_NEXT_STAT(handle, smb_fname);
298 /* Note if lp_posix_paths() is true, we can never
299 * get here as is_ntfs_stream_smb_fname() is
300 * always false. So we never need worry about
301 * not following links here. */
303 /* If the default stream is requested, just stat the base file. */
304 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
305 return streams_xattr_stat_base(handle, smb_fname, true);
308 /* Populate the stat struct with info from the base file. */
309 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
310 return -1;
313 /* Derive the xattr name to lookup. */
314 status = streams_xattr_get_name(handle, talloc_tos(),
315 smb_fname->stream_name, &xattr_name);
316 if (!NT_STATUS_IS_OK(status)) {
317 errno = map_errno_from_nt_status(status);
318 return -1;
321 /* Augment the base file's stat information before returning. */
322 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
323 smb_fname,
324 xattr_name);
325 if (smb_fname->st.st_ex_size == -1) {
326 SET_STAT_INVALID(smb_fname->st);
327 errno = ENOENT;
328 result = -1;
329 goto fail;
332 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
333 smb_fname->st.st_ex_mode &= ~S_IFMT;
334 smb_fname->st.st_ex_mode |= S_IFREG;
335 smb_fname->st.st_ex_blocks =
336 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
338 result = 0;
339 fail:
340 TALLOC_FREE(xattr_name);
341 return result;
344 static int streams_xattr_lstat(vfs_handle_struct *handle,
345 struct smb_filename *smb_fname)
347 NTSTATUS status;
348 int result = -1;
349 char *xattr_name = NULL;
351 if (!is_ntfs_stream_smb_fname(smb_fname)) {
352 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
355 /* If the default stream is requested, just stat the base file. */
356 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
357 return streams_xattr_stat_base(handle, smb_fname, false);
360 /* Populate the stat struct with info from the base file. */
361 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
362 return -1;
365 /* Derive the xattr name to lookup. */
366 status = streams_xattr_get_name(handle, talloc_tos(),
367 smb_fname->stream_name, &xattr_name);
368 if (!NT_STATUS_IS_OK(status)) {
369 errno = map_errno_from_nt_status(status);
370 return -1;
373 /* Augment the base file's stat information before returning. */
374 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
375 smb_fname,
376 xattr_name);
377 if (smb_fname->st.st_ex_size == -1) {
378 SET_STAT_INVALID(smb_fname->st);
379 errno = ENOENT;
380 result = -1;
381 goto fail;
384 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
385 smb_fname->st.st_ex_mode &= ~S_IFMT;
386 smb_fname->st.st_ex_mode |= S_IFREG;
387 smb_fname->st.st_ex_blocks =
388 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
390 result = 0;
392 fail:
393 TALLOC_FREE(xattr_name);
394 return result;
397 static int streams_xattr_open(vfs_handle_struct *handle,
398 struct smb_filename *smb_fname,
399 files_struct *fsp, int flags, mode_t mode)
401 NTSTATUS status;
402 struct streams_xattr_config *config = NULL;
403 struct stream_io *sio = NULL;
404 struct ea_struct ea;
405 char *xattr_name = NULL;
406 int pipe_fds[2];
407 int fakefd = -1;
408 int ret;
410 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
411 return -1);
413 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
414 smb_fname_str_dbg(smb_fname), flags));
416 if (!is_ntfs_stream_smb_fname(smb_fname)) {
417 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
420 /* If the default stream is requested, just open the base file. */
421 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
422 char *tmp_stream_name;
424 tmp_stream_name = smb_fname->stream_name;
425 smb_fname->stream_name = NULL;
427 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
429 smb_fname->stream_name = tmp_stream_name;
431 return ret;
434 status = streams_xattr_get_name(handle, talloc_tos(),
435 smb_fname->stream_name, &xattr_name);
436 if (!NT_STATUS_IS_OK(status)) {
437 errno = map_errno_from_nt_status(status);
438 goto fail;
442 * Return a valid fd, but ensure any attempt to use it returns an error
443 * (EPIPE).
445 ret = pipe(pipe_fds);
446 if (ret != 0) {
447 goto fail;
450 close(pipe_fds[1]);
451 pipe_fds[1] = -1;
452 fakefd = pipe_fds[0];
454 status = get_ea_value(talloc_tos(), handle->conn, NULL,
455 smb_fname, xattr_name, &ea);
457 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
459 if (!NT_STATUS_IS_OK(status)
460 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
462 * The base file is not there. This is an error even if we got
463 * O_CREAT, the higher levels should have created the base
464 * file for us.
466 DEBUG(10, ("streams_xattr_open: base file %s not around, "
467 "returning ENOENT\n", smb_fname->base_name));
468 errno = ENOENT;
469 goto fail;
472 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
473 (flags & O_TRUNC)) {
475 * The attribute does not exist or needs to be truncated
479 * Darn, xattrs need at least 1 byte
481 char null = '\0';
483 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
484 xattr_name, smb_fname->base_name));
486 ret = SMB_VFS_SETXATTR(fsp->conn,
487 smb_fname,
488 xattr_name,
489 &null, sizeof(null),
490 flags & O_EXCL ? XATTR_CREATE : 0);
491 if (ret != 0) {
492 goto fail;
496 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
497 if (sio == NULL) {
498 errno = ENOMEM;
499 goto fail;
502 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
503 xattr_name);
505 * so->base needs to be a copy of fsp->fsp_name->base_name,
506 * making it identical to streams_xattr_recheck(). If the
507 * open is changing directories, fsp->fsp_name->base_name
508 * will be the full path from the share root, whilst
509 * smb_fname will be relative to the $cwd.
511 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
512 fsp->fsp_name->base_name);
513 sio->fsp_name_ptr = fsp->fsp_name;
514 sio->handle = handle;
515 sio->fsp = fsp;
517 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
518 errno = ENOMEM;
519 goto fail;
522 return fakefd;
524 fail:
525 if (fakefd >= 0) {
526 close(fakefd);
527 fakefd = -1;
530 return -1;
533 static int streams_xattr_unlink(vfs_handle_struct *handle,
534 const struct smb_filename *smb_fname)
536 NTSTATUS status;
537 int ret = -1;
538 char *xattr_name = NULL;
540 if (!is_ntfs_stream_smb_fname(smb_fname)) {
541 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
544 /* If the default stream is requested, just open the base file. */
545 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
546 struct smb_filename *smb_fname_base = NULL;
548 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
549 if (smb_fname_base == NULL) {
550 errno = ENOMEM;
551 return -1;
554 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
556 TALLOC_FREE(smb_fname_base);
557 return ret;
560 status = streams_xattr_get_name(handle, talloc_tos(),
561 smb_fname->stream_name, &xattr_name);
562 if (!NT_STATUS_IS_OK(status)) {
563 errno = map_errno_from_nt_status(status);
564 goto fail;
567 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
569 if ((ret == -1) && (errno == ENOATTR)) {
570 errno = ENOENT;
571 goto fail;
574 ret = 0;
576 fail:
577 TALLOC_FREE(xattr_name);
578 return ret;
581 static int streams_xattr_rename(vfs_handle_struct *handle,
582 const struct smb_filename *smb_fname_src,
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_RENAME(handle, smb_fname_src,
599 smb_fname_dst);
602 /* For now don't allow renames from or to the default stream. */
603 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
604 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
605 errno = ENOSYS;
606 goto done;
609 /* Don't rename if the streams are identical. */
610 if (strcasecmp_m(smb_fname_src->stream_name,
611 smb_fname_dst->stream_name) == 0) {
612 goto done;
615 /* Get the xattr names. */
616 status = streams_xattr_get_name(handle, talloc_tos(),
617 smb_fname_src->stream_name,
618 &src_xattr_name);
619 if (!NT_STATUS_IS_OK(status)) {
620 errno = map_errno_from_nt_status(status);
621 goto fail;
623 status = streams_xattr_get_name(handle, talloc_tos(),
624 smb_fname_dst->stream_name,
625 &dst_xattr_name);
626 if (!NT_STATUS_IS_OK(status)) {
627 errno = map_errno_from_nt_status(status);
628 goto fail;
631 /* read the old stream */
632 status = get_ea_value(talloc_tos(), handle->conn, NULL,
633 smb_fname_src, src_xattr_name, &ea);
634 if (!NT_STATUS_IS_OK(status)) {
635 errno = ENOENT;
636 goto fail;
639 /* (over)write the new stream */
640 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
641 dst_xattr_name, ea.value.data, ea.value.length,
643 if (nret < 0) {
644 if (errno == ENOATTR) {
645 errno = ENOENT;
647 goto fail;
650 /* remove the old stream */
651 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
652 src_xattr_name);
653 if (oret < 0) {
654 if (errno == ENOATTR) {
655 errno = ENOENT;
657 goto fail;
660 done:
661 errno = 0;
662 ret = 0;
663 fail:
664 TALLOC_FREE(src_xattr_name);
665 TALLOC_FREE(dst_xattr_name);
666 return ret;
669 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
670 files_struct *fsp,
671 const struct smb_filename *smb_fname,
672 bool (*fn)(struct ea_struct *ea,
673 void *private_data),
674 void *private_data)
676 NTSTATUS status;
677 char **names;
678 size_t i, num_names;
679 struct streams_xattr_config *config;
681 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
682 return NT_STATUS_UNSUCCESSFUL);
684 status = get_ea_names_from_file(talloc_tos(),
685 handle->conn,
686 fsp,
687 smb_fname,
688 &names,
689 &num_names);
690 if (!NT_STATUS_IS_OK(status)) {
691 return status;
694 for (i=0; i<num_names; i++) {
695 struct ea_struct ea;
698 * We want to check with samba_private_attr_name()
699 * whether the xattr name is a private one,
700 * unfortunately it flags xattrs that begin with the
701 * default streams prefix as private.
703 * By only calling samba_private_attr_name() in case
704 * the xattr does NOT begin with the default prefix,
705 * we know that if it returns 'true' it definitely one
706 * of our internal xattr like "user.DOSATTRIB".
708 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
709 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
710 if (samba_private_attr_name(names[i])) {
711 continue;
715 if (strncmp(names[i], config->prefix,
716 config->prefix_len) != 0) {
717 continue;
720 status = get_ea_value(names,
721 handle->conn,
722 NULL,
723 smb_fname,
724 names[i],
725 &ea);
726 if (!NT_STATUS_IS_OK(status)) {
727 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
728 names[i],
729 smb_fname->base_name,
730 nt_errstr(status)));
731 continue;
734 ea.name = talloc_asprintf(
735 ea.value.data, ":%s%s",
736 names[i] + config->prefix_len,
737 config->store_stream_type ? "" : ":$DATA");
738 if (ea.name == NULL) {
739 DEBUG(0, ("talloc failed\n"));
740 continue;
743 if (!fn(&ea, private_data)) {
744 TALLOC_FREE(ea.value.data);
745 return NT_STATUS_OK;
748 TALLOC_FREE(ea.value.data);
751 TALLOC_FREE(names);
752 return NT_STATUS_OK;
755 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
756 struct stream_struct **streams,
757 const char *name, off_t size,
758 off_t alloc_size)
760 struct stream_struct *tmp;
762 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
763 (*num_streams)+1);
764 if (tmp == NULL) {
765 return false;
768 tmp[*num_streams].name = talloc_strdup(tmp, name);
769 if (tmp[*num_streams].name == NULL) {
770 return false;
773 tmp[*num_streams].size = size;
774 tmp[*num_streams].alloc_size = alloc_size;
776 *streams = tmp;
777 *num_streams += 1;
778 return true;
781 struct streaminfo_state {
782 TALLOC_CTX *mem_ctx;
783 vfs_handle_struct *handle;
784 unsigned int num_streams;
785 struct stream_struct *streams;
786 NTSTATUS status;
789 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
791 struct streaminfo_state *state =
792 (struct streaminfo_state *)private_data;
794 if (!add_one_stream(state->mem_ctx,
795 &state->num_streams, &state->streams,
796 ea->name, ea->value.length-1,
797 smb_roundup(state->handle->conn,
798 ea->value.length-1))) {
799 state->status = NT_STATUS_NO_MEMORY;
800 return false;
803 return true;
806 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
807 struct files_struct *fsp,
808 const struct smb_filename *smb_fname,
809 TALLOC_CTX *mem_ctx,
810 unsigned int *pnum_streams,
811 struct stream_struct **pstreams)
813 SMB_STRUCT_STAT sbuf;
814 int ret;
815 NTSTATUS status;
816 struct streaminfo_state state;
818 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
819 if (ret == -1) {
820 return map_nt_error_from_unix(errno);
823 state.streams = *pstreams;
824 state.num_streams = *pnum_streams;
825 state.mem_ctx = mem_ctx;
826 state.handle = handle;
827 state.status = NT_STATUS_OK;
829 if (S_ISLNK(sbuf.st_ex_mode)) {
831 * Currently we do't have SMB_VFS_LLISTXATTR
832 * inside the VFS which means there's no way
833 * to cope with a symlink when lp_posix_pathnames().
834 * returns true. For now ignore links.
835 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
837 status = NT_STATUS_OK;
838 } else {
839 status = walk_xattr_streams(handle, fsp, smb_fname,
840 collect_one_stream, &state);
843 if (!NT_STATUS_IS_OK(status)) {
844 TALLOC_FREE(state.streams);
845 return status;
848 if (!NT_STATUS_IS_OK(state.status)) {
849 TALLOC_FREE(state.streams);
850 return state.status;
853 *pnum_streams = state.num_streams;
854 *pstreams = state.streams;
856 return SMB_VFS_NEXT_STREAMINFO(handle,
857 fsp,
858 smb_fname,
859 mem_ctx,
860 pnum_streams,
861 pstreams);
864 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
865 enum timestamp_set_resolution *p_ts_res)
867 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
870 static int streams_xattr_connect(vfs_handle_struct *handle,
871 const char *service, const char *user)
873 struct streams_xattr_config *config;
874 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
875 const char *prefix;
876 int rc;
878 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
879 if (rc != 0) {
880 return rc;
883 config = talloc_zero(handle->conn, struct streams_xattr_config);
884 if (config == NULL) {
885 DEBUG(1, ("talloc_zero() failed\n"));
886 errno = ENOMEM;
887 return -1;
890 prefix = lp_parm_const_string(SNUM(handle->conn),
891 "streams_xattr", "prefix",
892 default_prefix);
893 config->prefix = talloc_strdup(config, prefix);
894 if (config->prefix == NULL) {
895 DEBUG(1, ("talloc_strdup() failed\n"));
896 errno = ENOMEM;
897 return -1;
899 config->prefix_len = strlen(config->prefix);
900 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
902 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
903 "streams_xattr",
904 "store_stream_type",
905 true);
907 SMB_VFS_HANDLE_SET_DATA(handle, config,
908 NULL, struct stream_xattr_config,
909 return -1);
911 return 0;
914 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
915 files_struct *fsp, const void *data,
916 size_t n, off_t offset)
918 struct stream_io *sio =
919 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
920 struct ea_struct ea;
921 NTSTATUS status;
922 struct smb_filename *smb_fname_base = NULL;
923 int ret;
925 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
927 if (sio == NULL) {
928 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
931 if (!streams_xattr_recheck(sio)) {
932 return -1;
935 /* Create an smb_filename with stream_name == NULL. */
936 smb_fname_base = synthetic_smb_fname(talloc_tos(),
937 sio->base,
938 NULL,
939 NULL,
940 fsp->fsp_name->flags);
941 if (smb_fname_base == NULL) {
942 errno = ENOMEM;
943 return -1;
946 status = get_ea_value(talloc_tos(), handle->conn, NULL,
947 smb_fname_base, sio->xattr_name, &ea);
948 if (!NT_STATUS_IS_OK(status)) {
949 return -1;
952 if ((offset + n) > ea.value.length-1) {
953 uint8_t *tmp;
955 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
956 offset + n + 1);
958 if (tmp == NULL) {
959 TALLOC_FREE(ea.value.data);
960 errno = ENOMEM;
961 return -1;
963 ea.value.data = tmp;
964 ea.value.length = offset + n + 1;
965 ea.value.data[offset+n] = 0;
968 memcpy(ea.value.data + offset, data, n);
970 ret = SMB_VFS_SETXATTR(fsp->conn,
971 fsp->fsp_name,
972 sio->xattr_name,
973 ea.value.data, ea.value.length, 0);
974 TALLOC_FREE(ea.value.data);
976 if (ret == -1) {
977 return -1;
980 return n;
983 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
984 files_struct *fsp, void *data,
985 size_t n, off_t offset)
987 struct stream_io *sio =
988 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
989 struct ea_struct ea;
990 NTSTATUS status;
991 size_t length, overlap;
992 struct smb_filename *smb_fname_base = NULL;
994 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
995 (int)offset, (int)n));
997 if (sio == NULL) {
998 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1001 if (!streams_xattr_recheck(sio)) {
1002 return -1;
1005 /* Create an smb_filename with stream_name == NULL. */
1006 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1007 sio->base,
1008 NULL,
1009 NULL,
1010 fsp->fsp_name->flags);
1011 if (smb_fname_base == NULL) {
1012 errno = ENOMEM;
1013 return -1;
1016 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1017 smb_fname_base, sio->xattr_name, &ea);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 return -1;
1022 length = ea.value.length-1;
1024 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1025 (int)length));
1027 /* Attempt to read past EOF. */
1028 if (length <= offset) {
1029 return 0;
1032 overlap = (offset + n) > length ? (length - offset) : n;
1033 memcpy(data, ea.value.data + offset, overlap);
1035 TALLOC_FREE(ea.value.data);
1036 return overlap;
1039 struct streams_xattr_pread_state {
1040 ssize_t nread;
1041 struct vfs_aio_state vfs_aio_state;
1044 static void streams_xattr_pread_done(struct tevent_req *subreq);
1046 static struct tevent_req *streams_xattr_pread_send(
1047 struct vfs_handle_struct *handle,
1048 TALLOC_CTX *mem_ctx,
1049 struct tevent_context *ev,
1050 struct files_struct *fsp,
1051 void *data,
1052 size_t n, off_t offset)
1054 struct tevent_req *req = NULL;
1055 struct tevent_req *subreq = NULL;
1056 struct streams_xattr_pread_state *state = NULL;
1057 struct stream_io *sio =
1058 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1060 req = tevent_req_create(mem_ctx, &state,
1061 struct streams_xattr_pread_state);
1062 if (req == NULL) {
1063 return NULL;
1066 if (sio == NULL) {
1067 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1068 data, n, offset);
1069 if (tevent_req_nomem(req, subreq)) {
1070 return tevent_req_post(req, ev);
1072 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1073 return req;
1076 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1077 if (state->nread != n) {
1078 if (state->nread != -1) {
1079 errno = EIO;
1081 tevent_req_error(req, errno);
1082 return tevent_req_post(req, ev);
1085 tevent_req_done(req);
1086 return tevent_req_post(req, ev);
1089 static void streams_xattr_pread_done(struct tevent_req *subreq)
1091 struct tevent_req *req = tevent_req_callback_data(
1092 subreq, struct tevent_req);
1093 struct streams_xattr_pread_state *state = tevent_req_data(
1094 req, struct streams_xattr_pread_state);
1096 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1097 TALLOC_FREE(subreq);
1099 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1100 return;
1102 tevent_req_done(req);
1105 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1106 struct vfs_aio_state *vfs_aio_state)
1108 struct streams_xattr_pread_state *state = tevent_req_data(
1109 req, struct streams_xattr_pread_state);
1111 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1112 return -1;
1115 *vfs_aio_state = state->vfs_aio_state;
1116 return state->nread;
1119 struct streams_xattr_pwrite_state {
1120 ssize_t nwritten;
1121 struct vfs_aio_state vfs_aio_state;
1124 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1126 static struct tevent_req *streams_xattr_pwrite_send(
1127 struct vfs_handle_struct *handle,
1128 TALLOC_CTX *mem_ctx,
1129 struct tevent_context *ev,
1130 struct files_struct *fsp,
1131 const void *data,
1132 size_t n, off_t offset)
1134 struct tevent_req *req = NULL;
1135 struct tevent_req *subreq = NULL;
1136 struct streams_xattr_pwrite_state *state = NULL;
1137 struct stream_io *sio =
1138 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1140 req = tevent_req_create(mem_ctx, &state,
1141 struct streams_xattr_pwrite_state);
1142 if (req == NULL) {
1143 return NULL;
1146 if (sio == NULL) {
1147 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1148 data, n, offset);
1149 if (tevent_req_nomem(req, subreq)) {
1150 return tevent_req_post(req, ev);
1152 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1153 return req;
1156 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1157 if (state->nwritten != n) {
1158 if (state->nwritten != -1) {
1159 errno = EIO;
1161 tevent_req_error(req, errno);
1162 return tevent_req_post(req, ev);
1165 tevent_req_done(req);
1166 return tevent_req_post(req, ev);
1169 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1171 struct tevent_req *req = tevent_req_callback_data(
1172 subreq, struct tevent_req);
1173 struct streams_xattr_pwrite_state *state = tevent_req_data(
1174 req, struct streams_xattr_pwrite_state);
1176 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1177 TALLOC_FREE(subreq);
1179 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1180 return;
1182 tevent_req_done(req);
1185 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1186 struct vfs_aio_state *vfs_aio_state)
1188 struct streams_xattr_pwrite_state *state = tevent_req_data(
1189 req, struct streams_xattr_pwrite_state);
1191 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1192 return -1;
1195 *vfs_aio_state = state->vfs_aio_state;
1196 return state->nwritten;
1199 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1200 struct files_struct *fsp,
1201 off_t offset)
1203 int ret;
1204 uint8_t *tmp;
1205 struct ea_struct ea;
1206 NTSTATUS status;
1207 struct stream_io *sio =
1208 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1209 struct smb_filename *smb_fname_base = NULL;
1211 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1212 fsp_str_dbg(fsp), (double)offset));
1214 if (sio == NULL) {
1215 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1218 if (!streams_xattr_recheck(sio)) {
1219 return -1;
1222 /* Create an smb_filename with stream_name == NULL. */
1223 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1224 sio->base,
1225 NULL,
1226 NULL,
1227 fsp->fsp_name->flags);
1228 if (smb_fname_base == NULL) {
1229 errno = ENOMEM;
1230 return -1;
1233 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1234 smb_fname_base, sio->xattr_name, &ea);
1235 if (!NT_STATUS_IS_OK(status)) {
1236 return -1;
1239 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1240 offset + 1);
1242 if (tmp == NULL) {
1243 TALLOC_FREE(ea.value.data);
1244 errno = ENOMEM;
1245 return -1;
1248 /* Did we expand ? */
1249 if (ea.value.length < offset + 1) {
1250 memset(&tmp[ea.value.length], '\0',
1251 offset + 1 - ea.value.length);
1254 ea.value.data = tmp;
1255 ea.value.length = offset + 1;
1256 ea.value.data[offset] = 0;
1258 ret = SMB_VFS_SETXATTR(fsp->conn,
1259 fsp->fsp_name,
1260 sio->xattr_name,
1261 ea.value.data, ea.value.length, 0);
1262 TALLOC_FREE(ea.value.data);
1264 if (ret == -1) {
1265 return -1;
1268 return 0;
1271 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1272 struct files_struct *fsp,
1273 uint32_t mode,
1274 off_t offset,
1275 off_t len)
1277 struct stream_io *sio =
1278 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1280 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1281 "len = %.0f\n",
1282 fsp_str_dbg(fsp), (double)offset, (double)len));
1284 if (sio == NULL) {
1285 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1288 if (!streams_xattr_recheck(sio)) {
1289 return -1;
1292 /* Let the pwrite code path handle it. */
1293 errno = ENOSYS;
1294 return -1;
1297 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1298 uid_t uid, gid_t gid)
1300 struct stream_io *sio =
1301 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1303 if (sio == NULL) {
1304 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1307 return 0;
1310 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1311 files_struct *fsp,
1312 mode_t mode)
1314 struct stream_io *sio =
1315 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1317 if (sio == NULL) {
1318 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1321 return 0;
1324 static int streams_xattr_fsync(vfs_handle_struct *handle, files_struct *fsp)
1326 struct stream_io *sio =
1327 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1329 if (sio == NULL) {
1330 return SMB_VFS_NEXT_FSYNC(handle, fsp);
1333 return 0;
1336 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1337 struct files_struct *fsp,
1338 const char *name,
1339 void *value,
1340 size_t size)
1342 struct stream_io *sio =
1343 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1345 if (sio == NULL) {
1346 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1349 errno = ENOTSUP;
1350 return -1;
1353 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1354 struct files_struct *fsp,
1355 char *list,
1356 size_t size)
1358 struct stream_io *sio =
1359 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1361 if (sio == NULL) {
1362 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1365 errno = ENOTSUP;
1366 return -1;
1369 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1370 struct files_struct *fsp,
1371 const char *name)
1373 struct stream_io *sio =
1374 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1376 if (sio == NULL) {
1377 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1380 errno = ENOTSUP;
1381 return -1;
1384 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1385 struct files_struct *fsp,
1386 const char *name,
1387 const void *value,
1388 size_t size,
1389 int flags)
1391 struct stream_io *sio =
1392 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1394 if (sio == NULL) {
1395 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1396 size, flags);
1399 errno = ENOTSUP;
1400 return -1;
1403 static int streams_xattr_fchmod_acl(vfs_handle_struct *handle,
1404 files_struct *fsp,
1405 mode_t mode)
1407 struct stream_io *sio =
1408 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1410 if (sio == NULL) {
1411 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
1414 return 0;
1417 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1418 files_struct *fsp,
1419 TALLOC_CTX *mem_ctx)
1421 struct stream_io *sio =
1422 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1424 if (sio == NULL) {
1425 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1428 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1429 handle, fsp->base_fsp->fsp_name,
1430 SMB_ACL_TYPE_ACCESS, mem_ctx);
1433 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1434 files_struct *fsp,
1435 SMB_ACL_T theacl)
1437 struct stream_io *sio =
1438 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1440 if (sio == NULL) {
1441 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1444 return 0;
1447 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1448 files_struct *fsp,
1449 TALLOC_CTX *mem_ctx,
1450 char **blob_description,
1451 DATA_BLOB *blob)
1453 struct stream_io *sio =
1454 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1456 if (sio == NULL) {
1457 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1458 blob_description, blob);
1461 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1462 handle, fsp->base_fsp->fsp_name, mem_ctx,
1463 blob_description, blob);
1466 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1467 files_struct *fsp,
1468 uint32_t security_info,
1469 TALLOC_CTX *mem_ctx,
1470 struct security_descriptor **ppdesc)
1472 struct stream_io *sio =
1473 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1475 if (sio == NULL) {
1476 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1477 mem_ctx, ppdesc);
1480 return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1481 security_info, mem_ctx, ppdesc);
1484 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1485 files_struct *fsp,
1486 uint32_t security_info_sent,
1487 const struct security_descriptor *psd)
1489 struct stream_io *sio =
1490 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1492 if (sio == NULL) {
1493 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1494 security_info_sent, psd);
1497 return NT_STATUS_OK;
1500 struct streams_xattr_fsync_state {
1501 int ret;
1502 struct vfs_aio_state vfs_aio_state;
1505 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1507 static struct tevent_req *streams_xattr_fsync_send(
1508 struct vfs_handle_struct *handle,
1509 TALLOC_CTX *mem_ctx,
1510 struct tevent_context *ev,
1511 struct files_struct *fsp)
1513 struct tevent_req *req = NULL;
1514 struct tevent_req *subreq = NULL;
1515 struct streams_xattr_fsync_state *state = NULL;
1516 struct stream_io *sio =
1517 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1519 req = tevent_req_create(mem_ctx, &state,
1520 struct streams_xattr_fsync_state);
1521 if (req == NULL) {
1522 return NULL;
1525 if (sio == NULL) {
1526 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1527 if (tevent_req_nomem(req, subreq)) {
1528 return tevent_req_post(req, ev);
1530 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1531 return req;
1535 * There's no pathname based sync variant and we don't have access to
1536 * the basefile handle, so we can't do anything here.
1539 tevent_req_done(req);
1540 return tevent_req_post(req, ev);
1543 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1545 struct tevent_req *req = tevent_req_callback_data(
1546 subreq, struct tevent_req);
1547 struct streams_xattr_fsync_state *state = tevent_req_data(
1548 req, struct streams_xattr_fsync_state);
1550 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1551 TALLOC_FREE(subreq);
1552 if (state->ret != 0) {
1553 tevent_req_error(req, errno);
1554 return;
1557 tevent_req_done(req);
1560 static int streams_xattr_fsync_recv(struct tevent_req *req,
1561 struct vfs_aio_state *vfs_aio_state)
1563 struct streams_xattr_fsync_state *state = tevent_req_data(
1564 req, struct streams_xattr_fsync_state);
1566 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1567 return -1;
1570 *vfs_aio_state = state->vfs_aio_state;
1571 return state->ret;
1574 static bool streams_xattr_lock(vfs_handle_struct *handle,
1575 files_struct *fsp,
1576 int op,
1577 off_t offset,
1578 off_t count,
1579 int type)
1581 struct stream_io *sio =
1582 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1584 if (sio == NULL) {
1585 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1588 return true;
1591 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1592 files_struct *fsp,
1593 off_t *poffset,
1594 off_t *pcount,
1595 int *ptype,
1596 pid_t *ppid)
1598 struct stream_io *sio =
1599 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1601 if (sio == NULL) {
1602 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1603 pcount, ptype, ppid);
1606 errno = ENOTSUP;
1607 return false;
1610 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1611 files_struct *fsp,
1612 uint32_t share_mode,
1613 uint32_t access_mask)
1615 struct stream_io *sio =
1616 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1618 if (sio == NULL) {
1619 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1620 share_mode, access_mask);
1623 return 0;
1626 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1627 files_struct *fsp,
1628 int leasetype)
1630 struct stream_io *sio =
1631 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1633 if (sio == NULL) {
1634 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1637 return 0;
1640 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1641 files_struct *fsp,
1642 struct lock_struct *plock)
1644 struct stream_io *sio =
1645 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1647 if (sio == NULL) {
1648 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1651 return true;
1654 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1655 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1656 .connect_fn = streams_xattr_connect,
1657 .open_fn = streams_xattr_open,
1658 .stat_fn = streams_xattr_stat,
1659 .fstat_fn = streams_xattr_fstat,
1660 .lstat_fn = streams_xattr_lstat,
1661 .pread_fn = streams_xattr_pread,
1662 .pwrite_fn = streams_xattr_pwrite,
1663 .pread_send_fn = streams_xattr_pread_send,
1664 .pread_recv_fn = streams_xattr_pread_recv,
1665 .pwrite_send_fn = streams_xattr_pwrite_send,
1666 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1667 .unlink_fn = streams_xattr_unlink,
1668 .rename_fn = streams_xattr_rename,
1669 .ftruncate_fn = streams_xattr_ftruncate,
1670 .fallocate_fn = streams_xattr_fallocate,
1671 .streaminfo_fn = streams_xattr_streaminfo,
1673 .fsync_send_fn = streams_xattr_fsync_send,
1674 .fsync_recv_fn = streams_xattr_fsync_recv,
1676 .lock_fn = streams_xattr_lock,
1677 .getlock_fn = streams_xattr_getlock,
1678 .kernel_flock_fn = streams_xattr_kernel_flock,
1679 .linux_setlease_fn = streams_xattr_linux_setlease,
1680 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1682 .fchown_fn = streams_xattr_fchown,
1683 .fchmod_fn = streams_xattr_fchmod,
1684 .fsync_fn = streams_xattr_fsync,
1686 .fgetxattr_fn = streams_xattr_fgetxattr,
1687 .flistxattr_fn = streams_xattr_flistxattr,
1688 .fremovexattr_fn = streams_xattr_fremovexattr,
1689 .fsetxattr_fn = streams_xattr_fsetxattr,
1691 .fchmod_acl_fn = streams_xattr_fchmod_acl,
1693 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1694 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1695 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1697 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1698 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1701 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *);
1702 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1704 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1705 &vfs_streams_xattr_fns);