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/>.
23 #include "smbd/smbd.h"
25 #include "onefs_config.h"
27 #include <sys/isi_enc.h>
29 NTSTATUS
onefs_stream_prep_smb_fname(TALLOC_CTX
*ctx
,
30 const struct smb_filename
*smb_fname_in
,
31 struct smb_filename
**smb_fname_out
)
33 char *stream_name
= NULL
;
37 * Only attempt to strip off the trailing :$DATA if there is an actual
38 * stream there. If it is the default stream, the smb_fname_out will
39 * just have a NULL stream so the base file is opened.
41 if (smb_fname_in
->stream_name
&&
42 !is_ntfs_default_stream_smb_fname(smb_fname_in
)) {
43 char *str_tmp
= smb_fname_in
->stream_name
;
45 /* First strip off the leading ':' */
46 if (str_tmp
[0] == ':') {
50 /* Create a new copy of the stream_name. */
51 stream_name
= talloc_strdup(ctx
, str_tmp
);
52 if (stream_name
== NULL
) {
53 return NT_STATUS_NO_MEMORY
;
56 /* Strip off the :$DATA if one exists. */
57 str_tmp
= strrchr_m(stream_name
, ':');
59 if (StrCaseCmp(str_tmp
, ":$DATA") != 0) {
60 return NT_STATUS_INVALID_PARAMETER
;
67 * If there was a stream that wasn't the default stream the leading
68 * colon and trailing :$DATA has now been stripped off. Create a new
69 * smb_filename to pass back.
71 status
= create_synthetic_smb_fname(ctx
, smb_fname_in
->base_name
,
72 stream_name
, &smb_fname_in
->st
,
74 TALLOC_FREE(stream_name
);
76 if (!NT_STATUS_IS_OK(status
)) {
77 DEBUG(5, ("Failed to prep stream name for %s: %s\n",
79 smb_fname_str_dbg(*smb_fname_out
) : "NULL",
85 int onefs_close(vfs_handle_struct
*handle
, struct files_struct
*fsp
)
90 ret
= SMB_VFS_NEXT_CLOSE(handle
, fsp
->base_fsp
);
92 ret2
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
94 return ret
? ret
: ret2
;
98 * Get the ADS directory fd for a file.
100 static int get_stream_dir_fd(connection_struct
*conn
, const char *base
,
107 DEBUG(10, ("Getting stream directory fd: %s (%d)\n", base
,
108 base_fdp
? *base_fdp
: -1));
110 /* If a valid base_fdp was given, use it. */
111 if (base_fdp
&& *base_fdp
>= 0) {
114 base_fd
= onefs_sys_create_file(conn
,
129 DEBUG(5, ("Failed getting base fd: %s\n",
135 /* Open the ADS directory. */
136 dir_fd
= onefs_sys_create_file(conn
,
151 /* Close base_fd if it's not need or on error. */
152 if (!base_fdp
|| dir_fd
< 0) {
158 /* Set the out base_fdp if successful and it was requested. */
159 if (base_fdp
&& dir_fd
>= 0) {
164 DEBUG(5, ("Failed getting stream directory fd: %s\n",
171 int onefs_rename(vfs_handle_struct
*handle
,
172 const struct smb_filename
*smb_fname_src
,
173 const struct smb_filename
*smb_fname_dst
)
175 struct smb_filename
*smb_fname_src_onefs
= NULL
;
176 struct smb_filename
*smb_fname_dst_onefs
= NULL
;
182 START_PROFILE(syscall_rename_at
);
184 if (!is_ntfs_stream_smb_fname(smb_fname_src
) &&
185 !is_ntfs_stream_smb_fname(smb_fname_dst
)) {
186 ret
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
191 /* For now don't allow renames from or to the default stream. */
192 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
193 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
194 DEBUG(3, ("Unable to rename to/from a default stream: %s -> "
195 "%s\n", smb_fname_str_dbg(smb_fname_src
),
196 smb_fname_str_dbg(smb_fname_dst
)));
201 /* prep stream smb_filename structs. */
202 status
= onefs_stream_prep_smb_fname(talloc_tos(), smb_fname_src
,
203 &smb_fname_src_onefs
);
204 if (!NT_STATUS_IS_OK(status
)) {
205 errno
= map_errno_from_nt_status(status
);
208 status
= onefs_stream_prep_smb_fname(talloc_tos(), smb_fname_dst
,
209 &smb_fname_dst_onefs
);
210 if (!NT_STATUS_IS_OK(status
)) {
211 errno
= map_errno_from_nt_status(status
);
215 dir_fd
= get_stream_dir_fd(handle
->conn
, smb_fname_src
->base_name
,
221 DEBUG(8, ("onefs_rename called for %s => %s\n",
222 smb_fname_str_dbg(smb_fname_src_onefs
),
223 smb_fname_str_dbg(smb_fname_dst_onefs
)));
225 /* Handle rename of stream to default stream specially. */
226 if (smb_fname_dst_onefs
->stream_name
== NULL
) {
227 ret
= enc_renameat(dir_fd
, smb_fname_src_onefs
->stream_name
,
228 ENC_DEFAULT
, AT_FDCWD
,
229 smb_fname_dst_onefs
->base_name
,
232 ret
= enc_renameat(dir_fd
, smb_fname_src_onefs
->stream_name
,
234 smb_fname_dst_onefs
->stream_name
,
239 END_PROFILE(syscall_rename_at
);
240 TALLOC_FREE(smb_fname_src_onefs
);
241 TALLOC_FREE(smb_fname_dst_onefs
);
252 * Merge a base file's sbuf into the a streams's sbuf.
254 static void merge_stat(SMB_STRUCT_STAT
*stream_sbuf
,
255 const SMB_STRUCT_STAT
*base_sbuf
)
257 int dos_flags
= (UF_DOS_NOINDEX
| UF_DOS_ARCHIVE
|
258 UF_DOS_HIDDEN
| UF_DOS_RO
| UF_DOS_SYSTEM
);
259 stream_sbuf
->st_ex_mtime
= base_sbuf
->st_ex_mtime
;
260 stream_sbuf
->st_ex_ctime
= base_sbuf
->st_ex_ctime
;
261 stream_sbuf
->st_ex_atime
= base_sbuf
->st_ex_atime
;
262 stream_sbuf
->st_ex_flags
&= ~dos_flags
;
263 stream_sbuf
->st_ex_flags
|= base_sbuf
->st_ex_flags
& dos_flags
;
266 /* fake timestamps */
267 static void onefs_adjust_stat_time(struct connection_struct
*conn
,
268 const char *fname
, SMB_STRUCT_STAT
*sbuf
)
270 struct onefs_vfs_share_config cfg
;
271 struct timeval tv_now
= {0, 0};
272 bool static_mtime
= False
;
273 bool static_atime
= False
;
275 if (!onefs_get_config(SNUM(conn
),
276 ONEFS_VFS_CONFIG_FAKETIMESTAMPS
, &cfg
)) {
280 if (IS_MTIME_STATIC_PATH(conn
, &cfg
, fname
)) {
281 sbuf
->st_ex_mtime
= sbuf
->st_ex_btime
;
284 if (IS_ATIME_STATIC_PATH(conn
, &cfg
, fname
)) {
285 sbuf
->st_ex_atime
= sbuf
->st_ex_btime
;
289 if (IS_CTIME_NOW_PATH(conn
, &cfg
, fname
)) {
290 if (cfg
.ctime_slop
< 0) {
291 sbuf
->st_ex_btime
.tv_sec
= INT_MAX
- 1;
293 GetTimeOfDay(&tv_now
);
294 sbuf
->st_ex_btime
.tv_sec
= tv_now
.tv_sec
+
299 if (!static_mtime
&& IS_MTIME_NOW_PATH(conn
,&cfg
,fname
)) {
300 if (cfg
.mtime_slop
< 0) {
301 sbuf
->st_ex_mtime
.tv_sec
= INT_MAX
- 1;
303 if (tv_now
.tv_sec
== 0)
304 GetTimeOfDay(&tv_now
);
305 sbuf
->st_ex_mtime
.tv_sec
= tv_now
.tv_sec
+
309 if (!static_atime
&& IS_ATIME_NOW_PATH(conn
,&cfg
,fname
)) {
310 if (cfg
.atime_slop
< 0) {
311 sbuf
->st_ex_atime
.tv_sec
= INT_MAX
- 1;
313 if (tv_now
.tv_sec
== 0)
314 GetTimeOfDay(&tv_now
);
315 sbuf
->st_ex_atime
.tv_sec
= tv_now
.tv_sec
+
321 static int stat_stream(struct connection_struct
*conn
, const char *base
,
322 const char *stream
, SMB_STRUCT_STAT
*sbuf
, int flags
)
324 SMB_STRUCT_STAT base_sbuf
;
325 int base_fd
= -1, dir_fd
, ret
, saved_errno
;
327 dir_fd
= get_stream_dir_fd(conn
, base
, &base_fd
);
332 /* Stat the stream. */
333 ret
= onefs_sys_fstat_at(dir_fd
, stream
, sbuf
, flags
);
335 DEBUG(10, ("stat of stream '%s' failed: %s\n", stream
,
338 /* Now stat the base file and merge the results. */
339 ret
= onefs_sys_fstat(base_fd
, &base_sbuf
);
341 merge_stat(sbuf
, &base_sbuf
);
352 int onefs_stat(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
)
354 struct smb_filename
*smb_fname_onefs
= NULL
;
358 status
= onefs_stream_prep_smb_fname(talloc_tos(), smb_fname
,
360 if (!NT_STATUS_IS_OK(status
)) {
361 errno
= map_errno_from_nt_status(status
);
366 * If the smb_fname has no stream or is :$DATA, then just stat the
367 * base stream. Otherwise stat the stream.
369 if (!is_ntfs_stream_smb_fname(smb_fname_onefs
)) {
370 ret
= onefs_sys_stat(smb_fname_onefs
->base_name
,
373 ret
= stat_stream(handle
->conn
, smb_fname_onefs
->base_name
,
374 smb_fname_onefs
->stream_name
, &smb_fname
->st
,
378 onefs_adjust_stat_time(handle
->conn
, smb_fname
->base_name
,
381 TALLOC_FREE(smb_fname_onefs
);
386 int onefs_fstat(vfs_handle_struct
*handle
, struct files_struct
*fsp
,
387 SMB_STRUCT_STAT
*sbuf
)
389 SMB_STRUCT_STAT base_sbuf
;
392 /* Stat the stream, by calling next_fstat on the stream's fd. */
393 ret
= onefs_sys_fstat(fsp
->fh
->fd
, sbuf
);
398 /* Stat the base file and merge the results. */
399 if (fsp
!= NULL
&& fsp
->base_fsp
!= NULL
) {
400 ret
= onefs_sys_fstat(fsp
->base_fsp
->fh
->fd
, &base_sbuf
);
402 merge_stat(sbuf
, &base_sbuf
);
406 onefs_adjust_stat_time(handle
->conn
, fsp
->fsp_name
->base_name
, sbuf
);
410 int onefs_lstat(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
)
412 struct smb_filename
*smb_fname_onefs
= NULL
;
416 status
= onefs_stream_prep_smb_fname(talloc_tos(), smb_fname
,
418 if (!NT_STATUS_IS_OK(status
)) {
419 errno
= map_errno_from_nt_status(status
);
424 * If the smb_fname has no stream or is :$DATA, then just stat the
425 * base stream. Otherwise stat the stream.
427 if (!is_ntfs_stream_smb_fname(smb_fname_onefs
)) {
428 ret
= onefs_sys_lstat(smb_fname_onefs
->base_name
,
431 ret
= stat_stream(handle
->conn
, smb_fname_onefs
->base_name
,
432 smb_fname_onefs
->stream_name
, &smb_fname
->st
,
433 AT_SYMLINK_NOFOLLOW
);
436 onefs_adjust_stat_time(handle
->conn
, smb_fname
->base_name
,
439 TALLOC_FREE(smb_fname_onefs
);
444 int onefs_unlink(vfs_handle_struct
*handle
,
445 const struct smb_filename
*smb_fname
)
447 struct smb_filename
*smb_fname_onefs
= NULL
;
449 int dir_fd
, saved_errno
;
453 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
454 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
457 status
= onefs_stream_prep_smb_fname(talloc_tos(), smb_fname
,
459 if (!NT_STATUS_IS_OK(status
)) {
460 errno
= map_errno_from_nt_status(status
);
464 /* Default stream (the ::$DATA was just stripped off). */
465 if (!is_ntfs_stream_smb_fname(smb_fname_onefs
)) {
466 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_onefs
);
470 dir_fd
= get_stream_dir_fd(handle
->conn
, smb_fname_onefs
->base_name
,
477 ret
= enc_unlinkat(dir_fd
, smb_fname_onefs
->stream_name
, ENC_DEFAULT
,
484 TALLOC_FREE(smb_fname_onefs
);
488 int onefs_vtimes_streams(vfs_handle_struct
*handle
,
489 const struct smb_filename
*smb_fname
,
490 int flags
, struct timespec times
[3])
492 struct smb_filename
*smb_fname_onefs
= NULL
;
498 START_PROFILE(syscall_ntimes
);
500 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
501 ret
= vtimes(smb_fname
->base_name
, times
, flags
);
505 status
= onefs_stream_prep_smb_fname(talloc_tos(), smb_fname
,
507 if (!NT_STATUS_IS_OK(status
)) {
508 errno
= map_errno_from_nt_status(status
);
512 /* Default stream (the ::$DATA was just stripped off). */
513 if (!is_ntfs_stream_smb_fname(smb_fname_onefs
)) {
514 ret
= vtimes(smb_fname_onefs
->base_name
, times
, flags
);
518 dirfd
= get_stream_dir_fd(handle
->conn
, smb_fname
->base_name
, NULL
);
524 ret
= enc_vtimesat(dirfd
, smb_fname_onefs
->stream_name
, ENC_DEFAULT
,
532 END_PROFILE(syscall_ntimes
);
533 TALLOC_FREE(smb_fname_onefs
);
538 * Streaminfo enumeration functionality
540 struct streaminfo_state
{
542 vfs_handle_struct
*handle
;
543 unsigned int num_streams
;
544 struct stream_struct
*streams
;
548 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
549 struct stream_struct
**streams
,
550 const char *name
, SMB_OFF_T size
,
551 SMB_OFF_T alloc_size
)
553 struct stream_struct
*tmp
;
555 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
561 tmp
[*num_streams
].name
= talloc_asprintf(mem_ctx
, ":%s:%s", name
,
563 if (tmp
[*num_streams
].name
== NULL
) {
567 tmp
[*num_streams
].size
= size
;
568 tmp
[*num_streams
].alloc_size
= alloc_size
;
575 static NTSTATUS
walk_onefs_streams(connection_struct
*conn
, files_struct
*fsp
,
577 struct streaminfo_state
*state
,
578 SMB_STRUCT_STAT
*base_sbuf
)
580 NTSTATUS status
= NT_STATUS_OK
;
581 bool opened_base_fd
= false;
586 SMB_STRUCT_DIR
*dirp
= NULL
;
587 SMB_STRUCT_DIRENT
*dp
= NULL
;
588 files_struct fake_fs
;
589 struct fd_handle fake_fh
;
590 SMB_STRUCT_STAT stream_sbuf
;
592 ZERO_STRUCT(fake_fh
);
593 ZERO_STRUCT(fake_fs
);
595 /* If the base file is already open, use its fd. */
596 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
597 base_fd
= fsp
->fh
->fd
;
599 opened_base_fd
= true;
602 dir_fd
= get_stream_dir_fd(conn
, fname
, &base_fd
);
604 return map_nt_error_from_unix(errno
);
607 /* Open the ADS directory. */
608 if ((dirp
= fdopendir(dir_fd
)) == NULL
) {
609 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
610 fname
, errno
, strerror(errno
)));
611 status
= map_nt_error_from_unix(errno
);
615 /* Initialize the dir state struct and add it to the list.
616 * This is a layer violation, and really should be handled by a
617 * VFS_FDOPENDIR() call which would properly setup the dir state.
618 * But since this is all within the onefs.so module, we cheat for
619 * now and call directly into the readdirplus code.
620 * NOTE: This state MUST be freed by a proper VFS_CLOSEDIR() call. */
621 ret
= onefs_rdp_add_dir_state(conn
, dirp
);
623 DEBUG(0, ("Error adding dir_state to the list\n"));
624 status
= map_nt_error_from_unix(errno
);
629 fake_fs
.fh
= &fake_fh
;
630 status
= create_synthetic_smb_fname(talloc_tos(), fname
, NULL
, NULL
,
632 if (!NT_STATUS_IS_OK(status
)) {
636 /* Iterate over the streams in the ADS directory. */
637 while ((dp
= SMB_VFS_READDIR(conn
, dirp
, NULL
)) != NULL
) {
638 /* Skip the "." and ".." entries */
639 if ((strcmp(dp
->d_name
, ".") == 0) ||
640 (strcmp(dp
->d_name
, "..") == 0))
643 /* Open actual stream */
644 if ((stream_fd
= onefs_sys_create_file(conn
,
658 DEBUG(0, ("Error opening stream %s:%s. "
659 "errno=%d (%s)\n", fname
, dp
->d_name
, errno
,
664 /* Figure out the stat info. */
665 fake_fh
.fd
= stream_fd
;
666 ret
= SMB_VFS_FSTAT(&fake_fs
, &stream_sbuf
);
670 DEBUG(0, ("Error fstating stream %s:%s. "
671 "errno=%d (%s)\n", fname
, dp
->d_name
, errno
,
676 merge_stat(&stream_sbuf
, base_sbuf
);
678 if (!add_one_stream(state
->mem_ctx
,
679 &state
->num_streams
, &state
->streams
,
680 dp
->d_name
, stream_sbuf
.st_ex_size
,
681 SMB_VFS_GET_ALLOC_SIZE(conn
, NULL
,
683 state
->status
= NT_STATUS_NO_MEMORY
;
689 /* Cleanup everything that was opened. */
691 SMB_VFS_CLOSEDIR(conn
, dirp
);
696 if (opened_base_fd
) {
697 SMB_ASSERT(base_fd
>= 0);
701 TALLOC_FREE(fake_fs
.fsp_name
);
705 NTSTATUS
onefs_streaminfo(vfs_handle_struct
*handle
,
706 struct files_struct
*fsp
,
709 unsigned int *num_streams
,
710 struct stream_struct
**streams
)
712 SMB_STRUCT_STAT sbuf
;
715 struct streaminfo_state state
;
717 /* Get a valid stat. */
718 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
719 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
721 struct smb_filename
*smb_fname
= NULL
;
723 status
= create_synthetic_smb_fname(talloc_tos(), fname
, NULL
,
725 if (!NT_STATUS_IS_OK(status
)) {
728 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
730 sbuf
= smb_fname
->st
;
732 TALLOC_FREE(smb_fname
);
736 return map_nt_error_from_unix(errno
);
739 state
.streams
= NULL
;
740 state
.num_streams
= 0;
742 if (lp_parm_bool(SNUM(handle
->conn
), PARM_ONEFS_TYPE
,
743 PARM_IGNORE_STREAMS
, PARM_IGNORE_STREAMS_DEFAULT
)) {
747 /* Add the default stream. */
748 if (S_ISREG(sbuf
.st_ex_mode
)) {
749 if (!add_one_stream(mem_ctx
,
750 &state
.num_streams
, &state
.streams
,
752 SMB_VFS_GET_ALLOC_SIZE(handle
->conn
, fsp
,
754 return NT_STATUS_NO_MEMORY
;
758 state
.mem_ctx
= mem_ctx
;
759 state
.handle
= handle
;
760 state
.status
= NT_STATUS_OK
;
762 /* If there are more streams, add them too. */
763 if (sbuf
.st_ex_flags
& UF_HASADS
) {
765 status
= walk_onefs_streams(handle
->conn
, fsp
, fname
,
768 if (!NT_STATUS_IS_OK(status
)) {
769 TALLOC_FREE(state
.streams
);
773 if (!NT_STATUS_IS_OK(state
.status
)) {
774 TALLOC_FREE(state
.streams
);
779 *num_streams
= state
.num_streams
;
780 *streams
= state
.streams
;