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