Linux Audio

Check our new training course

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