b2f38c2be3b849144fd59eec7972d99c28a61415
[samba.git] / source3 / modules / vfs_ceph.c
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>
8
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.
13
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.
18
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/>.
21 */
22
23 /*
24  * This VFS only works with the libceph.so user-space client. It is not needed
25  * if you are using the kernel client or the FUSE client.
26  *
27  * Add the following smb.conf parameter to each share that will be hosted on
28  * Ceph:
29  *
30  *   vfs objects = ceph [any others you need go here]
31  */
32
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"
42
43 #undef DBGC_CLASS
44 #define DBGC_CLASS DBGC_VFS
45
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
50
51 /*
52  * Use %llu whenever we have a 64bit unsigned int, and cast to (long long unsigned)
53  */
54 #define llu(_var) ((long long unsigned)_var)
55
56 /*
57  * Note, libceph's return code model is to return -errno! So we have to convert
58  * to what Samba expects, with is set errno to -return and return -1
59  */
60 #define WRAP_RETURN(_res) \
61         errno = 0; \
62         if (_res < 0) { \
63                 errno = -_res; \
64                 return -1; \
65         } \
66         return _res \
67
68 /*
69  * We mount only one file system and then all shares are assumed to be in that.
70  * FIXME: If we want to support more than one FS, then we have to deal with
71  * this differently.
72  *
73  * So, cmount tells us if we have been this way before and whether
74  * we need to mount ceph and cmount_cnt tells us how many times we have
75  * connected
76  */
77 static struct ceph_mount_info * cmount = NULL;
78 static uint32_t cmount_cnt = 0;
79
80 /* Check for NULL pointer parameters in cephwrap_* functions */
81
82 /* We don't want to have NULL function pointers lying around.  Someone
83    is sure to try and execute them.  These stubs are used to prevent
84    this possibility. */
85
86 static int cephwrap_connect(struct vfs_handle_struct *handle,  const char *service, const char *user)
87 {
88         int ret;
89         char buf[256];
90         int snum = SNUM(handle->conn);
91         const char *conf_file;
92         const char *user_id;
93
94         if (cmount) {
95                 handle->data = cmount; /* We have been here before */
96                 cmount_cnt++;
97                 return 0;
98         }
99
100         /* if config_file and/or user_id are NULL, ceph will use defaults */
101         conf_file = lp_parm_const_string(snum, "ceph", "config_file", NULL);
102         user_id = lp_parm_const_string(snum, "ceph", "user_id", NULL);
103
104         DBG_DEBUG("[CEPH] calling: ceph_create\n");
105         ret = ceph_create(&cmount, user_id);
106         if (ret) {
107                 goto err_out;
108         }
109
110         DBG_DEBUG("[CEPH] calling: ceph_conf_read_file with %s\n",
111                   (conf_file == NULL ? "default path" : conf_file));
112         ret = ceph_conf_read_file(cmount, conf_file);
113         if (ret) {
114                 goto err_cm_release;
115         }
116
117         DBG_DEBUG("[CEPH] calling: ceph_conf_get\n");
118         ret = ceph_conf_get(cmount, "log file", buf, sizeof(buf));
119         if (ret < 0) {
120                 goto err_cm_release;
121         }
122
123         /* libcephfs disables POSIX ACL support by default, enable it... */
124         ret = ceph_conf_set(cmount, "client_acl_type", "posix_acl");
125         if (ret < 0) {
126                 goto err_cm_release;
127         }
128         /* tell libcephfs to perform local permission checks */
129         ret = ceph_conf_set(cmount, "fuse_default_permissions", "false");
130         if (ret < 0) {
131                 goto err_cm_release;
132         }
133
134         DBG_DEBUG("[CEPH] calling: ceph_mount\n");
135         ret = ceph_mount(cmount, NULL);
136         if (ret < 0) {
137                 goto err_cm_release;
138         }
139
140         /*
141          * encode mount context/state into our vfs/connection holding structure
142          * cmount is a ceph_mount_t*
143          */
144         handle->data = cmount;
145         cmount_cnt++;
146
147         /*
148          * Unless we have an async implementation of getxattrat turn this off.
149          */
150         lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
151
152         return 0;
153
154 err_cm_release:
155         ceph_release(cmount);
156         cmount = NULL;
157 err_out:
158         /*
159          * Handle the error correctly. Ceph returns -errno.
160          */
161         DBG_DEBUG("[CEPH] Error return: %s\n", strerror(-ret));
162         WRAP_RETURN(ret);
163 }
164
165 static void cephwrap_disconnect(struct vfs_handle_struct *handle)
166 {
167         int ret;
168
169         if (!cmount) {
170                 DBG_ERR("[CEPH] Error, ceph not mounted\n");
171                 return;
172         }
173
174         /* Should we unmount/shutdown? Only if the last disconnect? */
175         if (--cmount_cnt) {
176                 DBG_DEBUG("[CEPH] Not shuting down CEPH because still more connections\n");
177                 return;
178         }
179
180         ret = ceph_unmount(cmount);
181         if (ret < 0) {
182                 DBG_ERR("[CEPH] failed to unmount: %s\n", strerror(-ret));
183         }
184
185         ret = ceph_release(cmount);
186         if (ret < 0) {
187                 DBG_ERR("[CEPH] failed to release: %s\n", strerror(-ret));
188         }
189
190         cmount = NULL;  /* Make it safe */
191 }
192
193 /* Disk operations */
194
195 static uint64_t cephwrap_disk_free(struct vfs_handle_struct *handle,
196                                 const struct smb_filename *smb_fname,
197                                 uint64_t *bsize,
198                                 uint64_t *dfree,
199                                 uint64_t *dsize)
200 {
201         struct statvfs statvfs_buf;
202         int ret;
203
204         if (!(ret = ceph_statfs(handle->data, smb_fname->base_name,
205                         &statvfs_buf))) {
206                 /*
207                  * Provide all the correct values.
208                  */
209                 *bsize = statvfs_buf.f_bsize;
210                 *dfree = statvfs_buf.f_bavail;
211                 *dsize = statvfs_buf.f_blocks;
212                 DBG_DEBUG("[CEPH] bsize: %llu, dfree: %llu, dsize: %llu\n",
213                         llu(*bsize), llu(*dfree), llu(*dsize));
214                 return *dfree;
215         } else {
216                 DBG_DEBUG("[CEPH] ceph_statfs returned %d\n", ret);
217                 WRAP_RETURN(ret);
218         }
219 }
220
221 static int cephwrap_get_quota(struct vfs_handle_struct *handle,
222                                 const struct smb_filename *smb_fname,
223                                 enum SMB_QUOTA_TYPE qtype,
224                                 unid_t id,
225                                 SMB_DISK_QUOTA *qt)
226 {
227         /* libceph: Ceph does not implement this */
228 #if 0
229 /* was ifdef HAVE_SYS_QUOTAS */
230         int ret;
231
232         ret = ceph_get_quota(handle->conn->connectpath, qtype, id, qt);
233
234         if (ret) {
235                 errno = -ret;
236                 ret = -1;
237         }
238
239         return ret;
240 #else
241         errno = ENOSYS;
242         return -1;
243 #endif
244 }
245
246 static int cephwrap_set_quota(struct vfs_handle_struct *handle,  enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
247 {
248         /* libceph: Ceph does not implement this */
249 #if 0
250 /* was ifdef HAVE_SYS_QUOTAS */
251         int ret;
252
253         ret = ceph_set_quota(handle->conn->connectpath, qtype, id, qt);
254         if (ret) {
255                 errno = -ret;
256                 ret = -1;
257         }
258
259         return ret;
260 #else
261         WRAP_RETURN(-ENOSYS);
262 #endif
263 }
264
265 static int cephwrap_statvfs(struct vfs_handle_struct *handle,
266                                 const struct smb_filename *smb_fname,
267                                 vfs_statvfs_struct *statbuf)
268 {
269         struct statvfs statvfs_buf;
270         int ret;
271
272         ret = ceph_statfs(handle->data, smb_fname->base_name, &statvfs_buf);
273         if (ret < 0) {
274                 WRAP_RETURN(ret);
275         }
276
277         statbuf->OptimalTransferSize = statvfs_buf.f_frsize;
278         statbuf->BlockSize = statvfs_buf.f_bsize;
279         statbuf->TotalBlocks = statvfs_buf.f_blocks;
280         statbuf->BlocksAvail = statvfs_buf.f_bfree;
281         statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
282         statbuf->TotalFileNodes = statvfs_buf.f_files;
283         statbuf->FreeFileNodes = statvfs_buf.f_ffree;
284         statbuf->FsIdentifier = statvfs_buf.f_fsid;
285         DBG_DEBUG("[CEPH] f_bsize: %ld, f_blocks: %ld, f_bfree: %ld, f_bavail: %ld\n",
286                 (long int)statvfs_buf.f_bsize, (long int)statvfs_buf.f_blocks,
287                 (long int)statvfs_buf.f_bfree, (long int)statvfs_buf.f_bavail);
288
289         return ret;
290 }
291
292 static uint32_t cephwrap_fs_capabilities(struct vfs_handle_struct *handle,
293                                          enum timestamp_set_resolution *p_ts_res)
294 {
295         uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
296
297         *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
298
299         return caps;
300 }
301
302 /* Directory operations */
303
304 static DIR *cephwrap_fdopendir(struct vfs_handle_struct *handle,
305                                struct files_struct *fsp,
306                                const char *mask,
307                                uint32_t attributes)
308 {
309         int ret = 0;
310         struct ceph_dir_result *result;
311         DBG_DEBUG("[CEPH] fdopendir(%p, %p)\n", handle, fsp);
312
313         ret = ceph_opendir(handle->data, fsp->fsp_name->base_name, &result);
314         if (ret < 0) {
315                 result = NULL;
316                 errno = -ret; /* We return result which is NULL in this case */
317         }
318
319         DBG_DEBUG("[CEPH] fdopendir(...) = %d\n", ret);
320         return (DIR *) result;
321 }
322
323 static struct dirent *cephwrap_readdir(struct vfs_handle_struct *handle,
324                                        struct files_struct *dirfsp,
325                                        DIR *dirp,
326                                        SMB_STRUCT_STAT *sbuf)
327 {
328         struct dirent *result;
329
330         DBG_DEBUG("[CEPH] readdir(%p, %p)\n", handle, dirp);
331         result = ceph_readdir(handle->data, (struct ceph_dir_result *) dirp);
332         DBG_DEBUG("[CEPH] readdir(...) = %p\n", result);
333
334         /* Default Posix readdir() does not give us stat info.
335          * Set to invalid to indicate we didn't return this info. */
336         if (sbuf)
337                 SET_STAT_INVALID(*sbuf);
338         return result;
339 }
340
341 static void cephwrap_seekdir(struct vfs_handle_struct *handle, DIR *dirp, long offset)
342 {
343         DBG_DEBUG("[CEPH] seekdir(%p, %p, %ld)\n", handle, dirp, offset);
344         ceph_seekdir(handle->data, (struct ceph_dir_result *) dirp, offset);
345 }
346
347 static long cephwrap_telldir(struct vfs_handle_struct *handle, DIR *dirp)
348 {
349         long ret;
350         DBG_DEBUG("[CEPH] telldir(%p, %p)\n", handle, dirp);
351         ret = ceph_telldir(handle->data, (struct ceph_dir_result *) dirp);
352         DBG_DEBUG("[CEPH] telldir(...) = %ld\n", ret);
353         WRAP_RETURN(ret);
354 }
355
356 static void cephwrap_rewinddir(struct vfs_handle_struct *handle, DIR *dirp)
357 {
358         DBG_DEBUG("[CEPH] rewinddir(%p, %p)\n", handle, dirp);
359         ceph_rewinddir(handle->data, (struct ceph_dir_result *) dirp);
360 }
361
362 static int cephwrap_mkdirat(struct vfs_handle_struct *handle,
363                         files_struct *dirfsp,
364                         const struct smb_filename *smb_fname,
365                         mode_t mode)
366 {
367         struct smb_filename *full_fname = NULL;
368         int result;
369
370         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
371                                                 dirfsp,
372                                                 smb_fname);
373         if (full_fname == NULL) {
374                 return -1;
375         }
376
377         DBG_DEBUG("[CEPH] mkdir(%p, %s)\n",
378                   handle, smb_fname_str_dbg(full_fname));
379
380         result = ceph_mkdir(handle->data, full_fname->base_name, mode);
381
382         TALLOC_FREE(full_fname);
383
384         return WRAP_RETURN(result);
385 }
386
387 static int cephwrap_closedir(struct vfs_handle_struct *handle, DIR *dirp)
388 {
389         int result;
390
391         DBG_DEBUG("[CEPH] closedir(%p, %p)\n", handle, dirp);
392         result = ceph_closedir(handle->data, (struct ceph_dir_result *) dirp);
393         DBG_DEBUG("[CEPH] closedir(...) = %d\n", result);
394         WRAP_RETURN(result);
395 }
396
397 /* File operations */
398
399 static int cephwrap_openat(struct vfs_handle_struct *handle,
400                            const struct files_struct *dirfsp,
401                            const struct smb_filename *smb_fname,
402                            files_struct *fsp,
403                            int flags,
404                            mode_t mode)
405 {
406         struct smb_filename *name = NULL;
407         bool have_opath = false;
408         bool became_root = false;
409         int result = -ENOENT;
410
411         /*
412          * ceph doesn't have openat().
413          */
414         if (fsp_get_pathref_fd(dirfsp) != AT_FDCWD) {
415                 name = full_path_from_dirfsp_atname(talloc_tos(),
416                                                     dirfsp,
417                                                     smb_fname);
418                 if (name == NULL) {
419                         return -1;
420                 }
421                 smb_fname = name;
422         }
423
424         DBG_DEBUG("[CEPH] openat(%p, %s, %p, %d, %d)\n", handle,
425                   smb_fname_str_dbg(smb_fname), fsp, flags, mode);
426
427         if (smb_fname->stream_name) {
428                 goto out;
429         }
430
431 #ifdef O_PATH
432         have_opath = true;
433         if (fsp->fsp_flags.is_pathref) {
434                 flags |= O_PATH;
435         }
436 #endif
437
438         if (fsp->fsp_flags.is_pathref && !have_opath) {
439                 become_root();
440                 became_root = true;
441         }
442
443         result = ceph_open(handle->data, smb_fname->base_name, flags, mode);
444
445         if (became_root) {
446                 unbecome_root();
447         }
448
449 out:
450         TALLOC_FREE(name);
451         fsp->fsp_flags.have_proc_fds = false;
452         DBG_DEBUG("[CEPH] open(...) = %d\n", result);
453         WRAP_RETURN(result);
454 }
455
456 static int cephwrap_close(struct vfs_handle_struct *handle, files_struct *fsp)
457 {
458         int result;
459
460         DBG_DEBUG("[CEPH] close(%p, %p)\n", handle, fsp);
461         result = ceph_close(handle->data, fsp_get_io_fd(fsp));
462         DBG_DEBUG("[CEPH] close(...) = %d\n", result);
463
464         WRAP_RETURN(result);
465 }
466
467 static ssize_t cephwrap_pread(struct vfs_handle_struct *handle, files_struct *fsp, void *data,
468                         size_t n, off_t offset)
469 {
470         ssize_t result;
471
472         DBG_DEBUG("[CEPH] pread(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
473
474         result = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
475         DBG_DEBUG("[CEPH] pread(...) = %llu\n", llu(result));
476         WRAP_RETURN(result);
477 }
478
479 struct cephwrap_pread_state {
480         ssize_t bytes_read;
481         struct vfs_aio_state vfs_aio_state;
482 };
483
484 /*
485  * Fake up an async ceph read by calling the synchronous API.
486  */
487 static struct tevent_req *cephwrap_pread_send(struct vfs_handle_struct *handle,
488                                               TALLOC_CTX *mem_ctx,
489                                               struct tevent_context *ev,
490                                               struct files_struct *fsp,
491                                               void *data,
492                                               size_t n, off_t offset)
493 {
494         struct tevent_req *req = NULL;
495         struct cephwrap_pread_state *state = NULL;
496         int ret = -1;
497
498         DBG_DEBUG("[CEPH] %s\n", __func__);
499         req = tevent_req_create(mem_ctx, &state, struct cephwrap_pread_state);
500         if (req == NULL) {
501                 return NULL;
502         }
503
504         ret = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
505         if (ret < 0) {
506                 /* ceph returns -errno on error. */
507                 tevent_req_error(req, -ret);
508                 return tevent_req_post(req, ev);
509         }
510
511         state->bytes_read = ret;
512         tevent_req_done(req);
513         /* Return and schedule the completion of the call. */
514         return tevent_req_post(req, ev);
515 }
516
517 static ssize_t cephwrap_pread_recv(struct tevent_req *req,
518                                    struct vfs_aio_state *vfs_aio_state)
519 {
520         struct cephwrap_pread_state *state =
521                 tevent_req_data(req, struct cephwrap_pread_state);
522
523         DBG_DEBUG("[CEPH] %s\n", __func__);
524         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
525                 return -1;
526         }
527         *vfs_aio_state = state->vfs_aio_state;
528         return state->bytes_read;
529 }
530
531 static ssize_t cephwrap_pwrite(struct vfs_handle_struct *handle, files_struct *fsp, const void *data,
532                         size_t n, off_t offset)
533 {
534         ssize_t result;
535
536         DBG_DEBUG("[CEPH] pwrite(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
537         result = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
538         DBG_DEBUG("[CEPH] pwrite(...) = %llu\n", llu(result));
539         WRAP_RETURN(result);
540 }
541
542 struct cephwrap_pwrite_state {
543         ssize_t bytes_written;
544         struct vfs_aio_state vfs_aio_state;
545 };
546
547 /*
548  * Fake up an async ceph write by calling the synchronous API.
549  */
550 static struct tevent_req *cephwrap_pwrite_send(struct vfs_handle_struct *handle,
551                                                TALLOC_CTX *mem_ctx,
552                                                struct tevent_context *ev,
553                                                struct files_struct *fsp,
554                                                const void *data,
555                                                size_t n, off_t offset)
556 {
557         struct tevent_req *req = NULL;
558         struct cephwrap_pwrite_state *state = NULL;
559         int ret = -1;
560
561         DBG_DEBUG("[CEPH] %s\n", __func__);
562         req = tevent_req_create(mem_ctx, &state, struct cephwrap_pwrite_state);
563         if (req == NULL) {
564                 return NULL;
565         }
566
567         ret = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
568         if (ret < 0) {
569                 /* ceph returns -errno on error. */
570                 tevent_req_error(req, -ret);
571                 return tevent_req_post(req, ev);
572         }
573
574         state->bytes_written = ret;
575         tevent_req_done(req);
576         /* Return and schedule the completion of the call. */
577         return tevent_req_post(req, ev);
578 }
579
580 static ssize_t cephwrap_pwrite_recv(struct tevent_req *req,
581                                     struct vfs_aio_state *vfs_aio_state)
582 {
583         struct cephwrap_pwrite_state *state =
584                 tevent_req_data(req, struct cephwrap_pwrite_state);
585
586         DBG_DEBUG("[CEPH] %s\n", __func__);
587         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
588                 return -1;
589         }
590         *vfs_aio_state = state->vfs_aio_state;
591         return state->bytes_written;
592 }
593
594 static off_t cephwrap_lseek(struct vfs_handle_struct *handle, files_struct *fsp, off_t offset, int whence)
595 {
596         off_t result = 0;
597
598         DBG_DEBUG("[CEPH] cephwrap_lseek\n");
599         result = ceph_lseek(handle->data, fsp_get_io_fd(fsp), offset, whence);
600         WRAP_RETURN(result);
601 }
602
603 static ssize_t cephwrap_sendfile(struct vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
604                         off_t offset, size_t n)
605 {
606         /*
607          * We cannot support sendfile because libceph is in user space.
608          */
609         DBG_DEBUG("[CEPH] cephwrap_sendfile\n");
610         errno = ENOTSUP;
611         return -1;
612 }
613
614 static ssize_t cephwrap_recvfile(struct vfs_handle_struct *handle,
615                         int fromfd,
616                         files_struct *tofsp,
617                         off_t offset,
618                         size_t n)
619 {
620         /*
621          * We cannot support recvfile because libceph is in user space.
622          */
623         DBG_DEBUG("[CEPH] cephwrap_recvfile\n");
624         errno=ENOTSUP;
625         return -1;
626 }
627
628 static int cephwrap_renameat(struct vfs_handle_struct *handle,
629                         files_struct *srcfsp,
630                         const struct smb_filename *smb_fname_src,
631                         files_struct *dstfsp,
632                         const struct smb_filename *smb_fname_dst)
633 {
634         struct smb_filename *full_fname_src = NULL;
635         struct smb_filename *full_fname_dst = NULL;
636         int result = -1;
637
638         DBG_DEBUG("[CEPH] cephwrap_renameat\n");
639         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
640                 errno = ENOENT;
641                 return result;
642         }
643
644         full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
645                                                   srcfsp,
646                                                   smb_fname_src);
647         if (full_fname_src == NULL) {
648                 errno = ENOMEM;
649                 return -1;
650         }
651         full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
652                                                   dstfsp,
653                                                   smb_fname_dst);
654         if (full_fname_dst == NULL) {
655                 TALLOC_FREE(full_fname_src);
656                 errno = ENOMEM;
657                 return -1;
658         }
659
660         result = ceph_rename(handle->data,
661                              full_fname_src->base_name,
662                              full_fname_dst->base_name);
663
664         TALLOC_FREE(full_fname_src);
665         TALLOC_FREE(full_fname_dst);
666
667         WRAP_RETURN(result);
668 }
669
670 /*
671  * Fake up an async ceph fsync by calling the synchronous API.
672  */
673
674 static struct tevent_req *cephwrap_fsync_send(struct vfs_handle_struct *handle,
675                                         TALLOC_CTX *mem_ctx,
676                                         struct tevent_context *ev,
677                                         files_struct *fsp)
678 {
679         struct tevent_req *req = NULL;
680         struct vfs_aio_state *state = NULL;
681         int ret = -1;
682
683         DBG_DEBUG("[CEPH] cephwrap_fsync_send\n");
684
685         req = tevent_req_create(mem_ctx, &state, struct vfs_aio_state);
686         if (req == NULL) {
687                 return NULL;
688         }
689
690         /* Make sync call. */
691         ret = ceph_fsync(handle->data, fsp_get_io_fd(fsp), false);
692
693         if (ret != 0) {
694                 /* ceph_fsync returns -errno on error. */
695                 tevent_req_error(req, -ret);
696                 return tevent_req_post(req, ev);
697         }
698
699         /* Mark it as done. */
700         tevent_req_done(req);
701         /* Return and schedule the completion of the call. */
702         return tevent_req_post(req, ev);
703 }
704
705 static int cephwrap_fsync_recv(struct tevent_req *req,
706                                 struct vfs_aio_state *vfs_aio_state)
707 {
708         struct vfs_aio_state *state =
709                 tevent_req_data(req, struct vfs_aio_state);
710
711         DBG_DEBUG("[CEPH] cephwrap_fsync_recv\n");
712
713         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
714                 return -1;
715         }
716         *vfs_aio_state = *state;
717         return 0;
718 }
719
720 #define SAMBA_STATX_ATTR_MASK   (CEPH_STATX_BASIC_STATS|CEPH_STATX_BTIME)
721
722 static void init_stat_ex_from_ceph_statx(struct stat_ex *dst, const struct ceph_statx *stx)
723 {
724         DBG_DEBUG("[CEPH]\tstx = {dev = %llx, ino = %llu, mode = 0x%x, "
725                   "nlink = %llu, uid = %d, gid = %d, rdev = %llx, size = %llu, "
726                   "blksize = %llu, blocks = %llu, atime = %llu, mtime = %llu, "
727                   "ctime = %llu, btime = %llu}\n",
728                   llu(stx->stx_dev), llu(stx->stx_ino), stx->stx_mode,
729                   llu(stx->stx_nlink), stx->stx_uid, stx->stx_gid,
730                   llu(stx->stx_rdev), llu(stx->stx_size), llu(stx->stx_blksize),
731                   llu(stx->stx_blocks), llu(stx->stx_atime.tv_sec),
732                   llu(stx->stx_mtime.tv_sec), llu(stx->stx_ctime.tv_sec),
733                   llu(stx->stx_btime.tv_sec));
734
735         if ((stx->stx_mask & SAMBA_STATX_ATTR_MASK) != SAMBA_STATX_ATTR_MASK) {
736                 DBG_WARNING("%s: stx->stx_mask is incorrect (wanted %x, got %x)",
737                                 __func__, SAMBA_STATX_ATTR_MASK, stx->stx_mask);
738         }
739
740         dst->st_ex_dev = stx->stx_dev;
741         dst->st_ex_rdev = stx->stx_rdev;
742         dst->st_ex_ino = stx->stx_ino;
743         dst->st_ex_mode = stx->stx_mode;
744         dst->st_ex_uid = stx->stx_uid;
745         dst->st_ex_gid = stx->stx_gid;
746         dst->st_ex_size = stx->stx_size;
747         dst->st_ex_nlink = stx->stx_nlink;
748         dst->st_ex_atime = stx->stx_atime;
749         dst->st_ex_btime = stx->stx_btime;
750         dst->st_ex_ctime = stx->stx_ctime;
751         dst->st_ex_mtime = stx->stx_mtime;
752         dst->st_ex_blksize = stx->stx_blksize;
753         dst->st_ex_blocks = stx->stx_blocks;
754 }
755
756 static int cephwrap_stat(struct vfs_handle_struct *handle,
757                         struct smb_filename *smb_fname)
758 {
759         int result = -1;
760         struct ceph_statx stx;
761
762         DBG_DEBUG("[CEPH] stat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
763
764         if (smb_fname->stream_name) {
765                 errno = ENOENT;
766                 return result;
767         }
768
769         result = ceph_statx(handle->data, smb_fname->base_name, &stx,
770                                 SAMBA_STATX_ATTR_MASK, 0);
771         DBG_DEBUG("[CEPH] statx(...) = %d\n", result);
772         if (result < 0) {
773                 WRAP_RETURN(result);
774         }
775
776         init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
777         DBG_DEBUG("[CEPH] mode = 0x%x\n", smb_fname->st.st_ex_mode);
778         return result;
779 }
780
781 static int cephwrap_fstat(struct vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
782 {
783         int result = -1;
784         struct ceph_statx stx;
785
786         DBG_DEBUG("[CEPH] fstat(%p, %d)\n", handle, fsp_get_io_fd(fsp));
787         result = ceph_fstatx(handle->data, fsp_get_io_fd(fsp), &stx,
788                                 SAMBA_STATX_ATTR_MASK, 0);
789         DBG_DEBUG("[CEPH] fstat(...) = %d\n", result);
790         if (result < 0) {
791                 WRAP_RETURN(result);
792         }
793
794         init_stat_ex_from_ceph_statx(sbuf, &stx);
795         DBG_DEBUG("[CEPH] mode = 0x%x\n", sbuf->st_ex_mode);
796         return result;
797 }
798
799 static int cephwrap_lstat(struct vfs_handle_struct *handle,
800                          struct smb_filename *smb_fname)
801 {
802         int result = -1;
803         struct ceph_statx stx;
804
805         DBG_DEBUG("[CEPH] lstat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
806
807         if (smb_fname->stream_name) {
808                 errno = ENOENT;
809                 return result;
810         }
811
812         result = ceph_statx(handle->data, smb_fname->base_name, &stx,
813                                 SAMBA_STATX_ATTR_MASK, AT_SYMLINK_NOFOLLOW);
814         DBG_DEBUG("[CEPH] lstat(...) = %d\n", result);
815         if (result < 0) {
816                 WRAP_RETURN(result);
817         }
818
819         init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
820         return result;
821 }
822
823 static int cephwrap_fntimes(struct vfs_handle_struct *handle,
824                             files_struct *fsp,
825                             struct smb_file_time *ft)
826 {
827         struct ceph_statx stx = { 0 };
828         int result;
829         int mask = 0;
830
831         if (!is_omit_timespec(&ft->atime)) {
832                 stx.stx_atime = ft->atime;
833                 mask |= CEPH_SETATTR_ATIME;
834         }
835         if (!is_omit_timespec(&ft->mtime)) {
836                 stx.stx_mtime = ft->mtime;
837                 mask |= CEPH_SETATTR_MTIME;
838         }
839         if (!is_omit_timespec(&ft->create_time)) {
840                 stx.stx_btime = ft->create_time;
841                 mask |= CEPH_SETATTR_BTIME;
842         }
843
844         if (!mask) {
845                 return 0;
846         }
847
848         if (!fsp->fsp_flags.is_pathref) {
849                 /*
850                  * We can use an io_fd to set xattrs.
851                  */
852                 result = ceph_fsetattrx(handle->data,
853                                         fsp_get_io_fd(fsp),
854                                         &stx,
855                                         mask);
856         } else {
857                 /*
858                  * This is no longer a handle based call.
859                  */
860                 result = ceph_setattrx(handle->data,
861                                        fsp->fsp_name->base_name,
862                                        &stx,
863                                        mask,
864                                        0);
865         }
866
867         DBG_DEBUG("[CEPH] ntimes(%p, %s, {%ld, %ld, %ld, %ld}) = %d\n",
868                   handle, fsp_str_dbg(fsp), ft->mtime.tv_sec, ft->atime.tv_sec,
869                   ft->ctime.tv_sec, ft->create_time.tv_sec, result);
870
871         return result;
872 }
873
874 static int cephwrap_unlinkat(struct vfs_handle_struct *handle,
875                         struct files_struct *dirfsp,
876                         const struct smb_filename *smb_fname,
877                         int flags)
878 {
879         struct smb_filename *full_fname = NULL;
880         int result = -1;
881
882         DBG_DEBUG("[CEPH] unlink(%p, %s)\n",
883                 handle,
884                 smb_fname_str_dbg(smb_fname));
885
886         if (smb_fname->stream_name) {
887                 errno = ENOENT;
888                 return result;
889         }
890
891         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
892                                                   dirfsp,
893                                                   smb_fname);
894         if (full_fname == NULL) {
895                 return -1;
896         }
897
898         if (flags & AT_REMOVEDIR) {
899                 result = ceph_rmdir(handle->data, full_fname->base_name);
900         } else {
901                 result = ceph_unlink(handle->data, full_fname->base_name);
902         }
903         TALLOC_FREE(full_fname);
904         DBG_DEBUG("[CEPH] unlink(...) = %d\n", result);
905         WRAP_RETURN(result);
906 }
907
908 static int cephwrap_fchmod(struct vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
909 {
910         int result;
911
912         DBG_DEBUG("[CEPH] fchmod(%p, %p, %d)\n", handle, fsp, mode);
913         if (!fsp->fsp_flags.is_pathref) {
914                 /*
915                  * We can use an io_fd to remove xattrs.
916                  */
917                 result = ceph_fchmod(handle->data, fsp_get_io_fd(fsp), mode);
918         } else {
919                 /*
920                  * This is no longer a handle based call.
921                  */
922                 result = ceph_chmod(handle->data,
923                                     fsp->fsp_name->base_name,
924                                     mode);
925         }
926         DBG_DEBUG("[CEPH] fchmod(...) = %d\n", result);
927         WRAP_RETURN(result);
928 }
929
930 static int cephwrap_fchown(struct vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
931 {
932         int result;
933
934         DBG_DEBUG("[CEPH] fchown(%p, %p, %d, %d)\n", handle, fsp, uid, gid);
935         result = ceph_fchown(handle->data, fsp_get_io_fd(fsp), uid, gid);
936         DBG_DEBUG("[CEPH] fchown(...) = %d\n", result);
937         WRAP_RETURN(result);
938 }
939
940 static int cephwrap_lchown(struct vfs_handle_struct *handle,
941                         const struct smb_filename *smb_fname,
942                         uid_t uid,
943                         gid_t gid)
944 {
945         int result;
946         DBG_DEBUG("[CEPH] lchown(%p, %s, %d, %d)\n", handle, smb_fname->base_name, uid, gid);
947         result = ceph_lchown(handle->data, smb_fname->base_name, uid, gid);
948         DBG_DEBUG("[CEPH] lchown(...) = %d\n", result);
949         WRAP_RETURN(result);
950 }
951
952 static int cephwrap_chdir(struct vfs_handle_struct *handle,
953                         const struct smb_filename *smb_fname)
954 {
955         int result = -1;
956         DBG_DEBUG("[CEPH] chdir(%p, %s)\n", handle, smb_fname->base_name);
957         result = ceph_chdir(handle->data, smb_fname->base_name);
958         DBG_DEBUG("[CEPH] chdir(...) = %d\n", result);
959         WRAP_RETURN(result);
960 }
961
962 static struct smb_filename *cephwrap_getwd(struct vfs_handle_struct *handle,
963                         TALLOC_CTX *ctx)
964 {
965         const char *cwd = ceph_getcwd(handle->data);
966         DBG_DEBUG("[CEPH] getwd(%p) = %s\n", handle, cwd);
967         return synthetic_smb_fname(ctx,
968                                 cwd,
969                                 NULL,
970                                 NULL,
971                                 0,
972                                 0);
973 }
974
975 static int strict_allocate_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
976 {
977         off_t space_to_write;
978         int result;
979         NTSTATUS status;
980         SMB_STRUCT_STAT *pst;
981
982         status = vfs_stat_fsp(fsp);
983         if (!NT_STATUS_IS_OK(status)) {
984                 return -1;
985         }
986         pst = &fsp->fsp_name->st;
987
988 #ifdef S_ISFIFO
989         if (S_ISFIFO(pst->st_ex_mode))
990                 return 0;
991 #endif
992
993         if (pst->st_ex_size == len)
994                 return 0;
995
996         /* Shrink - just ftruncate. */
997         if (pst->st_ex_size > len) {
998                 result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
999                 WRAP_RETURN(result);
1000         }
1001
1002         space_to_write = len - pst->st_ex_size;
1003         result = ceph_fallocate(handle->data, fsp_get_io_fd(fsp), 0, pst->st_ex_size,
1004                                 space_to_write);
1005         WRAP_RETURN(result);
1006 }
1007
1008 static int cephwrap_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
1009 {
1010         int result = -1;
1011
1012         DBG_DEBUG("[CEPH] ftruncate(%p, %p, %llu\n", handle, fsp, llu(len));
1013
1014         if (lp_strict_allocate(SNUM(fsp->conn))) {
1015                 return strict_allocate_ftruncate(handle, fsp, len);
1016         }
1017
1018         result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
1019         WRAP_RETURN(result);
1020 }
1021
1022 static int cephwrap_fallocate(struct vfs_handle_struct *handle,
1023                               struct files_struct *fsp,
1024                               uint32_t mode,
1025                               off_t offset,
1026                               off_t len)
1027 {
1028         int result;
1029
1030         DBG_DEBUG("[CEPH] fallocate(%p, %p, %u, %llu, %llu\n",
1031                   handle, fsp, mode, llu(offset), llu(len));
1032         /* unsupported mode flags are rejected by libcephfs */
1033         result = ceph_fallocate(handle->data, fsp_get_io_fd(fsp), mode, offset, len);
1034         DBG_DEBUG("[CEPH] fallocate(...) = %d\n", result);
1035         WRAP_RETURN(result);
1036 }
1037
1038 static bool cephwrap_lock(struct vfs_handle_struct *handle, files_struct *fsp, int op, off_t offset, off_t count, int type)
1039 {
1040         DBG_DEBUG("[CEPH] lock\n");
1041         return true;
1042 }
1043
1044 static int cephwrap_filesystem_sharemode(struct vfs_handle_struct *handle,
1045                                          files_struct *fsp,
1046                                          uint32_t share_access,
1047                                          uint32_t access_mask)
1048 {
1049         DBG_ERR("[CEPH] filesystem sharemodes unsupported! Consider setting "
1050                 "\"kernel share modes = no\"\n");
1051
1052         errno = ENOSYS;
1053         return -1;
1054 }
1055
1056 static int cephwrap_fcntl(vfs_handle_struct *handle,
1057                           files_struct *fsp, int cmd, va_list cmd_arg)
1058 {
1059         /*
1060          * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1061          * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1062          */
1063         if (cmd == F_GETFL) {
1064                 return 0;
1065         } else if (cmd == F_SETFL) {
1066                 va_list dup_cmd_arg;
1067                 int opt;
1068
1069                 va_copy(dup_cmd_arg, cmd_arg);
1070                 opt = va_arg(dup_cmd_arg, int);
1071                 va_end(dup_cmd_arg);
1072                 if (opt == 0) {
1073                         return 0;
1074                 }
1075                 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1076                 goto err_out;
1077         }
1078         DBG_ERR("unexpected fcntl: %d\n", cmd);
1079 err_out:
1080         errno = EINVAL;
1081         return -1;
1082 }
1083
1084 static bool cephwrap_getlock(struct vfs_handle_struct *handle, files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1085 {
1086         DBG_DEBUG("[CEPH] getlock returning false and errno=0\n");
1087
1088         errno = 0;
1089         return false;
1090 }
1091
1092 /*
1093  * We cannot let this fall through to the default, because the file might only
1094  * be accessible from libceph (which is a user-space client) but the fd might
1095  * be for some file the kernel knows about.
1096  */
1097 static int cephwrap_linux_setlease(struct vfs_handle_struct *handle, files_struct *fsp,
1098                                 int leasetype)
1099 {
1100         int result = -1;
1101
1102         DBG_DEBUG("[CEPH] linux_setlease\n");
1103         errno = ENOSYS;
1104         return result;
1105 }
1106
1107 static int cephwrap_symlinkat(struct vfs_handle_struct *handle,
1108                 const struct smb_filename *link_target,
1109                 struct files_struct *dirfsp,
1110                 const struct smb_filename *new_smb_fname)
1111 {
1112         struct smb_filename *full_fname = NULL;
1113         int result = -1;
1114
1115         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1116                                                 dirfsp,
1117                                                 new_smb_fname);
1118         if (full_fname == NULL) {
1119                 return -1;
1120         }
1121
1122         DBG_DEBUG("[CEPH] symlink(%p, %s, %s)\n", handle,
1123                         link_target->base_name,
1124                         full_fname->base_name);
1125
1126         result = ceph_symlink(handle->data,
1127                         link_target->base_name,
1128                         full_fname->base_name);
1129         TALLOC_FREE(full_fname);
1130         DBG_DEBUG("[CEPH] symlink(...) = %d\n", result);
1131         WRAP_RETURN(result);
1132 }
1133
1134 static int cephwrap_readlinkat(struct vfs_handle_struct *handle,
1135                 const struct files_struct *dirfsp,
1136                 const struct smb_filename *smb_fname,
1137                 char *buf,
1138                 size_t bufsiz)
1139 {
1140         struct smb_filename *full_fname = NULL;
1141         int result = -1;
1142
1143         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1144                                                 dirfsp,
1145                                                 smb_fname);
1146         if (full_fname == NULL) {
1147                 return -1;
1148         }
1149
1150         DBG_DEBUG("[CEPH] readlink(%p, %s, %p, %llu)\n", handle,
1151                         full_fname->base_name, buf, llu(bufsiz));
1152
1153         result = ceph_readlink(handle->data, full_fname->base_name, buf, bufsiz);
1154         TALLOC_FREE(full_fname);
1155         DBG_DEBUG("[CEPH] readlink(...) = %d\n", result);
1156         WRAP_RETURN(result);
1157 }
1158
1159 static int cephwrap_linkat(struct vfs_handle_struct *handle,
1160                 files_struct *srcfsp,
1161                 const struct smb_filename *old_smb_fname,
1162                 files_struct *dstfsp,
1163                 const struct smb_filename *new_smb_fname,
1164                 int flags)
1165 {
1166         struct smb_filename *full_fname_old = NULL;
1167         struct smb_filename *full_fname_new = NULL;
1168         int result = -1;
1169
1170         full_fname_old = full_path_from_dirfsp_atname(talloc_tos(),
1171                                         srcfsp,
1172                                         old_smb_fname);
1173         if (full_fname_old == NULL) {
1174                 return -1;
1175         }
1176         full_fname_new = full_path_from_dirfsp_atname(talloc_tos(),
1177                                         dstfsp,
1178                                         new_smb_fname);
1179         if (full_fname_new == NULL) {
1180                 TALLOC_FREE(full_fname_old);
1181                 return -1;
1182         }
1183
1184         DBG_DEBUG("[CEPH] link(%p, %s, %s)\n", handle,
1185                         full_fname_old->base_name,
1186                         full_fname_new->base_name);
1187
1188         result = ceph_link(handle->data,
1189                                 full_fname_old->base_name,
1190                                 full_fname_new->base_name);
1191         DBG_DEBUG("[CEPH] link(...) = %d\n", result);
1192         TALLOC_FREE(full_fname_old);
1193         TALLOC_FREE(full_fname_new);
1194         WRAP_RETURN(result);
1195 }
1196
1197 static int cephwrap_mknodat(struct vfs_handle_struct *handle,
1198                 files_struct *dirfsp,
1199                 const struct smb_filename *smb_fname,
1200                 mode_t mode,
1201                 SMB_DEV_T dev)
1202 {
1203         struct smb_filename *full_fname = NULL;
1204         int result = -1;
1205
1206         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1207                                                 dirfsp,
1208                                                 smb_fname);
1209         if (full_fname == NULL) {
1210                 return -1;
1211         }
1212
1213         DBG_DEBUG("[CEPH] mknodat(%p, %s)\n", handle, full_fname->base_name);
1214         result = ceph_mknod(handle->data, full_fname->base_name, mode, dev);
1215         DBG_DEBUG("[CEPH] mknodat(...) = %d\n", result);
1216
1217         TALLOC_FREE(full_fname);
1218
1219         WRAP_RETURN(result);
1220 }
1221
1222 /*
1223  * This is a simple version of real-path ... a better version is needed to
1224  * ask libceph about symbolic links.
1225  */
1226 static struct smb_filename *cephwrap_realpath(struct vfs_handle_struct *handle,
1227                                 TALLOC_CTX *ctx,
1228                                 const struct smb_filename *smb_fname)
1229 {
1230         char *result = NULL;
1231         const char *path = smb_fname->base_name;
1232         size_t len = strlen(path);
1233         struct smb_filename *result_fname = NULL;
1234         int r = -1;
1235
1236         if (len && (path[0] == '/')) {
1237                 r = asprintf(&result, "%s", path);
1238         } else if ((len >= 2) && (path[0] == '.') && (path[1] == '/')) {
1239                 if (len == 2) {
1240                         r = asprintf(&result, "%s",
1241                                         handle->conn->cwd_fsp->fsp_name->base_name);
1242                 } else {
1243                         r = asprintf(&result, "%s/%s",
1244                                         handle->conn->cwd_fsp->fsp_name->base_name, &path[2]);
1245                 }
1246         } else {
1247                 r = asprintf(&result, "%s/%s",
1248                                 handle->conn->cwd_fsp->fsp_name->base_name, path);
1249         }
1250
1251         if (r < 0) {
1252                 return NULL;
1253         }
1254
1255         DBG_DEBUG("[CEPH] realpath(%p, %s) = %s\n", handle, path, result);
1256         result_fname = synthetic_smb_fname(ctx,
1257                                 result,
1258                                 NULL,
1259                                 NULL,
1260                                 0,
1261                                 0);
1262         SAFE_FREE(result);
1263         return result_fname;
1264 }
1265
1266
1267 static int cephwrap_fchflags(struct vfs_handle_struct *handle,
1268                         struct files_struct *fsp,
1269                         unsigned int flags)
1270 {
1271         errno = ENOSYS;
1272         return -1;
1273 }
1274
1275 static NTSTATUS cephwrap_get_real_filename(
1276         struct vfs_handle_struct *handle,
1277         const struct smb_filename *path,
1278         const char *name,
1279         TALLOC_CTX *mem_ctx,
1280         char **found_name)
1281 {
1282         /*
1283          * Don't fall back to get_real_filename so callers can differentiate
1284          * between a full directory scan and an actual case-insensitive stat.
1285          */
1286         return NT_STATUS_NOT_SUPPORTED;
1287 }
1288
1289 static NTSTATUS cephwrap_get_real_filename_at(
1290         struct vfs_handle_struct *handle,
1291         struct files_struct *dirfsp,
1292         const char *name,
1293         TALLOC_CTX *mem_ctx,
1294         char **found_name)
1295 {
1296         /*
1297          * Don't fall back to get_real_filename so callers can differentiate
1298          * between a full directory scan and an actual case-insensitive stat.
1299          */
1300         return NT_STATUS_NOT_SUPPORTED;
1301 }
1302
1303 static const char *cephwrap_connectpath(struct vfs_handle_struct *handle,
1304                                        const struct smb_filename *smb_fname)
1305 {
1306         return handle->conn->connectpath;
1307 }
1308
1309 /****************************************************************
1310  Extended attribute operations.
1311 *****************************************************************/
1312
1313 static ssize_t cephwrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
1314 {
1315         int ret;
1316         DBG_DEBUG("[CEPH] fgetxattr(%p, %p, %s, %p, %llu)\n", handle, fsp, name, value, llu(size));
1317         ret = ceph_fgetxattr(handle->data, fsp_get_io_fd(fsp), name, value, size);
1318         DBG_DEBUG("[CEPH] fgetxattr(...) = %d\n", ret);
1319         if (ret < 0) {
1320                 WRAP_RETURN(ret);
1321         }
1322         return (ssize_t)ret;
1323 }
1324
1325 static ssize_t cephwrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
1326 {
1327         int ret;
1328         DBG_DEBUG("[CEPH] flistxattr(%p, %p, %p, %llu)\n",
1329                   handle, fsp, list, llu(size));
1330         if (!fsp->fsp_flags.is_pathref) {
1331                 /*
1332                  * We can use an io_fd to list xattrs.
1333                  */
1334                 ret = ceph_flistxattr(handle->data,
1335                                         fsp_get_io_fd(fsp),
1336                                         list,
1337                                         size);
1338         } else {
1339                 /*
1340                  * This is no longer a handle based call.
1341                  */
1342                 ret = ceph_listxattr(handle->data,
1343                                         fsp->fsp_name->base_name,
1344                                         list,
1345                                         size);
1346         }
1347         DBG_DEBUG("[CEPH] flistxattr(...) = %d\n", ret);
1348         if (ret < 0) {
1349                 WRAP_RETURN(ret);
1350         }
1351         return (ssize_t)ret;
1352 }
1353
1354 static int cephwrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
1355 {
1356         int ret;
1357         DBG_DEBUG("[CEPH] fremovexattr(%p, %p, %s)\n", handle, fsp, name);
1358         if (!fsp->fsp_flags.is_pathref) {
1359                 /*
1360                  * We can use an io_fd to remove xattrs.
1361                  */
1362                 ret = ceph_fremovexattr(handle->data, fsp_get_io_fd(fsp), name);
1363         } else {
1364                 /*
1365                  * This is no longer a handle based call.
1366                  */
1367                 ret = ceph_removexattr(handle->data,
1368                                         fsp->fsp_name->base_name,
1369                                         name);
1370         }
1371         DBG_DEBUG("[CEPH] fremovexattr(...) = %d\n", ret);
1372         WRAP_RETURN(ret);
1373 }
1374
1375 static int cephwrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
1376 {
1377         int ret;
1378         DBG_DEBUG("[CEPH] fsetxattr(%p, %p, %s, %p, %llu, %d)\n", handle, fsp, name, value, llu(size), flags);
1379         if (!fsp->fsp_flags.is_pathref) {
1380                 /*
1381                  * We can use an io_fd to set xattrs.
1382                  */
1383                 ret = ceph_fsetxattr(handle->data,
1384                                 fsp_get_io_fd(fsp),
1385                                 name,
1386                                 value,
1387                                 size,
1388                                 flags);
1389         } else {
1390                 /*
1391                  * This is no longer a handle based call.
1392                  */
1393                 ret = ceph_setxattr(handle->data,
1394                                 fsp->fsp_name->base_name,
1395                                 name,
1396                                 value,
1397                                 size,
1398                                 flags);
1399         }
1400         DBG_DEBUG("[CEPH] fsetxattr(...) = %d\n", ret);
1401         WRAP_RETURN(ret);
1402 }
1403
1404 static bool cephwrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
1405 {
1406
1407         /*
1408          * We do not support AIO yet.
1409          */
1410
1411         DBG_DEBUG("[CEPH] cephwrap_aio_force(%p, %p) = false (errno = ENOTSUP)\n", handle, fsp);
1412         errno = ENOTSUP;
1413         return false;
1414 }
1415
1416 static NTSTATUS cephwrap_create_dfs_pathat(struct vfs_handle_struct *handle,
1417                                 struct files_struct *dirfsp,
1418                                 const struct smb_filename *smb_fname,
1419                                 const struct referral *reflist,
1420                                 size_t referral_count)
1421 {
1422         TALLOC_CTX *frame = talloc_stackframe();
1423         NTSTATUS status = NT_STATUS_NO_MEMORY;
1424         int ret;
1425         char *msdfs_link = NULL;
1426         struct smb_filename *full_fname = NULL;
1427
1428         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1429                                                 dirfsp,
1430                                                 smb_fname);
1431         if (full_fname == NULL) {
1432                 goto out;
1433         }
1434
1435         /* Form the msdfs_link contents */
1436         msdfs_link = msdfs_link_string(frame,
1437                                         reflist,
1438                                         referral_count);
1439         if (msdfs_link == NULL) {
1440                 goto out;
1441         }
1442
1443         ret = ceph_symlink(handle->data,
1444                         msdfs_link,
1445                         full_fname->base_name);
1446         if (ret == 0) {
1447                 status = NT_STATUS_OK;
1448         } else {
1449                 status = map_nt_error_from_unix(-ret);
1450         }
1451
1452   out:
1453
1454         DBG_DEBUG("[CEPH] create_dfs_pathat(%s) = %s\n",
1455                         full_fname != NULL ? full_fname->base_name : "",
1456                         nt_errstr(status));
1457
1458         TALLOC_FREE(frame);
1459         return status;
1460 }
1461
1462 /*
1463  * Read and return the contents of a DFS redirect given a
1464  * pathname. A caller can pass in NULL for ppreflist and
1465  * preferral_count but still determine if this was a
1466  * DFS redirect point by getting NT_STATUS_OK back
1467  * without incurring the overhead of reading and parsing
1468  * the referral contents.
1469  */
1470
1471 static NTSTATUS cephwrap_read_dfs_pathat(struct vfs_handle_struct *handle,
1472                                 TALLOC_CTX *mem_ctx,
1473                                 struct files_struct *dirfsp,
1474                                 struct smb_filename *smb_fname,
1475                                 struct referral **ppreflist,
1476                                 size_t *preferral_count)
1477 {
1478         NTSTATUS status = NT_STATUS_NO_MEMORY;
1479         size_t bufsize;
1480         char *link_target = NULL;
1481         int referral_len;
1482         bool ok;
1483 #if defined(HAVE_BROKEN_READLINK)
1484         char link_target_buf[PATH_MAX];
1485 #else
1486         char link_target_buf[7];
1487 #endif
1488         struct ceph_statx stx;
1489         struct smb_filename *full_fname = NULL;
1490         int ret;
1491
1492         if (is_named_stream(smb_fname)) {
1493                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1494                 goto err;
1495         }
1496
1497         if (ppreflist == NULL && preferral_count == NULL) {
1498                 /*
1499                  * We're only checking if this is a DFS
1500                  * redirect. We don't need to return data.
1501                  */
1502                 bufsize = sizeof(link_target_buf);
1503                 link_target = link_target_buf;
1504         } else {
1505                 bufsize = PATH_MAX;
1506                 link_target = talloc_array(mem_ctx, char, bufsize);
1507                 if (!link_target) {
1508                         goto err;
1509                 }
1510         }
1511
1512         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1513                                                   dirfsp,
1514                                                   smb_fname);
1515         if (full_fname == NULL) {
1516                 status = NT_STATUS_NO_MEMORY;
1517                 goto err;
1518         }
1519
1520         ret = ceph_statx(handle->data,
1521                          full_fname->base_name,
1522                          &stx,
1523                          SAMBA_STATX_ATTR_MASK,
1524                          AT_SYMLINK_NOFOLLOW);
1525         if (ret < 0) {
1526                 status = map_nt_error_from_unix(-ret);
1527                 goto err;
1528         }
1529
1530         referral_len = ceph_readlink(handle->data,
1531                                 full_fname->base_name,
1532                                 link_target,
1533                                 bufsize - 1);
1534         if (referral_len < 0) {
1535                 /* ceph errors are -errno. */
1536                 if (-referral_len == EINVAL) {
1537                         DBG_INFO("%s is not a link.\n",
1538                                 full_fname->base_name);
1539                         status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1540                 } else {
1541                         status = map_nt_error_from_unix(-referral_len);
1542                         DBG_ERR("Error reading "
1543                                 "msdfs link %s: %s\n",
1544                                 full_fname->base_name,
1545                         strerror(errno));
1546                 }
1547                 goto err;
1548         }
1549         link_target[referral_len] = '\0';
1550
1551         DBG_INFO("%s -> %s\n",
1552                         full_fname->base_name,
1553                         link_target);
1554
1555         if (!strnequal(link_target, "msdfs:", 6)) {
1556                 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1557                 goto err;
1558         }
1559
1560         if (ppreflist == NULL && preferral_count == NULL) {
1561                 /* Early return for checking if this is a DFS link. */
1562                 TALLOC_FREE(full_fname);
1563                 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1564                 return NT_STATUS_OK;
1565         }
1566
1567         ok = parse_msdfs_symlink(mem_ctx,
1568                         lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
1569                         link_target,
1570                         ppreflist,
1571                         preferral_count);
1572
1573         if (ok) {
1574                 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1575                 status = NT_STATUS_OK;
1576         } else {
1577                 status = NT_STATUS_NO_MEMORY;
1578         }
1579
1580   err:
1581
1582         if (link_target != link_target_buf) {
1583                 TALLOC_FREE(link_target);
1584         }
1585         TALLOC_FREE(full_fname);
1586         return status;
1587 }
1588
1589 static struct vfs_fn_pointers ceph_fns = {
1590         /* Disk operations */
1591
1592         .connect_fn = cephwrap_connect,
1593         .disconnect_fn = cephwrap_disconnect,
1594         .disk_free_fn = cephwrap_disk_free,
1595         .get_quota_fn = cephwrap_get_quota,
1596         .set_quota_fn = cephwrap_set_quota,
1597         .statvfs_fn = cephwrap_statvfs,
1598         .fs_capabilities_fn = cephwrap_fs_capabilities,
1599
1600         /* Directory operations */
1601
1602         .fdopendir_fn = cephwrap_fdopendir,
1603         .readdir_fn = cephwrap_readdir,
1604         .seekdir_fn = cephwrap_seekdir,
1605         .telldir_fn = cephwrap_telldir,
1606         .rewind_dir_fn = cephwrap_rewinddir,
1607         .mkdirat_fn = cephwrap_mkdirat,
1608         .closedir_fn = cephwrap_closedir,
1609
1610         /* File operations */
1611
1612         .create_dfs_pathat_fn = cephwrap_create_dfs_pathat,
1613         .read_dfs_pathat_fn = cephwrap_read_dfs_pathat,
1614         .openat_fn = cephwrap_openat,
1615         .close_fn = cephwrap_close,
1616         .pread_fn = cephwrap_pread,
1617         .pread_send_fn = cephwrap_pread_send,
1618         .pread_recv_fn = cephwrap_pread_recv,
1619         .pwrite_fn = cephwrap_pwrite,
1620         .pwrite_send_fn = cephwrap_pwrite_send,
1621         .pwrite_recv_fn = cephwrap_pwrite_recv,
1622         .lseek_fn = cephwrap_lseek,
1623         .sendfile_fn = cephwrap_sendfile,
1624         .recvfile_fn = cephwrap_recvfile,
1625         .renameat_fn = cephwrap_renameat,
1626         .fsync_send_fn = cephwrap_fsync_send,
1627         .fsync_recv_fn = cephwrap_fsync_recv,
1628         .stat_fn = cephwrap_stat,
1629         .fstat_fn = cephwrap_fstat,
1630         .lstat_fn = cephwrap_lstat,
1631         .unlinkat_fn = cephwrap_unlinkat,
1632         .fchmod_fn = cephwrap_fchmod,
1633         .fchown_fn = cephwrap_fchown,
1634         .lchown_fn = cephwrap_lchown,
1635         .chdir_fn = cephwrap_chdir,
1636         .getwd_fn = cephwrap_getwd,
1637         .fntimes_fn = cephwrap_fntimes,
1638         .ftruncate_fn = cephwrap_ftruncate,
1639         .fallocate_fn = cephwrap_fallocate,
1640         .lock_fn = cephwrap_lock,
1641         .filesystem_sharemode_fn = cephwrap_filesystem_sharemode,
1642         .fcntl_fn = cephwrap_fcntl,
1643         .linux_setlease_fn = cephwrap_linux_setlease,
1644         .getlock_fn = cephwrap_getlock,
1645         .symlinkat_fn = cephwrap_symlinkat,
1646         .readlinkat_fn = cephwrap_readlinkat,
1647         .linkat_fn = cephwrap_linkat,
1648         .mknodat_fn = cephwrap_mknodat,
1649         .realpath_fn = cephwrap_realpath,
1650         .fchflags_fn = cephwrap_fchflags,
1651         .get_real_filename_fn = cephwrap_get_real_filename,
1652         .get_real_filename_at_fn = cephwrap_get_real_filename_at,
1653         .connectpath_fn = cephwrap_connectpath,
1654
1655         /* EA operations. */
1656         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1657         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1658         .fgetxattr_fn = cephwrap_fgetxattr,
1659         .flistxattr_fn = cephwrap_flistxattr,
1660         .fremovexattr_fn = cephwrap_fremovexattr,
1661         .fsetxattr_fn = cephwrap_fsetxattr,
1662
1663         /* Posix ACL Operations */
1664         .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1665         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1666         .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1667         .sys_acl_delete_def_fd_fn = posixacl_xattr_acl_delete_def_fd,
1668
1669         /* aio operations */
1670         .aio_force_fn = cephwrap_aio_force,
1671 };
1672
1673 static_decl_vfs;
1674 NTSTATUS vfs_ceph_init(TALLOC_CTX *ctx)
1675 {
1676         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1677                                 "ceph", &ceph_fns);
1678 }