2 * Unix SMB/CIFS implementation.
4 * Support for OneFS Alternate Data Streams
6 * Copyright (C) Tim Prouty, 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "onefs_config.h"
26 #include <sys/isi_enc.h>
29 * OneFS stores streams without the explicit :$DATA at the end, so this strips
30 * it off. All onefs_stream functions must call through this instead of
31 * split_ntfs_stream_name directly.
33 NTSTATUS onefs_split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
34 char **pbase, char **pstream)
39 status = split_ntfs_stream_name(mem_ctx, fname, pbase, pstream);
40 if (!NT_STATUS_IS_OK(status)) {
44 /* Default $DATA stream. */
45 if (pstream == NULL || *pstream == NULL) {
49 /* Strip off the $DATA. */
50 stream = strrchr_m(*pstream, ':');
57 NTSTATUS onefs_stream_prep_smb_fname(TALLOC_CTX *ctx,
58 const struct smb_filename *smb_fname_in,
59 struct smb_filename **smb_fname_out)
61 char *stream_name = NULL;
65 * Only attempt to strip off the trailing :$DATA if there is an actual
66 * stream there. If it is the default stream, the smb_fname_out will
67 * just have a NULL stream so the base file is opened.
69 if (smb_fname_in->stream_name &&
70 !is_ntfs_default_stream_smb_fname(smb_fname_in)) {
71 char *str_tmp = smb_fname_in->stream_name;
73 /* First strip off the leading ':' */
74 if (str_tmp[0] == ':') {
78 /* Create a new copy of the stream_name. */
79 stream_name = talloc_strdup(ctx, str_tmp);
80 if (stream_name == NULL) {
81 return NT_STATUS_NO_MEMORY;
84 /* Strip off the :$DATA if one exists. */
85 str_tmp = strrchr_m(stream_name, ':');
92 * If there was a stream that wasn't the default stream the leading
93 * colon and trailing :$DATA has now been stripped off. Create a new
94 * smb_filename to pass back.
96 status = create_synthetic_smb_fname(ctx, smb_fname_in->base_name,
97 stream_name, &smb_fname_in->st,
99 TALLOC_FREE(stream_name);
103 int onefs_is_stream(const char *path, char **pbase, char **pstream,
106 (*is_stream) = is_ntfs_stream_name(path);
112 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
114 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
122 int onefs_close(vfs_handle_struct *handle, struct files_struct *fsp)
127 ret = SMB_VFS_NEXT_CLOSE(handle, fsp->base_fsp);
129 ret2 = SMB_VFS_NEXT_CLOSE(handle, fsp);
131 return ret ? ret : ret2;
135 * Get the ADS directory fd for a file.
137 static int get_stream_dir_fd(connection_struct *conn, const char *base,
144 /* If a valid base_fdp was given, use it. */
145 if (base_fdp && *base_fdp >= 0) {
148 base_fd = onefs_sys_create_file(conn,
167 /* Open the ADS directory. */
168 dir_fd = onefs_sys_create_file(conn,
183 /* Close base_fd if it's not need or on error. */
184 if (!base_fdp || dir_fd < 0) {
190 /* Set the out base_fdp if successful and it was requested. */
191 if (base_fdp && dir_fd >= 0) {
198 int onefs_rename(vfs_handle_struct *handle,
199 const struct smb_filename *smb_fname_src,
200 const struct smb_filename *smb_fname_dst)
202 struct smb_filename *smb_fname_src_onefs = NULL;
203 struct smb_filename *smb_fname_dst_onefs = NULL;
209 START_PROFILE(syscall_rename_at);
211 if (!is_ntfs_stream_smb_fname(smb_fname_src) &&
212 !is_ntfs_stream_smb_fname(smb_fname_dst)) {
213 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
218 /* For now don't allow renames from or to the default stream. */
219 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
220 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
225 /* prep stream smb_filename structs. */
226 status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname_src,
227 &smb_fname_src_onefs);
228 if (!NT_STATUS_IS_OK(status)) {
229 errno = map_errno_from_nt_status(status);
232 status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname_dst,
233 &smb_fname_dst_onefs);
234 if (!NT_STATUS_IS_OK(status)) {
235 errno = map_errno_from_nt_status(status);
239 dir_fd = get_stream_dir_fd(handle->conn, smb_fname_src->base_name,
245 DEBUG(8, ("onefs_rename called for %s => %s\n",
246 smb_fname_str_dbg(smb_fname_src_onefs),
247 smb_fname_str_dbg(smb_fname_dst_onefs)));
249 /* Handle rename of stream to default stream specially. */
250 if (smb_fname_dst_onefs->stream_name == NULL) {
251 ret = enc_renameat(dir_fd, smb_fname_src_onefs->stream_name,
252 ENC_DEFAULT, AT_FDCWD,
253 smb_fname_dst_onefs->base_name,
256 ret = enc_renameat(dir_fd, smb_fname_src_onefs->stream_name,
258 smb_fname_dst_onefs->stream_name,
263 END_PROFILE(syscall_rename_at);
264 TALLOC_FREE(smb_fname_src_onefs);
265 TALLOC_FREE(smb_fname_dst_onefs);
276 * Merge a base file's sbuf into the a streams's sbuf.
278 static void merge_stat(SMB_STRUCT_STAT *stream_sbuf,
279 const SMB_STRUCT_STAT *base_sbuf)
281 int dos_flags = (UF_DOS_NOINDEX | UF_DOS_ARCHIVE |
282 UF_DOS_HIDDEN | UF_DOS_RO | UF_DOS_SYSTEM);
283 stream_sbuf->st_ex_mtime = base_sbuf->st_ex_mtime;
284 stream_sbuf->st_ex_ctime = base_sbuf->st_ex_ctime;
285 stream_sbuf->st_ex_atime = base_sbuf->st_ex_atime;
286 stream_sbuf->st_ex_flags &= ~dos_flags;
287 stream_sbuf->st_ex_flags |= base_sbuf->st_ex_flags & dos_flags;
290 /* fake timestamps */
291 static void onefs_adjust_stat_time(struct connection_struct *conn,
292 const char *fname, SMB_STRUCT_STAT *sbuf)
294 struct onefs_vfs_share_config cfg;
295 struct timeval tv_now = {0, 0};
296 bool static_mtime = False;
297 bool static_atime = False;
299 if (!onefs_get_config(SNUM(conn),
300 ONEFS_VFS_CONFIG_FAKETIMESTAMPS, &cfg)) {
304 if (IS_MTIME_STATIC_PATH(conn, &cfg, fname)) {
305 sbuf->st_ex_mtime = sbuf->st_ex_btime;
308 if (IS_ATIME_STATIC_PATH(conn, &cfg, fname)) {
309 sbuf->st_ex_atime = sbuf->st_ex_btime;
313 if (IS_CTIME_NOW_PATH(conn, &cfg, fname)) {
314 if (cfg.ctime_slop < 0) {
315 sbuf->st_ex_btime.tv_sec = INT_MAX - 1;
317 GetTimeOfDay(&tv_now);
318 sbuf->st_ex_btime.tv_sec = tv_now.tv_sec +
323 if (!static_mtime && IS_MTIME_NOW_PATH(conn,&cfg,fname)) {
324 if (cfg.mtime_slop < 0) {
325 sbuf->st_ex_mtime.tv_sec = INT_MAX - 1;
327 if (tv_now.tv_sec == 0)
328 GetTimeOfDay(&tv_now);
329 sbuf->st_ex_mtime.tv_sec = tv_now.tv_sec +
333 if (!static_atime && IS_ATIME_NOW_PATH(conn,&cfg,fname)) {
334 if (cfg.atime_slop < 0) {
335 sbuf->st_ex_atime.tv_sec = INT_MAX - 1;
337 if (tv_now.tv_sec == 0)
338 GetTimeOfDay(&tv_now);
339 sbuf->st_ex_atime.tv_sec = tv_now.tv_sec +
345 static int stat_stream(struct connection_struct *conn, const char *base,
346 const char *stream, SMB_STRUCT_STAT *sbuf, int flags)
348 SMB_STRUCT_STAT base_sbuf;
349 int base_fd = -1, dir_fd, ret, saved_errno;
351 dir_fd = get_stream_dir_fd(conn, base, &base_fd);
356 /* Stat the stream. */
357 ret = onefs_sys_fstat_at(dir_fd, stream, sbuf, flags);
359 /* Now stat the base file and merge the results. */
360 ret = onefs_sys_fstat(base_fd, &base_sbuf);
362 merge_stat(sbuf, &base_sbuf);
373 int onefs_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
375 struct smb_filename *smb_fname_onefs = NULL;
379 status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
381 if (!NT_STATUS_IS_OK(status)) {
382 errno = map_errno_from_nt_status(status);
387 * If the smb_fname has no stream or is :$DATA, then just stat the
388 * base stream. Otherwise stat the stream.
390 if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
391 ret = onefs_sys_stat(smb_fname_onefs->base_name,
394 ret = stat_stream(handle->conn, smb_fname_onefs->base_name,
395 smb_fname_onefs->stream_name, &smb_fname->st,
399 onefs_adjust_stat_time(handle->conn, smb_fname->base_name,
402 TALLOC_FREE(smb_fname_onefs);
407 int onefs_fstat(vfs_handle_struct *handle, struct files_struct *fsp,
408 SMB_STRUCT_STAT *sbuf)
410 SMB_STRUCT_STAT base_sbuf;
413 /* Stat the stream, by calling next_fstat on the stream's fd. */
414 ret = onefs_sys_fstat(fsp->fh->fd, sbuf);
419 /* Stat the base file and merge the results. */
420 if (fsp != NULL && fsp->base_fsp != NULL) {
421 ret = onefs_sys_fstat(fsp->base_fsp->fh->fd, &base_sbuf);
423 merge_stat(sbuf, &base_sbuf);
427 onefs_adjust_stat_time(handle->conn, fsp->fsp_name, sbuf);
431 int onefs_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
433 struct smb_filename *smb_fname_onefs = NULL;
437 status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
439 if (!NT_STATUS_IS_OK(status)) {
440 errno = map_errno_from_nt_status(status);
445 * If the smb_fname has no stream or is :$DATA, then just stat the
446 * base stream. Otherwise stat the stream.
448 if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
449 ret = onefs_sys_lstat(smb_fname_onefs->base_name,
452 ret = stat_stream(handle->conn, smb_fname_onefs->base_name,
453 smb_fname_onefs->stream_name, &smb_fname->st,
454 AT_SYMLINK_NOFOLLOW);
457 onefs_adjust_stat_time(handle->conn, smb_fname->base_name,
460 TALLOC_FREE(smb_fname_onefs);
465 int onefs_unlink(vfs_handle_struct *handle,
466 const struct smb_filename *smb_fname)
468 struct smb_filename *smb_fname_onefs = NULL;
470 int dir_fd, saved_errno;
474 if (!is_ntfs_stream_smb_fname(smb_fname)) {
475 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
478 status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
480 if (!NT_STATUS_IS_OK(status)) {
481 errno = map_errno_from_nt_status(status);
485 /* Default stream (the ::$DATA was just stripped off). */
486 if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
487 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_onefs);
491 dir_fd = get_stream_dir_fd(handle->conn, smb_fname_onefs->base_name,
498 ret = enc_unlinkat(dir_fd, smb_fname_onefs->stream_name, ENC_DEFAULT,
505 TALLOC_FREE(smb_fname_onefs);
509 int onefs_vtimes_streams(vfs_handle_struct *handle,
510 const struct smb_filename *smb_fname,
511 int flags, struct timespec times[3])
513 struct smb_filename *smb_fname_onefs = NULL;
519 START_PROFILE(syscall_ntimes);
521 if (!is_ntfs_stream_smb_fname(smb_fname)) {
522 ret = vtimes(smb_fname->base_name, times, flags);
526 status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
528 if (!NT_STATUS_IS_OK(status)) {
529 errno = map_errno_from_nt_status(status);
533 /* Default stream (the ::$DATA was just stripped off). */
534 if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
535 ret = vtimes(smb_fname_onefs->base_name, times, flags);
539 dirfd = get_stream_dir_fd(handle->conn, smb_fname->base_name, NULL);
545 ret = enc_vtimesat(dirfd, smb_fname_onefs->stream_name, ENC_DEFAULT,
553 END_PROFILE(syscall_ntimes);
554 TALLOC_FREE(smb_fname_onefs);
559 * Streaminfo enumeration functionality
561 struct streaminfo_state {
563 vfs_handle_struct *handle;
564 unsigned int num_streams;
565 struct stream_struct *streams;
569 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
570 struct stream_struct **streams,
571 const char *name, SMB_OFF_T size,
572 SMB_OFF_T alloc_size)
574 struct stream_struct *tmp;
576 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
582 tmp[*num_streams].name = talloc_asprintf(mem_ctx, ":%s:%s", name,
584 if (tmp[*num_streams].name == NULL) {
588 tmp[*num_streams].size = size;
589 tmp[*num_streams].alloc_size = alloc_size;
596 static NTSTATUS walk_onefs_streams(connection_struct *conn, files_struct *fsp,
598 struct streaminfo_state *state,
599 SMB_STRUCT_STAT *base_sbuf)
601 NTSTATUS status = NT_STATUS_OK;
602 bool opened_base_fd = false;
607 SMB_STRUCT_DIR *dirp = NULL;
608 SMB_STRUCT_DIRENT *dp = NULL;
609 files_struct fake_fs;
610 struct fd_handle fake_fh;
611 SMB_STRUCT_STAT stream_sbuf;
613 ZERO_STRUCT(fake_fh);
614 ZERO_STRUCT(fake_fs);
616 /* If the base file is already open, use its fd. */
617 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
618 base_fd = fsp->fh->fd;
620 opened_base_fd = true;
623 dir_fd = get_stream_dir_fd(conn, fname, &base_fd);
625 return map_nt_error_from_unix(errno);
628 /* Open the ADS directory. */
629 if ((dirp = fdopendir(dir_fd)) == NULL) {
630 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
631 fname, errno, strerror(errno)));
632 status = map_nt_error_from_unix(errno);
636 /* Initialize the dir state struct and add it to the list.
637 * This is a layer violation, and really should be handled by a
638 * VFS_FDOPENDIR() call which would properly setup the dir state.
639 * But since this is all within the onefs.so module, we cheat for
640 * now and call directly into the readdirplus code.
641 * NOTE: This state MUST be freed by a proper VFS_CLOSEDIR() call. */
642 ret = onefs_rdp_add_dir_state(conn, dirp);
644 DEBUG(0, ("Error adding dir_state to the list\n"));
645 status = map_nt_error_from_unix(errno);
650 fake_fs.fh = &fake_fh;
651 fake_fs.fsp_name = SMB_STRDUP(fname);
653 /* Iterate over the streams in the ADS directory. */
654 while ((dp = SMB_VFS_READDIR(conn, dirp, NULL)) != NULL) {
655 /* Skip the "." and ".." entries */
656 if ((strcmp(dp->d_name, ".") == 0) ||
657 (strcmp(dp->d_name, "..") == 0))
660 /* Open actual stream */
661 if ((stream_fd = onefs_sys_create_file(conn,
675 DEBUG(0, ("Error opening stream %s:%s. "
676 "errno=%d (%s)\n", fname, dp->d_name, errno,
681 /* Figure out the stat info. */
682 fake_fh.fd = stream_fd;
683 ret = SMB_VFS_FSTAT(&fake_fs, &stream_sbuf);
687 DEBUG(0, ("Error fstating stream %s:%s. "
688 "errno=%d (%s)\n", fname, dp->d_name, errno,
693 merge_stat(&stream_sbuf, base_sbuf);
695 if (!add_one_stream(state->mem_ctx,
696 &state->num_streams, &state->streams,
697 dp->d_name, stream_sbuf.st_ex_size,
698 SMB_VFS_GET_ALLOC_SIZE(conn, NULL,
700 state->status = NT_STATUS_NO_MEMORY;
706 /* Cleanup everything that was opened. */
708 SMB_VFS_CLOSEDIR(conn, dirp);
713 if (opened_base_fd) {
714 SMB_ASSERT(base_fd >= 0);
718 SAFE_FREE(fake_fs.fsp_name);
722 NTSTATUS onefs_streaminfo(vfs_handle_struct *handle,
723 struct files_struct *fsp,
726 unsigned int *num_streams,
727 struct stream_struct **streams)
729 SMB_STRUCT_STAT sbuf;
732 struct streaminfo_state state;
734 /* Get a valid stat. */
735 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
736 ret = SMB_VFS_FSTAT(fsp, &sbuf);
738 struct smb_filename *smb_fname = NULL;
740 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
742 if (!NT_STATUS_IS_OK(status)) {
745 ret = SMB_VFS_STAT(handle->conn, smb_fname);
747 sbuf = smb_fname->st;
749 TALLOC_FREE(smb_fname);
753 return map_nt_error_from_unix(errno);
756 state.streams = NULL;
757 state.num_streams = 0;
759 if (lp_parm_bool(SNUM(handle->conn), PARM_ONEFS_TYPE,
760 PARM_IGNORE_STREAMS, PARM_IGNORE_STREAMS_DEFAULT)) {
764 /* Add the default stream. */
765 if (S_ISREG(sbuf.st_ex_mode)) {
766 if (!add_one_stream(mem_ctx,
767 &state.num_streams, &state.streams,
769 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
771 return NT_STATUS_NO_MEMORY;
775 state.mem_ctx = mem_ctx;
776 state.handle = handle;
777 state.status = NT_STATUS_OK;
779 /* If there are more streams, add them too. */
780 if (sbuf.st_ex_flags & UF_HASADS) {
782 status = walk_onefs_streams(handle->conn, fsp, fname,
785 if (!NT_STATUS_IS_OK(status)) {
786 TALLOC_FREE(state.streams);
790 if (!NT_STATUS_IS_OK(state.status)) {
791 TALLOC_FREE(state.streams);
796 *num_streams = state.num_streams;
797 *streams = state.streams;