addr.c 32 KB

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