addr.c 32 KB

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