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