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