addr.c 32 KB

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