addr.c 33 KB

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