s3: VFS: vfs_streams_xattr. Implement unlinkat().
[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_unlinkat(vfs_handle_struct *handle,
598                         struct files_struct *dirfsp,
599                         const struct smb_filename *smb_fname,
600                         int flags)
601 {
602         int ret;
603         if (flags & AT_REMOVEDIR) {
604                 ret = SMB_VFS_NEXT_UNLINKAT(handle,
605                                 dirfsp,
606                                 smb_fname,
607                                 flags);
608         } else {
609                 ret = streams_xattr_unlink(handle,
610                                 smb_fname);
611         }
612         return ret;
613 }
614
615 static int streams_xattr_renameat(vfs_handle_struct *handle,
616                                 files_struct *srcfsp,
617                                 const struct smb_filename *smb_fname_src,
618                                 files_struct *dstfsp,
619                                 const struct smb_filename *smb_fname_dst)
620 {
621         NTSTATUS status;
622         int ret = -1;
623         char *src_xattr_name = NULL;
624         char *dst_xattr_name = NULL;
625         bool src_is_stream, dst_is_stream;
626         ssize_t oret;
627         ssize_t nret;
628         struct ea_struct ea;
629
630         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
631         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
632
633         if (!src_is_stream && !dst_is_stream) {
634                 return SMB_VFS_NEXT_RENAMEAT(handle,
635                                         srcfsp,
636                                         smb_fname_src,
637                                         dstfsp,
638                                         smb_fname_dst);
639         }
640
641         /* For now don't allow renames from or to the default stream. */
642         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
643             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
644                 errno = ENOSYS;
645                 goto done;
646         }
647
648         /* Don't rename if the streams are identical. */
649         if (strcasecmp_m(smb_fname_src->stream_name,
650                        smb_fname_dst->stream_name) == 0) {
651                 goto done;
652         }
653
654         /* Get the xattr names. */
655         status = streams_xattr_get_name(handle, talloc_tos(),
656                                         smb_fname_src->stream_name,
657                                         &src_xattr_name);
658         if (!NT_STATUS_IS_OK(status)) {
659                 errno = map_errno_from_nt_status(status);
660                 goto fail;
661         }
662         status = streams_xattr_get_name(handle, talloc_tos(),
663                                         smb_fname_dst->stream_name,
664                                         &dst_xattr_name);
665         if (!NT_STATUS_IS_OK(status)) {
666                 errno = map_errno_from_nt_status(status);
667                 goto fail;
668         }
669
670         /* read the old stream */
671         status = get_ea_value(talloc_tos(), handle->conn, NULL,
672                               smb_fname_src, src_xattr_name, &ea);
673         if (!NT_STATUS_IS_OK(status)) {
674                 errno = ENOENT;
675                 goto fail;
676         }
677
678         /* (over)write the new stream */
679         nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
680                                 dst_xattr_name, ea.value.data, ea.value.length,
681                                 0);
682         if (nret < 0) {
683                 if (errno == ENOATTR) {
684                         errno = ENOENT;
685                 }
686                 goto fail;
687         }
688
689         /* remove the old stream */
690         oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
691                                    src_xattr_name);
692         if (oret < 0) {
693                 if (errno == ENOATTR) {
694                         errno = ENOENT;
695                 }
696                 goto fail;
697         }
698
699  done:
700         errno = 0;
701         ret = 0;
702  fail:
703         TALLOC_FREE(src_xattr_name);
704         TALLOC_FREE(dst_xattr_name);
705         return ret;
706 }
707
708 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
709                                 files_struct *fsp,
710                                 const struct smb_filename *smb_fname,
711                                 bool (*fn)(struct ea_struct *ea,
712                                         void *private_data),
713                                 void *private_data)
714 {
715         NTSTATUS status;
716         char **names;
717         size_t i, num_names;
718         struct streams_xattr_config *config;
719
720         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
721                                 return NT_STATUS_UNSUCCESSFUL);
722
723         status = get_ea_names_from_file(talloc_tos(),
724                                 handle->conn,
725                                 fsp,
726                                 smb_fname,
727                                 &names,
728                                 &num_names);
729         if (!NT_STATUS_IS_OK(status)) {
730                 return status;
731         }
732
733         for (i=0; i<num_names; i++) {
734                 struct ea_struct ea;
735
736                 /*
737                  * We want to check with samba_private_attr_name()
738                  * whether the xattr name is a private one,
739                  * unfortunately it flags xattrs that begin with the
740                  * default streams prefix as private.
741                  *
742                  * By only calling samba_private_attr_name() in case
743                  * the xattr does NOT begin with the default prefix,
744                  * we know that if it returns 'true' it definitely one
745                  * of our internal xattr like "user.DOSATTRIB".
746                  */
747                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
748                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
749                         if (samba_private_attr_name(names[i])) {
750                                 continue;
751                         }
752                 }
753
754                 if (strncmp(names[i], config->prefix,
755                             config->prefix_len) != 0) {
756                         continue;
757                 }
758
759                 status = get_ea_value(names,
760                                         handle->conn,
761                                         NULL,
762                                         smb_fname,
763                                         names[i],
764                                         &ea);
765                 if (!NT_STATUS_IS_OK(status)) {
766                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
767                                 names[i],
768                                 smb_fname->base_name,
769                                 nt_errstr(status)));
770                         continue;
771                 }
772
773                 ea.name = talloc_asprintf(
774                         ea.value.data, ":%s%s",
775                         names[i] + config->prefix_len,
776                         config->store_stream_type ? "" : ":$DATA");
777                 if (ea.name == NULL) {
778                         DEBUG(0, ("talloc failed\n"));
779                         continue;
780                 }
781
782                 if (!fn(&ea, private_data)) {
783                         TALLOC_FREE(ea.value.data);
784                         return NT_STATUS_OK;
785                 }
786
787                 TALLOC_FREE(ea.value.data);
788         }
789
790         TALLOC_FREE(names);
791         return NT_STATUS_OK;
792 }
793
794 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
795                            struct stream_struct **streams,
796                            const char *name, off_t size,
797                            off_t alloc_size)
798 {
799         struct stream_struct *tmp;
800
801         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
802                                    (*num_streams)+1);
803         if (tmp == NULL) {
804                 return false;
805         }
806
807         tmp[*num_streams].name = talloc_strdup(tmp, name);
808         if (tmp[*num_streams].name == NULL) {
809                 return false;
810         }
811
812         tmp[*num_streams].size = size;
813         tmp[*num_streams].alloc_size = alloc_size;
814
815         *streams = tmp;
816         *num_streams += 1;
817         return true;
818 }
819
820 struct streaminfo_state {
821         TALLOC_CTX *mem_ctx;
822         vfs_handle_struct *handle;
823         unsigned int num_streams;
824         struct stream_struct *streams;
825         NTSTATUS status;
826 };
827
828 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
829 {
830         struct streaminfo_state *state =
831                 (struct streaminfo_state *)private_data;
832
833         if (!add_one_stream(state->mem_ctx,
834                             &state->num_streams, &state->streams,
835                             ea->name, ea->value.length-1,
836                             smb_roundup(state->handle->conn,
837                                         ea->value.length-1))) {
838                 state->status = NT_STATUS_NO_MEMORY;
839                 return false;
840         }
841
842         return true;
843 }
844
845 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
846                                          struct files_struct *fsp,
847                                          const struct smb_filename *smb_fname,
848                                          TALLOC_CTX *mem_ctx,
849                                          unsigned int *pnum_streams,
850                                          struct stream_struct **pstreams)
851 {
852         SMB_STRUCT_STAT sbuf;
853         int ret;
854         NTSTATUS status;
855         struct streaminfo_state state;
856
857         ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
858         if (ret == -1) {
859                 return map_nt_error_from_unix(errno);
860         }
861
862         state.streams = *pstreams;
863         state.num_streams = *pnum_streams;
864         state.mem_ctx = mem_ctx;
865         state.handle = handle;
866         state.status = NT_STATUS_OK;
867
868         if (S_ISLNK(sbuf.st_ex_mode)) {
869                 /*
870                  * Currently we do't have SMB_VFS_LLISTXATTR
871                  * inside the VFS which means there's no way
872                  * to cope with a symlink when lp_posix_pathnames().
873                  * returns true. For now ignore links.
874                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
875                  */
876                 status = NT_STATUS_OK;
877         } else {
878                 status = walk_xattr_streams(handle, fsp, smb_fname,
879                                     collect_one_stream, &state);
880         }
881
882         if (!NT_STATUS_IS_OK(status)) {
883                 TALLOC_FREE(state.streams);
884                 return status;
885         }
886
887         if (!NT_STATUS_IS_OK(state.status)) {
888                 TALLOC_FREE(state.streams);
889                 return state.status;
890         }
891
892         *pnum_streams = state.num_streams;
893         *pstreams = state.streams;
894
895         return SMB_VFS_NEXT_STREAMINFO(handle,
896                         fsp,
897                         smb_fname,
898                         mem_ctx,
899                         pnum_streams,
900                         pstreams);
901 }
902
903 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
904                         enum timestamp_set_resolution *p_ts_res)
905 {
906         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
907 }
908
909 static int streams_xattr_connect(vfs_handle_struct *handle,
910                                  const char *service, const char *user)
911 {
912         struct streams_xattr_config *config;
913         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
914         const char *prefix;
915         int rc;
916
917         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
918         if (rc != 0) {
919                 return rc;
920         }
921
922         config = talloc_zero(handle->conn, struct streams_xattr_config);
923         if (config == NULL) {
924                 DEBUG(1, ("talloc_zero() failed\n"));
925                 errno = ENOMEM;
926                 return -1;
927         }
928
929         prefix = lp_parm_const_string(SNUM(handle->conn),
930                                       "streams_xattr", "prefix",
931                                       default_prefix);
932         config->prefix = talloc_strdup(config, prefix);
933         if (config->prefix == NULL) {
934                 DEBUG(1, ("talloc_strdup() failed\n"));
935                 errno = ENOMEM;
936                 return -1;
937         }
938         config->prefix_len = strlen(config->prefix);
939         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
940
941         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
942                                                  "streams_xattr",
943                                                  "store_stream_type",
944                                                  true);
945
946         SMB_VFS_HANDLE_SET_DATA(handle, config,
947                                 NULL, struct stream_xattr_config,
948                                 return -1);
949
950         return 0;
951 }
952
953 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
954                                     files_struct *fsp, const void *data,
955                                     size_t n, off_t offset)
956 {
957         struct stream_io *sio =
958                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
959         struct ea_struct ea;
960         NTSTATUS status;
961         struct smb_filename *smb_fname_base = NULL;
962         int ret;
963
964         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
965
966         if (sio == NULL) {
967                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
968         }
969
970         if (!streams_xattr_recheck(sio)) {
971                 return -1;
972         }
973
974         /* Create an smb_filename with stream_name == NULL. */
975         smb_fname_base = synthetic_smb_fname(talloc_tos(),
976                                         sio->base,
977                                         NULL,
978                                         NULL,
979                                         fsp->fsp_name->flags);
980         if (smb_fname_base == NULL) {
981                 errno = ENOMEM;
982                 return -1;
983         }
984
985         status = get_ea_value(talloc_tos(), handle->conn, NULL,
986                               smb_fname_base, sio->xattr_name, &ea);
987         if (!NT_STATUS_IS_OK(status)) {
988                 return -1;
989         }
990
991         if ((offset + n) > ea.value.length-1) {
992                 uint8_t *tmp;
993
994                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
995                                            offset + n + 1);
996
997                 if (tmp == NULL) {
998                         TALLOC_FREE(ea.value.data);
999                         errno = ENOMEM;
1000                         return -1;
1001                 }
1002                 ea.value.data = tmp;
1003                 ea.value.length = offset + n + 1;
1004                 ea.value.data[offset+n] = 0;
1005         }
1006
1007         memcpy(ea.value.data + offset, data, n);
1008
1009         ret = SMB_VFS_SETXATTR(fsp->conn,
1010                                fsp->fsp_name,
1011                                sio->xattr_name,
1012                                ea.value.data, ea.value.length, 0);
1013         TALLOC_FREE(ea.value.data);
1014
1015         if (ret == -1) {
1016                 return -1;
1017         }
1018
1019         return n;
1020 }
1021
1022 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1023                                    files_struct *fsp, void *data,
1024                                    size_t n, off_t offset)
1025 {
1026         struct stream_io *sio =
1027                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1028         struct ea_struct ea;
1029         NTSTATUS status;
1030         size_t length, overlap;
1031         struct smb_filename *smb_fname_base = NULL;
1032
1033         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1034                    (int)offset, (int)n));
1035
1036         if (sio == NULL) {
1037                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1038         }
1039
1040         if (!streams_xattr_recheck(sio)) {
1041                 return -1;
1042         }
1043
1044         /* Create an smb_filename with stream_name == NULL. */
1045         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1046                                         sio->base,
1047                                         NULL,
1048                                         NULL,
1049                                         fsp->fsp_name->flags);
1050         if (smb_fname_base == NULL) {
1051                 errno = ENOMEM;
1052                 return -1;
1053         }
1054
1055         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1056                               smb_fname_base, sio->xattr_name, &ea);
1057         if (!NT_STATUS_IS_OK(status)) {
1058                 return -1;
1059         }
1060
1061         length = ea.value.length-1;
1062
1063         DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1064                    (int)length));
1065
1066         /* Attempt to read past EOF. */
1067         if (length <= offset) {
1068                 return 0;
1069         }
1070
1071         overlap = (offset + n) > length ? (length - offset) : n;
1072         memcpy(data, ea.value.data + offset, overlap);
1073
1074         TALLOC_FREE(ea.value.data);
1075         return overlap;
1076 }
1077
1078 struct streams_xattr_pread_state {
1079         ssize_t nread;
1080         struct vfs_aio_state vfs_aio_state;
1081 };
1082
1083 static void streams_xattr_pread_done(struct tevent_req *subreq);
1084
1085 static struct tevent_req *streams_xattr_pread_send(
1086         struct vfs_handle_struct *handle,
1087         TALLOC_CTX *mem_ctx,
1088         struct tevent_context *ev,
1089         struct files_struct *fsp,
1090         void *data,
1091         size_t n, off_t offset)
1092 {
1093         struct tevent_req *req = NULL;
1094         struct tevent_req *subreq = NULL;
1095         struct streams_xattr_pread_state *state = NULL;
1096         struct stream_io *sio =
1097                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1098
1099         req = tevent_req_create(mem_ctx, &state,
1100                                 struct streams_xattr_pread_state);
1101         if (req == NULL) {
1102                 return NULL;
1103         }
1104
1105         if (sio == NULL) {
1106                 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1107                                                  data, n, offset);
1108                 if (tevent_req_nomem(req, subreq)) {
1109                         return tevent_req_post(req, ev);
1110                 }
1111                 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1112                 return req;
1113         }
1114
1115         state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1116         if (state->nread != n) {
1117                 if (state->nread != -1) {
1118                         errno = EIO;
1119                 }
1120                 tevent_req_error(req, errno);
1121                 return tevent_req_post(req, ev);
1122         }
1123
1124         tevent_req_done(req);
1125         return tevent_req_post(req, ev);
1126 }
1127
1128 static void streams_xattr_pread_done(struct tevent_req *subreq)
1129 {
1130         struct tevent_req *req = tevent_req_callback_data(
1131                 subreq, struct tevent_req);
1132         struct streams_xattr_pread_state *state = tevent_req_data(
1133                 req, struct streams_xattr_pread_state);
1134
1135         state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1136         TALLOC_FREE(subreq);
1137
1138         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1139                 return;
1140         }
1141         tevent_req_done(req);
1142 }
1143
1144 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1145                                         struct vfs_aio_state *vfs_aio_state)
1146 {
1147         struct streams_xattr_pread_state *state = tevent_req_data(
1148                 req, struct streams_xattr_pread_state);
1149
1150         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1151                 return -1;
1152         }
1153
1154         *vfs_aio_state = state->vfs_aio_state;
1155         return state->nread;
1156 }
1157
1158 struct streams_xattr_pwrite_state {
1159         ssize_t nwritten;
1160         struct vfs_aio_state vfs_aio_state;
1161 };
1162
1163 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1164
1165 static struct tevent_req *streams_xattr_pwrite_send(
1166         struct vfs_handle_struct *handle,
1167         TALLOC_CTX *mem_ctx,
1168         struct tevent_context *ev,
1169         struct files_struct *fsp,
1170         const void *data,
1171         size_t n, off_t offset)
1172 {
1173         struct tevent_req *req = NULL;
1174         struct tevent_req *subreq = NULL;
1175         struct streams_xattr_pwrite_state *state = NULL;
1176         struct stream_io *sio =
1177                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1178
1179         req = tevent_req_create(mem_ctx, &state,
1180                                 struct streams_xattr_pwrite_state);
1181         if (req == NULL) {
1182                 return NULL;
1183         }
1184
1185         if (sio == NULL) {
1186                 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1187                                                   data, n, offset);
1188                 if (tevent_req_nomem(req, subreq)) {
1189                         return tevent_req_post(req, ev);
1190                 }
1191                 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1192                 return req;
1193         }
1194
1195         state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1196         if (state->nwritten != n) {
1197                 if (state->nwritten != -1) {
1198                         errno = EIO;
1199                 }
1200                 tevent_req_error(req, errno);
1201                 return tevent_req_post(req, ev);
1202         }
1203
1204         tevent_req_done(req);
1205         return tevent_req_post(req, ev);
1206 }
1207
1208 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1209 {
1210         struct tevent_req *req = tevent_req_callback_data(
1211                 subreq, struct tevent_req);
1212         struct streams_xattr_pwrite_state *state = tevent_req_data(
1213                 req, struct streams_xattr_pwrite_state);
1214
1215         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1216         TALLOC_FREE(subreq);
1217
1218         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1219                 return;
1220         }
1221         tevent_req_done(req);
1222 }
1223
1224 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1225                                          struct vfs_aio_state *vfs_aio_state)
1226 {
1227         struct streams_xattr_pwrite_state *state = tevent_req_data(
1228                 req, struct streams_xattr_pwrite_state);
1229
1230         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1231                 return -1;
1232         }
1233
1234         *vfs_aio_state = state->vfs_aio_state;
1235         return state->nwritten;
1236 }
1237
1238 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1239                                         struct files_struct *fsp,
1240                                         off_t offset)
1241 {
1242         int ret;
1243         uint8_t *tmp;
1244         struct ea_struct ea;
1245         NTSTATUS status;
1246         struct stream_io *sio =
1247                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1248         struct smb_filename *smb_fname_base = NULL;
1249
1250         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1251                    fsp_str_dbg(fsp), (double)offset));
1252
1253         if (sio == NULL) {
1254                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1255         }
1256
1257         if (!streams_xattr_recheck(sio)) {
1258                 return -1;
1259         }
1260
1261         /* Create an smb_filename with stream_name == NULL. */
1262         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1263                                         sio->base,
1264                                         NULL,
1265                                         NULL,
1266                                         fsp->fsp_name->flags);
1267         if (smb_fname_base == NULL) {
1268                 errno = ENOMEM;
1269                 return -1;
1270         }
1271
1272         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1273                               smb_fname_base, sio->xattr_name, &ea);
1274         if (!NT_STATUS_IS_OK(status)) {
1275                 return -1;
1276         }
1277
1278         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1279                                    offset + 1);
1280
1281         if (tmp == NULL) {
1282                 TALLOC_FREE(ea.value.data);
1283                 errno = ENOMEM;
1284                 return -1;
1285         }
1286
1287         /* Did we expand ? */
1288         if (ea.value.length < offset + 1) {
1289                 memset(&tmp[ea.value.length], '\0',
1290                         offset + 1 - ea.value.length);
1291         }
1292
1293         ea.value.data = tmp;
1294         ea.value.length = offset + 1;
1295         ea.value.data[offset] = 0;
1296
1297         ret = SMB_VFS_SETXATTR(fsp->conn,
1298                                fsp->fsp_name,
1299                                sio->xattr_name,
1300                                ea.value.data, ea.value.length, 0);
1301         TALLOC_FREE(ea.value.data);
1302
1303         if (ret == -1) {
1304                 return -1;
1305         }
1306
1307         return 0;
1308 }
1309
1310 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1311                                         struct files_struct *fsp,
1312                                         uint32_t mode,
1313                                         off_t offset,
1314                                         off_t len)
1315 {
1316         struct stream_io *sio =
1317                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1318
1319         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1320                 "len = %.0f\n",
1321                 fsp_str_dbg(fsp), (double)offset, (double)len));
1322
1323         if (sio == NULL) {
1324                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1325         }
1326
1327         if (!streams_xattr_recheck(sio)) {
1328                 return -1;
1329         }
1330
1331         /* Let the pwrite code path handle it. */
1332         errno = ENOSYS;
1333         return -1;
1334 }
1335
1336 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1337                                 uid_t uid, gid_t gid)
1338 {
1339         struct stream_io *sio =
1340                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1341
1342         if (sio == NULL) {
1343                 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1344         }
1345
1346         return 0;
1347 }
1348
1349 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1350                                 files_struct *fsp,
1351                                 mode_t mode)
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_FCHMOD(handle, fsp, mode);
1358         }
1359
1360         return 0;
1361 }
1362
1363 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1364                                        struct files_struct *fsp,
1365                                        const char *name,
1366                                        void *value,
1367                                        size_t size)
1368 {
1369         struct stream_io *sio =
1370                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1371
1372         if (sio == NULL) {
1373                 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1374         }
1375
1376         errno = ENOTSUP;
1377         return -1;
1378 }
1379
1380 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1381                                         struct files_struct *fsp,
1382                                         char *list,
1383                                         size_t size)
1384 {
1385         struct stream_io *sio =
1386                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1387
1388         if (sio == NULL) {
1389                 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1390         }
1391
1392         errno = ENOTSUP;
1393         return -1;
1394 }
1395
1396 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1397                                       struct files_struct *fsp,
1398                                       const char *name)
1399 {
1400         struct stream_io *sio =
1401                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1402
1403         if (sio == NULL) {
1404                 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1405         }
1406
1407         errno = ENOTSUP;
1408         return -1;
1409 }
1410
1411 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1412                                    struct files_struct *fsp,
1413                                    const char *name,
1414                                    const void *value,
1415                                    size_t size,
1416                                    int flags)
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_FSETXATTR(handle, fsp, name, value,
1423                                               size, flags);
1424         }
1425
1426         errno = ENOTSUP;
1427         return -1;
1428 }
1429
1430 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1431                                               files_struct *fsp,
1432                                               TALLOC_CTX *mem_ctx)
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_GET_FD(handle, fsp, mem_ctx);
1439         }
1440
1441         return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1442                 handle, fsp->base_fsp->fsp_name,
1443                 SMB_ACL_TYPE_ACCESS, mem_ctx);
1444 }
1445
1446 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1447                                         files_struct *fsp,
1448                                         SMB_ACL_T theacl)
1449 {
1450         struct stream_io *sio =
1451                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1452
1453         if (sio == NULL) {
1454                 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1455         }
1456
1457         return 0;
1458 }
1459
1460 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1461                                              files_struct *fsp,
1462                                              TALLOC_CTX *mem_ctx,
1463                                              char **blob_description,
1464                                              DATA_BLOB *blob)
1465 {
1466         struct stream_io *sio =
1467                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1468
1469         if (sio == NULL) {
1470                 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1471                                                         blob_description, blob);
1472         }
1473
1474         return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1475                 handle, fsp->base_fsp->fsp_name, mem_ctx,
1476                 blob_description, blob);
1477 }
1478
1479 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1480                                           files_struct *fsp,
1481                                           uint32_t security_info,
1482                                           TALLOC_CTX *mem_ctx,
1483                                           struct security_descriptor **ppdesc)
1484 {
1485         struct stream_io *sio =
1486                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1487
1488         if (sio == NULL) {
1489                 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1490                                                 mem_ctx, ppdesc);
1491         }
1492
1493         return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1494                                        security_info, mem_ctx, ppdesc);
1495 }
1496
1497 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1498                                           files_struct *fsp,
1499                                           uint32_t security_info_sent,
1500                                           const struct security_descriptor *psd)
1501 {
1502         struct stream_io *sio =
1503                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1504
1505         if (sio == NULL) {
1506                 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1507                                                 security_info_sent, psd);
1508         }
1509
1510         return NT_STATUS_OK;
1511 }
1512
1513 struct streams_xattr_fsync_state {
1514         int ret;
1515         struct vfs_aio_state vfs_aio_state;
1516 };
1517
1518 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1519
1520 static struct tevent_req *streams_xattr_fsync_send(
1521         struct vfs_handle_struct *handle,
1522         TALLOC_CTX *mem_ctx,
1523         struct tevent_context *ev,
1524         struct files_struct *fsp)
1525 {
1526         struct tevent_req *req = NULL;
1527         struct tevent_req *subreq = NULL;
1528         struct streams_xattr_fsync_state *state = NULL;
1529         struct stream_io *sio =
1530                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1531
1532         req = tevent_req_create(mem_ctx, &state,
1533                                 struct streams_xattr_fsync_state);
1534         if (req == NULL) {
1535                 return NULL;
1536         }
1537
1538         if (sio == NULL) {
1539                 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1540                 if (tevent_req_nomem(req, subreq)) {
1541                         return tevent_req_post(req, ev);
1542                 }
1543                 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1544                 return req;
1545         }
1546
1547         /*
1548          * There's no pathname based sync variant and we don't have access to
1549          * the basefile handle, so we can't do anything here.
1550          */
1551
1552         tevent_req_done(req);
1553         return tevent_req_post(req, ev);
1554 }
1555
1556 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1557 {
1558         struct tevent_req *req = tevent_req_callback_data(
1559                 subreq, struct tevent_req);
1560         struct streams_xattr_fsync_state *state = tevent_req_data(
1561                 req, struct streams_xattr_fsync_state);
1562
1563         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1564         TALLOC_FREE(subreq);
1565         if (state->ret != 0) {
1566                 tevent_req_error(req, errno);
1567                 return;
1568         }
1569
1570         tevent_req_done(req);
1571 }
1572
1573 static int streams_xattr_fsync_recv(struct tevent_req *req,
1574                                     struct vfs_aio_state *vfs_aio_state)
1575 {
1576         struct streams_xattr_fsync_state *state = tevent_req_data(
1577                 req, struct streams_xattr_fsync_state);
1578
1579         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1580                 return -1;
1581         }
1582
1583         *vfs_aio_state = state->vfs_aio_state;
1584         return state->ret;
1585 }
1586
1587 static bool streams_xattr_lock(vfs_handle_struct *handle,
1588                                files_struct *fsp,
1589                                int op,
1590                                off_t offset,
1591                                off_t count,
1592                                int type)
1593 {
1594         struct stream_io *sio =
1595                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1596
1597         if (sio == NULL) {
1598                 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1599         }
1600
1601         return true;
1602 }
1603
1604 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1605                                   files_struct *fsp,
1606                                   off_t *poffset,
1607                                   off_t *pcount,
1608                                   int *ptype,
1609                                   pid_t *ppid)
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_GETLOCK(handle, fsp, poffset,
1616                                             pcount, ptype, ppid);
1617         }
1618
1619         errno = ENOTSUP;
1620         return false;
1621 }
1622
1623 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1624                                       files_struct *fsp,
1625                                       uint32_t share_mode,
1626                                       uint32_t access_mask)
1627 {
1628         struct stream_io *sio =
1629                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1630
1631         if (sio == NULL) {
1632                 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1633                                                  share_mode, access_mask);
1634         }
1635
1636         return 0;
1637 }
1638
1639 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1640                                         files_struct *fsp,
1641                                         int leasetype)
1642 {
1643         struct stream_io *sio =
1644                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1645
1646         if (sio == NULL) {
1647                 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1648         }
1649
1650         return 0;
1651 }
1652
1653 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1654                                             files_struct *fsp,
1655                                             struct lock_struct *plock)
1656 {
1657         struct stream_io *sio =
1658                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1659
1660         if (sio == NULL) {
1661                 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1662         }
1663
1664         return true;
1665 }
1666
1667 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1668         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1669         .connect_fn = streams_xattr_connect,
1670         .open_fn = streams_xattr_open,
1671         .close_fn = streams_xattr_close,
1672         .stat_fn = streams_xattr_stat,
1673         .fstat_fn = streams_xattr_fstat,
1674         .lstat_fn = streams_xattr_lstat,
1675         .pread_fn = streams_xattr_pread,
1676         .pwrite_fn = streams_xattr_pwrite,
1677         .pread_send_fn = streams_xattr_pread_send,
1678         .pread_recv_fn = streams_xattr_pread_recv,
1679         .pwrite_send_fn = streams_xattr_pwrite_send,
1680         .pwrite_recv_fn = streams_xattr_pwrite_recv,
1681         .unlink_fn = streams_xattr_unlink,
1682         .unlinkat_fn = streams_xattr_unlinkat,
1683         .renameat_fn = streams_xattr_renameat,
1684         .ftruncate_fn = streams_xattr_ftruncate,
1685         .fallocate_fn = streams_xattr_fallocate,
1686         .streaminfo_fn = streams_xattr_streaminfo,
1687
1688         .fsync_send_fn = streams_xattr_fsync_send,
1689         .fsync_recv_fn = streams_xattr_fsync_recv,
1690
1691         .lock_fn = streams_xattr_lock,
1692         .getlock_fn = streams_xattr_getlock,
1693         .kernel_flock_fn = streams_xattr_kernel_flock,
1694         .linux_setlease_fn = streams_xattr_linux_setlease,
1695         .strict_lock_check_fn = streams_xattr_strict_lock_check,
1696
1697         .fchown_fn = streams_xattr_fchown,
1698         .fchmod_fn = streams_xattr_fchmod,
1699
1700         .fgetxattr_fn = streams_xattr_fgetxattr,
1701         .flistxattr_fn = streams_xattr_flistxattr,
1702         .fremovexattr_fn = streams_xattr_fremovexattr,
1703         .fsetxattr_fn = streams_xattr_fsetxattr,
1704
1705         .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1706         .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1707         .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1708
1709         .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1710         .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1711 };
1712
1713 static_decl_vfs;
1714 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1715 {
1716         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1717                                 &vfs_streams_xattr_fns);
1718 }