For pread/pwrite we need to do the setxattr on base_fsp
[Samba/nascimento.git] / source3 / modules / vfs_streams_xattr.c
blob7d7ec02fe722f59a7068f7ffec3a195cafbbbed0
1 /*
2 * Store streams in xattrs
4 * Copyright (C) Volker Lendecke, 2008
6 * Partly based on James Peach's Darwin module, which is
8 * Copyright (C) James Peach 2006-2007
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.
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.
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/>.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
29 #define XATTR_DOSSTREAM_PREFIX "user.DosStream."
31 struct stream_io {
32 char *base;
33 char *xattr_name;
36 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
38 struct MD5Context ctx;
39 unsigned char hash[16];
40 SMB_INO_T result;
41 char *upper_sname;
43 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
44 (unsigned long)sbuf->st_dev,
45 (unsigned long)sbuf->st_ino, sname));
47 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
48 SMB_ASSERT(upper_sname != NULL);
50 MD5Init(&ctx);
51 MD5Update(&ctx, (unsigned char *)&(sbuf->st_dev),
52 sizeof(sbuf->st_dev));
53 MD5Update(&ctx, (unsigned char *)&(sbuf->st_ino),
54 sizeof(sbuf->st_ino));
55 MD5Update(&ctx, (unsigned char *)upper_sname,
56 talloc_get_size(upper_sname)-1);
57 MD5Final(hash, &ctx);
59 TALLOC_FREE(upper_sname);
61 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
62 memcpy(&result, hash, sizeof(result));
64 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
66 return result;
69 static ssize_t get_xattr_size(connection_struct *conn, const char *fname,
70 const char *xattr_name)
72 NTSTATUS status;
73 struct ea_struct ea;
74 ssize_t result;
76 status = get_ea_value(talloc_tos(), conn, NULL, fname,
77 xattr_name, &ea);
79 if (!NT_STATUS_IS_OK(status)) {
80 return -1;
83 result = ea.value.length-1;
84 TALLOC_FREE(ea.value.data);
85 return result;
89 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
90 SMB_STRUCT_STAT *sbuf)
92 struct stream_io *io = (struct stream_io *)
93 VFS_FETCH_FSP_EXTENSION(handle, fsp);
95 if (io == NULL) {
96 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
99 if (SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf) == -1) {
100 return -1;
103 sbuf->st_size = get_xattr_size(handle->conn, io->base, io->xattr_name);
104 if (sbuf->st_size == -1) {
105 return -1;
108 sbuf->st_ino = stream_inode(sbuf, io->xattr_name);
109 sbuf->st_mode &= ~S_IFMT;
110 sbuf->st_mode |= S_IFREG;
111 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
113 return 0;
116 static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
117 SMB_STRUCT_STAT *sbuf)
119 NTSTATUS status;
120 char *base, *sname;
121 int result = -1;
122 char *xattr_name;
124 if (!is_ntfs_stream_name(fname)) {
125 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
128 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
129 if (!NT_STATUS_IS_OK(status)) {
130 errno = EINVAL;
131 goto fail;
134 if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
135 goto fail;
138 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
139 XATTR_DOSSTREAM_PREFIX, sname);
140 if (xattr_name == NULL) {
141 errno = ENOMEM;
142 goto fail;
145 sbuf->st_size = get_xattr_size(handle->conn, base, xattr_name);
146 if (sbuf->st_size == -1) {
147 errno = ENOENT;
148 goto fail;
151 sbuf->st_ino = stream_inode(sbuf, xattr_name);
152 sbuf->st_mode &= ~S_IFMT;
153 sbuf->st_mode |= S_IFREG;
154 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
156 result = 0;
157 fail:
158 TALLOC_FREE(base);
159 TALLOC_FREE(sname);
160 return result;
163 static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
164 SMB_STRUCT_STAT *sbuf)
166 NTSTATUS status;
167 char *base, *sname;
168 int result = -1;
169 char *xattr_name;
171 if (!is_ntfs_stream_name(fname)) {
172 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
175 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
176 if (!NT_STATUS_IS_OK(status)) {
177 errno = EINVAL;
178 goto fail;
181 if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
182 goto fail;
185 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
186 XATTR_DOSSTREAM_PREFIX, sname);
187 if (xattr_name == NULL) {
188 errno = ENOMEM;
189 goto fail;
192 sbuf->st_size = get_xattr_size(handle->conn, base, xattr_name);
193 if (sbuf->st_size == -1) {
194 errno = ENOENT;
195 goto fail;
198 sbuf->st_ino = stream_inode(sbuf, xattr_name);
199 sbuf->st_mode &= ~S_IFMT;
200 sbuf->st_mode |= S_IFREG;
201 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
203 result = 0;
204 fail:
205 TALLOC_FREE(base);
206 TALLOC_FREE(sname);
207 return result;
210 static int streams_xattr_open(vfs_handle_struct *handle, const char *fname,
211 files_struct *fsp, int flags, mode_t mode)
213 TALLOC_CTX *frame;
214 NTSTATUS status;
215 struct stream_io *sio;
216 char *base, *sname;
217 struct ea_struct ea;
218 char *xattr_name;
219 int baseflags;
220 int hostfd = -1;
222 if (!is_ntfs_stream_name(fname)) {
223 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
226 frame = talloc_stackframe();
228 status = split_ntfs_stream_name(talloc_tos(), fname,
229 &base, &sname);
230 if (!NT_STATUS_IS_OK(status)) {
231 errno = EINVAL;
232 goto fail;
235 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
236 XATTR_DOSSTREAM_PREFIX, sname);
237 if (xattr_name == NULL) {
238 errno = ENOMEM;
239 goto fail;
243 * We use baseflags to turn off nasty side-effects when opening the
244 * underlying file.
246 baseflags = flags;
247 baseflags &= ~O_TRUNC;
248 baseflags &= ~O_EXCL;
249 baseflags &= ~O_CREAT;
251 hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
253 /* It is legit to open a stream on a directory, but the base
254 * fd has to be read-only.
256 if ((hostfd == -1) && (errno == EISDIR)) {
257 baseflags &= ~O_ACCMODE;
258 baseflags |= O_RDONLY;
259 hostfd = SMB_VFS_OPEN(handle->conn, fname, fsp, baseflags,
260 mode);
263 if (hostfd == -1) {
264 goto fail;
267 status = get_ea_value(talloc_tos(), handle->conn, fsp, base,
268 xattr_name, &ea);
270 if (!NT_STATUS_IS_OK(status)
271 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
273 * The base file is not there. This is an error even if we got
274 * O_CREAT, the higher levels should have created the base
275 * file for us.
277 errno = ENOENT;
278 goto fail;
281 if (!NT_STATUS_IS_OK(status)) {
283 * The attribute does not exist
286 if (flags & O_CREAT) {
288 * Darn, xattrs need at least 1 byte
290 char null = '\0';
292 DEBUG(10, ("creating attribute %s on file %s\n",
293 xattr_name, base));
295 if (SMB_VFS_SETXATTR(
296 handle->conn, base, xattr_name,
297 &null, sizeof(null),
298 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
299 goto fail;
304 if (flags & O_TRUNC) {
305 char null = '\0';
306 if (SMB_VFS_SETXATTR(
307 handle->conn, base, xattr_name,
308 &null, sizeof(null),
309 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
310 goto fail;
314 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
315 struct stream_io);
316 if (sio == NULL) {
317 errno = ENOMEM;
318 goto fail;
321 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
322 xattr_name);
323 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
324 base);
326 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
327 errno = ENOMEM;
328 goto fail;
331 TALLOC_FREE(frame);
332 return hostfd;
334 fail:
335 if (hostfd >= 0) {
337 * BUGBUGBUG -- we would need to call fd_close_posix here, but
338 * we don't have a full fsp yet
340 SMB_VFS_CLOSE(fsp, hostfd);
343 TALLOC_FREE(frame);
344 return -1;
347 static int streams_xattr_unlink(vfs_handle_struct *handle, const char *fname)
349 NTSTATUS status;
350 char *base = NULL;
351 char *sname = NULL;
352 int ret = -1;
353 char *xattr_name;
355 if (!is_ntfs_stream_name(fname)) {
356 return SMB_VFS_NEXT_UNLINK(handle, fname);
359 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
360 if (!NT_STATUS_IS_OK(status)) {
361 errno = EINVAL;
362 goto fail;
365 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
366 XATTR_DOSSTREAM_PREFIX, sname);
367 if (xattr_name == NULL) {
368 errno = ENOMEM;
369 goto fail;
372 ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
374 if ((ret == -1) && (errno == ENOATTR)) {
375 errno = ENOENT;
376 goto fail;
379 ret = 0;
381 fail:
382 TALLOC_FREE(base);
383 TALLOC_FREE(sname);
384 return ret;
387 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
388 const char *fname,
389 bool (*fn)(struct ea_struct *ea,
390 void *private_data),
391 void *private_data)
393 NTSTATUS status;
394 char **names;
395 size_t i, num_names;
396 size_t prefix_len = strlen(XATTR_DOSSTREAM_PREFIX);
398 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
399 &names, &num_names);
400 if (!NT_STATUS_IS_OK(status)) {
401 return status;
404 for (i=0; i<num_names; i++) {
405 struct ea_struct ea;
407 if (strncmp(names[i], XATTR_DOSSTREAM_PREFIX,
408 prefix_len) != 0) {
409 continue;
412 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
413 if (!NT_STATUS_IS_OK(status)) {
414 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
415 names[i], fname, nt_errstr(status)));
416 continue;
419 ea.name = talloc_asprintf(ea.value.data, ":%s",
420 names[i] + prefix_len);
421 if (ea.name == NULL) {
422 DEBUG(0, ("talloc failed\n"));
423 continue;
426 if (!fn(&ea, private_data)) {
427 TALLOC_FREE(ea.value.data);
428 return NT_STATUS_OK;
431 TALLOC_FREE(ea.value.data);
434 TALLOC_FREE(names);
435 return NT_STATUS_OK;
438 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
439 struct stream_struct **streams,
440 const char *name, SMB_OFF_T size,
441 SMB_OFF_T alloc_size)
443 struct stream_struct *tmp;
445 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
446 (*num_streams)+1);
447 if (tmp == NULL) {
448 return false;
451 tmp[*num_streams].name = talloc_strdup(tmp, name);
452 if (tmp[*num_streams].name == NULL) {
453 return false;
456 tmp[*num_streams].size = size;
457 tmp[*num_streams].alloc_size = alloc_size;
459 *streams = tmp;
460 *num_streams += 1;
461 return true;
464 struct streaminfo_state {
465 TALLOC_CTX *mem_ctx;
466 vfs_handle_struct *handle;
467 unsigned int num_streams;
468 struct stream_struct *streams;
469 NTSTATUS status;
472 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
474 struct streaminfo_state *state =
475 (struct streaminfo_state *)private_data;
477 if (!add_one_stream(state->mem_ctx,
478 &state->num_streams, &state->streams,
479 ea->name, ea->value.length-1,
480 smb_roundup(state->handle->conn,
481 ea->value.length-1))) {
482 state->status = NT_STATUS_NO_MEMORY;
483 return false;
486 return true;
489 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
490 struct files_struct *fsp,
491 const char *fname,
492 TALLOC_CTX *mem_ctx,
493 unsigned int *pnum_streams,
494 struct stream_struct **pstreams)
496 SMB_STRUCT_STAT sbuf;
497 int ret;
498 NTSTATUS status;
499 struct streaminfo_state state;
501 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
502 if (is_ntfs_stream_name(fsp->fsp_name)) {
503 return NT_STATUS_INVALID_PARAMETER;
505 ret = SMB_VFS_FSTAT(fsp, &sbuf);
507 else {
508 if (is_ntfs_stream_name(fname)) {
509 return NT_STATUS_INVALID_PARAMETER;
511 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
514 if (ret == -1) {
515 return map_nt_error_from_unix(errno);
518 state.streams = NULL;
519 state.num_streams = 0;
521 if (!S_ISDIR(sbuf.st_mode)) {
522 if (!add_one_stream(mem_ctx,
523 &state.num_streams, &state.streams,
524 "::$DATA", sbuf.st_size,
525 get_allocation_size(handle->conn, fsp,
526 &sbuf))) {
527 return NT_STATUS_NO_MEMORY;
531 state.mem_ctx = mem_ctx;
532 state.handle = handle;
533 state.status = NT_STATUS_OK;
535 status = walk_xattr_streams(handle->conn, fsp, fname,
536 collect_one_stream, &state);
538 if (!NT_STATUS_IS_OK(status)) {
539 TALLOC_FREE(state.streams);
540 return status;
543 if (!NT_STATUS_IS_OK(state.status)) {
544 TALLOC_FREE(state.streams);
545 return state.status;
548 *pnum_streams = state.num_streams;
549 *pstreams = state.streams;
550 return NT_STATUS_OK;
553 static int streams_xattr_statvfs(struct vfs_handle_struct *handle,
554 const char *path,
555 struct vfs_statvfs_struct *statbuf)
557 int ret;
559 ret = SMB_VFS_NEXT_STATVFS(handle, path, statbuf);
560 statbuf->FsCapabilities |= FILE_NAMED_STREAMS;
561 return ret;
565 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
566 files_struct *fsp, const void *data,
567 size_t n, SMB_OFF_T offset)
569 struct stream_io *sio =
570 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
571 struct ea_struct ea;
572 NTSTATUS status;
573 int ret;
575 if (sio == NULL) {
576 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
579 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
580 sio->base, sio->xattr_name, &ea);
581 if (!NT_STATUS_IS_OK(status)) {
582 return -1;
585 if ((offset + n) > ea.value.length-1) {
586 uint8 *tmp;
588 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
589 offset + n + 1);
591 if (tmp == NULL) {
592 TALLOC_FREE(ea.value.data);
593 errno = ENOMEM;
594 return -1;
596 ea.value.data = tmp;
597 ea.value.length = offset + n + 1;
598 ea.value.data[offset+n] = 0;
601 memcpy(ea.value.data + offset, data, n);
603 ret = SMB_VFS_FSETXATTR(fsp->base_fsp, sio->xattr_name,
604 ea.value.data, ea.value.length, 0);
606 TALLOC_FREE(ea.value.data);
608 return n;
611 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
612 files_struct *fsp, void *data,
613 size_t n, SMB_OFF_T offset)
615 struct stream_io *sio =
616 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
617 struct ea_struct ea;
618 NTSTATUS status;
619 size_t length, overlap;
621 if (sio == NULL) {
622 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
625 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
626 sio->base, sio->xattr_name, &ea);
627 if (!NT_STATUS_IS_OK(status)) {
628 return -1;
631 length = ea.value.length-1;
633 /* Attempt to read past EOF. */
634 if (length <= offset) {
635 errno = EINVAL;
636 return -1;
639 overlap = (offset + n) > length ? (length - offset) : n;
640 memcpy(data, ea.value.data + offset, overlap);
642 TALLOC_FREE(ea.value.data);
643 return overlap;
646 /* VFS operations structure */
648 static vfs_op_tuple streams_xattr_ops[] = {
649 {SMB_VFS_OP(streams_xattr_statvfs), SMB_VFS_OP_STATVFS,
650 SMB_VFS_LAYER_TRANSPARENT},
651 {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
652 SMB_VFS_LAYER_TRANSPARENT},
653 {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
654 SMB_VFS_LAYER_TRANSPARENT},
655 {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
656 SMB_VFS_LAYER_TRANSPARENT},
657 {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
658 SMB_VFS_LAYER_TRANSPARENT},
659 {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
660 SMB_VFS_LAYER_TRANSPARENT},
661 {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
662 SMB_VFS_LAYER_TRANSPARENT},
663 {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
664 SMB_VFS_LAYER_TRANSPARENT},
665 {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
666 SMB_VFS_LAYER_TRANSPARENT},
667 {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
668 SMB_VFS_LAYER_OPAQUE},
669 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
672 NTSTATUS vfs_streams_xattr_init(void);
673 NTSTATUS vfs_streams_xattr_init(void)
675 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
676 streams_xattr_ops);