s3:vfs: Initialize pid to 0 in test_netatalk_lock()
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blob213a836754190673b0ba8125d3e3df7051c80c34
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 if (sio->xattr_name == NULL) {
194 DBG_DEBUG("sio->xattr_name==NULL\n");
195 return false;
197 TALLOC_FREE(xattr_name);
199 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
200 sio->fsp->fsp_name->base_name);
201 if (sio->base == NULL) {
202 DBG_DEBUG("sio->base==NULL\n");
203 return false;
206 sio->fsp_name_ptr = sio->fsp->fsp_name;
208 return true;
212 * Helper to stat/lstat the base file of an smb_fname.
214 static int streams_xattr_stat_base(vfs_handle_struct *handle,
215 struct smb_filename *smb_fname,
216 bool follow_links)
218 char *tmp_stream_name;
219 int result;
221 tmp_stream_name = smb_fname->stream_name;
222 smb_fname->stream_name = NULL;
223 if (follow_links) {
224 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
225 } else {
226 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
228 smb_fname->stream_name = tmp_stream_name;
229 return result;
232 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
233 SMB_STRUCT_STAT *sbuf)
235 struct smb_filename *smb_fname_base = NULL;
236 int ret = -1;
237 struct stream_io *io = (struct stream_io *)
238 VFS_FETCH_FSP_EXTENSION(handle, fsp);
240 if (io == NULL || fsp->base_fsp == NULL) {
241 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
244 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
246 if (!streams_xattr_recheck(io)) {
247 return -1;
250 /* Create an smb_filename with stream_name == NULL. */
251 smb_fname_base = synthetic_smb_fname(talloc_tos(),
252 io->base,
253 NULL,
254 NULL,
255 fsp->fsp_name->flags);
256 if (smb_fname_base == NULL) {
257 errno = ENOMEM;
258 return -1;
261 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
262 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
263 } else {
264 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
266 *sbuf = smb_fname_base->st;
268 if (ret == -1) {
269 TALLOC_FREE(smb_fname_base);
270 return -1;
273 sbuf->st_ex_size = get_xattr_size(handle->conn,
274 smb_fname_base, io->xattr_name);
275 if (sbuf->st_ex_size == -1) {
276 TALLOC_FREE(smb_fname_base);
277 SET_STAT_INVALID(*sbuf);
278 return -1;
281 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
283 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
284 sbuf->st_ex_mode &= ~S_IFMT;
285 sbuf->st_ex_mode &= ~S_IFDIR;
286 sbuf->st_ex_mode |= S_IFREG;
287 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
289 TALLOC_FREE(smb_fname_base);
290 return 0;
293 static int streams_xattr_stat(vfs_handle_struct *handle,
294 struct smb_filename *smb_fname)
296 NTSTATUS status;
297 int result = -1;
298 char *xattr_name = NULL;
300 if (!is_ntfs_stream_smb_fname(smb_fname)) {
301 return SMB_VFS_NEXT_STAT(handle, smb_fname);
304 /* Note if lp_posix_paths() is true, we can never
305 * get here as is_ntfs_stream_smb_fname() is
306 * always false. So we never need worry about
307 * not following links here. */
309 /* If the default stream is requested, just stat the base file. */
310 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
311 return streams_xattr_stat_base(handle, smb_fname, true);
314 /* Populate the stat struct with info from the base file. */
315 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
316 return -1;
319 /* Derive the xattr name to lookup. */
320 status = streams_xattr_get_name(handle, talloc_tos(),
321 smb_fname->stream_name, &xattr_name);
322 if (!NT_STATUS_IS_OK(status)) {
323 errno = map_errno_from_nt_status(status);
324 return -1;
327 /* Augment the base file's stat information before returning. */
328 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
329 smb_fname,
330 xattr_name);
331 if (smb_fname->st.st_ex_size == -1) {
332 SET_STAT_INVALID(smb_fname->st);
333 errno = ENOENT;
334 result = -1;
335 goto fail;
338 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
339 smb_fname->st.st_ex_mode &= ~S_IFMT;
340 smb_fname->st.st_ex_mode &= ~S_IFDIR;
341 smb_fname->st.st_ex_mode |= S_IFREG;
342 smb_fname->st.st_ex_blocks =
343 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
345 result = 0;
346 fail:
347 TALLOC_FREE(xattr_name);
348 return result;
351 static int streams_xattr_lstat(vfs_handle_struct *handle,
352 struct smb_filename *smb_fname)
354 NTSTATUS status;
355 int result = -1;
356 char *xattr_name = NULL;
358 if (!is_ntfs_stream_smb_fname(smb_fname)) {
359 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
362 /* If the default stream is requested, just stat the base file. */
363 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
364 return streams_xattr_stat_base(handle, smb_fname, false);
367 /* Populate the stat struct with info from the base file. */
368 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
369 return -1;
372 /* Derive the xattr name to lookup. */
373 status = streams_xattr_get_name(handle, talloc_tos(),
374 smb_fname->stream_name, &xattr_name);
375 if (!NT_STATUS_IS_OK(status)) {
376 errno = map_errno_from_nt_status(status);
377 return -1;
380 /* Augment the base file's stat information before returning. */
381 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
382 smb_fname,
383 xattr_name);
384 if (smb_fname->st.st_ex_size == -1) {
385 SET_STAT_INVALID(smb_fname->st);
386 errno = ENOENT;
387 result = -1;
388 goto fail;
391 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
392 smb_fname->st.st_ex_mode &= ~S_IFMT;
393 smb_fname->st.st_ex_mode |= S_IFREG;
394 smb_fname->st.st_ex_blocks =
395 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
397 result = 0;
399 fail:
400 TALLOC_FREE(xattr_name);
401 return result;
404 static int streams_xattr_open(vfs_handle_struct *handle,
405 struct smb_filename *smb_fname,
406 files_struct *fsp, int flags, mode_t mode)
408 NTSTATUS status;
409 struct streams_xattr_config *config = NULL;
410 struct stream_io *sio = NULL;
411 struct ea_struct ea;
412 char *xattr_name = NULL;
413 int pipe_fds[2];
414 int fakefd = -1;
415 bool set_empty_xattr = false;
416 int ret;
418 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
419 return -1);
421 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
422 smb_fname_str_dbg(smb_fname), flags));
424 if (!is_ntfs_stream_smb_fname(smb_fname)) {
425 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
428 /* If the default stream is requested, just open the base file. */
429 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
430 char *tmp_stream_name;
432 tmp_stream_name = smb_fname->stream_name;
433 smb_fname->stream_name = NULL;
435 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
437 smb_fname->stream_name = tmp_stream_name;
439 return ret;
442 status = streams_xattr_get_name(handle, talloc_tos(),
443 smb_fname->stream_name, &xattr_name);
444 if (!NT_STATUS_IS_OK(status)) {
445 errno = map_errno_from_nt_status(status);
446 goto fail;
449 status = get_ea_value(talloc_tos(), handle->conn, NULL,
450 smb_fname, xattr_name, &ea);
452 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
454 if (!NT_STATUS_IS_OK(status)) {
455 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
457 * The base file is not there. This is an error even if
458 * we got O_CREAT, the higher levels should have created
459 * the base file for us.
461 DBG_DEBUG("streams_xattr_open: base file %s not around, "
462 "returning ENOENT\n", smb_fname->base_name);
463 errno = ENOENT;
464 goto fail;
467 if (!(flags & O_CREAT)) {
468 errno = ENOATTR;
469 goto fail;
472 set_empty_xattr = true;
475 if (flags & O_TRUNC) {
476 set_empty_xattr = true;
479 if (set_empty_xattr) {
481 * The attribute does not exist or needs to be truncated
485 * Darn, xattrs need at least 1 byte
487 char null = '\0';
489 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
490 xattr_name, smb_fname->base_name));
492 ret = SMB_VFS_SETXATTR(fsp->conn,
493 smb_fname,
494 xattr_name,
495 &null, sizeof(null),
496 flags & O_EXCL ? XATTR_CREATE : 0);
497 if (ret != 0) {
498 goto fail;
503 * Return a valid fd, but ensure any attempt to use it returns an error
504 * (EPIPE).
506 ret = pipe(pipe_fds);
507 if (ret != 0) {
508 goto fail;
511 close(pipe_fds[1]);
512 pipe_fds[1] = -1;
513 fakefd = pipe_fds[0];
515 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
516 if (sio == NULL) {
517 errno = ENOMEM;
518 goto fail;
521 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
522 xattr_name);
523 if (sio->xattr_name == NULL) {
524 errno = ENOMEM;
525 goto fail;
529 * so->base needs to be a copy of fsp->fsp_name->base_name,
530 * making it identical to streams_xattr_recheck(). If the
531 * open is changing directories, fsp->fsp_name->base_name
532 * will be the full path from the share root, whilst
533 * smb_fname will be relative to the $cwd.
535 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
536 fsp->fsp_name->base_name);
537 if (sio->base == NULL) {
538 errno = ENOMEM;
539 goto fail;
542 sio->fsp_name_ptr = fsp->fsp_name;
543 sio->handle = handle;
544 sio->fsp = fsp;
546 return fakefd;
548 fail:
549 if (fakefd >= 0) {
550 close(fakefd);
551 fakefd = -1;
554 return -1;
557 static int streams_xattr_close(vfs_handle_struct *handle,
558 files_struct *fsp)
560 int ret;
561 int fd;
563 fd = fsp->fh->fd;
565 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
566 smb_fname_str_dbg(fsp->fsp_name), fd);
568 if (!is_ntfs_stream_smb_fname(fsp->fsp_name)) {
569 return SMB_VFS_NEXT_CLOSE(handle, fsp);
572 if (is_ntfs_default_stream_smb_fname(fsp->fsp_name)) {
573 return SMB_VFS_NEXT_CLOSE(handle, fsp);
576 ret = close(fd);
577 fsp->fh->fd = -1;
579 return ret;
582 static int streams_xattr_unlink(vfs_handle_struct *handle,
583 const struct smb_filename *smb_fname)
585 NTSTATUS status;
586 int ret = -1;
587 char *xattr_name = NULL;
589 if (!is_ntfs_stream_smb_fname(smb_fname)) {
590 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
593 /* If the default stream is requested, just open the base file. */
594 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
595 struct smb_filename *smb_fname_base = NULL;
597 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
598 if (smb_fname_base == NULL) {
599 errno = ENOMEM;
600 return -1;
603 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
605 TALLOC_FREE(smb_fname_base);
606 return ret;
609 status = streams_xattr_get_name(handle, talloc_tos(),
610 smb_fname->stream_name, &xattr_name);
611 if (!NT_STATUS_IS_OK(status)) {
612 errno = map_errno_from_nt_status(status);
613 goto fail;
616 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
618 if ((ret == -1) && (errno == ENOATTR)) {
619 errno = ENOENT;
620 goto fail;
623 ret = 0;
625 fail:
626 TALLOC_FREE(xattr_name);
627 return ret;
630 static int streams_xattr_rename(vfs_handle_struct *handle,
631 const struct smb_filename *smb_fname_src,
632 const struct smb_filename *smb_fname_dst)
634 NTSTATUS status;
635 int ret = -1;
636 char *src_xattr_name = NULL;
637 char *dst_xattr_name = NULL;
638 bool src_is_stream, dst_is_stream;
639 ssize_t oret;
640 ssize_t nret;
641 struct ea_struct ea;
643 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
644 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
646 if (!src_is_stream && !dst_is_stream) {
647 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
648 smb_fname_dst);
651 /* For now don't allow renames from or to the default stream. */
652 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
653 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
654 errno = ENOSYS;
655 goto done;
658 /* Don't rename if the streams are identical. */
659 if (strcasecmp_m(smb_fname_src->stream_name,
660 smb_fname_dst->stream_name) == 0) {
661 goto done;
664 /* Get the xattr names. */
665 status = streams_xattr_get_name(handle, talloc_tos(),
666 smb_fname_src->stream_name,
667 &src_xattr_name);
668 if (!NT_STATUS_IS_OK(status)) {
669 errno = map_errno_from_nt_status(status);
670 goto fail;
672 status = streams_xattr_get_name(handle, talloc_tos(),
673 smb_fname_dst->stream_name,
674 &dst_xattr_name);
675 if (!NT_STATUS_IS_OK(status)) {
676 errno = map_errno_from_nt_status(status);
677 goto fail;
680 /* read the old stream */
681 status = get_ea_value(talloc_tos(), handle->conn, NULL,
682 smb_fname_src, src_xattr_name, &ea);
683 if (!NT_STATUS_IS_OK(status)) {
684 errno = ENOENT;
685 goto fail;
688 /* (over)write the new stream */
689 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
690 dst_xattr_name, ea.value.data, ea.value.length,
692 if (nret < 0) {
693 if (errno == ENOATTR) {
694 errno = ENOENT;
696 goto fail;
699 /* remove the old stream */
700 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
701 src_xattr_name);
702 if (oret < 0) {
703 if (errno == ENOATTR) {
704 errno = ENOENT;
706 goto fail;
709 done:
710 errno = 0;
711 ret = 0;
712 fail:
713 TALLOC_FREE(src_xattr_name);
714 TALLOC_FREE(dst_xattr_name);
715 return ret;
718 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
719 files_struct *fsp,
720 const struct smb_filename *smb_fname,
721 bool (*fn)(struct ea_struct *ea,
722 void *private_data),
723 void *private_data)
725 NTSTATUS status;
726 char **names;
727 size_t i, num_names;
728 struct streams_xattr_config *config;
730 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
731 return NT_STATUS_UNSUCCESSFUL);
733 status = get_ea_names_from_file(talloc_tos(),
734 handle->conn,
735 fsp,
736 smb_fname,
737 &names,
738 &num_names);
739 if (!NT_STATUS_IS_OK(status)) {
740 return status;
743 for (i=0; i<num_names; i++) {
744 struct ea_struct ea;
747 * We want to check with samba_private_attr_name()
748 * whether the xattr name is a private one,
749 * unfortunately it flags xattrs that begin with the
750 * default streams prefix as private.
752 * By only calling samba_private_attr_name() in case
753 * the xattr does NOT begin with the default prefix,
754 * we know that if it returns 'true' it definitely one
755 * of our internal xattr like "user.DOSATTRIB".
757 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
758 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
759 if (samba_private_attr_name(names[i])) {
760 continue;
764 if (strncmp(names[i], config->prefix,
765 config->prefix_len) != 0) {
766 continue;
769 status = get_ea_value(names,
770 handle->conn,
771 NULL,
772 smb_fname,
773 names[i],
774 &ea);
775 if (!NT_STATUS_IS_OK(status)) {
776 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
777 names[i],
778 smb_fname->base_name,
779 nt_errstr(status)));
780 continue;
783 ea.name = talloc_asprintf(
784 ea.value.data, ":%s%s",
785 names[i] + config->prefix_len,
786 config->store_stream_type ? "" : ":$DATA");
787 if (ea.name == NULL) {
788 DEBUG(0, ("talloc failed\n"));
789 continue;
792 if (!fn(&ea, private_data)) {
793 TALLOC_FREE(ea.value.data);
794 return NT_STATUS_OK;
797 TALLOC_FREE(ea.value.data);
800 TALLOC_FREE(names);
801 return NT_STATUS_OK;
804 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
805 struct stream_struct **streams,
806 const char *name, off_t size,
807 off_t alloc_size)
809 struct stream_struct *tmp;
811 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
812 (*num_streams)+1);
813 if (tmp == NULL) {
814 return false;
817 tmp[*num_streams].name = talloc_strdup(tmp, name);
818 if (tmp[*num_streams].name == NULL) {
819 return false;
822 tmp[*num_streams].size = size;
823 tmp[*num_streams].alloc_size = alloc_size;
825 *streams = tmp;
826 *num_streams += 1;
827 return true;
830 struct streaminfo_state {
831 TALLOC_CTX *mem_ctx;
832 vfs_handle_struct *handle;
833 unsigned int num_streams;
834 struct stream_struct *streams;
835 NTSTATUS status;
838 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
840 struct streaminfo_state *state =
841 (struct streaminfo_state *)private_data;
843 if (!add_one_stream(state->mem_ctx,
844 &state->num_streams, &state->streams,
845 ea->name, ea->value.length-1,
846 smb_roundup(state->handle->conn,
847 ea->value.length-1))) {
848 state->status = NT_STATUS_NO_MEMORY;
849 return false;
852 return true;
855 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
856 struct files_struct *fsp,
857 const struct smb_filename *smb_fname,
858 TALLOC_CTX *mem_ctx,
859 unsigned int *pnum_streams,
860 struct stream_struct **pstreams)
862 SMB_STRUCT_STAT sbuf;
863 int ret;
864 NTSTATUS status;
865 struct streaminfo_state state;
867 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
868 if (ret == -1) {
869 return map_nt_error_from_unix(errno);
872 state.streams = *pstreams;
873 state.num_streams = *pnum_streams;
874 state.mem_ctx = mem_ctx;
875 state.handle = handle;
876 state.status = NT_STATUS_OK;
878 if (S_ISLNK(sbuf.st_ex_mode)) {
880 * Currently we do't have SMB_VFS_LLISTXATTR
881 * inside the VFS which means there's no way
882 * to cope with a symlink when lp_posix_pathnames().
883 * returns true. For now ignore links.
884 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
886 status = NT_STATUS_OK;
887 } else {
888 status = walk_xattr_streams(handle, fsp, smb_fname,
889 collect_one_stream, &state);
892 if (!NT_STATUS_IS_OK(status)) {
893 TALLOC_FREE(state.streams);
894 return status;
897 if (!NT_STATUS_IS_OK(state.status)) {
898 TALLOC_FREE(state.streams);
899 return state.status;
902 *pnum_streams = state.num_streams;
903 *pstreams = state.streams;
905 return SMB_VFS_NEXT_STREAMINFO(handle,
906 fsp,
907 smb_fname,
908 mem_ctx,
909 pnum_streams,
910 pstreams);
913 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
914 enum timestamp_set_resolution *p_ts_res)
916 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
919 static int streams_xattr_connect(vfs_handle_struct *handle,
920 const char *service, const char *user)
922 struct streams_xattr_config *config;
923 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
924 const char *prefix;
925 int rc;
927 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
928 if (rc != 0) {
929 return rc;
932 config = talloc_zero(handle->conn, struct streams_xattr_config);
933 if (config == NULL) {
934 DEBUG(1, ("talloc_zero() failed\n"));
935 errno = ENOMEM;
936 return -1;
939 prefix = lp_parm_const_string(SNUM(handle->conn),
940 "streams_xattr", "prefix",
941 default_prefix);
942 config->prefix = talloc_strdup(config, prefix);
943 if (config->prefix == NULL) {
944 DEBUG(1, ("talloc_strdup() failed\n"));
945 errno = ENOMEM;
946 return -1;
948 config->prefix_len = strlen(config->prefix);
949 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
951 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
952 "streams_xattr",
953 "store_stream_type",
954 true);
956 SMB_VFS_HANDLE_SET_DATA(handle, config,
957 NULL, struct stream_xattr_config,
958 return -1);
960 return 0;
963 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
964 files_struct *fsp, const void *data,
965 size_t n, off_t offset)
967 struct stream_io *sio =
968 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
969 struct ea_struct ea;
970 NTSTATUS status;
971 struct smb_filename *smb_fname_base = NULL;
972 int ret;
974 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
976 if (sio == NULL) {
977 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
980 if (!streams_xattr_recheck(sio)) {
981 return -1;
984 /* Create an smb_filename with stream_name == NULL. */
985 smb_fname_base = synthetic_smb_fname(talloc_tos(),
986 sio->base,
987 NULL,
988 NULL,
989 fsp->fsp_name->flags);
990 if (smb_fname_base == NULL) {
991 errno = ENOMEM;
992 return -1;
995 status = get_ea_value(talloc_tos(), handle->conn, NULL,
996 smb_fname_base, sio->xattr_name, &ea);
997 if (!NT_STATUS_IS_OK(status)) {
998 return -1;
1001 if ((offset + n) > ea.value.length-1) {
1002 uint8_t *tmp;
1004 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1005 offset + n + 1);
1007 if (tmp == NULL) {
1008 TALLOC_FREE(ea.value.data);
1009 errno = ENOMEM;
1010 return -1;
1012 ea.value.data = tmp;
1013 ea.value.length = offset + n + 1;
1014 ea.value.data[offset+n] = 0;
1017 memcpy(ea.value.data + offset, data, n);
1019 ret = SMB_VFS_SETXATTR(fsp->conn,
1020 fsp->fsp_name,
1021 sio->xattr_name,
1022 ea.value.data, ea.value.length, 0);
1023 TALLOC_FREE(ea.value.data);
1025 if (ret == -1) {
1026 return -1;
1029 return n;
1032 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1033 files_struct *fsp, void *data,
1034 size_t n, off_t offset)
1036 struct stream_io *sio =
1037 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1038 struct ea_struct ea;
1039 NTSTATUS status;
1040 size_t length, overlap;
1041 struct smb_filename *smb_fname_base = NULL;
1043 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1044 (int)offset, (int)n));
1046 if (sio == NULL) {
1047 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1050 if (!streams_xattr_recheck(sio)) {
1051 return -1;
1054 /* Create an smb_filename with stream_name == NULL. */
1055 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1056 sio->base,
1057 NULL,
1058 NULL,
1059 fsp->fsp_name->flags);
1060 if (smb_fname_base == NULL) {
1061 errno = ENOMEM;
1062 return -1;
1065 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1066 smb_fname_base, sio->xattr_name, &ea);
1067 if (!NT_STATUS_IS_OK(status)) {
1068 return -1;
1071 length = ea.value.length-1;
1073 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1074 (int)length));
1076 /* Attempt to read past EOF. */
1077 if (length <= offset) {
1078 return 0;
1081 overlap = (offset + n) > length ? (length - offset) : n;
1082 memcpy(data, ea.value.data + offset, overlap);
1084 TALLOC_FREE(ea.value.data);
1085 return overlap;
1088 struct streams_xattr_pread_state {
1089 ssize_t nread;
1090 struct vfs_aio_state vfs_aio_state;
1093 static void streams_xattr_pread_done(struct tevent_req *subreq);
1095 static struct tevent_req *streams_xattr_pread_send(
1096 struct vfs_handle_struct *handle,
1097 TALLOC_CTX *mem_ctx,
1098 struct tevent_context *ev,
1099 struct files_struct *fsp,
1100 void *data,
1101 size_t n, off_t offset)
1103 struct tevent_req *req = NULL;
1104 struct tevent_req *subreq = NULL;
1105 struct streams_xattr_pread_state *state = NULL;
1106 struct stream_io *sio =
1107 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1109 req = tevent_req_create(mem_ctx, &state,
1110 struct streams_xattr_pread_state);
1111 if (req == NULL) {
1112 return NULL;
1115 if (sio == NULL) {
1116 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1117 data, n, offset);
1118 if (tevent_req_nomem(req, subreq)) {
1119 return tevent_req_post(req, ev);
1121 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1122 return req;
1125 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1126 if (state->nread != n) {
1127 if (state->nread != -1) {
1128 errno = EIO;
1130 tevent_req_error(req, errno);
1131 return tevent_req_post(req, ev);
1134 tevent_req_done(req);
1135 return tevent_req_post(req, ev);
1138 static void streams_xattr_pread_done(struct tevent_req *subreq)
1140 struct tevent_req *req = tevent_req_callback_data(
1141 subreq, struct tevent_req);
1142 struct streams_xattr_pread_state *state = tevent_req_data(
1143 req, struct streams_xattr_pread_state);
1145 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1146 TALLOC_FREE(subreq);
1148 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1149 return;
1151 tevent_req_done(req);
1154 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1155 struct vfs_aio_state *vfs_aio_state)
1157 struct streams_xattr_pread_state *state = tevent_req_data(
1158 req, struct streams_xattr_pread_state);
1160 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1161 return -1;
1164 *vfs_aio_state = state->vfs_aio_state;
1165 return state->nread;
1168 struct streams_xattr_pwrite_state {
1169 ssize_t nwritten;
1170 struct vfs_aio_state vfs_aio_state;
1173 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1175 static struct tevent_req *streams_xattr_pwrite_send(
1176 struct vfs_handle_struct *handle,
1177 TALLOC_CTX *mem_ctx,
1178 struct tevent_context *ev,
1179 struct files_struct *fsp,
1180 const void *data,
1181 size_t n, off_t offset)
1183 struct tevent_req *req = NULL;
1184 struct tevent_req *subreq = NULL;
1185 struct streams_xattr_pwrite_state *state = NULL;
1186 struct stream_io *sio =
1187 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1189 req = tevent_req_create(mem_ctx, &state,
1190 struct streams_xattr_pwrite_state);
1191 if (req == NULL) {
1192 return NULL;
1195 if (sio == NULL) {
1196 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1197 data, n, offset);
1198 if (tevent_req_nomem(req, subreq)) {
1199 return tevent_req_post(req, ev);
1201 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1202 return req;
1205 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1206 if (state->nwritten != n) {
1207 if (state->nwritten != -1) {
1208 errno = EIO;
1210 tevent_req_error(req, errno);
1211 return tevent_req_post(req, ev);
1214 tevent_req_done(req);
1215 return tevent_req_post(req, ev);
1218 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1220 struct tevent_req *req = tevent_req_callback_data(
1221 subreq, struct tevent_req);
1222 struct streams_xattr_pwrite_state *state = tevent_req_data(
1223 req, struct streams_xattr_pwrite_state);
1225 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1226 TALLOC_FREE(subreq);
1228 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1229 return;
1231 tevent_req_done(req);
1234 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1235 struct vfs_aio_state *vfs_aio_state)
1237 struct streams_xattr_pwrite_state *state = tevent_req_data(
1238 req, struct streams_xattr_pwrite_state);
1240 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1241 return -1;
1244 *vfs_aio_state = state->vfs_aio_state;
1245 return state->nwritten;
1248 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1249 struct files_struct *fsp,
1250 off_t offset)
1252 int ret;
1253 uint8_t *tmp;
1254 struct ea_struct ea;
1255 NTSTATUS status;
1256 struct stream_io *sio =
1257 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1258 struct smb_filename *smb_fname_base = NULL;
1260 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1261 fsp_str_dbg(fsp), (double)offset));
1263 if (sio == NULL) {
1264 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1267 if (!streams_xattr_recheck(sio)) {
1268 return -1;
1271 /* Create an smb_filename with stream_name == NULL. */
1272 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1273 sio->base,
1274 NULL,
1275 NULL,
1276 fsp->fsp_name->flags);
1277 if (smb_fname_base == NULL) {
1278 errno = ENOMEM;
1279 return -1;
1282 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1283 smb_fname_base, sio->xattr_name, &ea);
1284 if (!NT_STATUS_IS_OK(status)) {
1285 return -1;
1288 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1289 offset + 1);
1291 if (tmp == NULL) {
1292 TALLOC_FREE(ea.value.data);
1293 errno = ENOMEM;
1294 return -1;
1297 /* Did we expand ? */
1298 if (ea.value.length < offset + 1) {
1299 memset(&tmp[ea.value.length], '\0',
1300 offset + 1 - ea.value.length);
1303 ea.value.data = tmp;
1304 ea.value.length = offset + 1;
1305 ea.value.data[offset] = 0;
1307 ret = SMB_VFS_SETXATTR(fsp->conn,
1308 fsp->fsp_name,
1309 sio->xattr_name,
1310 ea.value.data, ea.value.length, 0);
1311 TALLOC_FREE(ea.value.data);
1313 if (ret == -1) {
1314 return -1;
1317 return 0;
1320 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1321 struct files_struct *fsp,
1322 uint32_t mode,
1323 off_t offset,
1324 off_t len)
1326 struct stream_io *sio =
1327 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1329 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1330 "len = %.0f\n",
1331 fsp_str_dbg(fsp), (double)offset, (double)len));
1333 if (sio == NULL) {
1334 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1337 if (!streams_xattr_recheck(sio)) {
1338 return -1;
1341 /* Let the pwrite code path handle it. */
1342 errno = ENOSYS;
1343 return -1;
1346 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1347 uid_t uid, gid_t gid)
1349 struct stream_io *sio =
1350 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1352 if (sio == NULL) {
1353 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1356 return 0;
1359 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1360 files_struct *fsp,
1361 mode_t mode)
1363 struct stream_io *sio =
1364 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1366 if (sio == NULL) {
1367 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1370 return 0;
1373 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1374 struct files_struct *fsp,
1375 const char *name,
1376 void *value,
1377 size_t size)
1379 struct stream_io *sio =
1380 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1382 if (sio == NULL) {
1383 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1386 errno = ENOTSUP;
1387 return -1;
1390 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1391 struct files_struct *fsp,
1392 char *list,
1393 size_t size)
1395 struct stream_io *sio =
1396 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1398 if (sio == NULL) {
1399 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1402 errno = ENOTSUP;
1403 return -1;
1406 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1407 struct files_struct *fsp,
1408 const char *name)
1410 struct stream_io *sio =
1411 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1413 if (sio == NULL) {
1414 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1417 errno = ENOTSUP;
1418 return -1;
1421 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1422 struct files_struct *fsp,
1423 const char *name,
1424 const void *value,
1425 size_t size,
1426 int flags)
1428 struct stream_io *sio =
1429 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1431 if (sio == NULL) {
1432 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1433 size, flags);
1436 errno = ENOTSUP;
1437 return -1;
1440 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1441 files_struct *fsp,
1442 TALLOC_CTX *mem_ctx)
1444 struct stream_io *sio =
1445 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1447 if (sio == NULL) {
1448 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1451 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1452 handle, fsp->base_fsp->fsp_name,
1453 SMB_ACL_TYPE_ACCESS, mem_ctx);
1456 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1457 files_struct *fsp,
1458 SMB_ACL_T theacl)
1460 struct stream_io *sio =
1461 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1463 if (sio == NULL) {
1464 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1467 return 0;
1470 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1471 files_struct *fsp,
1472 TALLOC_CTX *mem_ctx,
1473 char **blob_description,
1474 DATA_BLOB *blob)
1476 struct stream_io *sio =
1477 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1479 if (sio == NULL) {
1480 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1481 blob_description, blob);
1484 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1485 handle, fsp->base_fsp->fsp_name, mem_ctx,
1486 blob_description, blob);
1489 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1490 files_struct *fsp,
1491 uint32_t security_info,
1492 TALLOC_CTX *mem_ctx,
1493 struct security_descriptor **ppdesc)
1495 struct stream_io *sio =
1496 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1498 if (sio == NULL) {
1499 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1500 mem_ctx, ppdesc);
1503 return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1504 security_info, mem_ctx, ppdesc);
1507 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1508 files_struct *fsp,
1509 uint32_t security_info_sent,
1510 const struct security_descriptor *psd)
1512 struct stream_io *sio =
1513 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1515 if (sio == NULL) {
1516 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1517 security_info_sent, psd);
1520 return NT_STATUS_OK;
1523 struct streams_xattr_fsync_state {
1524 int ret;
1525 struct vfs_aio_state vfs_aio_state;
1528 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1530 static struct tevent_req *streams_xattr_fsync_send(
1531 struct vfs_handle_struct *handle,
1532 TALLOC_CTX *mem_ctx,
1533 struct tevent_context *ev,
1534 struct files_struct *fsp)
1536 struct tevent_req *req = NULL;
1537 struct tevent_req *subreq = NULL;
1538 struct streams_xattr_fsync_state *state = NULL;
1539 struct stream_io *sio =
1540 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1542 req = tevent_req_create(mem_ctx, &state,
1543 struct streams_xattr_fsync_state);
1544 if (req == NULL) {
1545 return NULL;
1548 if (sio == NULL) {
1549 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1550 if (tevent_req_nomem(req, subreq)) {
1551 return tevent_req_post(req, ev);
1553 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1554 return req;
1558 * There's no pathname based sync variant and we don't have access to
1559 * the basefile handle, so we can't do anything here.
1562 tevent_req_done(req);
1563 return tevent_req_post(req, ev);
1566 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1568 struct tevent_req *req = tevent_req_callback_data(
1569 subreq, struct tevent_req);
1570 struct streams_xattr_fsync_state *state = tevent_req_data(
1571 req, struct streams_xattr_fsync_state);
1573 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1574 TALLOC_FREE(subreq);
1575 if (state->ret != 0) {
1576 tevent_req_error(req, errno);
1577 return;
1580 tevent_req_done(req);
1583 static int streams_xattr_fsync_recv(struct tevent_req *req,
1584 struct vfs_aio_state *vfs_aio_state)
1586 struct streams_xattr_fsync_state *state = tevent_req_data(
1587 req, struct streams_xattr_fsync_state);
1589 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1590 return -1;
1593 *vfs_aio_state = state->vfs_aio_state;
1594 return state->ret;
1597 static bool streams_xattr_lock(vfs_handle_struct *handle,
1598 files_struct *fsp,
1599 int op,
1600 off_t offset,
1601 off_t count,
1602 int type)
1604 struct stream_io *sio =
1605 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1607 if (sio == NULL) {
1608 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1611 return true;
1614 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1615 files_struct *fsp,
1616 off_t *poffset,
1617 off_t *pcount,
1618 int *ptype,
1619 pid_t *ppid)
1621 struct stream_io *sio =
1622 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1624 if (sio == NULL) {
1625 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1626 pcount, ptype, ppid);
1629 errno = ENOTSUP;
1630 return false;
1633 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1634 files_struct *fsp,
1635 uint32_t share_mode,
1636 uint32_t access_mask)
1638 struct stream_io *sio =
1639 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1641 if (sio == NULL) {
1642 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1643 share_mode, access_mask);
1646 return 0;
1649 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1650 files_struct *fsp,
1651 int leasetype)
1653 struct stream_io *sio =
1654 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1656 if (sio == NULL) {
1657 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1660 return 0;
1663 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1664 files_struct *fsp,
1665 struct lock_struct *plock)
1667 struct stream_io *sio =
1668 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1670 if (sio == NULL) {
1671 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1674 return true;
1677 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1678 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1679 .connect_fn = streams_xattr_connect,
1680 .open_fn = streams_xattr_open,
1681 .close_fn = streams_xattr_close,
1682 .stat_fn = streams_xattr_stat,
1683 .fstat_fn = streams_xattr_fstat,
1684 .lstat_fn = streams_xattr_lstat,
1685 .pread_fn = streams_xattr_pread,
1686 .pwrite_fn = streams_xattr_pwrite,
1687 .pread_send_fn = streams_xattr_pread_send,
1688 .pread_recv_fn = streams_xattr_pread_recv,
1689 .pwrite_send_fn = streams_xattr_pwrite_send,
1690 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1691 .unlink_fn = streams_xattr_unlink,
1692 .rename_fn = streams_xattr_rename,
1693 .ftruncate_fn = streams_xattr_ftruncate,
1694 .fallocate_fn = streams_xattr_fallocate,
1695 .streaminfo_fn = streams_xattr_streaminfo,
1697 .fsync_send_fn = streams_xattr_fsync_send,
1698 .fsync_recv_fn = streams_xattr_fsync_recv,
1700 .lock_fn = streams_xattr_lock,
1701 .getlock_fn = streams_xattr_getlock,
1702 .kernel_flock_fn = streams_xattr_kernel_flock,
1703 .linux_setlease_fn = streams_xattr_linux_setlease,
1704 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1706 .fchown_fn = streams_xattr_fchown,
1707 .fchmod_fn = streams_xattr_fchmod,
1709 .fgetxattr_fn = streams_xattr_fgetxattr,
1710 .flistxattr_fn = streams_xattr_flistxattr,
1711 .fremovexattr_fn = streams_xattr_fremovexattr,
1712 .fsetxattr_fn = streams_xattr_fsetxattr,
1714 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1715 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1716 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1718 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1719 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1722 static_decl_vfs;
1723 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1725 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1726 &vfs_streams_xattr_fns);