addr.c 34 KB

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