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