addr.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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(&ci->i_ceph_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(&ci->i_ceph_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. * Finish an async read(ahead) op.
  202. */
  203. static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg)
  204. {
  205. struct inode *inode = req->r_inode;
  206. struct ceph_osd_reply_head *replyhead;
  207. int rc, bytes;
  208. int i;
  209. /* parse reply */
  210. replyhead = msg->front.iov_base;
  211. WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
  212. rc = le32_to_cpu(replyhead->result);
  213. bytes = le32_to_cpu(msg->hdr.data_len);
  214. dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
  215. /* unlock all pages, zeroing any data we didn't read */
  216. for (i = 0; i < req->r_num_pages; i++, bytes -= PAGE_CACHE_SIZE) {
  217. struct page *page = req->r_pages[i];
  218. if (bytes < (int)PAGE_CACHE_SIZE) {
  219. /* zero (remainder of) page */
  220. int s = bytes < 0 ? 0 : bytes;
  221. zero_user_segment(page, s, PAGE_CACHE_SIZE);
  222. }
  223. dout("finish_read %p uptodate %p idx %lu\n", inode, page,
  224. page->index);
  225. flush_dcache_page(page);
  226. SetPageUptodate(page);
  227. unlock_page(page);
  228. page_cache_release(page);
  229. }
  230. kfree(req->r_pages);
  231. }
  232. /*
  233. * start an async read(ahead) operation. return nr_pages we submitted
  234. * a read for on success, or negative error code.
  235. */
  236. static int start_read(struct inode *inode, struct list_head *page_list, int max)
  237. {
  238. struct ceph_osd_client *osdc =
  239. &ceph_inode_to_client(inode)->client->osdc;
  240. struct ceph_inode_info *ci = ceph_inode(inode);
  241. struct page *page = list_entry(page_list->prev, struct page, lru);
  242. struct ceph_osd_request *req;
  243. u64 off;
  244. u64 len;
  245. int i;
  246. struct page **pages;
  247. pgoff_t next_index;
  248. int nr_pages = 0;
  249. int ret;
  250. off = page->index << PAGE_CACHE_SHIFT;
  251. /* count pages */
  252. next_index = page->index;
  253. list_for_each_entry_reverse(page, page_list, lru) {
  254. if (page->index != next_index)
  255. break;
  256. nr_pages++;
  257. next_index++;
  258. if (max && nr_pages == max)
  259. break;
  260. }
  261. len = nr_pages << PAGE_CACHE_SHIFT;
  262. dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
  263. off, len);
  264. req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode),
  265. off, &len,
  266. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  267. NULL, 0,
  268. ci->i_truncate_seq, ci->i_truncate_size,
  269. NULL, false, 1, 0);
  270. if (!req)
  271. return -ENOMEM;
  272. /* build page vector */
  273. nr_pages = len >> PAGE_CACHE_SHIFT;
  274. pages = kmalloc(sizeof(*pages) * nr_pages, GFP_NOFS);
  275. ret = -ENOMEM;
  276. if (!pages)
  277. goto out;
  278. for (i = 0; i < nr_pages; ++i) {
  279. page = list_entry(page_list->prev, struct page, lru);
  280. BUG_ON(PageLocked(page));
  281. list_del(&page->lru);
  282. dout("start_read %p adding %p idx %lu\n", inode, page,
  283. page->index);
  284. if (add_to_page_cache_lru(page, &inode->i_data, page->index,
  285. GFP_NOFS)) {
  286. page_cache_release(page);
  287. dout("start_read %p add_to_page_cache failed %p\n",
  288. inode, page);
  289. nr_pages = i;
  290. goto out_pages;
  291. }
  292. pages[i] = page;
  293. }
  294. req->r_pages = pages;
  295. req->r_num_pages = nr_pages;
  296. req->r_callback = finish_read;
  297. req->r_inode = inode;
  298. dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
  299. ret = ceph_osdc_start_request(osdc, req, false);
  300. if (ret < 0)
  301. goto out_pages;
  302. ceph_osdc_put_request(req);
  303. return nr_pages;
  304. out_pages:
  305. ceph_release_page_vector(pages, nr_pages);
  306. out:
  307. ceph_osdc_put_request(req);
  308. return ret;
  309. }
  310. /*
  311. * Read multiple pages. Leave pages we don't read + unlock in page_list;
  312. * the caller (VM) cleans them up.
  313. */
  314. static int ceph_readpages(struct file *file, struct address_space *mapping,
  315. struct list_head *page_list, unsigned nr_pages)
  316. {
  317. struct inode *inode = file->f_dentry->d_inode;
  318. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  319. int rc = 0;
  320. int max = 0;
  321. if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE)
  322. max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1)
  323. >> PAGE_SHIFT;
  324. dout("readpages %p file %p nr_pages %d max %d\n", inode, file, nr_pages,
  325. max);
  326. while (!list_empty(page_list)) {
  327. rc = start_read(inode, page_list, max);
  328. if (rc < 0)
  329. goto out;
  330. BUG_ON(rc == 0);
  331. }
  332. out:
  333. dout("readpages %p file %p ret %d\n", inode, file, rc);
  334. return rc;
  335. }
  336. /*
  337. * Get ref for the oldest snapc for an inode with dirty data... that is, the
  338. * only snap context we are allowed to write back.
  339. */
  340. static struct ceph_snap_context *get_oldest_context(struct inode *inode,
  341. u64 *snap_size)
  342. {
  343. struct ceph_inode_info *ci = ceph_inode(inode);
  344. struct ceph_snap_context *snapc = NULL;
  345. struct ceph_cap_snap *capsnap = NULL;
  346. spin_lock(&ci->i_ceph_lock);
  347. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  348. dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
  349. capsnap->context, capsnap->dirty_pages);
  350. if (capsnap->dirty_pages) {
  351. snapc = ceph_get_snap_context(capsnap->context);
  352. if (snap_size)
  353. *snap_size = capsnap->size;
  354. break;
  355. }
  356. }
  357. if (!snapc && ci->i_wrbuffer_ref_head) {
  358. snapc = ceph_get_snap_context(ci->i_head_snapc);
  359. dout(" head snapc %p has %d dirty pages\n",
  360. snapc, ci->i_wrbuffer_ref_head);
  361. }
  362. spin_unlock(&ci->i_ceph_lock);
  363. return snapc;
  364. }
  365. /*
  366. * Write a single page, but leave the page locked.
  367. *
  368. * If we get a write error, set the page error bit, but still adjust the
  369. * dirty page accounting (i.e., page is no longer dirty).
  370. */
  371. static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
  372. {
  373. struct inode *inode;
  374. struct ceph_inode_info *ci;
  375. struct ceph_fs_client *fsc;
  376. struct ceph_osd_client *osdc;
  377. loff_t page_off = page->index << PAGE_CACHE_SHIFT;
  378. int len = PAGE_CACHE_SIZE;
  379. loff_t i_size;
  380. int err = 0;
  381. struct ceph_snap_context *snapc, *oldest;
  382. u64 snap_size = 0;
  383. long writeback_stat;
  384. dout("writepage %p idx %lu\n", page, page->index);
  385. if (!page->mapping || !page->mapping->host) {
  386. dout("writepage %p - no mapping\n", page);
  387. return -EFAULT;
  388. }
  389. inode = page->mapping->host;
  390. ci = ceph_inode(inode);
  391. fsc = ceph_inode_to_client(inode);
  392. osdc = &fsc->client->osdc;
  393. /* verify this is a writeable snap context */
  394. snapc = (void *)page->private;
  395. if (snapc == NULL) {
  396. dout("writepage %p page %p not dirty?\n", inode, page);
  397. goto out;
  398. }
  399. oldest = get_oldest_context(inode, &snap_size);
  400. if (snapc->seq > oldest->seq) {
  401. dout("writepage %p page %p snapc %p not writeable - noop\n",
  402. inode, page, (void *)page->private);
  403. /* we should only noop if called by kswapd */
  404. WARN_ON((current->flags & PF_MEMALLOC) == 0);
  405. ceph_put_snap_context(oldest);
  406. goto out;
  407. }
  408. ceph_put_snap_context(oldest);
  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. dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
  417. inode, page, page->index, page_off, len, snapc);
  418. writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
  419. if (writeback_stat >
  420. CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
  421. set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
  422. set_page_writeback(page);
  423. err = ceph_osdc_writepages(osdc, ceph_vino(inode),
  424. &ci->i_layout, snapc,
  425. page_off, len,
  426. ci->i_truncate_seq, ci->i_truncate_size,
  427. &inode->i_mtime,
  428. &page, 1, 0, 0, true);
  429. if (err < 0) {
  430. dout("writepage setting page/mapping error %d %p\n", err, page);
  431. SetPageError(page);
  432. mapping_set_error(&inode->i_data, err);
  433. if (wbc)
  434. wbc->pages_skipped++;
  435. } else {
  436. dout("writepage cleaned page %p\n", page);
  437. err = 0; /* vfs expects us to return 0 */
  438. }
  439. page->private = 0;
  440. ClearPagePrivate(page);
  441. end_page_writeback(page);
  442. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  443. ceph_put_snap_context(snapc); /* page's reference */
  444. out:
  445. return err;
  446. }
  447. static int ceph_writepage(struct page *page, struct writeback_control *wbc)
  448. {
  449. int err;
  450. struct inode *inode = page->mapping->host;
  451. BUG_ON(!inode);
  452. ihold(inode);
  453. err = writepage_nounlock(page, wbc);
  454. unlock_page(page);
  455. iput(inode);
  456. return err;
  457. }
  458. /*
  459. * lame release_pages helper. release_pages() isn't exported to
  460. * modules.
  461. */
  462. static void ceph_release_pages(struct page **pages, int num)
  463. {
  464. struct pagevec pvec;
  465. int i;
  466. pagevec_init(&pvec, 0);
  467. for (i = 0; i < num; i++) {
  468. if (pagevec_add(&pvec, pages[i]) == 0)
  469. pagevec_release(&pvec);
  470. }
  471. pagevec_release(&pvec);
  472. }
  473. /*
  474. * async writeback completion handler.
  475. *
  476. * If we get an error, set the mapping error bit, but not the individual
  477. * page error bits.
  478. */
  479. static void writepages_finish(struct ceph_osd_request *req,
  480. struct ceph_msg *msg)
  481. {
  482. struct inode *inode = req->r_inode;
  483. struct ceph_osd_reply_head *replyhead;
  484. struct ceph_osd_op *op;
  485. struct ceph_inode_info *ci = ceph_inode(inode);
  486. unsigned wrote;
  487. struct page *page;
  488. int i;
  489. struct ceph_snap_context *snapc = req->r_snapc;
  490. struct address_space *mapping = inode->i_mapping;
  491. __s32 rc = -EIO;
  492. u64 bytes = 0;
  493. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  494. long writeback_stat;
  495. unsigned issued = ceph_caps_issued(ci);
  496. /* parse reply */
  497. replyhead = msg->front.iov_base;
  498. WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
  499. op = (void *)(replyhead + 1);
  500. rc = le32_to_cpu(replyhead->result);
  501. bytes = le64_to_cpu(op->extent.length);
  502. if (rc >= 0) {
  503. /*
  504. * Assume we wrote the pages we originally sent. The
  505. * osd might reply with fewer pages if our writeback
  506. * raced with a truncation and was adjusted at the osd,
  507. * so don't believe the reply.
  508. */
  509. wrote = req->r_num_pages;
  510. } else {
  511. wrote = 0;
  512. mapping_set_error(mapping, rc);
  513. }
  514. dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
  515. inode, rc, bytes, wrote);
  516. /* clean all pages */
  517. for (i = 0; i < req->r_num_pages; i++) {
  518. page = req->r_pages[i];
  519. BUG_ON(!page);
  520. WARN_ON(!PageUptodate(page));
  521. writeback_stat =
  522. atomic_long_dec_return(&fsc->writeback_count);
  523. if (writeback_stat <
  524. CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
  525. clear_bdi_congested(&fsc->backing_dev_info,
  526. BLK_RW_ASYNC);
  527. ceph_put_snap_context((void *)page->private);
  528. page->private = 0;
  529. ClearPagePrivate(page);
  530. dout("unlocking %d %p\n", i, page);
  531. end_page_writeback(page);
  532. /*
  533. * We lost the cache cap, need to truncate the page before
  534. * it is unlocked, otherwise we'd truncate it later in the
  535. * page truncation thread, possibly losing some data that
  536. * raced its way in
  537. */
  538. if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
  539. generic_error_remove_page(inode->i_mapping, page);
  540. unlock_page(page);
  541. }
  542. dout("%p wrote+cleaned %d pages\n", inode, wrote);
  543. ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc);
  544. ceph_release_pages(req->r_pages, req->r_num_pages);
  545. if (req->r_pages_from_pool)
  546. mempool_free(req->r_pages,
  547. ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
  548. else
  549. kfree(req->r_pages);
  550. ceph_osdc_put_request(req);
  551. }
  552. /*
  553. * allocate a page vec, either directly, or if necessary, via a the
  554. * mempool. we avoid the mempool if we can because req->r_num_pages
  555. * may be less than the maximum write size.
  556. */
  557. static void alloc_page_vec(struct ceph_fs_client *fsc,
  558. struct ceph_osd_request *req)
  559. {
  560. req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages,
  561. GFP_NOFS);
  562. if (!req->r_pages) {
  563. req->r_pages = mempool_alloc(fsc->wb_pagevec_pool, GFP_NOFS);
  564. req->r_pages_from_pool = 1;
  565. WARN_ON(!req->r_pages);
  566. }
  567. }
  568. /*
  569. * initiate async writeback
  570. */
  571. static int ceph_writepages_start(struct address_space *mapping,
  572. struct writeback_control *wbc)
  573. {
  574. struct inode *inode = mapping->host;
  575. struct ceph_inode_info *ci = ceph_inode(inode);
  576. struct ceph_fs_client *fsc;
  577. pgoff_t index, start, end;
  578. int range_whole = 0;
  579. int should_loop = 1;
  580. pgoff_t max_pages = 0, max_pages_ever = 0;
  581. struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
  582. struct pagevec pvec;
  583. int done = 0;
  584. int rc = 0;
  585. unsigned wsize = 1 << inode->i_blkbits;
  586. struct ceph_osd_request *req = NULL;
  587. int do_sync;
  588. u64 snap_size = 0;
  589. /*
  590. * Include a 'sync' in the OSD request if this is a data
  591. * integrity write (e.g., O_SYNC write or fsync()), or if our
  592. * cap is being revoked.
  593. */
  594. do_sync = wbc->sync_mode == WB_SYNC_ALL;
  595. if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
  596. do_sync = 1;
  597. dout("writepages_start %p dosync=%d (mode=%s)\n",
  598. inode, do_sync,
  599. wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
  600. (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
  601. fsc = ceph_inode_to_client(inode);
  602. if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) {
  603. pr_warning("writepage_start %p on forced umount\n", inode);
  604. return -EIO; /* we're in a forced umount, don't write! */
  605. }
  606. if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
  607. wsize = fsc->mount_options->wsize;
  608. if (wsize < PAGE_CACHE_SIZE)
  609. wsize = PAGE_CACHE_SIZE;
  610. max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
  611. pagevec_init(&pvec, 0);
  612. /* where to start/end? */
  613. if (wbc->range_cyclic) {
  614. start = mapping->writeback_index; /* Start from prev offset */
  615. end = -1;
  616. dout(" cyclic, start at %lu\n", start);
  617. } else {
  618. start = wbc->range_start >> PAGE_CACHE_SHIFT;
  619. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  620. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  621. range_whole = 1;
  622. should_loop = 0;
  623. dout(" not cyclic, %lu to %lu\n", start, end);
  624. }
  625. index = start;
  626. retry:
  627. /* find oldest snap context with dirty data */
  628. ceph_put_snap_context(snapc);
  629. snapc = get_oldest_context(inode, &snap_size);
  630. if (!snapc) {
  631. /* hmm, why does writepages get called when there
  632. is no dirty data? */
  633. dout(" no snap context with dirty data?\n");
  634. goto out;
  635. }
  636. dout(" oldest snapc is %p seq %lld (%d snaps)\n",
  637. snapc, snapc->seq, snapc->num_snaps);
  638. if (last_snapc && snapc != last_snapc) {
  639. /* if we switched to a newer snapc, restart our scan at the
  640. * start of the original file range. */
  641. dout(" snapc differs from last pass, restarting at %lu\n",
  642. index);
  643. index = start;
  644. }
  645. last_snapc = snapc;
  646. while (!done && index <= end) {
  647. unsigned i;
  648. int first;
  649. pgoff_t next;
  650. int pvec_pages, locked_pages;
  651. struct page *page;
  652. int want;
  653. u64 offset, len;
  654. struct ceph_osd_request_head *reqhead;
  655. struct ceph_osd_op *op;
  656. long writeback_stat;
  657. next = 0;
  658. locked_pages = 0;
  659. max_pages = max_pages_ever;
  660. get_more_pages:
  661. first = -1;
  662. want = min(end - index,
  663. min((pgoff_t)PAGEVEC_SIZE,
  664. max_pages - (pgoff_t)locked_pages) - 1)
  665. + 1;
  666. pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  667. PAGECACHE_TAG_DIRTY,
  668. want);
  669. dout("pagevec_lookup_tag got %d\n", pvec_pages);
  670. if (!pvec_pages && !locked_pages)
  671. break;
  672. for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
  673. page = pvec.pages[i];
  674. dout("? %p idx %lu\n", page, page->index);
  675. if (locked_pages == 0)
  676. lock_page(page); /* first page */
  677. else if (!trylock_page(page))
  678. break;
  679. /* only dirty pages, or our accounting breaks */
  680. if (unlikely(!PageDirty(page)) ||
  681. unlikely(page->mapping != mapping)) {
  682. dout("!dirty or !mapping %p\n", page);
  683. unlock_page(page);
  684. break;
  685. }
  686. if (!wbc->range_cyclic && page->index > end) {
  687. dout("end of range %p\n", page);
  688. done = 1;
  689. unlock_page(page);
  690. break;
  691. }
  692. if (next && (page->index != next)) {
  693. dout("not consecutive %p\n", page);
  694. unlock_page(page);
  695. break;
  696. }
  697. if (wbc->sync_mode != WB_SYNC_NONE) {
  698. dout("waiting on writeback %p\n", page);
  699. wait_on_page_writeback(page);
  700. }
  701. if ((snap_size && page_offset(page) > snap_size) ||
  702. (!snap_size &&
  703. page_offset(page) > i_size_read(inode))) {
  704. dout("%p page eof %llu\n", page, snap_size ?
  705. snap_size : i_size_read(inode));
  706. done = 1;
  707. unlock_page(page);
  708. break;
  709. }
  710. if (PageWriteback(page)) {
  711. dout("%p under writeback\n", page);
  712. unlock_page(page);
  713. break;
  714. }
  715. /* only if matching snap context */
  716. pgsnapc = (void *)page->private;
  717. if (pgsnapc->seq > snapc->seq) {
  718. dout("page snapc %p %lld > oldest %p %lld\n",
  719. pgsnapc, pgsnapc->seq, snapc, snapc->seq);
  720. unlock_page(page);
  721. if (!locked_pages)
  722. continue; /* keep looking for snap */
  723. break;
  724. }
  725. if (!clear_page_dirty_for_io(page)) {
  726. dout("%p !clear_page_dirty_for_io\n", page);
  727. unlock_page(page);
  728. break;
  729. }
  730. /* ok */
  731. if (locked_pages == 0) {
  732. /* prepare async write request */
  733. offset = (unsigned long long)page->index
  734. << PAGE_CACHE_SHIFT;
  735. len = wsize;
  736. req = ceph_osdc_new_request(&fsc->client->osdc,
  737. &ci->i_layout,
  738. ceph_vino(inode),
  739. offset, &len,
  740. CEPH_OSD_OP_WRITE,
  741. CEPH_OSD_FLAG_WRITE |
  742. CEPH_OSD_FLAG_ONDISK,
  743. snapc, do_sync,
  744. ci->i_truncate_seq,
  745. ci->i_truncate_size,
  746. &inode->i_mtime, true, 1, 0);
  747. if (!req) {
  748. rc = -ENOMEM;
  749. unlock_page(page);
  750. break;
  751. }
  752. max_pages = req->r_num_pages;
  753. alloc_page_vec(fsc, req);
  754. req->r_callback = writepages_finish;
  755. req->r_inode = inode;
  756. }
  757. /* note position of first page in pvec */
  758. if (first < 0)
  759. first = i;
  760. dout("%p will write page %p idx %lu\n",
  761. inode, page, page->index);
  762. writeback_stat =
  763. atomic_long_inc_return(&fsc->writeback_count);
  764. if (writeback_stat > CONGESTION_ON_THRESH(
  765. fsc->mount_options->congestion_kb)) {
  766. set_bdi_congested(&fsc->backing_dev_info,
  767. BLK_RW_ASYNC);
  768. }
  769. set_page_writeback(page);
  770. req->r_pages[locked_pages] = page;
  771. locked_pages++;
  772. next = page->index + 1;
  773. }
  774. /* did we get anything? */
  775. if (!locked_pages)
  776. goto release_pvec_pages;
  777. if (i) {
  778. int j;
  779. BUG_ON(!locked_pages || first < 0);
  780. if (pvec_pages && i == pvec_pages &&
  781. locked_pages < max_pages) {
  782. dout("reached end pvec, trying for more\n");
  783. pagevec_reinit(&pvec);
  784. goto get_more_pages;
  785. }
  786. /* shift unused pages over in the pvec... we
  787. * will need to release them below. */
  788. for (j = i; j < pvec_pages; j++) {
  789. dout(" pvec leftover page %p\n",
  790. pvec.pages[j]);
  791. pvec.pages[j-i+first] = pvec.pages[j];
  792. }
  793. pvec.nr -= i-first;
  794. }
  795. /* submit the write */
  796. offset = req->r_pages[0]->index << PAGE_CACHE_SHIFT;
  797. len = min((snap_size ? snap_size : i_size_read(inode)) - offset,
  798. (u64)locked_pages << PAGE_CACHE_SHIFT);
  799. dout("writepages got %d pages at %llu~%llu\n",
  800. locked_pages, offset, len);
  801. /* revise final length, page count */
  802. req->r_num_pages = locked_pages;
  803. reqhead = req->r_request->front.iov_base;
  804. op = (void *)(reqhead + 1);
  805. op->extent.length = cpu_to_le64(len);
  806. op->payload_len = cpu_to_le32(len);
  807. req->r_request->hdr.data_len = cpu_to_le32(len);
  808. rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
  809. BUG_ON(rc);
  810. req = NULL;
  811. /* continue? */
  812. index = next;
  813. wbc->nr_to_write -= locked_pages;
  814. if (wbc->nr_to_write <= 0)
  815. done = 1;
  816. release_pvec_pages:
  817. dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
  818. pvec.nr ? pvec.pages[0] : NULL);
  819. pagevec_release(&pvec);
  820. if (locked_pages && !done)
  821. goto retry;
  822. }
  823. if (should_loop && !done) {
  824. /* more to do; loop back to beginning of file */
  825. dout("writepages looping back to beginning of file\n");
  826. should_loop = 0;
  827. index = 0;
  828. goto retry;
  829. }
  830. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  831. mapping->writeback_index = index;
  832. out:
  833. if (req)
  834. ceph_osdc_put_request(req);
  835. ceph_put_snap_context(snapc);
  836. dout("writepages done, rc = %d\n", rc);
  837. return rc;
  838. }
  839. /*
  840. * See if a given @snapc is either writeable, or already written.
  841. */
  842. static int context_is_writeable_or_written(struct inode *inode,
  843. struct ceph_snap_context *snapc)
  844. {
  845. struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
  846. int ret = !oldest || snapc->seq <= oldest->seq;
  847. ceph_put_snap_context(oldest);
  848. return ret;
  849. }
  850. /*
  851. * We are only allowed to write into/dirty the page if the page is
  852. * clean, or already dirty within the same snap context.
  853. *
  854. * called with page locked.
  855. * return success with page locked,
  856. * or any failure (incl -EAGAIN) with page unlocked.
  857. */
  858. static int ceph_update_writeable_page(struct file *file,
  859. loff_t pos, unsigned len,
  860. struct page *page)
  861. {
  862. struct inode *inode = file->f_dentry->d_inode;
  863. struct ceph_inode_info *ci = ceph_inode(inode);
  864. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  865. loff_t page_off = pos & PAGE_CACHE_MASK;
  866. int pos_in_page = pos & ~PAGE_CACHE_MASK;
  867. int end_in_page = pos_in_page + len;
  868. loff_t i_size;
  869. int r;
  870. struct ceph_snap_context *snapc, *oldest;
  871. retry_locked:
  872. /* writepages currently holds page lock, but if we change that later, */
  873. wait_on_page_writeback(page);
  874. /* check snap context */
  875. BUG_ON(!ci->i_snap_realm);
  876. down_read(&mdsc->snap_rwsem);
  877. BUG_ON(!ci->i_snap_realm->cached_context);
  878. snapc = (void *)page->private;
  879. if (snapc && snapc != ci->i_head_snapc) {
  880. /*
  881. * this page is already dirty in another (older) snap
  882. * context! is it writeable now?
  883. */
  884. oldest = get_oldest_context(inode, NULL);
  885. up_read(&mdsc->snap_rwsem);
  886. if (snapc->seq > oldest->seq) {
  887. ceph_put_snap_context(oldest);
  888. dout(" page %p snapc %p not current or oldest\n",
  889. page, snapc);
  890. /*
  891. * queue for writeback, and wait for snapc to
  892. * be writeable or written
  893. */
  894. snapc = ceph_get_snap_context(snapc);
  895. unlock_page(page);
  896. ceph_queue_writeback(inode);
  897. r = wait_event_interruptible(ci->i_cap_wq,
  898. context_is_writeable_or_written(inode, snapc));
  899. ceph_put_snap_context(snapc);
  900. if (r == -ERESTARTSYS)
  901. return r;
  902. return -EAGAIN;
  903. }
  904. ceph_put_snap_context(oldest);
  905. /* yay, writeable, do it now (without dropping page lock) */
  906. dout(" page %p snapc %p not current, but oldest\n",
  907. page, snapc);
  908. if (!clear_page_dirty_for_io(page))
  909. goto retry_locked;
  910. r = writepage_nounlock(page, NULL);
  911. if (r < 0)
  912. goto fail_nosnap;
  913. goto retry_locked;
  914. }
  915. if (PageUptodate(page)) {
  916. dout(" page %p already uptodate\n", page);
  917. return 0;
  918. }
  919. /* full page? */
  920. if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
  921. return 0;
  922. /* past end of file? */
  923. i_size = inode->i_size; /* caller holds i_mutex */
  924. if (i_size + len > inode->i_sb->s_maxbytes) {
  925. /* file is too big */
  926. r = -EINVAL;
  927. goto fail;
  928. }
  929. if (page_off >= i_size ||
  930. (pos_in_page == 0 && (pos+len) >= i_size &&
  931. end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
  932. dout(" zeroing %p 0 - %d and %d - %d\n",
  933. page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
  934. zero_user_segments(page,
  935. 0, pos_in_page,
  936. end_in_page, PAGE_CACHE_SIZE);
  937. return 0;
  938. }
  939. /* we need to read it. */
  940. up_read(&mdsc->snap_rwsem);
  941. r = readpage_nounlock(file, page);
  942. if (r < 0)
  943. goto fail_nosnap;
  944. goto retry_locked;
  945. fail:
  946. up_read(&mdsc->snap_rwsem);
  947. fail_nosnap:
  948. unlock_page(page);
  949. return r;
  950. }
  951. /*
  952. * We are only allowed to write into/dirty the page if the page is
  953. * clean, or already dirty within the same snap context.
  954. */
  955. static int ceph_write_begin(struct file *file, struct address_space *mapping,
  956. loff_t pos, unsigned len, unsigned flags,
  957. struct page **pagep, void **fsdata)
  958. {
  959. struct inode *inode = file->f_dentry->d_inode;
  960. struct page *page;
  961. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  962. int r;
  963. do {
  964. /* get a page */
  965. page = grab_cache_page_write_begin(mapping, index, 0);
  966. if (!page)
  967. return -ENOMEM;
  968. *pagep = page;
  969. dout("write_begin file %p inode %p page %p %d~%d\n", file,
  970. inode, page, (int)pos, (int)len);
  971. r = ceph_update_writeable_page(file, pos, len, page);
  972. } while (r == -EAGAIN);
  973. return r;
  974. }
  975. /*
  976. * we don't do anything in here that simple_write_end doesn't do
  977. * except adjust dirty page accounting and drop read lock on
  978. * mdsc->snap_rwsem.
  979. */
  980. static int ceph_write_end(struct file *file, struct address_space *mapping,
  981. loff_t pos, unsigned len, unsigned copied,
  982. struct page *page, void *fsdata)
  983. {
  984. struct inode *inode = file->f_dentry->d_inode;
  985. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  986. struct ceph_mds_client *mdsc = fsc->mdsc;
  987. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  988. int check_cap = 0;
  989. dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
  990. inode, page, (int)pos, (int)copied, (int)len);
  991. /* zero the stale part of the page if we did a short copy */
  992. if (copied < len)
  993. zero_user_segment(page, from+copied, len);
  994. /* did file size increase? */
  995. /* (no need for i_size_read(); we caller holds i_mutex */
  996. if (pos+copied > inode->i_size)
  997. check_cap = ceph_inode_set_size(inode, pos+copied);
  998. if (!PageUptodate(page))
  999. SetPageUptodate(page);
  1000. set_page_dirty(page);
  1001. unlock_page(page);
  1002. up_read(&mdsc->snap_rwsem);
  1003. page_cache_release(page);
  1004. if (check_cap)
  1005. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
  1006. return copied;
  1007. }
  1008. /*
  1009. * we set .direct_IO to indicate direct io is supported, but since we
  1010. * intercept O_DIRECT reads and writes early, this function should
  1011. * never get called.
  1012. */
  1013. static ssize_t ceph_direct_io(int rw, struct kiocb *iocb,
  1014. const struct iovec *iov,
  1015. loff_t pos, unsigned long nr_segs)
  1016. {
  1017. WARN_ON(1);
  1018. return -EINVAL;
  1019. }
  1020. const struct address_space_operations ceph_aops = {
  1021. .readpage = ceph_readpage,
  1022. .readpages = ceph_readpages,
  1023. .writepage = ceph_writepage,
  1024. .writepages = ceph_writepages_start,
  1025. .write_begin = ceph_write_begin,
  1026. .write_end = ceph_write_end,
  1027. .set_page_dirty = ceph_set_page_dirty,
  1028. .invalidatepage = ceph_invalidatepage,
  1029. .releasepage = ceph_releasepage,
  1030. .direct_IO = ceph_direct_io,
  1031. };
  1032. /*
  1033. * vm ops
  1034. */
  1035. /*
  1036. * Reuse write_begin here for simplicity.
  1037. */
  1038. static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  1039. {
  1040. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  1041. struct page *page = vmf->page;
  1042. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  1043. loff_t off = page->index << PAGE_CACHE_SHIFT;
  1044. loff_t size, len;
  1045. int ret;
  1046. size = i_size_read(inode);
  1047. if (off + PAGE_CACHE_SIZE <= size)
  1048. len = PAGE_CACHE_SIZE;
  1049. else
  1050. len = size & ~PAGE_CACHE_MASK;
  1051. dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode,
  1052. off, len, page, page->index);
  1053. lock_page(page);
  1054. ret = VM_FAULT_NOPAGE;
  1055. if ((off > size) ||
  1056. (page->mapping != inode->i_mapping))
  1057. goto out;
  1058. ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
  1059. if (ret == 0) {
  1060. /* success. we'll keep the page locked. */
  1061. set_page_dirty(page);
  1062. up_read(&mdsc->snap_rwsem);
  1063. ret = VM_FAULT_LOCKED;
  1064. } else {
  1065. if (ret == -ENOMEM)
  1066. ret = VM_FAULT_OOM;
  1067. else
  1068. ret = VM_FAULT_SIGBUS;
  1069. }
  1070. out:
  1071. dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret);
  1072. if (ret != VM_FAULT_LOCKED)
  1073. unlock_page(page);
  1074. return ret;
  1075. }
  1076. static struct vm_operations_struct ceph_vmops = {
  1077. .fault = filemap_fault,
  1078. .page_mkwrite = ceph_page_mkwrite,
  1079. };
  1080. int ceph_mmap(struct file *file, struct vm_area_struct *vma)
  1081. {
  1082. struct address_space *mapping = file->f_mapping;
  1083. if (!mapping->a_ops->readpage)
  1084. return -ENOEXEC;
  1085. file_accessed(file);
  1086. vma->vm_ops = &ceph_vmops;
  1087. vma->vm_flags |= VM_CAN_NONLINEAR;
  1088. return 0;
  1089. }