xref: /linux/fs/ceph/file.c (revision e0484344c0413e1fcd5642b77d49c7648fb194ec)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 #include <linux/ceph/striper.h>
4 
5 #include <linux/module.h>
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/file.h>
9 #include <linux/mount.h>
10 #include <linux/namei.h>
11 #include <linux/writeback.h>
12 #include <linux/falloc.h>
13 #include <linux/iversion.h>
14 #include <linux/ktime.h>
15 
16 #include "super.h"
17 #include "mds_client.h"
18 #include "cache.h"
19 #include "io.h"
20 #include "metric.h"
21 
22 static __le32 ceph_flags_sys2wire(u32 flags)
23 {
24 	u32 wire_flags = 0;
25 
26 	switch (flags & O_ACCMODE) {
27 	case O_RDONLY:
28 		wire_flags |= CEPH_O_RDONLY;
29 		break;
30 	case O_WRONLY:
31 		wire_flags |= CEPH_O_WRONLY;
32 		break;
33 	case O_RDWR:
34 		wire_flags |= CEPH_O_RDWR;
35 		break;
36 	}
37 
38 	flags &= ~O_ACCMODE;
39 
40 #define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; }
41 
42 	ceph_sys2wire(O_CREAT);
43 	ceph_sys2wire(O_EXCL);
44 	ceph_sys2wire(O_TRUNC);
45 	ceph_sys2wire(O_DIRECTORY);
46 	ceph_sys2wire(O_NOFOLLOW);
47 
48 #undef ceph_sys2wire
49 
50 	if (flags)
51 		dout("unused open flags: %x\n", flags);
52 
53 	return cpu_to_le32(wire_flags);
54 }
55 
56 /*
57  * Ceph file operations
58  *
59  * Implement basic open/close functionality, and implement
60  * read/write.
61  *
62  * We implement three modes of file I/O:
63  *  - buffered uses the generic_file_aio_{read,write} helpers
64  *
65  *  - synchronous is used when there is multi-client read/write
66  *    sharing, avoids the page cache, and synchronously waits for an
67  *    ack from the OSD.
68  *
69  *  - direct io takes the variant of the sync path that references
70  *    user pages directly.
71  *
72  * fsync() flushes and waits on dirty pages, but just queues metadata
73  * for writeback: since the MDS can recover size and mtime there is no
74  * need to wait for MDS acknowledgement.
75  */
76 
77 /*
78  * How many pages to get in one call to iov_iter_get_pages().  This
79  * determines the size of the on-stack array used as a buffer.
80  */
81 #define ITER_GET_BVECS_PAGES	64
82 
83 static ssize_t __iter_get_bvecs(struct iov_iter *iter, size_t maxsize,
84 				struct bio_vec *bvecs)
85 {
86 	size_t size = 0;
87 	int bvec_idx = 0;
88 
89 	if (maxsize > iov_iter_count(iter))
90 		maxsize = iov_iter_count(iter);
91 
92 	while (size < maxsize) {
93 		struct page *pages[ITER_GET_BVECS_PAGES];
94 		ssize_t bytes;
95 		size_t start;
96 		int idx = 0;
97 
98 		bytes = iov_iter_get_pages(iter, pages, maxsize - size,
99 					   ITER_GET_BVECS_PAGES, &start);
100 		if (bytes < 0)
101 			return size ?: bytes;
102 
103 		iov_iter_advance(iter, bytes);
104 		size += bytes;
105 
106 		for ( ; bytes; idx++, bvec_idx++) {
107 			struct bio_vec bv = {
108 				.bv_page = pages[idx],
109 				.bv_len = min_t(int, bytes, PAGE_SIZE - start),
110 				.bv_offset = start,
111 			};
112 
113 			bvecs[bvec_idx] = bv;
114 			bytes -= bv.bv_len;
115 			start = 0;
116 		}
117 	}
118 
119 	return size;
120 }
121 
122 /*
123  * iov_iter_get_pages() only considers one iov_iter segment, no matter
124  * what maxsize or maxpages are given.  For ITER_BVEC that is a single
125  * page.
126  *
127  * Attempt to get up to @maxsize bytes worth of pages from @iter.
128  * Return the number of bytes in the created bio_vec array, or an error.
129  */
130 static ssize_t iter_get_bvecs_alloc(struct iov_iter *iter, size_t maxsize,
131 				    struct bio_vec **bvecs, int *num_bvecs)
132 {
133 	struct bio_vec *bv;
134 	size_t orig_count = iov_iter_count(iter);
135 	ssize_t bytes;
136 	int npages;
137 
138 	iov_iter_truncate(iter, maxsize);
139 	npages = iov_iter_npages(iter, INT_MAX);
140 	iov_iter_reexpand(iter, orig_count);
141 
142 	/*
143 	 * __iter_get_bvecs() may populate only part of the array -- zero it
144 	 * out.
145 	 */
146 	bv = kvmalloc_array(npages, sizeof(*bv), GFP_KERNEL | __GFP_ZERO);
147 	if (!bv)
148 		return -ENOMEM;
149 
150 	bytes = __iter_get_bvecs(iter, maxsize, bv);
151 	if (bytes < 0) {
152 		/*
153 		 * No pages were pinned -- just free the array.
154 		 */
155 		kvfree(bv);
156 		return bytes;
157 	}
158 
159 	*bvecs = bv;
160 	*num_bvecs = npages;
161 	return bytes;
162 }
163 
164 static void put_bvecs(struct bio_vec *bvecs, int num_bvecs, bool should_dirty)
165 {
166 	int i;
167 
168 	for (i = 0; i < num_bvecs; i++) {
169 		if (bvecs[i].bv_page) {
170 			if (should_dirty)
171 				set_page_dirty_lock(bvecs[i].bv_page);
172 			put_page(bvecs[i].bv_page);
173 		}
174 	}
175 	kvfree(bvecs);
176 }
177 
178 /*
179  * Prepare an open request.  Preallocate ceph_cap to avoid an
180  * inopportune ENOMEM later.
181  */
182 static struct ceph_mds_request *
183 prepare_open_request(struct super_block *sb, int flags, int create_mode)
184 {
185 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
186 	struct ceph_mds_request *req;
187 	int want_auth = USE_ANY_MDS;
188 	int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
189 
190 	if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
191 		want_auth = USE_AUTH_MDS;
192 
193 	req = ceph_mdsc_create_request(mdsc, op, want_auth);
194 	if (IS_ERR(req))
195 		goto out;
196 	req->r_fmode = ceph_flags_to_mode(flags);
197 	req->r_args.open.flags = ceph_flags_sys2wire(flags);
198 	req->r_args.open.mode = cpu_to_le32(create_mode);
199 out:
200 	return req;
201 }
202 
203 static int ceph_init_file_info(struct inode *inode, struct file *file,
204 					int fmode, bool isdir)
205 {
206 	struct ceph_inode_info *ci = ceph_inode(inode);
207 	struct ceph_file_info *fi;
208 
209 	dout("%s %p %p 0%o (%s)\n", __func__, inode, file,
210 			inode->i_mode, isdir ? "dir" : "regular");
211 	BUG_ON(inode->i_fop->release != ceph_release);
212 
213 	if (isdir) {
214 		struct ceph_dir_file_info *dfi =
215 			kmem_cache_zalloc(ceph_dir_file_cachep, GFP_KERNEL);
216 		if (!dfi)
217 			return -ENOMEM;
218 
219 		file->private_data = dfi;
220 		fi = &dfi->file_info;
221 		dfi->next_offset = 2;
222 		dfi->readdir_cache_idx = -1;
223 	} else {
224 		fi = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
225 		if (!fi)
226 			return -ENOMEM;
227 
228 		file->private_data = fi;
229 	}
230 
231 	ceph_get_fmode(ci, fmode, 1);
232 	fi->fmode = fmode;
233 
234 	spin_lock_init(&fi->rw_contexts_lock);
235 	INIT_LIST_HEAD(&fi->rw_contexts);
236 	fi->filp_gen = READ_ONCE(ceph_inode_to_client(inode)->filp_gen);
237 
238 	return 0;
239 }
240 
241 /*
242  * initialize private struct file data.
243  * if we fail, clean up by dropping fmode reference on the ceph_inode
244  */
245 static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
246 {
247 	int ret = 0;
248 
249 	switch (inode->i_mode & S_IFMT) {
250 	case S_IFREG:
251 		ceph_fscache_use_cookie(inode, file->f_mode & FMODE_WRITE);
252 		fallthrough;
253 	case S_IFDIR:
254 		ret = ceph_init_file_info(inode, file, fmode,
255 						S_ISDIR(inode->i_mode));
256 		break;
257 
258 	case S_IFLNK:
259 		dout("init_file %p %p 0%o (symlink)\n", inode, file,
260 		     inode->i_mode);
261 		break;
262 
263 	default:
264 		dout("init_file %p %p 0%o (special)\n", inode, file,
265 		     inode->i_mode);
266 		/*
267 		 * we need to drop the open ref now, since we don't
268 		 * have .release set to ceph_release.
269 		 */
270 		BUG_ON(inode->i_fop->release == ceph_release);
271 
272 		/* call the proper open fop */
273 		ret = inode->i_fop->open(inode, file);
274 	}
275 	return ret;
276 }
277 
278 /*
279  * try renew caps after session gets killed.
280  */
281 int ceph_renew_caps(struct inode *inode, int fmode)
282 {
283 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
284 	struct ceph_inode_info *ci = ceph_inode(inode);
285 	struct ceph_mds_request *req;
286 	int err, flags, wanted;
287 
288 	spin_lock(&ci->i_ceph_lock);
289 	__ceph_touch_fmode(ci, mdsc, fmode);
290 	wanted = __ceph_caps_file_wanted(ci);
291 	if (__ceph_is_any_real_caps(ci) &&
292 	    (!(wanted & CEPH_CAP_ANY_WR) || ci->i_auth_cap)) {
293 		int issued = __ceph_caps_issued(ci, NULL);
294 		spin_unlock(&ci->i_ceph_lock);
295 		dout("renew caps %p want %s issued %s updating mds_wanted\n",
296 		     inode, ceph_cap_string(wanted), ceph_cap_string(issued));
297 		ceph_check_caps(ci, 0, NULL);
298 		return 0;
299 	}
300 	spin_unlock(&ci->i_ceph_lock);
301 
302 	flags = 0;
303 	if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
304 		flags = O_RDWR;
305 	else if (wanted & CEPH_CAP_FILE_RD)
306 		flags = O_RDONLY;
307 	else if (wanted & CEPH_CAP_FILE_WR)
308 		flags = O_WRONLY;
309 #ifdef O_LAZY
310 	if (wanted & CEPH_CAP_FILE_LAZYIO)
311 		flags |= O_LAZY;
312 #endif
313 
314 	req = prepare_open_request(inode->i_sb, flags, 0);
315 	if (IS_ERR(req)) {
316 		err = PTR_ERR(req);
317 		goto out;
318 	}
319 
320 	req->r_inode = inode;
321 	ihold(inode);
322 	req->r_num_caps = 1;
323 
324 	err = ceph_mdsc_do_request(mdsc, NULL, req);
325 	ceph_mdsc_put_request(req);
326 out:
327 	dout("renew caps %p open result=%d\n", inode, err);
328 	return err < 0 ? err : 0;
329 }
330 
331 /*
332  * If we already have the requisite capabilities, we can satisfy
333  * the open request locally (no need to request new caps from the
334  * MDS).  We do, however, need to inform the MDS (asynchronously)
335  * if our wanted caps set expands.
336  */
337 int ceph_open(struct inode *inode, struct file *file)
338 {
339 	struct ceph_inode_info *ci = ceph_inode(inode);
340 	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
341 	struct ceph_mds_client *mdsc = fsc->mdsc;
342 	struct ceph_mds_request *req;
343 	struct ceph_file_info *fi = file->private_data;
344 	int err;
345 	int flags, fmode, wanted;
346 
347 	if (fi) {
348 		dout("open file %p is already opened\n", file);
349 		return 0;
350 	}
351 
352 	/* filter out O_CREAT|O_EXCL; vfs did that already.  yuck. */
353 	flags = file->f_flags & ~(O_CREAT|O_EXCL);
354 	if (S_ISDIR(inode->i_mode))
355 		flags = O_DIRECTORY;  /* mds likes to know */
356 
357 	dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode,
358 	     ceph_vinop(inode), file, flags, file->f_flags);
359 	fmode = ceph_flags_to_mode(flags);
360 	wanted = ceph_caps_for_mode(fmode);
361 
362 	/* snapped files are read-only */
363 	if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
364 		return -EROFS;
365 
366 	/* trivially open snapdir */
367 	if (ceph_snap(inode) == CEPH_SNAPDIR) {
368 		return ceph_init_file(inode, file, fmode);
369 	}
370 
371 	/*
372 	 * No need to block if we have caps on the auth MDS (for
373 	 * write) or any MDS (for read).  Update wanted set
374 	 * asynchronously.
375 	 */
376 	spin_lock(&ci->i_ceph_lock);
377 	if (__ceph_is_any_real_caps(ci) &&
378 	    (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
379 		int mds_wanted = __ceph_caps_mds_wanted(ci, true);
380 		int issued = __ceph_caps_issued(ci, NULL);
381 
382 		dout("open %p fmode %d want %s issued %s using existing\n",
383 		     inode, fmode, ceph_cap_string(wanted),
384 		     ceph_cap_string(issued));
385 		__ceph_touch_fmode(ci, mdsc, fmode);
386 		spin_unlock(&ci->i_ceph_lock);
387 
388 		/* adjust wanted? */
389 		if ((issued & wanted) != wanted &&
390 		    (mds_wanted & wanted) != wanted &&
391 		    ceph_snap(inode) != CEPH_SNAPDIR)
392 			ceph_check_caps(ci, 0, NULL);
393 
394 		return ceph_init_file(inode, file, fmode);
395 	} else if (ceph_snap(inode) != CEPH_NOSNAP &&
396 		   (ci->i_snap_caps & wanted) == wanted) {
397 		__ceph_touch_fmode(ci, mdsc, fmode);
398 		spin_unlock(&ci->i_ceph_lock);
399 		return ceph_init_file(inode, file, fmode);
400 	}
401 
402 	spin_unlock(&ci->i_ceph_lock);
403 
404 	dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
405 	req = prepare_open_request(inode->i_sb, flags, 0);
406 	if (IS_ERR(req)) {
407 		err = PTR_ERR(req);
408 		goto out;
409 	}
410 	req->r_inode = inode;
411 	ihold(inode);
412 
413 	req->r_num_caps = 1;
414 	err = ceph_mdsc_do_request(mdsc, NULL, req);
415 	if (!err)
416 		err = ceph_init_file(inode, file, req->r_fmode);
417 	ceph_mdsc_put_request(req);
418 	dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
419 out:
420 	return err;
421 }
422 
423 /* Clone the layout from a synchronous create, if the dir now has Dc caps */
424 static void
425 cache_file_layout(struct inode *dst, struct inode *src)
426 {
427 	struct ceph_inode_info *cdst = ceph_inode(dst);
428 	struct ceph_inode_info *csrc = ceph_inode(src);
429 
430 	spin_lock(&cdst->i_ceph_lock);
431 	if ((__ceph_caps_issued(cdst, NULL) & CEPH_CAP_DIR_CREATE) &&
432 	    !ceph_file_layout_is_valid(&cdst->i_cached_layout)) {
433 		memcpy(&cdst->i_cached_layout, &csrc->i_layout,
434 			sizeof(cdst->i_cached_layout));
435 		rcu_assign_pointer(cdst->i_cached_layout.pool_ns,
436 				   ceph_try_get_string(csrc->i_layout.pool_ns));
437 	}
438 	spin_unlock(&cdst->i_ceph_lock);
439 }
440 
441 /*
442  * Try to set up an async create. We need caps, a file layout, and inode number,
443  * and either a lease on the dentry or complete dir info. If any of those
444  * criteria are not satisfied, then return false and the caller can go
445  * synchronous.
446  */
447 static int try_prep_async_create(struct inode *dir, struct dentry *dentry,
448 				 struct ceph_file_layout *lo, u64 *pino)
449 {
450 	struct ceph_inode_info *ci = ceph_inode(dir);
451 	struct ceph_dentry_info *di = ceph_dentry(dentry);
452 	int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_CREATE;
453 	u64 ino;
454 
455 	spin_lock(&ci->i_ceph_lock);
456 	/* No auth cap means no chance for Dc caps */
457 	if (!ci->i_auth_cap)
458 		goto no_async;
459 
460 	/* Any delegated inos? */
461 	if (xa_empty(&ci->i_auth_cap->session->s_delegated_inos))
462 		goto no_async;
463 
464 	if (!ceph_file_layout_is_valid(&ci->i_cached_layout))
465 		goto no_async;
466 
467 	if ((__ceph_caps_issued(ci, NULL) & want) != want)
468 		goto no_async;
469 
470 	if (d_in_lookup(dentry)) {
471 		if (!__ceph_dir_is_complete(ci))
472 			goto no_async;
473 		spin_lock(&dentry->d_lock);
474 		di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
475 		spin_unlock(&dentry->d_lock);
476 	} else if (atomic_read(&ci->i_shared_gen) !=
477 		   READ_ONCE(di->lease_shared_gen)) {
478 		goto no_async;
479 	}
480 
481 	ino = ceph_get_deleg_ino(ci->i_auth_cap->session);
482 	if (!ino)
483 		goto no_async;
484 
485 	*pino = ino;
486 	ceph_take_cap_refs(ci, want, false);
487 	memcpy(lo, &ci->i_cached_layout, sizeof(*lo));
488 	rcu_assign_pointer(lo->pool_ns,
489 			   ceph_try_get_string(ci->i_cached_layout.pool_ns));
490 	got = want;
491 no_async:
492 	spin_unlock(&ci->i_ceph_lock);
493 	return got;
494 }
495 
496 static void restore_deleg_ino(struct inode *dir, u64 ino)
497 {
498 	struct ceph_inode_info *ci = ceph_inode(dir);
499 	struct ceph_mds_session *s = NULL;
500 
501 	spin_lock(&ci->i_ceph_lock);
502 	if (ci->i_auth_cap)
503 		s = ceph_get_mds_session(ci->i_auth_cap->session);
504 	spin_unlock(&ci->i_ceph_lock);
505 	if (s) {
506 		int err = ceph_restore_deleg_ino(s, ino);
507 		if (err)
508 			pr_warn("ceph: unable to restore delegated ino 0x%llx to session: %d\n",
509 				ino, err);
510 		ceph_put_mds_session(s);
511 	}
512 }
513 
514 static void ceph_async_create_cb(struct ceph_mds_client *mdsc,
515                                  struct ceph_mds_request *req)
516 {
517 	int result = req->r_err ? req->r_err :
518 			le32_to_cpu(req->r_reply_info.head->result);
519 
520 	if (result == -EJUKEBOX)
521 		goto out;
522 
523 	mapping_set_error(req->r_parent->i_mapping, result);
524 
525 	if (result) {
526 		struct dentry *dentry = req->r_dentry;
527 		struct inode *inode = d_inode(dentry);
528 		int pathlen = 0;
529 		u64 base = 0;
530 		char *path = ceph_mdsc_build_path(req->r_dentry, &pathlen,
531 						  &base, 0);
532 
533 		ceph_dir_clear_complete(req->r_parent);
534 		if (!d_unhashed(dentry))
535 			d_drop(dentry);
536 
537 		ceph_inode_shutdown(inode);
538 
539 		pr_warn("ceph: async create failure path=(%llx)%s result=%d!\n",
540 			base, IS_ERR(path) ? "<<bad>>" : path, result);
541 		ceph_mdsc_free_path(path, pathlen);
542 	}
543 
544 	if (req->r_target_inode) {
545 		struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
546 		u64 ino = ceph_vino(req->r_target_inode).ino;
547 
548 		if (req->r_deleg_ino != ino)
549 			pr_warn("%s: inode number mismatch! err=%d deleg_ino=0x%llx target=0x%llx\n",
550 				__func__, req->r_err, req->r_deleg_ino, ino);
551 		mapping_set_error(req->r_target_inode->i_mapping, result);
552 
553 		spin_lock(&ci->i_ceph_lock);
554 		if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
555 			ci->i_ceph_flags &= ~CEPH_I_ASYNC_CREATE;
556 			wake_up_bit(&ci->i_ceph_flags, CEPH_ASYNC_CREATE_BIT);
557 		}
558 		ceph_kick_flushing_inode_caps(req->r_session, ci);
559 		spin_unlock(&ci->i_ceph_lock);
560 	} else if (!result) {
561 		pr_warn("%s: no req->r_target_inode for 0x%llx\n", __func__,
562 			req->r_deleg_ino);
563 	}
564 out:
565 	ceph_mdsc_release_dir_caps(req);
566 }
567 
568 static int ceph_finish_async_create(struct inode *dir, struct dentry *dentry,
569 				    struct file *file, umode_t mode,
570 				    struct ceph_mds_request *req,
571 				    struct ceph_acl_sec_ctx *as_ctx,
572 				    struct ceph_file_layout *lo)
573 {
574 	int ret;
575 	char xattr_buf[4];
576 	struct ceph_mds_reply_inode in = { };
577 	struct ceph_mds_reply_info_in iinfo = { .in = &in };
578 	struct ceph_inode_info *ci = ceph_inode(dir);
579 	struct inode *inode;
580 	struct timespec64 now;
581 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
582 	struct ceph_vino vino = { .ino = req->r_deleg_ino,
583 				  .snap = CEPH_NOSNAP };
584 
585 	ktime_get_real_ts64(&now);
586 
587 	inode = ceph_get_inode(dentry->d_sb, vino);
588 	if (IS_ERR(inode))
589 		return PTR_ERR(inode);
590 
591 	iinfo.inline_version = CEPH_INLINE_NONE;
592 	iinfo.change_attr = 1;
593 	ceph_encode_timespec64(&iinfo.btime, &now);
594 
595 	iinfo.xattr_len = ARRAY_SIZE(xattr_buf);
596 	iinfo.xattr_data = xattr_buf;
597 	memset(iinfo.xattr_data, 0, iinfo.xattr_len);
598 
599 	in.ino = cpu_to_le64(vino.ino);
600 	in.snapid = cpu_to_le64(CEPH_NOSNAP);
601 	in.version = cpu_to_le64(1);	// ???
602 	in.cap.caps = in.cap.wanted = cpu_to_le32(CEPH_CAP_ALL_FILE);
603 	in.cap.cap_id = cpu_to_le64(1);
604 	in.cap.realm = cpu_to_le64(ci->i_snap_realm->ino);
605 	in.cap.flags = CEPH_CAP_FLAG_AUTH;
606 	in.ctime = in.mtime = in.atime = iinfo.btime;
607 	in.mode = cpu_to_le32((u32)mode);
608 	in.truncate_seq = cpu_to_le32(1);
609 	in.truncate_size = cpu_to_le64(-1ULL);
610 	in.xattr_version = cpu_to_le64(1);
611 	in.uid = cpu_to_le32(from_kuid(&init_user_ns, current_fsuid()));
612 	in.gid = cpu_to_le32(from_kgid(&init_user_ns, dir->i_mode & S_ISGID ?
613 				dir->i_gid : current_fsgid()));
614 	in.nlink = cpu_to_le32(1);
615 	in.max_size = cpu_to_le64(lo->stripe_unit);
616 
617 	ceph_file_layout_to_legacy(lo, &in.layout);
618 
619 	down_read(&mdsc->snap_rwsem);
620 	ret = ceph_fill_inode(inode, NULL, &iinfo, NULL, req->r_session,
621 			      req->r_fmode, NULL);
622 	up_read(&mdsc->snap_rwsem);
623 	if (ret) {
624 		dout("%s failed to fill inode: %d\n", __func__, ret);
625 		ceph_dir_clear_complete(dir);
626 		if (!d_unhashed(dentry))
627 			d_drop(dentry);
628 		if (inode->i_state & I_NEW)
629 			discard_new_inode(inode);
630 	} else {
631 		struct dentry *dn;
632 
633 		dout("%s d_adding new inode 0x%llx to 0x%llx/%s\n", __func__,
634 			vino.ino, ceph_ino(dir), dentry->d_name.name);
635 		ceph_dir_clear_ordered(dir);
636 		ceph_init_inode_acls(inode, as_ctx);
637 		if (inode->i_state & I_NEW) {
638 			/*
639 			 * If it's not I_NEW, then someone created this before
640 			 * we got here. Assume the server is aware of it at
641 			 * that point and don't worry about setting
642 			 * CEPH_I_ASYNC_CREATE.
643 			 */
644 			ceph_inode(inode)->i_ceph_flags = CEPH_I_ASYNC_CREATE;
645 			unlock_new_inode(inode);
646 		}
647 		if (d_in_lookup(dentry) || d_really_is_negative(dentry)) {
648 			if (!d_unhashed(dentry))
649 				d_drop(dentry);
650 			dn = d_splice_alias(inode, dentry);
651 			WARN_ON_ONCE(dn && dn != dentry);
652 		}
653 		file->f_mode |= FMODE_CREATED;
654 		ret = finish_open(file, dentry, ceph_open);
655 	}
656 	return ret;
657 }
658 
659 /*
660  * Do a lookup + open with a single request.  If we get a non-existent
661  * file or symlink, return 1 so the VFS can retry.
662  */
663 int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
664 		     struct file *file, unsigned flags, umode_t mode)
665 {
666 	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
667 	struct ceph_mds_client *mdsc = fsc->mdsc;
668 	struct ceph_mds_request *req;
669 	struct dentry *dn;
670 	struct ceph_acl_sec_ctx as_ctx = {};
671 	bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
672 	int mask;
673 	int err;
674 
675 	dout("atomic_open %p dentry %p '%pd' %s flags %d mode 0%o\n",
676 	     dir, dentry, dentry,
677 	     d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
678 
679 	if (dentry->d_name.len > NAME_MAX)
680 		return -ENAMETOOLONG;
681 
682 	if (flags & O_CREAT) {
683 		if (ceph_quota_is_max_files_exceeded(dir))
684 			return -EDQUOT;
685 		err = ceph_pre_init_acls(dir, &mode, &as_ctx);
686 		if (err < 0)
687 			return err;
688 		err = ceph_security_init_secctx(dentry, mode, &as_ctx);
689 		if (err < 0)
690 			goto out_ctx;
691 	} else if (!d_in_lookup(dentry)) {
692 		/* If it's not being looked up, it's negative */
693 		return -ENOENT;
694 	}
695 retry:
696 	/* do the open */
697 	req = prepare_open_request(dir->i_sb, flags, mode);
698 	if (IS_ERR(req)) {
699 		err = PTR_ERR(req);
700 		goto out_ctx;
701 	}
702 	req->r_dentry = dget(dentry);
703 	req->r_num_caps = 2;
704 	mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
705 	if (ceph_security_xattr_wanted(dir))
706 		mask |= CEPH_CAP_XATTR_SHARED;
707 	req->r_args.open.mask = cpu_to_le32(mask);
708 	req->r_parent = dir;
709 	ihold(dir);
710 
711 	if (flags & O_CREAT) {
712 		struct ceph_file_layout lo;
713 
714 		req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
715 		req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
716 		if (as_ctx.pagelist) {
717 			req->r_pagelist = as_ctx.pagelist;
718 			as_ctx.pagelist = NULL;
719 		}
720 		if (try_async &&
721 		    (req->r_dir_caps =
722 		      try_prep_async_create(dir, dentry, &lo,
723 					    &req->r_deleg_ino))) {
724 			set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
725 			req->r_args.open.flags |= cpu_to_le32(CEPH_O_EXCL);
726 			req->r_callback = ceph_async_create_cb;
727 			err = ceph_mdsc_submit_request(mdsc, dir, req);
728 			if (!err) {
729 				err = ceph_finish_async_create(dir, dentry,
730 							file, mode, req,
731 							&as_ctx, &lo);
732 			} else if (err == -EJUKEBOX) {
733 				restore_deleg_ino(dir, req->r_deleg_ino);
734 				ceph_mdsc_put_request(req);
735 				try_async = false;
736 				goto retry;
737 			}
738 			goto out_req;
739 		}
740 	}
741 
742 	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
743 	err = ceph_mdsc_do_request(mdsc,
744 				   (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
745 				   req);
746 	if (err == -ENOENT) {
747 		dentry = ceph_handle_snapdir(req, dentry);
748 		if (IS_ERR(dentry)) {
749 			err = PTR_ERR(dentry);
750 			goto out_req;
751 		}
752 		err = 0;
753 	}
754 
755 	if (!err && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
756 		err = ceph_handle_notrace_create(dir, dentry);
757 
758 	if (d_in_lookup(dentry)) {
759 		dn = ceph_finish_lookup(req, dentry, err);
760 		if (IS_ERR(dn))
761 			err = PTR_ERR(dn);
762 	} else {
763 		/* we were given a hashed negative dentry */
764 		dn = NULL;
765 	}
766 	if (err)
767 		goto out_req;
768 	if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
769 		/* make vfs retry on splice, ENOENT, or symlink */
770 		dout("atomic_open finish_no_open on dn %p\n", dn);
771 		err = finish_no_open(file, dn);
772 	} else {
773 		dout("atomic_open finish_open on dn %p\n", dn);
774 		if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
775 			struct inode *newino = d_inode(dentry);
776 
777 			cache_file_layout(dir, newino);
778 			ceph_init_inode_acls(newino, &as_ctx);
779 			file->f_mode |= FMODE_CREATED;
780 		}
781 		err = finish_open(file, dentry, ceph_open);
782 	}
783 out_req:
784 	ceph_mdsc_put_request(req);
785 out_ctx:
786 	ceph_release_acl_sec_ctx(&as_ctx);
787 	dout("atomic_open result=%d\n", err);
788 	return err;
789 }
790 
791 int ceph_release(struct inode *inode, struct file *file)
792 {
793 	struct ceph_inode_info *ci = ceph_inode(inode);
794 
795 	if (S_ISDIR(inode->i_mode)) {
796 		struct ceph_dir_file_info *dfi = file->private_data;
797 		dout("release inode %p dir file %p\n", inode, file);
798 		WARN_ON(!list_empty(&dfi->file_info.rw_contexts));
799 
800 		ceph_put_fmode(ci, dfi->file_info.fmode, 1);
801 
802 		if (dfi->last_readdir)
803 			ceph_mdsc_put_request(dfi->last_readdir);
804 		kfree(dfi->last_name);
805 		kfree(dfi->dir_info);
806 		kmem_cache_free(ceph_dir_file_cachep, dfi);
807 	} else {
808 		struct ceph_file_info *fi = file->private_data;
809 		dout("release inode %p regular file %p\n", inode, file);
810 		WARN_ON(!list_empty(&fi->rw_contexts));
811 
812 		ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE);
813 		ceph_put_fmode(ci, fi->fmode, 1);
814 
815 		kmem_cache_free(ceph_file_cachep, fi);
816 	}
817 
818 	/* wake up anyone waiting for caps on this inode */
819 	wake_up_all(&ci->i_cap_wq);
820 	return 0;
821 }
822 
823 enum {
824 	HAVE_RETRIED = 1,
825 	CHECK_EOF =    2,
826 	READ_INLINE =  3,
827 };
828 
829 /*
830  * Completely synchronous read and write methods.  Direct from __user
831  * buffer to osd, or directly to user pages (if O_DIRECT).
832  *
833  * If the read spans object boundary, just do multiple reads.  (That's not
834  * atomic, but good enough for now.)
835  *
836  * If we get a short result from the OSD, check against i_size; we need to
837  * only return a short read to the caller if we hit EOF.
838  */
839 static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
840 			      int *retry_op)
841 {
842 	struct file *file = iocb->ki_filp;
843 	struct inode *inode = file_inode(file);
844 	struct ceph_inode_info *ci = ceph_inode(inode);
845 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
846 	struct ceph_osd_client *osdc = &fsc->client->osdc;
847 	ssize_t ret;
848 	u64 off = iocb->ki_pos;
849 	u64 len = iov_iter_count(to);
850 	u64 i_size;
851 
852 	dout("sync_read on file %p %llu~%u %s\n", file, off, (unsigned)len,
853 	     (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
854 
855 	if (!len)
856 		return 0;
857 	/*
858 	 * flush any page cache pages in this range.  this
859 	 * will make concurrent normal and sync io slow,
860 	 * but it will at least behave sensibly when they are
861 	 * in sequence.
862 	 */
863 	ret = filemap_write_and_wait_range(inode->i_mapping,
864 					   off, off + len - 1);
865 	if (ret < 0)
866 		return ret;
867 
868 	ret = 0;
869 	while ((len = iov_iter_count(to)) > 0) {
870 		struct ceph_osd_request *req;
871 		struct page **pages;
872 		int num_pages;
873 		size_t page_off;
874 		bool more;
875 		int idx;
876 		size_t left;
877 
878 		req = ceph_osdc_new_request(osdc, &ci->i_layout,
879 					ci->i_vino, off, &len, 0, 1,
880 					CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
881 					NULL, ci->i_truncate_seq,
882 					ci->i_truncate_size, false);
883 		if (IS_ERR(req)) {
884 			ret = PTR_ERR(req);
885 			break;
886 		}
887 
888 		more = len < iov_iter_count(to);
889 
890 		num_pages = calc_pages_for(off, len);
891 		page_off = off & ~PAGE_MASK;
892 		pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
893 		if (IS_ERR(pages)) {
894 			ceph_osdc_put_request(req);
895 			ret = PTR_ERR(pages);
896 			break;
897 		}
898 
899 		osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_off,
900 						 false, false);
901 		ret = ceph_osdc_start_request(osdc, req, false);
902 		if (!ret)
903 			ret = ceph_osdc_wait_request(osdc, req);
904 
905 		ceph_update_read_metrics(&fsc->mdsc->metric,
906 					 req->r_start_latency,
907 					 req->r_end_latency,
908 					 len, ret);
909 
910 		ceph_osdc_put_request(req);
911 
912 		i_size = i_size_read(inode);
913 		dout("sync_read %llu~%llu got %zd i_size %llu%s\n",
914 		     off, len, ret, i_size, (more ? " MORE" : ""));
915 
916 		if (ret == -ENOENT)
917 			ret = 0;
918 		if (ret >= 0 && ret < len && (off + ret < i_size)) {
919 			int zlen = min(len - ret, i_size - off - ret);
920 			int zoff = page_off + ret;
921 			dout("sync_read zero gap %llu~%llu\n",
922                              off + ret, off + ret + zlen);
923 			ceph_zero_page_vector_range(zoff, zlen, pages);
924 			ret += zlen;
925 		}
926 
927 		idx = 0;
928 		left = ret > 0 ? ret : 0;
929 		while (left > 0) {
930 			size_t len, copied;
931 			page_off = off & ~PAGE_MASK;
932 			len = min_t(size_t, left, PAGE_SIZE - page_off);
933 			SetPageUptodate(pages[idx]);
934 			copied = copy_page_to_iter(pages[idx++],
935 						   page_off, len, to);
936 			off += copied;
937 			left -= copied;
938 			if (copied < len) {
939 				ret = -EFAULT;
940 				break;
941 			}
942 		}
943 		ceph_release_page_vector(pages, num_pages);
944 
945 		if (ret < 0) {
946 			if (ret == -EBLOCKLISTED)
947 				fsc->blocklisted = true;
948 			break;
949 		}
950 
951 		if (off >= i_size || !more)
952 			break;
953 	}
954 
955 	if (off > iocb->ki_pos) {
956 		if (off >= i_size) {
957 			*retry_op = CHECK_EOF;
958 			ret = i_size - iocb->ki_pos;
959 			iocb->ki_pos = i_size;
960 		} else {
961 			ret = off - iocb->ki_pos;
962 			iocb->ki_pos = off;
963 		}
964 	}
965 
966 	dout("sync_read result %zd retry_op %d\n", ret, *retry_op);
967 	return ret;
968 }
969 
970 struct ceph_aio_request {
971 	struct kiocb *iocb;
972 	size_t total_len;
973 	bool write;
974 	bool should_dirty;
975 	int error;
976 	struct list_head osd_reqs;
977 	unsigned num_reqs;
978 	atomic_t pending_reqs;
979 	struct timespec64 mtime;
980 	struct ceph_cap_flush *prealloc_cf;
981 };
982 
983 struct ceph_aio_work {
984 	struct work_struct work;
985 	struct ceph_osd_request *req;
986 };
987 
988 static void ceph_aio_retry_work(struct work_struct *work);
989 
990 static void ceph_aio_complete(struct inode *inode,
991 			      struct ceph_aio_request *aio_req)
992 {
993 	struct ceph_inode_info *ci = ceph_inode(inode);
994 	int ret;
995 
996 	if (!atomic_dec_and_test(&aio_req->pending_reqs))
997 		return;
998 
999 	if (aio_req->iocb->ki_flags & IOCB_DIRECT)
1000 		inode_dio_end(inode);
1001 
1002 	ret = aio_req->error;
1003 	if (!ret)
1004 		ret = aio_req->total_len;
1005 
1006 	dout("ceph_aio_complete %p rc %d\n", inode, ret);
1007 
1008 	if (ret >= 0 && aio_req->write) {
1009 		int dirty;
1010 
1011 		loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
1012 		if (endoff > i_size_read(inode)) {
1013 			if (ceph_inode_set_size(inode, endoff))
1014 				ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1015 		}
1016 
1017 		spin_lock(&ci->i_ceph_lock);
1018 		ci->i_inline_version = CEPH_INLINE_NONE;
1019 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1020 					       &aio_req->prealloc_cf);
1021 		spin_unlock(&ci->i_ceph_lock);
1022 		if (dirty)
1023 			__mark_inode_dirty(inode, dirty);
1024 
1025 	}
1026 
1027 	ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
1028 						CEPH_CAP_FILE_RD));
1029 
1030 	aio_req->iocb->ki_complete(aio_req->iocb, ret);
1031 
1032 	ceph_free_cap_flush(aio_req->prealloc_cf);
1033 	kfree(aio_req);
1034 }
1035 
1036 static void ceph_aio_complete_req(struct ceph_osd_request *req)
1037 {
1038 	int rc = req->r_result;
1039 	struct inode *inode = req->r_inode;
1040 	struct ceph_aio_request *aio_req = req->r_priv;
1041 	struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
1042 	struct ceph_client_metric *metric = &ceph_sb_to_mdsc(inode->i_sb)->metric;
1043 	unsigned int len = osd_data->bvec_pos.iter.bi_size;
1044 
1045 	BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_BVECS);
1046 	BUG_ON(!osd_data->num_bvecs);
1047 
1048 	dout("ceph_aio_complete_req %p rc %d bytes %u\n", inode, rc, len);
1049 
1050 	if (rc == -EOLDSNAPC) {
1051 		struct ceph_aio_work *aio_work;
1052 		BUG_ON(!aio_req->write);
1053 
1054 		aio_work = kmalloc(sizeof(*aio_work), GFP_NOFS);
1055 		if (aio_work) {
1056 			INIT_WORK(&aio_work->work, ceph_aio_retry_work);
1057 			aio_work->req = req;
1058 			queue_work(ceph_inode_to_client(inode)->inode_wq,
1059 				   &aio_work->work);
1060 			return;
1061 		}
1062 		rc = -ENOMEM;
1063 	} else if (!aio_req->write) {
1064 		if (rc == -ENOENT)
1065 			rc = 0;
1066 		if (rc >= 0 && len > rc) {
1067 			struct iov_iter i;
1068 			int zlen = len - rc;
1069 
1070 			/*
1071 			 * If read is satisfied by single OSD request,
1072 			 * it can pass EOF. Otherwise read is within
1073 			 * i_size.
1074 			 */
1075 			if (aio_req->num_reqs == 1) {
1076 				loff_t i_size = i_size_read(inode);
1077 				loff_t endoff = aio_req->iocb->ki_pos + rc;
1078 				if (endoff < i_size)
1079 					zlen = min_t(size_t, zlen,
1080 						     i_size - endoff);
1081 				aio_req->total_len = rc + zlen;
1082 			}
1083 
1084 			iov_iter_bvec(&i, READ, osd_data->bvec_pos.bvecs,
1085 				      osd_data->num_bvecs, len);
1086 			iov_iter_advance(&i, rc);
1087 			iov_iter_zero(zlen, &i);
1088 		}
1089 	}
1090 
1091 	/* r_start_latency == 0 means the request was not submitted */
1092 	if (req->r_start_latency) {
1093 		if (aio_req->write)
1094 			ceph_update_write_metrics(metric, req->r_start_latency,
1095 						  req->r_end_latency, len, rc);
1096 		else
1097 			ceph_update_read_metrics(metric, req->r_start_latency,
1098 						 req->r_end_latency, len, rc);
1099 	}
1100 
1101 	put_bvecs(osd_data->bvec_pos.bvecs, osd_data->num_bvecs,
1102 		  aio_req->should_dirty);
1103 	ceph_osdc_put_request(req);
1104 
1105 	if (rc < 0)
1106 		cmpxchg(&aio_req->error, 0, rc);
1107 
1108 	ceph_aio_complete(inode, aio_req);
1109 	return;
1110 }
1111 
1112 static void ceph_aio_retry_work(struct work_struct *work)
1113 {
1114 	struct ceph_aio_work *aio_work =
1115 		container_of(work, struct ceph_aio_work, work);
1116 	struct ceph_osd_request *orig_req = aio_work->req;
1117 	struct ceph_aio_request *aio_req = orig_req->r_priv;
1118 	struct inode *inode = orig_req->r_inode;
1119 	struct ceph_inode_info *ci = ceph_inode(inode);
1120 	struct ceph_snap_context *snapc;
1121 	struct ceph_osd_request *req;
1122 	int ret;
1123 
1124 	spin_lock(&ci->i_ceph_lock);
1125 	if (__ceph_have_pending_cap_snap(ci)) {
1126 		struct ceph_cap_snap *capsnap =
1127 			list_last_entry(&ci->i_cap_snaps,
1128 					struct ceph_cap_snap,
1129 					ci_item);
1130 		snapc = ceph_get_snap_context(capsnap->context);
1131 	} else {
1132 		BUG_ON(!ci->i_head_snapc);
1133 		snapc = ceph_get_snap_context(ci->i_head_snapc);
1134 	}
1135 	spin_unlock(&ci->i_ceph_lock);
1136 
1137 	req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 1,
1138 			false, GFP_NOFS);
1139 	if (!req) {
1140 		ret = -ENOMEM;
1141 		req = orig_req;
1142 		goto out;
1143 	}
1144 
1145 	req->r_flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
1146 	ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
1147 	ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
1148 
1149 	req->r_ops[0] = orig_req->r_ops[0];
1150 
1151 	req->r_mtime = aio_req->mtime;
1152 	req->r_data_offset = req->r_ops[0].extent.offset;
1153 
1154 	ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
1155 	if (ret) {
1156 		ceph_osdc_put_request(req);
1157 		req = orig_req;
1158 		goto out;
1159 	}
1160 
1161 	ceph_osdc_put_request(orig_req);
1162 
1163 	req->r_callback = ceph_aio_complete_req;
1164 	req->r_inode = inode;
1165 	req->r_priv = aio_req;
1166 
1167 	ret = ceph_osdc_start_request(req->r_osdc, req, false);
1168 out:
1169 	if (ret < 0) {
1170 		req->r_result = ret;
1171 		ceph_aio_complete_req(req);
1172 	}
1173 
1174 	ceph_put_snap_context(snapc);
1175 	kfree(aio_work);
1176 }
1177 
1178 static ssize_t
1179 ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
1180 		       struct ceph_snap_context *snapc,
1181 		       struct ceph_cap_flush **pcf)
1182 {
1183 	struct file *file = iocb->ki_filp;
1184 	struct inode *inode = file_inode(file);
1185 	struct ceph_inode_info *ci = ceph_inode(inode);
1186 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1187 	struct ceph_client_metric *metric = &fsc->mdsc->metric;
1188 	struct ceph_vino vino;
1189 	struct ceph_osd_request *req;
1190 	struct bio_vec *bvecs;
1191 	struct ceph_aio_request *aio_req = NULL;
1192 	int num_pages = 0;
1193 	int flags;
1194 	int ret = 0;
1195 	struct timespec64 mtime = current_time(inode);
1196 	size_t count = iov_iter_count(iter);
1197 	loff_t pos = iocb->ki_pos;
1198 	bool write = iov_iter_rw(iter) == WRITE;
1199 	bool should_dirty = !write && iter_is_iovec(iter);
1200 
1201 	if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
1202 		return -EROFS;
1203 
1204 	dout("sync_direct_%s on file %p %lld~%u snapc %p seq %lld\n",
1205 	     (write ? "write" : "read"), file, pos, (unsigned)count,
1206 	     snapc, snapc ? snapc->seq : 0);
1207 
1208 	if (write) {
1209 		int ret2;
1210 
1211 		ceph_fscache_invalidate(inode, true);
1212 
1213 		ret2 = invalidate_inode_pages2_range(inode->i_mapping,
1214 					pos >> PAGE_SHIFT,
1215 					(pos + count - 1) >> PAGE_SHIFT);
1216 		if (ret2 < 0)
1217 			dout("invalidate_inode_pages2_range returned %d\n", ret2);
1218 
1219 		flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
1220 	} else {
1221 		flags = CEPH_OSD_FLAG_READ;
1222 	}
1223 
1224 	while (iov_iter_count(iter) > 0) {
1225 		u64 size = iov_iter_count(iter);
1226 		ssize_t len;
1227 
1228 		if (write)
1229 			size = min_t(u64, size, fsc->mount_options->wsize);
1230 		else
1231 			size = min_t(u64, size, fsc->mount_options->rsize);
1232 
1233 		vino = ceph_vino(inode);
1234 		req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1235 					    vino, pos, &size, 0,
1236 					    1,
1237 					    write ? CEPH_OSD_OP_WRITE :
1238 						    CEPH_OSD_OP_READ,
1239 					    flags, snapc,
1240 					    ci->i_truncate_seq,
1241 					    ci->i_truncate_size,
1242 					    false);
1243 		if (IS_ERR(req)) {
1244 			ret = PTR_ERR(req);
1245 			break;
1246 		}
1247 
1248 		len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages);
1249 		if (len < 0) {
1250 			ceph_osdc_put_request(req);
1251 			ret = len;
1252 			break;
1253 		}
1254 		if (len != size)
1255 			osd_req_op_extent_update(req, 0, len);
1256 
1257 		/*
1258 		 * To simplify error handling, allow AIO when IO within i_size
1259 		 * or IO can be satisfied by single OSD request.
1260 		 */
1261 		if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
1262 		    (len == count || pos + count <= i_size_read(inode))) {
1263 			aio_req = kzalloc(sizeof(*aio_req), GFP_KERNEL);
1264 			if (aio_req) {
1265 				aio_req->iocb = iocb;
1266 				aio_req->write = write;
1267 				aio_req->should_dirty = should_dirty;
1268 				INIT_LIST_HEAD(&aio_req->osd_reqs);
1269 				if (write) {
1270 					aio_req->mtime = mtime;
1271 					swap(aio_req->prealloc_cf, *pcf);
1272 				}
1273 			}
1274 			/* ignore error */
1275 		}
1276 
1277 		if (write) {
1278 			/*
1279 			 * throw out any page cache pages in this range. this
1280 			 * may block.
1281 			 */
1282 			truncate_inode_pages_range(inode->i_mapping, pos,
1283 						   PAGE_ALIGN(pos + len) - 1);
1284 
1285 			req->r_mtime = mtime;
1286 		}
1287 
1288 		osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len);
1289 
1290 		if (aio_req) {
1291 			aio_req->total_len += len;
1292 			aio_req->num_reqs++;
1293 			atomic_inc(&aio_req->pending_reqs);
1294 
1295 			req->r_callback = ceph_aio_complete_req;
1296 			req->r_inode = inode;
1297 			req->r_priv = aio_req;
1298 			list_add_tail(&req->r_private_item, &aio_req->osd_reqs);
1299 
1300 			pos += len;
1301 			continue;
1302 		}
1303 
1304 		ret = ceph_osdc_start_request(req->r_osdc, req, false);
1305 		if (!ret)
1306 			ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1307 
1308 		if (write)
1309 			ceph_update_write_metrics(metric, req->r_start_latency,
1310 						  req->r_end_latency, len, ret);
1311 		else
1312 			ceph_update_read_metrics(metric, req->r_start_latency,
1313 						 req->r_end_latency, len, ret);
1314 
1315 		size = i_size_read(inode);
1316 		if (!write) {
1317 			if (ret == -ENOENT)
1318 				ret = 0;
1319 			if (ret >= 0 && ret < len && pos + ret < size) {
1320 				struct iov_iter i;
1321 				int zlen = min_t(size_t, len - ret,
1322 						 size - pos - ret);
1323 
1324 				iov_iter_bvec(&i, READ, bvecs, num_pages, len);
1325 				iov_iter_advance(&i, ret);
1326 				iov_iter_zero(zlen, &i);
1327 				ret += zlen;
1328 			}
1329 			if (ret >= 0)
1330 				len = ret;
1331 		}
1332 
1333 		put_bvecs(bvecs, num_pages, should_dirty);
1334 		ceph_osdc_put_request(req);
1335 		if (ret < 0)
1336 			break;
1337 
1338 		pos += len;
1339 		if (!write && pos >= size)
1340 			break;
1341 
1342 		if (write && pos > size) {
1343 			if (ceph_inode_set_size(inode, pos))
1344 				ceph_check_caps(ceph_inode(inode),
1345 						CHECK_CAPS_AUTHONLY,
1346 						NULL);
1347 		}
1348 	}
1349 
1350 	if (aio_req) {
1351 		LIST_HEAD(osd_reqs);
1352 
1353 		if (aio_req->num_reqs == 0) {
1354 			kfree(aio_req);
1355 			return ret;
1356 		}
1357 
1358 		ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
1359 					      CEPH_CAP_FILE_RD);
1360 
1361 		list_splice(&aio_req->osd_reqs, &osd_reqs);
1362 		inode_dio_begin(inode);
1363 		while (!list_empty(&osd_reqs)) {
1364 			req = list_first_entry(&osd_reqs,
1365 					       struct ceph_osd_request,
1366 					       r_private_item);
1367 			list_del_init(&req->r_private_item);
1368 			if (ret >= 0)
1369 				ret = ceph_osdc_start_request(req->r_osdc,
1370 							      req, false);
1371 			if (ret < 0) {
1372 				req->r_result = ret;
1373 				ceph_aio_complete_req(req);
1374 			}
1375 		}
1376 		return -EIOCBQUEUED;
1377 	}
1378 
1379 	if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
1380 		ret = pos - iocb->ki_pos;
1381 		iocb->ki_pos = pos;
1382 	}
1383 	return ret;
1384 }
1385 
1386 /*
1387  * Synchronous write, straight from __user pointer or user pages.
1388  *
1389  * If write spans object boundary, just do multiple writes.  (For a
1390  * correct atomic write, we should e.g. take write locks on all
1391  * objects, rollback on failure, etc.)
1392  */
1393 static ssize_t
1394 ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
1395 		struct ceph_snap_context *snapc)
1396 {
1397 	struct file *file = iocb->ki_filp;
1398 	struct inode *inode = file_inode(file);
1399 	struct ceph_inode_info *ci = ceph_inode(inode);
1400 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1401 	struct ceph_vino vino;
1402 	struct ceph_osd_request *req;
1403 	struct page **pages;
1404 	u64 len;
1405 	int num_pages;
1406 	int written = 0;
1407 	int flags;
1408 	int ret;
1409 	bool check_caps = false;
1410 	struct timespec64 mtime = current_time(inode);
1411 	size_t count = iov_iter_count(from);
1412 
1413 	if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
1414 		return -EROFS;
1415 
1416 	dout("sync_write on file %p %lld~%u snapc %p seq %lld\n",
1417 	     file, pos, (unsigned)count, snapc, snapc->seq);
1418 
1419 	ret = filemap_write_and_wait_range(inode->i_mapping,
1420 					   pos, pos + count - 1);
1421 	if (ret < 0)
1422 		return ret;
1423 
1424 	ceph_fscache_invalidate(inode, false);
1425 	ret = invalidate_inode_pages2_range(inode->i_mapping,
1426 					    pos >> PAGE_SHIFT,
1427 					    (pos + count - 1) >> PAGE_SHIFT);
1428 	if (ret < 0)
1429 		dout("invalidate_inode_pages2_range returned %d\n", ret);
1430 
1431 	flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
1432 
1433 	while ((len = iov_iter_count(from)) > 0) {
1434 		size_t left;
1435 		int n;
1436 
1437 		vino = ceph_vino(inode);
1438 		req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1439 					    vino, pos, &len, 0, 1,
1440 					    CEPH_OSD_OP_WRITE, flags, snapc,
1441 					    ci->i_truncate_seq,
1442 					    ci->i_truncate_size,
1443 					    false);
1444 		if (IS_ERR(req)) {
1445 			ret = PTR_ERR(req);
1446 			break;
1447 		}
1448 
1449 		/*
1450 		 * write from beginning of first page,
1451 		 * regardless of io alignment
1452 		 */
1453 		num_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1454 
1455 		pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
1456 		if (IS_ERR(pages)) {
1457 			ret = PTR_ERR(pages);
1458 			goto out;
1459 		}
1460 
1461 		left = len;
1462 		for (n = 0; n < num_pages; n++) {
1463 			size_t plen = min_t(size_t, left, PAGE_SIZE);
1464 			ret = copy_page_from_iter(pages[n], 0, plen, from);
1465 			if (ret != plen) {
1466 				ret = -EFAULT;
1467 				break;
1468 			}
1469 			left -= ret;
1470 		}
1471 
1472 		if (ret < 0) {
1473 			ceph_release_page_vector(pages, num_pages);
1474 			goto out;
1475 		}
1476 
1477 		req->r_inode = inode;
1478 
1479 		osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
1480 						false, true);
1481 
1482 		req->r_mtime = mtime;
1483 		ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1484 		if (!ret)
1485 			ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1486 
1487 		ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
1488 					  req->r_end_latency, len, ret);
1489 out:
1490 		ceph_osdc_put_request(req);
1491 		if (ret != 0) {
1492 			ceph_set_error_write(ci);
1493 			break;
1494 		}
1495 
1496 		ceph_clear_error_write(ci);
1497 		pos += len;
1498 		written += len;
1499 		if (pos > i_size_read(inode)) {
1500 			check_caps = ceph_inode_set_size(inode, pos);
1501 			if (check_caps)
1502 				ceph_check_caps(ceph_inode(inode),
1503 						CHECK_CAPS_AUTHONLY,
1504 						NULL);
1505 		}
1506 
1507 	}
1508 
1509 	if (ret != -EOLDSNAPC && written > 0) {
1510 		ret = written;
1511 		iocb->ki_pos = pos;
1512 	}
1513 	return ret;
1514 }
1515 
1516 /*
1517  * Wrap generic_file_aio_read with checks for cap bits on the inode.
1518  * Atomically grab references, so that those bits are not released
1519  * back to the MDS mid-read.
1520  *
1521  * Hmm, the sync read case isn't actually async... should it be?
1522  */
1523 static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
1524 {
1525 	struct file *filp = iocb->ki_filp;
1526 	struct ceph_file_info *fi = filp->private_data;
1527 	size_t len = iov_iter_count(to);
1528 	struct inode *inode = file_inode(filp);
1529 	struct ceph_inode_info *ci = ceph_inode(inode);
1530 	bool direct_lock = iocb->ki_flags & IOCB_DIRECT;
1531 	ssize_t ret;
1532 	int want, got = 0;
1533 	int retry_op = 0, read = 0;
1534 
1535 again:
1536 	dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
1537 	     inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode);
1538 
1539 	if (ceph_inode_is_shutdown(inode))
1540 		return -ESTALE;
1541 
1542 	if (direct_lock)
1543 		ceph_start_io_direct(inode);
1544 	else
1545 		ceph_start_io_read(inode);
1546 
1547 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
1548 		want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1549 	else
1550 		want = CEPH_CAP_FILE_CACHE;
1551 	ret = ceph_get_caps(filp, CEPH_CAP_FILE_RD, want, -1, &got);
1552 	if (ret < 0) {
1553 		if (iocb->ki_flags & IOCB_DIRECT)
1554 			ceph_end_io_direct(inode);
1555 		else
1556 			ceph_end_io_read(inode);
1557 		return ret;
1558 	}
1559 
1560 	if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
1561 	    (iocb->ki_flags & IOCB_DIRECT) ||
1562 	    (fi->flags & CEPH_F_SYNC)) {
1563 
1564 		dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n",
1565 		     inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
1566 		     ceph_cap_string(got));
1567 
1568 		if (ci->i_inline_version == CEPH_INLINE_NONE) {
1569 			if (!retry_op && (iocb->ki_flags & IOCB_DIRECT)) {
1570 				ret = ceph_direct_read_write(iocb, to,
1571 							     NULL, NULL);
1572 				if (ret >= 0 && ret < len)
1573 					retry_op = CHECK_EOF;
1574 			} else {
1575 				ret = ceph_sync_read(iocb, to, &retry_op);
1576 			}
1577 		} else {
1578 			retry_op = READ_INLINE;
1579 		}
1580 	} else {
1581 		CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1582 		dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
1583 		     inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
1584 		     ceph_cap_string(got));
1585 		ceph_add_rw_context(fi, &rw_ctx);
1586 		ret = generic_file_read_iter(iocb, to);
1587 		ceph_del_rw_context(fi, &rw_ctx);
1588 	}
1589 
1590 	dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
1591 	     inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
1592 	ceph_put_cap_refs(ci, got);
1593 
1594 	if (direct_lock)
1595 		ceph_end_io_direct(inode);
1596 	else
1597 		ceph_end_io_read(inode);
1598 
1599 	if (retry_op > HAVE_RETRIED && ret >= 0) {
1600 		int statret;
1601 		struct page *page = NULL;
1602 		loff_t i_size;
1603 		if (retry_op == READ_INLINE) {
1604 			page = __page_cache_alloc(GFP_KERNEL);
1605 			if (!page)
1606 				return -ENOMEM;
1607 		}
1608 
1609 		statret = __ceph_do_getattr(inode, page,
1610 					    CEPH_STAT_CAP_INLINE_DATA, !!page);
1611 		if (statret < 0) {
1612 			if (page)
1613 				__free_page(page);
1614 			if (statret == -ENODATA) {
1615 				BUG_ON(retry_op != READ_INLINE);
1616 				goto again;
1617 			}
1618 			return statret;
1619 		}
1620 
1621 		i_size = i_size_read(inode);
1622 		if (retry_op == READ_INLINE) {
1623 			BUG_ON(ret > 0 || read > 0);
1624 			if (iocb->ki_pos < i_size &&
1625 			    iocb->ki_pos < PAGE_SIZE) {
1626 				loff_t end = min_t(loff_t, i_size,
1627 						   iocb->ki_pos + len);
1628 				end = min_t(loff_t, end, PAGE_SIZE);
1629 				if (statret < end)
1630 					zero_user_segment(page, statret, end);
1631 				ret = copy_page_to_iter(page,
1632 						iocb->ki_pos & ~PAGE_MASK,
1633 						end - iocb->ki_pos, to);
1634 				iocb->ki_pos += ret;
1635 				read += ret;
1636 			}
1637 			if (iocb->ki_pos < i_size && read < len) {
1638 				size_t zlen = min_t(size_t, len - read,
1639 						    i_size - iocb->ki_pos);
1640 				ret = iov_iter_zero(zlen, to);
1641 				iocb->ki_pos += ret;
1642 				read += ret;
1643 			}
1644 			__free_pages(page, 0);
1645 			return read;
1646 		}
1647 
1648 		/* hit EOF or hole? */
1649 		if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
1650 		    ret < len) {
1651 			dout("sync_read hit hole, ppos %lld < size %lld"
1652 			     ", reading more\n", iocb->ki_pos, i_size);
1653 
1654 			read += ret;
1655 			len -= ret;
1656 			retry_op = HAVE_RETRIED;
1657 			goto again;
1658 		}
1659 	}
1660 
1661 	if (ret >= 0)
1662 		ret += read;
1663 
1664 	return ret;
1665 }
1666 
1667 /*
1668  * Take cap references to avoid releasing caps to MDS mid-write.
1669  *
1670  * If we are synchronous, and write with an old snap context, the OSD
1671  * may return EOLDSNAPC.  In that case, retry the write.. _after_
1672  * dropping our cap refs and allowing the pending snap to logically
1673  * complete _before_ this write occurs.
1674  *
1675  * If we are near ENOSPC, write synchronously.
1676  */
1677 static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
1678 {
1679 	struct file *file = iocb->ki_filp;
1680 	struct ceph_file_info *fi = file->private_data;
1681 	struct inode *inode = file_inode(file);
1682 	struct ceph_inode_info *ci = ceph_inode(inode);
1683 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1684 	struct ceph_osd_client *osdc = &fsc->client->osdc;
1685 	struct ceph_cap_flush *prealloc_cf;
1686 	ssize_t count, written = 0;
1687 	int err, want, got;
1688 	bool direct_lock = false;
1689 	u32 map_flags;
1690 	u64 pool_flags;
1691 	loff_t pos;
1692 	loff_t limit = max(i_size_read(inode), fsc->max_file_size);
1693 
1694 	if (ceph_inode_is_shutdown(inode))
1695 		return -ESTALE;
1696 
1697 	if (ceph_snap(inode) != CEPH_NOSNAP)
1698 		return -EROFS;
1699 
1700 	prealloc_cf = ceph_alloc_cap_flush();
1701 	if (!prealloc_cf)
1702 		return -ENOMEM;
1703 
1704 	if ((iocb->ki_flags & (IOCB_DIRECT | IOCB_APPEND)) == IOCB_DIRECT)
1705 		direct_lock = true;
1706 
1707 retry_snap:
1708 	if (direct_lock)
1709 		ceph_start_io_direct(inode);
1710 	else
1711 		ceph_start_io_write(inode);
1712 
1713 	/* We can write back this queue in page reclaim */
1714 	current->backing_dev_info = inode_to_bdi(inode);
1715 
1716 	if (iocb->ki_flags & IOCB_APPEND) {
1717 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
1718 		if (err < 0)
1719 			goto out;
1720 	}
1721 
1722 	err = generic_write_checks(iocb, from);
1723 	if (err <= 0)
1724 		goto out;
1725 
1726 	pos = iocb->ki_pos;
1727 	if (unlikely(pos >= limit)) {
1728 		err = -EFBIG;
1729 		goto out;
1730 	} else {
1731 		iov_iter_truncate(from, limit - pos);
1732 	}
1733 
1734 	count = iov_iter_count(from);
1735 	if (ceph_quota_is_max_bytes_exceeded(inode, pos + count)) {
1736 		err = -EDQUOT;
1737 		goto out;
1738 	}
1739 
1740 	down_read(&osdc->lock);
1741 	map_flags = osdc->osdmap->flags;
1742 	pool_flags = ceph_pg_pool_flags(osdc->osdmap, ci->i_layout.pool_id);
1743 	up_read(&osdc->lock);
1744 	if ((map_flags & CEPH_OSDMAP_FULL) ||
1745 	    (pool_flags & CEPH_POOL_FLAG_FULL)) {
1746 		err = -ENOSPC;
1747 		goto out;
1748 	}
1749 
1750 	err = file_remove_privs(file);
1751 	if (err)
1752 		goto out;
1753 
1754 	if (ci->i_inline_version != CEPH_INLINE_NONE) {
1755 		err = ceph_uninline_data(file, NULL);
1756 		if (err < 0)
1757 			goto out;
1758 	}
1759 
1760 	dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
1761 	     inode, ceph_vinop(inode), pos, count, i_size_read(inode));
1762 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
1763 		want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1764 	else
1765 		want = CEPH_CAP_FILE_BUFFER;
1766 	got = 0;
1767 	err = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, pos + count, &got);
1768 	if (err < 0)
1769 		goto out;
1770 
1771 	err = file_update_time(file);
1772 	if (err)
1773 		goto out_caps;
1774 
1775 	inode_inc_iversion_raw(inode);
1776 
1777 	dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
1778 	     inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
1779 
1780 	if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
1781 	    (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC) ||
1782 	    (ci->i_ceph_flags & CEPH_I_ERROR_WRITE)) {
1783 		struct ceph_snap_context *snapc;
1784 		struct iov_iter data;
1785 
1786 		spin_lock(&ci->i_ceph_lock);
1787 		if (__ceph_have_pending_cap_snap(ci)) {
1788 			struct ceph_cap_snap *capsnap =
1789 					list_last_entry(&ci->i_cap_snaps,
1790 							struct ceph_cap_snap,
1791 							ci_item);
1792 			snapc = ceph_get_snap_context(capsnap->context);
1793 		} else {
1794 			BUG_ON(!ci->i_head_snapc);
1795 			snapc = ceph_get_snap_context(ci->i_head_snapc);
1796 		}
1797 		spin_unlock(&ci->i_ceph_lock);
1798 
1799 		/* we might need to revert back to that point */
1800 		data = *from;
1801 		if (iocb->ki_flags & IOCB_DIRECT)
1802 			written = ceph_direct_read_write(iocb, &data, snapc,
1803 							 &prealloc_cf);
1804 		else
1805 			written = ceph_sync_write(iocb, &data, pos, snapc);
1806 		if (direct_lock)
1807 			ceph_end_io_direct(inode);
1808 		else
1809 			ceph_end_io_write(inode);
1810 		if (written > 0)
1811 			iov_iter_advance(from, written);
1812 		ceph_put_snap_context(snapc);
1813 	} else {
1814 		/*
1815 		 * No need to acquire the i_truncate_mutex. Because
1816 		 * the MDS revokes Fwb caps before sending truncate
1817 		 * message to us. We can't get Fwb cap while there
1818 		 * are pending vmtruncate. So write and vmtruncate
1819 		 * can not run at the same time
1820 		 */
1821 		written = generic_perform_write(file, from, pos);
1822 		if (likely(written >= 0))
1823 			iocb->ki_pos = pos + written;
1824 		ceph_end_io_write(inode);
1825 	}
1826 
1827 	if (written >= 0) {
1828 		int dirty;
1829 
1830 		spin_lock(&ci->i_ceph_lock);
1831 		ci->i_inline_version = CEPH_INLINE_NONE;
1832 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1833 					       &prealloc_cf);
1834 		spin_unlock(&ci->i_ceph_lock);
1835 		if (dirty)
1836 			__mark_inode_dirty(inode, dirty);
1837 		if (ceph_quota_is_max_bytes_approaching(inode, iocb->ki_pos))
1838 			ceph_check_caps(ci, 0, NULL);
1839 	}
1840 
1841 	dout("aio_write %p %llx.%llx %llu~%u  dropping cap refs on %s\n",
1842 	     inode, ceph_vinop(inode), pos, (unsigned)count,
1843 	     ceph_cap_string(got));
1844 	ceph_put_cap_refs(ci, got);
1845 
1846 	if (written == -EOLDSNAPC) {
1847 		dout("aio_write %p %llx.%llx %llu~%u" "got EOLDSNAPC, retrying\n",
1848 		     inode, ceph_vinop(inode), pos, (unsigned)count);
1849 		goto retry_snap;
1850 	}
1851 
1852 	if (written >= 0) {
1853 		if ((map_flags & CEPH_OSDMAP_NEARFULL) ||
1854 		    (pool_flags & CEPH_POOL_FLAG_NEARFULL))
1855 			iocb->ki_flags |= IOCB_DSYNC;
1856 		written = generic_write_sync(iocb, written);
1857 	}
1858 
1859 	goto out_unlocked;
1860 out_caps:
1861 	ceph_put_cap_refs(ci, got);
1862 out:
1863 	if (direct_lock)
1864 		ceph_end_io_direct(inode);
1865 	else
1866 		ceph_end_io_write(inode);
1867 out_unlocked:
1868 	ceph_free_cap_flush(prealloc_cf);
1869 	current->backing_dev_info = NULL;
1870 	return written ? written : err;
1871 }
1872 
1873 /*
1874  * llseek.  be sure to verify file size on SEEK_END.
1875  */
1876 static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
1877 {
1878 	struct inode *inode = file->f_mapping->host;
1879 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1880 	loff_t i_size;
1881 	loff_t ret;
1882 
1883 	inode_lock(inode);
1884 
1885 	if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
1886 		ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
1887 		if (ret < 0)
1888 			goto out;
1889 	}
1890 
1891 	i_size = i_size_read(inode);
1892 	switch (whence) {
1893 	case SEEK_END:
1894 		offset += i_size;
1895 		break;
1896 	case SEEK_CUR:
1897 		/*
1898 		 * Here we special-case the lseek(fd, 0, SEEK_CUR)
1899 		 * position-querying operation.  Avoid rewriting the "same"
1900 		 * f_pos value back to the file because a concurrent read(),
1901 		 * write() or lseek() might have altered it
1902 		 */
1903 		if (offset == 0) {
1904 			ret = file->f_pos;
1905 			goto out;
1906 		}
1907 		offset += file->f_pos;
1908 		break;
1909 	case SEEK_DATA:
1910 		if (offset < 0 || offset >= i_size) {
1911 			ret = -ENXIO;
1912 			goto out;
1913 		}
1914 		break;
1915 	case SEEK_HOLE:
1916 		if (offset < 0 || offset >= i_size) {
1917 			ret = -ENXIO;
1918 			goto out;
1919 		}
1920 		offset = i_size;
1921 		break;
1922 	}
1923 
1924 	ret = vfs_setpos(file, offset, max(i_size, fsc->max_file_size));
1925 
1926 out:
1927 	inode_unlock(inode);
1928 	return ret;
1929 }
1930 
1931 static inline void ceph_zero_partial_page(
1932 	struct inode *inode, loff_t offset, unsigned size)
1933 {
1934 	struct page *page;
1935 	pgoff_t index = offset >> PAGE_SHIFT;
1936 
1937 	page = find_lock_page(inode->i_mapping, index);
1938 	if (page) {
1939 		wait_on_page_writeback(page);
1940 		zero_user(page, offset & (PAGE_SIZE - 1), size);
1941 		unlock_page(page);
1942 		put_page(page);
1943 	}
1944 }
1945 
1946 static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
1947 				      loff_t length)
1948 {
1949 	loff_t nearly = round_up(offset, PAGE_SIZE);
1950 	if (offset < nearly) {
1951 		loff_t size = nearly - offset;
1952 		if (length < size)
1953 			size = length;
1954 		ceph_zero_partial_page(inode, offset, size);
1955 		offset += size;
1956 		length -= size;
1957 	}
1958 	if (length >= PAGE_SIZE) {
1959 		loff_t size = round_down(length, PAGE_SIZE);
1960 		truncate_pagecache_range(inode, offset, offset + size - 1);
1961 		offset += size;
1962 		length -= size;
1963 	}
1964 	if (length)
1965 		ceph_zero_partial_page(inode, offset, length);
1966 }
1967 
1968 static int ceph_zero_partial_object(struct inode *inode,
1969 				    loff_t offset, loff_t *length)
1970 {
1971 	struct ceph_inode_info *ci = ceph_inode(inode);
1972 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1973 	struct ceph_osd_request *req;
1974 	int ret = 0;
1975 	loff_t zero = 0;
1976 	int op;
1977 
1978 	if (!length) {
1979 		op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
1980 		length = &zero;
1981 	} else {
1982 		op = CEPH_OSD_OP_ZERO;
1983 	}
1984 
1985 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1986 					ceph_vino(inode),
1987 					offset, length,
1988 					0, 1, op,
1989 					CEPH_OSD_FLAG_WRITE,
1990 					NULL, 0, 0, false);
1991 	if (IS_ERR(req)) {
1992 		ret = PTR_ERR(req);
1993 		goto out;
1994 	}
1995 
1996 	req->r_mtime = inode->i_mtime;
1997 	ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1998 	if (!ret) {
1999 		ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
2000 		if (ret == -ENOENT)
2001 			ret = 0;
2002 	}
2003 	ceph_osdc_put_request(req);
2004 
2005 out:
2006 	return ret;
2007 }
2008 
2009 static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
2010 {
2011 	int ret = 0;
2012 	struct ceph_inode_info *ci = ceph_inode(inode);
2013 	s32 stripe_unit = ci->i_layout.stripe_unit;
2014 	s32 stripe_count = ci->i_layout.stripe_count;
2015 	s32 object_size = ci->i_layout.object_size;
2016 	u64 object_set_size = object_size * stripe_count;
2017 	u64 nearly, t;
2018 
2019 	/* round offset up to next period boundary */
2020 	nearly = offset + object_set_size - 1;
2021 	t = nearly;
2022 	nearly -= do_div(t, object_set_size);
2023 
2024 	while (length && offset < nearly) {
2025 		loff_t size = length;
2026 		ret = ceph_zero_partial_object(inode, offset, &size);
2027 		if (ret < 0)
2028 			return ret;
2029 		offset += size;
2030 		length -= size;
2031 	}
2032 	while (length >= object_set_size) {
2033 		int i;
2034 		loff_t pos = offset;
2035 		for (i = 0; i < stripe_count; ++i) {
2036 			ret = ceph_zero_partial_object(inode, pos, NULL);
2037 			if (ret < 0)
2038 				return ret;
2039 			pos += stripe_unit;
2040 		}
2041 		offset += object_set_size;
2042 		length -= object_set_size;
2043 	}
2044 	while (length) {
2045 		loff_t size = length;
2046 		ret = ceph_zero_partial_object(inode, offset, &size);
2047 		if (ret < 0)
2048 			return ret;
2049 		offset += size;
2050 		length -= size;
2051 	}
2052 	return ret;
2053 }
2054 
2055 static long ceph_fallocate(struct file *file, int mode,
2056 				loff_t offset, loff_t length)
2057 {
2058 	struct ceph_file_info *fi = file->private_data;
2059 	struct inode *inode = file_inode(file);
2060 	struct ceph_inode_info *ci = ceph_inode(inode);
2061 	struct ceph_cap_flush *prealloc_cf;
2062 	int want, got = 0;
2063 	int dirty;
2064 	int ret = 0;
2065 	loff_t endoff = 0;
2066 	loff_t size;
2067 
2068 	if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2069 		return -EOPNOTSUPP;
2070 
2071 	if (!S_ISREG(inode->i_mode))
2072 		return -EOPNOTSUPP;
2073 
2074 	prealloc_cf = ceph_alloc_cap_flush();
2075 	if (!prealloc_cf)
2076 		return -ENOMEM;
2077 
2078 	inode_lock(inode);
2079 
2080 	if (ceph_snap(inode) != CEPH_NOSNAP) {
2081 		ret = -EROFS;
2082 		goto unlock;
2083 	}
2084 
2085 	if (ci->i_inline_version != CEPH_INLINE_NONE) {
2086 		ret = ceph_uninline_data(file, NULL);
2087 		if (ret < 0)
2088 			goto unlock;
2089 	}
2090 
2091 	size = i_size_read(inode);
2092 
2093 	/* Are we punching a hole beyond EOF? */
2094 	if (offset >= size)
2095 		goto unlock;
2096 	if ((offset + length) > size)
2097 		length = size - offset;
2098 
2099 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
2100 		want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
2101 	else
2102 		want = CEPH_CAP_FILE_BUFFER;
2103 
2104 	ret = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, endoff, &got);
2105 	if (ret < 0)
2106 		goto unlock;
2107 
2108 	filemap_invalidate_lock(inode->i_mapping);
2109 	ceph_fscache_invalidate(inode, false);
2110 	ceph_zero_pagecache_range(inode, offset, length);
2111 	ret = ceph_zero_objects(inode, offset, length);
2112 
2113 	if (!ret) {
2114 		spin_lock(&ci->i_ceph_lock);
2115 		ci->i_inline_version = CEPH_INLINE_NONE;
2116 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
2117 					       &prealloc_cf);
2118 		spin_unlock(&ci->i_ceph_lock);
2119 		if (dirty)
2120 			__mark_inode_dirty(inode, dirty);
2121 	}
2122 	filemap_invalidate_unlock(inode->i_mapping);
2123 
2124 	ceph_put_cap_refs(ci, got);
2125 unlock:
2126 	inode_unlock(inode);
2127 	ceph_free_cap_flush(prealloc_cf);
2128 	return ret;
2129 }
2130 
2131 /*
2132  * This function tries to get FILE_WR capabilities for dst_ci and FILE_RD for
2133  * src_ci.  Two attempts are made to obtain both caps, and an error is return if
2134  * this fails; zero is returned on success.
2135  */
2136 static int get_rd_wr_caps(struct file *src_filp, int *src_got,
2137 			  struct file *dst_filp,
2138 			  loff_t dst_endoff, int *dst_got)
2139 {
2140 	int ret = 0;
2141 	bool retrying = false;
2142 
2143 retry_caps:
2144 	ret = ceph_get_caps(dst_filp, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER,
2145 			    dst_endoff, dst_got);
2146 	if (ret < 0)
2147 		return ret;
2148 
2149 	/*
2150 	 * Since we're already holding the FILE_WR capability for the dst file,
2151 	 * we would risk a deadlock by using ceph_get_caps.  Thus, we'll do some
2152 	 * retry dance instead to try to get both capabilities.
2153 	 */
2154 	ret = ceph_try_get_caps(file_inode(src_filp),
2155 				CEPH_CAP_FILE_RD, CEPH_CAP_FILE_SHARED,
2156 				false, src_got);
2157 	if (ret <= 0) {
2158 		/* Start by dropping dst_ci caps and getting src_ci caps */
2159 		ceph_put_cap_refs(ceph_inode(file_inode(dst_filp)), *dst_got);
2160 		if (retrying) {
2161 			if (!ret)
2162 				/* ceph_try_get_caps masks EAGAIN */
2163 				ret = -EAGAIN;
2164 			return ret;
2165 		}
2166 		ret = ceph_get_caps(src_filp, CEPH_CAP_FILE_RD,
2167 				    CEPH_CAP_FILE_SHARED, -1, src_got);
2168 		if (ret < 0)
2169 			return ret;
2170 		/*... drop src_ci caps too, and retry */
2171 		ceph_put_cap_refs(ceph_inode(file_inode(src_filp)), *src_got);
2172 		retrying = true;
2173 		goto retry_caps;
2174 	}
2175 	return ret;
2176 }
2177 
2178 static void put_rd_wr_caps(struct ceph_inode_info *src_ci, int src_got,
2179 			   struct ceph_inode_info *dst_ci, int dst_got)
2180 {
2181 	ceph_put_cap_refs(src_ci, src_got);
2182 	ceph_put_cap_refs(dst_ci, dst_got);
2183 }
2184 
2185 /*
2186  * This function does several size-related checks, returning an error if:
2187  *  - source file is smaller than off+len
2188  *  - destination file size is not OK (inode_newsize_ok())
2189  *  - max bytes quotas is exceeded
2190  */
2191 static int is_file_size_ok(struct inode *src_inode, struct inode *dst_inode,
2192 			   loff_t src_off, loff_t dst_off, size_t len)
2193 {
2194 	loff_t size, endoff;
2195 
2196 	size = i_size_read(src_inode);
2197 	/*
2198 	 * Don't copy beyond source file EOF.  Instead of simply setting length
2199 	 * to (size - src_off), just drop to VFS default implementation, as the
2200 	 * local i_size may be stale due to other clients writing to the source
2201 	 * inode.
2202 	 */
2203 	if (src_off + len > size) {
2204 		dout("Copy beyond EOF (%llu + %zu > %llu)\n",
2205 		     src_off, len, size);
2206 		return -EOPNOTSUPP;
2207 	}
2208 	size = i_size_read(dst_inode);
2209 
2210 	endoff = dst_off + len;
2211 	if (inode_newsize_ok(dst_inode, endoff))
2212 		return -EOPNOTSUPP;
2213 
2214 	if (ceph_quota_is_max_bytes_exceeded(dst_inode, endoff))
2215 		return -EDQUOT;
2216 
2217 	return 0;
2218 }
2219 
2220 static struct ceph_osd_request *
2221 ceph_alloc_copyfrom_request(struct ceph_osd_client *osdc,
2222 			    u64 src_snapid,
2223 			    struct ceph_object_id *src_oid,
2224 			    struct ceph_object_locator *src_oloc,
2225 			    struct ceph_object_id *dst_oid,
2226 			    struct ceph_object_locator *dst_oloc,
2227 			    u32 truncate_seq, u64 truncate_size)
2228 {
2229 	struct ceph_osd_request *req;
2230 	int ret;
2231 	u32 src_fadvise_flags =
2232 		CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
2233 		CEPH_OSD_OP_FLAG_FADVISE_NOCACHE;
2234 	u32 dst_fadvise_flags =
2235 		CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
2236 		CEPH_OSD_OP_FLAG_FADVISE_DONTNEED;
2237 
2238 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
2239 	if (!req)
2240 		return ERR_PTR(-ENOMEM);
2241 
2242 	req->r_flags = CEPH_OSD_FLAG_WRITE;
2243 
2244 	ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
2245 	ceph_oid_copy(&req->r_t.base_oid, dst_oid);
2246 
2247 	ret = osd_req_op_copy_from_init(req, src_snapid, 0,
2248 					src_oid, src_oloc,
2249 					src_fadvise_flags,
2250 					dst_fadvise_flags,
2251 					truncate_seq,
2252 					truncate_size,
2253 					CEPH_OSD_COPY_FROM_FLAG_TRUNCATE_SEQ);
2254 	if (ret)
2255 		goto out;
2256 
2257 	ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
2258 	if (ret)
2259 		goto out;
2260 
2261 	return req;
2262 
2263 out:
2264 	ceph_osdc_put_request(req);
2265 	return ERR_PTR(ret);
2266 }
2267 
2268 static ssize_t ceph_do_objects_copy(struct ceph_inode_info *src_ci, u64 *src_off,
2269 				    struct ceph_inode_info *dst_ci, u64 *dst_off,
2270 				    struct ceph_fs_client *fsc,
2271 				    size_t len, unsigned int flags)
2272 {
2273 	struct ceph_object_locator src_oloc, dst_oloc;
2274 	struct ceph_object_id src_oid, dst_oid;
2275 	struct ceph_osd_client *osdc;
2276 	struct ceph_osd_request *req;
2277 	size_t bytes = 0;
2278 	u64 src_objnum, src_objoff, dst_objnum, dst_objoff;
2279 	u32 src_objlen, dst_objlen;
2280 	u32 object_size = src_ci->i_layout.object_size;
2281 	int ret;
2282 
2283 	src_oloc.pool = src_ci->i_layout.pool_id;
2284 	src_oloc.pool_ns = ceph_try_get_string(src_ci->i_layout.pool_ns);
2285 	dst_oloc.pool = dst_ci->i_layout.pool_id;
2286 	dst_oloc.pool_ns = ceph_try_get_string(dst_ci->i_layout.pool_ns);
2287 	osdc = &fsc->client->osdc;
2288 
2289 	while (len >= object_size) {
2290 		ceph_calc_file_object_mapping(&src_ci->i_layout, *src_off,
2291 					      object_size, &src_objnum,
2292 					      &src_objoff, &src_objlen);
2293 		ceph_calc_file_object_mapping(&dst_ci->i_layout, *dst_off,
2294 					      object_size, &dst_objnum,
2295 					      &dst_objoff, &dst_objlen);
2296 		ceph_oid_init(&src_oid);
2297 		ceph_oid_printf(&src_oid, "%llx.%08llx",
2298 				src_ci->i_vino.ino, src_objnum);
2299 		ceph_oid_init(&dst_oid);
2300 		ceph_oid_printf(&dst_oid, "%llx.%08llx",
2301 				dst_ci->i_vino.ino, dst_objnum);
2302 		/* Do an object remote copy */
2303 		req = ceph_alloc_copyfrom_request(osdc, src_ci->i_vino.snap,
2304 						  &src_oid, &src_oloc,
2305 						  &dst_oid, &dst_oloc,
2306 						  dst_ci->i_truncate_seq,
2307 						  dst_ci->i_truncate_size);
2308 		if (IS_ERR(req))
2309 			ret = PTR_ERR(req);
2310 		else {
2311 			ceph_osdc_start_request(osdc, req, false);
2312 			ret = ceph_osdc_wait_request(osdc, req);
2313 			ceph_update_copyfrom_metrics(&fsc->mdsc->metric,
2314 						     req->r_start_latency,
2315 						     req->r_end_latency,
2316 						     object_size, ret);
2317 			ceph_osdc_put_request(req);
2318 		}
2319 		if (ret) {
2320 			if (ret == -EOPNOTSUPP) {
2321 				fsc->have_copy_from2 = false;
2322 				pr_notice("OSDs don't support copy-from2; disabling copy offload\n");
2323 			}
2324 			dout("ceph_osdc_copy_from returned %d\n", ret);
2325 			if (!bytes)
2326 				bytes = ret;
2327 			goto out;
2328 		}
2329 		len -= object_size;
2330 		bytes += object_size;
2331 		*src_off += object_size;
2332 		*dst_off += object_size;
2333 	}
2334 
2335 out:
2336 	ceph_oloc_destroy(&src_oloc);
2337 	ceph_oloc_destroy(&dst_oloc);
2338 	return bytes;
2339 }
2340 
2341 static ssize_t __ceph_copy_file_range(struct file *src_file, loff_t src_off,
2342 				      struct file *dst_file, loff_t dst_off,
2343 				      size_t len, unsigned int flags)
2344 {
2345 	struct inode *src_inode = file_inode(src_file);
2346 	struct inode *dst_inode = file_inode(dst_file);
2347 	struct ceph_inode_info *src_ci = ceph_inode(src_inode);
2348 	struct ceph_inode_info *dst_ci = ceph_inode(dst_inode);
2349 	struct ceph_cap_flush *prealloc_cf;
2350 	struct ceph_fs_client *src_fsc = ceph_inode_to_client(src_inode);
2351 	loff_t size;
2352 	ssize_t ret = -EIO, bytes;
2353 	u64 src_objnum, dst_objnum, src_objoff, dst_objoff;
2354 	u32 src_objlen, dst_objlen;
2355 	int src_got = 0, dst_got = 0, err, dirty;
2356 
2357 	if (src_inode->i_sb != dst_inode->i_sb) {
2358 		struct ceph_fs_client *dst_fsc = ceph_inode_to_client(dst_inode);
2359 
2360 		if (ceph_fsid_compare(&src_fsc->client->fsid,
2361 				      &dst_fsc->client->fsid)) {
2362 			dout("Copying files across clusters: src: %pU dst: %pU\n",
2363 			     &src_fsc->client->fsid, &dst_fsc->client->fsid);
2364 			return -EXDEV;
2365 		}
2366 	}
2367 	if (ceph_snap(dst_inode) != CEPH_NOSNAP)
2368 		return -EROFS;
2369 
2370 	/*
2371 	 * Some of the checks below will return -EOPNOTSUPP, which will force a
2372 	 * fallback to the default VFS copy_file_range implementation.  This is
2373 	 * desirable in several cases (for ex, the 'len' is smaller than the
2374 	 * size of the objects, or in cases where that would be more
2375 	 * efficient).
2376 	 */
2377 
2378 	if (ceph_test_mount_opt(src_fsc, NOCOPYFROM))
2379 		return -EOPNOTSUPP;
2380 
2381 	if (!src_fsc->have_copy_from2)
2382 		return -EOPNOTSUPP;
2383 
2384 	/*
2385 	 * Striped file layouts require that we copy partial objects, but the
2386 	 * OSD copy-from operation only supports full-object copies.  Limit
2387 	 * this to non-striped file layouts for now.
2388 	 */
2389 	if ((src_ci->i_layout.stripe_unit != dst_ci->i_layout.stripe_unit) ||
2390 	    (src_ci->i_layout.stripe_count != 1) ||
2391 	    (dst_ci->i_layout.stripe_count != 1) ||
2392 	    (src_ci->i_layout.object_size != dst_ci->i_layout.object_size)) {
2393 		dout("Invalid src/dst files layout\n");
2394 		return -EOPNOTSUPP;
2395 	}
2396 
2397 	if (len < src_ci->i_layout.object_size)
2398 		return -EOPNOTSUPP; /* no remote copy will be done */
2399 
2400 	prealloc_cf = ceph_alloc_cap_flush();
2401 	if (!prealloc_cf)
2402 		return -ENOMEM;
2403 
2404 	/* Start by sync'ing the source and destination files */
2405 	ret = file_write_and_wait_range(src_file, src_off, (src_off + len));
2406 	if (ret < 0) {
2407 		dout("failed to write src file (%zd)\n", ret);
2408 		goto out;
2409 	}
2410 	ret = file_write_and_wait_range(dst_file, dst_off, (dst_off + len));
2411 	if (ret < 0) {
2412 		dout("failed to write dst file (%zd)\n", ret);
2413 		goto out;
2414 	}
2415 
2416 	/*
2417 	 * We need FILE_WR caps for dst_ci and FILE_RD for src_ci as other
2418 	 * clients may have dirty data in their caches.  And OSDs know nothing
2419 	 * about caps, so they can't safely do the remote object copies.
2420 	 */
2421 	err = get_rd_wr_caps(src_file, &src_got,
2422 			     dst_file, (dst_off + len), &dst_got);
2423 	if (err < 0) {
2424 		dout("get_rd_wr_caps returned %d\n", err);
2425 		ret = -EOPNOTSUPP;
2426 		goto out;
2427 	}
2428 
2429 	ret = is_file_size_ok(src_inode, dst_inode, src_off, dst_off, len);
2430 	if (ret < 0)
2431 		goto out_caps;
2432 
2433 	/* Drop dst file cached pages */
2434 	ceph_fscache_invalidate(dst_inode, false);
2435 	ret = invalidate_inode_pages2_range(dst_inode->i_mapping,
2436 					    dst_off >> PAGE_SHIFT,
2437 					    (dst_off + len) >> PAGE_SHIFT);
2438 	if (ret < 0) {
2439 		dout("Failed to invalidate inode pages (%zd)\n", ret);
2440 		ret = 0; /* XXX */
2441 	}
2442 	ceph_calc_file_object_mapping(&src_ci->i_layout, src_off,
2443 				      src_ci->i_layout.object_size,
2444 				      &src_objnum, &src_objoff, &src_objlen);
2445 	ceph_calc_file_object_mapping(&dst_ci->i_layout, dst_off,
2446 				      dst_ci->i_layout.object_size,
2447 				      &dst_objnum, &dst_objoff, &dst_objlen);
2448 	/* object-level offsets need to the same */
2449 	if (src_objoff != dst_objoff) {
2450 		ret = -EOPNOTSUPP;
2451 		goto out_caps;
2452 	}
2453 
2454 	/*
2455 	 * Do a manual copy if the object offset isn't object aligned.
2456 	 * 'src_objlen' contains the bytes left until the end of the object,
2457 	 * starting at the src_off
2458 	 */
2459 	if (src_objoff) {
2460 		dout("Initial partial copy of %u bytes\n", src_objlen);
2461 
2462 		/*
2463 		 * we need to temporarily drop all caps as we'll be calling
2464 		 * {read,write}_iter, which will get caps again.
2465 		 */
2466 		put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
2467 		ret = do_splice_direct(src_file, &src_off, dst_file,
2468 				       &dst_off, src_objlen, flags);
2469 		/* Abort on short copies or on error */
2470 		if (ret < src_objlen) {
2471 			dout("Failed partial copy (%zd)\n", ret);
2472 			goto out;
2473 		}
2474 		len -= ret;
2475 		err = get_rd_wr_caps(src_file, &src_got,
2476 				     dst_file, (dst_off + len), &dst_got);
2477 		if (err < 0)
2478 			goto out;
2479 		err = is_file_size_ok(src_inode, dst_inode,
2480 				      src_off, dst_off, len);
2481 		if (err < 0)
2482 			goto out_caps;
2483 	}
2484 
2485 	size = i_size_read(dst_inode);
2486 	bytes = ceph_do_objects_copy(src_ci, &src_off, dst_ci, &dst_off,
2487 				     src_fsc, len, flags);
2488 	if (bytes <= 0) {
2489 		if (!ret)
2490 			ret = bytes;
2491 		goto out_caps;
2492 	}
2493 	dout("Copied %zu bytes out of %zu\n", bytes, len);
2494 	len -= bytes;
2495 	ret += bytes;
2496 
2497 	file_update_time(dst_file);
2498 	inode_inc_iversion_raw(dst_inode);
2499 
2500 	if (dst_off > size) {
2501 		/* Let the MDS know about dst file size change */
2502 		if (ceph_inode_set_size(dst_inode, dst_off) ||
2503 		    ceph_quota_is_max_bytes_approaching(dst_inode, dst_off))
2504 			ceph_check_caps(dst_ci, CHECK_CAPS_AUTHONLY, NULL);
2505 	}
2506 	/* Mark Fw dirty */
2507 	spin_lock(&dst_ci->i_ceph_lock);
2508 	dst_ci->i_inline_version = CEPH_INLINE_NONE;
2509 	dirty = __ceph_mark_dirty_caps(dst_ci, CEPH_CAP_FILE_WR, &prealloc_cf);
2510 	spin_unlock(&dst_ci->i_ceph_lock);
2511 	if (dirty)
2512 		__mark_inode_dirty(dst_inode, dirty);
2513 
2514 out_caps:
2515 	put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
2516 
2517 	/*
2518 	 * Do the final manual copy if we still have some bytes left, unless
2519 	 * there were errors in remote object copies (len >= object_size).
2520 	 */
2521 	if (len && (len < src_ci->i_layout.object_size)) {
2522 		dout("Final partial copy of %zu bytes\n", len);
2523 		bytes = do_splice_direct(src_file, &src_off, dst_file,
2524 					 &dst_off, len, flags);
2525 		if (bytes > 0)
2526 			ret += bytes;
2527 		else
2528 			dout("Failed partial copy (%zd)\n", bytes);
2529 	}
2530 
2531 out:
2532 	ceph_free_cap_flush(prealloc_cf);
2533 
2534 	return ret;
2535 }
2536 
2537 static ssize_t ceph_copy_file_range(struct file *src_file, loff_t src_off,
2538 				    struct file *dst_file, loff_t dst_off,
2539 				    size_t len, unsigned int flags)
2540 {
2541 	ssize_t ret;
2542 
2543 	ret = __ceph_copy_file_range(src_file, src_off, dst_file, dst_off,
2544 				     len, flags);
2545 
2546 	if (ret == -EOPNOTSUPP || ret == -EXDEV)
2547 		ret = generic_copy_file_range(src_file, src_off, dst_file,
2548 					      dst_off, len, flags);
2549 	return ret;
2550 }
2551 
2552 const struct file_operations ceph_file_fops = {
2553 	.open = ceph_open,
2554 	.release = ceph_release,
2555 	.llseek = ceph_llseek,
2556 	.read_iter = ceph_read_iter,
2557 	.write_iter = ceph_write_iter,
2558 	.mmap = ceph_mmap,
2559 	.fsync = ceph_fsync,
2560 	.lock = ceph_lock,
2561 	.setlease = simple_nosetlease,
2562 	.flock = ceph_flock,
2563 	.splice_read = generic_file_splice_read,
2564 	.splice_write = iter_file_splice_write,
2565 	.unlocked_ioctl = ceph_ioctl,
2566 	.compat_ioctl = compat_ptr_ioctl,
2567 	.fallocate	= ceph_fallocate,
2568 	.copy_file_range = ceph_copy_file_range,
2569 };
2570