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