addr.c 32 KB

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