addr.c 32 KB

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