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