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
, const char *oldname
,
201 TALLOC_CTX
*frame
= NULL
;
212 START_PROFILE(syscall_rename_at
);
214 frame
= talloc_stackframe();
216 ret
= onefs_is_stream(oldname
, &obase
, &osname
, &old_is_stream
);
218 END_PROFILE(syscall_rename_at
);
222 ret
= onefs_is_stream(newname
, &nbase
, &nsname
, &new_is_stream
);
224 END_PROFILE(syscall_rename_at
);
228 if (!old_is_stream
&& !new_is_stream
) {
229 ret
= SMB_VFS_NEXT_RENAME(handle
, oldname
, newname
);
230 END_PROFILE(syscall_rename_at
);
234 dir_fd
= get_stream_dir_fd(handle
->conn
, obase
, NULL
);
239 DEBUG(8,("onefs_rename called for %s : %s => %s : %s\n",
240 obase
, osname
, nbase
, nsname
));
242 /* Handle rename of stream to default stream specially. */
243 if (nsname
== NULL
) {
244 ret
= enc_renameat(dir_fd
, osname
, ENC_DEFAULT
, AT_FDCWD
,
247 ret
= enc_renameat(dir_fd
, osname
, ENC_DEFAULT
, dir_fd
, nsname
,
252 END_PROFILE(syscall_rename_at
);
264 * Merge a base file's sbuf into the a streams's sbuf.
266 static void merge_stat(SMB_STRUCT_STAT
*stream_sbuf
,
267 const SMB_STRUCT_STAT
*base_sbuf
)
269 int dos_flags
= (UF_DOS_NOINDEX
| UF_DOS_ARCHIVE
|
270 UF_DOS_HIDDEN
| UF_DOS_RO
| UF_DOS_SYSTEM
);
271 stream_sbuf
->st_ex_mtime
= base_sbuf
->st_ex_mtime
;
272 stream_sbuf
->st_ex_ctime
= base_sbuf
->st_ex_ctime
;
273 stream_sbuf
->st_ex_atime
= base_sbuf
->st_ex_atime
;
274 stream_sbuf
->st_ex_flags
&= ~dos_flags
;
275 stream_sbuf
->st_ex_flags
|= base_sbuf
->st_ex_flags
& dos_flags
;
278 /* fake timestamps */
279 static void onefs_adjust_stat_time(struct connection_struct
*conn
,
280 const char *fname
, SMB_STRUCT_STAT
*sbuf
)
282 struct onefs_vfs_share_config cfg
;
283 struct timeval tv_now
= {0, 0};
284 bool static_mtime
= False
;
285 bool static_atime
= False
;
287 if (!onefs_get_config(SNUM(conn
),
288 ONEFS_VFS_CONFIG_FAKETIMESTAMPS
, &cfg
)) {
292 if (IS_MTIME_STATIC_PATH(conn
, &cfg
, fname
)) {
293 sbuf
->st_ex_mtime
= sbuf
->st_ex_btime
;
296 if (IS_ATIME_STATIC_PATH(conn
, &cfg
, fname
)) {
297 sbuf
->st_ex_atime
= sbuf
->st_ex_btime
;
301 if (IS_CTIME_NOW_PATH(conn
, &cfg
, fname
)) {
302 if (cfg
.ctime_slop
< 0) {
303 sbuf
->st_ex_btime
.tv_sec
= INT_MAX
- 1;
305 GetTimeOfDay(&tv_now
);
306 sbuf
->st_ex_btime
.tv_sec
= tv_now
.tv_sec
+
311 if (!static_mtime
&& IS_MTIME_NOW_PATH(conn
,&cfg
,fname
)) {
312 if (cfg
.mtime_slop
< 0) {
313 sbuf
->st_ex_mtime
.tv_sec
= INT_MAX
- 1;
315 if (tv_now
.tv_sec
== 0)
316 GetTimeOfDay(&tv_now
);
317 sbuf
->st_ex_mtime
.tv_sec
= tv_now
.tv_sec
+
321 if (!static_atime
&& IS_ATIME_NOW_PATH(conn
,&cfg
,fname
)) {
322 if (cfg
.atime_slop
< 0) {
323 sbuf
->st_ex_atime
.tv_sec
= INT_MAX
- 1;
325 if (tv_now
.tv_sec
== 0)
326 GetTimeOfDay(&tv_now
);
327 sbuf
->st_ex_atime
.tv_sec
= tv_now
.tv_sec
+
333 static int stat_stream(struct connection_struct
*conn
, const char *base
,
334 const char *stream
, SMB_STRUCT_STAT
*sbuf
, int flags
)
336 SMB_STRUCT_STAT base_sbuf
;
337 int base_fd
= -1, dir_fd
, ret
, saved_errno
;
339 dir_fd
= get_stream_dir_fd(conn
, base
, &base_fd
);
344 /* Stat the stream. */
345 ret
= onefs_sys_fstat_at(dir_fd
, stream
, sbuf
, flags
);
347 /* Now stat the base file and merge the results. */
348 ret
= onefs_sys_fstat(base_fd
, &base_sbuf
);
350 merge_stat(sbuf
, &base_sbuf
);
361 int onefs_stat(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
)
363 struct smb_filename
*smb_fname_onefs
= NULL
;
367 status
= onefs_stream_prep_smb_fname(talloc_tos(), smb_fname
,
369 if (!NT_STATUS_IS_OK(status
)) {
370 errno
= map_errno_from_nt_status(status
);
375 * If the smb_fname has no stream or is :$DATA, then just stat the
376 * base stream. Otherwise stat the stream.
378 if (!is_ntfs_stream_smb_fname(smb_fname_onefs
)) {
379 ret
= onefs_sys_stat(smb_fname_onefs
->base_name
,
382 ret
= stat_stream(handle
->conn
, smb_fname_onefs
->base_name
,
383 smb_fname_onefs
->stream_name
, &smb_fname
->st
,
387 onefs_adjust_stat_time(handle
->conn
, smb_fname
->base_name
,
390 TALLOC_FREE(smb_fname_onefs
);
395 int onefs_fstat(vfs_handle_struct
*handle
, struct files_struct
*fsp
,
396 SMB_STRUCT_STAT
*sbuf
)
398 SMB_STRUCT_STAT base_sbuf
;
401 /* Stat the stream, by calling next_fstat on the stream's fd. */
402 ret
= onefs_sys_fstat(fsp
->fh
->fd
, sbuf
);
407 /* Stat the base file and merge the results. */
408 if (fsp
!= NULL
&& fsp
->base_fsp
!= NULL
) {
409 ret
= onefs_sys_fstat(fsp
->base_fsp
->fh
->fd
, &base_sbuf
);
411 merge_stat(sbuf
, &base_sbuf
);
415 onefs_adjust_stat_time(handle
->conn
, fsp
->fsp_name
, sbuf
);
419 int onefs_lstat(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
)
421 struct smb_filename
*smb_fname_onefs
= NULL
;
425 status
= onefs_stream_prep_smb_fname(talloc_tos(), smb_fname
,
427 if (!NT_STATUS_IS_OK(status
)) {
428 errno
= map_errno_from_nt_status(status
);
433 * If the smb_fname has no stream or is :$DATA, then just stat the
434 * base stream. Otherwise stat the stream.
436 if (!is_ntfs_stream_smb_fname(smb_fname_onefs
)) {
437 ret
= onefs_sys_lstat(smb_fname_onefs
->base_name
,
440 ret
= stat_stream(handle
->conn
, smb_fname_onefs
->base_name
,
441 smb_fname_onefs
->stream_name
, &smb_fname
->st
,
442 AT_SYMLINK_NOFOLLOW
);
445 onefs_adjust_stat_time(handle
->conn
, smb_fname
->base_name
,
448 TALLOC_FREE(smb_fname_onefs
);
453 int onefs_unlink(vfs_handle_struct
*handle
, const char *path
)
459 int dir_fd
, saved_errno
;
461 ret
= onefs_is_stream(path
, &base
, &stream
, &is_stream
);
467 return SMB_VFS_NEXT_UNLINK(handle
, path
);
470 /* If it's the ::$DATA stream just unlink the base file name. */
472 return SMB_VFS_NEXT_UNLINK(handle
, base
);
475 dir_fd
= get_stream_dir_fd(handle
->conn
, base
, NULL
);
480 ret
= enc_unlinkat(dir_fd
, stream
, ENC_DEFAULT
, 0);
488 int onefs_vtimes_streams(vfs_handle_struct
*handle
, const char *fname
,
489 int flags
, struct timespec times
[3])
498 START_PROFILE(syscall_ntimes
);
500 ret
= onefs_is_stream(fname
, &base
, &stream
, &is_stream
);
505 ret
= vtimes(fname
, times
, flags
);
509 dirfd
= get_stream_dir_fd(handle
->conn
, base
, NULL
);
514 ret
= enc_vtimesat(dirfd
, stream
, ENC_DEFAULT
, times
, flags
);
516 END_PROFILE(syscall_ntimes
);
524 int onefs_chflags(vfs_handle_struct
*handle
, const char *path
,
530 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path
,
532 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
538 * Only set the attributes on the base file. ifs_createfile handles
539 * file creation attribute semantics.
541 return SMB_VFS_NEXT_CHFLAGS(handle
, base
, flags
);
545 * Streaminfo enumeration functionality
547 struct streaminfo_state
{
549 vfs_handle_struct
*handle
;
550 unsigned int num_streams
;
551 struct stream_struct
*streams
;
555 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
556 struct stream_struct
**streams
,
557 const char *name
, SMB_OFF_T size
,
558 SMB_OFF_T alloc_size
)
560 struct stream_struct
*tmp
;
562 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
568 tmp
[*num_streams
].name
= talloc_asprintf(mem_ctx
, ":%s:%s", name
,
570 if (tmp
[*num_streams
].name
== NULL
) {
574 tmp
[*num_streams
].size
= size
;
575 tmp
[*num_streams
].alloc_size
= alloc_size
;
582 static NTSTATUS
walk_onefs_streams(connection_struct
*conn
, files_struct
*fsp
,
584 struct streaminfo_state
*state
,
585 SMB_STRUCT_STAT
*base_sbuf
)
587 NTSTATUS status
= NT_STATUS_OK
;
588 bool opened_base_fd
= false;
593 SMB_STRUCT_DIR
*dirp
= NULL
;
594 SMB_STRUCT_DIRENT
*dp
= NULL
;
595 files_struct fake_fs
;
596 struct fd_handle fake_fh
;
597 SMB_STRUCT_STAT stream_sbuf
;
599 ZERO_STRUCT(fake_fh
);
600 ZERO_STRUCT(fake_fs
);
602 /* If the base file is already open, use its fd. */
603 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
604 base_fd
= fsp
->fh
->fd
;
606 opened_base_fd
= true;
609 dir_fd
= get_stream_dir_fd(conn
, fname
, &base_fd
);
611 return map_nt_error_from_unix(errno
);
614 /* Open the ADS directory. */
615 if ((dirp
= fdopendir(dir_fd
)) == NULL
) {
616 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
617 fname
, errno
, strerror(errno
)));
618 status
= map_nt_error_from_unix(errno
);
622 /* Initialize the dir state struct and add it to the list.
623 * This is a layer violation, and really should be handled by a
624 * VFS_FDOPENDIR() call which would properly setup the dir state.
625 * But since this is all within the onefs.so module, we cheat for
626 * now and call directly into the readdirplus code.
627 * NOTE: This state MUST be freed by a proper VFS_CLOSEDIR() call. */
628 ret
= onefs_rdp_add_dir_state(conn
, dirp
);
630 DEBUG(0, ("Error adding dir_state to the list\n"));
631 status
= map_nt_error_from_unix(errno
);
636 fake_fs
.fh
= &fake_fh
;
637 fake_fs
.fsp_name
= SMB_STRDUP(fname
);
639 /* Iterate over the streams in the ADS directory. */
640 while ((dp
= SMB_VFS_READDIR(conn
, dirp
, NULL
)) != NULL
) {
641 /* Skip the "." and ".." entries */
642 if ((strcmp(dp
->d_name
, ".") == 0) ||
643 (strcmp(dp
->d_name
, "..") == 0))
646 /* Open actual stream */
647 if ((stream_fd
= onefs_sys_create_file(conn
,
661 DEBUG(0, ("Error opening stream %s:%s. "
662 "errno=%d (%s)\n", fname
, dp
->d_name
, errno
,
667 /* Figure out the stat info. */
668 fake_fh
.fd
= stream_fd
;
669 ret
= SMB_VFS_FSTAT(&fake_fs
, &stream_sbuf
);
673 DEBUG(0, ("Error fstating stream %s:%s. "
674 "errno=%d (%s)\n", fname
, dp
->d_name
, errno
,
679 merge_stat(&stream_sbuf
, base_sbuf
);
681 if (!add_one_stream(state
->mem_ctx
,
682 &state
->num_streams
, &state
->streams
,
683 dp
->d_name
, stream_sbuf
.st_ex_size
,
684 SMB_VFS_GET_ALLOC_SIZE(conn
, NULL
,
686 state
->status
= NT_STATUS_NO_MEMORY
;
692 /* Cleanup everything that was opened. */
694 SMB_VFS_CLOSEDIR(conn
, dirp
);
699 if (opened_base_fd
) {
700 SMB_ASSERT(base_fd
>= 0);
704 SAFE_FREE(fake_fs
.fsp_name
);
708 NTSTATUS
onefs_streaminfo(vfs_handle_struct
*handle
,
709 struct files_struct
*fsp
,
712 unsigned int *num_streams
,
713 struct stream_struct
**streams
)
715 SMB_STRUCT_STAT sbuf
;
718 struct streaminfo_state state
;
720 /* Get a valid stat. */
721 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
722 if (is_ntfs_stream_name(fsp
->fsp_name
)) {
723 return NT_STATUS_INVALID_PARAMETER
;
725 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
727 struct smb_filename
*smb_fname
= NULL
;
729 if (is_ntfs_stream_name(fname
)) {
730 return NT_STATUS_INVALID_PARAMETER
;
733 status
= create_synthetic_smb_fname(talloc_tos(), fname
, NULL
,
735 if (!NT_STATUS_IS_OK(status
)) {
738 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
740 sbuf
= smb_fname
->st
;
742 TALLOC_FREE(smb_fname
);
746 return map_nt_error_from_unix(errno
);
749 state
.streams
= NULL
;
750 state
.num_streams
= 0;
752 if (lp_parm_bool(SNUM(handle
->conn
), PARM_ONEFS_TYPE
,
753 PARM_IGNORE_STREAMS
, PARM_IGNORE_STREAMS_DEFAULT
)) {
757 /* Add the default stream. */
758 if (S_ISREG(sbuf
.st_ex_mode
)) {
759 if (!add_one_stream(mem_ctx
,
760 &state
.num_streams
, &state
.streams
,
762 SMB_VFS_GET_ALLOC_SIZE(handle
->conn
, fsp
,
764 return NT_STATUS_NO_MEMORY
;
768 state
.mem_ctx
= mem_ctx
;
769 state
.handle
= handle
;
770 state
.status
= NT_STATUS_OK
;
772 /* If there are more streams, add them too. */
773 if (sbuf
.st_ex_flags
& UF_HASADS
) {
775 status
= walk_onefs_streams(handle
->conn
, fsp
, fname
,
778 if (!NT_STATUS_IS_OK(status
)) {
779 TALLOC_FREE(state
.streams
);
783 if (!NT_STATUS_IS_OK(state
.status
)) {
784 TALLOC_FREE(state
.streams
);
789 *num_streams
= state
.num_streams
;
790 *streams
= state
.streams
;