addr.c 36 KB

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