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 int onefs_is_stream(const char *path
, char **pbase
, char **pstream
,
60 (*is_stream
) = is_ntfs_stream_name(path
);
66 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path
,
68 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
76 int onefs_close(vfs_handle_struct
*handle
, struct files_struct
*fsp
)
81 ret
= SMB_VFS_NEXT_CLOSE(handle
, fsp
->base_fsp
);
83 ret2
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
85 return ret
? ret
: ret2
;
89 * Get the ADS directory fd for a file.
91 static int get_stream_dir_fd(connection_struct
*conn
, const char *base
,
98 /* If a valid base_fdp was given, use it. */
99 if (base_fdp
&& *base_fdp
>= 0) {
102 base_fd
= onefs_sys_create_file(conn
,
121 /* Open the ADS directory. */
122 dir_fd
= onefs_sys_create_file(conn
,
137 /* Close base_fd if it's not need or on error. */
138 if (!base_fdp
|| dir_fd
< 0) {
144 /* Set the out base_fdp if successful and it was requested. */
145 if (base_fdp
&& dir_fd
>= 0) {
152 int onefs_rename(vfs_handle_struct
*handle
, const char *oldname
,
155 TALLOC_CTX
*frame
= NULL
;
166 START_PROFILE(syscall_rename_at
);
168 frame
= talloc_stackframe();
170 ret
= onefs_is_stream(oldname
, &obase
, &osname
, &old_is_stream
);
172 END_PROFILE(syscall_rename_at
);
176 ret
= onefs_is_stream(newname
, &nbase
, &nsname
, &new_is_stream
);
178 END_PROFILE(syscall_rename_at
);
182 if (!old_is_stream
&& !new_is_stream
) {
183 ret
= SMB_VFS_NEXT_RENAME(handle
, oldname
, newname
);
184 END_PROFILE(syscall_rename_at
);
188 dir_fd
= get_stream_dir_fd(handle
->conn
, obase
, NULL
);
193 DEBUG(8,("onefs_rename called for %s : %s => %s : %s\n",
194 obase
, osname
, nbase
, nsname
));
196 /* Handle rename of stream to default stream specially. */
197 if (nsname
== NULL
) {
198 ret
= enc_renameat(dir_fd
, osname
, ENC_DEFAULT
, AT_FDCWD
,
201 ret
= enc_renameat(dir_fd
, osname
, ENC_DEFAULT
, dir_fd
, nsname
,
206 END_PROFILE(syscall_rename_at
);
218 * Merge a base file's sbuf into the a streams's sbuf.
220 static void merge_stat(SMB_STRUCT_STAT
*stream_sbuf
,
221 const SMB_STRUCT_STAT
*base_sbuf
)
223 int dos_flags
= (UF_DOS_NOINDEX
| UF_DOS_ARCHIVE
|
224 UF_DOS_HIDDEN
| UF_DOS_RO
| UF_DOS_SYSTEM
);
225 stream_sbuf
->st_ex_mtime
= base_sbuf
->st_ex_mtime
;
226 stream_sbuf
->st_ex_ctime
= base_sbuf
->st_ex_ctime
;
227 stream_sbuf
->st_ex_atime
= base_sbuf
->st_ex_atime
;
228 stream_sbuf
->st_ex_flags
&= ~dos_flags
;
229 stream_sbuf
->st_ex_flags
|= base_sbuf
->st_ex_flags
& dos_flags
;
232 /* fake timestamps */
233 static void onefs_adjust_stat_time(struct connection_struct
*conn
,
234 const char *fname
, SMB_STRUCT_STAT
*sbuf
)
236 struct onefs_vfs_share_config cfg
;
237 struct timeval tv_now
= {0, 0};
238 bool static_mtime
= False
;
239 bool static_atime
= False
;
241 if (!onefs_get_config(SNUM(conn
),
242 ONEFS_VFS_CONFIG_FAKETIMESTAMPS
, &cfg
)) {
246 if (IS_MTIME_STATIC_PATH(conn
, &cfg
, fname
)) {
247 sbuf
->st_ex_mtime
= sbuf
->st_ex_btime
;
250 if (IS_ATIME_STATIC_PATH(conn
, &cfg
, fname
)) {
251 sbuf
->st_ex_atime
= sbuf
->st_ex_btime
;
255 if (IS_CTIME_NOW_PATH(conn
, &cfg
, fname
)) {
256 if (cfg
.ctime_slop
< 0) {
257 sbuf
->st_ex_btime
.tv_sec
= INT_MAX
- 1;
259 GetTimeOfDay(&tv_now
);
260 sbuf
->st_ex_btime
.tv_sec
= tv_now
.tv_sec
+
265 if (!static_mtime
&& IS_MTIME_NOW_PATH(conn
,&cfg
,fname
)) {
266 if (cfg
.mtime_slop
< 0) {
267 sbuf
->st_ex_mtime
.tv_sec
= INT_MAX
- 1;
269 if (tv_now
.tv_sec
== 0)
270 GetTimeOfDay(&tv_now
);
271 sbuf
->st_ex_mtime
.tv_sec
= tv_now
.tv_sec
+
275 if (!static_atime
&& IS_ATIME_NOW_PATH(conn
,&cfg
,fname
)) {
276 if (cfg
.atime_slop
< 0) {
277 sbuf
->st_ex_atime
.tv_sec
= INT_MAX
- 1;
279 if (tv_now
.tv_sec
== 0)
280 GetTimeOfDay(&tv_now
);
281 sbuf
->st_ex_atime
.tv_sec
= tv_now
.tv_sec
+
287 static int stat_stream(struct connection_struct
*conn
, const char *base
,
288 const char *stream
, SMB_STRUCT_STAT
*sbuf
, int flags
)
290 SMB_STRUCT_STAT base_sbuf
;
291 int base_fd
= -1, dir_fd
, ret
, saved_errno
;
293 dir_fd
= get_stream_dir_fd(conn
, base
, &base_fd
);
298 /* Stat the stream. */
299 ret
= onefs_sys_fstat_at(dir_fd
, stream
, sbuf
, flags
);
301 /* Now stat the base file and merge the results. */
302 ret
= onefs_sys_fstat(base_fd
, &base_sbuf
);
304 merge_stat(sbuf
, &base_sbuf
);
315 int onefs_stat(vfs_handle_struct
*handle
, const char *path
,
316 SMB_STRUCT_STAT
*sbuf
)
323 ret
= onefs_is_stream(path
, &base
, &stream
, &is_stream
);
328 ret
= onefs_sys_stat(path
, sbuf
);
329 } else if (!stream
) {
330 /* If it's the ::$DATA stream just stat the base file name. */
331 ret
= onefs_sys_stat(base
, sbuf
);
333 ret
= stat_stream(handle
->conn
, base
, stream
, sbuf
, 0);
336 onefs_adjust_stat_time(handle
->conn
, path
, sbuf
);
340 int onefs_fstat(vfs_handle_struct
*handle
, struct files_struct
*fsp
,
341 SMB_STRUCT_STAT
*sbuf
)
343 SMB_STRUCT_STAT base_sbuf
;
346 /* Stat the stream, by calling next_fstat on the stream's fd. */
347 ret
= onefs_sys_fstat(fsp
->fh
->fd
, sbuf
);
352 /* Stat the base file and merge the results. */
353 if (fsp
!= NULL
&& fsp
->base_fsp
!= NULL
) {
354 ret
= onefs_sys_fstat(fsp
->base_fsp
->fh
->fd
, &base_sbuf
);
356 merge_stat(sbuf
, &base_sbuf
);
360 onefs_adjust_stat_time(handle
->conn
, fsp
->fsp_name
, sbuf
);
364 int onefs_lstat(vfs_handle_struct
*handle
, const char *path
,
365 SMB_STRUCT_STAT
*sbuf
)
372 ret
= onefs_is_stream(path
, &base
, &stream
, &is_stream
);
377 ret
= onefs_sys_lstat(path
, sbuf
);
378 } else if (!stream
) {
379 /* If it's the ::$DATA stream just stat the base file name. */
380 ret
= onefs_sys_lstat(base
, sbuf
);
382 ret
= stat_stream(handle
->conn
, base
, stream
, sbuf
,
383 AT_SYMLINK_NOFOLLOW
);
386 onefs_adjust_stat_time(handle
->conn
, path
, sbuf
);
390 int onefs_unlink(vfs_handle_struct
*handle
, const char *path
)
396 int dir_fd
, saved_errno
;
398 ret
= onefs_is_stream(path
, &base
, &stream
, &is_stream
);
404 return SMB_VFS_NEXT_UNLINK(handle
, path
);
407 /* If it's the ::$DATA stream just unlink the base file name. */
409 return SMB_VFS_NEXT_UNLINK(handle
, base
);
412 dir_fd
= get_stream_dir_fd(handle
->conn
, base
, NULL
);
417 ret
= enc_unlinkat(dir_fd
, stream
, ENC_DEFAULT
, 0);
425 int onefs_vtimes_streams(vfs_handle_struct
*handle
, const char *fname
,
426 int flags
, struct timespec times
[3])
435 START_PROFILE(syscall_ntimes
);
437 ret
= onefs_is_stream(fname
, &base
, &stream
, &is_stream
);
442 ret
= vtimes(fname
, times
, flags
);
446 dirfd
= get_stream_dir_fd(handle
->conn
, base
, NULL
);
451 ret
= enc_vtimesat(dirfd
, stream
, ENC_DEFAULT
, times
, flags
);
453 END_PROFILE(syscall_ntimes
);
461 int onefs_chflags(vfs_handle_struct
*handle
, const char *path
,
467 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path
,
469 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
475 * Only set the attributes on the base file. ifs_createfile handles
476 * file creation attribute semantics.
478 return SMB_VFS_NEXT_CHFLAGS(handle
, base
, flags
);
482 * Streaminfo enumeration functionality
484 struct streaminfo_state
{
486 vfs_handle_struct
*handle
;
487 unsigned int num_streams
;
488 struct stream_struct
*streams
;
492 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
493 struct stream_struct
**streams
,
494 const char *name
, SMB_OFF_T size
,
495 SMB_OFF_T alloc_size
)
497 struct stream_struct
*tmp
;
499 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
505 tmp
[*num_streams
].name
= talloc_asprintf(mem_ctx
, ":%s:%s", name
,
507 if (tmp
[*num_streams
].name
== NULL
) {
511 tmp
[*num_streams
].size
= size
;
512 tmp
[*num_streams
].alloc_size
= alloc_size
;
519 static NTSTATUS
walk_onefs_streams(connection_struct
*conn
, files_struct
*fsp
,
521 struct streaminfo_state
*state
,
522 SMB_STRUCT_STAT
*base_sbuf
)
524 NTSTATUS status
= NT_STATUS_OK
;
525 bool opened_base_fd
= false;
530 SMB_STRUCT_DIR
*dirp
= NULL
;
531 SMB_STRUCT_DIRENT
*dp
= NULL
;
532 files_struct fake_fs
;
533 struct fd_handle fake_fh
;
534 SMB_STRUCT_STAT stream_sbuf
;
536 ZERO_STRUCT(fake_fh
);
537 ZERO_STRUCT(fake_fs
);
539 /* If the base file is already open, use its fd. */
540 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
541 base_fd
= fsp
->fh
->fd
;
543 opened_base_fd
= true;
546 dir_fd
= get_stream_dir_fd(conn
, fname
, &base_fd
);
548 return map_nt_error_from_unix(errno
);
551 /* Open the ADS directory. */
552 if ((dirp
= fdopendir(dir_fd
)) == NULL
) {
553 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
554 fname
, errno
, strerror(errno
)));
555 status
= map_nt_error_from_unix(errno
);
559 /* Initialize the dir state struct and add it to the list.
560 * This is a layer violation, and really should be handled by a
561 * VFS_FDOPENDIR() call which would properly setup the dir state.
562 * But since this is all within the onefs.so module, we cheat for
563 * now and call directly into the readdirplus code.
564 * NOTE: This state MUST be freed by a proper VFS_CLOSEDIR() call. */
565 ret
= onefs_rdp_add_dir_state(conn
, dirp
);
567 DEBUG(0, ("Error adding dir_state to the list\n"));
568 status
= map_nt_error_from_unix(errno
);
573 fake_fs
.fh
= &fake_fh
;
574 fake_fs
.fsp_name
= SMB_STRDUP(fname
);
576 /* Iterate over the streams in the ADS directory. */
577 while ((dp
= SMB_VFS_READDIR(conn
, dirp
, NULL
)) != NULL
) {
578 /* Skip the "." and ".." entries */
579 if ((strcmp(dp
->d_name
, ".") == 0) ||
580 (strcmp(dp
->d_name
, "..") == 0))
583 /* Open actual stream */
584 if ((stream_fd
= onefs_sys_create_file(conn
,
598 DEBUG(0, ("Error opening stream %s:%s. "
599 "errno=%d (%s)\n", fname
, dp
->d_name
, errno
,
604 /* Figure out the stat info. */
605 fake_fh
.fd
= stream_fd
;
606 ret
= SMB_VFS_FSTAT(&fake_fs
, &stream_sbuf
);
610 DEBUG(0, ("Error fstating stream %s:%s. "
611 "errno=%d (%s)\n", fname
, dp
->d_name
, errno
,
616 merge_stat(&stream_sbuf
, base_sbuf
);
618 if (!add_one_stream(state
->mem_ctx
,
619 &state
->num_streams
, &state
->streams
,
620 dp
->d_name
, stream_sbuf
.st_ex_size
,
621 SMB_VFS_GET_ALLOC_SIZE(conn
, NULL
,
623 state
->status
= NT_STATUS_NO_MEMORY
;
629 /* Cleanup everything that was opened. */
631 SMB_VFS_CLOSEDIR(conn
, dirp
);
636 if (opened_base_fd
) {
637 SMB_ASSERT(base_fd
>= 0);
641 SAFE_FREE(fake_fs
.fsp_name
);
645 NTSTATUS
onefs_streaminfo(vfs_handle_struct
*handle
,
646 struct files_struct
*fsp
,
649 unsigned int *num_streams
,
650 struct stream_struct
**streams
)
652 SMB_STRUCT_STAT sbuf
;
655 struct streaminfo_state state
;
657 /* Get a valid stat. */
658 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
659 if (is_ntfs_stream_name(fsp
->fsp_name
)) {
660 return NT_STATUS_INVALID_PARAMETER
;
662 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
664 if (is_ntfs_stream_name(fname
)) {
665 return NT_STATUS_INVALID_PARAMETER
;
667 ret
= SMB_VFS_STAT(handle
->conn
, fname
, &sbuf
);
671 return map_nt_error_from_unix(errno
);
674 state
.streams
= NULL
;
675 state
.num_streams
= 0;
677 if (lp_parm_bool(SNUM(handle
->conn
), PARM_ONEFS_TYPE
,
678 PARM_IGNORE_STREAMS
, PARM_IGNORE_STREAMS_DEFAULT
)) {
682 /* Add the default stream. */
683 if (S_ISREG(sbuf
.st_ex_mode
)) {
684 if (!add_one_stream(mem_ctx
,
685 &state
.num_streams
, &state
.streams
,
687 SMB_VFS_GET_ALLOC_SIZE(handle
->conn
, fsp
,
689 return NT_STATUS_NO_MEMORY
;
693 state
.mem_ctx
= mem_ctx
;
694 state
.handle
= handle
;
695 state
.status
= NT_STATUS_OK
;
697 /* If there are more streams, add them too. */
698 if (sbuf
.st_ex_flags
& UF_HASADS
) {
700 status
= walk_onefs_streams(handle
->conn
, fsp
, fname
,
703 if (!NT_STATUS_IS_OK(status
)) {
704 TALLOC_FREE(state
.streams
);
708 if (!NT_STATUS_IS_OK(state
.status
)) {
709 TALLOC_FREE(state
.streams
);
714 *num_streams
= state
.num_streams
;
715 *streams
= state
.streams
;