1234 lines · 33.1 KB · C
RawDownload
#include <linux/ceph/ceph_debug.h> #include <linux/backing-dev.h>#include <linux/fs.h>#include <linux/mm.h>#include <linux/pagemap.h>#include <linux/writeback.h>	/* generic_writepages */#include <linux/slab.h>#include <linux/pagevec.h>#include <linux/task_io_accounting_ops.h> #include "super.h"#include "mds_client.h"#include <linux/ceph/osd_client.h> /* * Ceph address space ops. * * There are a few funny things going on here. * * The page->private field is used to reference a struct * ceph_snap_context for _every_ dirty page.  This indicates which * snapshot the page was logically dirtied in, and thus which snap * context needs to be associated with the osd write during writeback. * * Similarly, struct ceph_inode_info maintains a set of counters to * count dirty pages on the inode.  In the absence of snapshots, * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. * * When a snapshot is taken (that is, when the client receives * notification that a snapshot was taken), each inode with caps and * with dirty pages (dirty pages implies there is a cap) gets a new * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending * order, new snaps go to the tail).  The i_wrbuffer_ref_head count is * moved to capsnap->dirty. (Unless a sync write is currently in * progress.  In that case, the capsnap is said to be "pending", new * writes cannot start, and the capsnap isn't "finalized" until the * write completes (or fails) and a final size/mtime for the inode for * that snap can be settled upon.)  i_wrbuffer_ref_head is reset to 0. * * On writeback, we must submit writes to the osd IN SNAP ORDER.  So, * we look for the first capsnap in i_cap_snaps and write out pages in * that snap context _only_.  Then we move on to the next capsnap, * eventually reaching the "live" or "head" context (i.e., pages that * are not yet snapped) and are writing the most recently dirtied * pages. * * Invalidate and so forth must take care to ensure the dirty page * accounting is preserved. */ #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))#define CONGESTION_OFF_THRESH(congestion_kb)				\	(CONGESTION_ON_THRESH(congestion_kb) -				\	 (CONGESTION_ON_THRESH(congestion_kb) >> 2))   /* * Dirty a page.  Optimistically adjust accounting, on the assumption * that we won't race with invalidate.  If we do, readjust. */static int ceph_set_page_dirty(struct page *page){	struct address_space *mapping = page->mapping;	struct inode *inode;	struct ceph_inode_info *ci;	int undo = 0;	struct ceph_snap_context *snapc; 	if (unlikely(!mapping))		return !TestSetPageDirty(page); 	if (TestSetPageDirty(page)) {		dout("%p set_page_dirty %p idx %lu -- already dirty\n",		     mapping->host, page, page->index);		return 0;	} 	inode = mapping->host;	ci = ceph_inode(inode); 	/*	 * Note that we're grabbing a snapc ref here without holding	 * any locks!	 */	snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context); 	/* dirty the head */	spin_lock(&ci->i_ceph_lock);	if (ci->i_head_snapc == NULL)		ci->i_head_snapc = ceph_get_snap_context(snapc);	++ci->i_wrbuffer_ref_head;	if (ci->i_wrbuffer_ref == 0)		ihold(inode);	++ci->i_wrbuffer_ref;	dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "	     "snapc %p seq %lld (%d snaps)\n",	     mapping->host, page, page->index,	     ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,	     ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,	     snapc, snapc->seq, snapc->num_snaps);	spin_unlock(&ci->i_ceph_lock); 	/* now adjust page */	spin_lock_irq(&mapping->tree_lock);	if (page->mapping) {	/* Race with truncate? */		WARN_ON_ONCE(!PageUptodate(page));		account_page_dirtied(page, page->mapping);		radix_tree_tag_set(&mapping->page_tree,				page_index(page), PAGECACHE_TAG_DIRTY); 		/*		 * Reference snap context in page->private.  Also set		 * PagePrivate so that we get invalidatepage callback.		 */		page->private = (unsigned long)snapc;		SetPagePrivate(page);	} else {		dout("ANON set_page_dirty %p (raced truncate?)\n", page);		undo = 1;	} 	spin_unlock_irq(&mapping->tree_lock); 	if (undo)		/* whoops, we failed to dirty the page */		ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 	BUG_ON(!PageDirty(page));	return 1;} /* * If we are truncating the full page (i.e. offset == 0), adjust the * dirty page counters appropriately.  Only called if there is private * data on the page. */static void ceph_invalidatepage(struct page *page, unsigned long offset){	struct inode *inode;	struct ceph_inode_info *ci;	struct ceph_snap_context *snapc = (void *)page->private; 	BUG_ON(!PageLocked(page));	BUG_ON(!page->private);	BUG_ON(!PagePrivate(page));	BUG_ON(!page->mapping); 	inode = page->mapping->host; 	/*	 * We can get non-dirty pages here due to races between	 * set_page_dirty and truncate_complete_page; just spit out a	 * warning, in case we end up with accounting problems later.	 */	if (!PageDirty(page))		pr_err("%p invalidatepage %p page not dirty\n", inode, page); 	if (offset == 0)		ClearPageChecked(page); 	ci = ceph_inode(inode);	if (offset == 0) {		dout("%p invalidatepage %p idx %lu full dirty page %lu\n",		     inode, page, page->index, offset);		ceph_put_wrbuffer_cap_refs(ci, 1, snapc);		ceph_put_snap_context(snapc);		page->private = 0;		ClearPagePrivate(page);	} else {		dout("%p invalidatepage %p idx %lu partial dirty page\n",		     inode, page, page->index);	}} /* just a sanity check */static int ceph_releasepage(struct page *page, gfp_t g){	struct inode *inode = page->mapping ? page->mapping->host : NULL;	dout("%p releasepage %p idx %lu\n", inode, page, page->index);	WARN_ON(PageDirty(page));	WARN_ON(page->private);	WARN_ON(PagePrivate(page));	return 0;} /* * read a single page, without unlocking it. */static int readpage_nounlock(struct file *filp, struct page *page){	struct inode *inode = filp->f_dentry->d_inode;	struct ceph_inode_info *ci = ceph_inode(inode);	struct ceph_osd_client *osdc = 		&ceph_inode_to_client(inode)->client->osdc;	int err = 0;	u64 len = PAGE_CACHE_SIZE; 	dout("readpage inode %p file %p page %p index %lu\n",	     inode, filp, page, page->index);	err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,				  page->index << PAGE_CACHE_SHIFT, &len,				  ci->i_truncate_seq, ci->i_truncate_size,				  &page, 1, 0);	if (err == -ENOENT)		err = 0;	if (err < 0) {		SetPageError(page);		goto out;	} else if (err < PAGE_CACHE_SIZE) {		/* zero fill remainder of page */		zero_user_segment(page, err, PAGE_CACHE_SIZE);	}	SetPageUptodate(page); out:	return err < 0 ? err : 0;} static int ceph_readpage(struct file *filp, struct page *page){	int r = readpage_nounlock(filp, page);	unlock_page(page);	return r;} /* * Finish an async read(ahead) op. */static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg){	struct inode *inode = req->r_inode;	struct ceph_osd_reply_head *replyhead;	int rc, bytes;	int i; 	/* parse reply */	replyhead = msg->front.iov_base;	WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);	rc = le32_to_cpu(replyhead->result);	bytes = le32_to_cpu(msg->hdr.data_len); 	dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes); 	/* unlock all pages, zeroing any data we didn't read */	for (i = 0; i < req->r_num_pages; i++, bytes -= PAGE_CACHE_SIZE) {		struct page *page = req->r_pages[i]; 		if (bytes < (int)PAGE_CACHE_SIZE) {			/* zero (remainder of) page */			int s = bytes < 0 ? 0 : bytes;			zero_user_segment(page, s, PAGE_CACHE_SIZE);		} 		dout("finish_read %p uptodate %p idx %lu\n", inode, page,		     page->index);		flush_dcache_page(page);		SetPageUptodate(page);		unlock_page(page);		page_cache_release(page);	}	kfree(req->r_pages);} /* * start an async read(ahead) operation.  return nr_pages we submitted * a read for on success, or negative error code. */static int start_read(struct inode *inode, struct list_head *page_list, int max){	struct ceph_osd_client *osdc =		&ceph_inode_to_client(inode)->client->osdc;	struct ceph_inode_info *ci = ceph_inode(inode);	struct page *page = list_entry(page_list->prev, struct page, lru);	struct ceph_osd_request *req;	u64 off;	u64 len;	int i;	struct page **pages;	pgoff_t next_index;	int nr_pages = 0;	int ret; 	off = page->index << PAGE_CACHE_SHIFT; 	/* count pages */	next_index = page->index;	list_for_each_entry_reverse(page, page_list, lru) {		if (page->index != next_index)			break;		nr_pages++;		next_index++;		if (max && nr_pages == max)			break;	}	len = nr_pages << PAGE_CACHE_SHIFT;	dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,	     off, len); 	req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode),				    off, &len,				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,				    NULL, 0,				    ci->i_truncate_seq, ci->i_truncate_size,				    NULL, false, 1, 0);	if (!req)		return -ENOMEM; 	/* build page vector */	nr_pages = len >> PAGE_CACHE_SHIFT;	pages = kmalloc(sizeof(*pages) * nr_pages, GFP_NOFS);	ret = -ENOMEM;	if (!pages)		goto out;	for (i = 0; i < nr_pages; ++i) {		page = list_entry(page_list->prev, struct page, lru);		BUG_ON(PageLocked(page));		list_del(&page->lru);		 		dout("start_read %p adding %p idx %lu\n", inode, page,		     page->index);		if (add_to_page_cache_lru(page, &inode->i_data, page->index,					  GFP_NOFS)) {			page_cache_release(page);			dout("start_read %p add_to_page_cache failed %p\n",			     inode, page);			nr_pages = i;			goto out_pages;		}		pages[i] = page;	}	req->r_pages = pages;	req->r_num_pages = nr_pages;	req->r_callback = finish_read;	req->r_inode = inode; 	dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);	ret = ceph_osdc_start_request(osdc, req, false);	if (ret < 0)		goto out_pages;	ceph_osdc_put_request(req);	return nr_pages; out_pages:	ceph_release_page_vector(pages, nr_pages);out:	ceph_osdc_put_request(req);	return ret;}  /* * Read multiple pages.  Leave pages we don't read + unlock in page_list; * the caller (VM) cleans them up. */static int ceph_readpages(struct file *file, struct address_space *mapping,			  struct list_head *page_list, unsigned nr_pages){	struct inode *inode = file->f_dentry->d_inode;	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);	int rc = 0;	int max = 0; 	if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE)		max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1)			>> PAGE_SHIFT; 	dout("readpages %p file %p nr_pages %d max %d\n", inode, file, nr_pages,	     max);	while (!list_empty(page_list)) {		rc = start_read(inode, page_list, max);		if (rc < 0)			goto out;		BUG_ON(rc == 0);	}out:	dout("readpages %p file %p ret %d\n", inode, file, rc);	return rc;} /* * Get ref for the oldest snapc for an inode with dirty data... that is, the * only snap context we are allowed to write back. */static struct ceph_snap_context *get_oldest_context(struct inode *inode,						    u64 *snap_size){	struct ceph_inode_info *ci = ceph_inode(inode);	struct ceph_snap_context *snapc = NULL;	struct ceph_cap_snap *capsnap = NULL; 	spin_lock(&ci->i_ceph_lock);	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {		dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,		     capsnap->context, capsnap->dirty_pages);		if (capsnap->dirty_pages) {			snapc = ceph_get_snap_context(capsnap->context);			if (snap_size)				*snap_size = capsnap->size;			break;		}	}	if (!snapc && ci->i_wrbuffer_ref_head) {		snapc = ceph_get_snap_context(ci->i_head_snapc);		dout(" head snapc %p has %d dirty pages\n",		     snapc, ci->i_wrbuffer_ref_head);	}	spin_unlock(&ci->i_ceph_lock);	return snapc;} /* * Write a single page, but leave the page locked. * * If we get a write error, set the page error bit, but still adjust the * dirty page accounting (i.e., page is no longer dirty). */static int writepage_nounlock(struct page *page, struct writeback_control *wbc){	struct inode *inode;	struct ceph_inode_info *ci;	struct ceph_fs_client *fsc;	struct ceph_osd_client *osdc;	loff_t page_off = page->index << PAGE_CACHE_SHIFT;	int len = PAGE_CACHE_SIZE;	loff_t i_size;	int err = 0;	struct ceph_snap_context *snapc, *oldest;	u64 snap_size = 0;	long writeback_stat; 	dout("writepage %p idx %lu\n", page, page->index); 	if (!page->mapping || !page->mapping->host) {		dout("writepage %p - no mapping\n", page);		return -EFAULT;	}	inode = page->mapping->host;	ci = ceph_inode(inode);	fsc = ceph_inode_to_client(inode);	osdc = &fsc->client->osdc; 	/* verify this is a writeable snap context */	snapc = (void *)page->private;	if (snapc == NULL) {		dout("writepage %p page %p not dirty?\n", inode, page);		goto out;	}	oldest = get_oldest_context(inode, &snap_size);	if (snapc->seq > oldest->seq) {		dout("writepage %p page %p snapc %p not writeable - noop\n",		     inode, page, (void *)page->private);		/* we should only noop if called by kswapd */		WARN_ON((current->flags & PF_MEMALLOC) == 0);		ceph_put_snap_context(oldest);		goto out;	}	ceph_put_snap_context(oldest); 	/* is this a partial page at end of file? */	if (snap_size)		i_size = snap_size;	else		i_size = i_size_read(inode);	if (i_size < page_off + len)		len = i_size - page_off; 	dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",	     inode, page, page->index, page_off, len, snapc); 	writeback_stat = atomic_long_inc_return(&fsc->writeback_count);	if (writeback_stat >	    CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))		set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC); 	set_page_writeback(page);	err = ceph_osdc_writepages(osdc, ceph_vino(inode),				   &ci->i_layout, snapc,				   page_off, len,				   ci->i_truncate_seq, ci->i_truncate_size,				   &inode->i_mtime,				   &page, 1, 0, 0, true);	if (err < 0) {		dout("writepage setting page/mapping error %d %p\n", err, page);		SetPageError(page);		mapping_set_error(&inode->i_data, err);		if (wbc)			wbc->pages_skipped++;	} else {		dout("writepage cleaned page %p\n", page);		err = 0;  /* vfs expects us to return 0 */	}	page->private = 0;	ClearPagePrivate(page);	end_page_writeback(page);	ceph_put_wrbuffer_cap_refs(ci, 1, snapc);	ceph_put_snap_context(snapc);  /* page's reference */out:

Lines 1–500 of 1234.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t