addr.c 35 KB

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