addr.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. struct writeback_control *wbc = req->r_wbc;
  450. __s32 rc = -EIO;
  451. u64 bytes = 0;
  452. struct ceph_client *client = ceph_inode_to_client(inode);
  453. long writeback_stat;
  454. unsigned issued = __ceph_caps_issued(ci, NULL);
  455. /* parse reply */
  456. replyhead = msg->front.iov_base;
  457. WARN_ON(le32_to_cpu(replyhead->num_ops) == 0);
  458. op = (void *)(replyhead + 1);
  459. rc = le32_to_cpu(replyhead->result);
  460. bytes = le64_to_cpu(op->extent.length);
  461. if (rc >= 0) {
  462. /*
  463. * Assume we wrote the pages we originally sent. The
  464. * osd might reply with fewer pages if our writeback
  465. * raced with a truncation and was adjusted at the osd,
  466. * so don't believe the reply.
  467. */
  468. wrote = req->r_num_pages;
  469. } else {
  470. wrote = 0;
  471. mapping_set_error(mapping, rc);
  472. }
  473. dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
  474. inode, rc, bytes, wrote);
  475. /* clean all pages */
  476. for (i = 0; i < req->r_num_pages; i++) {
  477. page = req->r_pages[i];
  478. BUG_ON(!page);
  479. WARN_ON(!PageUptodate(page));
  480. writeback_stat =
  481. atomic_long_dec_return(&client->writeback_count);
  482. if (writeback_stat <
  483. CONGESTION_OFF_THRESH(client->mount_args->congestion_kb))
  484. clear_bdi_congested(&client->backing_dev_info,
  485. BLK_RW_ASYNC);
  486. if (i >= wrote) {
  487. dout("inode %p skipping page %p\n", inode, page);
  488. wbc->pages_skipped++;
  489. }
  490. ceph_put_snap_context((void *)page->private);
  491. page->private = 0;
  492. ClearPagePrivate(page);
  493. dout("unlocking %d %p\n", i, page);
  494. end_page_writeback(page);
  495. /*
  496. * We lost the cache cap, need to truncate the page before
  497. * it is unlocked, otherwise we'd truncate it later in the
  498. * page truncation thread, possibly losing some data that
  499. * raced its way in
  500. */
  501. if ((issued & CEPH_CAP_FILE_CACHE) == 0)
  502. generic_error_remove_page(inode->i_mapping, page);
  503. unlock_page(page);
  504. }
  505. dout("%p wrote+cleaned %d pages\n", inode, wrote);
  506. ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc);
  507. ceph_release_pages(req->r_pages, req->r_num_pages);
  508. if (req->r_pages_from_pool)
  509. mempool_free(req->r_pages,
  510. ceph_client(inode->i_sb)->wb_pagevec_pool);
  511. else
  512. kfree(req->r_pages);
  513. ceph_osdc_put_request(req);
  514. }
  515. /*
  516. * allocate a page vec, either directly, or if necessary, via a the
  517. * mempool. we avoid the mempool if we can because req->r_num_pages
  518. * may be less than the maximum write size.
  519. */
  520. static void alloc_page_vec(struct ceph_client *client,
  521. struct ceph_osd_request *req)
  522. {
  523. req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages,
  524. GFP_NOFS);
  525. if (!req->r_pages) {
  526. req->r_pages = mempool_alloc(client->wb_pagevec_pool, GFP_NOFS);
  527. req->r_pages_from_pool = 1;
  528. WARN_ON(!req->r_pages);
  529. }
  530. }
  531. /*
  532. * initiate async writeback
  533. */
  534. static int ceph_writepages_start(struct address_space *mapping,
  535. struct writeback_control *wbc)
  536. {
  537. struct inode *inode = mapping->host;
  538. struct backing_dev_info *bdi = mapping->backing_dev_info;
  539. struct ceph_inode_info *ci = ceph_inode(inode);
  540. struct ceph_client *client;
  541. pgoff_t index, start, end;
  542. int range_whole = 0;
  543. int should_loop = 1;
  544. pgoff_t max_pages = 0, max_pages_ever = 0;
  545. struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
  546. struct pagevec pvec;
  547. int done = 0;
  548. int rc = 0;
  549. unsigned wsize = 1 << inode->i_blkbits;
  550. struct ceph_osd_request *req = NULL;
  551. int do_sync;
  552. u64 snap_size = 0;
  553. /*
  554. * Include a 'sync' in the OSD request if this is a data
  555. * integrity write (e.g., O_SYNC write or fsync()), or if our
  556. * cap is being revoked.
  557. */
  558. do_sync = wbc->sync_mode == WB_SYNC_ALL;
  559. if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
  560. do_sync = 1;
  561. dout("writepages_start %p dosync=%d (mode=%s)\n",
  562. inode, do_sync,
  563. wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
  564. (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
  565. client = ceph_inode_to_client(inode);
  566. if (client->mount_state == CEPH_MOUNT_SHUTDOWN) {
  567. pr_warning("writepage_start %p on forced umount\n", inode);
  568. return -EIO; /* we're in a forced umount, don't write! */
  569. }
  570. if (client->mount_args->wsize && client->mount_args->wsize < wsize)
  571. wsize = client->mount_args->wsize;
  572. if (wsize < PAGE_CACHE_SIZE)
  573. wsize = PAGE_CACHE_SIZE;
  574. max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
  575. pagevec_init(&pvec, 0);
  576. /* ?? */
  577. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  578. dout(" writepages congested\n");
  579. wbc->encountered_congestion = 1;
  580. goto out_final;
  581. }
  582. /* where to start/end? */
  583. if (wbc->range_cyclic) {
  584. start = mapping->writeback_index; /* Start from prev offset */
  585. end = -1;
  586. dout(" cyclic, start at %lu\n", start);
  587. } else {
  588. start = wbc->range_start >> PAGE_CACHE_SHIFT;
  589. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  590. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  591. range_whole = 1;
  592. should_loop = 0;
  593. dout(" not cyclic, %lu to %lu\n", start, end);
  594. }
  595. index = start;
  596. retry:
  597. /* find oldest snap context with dirty data */
  598. ceph_put_snap_context(snapc);
  599. snapc = get_oldest_context(inode, &snap_size);
  600. if (!snapc) {
  601. /* hmm, why does writepages get called when there
  602. is no dirty data? */
  603. dout(" no snap context with dirty data?\n");
  604. goto out;
  605. }
  606. dout(" oldest snapc is %p seq %lld (%d snaps)\n",
  607. snapc, snapc->seq, snapc->num_snaps);
  608. if (last_snapc && snapc != last_snapc) {
  609. /* if we switched to a newer snapc, restart our scan at the
  610. * start of the original file range. */
  611. dout(" snapc differs from last pass, restarting at %lu\n",
  612. index);
  613. index = start;
  614. }
  615. last_snapc = snapc;
  616. while (!done && index <= end) {
  617. unsigned i;
  618. int first;
  619. pgoff_t next;
  620. int pvec_pages, locked_pages;
  621. struct page *page;
  622. int want;
  623. u64 offset, len;
  624. struct ceph_osd_request_head *reqhead;
  625. struct ceph_osd_op *op;
  626. long writeback_stat;
  627. next = 0;
  628. locked_pages = 0;
  629. max_pages = max_pages_ever;
  630. get_more_pages:
  631. first = -1;
  632. want = min(end - index,
  633. min((pgoff_t)PAGEVEC_SIZE,
  634. max_pages - (pgoff_t)locked_pages) - 1)
  635. + 1;
  636. pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  637. PAGECACHE_TAG_DIRTY,
  638. want);
  639. dout("pagevec_lookup_tag got %d\n", pvec_pages);
  640. if (!pvec_pages && !locked_pages)
  641. break;
  642. for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
  643. page = pvec.pages[i];
  644. dout("? %p idx %lu\n", page, page->index);
  645. if (locked_pages == 0)
  646. lock_page(page); /* first page */
  647. else if (!trylock_page(page))
  648. break;
  649. /* only dirty pages, or our accounting breaks */
  650. if (unlikely(!PageDirty(page)) ||
  651. unlikely(page->mapping != mapping)) {
  652. dout("!dirty or !mapping %p\n", page);
  653. unlock_page(page);
  654. break;
  655. }
  656. if (!wbc->range_cyclic && page->index > end) {
  657. dout("end of range %p\n", page);
  658. done = 1;
  659. unlock_page(page);
  660. break;
  661. }
  662. if (next && (page->index != next)) {
  663. dout("not consecutive %p\n", page);
  664. unlock_page(page);
  665. break;
  666. }
  667. if (wbc->sync_mode != WB_SYNC_NONE) {
  668. dout("waiting on writeback %p\n", page);
  669. wait_on_page_writeback(page);
  670. }
  671. if ((snap_size && page_offset(page) > snap_size) ||
  672. (!snap_size &&
  673. page_offset(page) > i_size_read(inode))) {
  674. dout("%p page eof %llu\n", page, snap_size ?
  675. snap_size : i_size_read(inode));
  676. done = 1;
  677. unlock_page(page);
  678. break;
  679. }
  680. if (PageWriteback(page)) {
  681. dout("%p under writeback\n", page);
  682. unlock_page(page);
  683. break;
  684. }
  685. /* only if matching snap context */
  686. pgsnapc = (void *)page->private;
  687. if (pgsnapc->seq > snapc->seq) {
  688. dout("page snapc %p %lld > oldest %p %lld\n",
  689. pgsnapc, pgsnapc->seq, snapc, snapc->seq);
  690. unlock_page(page);
  691. if (!locked_pages)
  692. continue; /* keep looking for snap */
  693. break;
  694. }
  695. if (!clear_page_dirty_for_io(page)) {
  696. dout("%p !clear_page_dirty_for_io\n", page);
  697. unlock_page(page);
  698. break;
  699. }
  700. /* ok */
  701. if (locked_pages == 0) {
  702. /* prepare async write request */
  703. offset = page->index << PAGE_CACHE_SHIFT;
  704. len = wsize;
  705. req = ceph_osdc_new_request(&client->osdc,
  706. &ci->i_layout,
  707. ceph_vino(inode),
  708. offset, &len,
  709. CEPH_OSD_OP_WRITE,
  710. CEPH_OSD_FLAG_WRITE |
  711. CEPH_OSD_FLAG_ONDISK,
  712. snapc, do_sync,
  713. ci->i_truncate_seq,
  714. ci->i_truncate_size,
  715. &inode->i_mtime, true, 1);
  716. max_pages = req->r_num_pages;
  717. alloc_page_vec(client, req);
  718. req->r_callback = writepages_finish;
  719. req->r_inode = inode;
  720. req->r_wbc = wbc;
  721. }
  722. /* note position of first page in pvec */
  723. if (first < 0)
  724. first = i;
  725. dout("%p will write page %p idx %lu\n",
  726. inode, page, page->index);
  727. writeback_stat = atomic_long_inc_return(&client->writeback_count);
  728. if (writeback_stat > CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) {
  729. set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC);
  730. }
  731. set_page_writeback(page);
  732. req->r_pages[locked_pages] = page;
  733. locked_pages++;
  734. next = page->index + 1;
  735. }
  736. /* did we get anything? */
  737. if (!locked_pages)
  738. goto release_pvec_pages;
  739. if (i) {
  740. int j;
  741. BUG_ON(!locked_pages || first < 0);
  742. if (pvec_pages && i == pvec_pages &&
  743. locked_pages < max_pages) {
  744. dout("reached end pvec, trying for more\n");
  745. pagevec_reinit(&pvec);
  746. goto get_more_pages;
  747. }
  748. /* shift unused pages over in the pvec... we
  749. * will need to release them below. */
  750. for (j = i; j < pvec_pages; j++) {
  751. dout(" pvec leftover page %p\n",
  752. pvec.pages[j]);
  753. pvec.pages[j-i+first] = pvec.pages[j];
  754. }
  755. pvec.nr -= i-first;
  756. }
  757. /* submit the write */
  758. offset = req->r_pages[0]->index << PAGE_CACHE_SHIFT;
  759. len = min((snap_size ? snap_size : i_size_read(inode)) - offset,
  760. (u64)locked_pages << PAGE_CACHE_SHIFT);
  761. dout("writepages got %d pages at %llu~%llu\n",
  762. locked_pages, offset, len);
  763. /* revise final length, page count */
  764. req->r_num_pages = locked_pages;
  765. reqhead = req->r_request->front.iov_base;
  766. op = (void *)(reqhead + 1);
  767. op->extent.length = cpu_to_le64(len);
  768. op->payload_len = cpu_to_le32(len);
  769. req->r_request->hdr.data_len = cpu_to_le32(len);
  770. ceph_osdc_start_request(&client->osdc, req, true);
  771. req = NULL;
  772. /* continue? */
  773. index = next;
  774. wbc->nr_to_write -= locked_pages;
  775. if (wbc->nr_to_write <= 0)
  776. done = 1;
  777. release_pvec_pages:
  778. dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
  779. pvec.nr ? pvec.pages[0] : NULL);
  780. pagevec_release(&pvec);
  781. if (locked_pages && !done)
  782. goto retry;
  783. }
  784. if (should_loop && !done) {
  785. /* more to do; loop back to beginning of file */
  786. dout("writepages looping back to beginning of file\n");
  787. should_loop = 0;
  788. index = 0;
  789. goto retry;
  790. }
  791. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  792. mapping->writeback_index = index;
  793. out:
  794. if (req)
  795. ceph_osdc_put_request(req);
  796. if (rc > 0)
  797. rc = 0; /* vfs expects us to return 0 */
  798. ceph_put_snap_context(snapc);
  799. dout("writepages done, rc = %d\n", rc);
  800. out_final:
  801. return rc;
  802. }
  803. /*
  804. * See if a given @snapc is either writeable, or already written.
  805. */
  806. static int context_is_writeable_or_written(struct inode *inode,
  807. struct ceph_snap_context *snapc)
  808. {
  809. struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
  810. int ret = !oldest || snapc->seq <= oldest->seq;
  811. ceph_put_snap_context(oldest);
  812. return ret;
  813. }
  814. /*
  815. * We are only allowed to write into/dirty the page if the page is
  816. * clean, or already dirty within the same snap context.
  817. *
  818. * called with page locked.
  819. * return success with page locked,
  820. * or any failure (incl -EAGAIN) with page unlocked.
  821. */
  822. static int ceph_update_writeable_page(struct file *file,
  823. loff_t pos, unsigned len,
  824. struct page *page)
  825. {
  826. struct inode *inode = file->f_dentry->d_inode;
  827. struct ceph_inode_info *ci = ceph_inode(inode);
  828. struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
  829. loff_t page_off = pos & PAGE_CACHE_MASK;
  830. int pos_in_page = pos & ~PAGE_CACHE_MASK;
  831. int end_in_page = pos_in_page + len;
  832. loff_t i_size;
  833. int r;
  834. struct ceph_snap_context *snapc, *oldest;
  835. retry_locked:
  836. /* writepages currently holds page lock, but if we change that later, */
  837. wait_on_page_writeback(page);
  838. /* check snap context */
  839. BUG_ON(!ci->i_snap_realm);
  840. down_read(&mdsc->snap_rwsem);
  841. BUG_ON(!ci->i_snap_realm->cached_context);
  842. snapc = (void *)page->private;
  843. if (snapc && snapc != ci->i_head_snapc) {
  844. /*
  845. * this page is already dirty in another (older) snap
  846. * context! is it writeable now?
  847. */
  848. oldest = get_oldest_context(inode, NULL);
  849. up_read(&mdsc->snap_rwsem);
  850. if (snapc->seq > oldest->seq) {
  851. ceph_put_snap_context(oldest);
  852. dout(" page %p snapc %p not current or oldest\n",
  853. page, snapc);
  854. /*
  855. * queue for writeback, and wait for snapc to
  856. * be writeable or written
  857. */
  858. snapc = ceph_get_snap_context(snapc);
  859. unlock_page(page);
  860. ceph_queue_writeback(inode);
  861. r = wait_event_interruptible(ci->i_cap_wq,
  862. context_is_writeable_or_written(inode, snapc));
  863. ceph_put_snap_context(snapc);
  864. if (r == -ERESTARTSYS)
  865. return r;
  866. return -EAGAIN;
  867. }
  868. ceph_put_snap_context(oldest);
  869. /* yay, writeable, do it now (without dropping page lock) */
  870. dout(" page %p snapc %p not current, but oldest\n",
  871. page, snapc);
  872. if (!clear_page_dirty_for_io(page))
  873. goto retry_locked;
  874. r = writepage_nounlock(page, NULL);
  875. if (r < 0)
  876. goto fail_nosnap;
  877. goto retry_locked;
  878. }
  879. if (PageUptodate(page)) {
  880. dout(" page %p already uptodate\n", page);
  881. return 0;
  882. }
  883. /* full page? */
  884. if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
  885. return 0;
  886. /* past end of file? */
  887. i_size = inode->i_size; /* caller holds i_mutex */
  888. if (i_size + len > inode->i_sb->s_maxbytes) {
  889. /* file is too big */
  890. r = -EINVAL;
  891. goto fail;
  892. }
  893. if (page_off >= i_size ||
  894. (pos_in_page == 0 && (pos+len) >= i_size &&
  895. end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
  896. dout(" zeroing %p 0 - %d and %d - %d\n",
  897. page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
  898. zero_user_segments(page,
  899. 0, pos_in_page,
  900. end_in_page, PAGE_CACHE_SIZE);
  901. return 0;
  902. }
  903. /* we need to read it. */
  904. up_read(&mdsc->snap_rwsem);
  905. r = readpage_nounlock(file, page);
  906. if (r < 0)
  907. goto fail_nosnap;
  908. goto retry_locked;
  909. fail:
  910. up_read(&mdsc->snap_rwsem);
  911. fail_nosnap:
  912. unlock_page(page);
  913. return r;
  914. }
  915. /*
  916. * We are only allowed to write into/dirty the page if the page is
  917. * clean, or already dirty within the same snap context.
  918. */
  919. static int ceph_write_begin(struct file *file, struct address_space *mapping,
  920. loff_t pos, unsigned len, unsigned flags,
  921. struct page **pagep, void **fsdata)
  922. {
  923. struct inode *inode = file->f_dentry->d_inode;
  924. struct page *page;
  925. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  926. int r;
  927. do {
  928. /* get a page */
  929. page = grab_cache_page_write_begin(mapping, index, 0);
  930. if (!page)
  931. return -ENOMEM;
  932. *pagep = page;
  933. dout("write_begin file %p inode %p page %p %d~%d\n", file,
  934. inode, page, (int)pos, (int)len);
  935. r = ceph_update_writeable_page(file, pos, len, page);
  936. } while (r == -EAGAIN);
  937. return r;
  938. }
  939. /*
  940. * we don't do anything in here that simple_write_end doesn't do
  941. * except adjust dirty page accounting and drop read lock on
  942. * mdsc->snap_rwsem.
  943. */
  944. static int ceph_write_end(struct file *file, struct address_space *mapping,
  945. loff_t pos, unsigned len, unsigned copied,
  946. struct page *page, void *fsdata)
  947. {
  948. struct inode *inode = file->f_dentry->d_inode;
  949. struct ceph_client *client = ceph_inode_to_client(inode);
  950. struct ceph_mds_client *mdsc = &client->mdsc;
  951. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  952. int check_cap = 0;
  953. dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
  954. inode, page, (int)pos, (int)copied, (int)len);
  955. /* zero the stale part of the page if we did a short copy */
  956. if (copied < len)
  957. zero_user_segment(page, from+copied, len);
  958. /* did file size increase? */
  959. /* (no need for i_size_read(); we caller holds i_mutex */
  960. if (pos+copied > inode->i_size)
  961. check_cap = ceph_inode_set_size(inode, pos+copied);
  962. if (!PageUptodate(page))
  963. SetPageUptodate(page);
  964. set_page_dirty(page);
  965. unlock_page(page);
  966. up_read(&mdsc->snap_rwsem);
  967. page_cache_release(page);
  968. if (check_cap)
  969. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
  970. return copied;
  971. }
  972. /*
  973. * we set .direct_IO to indicate direct io is supported, but since we
  974. * intercept O_DIRECT reads and writes early, this function should
  975. * never get called.
  976. */
  977. static ssize_t ceph_direct_io(int rw, struct kiocb *iocb,
  978. const struct iovec *iov,
  979. loff_t pos, unsigned long nr_segs)
  980. {
  981. WARN_ON(1);
  982. return -EINVAL;
  983. }
  984. const struct address_space_operations ceph_aops = {
  985. .readpage = ceph_readpage,
  986. .readpages = ceph_readpages,
  987. .writepage = ceph_writepage,
  988. .writepages = ceph_writepages_start,
  989. .write_begin = ceph_write_begin,
  990. .write_end = ceph_write_end,
  991. .set_page_dirty = ceph_set_page_dirty,
  992. .invalidatepage = ceph_invalidatepage,
  993. .releasepage = ceph_releasepage,
  994. .direct_IO = ceph_direct_io,
  995. };
  996. /*
  997. * vm ops
  998. */
  999. /*
  1000. * Reuse write_begin here for simplicity.
  1001. */
  1002. static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  1003. {
  1004. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  1005. struct page *page = vmf->page;
  1006. struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
  1007. loff_t off = page->index << PAGE_CACHE_SHIFT;
  1008. loff_t size, len;
  1009. int ret;
  1010. size = i_size_read(inode);
  1011. if (off + PAGE_CACHE_SIZE <= size)
  1012. len = PAGE_CACHE_SIZE;
  1013. else
  1014. len = size & ~PAGE_CACHE_MASK;
  1015. dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode,
  1016. off, len, page, page->index);
  1017. lock_page(page);
  1018. ret = VM_FAULT_NOPAGE;
  1019. if ((off > size) ||
  1020. (page->mapping != inode->i_mapping))
  1021. goto out;
  1022. ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
  1023. if (ret == 0) {
  1024. /* success. we'll keep the page locked. */
  1025. set_page_dirty(page);
  1026. up_read(&mdsc->snap_rwsem);
  1027. ret = VM_FAULT_LOCKED;
  1028. } else {
  1029. if (ret == -ENOMEM)
  1030. ret = VM_FAULT_OOM;
  1031. else
  1032. ret = VM_FAULT_SIGBUS;
  1033. }
  1034. out:
  1035. dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret);
  1036. if (ret != VM_FAULT_LOCKED)
  1037. unlock_page(page);
  1038. return ret;
  1039. }
  1040. static struct vm_operations_struct ceph_vmops = {
  1041. .fault = filemap_fault,
  1042. .page_mkwrite = ceph_page_mkwrite,
  1043. };
  1044. int ceph_mmap(struct file *file, struct vm_area_struct *vma)
  1045. {
  1046. struct address_space *mapping = file->f_mapping;
  1047. if (!mapping->a_ops->readpage)
  1048. return -ENOEXEC;
  1049. file_accessed(file);
  1050. vma->vm_ops = &ceph_vmops;
  1051. vma->vm_flags |= VM_CAN_NONLINEAR;
  1052. return 0;
  1053. }