PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ceph/addr.c

https://github.com/mstsirkin/linux
C | 1181 lines | 847 code | 140 blank | 194 comment | 163 complexity | 9bdc9baf2deb7cd0287d34e02009798d MD5 | raw file
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/backing-dev.h>
  3. #include <linux/fs.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/writeback.h> /* generic_writepages */
  7. #include <linux/slab.h>
  8. #include <linux/pagevec.h>
  9. #include <linux/task_io_accounting_ops.h>
  10. #include "super.h"
  11. #include "mds_client.h"
  12. #include <linux/ceph/osd_client.h>
  13. /*
  14. * Ceph address space ops.
  15. *
  16. * There are a few funny things going on here.
  17. *
  18. * The page->private field is used to reference a struct
  19. * ceph_snap_context for _every_ dirty page. This indicates which
  20. * snapshot the page was logically dirtied in, and thus which snap
  21. * context needs to be associated with the osd write during writeback.
  22. *
  23. * Similarly, struct ceph_inode_info maintains a set of counters to
  24. * count dirty pages on the inode. In the absence of snapshots,
  25. * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
  26. *
  27. * When a snapshot is taken (that is, when the client receives
  28. * notification that a snapshot was taken), each inode with caps and
  29. * with dirty pages (dirty pages implies there is a cap) gets a new
  30. * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
  31. * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
  32. * moved to capsnap->dirty. (Unless a sync write is currently in
  33. * progress. In that case, the capsnap is said to be "pending", new
  34. * writes cannot start, and the capsnap isn't "finalized" until the
  35. * write completes (or fails) and a final size/mtime for the inode for
  36. * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
  37. *
  38. * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
  39. * we look for the first capsnap in i_cap_snaps and write out pages in
  40. * that snap context _only_. Then we move on to the next capsnap,
  41. * eventually reaching the "live" or "head" context (i.e., pages that
  42. * are not yet snapped) and are writing the most recently dirtied
  43. * pages.
  44. *
  45. * Invalidate and so forth must take care to ensure the dirty page
  46. * accounting is preserved.
  47. */
  48. #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
  49. #define CONGESTION_OFF_THRESH(congestion_kb) \
  50. (CONGESTION_ON_THRESH(congestion_kb) - \
  51. (CONGESTION_ON_THRESH(congestion_kb) >> 2))
  52. /*
  53. * Dirty a page. Optimistically adjust accounting, on the assumption
  54. * that we won't race with invalidate. If we do, readjust.
  55. */
  56. static int ceph_set_page_dirty(struct page *page)
  57. {
  58. struct address_space *mapping = page->mapping;
  59. struct inode *inode;
  60. struct ceph_inode_info *ci;
  61. int undo = 0;
  62. struct ceph_snap_context *snapc;
  63. if (unlikely(!mapping))
  64. return !TestSetPageDirty(page);
  65. if (TestSetPageDirty(page)) {
  66. dout("%p set_page_dirty %p idx %lu -- already dirty\n",
  67. mapping->host, page, page->index);
  68. return 0;
  69. }
  70. inode = mapping->host;
  71. ci = ceph_inode(inode);
  72. /*
  73. * Note that we're grabbing a snapc ref here without holding
  74. * any locks!
  75. */
  76. snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context);
  77. /* dirty the head */
  78. spin_lock(&inode->i_lock);
  79. if (ci->i_head_snapc == NULL)
  80. ci->i_head_snapc = ceph_get_snap_context(snapc);
  81. ++ci->i_wrbuffer_ref_head;
  82. if (ci->i_wrbuffer_ref == 0)
  83. ihold(inode);
  84. ++ci->i_wrbuffer_ref;
  85. dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
  86. "snapc %p seq %lld (%d snaps)\n",
  87. mapping->host, page, page->index,
  88. ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
  89. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  90. snapc, snapc->seq, snapc->num_snaps);
  91. spin_unlock(&inode->i_lock);
  92. /* now adjust page */
  93. spin_lock_irq(&mapping->tree_lock);
  94. if (page->mapping) { /* Race with truncate? */
  95. WARN_ON_ONCE(!PageUptodate(page));
  96. account_page_dirtied(page, page->mapping);
  97. radix_tree_tag_set(&mapping->page_tree,
  98. page_index(page), PAGECACHE_TAG_DIRTY);
  99. /*
  100. * Reference snap context in page->private. Also set
  101. * PagePrivate so that we get invalidatepage callback.
  102. */
  103. page->private = (unsigned long)snapc;
  104. SetPagePrivate(page);
  105. } else {
  106. dout("ANON set_page_dirty %p (raced truncate?)\n", page);
  107. undo = 1;
  108. }
  109. spin_unlock_irq(&mapping->tree_lock);
  110. if (undo)
  111. /* whoops, we failed to dirty the page */
  112. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  113. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  114. BUG_ON(!PageDirty(page));
  115. return 1;
  116. }
  117. /*
  118. * If we are truncating the full page (i.e. offset == 0), adjust the
  119. * dirty page counters appropriately. Only called if there is private
  120. * data on the page.
  121. */
  122. static void ceph_invalidatepage(struct page *page, unsigned long offset)
  123. {
  124. struct inode *inode;
  125. struct ceph_inode_info *ci;
  126. struct ceph_snap_context *snapc = (void *)page->private;
  127. BUG_ON(!PageLocked(page));
  128. BUG_ON(!page->private);
  129. BUG_ON(!PagePrivate(page));
  130. BUG_ON(!page->mapping);
  131. inode = page->mapping->host;
  132. /*
  133. * We can get non-dirty pages here due to races between
  134. * set_page_dirty and truncate_complete_page; just spit out a
  135. * warning, in case we end up with accounting problems later.
  136. */
  137. if (!PageDirty(page))
  138. pr_err("%p invalidatepage %p page not dirty\n", inode, page);
  139. if (offset == 0)
  140. ClearPageChecked(page);
  141. ci = ceph_inode(inode);
  142. if (offset == 0) {
  143. dout("%p invalidatepage %p idx %lu full dirty page %lu\n",
  144. inode, page, page->index, offset);
  145. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  146. ceph_put_snap_context(snapc);
  147. page->private = 0;
  148. ClearPagePrivate(page);
  149. } else {
  150. dout("%p invalidatepage %p idx %lu partial dirty page\n",
  151. inode, page, page->index);
  152. }
  153. }
  154. /* just a sanity check */
  155. static int ceph_releasepage(struct page *page, gfp_t g)
  156. {
  157. struct inode *inode = page->mapping ? page->mapping->host : NULL;
  158. dout("%p releasepage %p idx %lu\n", inode, page, page->index);
  159. WARN_ON(PageDirty(page));
  160. WARN_ON(page->private);
  161. WARN_ON(PagePrivate(page));
  162. return 0;
  163. }
  164. /*
  165. * read a single page, without unlocking it.
  166. */
  167. static int readpage_nounlock(struct file *filp, struct page *page)
  168. {
  169. struct inode *inode = filp->f_dentry->d_inode;
  170. struct ceph_inode_info *ci = ceph_inode(inode);
  171. struct ceph_osd_client *osdc =
  172. &ceph_inode_to_client(inode)->client->osdc;
  173. int err = 0;
  174. u64 len = PAGE_CACHE_SIZE;
  175. dout("readpage inode %p file %p page %p index %lu\n",
  176. inode, filp, page, page->index);
  177. err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
  178. page->index << PAGE_CACHE_SHIFT, &len,
  179. ci->i_truncate_seq, ci->i_truncate_size,
  180. &page, 1, 0);
  181. if (err == -ENOENT)
  182. err = 0;
  183. if (err < 0) {
  184. SetPageError(page);
  185. goto out;
  186. } else if (err < PAGE_CACHE_SIZE) {
  187. /* zero fill remainder of page */
  188. zero_user_segment(page, err, PAGE_CACHE_SIZE);
  189. }
  190. SetPageUptodate(page);
  191. out:
  192. return err < 0 ? err : 0;
  193. }
  194. static int ceph_readpage(struct file *filp, struct page *page)
  195. {
  196. int r = readpage_nounlock(filp, page);
  197. unlock_page(page);
  198. return r;
  199. }
  200. /*
  201. * Build a vector of contiguous pages from the provided page list.
  202. */
  203. static struct page **page_vector_from_list(struct list_head *page_list,
  204. unsigned *nr_pages)
  205. {
  206. struct page **pages;
  207. struct page *page;
  208. int next_index, contig_pages = 0;
  209. /* build page vector */
  210. pages = kmalloc(sizeof(*pages) * *nr_pages, GFP_NOFS);
  211. if (!pages)
  212. return ERR_PTR(-ENOMEM);
  213. BUG_ON(list_empty(page_list));
  214. next_index = list_entry(page_list->prev, struct page, lru)->index;
  215. list_for_each_entry_reverse(page, page_list, lru) {
  216. if (page->index == next_index) {
  217. dout("readpages page %d %p\n", contig_pages, page);
  218. pages[contig_pages] = page;
  219. contig_pages++;
  220. next_index++;
  221. } else {
  222. break;
  223. }
  224. }
  225. *nr_pages = contig_pages;
  226. return pages;
  227. }
  228. /*
  229. * Read multiple pages. Leave pages we don't read + unlock in page_list;
  230. * the caller (VM) cleans them up.
  231. */
  232. static int ceph_readpages(struct file *file, struct address_space *mapping,
  233. struct list_head *page_list, unsigned nr_pages)
  234. {
  235. struct inode *inode = file->f_dentry->d_inode;
  236. struct ceph_inode_info *ci = ceph_inode(inode);
  237. struct ceph_osd_client *osdc =
  238. &ceph_inode_to_client(inode)->client->osdc;
  239. int rc = 0;
  240. struct page **pages;
  241. loff_t offset;
  242. u64 len;
  243. dout("readpages %p file %p nr_pages %d\n",
  244. inode, file, nr_pages);
  245. pages = page_vector_from_list(page_list, &nr_pages);
  246. if (IS_ERR(pages))
  247. return PTR_ERR(pages);
  248. /* guess read extent */
  249. offset = pages[0]->index << PAGE_CACHE_SHIFT;
  250. len = nr_pages << PAGE_CACHE_SHIFT;
  251. rc = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
  252. offset, &len,
  253. ci->i_truncate_seq, ci->i_truncate_size,
  254. pages, nr_pages, 0);
  255. if (rc == -ENOENT)
  256. rc = 0;
  257. if (rc < 0)
  258. goto out;
  259. for (; !list_empty(page_list) && len > 0;
  260. rc -= PAGE_CACHE_SIZE, len -= PAGE_CACHE_SIZE) {
  261. struct page *page =
  262. list_entry(page_list->prev, struct page, lru);
  263. list_del(&page->lru);
  264. if (rc < (int)PAGE_CACHE_SIZE) {
  265. /* zero (remainder of) page */
  266. int s = rc < 0 ? 0 : rc;
  267. zero_user_segment(page, s, PAGE_CACHE_SIZE);
  268. }
  269. if (add_to_page_cache_lru(page, mapping, page->index,
  270. GFP_NOFS)) {
  271. page_cache_release(page);
  272. dout("readpages %p add_to_page_cache failed %p\n",
  273. inode, page);
  274. continue;
  275. }
  276. dout("readpages %p adding %p idx %lu\n", inode, page,
  277. page->index);
  278. flush_dcache_page(page);
  279. SetPageUptodate(page);
  280. unlock_page(page);
  281. page_cache_release(page);
  282. }
  283. rc = 0;
  284. out:
  285. kfree(pages);
  286. return rc;
  287. }
  288. /*
  289. * Get ref for the oldest snapc for an inode with dirty data... that is, the
  290. * only snap context we are allowed to write back.
  291. */
  292. static struct ceph_snap_context *get_oldest_context(struct inode *inode,
  293. u64 *snap_size)
  294. {
  295. struct ceph_inode_info *ci = ceph_inode(inode);
  296. struct ceph_snap_context *snapc = NULL;
  297. struct ceph_cap_snap *capsnap = NULL;
  298. spin_lock(&inode->i_lock);
  299. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  300. dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
  301. capsnap->context, capsnap->dirty_pages);
  302. if (capsnap->dirty_pages) {
  303. snapc = ceph_get_snap_context(capsnap->context);
  304. if (snap_size)
  305. *snap_size = capsnap->size;
  306. break;
  307. }
  308. }
  309. if (!snapc && ci->i_wrbuffer_ref_head) {
  310. snapc = ceph_get_snap_context(ci->i_head_snapc);
  311. dout(" head snapc %p has %d dirty pages\n",
  312. snapc, ci->i_wrbuffer_ref_head);
  313. }
  314. spin_unlock(&inode->i_lock);
  315. return snapc;
  316. }
  317. /*
  318. * Write a single page, but leave the page locked.
  319. *
  320. * If we get a write error, set the page error bit, but still adjust the
  321. * dirty page accounting (i.e., page is no longer dirty).
  322. */
  323. static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
  324. {
  325. struct inode *inode;
  326. struct ceph_inode_info *ci;
  327. struct ceph_fs_client *fsc;
  328. struct ceph_osd_client *osdc;
  329. loff_t page_off = page->index << PAGE_CACHE_SHIFT;
  330. int len = PAGE_CACHE_SIZE;
  331. loff_t i_size;
  332. int err = 0;
  333. struct ceph_snap_context *snapc, *oldest;
  334. u64 snap_size = 0;
  335. long writeback_stat;
  336. dout("writepage %p idx %lu\n", page, page->index);
  337. if (!page->mapping || !page->mapping->host) {
  338. dout("writepage %p - no mapping\n", page);
  339. return -EFAULT;
  340. }
  341. inode = page->mapping->host;
  342. ci = ceph_inode(inode);
  343. fsc = ceph_inode_to_client(inode);
  344. osdc = &fsc->client->osdc;
  345. /* verify this is a writeable snap context */
  346. snapc = (void *)page->private;
  347. if (snapc == NULL) {
  348. dout("writepage %p page %p not dirty?\n", inode, page);
  349. goto out;
  350. }
  351. oldest = get_oldest_context(inode, &snap_size);
  352. if (snapc->seq > oldest->seq) {
  353. dout("writepage %p page %p snapc %p not writeable - noop\n",
  354. inode, page, (void *)page->private);
  355. /* we should only noop if called by kswapd */
  356. WARN_ON((current->flags & PF_MEMALLOC) == 0);
  357. ceph_put_snap_context(oldest);
  358. goto out;
  359. }
  360. ceph_put_snap_context(oldest);
  361. /* is this a partial page at end of file? */
  362. if (snap_size)
  363. i_size = snap_size;
  364. else
  365. i_size = i_size_read(inode);
  366. if (i_size < page_off + len)
  367. len = i_size - page_off;
  368. dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
  369. inode, page, page->index, page_off, len, snapc);
  370. writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
  371. if (writeback_stat >
  372. CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
  373. set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
  374. set_page_writeback(page);
  375. err = ceph_osdc_writepages(osdc, ceph_vino(inode),
  376. &ci->i_layout, snapc,
  377. page_off, len,
  378. ci->i_truncate_seq, ci->i_truncate_size,
  379. &inode->i_mtime,
  380. &page, 1, 0, 0, true);
  381. if (err < 0) {
  382. dout("writepage setting page/mapping error %d %p\n", err, page);
  383. SetPageError(page);
  384. mapping_set_error(&inode->i_data, err);
  385. if (wbc)
  386. wbc->pages_skipped++;
  387. } else {
  388. dout("writepage cleaned page %p\n", page);
  389. err = 0; /* vfs expects us to return 0 */
  390. }
  391. page->private = 0;
  392. ClearPagePrivate(page);
  393. end_page_writeback(page);
  394. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  395. ceph_put_snap_context(snapc); /* page's reference */
  396. out:
  397. return err;
  398. }
  399. static int ceph_writepage(struct page *page, struct writeback_control *wbc)
  400. {
  401. int err;
  402. struct inode *inode = page->mapping->host;
  403. BUG_ON(!inode);
  404. ihold(inode);
  405. err = writepage_nounlock(page, wbc);
  406. unlock_page(page);
  407. iput(inode);
  408. return err;
  409. }
  410. /*
  411. * lame release_pages helper. release_pages() isn't exported to
  412. * modules.
  413. */
  414. static void ceph_release_pages(struct page **pages, int num)
  415. {
  416. struct pagevec pvec;
  417. int i;
  418. pagevec_init(&pvec, 0);
  419. for (i = 0; i < num; i++) {
  420. if (pagevec_add(&pvec, pages[i]) == 0)
  421. pagevec_release(&pvec);
  422. }
  423. pagevec_release(&pvec);
  424. }
  425. /*
  426. * async writeback completion handler.
  427. *
  428. * If we get an error, set the mapping error bit, but not the individual
  429. * page error bits.
  430. */
  431. static void writepages_finish(struct ceph_osd_request *req,
  432. struct ceph_msg *msg)
  433. {
  434. struct inode *inode = req->r_inode;
  435. struct ceph_osd_reply_head *replyhead;
  436. struct ceph_osd_op *op;
  437. struct ceph_inode_info *ci = ceph_inode(inode);
  438. unsigned wrote;
  439. struct page *page;
  440. int i;
  441. struct ceph_snap_context *snapc = req->r_snapc;
  442. struct address_space *mapping = inode->i_mapping;
  443. __s32 rc = -EIO;
  444. u64 bytes = 0;
  445. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  446. long writeback_stat;
  447. unsigned issued = ceph_caps_issued(ci);
  448. /* parse reply */
  449. replyhead = msg->front.iov_base;
  450. WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
  451. op = (void *)(replyhead + 1);
  452. rc = le32_to_cpu(replyhead->result);
  453. bytes = le64_to_cpu(op->extent.length);
  454. if (rc >= 0) {
  455. /*
  456. * Assume we wrote the pages we originally sent. The
  457. * osd might reply with fewer pages if our writeback
  458. * raced with a truncation and was adjusted at the osd,
  459. * so don't believe the reply.
  460. */
  461. wrote = req->r_num_pages;
  462. } else {
  463. wrote = 0;
  464. mapping_set_error(mapping, rc);
  465. }
  466. dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
  467. inode, rc, bytes, wrote);
  468. /* clean all pages */
  469. for (i = 0; i < req->r_num_pages; i++) {
  470. page = req->r_pages[i];
  471. BUG_ON(!page);
  472. WARN_ON(!PageUptodate(page));
  473. writeback_stat =
  474. atomic_long_dec_return(&fsc->writeback_count);
  475. if (writeback_stat <
  476. CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
  477. clear_bdi_congested(&fsc->backing_dev_info,
  478. BLK_RW_ASYNC);
  479. ceph_put_snap_context((void *)page->private);
  480. page->private = 0;
  481. ClearPagePrivate(page);
  482. dout("unlocking %d %p\n", i, page);
  483. end_page_writeback(page);
  484. /*
  485. * We lost the cache cap, need to truncate the page before
  486. * it is unlocked, otherwise we'd truncate it later in the
  487. * page truncation thread, possibly losing some data that
  488. * raced its way in
  489. */
  490. if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
  491. generic_error_remove_page(inode->i_mapping, page);
  492. unlock_page(page);
  493. }
  494. dout("%p wrote+cleaned %d pages\n", inode, wrote);
  495. ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc);
  496. ceph_release_pages(req->r_pages, req->r_num_pages);
  497. if (req->r_pages_from_pool)
  498. mempool_free(req->r_pages,
  499. ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
  500. else
  501. kfree(req->r_pages);
  502. ceph_osdc_put_request(req);
  503. }
  504. /*
  505. * allocate a page vec, either directly, or if necessary, via a the
  506. * mempool. we avoid the mempool if we can because req->r_num_pages
  507. * may be less than the maximum write size.
  508. */
  509. static void alloc_page_vec(struct ceph_fs_client *fsc,
  510. struct ceph_osd_request *req)
  511. {
  512. req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages,
  513. GFP_NOFS);
  514. if (!req->r_pages) {
  515. req->r_pages = mempool_alloc(fsc->wb_pagevec_pool, GFP_NOFS);
  516. req->r_pages_from_pool = 1;
  517. WARN_ON(!req->r_pages);
  518. }
  519. }
  520. /*
  521. * initiate async writeback
  522. */
  523. static int ceph_writepages_start(struct address_space *mapping,
  524. struct writeback_control *wbc)
  525. {
  526. struct inode *inode = mapping->host;
  527. struct ceph_inode_info *ci = ceph_inode(inode);
  528. struct ceph_fs_client *fsc;
  529. pgoff_t index, start, end;
  530. int range_whole = 0;
  531. int should_loop = 1;
  532. pgoff_t max_pages = 0, max_pages_ever = 0;
  533. struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
  534. struct pagevec pvec;
  535. int done = 0;
  536. int rc = 0;
  537. unsigned wsize = 1 << inode->i_blkbits;
  538. struct ceph_osd_request *req = NULL;
  539. int do_sync;
  540. u64 snap_size = 0;
  541. /*
  542. * Include a 'sync' in the OSD request if this is a data
  543. * integrity write (e.g., O_SYNC write or fsync()), or if our
  544. * cap is being revoked.
  545. */
  546. do_sync = wbc->sync_mode == WB_SYNC_ALL;
  547. if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
  548. do_sync = 1;
  549. dout("writepages_start %p dosync=%d (mode=%s)\n",
  550. inode, do_sync,
  551. wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
  552. (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
  553. fsc = ceph_inode_to_client(inode);
  554. if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) {
  555. pr_warning("writepage_start %p on forced umount\n", inode);
  556. return -EIO; /* we're in a forced umount, don't write! */
  557. }
  558. if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
  559. wsize = fsc->mount_options->wsize;
  560. if (wsize < PAGE_CACHE_SIZE)
  561. wsize = PAGE_CACHE_SIZE;
  562. max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
  563. pagevec_init(&pvec, 0);
  564. /* where to start/end? */
  565. if (wbc->range_cyclic) {
  566. start = mapping->writeback_index; /* Start from prev offset */
  567. end = -1;
  568. dout(" cyclic, start at %lu\n", start);
  569. } else {
  570. start = wbc->range_start >> PAGE_CACHE_SHIFT;
  571. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  572. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  573. range_whole = 1;
  574. should_loop = 0;
  575. dout(" not cyclic, %lu to %lu\n", start, end);
  576. }
  577. index = start;
  578. retry:
  579. /* find oldest snap context with dirty data */
  580. ceph_put_snap_context(snapc);
  581. snapc = get_oldest_context(inode, &snap_size);
  582. if (!snapc) {
  583. /* hmm, why does writepages get called when there
  584. is no dirty data? */
  585. dout(" no snap context with dirty data?\n");
  586. goto out;
  587. }
  588. dout(" oldest snapc is %p seq %lld (%d snaps)\n",
  589. snapc, snapc->seq, snapc->num_snaps);
  590. if (last_snapc && snapc != last_snapc) {
  591. /* if we switched to a newer snapc, restart our scan at the
  592. * start of the original file range. */
  593. dout(" snapc differs from last pass, restarting at %lu\n",
  594. index);
  595. index = start;
  596. }
  597. last_snapc = snapc;
  598. while (!done && index <= end) {
  599. unsigned i;
  600. int first;
  601. pgoff_t next;
  602. int pvec_pages, locked_pages;
  603. struct page *page;
  604. int want;
  605. u64 offset, len;
  606. struct ceph_osd_request_head *reqhead;
  607. struct ceph_osd_op *op;
  608. long writeback_stat;
  609. next = 0;
  610. locked_pages = 0;
  611. max_pages = max_pages_ever;
  612. get_more_pages:
  613. first = -1;
  614. want = min(end - index,
  615. min((pgoff_t)PAGEVEC_SIZE,
  616. max_pages - (pgoff_t)locked_pages) - 1)
  617. + 1;
  618. pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  619. PAGECACHE_TAG_DIRTY,
  620. want);
  621. dout("pagevec_lookup_tag got %d\n", pvec_pages);
  622. if (!pvec_pages && !locked_pages)
  623. break;
  624. for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
  625. page = pvec.pages[i];
  626. dout("? %p idx %lu\n", page, page->index);
  627. if (locked_pages == 0)
  628. lock_page(page); /* first page */
  629. else if (!trylock_page(page))
  630. break;
  631. /* only dirty pages, or our accounting breaks */
  632. if (unlikely(!PageDirty(page)) ||
  633. unlikely(page->mapping != mapping)) {
  634. dout("!dirty or !mapping %p\n", page);
  635. unlock_page(page);
  636. break;
  637. }
  638. if (!wbc->range_cyclic && page->index > end) {
  639. dout("end of range %p\n", page);
  640. done = 1;
  641. unlock_page(page);
  642. break;
  643. }
  644. if (next && (page->index != next)) {
  645. dout("not consecutive %p\n", page);
  646. unlock_page(page);
  647. break;
  648. }
  649. if (wbc->sync_mode != WB_SYNC_NONE) {
  650. dout("waiting on writeback %p\n", page);
  651. wait_on_page_writeback(page);
  652. }
  653. if ((snap_size && page_offset(page) > snap_size) ||
  654. (!snap_size &&
  655. page_offset(page) > i_size_read(inode))) {
  656. dout("%p page eof %llu\n", page, snap_size ?
  657. snap_size : i_size_read(inode));
  658. done = 1;
  659. unlock_page(page);
  660. break;
  661. }
  662. if (PageWriteback(page)) {
  663. dout("%p under writeback\n", page);
  664. unlock_page(page);
  665. break;
  666. }
  667. /* only if matching snap context */
  668. pgsnapc = (void *)page->private;
  669. if (pgsnapc->seq > snapc->seq) {
  670. dout("page snapc %p %lld > oldest %p %lld\n",
  671. pgsnapc, pgsnapc->seq, snapc, snapc->seq);
  672. unlock_page(page);
  673. if (!locked_pages)
  674. continue; /* keep looking for snap */
  675. break;
  676. }
  677. if (!clear_page_dirty_for_io(page)) {
  678. dout("%p !clear_page_dirty_for_io\n", page);
  679. unlock_page(page);
  680. break;
  681. }
  682. /* ok */
  683. if (locked_pages == 0) {
  684. /* prepare async write request */
  685. offset = (unsigned long long)page->index
  686. << PAGE_CACHE_SHIFT;
  687. len = wsize;
  688. req = ceph_osdc_new_request(&fsc->client->osdc,
  689. &ci->i_layout,
  690. ceph_vino(inode),
  691. offset, &len,
  692. CEPH_OSD_OP_WRITE,
  693. CEPH_OSD_FLAG_WRITE |
  694. CEPH_OSD_FLAG_ONDISK,
  695. snapc, do_sync,
  696. ci->i_truncate_seq,
  697. ci->i_truncate_size,
  698. &inode->i_mtime, true, 1, 0);
  699. if (!req) {
  700. rc = -ENOMEM;
  701. unlock_page(page);
  702. break;
  703. }
  704. max_pages = req->r_num_pages;
  705. alloc_page_vec(fsc, req);
  706. req->r_callback = writepages_finish;
  707. req->r_inode = inode;
  708. }
  709. /* note position of first page in pvec */
  710. if (first < 0)
  711. first = i;
  712. dout("%p will write page %p idx %lu\n",
  713. inode, page, page->index);
  714. writeback_stat =
  715. atomic_long_inc_return(&fsc->writeback_count);
  716. if (writeback_stat > CONGESTION_ON_THRESH(
  717. fsc->mount_options->congestion_kb)) {
  718. set_bdi_congested(&fsc->backing_dev_info,
  719. BLK_RW_ASYNC);
  720. }
  721. set_page_writeback(page);
  722. req->r_pages[locked_pages] = page;
  723. locked_pages++;
  724. next = page->index + 1;
  725. }
  726. /* did we get anything? */
  727. if (!locked_pages)
  728. goto release_pvec_pages;
  729. if (i) {
  730. int j;
  731. BUG_ON(!locked_pages || first < 0);
  732. if (pvec_pages && i == pvec_pages &&
  733. locked_pages < max_pages) {
  734. dout("reached end pvec, trying for more\n");
  735. pagevec_reinit(&pvec);
  736. goto get_more_pages;
  737. }
  738. /* shift unused pages over in the pvec... we
  739. * will need to release them below. */
  740. for (j = i; j < pvec_pages; j++) {
  741. dout(" pvec leftover page %p\n",
  742. pvec.pages[j]);
  743. pvec.pages[j-i+first] = pvec.pages[j];
  744. }
  745. pvec.nr -= i-first;
  746. }
  747. /* submit the write */
  748. offset = req->r_pages[0]->index << PAGE_CACHE_SHIFT;
  749. len = min((snap_size ? snap_size : i_size_read(inode)) - offset,
  750. (u64)locked_pages << PAGE_CACHE_SHIFT);
  751. dout("writepages got %d pages at %llu~%llu\n",
  752. locked_pages, offset, len);
  753. /* revise final length, page count */
  754. req->r_num_pages = locked_pages;
  755. reqhead = req->r_request->front.iov_base;
  756. op = (void *)(reqhead + 1);
  757. op->extent.length = cpu_to_le64(len);
  758. op->payload_len = cpu_to_le32(len);
  759. req->r_request->hdr.data_len = cpu_to_le32(len);
  760. rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
  761. BUG_ON(rc);
  762. req = NULL;
  763. /* continue? */
  764. index = next;
  765. wbc->nr_to_write -= locked_pages;
  766. if (wbc->nr_to_write <= 0)
  767. done = 1;
  768. release_pvec_pages:
  769. dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
  770. pvec.nr ? pvec.pages[0] : NULL);
  771. pagevec_release(&pvec);
  772. if (locked_pages && !done)
  773. goto retry;
  774. }
  775. if (should_loop && !done) {
  776. /* more to do; loop back to beginning of file */
  777. dout("writepages looping back to beginning of file\n");
  778. should_loop = 0;
  779. index = 0;
  780. goto retry;
  781. }
  782. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  783. mapping->writeback_index = index;
  784. out:
  785. if (req)
  786. ceph_osdc_put_request(req);
  787. ceph_put_snap_context(snapc);
  788. dout("writepages done, rc = %d\n", rc);
  789. return rc;
  790. }
  791. /*
  792. * See if a given @snapc is either writeable, or already written.
  793. */
  794. static int context_is_writeable_or_written(struct inode *inode,
  795. struct ceph_snap_context *snapc)
  796. {
  797. struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
  798. int ret = !oldest || snapc->seq <= oldest->seq;
  799. ceph_put_snap_context(oldest);
  800. return ret;
  801. }
  802. /*
  803. * We are only allowed to write into/dirty the page if the page is
  804. * clean, or already dirty within the same snap context.
  805. *
  806. * called with page locked.
  807. * return success with page locked,
  808. * or any failure (incl -EAGAIN) with page unlocked.
  809. */
  810. static int ceph_update_writeable_page(struct file *file,
  811. loff_t pos, unsigned len,
  812. struct page *page)
  813. {
  814. struct inode *inode = file->f_dentry->d_inode;
  815. struct ceph_inode_info *ci = ceph_inode(inode);
  816. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  817. loff_t page_off = pos & PAGE_CACHE_MASK;
  818. int pos_in_page = pos & ~PAGE_CACHE_MASK;
  819. int end_in_page = pos_in_page + len;
  820. loff_t i_size;
  821. int r;
  822. struct ceph_snap_context *snapc, *oldest;
  823. retry_locked:
  824. /* writepages currently holds page lock, but if we change that later, */
  825. wait_on_page_writeback(page);
  826. /* check snap context */
  827. BUG_ON(!ci->i_snap_realm);
  828. down_read(&mdsc->snap_rwsem);
  829. BUG_ON(!ci->i_snap_realm->cached_context);
  830. snapc = (void *)page->private;
  831. if (snapc && snapc != ci->i_head_snapc) {
  832. /*
  833. * this page is already dirty in another (older) snap
  834. * context! is it writeable now?
  835. */
  836. oldest = get_oldest_context(inode, NULL);
  837. up_read(&mdsc->snap_rwsem);
  838. if (snapc->seq > oldest->seq) {
  839. ceph_put_snap_context(oldest);
  840. dout(" page %p snapc %p not current or oldest\n",
  841. page, snapc);
  842. /*
  843. * queue for writeback, and wait for snapc to
  844. * be writeable or written
  845. */
  846. snapc = ceph_get_snap_context(snapc);
  847. unlock_page(page);
  848. ceph_queue_writeback(inode);
  849. r = wait_event_interruptible(ci->i_cap_wq,
  850. context_is_writeable_or_written(inode, snapc));
  851. ceph_put_snap_context(snapc);
  852. if (r == -ERESTARTSYS)
  853. return r;
  854. return -EAGAIN;
  855. }
  856. ceph_put_snap_context(oldest);
  857. /* yay, writeable, do it now (without dropping page lock) */
  858. dout(" page %p snapc %p not current, but oldest\n",
  859. page, snapc);
  860. if (!clear_page_dirty_for_io(page))
  861. goto retry_locked;
  862. r = writepage_nounlock(page, NULL);
  863. if (r < 0)
  864. goto fail_nosnap;
  865. goto retry_locked;
  866. }
  867. if (PageUptodate(page)) {
  868. dout(" page %p already uptodate\n", page);
  869. return 0;
  870. }
  871. /* full page? */
  872. if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
  873. return 0;
  874. /* past end of file? */
  875. i_size = inode->i_size; /* caller holds i_mutex */
  876. if (i_size + len > inode->i_sb->s_maxbytes) {
  877. /* file is too big */
  878. r = -EINVAL;
  879. goto fail;
  880. }
  881. if (page_off >= i_size ||
  882. (pos_in_page == 0 && (pos+len) >= i_size &&
  883. end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
  884. dout(" zeroing %p 0 - %d and %d - %d\n",
  885. page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
  886. zero_user_segments(page,
  887. 0, pos_in_page,
  888. end_in_page, PAGE_CACHE_SIZE);
  889. return 0;
  890. }
  891. /* we need to read it. */
  892. up_read(&mdsc->snap_rwsem);
  893. r = readpage_nounlock(file, page);
  894. if (r < 0)
  895. goto fail_nosnap;
  896. goto retry_locked;
  897. fail:
  898. up_read(&mdsc->snap_rwsem);
  899. fail_nosnap:
  900. unlock_page(page);
  901. return r;
  902. }
  903. /*
  904. * We are only allowed to write into/dirty the page if the page is
  905. * clean, or already dirty within the same snap context.
  906. */
  907. static int ceph_write_begin(struct file *file, struct address_space *mapping,
  908. loff_t pos, unsigned len, unsigned flags,
  909. struct page **pagep, void **fsdata)
  910. {
  911. struct inode *inode = file->f_dentry->d_inode;
  912. struct page *page;
  913. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  914. int r;
  915. do {
  916. /* get a page */
  917. page = grab_cache_page_write_begin(mapping, index, 0);
  918. if (!page)
  919. return -ENOMEM;
  920. *pagep = page;
  921. dout("write_begin file %p inode %p page %p %d~%d\n", file,
  922. inode, page, (int)pos, (int)len);
  923. r = ceph_update_writeable_page(file, pos, len, page);
  924. } while (r == -EAGAIN);
  925. return r;
  926. }
  927. /*
  928. * we don't do anything in here that simple_write_end doesn't do
  929. * except adjust dirty page accounting and drop read lock on
  930. * mdsc->snap_rwsem.
  931. */
  932. static int ceph_write_end(struct file *file, struct address_space *mapping,
  933. loff_t pos, unsigned len, unsigned copied,
  934. struct page *page, void *fsdata)
  935. {
  936. struct inode *inode = file->f_dentry->d_inode;
  937. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  938. struct ceph_mds_client *mdsc = fsc->mdsc;
  939. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  940. int check_cap = 0;
  941. dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
  942. inode, page, (int)pos, (int)copied, (int)len);
  943. /* zero the stale part of the page if we did a short copy */
  944. if (copied < len)
  945. zero_user_segment(page, from+copied, len);
  946. /* did file size increase? */
  947. /* (no need for i_size_read(); we caller holds i_mutex */
  948. if (pos+copied > inode->i_size)
  949. check_cap = ceph_inode_set_size(inode, pos+copied);
  950. if (!PageUptodate(page))
  951. SetPageUptodate(page);
  952. set_page_dirty(page);
  953. unlock_page(page);
  954. up_read(&mdsc->snap_rwsem);
  955. page_cache_release(page);
  956. if (check_cap)
  957. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
  958. return copied;
  959. }
  960. /*
  961. * we set .direct_IO to indicate direct io is supported, but since we
  962. * intercept O_DIRECT reads and writes early, this function should
  963. * never get called.
  964. */
  965. static ssize_t ceph_direct_io(int rw, struct kiocb *iocb,
  966. const struct iovec *iov,
  967. loff_t pos, unsigned long nr_segs)
  968. {
  969. WARN_ON(1);
  970. return -EINVAL;
  971. }
  972. const struct address_space_operations ceph_aops = {
  973. .readpage = ceph_readpage,
  974. .readpages = ceph_readpages,
  975. .writepage = ceph_writepage,
  976. .writepages = ceph_writepages_start,
  977. .write_begin = ceph_write_begin,
  978. .write_end = ceph_write_end,
  979. .set_page_dirty = ceph_set_page_dirty,
  980. .invalidatepage = ceph_invalidatepage,
  981. .releasepage = ceph_releasepage,
  982. .direct_IO = ceph_direct_io,
  983. };
  984. /*
  985. * vm ops
  986. */
  987. /*
  988. * Reuse write_begin here for simplicity.
  989. */
  990. static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  991. {
  992. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  993. struct page *page = vmf->page;
  994. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  995. loff_t off = page->index << PAGE_CACHE_SHIFT;
  996. loff_t size, len;
  997. int ret;
  998. size = i_size_read(inode);
  999. if (off + PAGE_CACHE_SIZE <= size)
  1000. len = PAGE_CACHE_SIZE;
  1001. else
  1002. len = size & ~PAGE_CACHE_MASK;
  1003. dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode,
  1004. off, len, page, page->index);
  1005. lock_page(page);
  1006. ret = VM_FAULT_NOPAGE;
  1007. if ((off > size) ||
  1008. (page->mapping != inode->i_mapping))
  1009. goto out;
  1010. ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
  1011. if (ret == 0) {
  1012. /* success. we'll keep the page locked. */
  1013. set_page_dirty(page);
  1014. up_read(&mdsc->snap_rwsem);
  1015. ret = VM_FAULT_LOCKED;
  1016. } else {
  1017. if (ret == -ENOMEM)
  1018. ret = VM_FAULT_OOM;
  1019. else
  1020. ret = VM_FAULT_SIGBUS;
  1021. }
  1022. out:
  1023. dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret);
  1024. if (ret != VM_FAULT_LOCKED)
  1025. unlock_page(page);
  1026. return ret;
  1027. }
  1028. static struct vm_operations_struct ceph_vmops = {
  1029. .fault = filemap_fault,
  1030. .page_mkwrite = ceph_page_mkwrite,
  1031. };
  1032. int ceph_mmap(struct file *file, struct vm_area_struct *vma)
  1033. {
  1034. struct address_space *mapping = file->f_mapping;
  1035. if (!mapping->a_ops->readpage)
  1036. return -ENOEXEC;
  1037. file_accessed(file);
  1038. vma->vm_ops = &ceph_vmops;
  1039. vma->vm_flags |= VM_CAN_NONLINEAR;
  1040. return 0;
  1041. }