addr.c 36 KB

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