addr.c 32 KB

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