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