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