addr.c 31 KB

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