5fdd1bec77303c389ddad16b9fd08d7044c88cf7
[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 = NULL;
593
594         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
595         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
596
597         if (!src_is_stream && !dst_is_stream) {
598                 return SMB_VFS_NEXT_RENAMEAT(handle,
599                                         srcfsp,
600                                         smb_fname_src,
601                                         dstfsp,
602                                         smb_fname_dst);
603         }
604
605         /* For now don't allow renames from or to the default stream. */
606         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
607             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
608                 errno = ENOSYS;
609                 goto done;
610         }
611
612         /* Don't rename if the streams are identical. */
613         if (strcasecmp_m(smb_fname_src->stream_name,
614                        smb_fname_dst->stream_name) == 0) {
615                 goto done;
616         }
617
618         /* Get the xattr names. */
619         status = streams_xattr_get_name(handle, talloc_tos(),
620                                         smb_fname_src->stream_name,
621                                         &src_xattr_name);
622         if (!NT_STATUS_IS_OK(status)) {
623                 errno = map_errno_from_nt_status(status);
624                 goto fail;
625         }
626         status = streams_xattr_get_name(handle, talloc_tos(),
627                                         smb_fname_dst->stream_name,
628                                         &dst_xattr_name);
629         if (!NT_STATUS_IS_OK(status)) {
630                 errno = map_errno_from_nt_status(status);
631                 goto fail;
632         }
633
634         /* read the old stream */
635         status = get_ea_value(talloc_tos(), handle->conn, NULL,
636                               smb_fname_src, src_xattr_name, &ea);
637         if (!NT_STATUS_IS_OK(status)) {
638                 errno = map_errno_from_nt_status(status);
639                 goto fail;
640         }
641
642         if (smb_fname_dst->fsp == NULL) {
643                 status = synthetic_pathref(talloc_tos(),
644                                 handle->conn->cwd_fsp,
645                                 smb_fname_dst->base_name,
646                                 NULL,
647                                 NULL,
648                                 smb_fname_dst->twrp,
649                                 smb_fname_dst->flags,
650                                 &pathref);
651                 if (!NT_STATUS_IS_OK(status)) {
652                         errno = ENOENT;
653                         goto fail;
654                 }
655                 smb_fname_dst = pathref;
656         }
657         /* (over)write the new stream */
658         nret = SMB_VFS_FSETXATTR(
659                         smb_fname_dst->fsp,
660                         dst_xattr_name, ea.value.data, ea.value.length,
661                         0);
662         if (nret < 0) {
663                 if (errno == ENOATTR) {
664                         errno = ENOENT;
665                 }
666                 goto fail;
667         }
668
669         /*
670          * remove the old stream.
671          * Note that now we're doing this by handle
672          * not by pathname we must do it on the base_fsp,
673          * as we have to remove the actual xattr on the base file.
674          */
675         SMB_ASSERT(smb_fname_src->fsp->base_fsp != NULL);
676
677         oret = SMB_VFS_FREMOVEXATTR(smb_fname_src->fsp->base_fsp,
678                                    src_xattr_name);
679         if (oret < 0) {
680                 if (errno == ENOATTR) {
681                         errno = ENOENT;
682                 }
683                 goto fail;
684         }
685
686  done:
687         errno = 0;
688         ret = 0;
689  fail:
690         TALLOC_FREE(pathref);
691         TALLOC_FREE(src_xattr_name);
692         TALLOC_FREE(dst_xattr_name);
693         return ret;
694 }
695
696 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
697                                 files_struct *fsp,
698                                 const struct smb_filename *smb_fname,
699                                 bool (*fn)(struct ea_struct *ea,
700                                         void *private_data),
701                                 void *private_data)
702 {
703         NTSTATUS status;
704         char **names;
705         size_t i, num_names;
706         struct streams_xattr_config *config;
707
708         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
709                                 return NT_STATUS_UNSUCCESSFUL);
710
711         status = get_ea_names_from_file(talloc_tos(),
712                                 smb_fname->fsp,
713                                 &names,
714                                 &num_names);
715         if (!NT_STATUS_IS_OK(status)) {
716                 return status;
717         }
718
719         for (i=0; i<num_names; i++) {
720                 struct ea_struct ea;
721
722                 /*
723                  * We want to check with samba_private_attr_name()
724                  * whether the xattr name is a private one,
725                  * unfortunately it flags xattrs that begin with the
726                  * default streams prefix as private.
727                  *
728                  * By only calling samba_private_attr_name() in case
729                  * the xattr does NOT begin with the default prefix,
730                  * we know that if it returns 'true' it definitely one
731                  * of our internal xattr like "user.DOSATTRIB".
732                  */
733                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
734                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
735                         if (samba_private_attr_name(names[i])) {
736                                 continue;
737                         }
738                 }
739
740                 if (strncmp(names[i], config->prefix,
741                             config->prefix_len) != 0) {
742                         continue;
743                 }
744
745                 status = get_ea_value(names,
746                                         handle->conn,
747                                         NULL,
748                                         smb_fname,
749                                         names[i],
750                                         &ea);
751                 if (!NT_STATUS_IS_OK(status)) {
752                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
753                                 names[i],
754                                 smb_fname->base_name,
755                                 nt_errstr(status)));
756                         continue;
757                 }
758
759                 ea.name = talloc_asprintf(
760                         ea.value.data, ":%s%s",
761                         names[i] + config->prefix_len,
762                         config->store_stream_type ? "" : ":$DATA");
763                 if (ea.name == NULL) {
764                         DEBUG(0, ("talloc failed\n"));
765                         continue;
766                 }
767
768                 if (!fn(&ea, private_data)) {
769                         TALLOC_FREE(ea.value.data);
770                         return NT_STATUS_OK;
771                 }
772
773                 TALLOC_FREE(ea.value.data);
774         }
775
776         TALLOC_FREE(names);
777         return NT_STATUS_OK;
778 }
779
780 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
781                            struct stream_struct **streams,
782                            const char *name, off_t size,
783                            off_t alloc_size)
784 {
785         struct stream_struct *tmp;
786
787         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
788                                    (*num_streams)+1);
789         if (tmp == NULL) {
790                 return false;
791         }
792
793         tmp[*num_streams].name = talloc_strdup(tmp, name);
794         if (tmp[*num_streams].name == NULL) {
795                 return false;
796         }
797
798         tmp[*num_streams].size = size;
799         tmp[*num_streams].alloc_size = alloc_size;
800
801         *streams = tmp;
802         *num_streams += 1;
803         return true;
804 }
805
806 struct streaminfo_state {
807         TALLOC_CTX *mem_ctx;
808         vfs_handle_struct *handle;
809         unsigned int num_streams;
810         struct stream_struct *streams;
811         NTSTATUS status;
812 };
813
814 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
815 {
816         struct streaminfo_state *state =
817                 (struct streaminfo_state *)private_data;
818
819         if (!add_one_stream(state->mem_ctx,
820                             &state->num_streams, &state->streams,
821                             ea->name, ea->value.length-1,
822                             smb_roundup(state->handle->conn,
823                                         ea->value.length-1))) {
824                 state->status = NT_STATUS_NO_MEMORY;
825                 return false;
826         }
827
828         return true;
829 }
830
831 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
832                                          struct files_struct *fsp,
833                                          const struct smb_filename *smb_fname,
834                                          TALLOC_CTX *mem_ctx,
835                                          unsigned int *pnum_streams,
836                                          struct stream_struct **pstreams)
837 {
838         SMB_STRUCT_STAT sbuf;
839         int ret;
840         NTSTATUS status;
841         struct streaminfo_state state;
842
843         ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
844         if (ret == -1) {
845                 return map_nt_error_from_unix(errno);
846         }
847
848         state.streams = *pstreams;
849         state.num_streams = *pnum_streams;
850         state.mem_ctx = mem_ctx;
851         state.handle = handle;
852         state.status = NT_STATUS_OK;
853
854         if (S_ISLNK(sbuf.st_ex_mode)) {
855                 /*
856                  * Currently we do't have SMB_VFS_LLISTXATTR
857                  * inside the VFS which means there's no way
858                  * to cope with a symlink when lp_posix_pathnames().
859                  * returns true. For now ignore links.
860                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
861                  */
862                 status = NT_STATUS_OK;
863         } else {
864                 status = walk_xattr_streams(handle, fsp, smb_fname,
865                                     collect_one_stream, &state);
866         }
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_STREAMINFO(handle,
882                         fsp,
883                         smb_fname,
884                         mem_ctx,
885                         pnum_streams,
886                         pstreams);
887 }
888
889 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
890                                          struct files_struct *fsp,
891                                          TALLOC_CTX *mem_ctx,
892                                          unsigned int *pnum_streams,
893                                          struct stream_struct **pstreams)
894 {
895         NTSTATUS status;
896         struct streaminfo_state state;
897
898         state.streams = *pstreams;
899         state.num_streams = *pnum_streams;
900         state.mem_ctx = mem_ctx;
901         state.handle = handle;
902         state.status = NT_STATUS_OK;
903
904         status = walk_xattr_streams(handle,
905                                     fsp,
906                                     fsp->fsp_name,
907                                     collect_one_stream,
908                                     &state);
909
910         if (!NT_STATUS_IS_OK(status)) {
911                 TALLOC_FREE(state.streams);
912                 return status;
913         }
914
915         if (!NT_STATUS_IS_OK(state.status)) {
916                 TALLOC_FREE(state.streams);
917                 return state.status;
918         }
919
920         *pnum_streams = state.num_streams;
921         *pstreams = state.streams;
922
923         return SMB_VFS_NEXT_FSTREAMINFO(handle,
924                         fsp,
925                         mem_ctx,
926                         pnum_streams,
927                         pstreams);
928 }
929
930 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
931                         enum timestamp_set_resolution *p_ts_res)
932 {
933         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
934 }
935
936 static int streams_xattr_connect(vfs_handle_struct *handle,
937                                  const char *service, const char *user)
938 {
939         struct streams_xattr_config *config;
940         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
941         const char *prefix;
942         int rc;
943
944         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
945         if (rc != 0) {
946                 return rc;
947         }
948
949         config = talloc_zero(handle->conn, struct streams_xattr_config);
950         if (config == NULL) {
951                 DEBUG(1, ("talloc_zero() failed\n"));
952                 errno = ENOMEM;
953                 return -1;
954         }
955
956         prefix = lp_parm_const_string(SNUM(handle->conn),
957                                       "streams_xattr", "prefix",
958                                       default_prefix);
959         config->prefix = talloc_strdup(config, prefix);
960         if (config->prefix == NULL) {
961                 DEBUG(1, ("talloc_strdup() failed\n"));
962                 errno = ENOMEM;
963                 return -1;
964         }
965         config->prefix_len = strlen(config->prefix);
966         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
967
968         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
969                                                  "streams_xattr",
970                                                  "store_stream_type",
971                                                  true);
972
973         SMB_VFS_HANDLE_SET_DATA(handle, config,
974                                 NULL, struct stream_xattr_config,
975                                 return -1);
976
977         return 0;
978 }
979
980 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
981                                     files_struct *fsp, const void *data,
982                                     size_t n, off_t offset)
983 {
984         struct stream_io *sio =
985                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
986         struct ea_struct ea;
987         NTSTATUS status;
988         struct smb_filename *smb_fname_base = NULL;
989         int ret;
990
991         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
992
993         if (sio == NULL) {
994                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
995         }
996
997         if (!streams_xattr_recheck(sio)) {
998                 return -1;
999         }
1000
1001         if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
1002                 /*
1003                  * Requested write is beyond what can be read based on
1004                  * samba configuration.
1005                  * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
1006                  * entire file to be skipped by File Explorer. VFAT returns
1007                  * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
1008                  * to skip writing metadata, but copy data.
1009                  */
1010                 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
1011                         "supported extended attribute size. "
1012                         "Depending on filesystem type and operating system "
1013                         "(OS) specifics, this value may be increased using "
1014                         "the value of the parameter: "
1015                         "smbd max xattr size = <bytes>. Consult OS and "
1016                         "filesystem manpages prior to increasing this limit.\n",
1017                         sio->xattr_name, sio->base);
1018                 errno = EOVERFLOW;
1019                 return -1;
1020         }
1021
1022         /* Create an smb_filename with stream_name == NULL. */
1023         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1024                                         sio->base,
1025                                         NULL,
1026                                         NULL,
1027                                         fsp->fsp_name->twrp,
1028                                         fsp->fsp_name->flags);
1029         if (smb_fname_base == NULL) {
1030                 errno = ENOMEM;
1031                 return -1;
1032         }
1033
1034         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1035                               smb_fname_base, sio->xattr_name, &ea);
1036         if (!NT_STATUS_IS_OK(status)) {
1037                 return -1;
1038         }
1039
1040         if ((offset + n) > ea.value.length-1) {
1041                 uint8_t *tmp;
1042
1043                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1044                                            offset + n + 1);
1045
1046                 if (tmp == NULL) {
1047                         TALLOC_FREE(ea.value.data);
1048                         errno = ENOMEM;
1049                         return -1;
1050                 }
1051                 ea.value.data = tmp;
1052                 ea.value.length = offset + n + 1;
1053                 ea.value.data[offset+n] = 0;
1054         }
1055
1056         memcpy(ea.value.data + offset, data, n);
1057
1058         ret = SMB_VFS_FSETXATTR(fsp->base_fsp ? fsp->base_fsp : fsp,
1059                                sio->xattr_name,
1060                                ea.value.data, ea.value.length, 0);
1061         TALLOC_FREE(ea.value.data);
1062
1063         if (ret == -1) {
1064                 return -1;
1065         }
1066
1067         return n;
1068 }
1069
1070 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1071                                    files_struct *fsp, void *data,
1072                                    size_t n, off_t offset)
1073 {
1074         struct stream_io *sio =
1075                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1076         struct ea_struct ea;
1077         NTSTATUS status;
1078         size_t length, overlap;
1079         struct smb_filename *smb_fname_base = NULL;
1080
1081         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1082                    (int)offset, (int)n));
1083
1084         if (sio == NULL) {
1085                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1086         }
1087
1088         if (!streams_xattr_recheck(sio)) {
1089                 return -1;
1090         }
1091
1092         /* Create an smb_filename with stream_name == NULL. */
1093         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1094                                         sio->base,
1095                                         NULL,
1096                                         NULL,
1097                                         fsp->fsp_name->twrp,
1098                                         fsp->fsp_name->flags);
1099         if (smb_fname_base == NULL) {
1100                 errno = ENOMEM;
1101                 return -1;
1102         }
1103
1104         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1105                               smb_fname_base, sio->xattr_name, &ea);
1106         if (!NT_STATUS_IS_OK(status)) {
1107                 return -1;
1108         }
1109
1110         length = ea.value.length-1;
1111
1112         DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1113                    (int)length));
1114
1115         /* Attempt to read past EOF. */
1116         if (length <= offset) {
1117                 return 0;
1118         }
1119
1120         overlap = (offset + n) > length ? (length - offset) : n;
1121         memcpy(data, ea.value.data + offset, overlap);
1122
1123         TALLOC_FREE(ea.value.data);
1124         return overlap;
1125 }
1126
1127 struct streams_xattr_pread_state {
1128         ssize_t nread;
1129         struct vfs_aio_state vfs_aio_state;
1130 };
1131
1132 static void streams_xattr_pread_done(struct tevent_req *subreq);
1133
1134 static struct tevent_req *streams_xattr_pread_send(
1135         struct vfs_handle_struct *handle,
1136         TALLOC_CTX *mem_ctx,
1137         struct tevent_context *ev,
1138         struct files_struct *fsp,
1139         void *data,
1140         size_t n, off_t offset)
1141 {
1142         struct tevent_req *req = NULL;
1143         struct tevent_req *subreq = NULL;
1144         struct streams_xattr_pread_state *state = NULL;
1145         struct stream_io *sio =
1146                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1147
1148         req = tevent_req_create(mem_ctx, &state,
1149                                 struct streams_xattr_pread_state);
1150         if (req == NULL) {
1151                 return NULL;
1152         }
1153
1154         if (sio == NULL) {
1155                 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1156                                                  data, n, offset);
1157                 if (tevent_req_nomem(req, subreq)) {
1158                         return tevent_req_post(req, ev);
1159                 }
1160                 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1161                 return req;
1162         }
1163
1164         state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1165         if (state->nread != n) {
1166                 if (state->nread != -1) {
1167                         errno = EIO;
1168                 }
1169                 tevent_req_error(req, errno);
1170                 return tevent_req_post(req, ev);
1171         }
1172
1173         tevent_req_done(req);
1174         return tevent_req_post(req, ev);
1175 }
1176
1177 static void streams_xattr_pread_done(struct tevent_req *subreq)
1178 {
1179         struct tevent_req *req = tevent_req_callback_data(
1180                 subreq, struct tevent_req);
1181         struct streams_xattr_pread_state *state = tevent_req_data(
1182                 req, struct streams_xattr_pread_state);
1183
1184         state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1185         TALLOC_FREE(subreq);
1186
1187         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1188                 return;
1189         }
1190         tevent_req_done(req);
1191 }
1192
1193 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1194                                         struct vfs_aio_state *vfs_aio_state)
1195 {
1196         struct streams_xattr_pread_state *state = tevent_req_data(
1197                 req, struct streams_xattr_pread_state);
1198
1199         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1200                 return -1;
1201         }
1202
1203         *vfs_aio_state = state->vfs_aio_state;
1204         return state->nread;
1205 }
1206
1207 struct streams_xattr_pwrite_state {
1208         ssize_t nwritten;
1209         struct vfs_aio_state vfs_aio_state;
1210 };
1211
1212 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1213
1214 static struct tevent_req *streams_xattr_pwrite_send(
1215         struct vfs_handle_struct *handle,
1216         TALLOC_CTX *mem_ctx,
1217         struct tevent_context *ev,
1218         struct files_struct *fsp,
1219         const void *data,
1220         size_t n, off_t offset)
1221 {
1222         struct tevent_req *req = NULL;
1223         struct tevent_req *subreq = NULL;
1224         struct streams_xattr_pwrite_state *state = NULL;
1225         struct stream_io *sio =
1226                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1227
1228         req = tevent_req_create(mem_ctx, &state,
1229                                 struct streams_xattr_pwrite_state);
1230         if (req == NULL) {
1231                 return NULL;
1232         }
1233
1234         if (sio == NULL) {
1235                 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1236                                                   data, n, offset);
1237                 if (tevent_req_nomem(req, subreq)) {
1238                         return tevent_req_post(req, ev);
1239                 }
1240                 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1241                 return req;
1242         }
1243
1244         state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1245         if (state->nwritten != n) {
1246                 if (state->nwritten != -1) {
1247                         errno = EIO;
1248                 }
1249                 tevent_req_error(req, errno);
1250                 return tevent_req_post(req, ev);
1251         }
1252
1253         tevent_req_done(req);
1254         return tevent_req_post(req, ev);
1255 }
1256
1257 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1258 {
1259         struct tevent_req *req = tevent_req_callback_data(
1260                 subreq, struct tevent_req);
1261         struct streams_xattr_pwrite_state *state = tevent_req_data(
1262                 req, struct streams_xattr_pwrite_state);
1263
1264         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1265         TALLOC_FREE(subreq);
1266
1267         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1268                 return;
1269         }
1270         tevent_req_done(req);
1271 }
1272
1273 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1274                                          struct vfs_aio_state *vfs_aio_state)
1275 {
1276         struct streams_xattr_pwrite_state *state = tevent_req_data(
1277                 req, struct streams_xattr_pwrite_state);
1278
1279         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1280                 return -1;
1281         }
1282
1283         *vfs_aio_state = state->vfs_aio_state;
1284         return state->nwritten;
1285 }
1286
1287 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1288                                         struct files_struct *fsp,
1289                                         off_t offset)
1290 {
1291         int ret;
1292         uint8_t *tmp;
1293         struct ea_struct ea;
1294         NTSTATUS status;
1295         struct stream_io *sio =
1296                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1297         struct smb_filename *smb_fname_base = NULL;
1298
1299         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1300                    fsp_str_dbg(fsp), (double)offset));
1301
1302         if (sio == NULL) {
1303                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1304         }
1305
1306         if (!streams_xattr_recheck(sio)) {
1307                 return -1;
1308         }
1309
1310         /* Create an smb_filename with stream_name == NULL. */
1311         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1312                                         sio->base,
1313                                         NULL,
1314                                         NULL,
1315                                         fsp->fsp_name->twrp,
1316                                         fsp->fsp_name->flags);
1317         if (smb_fname_base == NULL) {
1318                 errno = ENOMEM;
1319                 return -1;
1320         }
1321
1322         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1323                               smb_fname_base, sio->xattr_name, &ea);
1324         if (!NT_STATUS_IS_OK(status)) {
1325                 return -1;
1326         }
1327
1328         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1329                                    offset + 1);
1330
1331         if (tmp == NULL) {
1332                 TALLOC_FREE(ea.value.data);
1333                 errno = ENOMEM;
1334                 return -1;
1335         }
1336
1337         /* Did we expand ? */
1338         if (ea.value.length < offset + 1) {
1339                 memset(&tmp[ea.value.length], '\0',
1340                         offset + 1 - ea.value.length);
1341         }
1342
1343         ea.value.data = tmp;
1344         ea.value.length = offset + 1;
1345         ea.value.data[offset] = 0;
1346
1347         ret = SMB_VFS_FSETXATTR(fsp->base_fsp ? fsp->base_fsp : fsp,
1348                                sio->xattr_name,
1349                                ea.value.data, ea.value.length, 0);
1350
1351         TALLOC_FREE(ea.value.data);
1352
1353         if (ret == -1) {
1354                 return -1;
1355         }
1356
1357         return 0;
1358 }
1359
1360 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1361                                         struct files_struct *fsp,
1362                                         uint32_t mode,
1363                                         off_t offset,
1364                                         off_t len)
1365 {
1366         struct stream_io *sio =
1367                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1368
1369         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1370                 "len = %.0f\n",
1371                 fsp_str_dbg(fsp), (double)offset, (double)len));
1372
1373         if (sio == NULL) {
1374                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1375         }
1376
1377         if (!streams_xattr_recheck(sio)) {
1378                 return -1;
1379         }
1380
1381         /* Let the pwrite code path handle it. */
1382         errno = ENOSYS;
1383         return -1;
1384 }
1385
1386 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1387                                 uid_t uid, gid_t gid)
1388 {
1389         struct stream_io *sio =
1390                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1391
1392         if (sio == NULL) {
1393                 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1394         }
1395
1396         return 0;
1397 }
1398
1399 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1400                                 files_struct *fsp,
1401                                 mode_t mode)
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_FCHMOD(handle, fsp, mode);
1408         }
1409
1410         return 0;
1411 }
1412
1413 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1414                                        struct files_struct *fsp,
1415                                        const char *name,
1416                                        void *value,
1417                                        size_t size)
1418 {
1419         struct stream_io *sio =
1420                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1421
1422         if (sio == NULL) {
1423                 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1424         }
1425
1426         errno = ENOTSUP;
1427         return -1;
1428 }
1429
1430 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1431                                         struct files_struct *fsp,
1432                                         char *list,
1433                                         size_t size)
1434 {
1435         struct stream_io *sio =
1436                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1437
1438         if (sio == NULL) {
1439                 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1440         }
1441
1442         errno = ENOTSUP;
1443         return -1;
1444 }
1445
1446 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1447                                       struct files_struct *fsp,
1448                                       const char *name)
1449 {
1450         struct stream_io *sio =
1451                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1452
1453         if (sio == NULL) {
1454                 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1455         }
1456
1457         errno = ENOTSUP;
1458         return -1;
1459 }
1460
1461 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1462                                    struct files_struct *fsp,
1463                                    const char *name,
1464                                    const void *value,
1465                                    size_t size,
1466                                    int flags)
1467 {
1468         struct stream_io *sio =
1469                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1470
1471         if (sio == NULL) {
1472                 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1473                                               size, flags);
1474         }
1475
1476         errno = ENOTSUP;
1477         return -1;
1478 }
1479
1480 struct streams_xattr_fsync_state {
1481         int ret;
1482         struct vfs_aio_state vfs_aio_state;
1483 };
1484
1485 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1486
1487 static struct tevent_req *streams_xattr_fsync_send(
1488         struct vfs_handle_struct *handle,
1489         TALLOC_CTX *mem_ctx,
1490         struct tevent_context *ev,
1491         struct files_struct *fsp)
1492 {
1493         struct tevent_req *req = NULL;
1494         struct tevent_req *subreq = NULL;
1495         struct streams_xattr_fsync_state *state = NULL;
1496         struct stream_io *sio =
1497                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1498
1499         req = tevent_req_create(mem_ctx, &state,
1500                                 struct streams_xattr_fsync_state);
1501         if (req == NULL) {
1502                 return NULL;
1503         }
1504
1505         if (sio == NULL) {
1506                 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1507                 if (tevent_req_nomem(req, subreq)) {
1508                         return tevent_req_post(req, ev);
1509                 }
1510                 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1511                 return req;
1512         }
1513
1514         /*
1515          * There's no pathname based sync variant and we don't have access to
1516          * the basefile handle, so we can't do anything here.
1517          */
1518
1519         tevent_req_done(req);
1520         return tevent_req_post(req, ev);
1521 }
1522
1523 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1524 {
1525         struct tevent_req *req = tevent_req_callback_data(
1526                 subreq, struct tevent_req);
1527         struct streams_xattr_fsync_state *state = tevent_req_data(
1528                 req, struct streams_xattr_fsync_state);
1529
1530         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1531         TALLOC_FREE(subreq);
1532         if (state->ret != 0) {
1533                 tevent_req_error(req, errno);
1534                 return;
1535         }
1536
1537         tevent_req_done(req);
1538 }
1539
1540 static int streams_xattr_fsync_recv(struct tevent_req *req,
1541                                     struct vfs_aio_state *vfs_aio_state)
1542 {
1543         struct streams_xattr_fsync_state *state = tevent_req_data(
1544                 req, struct streams_xattr_fsync_state);
1545
1546         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1547                 return -1;
1548         }
1549
1550         *vfs_aio_state = state->vfs_aio_state;
1551         return state->ret;
1552 }
1553
1554 static bool streams_xattr_lock(vfs_handle_struct *handle,
1555                                files_struct *fsp,
1556                                int op,
1557                                off_t offset,
1558                                off_t count,
1559                                int type)
1560 {
1561         struct stream_io *sio =
1562                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1563
1564         if (sio == NULL) {
1565                 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1566         }
1567
1568         return true;
1569 }
1570
1571 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1572                                   files_struct *fsp,
1573                                   off_t *poffset,
1574                                   off_t *pcount,
1575                                   int *ptype,
1576                                   pid_t *ppid)
1577 {
1578         struct stream_io *sio =
1579                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1580
1581         if (sio == NULL) {
1582                 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1583                                             pcount, ptype, ppid);
1584         }
1585
1586         errno = ENOTSUP;
1587         return false;
1588 }
1589
1590 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1591                                       files_struct *fsp,
1592                                       uint32_t share_access,
1593                                       uint32_t access_mask)
1594 {
1595         struct stream_io *sio =
1596                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1597
1598         if (sio == NULL) {
1599                 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1600                                                  share_access, access_mask);
1601         }
1602
1603         return 0;
1604 }
1605
1606 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1607                                         files_struct *fsp,
1608                                         int leasetype)
1609 {
1610         struct stream_io *sio =
1611                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1612
1613         if (sio == NULL) {
1614                 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1615         }
1616
1617         return 0;
1618 }
1619
1620 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1621                                             files_struct *fsp,
1622                                             struct lock_struct *plock)
1623 {
1624         struct stream_io *sio =
1625                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1626
1627         if (sio == NULL) {
1628                 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1629         }
1630
1631         return true;
1632 }
1633
1634 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1635         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1636         .connect_fn = streams_xattr_connect,
1637         .openat_fn = streams_xattr_openat,
1638         .close_fn = streams_xattr_close,
1639         .stat_fn = streams_xattr_stat,
1640         .fstat_fn = streams_xattr_fstat,
1641         .lstat_fn = streams_xattr_lstat,
1642         .pread_fn = streams_xattr_pread,
1643         .pwrite_fn = streams_xattr_pwrite,
1644         .pread_send_fn = streams_xattr_pread_send,
1645         .pread_recv_fn = streams_xattr_pread_recv,
1646         .pwrite_send_fn = streams_xattr_pwrite_send,
1647         .pwrite_recv_fn = streams_xattr_pwrite_recv,
1648         .unlinkat_fn = streams_xattr_unlinkat,
1649         .renameat_fn = streams_xattr_renameat,
1650         .ftruncate_fn = streams_xattr_ftruncate,
1651         .fallocate_fn = streams_xattr_fallocate,
1652         .streaminfo_fn = streams_xattr_streaminfo,
1653         .fstreaminfo_fn = streams_xattr_fstreaminfo,
1654
1655         .fsync_send_fn = streams_xattr_fsync_send,
1656         .fsync_recv_fn = streams_xattr_fsync_recv,
1657
1658         .lock_fn = streams_xattr_lock,
1659         .getlock_fn = streams_xattr_getlock,
1660         .kernel_flock_fn = streams_xattr_kernel_flock,
1661         .linux_setlease_fn = streams_xattr_linux_setlease,
1662         .strict_lock_check_fn = streams_xattr_strict_lock_check,
1663
1664         .fchown_fn = streams_xattr_fchown,
1665         .fchmod_fn = streams_xattr_fchmod,
1666
1667         .fgetxattr_fn = streams_xattr_fgetxattr,
1668         .flistxattr_fn = streams_xattr_flistxattr,
1669         .fremovexattr_fn = streams_xattr_fremovexattr,
1670         .fsetxattr_fn = streams_xattr_fsetxattr,
1671 };
1672
1673 static_decl_vfs;
1674 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1675 {
1676         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1677                                 &vfs_streams_xattr_fns);
1678 }