smbd: Introduce fsp_is_alternate_stream()
[samba.git] / source3 / modules / vfs_streams_xattr.c
1 /*
2  * Store streams in xattrs
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  *
6  * Partly based on James Peach's Darwin module, which is
7  *
8  * Copyright (C) James Peach 2006-2007
9  *
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.
14  *
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.
19  *
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/>.
22  */
23
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"
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_VFS
33
34 struct streams_xattr_config {
35         const char *prefix;
36         size_t prefix_len;
37         bool store_stream_type;
38 };
39
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;
46 };
47
48 static ssize_t get_xattr_size_fsp(struct files_struct *fsp,
49                                   const char *xattr_name)
50 {
51         NTSTATUS status;
52         struct ea_struct ea;
53         ssize_t result;
54
55         status = get_ea_value_fsp(talloc_tos(),
56                                   fsp,
57                                   xattr_name,
58                                   &ea);
59         if (!NT_STATUS_IS_OK(status)) {
60                 return -1;
61         }
62
63         result = ea.value.length-1;
64         TALLOC_FREE(ea.value.data);
65         return result;
66 }
67
68 /**
69  * Given a stream name, populate xattr_name with the xattr name to use for
70  * accessing the stream.
71  */
72 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
73                                        TALLOC_CTX *ctx,
74                                        const char *stream_name,
75                                        char **xattr_name)
76 {
77         char *sname;
78         char *stype;
79         struct streams_xattr_config *config;
80
81         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
82                                 return NT_STATUS_UNSUCCESSFUL);
83
84         sname = talloc_strdup(ctx, stream_name + 1);
85         if (sname == NULL) {
86                 return NT_STATUS_NO_MEMORY;
87         }
88
89         /*
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.
94          *
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.
99          *
100          * In check_path_syntax() we've already ensured the streamname
101          * we got from the client is valid.
102          */
103         stype = strrchr_m(sname, ':');
104
105         if (stype) {
106                 /*
107                  * We only support one stream type: "$DATA"
108                  */
109                 if (strcasecmp_m(stype, ":$DATA") != 0) {
110                         talloc_free(sname);
111                         return NT_STATUS_INVALID_PARAMETER;
112                 }
113
114                 /* Split name and type */
115                 stype[0] = '\0';
116         }
117
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;
125         }
126
127         DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
128                    stream_name));
129
130         talloc_free(sname);
131         return NT_STATUS_OK;
132 }
133
134 static bool streams_xattr_recheck(struct stream_io *sio)
135 {
136         NTSTATUS status;
137         char *xattr_name = NULL;
138
139         if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
140                 return true;
141         }
142
143         if (sio->fsp->fsp_name->stream_name == NULL) {
144                 /* how can this happen */
145                 errno = EINVAL;
146                 return false;
147         }
148
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;
154         }
155
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;
163         }
164         TALLOC_FREE(xattr_name);
165
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;
171         }
172
173         sio->fsp_name_ptr = sio->fsp->fsp_name;
174
175         return true;
176 }
177
178 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
179                                SMB_STRUCT_STAT *sbuf)
180 {
181         int ret = -1;
182         struct stream_io *io = (struct stream_io *)
183                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
184
185         if (io == NULL || !fsp_is_alternate_stream(fsp)) {
186                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
187         }
188
189         DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
190
191         if (!streams_xattr_recheck(io)) {
192                 return -1;
193         }
194
195         ret = SMB_VFS_NEXT_FSTAT(handle, fsp->base_fsp, sbuf);
196         if (ret == -1) {
197                 return -1;
198         }
199
200         sbuf->st_ex_size = get_xattr_size_fsp(fsp->base_fsp,
201                                               io->xattr_name);
202         if (sbuf->st_ex_size == -1) {
203                 SET_STAT_INVALID(*sbuf);
204                 return -1;
205         }
206
207         DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
208
209         sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
210         sbuf->st_ex_mode &= ~S_IFMT;
211         sbuf->st_ex_mode &= ~S_IFDIR;
212         sbuf->st_ex_mode |= S_IFREG;
213         sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
214
215         return 0;
216 }
217
218 static int streams_xattr_stat(vfs_handle_struct *handle,
219                               struct smb_filename *smb_fname)
220 {
221         NTSTATUS status;
222         int result = -1;
223         char *xattr_name = NULL;
224         char *tmp_stream_name = NULL;
225         struct smb_filename *pathref = NULL;
226         struct files_struct *fsp = smb_fname->fsp;
227
228         if (!is_named_stream(smb_fname)) {
229                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
230         }
231
232         /* Note if lp_posix_paths() is true, we can never
233          * get here as is_named_stream() is
234          * always false. So we never need worry about
235          * not following links here. */
236
237         /* Populate the stat struct with info from the base file. */
238         tmp_stream_name = smb_fname->stream_name;
239         smb_fname->stream_name = NULL;
240         result = SMB_VFS_NEXT_STAT(handle, smb_fname);
241         smb_fname->stream_name = tmp_stream_name;
242
243         if (result == -1) {
244                 return -1;
245         }
246
247         /* Derive the xattr name to lookup. */
248         status = streams_xattr_get_name(handle, talloc_tos(),
249                                         smb_fname->stream_name, &xattr_name);
250         if (!NT_STATUS_IS_OK(status)) {
251                 errno = map_errno_from_nt_status(status);
252                 return -1;
253         }
254
255         /* Augment the base file's stat information before returning. */
256         if (fsp == NULL) {
257                 status = synthetic_pathref(talloc_tos(),
258                                            handle->conn->cwd_fsp,
259                                            smb_fname->base_name,
260                                            NULL,
261                                            NULL,
262                                            smb_fname->twrp,
263                                            smb_fname->flags,
264                                            &pathref);
265                 if (!NT_STATUS_IS_OK(status)) {
266                         TALLOC_FREE(xattr_name);
267                         SET_STAT_INVALID(smb_fname->st);
268                         errno = ENOENT;
269                         return -1;
270                 }
271                 fsp = pathref->fsp;
272         } else {
273                 fsp = fsp->base_fsp;
274         }
275
276         smb_fname->st.st_ex_size = get_xattr_size_fsp(fsp,
277                                                       xattr_name);
278         if (smb_fname->st.st_ex_size == -1) {
279                 TALLOC_FREE(xattr_name);
280                 TALLOC_FREE(pathref);
281                 SET_STAT_INVALID(smb_fname->st);
282                 errno = ENOENT;
283                 return -1;
284         }
285
286         smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
287         smb_fname->st.st_ex_mode &= ~S_IFMT;
288         smb_fname->st.st_ex_mode |= S_IFREG;
289         smb_fname->st.st_ex_blocks =
290             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
291
292         TALLOC_FREE(xattr_name);
293         TALLOC_FREE(pathref);
294         return 0;
295 }
296
297 static int streams_xattr_lstat(vfs_handle_struct *handle,
298                                struct smb_filename *smb_fname)
299 {
300         if (is_named_stream(smb_fname)) {
301                 /*
302                  * There can never be EA's on a symlink.
303                  * Windows will never see a symlink, and
304                  * in SMB_FILENAME_POSIX_PATH mode we don't
305                  * allow EA's on a symlink.
306                  */
307                 SET_STAT_INVALID(smb_fname->st);
308                 errno = ENOENT;
309                 return -1;
310         }
311         return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
312 }
313
314 static int streams_xattr_openat(struct vfs_handle_struct *handle,
315                                 const struct files_struct *dirfsp,
316                                 const struct smb_filename *smb_fname,
317                                 files_struct *fsp,
318                                 int flags,
319                                 mode_t mode)
320 {
321         NTSTATUS status;
322         struct streams_xattr_config *config = NULL;
323         struct stream_io *sio = NULL;
324         struct ea_struct ea;
325         char *xattr_name = NULL;
326         int fakefd = -1;
327         bool set_empty_xattr = false;
328         int ret;
329
330         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
331                                 return -1);
332
333         DBG_DEBUG("called for %s with flags 0x%x\n",
334                   smb_fname_str_dbg(smb_fname),
335                   flags);
336
337         if (!is_named_stream(smb_fname)) {
338                 return SMB_VFS_NEXT_OPENAT(handle,
339                                            dirfsp,
340                                            smb_fname,
341                                            fsp,
342                                            flags,
343                                            mode);
344         }
345
346         /*
347          * For now assert this, so the below SMB_VFS_SETXATTR() works.
348          */
349         SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
350
351         status = streams_xattr_get_name(handle, talloc_tos(),
352                                         smb_fname->stream_name, &xattr_name);
353         if (!NT_STATUS_IS_OK(status)) {
354                 errno = map_errno_from_nt_status(status);
355                 goto fail;
356         }
357
358         status = get_ea_value_fsp(talloc_tos(),
359                                   fsp->base_fsp,
360                                   xattr_name,
361                                   &ea);
362
363         DBG_DEBUG("get_ea_value_fsp returned %s\n", nt_errstr(status));
364
365         if (!NT_STATUS_IS_OK(status)) {
366                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
367                         /*
368                          * The base file is not there. This is an error even if
369                          * we got O_CREAT, the higher levels should have created
370                          * the base file for us.
371                          */
372                         DBG_DEBUG("streams_xattr_open: base file %s not around, "
373                                   "returning ENOENT\n", smb_fname->base_name);
374                         errno = ENOENT;
375                         goto fail;
376                 }
377
378                 if (!(flags & O_CREAT)) {
379                         errno = ENOATTR;
380                         goto fail;
381                 }
382
383                 set_empty_xattr = true;
384         }
385
386         if (flags & O_TRUNC) {
387                 set_empty_xattr = true;
388         }
389
390         if (set_empty_xattr) {
391                 /*
392                  * The attribute does not exist or needs to be truncated
393                  */
394
395                 /*
396                  * Darn, xattrs need at least 1 byte
397                  */
398                 char null = '\0';
399
400                 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
401                            xattr_name, smb_fname->base_name));
402
403                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
404                                        xattr_name,
405                                        &null, sizeof(null),
406                                        flags & O_EXCL ? XATTR_CREATE : 0);
407                 if (ret != 0) {
408                         goto fail;
409                 }
410         }
411
412         fakefd = vfs_fake_fd();
413
414         sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
415         if (sio == NULL) {
416                 errno = ENOMEM;
417                 goto fail;
418         }
419
420         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
421                                         xattr_name);
422         if (sio->xattr_name == NULL) {
423                 errno = ENOMEM;
424                 goto fail;
425         }
426
427         /*
428          * so->base needs to be a copy of fsp->fsp_name->base_name,
429          * making it identical to streams_xattr_recheck(). If the
430          * open is changing directories, fsp->fsp_name->base_name
431          * will be the full path from the share root, whilst
432          * smb_fname will be relative to the $cwd.
433          */
434         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
435                                   fsp->fsp_name->base_name);
436         if (sio->base == NULL) {
437                 errno = ENOMEM;
438                 goto fail;
439         }
440
441         sio->fsp_name_ptr = fsp->fsp_name;
442         sio->handle = handle;
443         sio->fsp = fsp;
444
445         return fakefd;
446
447  fail:
448         if (fakefd >= 0) {
449                 vfs_fake_fd_close(fakefd);
450                 fakefd = -1;
451         }
452
453         return -1;
454 }
455
456 static int streams_xattr_close(vfs_handle_struct *handle,
457                                files_struct *fsp)
458 {
459         int ret;
460         int fd;
461
462         fd = fsp_get_pathref_fd(fsp);
463
464         DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
465                         smb_fname_str_dbg(fsp->fsp_name), fd);
466
467         if (!is_named_stream(fsp->fsp_name)) {
468                 return SMB_VFS_NEXT_CLOSE(handle, fsp);
469         }
470
471         ret = vfs_fake_fd_close(fd);
472         fsp_set_fd(fsp, -1);
473
474         return ret;
475 }
476
477 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
478                         struct files_struct *dirfsp,
479                         const struct smb_filename *smb_fname,
480                         int flags)
481 {
482         NTSTATUS status;
483         int ret = -1;
484         char *xattr_name = NULL;
485
486         if (!is_named_stream(smb_fname)) {
487                 return SMB_VFS_NEXT_UNLINKAT(handle,
488                                         dirfsp,
489                                         smb_fname,
490                                         flags);
491         }
492
493         status = streams_xattr_get_name(handle, talloc_tos(),
494                                         smb_fname->stream_name, &xattr_name);
495         if (!NT_STATUS_IS_OK(status)) {
496                 errno = map_errno_from_nt_status(status);
497                 goto fail;
498         }
499
500         SMB_ASSERT(smb_fname->fsp != NULL);
501         SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
502
503         ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
504
505         if ((ret == -1) && (errno == ENOATTR)) {
506                 errno = ENOENT;
507                 goto fail;
508         }
509
510         ret = 0;
511
512  fail:
513         TALLOC_FREE(xattr_name);
514         return ret;
515 }
516
517 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
518                         struct files_struct *dirfsp,
519                         const struct smb_filename *smb_fname,
520                         int flags)
521 {
522         int ret;
523         if (flags & AT_REMOVEDIR) {
524                 ret = SMB_VFS_NEXT_UNLINKAT(handle,
525                                 dirfsp,
526                                 smb_fname,
527                                 flags);
528         } else {
529                 ret = streams_xattr_unlink_internal(handle,
530                                 dirfsp,
531                                 smb_fname,
532                                 flags);
533         }
534         return ret;
535 }
536
537 static int streams_xattr_renameat(vfs_handle_struct *handle,
538                                 files_struct *srcfsp,
539                                 const struct smb_filename *smb_fname_src,
540                                 files_struct *dstfsp,
541                                 const struct smb_filename *smb_fname_dst)
542 {
543         NTSTATUS status;
544         int ret = -1;
545         char *src_xattr_name = NULL;
546         char *dst_xattr_name = NULL;
547         bool src_is_stream, dst_is_stream;
548         ssize_t oret;
549         ssize_t nret;
550         struct ea_struct ea;
551         struct smb_filename *pathref_src = NULL;
552         struct smb_filename *pathref_dst = NULL;
553         struct smb_filename *full_src = NULL;
554         struct smb_filename *full_dst = NULL;
555
556         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
557         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
558
559         if (!src_is_stream && !dst_is_stream) {
560                 return SMB_VFS_NEXT_RENAMEAT(handle,
561                                         srcfsp,
562                                         smb_fname_src,
563                                         dstfsp,
564                                         smb_fname_dst);
565         }
566
567         /* For now don't allow renames from or to the default stream. */
568         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
569             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
570                 errno = ENOSYS;
571                 goto done;
572         }
573
574         /* Don't rename if the streams are identical. */
575         if (strcasecmp_m(smb_fname_src->stream_name,
576                        smb_fname_dst->stream_name) == 0) {
577                 goto done;
578         }
579
580         /* Get the xattr names. */
581         status = streams_xattr_get_name(handle, talloc_tos(),
582                                         smb_fname_src->stream_name,
583                                         &src_xattr_name);
584         if (!NT_STATUS_IS_OK(status)) {
585                 errno = map_errno_from_nt_status(status);
586                 goto fail;
587         }
588         status = streams_xattr_get_name(handle, talloc_tos(),
589                                         smb_fname_dst->stream_name,
590                                         &dst_xattr_name);
591         if (!NT_STATUS_IS_OK(status)) {
592                 errno = map_errno_from_nt_status(status);
593                 goto fail;
594         }
595
596         full_src = full_path_from_dirfsp_atname(talloc_tos(),
597                                                 srcfsp,
598                                                 smb_fname_src);
599         if (full_src == NULL) {
600                 errno = ENOMEM;
601                 goto fail;
602         }
603         full_dst = full_path_from_dirfsp_atname(talloc_tos(),
604                                                 dstfsp,
605                                                 smb_fname_dst);
606         if (full_dst == NULL) {
607                 errno = ENOMEM;
608                 goto fail;
609         }
610
611         /* Get a pathref for full_src (base file, no stream name). */
612         status = synthetic_pathref(talloc_tos(),
613                                 handle->conn->cwd_fsp,
614                                 full_src->base_name,
615                                 NULL,
616                                 NULL,
617                                 full_src->twrp,
618                                 full_src->flags,
619                                 &pathref_src);
620         if (!NT_STATUS_IS_OK(status)) {
621                 errno = ENOENT;
622                 goto fail;
623         }
624
625         /* Read the old stream from the base file fsp. */
626         status = get_ea_value_fsp(talloc_tos(),
627                                   pathref_src->fsp,
628                                   src_xattr_name,
629                                   &ea);
630         if (!NT_STATUS_IS_OK(status)) {
631                 errno = map_errno_from_nt_status(status);
632                 goto fail;
633         }
634
635         /* Get a pathref for full_dst (base file, no stream name). */
636         status = synthetic_pathref(talloc_tos(),
637                                 handle->conn->cwd_fsp,
638                                 full_dst->base_name,
639                                 NULL,
640                                 NULL,
641                                 full_dst->twrp,
642                                 full_dst->flags,
643                                 &pathref_dst);
644         if (!NT_STATUS_IS_OK(status)) {
645                 errno = ENOENT;
646                 goto fail;
647         }
648
649         /* (Over)write the new stream on the base file fsp. */
650         nret = SMB_VFS_FSETXATTR(
651                         pathref_dst->fsp,
652                         dst_xattr_name,
653                         ea.value.data,
654                         ea.value.length,
655                         0);
656         if (nret < 0) {
657                 if (errno == ENOATTR) {
658                         errno = ENOENT;
659                 }
660                 goto fail;
661         }
662
663         /*
664          * Remove the old stream from the base file fsp.
665          */
666         oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
667                                     src_xattr_name);
668         if (oret < 0) {
669                 if (errno == ENOATTR) {
670                         errno = ENOENT;
671                 }
672                 goto fail;
673         }
674
675  done:
676         errno = 0;
677         ret = 0;
678  fail:
679         TALLOC_FREE(pathref_src);
680         TALLOC_FREE(pathref_dst);
681         TALLOC_FREE(full_src);
682         TALLOC_FREE(full_dst);
683         TALLOC_FREE(src_xattr_name);
684         TALLOC_FREE(dst_xattr_name);
685         return ret;
686 }
687
688 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
689                                 files_struct *fsp,
690                                 const struct smb_filename *smb_fname,
691                                 bool (*fn)(struct ea_struct *ea,
692                                         void *private_data),
693                                 void *private_data)
694 {
695         NTSTATUS status;
696         char **names;
697         size_t i, num_names;
698         struct streams_xattr_config *config;
699
700         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
701                                 return NT_STATUS_UNSUCCESSFUL);
702
703         status = get_ea_names_from_fsp(talloc_tos(),
704                                 smb_fname->fsp,
705                                 &names,
706                                 &num_names);
707         if (!NT_STATUS_IS_OK(status)) {
708                 return status;
709         }
710
711         for (i=0; i<num_names; i++) {
712                 struct ea_struct ea;
713
714                 /*
715                  * We want to check with samba_private_attr_name()
716                  * whether the xattr name is a private one,
717                  * unfortunately it flags xattrs that begin with the
718                  * default streams prefix as private.
719                  *
720                  * By only calling samba_private_attr_name() in case
721                  * the xattr does NOT begin with the default prefix,
722                  * we know that if it returns 'true' it definitely one
723                  * of our internal xattr like "user.DOSATTRIB".
724                  */
725                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
726                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
727                         if (samba_private_attr_name(names[i])) {
728                                 continue;
729                         }
730                 }
731
732                 if (strncmp(names[i], config->prefix,
733                             config->prefix_len) != 0) {
734                         continue;
735                 }
736
737                 status = get_ea_value_fsp(names,
738                                           smb_fname->fsp,
739                                           names[i],
740                                           &ea);
741                 if (!NT_STATUS_IS_OK(status)) {
742                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
743                                 names[i],
744                                 smb_fname->base_name,
745                                 nt_errstr(status)));
746                         continue;
747                 }
748
749                 ea.name = talloc_asprintf(
750                         ea.value.data, ":%s%s",
751                         names[i] + config->prefix_len,
752                         config->store_stream_type ? "" : ":$DATA");
753                 if (ea.name == NULL) {
754                         DEBUG(0, ("talloc failed\n"));
755                         continue;
756                 }
757
758                 if (!fn(&ea, private_data)) {
759                         TALLOC_FREE(ea.value.data);
760                         return NT_STATUS_OK;
761                 }
762
763                 TALLOC_FREE(ea.value.data);
764         }
765
766         TALLOC_FREE(names);
767         return NT_STATUS_OK;
768 }
769
770 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
771                            struct stream_struct **streams,
772                            const char *name, off_t size,
773                            off_t alloc_size)
774 {
775         struct stream_struct *tmp;
776
777         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
778                                    (*num_streams)+1);
779         if (tmp == NULL) {
780                 return false;
781         }
782
783         tmp[*num_streams].name = talloc_strdup(tmp, name);
784         if (tmp[*num_streams].name == NULL) {
785                 return false;
786         }
787
788         tmp[*num_streams].size = size;
789         tmp[*num_streams].alloc_size = alloc_size;
790
791         *streams = tmp;
792         *num_streams += 1;
793         return true;
794 }
795
796 struct streaminfo_state {
797         TALLOC_CTX *mem_ctx;
798         vfs_handle_struct *handle;
799         unsigned int num_streams;
800         struct stream_struct *streams;
801         NTSTATUS status;
802 };
803
804 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
805 {
806         struct streaminfo_state *state =
807                 (struct streaminfo_state *)private_data;
808
809         if (!add_one_stream(state->mem_ctx,
810                             &state->num_streams, &state->streams,
811                             ea->name, ea->value.length-1,
812                             smb_roundup(state->handle->conn,
813                                         ea->value.length-1))) {
814                 state->status = NT_STATUS_NO_MEMORY;
815                 return false;
816         }
817
818         return true;
819 }
820
821 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
822                                          struct files_struct *fsp,
823                                          TALLOC_CTX *mem_ctx,
824                                          unsigned int *pnum_streams,
825                                          struct stream_struct **pstreams)
826 {
827         NTSTATUS status;
828         struct streaminfo_state state;
829
830         state.streams = *pstreams;
831         state.num_streams = *pnum_streams;
832         state.mem_ctx = mem_ctx;
833         state.handle = handle;
834         state.status = NT_STATUS_OK;
835
836         status = walk_xattr_streams(handle,
837                                     fsp,
838                                     fsp->fsp_name,
839                                     collect_one_stream,
840                                     &state);
841
842         if (!NT_STATUS_IS_OK(status)) {
843                 TALLOC_FREE(state.streams);
844                 return status;
845         }
846
847         if (!NT_STATUS_IS_OK(state.status)) {
848                 TALLOC_FREE(state.streams);
849                 return state.status;
850         }
851
852         *pnum_streams = state.num_streams;
853         *pstreams = state.streams;
854
855         return SMB_VFS_NEXT_FSTREAMINFO(handle,
856                         fsp,
857                         mem_ctx,
858                         pnum_streams,
859                         pstreams);
860 }
861
862 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
863                         enum timestamp_set_resolution *p_ts_res)
864 {
865         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
866 }
867
868 static int streams_xattr_connect(vfs_handle_struct *handle,
869                                  const char *service, const char *user)
870 {
871         struct streams_xattr_config *config;
872         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
873         const char *prefix;
874         int rc;
875
876         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
877         if (rc != 0) {
878                 return rc;
879         }
880
881         config = talloc_zero(handle->conn, struct streams_xattr_config);
882         if (config == NULL) {
883                 DEBUG(1, ("talloc_zero() failed\n"));
884                 errno = ENOMEM;
885                 return -1;
886         }
887
888         prefix = lp_parm_const_string(SNUM(handle->conn),
889                                       "streams_xattr", "prefix",
890                                       default_prefix);
891         config->prefix = talloc_strdup(config, prefix);
892         if (config->prefix == NULL) {
893                 DEBUG(1, ("talloc_strdup() failed\n"));
894                 errno = ENOMEM;
895                 return -1;
896         }
897         config->prefix_len = strlen(config->prefix);
898         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
899
900         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
901                                                  "streams_xattr",
902                                                  "store_stream_type",
903                                                  true);
904
905         SMB_VFS_HANDLE_SET_DATA(handle, config,
906                                 NULL, struct stream_xattr_config,
907                                 return -1);
908
909         return 0;
910 }
911
912 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
913                                     files_struct *fsp, const void *data,
914                                     size_t n, off_t offset)
915 {
916         struct stream_io *sio =
917                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
918         struct ea_struct ea;
919         NTSTATUS status;
920         int ret;
921
922         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
923
924         if (sio == NULL) {
925                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
926         }
927
928         if (!streams_xattr_recheck(sio)) {
929                 return -1;
930         }
931
932         if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
933                 /*
934                  * Requested write is beyond what can be read based on
935                  * samba configuration.
936                  * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
937                  * entire file to be skipped by File Explorer. VFAT returns
938                  * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
939                  * to skip writing metadata, but copy data.
940                  */
941                 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
942                         "supported extended attribute size. "
943                         "Depending on filesystem type and operating system "
944                         "(OS) specifics, this value may be increased using "
945                         "the value of the parameter: "
946                         "smbd max xattr size = <bytes>. Consult OS and "
947                         "filesystem manpages prior to increasing this limit.\n",
948                         sio->xattr_name, sio->base);
949                 errno = EOVERFLOW;
950                 return -1;
951         }
952
953         status = get_ea_value_fsp(talloc_tos(),
954                                   fsp->base_fsp,
955                                   sio->xattr_name,
956                                   &ea);
957         if (!NT_STATUS_IS_OK(status)) {
958                 return -1;
959         }
960
961         if ((offset + n) > ea.value.length-1) {
962                 uint8_t *tmp;
963
964                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
965                                            offset + n + 1);
966
967                 if (tmp == NULL) {
968                         TALLOC_FREE(ea.value.data);
969                         errno = ENOMEM;
970                         return -1;
971                 }
972                 ea.value.data = tmp;
973                 ea.value.length = offset + n + 1;
974                 ea.value.data[offset+n] = 0;
975         }
976
977         memcpy(ea.value.data + offset, data, n);
978
979         ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
980                                 sio->xattr_name,
981                                 ea.value.data,
982                                 ea.value.length,
983                                 0);
984         TALLOC_FREE(ea.value.data);
985
986         if (ret == -1) {
987                 return -1;
988         }
989
990         return n;
991 }
992
993 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
994                                    files_struct *fsp, void *data,
995                                    size_t n, off_t offset)
996 {
997         struct stream_io *sio =
998                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
999         struct ea_struct ea;
1000         NTSTATUS status;
1001         size_t length, overlap;
1002
1003         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1004                    (int)offset, (int)n));
1005
1006         if (sio == NULL) {
1007                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1008         }
1009
1010         if (!streams_xattr_recheck(sio)) {
1011                 return -1;
1012         }
1013
1014         status = get_ea_value_fsp(talloc_tos(),
1015                                   fsp->base_fsp,
1016                                   sio->xattr_name,
1017                                   &ea);
1018         if (!NT_STATUS_IS_OK(status)) {
1019                 return -1;
1020         }
1021
1022         length = ea.value.length-1;
1023
1024         DBG_DEBUG("get_ea_value_fsp returned %d bytes\n",
1025                    (int)length);
1026
1027         /* Attempt to read past EOF. */
1028         if (length <= offset) {
1029                 return 0;
1030         }
1031
1032         overlap = (offset + n) > length ? (length - offset) : n;
1033         memcpy(data, ea.value.data + offset, overlap);
1034
1035         TALLOC_FREE(ea.value.data);
1036         return overlap;
1037 }
1038
1039 struct streams_xattr_pread_state {
1040         ssize_t nread;
1041         struct vfs_aio_state vfs_aio_state;
1042 };
1043
1044 static void streams_xattr_pread_done(struct tevent_req *subreq);
1045
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)
1053 {
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);
1059
1060         req = tevent_req_create(mem_ctx, &state,
1061                                 struct streams_xattr_pread_state);
1062         if (req == NULL) {
1063                 return NULL;
1064         }
1065
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);
1071                 }
1072                 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1073                 return req;
1074         }
1075
1076         state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1077         if (state->nread != n) {
1078                 if (state->nread != -1) {
1079                         errno = EIO;
1080                 }
1081                 tevent_req_error(req, errno);
1082                 return tevent_req_post(req, ev);
1083         }
1084
1085         tevent_req_done(req);
1086         return tevent_req_post(req, ev);
1087 }
1088
1089 static void streams_xattr_pread_done(struct tevent_req *subreq)
1090 {
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);
1095
1096         state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1097         TALLOC_FREE(subreq);
1098
1099         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1100                 return;
1101         }
1102         tevent_req_done(req);
1103 }
1104
1105 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1106                                         struct vfs_aio_state *vfs_aio_state)
1107 {
1108         struct streams_xattr_pread_state *state = tevent_req_data(
1109                 req, struct streams_xattr_pread_state);
1110
1111         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1112                 return -1;
1113         }
1114
1115         *vfs_aio_state = state->vfs_aio_state;
1116         return state->nread;
1117 }
1118
1119 struct streams_xattr_pwrite_state {
1120         ssize_t nwritten;
1121         struct vfs_aio_state vfs_aio_state;
1122 };
1123
1124 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1125
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)
1133 {
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);
1139
1140         req = tevent_req_create(mem_ctx, &state,
1141                                 struct streams_xattr_pwrite_state);
1142         if (req == NULL) {
1143                 return NULL;
1144         }
1145
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);
1151                 }
1152                 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1153                 return req;
1154         }
1155
1156         state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1157         if (state->nwritten != n) {
1158                 if (state->nwritten != -1) {
1159                         errno = EIO;
1160                 }
1161                 tevent_req_error(req, errno);
1162                 return tevent_req_post(req, ev);
1163         }
1164
1165         tevent_req_done(req);
1166         return tevent_req_post(req, ev);
1167 }
1168
1169 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1170 {
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);
1175
1176         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1177         TALLOC_FREE(subreq);
1178
1179         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1180                 return;
1181         }
1182         tevent_req_done(req);
1183 }
1184
1185 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1186                                          struct vfs_aio_state *vfs_aio_state)
1187 {
1188         struct streams_xattr_pwrite_state *state = tevent_req_data(
1189                 req, struct streams_xattr_pwrite_state);
1190
1191         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1192                 return -1;
1193         }
1194
1195         *vfs_aio_state = state->vfs_aio_state;
1196         return state->nwritten;
1197 }
1198
1199 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1200                                         struct files_struct *fsp,
1201                                         off_t offset)
1202 {
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
1210         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1211                    fsp_str_dbg(fsp), (double)offset));
1212
1213         if (sio == NULL) {
1214                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1215         }
1216
1217         if (!streams_xattr_recheck(sio)) {
1218                 return -1;
1219         }
1220
1221         status = get_ea_value_fsp(talloc_tos(),
1222                                   fsp->base_fsp,
1223                                   sio->xattr_name,
1224                                   &ea);
1225         if (!NT_STATUS_IS_OK(status)) {
1226                 return -1;
1227         }
1228
1229         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1230                                    offset + 1);
1231
1232         if (tmp == NULL) {
1233                 TALLOC_FREE(ea.value.data);
1234                 errno = ENOMEM;
1235                 return -1;
1236         }
1237
1238         /* Did we expand ? */
1239         if (ea.value.length < offset + 1) {
1240                 memset(&tmp[ea.value.length], '\0',
1241                         offset + 1 - ea.value.length);
1242         }
1243
1244         ea.value.data = tmp;
1245         ea.value.length = offset + 1;
1246         ea.value.data[offset] = 0;
1247
1248         ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1249                                 sio->xattr_name,
1250                                 ea.value.data,
1251                                 ea.value.length,
1252                                 0);
1253
1254         TALLOC_FREE(ea.value.data);
1255
1256         if (ret == -1) {
1257                 return -1;
1258         }
1259
1260         return 0;
1261 }
1262
1263 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1264                                         struct files_struct *fsp,
1265                                         uint32_t mode,
1266                                         off_t offset,
1267                                         off_t len)
1268 {
1269         struct stream_io *sio =
1270                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1271
1272         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1273                 "len = %.0f\n",
1274                 fsp_str_dbg(fsp), (double)offset, (double)len));
1275
1276         if (sio == NULL) {
1277                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1278         }
1279
1280         if (!streams_xattr_recheck(sio)) {
1281                 return -1;
1282         }
1283
1284         /* Let the pwrite code path handle it. */
1285         errno = ENOSYS;
1286         return -1;
1287 }
1288
1289 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1290                                 uid_t uid, gid_t gid)
1291 {
1292         struct stream_io *sio =
1293                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1294
1295         if (sio == NULL) {
1296                 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1297         }
1298
1299         return 0;
1300 }
1301
1302 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1303                                 files_struct *fsp,
1304                                 mode_t mode)
1305 {
1306         struct stream_io *sio =
1307                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1308
1309         if (sio == NULL) {
1310                 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1311         }
1312
1313         return 0;
1314 }
1315
1316 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1317                                        struct files_struct *fsp,
1318                                        const char *name,
1319                                        void *value,
1320                                        size_t size)
1321 {
1322         struct stream_io *sio =
1323                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1324
1325         if (sio == NULL) {
1326                 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1327         }
1328
1329         errno = ENOTSUP;
1330         return -1;
1331 }
1332
1333 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1334                                         struct files_struct *fsp,
1335                                         char *list,
1336                                         size_t size)
1337 {
1338         struct stream_io *sio =
1339                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1340
1341         if (sio == NULL) {
1342                 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1343         }
1344
1345         errno = ENOTSUP;
1346         return -1;
1347 }
1348
1349 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1350                                       struct files_struct *fsp,
1351                                       const char *name)
1352 {
1353         struct stream_io *sio =
1354                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1355
1356         if (sio == NULL) {
1357                 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1358         }
1359
1360         errno = ENOTSUP;
1361         return -1;
1362 }
1363
1364 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1365                                    struct files_struct *fsp,
1366                                    const char *name,
1367                                    const void *value,
1368                                    size_t size,
1369                                    int flags)
1370 {
1371         struct stream_io *sio =
1372                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1373
1374         if (sio == NULL) {
1375                 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1376                                               size, flags);
1377         }
1378
1379         errno = ENOTSUP;
1380         return -1;
1381 }
1382
1383 struct streams_xattr_fsync_state {
1384         int ret;
1385         struct vfs_aio_state vfs_aio_state;
1386 };
1387
1388 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1389
1390 static struct tevent_req *streams_xattr_fsync_send(
1391         struct vfs_handle_struct *handle,
1392         TALLOC_CTX *mem_ctx,
1393         struct tevent_context *ev,
1394         struct files_struct *fsp)
1395 {
1396         struct tevent_req *req = NULL;
1397         struct tevent_req *subreq = NULL;
1398         struct streams_xattr_fsync_state *state = NULL;
1399         struct stream_io *sio =
1400                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1401
1402         req = tevent_req_create(mem_ctx, &state,
1403                                 struct streams_xattr_fsync_state);
1404         if (req == NULL) {
1405                 return NULL;
1406         }
1407
1408         if (sio == NULL) {
1409                 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1410                 if (tevent_req_nomem(req, subreq)) {
1411                         return tevent_req_post(req, ev);
1412                 }
1413                 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1414                 return req;
1415         }
1416
1417         /*
1418          * There's no pathname based sync variant and we don't have access to
1419          * the basefile handle, so we can't do anything here.
1420          */
1421
1422         tevent_req_done(req);
1423         return tevent_req_post(req, ev);
1424 }
1425
1426 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1427 {
1428         struct tevent_req *req = tevent_req_callback_data(
1429                 subreq, struct tevent_req);
1430         struct streams_xattr_fsync_state *state = tevent_req_data(
1431                 req, struct streams_xattr_fsync_state);
1432
1433         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1434         TALLOC_FREE(subreq);
1435         if (state->ret != 0) {
1436                 tevent_req_error(req, errno);
1437                 return;
1438         }
1439
1440         tevent_req_done(req);
1441 }
1442
1443 static int streams_xattr_fsync_recv(struct tevent_req *req,
1444                                     struct vfs_aio_state *vfs_aio_state)
1445 {
1446         struct streams_xattr_fsync_state *state = tevent_req_data(
1447                 req, struct streams_xattr_fsync_state);
1448
1449         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1450                 return -1;
1451         }
1452
1453         *vfs_aio_state = state->vfs_aio_state;
1454         return state->ret;
1455 }
1456
1457 static bool streams_xattr_lock(vfs_handle_struct *handle,
1458                                files_struct *fsp,
1459                                int op,
1460                                off_t offset,
1461                                off_t count,
1462                                int type)
1463 {
1464         struct stream_io *sio =
1465                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1466
1467         if (sio == NULL) {
1468                 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1469         }
1470
1471         return true;
1472 }
1473
1474 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1475                                   files_struct *fsp,
1476                                   off_t *poffset,
1477                                   off_t *pcount,
1478                                   int *ptype,
1479                                   pid_t *ppid)
1480 {
1481         struct stream_io *sio =
1482                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1483
1484         if (sio == NULL) {
1485                 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1486                                             pcount, ptype, ppid);
1487         }
1488
1489         errno = ENOTSUP;
1490         return false;
1491 }
1492
1493 static int streams_xattr_filesystem_sharemode(vfs_handle_struct *handle,
1494                                               files_struct *fsp,
1495                                               uint32_t share_access,
1496                                               uint32_t access_mask)
1497 {
1498         struct stream_io *sio =
1499                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1500
1501         if (sio == NULL) {
1502                 return SMB_VFS_NEXT_FILESYSTEM_SHAREMODE(handle,
1503                                                          fsp,
1504                                                          share_access,
1505                                                          access_mask);
1506         }
1507
1508         return 0;
1509 }
1510
1511 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1512                                         files_struct *fsp,
1513                                         int leasetype)
1514 {
1515         struct stream_io *sio =
1516                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1517
1518         if (sio == NULL) {
1519                 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1520         }
1521
1522         return 0;
1523 }
1524
1525 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1526                                             files_struct *fsp,
1527                                             struct lock_struct *plock)
1528 {
1529         struct stream_io *sio =
1530                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1531
1532         if (sio == NULL) {
1533                 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1534         }
1535
1536         return true;
1537 }
1538
1539 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1540         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1541         .connect_fn = streams_xattr_connect,
1542         .openat_fn = streams_xattr_openat,
1543         .close_fn = streams_xattr_close,
1544         .stat_fn = streams_xattr_stat,
1545         .fstat_fn = streams_xattr_fstat,
1546         .lstat_fn = streams_xattr_lstat,
1547         .pread_fn = streams_xattr_pread,
1548         .pwrite_fn = streams_xattr_pwrite,
1549         .pread_send_fn = streams_xattr_pread_send,
1550         .pread_recv_fn = streams_xattr_pread_recv,
1551         .pwrite_send_fn = streams_xattr_pwrite_send,
1552         .pwrite_recv_fn = streams_xattr_pwrite_recv,
1553         .unlinkat_fn = streams_xattr_unlinkat,
1554         .renameat_fn = streams_xattr_renameat,
1555         .ftruncate_fn = streams_xattr_ftruncate,
1556         .fallocate_fn = streams_xattr_fallocate,
1557         .fstreaminfo_fn = streams_xattr_fstreaminfo,
1558
1559         .fsync_send_fn = streams_xattr_fsync_send,
1560         .fsync_recv_fn = streams_xattr_fsync_recv,
1561
1562         .lock_fn = streams_xattr_lock,
1563         .getlock_fn = streams_xattr_getlock,
1564         .filesystem_sharemode_fn = streams_xattr_filesystem_sharemode,
1565         .linux_setlease_fn = streams_xattr_linux_setlease,
1566         .strict_lock_check_fn = streams_xattr_strict_lock_check,
1567
1568         .fchown_fn = streams_xattr_fchown,
1569         .fchmod_fn = streams_xattr_fchmod,
1570
1571         .fgetxattr_fn = streams_xattr_fgetxattr,
1572         .flistxattr_fn = streams_xattr_flistxattr,
1573         .fremovexattr_fn = streams_xattr_fremovexattr,
1574         .fsetxattr_fn = streams_xattr_fsetxattr,
1575 };
1576
1577 static_decl_vfs;
1578 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1579 {
1580         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1581                                 &vfs_streams_xattr_fns);
1582 }