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