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