addr.c 32 KB

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