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,
309 /* OpenDir_fsp() falls back to regular open */
314 static struct dirent *cephwrap_readdir(struct vfs_handle_struct *handle,
316 SMB_STRUCT_STAT *sbuf)
318 struct dirent *result;
320 DBG_DEBUG("[CEPH] readdir(%p, %p)\n", handle, dirp);
321 result = ceph_readdir(handle->data, (struct ceph_dir_result *) dirp);
322 DBG_DEBUG("[CEPH] readdir(...) = %p\n", result);
324 /* Default Posix readdir() does not give us stat info.
325 * Set to invalid to indicate we didn't return this info. */
327 SET_STAT_INVALID(*sbuf);
331 static void cephwrap_seekdir(struct vfs_handle_struct *handle, DIR *dirp, long offset)
333 DBG_DEBUG("[CEPH] seekdir(%p, %p, %ld)\n", handle, dirp, offset);
334 ceph_seekdir(handle->data, (struct ceph_dir_result *) dirp, offset);
337 static long cephwrap_telldir(struct vfs_handle_struct *handle, DIR *dirp)
340 DBG_DEBUG("[CEPH] telldir(%p, %p)\n", handle, dirp);
341 ret = ceph_telldir(handle->data, (struct ceph_dir_result *) dirp);
342 DBG_DEBUG("[CEPH] telldir(...) = %ld\n", ret);
346 static void cephwrap_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
348 DBG_DEBUG("[CEPH] rewinddir(%p, %p)\n", handle, dirp);
349 ceph_rewinddir(handle->data, (struct ceph_dir_result *) dirp);
352 static int cephwrap_mkdirat(struct vfs_handle_struct *handle,
353 files_struct *dirfsp,
354 const struct smb_filename *smb_fname,
359 const char *path = smb_fname->base_name;
361 DBG_DEBUG("[CEPH] mkdir(%p, %s)\n", handle, path);
363 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
365 if (lp_inherit_acls(SNUM(handle->conn))
366 && parent_dirname(talloc_tos(), path, &parent, NULL)
367 && directory_has_default_acl(handle->conn, parent)) {
373 result = ceph_mkdir(handle->data, path, mode);
374 return WRAP_RETURN(result);
377 static int cephwrap_closedir(struct vfs_handle_struct *handle, DIR *dirp)
381 DBG_DEBUG("[CEPH] closedir(%p, %p)\n", handle, dirp);
382 result = ceph_closedir(handle->data, (struct ceph_dir_result *) dirp);
383 DBG_DEBUG("[CEPH] closedir(...) = %d\n", result);
387 /* File operations */
389 static int cephwrap_open(struct vfs_handle_struct *handle,
390 struct smb_filename *smb_fname,
391 files_struct *fsp, int flags, mode_t mode)
393 int result = -ENOENT;
394 DBG_DEBUG("[CEPH] open(%p, %s, %p, %d, %d)\n", handle,
395 smb_fname_str_dbg(smb_fname), fsp, flags, mode);
397 if (smb_fname->stream_name) {
401 result = ceph_open(handle->data, smb_fname->base_name, flags, mode);
403 DBG_DEBUG("[CEPH] open(...) = %d\n", result);
407 static int cephwrap_close(struct vfs_handle_struct *handle, files_struct *fsp)
411 DBG_DEBUG("[CEPH] close(%p, %p)\n", handle, fsp);
412 result = ceph_close(handle->data, fsp->fh->fd);
413 DBG_DEBUG("[CEPH] close(...) = %d\n", result);
418 static ssize_t cephwrap_pread(struct vfs_handle_struct *handle, files_struct *fsp, void *data,
419 size_t n, off_t offset)
423 DBG_DEBUG("[CEPH] pread(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
425 result = ceph_read(handle->data, fsp->fh->fd, data, n, offset);
426 DBG_DEBUG("[CEPH] pread(...) = %llu\n", llu(result));
430 struct cephwrap_pread_state {
432 struct vfs_aio_state vfs_aio_state;
436 * Fake up an async ceph read by calling the synchronous API.
438 static struct tevent_req *cephwrap_pread_send(struct vfs_handle_struct *handle,
440 struct tevent_context *ev,
441 struct files_struct *fsp,
443 size_t n, off_t offset)
445 struct tevent_req *req = NULL;
446 struct cephwrap_pread_state *state = NULL;
449 DBG_DEBUG("[CEPH] %s\n", __func__);
450 req = tevent_req_create(mem_ctx, &state, struct cephwrap_pread_state);
455 ret = ceph_read(handle->data, fsp->fh->fd, data, n, offset);
457 /* ceph returns -errno on error. */
458 tevent_req_error(req, -ret);
459 return tevent_req_post(req, ev);
462 state->bytes_read = ret;
463 tevent_req_done(req);
464 /* Return and schedule the completion of the call. */
465 return tevent_req_post(req, ev);
468 static ssize_t cephwrap_pread_recv(struct tevent_req *req,
469 struct vfs_aio_state *vfs_aio_state)
471 struct cephwrap_pread_state *state =
472 tevent_req_data(req, struct cephwrap_pread_state);
474 DBG_DEBUG("[CEPH] %s\n", __func__);
475 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
478 *vfs_aio_state = state->vfs_aio_state;
479 return state->bytes_read;
482 static ssize_t cephwrap_pwrite(struct vfs_handle_struct *handle, files_struct *fsp, const void *data,
483 size_t n, off_t offset)
487 DBG_DEBUG("[CEPH] pwrite(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
488 result = ceph_write(handle->data, fsp->fh->fd, data, n, offset);
489 DBG_DEBUG("[CEPH] pwrite(...) = %llu\n", llu(result));
493 struct cephwrap_pwrite_state {
494 ssize_t bytes_written;
495 struct vfs_aio_state vfs_aio_state;
499 * Fake up an async ceph write by calling the synchronous API.
501 static struct tevent_req *cephwrap_pwrite_send(struct vfs_handle_struct *handle,
503 struct tevent_context *ev,
504 struct files_struct *fsp,
506 size_t n, off_t offset)
508 struct tevent_req *req = NULL;
509 struct cephwrap_pwrite_state *state = NULL;
512 DBG_DEBUG("[CEPH] %s\n", __func__);
513 req = tevent_req_create(mem_ctx, &state, struct cephwrap_pwrite_state);
518 ret = ceph_write(handle->data, fsp->fh->fd, data, n, offset);
520 /* ceph returns -errno on error. */
521 tevent_req_error(req, -ret);
522 return tevent_req_post(req, ev);
525 state->bytes_written = ret;
526 tevent_req_done(req);
527 /* Return and schedule the completion of the call. */
528 return tevent_req_post(req, ev);
531 static ssize_t cephwrap_pwrite_recv(struct tevent_req *req,
532 struct vfs_aio_state *vfs_aio_state)
534 struct cephwrap_pwrite_state *state =
535 tevent_req_data(req, struct cephwrap_pwrite_state);
537 DBG_DEBUG("[CEPH] %s\n", __func__);
538 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
541 *vfs_aio_state = state->vfs_aio_state;
542 return state->bytes_written;
545 static off_t cephwrap_lseek(struct vfs_handle_struct *handle, files_struct *fsp, off_t offset, int whence)
549 DBG_DEBUG("[CEPH] cephwrap_lseek\n");
550 result = ceph_lseek(handle->data, fsp->fh->fd, offset, whence);
554 static ssize_t cephwrap_sendfile(struct vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
555 off_t offset, size_t n)
558 * We cannot support sendfile because libceph is in user space.
560 DBG_DEBUG("[CEPH] cephwrap_sendfile\n");
565 static ssize_t cephwrap_recvfile(struct vfs_handle_struct *handle,
572 * We cannot support recvfile because libceph is in user space.
574 DBG_DEBUG("[CEPH] cephwrap_recvfile\n");
579 static int cephwrap_renameat(struct vfs_handle_struct *handle,
580 files_struct *srcfsp,
581 const struct smb_filename *smb_fname_src,
582 files_struct *dstfsp,
583 const struct smb_filename *smb_fname_dst)
586 DBG_DEBUG("[CEPH] cephwrap_renameat\n");
587 if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
592 SMB_ASSERT(srcfsp == srcfsp->conn->cwd_fsp);
593 SMB_ASSERT(dstfsp == dstfsp->conn->cwd_fsp);
595 result = ceph_rename(handle->data, smb_fname_src->base_name, smb_fname_dst->base_name);
600 * Fake up an async ceph fsync by calling the synchronous API.
603 static struct tevent_req *cephwrap_fsync_send(struct vfs_handle_struct *handle,
605 struct tevent_context *ev,
608 struct tevent_req *req = NULL;
609 struct vfs_aio_state *state = NULL;
612 DBG_DEBUG("[CEPH] cephwrap_fsync_send\n");
614 req = tevent_req_create(mem_ctx, &state, struct vfs_aio_state);
619 /* Make sync call. */
620 ret = ceph_fsync(handle->data, fsp->fh->fd, false);
623 /* ceph_fsync returns -errno on error. */
624 tevent_req_error(req, -ret);
625 return tevent_req_post(req, ev);
628 /* Mark it as done. */
629 tevent_req_done(req);
630 /* Return and schedule the completion of the call. */
631 return tevent_req_post(req, ev);
634 static int cephwrap_fsync_recv(struct tevent_req *req,
635 struct vfs_aio_state *vfs_aio_state)
637 struct vfs_aio_state *state =
638 tevent_req_data(req, struct vfs_aio_state);
640 DBG_DEBUG("[CEPH] cephwrap_fsync_recv\n");
642 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
645 *vfs_aio_state = *state;
649 #define SAMBA_STATX_ATTR_MASK (CEPH_STATX_BASIC_STATS|CEPH_STATX_BTIME)
651 static void init_stat_ex_from_ceph_statx(struct stat_ex *dst, const struct ceph_statx *stx)
653 DBG_DEBUG("[CEPH]\tstx = {dev = %llx, ino = %llu, mode = 0x%x, "
654 "nlink = %llu, uid = %d, gid = %d, rdev = %llx, size = %llu, "
655 "blksize = %llu, blocks = %llu, atime = %llu, mtime = %llu, "
656 "ctime = %llu, btime = %llu}\n",
657 llu(stx->stx_dev), llu(stx->stx_ino), stx->stx_mode,
658 llu(stx->stx_nlink), stx->stx_uid, stx->stx_gid,
659 llu(stx->stx_rdev), llu(stx->stx_size), llu(stx->stx_blksize),
660 llu(stx->stx_blocks), llu(stx->stx_atime.tv_sec),
661 llu(stx->stx_mtime.tv_sec), llu(stx->stx_ctime.tv_sec),
662 llu(stx->stx_btime.tv_sec));
664 if ((stx->stx_mask & SAMBA_STATX_ATTR_MASK) != SAMBA_STATX_ATTR_MASK) {
665 DBG_WARNING("%s: stx->stx_mask is incorrect (wanted %x, got %x)",
666 __func__, SAMBA_STATX_ATTR_MASK, stx->stx_mask);
669 dst->st_ex_dev = stx->stx_dev;
670 dst->st_ex_rdev = stx->stx_rdev;
671 dst->st_ex_ino = stx->stx_ino;
672 dst->st_ex_mode = stx->stx_mode;
673 dst->st_ex_uid = stx->stx_uid;
674 dst->st_ex_gid = stx->stx_gid;
675 dst->st_ex_size = stx->stx_size;
676 dst->st_ex_nlink = stx->stx_nlink;
677 dst->st_ex_atime = stx->stx_atime;
678 dst->st_ex_btime = stx->stx_btime;
679 dst->st_ex_ctime = stx->stx_ctime;
680 dst->st_ex_mtime = stx->stx_mtime;
681 dst->st_ex_itime = dst->st_ex_btime;
682 dst->st_ex_iflags = ST_EX_IFLAG_CALCULATED_ITIME;
683 dst->st_ex_blksize = stx->stx_blksize;
684 dst->st_ex_blocks = stx->stx_blocks;
685 dst->st_ex_file_id = dst->st_ex_ino;
686 dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_FILE_ID;
689 static int cephwrap_stat(struct vfs_handle_struct *handle,
690 struct smb_filename *smb_fname)
693 struct ceph_statx stx;
695 DBG_DEBUG("[CEPH] stat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
697 if (smb_fname->stream_name) {
702 result = ceph_statx(handle->data, smb_fname->base_name, &stx,
703 SAMBA_STATX_ATTR_MASK, 0);
704 DBG_DEBUG("[CEPH] statx(...) = %d\n", result);
709 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
710 DBG_DEBUG("[CEPH] mode = 0x%x\n", smb_fname->st.st_ex_mode);
714 static int cephwrap_fstat(struct vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
717 struct ceph_statx stx;
719 DBG_DEBUG("[CEPH] fstat(%p, %d)\n", handle, fsp->fh->fd);
720 result = ceph_fstatx(handle->data, fsp->fh->fd, &stx,
721 SAMBA_STATX_ATTR_MASK, 0);
722 DBG_DEBUG("[CEPH] fstat(...) = %d\n", result);
727 init_stat_ex_from_ceph_statx(sbuf, &stx);
728 DBG_DEBUG("[CEPH] mode = 0x%x\n", sbuf->st_ex_mode);
732 static int cephwrap_lstat(struct vfs_handle_struct *handle,
733 struct smb_filename *smb_fname)
736 struct ceph_statx stx;
738 DBG_DEBUG("[CEPH] lstat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
740 if (smb_fname->stream_name) {
745 result = ceph_statx(handle->data, smb_fname->base_name, &stx,
746 SAMBA_STATX_ATTR_MASK, AT_SYMLINK_NOFOLLOW);
747 DBG_DEBUG("[CEPH] lstat(...) = %d\n", result);
752 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
756 static int cephwrap_ntimes(struct vfs_handle_struct *handle,
757 const struct smb_filename *smb_fname,
758 struct smb_file_time *ft)
760 struct ceph_statx stx = { 0 };
764 if (!is_omit_timespec(&ft->atime)) {
765 stx.stx_atime = ft->atime;
766 mask |= CEPH_SETATTR_ATIME;
768 if (!is_omit_timespec(&ft->mtime)) {
769 stx.stx_mtime = ft->mtime;
770 mask |= CEPH_SETATTR_MTIME;
772 if (!is_omit_timespec(&ft->create_time)) {
773 stx.stx_btime = ft->create_time;
774 mask |= CEPH_SETATTR_BTIME;
781 result = ceph_setattrx(handle->data, smb_fname->base_name, &stx, mask, 0);
782 DBG_DEBUG("[CEPH] ntimes(%p, %s, {%ld, %ld, %ld, %ld}) = %d\n", handle, smb_fname_str_dbg(smb_fname),
783 ft->mtime.tv_sec, ft->atime.tv_sec, ft->ctime.tv_sec,
784 ft->create_time.tv_sec, result);
788 static int cephwrap_unlinkat(struct vfs_handle_struct *handle,
789 struct files_struct *dirfsp,
790 const struct smb_filename *smb_fname,
795 DBG_DEBUG("[CEPH] unlink(%p, %s)\n",
797 smb_fname_str_dbg(smb_fname));
798 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
799 if (smb_fname->stream_name) {
803 if (flags & AT_REMOVEDIR) {
804 result = ceph_rmdir(handle->data, smb_fname->base_name);
806 result = ceph_unlink(handle->data, smb_fname->base_name);
808 DBG_DEBUG("[CEPH] unlink(...) = %d\n", result);
812 static int cephwrap_chmod(struct vfs_handle_struct *handle,
813 const struct smb_filename *smb_fname,
818 DBG_DEBUG("[CEPH] chmod(%p, %s, %d)\n", handle, smb_fname->base_name, mode);
819 result = ceph_chmod(handle->data, smb_fname->base_name, mode);
820 DBG_DEBUG("[CEPH] chmod(...) = %d\n", result);
824 static int cephwrap_fchmod(struct vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
828 DBG_DEBUG("[CEPH] fchmod(%p, %p, %d)\n", handle, fsp, mode);
829 result = ceph_fchmod(handle->data, fsp->fh->fd, mode);
830 DBG_DEBUG("[CEPH] fchmod(...) = %d\n", result);
834 static int cephwrap_fchown(struct vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
838 DBG_DEBUG("[CEPH] fchown(%p, %p, %d, %d)\n", handle, fsp, uid, gid);
839 result = ceph_fchown(handle->data, fsp->fh->fd, uid, gid);
840 DBG_DEBUG("[CEPH] fchown(...) = %d\n", result);
844 static int cephwrap_lchown(struct vfs_handle_struct *handle,
845 const struct smb_filename *smb_fname,
850 DBG_DEBUG("[CEPH] lchown(%p, %s, %d, %d)\n", handle, smb_fname->base_name, uid, gid);
851 result = ceph_lchown(handle->data, smb_fname->base_name, uid, gid);
852 DBG_DEBUG("[CEPH] lchown(...) = %d\n", result);
856 static int cephwrap_chdir(struct vfs_handle_struct *handle,
857 const struct smb_filename *smb_fname)
860 DBG_DEBUG("[CEPH] chdir(%p, %s)\n", handle, smb_fname->base_name);
861 result = ceph_chdir(handle->data, smb_fname->base_name);
862 DBG_DEBUG("[CEPH] chdir(...) = %d\n", result);
866 static struct smb_filename *cephwrap_getwd(struct vfs_handle_struct *handle,
869 const char *cwd = ceph_getcwd(handle->data);
870 DBG_DEBUG("[CEPH] getwd(%p) = %s\n", handle, cwd);
871 return synthetic_smb_fname(ctx,
878 static int strict_allocate_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
880 off_t space_to_write;
883 SMB_STRUCT_STAT *pst;
885 status = vfs_stat_fsp(fsp);
886 if (!NT_STATUS_IS_OK(status)) {
889 pst = &fsp->fsp_name->st;
892 if (S_ISFIFO(pst->st_ex_mode))
896 if (pst->st_ex_size == len)
899 /* Shrink - just ftruncate. */
900 if (pst->st_ex_size > len) {
901 result = ceph_ftruncate(handle->data, fsp->fh->fd, len);
905 space_to_write = len - pst->st_ex_size;
906 result = ceph_fallocate(handle->data, fsp->fh->fd, 0, pst->st_ex_size,
911 static int cephwrap_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
915 DBG_DEBUG("[CEPH] ftruncate(%p, %p, %llu\n", handle, fsp, llu(len));
917 if (lp_strict_allocate(SNUM(fsp->conn))) {
918 return strict_allocate_ftruncate(handle, fsp, len);
921 result = ceph_ftruncate(handle->data, fsp->fh->fd, len);
925 static int cephwrap_fallocate(struct vfs_handle_struct *handle,
926 struct files_struct *fsp,
933 DBG_DEBUG("[CEPH] fallocate(%p, %p, %u, %llu, %llu\n",
934 handle, fsp, mode, llu(offset), llu(len));
935 /* unsupported mode flags are rejected by libcephfs */
936 result = ceph_fallocate(handle->data, fsp->fh->fd, mode, offset, len);
937 DBG_DEBUG("[CEPH] fallocate(...) = %d\n", result);
941 static bool cephwrap_lock(struct vfs_handle_struct *handle, files_struct *fsp, int op, off_t offset, off_t count, int type)
943 DBG_DEBUG("[CEPH] lock\n");
947 static int cephwrap_kernel_flock(struct vfs_handle_struct *handle,
949 uint32_t share_access,
950 uint32_t access_mask)
952 DBG_ERR("[CEPH] flock unsupported! Consider setting "
953 "\"kernel share modes = no\"\n");
959 static int cephwrap_fcntl(vfs_handle_struct *handle,
960 files_struct *fsp, int cmd, va_list cmd_arg)
963 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
964 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
966 if (cmd == F_GETFL) {
968 } else if (cmd == F_SETFL) {
972 va_copy(dup_cmd_arg, cmd_arg);
973 opt = va_arg(dup_cmd_arg, int);
978 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
981 DBG_ERR("unexpected fcntl: %d\n", cmd);
987 static bool cephwrap_getlock(struct vfs_handle_struct *handle, files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
989 DBG_DEBUG("[CEPH] getlock returning false and errno=0\n");
996 * We cannot let this fall through to the default, because the file might only
997 * be accessible from libceph (which is a user-space client) but the fd might
998 * be for some file the kernel knows about.
1000 static int cephwrap_linux_setlease(struct vfs_handle_struct *handle, files_struct *fsp,
1005 DBG_DEBUG("[CEPH] linux_setlease\n");
1010 static int cephwrap_symlinkat(struct vfs_handle_struct *handle,
1011 const char *link_target,
1012 struct files_struct *dirfsp,
1013 const struct smb_filename *new_smb_fname)
1016 DBG_DEBUG("[CEPH] symlink(%p, %s, %s)\n", handle,
1018 new_smb_fname->base_name);
1020 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1022 result = ceph_symlink(handle->data,
1024 new_smb_fname->base_name);
1025 DBG_DEBUG("[CEPH] symlink(...) = %d\n", result);
1026 WRAP_RETURN(result);
1029 static int cephwrap_readlinkat(struct vfs_handle_struct *handle,
1030 files_struct *dirfsp,
1031 const struct smb_filename *smb_fname,
1036 DBG_DEBUG("[CEPH] readlink(%p, %s, %p, %llu)\n", handle,
1037 smb_fname->base_name, buf, llu(bufsiz));
1039 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1041 result = ceph_readlink(handle->data, smb_fname->base_name, buf, bufsiz);
1042 DBG_DEBUG("[CEPH] readlink(...) = %d\n", result);
1043 WRAP_RETURN(result);
1046 static int cephwrap_linkat(struct vfs_handle_struct *handle,
1047 files_struct *srcfsp,
1048 const struct smb_filename *old_smb_fname,
1049 files_struct *dstfsp,
1050 const struct smb_filename *new_smb_fname,
1054 DBG_DEBUG("[CEPH] link(%p, %s, %s)\n", handle,
1055 old_smb_fname->base_name,
1056 new_smb_fname->base_name);
1058 SMB_ASSERT(srcfsp == srcfsp->conn->cwd_fsp);
1059 SMB_ASSERT(dstfsp == dstfsp->conn->cwd_fsp);
1061 result = ceph_link(handle->data,
1062 old_smb_fname->base_name,
1063 new_smb_fname->base_name);
1064 DBG_DEBUG("[CEPH] link(...) = %d\n", result);
1065 WRAP_RETURN(result);
1068 static int cephwrap_mknodat(struct vfs_handle_struct *handle,
1069 files_struct *dirfsp,
1070 const struct smb_filename *smb_fname,
1075 DBG_DEBUG("[CEPH] mknodat(%p, %s)\n", handle, smb_fname->base_name);
1076 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1077 result = ceph_mknod(handle->data, smb_fname->base_name, mode, dev);
1078 DBG_DEBUG("[CEPH] mknodat(...) = %d\n", result);
1079 WRAP_RETURN(result);
1083 * This is a simple version of real-path ... a better version is needed to
1084 * ask libceph about symbolic links.
1086 static struct smb_filename *cephwrap_realpath(struct vfs_handle_struct *handle,
1088 const struct smb_filename *smb_fname)
1090 char *result = NULL;
1091 const char *path = smb_fname->base_name;
1092 size_t len = strlen(path);
1093 struct smb_filename *result_fname = NULL;
1096 if (len && (path[0] == '/')) {
1097 r = asprintf(&result, "%s", path);
1098 } else if ((len >= 2) && (path[0] == '.') && (path[1] == '/')) {
1100 r = asprintf(&result, "%s",
1101 handle->conn->cwd_fsp->fsp_name->base_name);
1103 r = asprintf(&result, "%s/%s",
1104 handle->conn->cwd_fsp->fsp_name->base_name, &path[2]);
1107 r = asprintf(&result, "%s/%s",
1108 handle->conn->cwd_fsp->fsp_name->base_name, path);
1115 DBG_DEBUG("[CEPH] realpath(%p, %s) = %s\n", handle, path, result);
1116 result_fname = synthetic_smb_fname(ctx,
1122 return result_fname;
1125 static int cephwrap_chflags(struct vfs_handle_struct *handle,
1126 const struct smb_filename *smb_fname,
1133 static int cephwrap_get_real_filename(struct vfs_handle_struct *handle,
1136 TALLOC_CTX *mem_ctx,
1140 * Don't fall back to get_real_filename so callers can differentiate
1141 * between a full directory scan and an actual case-insensitive stat.
1147 static const char *cephwrap_connectpath(struct vfs_handle_struct *handle,
1148 const struct smb_filename *smb_fname)
1150 return handle->conn->connectpath;
1153 /****************************************************************
1154 Extended attribute operations.
1155 *****************************************************************/
1157 static ssize_t cephwrap_getxattr(struct vfs_handle_struct *handle,
1158 const struct smb_filename *smb_fname,
1164 DBG_DEBUG("[CEPH] getxattr(%p, %s, %s, %p, %llu)\n", handle,
1165 smb_fname->base_name, name, value, llu(size));
1166 ret = ceph_getxattr(handle->data,
1167 smb_fname->base_name, name, value, size);
1168 DBG_DEBUG("[CEPH] getxattr(...) = %d\n", ret);
1172 return (ssize_t)ret;
1175 static ssize_t cephwrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
1178 DBG_DEBUG("[CEPH] fgetxattr(%p, %p, %s, %p, %llu)\n", handle, fsp, name, value, llu(size));
1179 ret = ceph_fgetxattr(handle->data, fsp->fh->fd, name, value, size);
1180 DBG_DEBUG("[CEPH] fgetxattr(...) = %d\n", ret);
1184 return (ssize_t)ret;
1187 static ssize_t cephwrap_listxattr(struct vfs_handle_struct *handle,
1188 const struct smb_filename *smb_fname,
1193 DBG_DEBUG("[CEPH] listxattr(%p, %s, %p, %llu)\n", handle,
1194 smb_fname->base_name, list, llu(size));
1195 ret = ceph_listxattr(handle->data, smb_fname->base_name, list, size);
1196 DBG_DEBUG("[CEPH] listxattr(...) = %d\n", ret);
1200 return (ssize_t)ret;
1203 static ssize_t cephwrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
1206 DBG_DEBUG("[CEPH] flistxattr(%p, %p, %p, %llu)\n",
1207 handle, fsp, list, llu(size));
1208 ret = ceph_flistxattr(handle->data, fsp->fh->fd, list, size);
1209 DBG_DEBUG("[CEPH] flistxattr(...) = %d\n", ret);
1213 return (ssize_t)ret;
1216 static int cephwrap_removexattr(struct vfs_handle_struct *handle,
1217 const struct smb_filename *smb_fname,
1221 DBG_DEBUG("[CEPH] removexattr(%p, %s, %s)\n", handle,
1222 smb_fname->base_name, name);
1223 ret = ceph_removexattr(handle->data, smb_fname->base_name, name);
1224 DBG_DEBUG("[CEPH] removexattr(...) = %d\n", ret);
1228 static int cephwrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
1231 DBG_DEBUG("[CEPH] fremovexattr(%p, %p, %s)\n", handle, fsp, name);
1232 ret = ceph_fremovexattr(handle->data, fsp->fh->fd, name);
1233 DBG_DEBUG("[CEPH] fremovexattr(...) = %d\n", ret);
1237 static int cephwrap_setxattr(struct vfs_handle_struct *handle,
1238 const struct smb_filename *smb_fname,
1245 DBG_DEBUG("[CEPH] setxattr(%p, %s, %s, %p, %llu, %d)\n", handle,
1246 smb_fname->base_name, name, value, llu(size), flags);
1247 ret = ceph_setxattr(handle->data, smb_fname->base_name,
1248 name, value, size, flags);
1249 DBG_DEBUG("[CEPH] setxattr(...) = %d\n", ret);
1253 static int cephwrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
1256 DBG_DEBUG("[CEPH] fsetxattr(%p, %p, %s, %p, %llu, %d)\n", handle, fsp, name, value, llu(size), flags);
1257 ret = ceph_fsetxattr(handle->data, fsp->fh->fd,
1258 name, value, size, flags);
1259 DBG_DEBUG("[CEPH] fsetxattr(...) = %d\n", ret);
1263 static bool cephwrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
1267 * We do not support AIO yet.
1270 DBG_DEBUG("[CEPH] cephwrap_aio_force(%p, %p) = false (errno = ENOTSUP)\n", handle, fsp);
1275 static NTSTATUS cephwrap_create_dfs_pathat(struct vfs_handle_struct *handle,
1276 struct files_struct *dirfsp,
1277 const struct smb_filename *smb_fname,
1278 const struct referral *reflist,
1279 size_t referral_count)
1281 TALLOC_CTX *frame = talloc_stackframe();
1282 NTSTATUS status = NT_STATUS_NO_MEMORY;
1284 char *msdfs_link = NULL;
1286 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1288 /* Form the msdfs_link contents */
1289 msdfs_link = msdfs_link_string(frame,
1292 if (msdfs_link == NULL) {
1296 ret = ceph_symlink(handle->data,
1298 smb_fname->base_name);
1300 status = NT_STATUS_OK;
1302 status = map_nt_error_from_unix(-ret);
1307 DBG_DEBUG("[CEPH] create_dfs_pathat(%s) = %s\n",
1308 smb_fname->base_name,
1316 * Read and return the contents of a DFS redirect given a
1317 * pathname. A caller can pass in NULL for ppreflist and
1318 * preferral_count but still determine if this was a
1319 * DFS redirect point by getting NT_STATUS_OK back
1320 * without incurring the overhead of reading and parsing
1321 * the referral contents.
1324 static NTSTATUS cephwrap_read_dfs_pathat(struct vfs_handle_struct *handle,
1325 TALLOC_CTX *mem_ctx,
1326 struct files_struct *dirfsp,
1327 const struct smb_filename *smb_fname,
1328 struct referral **ppreflist,
1329 size_t *preferral_count)
1331 NTSTATUS status = NT_STATUS_NO_MEMORY;
1333 char *link_target = NULL;
1336 #if defined(HAVE_BROKEN_READLINK)
1337 char link_target_buf[PATH_MAX];
1339 char link_target_buf[7];
1342 SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1344 if (ppreflist == NULL && preferral_count == NULL) {
1346 * We're only checking if this is a DFS
1347 * redirect. We don't need to return data.
1349 bufsize = sizeof(link_target_buf);
1350 link_target = link_target_buf;
1353 link_target = talloc_array(mem_ctx, char, bufsize);
1359 referral_len = ceph_readlink(handle->data,
1360 smb_fname->base_name,
1363 if (referral_len < 0) {
1364 /* ceph errors are -errno. */
1365 if (-referral_len == EINVAL) {
1366 DBG_INFO("%s is not a link.\n",
1367 smb_fname->base_name);
1368 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1370 status = map_nt_error_from_unix(-referral_len);
1371 DBG_ERR("Error reading "
1372 "msdfs link %s: %s\n",
1373 smb_fname->base_name,
1378 link_target[referral_len] = '\0';
1380 DBG_INFO("%s -> %s\n",
1381 smb_fname->base_name,
1384 if (!strnequal(link_target, "msdfs:", 6)) {
1385 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1389 if (ppreflist == NULL && preferral_count == NULL) {
1390 /* Early return for checking if this is a DFS link. */
1391 return NT_STATUS_OK;
1394 ok = parse_msdfs_symlink(mem_ctx,
1395 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
1401 status = NT_STATUS_OK;
1403 status = NT_STATUS_NO_MEMORY;
1408 if (link_target != link_target_buf) {
1409 TALLOC_FREE(link_target);
1414 static struct vfs_fn_pointers ceph_fns = {
1415 /* Disk operations */
1417 .connect_fn = cephwrap_connect,
1418 .disconnect_fn = cephwrap_disconnect,
1419 .disk_free_fn = cephwrap_disk_free,
1420 .get_quota_fn = cephwrap_get_quota,
1421 .set_quota_fn = cephwrap_set_quota,
1422 .statvfs_fn = cephwrap_statvfs,
1423 .fs_capabilities_fn = cephwrap_fs_capabilities,
1425 /* Directory operations */
1427 .fdopendir_fn = cephwrap_fdopendir,
1428 .readdir_fn = cephwrap_readdir,
1429 .seekdir_fn = cephwrap_seekdir,
1430 .telldir_fn = cephwrap_telldir,
1431 .rewind_dir_fn = cephwrap_rewinddir,
1432 .mkdirat_fn = cephwrap_mkdirat,
1433 .closedir_fn = cephwrap_closedir,
1435 /* File operations */
1437 .create_dfs_pathat_fn = cephwrap_create_dfs_pathat,
1438 .read_dfs_pathat_fn = cephwrap_read_dfs_pathat,
1439 .open_fn = cephwrap_open,
1440 .close_fn = cephwrap_close,
1441 .pread_fn = cephwrap_pread,
1442 .pread_send_fn = cephwrap_pread_send,
1443 .pread_recv_fn = cephwrap_pread_recv,
1444 .pwrite_fn = cephwrap_pwrite,
1445 .pwrite_send_fn = cephwrap_pwrite_send,
1446 .pwrite_recv_fn = cephwrap_pwrite_recv,
1447 .lseek_fn = cephwrap_lseek,
1448 .sendfile_fn = cephwrap_sendfile,
1449 .recvfile_fn = cephwrap_recvfile,
1450 .renameat_fn = cephwrap_renameat,
1451 .fsync_send_fn = cephwrap_fsync_send,
1452 .fsync_recv_fn = cephwrap_fsync_recv,
1453 .stat_fn = cephwrap_stat,
1454 .fstat_fn = cephwrap_fstat,
1455 .lstat_fn = cephwrap_lstat,
1456 .unlinkat_fn = cephwrap_unlinkat,
1457 .chmod_fn = cephwrap_chmod,
1458 .fchmod_fn = cephwrap_fchmod,
1459 .fchown_fn = cephwrap_fchown,
1460 .lchown_fn = cephwrap_lchown,
1461 .chdir_fn = cephwrap_chdir,
1462 .getwd_fn = cephwrap_getwd,
1463 .ntimes_fn = cephwrap_ntimes,
1464 .ftruncate_fn = cephwrap_ftruncate,
1465 .fallocate_fn = cephwrap_fallocate,
1466 .lock_fn = cephwrap_lock,
1467 .kernel_flock_fn = cephwrap_kernel_flock,
1468 .fcntl_fn = cephwrap_fcntl,
1469 .linux_setlease_fn = cephwrap_linux_setlease,
1470 .getlock_fn = cephwrap_getlock,
1471 .symlinkat_fn = cephwrap_symlinkat,
1472 .readlinkat_fn = cephwrap_readlinkat,
1473 .linkat_fn = cephwrap_linkat,
1474 .mknodat_fn = cephwrap_mknodat,
1475 .realpath_fn = cephwrap_realpath,
1476 .chflags_fn = cephwrap_chflags,
1477 .get_real_filename_fn = cephwrap_get_real_filename,
1478 .connectpath_fn = cephwrap_connectpath,
1480 /* EA operations. */
1481 .getxattr_fn = cephwrap_getxattr,
1482 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1483 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1484 .fgetxattr_fn = cephwrap_fgetxattr,
1485 .listxattr_fn = cephwrap_listxattr,
1486 .flistxattr_fn = cephwrap_flistxattr,
1487 .removexattr_fn = cephwrap_removexattr,
1488 .fremovexattr_fn = cephwrap_fremovexattr,
1489 .setxattr_fn = cephwrap_setxattr,
1490 .fsetxattr_fn = cephwrap_fsetxattr,
1492 /* Posix ACL Operations */
1493 .sys_acl_get_file_fn = posixacl_xattr_acl_get_file,
1494 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1495 .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1496 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1497 .sys_acl_set_file_fn = posixacl_xattr_acl_set_file,
1498 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1499 .sys_acl_delete_def_file_fn = posixacl_xattr_acl_delete_def_file,
1501 /* aio operations */
1502 .aio_force_fn = cephwrap_aio_force,
1506 NTSTATUS vfs_ceph_init(TALLOC_CTX *ctx)
1508 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,