addr.c 34 KB

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