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