xref: /openbmc/linux/fs/ceph/addr.c (revision 62975d27)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 
4 #include <linux/backing-dev.h>
5 #include <linux/fs.h>
6 #include <linux/mm.h>
7 #include <linux/pagemap.h>
8 #include <linux/writeback.h>	/* generic_writepages */
9 #include <linux/slab.h>
10 #include <linux/pagevec.h>
11 #include <linux/task_io_accounting_ops.h>
12 #include <linux/signal.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 "metric.h"
20 #include <linux/ceph/osd_client.h>
21 #include <linux/ceph/striper.h>
22 
23 /*
24  * Ceph address space ops.
25  *
26  * There are a few funny things going on here.
27  *
28  * The page->private field is used to reference a struct
29  * ceph_snap_context for _every_ dirty page.  This indicates which
30  * snapshot the page was logically dirtied in, and thus which snap
31  * context needs to be associated with the osd write during writeback.
32  *
33  * Similarly, struct ceph_inode_info maintains a set of counters to
34  * count dirty pages on the inode.  In the absence of snapshots,
35  * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
36  *
37  * When a snapshot is taken (that is, when the client receives
38  * notification that a snapshot was taken), each inode with caps and
39  * with dirty pages (dirty pages implies there is a cap) gets a new
40  * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
41  * order, new snaps go to the tail).  The i_wrbuffer_ref_head count is
42  * moved to capsnap->dirty. (Unless a sync write is currently in
43  * progress.  In that case, the capsnap is said to be "pending", new
44  * writes cannot start, and the capsnap isn't "finalized" until the
45  * write completes (or fails) and a final size/mtime for the inode for
46  * that snap can be settled upon.)  i_wrbuffer_ref_head is reset to 0.
47  *
48  * On writeback, we must submit writes to the osd IN SNAP ORDER.  So,
49  * we look for the first capsnap in i_cap_snaps and write out pages in
50  * that snap context _only_.  Then we move on to the next capsnap,
51  * eventually reaching the "live" or "head" context (i.e., pages that
52  * are not yet snapped) and are writing the most recently dirtied
53  * pages.
54  *
55  * Invalidate and so forth must take care to ensure the dirty page
56  * accounting is preserved.
57  */
58 
59 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
60 #define CONGESTION_OFF_THRESH(congestion_kb)				\
61 	(CONGESTION_ON_THRESH(congestion_kb) -				\
62 	 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
63 
64 static inline struct ceph_snap_context *page_snap_context(struct page *page)
65 {
66 	if (PagePrivate(page))
67 		return (void *)page->private;
68 	return NULL;
69 }
70 
71 /*
72  * Dirty a page.  Optimistically adjust accounting, on the assumption
73  * that we won't race with invalidate.  If we do, readjust.
74  */
75 static int ceph_set_page_dirty(struct page *page)
76 {
77 	struct address_space *mapping = page->mapping;
78 	struct inode *inode;
79 	struct ceph_inode_info *ci;
80 	struct ceph_snap_context *snapc;
81 	int ret;
82 
83 	if (unlikely(!mapping))
84 		return !TestSetPageDirty(page);
85 
86 	if (PageDirty(page)) {
87 		dout("%p set_page_dirty %p idx %lu -- already dirty\n",
88 		     mapping->host, page, page->index);
89 		BUG_ON(!PagePrivate(page));
90 		return 0;
91 	}
92 
93 	inode = mapping->host;
94 	ci = ceph_inode(inode);
95 
96 	/* dirty the head */
97 	spin_lock(&ci->i_ceph_lock);
98 	BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
99 	if (__ceph_have_pending_cap_snap(ci)) {
100 		struct ceph_cap_snap *capsnap =
101 				list_last_entry(&ci->i_cap_snaps,
102 						struct ceph_cap_snap,
103 						ci_item);
104 		snapc = ceph_get_snap_context(capsnap->context);
105 		capsnap->dirty_pages++;
106 	} else {
107 		BUG_ON(!ci->i_head_snapc);
108 		snapc = ceph_get_snap_context(ci->i_head_snapc);
109 		++ci->i_wrbuffer_ref_head;
110 	}
111 	if (ci->i_wrbuffer_ref == 0)
112 		ihold(inode);
113 	++ci->i_wrbuffer_ref;
114 	dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
115 	     "snapc %p seq %lld (%d snaps)\n",
116 	     mapping->host, page, page->index,
117 	     ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
118 	     ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
119 	     snapc, snapc->seq, snapc->num_snaps);
120 	spin_unlock(&ci->i_ceph_lock);
121 
122 	/*
123 	 * Reference snap context in page->private.  Also set
124 	 * PagePrivate so that we get invalidatepage callback.
125 	 */
126 	BUG_ON(PagePrivate(page));
127 	page->private = (unsigned long)snapc;
128 	SetPagePrivate(page);
129 
130 	ret = __set_page_dirty_nobuffers(page);
131 	WARN_ON(!PageLocked(page));
132 	WARN_ON(!page->mapping);
133 
134 	return ret;
135 }
136 
137 /*
138  * If we are truncating the full page (i.e. offset == 0), adjust the
139  * dirty page counters appropriately.  Only called if there is private
140  * data on the page.
141  */
142 static void ceph_invalidatepage(struct page *page, unsigned int offset,
143 				unsigned int length)
144 {
145 	struct inode *inode;
146 	struct ceph_inode_info *ci;
147 	struct ceph_snap_context *snapc = page_snap_context(page);
148 
149 	inode = page->mapping->host;
150 	ci = ceph_inode(inode);
151 
152 	if (offset != 0 || length != PAGE_SIZE) {
153 		dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
154 		     inode, page, page->index, offset, length);
155 		return;
156 	}
157 
158 	ceph_invalidate_fscache_page(inode, page);
159 
160 	WARN_ON(!PageLocked(page));
161 	if (!PagePrivate(page))
162 		return;
163 
164 	dout("%p invalidatepage %p idx %lu full dirty page\n",
165 	     inode, page, page->index);
166 
167 	ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
168 	ceph_put_snap_context(snapc);
169 	page->private = 0;
170 	ClearPagePrivate(page);
171 }
172 
173 static int ceph_releasepage(struct page *page, gfp_t g)
174 {
175 	dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
176 	     page, page->index, PageDirty(page) ? "" : "not ");
177 
178 	/* Can we release the page from the cache? */
179 	if (!ceph_release_fscache_page(page, g))
180 		return 0;
181 
182 	return !PagePrivate(page);
183 }
184 
185 /*
186  * Read some contiguous pages.  If we cross a stripe boundary, shorten
187  * *plen.  Return number of bytes read, or error.
188  */
189 static int ceph_sync_readpages(struct ceph_fs_client *fsc,
190 			       struct ceph_vino vino,
191 			       struct ceph_file_layout *layout,
192 			       u64 off, u64 *plen,
193 			       u32 truncate_seq, u64 truncate_size,
194 			       struct page **pages, int num_pages,
195 			       int page_align)
196 {
197 	struct ceph_osd_client *osdc = &fsc->client->osdc;
198 	struct ceph_osd_request *req;
199 	int rc = 0;
200 
201 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
202 	     vino.snap, off, *plen);
203 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
204 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
205 				    NULL, truncate_seq, truncate_size,
206 				    false);
207 	if (IS_ERR(req))
208 		return PTR_ERR(req);
209 
210 	/* it may be a short read due to an object boundary */
211 	osd_req_op_extent_osd_data_pages(req, 0,
212 				pages, *plen, page_align, false, false);
213 
214 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
215 	     off, *plen, *plen, page_align);
216 
217 	rc = ceph_osdc_start_request(osdc, req, false);
218 	if (!rc)
219 		rc = ceph_osdc_wait_request(osdc, req);
220 
221 	ceph_update_read_latency(&fsc->mdsc->metric, req->r_start_latency,
222 				 req->r_end_latency, rc);
223 
224 	ceph_osdc_put_request(req);
225 	dout("readpages result %d\n", rc);
226 	return rc;
227 }
228 
229 /*
230  * read a single page, without unlocking it.
231  */
232 static int ceph_do_readpage(struct file *filp, struct page *page)
233 {
234 	struct inode *inode = file_inode(filp);
235 	struct ceph_inode_info *ci = ceph_inode(inode);
236 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
237 	int err = 0;
238 	u64 off = page_offset(page);
239 	u64 len = PAGE_SIZE;
240 
241 	if (off >= i_size_read(inode)) {
242 		zero_user_segment(page, 0, PAGE_SIZE);
243 		SetPageUptodate(page);
244 		return 0;
245 	}
246 
247 	if (ci->i_inline_version != CEPH_INLINE_NONE) {
248 		/*
249 		 * Uptodate inline data should have been added
250 		 * into page cache while getting Fcr caps.
251 		 */
252 		if (off == 0)
253 			return -EINVAL;
254 		zero_user_segment(page, 0, PAGE_SIZE);
255 		SetPageUptodate(page);
256 		return 0;
257 	}
258 
259 	err = ceph_readpage_from_fscache(inode, page);
260 	if (err == 0)
261 		return -EINPROGRESS;
262 
263 	dout("readpage inode %p file %p page %p index %lu\n",
264 	     inode, filp, page, page->index);
265 	err = ceph_sync_readpages(fsc, ceph_vino(inode),
266 				  &ci->i_layout, off, &len,
267 				  ci->i_truncate_seq, ci->i_truncate_size,
268 				  &page, 1, 0);
269 	if (err == -ENOENT)
270 		err = 0;
271 	if (err < 0) {
272 		SetPageError(page);
273 		ceph_fscache_readpage_cancel(inode, page);
274 		if (err == -EBLACKLISTED)
275 			fsc->blacklisted = true;
276 		goto out;
277 	}
278 	if (err < PAGE_SIZE)
279 		/* zero fill remainder of page */
280 		zero_user_segment(page, err, PAGE_SIZE);
281 	else
282 		flush_dcache_page(page);
283 
284 	SetPageUptodate(page);
285 	ceph_readpage_to_fscache(inode, page);
286 
287 out:
288 	return err < 0 ? err : 0;
289 }
290 
291 static int ceph_readpage(struct file *filp, struct page *page)
292 {
293 	int r = ceph_do_readpage(filp, page);
294 	if (r != -EINPROGRESS)
295 		unlock_page(page);
296 	else
297 		r = 0;
298 	return r;
299 }
300 
301 /*
302  * Finish an async read(ahead) op.
303  */
304 static void finish_read(struct ceph_osd_request *req)
305 {
306 	struct inode *inode = req->r_inode;
307 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
308 	struct ceph_osd_data *osd_data;
309 	int rc = req->r_result <= 0 ? req->r_result : 0;
310 	int bytes = req->r_result >= 0 ? req->r_result : 0;
311 	int num_pages;
312 	int i;
313 
314 	dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
315 	if (rc == -EBLACKLISTED)
316 		ceph_inode_to_client(inode)->blacklisted = true;
317 
318 	/* unlock all pages, zeroing any data we didn't read */
319 	osd_data = osd_req_op_extent_osd_data(req, 0);
320 	BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
321 	num_pages = calc_pages_for((u64)osd_data->alignment,
322 					(u64)osd_data->length);
323 	for (i = 0; i < num_pages; i++) {
324 		struct page *page = osd_data->pages[i];
325 
326 		if (rc < 0 && rc != -ENOENT) {
327 			ceph_fscache_readpage_cancel(inode, page);
328 			goto unlock;
329 		}
330 		if (bytes < (int)PAGE_SIZE) {
331 			/* zero (remainder of) page */
332 			int s = bytes < 0 ? 0 : bytes;
333 			zero_user_segment(page, s, PAGE_SIZE);
334 		}
335  		dout("finish_read %p uptodate %p idx %lu\n", inode, page,
336 		     page->index);
337 		flush_dcache_page(page);
338 		SetPageUptodate(page);
339 		ceph_readpage_to_fscache(inode, page);
340 unlock:
341 		unlock_page(page);
342 		put_page(page);
343 		bytes -= PAGE_SIZE;
344 	}
345 
346 	ceph_update_read_latency(&fsc->mdsc->metric, req->r_start_latency,
347 				 req->r_end_latency, rc);
348 
349 	kfree(osd_data->pages);
350 }
351 
352 /*
353  * start an async read(ahead) operation.  return nr_pages we submitted
354  * a read for on success, or negative error code.
355  */
356 static int start_read(struct inode *inode, struct ceph_rw_context *rw_ctx,
357 		      struct list_head *page_list, int max)
358 {
359 	struct ceph_osd_client *osdc =
360 		&ceph_inode_to_client(inode)->client->osdc;
361 	struct ceph_inode_info *ci = ceph_inode(inode);
362 	struct page *page = lru_to_page(page_list);
363 	struct ceph_vino vino;
364 	struct ceph_osd_request *req;
365 	u64 off;
366 	u64 len;
367 	int i;
368 	struct page **pages;
369 	pgoff_t next_index;
370 	int nr_pages = 0;
371 	int got = 0;
372 	int ret = 0;
373 
374 	if (!rw_ctx) {
375 		/* caller of readpages does not hold buffer and read caps
376 		 * (fadvise, madvise and readahead cases) */
377 		int want = CEPH_CAP_FILE_CACHE;
378 		ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want,
379 					true, &got);
380 		if (ret < 0) {
381 			dout("start_read %p, error getting cap\n", inode);
382 		} else if (!(got & want)) {
383 			dout("start_read %p, no cache cap\n", inode);
384 			ret = 0;
385 		}
386 		if (ret <= 0) {
387 			if (got)
388 				ceph_put_cap_refs(ci, got);
389 			while (!list_empty(page_list)) {
390 				page = lru_to_page(page_list);
391 				list_del(&page->lru);
392 				put_page(page);
393 			}
394 			return ret;
395 		}
396 	}
397 
398 	off = (u64) page_offset(page);
399 
400 	/* count pages */
401 	next_index = page->index;
402 	list_for_each_entry_reverse(page, page_list, lru) {
403 		if (page->index != next_index)
404 			break;
405 		nr_pages++;
406 		next_index++;
407 		if (max && nr_pages == max)
408 			break;
409 	}
410 	len = nr_pages << PAGE_SHIFT;
411 	dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
412 	     off, len);
413 	vino = ceph_vino(inode);
414 	req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
415 				    0, 1, CEPH_OSD_OP_READ,
416 				    CEPH_OSD_FLAG_READ, NULL,
417 				    ci->i_truncate_seq, ci->i_truncate_size,
418 				    false);
419 	if (IS_ERR(req)) {
420 		ret = PTR_ERR(req);
421 		goto out;
422 	}
423 
424 	/* build page vector */
425 	nr_pages = calc_pages_for(0, len);
426 	pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
427 	if (!pages) {
428 		ret = -ENOMEM;
429 		goto out_put;
430 	}
431 	for (i = 0; i < nr_pages; ++i) {
432 		page = list_entry(page_list->prev, struct page, lru);
433 		BUG_ON(PageLocked(page));
434 		list_del(&page->lru);
435 
436  		dout("start_read %p adding %p idx %lu\n", inode, page,
437 		     page->index);
438 		if (add_to_page_cache_lru(page, &inode->i_data, page->index,
439 					  GFP_KERNEL)) {
440 			ceph_fscache_uncache_page(inode, page);
441 			put_page(page);
442 			dout("start_read %p add_to_page_cache failed %p\n",
443 			     inode, page);
444 			nr_pages = i;
445 			if (nr_pages > 0) {
446 				len = nr_pages << PAGE_SHIFT;
447 				osd_req_op_extent_update(req, 0, len);
448 				break;
449 			}
450 			goto out_pages;
451 		}
452 		pages[i] = page;
453 	}
454 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
455 	req->r_callback = finish_read;
456 	req->r_inode = inode;
457 
458 	dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
459 	ret = ceph_osdc_start_request(osdc, req, false);
460 	if (ret < 0)
461 		goto out_pages;
462 	ceph_osdc_put_request(req);
463 
464 	/* After adding locked pages to page cache, the inode holds cache cap.
465 	 * So we can drop our cap refs. */
466 	if (got)
467 		ceph_put_cap_refs(ci, got);
468 
469 	return nr_pages;
470 
471 out_pages:
472 	for (i = 0; i < nr_pages; ++i) {
473 		ceph_fscache_readpage_cancel(inode, pages[i]);
474 		unlock_page(pages[i]);
475 	}
476 	ceph_put_page_vector(pages, nr_pages, false);
477 out_put:
478 	ceph_osdc_put_request(req);
479 out:
480 	if (got)
481 		ceph_put_cap_refs(ci, got);
482 	return ret;
483 }
484 
485 
486 /*
487  * Read multiple pages.  Leave pages we don't read + unlock in page_list;
488  * the caller (VM) cleans them up.
489  */
490 static int ceph_readpages(struct file *file, struct address_space *mapping,
491 			  struct list_head *page_list, unsigned nr_pages)
492 {
493 	struct inode *inode = file_inode(file);
494 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
495 	struct ceph_file_info *fi = file->private_data;
496 	struct ceph_rw_context *rw_ctx;
497 	int rc = 0;
498 	int max = 0;
499 
500 	if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
501 		return -EINVAL;
502 
503 	rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
504 					 &nr_pages);
505 
506 	if (rc == 0)
507 		goto out;
508 
509 	rw_ctx = ceph_find_rw_context(fi);
510 	max = fsc->mount_options->rsize >> PAGE_SHIFT;
511 	dout("readpages %p file %p ctx %p nr_pages %d max %d\n",
512 	     inode, file, rw_ctx, nr_pages, max);
513 	while (!list_empty(page_list)) {
514 		rc = start_read(inode, rw_ctx, page_list, max);
515 		if (rc < 0)
516 			goto out;
517 	}
518 out:
519 	ceph_fscache_readpages_cancel(inode, page_list);
520 
521 	dout("readpages %p file %p ret %d\n", inode, file, rc);
522 	return rc;
523 }
524 
525 struct ceph_writeback_ctl
526 {
527 	loff_t i_size;
528 	u64 truncate_size;
529 	u32 truncate_seq;
530 	bool size_stable;
531 	bool head_snapc;
532 };
533 
534 /*
535  * Get ref for the oldest snapc for an inode with dirty data... that is, the
536  * only snap context we are allowed to write back.
537  */
538 static struct ceph_snap_context *
539 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
540 		   struct ceph_snap_context *page_snapc)
541 {
542 	struct ceph_inode_info *ci = ceph_inode(inode);
543 	struct ceph_snap_context *snapc = NULL;
544 	struct ceph_cap_snap *capsnap = NULL;
545 
546 	spin_lock(&ci->i_ceph_lock);
547 	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
548 		dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
549 		     capsnap->context, capsnap->dirty_pages);
550 		if (!capsnap->dirty_pages)
551 			continue;
552 
553 		/* get i_size, truncate_{seq,size} for page_snapc? */
554 		if (snapc && capsnap->context != page_snapc)
555 			continue;
556 
557 		if (ctl) {
558 			if (capsnap->writing) {
559 				ctl->i_size = i_size_read(inode);
560 				ctl->size_stable = false;
561 			} else {
562 				ctl->i_size = capsnap->size;
563 				ctl->size_stable = true;
564 			}
565 			ctl->truncate_size = capsnap->truncate_size;
566 			ctl->truncate_seq = capsnap->truncate_seq;
567 			ctl->head_snapc = false;
568 		}
569 
570 		if (snapc)
571 			break;
572 
573 		snapc = ceph_get_snap_context(capsnap->context);
574 		if (!page_snapc ||
575 		    page_snapc == snapc ||
576 		    page_snapc->seq > snapc->seq)
577 			break;
578 	}
579 	if (!snapc && ci->i_wrbuffer_ref_head) {
580 		snapc = ceph_get_snap_context(ci->i_head_snapc);
581 		dout(" head snapc %p has %d dirty pages\n",
582 		     snapc, ci->i_wrbuffer_ref_head);
583 		if (ctl) {
584 			ctl->i_size = i_size_read(inode);
585 			ctl->truncate_size = ci->i_truncate_size;
586 			ctl->truncate_seq = ci->i_truncate_seq;
587 			ctl->size_stable = false;
588 			ctl->head_snapc = true;
589 		}
590 	}
591 	spin_unlock(&ci->i_ceph_lock);
592 	return snapc;
593 }
594 
595 static u64 get_writepages_data_length(struct inode *inode,
596 				      struct page *page, u64 start)
597 {
598 	struct ceph_inode_info *ci = ceph_inode(inode);
599 	struct ceph_snap_context *snapc = page_snap_context(page);
600 	struct ceph_cap_snap *capsnap = NULL;
601 	u64 end = i_size_read(inode);
602 
603 	if (snapc != ci->i_head_snapc) {
604 		bool found = false;
605 		spin_lock(&ci->i_ceph_lock);
606 		list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
607 			if (capsnap->context == snapc) {
608 				if (!capsnap->writing)
609 					end = capsnap->size;
610 				found = true;
611 				break;
612 			}
613 		}
614 		spin_unlock(&ci->i_ceph_lock);
615 		WARN_ON(!found);
616 	}
617 	if (end > page_offset(page) + PAGE_SIZE)
618 		end = page_offset(page) + PAGE_SIZE;
619 	return end > start ? end - start : 0;
620 }
621 
622 /*
623  * do a synchronous write on N pages
624  */
625 static int ceph_sync_writepages(struct ceph_fs_client *fsc,
626 				struct ceph_vino vino,
627 				struct ceph_file_layout *layout,
628 				struct ceph_snap_context *snapc,
629 				u64 off, u64 len,
630 				u32 truncate_seq, u64 truncate_size,
631 				struct timespec64 *mtime,
632 				struct page **pages, int num_pages)
633 {
634 	struct ceph_osd_client *osdc = &fsc->client->osdc;
635 	struct ceph_osd_request *req;
636 	int rc = 0;
637 	int page_align = off & ~PAGE_MASK;
638 
639 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
640 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
641 				    snapc, truncate_seq, truncate_size,
642 				    true);
643 	if (IS_ERR(req))
644 		return PTR_ERR(req);
645 
646 	/* it may be a short write due to an object boundary */
647 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
648 				false, false);
649 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
650 
651 	req->r_mtime = *mtime;
652 	rc = ceph_osdc_start_request(osdc, req, true);
653 	if (!rc)
654 		rc = ceph_osdc_wait_request(osdc, req);
655 
656 	ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
657 				  req->r_end_latency, rc);
658 
659 	ceph_osdc_put_request(req);
660 	if (rc == 0)
661 		rc = len;
662 	dout("writepages result %d\n", rc);
663 	return rc;
664 }
665 
666 /*
667  * Write a single page, but leave the page locked.
668  *
669  * If we get a write error, mark the mapping for error, but still adjust the
670  * dirty page accounting (i.e., page is no longer dirty).
671  */
672 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
673 {
674 	struct inode *inode;
675 	struct ceph_inode_info *ci;
676 	struct ceph_fs_client *fsc;
677 	struct ceph_snap_context *snapc, *oldest;
678 	loff_t page_off = page_offset(page);
679 	int err, len = PAGE_SIZE;
680 	struct ceph_writeback_ctl ceph_wbc;
681 
682 	dout("writepage %p idx %lu\n", page, page->index);
683 
684 	inode = page->mapping->host;
685 	ci = ceph_inode(inode);
686 	fsc = ceph_inode_to_client(inode);
687 
688 	/* verify this is a writeable snap context */
689 	snapc = page_snap_context(page);
690 	if (!snapc) {
691 		dout("writepage %p page %p not dirty?\n", inode, page);
692 		return 0;
693 	}
694 	oldest = get_oldest_context(inode, &ceph_wbc, snapc);
695 	if (snapc->seq > oldest->seq) {
696 		dout("writepage %p page %p snapc %p not writeable - noop\n",
697 		     inode, page, snapc);
698 		/* we should only noop if called by kswapd */
699 		WARN_ON(!(current->flags & PF_MEMALLOC));
700 		ceph_put_snap_context(oldest);
701 		redirty_page_for_writepage(wbc, page);
702 		return 0;
703 	}
704 	ceph_put_snap_context(oldest);
705 
706 	/* is this a partial page at end of file? */
707 	if (page_off >= ceph_wbc.i_size) {
708 		dout("%p page eof %llu\n", page, ceph_wbc.i_size);
709 		page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
710 		return 0;
711 	}
712 
713 	if (ceph_wbc.i_size < page_off + len)
714 		len = ceph_wbc.i_size - page_off;
715 
716 	dout("writepage %p page %p index %lu on %llu~%u snapc %p seq %lld\n",
717 	     inode, page, page->index, page_off, len, snapc, snapc->seq);
718 
719 	if (atomic_long_inc_return(&fsc->writeback_count) >
720 	    CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
721 		set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
722 
723 	set_page_writeback(page);
724 	err = ceph_sync_writepages(fsc, ceph_vino(inode),
725 				   &ci->i_layout, snapc, page_off, len,
726 				   ceph_wbc.truncate_seq,
727 				   ceph_wbc.truncate_size,
728 				   &inode->i_mtime, &page, 1);
729 	if (err < 0) {
730 		struct writeback_control tmp_wbc;
731 		if (!wbc)
732 			wbc = &tmp_wbc;
733 		if (err == -ERESTARTSYS) {
734 			/* killed by SIGKILL */
735 			dout("writepage interrupted page %p\n", page);
736 			redirty_page_for_writepage(wbc, page);
737 			end_page_writeback(page);
738 			return err;
739 		}
740 		if (err == -EBLACKLISTED)
741 			fsc->blacklisted = true;
742 		dout("writepage setting page/mapping error %d %p\n",
743 		     err, page);
744 		mapping_set_error(&inode->i_data, err);
745 		wbc->pages_skipped++;
746 	} else {
747 		dout("writepage cleaned page %p\n", page);
748 		err = 0;  /* vfs expects us to return 0 */
749 	}
750 	page->private = 0;
751 	ClearPagePrivate(page);
752 	end_page_writeback(page);
753 	ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
754 	ceph_put_snap_context(snapc);  /* page's reference */
755 
756 	if (atomic_long_dec_return(&fsc->writeback_count) <
757 	    CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
758 		clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
759 
760 	return err;
761 }
762 
763 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
764 {
765 	int err;
766 	struct inode *inode = page->mapping->host;
767 	BUG_ON(!inode);
768 	ihold(inode);
769 	err = writepage_nounlock(page, wbc);
770 	if (err == -ERESTARTSYS) {
771 		/* direct memory reclaimer was killed by SIGKILL. return 0
772 		 * to prevent caller from setting mapping/page error */
773 		err = 0;
774 	}
775 	unlock_page(page);
776 	iput(inode);
777 	return err;
778 }
779 
780 /*
781  * async writeback completion handler.
782  *
783  * If we get an error, set the mapping error bit, but not the individual
784  * page error bits.
785  */
786 static void writepages_finish(struct ceph_osd_request *req)
787 {
788 	struct inode *inode = req->r_inode;
789 	struct ceph_inode_info *ci = ceph_inode(inode);
790 	struct ceph_osd_data *osd_data;
791 	struct page *page;
792 	int num_pages, total_pages = 0;
793 	int i, j;
794 	int rc = req->r_result;
795 	struct ceph_snap_context *snapc = req->r_snapc;
796 	struct address_space *mapping = inode->i_mapping;
797 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
798 	bool remove_page;
799 
800 	dout("writepages_finish %p rc %d\n", inode, rc);
801 	if (rc < 0) {
802 		mapping_set_error(mapping, rc);
803 		ceph_set_error_write(ci);
804 		if (rc == -EBLACKLISTED)
805 			fsc->blacklisted = true;
806 	} else {
807 		ceph_clear_error_write(ci);
808 	}
809 
810 	ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
811 				  req->r_end_latency, rc);
812 
813 	/*
814 	 * We lost the cache cap, need to truncate the page before
815 	 * it is unlocked, otherwise we'd truncate it later in the
816 	 * page truncation thread, possibly losing some data that
817 	 * raced its way in
818 	 */
819 	remove_page = !(ceph_caps_issued(ci) &
820 			(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
821 
822 	/* clean all pages */
823 	for (i = 0; i < req->r_num_ops; i++) {
824 		if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
825 			break;
826 
827 		osd_data = osd_req_op_extent_osd_data(req, i);
828 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
829 		num_pages = calc_pages_for((u64)osd_data->alignment,
830 					   (u64)osd_data->length);
831 		total_pages += num_pages;
832 		for (j = 0; j < num_pages; j++) {
833 			page = osd_data->pages[j];
834 			BUG_ON(!page);
835 			WARN_ON(!PageUptodate(page));
836 
837 			if (atomic_long_dec_return(&fsc->writeback_count) <
838 			     CONGESTION_OFF_THRESH(
839 					fsc->mount_options->congestion_kb))
840 				clear_bdi_congested(inode_to_bdi(inode),
841 						    BLK_RW_ASYNC);
842 
843 			ceph_put_snap_context(page_snap_context(page));
844 			page->private = 0;
845 			ClearPagePrivate(page);
846 			dout("unlocking %p\n", page);
847 			end_page_writeback(page);
848 
849 			if (remove_page)
850 				generic_error_remove_page(inode->i_mapping,
851 							  page);
852 
853 			unlock_page(page);
854 		}
855 		dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
856 		     inode, osd_data->length, rc >= 0 ? num_pages : 0);
857 
858 		release_pages(osd_data->pages, num_pages);
859 	}
860 
861 	ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
862 
863 	osd_data = osd_req_op_extent_osd_data(req, 0);
864 	if (osd_data->pages_from_pool)
865 		mempool_free(osd_data->pages,
866 			     ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
867 	else
868 		kfree(osd_data->pages);
869 	ceph_osdc_put_request(req);
870 }
871 
872 /*
873  * initiate async writeback
874  */
875 static int ceph_writepages_start(struct address_space *mapping,
876 				 struct writeback_control *wbc)
877 {
878 	struct inode *inode = mapping->host;
879 	struct ceph_inode_info *ci = ceph_inode(inode);
880 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
881 	struct ceph_vino vino = ceph_vino(inode);
882 	pgoff_t index, start_index, end = -1;
883 	struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
884 	struct pagevec pvec;
885 	int rc = 0;
886 	unsigned int wsize = i_blocksize(inode);
887 	struct ceph_osd_request *req = NULL;
888 	struct ceph_writeback_ctl ceph_wbc;
889 	bool should_loop, range_whole = false;
890 	bool done = false;
891 
892 	dout("writepages_start %p (mode=%s)\n", inode,
893 	     wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
894 	     (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
895 
896 	if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
897 		if (ci->i_wrbuffer_ref > 0) {
898 			pr_warn_ratelimited(
899 				"writepage_start %p %lld forced umount\n",
900 				inode, ceph_ino(inode));
901 		}
902 		mapping_set_error(mapping, -EIO);
903 		return -EIO; /* we're in a forced umount, don't write! */
904 	}
905 	if (fsc->mount_options->wsize < wsize)
906 		wsize = fsc->mount_options->wsize;
907 
908 	pagevec_init(&pvec);
909 
910 	start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
911 	index = start_index;
912 
913 retry:
914 	/* find oldest snap context with dirty data */
915 	snapc = get_oldest_context(inode, &ceph_wbc, NULL);
916 	if (!snapc) {
917 		/* hmm, why does writepages get called when there
918 		   is no dirty data? */
919 		dout(" no snap context with dirty data?\n");
920 		goto out;
921 	}
922 	dout(" oldest snapc is %p seq %lld (%d snaps)\n",
923 	     snapc, snapc->seq, snapc->num_snaps);
924 
925 	should_loop = false;
926 	if (ceph_wbc.head_snapc && snapc != last_snapc) {
927 		/* where to start/end? */
928 		if (wbc->range_cyclic) {
929 			index = start_index;
930 			end = -1;
931 			if (index > 0)
932 				should_loop = true;
933 			dout(" cyclic, start at %lu\n", index);
934 		} else {
935 			index = wbc->range_start >> PAGE_SHIFT;
936 			end = wbc->range_end >> PAGE_SHIFT;
937 			if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
938 				range_whole = true;
939 			dout(" not cyclic, %lu to %lu\n", index, end);
940 		}
941 	} else if (!ceph_wbc.head_snapc) {
942 		/* Do not respect wbc->range_{start,end}. Dirty pages
943 		 * in that range can be associated with newer snapc.
944 		 * They are not writeable until we write all dirty pages
945 		 * associated with 'snapc' get written */
946 		if (index > 0)
947 			should_loop = true;
948 		dout(" non-head snapc, range whole\n");
949 	}
950 
951 	ceph_put_snap_context(last_snapc);
952 	last_snapc = snapc;
953 
954 	while (!done && index <= end) {
955 		int num_ops = 0, op_idx;
956 		unsigned i, pvec_pages, max_pages, locked_pages = 0;
957 		struct page **pages = NULL, **data_pages;
958 		mempool_t *pool = NULL;	/* Becomes non-null if mempool used */
959 		struct page *page;
960 		pgoff_t strip_unit_end = 0;
961 		u64 offset = 0, len = 0;
962 
963 		max_pages = wsize >> PAGE_SHIFT;
964 
965 get_more_pages:
966 		pvec_pages = pagevec_lookup_range_nr_tag(&pvec, mapping, &index,
967 						end, PAGECACHE_TAG_DIRTY,
968 						max_pages - locked_pages);
969 		dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
970 		if (!pvec_pages && !locked_pages)
971 			break;
972 		for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
973 			page = pvec.pages[i];
974 			dout("? %p idx %lu\n", page, page->index);
975 			if (locked_pages == 0)
976 				lock_page(page);  /* first page */
977 			else if (!trylock_page(page))
978 				break;
979 
980 			/* only dirty pages, or our accounting breaks */
981 			if (unlikely(!PageDirty(page)) ||
982 			    unlikely(page->mapping != mapping)) {
983 				dout("!dirty or !mapping %p\n", page);
984 				unlock_page(page);
985 				continue;
986 			}
987 			/* only if matching snap context */
988 			pgsnapc = page_snap_context(page);
989 			if (pgsnapc != snapc) {
990 				dout("page snapc %p %lld != oldest %p %lld\n",
991 				     pgsnapc, pgsnapc->seq, snapc, snapc->seq);
992 				if (!should_loop &&
993 				    !ceph_wbc.head_snapc &&
994 				    wbc->sync_mode != WB_SYNC_NONE)
995 					should_loop = true;
996 				unlock_page(page);
997 				continue;
998 			}
999 			if (page_offset(page) >= ceph_wbc.i_size) {
1000 				dout("%p page eof %llu\n",
1001 				     page, ceph_wbc.i_size);
1002 				if ((ceph_wbc.size_stable ||
1003 				    page_offset(page) >= i_size_read(inode)) &&
1004 				    clear_page_dirty_for_io(page))
1005 					mapping->a_ops->invalidatepage(page,
1006 								0, PAGE_SIZE);
1007 				unlock_page(page);
1008 				continue;
1009 			}
1010 			if (strip_unit_end && (page->index > strip_unit_end)) {
1011 				dout("end of strip unit %p\n", page);
1012 				unlock_page(page);
1013 				break;
1014 			}
1015 			if (PageWriteback(page)) {
1016 				if (wbc->sync_mode == WB_SYNC_NONE) {
1017 					dout("%p under writeback\n", page);
1018 					unlock_page(page);
1019 					continue;
1020 				}
1021 				dout("waiting on writeback %p\n", page);
1022 				wait_on_page_writeback(page);
1023 			}
1024 
1025 			if (!clear_page_dirty_for_io(page)) {
1026 				dout("%p !clear_page_dirty_for_io\n", page);
1027 				unlock_page(page);
1028 				continue;
1029 			}
1030 
1031 			/*
1032 			 * We have something to write.  If this is
1033 			 * the first locked page this time through,
1034 			 * calculate max possinle write size and
1035 			 * allocate a page array
1036 			 */
1037 			if (locked_pages == 0) {
1038 				u64 objnum;
1039 				u64 objoff;
1040 				u32 xlen;
1041 
1042 				/* prepare async write request */
1043 				offset = (u64)page_offset(page);
1044 				ceph_calc_file_object_mapping(&ci->i_layout,
1045 							      offset, wsize,
1046 							      &objnum, &objoff,
1047 							      &xlen);
1048 				len = xlen;
1049 
1050 				num_ops = 1;
1051 				strip_unit_end = page->index +
1052 					((len - 1) >> PAGE_SHIFT);
1053 
1054 				BUG_ON(pages);
1055 				max_pages = calc_pages_for(0, (u64)len);
1056 				pages = kmalloc_array(max_pages,
1057 						      sizeof(*pages),
1058 						      GFP_NOFS);
1059 				if (!pages) {
1060 					pool = fsc->wb_pagevec_pool;
1061 					pages = mempool_alloc(pool, GFP_NOFS);
1062 					BUG_ON(!pages);
1063 				}
1064 
1065 				len = 0;
1066 			} else if (page->index !=
1067 				   (offset + len) >> PAGE_SHIFT) {
1068 				if (num_ops >= (pool ?  CEPH_OSD_SLAB_OPS :
1069 							CEPH_OSD_MAX_OPS)) {
1070 					redirty_page_for_writepage(wbc, page);
1071 					unlock_page(page);
1072 					break;
1073 				}
1074 
1075 				num_ops++;
1076 				offset = (u64)page_offset(page);
1077 				len = 0;
1078 			}
1079 
1080 			/* note position of first page in pvec */
1081 			dout("%p will write page %p idx %lu\n",
1082 			     inode, page, page->index);
1083 
1084 			if (atomic_long_inc_return(&fsc->writeback_count) >
1085 			    CONGESTION_ON_THRESH(
1086 				    fsc->mount_options->congestion_kb)) {
1087 				set_bdi_congested(inode_to_bdi(inode),
1088 						  BLK_RW_ASYNC);
1089 			}
1090 
1091 
1092 			pages[locked_pages++] = page;
1093 			pvec.pages[i] = NULL;
1094 
1095 			len += PAGE_SIZE;
1096 		}
1097 
1098 		/* did we get anything? */
1099 		if (!locked_pages)
1100 			goto release_pvec_pages;
1101 		if (i) {
1102 			unsigned j, n = 0;
1103 			/* shift unused page to beginning of pvec */
1104 			for (j = 0; j < pvec_pages; j++) {
1105 				if (!pvec.pages[j])
1106 					continue;
1107 				if (n < j)
1108 					pvec.pages[n] = pvec.pages[j];
1109 				n++;
1110 			}
1111 			pvec.nr = n;
1112 
1113 			if (pvec_pages && i == pvec_pages &&
1114 			    locked_pages < max_pages) {
1115 				dout("reached end pvec, trying for more\n");
1116 				pagevec_release(&pvec);
1117 				goto get_more_pages;
1118 			}
1119 		}
1120 
1121 new_request:
1122 		offset = page_offset(pages[0]);
1123 		len = wsize;
1124 
1125 		req = ceph_osdc_new_request(&fsc->client->osdc,
1126 					&ci->i_layout, vino,
1127 					offset, &len, 0, num_ops,
1128 					CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1129 					snapc, ceph_wbc.truncate_seq,
1130 					ceph_wbc.truncate_size, false);
1131 		if (IS_ERR(req)) {
1132 			req = ceph_osdc_new_request(&fsc->client->osdc,
1133 						&ci->i_layout, vino,
1134 						offset, &len, 0,
1135 						min(num_ops,
1136 						    CEPH_OSD_SLAB_OPS),
1137 						CEPH_OSD_OP_WRITE,
1138 						CEPH_OSD_FLAG_WRITE,
1139 						snapc, ceph_wbc.truncate_seq,
1140 						ceph_wbc.truncate_size, true);
1141 			BUG_ON(IS_ERR(req));
1142 		}
1143 		BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1144 			     PAGE_SIZE - offset);
1145 
1146 		req->r_callback = writepages_finish;
1147 		req->r_inode = inode;
1148 
1149 		/* Format the osd request message and submit the write */
1150 		len = 0;
1151 		data_pages = pages;
1152 		op_idx = 0;
1153 		for (i = 0; i < locked_pages; i++) {
1154 			u64 cur_offset = page_offset(pages[i]);
1155 			if (offset + len != cur_offset) {
1156 				if (op_idx + 1 == req->r_num_ops)
1157 					break;
1158 				osd_req_op_extent_dup_last(req, op_idx,
1159 							   cur_offset - offset);
1160 				dout("writepages got pages at %llu~%llu\n",
1161 				     offset, len);
1162 				osd_req_op_extent_osd_data_pages(req, op_idx,
1163 							data_pages, len, 0,
1164 							!!pool, false);
1165 				osd_req_op_extent_update(req, op_idx, len);
1166 
1167 				len = 0;
1168 				offset = cur_offset;
1169 				data_pages = pages + i;
1170 				op_idx++;
1171 			}
1172 
1173 			set_page_writeback(pages[i]);
1174 			len += PAGE_SIZE;
1175 		}
1176 
1177 		if (ceph_wbc.size_stable) {
1178 			len = min(len, ceph_wbc.i_size - offset);
1179 		} else if (i == locked_pages) {
1180 			/* writepages_finish() clears writeback pages
1181 			 * according to the data length, so make sure
1182 			 * data length covers all locked pages */
1183 			u64 min_len = len + 1 - PAGE_SIZE;
1184 			len = get_writepages_data_length(inode, pages[i - 1],
1185 							 offset);
1186 			len = max(len, min_len);
1187 		}
1188 		dout("writepages got pages at %llu~%llu\n", offset, len);
1189 
1190 		osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1191 						 0, !!pool, false);
1192 		osd_req_op_extent_update(req, op_idx, len);
1193 
1194 		BUG_ON(op_idx + 1 != req->r_num_ops);
1195 
1196 		pool = NULL;
1197 		if (i < locked_pages) {
1198 			BUG_ON(num_ops <= req->r_num_ops);
1199 			num_ops -= req->r_num_ops;
1200 			locked_pages -= i;
1201 
1202 			/* allocate new pages array for next request */
1203 			data_pages = pages;
1204 			pages = kmalloc_array(locked_pages, sizeof(*pages),
1205 					      GFP_NOFS);
1206 			if (!pages) {
1207 				pool = fsc->wb_pagevec_pool;
1208 				pages = mempool_alloc(pool, GFP_NOFS);
1209 				BUG_ON(!pages);
1210 			}
1211 			memcpy(pages, data_pages + i,
1212 			       locked_pages * sizeof(*pages));
1213 			memset(data_pages + i, 0,
1214 			       locked_pages * sizeof(*pages));
1215 		} else {
1216 			BUG_ON(num_ops != req->r_num_ops);
1217 			index = pages[i - 1]->index + 1;
1218 			/* request message now owns the pages array */
1219 			pages = NULL;
1220 		}
1221 
1222 		req->r_mtime = inode->i_mtime;
1223 		rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1224 		BUG_ON(rc);
1225 		req = NULL;
1226 
1227 		wbc->nr_to_write -= i;
1228 		if (pages)
1229 			goto new_request;
1230 
1231 		/*
1232 		 * We stop writing back only if we are not doing
1233 		 * integrity sync. In case of integrity sync we have to
1234 		 * keep going until we have written all the pages
1235 		 * we tagged for writeback prior to entering this loop.
1236 		 */
1237 		if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1238 			done = true;
1239 
1240 release_pvec_pages:
1241 		dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1242 		     pvec.nr ? pvec.pages[0] : NULL);
1243 		pagevec_release(&pvec);
1244 	}
1245 
1246 	if (should_loop && !done) {
1247 		/* more to do; loop back to beginning of file */
1248 		dout("writepages looping back to beginning of file\n");
1249 		end = start_index - 1; /* OK even when start_index == 0 */
1250 
1251 		/* to write dirty pages associated with next snapc,
1252 		 * we need to wait until current writes complete */
1253 		if (wbc->sync_mode != WB_SYNC_NONE &&
1254 		    start_index == 0 && /* all dirty pages were checked */
1255 		    !ceph_wbc.head_snapc) {
1256 			struct page *page;
1257 			unsigned i, nr;
1258 			index = 0;
1259 			while ((index <= end) &&
1260 			       (nr = pagevec_lookup_tag(&pvec, mapping, &index,
1261 						PAGECACHE_TAG_WRITEBACK))) {
1262 				for (i = 0; i < nr; i++) {
1263 					page = pvec.pages[i];
1264 					if (page_snap_context(page) != snapc)
1265 						continue;
1266 					wait_on_page_writeback(page);
1267 				}
1268 				pagevec_release(&pvec);
1269 				cond_resched();
1270 			}
1271 		}
1272 
1273 		start_index = 0;
1274 		index = 0;
1275 		goto retry;
1276 	}
1277 
1278 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1279 		mapping->writeback_index = index;
1280 
1281 out:
1282 	ceph_osdc_put_request(req);
1283 	ceph_put_snap_context(last_snapc);
1284 	dout("writepages dend - startone, rc = %d\n", rc);
1285 	return rc;
1286 }
1287 
1288 
1289 
1290 /*
1291  * See if a given @snapc is either writeable, or already written.
1292  */
1293 static int context_is_writeable_or_written(struct inode *inode,
1294 					   struct ceph_snap_context *snapc)
1295 {
1296 	struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1297 	int ret = !oldest || snapc->seq <= oldest->seq;
1298 
1299 	ceph_put_snap_context(oldest);
1300 	return ret;
1301 }
1302 
1303 /*
1304  * We are only allowed to write into/dirty the page if the page is
1305  * clean, or already dirty within the same snap context.
1306  *
1307  * called with page locked.
1308  * return success with page locked,
1309  * or any failure (incl -EAGAIN) with page unlocked.
1310  */
1311 static int ceph_update_writeable_page(struct file *file,
1312 			    loff_t pos, unsigned len,
1313 			    struct page *page)
1314 {
1315 	struct inode *inode = file_inode(file);
1316 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1317 	struct ceph_inode_info *ci = ceph_inode(inode);
1318 	loff_t page_off = pos & PAGE_MASK;
1319 	int pos_in_page = pos & ~PAGE_MASK;
1320 	int end_in_page = pos_in_page + len;
1321 	loff_t i_size;
1322 	int r;
1323 	struct ceph_snap_context *snapc, *oldest;
1324 
1325 	if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1326 		dout(" page %p forced umount\n", page);
1327 		unlock_page(page);
1328 		return -EIO;
1329 	}
1330 
1331 retry_locked:
1332 	/* writepages currently holds page lock, but if we change that later, */
1333 	wait_on_page_writeback(page);
1334 
1335 	snapc = page_snap_context(page);
1336 	if (snapc && snapc != ci->i_head_snapc) {
1337 		/*
1338 		 * this page is already dirty in another (older) snap
1339 		 * context!  is it writeable now?
1340 		 */
1341 		oldest = get_oldest_context(inode, NULL, NULL);
1342 		if (snapc->seq > oldest->seq) {
1343 			ceph_put_snap_context(oldest);
1344 			dout(" page %p snapc %p not current or oldest\n",
1345 			     page, snapc);
1346 			/*
1347 			 * queue for writeback, and wait for snapc to
1348 			 * be writeable or written
1349 			 */
1350 			snapc = ceph_get_snap_context(snapc);
1351 			unlock_page(page);
1352 			ceph_queue_writeback(inode);
1353 			r = wait_event_killable(ci->i_cap_wq,
1354 			       context_is_writeable_or_written(inode, snapc));
1355 			ceph_put_snap_context(snapc);
1356 			if (r == -ERESTARTSYS)
1357 				return r;
1358 			return -EAGAIN;
1359 		}
1360 		ceph_put_snap_context(oldest);
1361 
1362 		/* yay, writeable, do it now (without dropping page lock) */
1363 		dout(" page %p snapc %p not current, but oldest\n",
1364 		     page, snapc);
1365 		if (!clear_page_dirty_for_io(page))
1366 			goto retry_locked;
1367 		r = writepage_nounlock(page, NULL);
1368 		if (r < 0)
1369 			goto fail_unlock;
1370 		goto retry_locked;
1371 	}
1372 
1373 	if (PageUptodate(page)) {
1374 		dout(" page %p already uptodate\n", page);
1375 		return 0;
1376 	}
1377 
1378 	/* full page? */
1379 	if (pos_in_page == 0 && len == PAGE_SIZE)
1380 		return 0;
1381 
1382 	/* past end of file? */
1383 	i_size = i_size_read(inode);
1384 
1385 	if (page_off >= i_size ||
1386 	    (pos_in_page == 0 && (pos+len) >= i_size &&
1387 	     end_in_page - pos_in_page != PAGE_SIZE)) {
1388 		dout(" zeroing %p 0 - %d and %d - %d\n",
1389 		     page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1390 		zero_user_segments(page,
1391 				   0, pos_in_page,
1392 				   end_in_page, PAGE_SIZE);
1393 		return 0;
1394 	}
1395 
1396 	/* we need to read it. */
1397 	r = ceph_do_readpage(file, page);
1398 	if (r < 0) {
1399 		if (r == -EINPROGRESS)
1400 			return -EAGAIN;
1401 		goto fail_unlock;
1402 	}
1403 	goto retry_locked;
1404 fail_unlock:
1405 	unlock_page(page);
1406 	return r;
1407 }
1408 
1409 /*
1410  * We are only allowed to write into/dirty the page if the page is
1411  * clean, or already dirty within the same snap context.
1412  */
1413 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1414 			    loff_t pos, unsigned len, unsigned flags,
1415 			    struct page **pagep, void **fsdata)
1416 {
1417 	struct inode *inode = file_inode(file);
1418 	struct page *page;
1419 	pgoff_t index = pos >> PAGE_SHIFT;
1420 	int r;
1421 
1422 	do {
1423 		/* get a page */
1424 		page = grab_cache_page_write_begin(mapping, index, 0);
1425 		if (!page)
1426 			return -ENOMEM;
1427 
1428 		dout("write_begin file %p inode %p page %p %d~%d\n", file,
1429 		     inode, page, (int)pos, (int)len);
1430 
1431 		r = ceph_update_writeable_page(file, pos, len, page);
1432 		if (r < 0)
1433 			put_page(page);
1434 		else
1435 			*pagep = page;
1436 	} while (r == -EAGAIN);
1437 
1438 	return r;
1439 }
1440 
1441 /*
1442  * we don't do anything in here that simple_write_end doesn't do
1443  * except adjust dirty page accounting
1444  */
1445 static int ceph_write_end(struct file *file, struct address_space *mapping,
1446 			  loff_t pos, unsigned len, unsigned copied,
1447 			  struct page *page, void *fsdata)
1448 {
1449 	struct inode *inode = file_inode(file);
1450 	bool check_cap = false;
1451 
1452 	dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1453 	     inode, page, (int)pos, (int)copied, (int)len);
1454 
1455 	/* zero the stale part of the page if we did a short copy */
1456 	if (!PageUptodate(page)) {
1457 		if (copied < len) {
1458 			copied = 0;
1459 			goto out;
1460 		}
1461 		SetPageUptodate(page);
1462 	}
1463 
1464 	/* did file size increase? */
1465 	if (pos+copied > i_size_read(inode))
1466 		check_cap = ceph_inode_set_size(inode, pos+copied);
1467 
1468 	set_page_dirty(page);
1469 
1470 out:
1471 	unlock_page(page);
1472 	put_page(page);
1473 
1474 	if (check_cap)
1475 		ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1476 
1477 	return copied;
1478 }
1479 
1480 /*
1481  * we set .direct_IO to indicate direct io is supported, but since we
1482  * intercept O_DIRECT reads and writes early, this function should
1483  * never get called.
1484  */
1485 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1486 {
1487 	WARN_ON(1);
1488 	return -EINVAL;
1489 }
1490 
1491 const struct address_space_operations ceph_aops = {
1492 	.readpage = ceph_readpage,
1493 	.readpages = ceph_readpages,
1494 	.writepage = ceph_writepage,
1495 	.writepages = ceph_writepages_start,
1496 	.write_begin = ceph_write_begin,
1497 	.write_end = ceph_write_end,
1498 	.set_page_dirty = ceph_set_page_dirty,
1499 	.invalidatepage = ceph_invalidatepage,
1500 	.releasepage = ceph_releasepage,
1501 	.direct_IO = ceph_direct_io,
1502 };
1503 
1504 static void ceph_block_sigs(sigset_t *oldset)
1505 {
1506 	sigset_t mask;
1507 	siginitsetinv(&mask, sigmask(SIGKILL));
1508 	sigprocmask(SIG_BLOCK, &mask, oldset);
1509 }
1510 
1511 static void ceph_restore_sigs(sigset_t *oldset)
1512 {
1513 	sigprocmask(SIG_SETMASK, oldset, NULL);
1514 }
1515 
1516 /*
1517  * vm ops
1518  */
1519 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1520 {
1521 	struct vm_area_struct *vma = vmf->vma;
1522 	struct inode *inode = file_inode(vma->vm_file);
1523 	struct ceph_inode_info *ci = ceph_inode(inode);
1524 	struct ceph_file_info *fi = vma->vm_file->private_data;
1525 	struct page *pinned_page = NULL;
1526 	loff_t off = vmf->pgoff << PAGE_SHIFT;
1527 	int want, got, err;
1528 	sigset_t oldset;
1529 	vm_fault_t ret = VM_FAULT_SIGBUS;
1530 
1531 	ceph_block_sigs(&oldset);
1532 
1533 	dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1534 	     inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1535 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
1536 		want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1537 	else
1538 		want = CEPH_CAP_FILE_CACHE;
1539 
1540 	got = 0;
1541 	err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1,
1542 			    &got, &pinned_page);
1543 	if (err < 0)
1544 		goto out_restore;
1545 
1546 	dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1547 	     inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1548 
1549 	if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1550 	    ci->i_inline_version == CEPH_INLINE_NONE) {
1551 		CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1552 		ceph_add_rw_context(fi, &rw_ctx);
1553 		ret = filemap_fault(vmf);
1554 		ceph_del_rw_context(fi, &rw_ctx);
1555 		dout("filemap_fault %p %llu~%zd drop cap refs %s ret %x\n",
1556 			inode, off, (size_t)PAGE_SIZE,
1557 				ceph_cap_string(got), ret);
1558 	} else
1559 		err = -EAGAIN;
1560 
1561 	if (pinned_page)
1562 		put_page(pinned_page);
1563 	ceph_put_cap_refs(ci, got);
1564 
1565 	if (err != -EAGAIN)
1566 		goto out_restore;
1567 
1568 	/* read inline data */
1569 	if (off >= PAGE_SIZE) {
1570 		/* does not support inline data > PAGE_SIZE */
1571 		ret = VM_FAULT_SIGBUS;
1572 	} else {
1573 		struct address_space *mapping = inode->i_mapping;
1574 		struct page *page = find_or_create_page(mapping, 0,
1575 						mapping_gfp_constraint(mapping,
1576 						~__GFP_FS));
1577 		if (!page) {
1578 			ret = VM_FAULT_OOM;
1579 			goto out_inline;
1580 		}
1581 		err = __ceph_do_getattr(inode, page,
1582 					 CEPH_STAT_CAP_INLINE_DATA, true);
1583 		if (err < 0 || off >= i_size_read(inode)) {
1584 			unlock_page(page);
1585 			put_page(page);
1586 			ret = vmf_error(err);
1587 			goto out_inline;
1588 		}
1589 		if (err < PAGE_SIZE)
1590 			zero_user_segment(page, err, PAGE_SIZE);
1591 		else
1592 			flush_dcache_page(page);
1593 		SetPageUptodate(page);
1594 		vmf->page = page;
1595 		ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1596 out_inline:
1597 		dout("filemap_fault %p %llu~%zd read inline data ret %x\n",
1598 		     inode, off, (size_t)PAGE_SIZE, ret);
1599 	}
1600 out_restore:
1601 	ceph_restore_sigs(&oldset);
1602 	if (err < 0)
1603 		ret = vmf_error(err);
1604 
1605 	return ret;
1606 }
1607 
1608 /*
1609  * Reuse write_begin here for simplicity.
1610  */
1611 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1612 {
1613 	struct vm_area_struct *vma = vmf->vma;
1614 	struct inode *inode = file_inode(vma->vm_file);
1615 	struct ceph_inode_info *ci = ceph_inode(inode);
1616 	struct ceph_file_info *fi = vma->vm_file->private_data;
1617 	struct ceph_cap_flush *prealloc_cf;
1618 	struct page *page = vmf->page;
1619 	loff_t off = page_offset(page);
1620 	loff_t size = i_size_read(inode);
1621 	size_t len;
1622 	int want, got, err;
1623 	sigset_t oldset;
1624 	vm_fault_t ret = VM_FAULT_SIGBUS;
1625 
1626 	prealloc_cf = ceph_alloc_cap_flush();
1627 	if (!prealloc_cf)
1628 		return VM_FAULT_OOM;
1629 
1630 	sb_start_pagefault(inode->i_sb);
1631 	ceph_block_sigs(&oldset);
1632 
1633 	if (ci->i_inline_version != CEPH_INLINE_NONE) {
1634 		struct page *locked_page = NULL;
1635 		if (off == 0) {
1636 			lock_page(page);
1637 			locked_page = page;
1638 		}
1639 		err = ceph_uninline_data(vma->vm_file, locked_page);
1640 		if (locked_page)
1641 			unlock_page(locked_page);
1642 		if (err < 0)
1643 			goto out_free;
1644 	}
1645 
1646 	if (off + PAGE_SIZE <= size)
1647 		len = PAGE_SIZE;
1648 	else
1649 		len = size & ~PAGE_MASK;
1650 
1651 	dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1652 	     inode, ceph_vinop(inode), off, len, size);
1653 	if (fi->fmode & CEPH_FILE_MODE_LAZY)
1654 		want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1655 	else
1656 		want = CEPH_CAP_FILE_BUFFER;
1657 
1658 	got = 0;
1659 	err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len,
1660 			    &got, NULL);
1661 	if (err < 0)
1662 		goto out_free;
1663 
1664 	dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1665 	     inode, off, len, ceph_cap_string(got));
1666 
1667 	/* Update time before taking page lock */
1668 	file_update_time(vma->vm_file);
1669 	inode_inc_iversion_raw(inode);
1670 
1671 	do {
1672 		lock_page(page);
1673 
1674 		if (page_mkwrite_check_truncate(page, inode) < 0) {
1675 			unlock_page(page);
1676 			ret = VM_FAULT_NOPAGE;
1677 			break;
1678 		}
1679 
1680 		err = ceph_update_writeable_page(vma->vm_file, off, len, page);
1681 		if (err >= 0) {
1682 			/* success.  we'll keep the page locked. */
1683 			set_page_dirty(page);
1684 			ret = VM_FAULT_LOCKED;
1685 		}
1686 	} while (err == -EAGAIN);
1687 
1688 	if (ret == VM_FAULT_LOCKED ||
1689 	    ci->i_inline_version != CEPH_INLINE_NONE) {
1690 		int dirty;
1691 		spin_lock(&ci->i_ceph_lock);
1692 		ci->i_inline_version = CEPH_INLINE_NONE;
1693 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1694 					       &prealloc_cf);
1695 		spin_unlock(&ci->i_ceph_lock);
1696 		if (dirty)
1697 			__mark_inode_dirty(inode, dirty);
1698 	}
1699 
1700 	dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
1701 	     inode, off, len, ceph_cap_string(got), ret);
1702 	ceph_put_cap_refs(ci, got);
1703 out_free:
1704 	ceph_restore_sigs(&oldset);
1705 	sb_end_pagefault(inode->i_sb);
1706 	ceph_free_cap_flush(prealloc_cf);
1707 	if (err < 0)
1708 		ret = vmf_error(err);
1709 	return ret;
1710 }
1711 
1712 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1713 			   char	*data, size_t len)
1714 {
1715 	struct address_space *mapping = inode->i_mapping;
1716 	struct page *page;
1717 
1718 	if (locked_page) {
1719 		page = locked_page;
1720 	} else {
1721 		if (i_size_read(inode) == 0)
1722 			return;
1723 		page = find_or_create_page(mapping, 0,
1724 					   mapping_gfp_constraint(mapping,
1725 					   ~__GFP_FS));
1726 		if (!page)
1727 			return;
1728 		if (PageUptodate(page)) {
1729 			unlock_page(page);
1730 			put_page(page);
1731 			return;
1732 		}
1733 	}
1734 
1735 	dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1736 	     inode, ceph_vinop(inode), len, locked_page);
1737 
1738 	if (len > 0) {
1739 		void *kaddr = kmap_atomic(page);
1740 		memcpy(kaddr, data, len);
1741 		kunmap_atomic(kaddr);
1742 	}
1743 
1744 	if (page != locked_page) {
1745 		if (len < PAGE_SIZE)
1746 			zero_user_segment(page, len, PAGE_SIZE);
1747 		else
1748 			flush_dcache_page(page);
1749 
1750 		SetPageUptodate(page);
1751 		unlock_page(page);
1752 		put_page(page);
1753 	}
1754 }
1755 
1756 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1757 {
1758 	struct inode *inode = file_inode(filp);
1759 	struct ceph_inode_info *ci = ceph_inode(inode);
1760 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1761 	struct ceph_osd_request *req;
1762 	struct page *page = NULL;
1763 	u64 len, inline_version;
1764 	int err = 0;
1765 	bool from_pagecache = false;
1766 
1767 	spin_lock(&ci->i_ceph_lock);
1768 	inline_version = ci->i_inline_version;
1769 	spin_unlock(&ci->i_ceph_lock);
1770 
1771 	dout("uninline_data %p %llx.%llx inline_version %llu\n",
1772 	     inode, ceph_vinop(inode), inline_version);
1773 
1774 	if (inline_version == 1 || /* initial version, no data */
1775 	    inline_version == CEPH_INLINE_NONE)
1776 		goto out;
1777 
1778 	if (locked_page) {
1779 		page = locked_page;
1780 		WARN_ON(!PageUptodate(page));
1781 	} else if (ceph_caps_issued(ci) &
1782 		   (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1783 		page = find_get_page(inode->i_mapping, 0);
1784 		if (page) {
1785 			if (PageUptodate(page)) {
1786 				from_pagecache = true;
1787 				lock_page(page);
1788 			} else {
1789 				put_page(page);
1790 				page = NULL;
1791 			}
1792 		}
1793 	}
1794 
1795 	if (page) {
1796 		len = i_size_read(inode);
1797 		if (len > PAGE_SIZE)
1798 			len = PAGE_SIZE;
1799 	} else {
1800 		page = __page_cache_alloc(GFP_NOFS);
1801 		if (!page) {
1802 			err = -ENOMEM;
1803 			goto out;
1804 		}
1805 		err = __ceph_do_getattr(inode, page,
1806 					CEPH_STAT_CAP_INLINE_DATA, true);
1807 		if (err < 0) {
1808 			/* no inline data */
1809 			if (err == -ENODATA)
1810 				err = 0;
1811 			goto out;
1812 		}
1813 		len = err;
1814 	}
1815 
1816 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1817 				    ceph_vino(inode), 0, &len, 0, 1,
1818 				    CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1819 				    NULL, 0, 0, false);
1820 	if (IS_ERR(req)) {
1821 		err = PTR_ERR(req);
1822 		goto out;
1823 	}
1824 
1825 	req->r_mtime = inode->i_mtime;
1826 	err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1827 	if (!err)
1828 		err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1829 	ceph_osdc_put_request(req);
1830 	if (err < 0)
1831 		goto out;
1832 
1833 	req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1834 				    ceph_vino(inode), 0, &len, 1, 3,
1835 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1836 				    NULL, ci->i_truncate_seq,
1837 				    ci->i_truncate_size, false);
1838 	if (IS_ERR(req)) {
1839 		err = PTR_ERR(req);
1840 		goto out;
1841 	}
1842 
1843 	osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1844 
1845 	{
1846 		__le64 xattr_buf = cpu_to_le64(inline_version);
1847 		err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1848 					    "inline_version", &xattr_buf,
1849 					    sizeof(xattr_buf),
1850 					    CEPH_OSD_CMPXATTR_OP_GT,
1851 					    CEPH_OSD_CMPXATTR_MODE_U64);
1852 		if (err)
1853 			goto out_put;
1854 	}
1855 
1856 	{
1857 		char xattr_buf[32];
1858 		int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1859 					 "%llu", inline_version);
1860 		err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1861 					    "inline_version",
1862 					    xattr_buf, xattr_len, 0, 0);
1863 		if (err)
1864 			goto out_put;
1865 	}
1866 
1867 	req->r_mtime = inode->i_mtime;
1868 	err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1869 	if (!err)
1870 		err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1871 
1872 	ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
1873 				  req->r_end_latency, err);
1874 
1875 out_put:
1876 	ceph_osdc_put_request(req);
1877 	if (err == -ECANCELED)
1878 		err = 0;
1879 out:
1880 	if (page && page != locked_page) {
1881 		if (from_pagecache) {
1882 			unlock_page(page);
1883 			put_page(page);
1884 		} else
1885 			__free_pages(page, 0);
1886 	}
1887 
1888 	dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1889 	     inode, ceph_vinop(inode), inline_version, err);
1890 	return err;
1891 }
1892 
1893 static const struct vm_operations_struct ceph_vmops = {
1894 	.fault		= ceph_filemap_fault,
1895 	.page_mkwrite	= ceph_page_mkwrite,
1896 };
1897 
1898 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1899 {
1900 	struct address_space *mapping = file->f_mapping;
1901 
1902 	if (!mapping->a_ops->readpage)
1903 		return -ENOEXEC;
1904 	file_accessed(file);
1905 	vma->vm_ops = &ceph_vmops;
1906 	return 0;
1907 }
1908 
1909 enum {
1910 	POOL_READ	= 1,
1911 	POOL_WRITE	= 2,
1912 };
1913 
1914 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1915 				s64 pool, struct ceph_string *pool_ns)
1916 {
1917 	struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1918 	struct ceph_mds_client *mdsc = fsc->mdsc;
1919 	struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1920 	struct rb_node **p, *parent;
1921 	struct ceph_pool_perm *perm;
1922 	struct page **pages;
1923 	size_t pool_ns_len;
1924 	int err = 0, err2 = 0, have = 0;
1925 
1926 	down_read(&mdsc->pool_perm_rwsem);
1927 	p = &mdsc->pool_perm_tree.rb_node;
1928 	while (*p) {
1929 		perm = rb_entry(*p, struct ceph_pool_perm, node);
1930 		if (pool < perm->pool)
1931 			p = &(*p)->rb_left;
1932 		else if (pool > perm->pool)
1933 			p = &(*p)->rb_right;
1934 		else {
1935 			int ret = ceph_compare_string(pool_ns,
1936 						perm->pool_ns,
1937 						perm->pool_ns_len);
1938 			if (ret < 0)
1939 				p = &(*p)->rb_left;
1940 			else if (ret > 0)
1941 				p = &(*p)->rb_right;
1942 			else {
1943 				have = perm->perm;
1944 				break;
1945 			}
1946 		}
1947 	}
1948 	up_read(&mdsc->pool_perm_rwsem);
1949 	if (*p)
1950 		goto out;
1951 
1952 	if (pool_ns)
1953 		dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1954 		     pool, (int)pool_ns->len, pool_ns->str);
1955 	else
1956 		dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1957 
1958 	down_write(&mdsc->pool_perm_rwsem);
1959 	p = &mdsc->pool_perm_tree.rb_node;
1960 	parent = NULL;
1961 	while (*p) {
1962 		parent = *p;
1963 		perm = rb_entry(parent, struct ceph_pool_perm, node);
1964 		if (pool < perm->pool)
1965 			p = &(*p)->rb_left;
1966 		else if (pool > perm->pool)
1967 			p = &(*p)->rb_right;
1968 		else {
1969 			int ret = ceph_compare_string(pool_ns,
1970 						perm->pool_ns,
1971 						perm->pool_ns_len);
1972 			if (ret < 0)
1973 				p = &(*p)->rb_left;
1974 			else if (ret > 0)
1975 				p = &(*p)->rb_right;
1976 			else {
1977 				have = perm->perm;
1978 				break;
1979 			}
1980 		}
1981 	}
1982 	if (*p) {
1983 		up_write(&mdsc->pool_perm_rwsem);
1984 		goto out;
1985 	}
1986 
1987 	rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1988 					 1, false, GFP_NOFS);
1989 	if (!rd_req) {
1990 		err = -ENOMEM;
1991 		goto out_unlock;
1992 	}
1993 
1994 	rd_req->r_flags = CEPH_OSD_FLAG_READ;
1995 	osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1996 	rd_req->r_base_oloc.pool = pool;
1997 	if (pool_ns)
1998 		rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1999 	ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
2000 
2001 	err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
2002 	if (err)
2003 		goto out_unlock;
2004 
2005 	wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
2006 					 1, false, GFP_NOFS);
2007 	if (!wr_req) {
2008 		err = -ENOMEM;
2009 		goto out_unlock;
2010 	}
2011 
2012 	wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
2013 	osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
2014 	ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
2015 	ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
2016 
2017 	err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
2018 	if (err)
2019 		goto out_unlock;
2020 
2021 	/* one page should be large enough for STAT data */
2022 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
2023 	if (IS_ERR(pages)) {
2024 		err = PTR_ERR(pages);
2025 		goto out_unlock;
2026 	}
2027 
2028 	osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
2029 				     0, false, true);
2030 	err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
2031 
2032 	wr_req->r_mtime = ci->vfs_inode.i_mtime;
2033 	err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
2034 
2035 	if (!err)
2036 		err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
2037 	if (!err2)
2038 		err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
2039 
2040 	if (err >= 0 || err == -ENOENT)
2041 		have |= POOL_READ;
2042 	else if (err != -EPERM) {
2043 		if (err == -EBLACKLISTED)
2044 			fsc->blacklisted = true;
2045 		goto out_unlock;
2046 	}
2047 
2048 	if (err2 == 0 || err2 == -EEXIST)
2049 		have |= POOL_WRITE;
2050 	else if (err2 != -EPERM) {
2051 		if (err2 == -EBLACKLISTED)
2052 			fsc->blacklisted = true;
2053 		err = err2;
2054 		goto out_unlock;
2055 	}
2056 
2057 	pool_ns_len = pool_ns ? pool_ns->len : 0;
2058 	perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
2059 	if (!perm) {
2060 		err = -ENOMEM;
2061 		goto out_unlock;
2062 	}
2063 
2064 	perm->pool = pool;
2065 	perm->perm = have;
2066 	perm->pool_ns_len = pool_ns_len;
2067 	if (pool_ns_len > 0)
2068 		memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2069 	perm->pool_ns[pool_ns_len] = 0;
2070 
2071 	rb_link_node(&perm->node, parent, p);
2072 	rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
2073 	err = 0;
2074 out_unlock:
2075 	up_write(&mdsc->pool_perm_rwsem);
2076 
2077 	ceph_osdc_put_request(rd_req);
2078 	ceph_osdc_put_request(wr_req);
2079 out:
2080 	if (!err)
2081 		err = have;
2082 	if (pool_ns)
2083 		dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
2084 		     pool, (int)pool_ns->len, pool_ns->str, err);
2085 	else
2086 		dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
2087 	return err;
2088 }
2089 
2090 int ceph_pool_perm_check(struct inode *inode, int need)
2091 {
2092 	struct ceph_inode_info *ci = ceph_inode(inode);
2093 	struct ceph_string *pool_ns;
2094 	s64 pool;
2095 	int ret, flags;
2096 
2097 	if (ci->i_vino.snap != CEPH_NOSNAP) {
2098 		/*
2099 		 * Pool permission check needs to write to the first object.
2100 		 * But for snapshot, head of the first object may have alread
2101 		 * been deleted. Skip check to avoid creating orphan object.
2102 		 */
2103 		return 0;
2104 	}
2105 
2106 	if (ceph_test_mount_opt(ceph_inode_to_client(inode),
2107 				NOPOOLPERM))
2108 		return 0;
2109 
2110 	spin_lock(&ci->i_ceph_lock);
2111 	flags = ci->i_ceph_flags;
2112 	pool = ci->i_layout.pool_id;
2113 	spin_unlock(&ci->i_ceph_lock);
2114 check:
2115 	if (flags & CEPH_I_POOL_PERM) {
2116 		if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2117 			dout("ceph_pool_perm_check pool %lld no read perm\n",
2118 			     pool);
2119 			return -EPERM;
2120 		}
2121 		if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2122 			dout("ceph_pool_perm_check pool %lld no write perm\n",
2123 			     pool);
2124 			return -EPERM;
2125 		}
2126 		return 0;
2127 	}
2128 
2129 	pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2130 	ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2131 	ceph_put_string(pool_ns);
2132 	if (ret < 0)
2133 		return ret;
2134 
2135 	flags = CEPH_I_POOL_PERM;
2136 	if (ret & POOL_READ)
2137 		flags |= CEPH_I_POOL_RD;
2138 	if (ret & POOL_WRITE)
2139 		flags |= CEPH_I_POOL_WR;
2140 
2141 	spin_lock(&ci->i_ceph_lock);
2142 	if (pool == ci->i_layout.pool_id &&
2143 	    pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2144 		ci->i_ceph_flags |= flags;
2145         } else {
2146 		pool = ci->i_layout.pool_id;
2147 		flags = ci->i_ceph_flags;
2148 	}
2149 	spin_unlock(&ci->i_ceph_lock);
2150 	goto check;
2151 }
2152 
2153 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2154 {
2155 	struct ceph_pool_perm *perm;
2156 	struct rb_node *n;
2157 
2158 	while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2159 		n = rb_first(&mdsc->pool_perm_tree);
2160 		perm = rb_entry(n, struct ceph_pool_perm, node);
2161 		rb_erase(n, &mdsc->pool_perm_tree);
2162 		kfree(perm);
2163 	}
2164 }
2165