addr.c 30 KB

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