utils: Fix up 14a533680245
[samba4-gss.git] / source3 / modules / vfs_ceph.c
blob26b51ac78c583381b1901d1eae95abb97b256ea5
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
53 * unsigned)
55 #define llu(_var) ((long long unsigned)_var)
58 * Note, libcephfs's return code model is to return -errno. Thus we have to
59 * convert to what Samba expects: set errno to non-negative value and return -1.
61 * Using convenience helper functions to avoid non-hygienic macro.
63 static inline int status_code(int ret)
65 if (ret < 0) {
66 errno = -ret;
67 return -1;
69 return ret;
72 static inline ssize_t lstatus_code(intmax_t ret)
74 if (ret < 0) {
75 errno = -((int)ret);
76 return -1;
78 return (ssize_t)ret;
82 * Track unique connections, as virtual mounts, to cephfs file systems.
83 * Individual mounts will be set on the handle->data attribute, but
84 * the mounts themselves will be shared so as not to spawn extra mounts
85 * to the same cephfs.
87 * Individual mounts are IDed by a 'cookie' value that is a string built
88 * from identifying parameters found in smb.conf.
91 static struct cephmount_cached {
92 char *cookie;
93 uint32_t count;
94 struct ceph_mount_info *mount;
95 struct cephmount_cached *next, *prev;
96 } *cephmount_cached;
98 static int cephmount_cache_add(const char *cookie,
99 struct ceph_mount_info *mount)
101 struct cephmount_cached *entry = NULL;
103 entry = talloc_zero(NULL, struct cephmount_cached);
104 if (entry == NULL) {
105 errno = ENOMEM;
106 return -1;
109 entry->cookie = talloc_strdup(entry, cookie);
110 if (entry->cookie == NULL) {
111 talloc_free(entry);
112 errno = ENOMEM;
113 return -1;
116 entry->mount = mount;
117 entry->count = 1;
119 DBG_DEBUG("adding mount cache entry for %s\n", entry->cookie);
120 DLIST_ADD(cephmount_cached, entry);
121 return 0;
124 static struct ceph_mount_info *cephmount_cache_update(const char *cookie)
126 struct cephmount_cached *entry = NULL;
128 for (entry = cephmount_cached; entry; entry = entry->next) {
129 if (strcmp(entry->cookie, cookie) == 0) {
130 entry->count++;
131 DBG_DEBUG("updated mount cache: count is [%"
132 PRIu32 "]\n", entry->count);
133 return entry->mount;
137 errno = ENOENT;
138 return NULL;
141 static int cephmount_cache_remove(struct ceph_mount_info *mount)
143 struct cephmount_cached *entry = NULL;
145 for (entry = cephmount_cached; entry; entry = entry->next) {
146 if (entry->mount == mount) {
147 if (--entry->count) {
148 DBG_DEBUG("updated mount cache: count is [%"
149 PRIu32 "]\n", entry->count);
150 return entry->count;
153 DBG_DEBUG("removing mount cache entry for %s\n",
154 entry->cookie);
155 DLIST_REMOVE(cephmount_cached, entry);
156 talloc_free(entry);
157 return 0;
160 errno = ENOENT;
161 return -1;
164 static char *cephmount_get_cookie(TALLOC_CTX * mem_ctx, const int snum)
166 const char *conf_file =
167 lp_parm_const_string(snum, "ceph", "config_file", ".");
168 const char *user_id = lp_parm_const_string(snum, "ceph", "user_id", "");
169 const char *fsname =
170 lp_parm_const_string(snum, "ceph", "filesystem", "");
171 return talloc_asprintf(mem_ctx, "(%s/%s/%s)", conf_file, user_id,
172 fsname);
175 static struct ceph_mount_info *cephmount_mount_fs(const int snum)
177 int ret;
178 char buf[256];
179 struct ceph_mount_info *mnt = NULL;
180 /* if config_file and/or user_id are NULL, ceph will use defaults */
181 const char *conf_file =
182 lp_parm_const_string(snum, "ceph", "config_file", NULL);
183 const char *user_id =
184 lp_parm_const_string(snum, "ceph", "user_id", NULL);
185 const char *fsname =
186 lp_parm_const_string(snum, "ceph", "filesystem", NULL);
188 DBG_DEBUG("[CEPH] calling: ceph_create\n");
189 ret = ceph_create(&mnt, user_id);
190 if (ret) {
191 errno = -ret;
192 return NULL;
195 DBG_DEBUG("[CEPH] calling: ceph_conf_read_file with %s\n",
196 (conf_file == NULL ? "default path" : conf_file));
197 ret = ceph_conf_read_file(mnt, conf_file);
198 if (ret) {
199 goto err_cm_release;
202 DBG_DEBUG("[CEPH] calling: ceph_conf_get\n");
203 ret = ceph_conf_get(mnt, "log file", buf, sizeof(buf));
204 if (ret < 0) {
205 goto err_cm_release;
208 /* libcephfs disables POSIX ACL support by default, enable it... */
209 ret = ceph_conf_set(mnt, "client_acl_type", "posix_acl");
210 if (ret < 0) {
211 goto err_cm_release;
213 /* tell libcephfs to perform local permission checks */
214 ret = ceph_conf_set(mnt, "fuse_default_permissions", "false");
215 if (ret < 0) {
216 goto err_cm_release;
219 * select a cephfs file system to use:
220 * In ceph, multiple file system support has been stable since
221 * 'pacific'. Permit different shares to access different file systems.
223 if (fsname != NULL) {
224 ret = ceph_select_filesystem(mnt, fsname);
225 if (ret < 0) {
226 goto err_cm_release;
230 DBG_DEBUG("[CEPH] calling: ceph_mount\n");
231 ret = ceph_mount(mnt, NULL);
232 if (ret >= 0) {
233 goto cm_done;
236 err_cm_release:
237 ceph_release(mnt);
238 mnt = NULL;
239 DBG_DEBUG("[CEPH] Error mounting fs: %s\n", strerror(-ret));
240 cm_done:
242 * Handle the error correctly. Ceph returns -errno.
244 if (ret) {
245 errno = -ret;
247 return mnt;
250 /* Check for NULL pointer parameters in cephwrap_* functions */
252 /* We don't want to have NULL function pointers lying around. Someone
253 is sure to try and execute them. These stubs are used to prevent
254 this possibility. */
256 static int cephwrap_connect(struct vfs_handle_struct *handle,
257 const char *service, const char *user)
259 int ret = 0;
260 struct ceph_mount_info *cmount = NULL;
261 int snum = SNUM(handle->conn);
262 char *cookie = cephmount_get_cookie(handle, snum);
263 if (cookie == NULL) {
264 return -1;
267 cmount = cephmount_cache_update(cookie);
268 if (cmount != NULL) {
269 goto connect_ok;
272 cmount = cephmount_mount_fs(snum);
273 if (cmount == NULL) {
274 ret = -1;
275 goto connect_fail;
277 ret = cephmount_cache_add(cookie, cmount);
278 if (ret) {
279 goto connect_fail;
282 connect_ok:
283 handle->data = cmount;
284 DBG_WARNING("Connection established with the server: %s\n", cookie);
286 * Unless we have an async implementation of getxattrat turn this off.
288 lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
289 connect_fail:
290 talloc_free(cookie);
291 return ret;
294 static void cephwrap_disconnect(struct vfs_handle_struct *handle)
296 int ret = cephmount_cache_remove(handle->data);
297 if (ret < 0) {
298 DBG_ERR("failed to remove ceph mount from cache: %s\n",
299 strerror(errno));
300 return;
302 if (ret > 0) {
303 DBG_DEBUG("mount cache entry still in use\n");
304 return;
307 ret = ceph_unmount(handle->data);
308 if (ret < 0) {
309 DBG_ERR("[CEPH] failed to unmount: %s\n", strerror(-ret));
312 ret = ceph_release(handle->data);
313 if (ret < 0) {
314 DBG_ERR("[CEPH] failed to release: %s\n", strerror(-ret));
316 handle->data = NULL;
319 /* Disk operations */
321 static uint64_t cephwrap_disk_free(struct vfs_handle_struct *handle,
322 const struct smb_filename *smb_fname,
323 uint64_t *bsize,
324 uint64_t *dfree,
325 uint64_t *dsize)
327 struct statvfs statvfs_buf = { 0 };
328 int ret;
330 ret = ceph_statfs(handle->data, smb_fname->base_name, &statvfs_buf);
331 if (ret < 0) {
332 DBG_DEBUG("[CEPH] ceph_statfs returned %d\n", ret);
333 return (uint64_t)status_code(ret);
336 * Provide all the correct values.
338 *bsize = statvfs_buf.f_bsize;
339 *dfree = statvfs_buf.f_bavail;
340 *dsize = statvfs_buf.f_blocks;
341 DBG_DEBUG("[CEPH] bsize: %llu, dfree: %llu, dsize: %llu\n", llu(*bsize),
342 llu(*dfree), llu(*dsize));
343 return *dfree;
346 static int cephwrap_statvfs(struct vfs_handle_struct *handle,
347 const struct smb_filename *smb_fname,
348 struct vfs_statvfs_struct *statbuf)
350 struct statvfs statvfs_buf = { 0 };
351 int ret;
353 ret = ceph_statfs(handle->data, smb_fname->base_name, &statvfs_buf);
354 if (ret < 0) {
355 return status_code(ret);
358 statbuf->OptimalTransferSize = statvfs_buf.f_frsize;
359 statbuf->BlockSize = statvfs_buf.f_bsize;
360 statbuf->TotalBlocks = statvfs_buf.f_blocks;
361 statbuf->BlocksAvail = statvfs_buf.f_bfree;
362 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
363 statbuf->TotalFileNodes = statvfs_buf.f_files;
364 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
365 statbuf->FsIdentifier = statvfs_buf.f_fsid;
366 DBG_DEBUG("[CEPH] f_bsize: %ld, f_blocks: %ld, f_bfree: %ld, "
367 "f_bavail: %ld\n",
368 (long int)statvfs_buf.f_bsize,
369 (long int)statvfs_buf.f_blocks,
370 (long int)statvfs_buf.f_bfree,
371 (long int)statvfs_buf.f_bavail);
373 return ret;
376 static uint32_t cephwrap_fs_capabilities(
377 struct vfs_handle_struct *handle,
378 enum timestamp_set_resolution *p_ts_res)
380 uint32_t caps;
382 caps = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
383 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
385 return caps;
388 /* Directory operations */
390 static DIR *cephwrap_fdopendir(struct vfs_handle_struct *handle,
391 struct files_struct *fsp,
392 const char *mask,
393 uint32_t attributes)
395 int ret = 0;
396 struct ceph_dir_result *result = NULL;
398 int dirfd = fsp_get_io_fd(fsp);
399 DBG_DEBUG("[CEPH] fdopendir(%p, %d)\n", handle, dirfd);
400 ret = ceph_fdopendir(handle->data, dirfd, &result);
401 if (ret < 0) {
402 result = NULL;
403 errno = -ret; /* We return result which is NULL in this case */
406 DBG_DEBUG("[CEPH] fdopendir(...) = %d\n", ret);
407 return (DIR *) result;
410 static struct dirent *cephwrap_readdir(struct vfs_handle_struct *handle,
411 struct files_struct *dirfsp,
412 DIR *dirp)
414 struct dirent *result = NULL;
416 DBG_DEBUG("[CEPH] readdir(%p, %p)\n", handle, dirp);
417 result = ceph_readdir(handle->data, (struct ceph_dir_result *) dirp);
418 DBG_DEBUG("[CEPH] readdir(...) = %p\n", result);
420 return result;
423 static void cephwrap_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
425 DBG_DEBUG("[CEPH] rewinddir(%p, %p)\n", handle, dirp);
426 ceph_rewinddir(handle->data, (struct ceph_dir_result *) dirp);
429 static int cephwrap_mkdirat(struct vfs_handle_struct *handle,
430 files_struct *dirfsp,
431 const struct smb_filename *smb_fname,
432 mode_t mode)
434 int result = -1;
435 int dirfd = fsp_get_pathref_fd(dirfsp);
437 DBG_DEBUG("[CEPH] mkdirat(%p, %d, %s)\n",
438 handle,
439 dirfd,
440 smb_fname->base_name);
442 result = ceph_mkdirat(handle->data, dirfd, smb_fname->base_name, mode);
444 DBG_DEBUG("[CEPH] mkdirat(...) = %d\n", result);
446 return status_code(result);
449 static int cephwrap_closedir(struct vfs_handle_struct *handle, DIR *dirp)
451 int result;
453 DBG_DEBUG("[CEPH] closedir(%p, %p)\n", handle, dirp);
454 result = ceph_closedir(handle->data, (struct ceph_dir_result *) dirp);
455 DBG_DEBUG("[CEPH] closedir(...) = %d\n", result);
456 return status_code(result);
459 /* File operations */
461 static int cephwrap_openat(struct vfs_handle_struct *handle,
462 const struct files_struct *dirfsp,
463 const struct smb_filename *smb_fname,
464 files_struct *fsp,
465 const struct vfs_open_how *how)
467 int flags = how->flags;
468 mode_t mode = how->mode;
469 struct smb_filename *name = NULL;
470 bool have_opath = false;
471 bool became_root = false;
472 int result = -ENOENT;
473 int dirfd = -1;
475 if (how->resolve != 0) {
476 errno = ENOSYS;
477 return -1;
480 if (smb_fname->stream_name) {
481 goto out;
484 #ifdef O_PATH
485 have_opath = true;
486 if (fsp->fsp_flags.is_pathref) {
487 flags |= O_PATH;
489 #endif
491 dirfd = fsp_get_pathref_fd(dirfsp);
493 DBG_DEBUG("[CEPH] openat(%p, %d, %p, %d, %d)\n",
494 handle, dirfd, fsp, flags, mode);
496 if (fsp->fsp_flags.is_pathref && !have_opath) {
497 become_root();
498 became_root = true;
501 result = ceph_openat(handle->data,
502 dirfd,
503 smb_fname->base_name,
504 flags,
505 mode);
507 if (became_root) {
508 unbecome_root();
511 out:
512 TALLOC_FREE(name);
513 fsp->fsp_flags.have_proc_fds = false;
514 DBG_DEBUG("[CEPH] open(...) = %d\n", result);
515 return status_code(result);
518 static int cephwrap_close(struct vfs_handle_struct *handle, files_struct *fsp)
520 int result;
522 DBG_DEBUG("[CEPH] close(%p, %p)\n", handle, fsp);
523 result = ceph_close(handle->data, fsp_get_pathref_fd(fsp));
524 DBG_DEBUG("[CEPH] close(...) = %d\n", result);
525 return status_code(result);
528 static ssize_t cephwrap_pread(struct vfs_handle_struct *handle,
529 files_struct *fsp,
530 void *data,
531 size_t n,
532 off_t offset)
534 ssize_t result;
536 DBG_DEBUG("[CEPH] pread(%p, %p, %p, %llu, %llu)\n",
537 handle,
538 fsp,
539 data,
540 llu(n),
541 llu(offset));
543 result = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
544 DBG_DEBUG("[CEPH] pread(...) = %llu\n", llu(result));
545 return lstatus_code(result);
548 struct cephwrap_pread_state {
549 ssize_t bytes_read;
550 struct vfs_aio_state vfs_aio_state;
554 * Fake up an async ceph read by calling the synchronous API.
556 static struct tevent_req *cephwrap_pread_send(struct vfs_handle_struct *handle,
557 TALLOC_CTX *mem_ctx,
558 struct tevent_context *ev,
559 struct files_struct *fsp,
560 void *data,
561 size_t n, off_t offset)
563 struct tevent_req *req = NULL;
564 struct cephwrap_pread_state *state = NULL;
565 int ret = -1;
567 DBG_DEBUG("[CEPH] %s\n", __func__);
568 req = tevent_req_create(mem_ctx, &state, struct cephwrap_pread_state);
569 if (req == NULL) {
570 return NULL;
573 ret = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
574 if (ret < 0) {
575 /* ceph returns -errno on error. */
576 tevent_req_error(req, -ret);
577 return tevent_req_post(req, ev);
580 state->bytes_read = ret;
581 tevent_req_done(req);
582 /* Return and schedule the completion of the call. */
583 return tevent_req_post(req, ev);
586 static ssize_t cephwrap_pread_recv(struct tevent_req *req,
587 struct vfs_aio_state *vfs_aio_state)
589 struct cephwrap_pread_state *state =
590 tevent_req_data(req, struct cephwrap_pread_state);
592 DBG_DEBUG("[CEPH] %s\n", __func__);
593 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
594 return -1;
596 *vfs_aio_state = state->vfs_aio_state;
597 return state->bytes_read;
600 static ssize_t cephwrap_pwrite(struct vfs_handle_struct *handle,
601 files_struct *fsp,
602 const void *data,
603 size_t n,
604 off_t offset)
606 ssize_t result;
608 DBG_DEBUG("[CEPH] pwrite(%p, %p, %p, %llu, %llu)\n",
609 handle,
610 fsp,
611 data,
612 llu(n),
613 llu(offset));
614 result = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
615 DBG_DEBUG("[CEPH] pwrite(...) = %llu\n", llu(result));
616 return lstatus_code(result);
619 struct cephwrap_pwrite_state {
620 ssize_t bytes_written;
621 struct vfs_aio_state vfs_aio_state;
625 * Fake up an async ceph write by calling the synchronous API.
627 static struct tevent_req *cephwrap_pwrite_send(struct vfs_handle_struct *handle,
628 TALLOC_CTX *mem_ctx,
629 struct tevent_context *ev,
630 struct files_struct *fsp,
631 const void *data,
632 size_t n, off_t offset)
634 struct tevent_req *req = NULL;
635 struct cephwrap_pwrite_state *state = NULL;
636 int ret = -1;
638 DBG_DEBUG("[CEPH] %s\n", __func__);
639 req = tevent_req_create(mem_ctx, &state, struct cephwrap_pwrite_state);
640 if (req == NULL) {
641 return NULL;
644 ret = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
645 if (ret < 0) {
646 /* ceph returns -errno on error. */
647 tevent_req_error(req, -ret);
648 return tevent_req_post(req, ev);
651 state->bytes_written = ret;
652 tevent_req_done(req);
653 /* Return and schedule the completion of the call. */
654 return tevent_req_post(req, ev);
657 static ssize_t cephwrap_pwrite_recv(struct tevent_req *req,
658 struct vfs_aio_state *vfs_aio_state)
660 struct cephwrap_pwrite_state *state =
661 tevent_req_data(req, struct cephwrap_pwrite_state);
663 DBG_DEBUG("[CEPH] %s\n", __func__);
664 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
665 return -1;
667 *vfs_aio_state = state->vfs_aio_state;
668 return state->bytes_written;
671 static off_t cephwrap_lseek(struct vfs_handle_struct *handle,
672 files_struct *fsp,
673 off_t offset,
674 int whence)
676 off_t result = 0;
678 DBG_DEBUG("[CEPH] cephwrap_lseek\n");
679 result = ceph_lseek(handle->data, fsp_get_io_fd(fsp), offset, whence);
680 return lstatus_code(result);
683 static ssize_t cephwrap_sendfile(struct vfs_handle_struct *handle,
684 int tofd,
685 files_struct *fromfsp,
686 const DATA_BLOB *hdr,
687 off_t offset,
688 size_t n)
691 * We cannot support sendfile because libcephfs is in user space.
693 DBG_DEBUG("[CEPH] cephwrap_sendfile\n");
694 errno = ENOTSUP;
695 return -1;
698 static ssize_t cephwrap_recvfile(struct vfs_handle_struct *handle,
699 int fromfd,
700 files_struct *tofsp,
701 off_t offset,
702 size_t n)
705 * We cannot support recvfile because libcephfs is in user space.
707 DBG_DEBUG("[CEPH] cephwrap_recvfile\n");
708 errno = ENOTSUP;
709 return -1;
712 static int cephwrap_renameat(struct vfs_handle_struct *handle,
713 files_struct *srcfsp,
714 const struct smb_filename *smb_fname_src,
715 files_struct *dstfsp,
716 const struct smb_filename *smb_fname_dst,
717 const struct vfs_rename_how *how)
719 struct smb_filename *full_fname_src = NULL;
720 struct smb_filename *full_fname_dst = NULL;
721 int result = -1;
723 DBG_DEBUG("[CEPH] cephwrap_renameat\n");
724 if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
725 errno = ENOENT;
726 return result;
729 if (how->flags != 0) {
730 errno = EINVAL;
731 return -1;
734 full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
735 srcfsp,
736 smb_fname_src);
737 if (full_fname_src == NULL) {
738 errno = ENOMEM;
739 return -1;
741 full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
742 dstfsp,
743 smb_fname_dst);
744 if (full_fname_dst == NULL) {
745 TALLOC_FREE(full_fname_src);
746 errno = ENOMEM;
747 return -1;
750 result = ceph_rename(handle->data,
751 full_fname_src->base_name,
752 full_fname_dst->base_name);
754 TALLOC_FREE(full_fname_src);
755 TALLOC_FREE(full_fname_dst);
757 return status_code(result);
761 * Fake up an async ceph fsync by calling the synchronous API.
764 static struct tevent_req *cephwrap_fsync_send(struct vfs_handle_struct *handle,
765 TALLOC_CTX *mem_ctx,
766 struct tevent_context *ev,
767 files_struct *fsp)
769 struct tevent_req *req = NULL;
770 struct vfs_aio_state *state = NULL;
771 int ret = -1;
773 DBG_DEBUG("[CEPH] cephwrap_fsync_send\n");
775 req = tevent_req_create(mem_ctx, &state, struct vfs_aio_state);
776 if (req == NULL) {
777 return NULL;
780 /* Make sync call. */
781 ret = ceph_fsync(handle->data, fsp_get_io_fd(fsp), false);
783 if (ret != 0) {
784 /* ceph_fsync returns -errno on error. */
785 tevent_req_error(req, -ret);
786 return tevent_req_post(req, ev);
789 /* Mark it as done. */
790 tevent_req_done(req);
791 /* Return and schedule the completion of the call. */
792 return tevent_req_post(req, ev);
795 static int cephwrap_fsync_recv(struct tevent_req *req,
796 struct vfs_aio_state *vfs_aio_state)
798 struct vfs_aio_state *state =
799 tevent_req_data(req, struct vfs_aio_state);
801 DBG_DEBUG("[CEPH] cephwrap_fsync_recv\n");
803 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
804 return -1;
806 *vfs_aio_state = *state;
807 return 0;
810 #define SAMBA_STATX_ATTR_MASK (CEPH_STATX_BASIC_STATS|CEPH_STATX_BTIME)
812 static void init_stat_ex_from_ceph_statx(struct stat_ex *dst,
813 const struct ceph_statx *stx)
815 DBG_DEBUG("[CEPH]\tstx = {dev = %llx, ino = %llu, mode = 0x%x, "
816 "nlink = %llu, uid = %d, gid = %d, rdev = %llx, size = %llu, "
817 "blksize = %llu, blocks = %llu, atime = %llu, mtime = %llu, "
818 "ctime = %llu, btime = %llu}\n",
819 llu(stx->stx_dev), llu(stx->stx_ino), stx->stx_mode,
820 llu(stx->stx_nlink), stx->stx_uid, stx->stx_gid,
821 llu(stx->stx_rdev), llu(stx->stx_size), llu(stx->stx_blksize),
822 llu(stx->stx_blocks), llu(stx->stx_atime.tv_sec),
823 llu(stx->stx_mtime.tv_sec), llu(stx->stx_ctime.tv_sec),
824 llu(stx->stx_btime.tv_sec));
826 if ((stx->stx_mask & SAMBA_STATX_ATTR_MASK) != SAMBA_STATX_ATTR_MASK) {
827 DBG_WARNING("%s: stx->stx_mask is incorrect "
828 "(wanted %x, got %x)\n",
829 __func__,
830 SAMBA_STATX_ATTR_MASK,
831 stx->stx_mask);
834 dst->st_ex_dev = stx->stx_dev;
835 dst->st_ex_rdev = stx->stx_rdev;
836 dst->st_ex_ino = stx->stx_ino;
837 dst->st_ex_mode = stx->stx_mode;
838 dst->st_ex_uid = stx->stx_uid;
839 dst->st_ex_gid = stx->stx_gid;
840 dst->st_ex_size = stx->stx_size;
841 dst->st_ex_nlink = stx->stx_nlink;
842 dst->st_ex_atime = stx->stx_atime;
843 dst->st_ex_btime = stx->stx_btime;
844 dst->st_ex_ctime = stx->stx_ctime;
845 dst->st_ex_mtime = stx->stx_mtime;
846 dst->st_ex_blksize = stx->stx_blksize;
847 dst->st_ex_blocks = stx->stx_blocks;
850 static int cephwrap_stat(struct vfs_handle_struct *handle,
851 struct smb_filename *smb_fname)
853 int result = -1;
854 struct ceph_statx stx = { 0 };
856 DBG_DEBUG("[CEPH] stat(%p, %s)\n",
857 handle,
858 smb_fname_str_dbg(smb_fname));
860 if (smb_fname->stream_name) {
861 errno = ENOENT;
862 return result;
865 result = ceph_statx(handle->data, smb_fname->base_name, &stx,
866 SAMBA_STATX_ATTR_MASK, 0);
867 DBG_DEBUG("[CEPH] statx(...) = %d\n", result);
868 if (result < 0) {
869 return status_code(result);
872 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
873 DBG_DEBUG("[CEPH] mode = 0x%x\n", smb_fname->st.st_ex_mode);
874 return result;
877 static int cephwrap_fstat(struct vfs_handle_struct *handle,
878 files_struct *fsp,
879 SMB_STRUCT_STAT *sbuf)
881 int result = -1;
882 struct ceph_statx stx = { 0 };
883 int fd = fsp_get_pathref_fd(fsp);
885 DBG_DEBUG("[CEPH] fstat(%p, %d)\n", handle, fd);
886 result = ceph_fstatx(handle->data, fd, &stx,
887 SAMBA_STATX_ATTR_MASK, 0);
888 DBG_DEBUG("[CEPH] fstat(...) = %d\n", result);
889 if (result < 0) {
890 return status_code(result);
893 init_stat_ex_from_ceph_statx(sbuf, &stx);
894 DBG_DEBUG("[CEPH] mode = 0x%x\n", sbuf->st_ex_mode);
895 return result;
898 static int cephwrap_fstatat(struct vfs_handle_struct *handle,
899 const struct files_struct *dirfsp,
900 const struct smb_filename *smb_fname,
901 SMB_STRUCT_STAT *sbuf,
902 int flags)
904 int result = -1;
905 struct ceph_statx stx = { 0 };
906 int dirfd = fsp_get_pathref_fd(dirfsp);
908 DBG_DEBUG("[CEPH] fstatat(%p, %d, %s)\n",
909 handle, dirfd, smb_fname->base_name);
910 result = ceph_statxat(handle->data, dirfd, smb_fname->base_name,
911 &stx, SAMBA_STATX_ATTR_MASK, 0);
913 DBG_DEBUG("[CEPH] fstatat(...) = %d\n", result);
914 if (result < 0) {
915 return status_code(result);
918 init_stat_ex_from_ceph_statx(sbuf, &stx);
919 DBG_DEBUG("[CEPH] mode = 0x%x\n", sbuf->st_ex_mode);
921 return 0;
924 static int cephwrap_lstat(struct vfs_handle_struct *handle,
925 struct smb_filename *smb_fname)
927 int result = -1;
928 struct ceph_statx stx = { 0 };
930 DBG_DEBUG("[CEPH] lstat(%p, %s)\n",
931 handle,
932 smb_fname_str_dbg(smb_fname));
934 if (smb_fname->stream_name) {
935 errno = ENOENT;
936 return result;
939 result = ceph_statx(handle->data, smb_fname->base_name, &stx,
940 SAMBA_STATX_ATTR_MASK, AT_SYMLINK_NOFOLLOW);
941 DBG_DEBUG("[CEPH] lstat(...) = %d\n", result);
942 if (result < 0) {
943 return status_code(result);
946 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
947 return result;
950 static int cephwrap_fntimes(struct vfs_handle_struct *handle,
951 files_struct *fsp,
952 struct smb_file_time *ft)
954 struct ceph_statx stx = { 0 };
955 int result;
956 int mask = 0;
958 if (!is_omit_timespec(&ft->atime)) {
959 stx.stx_atime = ft->atime;
960 mask |= CEPH_SETATTR_ATIME;
962 if (!is_omit_timespec(&ft->mtime)) {
963 stx.stx_mtime = ft->mtime;
964 mask |= CEPH_SETATTR_MTIME;
966 if (!is_omit_timespec(&ft->create_time)) {
967 stx.stx_btime = ft->create_time;
968 mask |= CEPH_SETATTR_BTIME;
971 if (!mask) {
972 return 0;
975 if (!fsp->fsp_flags.is_pathref) {
977 * We can use an io_fd to set xattrs.
979 result = ceph_fsetattrx(handle->data,
980 fsp_get_io_fd(fsp),
981 &stx,
982 mask);
983 } else {
985 * This is no longer a handle based call.
987 result = ceph_setattrx(handle->data,
988 fsp->fsp_name->base_name,
989 &stx,
990 mask,
994 DBG_DEBUG("[CEPH] ntimes(%p, %s, {%ld, %ld, %ld, %ld}) = %d\n",
995 handle, fsp_str_dbg(fsp), ft->mtime.tv_sec, ft->atime.tv_sec,
996 ft->ctime.tv_sec, ft->create_time.tv_sec, result);
998 return result;
1001 static int cephwrap_unlinkat(struct vfs_handle_struct *handle,
1002 struct files_struct *dirfsp,
1003 const struct smb_filename *smb_fname,
1004 int flags)
1006 int result = -1;
1007 int dirfd = fsp_get_pathref_fd(dirfsp);
1009 DBG_DEBUG("[CEPH] unlinkat(%p, %d, %s)\n",
1010 handle,
1011 dirfd,
1012 smb_fname_str_dbg(smb_fname));
1014 if (smb_fname->stream_name) {
1015 errno = ENOENT;
1016 return result;
1019 result = ceph_unlinkat(handle->data,
1020 dirfd,
1021 smb_fname->base_name,
1022 flags);
1023 DBG_DEBUG("[CEPH] unlinkat(...) = %d\n", result);
1024 return status_code(result);
1027 static int cephwrap_fchmod(struct vfs_handle_struct *handle,
1028 files_struct *fsp,
1029 mode_t mode)
1031 int result;
1033 DBG_DEBUG("[CEPH] fchmod(%p, %p, %d)\n", handle, fsp, mode);
1034 if (!fsp->fsp_flags.is_pathref) {
1036 * We can use an io_fd to change permissions.
1038 result = ceph_fchmod(handle->data, fsp_get_io_fd(fsp), mode);
1039 } else {
1041 * This is no longer a handle based call.
1043 result = ceph_chmod(handle->data,
1044 fsp->fsp_name->base_name,
1045 mode);
1047 DBG_DEBUG("[CEPH] fchmod(...) = %d\n", result);
1048 return status_code(result);
1051 static int cephwrap_fchown(struct vfs_handle_struct *handle,
1052 files_struct *fsp,
1053 uid_t uid,
1054 gid_t gid)
1056 int result;
1058 DBG_DEBUG("[CEPH] fchown(%p, %p, %d, %d)\n", handle, fsp, uid, gid);
1059 if (!fsp->fsp_flags.is_pathref) {
1061 * We can use an io_fd to change ownership.
1063 result = ceph_fchown(handle->data,
1064 fsp_get_io_fd(fsp),
1065 uid,
1066 gid);
1067 } else {
1069 * This is no longer a handle based call.
1071 result = ceph_chown(handle->data,
1072 fsp->fsp_name->base_name,
1073 uid,
1074 gid);
1077 DBG_DEBUG("[CEPH] fchown(...) = %d\n", result);
1078 return status_code(result);
1081 static int cephwrap_lchown(struct vfs_handle_struct *handle,
1082 const struct smb_filename *smb_fname,
1083 uid_t uid,
1084 gid_t gid)
1086 int result;
1087 DBG_DEBUG("[CEPH] lchown(%p, %s, %d, %d)\n",
1088 handle,
1089 smb_fname->base_name,
1090 uid,
1091 gid);
1092 result = ceph_lchown(handle->data, smb_fname->base_name, uid, gid);
1093 DBG_DEBUG("[CEPH] lchown(...) = %d\n", result);
1094 return status_code(result);
1097 static int cephwrap_chdir(struct vfs_handle_struct *handle,
1098 const struct smb_filename *smb_fname)
1100 int result = -1;
1101 DBG_DEBUG("[CEPH] chdir(%p, %s)\n", handle, smb_fname->base_name);
1102 result = ceph_chdir(handle->data, smb_fname->base_name);
1103 DBG_DEBUG("[CEPH] chdir(...) = %d\n", result);
1104 return status_code(result);
1107 static struct smb_filename *cephwrap_getwd(struct vfs_handle_struct *handle,
1108 TALLOC_CTX *ctx)
1110 const char *cwd = ceph_getcwd(handle->data);
1111 DBG_DEBUG("[CEPH] getwd(%p) = %s\n", handle, cwd);
1112 return synthetic_smb_fname(ctx, cwd, NULL, NULL, 0, 0);
1115 static int strict_allocate_ftruncate(struct vfs_handle_struct *handle,
1116 files_struct *fsp,
1117 off_t len)
1119 off_t space_to_write;
1120 int result;
1121 NTSTATUS status;
1122 SMB_STRUCT_STAT *pst;
1124 status = vfs_stat_fsp(fsp);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 return -1;
1128 pst = &fsp->fsp_name->st;
1130 #ifdef S_ISFIFO
1131 if (S_ISFIFO(pst->st_ex_mode))
1132 return 0;
1133 #endif
1135 if (pst->st_ex_size == len)
1136 return 0;
1138 /* Shrink - just ftruncate. */
1139 if (pst->st_ex_size > len) {
1140 result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
1141 return status_code(result);
1144 space_to_write = len - pst->st_ex_size;
1145 result = ceph_fallocate(handle->data,
1146 fsp_get_io_fd(fsp),
1148 pst->st_ex_size,
1149 space_to_write);
1150 return status_code(result);
1153 static int cephwrap_ftruncate(struct vfs_handle_struct *handle,
1154 files_struct *fsp,
1155 off_t len)
1157 int result = -1;
1159 DBG_DEBUG("[CEPH] ftruncate(%p, %p, %llu\n", handle, fsp, llu(len));
1161 if (lp_strict_allocate(SNUM(fsp->conn))) {
1162 return strict_allocate_ftruncate(handle, fsp, len);
1165 result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
1166 return status_code(result);
1169 static int cephwrap_fallocate(struct vfs_handle_struct *handle,
1170 struct files_struct *fsp,
1171 uint32_t mode,
1172 off_t offset,
1173 off_t len)
1175 int result;
1177 DBG_DEBUG("[CEPH] fallocate(%p, %p, %u, %llu, %llu\n",
1178 handle, fsp, mode, llu(offset), llu(len));
1179 /* unsupported mode flags are rejected by libcephfs */
1180 result = ceph_fallocate(
1181 handle->data, fsp_get_io_fd(fsp), mode, offset, len);
1182 DBG_DEBUG("[CEPH] fallocate(...) = %d\n", result);
1183 return status_code(result);
1186 static bool cephwrap_lock(struct vfs_handle_struct *handle,
1187 files_struct *fsp,
1188 int op,
1189 off_t offset,
1190 off_t count,
1191 int type)
1193 DBG_DEBUG("[CEPH] lock\n");
1194 return true;
1197 static int cephwrap_filesystem_sharemode(struct vfs_handle_struct *handle,
1198 files_struct *fsp,
1199 uint32_t share_access,
1200 uint32_t access_mask)
1202 DBG_ERR("[CEPH] filesystem sharemodes unsupported! Consider setting "
1203 "\"kernel share modes = no\"\n");
1205 return vfs_not_implemented_filesystem_sharemode(handle,
1206 fsp,
1207 share_access,
1208 access_mask);
1211 static int cephwrap_fcntl(vfs_handle_struct *handle,
1212 files_struct *fsp, int cmd, va_list cmd_arg)
1215 * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1216 * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1218 if (cmd == F_GETFL) {
1219 return 0;
1220 } else if (cmd == F_SETFL) {
1221 va_list dup_cmd_arg;
1222 int opt;
1224 va_copy(dup_cmd_arg, cmd_arg);
1225 opt = va_arg(dup_cmd_arg, int);
1226 va_end(dup_cmd_arg);
1227 if (opt == 0) {
1228 return 0;
1230 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1231 goto err_out;
1233 DBG_ERR("unexpected fcntl: %d\n", cmd);
1234 err_out:
1235 errno = EINVAL;
1236 return -1;
1239 static bool cephwrap_getlock(struct vfs_handle_struct *handle,
1240 files_struct *fsp,
1241 off_t *poffset,
1242 off_t *pcount,
1243 int *ptype,
1244 pid_t *ppid)
1246 DBG_DEBUG("[CEPH] getlock returning false and errno=0\n");
1248 errno = 0;
1249 return false;
1252 static int cephwrap_symlinkat(struct vfs_handle_struct *handle,
1253 const struct smb_filename *link_target,
1254 struct files_struct *dirfsp,
1255 const struct smb_filename *new_smb_fname)
1257 int result = -1;
1258 int dirfd = fsp_get_pathref_fd(dirfsp);
1260 DBG_DEBUG("[CEPH] symlinkat(%p, %s, %d, %s)\n",
1261 handle,
1262 link_target->base_name,
1263 dirfd,
1264 new_smb_fname->base_name);
1266 result = ceph_symlinkat(handle->data,
1267 link_target->base_name,
1268 dirfd,
1269 new_smb_fname->base_name);
1270 DBG_DEBUG("[CEPH] symlinkat(...) = %d\n", result);
1271 return status_code(result);
1274 static int cephwrap_readlinkat(struct vfs_handle_struct *handle,
1275 const struct files_struct *dirfsp,
1276 const struct smb_filename *smb_fname,
1277 char *buf,
1278 size_t bufsiz)
1280 int result = -1;
1281 int dirfd = fsp_get_pathref_fd(dirfsp);
1283 DBG_DEBUG("[CEPH] readlinkat(%p, %d, %s, %p, %llu)\n",
1284 handle,
1285 dirfd,
1286 smb_fname->base_name,
1287 buf,
1288 llu(bufsiz));
1290 result = ceph_readlinkat(handle->data,
1291 dirfd,
1292 smb_fname->base_name,
1293 buf,
1294 bufsiz);
1296 DBG_DEBUG("[CEPH] readlinkat(...) = %d\n", result);
1297 return status_code(result);
1300 static int cephwrap_linkat(struct vfs_handle_struct *handle,
1301 files_struct *srcfsp,
1302 const struct smb_filename *old_smb_fname,
1303 files_struct *dstfsp,
1304 const struct smb_filename *new_smb_fname,
1305 int flags)
1307 struct smb_filename *full_fname_old = NULL;
1308 struct smb_filename *full_fname_new = NULL;
1309 int result = -1;
1311 full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
1312 srcfsp,
1313 old_smb_fname);
1314 if (full_fname_old == NULL) {
1315 return -1;
1317 full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
1318 dstfsp,
1319 new_smb_fname);
1320 if (full_fname_new == NULL) {
1321 TALLOC_FREE(full_fname_old);
1322 return -1;
1325 DBG_DEBUG("[CEPH] link(%p, %s, %s)\n", handle,
1326 full_fname_old->base_name,
1327 full_fname_new->base_name);
1329 result = ceph_link(handle->data,
1330 full_fname_old->base_name,
1331 full_fname_new->base_name);
1332 DBG_DEBUG("[CEPH] link(...) = %d\n", result);
1333 TALLOC_FREE(full_fname_old);
1334 TALLOC_FREE(full_fname_new);
1335 return status_code(result);
1338 static int cephwrap_mknodat(struct vfs_handle_struct *handle,
1339 files_struct *dirfsp,
1340 const struct smb_filename *smb_fname,
1341 mode_t mode,
1342 SMB_DEV_T dev)
1344 struct smb_filename *full_fname = NULL;
1345 int result = -1;
1347 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1348 dirfsp,
1349 smb_fname);
1350 if (full_fname == NULL) {
1351 return -1;
1354 DBG_DEBUG("[CEPH] mknodat(%p, %s)\n", handle, full_fname->base_name);
1355 result = ceph_mknod(handle->data, full_fname->base_name, mode, dev);
1356 DBG_DEBUG("[CEPH] mknodat(...) = %d\n", result);
1358 TALLOC_FREE(full_fname);
1360 return status_code(result);
1364 * This is a simple version of real-path ... a better version is needed to
1365 * ask libcephfs about symbolic links.
1367 static struct smb_filename *cephwrap_realpath(struct vfs_handle_struct *handle,
1368 TALLOC_CTX *ctx,
1369 const struct smb_filename *smb_fname)
1371 char *result = NULL;
1372 const char *cwd = handle->conn->cwd_fsp->fsp_name->base_name;
1373 const char *path = smb_fname->base_name;
1374 size_t len = strlen(path);
1375 struct smb_filename *result_fname = NULL;
1377 if (path[0] == '/') {
1378 result = talloc_strdup(ctx, path);
1379 } else if ((len >= 2) && (path[0] == '.') && (path[1] == '/')) {
1380 if (len == 2) {
1381 result = talloc_strdup(ctx, cwd);
1382 } else {
1383 result = talloc_asprintf(ctx, "%s/%s", cwd, &path[2]);
1385 } else {
1386 result = talloc_asprintf(ctx, "%s/%s", cwd, path);
1389 if (result == NULL) {
1390 return NULL;
1393 DBG_DEBUG("[CEPH] realpath(%p, %s) = %s\n", handle, path, result);
1394 result_fname = synthetic_smb_fname(ctx, result, NULL, NULL, 0, 0);
1395 TALLOC_FREE(result);
1396 return result_fname;
1399 static NTSTATUS cephwrap_get_real_filename_at(
1400 struct vfs_handle_struct *handle,
1401 struct files_struct *dirfsp,
1402 const char *name,
1403 TALLOC_CTX *mem_ctx,
1404 char **found_name)
1407 * Don't fall back to get_real_filename so callers can differentiate
1408 * between a full directory scan and an actual case-insensitive stat.
1410 return NT_STATUS_NOT_SUPPORTED;
1413 static const char *cephwrap_connectpath(
1414 struct vfs_handle_struct *handle,
1415 const struct files_struct *dirfsp,
1416 const struct smb_filename *smb_fname)
1418 return handle->conn->connectpath;
1421 static NTSTATUS cephwrap_fget_dos_attributes(struct vfs_handle_struct *handle,
1422 struct files_struct *fsp,
1423 uint32_t *dosmode)
1425 struct timespec saved_btime = fsp->fsp_name->st.st_ex_btime;
1426 NTSTATUS status;
1428 status = fget_ea_dos_attribute(fsp, dosmode);
1431 * Restore previously stored btime from statx timestamps as it should be
1432 * the only source of truth. create_time from dos attribute, if any, may
1433 * have older values which isn't trustworthy to be looked at for other
1434 * open file handle operations.
1436 fsp->fsp_name->st.st_ex_btime = saved_btime;
1438 return status;
1441 static NTSTATUS cephwrap_fset_dos_attributes(struct vfs_handle_struct *handle,
1442 struct files_struct *fsp,
1443 uint32_t dosmode)
1445 struct timespec saved_btime = fsp->fsp_name->st.st_ex_btime;
1446 NTSTATUS status;
1448 status = set_ea_dos_attribute(handle->conn, fsp->fsp_name, dosmode);
1451 * Restore previously stored btime from statx timestamps. This is done
1452 * to ensure that we have the exact btime in fsp stat information while
1453 * the file handle is still open since the create_time stored as part of
1454 * dos attributes can loose its precision when converted back to btime.
1456 fsp->fsp_name->st.st_ex_btime = saved_btime;
1458 return status;
1461 /****************************************************************
1462 Extended attribute operations.
1463 *****************************************************************/
1465 static ssize_t cephwrap_fgetxattr(struct vfs_handle_struct *handle,
1466 struct files_struct *fsp,
1467 const char *name,
1468 void *value,
1469 size_t size)
1471 int ret;
1472 DBG_DEBUG("[CEPH] fgetxattr(%p, %p, %s, %p, %llu)\n",
1473 handle,
1474 fsp,
1475 name,
1476 value,
1477 llu(size));
1478 if (!fsp->fsp_flags.is_pathref) {
1479 ret = ceph_fgetxattr(handle->data,
1480 fsp_get_io_fd(fsp),
1481 name,
1482 value,
1483 size);
1484 } else {
1485 ret = ceph_getxattr(handle->data,
1486 fsp->fsp_name->base_name,
1487 name,
1488 value,
1489 size);
1491 DBG_DEBUG("[CEPH] fgetxattr(...) = %d\n", ret);
1492 return lstatus_code(ret);
1495 static ssize_t cephwrap_flistxattr(struct vfs_handle_struct *handle,
1496 struct files_struct *fsp,
1497 char *list,
1498 size_t size)
1500 int ret;
1501 DBG_DEBUG("[CEPH] flistxattr(%p, %p, %p, %llu)\n",
1502 handle, fsp, list, llu(size));
1503 if (!fsp->fsp_flags.is_pathref) {
1505 * We can use an io_fd to list xattrs.
1507 ret = ceph_flistxattr(handle->data,
1508 fsp_get_io_fd(fsp),
1509 list,
1510 size);
1511 } else {
1513 * This is no longer a handle based call.
1515 ret = ceph_listxattr(handle->data,
1516 fsp->fsp_name->base_name,
1517 list,
1518 size);
1520 DBG_DEBUG("[CEPH] flistxattr(...) = %d\n", ret);
1521 return lstatus_code(ret);
1524 static int cephwrap_fremovexattr(struct vfs_handle_struct *handle,
1525 struct files_struct *fsp,
1526 const char *name)
1528 int ret;
1529 DBG_DEBUG("[CEPH] fremovexattr(%p, %p, %s)\n", handle, fsp, name);
1530 if (!fsp->fsp_flags.is_pathref) {
1532 * We can use an io_fd to remove xattrs.
1534 ret = ceph_fremovexattr(handle->data, fsp_get_io_fd(fsp), name);
1535 } else {
1537 * This is no longer a handle based call.
1539 ret = ceph_removexattr(handle->data,
1540 fsp->fsp_name->base_name,
1541 name);
1543 DBG_DEBUG("[CEPH] fremovexattr(...) = %d\n", ret);
1544 return status_code(ret);
1547 static int cephwrap_fsetxattr(struct vfs_handle_struct *handle,
1548 struct files_struct *fsp,
1549 const char *name,
1550 const void *value,
1551 size_t size,
1552 int flags)
1554 int ret;
1555 DBG_DEBUG("[CEPH] fsetxattr(%p, %p, %s, %p, %llu, %d)\n",
1556 handle,
1557 fsp,
1558 name,
1559 value,
1560 llu(size),
1561 flags);
1562 if (!fsp->fsp_flags.is_pathref) {
1564 * We can use an io_fd to set xattrs.
1566 ret = ceph_fsetxattr(handle->data,
1567 fsp_get_io_fd(fsp),
1568 name,
1569 value,
1570 size,
1571 flags);
1572 } else {
1574 * This is no longer a handle based call.
1576 ret = ceph_setxattr(handle->data,
1577 fsp->fsp_name->base_name,
1578 name,
1579 value,
1580 size,
1581 flags);
1583 DBG_DEBUG("[CEPH] fsetxattr(...) = %d\n", ret);
1584 return status_code(ret);
1587 static NTSTATUS cephwrap_create_dfs_pathat(struct vfs_handle_struct *handle,
1588 struct files_struct *dirfsp,
1589 const struct smb_filename *smb_fname,
1590 const struct referral *reflist,
1591 size_t referral_count)
1593 TALLOC_CTX *frame = talloc_stackframe();
1594 NTSTATUS status = NT_STATUS_NO_MEMORY;
1595 int ret;
1596 char *msdfs_link = NULL;
1597 struct smb_filename *full_fname = NULL;
1599 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1600 dirfsp,
1601 smb_fname);
1602 if (full_fname == NULL) {
1603 goto out;
1606 /* Form the msdfs_link contents */
1607 msdfs_link = msdfs_link_string(frame,
1608 reflist,
1609 referral_count);
1610 if (msdfs_link == NULL) {
1611 goto out;
1614 ret = ceph_symlink(handle->data,
1615 msdfs_link,
1616 full_fname->base_name);
1617 if (ret == 0) {
1618 status = NT_STATUS_OK;
1619 } else {
1620 status = map_nt_error_from_unix(-ret);
1623 out:
1625 DBG_DEBUG("[CEPH] create_dfs_pathat(%s) = %s\n",
1626 full_fname != NULL ? full_fname->base_name : "",
1627 nt_errstr(status));
1629 TALLOC_FREE(frame);
1630 return status;
1634 * Read and return the contents of a DFS redirect given a
1635 * pathname. A caller can pass in NULL for ppreflist and
1636 * preferral_count but still determine if this was a
1637 * DFS redirect point by getting NT_STATUS_OK back
1638 * without incurring the overhead of reading and parsing
1639 * the referral contents.
1642 static NTSTATUS cephwrap_read_dfs_pathat(struct vfs_handle_struct *handle,
1643 TALLOC_CTX *mem_ctx,
1644 struct files_struct *dirfsp,
1645 struct smb_filename *smb_fname,
1646 struct referral **ppreflist,
1647 size_t *preferral_count)
1649 NTSTATUS status = NT_STATUS_NO_MEMORY;
1650 size_t bufsize;
1651 char *link_target = NULL;
1652 int referral_len;
1653 bool ok;
1654 #if defined(HAVE_BROKEN_READLINK)
1655 char link_target_buf[PATH_MAX];
1656 #else
1657 char link_target_buf[7];
1658 #endif
1659 struct ceph_statx stx = { 0 };
1660 struct smb_filename *full_fname = NULL;
1661 int ret;
1663 if (is_named_stream(smb_fname)) {
1664 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1665 goto err;
1668 if (ppreflist == NULL && preferral_count == NULL) {
1670 * We're only checking if this is a DFS
1671 * redirect. We don't need to return data.
1673 bufsize = sizeof(link_target_buf);
1674 link_target = link_target_buf;
1675 } else {
1676 bufsize = PATH_MAX;
1677 link_target = talloc_array(mem_ctx, char, bufsize);
1678 if (!link_target) {
1679 goto err;
1683 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1684 dirfsp,
1685 smb_fname);
1686 if (full_fname == NULL) {
1687 status = NT_STATUS_NO_MEMORY;
1688 goto err;
1691 ret = ceph_statx(handle->data,
1692 full_fname->base_name,
1693 &stx,
1694 SAMBA_STATX_ATTR_MASK,
1695 AT_SYMLINK_NOFOLLOW);
1696 if (ret < 0) {
1697 status = map_nt_error_from_unix(-ret);
1698 goto err;
1701 referral_len = ceph_readlink(handle->data,
1702 full_fname->base_name,
1703 link_target,
1704 bufsize - 1);
1705 if (referral_len < 0) {
1706 /* ceph errors are -errno. */
1707 if (-referral_len == EINVAL) {
1708 DBG_INFO("%s is not a link.\n",
1709 full_fname->base_name);
1710 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1711 } else {
1712 status = map_nt_error_from_unix(-referral_len);
1713 DBG_ERR("Error reading "
1714 "msdfs link %s: %s\n",
1715 full_fname->base_name,
1716 strerror(errno));
1718 goto err;
1720 link_target[referral_len] = '\0';
1722 DBG_INFO("%s -> %s\n",
1723 full_fname->base_name,
1724 link_target);
1726 if (!strnequal(link_target, "msdfs:", 6)) {
1727 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1728 goto err;
1731 if (ppreflist == NULL && preferral_count == NULL) {
1732 /* Early return for checking if this is a DFS link. */
1733 TALLOC_FREE(full_fname);
1734 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1735 return NT_STATUS_OK;
1738 ok = parse_msdfs_symlink(mem_ctx,
1739 lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
1740 link_target,
1741 ppreflist,
1742 preferral_count);
1744 if (ok) {
1745 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1746 status = NT_STATUS_OK;
1747 } else {
1748 status = NT_STATUS_NO_MEMORY;
1751 err:
1753 if (link_target != link_target_buf) {
1754 TALLOC_FREE(link_target);
1756 TALLOC_FREE(full_fname);
1757 return status;
1760 static struct vfs_fn_pointers ceph_fns = {
1761 /* Disk operations */
1763 .connect_fn = cephwrap_connect,
1764 .disconnect_fn = cephwrap_disconnect,
1765 .disk_free_fn = cephwrap_disk_free,
1766 .get_quota_fn = vfs_not_implemented_get_quota,
1767 .set_quota_fn = vfs_not_implemented_set_quota,
1768 .statvfs_fn = cephwrap_statvfs,
1769 .fs_capabilities_fn = cephwrap_fs_capabilities,
1771 /* Directory operations */
1773 .fdopendir_fn = cephwrap_fdopendir,
1774 .readdir_fn = cephwrap_readdir,
1775 .rewind_dir_fn = cephwrap_rewinddir,
1776 .mkdirat_fn = cephwrap_mkdirat,
1777 .closedir_fn = cephwrap_closedir,
1779 /* File operations */
1781 .create_dfs_pathat_fn = cephwrap_create_dfs_pathat,
1782 .read_dfs_pathat_fn = cephwrap_read_dfs_pathat,
1783 .openat_fn = cephwrap_openat,
1784 .close_fn = cephwrap_close,
1785 .pread_fn = cephwrap_pread,
1786 .pread_send_fn = cephwrap_pread_send,
1787 .pread_recv_fn = cephwrap_pread_recv,
1788 .pwrite_fn = cephwrap_pwrite,
1789 .pwrite_send_fn = cephwrap_pwrite_send,
1790 .pwrite_recv_fn = cephwrap_pwrite_recv,
1791 .lseek_fn = cephwrap_lseek,
1792 .sendfile_fn = cephwrap_sendfile,
1793 .recvfile_fn = cephwrap_recvfile,
1794 .renameat_fn = cephwrap_renameat,
1795 .fsync_send_fn = cephwrap_fsync_send,
1796 .fsync_recv_fn = cephwrap_fsync_recv,
1797 .stat_fn = cephwrap_stat,
1798 .fstat_fn = cephwrap_fstat,
1799 .lstat_fn = cephwrap_lstat,
1800 .fstatat_fn = cephwrap_fstatat,
1801 .unlinkat_fn = cephwrap_unlinkat,
1802 .fchmod_fn = cephwrap_fchmod,
1803 .fchown_fn = cephwrap_fchown,
1804 .lchown_fn = cephwrap_lchown,
1805 .chdir_fn = cephwrap_chdir,
1806 .getwd_fn = cephwrap_getwd,
1807 .fntimes_fn = cephwrap_fntimes,
1808 .ftruncate_fn = cephwrap_ftruncate,
1809 .fallocate_fn = cephwrap_fallocate,
1810 .lock_fn = cephwrap_lock,
1811 .filesystem_sharemode_fn = cephwrap_filesystem_sharemode,
1812 .fcntl_fn = cephwrap_fcntl,
1813 .linux_setlease_fn = vfs_not_implemented_linux_setlease,
1814 .getlock_fn = cephwrap_getlock,
1815 .symlinkat_fn = cephwrap_symlinkat,
1816 .readlinkat_fn = cephwrap_readlinkat,
1817 .linkat_fn = cephwrap_linkat,
1818 .mknodat_fn = cephwrap_mknodat,
1819 .realpath_fn = cephwrap_realpath,
1820 .fchflags_fn = vfs_not_implemented_fchflags,
1821 .get_real_filename_at_fn = cephwrap_get_real_filename_at,
1822 .connectpath_fn = cephwrap_connectpath,
1823 .fget_dos_attributes_fn = cephwrap_fget_dos_attributes,
1824 .fset_dos_attributes_fn = cephwrap_fset_dos_attributes,
1826 /* EA operations. */
1827 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1828 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1829 .fgetxattr_fn = cephwrap_fgetxattr,
1830 .flistxattr_fn = cephwrap_flistxattr,
1831 .fremovexattr_fn = cephwrap_fremovexattr,
1832 .fsetxattr_fn = cephwrap_fsetxattr,
1834 /* Posix ACL Operations */
1835 .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1836 .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1837 .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1838 .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
1840 /* aio operations */
1841 .aio_force_fn = vfs_not_implemented_aio_force,
1844 static_decl_vfs;
1845 NTSTATUS vfs_ceph_init(TALLOC_CTX *ctx)
1847 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1848 "ceph", &ceph_fns);