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