Use fxattr calls whenever possible (trying to work around the strange Linux kernel...
[ira/wip.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
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
28
29 struct stream_io {
30         char *base;
31         char *xattr_name;
32 };
33
34 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
35 {
36         struct MD5Context ctx;
37         unsigned char hash[16];
38         SMB_INO_T result;
39         char *upper_sname;
40
41         DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
42                    (unsigned long)sbuf->st_dev,
43                    (unsigned long)sbuf->st_ino, sname));
44
45         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
46         SMB_ASSERT(upper_sname != NULL);
47
48         MD5Init(&ctx);
49         MD5Update(&ctx, (unsigned char *)&(sbuf->st_dev),
50                   sizeof(sbuf->st_dev));
51         MD5Update(&ctx, (unsigned char *)&(sbuf->st_ino),
52                   sizeof(sbuf->st_ino));
53         MD5Update(&ctx, (unsigned char *)upper_sname,
54                   talloc_get_size(upper_sname)-1);
55         MD5Final(hash, &ctx);
56
57         TALLOC_FREE(upper_sname);
58
59         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
60         memcpy(&result, hash, sizeof(result));
61
62         DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
63
64         return result;
65 }
66
67 static ssize_t get_xattr_size(connection_struct *conn,
68                                 files_struct *fsp,
69                                 const char *fname,
70                                 const char *xattr_name)
71 {
72         NTSTATUS status;
73         struct ea_struct ea;
74         ssize_t result;
75
76         status = get_ea_value(talloc_tos(), conn, fsp, fname,
77                               xattr_name, &ea);
78
79         if (!NT_STATUS_IS_OK(status)) {
80                 return -1;
81         }
82
83         result = ea.value.length-1;
84         TALLOC_FREE(ea.value.data);
85         return result;
86 }
87
88
89 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
90                                SMB_STRUCT_STAT *sbuf)
91 {
92         struct stream_io *io = (struct stream_io *)
93                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
94
95         DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
96
97         if (io == NULL) {
98                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
99         }
100
101         if (SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf) == -1) {
102                 return -1;
103         }
104
105         sbuf->st_size = get_xattr_size(handle->conn, fsp->base_fsp,
106                                         io->base, io->xattr_name);
107         if (sbuf->st_size == -1) {
108                 return -1;
109         }
110
111         DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf->st_size));
112
113         sbuf->st_ino = stream_inode(sbuf, io->xattr_name);
114         sbuf->st_mode &= ~S_IFMT;
115         sbuf->st_mode |= S_IFREG;
116         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
117
118         return 0;
119 }
120
121 static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
122                               SMB_STRUCT_STAT *sbuf)
123 {
124         NTSTATUS status;
125         char *base = NULL, *sname = NULL;
126         int result = -1;
127         char *xattr_name;
128
129         if (!is_ntfs_stream_name(fname)) {
130                 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
131         }
132
133         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
134         if (!NT_STATUS_IS_OK(status)) {
135                 errno = EINVAL;
136                 return -1;
137         }
138
139         if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
140                 goto fail;
141         }
142
143         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
144                                      SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
145         if (xattr_name == NULL) {
146                 errno = ENOMEM;
147                 goto fail;
148         }
149
150         sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
151         if (sbuf->st_size == -1) {
152                 errno = ENOENT;
153                 goto fail;
154         }
155
156         sbuf->st_ino = stream_inode(sbuf, xattr_name);
157         sbuf->st_mode &= ~S_IFMT;
158         sbuf->st_mode |= S_IFREG;
159         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
160
161         result = 0;
162  fail:
163         TALLOC_FREE(base);
164         TALLOC_FREE(sname);
165         return result;
166 }
167
168 static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
169                                SMB_STRUCT_STAT *sbuf)
170 {
171         NTSTATUS status;
172         char *base, *sname;
173         int result = -1;
174         char *xattr_name;
175
176         if (!is_ntfs_stream_name(fname)) {
177                 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
178         }
179
180         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
181         if (!NT_STATUS_IS_OK(status)) {
182                 errno = EINVAL;
183                 goto fail;
184         }
185
186         if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
187                 goto fail;
188         }
189
190         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
191                                      SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
192         if (xattr_name == NULL) {
193                 errno = ENOMEM;
194                 goto fail;
195         }
196
197         sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
198         if (sbuf->st_size == -1) {
199                 errno = ENOENT;
200                 goto fail;
201         }
202
203         sbuf->st_ino = stream_inode(sbuf, xattr_name);
204         sbuf->st_mode &= ~S_IFMT;
205         sbuf->st_mode |= S_IFREG;
206         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
207
208         result = 0;
209  fail:
210         TALLOC_FREE(base);
211         TALLOC_FREE(sname);
212         return result;
213 }
214
215 static int streams_xattr_open(vfs_handle_struct *handle,  const char *fname,
216                               files_struct *fsp, int flags, mode_t mode)
217 {
218         TALLOC_CTX *frame;
219         NTSTATUS status;
220         struct stream_io *sio;
221         char *base, *sname;
222         struct ea_struct ea;
223         char *xattr_name;
224         int baseflags;
225         int hostfd = -1;
226
227         DEBUG(10, ("streams_xattr_open called for %s\n", fname));
228
229         if (!is_ntfs_stream_name(fname)) {
230                 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
231         }
232
233         frame = talloc_stackframe();
234
235         status = split_ntfs_stream_name(talloc_tos(), fname,
236                                         &base, &sname);
237         if (!NT_STATUS_IS_OK(status)) {
238                 errno = EINVAL;
239                 goto fail;
240         }
241
242         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
243                                      SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
244         if (xattr_name == NULL) {
245                 errno = ENOMEM;
246                 goto fail;
247         }
248
249         /*
250          * We use baseflags to turn off nasty side-effects when opening the
251          * underlying file.
252          */
253         baseflags = flags;
254         baseflags &= ~O_TRUNC;
255         baseflags &= ~O_EXCL;
256         baseflags &= ~O_CREAT;
257
258         hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
259
260         /* It is legit to open a stream on a directory, but the base
261          * fd has to be read-only.
262          */
263         if ((hostfd == -1) && (errno == EISDIR)) {
264                 baseflags &= ~O_ACCMODE;
265                 baseflags |= O_RDONLY;
266                 hostfd = SMB_VFS_OPEN(handle->conn, fname, fsp, baseflags,
267                                       mode);
268         }
269
270         if (hostfd == -1) {
271                 goto fail;
272         }
273
274         status = get_ea_value(talloc_tos(), handle->conn, NULL, base,
275                               xattr_name, &ea);
276
277         DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
278
279         if (!NT_STATUS_IS_OK(status)
280             && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
281                 /*
282                  * The base file is not there. This is an error even if we got
283                  * O_CREAT, the higher levels should have created the base
284                  * file for us.
285                  */
286                 DEBUG(10, ("streams_xattr_open: base file %s not around, "
287                            "returning ENOENT\n", base));
288                 errno = ENOENT;
289                 goto fail;
290         }
291
292         if (!NT_STATUS_IS_OK(status)) {
293                 /*
294                  * The attribute does not exist
295                  */
296
297                 if (flags & O_CREAT) {
298                         /*
299                          * Darn, xattrs need at least 1 byte
300                          */
301                         char null = '\0';
302
303                         DEBUG(10, ("creating attribute %s on file %s\n",
304                                    xattr_name, base));
305
306                         if (fsp->base_fsp->fh->fd != -1) {
307                                 if (SMB_VFS_FSETXATTR(
308                                         fsp->base_fsp, xattr_name,
309                                         &null, sizeof(null),
310                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
311                                         goto fail;
312                                 }
313                         } else {
314                                 if (SMB_VFS_SETXATTR(
315                                         handle->conn, base, xattr_name,
316                                         &null, sizeof(null),
317                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
318                                         goto fail;
319                                 }
320                         }
321                 }
322         }
323
324         if (flags & O_TRUNC) {
325                 char null = '\0';
326                 if (fsp->base_fsp->fh->fd != -1) {
327                         if (SMB_VFS_FSETXATTR(
328                                         fsp->base_fsp, xattr_name,
329                                         &null, sizeof(null),
330                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
331                                 goto fail;
332                         }
333                 } else {
334                         if (SMB_VFS_SETXATTR(
335                                         handle->conn, base, xattr_name,
336                                         &null, sizeof(null),
337                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
338                                 goto fail;
339                         }
340                 }
341         }
342
343         sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
344                                                         struct stream_io);
345         if (sio == NULL) {
346                 errno = ENOMEM;
347                 goto fail;
348         }
349
350         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
351                                         xattr_name);
352         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
353                                   base);
354
355         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
356                 errno = ENOMEM;
357                 goto fail;
358         }
359
360         TALLOC_FREE(frame);
361         return hostfd;
362
363  fail:
364         if (hostfd >= 0) {
365                 /*
366                  * BUGBUGBUG -- we would need to call fd_close_posix here, but
367                  * we don't have a full fsp yet
368                  */
369                 SMB_VFS_CLOSE(fsp);
370         }
371
372         TALLOC_FREE(frame);
373         return -1;
374 }
375
376 static int streams_xattr_unlink(vfs_handle_struct *handle,  const char *fname)
377 {
378         NTSTATUS status;
379         char *base = NULL;
380         char *sname = NULL;
381         int ret = -1;
382         char *xattr_name;
383
384         if (!is_ntfs_stream_name(fname)) {
385                 return SMB_VFS_NEXT_UNLINK(handle, fname);
386         }
387
388         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
389         if (!NT_STATUS_IS_OK(status)) {
390                 errno = EINVAL;
391                 goto fail;
392         }
393
394         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
395                                      SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
396         if (xattr_name == NULL) {
397                 errno = ENOMEM;
398                 goto fail;
399         }
400
401         ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
402
403         if ((ret == -1) && (errno == ENOATTR)) {
404                 errno = ENOENT;
405                 goto fail;
406         }
407
408         ret = 0;
409
410  fail:
411         TALLOC_FREE(base);
412         TALLOC_FREE(sname);
413         return ret;
414 }
415
416 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
417                                    const char *fname,
418                                    bool (*fn)(struct ea_struct *ea,
419                                               void *private_data),
420                                    void *private_data)
421 {
422         NTSTATUS status;
423         char **names;
424         size_t i, num_names;
425         size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
426
427         status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
428                                         &names, &num_names);
429         if (!NT_STATUS_IS_OK(status)) {
430                 return status;
431         }
432
433         for (i=0; i<num_names; i++) {
434                 struct ea_struct ea;
435
436                 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
437                             prefix_len) != 0) {
438                         continue;
439                 }
440
441                 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
442                 if (!NT_STATUS_IS_OK(status)) {
443                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
444                                    names[i], fname, nt_errstr(status)));
445                         continue;
446                 }
447
448                 ea.name = talloc_asprintf(ea.value.data, ":%s",
449                                           names[i] + prefix_len);
450                 if (ea.name == NULL) {
451                         DEBUG(0, ("talloc failed\n"));
452                         continue;
453                 }
454
455                 if (!fn(&ea, private_data)) {
456                         TALLOC_FREE(ea.value.data);
457                         return NT_STATUS_OK;
458                 }
459
460                 TALLOC_FREE(ea.value.data);
461         }
462
463         TALLOC_FREE(names);
464         return NT_STATUS_OK;
465 }
466
467 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
468                            struct stream_struct **streams,
469                            const char *name, SMB_OFF_T size,
470                            SMB_OFF_T alloc_size)
471 {
472         struct stream_struct *tmp;
473
474         tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
475                                    (*num_streams)+1);
476         if (tmp == NULL) {
477                 return false;
478         }
479
480         tmp[*num_streams].name = talloc_strdup(tmp, name);
481         if (tmp[*num_streams].name == NULL) {
482                 return false;
483         }
484
485         tmp[*num_streams].size = size;
486         tmp[*num_streams].alloc_size = alloc_size;
487
488         *streams = tmp;
489         *num_streams += 1;
490         return true;
491 }
492
493 struct streaminfo_state {
494         TALLOC_CTX *mem_ctx;
495         vfs_handle_struct *handle;
496         unsigned int num_streams;
497         struct stream_struct *streams;
498         NTSTATUS status;
499 };
500
501 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
502 {
503         struct streaminfo_state *state =
504                 (struct streaminfo_state *)private_data;
505
506         if (!add_one_stream(state->mem_ctx,
507                             &state->num_streams, &state->streams,
508                             ea->name, ea->value.length-1,
509                             smb_roundup(state->handle->conn,
510                                         ea->value.length-1))) {
511                 state->status = NT_STATUS_NO_MEMORY;
512                 return false;
513         }
514
515         return true;
516 }
517
518 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
519                                          struct files_struct *fsp,
520                                          const char *fname,
521                                          TALLOC_CTX *mem_ctx,
522                                          unsigned int *pnum_streams,
523                                          struct stream_struct **pstreams)
524 {
525         SMB_STRUCT_STAT sbuf;
526         int ret;
527         NTSTATUS status;
528         struct streaminfo_state state;
529
530         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
531                 if (is_ntfs_stream_name(fsp->fsp_name)) {
532                         return NT_STATUS_INVALID_PARAMETER;
533                 }
534                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
535         }
536         else {
537                 if (is_ntfs_stream_name(fname)) {
538                         return NT_STATUS_INVALID_PARAMETER;
539                 }
540                 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
541         }
542
543         if (ret == -1) {
544                 return map_nt_error_from_unix(errno);
545         }
546
547         state.streams = NULL;
548         state.num_streams = 0;
549
550         if (!S_ISDIR(sbuf.st_mode)) {
551                 if (!add_one_stream(mem_ctx,
552                                     &state.num_streams, &state.streams,
553                                     "::$DATA", sbuf.st_size,
554                                     get_allocation_size(handle->conn, fsp,
555                                                         &sbuf))) {
556                         return NT_STATUS_NO_MEMORY;
557                 }
558         }
559
560         state.mem_ctx = mem_ctx;
561         state.handle = handle;
562         state.status = NT_STATUS_OK;
563
564         status = walk_xattr_streams(handle->conn, fsp, fname,
565                                     collect_one_stream, &state);
566
567         if (!NT_STATUS_IS_OK(status)) {
568                 TALLOC_FREE(state.streams);
569                 return status;
570         }
571
572         if (!NT_STATUS_IS_OK(state.status)) {
573                 TALLOC_FREE(state.streams);
574                 return state.status;
575         }
576
577         *pnum_streams = state.num_streams;
578         *pstreams = state.streams;
579         return NT_STATUS_OK;
580 }
581
582 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle)
583 {
584         return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
585 }
586
587 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
588                                     files_struct *fsp, const void *data,
589                                     size_t n, SMB_OFF_T offset)
590 {
591         struct stream_io *sio =
592                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
593         struct ea_struct ea;
594         NTSTATUS status;
595         int ret;
596
597         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
598
599         if (sio == NULL) {
600                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
601         }
602
603         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
604                               sio->base, sio->xattr_name, &ea);
605         if (!NT_STATUS_IS_OK(status)) {
606                 return -1;
607         }
608
609         if ((offset + n) > ea.value.length-1) {
610                 uint8 *tmp;
611
612                 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
613                                            offset + n + 1);
614
615                 if (tmp == NULL) {
616                         TALLOC_FREE(ea.value.data);
617                         errno = ENOMEM;
618                         return -1;
619                 }
620                 ea.value.data = tmp;
621                 ea.value.length = offset + n + 1;
622                 ea.value.data[offset+n] = 0;
623         }
624
625         memcpy(ea.value.data + offset, data, n);
626
627         if (fsp->base_fsp->fh->fd != -1) {
628                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
629                                 sio->xattr_name,
630                                 ea.value.data, ea.value.length, 0);
631         } else {
632                 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
633                                 sio->xattr_name,
634                                 ea.value.data, ea.value.length, 0);
635         }
636         TALLOC_FREE(ea.value.data);
637
638         if (ret == -1) {
639                 return -1;
640         }
641
642         return n;
643 }
644
645 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
646                                    files_struct *fsp, void *data,
647                                    size_t n, SMB_OFF_T offset)
648 {
649         struct stream_io *sio =
650                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
651         struct ea_struct ea;
652         NTSTATUS status;
653         size_t length, overlap;
654
655         if (sio == NULL) {
656                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
657         }
658
659         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
660                               sio->base, sio->xattr_name, &ea);
661         if (!NT_STATUS_IS_OK(status)) {
662                 return -1;
663         }
664
665         length = ea.value.length-1;
666
667         /* Attempt to read past EOF. */
668         if (length <= offset) {
669                 errno = EINVAL;
670                 return -1;
671         }
672
673         overlap = (offset + n) > length ? (length - offset) : n;
674         memcpy(data, ea.value.data + offset, overlap);
675
676         TALLOC_FREE(ea.value.data);
677         return overlap;
678 }
679
680 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
681                                         struct files_struct *fsp,
682                                         SMB_OFF_T offset)
683 {
684         int ret;
685         uint8 *tmp;
686         struct ea_struct ea;
687         NTSTATUS status;
688         struct stream_io *sio =
689                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
690
691         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
692                 fsp->fsp_name,
693                 (double)offset ));
694
695         if (sio == NULL) {
696                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
697         }
698
699         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
700                               sio->base, sio->xattr_name, &ea);
701         if (!NT_STATUS_IS_OK(status)) {
702                 return -1;
703         }
704
705         tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
706                                    offset + 1);
707
708         if (tmp == NULL) {
709                 TALLOC_FREE(ea.value.data);
710                 errno = ENOMEM;
711                 return -1;
712         }
713
714         /* Did we expand ? */
715         if (ea.value.length < offset + 1) {
716                 memset(&tmp[ea.value.length], '\0',
717                         offset + 1 - ea.value.length);
718         }
719
720         ea.value.data = tmp;
721         ea.value.length = offset + 1;
722         ea.value.data[offset] = 0;
723
724         if (fsp->base_fsp->fh->fd != -1) {
725                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
726                                 sio->xattr_name,
727                                 ea.value.data, ea.value.length, 0);
728         } else {
729                 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
730                                 sio->xattr_name,
731                                 ea.value.data, ea.value.length, 0);
732         }
733
734         TALLOC_FREE(ea.value.data);
735
736         if (ret == -1) {
737                 return -1;
738         }
739
740         return 0;
741 }
742
743 /* VFS operations structure */
744
745 static vfs_op_tuple streams_xattr_ops[] = {
746         {SMB_VFS_OP(streams_xattr_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
747          SMB_VFS_LAYER_TRANSPARENT},
748         {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
749          SMB_VFS_LAYER_TRANSPARENT},
750         {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
751          SMB_VFS_LAYER_TRANSPARENT},
752         {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
753          SMB_VFS_LAYER_TRANSPARENT},
754         {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
755          SMB_VFS_LAYER_TRANSPARENT},
756         {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
757          SMB_VFS_LAYER_TRANSPARENT},
758         {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
759          SMB_VFS_LAYER_TRANSPARENT},
760         {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
761          SMB_VFS_LAYER_TRANSPARENT},
762         {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
763          SMB_VFS_LAYER_TRANSPARENT},
764         {SMB_VFS_OP(streams_xattr_ftruncate),  SMB_VFS_OP_FTRUNCATE,
765          SMB_VFS_LAYER_TRANSPARENT},
766         {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
767          SMB_VFS_LAYER_OPAQUE},
768         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
769 };
770
771 NTSTATUS vfs_streams_xattr_init(void);
772 NTSTATUS vfs_streams_xattr_init(void)
773 {
774         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
775                                 streams_xattr_ops);
776 }