addr.c 31 KB

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