2 Unix SMB/CIFS implementation.
3 Wrap disk only vfs functions to sidestep dodgy compilers.
4 Copyright (C) Tim Potter 1998
5 Copyright (C) Jeremy Allison 2007
6 Copyright (C) Brian Chrisman 2011 <bchrisman@gmail.com>
7 Copyright (C) Richard Sharpe 2011 <realrichardsharpe@gmail.com>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * This VFS only works with the libceph.so user-space client. It is not needed
25 * if you are using the kernel client or the FUSE client.
27 * Add the following smb.conf parameter to each share that will be hosted on
30 * vfs objects = ceph [any others you need go here]
34 #include "smbd/smbd.h"
35 #include "system/filesys.h"
37 #include <sys/statvfs.h>
38 #include "cephfs/libcephfs.h"
39 #include "smbprofile.h"
40 #include "modules/posixacl_xattr.h"
41 #include "lib/util/tevent_unix.h"
44 #define DBGC_CLASS DBGC_VFS
46 #ifndef LIBCEPHFS_VERSION
47 #define LIBCEPHFS_VERSION(maj, min, extra) ((maj << 16) + (min << 8) + extra)
48 #define LIBCEPHFS_VERSION_CODE LIBCEPHFS_VERSION(0, 0, 0)
52 * Use %llu whenever we have a 64bit unsigned int, and cast to (long long unsigned)
54 #define llu(_var) ((long long unsigned)_var)
57 * Note, libceph's return code model is to return -errno! So we have to convert
58 * to what Samba expects, with is set errno to -return and return -1
60 #define WRAP_RETURN(_res) \
69 * We mount only one file system and then all shares are assumed to be in that.
70 * FIXME: If we want to support more than one FS, then we have to deal with
73 * So, cmount tells us if we have been this way before and whether
74 * we need to mount ceph and cmount_cnt tells us how many times we have
77 static struct ceph_mount_info * cmount = NULL;
78 static uint32_t cmount_cnt = 0;
80 /* Check for NULL pointer parameters in cephwrap_* functions */
82 /* We don't want to have NULL function pointers lying around. Someone
83 is sure to try and execute them. These stubs are used to prevent
86 static int cephwrap_connect(struct vfs_handle_struct *handle, const char *service, const char *user)
90 int snum = SNUM(handle->conn);
91 const char *conf_file;
95 handle->data = cmount; /* We have been here before */
100 /* if config_file and/or user_id are NULL, ceph will use defaults */
101 conf_file = lp_parm_const_string(snum, "ceph", "config_file", NULL);
102 user_id = lp_parm_const_string(snum, "ceph", "user_id", NULL);
104 DBG_DEBUG("[CEPH] calling: ceph_create\n");
105 ret = ceph_create(&cmount, user_id);
110 DBG_DEBUG("[CEPH] calling: ceph_conf_read_file with %s\n",
111 (conf_file == NULL ? "default path" : conf_file));
112 ret = ceph_conf_read_file(cmount, conf_file);
117 DBG_DEBUG("[CEPH] calling: ceph_conf_get\n");
118 ret = ceph_conf_get(cmount, "log file", buf, sizeof(buf));
123 /* libcephfs disables POSIX ACL support by default, enable it... */
124 ret = ceph_conf_set(cmount, "client_acl_type", "posix_acl");
128 /* tell libcephfs to perform local permission checks */
129 ret = ceph_conf_set(cmount, "fuse_default_permissions", "false");
134 DBG_DEBUG("[CEPH] calling: ceph_mount\n");
135 ret = ceph_mount(cmount, NULL);
141 * encode mount context/state into our vfs/connection holding structure
142 * cmount is a ceph_mount_t*
144 handle->data = cmount;
148 * Unless we have an async implementation of getxattrat turn this off.
150 lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
155 ceph_release(cmount);
159 * Handle the error correctly. Ceph returns -errno.
161 DBG_DEBUG("[CEPH] Error return: %s\n", strerror(-ret));
165 static void cephwrap_disconnect(struct vfs_handle_struct *handle)
170 DBG_ERR("[CEPH] Error, ceph not mounted\n");
174 /* Should we unmount/shutdown? Only if the last disconnect? */
176 DBG_DEBUG("[CEPH] Not shuting down CEPH because still more connections\n");
180 ret = ceph_unmount(cmount);
182 DBG_ERR("[CEPH] failed to unmount: %s\n", strerror(-ret));
185 ret = ceph_release(cmount);
187 DBG_ERR("[CEPH] failed to release: %s\n", strerror(-ret));
190 cmount = NULL; /* Make it safe */
193 /* Disk operations */
195 static uint64_t cephwrap_disk_free(struct vfs_handle_struct *handle,
196 const struct smb_filename *smb_fname,
201 struct statvfs statvfs_buf;
204 if (!(ret = ceph_statfs(handle->data, smb_fname->base_name,
207 * Provide all the correct values.
209 *bsize = statvfs_buf.f_bsize;
210 *dfree = statvfs_buf.f_bavail;
211 *dsize = statvfs_buf.f_blocks;
212 DBG_DEBUG("[CEPH] bsize: %llu, dfree: %llu, dsize: %llu\n",
213 llu(*bsize), llu(*dfree), llu(*dsize));
216 DBG_DEBUG("[CEPH] ceph_statfs returned %d\n", ret);
221 static int cephwrap_get_quota(struct vfs_handle_struct *handle,
222 const struct smb_filename *smb_fname,
223 enum SMB_QUOTA_TYPE qtype,
227 /* libceph: Ceph does not implement this */
229 /* was ifdef HAVE_SYS_QUOTAS */
232 ret = ceph_get_quota(handle->conn->connectpath, qtype, id, qt);
246 static int cephwrap_set_quota(struct vfs_handle_struct *handle, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
248 /* libceph: Ceph does not implement this */
250 /* was ifdef HAVE_SYS_QUOTAS */
253 ret = ceph_set_quota(handle->conn->connectpath, qtype, id, qt);
261 WRAP_RETURN(-ENOSYS);
265 static int cephwrap_statvfs(struct vfs_handle_struct *handle,
266 const struct smb_filename *smb_fname,
267 vfs_statvfs_struct *statbuf)
269 struct statvfs statvfs_buf;
272 ret = ceph_statfs(handle->data, smb_fname->base_name, &statvfs_buf);
277 statbuf->OptimalTransferSize = statvfs_buf.f_frsize;
278 statbuf->BlockSize = statvfs_buf.f_bsize;
279 statbuf->TotalBlocks = statvfs_buf.f_blocks;
280 statbuf->BlocksAvail = statvfs_buf.f_bfree;
281 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
282 statbuf->TotalFileNodes = statvfs_buf.f_files;
283 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
284 statbuf->FsIdentifier = statvfs_buf.f_fsid;
285 DBG_DEBUG("[CEPH] f_bsize: %ld, f_blocks: %ld, f_bfree: %ld, f_bavail: %ld\n",
286 (long int)statvfs_buf.f_bsize, (long int)statvfs_buf.f_blocks,
287 (long int)statvfs_buf.f_bfree, (long int)statvfs_buf.f_bavail);
292 static uint32_t cephwrap_fs_capabilities(struct vfs_handle_struct *handle,
293 enum timestamp_set_resolution *p_ts_res)
295 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
297 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
302 /* Directory operations */
304 static DIR *cephwrap_fdopendir(struct vfs_handle_struct *handle,
305 struct files_struct *fsp,
310 struct ceph_dir_result *result;
311 DBG_DEBUG("[CEPH] fdopendir(%p, %p)\n", handle, fsp);
313 ret = ceph_opendir(handle->data, fsp->fsp_name->base_name, &result);
316 errno = -ret; /* We return result which is NULL in this case */
319 DBG_DEBUG("[CEPH] fdopendir(...) = %d\n", ret);
320 return (DIR *) result;
323 static struct dirent *cephwrap_readdir(struct vfs_handle_struct *handle,
324 struct files_struct *dirfsp,
326 SMB_STRUCT_STAT *sbuf)
328 struct dirent *result;
330 DBG_DEBUG("[CEPH] readdir(%p, %p)\n", handle, dirp);
331 result = ceph_readdir(handle->data, (struct ceph_dir_result *) dirp);
332 DBG_DEBUG("[CEPH] readdir(...) = %p\n", result);
334 /* Default Posix readdir() does not give us stat info.
335 * Set to invalid to indicate we didn't return this info. */
337 SET_STAT_INVALID(*sbuf);
341 static void cephwrap_seekdir(struct vfs_handle_struct *handle, DIR *dirp, long offset)
343 DBG_DEBUG("[CEPH] seekdir(%p, %p, %ld)\n", handle, dirp, offset);
344 ceph_seekdir(handle->data, (struct ceph_dir_result *) dirp, offset);
347 static long cephwrap_telldir(struct vfs_handle_struct *handle, DIR *dirp)
350 DBG_DEBUG("[CEPH] telldir(%p, %p)\n", handle, dirp);
351 ret = ceph_telldir(handle->data, (struct ceph_dir_result *) dirp);
352 DBG_DEBUG("[CEPH] telldir(...) = %ld\n", ret);
356 static void cephwrap_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
358 DBG_DEBUG("[CEPH] rewinddir(%p, %p)\n", handle, dirp);
359 ceph_rewinddir(handle->data, (struct ceph_dir_result *) dirp);
362 static int cephwrap_mkdirat(struct vfs_handle_struct *handle,
363 files_struct *dirfsp,
364 const struct smb_filename *smb_fname,
367 struct smb_filename *full_fname = NULL;
370 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
373 if (full_fname == NULL) {
377 DBG_DEBUG("[CEPH] mkdir(%p, %s)\n",
378 handle, smb_fname_str_dbg(full_fname));
380 result = ceph_mkdir(handle->data, full_fname->base_name, mode);
382 TALLOC_FREE(full_fname);
384 return WRAP_RETURN(result);
387 static int cephwrap_closedir(struct vfs_handle_struct *handle, DIR *dirp)
391 DBG_DEBUG("[CEPH] closedir(%p, %p)\n", handle, dirp);
392 result = ceph_closedir(handle->data, (struct ceph_dir_result *) dirp);
393 DBG_DEBUG("[CEPH] closedir(...) = %d\n", result);
397 /* File operations */
399 static int cephwrap_openat(struct vfs_handle_struct *handle,
400 const struct files_struct *dirfsp,
401 const struct smb_filename *smb_fname,
406 struct smb_filename *name = NULL;
407 bool have_opath = false;
408 bool became_root = false;
409 int result = -ENOENT;
412 * ceph doesn't have openat().
414 if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
415 name = full_path_from_dirfsp_atname(talloc_tos(),
424 DBG_DEBUG("[CEPH] openat(%p, %s, %p, %d, %d)\n", handle,
425 smb_fname_str_dbg(smb_fname), fsp, flags, mode);
427 if (smb_fname->stream_name) {
433 if (fsp->fsp_flags.is_pathref) {
438 if (fsp->fsp_flags.is_pathref && !have_opath) {
443 result = ceph_open(handle->data, smb_fname->base_name, flags, mode);
451 fsp->fsp_flags.have_proc_fds = false;
452 DBG_DEBUG("[CEPH] open(...) = %d\n", result);
456 static int cephwrap_close(struct vfs_handle_struct *handle, files_struct *fsp)
460 DBG_DEBUG("[CEPH] close(%p, %p)\n", handle, fsp);
461 result = ceph_close(handle->data, fsp_get_io_fd(fsp));
462 DBG_DEBUG("[CEPH] close(...) = %d\n", result);
467 static ssize_t cephwrap_pread(struct vfs_handle_struct *handle, files_struct *fsp, void *data,
468 size_t n, off_t offset)
472 DBG_DEBUG("[CEPH] pread(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
474 result = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
475 DBG_DEBUG("[CEPH] pread(...) = %llu\n", llu(result));
479 struct cephwrap_pread_state {
481 struct vfs_aio_state vfs_aio_state;
485 * Fake up an async ceph read by calling the synchronous API.
487 static struct tevent_req *cephwrap_pread_send(struct vfs_handle_struct *handle,
489 struct tevent_context *ev,
490 struct files_struct *fsp,
492 size_t n, off_t offset)
494 struct tevent_req *req = NULL;
495 struct cephwrap_pread_state *state = NULL;
498 DBG_DEBUG("[CEPH] %s\n", __func__);
499 req = tevent_req_create(mem_ctx, &state, struct cephwrap_pread_state);
504 ret = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
506 /* ceph returns -errno on error. */
507 tevent_req_error(req, -ret);
508 return tevent_req_post(req, ev);
511 state->bytes_read = ret;
512 tevent_req_done(req);
513 /* Return and schedule the completion of the call. */
514 return tevent_req_post(req, ev);
517 static ssize_t cephwrap_pread_recv(struct tevent_req *req,
518 struct vfs_aio_state *vfs_aio_state)
520 struct cephwrap_pread_state *state =
521 tevent_req_data(req, struct cephwrap_pread_state);
523 DBG_DEBUG("[CEPH] %s\n", __func__);
524 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
527 *vfs_aio_state = state->vfs_aio_state;
528 return state->bytes_read;
531 static ssize_t cephwrap_pwrite(struct vfs_handle_struct *handle, files_struct *fsp, const void *data,
532 size_t n, off_t offset)
536 DBG_DEBUG("[CEPH] pwrite(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
537 result = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
538 DBG_DEBUG("[CEPH] pwrite(...) = %llu\n", llu(result));
542 struct cephwrap_pwrite_state {
543 ssize_t bytes_written;
544 struct vfs_aio_state vfs_aio_state;
548 * Fake up an async ceph write by calling the synchronous API.
550 static struct tevent_req *cephwrap_pwrite_send(struct vfs_handle_struct *handle,
552 struct tevent_context *ev,
553 struct files_struct *fsp,
555 size_t n, off_t offset)
557 struct tevent_req *req = NULL;
558 struct cephwrap_pwrite_state *state = NULL;
561 DBG_DEBUG("[CEPH] %s\n", __func__);
562 req = tevent_req_create(mem_ctx, &state, struct cephwrap_pwrite_state);
567 ret = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
569 /* ceph returns -errno on error. */
570 tevent_req_error(req, -ret);
571 return tevent_req_post(req, ev);
574 state->bytes_written = ret;
575 tevent_req_done(req);
576 /* Return and schedule the completion of the call. */
577 return tevent_req_post(req, ev);
580 static ssize_t cephwrap_pwrite_recv(struct tevent_req *req,
581 struct vfs_aio_state *vfs_aio_state)
583 struct cephwrap_pwrite_state *state =
584 tevent_req_data(req, struct cephwrap_pwrite_state);
586 DBG_DEBUG("[CEPH] %s\n", __func__);
587 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
590 *vfs_aio_state = state->vfs_aio_state;
591 return state->bytes_written;
594 static off_t cephwrap_lseek(struct vfs_handle_struct *handle, files_struct *fsp, off_t offset, int whence)
598 DBG_DEBUG("[CEPH] cephwrap_lseek\n");
599 result = ceph_lseek(handle->data, fsp_get_io_fd(fsp), offset, whence);
603 static ssize_t cephwrap_sendfile(struct vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
604 off_t offset, size_t n)
607 * We cannot support sendfile because libceph is in user space.
609 DBG_DEBUG("[CEPH] cephwrap_sendfile\n");
614 static ssize_t cephwrap_recvfile(struct vfs_handle_struct *handle,
621 * We cannot support recvfile because libceph is in user space.
623 DBG_DEBUG("[CEPH] cephwrap_recvfile\n");
628 static int cephwrap_renameat(struct vfs_handle_struct *handle,
629 files_struct *srcfsp,
630 const struct smb_filename *smb_fname_src,
631 files_struct *dstfsp,
632 const struct smb_filename *smb_fname_dst)
634 struct smb_filename *full_fname_src = NULL;
635 struct smb_filename *full_fname_dst = NULL;
638 DBG_DEBUG("[CEPH] cephwrap_renameat\n");
639 if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
644 full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
647 if (full_fname_src == NULL) {
651 full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
654 if (full_fname_dst == NULL) {
655 TALLOC_FREE(full_fname_src);
660 result = ceph_rename(handle->data,
661 full_fname_src->base_name,
662 full_fname_dst->base_name);
664 TALLOC_FREE(full_fname_src);
665 TALLOC_FREE(full_fname_dst);
671 * Fake up an async ceph fsync by calling the synchronous API.
674 static struct tevent_req *cephwrap_fsync_send(struct vfs_handle_struct *handle,
676 struct tevent_context *ev,
679 struct tevent_req *req = NULL;
680 struct vfs_aio_state *state = NULL;
683 DBG_DEBUG("[CEPH] cephwrap_fsync_send\n");
685 req = tevent_req_create(mem_ctx, &state, struct vfs_aio_state);
690 /* Make sync call. */
691 ret = ceph_fsync(handle->data, fsp_get_io_fd(fsp), false);
694 /* ceph_fsync returns -errno on error. */
695 tevent_req_error(req, -ret);
696 return tevent_req_post(req, ev);
699 /* Mark it as done. */
700 tevent_req_done(req);
701 /* Return and schedule the completion of the call. */
702 return tevent_req_post(req, ev);
705 static int cephwrap_fsync_recv(struct tevent_req *req,
706 struct vfs_aio_state *vfs_aio_state)
708 struct vfs_aio_state *state =
709 tevent_req_data(req, struct vfs_aio_state);
711 DBG_DEBUG("[CEPH] cephwrap_fsync_recv\n");
713 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
716 *vfs_aio_state = *state;
720 #define SAMBA_STATX_ATTR_MASK (CEPH_STATX_BASIC_STATS|CEPH_STATX_BTIME)
722 static void init_stat_ex_from_ceph_statx(struct stat_ex *dst, const struct ceph_statx *stx)
724 DBG_DEBUG("[CEPH]\tstx = {dev = %llx, ino = %llu, mode = 0x%x, "
725 "nlink = %llu, uid = %d, gid = %d, rdev = %llx, size = %llu, "
726 "blksize = %llu, blocks = %llu, atime = %llu, mtime = %llu, "
727 "ctime = %llu, btime = %llu}\n",
728 llu(stx->stx_dev), llu(stx->stx_ino), stx->stx_mode,
729 llu(stx->stx_nlink), stx->stx_uid, stx->stx_gid,
730 llu(stx->stx_rdev), llu(stx->stx_size), llu(stx->stx_blksize),
731 llu(stx->stx_blocks), llu(stx->stx_atime.tv_sec),
732 llu(stx->stx_mtime.tv_sec), llu(stx->stx_ctime.tv_sec),
733 llu(stx->stx_btime.tv_sec));
735 if ((stx->stx_mask & SAMBA_STATX_ATTR_MASK) != SAMBA_STATX_ATTR_MASK) {
736 DBG_WARNING("%s: stx->stx_mask is incorrect (wanted %x, got %x)",
737 __func__, SAMBA_STATX_ATTR_MASK, stx->stx_mask);
740 dst->st_ex_dev = stx->stx_dev;
741 dst->st_ex_rdev = stx->stx_rdev;
742 dst->st_ex_ino = stx->stx_ino;
743 dst->st_ex_mode = stx->stx_mode;
744 dst->st_ex_uid = stx->stx_uid;
745 dst->st_ex_gid = stx->stx_gid;
746 dst->st_ex_size = stx->stx_size;
747 dst->st_ex_nlink = stx->stx_nlink;
748 dst->st_ex_atime = stx->stx_atime;
749 dst->st_ex_btime = stx->stx_btime;
750 dst->st_ex_ctime = stx->stx_ctime;
751 dst->st_ex_mtime = stx->stx_mtime;
752 dst->st_ex_blksize = stx->stx_blksize;
753 dst->st_ex_blocks = stx->stx_blocks;
756 static int cephwrap_stat(struct vfs_handle_struct *handle,
757 struct smb_filename *smb_fname)
760 struct ceph_statx stx;
762 DBG_DEBUG("[CEPH] stat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
764 if (smb_fname->stream_name) {
769 result = ceph_statx(handle->data, smb_fname->base_name, &stx,
770 SAMBA_STATX_ATTR_MASK, 0);
771 DBG_DEBUG("[CEPH] statx(...) = %d\n", result);
776 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
777 DBG_DEBUG("[CEPH] mode = 0x%x\n", smb_fname->st.st_ex_mode);
781 static int cephwrap_fstat(struct vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
784 struct ceph_statx stx;
786 DBG_DEBUG("[CEPH] fstat(%p, %d)\n", handle, fsp_get_io_fd(fsp));
787 result = ceph_fstatx(handle->data, fsp_get_io_fd(fsp), &stx,
788 SAMBA_STATX_ATTR_MASK, 0);
789 DBG_DEBUG("[CEPH] fstat(...) = %d\n", result);
794 init_stat_ex_from_ceph_statx(sbuf, &stx);
795 DBG_DEBUG("[CEPH] mode = 0x%x\n", sbuf->st_ex_mode);
799 static int cephwrap_lstat(struct vfs_handle_struct *handle,
800 struct smb_filename *smb_fname)
803 struct ceph_statx stx;
805 DBG_DEBUG("[CEPH] lstat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
807 if (smb_fname->stream_name) {
812 result = ceph_statx(handle->data, smb_fname->base_name, &stx,
813 SAMBA_STATX_ATTR_MASK, AT_SYMLINK_NOFOLLOW);
814 DBG_DEBUG("[CEPH] lstat(...) = %d\n", result);
819 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
823 static int cephwrap_fntimes(struct vfs_handle_struct *handle,
825 struct smb_file_time *ft)
827 struct ceph_statx stx = { 0 };
831 if (!is_omit_timespec(&ft->atime)) {
832 stx.stx_atime = ft->atime;
833 mask |= CEPH_SETATTR_ATIME;
835 if (!is_omit_timespec(&ft->mtime)) {
836 stx.stx_mtime = ft->mtime;
837 mask |= CEPH_SETATTR_MTIME;
839 if (!is_omit_timespec(&ft->create_time)) {
840 stx.stx_btime = ft->create_time;
841 mask |= CEPH_SETATTR_BTIME;
848 if (!fsp->fsp_flags.is_pathref) {
850 * We can use an io_fd to set xattrs.
852 result = ceph_fsetattrx(handle->data,
858 * This is no longer a handle based call.
860 result = ceph_setattrx(handle->data,
861 fsp->fsp_name->base_name,
867 DBG_DEBUG("[CEPH] ntimes(%p, %s, {%ld, %ld, %ld, %ld}) = %d\n",
868 handle, fsp_str_dbg(fsp), ft->mtime.tv_sec, ft->atime.tv_sec,
869 ft->ctime.tv_sec, ft->create_time.tv_sec, result);
874 static int cephwrap_unlinkat(struct vfs_handle_struct *handle,
875 struct files_struct *dirfsp,
876 const struct smb_filename *smb_fname,
879 struct smb_filename *full_fname = NULL;
882 DBG_DEBUG("[CEPH] unlink(%p, %s)\n",
884 smb_fname_str_dbg(smb_fname));
886 if (smb_fname->stream_name) {
891 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
894 if (full_fname == NULL) {
898 if (flags & AT_REMOVEDIR) {
899 result = ceph_rmdir(handle->data, full_fname->base_name);
901 result = ceph_unlink(handle->data, full_fname->base_name);
903 TALLOC_FREE(full_fname);
904 DBG_DEBUG("[CEPH] unlink(...) = %d\n", result);
908 static int cephwrap_fchmod(struct vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
912 DBG_DEBUG("[CEPH] fchmod(%p, %p, %d)\n", handle, fsp, mode);
913 if (!fsp->fsp_flags.is_pathref) {
915 * We can use an io_fd to remove xattrs.
917 result = ceph_fchmod(handle->data, fsp_get_io_fd(fsp), mode);
920 * This is no longer a handle based call.
922 result = ceph_chmod(handle->data,
923 fsp->fsp_name->base_name,
926 DBG_DEBUG("[CEPH] fchmod(...) = %d\n", result);
930 static int cephwrap_fchown(struct vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
934 DBG_DEBUG("[CEPH] fchown(%p, %p, %d, %d)\n", handle, fsp, uid, gid);
935 result = ceph_fchown(handle->data, fsp_get_io_fd(fsp), uid, gid);
936 DBG_DEBUG("[CEPH] fchown(...) = %d\n", result);
940 static int cephwrap_lchown(struct vfs_handle_struct *handle,
941 const struct smb_filename *smb_fname,
946 DBG_DEBUG("[CEPH] lchown(%p, %s, %d, %d)\n", handle, smb_fname->base_name, uid, gid);
947 result = ceph_lchown(handle->data, smb_fname->base_name, uid, gid);
948 DBG_DEBUG("[CEPH] lchown(...) = %d\n", result);
952 static int cephwrap_chdir(struct vfs_handle_struct *handle,
953 const struct smb_filename *smb_fname)
956 DBG_DEBUG("[CEPH] chdir(%p, %s)\n", handle, smb_fname->base_name);
957 result = ceph_chdir(handle->data, smb_fname->base_name);
958 DBG_DEBUG("[CEPH] chdir(...) = %d\n", result);
962 static struct smb_filename *cephwrap_getwd(struct vfs_handle_struct *handle,
965 const char *cwd = ceph_getcwd(handle->data);
966 DBG_DEBUG("[CEPH] getwd(%p) = %s\n", handle, cwd);
967 return synthetic_smb_fname(ctx,
975 static int strict_allocate_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
977 off_t space_to_write;
980 SMB_STRUCT_STAT *pst;
982 status = vfs_stat_fsp(fsp);
983 if (!NT_STATUS_IS_OK(status)) {
986 pst = &fsp->fsp_name->st;
989 if (S_ISFIFO(pst->st_ex_mode))
993 if (pst->st_ex_size == len)
996 /* Shrink - just ftruncate. */
997 if (pst->st_ex_size > len) {
998 result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
1002 space_to_write = len - pst->st_ex_size;
1003 result = ceph_fallocate(handle->data, fsp_get_io_fd(fsp), 0, pst->st_ex_size,
1005 WRAP_RETURN(result);
1008 static int cephwrap_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
1012 DBG_DEBUG("[CEPH] ftruncate(%p, %p, %llu\n", handle, fsp, llu(len));
1014 if (lp_strict_allocate(SNUM(fsp->conn))) {
1015 return strict_allocate_ftruncate(handle, fsp, len);
1018 result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
1019 WRAP_RETURN(result);
1022 static int cephwrap_fallocate(struct vfs_handle_struct *handle,
1023 struct files_struct *fsp,
1030 DBG_DEBUG("[CEPH] fallocate(%p, %p, %u, %llu, %llu\n",
1031 handle, fsp, mode, llu(offset), llu(len));
1032 /* unsupported mode flags are rejected by libcephfs */
1033 result = ceph_fallocate(handle->data, fsp_get_io_fd(fsp), mode, offset, len);
1034 DBG_DEBUG("[CEPH] fallocate(...) = %d\n", result);
1035 WRAP_RETURN(result);
1038 static bool cephwrap_lock(struct vfs_handle_struct *handle, files_struct *fsp, int op, off_t offset, off_t count, int type)
1040 DBG_DEBUG("[CEPH] lock\n");
1044 static int cephwrap_filesystem_sharemode(struct vfs_handle_struct *handle,
1046 uint32_t share_access,
1047 uint32_t access_mask)
1049 DBG_ERR("[CEPH] filesystem sharemodes unsupported! Consider setting "
1050 "\"kernel share modes = no\"\n");
1056 static int cephwrap_fcntl(vfs_handle_struct *handle,
1057 files_struct *fsp, int cmd, va_list cmd_arg)
1060 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1061 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1063 if (cmd == F_GETFL) {
1065 } else if (cmd == F_SETFL) {
1066 va_list dup_cmd_arg;
1069 va_copy(dup_cmd_arg, cmd_arg);
1070 opt = va_arg(dup_cmd_arg, int);
1071 va_end(dup_cmd_arg);
1075 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1078 DBG_ERR("unexpected fcntl: %d\n", cmd);
1084 static bool cephwrap_getlock(struct vfs_handle_struct *handle, files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1086 DBG_DEBUG("[CEPH] getlock returning false and errno=0\n");
1093 * We cannot let this fall through to the default, because the file might only
1094 * be accessible from libceph (which is a user-space client) but the fd might
1095 * be for some file the kernel knows about.
1097 static int cephwrap_linux_setlease(struct vfs_handle_struct *handle, files_struct *fsp,
1102 DBG_DEBUG("[CEPH] linux_setlease\n");
1107 static int cephwrap_symlinkat(struct vfs_handle_struct *handle,
1108 const struct smb_filename *link_target,
1109 struct files_struct *dirfsp,
1110 const struct smb_filename *new_smb_fname)
1112 struct smb_filename *full_fname = NULL;
1115 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1118 if (full_fname == NULL) {
1122 DBG_DEBUG("[CEPH] symlink(%p, %s, %s)\n", handle,
1123 link_target->base_name,
1124 full_fname->base_name);
1126 result = ceph_symlink(handle->data,
1127 link_target->base_name,
1128 full_fname->base_name);
1129 TALLOC_FREE(full_fname);
1130 DBG_DEBUG("[CEPH] symlink(...) = %d\n", result);
1131 WRAP_RETURN(result);
1134 static int cephwrap_readlinkat(struct vfs_handle_struct *handle,
1135 const struct files_struct *dirfsp,
1136 const struct smb_filename *smb_fname,
1140 struct smb_filename *full_fname = NULL;
1143 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1146 if (full_fname == NULL) {
1150 DBG_DEBUG("[CEPH] readlink(%p, %s, %p, %llu)\n", handle,
1151 full_fname->base_name, buf, llu(bufsiz));
1153 result = ceph_readlink(handle->data, full_fname->base_name, buf, bufsiz);
1154 TALLOC_FREE(full_fname);
1155 DBG_DEBUG("[CEPH] readlink(...) = %d\n", result);
1156 WRAP_RETURN(result);
1159 static int cephwrap_linkat(struct vfs_handle_struct *handle,
1160 files_struct *srcfsp,
1161 const struct smb_filename *old_smb_fname,
1162 files_struct *dstfsp,
1163 const struct smb_filename *new_smb_fname,
1166 struct smb_filename *full_fname_old = NULL;
1167 struct smb_filename *full_fname_new = NULL;
1170 full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
1173 if (full_fname_old == NULL) {
1176 full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
1179 if (full_fname_new == NULL) {
1180 TALLOC_FREE(full_fname_old);
1184 DBG_DEBUG("[CEPH] link(%p, %s, %s)\n", handle,
1185 full_fname_old->base_name,
1186 full_fname_new->base_name);
1188 result = ceph_link(handle->data,
1189 full_fname_old->base_name,
1190 full_fname_new->base_name);
1191 DBG_DEBUG("[CEPH] link(...) = %d\n", result);
1192 TALLOC_FREE(full_fname_old);
1193 TALLOC_FREE(full_fname_new);
1194 WRAP_RETURN(result);
1197 static int cephwrap_mknodat(struct vfs_handle_struct *handle,
1198 files_struct *dirfsp,
1199 const struct smb_filename *smb_fname,
1203 struct smb_filename *full_fname = NULL;
1206 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1209 if (full_fname == NULL) {
1213 DBG_DEBUG("[CEPH] mknodat(%p, %s)\n", handle, full_fname->base_name);
1214 result = ceph_mknod(handle->data, full_fname->base_name, mode, dev);
1215 DBG_DEBUG("[CEPH] mknodat(...) = %d\n", result);
1217 TALLOC_FREE(full_fname);
1219 WRAP_RETURN(result);
1223 * This is a simple version of real-path ... a better version is needed to
1224 * ask libceph about symbolic links.
1226 static struct smb_filename *cephwrap_realpath(struct vfs_handle_struct *handle,
1228 const struct smb_filename *smb_fname)
1230 char *result = NULL;
1231 const char *path = smb_fname->base_name;
1232 size_t len = strlen(path);
1233 struct smb_filename *result_fname = NULL;
1236 if (len && (path[0] == '/')) {
1237 r = asprintf(&result, "%s", path);
1238 } else if ((len >= 2) && (path[0] == '.') && (path[1] == '/')) {
1240 r = asprintf(&result, "%s",
1241 handle->conn->cwd_fsp->fsp_name->base_name);
1243 r = asprintf(&result, "%s/%s",
1244 handle->conn->cwd_fsp->fsp_name->base_name, &path[2]);
1247 r = asprintf(&result, "%s/%s",
1248 handle->conn->cwd_fsp->fsp_name->base_name, path);
1255 DBG_DEBUG("[CEPH] realpath(%p, %s) = %s\n", handle, path, result);
1256 result_fname = synthetic_smb_fname(ctx,
1263 return result_fname;
1267 static int cephwrap_fchflags(struct vfs_handle_struct *handle,
1268 struct files_struct *fsp,
1275 static NTSTATUS cephwrap_get_real_filename(
1276 struct vfs_handle_struct *handle,
1277 const struct smb_filename *path,
1279 TALLOC_CTX *mem_ctx,
1283 * Don't fall back to get_real_filename so callers can differentiate
1284 * between a full directory scan and an actual case-insensitive stat.
1286 return NT_STATUS_NOT_SUPPORTED;
1289 static NTSTATUS cephwrap_get_real_filename_at(
1290 struct vfs_handle_struct *handle,
1291 struct files_struct *dirfsp,
1293 TALLOC_CTX *mem_ctx,
1297 * Don't fall back to get_real_filename so callers can differentiate
1298 * between a full directory scan and an actual case-insensitive stat.
1300 return NT_STATUS_NOT_SUPPORTED;
1303 static const char *cephwrap_connectpath(struct vfs_handle_struct *handle,
1304 const struct smb_filename *smb_fname)
1306 return handle->conn->connectpath;
1309 /****************************************************************
1310 Extended attribute operations.
1311 *****************************************************************/
1313 static ssize_t cephwrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
1316 DBG_DEBUG("[CEPH] fgetxattr(%p, %p, %s, %p, %llu)\n", handle, fsp, name, value, llu(size));
1317 ret = ceph_fgetxattr(handle->data, fsp_get_io_fd(fsp), name, value, size);
1318 DBG_DEBUG("[CEPH] fgetxattr(...) = %d\n", ret);
1322 return (ssize_t)ret;
1325 static ssize_t cephwrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
1328 DBG_DEBUG("[CEPH] flistxattr(%p, %p, %p, %llu)\n",
1329 handle, fsp, list, llu(size));
1330 if (!fsp->fsp_flags.is_pathref) {
1332 * We can use an io_fd to list xattrs.
1334 ret = ceph_flistxattr(handle->data,
1340 * This is no longer a handle based call.
1342 ret = ceph_listxattr(handle->data,
1343 fsp->fsp_name->base_name,
1347 DBG_DEBUG("[CEPH] flistxattr(...) = %d\n", ret);
1351 return (ssize_t)ret;
1354 static int cephwrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
1357 DBG_DEBUG("[CEPH] fremovexattr(%p, %p, %s)\n", handle, fsp, name);
1358 if (!fsp->fsp_flags.is_pathref) {
1360 * We can use an io_fd to remove xattrs.
1362 ret = ceph_fremovexattr(handle->data, fsp_get_io_fd(fsp), name);
1365 * This is no longer a handle based call.
1367 ret = ceph_removexattr(handle->data,
1368 fsp->fsp_name->base_name,
1371 DBG_DEBUG("[CEPH] fremovexattr(...) = %d\n", ret);
1375 static int cephwrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
1378 DBG_DEBUG("[CEPH] fsetxattr(%p, %p, %s, %p, %llu, %d)\n", handle, fsp, name, value, llu(size), flags);
1379 if (!fsp->fsp_flags.is_pathref) {
1381 * We can use an io_fd to set xattrs.
1383 ret = ceph_fsetxattr(handle->data,
1391 * This is no longer a handle based call.
1393 ret = ceph_setxattr(handle->data,
1394 fsp->fsp_name->base_name,
1400 DBG_DEBUG("[CEPH] fsetxattr(...) = %d\n", ret);
1404 static bool cephwrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
1408 * We do not support AIO yet.
1411 DBG_DEBUG("[CEPH] cephwrap_aio_force(%p, %p) = false (errno = ENOTSUP)\n", handle, fsp);
1416 static NTSTATUS cephwrap_create_dfs_pathat(struct vfs_handle_struct *handle,
1417 struct files_struct *dirfsp,
1418 const struct smb_filename *smb_fname,
1419 const struct referral *reflist,
1420 size_t referral_count)
1422 TALLOC_CTX *frame = talloc_stackframe();
1423 NTSTATUS status = NT_STATUS_NO_MEMORY;
1425 char *msdfs_link = NULL;
1426 struct smb_filename *full_fname = NULL;
1428 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1431 if (full_fname == NULL) {
1435 /* Form the msdfs_link contents */
1436 msdfs_link = msdfs_link_string(frame,
1439 if (msdfs_link == NULL) {
1443 ret = ceph_symlink(handle->data,
1445 full_fname->base_name);
1447 status = NT_STATUS_OK;
1449 status = map_nt_error_from_unix(-ret);
1454 DBG_DEBUG("[CEPH] create_dfs_pathat(%s) = %s\n",
1455 full_fname != NULL ? full_fname->base_name : "",
1463 * Read and return the contents of a DFS redirect given a
1464 * pathname. A caller can pass in NULL for ppreflist and
1465 * preferral_count but still determine if this was a
1466 * DFS redirect point by getting NT_STATUS_OK back
1467 * without incurring the overhead of reading and parsing
1468 * the referral contents.
1471 static NTSTATUS cephwrap_read_dfs_pathat(struct vfs_handle_struct *handle,
1472 TALLOC_CTX *mem_ctx,
1473 struct files_struct *dirfsp,
1474 struct smb_filename *smb_fname,
1475 struct referral **ppreflist,
1476 size_t *preferral_count)
1478 NTSTATUS status = NT_STATUS_NO_MEMORY;
1480 char *link_target = NULL;
1483 #if defined(HAVE_BROKEN_READLINK)
1484 char link_target_buf[PATH_MAX];
1486 char link_target_buf[7];
1488 struct ceph_statx stx;
1489 struct smb_filename *full_fname = NULL;
1492 if (is_named_stream(smb_fname)) {
1493 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1497 if (ppreflist == NULL && preferral_count == NULL) {
1499 * We're only checking if this is a DFS
1500 * redirect. We don't need to return data.
1502 bufsize = sizeof(link_target_buf);
1503 link_target = link_target_buf;
1506 link_target = talloc_array(mem_ctx, char, bufsize);
1512 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1515 if (full_fname == NULL) {
1516 status = NT_STATUS_NO_MEMORY;
1520 ret = ceph_statx(handle->data,
1521 full_fname->base_name,
1523 SAMBA_STATX_ATTR_MASK,
1524 AT_SYMLINK_NOFOLLOW);
1526 status = map_nt_error_from_unix(-ret);
1530 referral_len = ceph_readlink(handle->data,
1531 full_fname->base_name,
1534 if (referral_len < 0) {
1535 /* ceph errors are -errno. */
1536 if (-referral_len == EINVAL) {
1537 DBG_INFO("%s is not a link.\n",
1538 full_fname->base_name);
1539 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1541 status = map_nt_error_from_unix(-referral_len);
1542 DBG_ERR("Error reading "
1543 "msdfs link %s: %s\n",
1544 full_fname->base_name,
1549 link_target[referral_len] = '\0';
1551 DBG_INFO("%s -> %s\n",
1552 full_fname->base_name,
1555 if (!strnequal(link_target, "msdfs:", 6)) {
1556 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1560 if (ppreflist == NULL && preferral_count == NULL) {
1561 /* Early return for checking if this is a DFS link. */
1562 TALLOC_FREE(full_fname);
1563 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1564 return NT_STATUS_OK;
1567 ok = parse_msdfs_symlink(mem_ctx,
1568 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
1574 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1575 status = NT_STATUS_OK;
1577 status = NT_STATUS_NO_MEMORY;
1582 if (link_target != link_target_buf) {
1583 TALLOC_FREE(link_target);
1585 TALLOC_FREE(full_fname);
1589 static struct vfs_fn_pointers ceph_fns = {
1590 /* Disk operations */
1592 .connect_fn = cephwrap_connect,
1593 .disconnect_fn = cephwrap_disconnect,
1594 .disk_free_fn = cephwrap_disk_free,
1595 .get_quota_fn = cephwrap_get_quota,
1596 .set_quota_fn = cephwrap_set_quota,
1597 .statvfs_fn = cephwrap_statvfs,
1598 .fs_capabilities_fn = cephwrap_fs_capabilities,
1600 /* Directory operations */
1602 .fdopendir_fn = cephwrap_fdopendir,
1603 .readdir_fn = cephwrap_readdir,
1604 .seekdir_fn = cephwrap_seekdir,
1605 .telldir_fn = cephwrap_telldir,
1606 .rewind_dir_fn = cephwrap_rewinddir,
1607 .mkdirat_fn = cephwrap_mkdirat,
1608 .closedir_fn = cephwrap_closedir,
1610 /* File operations */
1612 .create_dfs_pathat_fn = cephwrap_create_dfs_pathat,
1613 .read_dfs_pathat_fn = cephwrap_read_dfs_pathat,
1614 .openat_fn = cephwrap_openat,
1615 .close_fn = cephwrap_close,
1616 .pread_fn = cephwrap_pread,
1617 .pread_send_fn = cephwrap_pread_send,
1618 .pread_recv_fn = cephwrap_pread_recv,
1619 .pwrite_fn = cephwrap_pwrite,
1620 .pwrite_send_fn = cephwrap_pwrite_send,
1621 .pwrite_recv_fn = cephwrap_pwrite_recv,
1622 .lseek_fn = cephwrap_lseek,
1623 .sendfile_fn = cephwrap_sendfile,
1624 .recvfile_fn = cephwrap_recvfile,
1625 .renameat_fn = cephwrap_renameat,
1626 .fsync_send_fn = cephwrap_fsync_send,
1627 .fsync_recv_fn = cephwrap_fsync_recv,
1628 .stat_fn = cephwrap_stat,
1629 .fstat_fn = cephwrap_fstat,
1630 .lstat_fn = cephwrap_lstat,
1631 .unlinkat_fn = cephwrap_unlinkat,
1632 .fchmod_fn = cephwrap_fchmod,
1633 .fchown_fn = cephwrap_fchown,
1634 .lchown_fn = cephwrap_lchown,
1635 .chdir_fn = cephwrap_chdir,
1636 .getwd_fn = cephwrap_getwd,
1637 .fntimes_fn = cephwrap_fntimes,
1638 .ftruncate_fn = cephwrap_ftruncate,
1639 .fallocate_fn = cephwrap_fallocate,
1640 .lock_fn = cephwrap_lock,
1641 .filesystem_sharemode_fn = cephwrap_filesystem_sharemode,
1642 .fcntl_fn = cephwrap_fcntl,
1643 .linux_setlease_fn = cephwrap_linux_setlease,
1644 .getlock_fn = cephwrap_getlock,
1645 .symlinkat_fn = cephwrap_symlinkat,
1646 .readlinkat_fn = cephwrap_readlinkat,
1647 .linkat_fn = cephwrap_linkat,
1648 .mknodat_fn = cephwrap_mknodat,
1649 .realpath_fn = cephwrap_realpath,
1650 .fchflags_fn = cephwrap_fchflags,
1651 .get_real_filename_fn = cephwrap_get_real_filename,
1652 .get_real_filename_at_fn = cephwrap_get_real_filename_at,
1653 .connectpath_fn = cephwrap_connectpath,
1655 /* EA operations. */
1656 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1657 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1658 .fgetxattr_fn = cephwrap_fgetxattr,
1659 .flistxattr_fn = cephwrap_flistxattr,
1660 .fremovexattr_fn = cephwrap_fremovexattr,
1661 .fsetxattr_fn = cephwrap_fsetxattr,
1663 /* Posix ACL Operations */
1664 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1665 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1666 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1667 .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
1669 /* aio operations */
1670 .aio_force_fn = cephwrap_aio_force,
1674 NTSTATUS vfs_ceph_init(TALLOC_CTX *ctx)
1676 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,