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, const char *path)
471 int dir_fd, saved_errno;
473 ret = onefs_is_stream(path, &base, &stream, &is_stream);
479 return SMB_VFS_NEXT_UNLINK(handle, path);
482 /* If it's the ::$DATA stream just unlink the base file name. */
484 return SMB_VFS_NEXT_UNLINK(handle, base);
487 dir_fd = get_stream_dir_fd(handle->conn, base, NULL);
492 ret = enc_unlinkat(dir_fd, stream, ENC_DEFAULT, 0);
500 int onefs_vtimes_streams(vfs_handle_struct *handle, const char *fname,
501 int flags, struct timespec times[3])
510 START_PROFILE(syscall_ntimes);
512 ret = onefs_is_stream(fname, &base, &stream, &is_stream);
517 ret = vtimes(fname, times, flags);
521 dirfd = get_stream_dir_fd(handle->conn, base, NULL);
526 ret = enc_vtimesat(dirfd, stream, ENC_DEFAULT, times, flags);
528 END_PROFILE(syscall_ntimes);
536 int onefs_chflags(vfs_handle_struct *handle, const char *path,
542 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
544 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
550 * Only set the attributes on the base file. ifs_createfile handles
551 * file creation attribute semantics.
553 return SMB_VFS_NEXT_CHFLAGS(handle, base, flags);
557 * Streaminfo enumeration functionality
559 struct streaminfo_state {
561 vfs_handle_struct *handle;
562 unsigned int num_streams;
563 struct stream_struct *streams;
567 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
568 struct stream_struct **streams,
569 const char *name, SMB_OFF_T size,
570 SMB_OFF_T alloc_size)
572 struct stream_struct *tmp;
574 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
580 tmp[*num_streams].name = talloc_asprintf(mem_ctx, ":%s:%s", name,
582 if (tmp[*num_streams].name == NULL) {
586 tmp[*num_streams].size = size;
587 tmp[*num_streams].alloc_size = alloc_size;
594 static NTSTATUS walk_onefs_streams(connection_struct *conn, files_struct *fsp,
596 struct streaminfo_state *state,
597 SMB_STRUCT_STAT *base_sbuf)
599 NTSTATUS status = NT_STATUS_OK;
600 bool opened_base_fd = false;
605 SMB_STRUCT_DIR *dirp = NULL;
606 SMB_STRUCT_DIRENT *dp = NULL;
607 files_struct fake_fs;
608 struct fd_handle fake_fh;
609 SMB_STRUCT_STAT stream_sbuf;
611 ZERO_STRUCT(fake_fh);
612 ZERO_STRUCT(fake_fs);
614 /* If the base file is already open, use its fd. */
615 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
616 base_fd = fsp->fh->fd;
618 opened_base_fd = true;
621 dir_fd = get_stream_dir_fd(conn, fname, &base_fd);
623 return map_nt_error_from_unix(errno);
626 /* Open the ADS directory. */
627 if ((dirp = fdopendir(dir_fd)) == NULL) {
628 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
629 fname, errno, strerror(errno)));
630 status = map_nt_error_from_unix(errno);
634 /* Initialize the dir state struct and add it to the list.
635 * This is a layer violation, and really should be handled by a
636 * VFS_FDOPENDIR() call which would properly setup the dir state.
637 * But since this is all within the onefs.so module, we cheat for
638 * now and call directly into the readdirplus code.
639 * NOTE: This state MUST be freed by a proper VFS_CLOSEDIR() call. */
640 ret = onefs_rdp_add_dir_state(conn, dirp);
642 DEBUG(0, ("Error adding dir_state to the list\n"));
643 status = map_nt_error_from_unix(errno);
648 fake_fs.fh = &fake_fh;
649 fake_fs.fsp_name = SMB_STRDUP(fname);
651 /* Iterate over the streams in the ADS directory. */
652 while ((dp = SMB_VFS_READDIR(conn, dirp, NULL)) != NULL) {
653 /* Skip the "." and ".." entries */
654 if ((strcmp(dp->d_name, ".") == 0) ||
655 (strcmp(dp->d_name, "..") == 0))
658 /* Open actual stream */
659 if ((stream_fd = onefs_sys_create_file(conn,
673 DEBUG(0, ("Error opening stream %s:%s. "
674 "errno=%d (%s)\n", fname, dp->d_name, errno,
679 /* Figure out the stat info. */
680 fake_fh.fd = stream_fd;
681 ret = SMB_VFS_FSTAT(&fake_fs, &stream_sbuf);
685 DEBUG(0, ("Error fstating stream %s:%s. "
686 "errno=%d (%s)\n", fname, dp->d_name, errno,
691 merge_stat(&stream_sbuf, base_sbuf);
693 if (!add_one_stream(state->mem_ctx,
694 &state->num_streams, &state->streams,
695 dp->d_name, stream_sbuf.st_ex_size,
696 SMB_VFS_GET_ALLOC_SIZE(conn, NULL,
698 state->status = NT_STATUS_NO_MEMORY;
704 /* Cleanup everything that was opened. */
706 SMB_VFS_CLOSEDIR(conn, dirp);
711 if (opened_base_fd) {
712 SMB_ASSERT(base_fd >= 0);
716 SAFE_FREE(fake_fs.fsp_name);
720 NTSTATUS onefs_streaminfo(vfs_handle_struct *handle,
721 struct files_struct *fsp,
724 unsigned int *num_streams,
725 struct stream_struct **streams)
727 SMB_STRUCT_STAT sbuf;
730 struct streaminfo_state state;
732 /* Get a valid stat. */
733 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
734 if (is_ntfs_stream_name(fsp->fsp_name)) {
735 return NT_STATUS_INVALID_PARAMETER;
737 ret = SMB_VFS_FSTAT(fsp, &sbuf);
739 struct smb_filename *smb_fname = NULL;
741 if (is_ntfs_stream_name(fname)) {
742 return NT_STATUS_INVALID_PARAMETER;
745 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
747 if (!NT_STATUS_IS_OK(status)) {
750 ret = SMB_VFS_STAT(handle->conn, smb_fname);
752 sbuf = smb_fname->st;
754 TALLOC_FREE(smb_fname);
758 return map_nt_error_from_unix(errno);
761 state.streams = NULL;
762 state.num_streams = 0;
764 if (lp_parm_bool(SNUM(handle->conn), PARM_ONEFS_TYPE,
765 PARM_IGNORE_STREAMS, PARM_IGNORE_STREAMS_DEFAULT)) {
769 /* Add the default stream. */
770 if (S_ISREG(sbuf.st_ex_mode)) {
771 if (!add_one_stream(mem_ctx,
772 &state.num_streams, &state.streams,
774 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
776 return NT_STATUS_NO_MEMORY;
780 state.mem_ctx = mem_ctx;
781 state.handle = handle;
782 state.status = NT_STATUS_OK;
784 /* If there are more streams, add them too. */
785 if (sbuf.st_ex_flags & UF_HASADS) {
787 status = walk_onefs_streams(handle->conn, fsp, fname,
790 if (!NT_STATUS_IS_OK(status)) {
791 TALLOC_FREE(state.streams);
795 if (!NT_STATUS_IS_OK(state.status)) {
796 TALLOC_FREE(state.streams);
801 *num_streams = state.num_streams;
802 *streams = state.streams;