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