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