addr.c 32 KB

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