vfs_ceph: Indicate a successful connection in logs
[samba4-gss.git] / source3 / modules / vfs_ceph.c
blob88712e872a5a71ad8f4ec304b3b4aab4003150eb
1 /*
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 libcephfs.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
28 * Ceph:
30 * vfs objects = [any others you need go here] ceph
33 #include "includes.h"
34 #include "smbd/smbd.h"
35 #include "system/filesys.h"
36 #include <dirent.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"
43 #undef DBGC_CLASS
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)
49 #endif
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, libcephfs's return code model is to return -errno! So we have to
58 * convert to what Samba expects, which is to set errno to -return and return -1
60 #define WRAP_RETURN(_res) \
61 errno = 0; \
62 if (_res < 0) { \
63 errno = -_res; \
64 return -1; \
65 } \
66 return _res \
69 * Track unique connections, as virtual mounts, to cephfs file systems.
70 * Individual mounts will be set on the handle->data attribute, but
71 * the mounts themselves will be shared so as not to spawn extra mounts
72 * to the same cephfs.
74 * Individual mounts are IDed by a 'cookie' value that is a string built
75 * from identifying parameters found in smb.conf.
78 static struct cephmount_cached {
79 char *cookie;
80 uint32_t count;
81 struct ceph_mount_info *mount;
82 struct cephmount_cached *next, *prev;
83 } *cephmount_cached;
85 static int cephmount_cache_add(const char *cookie,
86 struct ceph_mount_info *mount)
88 struct cephmount_cached *entry = NULL;
90 entry = talloc_zero(NULL, struct cephmount_cached);
91 if (entry == NULL) {
92 errno = ENOMEM;
93 return -1;
96 entry->cookie = talloc_strdup(entry, cookie);
97 if (entry->cookie == NULL) {
98 talloc_free(entry);
99 errno = ENOMEM;
100 return -1;
103 entry->mount = mount;
104 entry->count = 1;
106 DBG_DEBUG("adding mount cache entry for %s\n", entry->cookie);
107 DLIST_ADD(cephmount_cached, entry);
108 return 0;
111 static struct ceph_mount_info *cephmount_cache_update(const char *cookie)
113 struct cephmount_cached *entry = NULL;
115 for (entry = cephmount_cached; entry; entry = entry->next) {
116 if (strcmp(entry->cookie, cookie) == 0) {
117 entry->count++;
118 DBG_DEBUG("updated mount cache: count is [%"
119 PRIu32 "]\n", entry->count);
120 return entry->mount;
124 errno = ENOENT;
125 return NULL;
128 static int cephmount_cache_remove(struct ceph_mount_info *mount)
130 struct cephmount_cached *entry = NULL;
132 for (entry = cephmount_cached; entry; entry = entry->next) {
133 if (entry->mount == mount) {
134 if (--entry->count) {
135 DBG_DEBUG("updated mount cache: count is [%"
136 PRIu32 "]\n", entry->count);
137 return entry->count;
140 DBG_DEBUG("removing mount cache entry for %s\n",
141 entry->cookie);
142 DLIST_REMOVE(cephmount_cached, entry);
143 talloc_free(entry);
144 return 0;
147 errno = ENOENT;
148 return -1;
151 static char *cephmount_get_cookie(TALLOC_CTX * mem_ctx, const int snum)
153 const char *conf_file =
154 lp_parm_const_string(snum, "ceph", "config_file", ".");
155 const char *user_id = lp_parm_const_string(snum, "ceph", "user_id", "");
156 const char *fsname =
157 lp_parm_const_string(snum, "ceph", "filesystem", "");
158 return talloc_asprintf(mem_ctx, "(%s/%s/%s)", conf_file, user_id,
159 fsname);
162 static int cephmount_select_fs(struct ceph_mount_info *mnt, const char *fsname)
165 * ceph_select_filesystem was added in ceph 'nautilus' (v14).
166 * Earlier versions of libcephfs will lack that API function.
167 * At the time of this writing (Feb 2023) all versions of ceph
168 * supported by ceph upstream have this function.
170 #if defined(HAVE_CEPH_SELECT_FILESYSTEM)
171 DBG_DEBUG("[CEPH] calling: ceph_select_filesystem with %s\n", fsname);
172 return ceph_select_filesystem(mnt, fsname);
173 #else
174 DBG_ERR("[CEPH] ceph_select_filesystem not available\n");
175 return -ENOTSUP;
176 #endif
179 static struct ceph_mount_info *cephmount_mount_fs(const int snum)
181 int ret;
182 char buf[256];
183 struct ceph_mount_info *mnt = NULL;
184 /* if config_file and/or user_id are NULL, ceph will use defaults */
185 const char *conf_file =
186 lp_parm_const_string(snum, "ceph", "config_file", NULL);
187 const char *user_id =
188 lp_parm_const_string(snum, "ceph", "user_id", NULL);
189 const char *fsname =
190 lp_parm_const_string(snum, "ceph", "filesystem", NULL);
192 DBG_DEBUG("[CEPH] calling: ceph_create\n");
193 ret = ceph_create(&mnt, user_id);
194 if (ret) {
195 errno = -ret;
196 return NULL;
199 DBG_DEBUG("[CEPH] calling: ceph_conf_read_file with %s\n",
200 (conf_file == NULL ? "default path" : conf_file));
201 ret = ceph_conf_read_file(mnt, conf_file);
202 if (ret) {
203 goto err_cm_release;
206 DBG_DEBUG("[CEPH] calling: ceph_conf_get\n");
207 ret = ceph_conf_get(mnt, "log file", buf, sizeof(buf));
208 if (ret < 0) {
209 goto err_cm_release;
212 /* libcephfs disables POSIX ACL support by default, enable it... */
213 ret = ceph_conf_set(mnt, "client_acl_type", "posix_acl");
214 if (ret < 0) {
215 goto err_cm_release;
217 /* tell libcephfs to perform local permission checks */
218 ret = ceph_conf_set(mnt, "fuse_default_permissions", "false");
219 if (ret < 0) {
220 goto err_cm_release;
223 * select a cephfs file system to use:
224 * In ceph, multiple file system support has been stable since 'pacific'.
225 * Permit different shares to access different file systems.
227 if (fsname != NULL) {
228 ret = cephmount_select_fs(mnt, fsname);
229 if (ret < 0) {
230 goto err_cm_release;
234 DBG_DEBUG("[CEPH] calling: ceph_mount\n");
235 ret = ceph_mount(mnt, NULL);
236 if (ret >= 0) {
237 goto cm_done;
240 err_cm_release:
241 ceph_release(mnt);
242 mnt = NULL;
243 DBG_DEBUG("[CEPH] Error mounting fs: %s\n", strerror(-ret));
244 cm_done:
246 * Handle the error correctly. Ceph returns -errno.
248 if (ret) {
249 errno = -ret;
251 return mnt;
254 /* Check for NULL pointer parameters in cephwrap_* functions */
256 /* We don't want to have NULL function pointers lying around. Someone
257 is sure to try and execute them. These stubs are used to prevent
258 this possibility. */
260 static int cephwrap_connect(struct vfs_handle_struct *handle,
261 const char *service, const char *user)
263 int ret = 0;
264 struct ceph_mount_info *cmount = NULL;
265 int snum = SNUM(handle->conn);
266 char *cookie = cephmount_get_cookie(handle, snum);
267 if (cookie == NULL) {
268 return -1;
271 cmount = cephmount_cache_update(cookie);
272 if (cmount != NULL) {
273 goto connect_ok;
276 cmount = cephmount_mount_fs(snum);
277 if (cmount == NULL) {
278 ret = -1;
279 goto connect_fail;
281 ret = cephmount_cache_add(cookie, cmount);
282 if (ret) {
283 goto connect_fail;
286 connect_ok:
287 handle->data = cmount;
288 DBG_WARNING("Connection established with the server: %s\n", cookie);
290 * Unless we have an async implementation of getxattrat turn this off.
292 lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
293 connect_fail:
294 talloc_free(cookie);
295 return ret;
298 static void cephwrap_disconnect(struct vfs_handle_struct *handle)
300 int ret = cephmount_cache_remove(handle->data);
301 if (ret < 0) {
302 DBG_ERR("failed to remove ceph mount from cache: %s\n",
303 strerror(errno));
304 return;
306 if (ret > 0) {
307 DBG_DEBUG("mount cache entry still in use\n");
308 return;
311 ret = ceph_unmount(handle->data);
312 if (ret < 0) {
313 DBG_ERR("[CEPH] failed to unmount: %s\n", strerror(-ret));
316 ret = ceph_release(handle->data);
317 if (ret < 0) {
318 DBG_ERR("[CEPH] failed to release: %s\n", strerror(-ret));
320 handle->data = NULL;
323 /* Disk operations */
325 static uint64_t cephwrap_disk_free(struct vfs_handle_struct *handle,
326 const struct smb_filename *smb_fname,
327 uint64_t *bsize,
328 uint64_t *dfree,
329 uint64_t *dsize)
331 struct statvfs statvfs_buf = { 0 };
332 int ret;
334 if (!(ret = ceph_statfs(handle->data, smb_fname->base_name,
335 &statvfs_buf))) {
337 * Provide all the correct values.
339 *bsize = statvfs_buf.f_bsize;
340 *dfree = statvfs_buf.f_bavail;
341 *dsize = statvfs_buf.f_blocks;
342 DBG_DEBUG("[CEPH] bsize: %llu, dfree: %llu, dsize: %llu\n",
343 llu(*bsize), llu(*dfree), llu(*dsize));
344 return *dfree;
345 } else {
346 DBG_DEBUG("[CEPH] ceph_statfs returned %d\n", ret);
347 WRAP_RETURN(ret);
351 static int cephwrap_get_quota(struct vfs_handle_struct *handle,
352 const struct smb_filename *smb_fname,
353 enum SMB_QUOTA_TYPE qtype,
354 unid_t id,
355 SMB_DISK_QUOTA *qt)
357 /* libcephfs: Ceph does not implement this */
358 #if 0
359 /* was ifdef HAVE_SYS_QUOTAS */
360 int ret;
362 ret = ceph_get_quota(handle->conn->connectpath, qtype, id, qt);
364 if (ret) {
365 errno = -ret;
366 ret = -1;
369 return ret;
370 #else
371 errno = ENOSYS;
372 return -1;
373 #endif
376 static int cephwrap_set_quota(struct vfs_handle_struct *handle, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
378 /* libcephfs: Ceph does not implement this */
379 #if 0
380 /* was ifdef HAVE_SYS_QUOTAS */
381 int ret;
383 ret = ceph_set_quota(handle->conn->connectpath, qtype, id, qt);
384 if (ret) {
385 errno = -ret;
386 ret = -1;
389 return ret;
390 #else
391 WRAP_RETURN(-ENOSYS);
392 #endif
395 static int cephwrap_statvfs(struct vfs_handle_struct *handle,
396 const struct smb_filename *smb_fname,
397 struct vfs_statvfs_struct *statbuf)
399 struct statvfs statvfs_buf = { 0 };
400 int ret;
402 ret = ceph_statfs(handle->data, smb_fname->base_name, &statvfs_buf);
403 if (ret < 0) {
404 WRAP_RETURN(ret);
407 statbuf->OptimalTransferSize = statvfs_buf.f_frsize;
408 statbuf->BlockSize = statvfs_buf.f_bsize;
409 statbuf->TotalBlocks = statvfs_buf.f_blocks;
410 statbuf->BlocksAvail = statvfs_buf.f_bfree;
411 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
412 statbuf->TotalFileNodes = statvfs_buf.f_files;
413 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
414 statbuf->FsIdentifier = statvfs_buf.f_fsid;
415 DBG_DEBUG("[CEPH] f_bsize: %ld, f_blocks: %ld, f_bfree: %ld, f_bavail: %ld\n",
416 (long int)statvfs_buf.f_bsize, (long int)statvfs_buf.f_blocks,
417 (long int)statvfs_buf.f_bfree, (long int)statvfs_buf.f_bavail);
419 return ret;
422 static uint32_t cephwrap_fs_capabilities(struct vfs_handle_struct *handle,
423 enum timestamp_set_resolution *p_ts_res)
425 uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
427 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
429 return caps;
432 /* Directory operations */
434 static DIR *cephwrap_fdopendir(struct vfs_handle_struct *handle,
435 struct files_struct *fsp,
436 const char *mask,
437 uint32_t attributes)
439 int ret = 0;
440 struct ceph_dir_result *result = NULL;
441 DBG_DEBUG("[CEPH] fdopendir(%p, %p)\n", handle, fsp);
443 ret = ceph_opendir(handle->data, fsp->fsp_name->base_name, &result);
444 if (ret < 0) {
445 result = NULL;
446 errno = -ret; /* We return result which is NULL in this case */
449 DBG_DEBUG("[CEPH] fdopendir(...) = %d\n", ret);
450 return (DIR *) result;
453 static struct dirent *cephwrap_readdir(struct vfs_handle_struct *handle,
454 struct files_struct *dirfsp,
455 DIR *dirp)
457 struct dirent *result = NULL;
459 DBG_DEBUG("[CEPH] readdir(%p, %p)\n", handle, dirp);
460 result = ceph_readdir(handle->data, (struct ceph_dir_result *) dirp);
461 DBG_DEBUG("[CEPH] readdir(...) = %p\n", result);
463 return result;
466 static void cephwrap_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
468 DBG_DEBUG("[CEPH] rewinddir(%p, %p)\n", handle, dirp);
469 ceph_rewinddir(handle->data, (struct ceph_dir_result *) dirp);
472 static int cephwrap_mkdirat(struct vfs_handle_struct *handle,
473 files_struct *dirfsp,
474 const struct smb_filename *smb_fname,
475 mode_t mode)
477 int result = -1;
478 #ifdef HAVE_CEPH_MKDIRAT
479 int dirfd = fsp_get_pathref_fd(dirfsp);
481 DBG_DEBUG("[CEPH] mkdirat(%p, %d, %s)\n",
482 handle,
483 dirfd,
484 smb_fname->base_name);
486 result = ceph_mkdirat(handle->data, dirfd, smb_fname->base_name, mode);
488 DBG_DEBUG("[CEPH] mkdirat(...) = %d\n", result);
490 WRAP_RETURN(result);
491 #else
492 struct smb_filename *full_fname = NULL;
494 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
495 dirfsp,
496 smb_fname);
497 if (full_fname == NULL) {
498 return -1;
501 DBG_DEBUG("[CEPH] mkdir(%p, %s)\n",
502 handle, smb_fname_str_dbg(full_fname));
504 result = ceph_mkdir(handle->data, full_fname->base_name, mode);
506 TALLOC_FREE(full_fname);
508 WRAP_RETURN(result);
509 #endif
512 static int cephwrap_closedir(struct vfs_handle_struct *handle, DIR *dirp)
514 int result;
516 DBG_DEBUG("[CEPH] closedir(%p, %p)\n", handle, dirp);
517 result = ceph_closedir(handle->data, (struct ceph_dir_result *) dirp);
518 DBG_DEBUG("[CEPH] closedir(...) = %d\n", result);
519 WRAP_RETURN(result);
522 /* File operations */
524 static int cephwrap_openat(struct vfs_handle_struct *handle,
525 const struct files_struct *dirfsp,
526 const struct smb_filename *smb_fname,
527 files_struct *fsp,
528 const struct vfs_open_how *how)
530 int flags = how->flags;
531 mode_t mode = how->mode;
532 struct smb_filename *name = NULL;
533 bool have_opath = false;
534 bool became_root = false;
535 int result = -ENOENT;
536 #ifdef HAVE_CEPH_OPENAT
537 int dirfd = -1;
538 #endif
540 if (how->resolve != 0) {
541 errno = ENOSYS;
542 return -1;
545 if (smb_fname->stream_name) {
546 goto out;
549 #ifdef O_PATH
550 have_opath = true;
551 if (fsp->fsp_flags.is_pathref) {
552 flags |= O_PATH;
554 #endif
556 #ifdef HAVE_CEPH_OPENAT
557 dirfd = fsp_get_pathref_fd(dirfsp);
559 DBG_DEBUG("[CEPH] openat(%p, %d, %p, %d, %d)\n",
560 handle, dirfd, fsp, flags, mode);
562 if (fsp->fsp_flags.is_pathref && !have_opath) {
563 become_root();
564 became_root = true;
567 result = ceph_openat(handle->data,
568 dirfd,
569 smb_fname->base_name,
570 flags,
571 mode);
573 #else
574 if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
575 name = full_path_from_dirfsp_atname(talloc_tos(),
576 dirfsp,
577 smb_fname);
578 if (name == NULL) {
579 return -1;
581 smb_fname = name;
584 DBG_DEBUG("[CEPH] openat(%p, %s, %p, %d, %d)\n", handle,
585 smb_fname_str_dbg(smb_fname), fsp, flags, mode);
587 if (fsp->fsp_flags.is_pathref && !have_opath) {
588 become_root();
589 became_root = true;
592 result = ceph_open(handle->data, smb_fname->base_name, flags, mode);
593 #endif
594 if (became_root) {
595 unbecome_root();
597 out:
598 TALLOC_FREE(name);
599 fsp->fsp_flags.have_proc_fds = false;
600 DBG_DEBUG("[CEPH] open(...) = %d\n", result);
601 WRAP_RETURN(result);
604 static int cephwrap_close(struct vfs_handle_struct *handle, files_struct *fsp)
606 int result;
608 DBG_DEBUG("[CEPH] close(%p, %p)\n", handle, fsp);
609 result = ceph_close(handle->data, fsp_get_pathref_fd(fsp));
610 DBG_DEBUG("[CEPH] close(...) = %d\n", result);
612 WRAP_RETURN(result);
615 static ssize_t cephwrap_pread(struct vfs_handle_struct *handle, files_struct *fsp, void *data,
616 size_t n, off_t offset)
618 ssize_t result;
620 DBG_DEBUG("[CEPH] pread(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
622 result = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
623 DBG_DEBUG("[CEPH] pread(...) = %llu\n", llu(result));
624 WRAP_RETURN(result);
627 struct cephwrap_pread_state {
628 ssize_t bytes_read;
629 struct vfs_aio_state vfs_aio_state;
633 * Fake up an async ceph read by calling the synchronous API.
635 static struct tevent_req *cephwrap_pread_send(struct vfs_handle_struct *handle,
636 TALLOC_CTX *mem_ctx,
637 struct tevent_context *ev,
638 struct files_struct *fsp,
639 void *data,
640 size_t n, off_t offset)
642 struct tevent_req *req = NULL;
643 struct cephwrap_pread_state *state = NULL;
644 int ret = -1;
646 DBG_DEBUG("[CEPH] %s\n", __func__);
647 req = tevent_req_create(mem_ctx, &state, struct cephwrap_pread_state);
648 if (req == NULL) {
649 return NULL;
652 ret = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
653 if (ret < 0) {
654 /* ceph returns -errno on error. */
655 tevent_req_error(req, -ret);
656 return tevent_req_post(req, ev);
659 state->bytes_read = ret;
660 tevent_req_done(req);
661 /* Return and schedule the completion of the call. */
662 return tevent_req_post(req, ev);
665 static ssize_t cephwrap_pread_recv(struct tevent_req *req,
666 struct vfs_aio_state *vfs_aio_state)
668 struct cephwrap_pread_state *state =
669 tevent_req_data(req, struct cephwrap_pread_state);
671 DBG_DEBUG("[CEPH] %s\n", __func__);
672 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
673 return -1;
675 *vfs_aio_state = state->vfs_aio_state;
676 return state->bytes_read;
679 static ssize_t cephwrap_pwrite(struct vfs_handle_struct *handle, files_struct *fsp, const void *data,
680 size_t n, off_t offset)
682 ssize_t result;
684 DBG_DEBUG("[CEPH] pwrite(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
685 result = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
686 DBG_DEBUG("[CEPH] pwrite(...) = %llu\n", llu(result));
687 WRAP_RETURN(result);
690 struct cephwrap_pwrite_state {
691 ssize_t bytes_written;
692 struct vfs_aio_state vfs_aio_state;
696 * Fake up an async ceph write by calling the synchronous API.
698 static struct tevent_req *cephwrap_pwrite_send(struct vfs_handle_struct *handle,
699 TALLOC_CTX *mem_ctx,
700 struct tevent_context *ev,
701 struct files_struct *fsp,
702 const void *data,
703 size_t n, off_t offset)
705 struct tevent_req *req = NULL;
706 struct cephwrap_pwrite_state *state = NULL;
707 int ret = -1;
709 DBG_DEBUG("[CEPH] %s\n", __func__);
710 req = tevent_req_create(mem_ctx, &state, struct cephwrap_pwrite_state);
711 if (req == NULL) {
712 return NULL;
715 ret = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
716 if (ret < 0) {
717 /* ceph returns -errno on error. */
718 tevent_req_error(req, -ret);
719 return tevent_req_post(req, ev);
722 state->bytes_written = ret;
723 tevent_req_done(req);
724 /* Return and schedule the completion of the call. */
725 return tevent_req_post(req, ev);
728 static ssize_t cephwrap_pwrite_recv(struct tevent_req *req,
729 struct vfs_aio_state *vfs_aio_state)
731 struct cephwrap_pwrite_state *state =
732 tevent_req_data(req, struct cephwrap_pwrite_state);
734 DBG_DEBUG("[CEPH] %s\n", __func__);
735 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
736 return -1;
738 *vfs_aio_state = state->vfs_aio_state;
739 return state->bytes_written;
742 static off_t cephwrap_lseek(struct vfs_handle_struct *handle, files_struct *fsp, off_t offset, int whence)
744 off_t result = 0;
746 DBG_DEBUG("[CEPH] cephwrap_lseek\n");
747 result = ceph_lseek(handle->data, fsp_get_io_fd(fsp), offset, whence);
748 WRAP_RETURN(result);
751 static ssize_t cephwrap_sendfile(struct vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
752 off_t offset, size_t n)
755 * We cannot support sendfile because libcephfs is in user space.
757 DBG_DEBUG("[CEPH] cephwrap_sendfile\n");
758 errno = ENOTSUP;
759 return -1;
762 static ssize_t cephwrap_recvfile(struct vfs_handle_struct *handle,
763 int fromfd,
764 files_struct *tofsp,
765 off_t offset,
766 size_t n)
769 * We cannot support recvfile because libcephfs is in user space.
771 DBG_DEBUG("[CEPH] cephwrap_recvfile\n");
772 errno=ENOTSUP;
773 return -1;
776 static int cephwrap_renameat(struct vfs_handle_struct *handle,
777 files_struct *srcfsp,
778 const struct smb_filename *smb_fname_src,
779 files_struct *dstfsp,
780 const struct smb_filename *smb_fname_dst)
782 struct smb_filename *full_fname_src = NULL;
783 struct smb_filename *full_fname_dst = NULL;
784 int result = -1;
786 DBG_DEBUG("[CEPH] cephwrap_renameat\n");
787 if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
788 errno = ENOENT;
789 return result;
792 full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
793 srcfsp,
794 smb_fname_src);
795 if (full_fname_src == NULL) {
796 errno = ENOMEM;
797 return -1;
799 full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
800 dstfsp,
801 smb_fname_dst);
802 if (full_fname_dst == NULL) {
803 TALLOC_FREE(full_fname_src);
804 errno = ENOMEM;
805 return -1;
808 result = ceph_rename(handle->data,
809 full_fname_src->base_name,
810 full_fname_dst->base_name);
812 TALLOC_FREE(full_fname_src);
813 TALLOC_FREE(full_fname_dst);
815 WRAP_RETURN(result);
819 * Fake up an async ceph fsync by calling the synchronous API.
822 static struct tevent_req *cephwrap_fsync_send(struct vfs_handle_struct *handle,
823 TALLOC_CTX *mem_ctx,
824 struct tevent_context *ev,
825 files_struct *fsp)
827 struct tevent_req *req = NULL;
828 struct vfs_aio_state *state = NULL;
829 int ret = -1;
831 DBG_DEBUG("[CEPH] cephwrap_fsync_send\n");
833 req = tevent_req_create(mem_ctx, &state, struct vfs_aio_state);
834 if (req == NULL) {
835 return NULL;
838 /* Make sync call. */
839 ret = ceph_fsync(handle->data, fsp_get_io_fd(fsp), false);
841 if (ret != 0) {
842 /* ceph_fsync returns -errno on error. */
843 tevent_req_error(req, -ret);
844 return tevent_req_post(req, ev);
847 /* Mark it as done. */
848 tevent_req_done(req);
849 /* Return and schedule the completion of the call. */
850 return tevent_req_post(req, ev);
853 static int cephwrap_fsync_recv(struct tevent_req *req,
854 struct vfs_aio_state *vfs_aio_state)
856 struct vfs_aio_state *state =
857 tevent_req_data(req, struct vfs_aio_state);
859 DBG_DEBUG("[CEPH] cephwrap_fsync_recv\n");
861 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
862 return -1;
864 *vfs_aio_state = *state;
865 return 0;
868 #define SAMBA_STATX_ATTR_MASK (CEPH_STATX_BASIC_STATS|CEPH_STATX_BTIME)
870 static void init_stat_ex_from_ceph_statx(struct stat_ex *dst, const struct ceph_statx *stx)
872 DBG_DEBUG("[CEPH]\tstx = {dev = %llx, ino = %llu, mode = 0x%x, "
873 "nlink = %llu, uid = %d, gid = %d, rdev = %llx, size = %llu, "
874 "blksize = %llu, blocks = %llu, atime = %llu, mtime = %llu, "
875 "ctime = %llu, btime = %llu}\n",
876 llu(stx->stx_dev), llu(stx->stx_ino), stx->stx_mode,
877 llu(stx->stx_nlink), stx->stx_uid, stx->stx_gid,
878 llu(stx->stx_rdev), llu(stx->stx_size), llu(stx->stx_blksize),
879 llu(stx->stx_blocks), llu(stx->stx_atime.tv_sec),
880 llu(stx->stx_mtime.tv_sec), llu(stx->stx_ctime.tv_sec),
881 llu(stx->stx_btime.tv_sec));
883 if ((stx->stx_mask & SAMBA_STATX_ATTR_MASK) != SAMBA_STATX_ATTR_MASK) {
884 DBG_WARNING("%s: stx->stx_mask is incorrect (wanted %x, got %x)\n",
885 __func__, SAMBA_STATX_ATTR_MASK, stx->stx_mask);
888 dst->st_ex_dev = stx->stx_dev;
889 dst->st_ex_rdev = stx->stx_rdev;
890 dst->st_ex_ino = stx->stx_ino;
891 dst->st_ex_mode = stx->stx_mode;
892 dst->st_ex_uid = stx->stx_uid;
893 dst->st_ex_gid = stx->stx_gid;
894 dst->st_ex_size = stx->stx_size;
895 dst->st_ex_nlink = stx->stx_nlink;
896 dst->st_ex_atime = stx->stx_atime;
897 dst->st_ex_btime = stx->stx_btime;
898 dst->st_ex_ctime = stx->stx_ctime;
899 dst->st_ex_mtime = stx->stx_mtime;
900 dst->st_ex_blksize = stx->stx_blksize;
901 dst->st_ex_blocks = stx->stx_blocks;
904 static int cephwrap_stat(struct vfs_handle_struct *handle,
905 struct smb_filename *smb_fname)
907 int result = -1;
908 struct ceph_statx stx = { 0 };
910 DBG_DEBUG("[CEPH] stat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
912 if (smb_fname->stream_name) {
913 errno = ENOENT;
914 return result;
917 result = ceph_statx(handle->data, smb_fname->base_name, &stx,
918 SAMBA_STATX_ATTR_MASK, 0);
919 DBG_DEBUG("[CEPH] statx(...) = %d\n", result);
920 if (result < 0) {
921 WRAP_RETURN(result);
924 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
925 DBG_DEBUG("[CEPH] mode = 0x%x\n", smb_fname->st.st_ex_mode);
926 return result;
929 static int cephwrap_fstat(struct vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
931 int result = -1;
932 struct ceph_statx stx = { 0 };
933 int fd = fsp_get_pathref_fd(fsp);
935 DBG_DEBUG("[CEPH] fstat(%p, %d)\n", handle, fd);
936 result = ceph_fstatx(handle->data, fd, &stx,
937 SAMBA_STATX_ATTR_MASK, 0);
938 DBG_DEBUG("[CEPH] fstat(...) = %d\n", result);
939 if (result < 0) {
940 WRAP_RETURN(result);
943 init_stat_ex_from_ceph_statx(sbuf, &stx);
944 DBG_DEBUG("[CEPH] mode = 0x%x\n", sbuf->st_ex_mode);
945 return result;
948 static int cephwrap_lstat(struct vfs_handle_struct *handle,
949 struct smb_filename *smb_fname)
951 int result = -1;
952 struct ceph_statx stx = { 0 };
954 DBG_DEBUG("[CEPH] lstat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
956 if (smb_fname->stream_name) {
957 errno = ENOENT;
958 return result;
961 result = ceph_statx(handle->data, smb_fname->base_name, &stx,
962 SAMBA_STATX_ATTR_MASK, AT_SYMLINK_NOFOLLOW);
963 DBG_DEBUG("[CEPH] lstat(...) = %d\n", result);
964 if (result < 0) {
965 WRAP_RETURN(result);
968 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
969 return result;
972 static int cephwrap_fntimes(struct vfs_handle_struct *handle,
973 files_struct *fsp,
974 struct smb_file_time *ft)
976 struct ceph_statx stx = { 0 };
977 int result;
978 int mask = 0;
980 if (!is_omit_timespec(&ft->atime)) {
981 stx.stx_atime = ft->atime;
982 mask |= CEPH_SETATTR_ATIME;
984 if (!is_omit_timespec(&ft->mtime)) {
985 stx.stx_mtime = ft->mtime;
986 mask |= CEPH_SETATTR_MTIME;
988 if (!is_omit_timespec(&ft->create_time)) {
989 stx.stx_btime = ft->create_time;
990 mask |= CEPH_SETATTR_BTIME;
993 if (!mask) {
994 return 0;
997 if (!fsp->fsp_flags.is_pathref) {
999 * We can use an io_fd to set xattrs.
1001 result = ceph_fsetattrx(handle->data,
1002 fsp_get_io_fd(fsp),
1003 &stx,
1004 mask);
1005 } else {
1007 * This is no longer a handle based call.
1009 result = ceph_setattrx(handle->data,
1010 fsp->fsp_name->base_name,
1011 &stx,
1012 mask,
1016 DBG_DEBUG("[CEPH] ntimes(%p, %s, {%ld, %ld, %ld, %ld}) = %d\n",
1017 handle, fsp_str_dbg(fsp), ft->mtime.tv_sec, ft->atime.tv_sec,
1018 ft->ctime.tv_sec, ft->create_time.tv_sec, result);
1020 return result;
1023 static int cephwrap_unlinkat(struct vfs_handle_struct *handle,
1024 struct files_struct *dirfsp,
1025 const struct smb_filename *smb_fname,
1026 int flags)
1028 int result = -1;
1029 #ifdef HAVE_CEPH_UNLINKAT
1030 int dirfd = fsp_get_pathref_fd(dirfsp);
1032 DBG_DEBUG("[CEPH] unlinkat(%p, %d, %s)\n",
1033 handle,
1034 dirfd,
1035 smb_fname_str_dbg(smb_fname));
1037 if (smb_fname->stream_name) {
1038 errno = ENOENT;
1039 return result;
1042 result = ceph_unlinkat(handle->data,
1043 dirfd,
1044 smb_fname->base_name,
1045 flags);
1046 DBG_DEBUG("[CEPH] unlinkat(...) = %d\n", result);
1047 WRAP_RETURN(result);
1048 #else
1049 struct smb_filename *full_fname = NULL;
1051 DBG_DEBUG("[CEPH] unlink(%p, %s)\n",
1052 handle,
1053 smb_fname_str_dbg(smb_fname));
1055 if (smb_fname->stream_name) {
1056 errno = ENOENT;
1057 return result;
1060 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1061 dirfsp,
1062 smb_fname);
1063 if (full_fname == NULL) {
1064 return -1;
1067 if (flags & AT_REMOVEDIR) {
1068 result = ceph_rmdir(handle->data, full_fname->base_name);
1069 } else {
1070 result = ceph_unlink(handle->data, full_fname->base_name);
1072 TALLOC_FREE(full_fname);
1073 DBG_DEBUG("[CEPH] unlink(...) = %d\n", result);
1074 WRAP_RETURN(result);
1075 #endif
1078 static int cephwrap_fchmod(struct vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1080 int result;
1082 DBG_DEBUG("[CEPH] fchmod(%p, %p, %d)\n", handle, fsp, mode);
1083 if (!fsp->fsp_flags.is_pathref) {
1085 * We can use an io_fd to change permissions.
1087 result = ceph_fchmod(handle->data, fsp_get_io_fd(fsp), mode);
1088 } else {
1090 * This is no longer a handle based call.
1092 result = ceph_chmod(handle->data,
1093 fsp->fsp_name->base_name,
1094 mode);
1096 DBG_DEBUG("[CEPH] fchmod(...) = %d\n", result);
1097 WRAP_RETURN(result);
1100 static int cephwrap_fchown(struct vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
1102 int result;
1104 DBG_DEBUG("[CEPH] fchown(%p, %p, %d, %d)\n", handle, fsp, uid, gid);
1105 if (!fsp->fsp_flags.is_pathref) {
1107 * We can use an io_fd to change ownership.
1109 result = ceph_fchown(handle->data,
1110 fsp_get_io_fd(fsp),
1111 uid,
1112 gid);
1113 } else {
1115 * This is no longer a handle based call.
1117 result = ceph_chown(handle->data,
1118 fsp->fsp_name->base_name,
1119 uid,
1120 gid);
1123 DBG_DEBUG("[CEPH] fchown(...) = %d\n", result);
1124 WRAP_RETURN(result);
1127 static int cephwrap_lchown(struct vfs_handle_struct *handle,
1128 const struct smb_filename *smb_fname,
1129 uid_t uid,
1130 gid_t gid)
1132 int result;
1133 DBG_DEBUG("[CEPH] lchown(%p, %s, %d, %d)\n", handle, smb_fname->base_name, uid, gid);
1134 result = ceph_lchown(handle->data, smb_fname->base_name, uid, gid);
1135 DBG_DEBUG("[CEPH] lchown(...) = %d\n", result);
1136 WRAP_RETURN(result);
1139 static int cephwrap_chdir(struct vfs_handle_struct *handle,
1140 const struct smb_filename *smb_fname)
1142 int result = -1;
1143 DBG_DEBUG("[CEPH] chdir(%p, %s)\n", handle, smb_fname->base_name);
1144 result = ceph_chdir(handle->data, smb_fname->base_name);
1145 DBG_DEBUG("[CEPH] chdir(...) = %d\n", result);
1146 WRAP_RETURN(result);
1149 static struct smb_filename *cephwrap_getwd(struct vfs_handle_struct *handle,
1150 TALLOC_CTX *ctx)
1152 const char *cwd = ceph_getcwd(handle->data);
1153 DBG_DEBUG("[CEPH] getwd(%p) = %s\n", handle, cwd);
1154 return synthetic_smb_fname(ctx,
1155 cwd,
1156 NULL,
1157 NULL,
1162 static int strict_allocate_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
1164 off_t space_to_write;
1165 int result;
1166 NTSTATUS status;
1167 SMB_STRUCT_STAT *pst;
1169 status = vfs_stat_fsp(fsp);
1170 if (!NT_STATUS_IS_OK(status)) {
1171 return -1;
1173 pst = &fsp->fsp_name->st;
1175 #ifdef S_ISFIFO
1176 if (S_ISFIFO(pst->st_ex_mode))
1177 return 0;
1178 #endif
1180 if (pst->st_ex_size == len)
1181 return 0;
1183 /* Shrink - just ftruncate. */
1184 if (pst->st_ex_size > len) {
1185 result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
1186 WRAP_RETURN(result);
1189 space_to_write = len - pst->st_ex_size;
1190 result = ceph_fallocate(handle->data, fsp_get_io_fd(fsp), 0, pst->st_ex_size,
1191 space_to_write);
1192 WRAP_RETURN(result);
1195 static int cephwrap_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
1197 int result = -1;
1199 DBG_DEBUG("[CEPH] ftruncate(%p, %p, %llu\n", handle, fsp, llu(len));
1201 if (lp_strict_allocate(SNUM(fsp->conn))) {
1202 return strict_allocate_ftruncate(handle, fsp, len);
1205 result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
1206 WRAP_RETURN(result);
1209 static int cephwrap_fallocate(struct vfs_handle_struct *handle,
1210 struct files_struct *fsp,
1211 uint32_t mode,
1212 off_t offset,
1213 off_t len)
1215 int result;
1217 DBG_DEBUG("[CEPH] fallocate(%p, %p, %u, %llu, %llu\n",
1218 handle, fsp, mode, llu(offset), llu(len));
1219 /* unsupported mode flags are rejected by libcephfs */
1220 result = ceph_fallocate(handle->data, fsp_get_io_fd(fsp), mode, offset, len);
1221 DBG_DEBUG("[CEPH] fallocate(...) = %d\n", result);
1222 WRAP_RETURN(result);
1225 static bool cephwrap_lock(struct vfs_handle_struct *handle, files_struct *fsp, int op, off_t offset, off_t count, int type)
1227 DBG_DEBUG("[CEPH] lock\n");
1228 return true;
1231 static int cephwrap_filesystem_sharemode(struct vfs_handle_struct *handle,
1232 files_struct *fsp,
1233 uint32_t share_access,
1234 uint32_t access_mask)
1236 DBG_ERR("[CEPH] filesystem sharemodes unsupported! Consider setting "
1237 "\"kernel share modes = no\"\n");
1239 errno = ENOSYS;
1240 return -1;
1243 static int cephwrap_fcntl(vfs_handle_struct *handle,
1244 files_struct *fsp, int cmd, va_list cmd_arg)
1247 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1248 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1250 if (cmd == F_GETFL) {
1251 return 0;
1252 } else if (cmd == F_SETFL) {
1253 va_list dup_cmd_arg;
1254 int opt;
1256 va_copy(dup_cmd_arg, cmd_arg);
1257 opt = va_arg(dup_cmd_arg, int);
1258 va_end(dup_cmd_arg);
1259 if (opt == 0) {
1260 return 0;
1262 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1263 goto err_out;
1265 DBG_ERR("unexpected fcntl: %d\n", cmd);
1266 err_out:
1267 errno = EINVAL;
1268 return -1;
1271 static bool cephwrap_getlock(struct vfs_handle_struct *handle, files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1273 DBG_DEBUG("[CEPH] getlock returning false and errno=0\n");
1275 errno = 0;
1276 return false;
1280 * We cannot let this fall through to the default, because the file might only
1281 * be accessible from libcephfs (which is a user-space client) but the fd might
1282 * be for some file the kernel knows about.
1284 static int cephwrap_linux_setlease(struct vfs_handle_struct *handle, files_struct *fsp,
1285 int leasetype)
1287 int result = -1;
1289 DBG_DEBUG("[CEPH] linux_setlease\n");
1290 errno = ENOSYS;
1291 return result;
1294 static int cephwrap_symlinkat(struct vfs_handle_struct *handle,
1295 const struct smb_filename *link_target,
1296 struct files_struct *dirfsp,
1297 const struct smb_filename *new_smb_fname)
1299 int result = -1;
1300 #ifdef HAVE_CEPH_SYMLINKAT
1301 int dirfd = fsp_get_pathref_fd(dirfsp);
1303 DBG_DEBUG("[CEPH] symlinkat(%p, %s, %d, %s)\n",
1304 handle,
1305 link_target->base_name,
1306 dirfd,
1307 new_smb_fname->base_name);
1309 result = ceph_symlinkat(handle->data,
1310 link_target->base_name,
1311 dirfd,
1312 new_smb_fname->base_name);
1313 DBG_DEBUG("[CEPH] symlinkat(...) = %d\n", result);
1314 WRAP_RETURN(result);
1315 #else
1316 struct smb_filename *full_fname = NULL;
1318 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1319 dirfsp,
1320 new_smb_fname);
1321 if (full_fname == NULL) {
1322 return -1;
1325 DBG_DEBUG("[CEPH] symlink(%p, %s, %s)\n", handle,
1326 link_target->base_name,
1327 full_fname->base_name);
1329 result = ceph_symlink(handle->data,
1330 link_target->base_name,
1331 full_fname->base_name);
1332 TALLOC_FREE(full_fname);
1333 DBG_DEBUG("[CEPH] symlink(...) = %d\n", result);
1334 WRAP_RETURN(result);
1335 #endif
1338 static int cephwrap_readlinkat(struct vfs_handle_struct *handle,
1339 const struct files_struct *dirfsp,
1340 const struct smb_filename *smb_fname,
1341 char *buf,
1342 size_t bufsiz)
1344 int result = -1;
1345 #ifdef HAVE_CEPH_READLINKAT
1346 int dirfd = fsp_get_pathref_fd(dirfsp);
1348 DBG_DEBUG("[CEPH] readlinkat(%p, %d, %s, %p, %llu)\n",
1349 handle,
1350 dirfd,
1351 smb_fname->base_name,
1352 buf,
1353 llu(bufsiz));
1355 result = ceph_readlinkat(handle->data,
1356 dirfd,
1357 smb_fname->base_name,
1358 buf,
1359 bufsiz);
1361 DBG_DEBUG("[CEPH] readlinkat(...) = %d\n", result);
1362 WRAP_RETURN(result);
1363 #else
1364 struct smb_filename *full_fname = NULL;
1366 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1367 dirfsp,
1368 smb_fname);
1369 if (full_fname == NULL) {
1370 return -1;
1373 DBG_DEBUG("[CEPH] readlink(%p, %s, %p, %llu)\n", handle,
1374 full_fname->base_name, buf, llu(bufsiz));
1376 result = ceph_readlink(handle->data, full_fname->base_name, buf, bufsiz);
1377 TALLOC_FREE(full_fname);
1378 DBG_DEBUG("[CEPH] readlink(...) = %d\n", result);
1379 WRAP_RETURN(result);
1380 #endif
1383 static int cephwrap_linkat(struct vfs_handle_struct *handle,
1384 files_struct *srcfsp,
1385 const struct smb_filename *old_smb_fname,
1386 files_struct *dstfsp,
1387 const struct smb_filename *new_smb_fname,
1388 int flags)
1390 struct smb_filename *full_fname_old = NULL;
1391 struct smb_filename *full_fname_new = NULL;
1392 int result = -1;
1394 full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
1395 srcfsp,
1396 old_smb_fname);
1397 if (full_fname_old == NULL) {
1398 return -1;
1400 full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
1401 dstfsp,
1402 new_smb_fname);
1403 if (full_fname_new == NULL) {
1404 TALLOC_FREE(full_fname_old);
1405 return -1;
1408 DBG_DEBUG("[CEPH] link(%p, %s, %s)\n", handle,
1409 full_fname_old->base_name,
1410 full_fname_new->base_name);
1412 result = ceph_link(handle->data,
1413 full_fname_old->base_name,
1414 full_fname_new->base_name);
1415 DBG_DEBUG("[CEPH] link(...) = %d\n", result);
1416 TALLOC_FREE(full_fname_old);
1417 TALLOC_FREE(full_fname_new);
1418 WRAP_RETURN(result);
1421 static int cephwrap_mknodat(struct vfs_handle_struct *handle,
1422 files_struct *dirfsp,
1423 const struct smb_filename *smb_fname,
1424 mode_t mode,
1425 SMB_DEV_T dev)
1427 struct smb_filename *full_fname = NULL;
1428 int result = -1;
1430 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1431 dirfsp,
1432 smb_fname);
1433 if (full_fname == NULL) {
1434 return -1;
1437 DBG_DEBUG("[CEPH] mknodat(%p, %s)\n", handle, full_fname->base_name);
1438 result = ceph_mknod(handle->data, full_fname->base_name, mode, dev);
1439 DBG_DEBUG("[CEPH] mknodat(...) = %d\n", result);
1441 TALLOC_FREE(full_fname);
1443 WRAP_RETURN(result);
1447 * This is a simple version of real-path ... a better version is needed to
1448 * ask libcephfs about symbolic links.
1450 static struct smb_filename *cephwrap_realpath(struct vfs_handle_struct *handle,
1451 TALLOC_CTX *ctx,
1452 const struct smb_filename *smb_fname)
1454 char *result = NULL;
1455 const char *path = smb_fname->base_name;
1456 size_t len = strlen(path);
1457 struct smb_filename *result_fname = NULL;
1458 int r = -1;
1460 if (len && (path[0] == '/')) {
1461 r = asprintf(&result, "%s", path);
1462 } else if ((len >= 2) && (path[0] == '.') && (path[1] == '/')) {
1463 if (len == 2) {
1464 r = asprintf(&result, "%s",
1465 handle->conn->cwd_fsp->fsp_name->base_name);
1466 } else {
1467 r = asprintf(&result, "%s/%s",
1468 handle->conn->cwd_fsp->fsp_name->base_name, &path[2]);
1470 } else {
1471 r = asprintf(&result, "%s/%s",
1472 handle->conn->cwd_fsp->fsp_name->base_name, path);
1475 if (r < 0) {
1476 return NULL;
1479 DBG_DEBUG("[CEPH] realpath(%p, %s) = %s\n", handle, path, result);
1480 result_fname = synthetic_smb_fname(ctx,
1481 result,
1482 NULL,
1483 NULL,
1486 SAFE_FREE(result);
1487 return result_fname;
1491 static int cephwrap_fchflags(struct vfs_handle_struct *handle,
1492 struct files_struct *fsp,
1493 unsigned int flags)
1495 errno = ENOSYS;
1496 return -1;
1499 static NTSTATUS cephwrap_get_real_filename_at(
1500 struct vfs_handle_struct *handle,
1501 struct files_struct *dirfsp,
1502 const char *name,
1503 TALLOC_CTX *mem_ctx,
1504 char **found_name)
1507 * Don't fall back to get_real_filename so callers can differentiate
1508 * between a full directory scan and an actual case-insensitive stat.
1510 return NT_STATUS_NOT_SUPPORTED;
1513 static const char *cephwrap_connectpath(
1514 struct vfs_handle_struct *handle,
1515 const struct files_struct *dirfsp,
1516 const struct smb_filename *smb_fname)
1518 return handle->conn->connectpath;
1521 /****************************************************************
1522 Extended attribute operations.
1523 *****************************************************************/
1525 static ssize_t cephwrap_fgetxattr(struct vfs_handle_struct *handle,
1526 struct files_struct *fsp,
1527 const char *name,
1528 void *value,
1529 size_t size)
1531 int ret;
1532 DBG_DEBUG("[CEPH] fgetxattr(%p, %p, %s, %p, %llu)\n",
1533 handle,
1534 fsp,
1535 name,
1536 value,
1537 llu(size));
1538 if (!fsp->fsp_flags.is_pathref) {
1539 ret = ceph_fgetxattr(handle->data,
1540 fsp_get_io_fd(fsp),
1541 name,
1542 value,
1543 size);
1544 } else {
1545 ret = ceph_getxattr(handle->data,
1546 fsp->fsp_name->base_name,
1547 name,
1548 value,
1549 size);
1551 DBG_DEBUG("[CEPH] fgetxattr(...) = %d\n", ret);
1552 if (ret < 0) {
1553 WRAP_RETURN(ret);
1555 return (ssize_t)ret;
1558 static ssize_t cephwrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
1560 int ret;
1561 DBG_DEBUG("[CEPH] flistxattr(%p, %p, %p, %llu)\n",
1562 handle, fsp, list, llu(size));
1563 if (!fsp->fsp_flags.is_pathref) {
1565 * We can use an io_fd to list xattrs.
1567 ret = ceph_flistxattr(handle->data,
1568 fsp_get_io_fd(fsp),
1569 list,
1570 size);
1571 } else {
1573 * This is no longer a handle based call.
1575 ret = ceph_listxattr(handle->data,
1576 fsp->fsp_name->base_name,
1577 list,
1578 size);
1580 DBG_DEBUG("[CEPH] flistxattr(...) = %d\n", ret);
1581 if (ret < 0) {
1582 WRAP_RETURN(ret);
1584 return (ssize_t)ret;
1587 static int cephwrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
1589 int ret;
1590 DBG_DEBUG("[CEPH] fremovexattr(%p, %p, %s)\n", handle, fsp, name);
1591 if (!fsp->fsp_flags.is_pathref) {
1593 * We can use an io_fd to remove xattrs.
1595 ret = ceph_fremovexattr(handle->data, fsp_get_io_fd(fsp), name);
1596 } else {
1598 * This is no longer a handle based call.
1600 ret = ceph_removexattr(handle->data,
1601 fsp->fsp_name->base_name,
1602 name);
1604 DBG_DEBUG("[CEPH] fremovexattr(...) = %d\n", ret);
1605 WRAP_RETURN(ret);
1608 static int cephwrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
1610 int ret;
1611 DBG_DEBUG("[CEPH] fsetxattr(%p, %p, %s, %p, %llu, %d)\n", handle, fsp, name, value, llu(size), flags);
1612 if (!fsp->fsp_flags.is_pathref) {
1614 * We can use an io_fd to set xattrs.
1616 ret = ceph_fsetxattr(handle->data,
1617 fsp_get_io_fd(fsp),
1618 name,
1619 value,
1620 size,
1621 flags);
1622 } else {
1624 * This is no longer a handle based call.
1626 ret = ceph_setxattr(handle->data,
1627 fsp->fsp_name->base_name,
1628 name,
1629 value,
1630 size,
1631 flags);
1633 DBG_DEBUG("[CEPH] fsetxattr(...) = %d\n", ret);
1634 WRAP_RETURN(ret);
1637 static bool cephwrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
1641 * We do not support AIO yet.
1644 DBG_DEBUG("[CEPH] cephwrap_aio_force(%p, %p) = false (errno = ENOTSUP)\n", handle, fsp);
1645 errno = ENOTSUP;
1646 return false;
1649 static NTSTATUS cephwrap_create_dfs_pathat(struct vfs_handle_struct *handle,
1650 struct files_struct *dirfsp,
1651 const struct smb_filename *smb_fname,
1652 const struct referral *reflist,
1653 size_t referral_count)
1655 TALLOC_CTX *frame = talloc_stackframe();
1656 NTSTATUS status = NT_STATUS_NO_MEMORY;
1657 int ret;
1658 char *msdfs_link = NULL;
1659 struct smb_filename *full_fname = NULL;
1661 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1662 dirfsp,
1663 smb_fname);
1664 if (full_fname == NULL) {
1665 goto out;
1668 /* Form the msdfs_link contents */
1669 msdfs_link = msdfs_link_string(frame,
1670 reflist,
1671 referral_count);
1672 if (msdfs_link == NULL) {
1673 goto out;
1676 ret = ceph_symlink(handle->data,
1677 msdfs_link,
1678 full_fname->base_name);
1679 if (ret == 0) {
1680 status = NT_STATUS_OK;
1681 } else {
1682 status = map_nt_error_from_unix(-ret);
1685 out:
1687 DBG_DEBUG("[CEPH] create_dfs_pathat(%s) = %s\n",
1688 full_fname != NULL ? full_fname->base_name : "",
1689 nt_errstr(status));
1691 TALLOC_FREE(frame);
1692 return status;
1696 * Read and return the contents of a DFS redirect given a
1697 * pathname. A caller can pass in NULL for ppreflist and
1698 * preferral_count but still determine if this was a
1699 * DFS redirect point by getting NT_STATUS_OK back
1700 * without incurring the overhead of reading and parsing
1701 * the referral contents.
1704 static NTSTATUS cephwrap_read_dfs_pathat(struct vfs_handle_struct *handle,
1705 TALLOC_CTX *mem_ctx,
1706 struct files_struct *dirfsp,
1707 struct smb_filename *smb_fname,
1708 struct referral **ppreflist,
1709 size_t *preferral_count)
1711 NTSTATUS status = NT_STATUS_NO_MEMORY;
1712 size_t bufsize;
1713 char *link_target = NULL;
1714 int referral_len;
1715 bool ok;
1716 #if defined(HAVE_BROKEN_READLINK)
1717 char link_target_buf[PATH_MAX];
1718 #else
1719 char link_target_buf[7];
1720 #endif
1721 struct ceph_statx stx = { 0 };
1722 struct smb_filename *full_fname = NULL;
1723 int ret;
1725 if (is_named_stream(smb_fname)) {
1726 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1727 goto err;
1730 if (ppreflist == NULL && preferral_count == NULL) {
1732 * We're only checking if this is a DFS
1733 * redirect. We don't need to return data.
1735 bufsize = sizeof(link_target_buf);
1736 link_target = link_target_buf;
1737 } else {
1738 bufsize = PATH_MAX;
1739 link_target = talloc_array(mem_ctx, char, bufsize);
1740 if (!link_target) {
1741 goto err;
1745 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1746 dirfsp,
1747 smb_fname);
1748 if (full_fname == NULL) {
1749 status = NT_STATUS_NO_MEMORY;
1750 goto err;
1753 ret = ceph_statx(handle->data,
1754 full_fname->base_name,
1755 &stx,
1756 SAMBA_STATX_ATTR_MASK,
1757 AT_SYMLINK_NOFOLLOW);
1758 if (ret < 0) {
1759 status = map_nt_error_from_unix(-ret);
1760 goto err;
1763 referral_len = ceph_readlink(handle->data,
1764 full_fname->base_name,
1765 link_target,
1766 bufsize - 1);
1767 if (referral_len < 0) {
1768 /* ceph errors are -errno. */
1769 if (-referral_len == EINVAL) {
1770 DBG_INFO("%s is not a link.\n",
1771 full_fname->base_name);
1772 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1773 } else {
1774 status = map_nt_error_from_unix(-referral_len);
1775 DBG_ERR("Error reading "
1776 "msdfs link %s: %s\n",
1777 full_fname->base_name,
1778 strerror(errno));
1780 goto err;
1782 link_target[referral_len] = '\0';
1784 DBG_INFO("%s -> %s\n",
1785 full_fname->base_name,
1786 link_target);
1788 if (!strnequal(link_target, "msdfs:", 6)) {
1789 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1790 goto err;
1793 if (ppreflist == NULL && preferral_count == NULL) {
1794 /* Early return for checking if this is a DFS link. */
1795 TALLOC_FREE(full_fname);
1796 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1797 return NT_STATUS_OK;
1800 ok = parse_msdfs_symlink(mem_ctx,
1801 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
1802 link_target,
1803 ppreflist,
1804 preferral_count);
1806 if (ok) {
1807 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1808 status = NT_STATUS_OK;
1809 } else {
1810 status = NT_STATUS_NO_MEMORY;
1813 err:
1815 if (link_target != link_target_buf) {
1816 TALLOC_FREE(link_target);
1818 TALLOC_FREE(full_fname);
1819 return status;
1822 static struct vfs_fn_pointers ceph_fns = {
1823 /* Disk operations */
1825 .connect_fn = cephwrap_connect,
1826 .disconnect_fn = cephwrap_disconnect,
1827 .disk_free_fn = cephwrap_disk_free,
1828 .get_quota_fn = cephwrap_get_quota,
1829 .set_quota_fn = cephwrap_set_quota,
1830 .statvfs_fn = cephwrap_statvfs,
1831 .fs_capabilities_fn = cephwrap_fs_capabilities,
1833 /* Directory operations */
1835 .fdopendir_fn = cephwrap_fdopendir,
1836 .readdir_fn = cephwrap_readdir,
1837 .rewind_dir_fn = cephwrap_rewinddir,
1838 .mkdirat_fn = cephwrap_mkdirat,
1839 .closedir_fn = cephwrap_closedir,
1841 /* File operations */
1843 .create_dfs_pathat_fn = cephwrap_create_dfs_pathat,
1844 .read_dfs_pathat_fn = cephwrap_read_dfs_pathat,
1845 .openat_fn = cephwrap_openat,
1846 .close_fn = cephwrap_close,
1847 .pread_fn = cephwrap_pread,
1848 .pread_send_fn = cephwrap_pread_send,
1849 .pread_recv_fn = cephwrap_pread_recv,
1850 .pwrite_fn = cephwrap_pwrite,
1851 .pwrite_send_fn = cephwrap_pwrite_send,
1852 .pwrite_recv_fn = cephwrap_pwrite_recv,
1853 .lseek_fn = cephwrap_lseek,
1854 .sendfile_fn = cephwrap_sendfile,
1855 .recvfile_fn = cephwrap_recvfile,
1856 .renameat_fn = cephwrap_renameat,
1857 .fsync_send_fn = cephwrap_fsync_send,
1858 .fsync_recv_fn = cephwrap_fsync_recv,
1859 .stat_fn = cephwrap_stat,
1860 .fstat_fn = cephwrap_fstat,
1861 .lstat_fn = cephwrap_lstat,
1862 .unlinkat_fn = cephwrap_unlinkat,
1863 .fchmod_fn = cephwrap_fchmod,
1864 .fchown_fn = cephwrap_fchown,
1865 .lchown_fn = cephwrap_lchown,
1866 .chdir_fn = cephwrap_chdir,
1867 .getwd_fn = cephwrap_getwd,
1868 .fntimes_fn = cephwrap_fntimes,
1869 .ftruncate_fn = cephwrap_ftruncate,
1870 .fallocate_fn = cephwrap_fallocate,
1871 .lock_fn = cephwrap_lock,
1872 .filesystem_sharemode_fn = cephwrap_filesystem_sharemode,
1873 .fcntl_fn = cephwrap_fcntl,
1874 .linux_setlease_fn = cephwrap_linux_setlease,
1875 .getlock_fn = cephwrap_getlock,
1876 .symlinkat_fn = cephwrap_symlinkat,
1877 .readlinkat_fn = cephwrap_readlinkat,
1878 .linkat_fn = cephwrap_linkat,
1879 .mknodat_fn = cephwrap_mknodat,
1880 .realpath_fn = cephwrap_realpath,
1881 .fchflags_fn = cephwrap_fchflags,
1882 .get_real_filename_at_fn = cephwrap_get_real_filename_at,
1883 .connectpath_fn = cephwrap_connectpath,
1885 /* EA operations. */
1886 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1887 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1888 .fgetxattr_fn = cephwrap_fgetxattr,
1889 .flistxattr_fn = cephwrap_flistxattr,
1890 .fremovexattr_fn = cephwrap_fremovexattr,
1891 .fsetxattr_fn = cephwrap_fsetxattr,
1893 /* Posix ACL Operations */
1894 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1895 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1896 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1897 .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
1899 /* aio operations */
1900 .aio_force_fn = cephwrap_aio_force,
1903 static_decl_vfs;
1904 NTSTATUS vfs_ceph_init(TALLOC_CTX *ctx)
1906 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1907 "ceph", &ceph_fns);