addr.c 35 KB

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