vfs: Add dirfsp arg to SMB_VFS_READDIR()
[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         int result;
368         struct smb_filename *parent = NULL;
369         bool ok;
370
371         DBG_DEBUG("[CEPH] mkdir(%p, %s)\n",
372                   handle, smb_fname_str_dbg(smb_fname));
373
374         SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
375
376         if (lp_inherit_acls(SNUM(handle->conn))) {
377                 ok = parent_smb_fname(talloc_tos(), smb_fname, &parent, NULL);
378                 if (ok && directory_has_default_acl(handle->conn,
379                                 dirfsp,
380                                 parent))
381                 {
382                         mode = 0777;
383                 }
384         }
385
386         TALLOC_FREE(parent);
387
388         result = ceph_mkdir(handle->data, smb_fname->base_name, mode);
389         return WRAP_RETURN(result);
390 }
391
392 static int cephwrap_closedir(struct vfs_handle_struct *handle, DIR *dirp)
393 {
394         int result;
395
396         DBG_DEBUG("[CEPH] closedir(%p, %p)\n", handle, dirp);
397         result = ceph_closedir(handle->data, (struct ceph_dir_result *) dirp);
398         DBG_DEBUG("[CEPH] closedir(...) = %d\n", result);
399         WRAP_RETURN(result);
400 }
401
402 /* File operations */
403
404 static int cephwrap_openat(struct vfs_handle_struct *handle,
405                            const struct files_struct *dirfsp,
406                            const struct smb_filename *smb_fname,
407                            files_struct *fsp,
408                            int flags,
409                            mode_t mode)
410 {
411         bool have_opath = false;
412         bool became_root = false;
413         int result = -ENOENT;
414
415         /*
416          * cephfs API doesn't have ceph_openat(), so for now assert this.
417          */
418         SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
419
420         DBG_DEBUG("[CEPH] openat(%p, %s, %p, %d, %d)\n", handle,
421                   smb_fname_str_dbg(smb_fname), fsp, flags, mode);
422
423         if (smb_fname->stream_name) {
424                 goto out;
425         }
426
427 #ifdef O_PATH
428         have_opath = true;
429         if (fsp->fsp_flags.is_pathref) {
430                 flags |= O_PATH;
431         }
432 #endif
433
434         if (fsp->fsp_flags.is_pathref && !have_opath) {
435                 become_root();
436                 became_root = true;
437         }
438
439         result = ceph_open(handle->data, smb_fname->base_name, flags, mode);
440
441         if (became_root) {
442                 unbecome_root();
443         }
444
445 out:
446         fsp->fsp_flags.have_proc_fds = false;
447         DBG_DEBUG("[CEPH] open(...) = %d\n", result);
448         WRAP_RETURN(result);
449 }
450
451 static int cephwrap_close(struct vfs_handle_struct *handle, files_struct *fsp)
452 {
453         int result;
454
455         DBG_DEBUG("[CEPH] close(%p, %p)\n", handle, fsp);
456         result = ceph_close(handle->data, fsp_get_io_fd(fsp));
457         DBG_DEBUG("[CEPH] close(...) = %d\n", result);
458
459         WRAP_RETURN(result);
460 }
461
462 static ssize_t cephwrap_pread(struct vfs_handle_struct *handle, files_struct *fsp, void *data,
463                         size_t n, off_t offset)
464 {
465         ssize_t result;
466
467         DBG_DEBUG("[CEPH] pread(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
468
469         result = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
470         DBG_DEBUG("[CEPH] pread(...) = %llu\n", llu(result));
471         WRAP_RETURN(result);
472 }
473
474 struct cephwrap_pread_state {
475         ssize_t bytes_read;
476         struct vfs_aio_state vfs_aio_state;
477 };
478
479 /*
480  * Fake up an async ceph read by calling the synchronous API.
481  */
482 static struct tevent_req *cephwrap_pread_send(struct vfs_handle_struct *handle,
483                                               TALLOC_CTX *mem_ctx,
484                                               struct tevent_context *ev,
485                                               struct files_struct *fsp,
486                                               void *data,
487                                               size_t n, off_t offset)
488 {
489         struct tevent_req *req = NULL;
490         struct cephwrap_pread_state *state = NULL;
491         int ret = -1;
492
493         DBG_DEBUG("[CEPH] %s\n", __func__);
494         req = tevent_req_create(mem_ctx, &state, struct cephwrap_pread_state);
495         if (req == NULL) {
496                 return NULL;
497         }
498
499         ret = ceph_read(handle->data, fsp_get_io_fd(fsp), data, n, offset);
500         if (ret < 0) {
501                 /* ceph returns -errno on error. */
502                 tevent_req_error(req, -ret);
503                 return tevent_req_post(req, ev);
504         }
505
506         state->bytes_read = ret;
507         tevent_req_done(req);
508         /* Return and schedule the completion of the call. */
509         return tevent_req_post(req, ev);
510 }
511
512 static ssize_t cephwrap_pread_recv(struct tevent_req *req,
513                                    struct vfs_aio_state *vfs_aio_state)
514 {
515         struct cephwrap_pread_state *state =
516                 tevent_req_data(req, struct cephwrap_pread_state);
517
518         DBG_DEBUG("[CEPH] %s\n", __func__);
519         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
520                 return -1;
521         }
522         *vfs_aio_state = state->vfs_aio_state;
523         return state->bytes_read;
524 }
525
526 static ssize_t cephwrap_pwrite(struct vfs_handle_struct *handle, files_struct *fsp, const void *data,
527                         size_t n, off_t offset)
528 {
529         ssize_t result;
530
531         DBG_DEBUG("[CEPH] pwrite(%p, %p, %p, %llu, %llu)\n", handle, fsp, data, llu(n), llu(offset));
532         result = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
533         DBG_DEBUG("[CEPH] pwrite(...) = %llu\n", llu(result));
534         WRAP_RETURN(result);
535 }
536
537 struct cephwrap_pwrite_state {
538         ssize_t bytes_written;
539         struct vfs_aio_state vfs_aio_state;
540 };
541
542 /*
543  * Fake up an async ceph write by calling the synchronous API.
544  */
545 static struct tevent_req *cephwrap_pwrite_send(struct vfs_handle_struct *handle,
546                                                TALLOC_CTX *mem_ctx,
547                                                struct tevent_context *ev,
548                                                struct files_struct *fsp,
549                                                const void *data,
550                                                size_t n, off_t offset)
551 {
552         struct tevent_req *req = NULL;
553         struct cephwrap_pwrite_state *state = NULL;
554         int ret = -1;
555
556         DBG_DEBUG("[CEPH] %s\n", __func__);
557         req = tevent_req_create(mem_ctx, &state, struct cephwrap_pwrite_state);
558         if (req == NULL) {
559                 return NULL;
560         }
561
562         ret = ceph_write(handle->data, fsp_get_io_fd(fsp), data, n, offset);
563         if (ret < 0) {
564                 /* ceph returns -errno on error. */
565                 tevent_req_error(req, -ret);
566                 return tevent_req_post(req, ev);
567         }
568
569         state->bytes_written = ret;
570         tevent_req_done(req);
571         /* Return and schedule the completion of the call. */
572         return tevent_req_post(req, ev);
573 }
574
575 static ssize_t cephwrap_pwrite_recv(struct tevent_req *req,
576                                     struct vfs_aio_state *vfs_aio_state)
577 {
578         struct cephwrap_pwrite_state *state =
579                 tevent_req_data(req, struct cephwrap_pwrite_state);
580
581         DBG_DEBUG("[CEPH] %s\n", __func__);
582         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
583                 return -1;
584         }
585         *vfs_aio_state = state->vfs_aio_state;
586         return state->bytes_written;
587 }
588
589 static off_t cephwrap_lseek(struct vfs_handle_struct *handle, files_struct *fsp, off_t offset, int whence)
590 {
591         off_t result = 0;
592
593         DBG_DEBUG("[CEPH] cephwrap_lseek\n");
594         result = ceph_lseek(handle->data, fsp_get_io_fd(fsp), offset, whence);
595         WRAP_RETURN(result);
596 }
597
598 static ssize_t cephwrap_sendfile(struct vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
599                         off_t offset, size_t n)
600 {
601         /*
602          * We cannot support sendfile because libceph is in user space.
603          */
604         DBG_DEBUG("[CEPH] cephwrap_sendfile\n");
605         errno = ENOTSUP;
606         return -1;
607 }
608
609 static ssize_t cephwrap_recvfile(struct vfs_handle_struct *handle,
610                         int fromfd,
611                         files_struct *tofsp,
612                         off_t offset,
613                         size_t n)
614 {
615         /*
616          * We cannot support recvfile because libceph is in user space.
617          */
618         DBG_DEBUG("[CEPH] cephwrap_recvfile\n");
619         errno=ENOTSUP;
620         return -1;
621 }
622
623 static int cephwrap_renameat(struct vfs_handle_struct *handle,
624                         files_struct *srcfsp,
625                         const struct smb_filename *smb_fname_src,
626                         files_struct *dstfsp,
627                         const struct smb_filename *smb_fname_dst)
628 {
629         int result = -1;
630         DBG_DEBUG("[CEPH] cephwrap_renameat\n");
631         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
632                 errno = ENOENT;
633                 return result;
634         }
635
636         SMB_ASSERT(srcfsp == srcfsp->conn->cwd_fsp);
637         SMB_ASSERT(dstfsp == dstfsp->conn->cwd_fsp);
638
639         result = ceph_rename(handle->data, smb_fname_src->base_name, smb_fname_dst->base_name);
640         WRAP_RETURN(result);
641 }
642
643 /*
644  * Fake up an async ceph fsync by calling the synchronous API.
645  */
646
647 static struct tevent_req *cephwrap_fsync_send(struct vfs_handle_struct *handle,
648                                         TALLOC_CTX *mem_ctx,
649                                         struct tevent_context *ev,
650                                         files_struct *fsp)
651 {
652         struct tevent_req *req = NULL;
653         struct vfs_aio_state *state = NULL;
654         int ret = -1;
655
656         DBG_DEBUG("[CEPH] cephwrap_fsync_send\n");
657
658         req = tevent_req_create(mem_ctx, &state, struct vfs_aio_state);
659         if (req == NULL) {
660                 return NULL;
661         }
662
663         /* Make sync call. */
664         ret = ceph_fsync(handle->data, fsp_get_io_fd(fsp), false);
665
666         if (ret != 0) {
667                 /* ceph_fsync returns -errno on error. */
668                 tevent_req_error(req, -ret);
669                 return tevent_req_post(req, ev);
670         }
671
672         /* Mark it as done. */
673         tevent_req_done(req);
674         /* Return and schedule the completion of the call. */
675         return tevent_req_post(req, ev);
676 }
677
678 static int cephwrap_fsync_recv(struct tevent_req *req,
679                                 struct vfs_aio_state *vfs_aio_state)
680 {
681         struct vfs_aio_state *state =
682                 tevent_req_data(req, struct vfs_aio_state);
683
684         DBG_DEBUG("[CEPH] cephwrap_fsync_recv\n");
685
686         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
687                 return -1;
688         }
689         *vfs_aio_state = *state;
690         return 0;
691 }
692
693 #define SAMBA_STATX_ATTR_MASK   (CEPH_STATX_BASIC_STATS|CEPH_STATX_BTIME)
694
695 static void init_stat_ex_from_ceph_statx(struct stat_ex *dst, const struct ceph_statx *stx)
696 {
697         DBG_DEBUG("[CEPH]\tstx = {dev = %llx, ino = %llu, mode = 0x%x, "
698                   "nlink = %llu, uid = %d, gid = %d, rdev = %llx, size = %llu, "
699                   "blksize = %llu, blocks = %llu, atime = %llu, mtime = %llu, "
700                   "ctime = %llu, btime = %llu}\n",
701                   llu(stx->stx_dev), llu(stx->stx_ino), stx->stx_mode,
702                   llu(stx->stx_nlink), stx->stx_uid, stx->stx_gid,
703                   llu(stx->stx_rdev), llu(stx->stx_size), llu(stx->stx_blksize),
704                   llu(stx->stx_blocks), llu(stx->stx_atime.tv_sec),
705                   llu(stx->stx_mtime.tv_sec), llu(stx->stx_ctime.tv_sec),
706                   llu(stx->stx_btime.tv_sec));
707
708         if ((stx->stx_mask & SAMBA_STATX_ATTR_MASK) != SAMBA_STATX_ATTR_MASK) {
709                 DBG_WARNING("%s: stx->stx_mask is incorrect (wanted %x, got %x)",
710                                 __func__, SAMBA_STATX_ATTR_MASK, stx->stx_mask);
711         }
712
713         dst->st_ex_dev = stx->stx_dev;
714         dst->st_ex_rdev = stx->stx_rdev;
715         dst->st_ex_ino = stx->stx_ino;
716         dst->st_ex_mode = stx->stx_mode;
717         dst->st_ex_uid = stx->stx_uid;
718         dst->st_ex_gid = stx->stx_gid;
719         dst->st_ex_size = stx->stx_size;
720         dst->st_ex_nlink = stx->stx_nlink;
721         dst->st_ex_atime = stx->stx_atime;
722         dst->st_ex_btime = stx->stx_btime;
723         dst->st_ex_ctime = stx->stx_ctime;
724         dst->st_ex_mtime = stx->stx_mtime;
725         dst->st_ex_itime = dst->st_ex_btime;
726         dst->st_ex_iflags = ST_EX_IFLAG_CALCULATED_ITIME;
727         dst->st_ex_blksize = stx->stx_blksize;
728         dst->st_ex_blocks = stx->stx_blocks;
729         dst->st_ex_file_id = dst->st_ex_ino;
730         dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_FILE_ID;
731 }
732
733 static int cephwrap_stat(struct vfs_handle_struct *handle,
734                         struct smb_filename *smb_fname)
735 {
736         int result = -1;
737         struct ceph_statx stx;
738
739         DBG_DEBUG("[CEPH] stat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
740
741         if (smb_fname->stream_name) {
742                 errno = ENOENT;
743                 return result;
744         }
745
746         result = ceph_statx(handle->data, smb_fname->base_name, &stx,
747                                 SAMBA_STATX_ATTR_MASK, 0);
748         DBG_DEBUG("[CEPH] statx(...) = %d\n", result);
749         if (result < 0) {
750                 WRAP_RETURN(result);
751         }
752
753         init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
754         DBG_DEBUG("[CEPH] mode = 0x%x\n", smb_fname->st.st_ex_mode);
755         return result;
756 }
757
758 static int cephwrap_fstat(struct vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
759 {
760         int result = -1;
761         struct ceph_statx stx;
762
763         DBG_DEBUG("[CEPH] fstat(%p, %d)\n", handle, fsp_get_io_fd(fsp));
764         result = ceph_fstatx(handle->data, fsp_get_io_fd(fsp), &stx,
765                                 SAMBA_STATX_ATTR_MASK, 0);
766         DBG_DEBUG("[CEPH] fstat(...) = %d\n", result);
767         if (result < 0) {
768                 WRAP_RETURN(result);
769         }
770
771         init_stat_ex_from_ceph_statx(sbuf, &stx);
772         DBG_DEBUG("[CEPH] mode = 0x%x\n", sbuf->st_ex_mode);
773         return result;
774 }
775
776 static int cephwrap_lstat(struct vfs_handle_struct *handle,
777                          struct smb_filename *smb_fname)
778 {
779         int result = -1;
780         struct ceph_statx stx;
781
782         DBG_DEBUG("[CEPH] lstat(%p, %s)\n", handle, smb_fname_str_dbg(smb_fname));
783
784         if (smb_fname->stream_name) {
785                 errno = ENOENT;
786                 return result;
787         }
788
789         result = ceph_statx(handle->data, smb_fname->base_name, &stx,
790                                 SAMBA_STATX_ATTR_MASK, AT_SYMLINK_NOFOLLOW);
791         DBG_DEBUG("[CEPH] lstat(...) = %d\n", result);
792         if (result < 0) {
793                 WRAP_RETURN(result);
794         }
795
796         init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
797         return result;
798 }
799
800 static int cephwrap_ntimes(struct vfs_handle_struct *handle,
801                          const struct smb_filename *smb_fname,
802                          struct smb_file_time *ft)
803 {
804         struct ceph_statx stx = { 0 };
805         int result;
806         int mask = 0;
807
808         if (!is_omit_timespec(&ft->atime)) {
809                 stx.stx_atime = ft->atime;
810                 mask |= CEPH_SETATTR_ATIME;
811         }
812         if (!is_omit_timespec(&ft->mtime)) {
813                 stx.stx_mtime = ft->mtime;
814                 mask |= CEPH_SETATTR_MTIME;
815         }
816         if (!is_omit_timespec(&ft->create_time)) {
817                 stx.stx_btime = ft->create_time;
818                 mask |= CEPH_SETATTR_BTIME;
819         }
820
821         if (!mask) {
822                 return 0;
823         }
824
825         result = ceph_setattrx(handle->data, smb_fname->base_name, &stx, mask, 0);
826         DBG_DEBUG("[CEPH] ntimes(%p, %s, {%ld, %ld, %ld, %ld}) = %d\n", handle, smb_fname_str_dbg(smb_fname),
827                                 ft->mtime.tv_sec, ft->atime.tv_sec, ft->ctime.tv_sec,
828                                 ft->create_time.tv_sec, result);
829         return result;
830 }
831
832 static int cephwrap_unlinkat(struct vfs_handle_struct *handle,
833                         struct files_struct *dirfsp,
834                         const struct smb_filename *smb_fname,
835                         int flags)
836 {
837         int result = -1;
838
839         DBG_DEBUG("[CEPH] unlink(%p, %s)\n",
840                 handle,
841                 smb_fname_str_dbg(smb_fname));
842         SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
843         if (smb_fname->stream_name) {
844                 errno = ENOENT;
845                 return result;
846         }
847         if (flags & AT_REMOVEDIR) {
848                 result = ceph_rmdir(handle->data, smb_fname->base_name);
849         } else {
850                 result = ceph_unlink(handle->data, smb_fname->base_name);
851         }
852         DBG_DEBUG("[CEPH] unlink(...) = %d\n", result);
853         WRAP_RETURN(result);
854 }
855
856 static int cephwrap_chmod(struct vfs_handle_struct *handle,
857                         const struct smb_filename *smb_fname,
858                         mode_t mode)
859 {
860         int result;
861
862         DBG_DEBUG("[CEPH] chmod(%p, %s, %d)\n", handle, smb_fname->base_name, mode);
863         result = ceph_chmod(handle->data, smb_fname->base_name, mode);
864         DBG_DEBUG("[CEPH] chmod(...) = %d\n", result);
865         WRAP_RETURN(result);
866 }
867
868 static int cephwrap_fchmod(struct vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
869 {
870         int result;
871
872         DBG_DEBUG("[CEPH] fchmod(%p, %p, %d)\n", handle, fsp, mode);
873         result = ceph_fchmod(handle->data, fsp_get_io_fd(fsp), mode);
874         DBG_DEBUG("[CEPH] fchmod(...) = %d\n", result);
875         WRAP_RETURN(result);
876 }
877
878 static int cephwrap_fchown(struct vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
879 {
880         int result;
881
882         DBG_DEBUG("[CEPH] fchown(%p, %p, %d, %d)\n", handle, fsp, uid, gid);
883         result = ceph_fchown(handle->data, fsp_get_io_fd(fsp), uid, gid);
884         DBG_DEBUG("[CEPH] fchown(...) = %d\n", result);
885         WRAP_RETURN(result);
886 }
887
888 static int cephwrap_lchown(struct vfs_handle_struct *handle,
889                         const struct smb_filename *smb_fname,
890                         uid_t uid,
891                         gid_t gid)
892 {
893         int result;
894         DBG_DEBUG("[CEPH] lchown(%p, %s, %d, %d)\n", handle, smb_fname->base_name, uid, gid);
895         result = ceph_lchown(handle->data, smb_fname->base_name, uid, gid);
896         DBG_DEBUG("[CEPH] lchown(...) = %d\n", result);
897         WRAP_RETURN(result);
898 }
899
900 static int cephwrap_chdir(struct vfs_handle_struct *handle,
901                         const struct smb_filename *smb_fname)
902 {
903         int result = -1;
904         DBG_DEBUG("[CEPH] chdir(%p, %s)\n", handle, smb_fname->base_name);
905         result = ceph_chdir(handle->data, smb_fname->base_name);
906         DBG_DEBUG("[CEPH] chdir(...) = %d\n", result);
907         WRAP_RETURN(result);
908 }
909
910 static struct smb_filename *cephwrap_getwd(struct vfs_handle_struct *handle,
911                         TALLOC_CTX *ctx)
912 {
913         const char *cwd = ceph_getcwd(handle->data);
914         DBG_DEBUG("[CEPH] getwd(%p) = %s\n", handle, cwd);
915         return synthetic_smb_fname(ctx,
916                                 cwd,
917                                 NULL,
918                                 NULL,
919                                 0,
920                                 0);
921 }
922
923 static int strict_allocate_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
924 {
925         off_t space_to_write;
926         int result;
927         NTSTATUS status;
928         SMB_STRUCT_STAT *pst;
929
930         status = vfs_stat_fsp(fsp);
931         if (!NT_STATUS_IS_OK(status)) {
932                 return -1;
933         }
934         pst = &fsp->fsp_name->st;
935
936 #ifdef S_ISFIFO
937         if (S_ISFIFO(pst->st_ex_mode))
938                 return 0;
939 #endif
940
941         if (pst->st_ex_size == len)
942                 return 0;
943
944         /* Shrink - just ftruncate. */
945         if (pst->st_ex_size > len) {
946                 result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
947                 WRAP_RETURN(result);
948         }
949
950         space_to_write = len - pst->st_ex_size;
951         result = ceph_fallocate(handle->data, fsp_get_io_fd(fsp), 0, pst->st_ex_size,
952                                 space_to_write);
953         WRAP_RETURN(result);
954 }
955
956 static int cephwrap_ftruncate(struct vfs_handle_struct *handle, files_struct *fsp, off_t len)
957 {
958         int result = -1;
959
960         DBG_DEBUG("[CEPH] ftruncate(%p, %p, %llu\n", handle, fsp, llu(len));
961
962         if (lp_strict_allocate(SNUM(fsp->conn))) {
963                 return strict_allocate_ftruncate(handle, fsp, len);
964         }
965
966         result = ceph_ftruncate(handle->data, fsp_get_io_fd(fsp), len);
967         WRAP_RETURN(result);
968 }
969
970 static int cephwrap_fallocate(struct vfs_handle_struct *handle,
971                               struct files_struct *fsp,
972                               uint32_t mode,
973                               off_t offset,
974                               off_t len)
975 {
976         int result;
977
978         DBG_DEBUG("[CEPH] fallocate(%p, %p, %u, %llu, %llu\n",
979                   handle, fsp, mode, llu(offset), llu(len));
980         /* unsupported mode flags are rejected by libcephfs */
981         result = ceph_fallocate(handle->data, fsp_get_io_fd(fsp), mode, offset, len);
982         DBG_DEBUG("[CEPH] fallocate(...) = %d\n", result);
983         WRAP_RETURN(result);
984 }
985
986 static bool cephwrap_lock(struct vfs_handle_struct *handle, files_struct *fsp, int op, off_t offset, off_t count, int type)
987 {
988         DBG_DEBUG("[CEPH] lock\n");
989         return true;
990 }
991
992 static int cephwrap_kernel_flock(struct vfs_handle_struct *handle,
993                                  files_struct *fsp,
994                                  uint32_t share_access,
995                                  uint32_t access_mask)
996 {
997         DBG_ERR("[CEPH] flock unsupported! Consider setting "
998                 "\"kernel share modes = no\"\n");
999
1000         errno = ENOSYS;
1001         return -1;
1002 }
1003
1004 static int cephwrap_fcntl(vfs_handle_struct *handle,
1005                           files_struct *fsp, int cmd, va_list cmd_arg)
1006 {
1007         /*
1008          * SMB_VFS_FCNTL() is currently only called by vfs_set_blocking() to
1009          * clear O_NONBLOCK, etc for LOCK_MAND and FIFOs. Ignore it.
1010          */
1011         if (cmd == F_GETFL) {
1012                 return 0;
1013         } else if (cmd == F_SETFL) {
1014                 va_list dup_cmd_arg;
1015                 int opt;
1016
1017                 va_copy(dup_cmd_arg, cmd_arg);
1018                 opt = va_arg(dup_cmd_arg, int);
1019                 va_end(dup_cmd_arg);
1020                 if (opt == 0) {
1021                         return 0;
1022                 }
1023                 DBG_ERR("unexpected fcntl SETFL(%d)\n", opt);
1024                 goto err_out;
1025         }
1026         DBG_ERR("unexpected fcntl: %d\n", cmd);
1027 err_out:
1028         errno = EINVAL;
1029         return -1;
1030 }
1031
1032 static bool cephwrap_getlock(struct vfs_handle_struct *handle, files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1033 {
1034         DBG_DEBUG("[CEPH] getlock returning false and errno=0\n");
1035
1036         errno = 0;
1037         return false;
1038 }
1039
1040 /*
1041  * We cannot let this fall through to the default, because the file might only
1042  * be accessible from libceph (which is a user-space client) but the fd might
1043  * be for some file the kernel knows about.
1044  */
1045 static int cephwrap_linux_setlease(struct vfs_handle_struct *handle, files_struct *fsp,
1046                                 int leasetype)
1047 {
1048         int result = -1;
1049
1050         DBG_DEBUG("[CEPH] linux_setlease\n");
1051         errno = ENOSYS;
1052         return result;
1053 }
1054
1055 static int cephwrap_symlinkat(struct vfs_handle_struct *handle,
1056                 const struct smb_filename *link_target,
1057                 struct files_struct *dirfsp,
1058                 const struct smb_filename *new_smb_fname)
1059 {
1060         int result = -1;
1061         DBG_DEBUG("[CEPH] symlink(%p, %s, %s)\n", handle,
1062                         link_target->base_name,
1063                         new_smb_fname->base_name);
1064
1065         SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1066
1067         result = ceph_symlink(handle->data,
1068                         link_target->base_name,
1069                         new_smb_fname->base_name);
1070         DBG_DEBUG("[CEPH] symlink(...) = %d\n", result);
1071         WRAP_RETURN(result);
1072 }
1073
1074 static int cephwrap_readlinkat(struct vfs_handle_struct *handle,
1075                 const struct files_struct *dirfsp,
1076                 const struct smb_filename *smb_fname,
1077                 char *buf,
1078                 size_t bufsiz)
1079 {
1080         int result = -1;
1081         DBG_DEBUG("[CEPH] readlink(%p, %s, %p, %llu)\n", handle,
1082                         smb_fname->base_name, buf, llu(bufsiz));
1083
1084         SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1085
1086         result = ceph_readlink(handle->data, smb_fname->base_name, buf, bufsiz);
1087         DBG_DEBUG("[CEPH] readlink(...) = %d\n", result);
1088         WRAP_RETURN(result);
1089 }
1090
1091 static int cephwrap_linkat(struct vfs_handle_struct *handle,
1092                 files_struct *srcfsp,
1093                 const struct smb_filename *old_smb_fname,
1094                 files_struct *dstfsp,
1095                 const struct smb_filename *new_smb_fname,
1096                 int flags)
1097 {
1098         int result = -1;
1099         DBG_DEBUG("[CEPH] link(%p, %s, %s)\n", handle,
1100                         old_smb_fname->base_name,
1101                         new_smb_fname->base_name);
1102
1103         SMB_ASSERT(srcfsp == srcfsp->conn->cwd_fsp);
1104         SMB_ASSERT(dstfsp == dstfsp->conn->cwd_fsp);
1105
1106         result = ceph_link(handle->data,
1107                                 old_smb_fname->base_name,
1108                                 new_smb_fname->base_name);
1109         DBG_DEBUG("[CEPH] link(...) = %d\n", result);
1110         WRAP_RETURN(result);
1111 }
1112
1113 static int cephwrap_mknodat(struct vfs_handle_struct *handle,
1114                 files_struct *dirfsp,
1115                 const struct smb_filename *smb_fname,
1116                 mode_t mode,
1117                 SMB_DEV_T dev)
1118 {
1119         int result = -1;
1120         DBG_DEBUG("[CEPH] mknodat(%p, %s)\n", handle, smb_fname->base_name);
1121         SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1122         result = ceph_mknod(handle->data, smb_fname->base_name, mode, dev);
1123         DBG_DEBUG("[CEPH] mknodat(...) = %d\n", result);
1124         WRAP_RETURN(result);
1125 }
1126
1127 /*
1128  * This is a simple version of real-path ... a better version is needed to
1129  * ask libceph about symbolic links.
1130  */
1131 static struct smb_filename *cephwrap_realpath(struct vfs_handle_struct *handle,
1132                                 TALLOC_CTX *ctx,
1133                                 const struct smb_filename *smb_fname)
1134 {
1135         char *result = NULL;
1136         const char *path = smb_fname->base_name;
1137         size_t len = strlen(path);
1138         struct smb_filename *result_fname = NULL;
1139         int r = -1;
1140
1141         if (len && (path[0] == '/')) {
1142                 r = asprintf(&result, "%s", path);
1143         } else if ((len >= 2) && (path[0] == '.') && (path[1] == '/')) {
1144                 if (len == 2) {
1145                         r = asprintf(&result, "%s",
1146                                         handle->conn->cwd_fsp->fsp_name->base_name);
1147                 } else {
1148                         r = asprintf(&result, "%s/%s",
1149                                         handle->conn->cwd_fsp->fsp_name->base_name, &path[2]);
1150                 }
1151         } else {
1152                 r = asprintf(&result, "%s/%s",
1153                                 handle->conn->cwd_fsp->fsp_name->base_name, path);
1154         }
1155
1156         if (r < 0) {
1157                 return NULL;
1158         }
1159
1160         DBG_DEBUG("[CEPH] realpath(%p, %s) = %s\n", handle, path, result);
1161         result_fname = synthetic_smb_fname(ctx,
1162                                 result,
1163                                 NULL,
1164                                 NULL,
1165                                 0,
1166                                 0);
1167         SAFE_FREE(result);
1168         return result_fname;
1169 }
1170
1171 static int cephwrap_chflags(struct vfs_handle_struct *handle,
1172                         const struct smb_filename *smb_fname,
1173                         unsigned int flags)
1174 {
1175         errno = ENOSYS;
1176         return -1;
1177 }
1178
1179 static int cephwrap_get_real_filename(struct vfs_handle_struct *handle,
1180                                      const struct smb_filename *path,
1181                                      const char *name,
1182                                      TALLOC_CTX *mem_ctx,
1183                                      char **found_name)
1184 {
1185         /*
1186          * Don't fall back to get_real_filename so callers can differentiate
1187          * between a full directory scan and an actual case-insensitive stat.
1188          */
1189         errno = EOPNOTSUPP;
1190         return -1;
1191 }
1192
1193 static const char *cephwrap_connectpath(struct vfs_handle_struct *handle,
1194                                        const struct smb_filename *smb_fname)
1195 {
1196         return handle->conn->connectpath;
1197 }
1198
1199 /****************************************************************
1200  Extended attribute operations.
1201 *****************************************************************/
1202
1203 static ssize_t cephwrap_getxattr(struct vfs_handle_struct *handle,
1204                         const struct smb_filename *smb_fname,
1205                         const char *name,
1206                         void *value,
1207                         size_t size)
1208 {
1209         int ret;
1210         DBG_DEBUG("[CEPH] getxattr(%p, %s, %s, %p, %llu)\n", handle,
1211                         smb_fname->base_name, name, value, llu(size));
1212         ret = ceph_getxattr(handle->data,
1213                         smb_fname->base_name, name, value, size);
1214         DBG_DEBUG("[CEPH] getxattr(...) = %d\n", ret);
1215         if (ret < 0) {
1216                 WRAP_RETURN(ret);
1217         }
1218         return (ssize_t)ret;
1219 }
1220
1221 static ssize_t cephwrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
1222 {
1223         int ret;
1224         DBG_DEBUG("[CEPH] fgetxattr(%p, %p, %s, %p, %llu)\n", handle, fsp, name, value, llu(size));
1225         ret = ceph_fgetxattr(handle->data, fsp_get_io_fd(fsp), name, value, size);
1226         DBG_DEBUG("[CEPH] fgetxattr(...) = %d\n", ret);
1227         if (ret < 0) {
1228                 WRAP_RETURN(ret);
1229         }
1230         return (ssize_t)ret;
1231 }
1232
1233 static ssize_t cephwrap_listxattr(struct vfs_handle_struct *handle,
1234                         const struct smb_filename *smb_fname,
1235                         char *list,
1236                         size_t size)
1237 {
1238         int ret;
1239         DBG_DEBUG("[CEPH] listxattr(%p, %s, %p, %llu)\n", handle,
1240                         smb_fname->base_name, list, llu(size));
1241         ret = ceph_listxattr(handle->data, smb_fname->base_name, list, size);
1242         DBG_DEBUG("[CEPH] listxattr(...) = %d\n", ret);
1243         if (ret < 0) {
1244                 WRAP_RETURN(ret);
1245         }
1246         return (ssize_t)ret;
1247 }
1248
1249 static ssize_t cephwrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
1250 {
1251         int ret;
1252         DBG_DEBUG("[CEPH] flistxattr(%p, %p, %p, %llu)\n",
1253                   handle, fsp, list, llu(size));
1254         ret = ceph_flistxattr(handle->data, fsp_get_io_fd(fsp), list, size);
1255         DBG_DEBUG("[CEPH] flistxattr(...) = %d\n", ret);
1256         if (ret < 0) {
1257                 WRAP_RETURN(ret);
1258         }
1259         return (ssize_t)ret;
1260 }
1261
1262 static int cephwrap_removexattr(struct vfs_handle_struct *handle,
1263                                 const struct smb_filename *smb_fname,
1264                                 const char *name)
1265 {
1266         int ret;
1267         DBG_DEBUG("[CEPH] removexattr(%p, %s, %s)\n", handle,
1268                         smb_fname->base_name, name);
1269         ret = ceph_removexattr(handle->data, smb_fname->base_name, name);
1270         DBG_DEBUG("[CEPH] removexattr(...) = %d\n", ret);
1271         WRAP_RETURN(ret);
1272 }
1273
1274 static int cephwrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
1275 {
1276         int ret;
1277         DBG_DEBUG("[CEPH] fremovexattr(%p, %p, %s)\n", handle, fsp, name);
1278         ret = ceph_fremovexattr(handle->data, fsp_get_io_fd(fsp), name);
1279         DBG_DEBUG("[CEPH] fremovexattr(...) = %d\n", ret);
1280         WRAP_RETURN(ret);
1281 }
1282
1283 static int cephwrap_setxattr(struct vfs_handle_struct *handle,
1284                                 const struct smb_filename *smb_fname,
1285                                 const char *name,
1286                                 const void *value,
1287                                 size_t size,
1288                                 int flags)
1289 {
1290         int ret;
1291         DBG_DEBUG("[CEPH] setxattr(%p, %s, %s, %p, %llu, %d)\n", handle,
1292                         smb_fname->base_name, name, value, llu(size), flags);
1293         ret = ceph_setxattr(handle->data, smb_fname->base_name,
1294                         name, value, size, flags);
1295         DBG_DEBUG("[CEPH] setxattr(...) = %d\n", ret);
1296         WRAP_RETURN(ret);
1297 }
1298
1299 static int cephwrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
1300 {
1301         int ret;
1302         DBG_DEBUG("[CEPH] fsetxattr(%p, %p, %s, %p, %llu, %d)\n", handle, fsp, name, value, llu(size), flags);
1303         ret = ceph_fsetxattr(handle->data, fsp_get_io_fd(fsp),
1304                              name, value, size, flags);
1305         DBG_DEBUG("[CEPH] fsetxattr(...) = %d\n", ret);
1306         WRAP_RETURN(ret);
1307 }
1308
1309 static bool cephwrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
1310 {
1311
1312         /*
1313          * We do not support AIO yet.
1314          */
1315
1316         DBG_DEBUG("[CEPH] cephwrap_aio_force(%p, %p) = false (errno = ENOTSUP)\n", handle, fsp);
1317         errno = ENOTSUP;
1318         return false;
1319 }
1320
1321 static NTSTATUS cephwrap_create_dfs_pathat(struct vfs_handle_struct *handle,
1322                                 struct files_struct *dirfsp,
1323                                 const struct smb_filename *smb_fname,
1324                                 const struct referral *reflist,
1325                                 size_t referral_count)
1326 {
1327         TALLOC_CTX *frame = talloc_stackframe();
1328         NTSTATUS status = NT_STATUS_NO_MEMORY;
1329         int ret;
1330         char *msdfs_link = NULL;
1331
1332         SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1333
1334         /* Form the msdfs_link contents */
1335         msdfs_link = msdfs_link_string(frame,
1336                                         reflist,
1337                                         referral_count);
1338         if (msdfs_link == NULL) {
1339                 goto out;
1340         }
1341
1342         ret = ceph_symlink(handle->data,
1343                         msdfs_link,
1344                         smb_fname->base_name);
1345         if (ret == 0) {
1346                 status = NT_STATUS_OK;
1347         } else {
1348                 status = map_nt_error_from_unix(-ret);
1349         }
1350
1351   out:
1352
1353         DBG_DEBUG("[CEPH] create_dfs_pathat(%s) = %s\n",
1354                         smb_fname->base_name,
1355                         nt_errstr(status));
1356
1357         TALLOC_FREE(frame);
1358         return status;
1359 }
1360
1361 /*
1362  * Read and return the contents of a DFS redirect given a
1363  * pathname. A caller can pass in NULL for ppreflist and
1364  * preferral_count but still determine if this was a
1365  * DFS redirect point by getting NT_STATUS_OK back
1366  * without incurring the overhead of reading and parsing
1367  * the referral contents.
1368  */
1369
1370 static NTSTATUS cephwrap_read_dfs_pathat(struct vfs_handle_struct *handle,
1371                                 TALLOC_CTX *mem_ctx,
1372                                 struct files_struct *dirfsp,
1373                                 struct smb_filename *smb_fname,
1374                                 struct referral **ppreflist,
1375                                 size_t *preferral_count)
1376 {
1377         NTSTATUS status = NT_STATUS_NO_MEMORY;
1378         size_t bufsize;
1379         char *link_target = NULL;
1380         int referral_len;
1381         bool ok;
1382 #if defined(HAVE_BROKEN_READLINK)
1383         char link_target_buf[PATH_MAX];
1384 #else
1385         char link_target_buf[7];
1386 #endif
1387         struct ceph_statx stx;
1388         int ret;
1389
1390         SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
1391
1392         if (is_named_stream(smb_fname)) {
1393                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1394                 goto err;
1395         }
1396
1397         if (ppreflist == NULL && preferral_count == NULL) {
1398                 /*
1399                  * We're only checking if this is a DFS
1400                  * redirect. We don't need to return data.
1401                  */
1402                 bufsize = sizeof(link_target_buf);
1403                 link_target = link_target_buf;
1404         } else {
1405                 bufsize = PATH_MAX;
1406                 link_target = talloc_array(mem_ctx, char, bufsize);
1407                 if (!link_target) {
1408                         goto err;
1409                 }
1410         }
1411
1412         ret = ceph_statx(handle->data,
1413                          smb_fname->base_name,
1414                          &stx,
1415                          SAMBA_STATX_ATTR_MASK,
1416                          AT_SYMLINK_NOFOLLOW);
1417         if (ret < 0) {
1418                 status = map_nt_error_from_unix(-ret);
1419                 goto err;
1420         }
1421
1422         referral_len = ceph_readlink(handle->data,
1423                                 smb_fname->base_name,
1424                                 link_target,
1425                                 bufsize - 1);
1426         if (referral_len < 0) {
1427                 /* ceph errors are -errno. */
1428                 if (-referral_len == EINVAL) {
1429                         DBG_INFO("%s is not a link.\n",
1430                                 smb_fname->base_name);
1431                         status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1432                 } else {
1433                         status = map_nt_error_from_unix(-referral_len);
1434                         DBG_ERR("Error reading "
1435                                 "msdfs link %s: %s\n",
1436                                 smb_fname->base_name,
1437                         strerror(errno));
1438                 }
1439                 goto err;
1440         }
1441         link_target[referral_len] = '\0';
1442
1443         DBG_INFO("%s -> %s\n",
1444                         smb_fname->base_name,
1445                         link_target);
1446
1447         if (!strnequal(link_target, "msdfs:", 6)) {
1448                 status = NT_STATUS_OBJECT_TYPE_MISMATCH;
1449                 goto err;
1450         }
1451
1452         if (ppreflist == NULL && preferral_count == NULL) {
1453                 /* Early return for checking if this is a DFS link. */
1454                 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1455                 return NT_STATUS_OK;
1456         }
1457
1458         ok = parse_msdfs_symlink(mem_ctx,
1459                         lp_msdfs_shuffle_referrals(SNUM(handle->conn)),
1460                         link_target,
1461                         ppreflist,
1462                         preferral_count);
1463
1464         if (ok) {
1465                 init_stat_ex_from_ceph_statx(&smb_fname->st, &stx);
1466                 status = NT_STATUS_OK;
1467         } else {
1468                 status = NT_STATUS_NO_MEMORY;
1469         }
1470
1471   err:
1472
1473         if (link_target != link_target_buf) {
1474                 TALLOC_FREE(link_target);
1475         }
1476         return status;
1477 }
1478
1479 static struct vfs_fn_pointers ceph_fns = {
1480         /* Disk operations */
1481
1482         .connect_fn = cephwrap_connect,
1483         .disconnect_fn = cephwrap_disconnect,
1484         .disk_free_fn = cephwrap_disk_free,
1485         .get_quota_fn = cephwrap_get_quota,
1486         .set_quota_fn = cephwrap_set_quota,
1487         .statvfs_fn = cephwrap_statvfs,
1488         .fs_capabilities_fn = cephwrap_fs_capabilities,
1489
1490         /* Directory operations */
1491
1492         .fdopendir_fn = cephwrap_fdopendir,
1493         .readdir_fn = cephwrap_readdir,
1494         .seekdir_fn = cephwrap_seekdir,
1495         .telldir_fn = cephwrap_telldir,
1496         .rewind_dir_fn = cephwrap_rewinddir,
1497         .mkdirat_fn = cephwrap_mkdirat,
1498         .closedir_fn = cephwrap_closedir,
1499
1500         /* File operations */
1501
1502         .create_dfs_pathat_fn = cephwrap_create_dfs_pathat,
1503         .read_dfs_pathat_fn = cephwrap_read_dfs_pathat,
1504         .openat_fn = cephwrap_openat,
1505         .close_fn = cephwrap_close,
1506         .pread_fn = cephwrap_pread,
1507         .pread_send_fn = cephwrap_pread_send,
1508         .pread_recv_fn = cephwrap_pread_recv,
1509         .pwrite_fn = cephwrap_pwrite,
1510         .pwrite_send_fn = cephwrap_pwrite_send,
1511         .pwrite_recv_fn = cephwrap_pwrite_recv,
1512         .lseek_fn = cephwrap_lseek,
1513         .sendfile_fn = cephwrap_sendfile,
1514         .recvfile_fn = cephwrap_recvfile,
1515         .renameat_fn = cephwrap_renameat,
1516         .fsync_send_fn = cephwrap_fsync_send,
1517         .fsync_recv_fn = cephwrap_fsync_recv,
1518         .stat_fn = cephwrap_stat,
1519         .fstat_fn = cephwrap_fstat,
1520         .lstat_fn = cephwrap_lstat,
1521         .unlinkat_fn = cephwrap_unlinkat,
1522         .chmod_fn = cephwrap_chmod,
1523         .fchmod_fn = cephwrap_fchmod,
1524         .fchown_fn = cephwrap_fchown,
1525         .lchown_fn = cephwrap_lchown,
1526         .chdir_fn = cephwrap_chdir,
1527         .getwd_fn = cephwrap_getwd,
1528         .ntimes_fn = cephwrap_ntimes,
1529         .ftruncate_fn = cephwrap_ftruncate,
1530         .fallocate_fn = cephwrap_fallocate,
1531         .lock_fn = cephwrap_lock,
1532         .kernel_flock_fn = cephwrap_kernel_flock,
1533         .fcntl_fn = cephwrap_fcntl,
1534         .linux_setlease_fn = cephwrap_linux_setlease,
1535         .getlock_fn = cephwrap_getlock,
1536         .symlinkat_fn = cephwrap_symlinkat,
1537         .readlinkat_fn = cephwrap_readlinkat,
1538         .linkat_fn = cephwrap_linkat,
1539         .mknodat_fn = cephwrap_mknodat,
1540         .realpath_fn = cephwrap_realpath,
1541         .chflags_fn = cephwrap_chflags,
1542         .get_real_filename_fn = cephwrap_get_real_filename,
1543         .connectpath_fn = cephwrap_connectpath,
1544
1545         /* EA operations. */
1546         .getxattr_fn = cephwrap_getxattr,
1547         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1548         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1549         .fgetxattr_fn = cephwrap_fgetxattr,
1550         .listxattr_fn = cephwrap_listxattr,
1551         .flistxattr_fn = cephwrap_flistxattr,
1552         .removexattr_fn = cephwrap_removexattr,
1553         .fremovexattr_fn = cephwrap_fremovexattr,
1554         .setxattr_fn = cephwrap_setxattr,
1555         .fsetxattr_fn = cephwrap_fsetxattr,
1556
1557         /* Posix ACL Operations */
1558         .sys_acl_get_file_fn = posixacl_xattr_acl_get_file,
1559         .sys_acl_get_fd_fn = posixacl_xattr_acl_get_fd,
1560         .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
1561         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
1562         .sys_acl_set_file_fn = posixacl_xattr_acl_set_file,
1563         .sys_acl_set_fd_fn = posixacl_xattr_acl_set_fd,
1564         .sys_acl_delete_def_file_fn = posixacl_xattr_acl_delete_def_file,
1565
1566         /* aio operations */
1567         .aio_force_fn = cephwrap_aio_force,
1568 };
1569
1570 static_decl_vfs;
1571 NTSTATUS vfs_ceph_init(TALLOC_CTX *ctx)
1572 {
1573         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1574                                 "ceph", &ceph_fns);
1575 }