aops.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/pagevec.h>
  16. #include <linux/mpage.h>
  17. #include <linux/fs.h>
  18. #include <linux/writeback.h>
  19. #include <linux/swap.h>
  20. #include <linux/gfs2_ondisk.h>
  21. #include <linux/backing-dev.h>
  22. #include <linux/uio.h>
  23. #include <trace/events/writeback.h>
  24. #include "gfs2.h"
  25. #include "incore.h"
  26. #include "bmap.h"
  27. #include "glock.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "trans.h"
  33. #include "rgrp.h"
  34. #include "super.h"
  35. #include "util.h"
  36. #include "glops.h"
  37. static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  38. unsigned int from, unsigned int len)
  39. {
  40. struct buffer_head *head = page_buffers(page);
  41. unsigned int bsize = head->b_size;
  42. struct buffer_head *bh;
  43. unsigned int to = from + len;
  44. unsigned int start, end;
  45. for (bh = head, start = 0; bh != head || !start;
  46. bh = bh->b_this_page, start = end) {
  47. end = start + bsize;
  48. if (end <= from)
  49. continue;
  50. if (start >= to)
  51. break;
  52. if (gfs2_is_jdata(ip))
  53. set_buffer_uptodate(bh);
  54. gfs2_trans_add_data(ip->i_gl, bh);
  55. }
  56. }
  57. /**
  58. * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  59. * @inode: The inode
  60. * @lblock: The block number to look up
  61. * @bh_result: The buffer head to return the result in
  62. * @create: Non-zero if we may add block to the file
  63. *
  64. * Returns: errno
  65. */
  66. static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
  67. struct buffer_head *bh_result, int create)
  68. {
  69. int error;
  70. error = gfs2_block_map(inode, lblock, bh_result, 0);
  71. if (error)
  72. return error;
  73. if (!buffer_mapped(bh_result))
  74. return -EIO;
  75. return 0;
  76. }
  77. static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
  78. struct buffer_head *bh_result, int create)
  79. {
  80. return gfs2_block_map(inode, lblock, bh_result, 0);
  81. }
  82. /**
  83. * gfs2_writepage_common - Common bits of writepage
  84. * @page: The page to be written
  85. * @wbc: The writeback control
  86. *
  87. * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
  88. */
  89. static int gfs2_writepage_common(struct page *page,
  90. struct writeback_control *wbc)
  91. {
  92. struct inode *inode = page->mapping->host;
  93. struct gfs2_inode *ip = GFS2_I(inode);
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. loff_t i_size = i_size_read(inode);
  96. pgoff_t end_index = i_size >> PAGE_SHIFT;
  97. unsigned offset;
  98. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
  99. goto out;
  100. if (current->journal_info)
  101. goto redirty;
  102. /* Is the page fully outside i_size? (truncate in progress) */
  103. offset = i_size & (PAGE_SIZE-1);
  104. if (page->index > end_index || (page->index == end_index && !offset)) {
  105. page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
  106. goto out;
  107. }
  108. return 1;
  109. redirty:
  110. redirty_page_for_writepage(wbc, page);
  111. out:
  112. unlock_page(page);
  113. return 0;
  114. }
  115. /**
  116. * gfs2_writepage - Write page for writeback mappings
  117. * @page: The page
  118. * @wbc: The writeback control
  119. *
  120. */
  121. static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
  122. {
  123. int ret;
  124. ret = gfs2_writepage_common(page, wbc);
  125. if (ret <= 0)
  126. return ret;
  127. return nobh_writepage(page, gfs2_get_block_noalloc, wbc);
  128. }
  129. /* This is the same as calling block_write_full_page, but it also
  130. * writes pages outside of i_size
  131. */
  132. static int gfs2_write_full_page(struct page *page, get_block_t *get_block,
  133. struct writeback_control *wbc)
  134. {
  135. struct inode * const inode = page->mapping->host;
  136. loff_t i_size = i_size_read(inode);
  137. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  138. unsigned offset;
  139. /*
  140. * The page straddles i_size. It must be zeroed out on each and every
  141. * writepage invocation because it may be mmapped. "A file is mapped
  142. * in multiples of the page size. For a file that is not a multiple of
  143. * the page size, the remaining memory is zeroed when mapped, and
  144. * writes to that region are not written out to the file."
  145. */
  146. offset = i_size & (PAGE_SIZE-1);
  147. if (page->index == end_index && offset)
  148. zero_user_segment(page, offset, PAGE_SIZE);
  149. return __block_write_full_page(inode, page, get_block, wbc,
  150. end_buffer_async_write);
  151. }
  152. /**
  153. * __gfs2_jdata_writepage - The core of jdata writepage
  154. * @page: The page to write
  155. * @wbc: The writeback control
  156. *
  157. * This is shared between writepage and writepages and implements the
  158. * core of the writepage operation. If a transaction is required then
  159. * PageChecked will have been set and the transaction will have
  160. * already been started before this is called.
  161. */
  162. static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  163. {
  164. struct inode *inode = page->mapping->host;
  165. struct gfs2_inode *ip = GFS2_I(inode);
  166. struct gfs2_sbd *sdp = GFS2_SB(inode);
  167. if (PageChecked(page)) {
  168. ClearPageChecked(page);
  169. if (!page_has_buffers(page)) {
  170. create_empty_buffers(page, inode->i_sb->s_blocksize,
  171. BIT(BH_Dirty)|BIT(BH_Uptodate));
  172. }
  173. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize);
  174. }
  175. return gfs2_write_full_page(page, gfs2_get_block_noalloc, wbc);
  176. }
  177. /**
  178. * gfs2_jdata_writepage - Write complete page
  179. * @page: Page to write
  180. * @wbc: The writeback control
  181. *
  182. * Returns: errno
  183. *
  184. */
  185. static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  186. {
  187. struct inode *inode = page->mapping->host;
  188. struct gfs2_inode *ip = GFS2_I(inode);
  189. struct gfs2_sbd *sdp = GFS2_SB(inode);
  190. int ret;
  191. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
  192. goto out;
  193. if (PageChecked(page) || current->journal_info)
  194. goto out_ignore;
  195. ret = __gfs2_jdata_writepage(page, wbc);
  196. return ret;
  197. out_ignore:
  198. redirty_page_for_writepage(wbc, page);
  199. out:
  200. unlock_page(page);
  201. return 0;
  202. }
  203. /**
  204. * gfs2_writepages - Write a bunch of dirty pages back to disk
  205. * @mapping: The mapping to write
  206. * @wbc: Write-back control
  207. *
  208. * Used for both ordered and writeback modes.
  209. */
  210. static int gfs2_writepages(struct address_space *mapping,
  211. struct writeback_control *wbc)
  212. {
  213. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  214. int ret = mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  215. /*
  216. * Even if we didn't write any pages here, we might still be holding
  217. * dirty pages in the ail. We forcibly flush the ail because we don't
  218. * want balance_dirty_pages() to loop indefinitely trying to write out
  219. * pages held in the ail that it can't find.
  220. */
  221. if (ret == 0)
  222. set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
  223. return ret;
  224. }
  225. /**
  226. * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
  227. * @mapping: The mapping
  228. * @wbc: The writeback control
  229. * @pvec: The vector of pages
  230. * @nr_pages: The number of pages to write
  231. * @done_index: Page index
  232. *
  233. * Returns: non-zero if loop should terminate, zero otherwise
  234. */
  235. static int gfs2_write_jdata_pagevec(struct address_space *mapping,
  236. struct writeback_control *wbc,
  237. struct pagevec *pvec,
  238. int nr_pages,
  239. pgoff_t *done_index)
  240. {
  241. struct inode *inode = mapping->host;
  242. struct gfs2_sbd *sdp = GFS2_SB(inode);
  243. unsigned nrblocks = nr_pages * (PAGE_SIZE/inode->i_sb->s_blocksize);
  244. int i;
  245. int ret;
  246. ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
  247. if (ret < 0)
  248. return ret;
  249. for(i = 0; i < nr_pages; i++) {
  250. struct page *page = pvec->pages[i];
  251. *done_index = page->index;
  252. lock_page(page);
  253. if (unlikely(page->mapping != mapping)) {
  254. continue_unlock:
  255. unlock_page(page);
  256. continue;
  257. }
  258. if (!PageDirty(page)) {
  259. /* someone wrote it for us */
  260. goto continue_unlock;
  261. }
  262. if (PageWriteback(page)) {
  263. if (wbc->sync_mode != WB_SYNC_NONE)
  264. wait_on_page_writeback(page);
  265. else
  266. goto continue_unlock;
  267. }
  268. BUG_ON(PageWriteback(page));
  269. if (!clear_page_dirty_for_io(page))
  270. goto continue_unlock;
  271. trace_wbc_writepage(wbc, inode_to_bdi(inode));
  272. ret = __gfs2_jdata_writepage(page, wbc);
  273. if (unlikely(ret)) {
  274. if (ret == AOP_WRITEPAGE_ACTIVATE) {
  275. unlock_page(page);
  276. ret = 0;
  277. } else {
  278. /*
  279. * done_index is set past this page,
  280. * so media errors will not choke
  281. * background writeout for the entire
  282. * file. This has consequences for
  283. * range_cyclic semantics (ie. it may
  284. * not be suitable for data integrity
  285. * writeout).
  286. */
  287. *done_index = page->index + 1;
  288. ret = 1;
  289. break;
  290. }
  291. }
  292. /*
  293. * We stop writing back only if we are not doing
  294. * integrity sync. In case of integrity sync we have to
  295. * keep going until we have written all the pages
  296. * we tagged for writeback prior to entering this loop.
  297. */
  298. if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
  299. ret = 1;
  300. break;
  301. }
  302. }
  303. gfs2_trans_end(sdp);
  304. return ret;
  305. }
  306. /**
  307. * gfs2_write_cache_jdata - Like write_cache_pages but different
  308. * @mapping: The mapping to write
  309. * @wbc: The writeback control
  310. *
  311. * The reason that we use our own function here is that we need to
  312. * start transactions before we grab page locks. This allows us
  313. * to get the ordering right.
  314. */
  315. static int gfs2_write_cache_jdata(struct address_space *mapping,
  316. struct writeback_control *wbc)
  317. {
  318. int ret = 0;
  319. int done = 0;
  320. struct pagevec pvec;
  321. int nr_pages;
  322. pgoff_t uninitialized_var(writeback_index);
  323. pgoff_t index;
  324. pgoff_t end;
  325. pgoff_t done_index;
  326. int cycled;
  327. int range_whole = 0;
  328. int tag;
  329. pagevec_init(&pvec);
  330. if (wbc->range_cyclic) {
  331. writeback_index = mapping->writeback_index; /* prev offset */
  332. index = writeback_index;
  333. if (index == 0)
  334. cycled = 1;
  335. else
  336. cycled = 0;
  337. end = -1;
  338. } else {
  339. index = wbc->range_start >> PAGE_SHIFT;
  340. end = wbc->range_end >> PAGE_SHIFT;
  341. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  342. range_whole = 1;
  343. cycled = 1; /* ignore range_cyclic tests */
  344. }
  345. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  346. tag = PAGECACHE_TAG_TOWRITE;
  347. else
  348. tag = PAGECACHE_TAG_DIRTY;
  349. retry:
  350. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  351. tag_pages_for_writeback(mapping, index, end);
  352. done_index = index;
  353. while (!done && (index <= end)) {
  354. nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
  355. tag);
  356. if (nr_pages == 0)
  357. break;
  358. ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, &done_index);
  359. if (ret)
  360. done = 1;
  361. if (ret > 0)
  362. ret = 0;
  363. pagevec_release(&pvec);
  364. cond_resched();
  365. }
  366. if (!cycled && !done) {
  367. /*
  368. * range_cyclic:
  369. * We hit the last page and there is more work to be done: wrap
  370. * back to the start of the file
  371. */
  372. cycled = 1;
  373. index = 0;
  374. end = writeback_index - 1;
  375. goto retry;
  376. }
  377. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  378. mapping->writeback_index = done_index;
  379. return ret;
  380. }
  381. /**
  382. * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
  383. * @mapping: The mapping to write
  384. * @wbc: The writeback control
  385. *
  386. */
  387. static int gfs2_jdata_writepages(struct address_space *mapping,
  388. struct writeback_control *wbc)
  389. {
  390. struct gfs2_inode *ip = GFS2_I(mapping->host);
  391. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  392. int ret;
  393. ret = gfs2_write_cache_jdata(mapping, wbc);
  394. if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
  395. gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
  396. GFS2_LFC_JDATA_WPAGES);
  397. ret = gfs2_write_cache_jdata(mapping, wbc);
  398. }
  399. return ret;
  400. }
  401. /**
  402. * stuffed_readpage - Fill in a Linux page with stuffed file data
  403. * @ip: the inode
  404. * @page: the page
  405. *
  406. * Returns: errno
  407. */
  408. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  409. {
  410. struct buffer_head *dibh;
  411. u64 dsize = i_size_read(&ip->i_inode);
  412. void *kaddr;
  413. int error;
  414. /*
  415. * Due to the order of unstuffing files and ->fault(), we can be
  416. * asked for a zero page in the case of a stuffed file being extended,
  417. * so we need to supply one here. It doesn't happen often.
  418. */
  419. if (unlikely(page->index)) {
  420. zero_user(page, 0, PAGE_SIZE);
  421. SetPageUptodate(page);
  422. return 0;
  423. }
  424. error = gfs2_meta_inode_buffer(ip, &dibh);
  425. if (error)
  426. return error;
  427. kaddr = kmap_atomic(page);
  428. if (dsize > gfs2_max_stuffed_size(ip))
  429. dsize = gfs2_max_stuffed_size(ip);
  430. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
  431. memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
  432. kunmap_atomic(kaddr);
  433. flush_dcache_page(page);
  434. brelse(dibh);
  435. SetPageUptodate(page);
  436. return 0;
  437. }
  438. /**
  439. * __gfs2_readpage - readpage
  440. * @file: The file to read a page for
  441. * @page: The page to read
  442. *
  443. * This is the core of gfs2's readpage. It's used by the internal file
  444. * reading code as in that case we already hold the glock. Also it's
  445. * called by gfs2_readpage() once the required lock has been granted.
  446. */
  447. static int __gfs2_readpage(void *file, struct page *page)
  448. {
  449. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  450. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  451. int error;
  452. if (gfs2_is_stuffed(ip)) {
  453. error = stuffed_readpage(ip, page);
  454. unlock_page(page);
  455. } else {
  456. error = mpage_readpage(page, gfs2_block_map);
  457. }
  458. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  459. return -EIO;
  460. return error;
  461. }
  462. /**
  463. * gfs2_readpage - read a page of a file
  464. * @file: The file to read
  465. * @page: The page of the file
  466. *
  467. * This deals with the locking required. We have to unlock and
  468. * relock the page in order to get the locking in the right
  469. * order.
  470. */
  471. static int gfs2_readpage(struct file *file, struct page *page)
  472. {
  473. struct address_space *mapping = page->mapping;
  474. struct gfs2_inode *ip = GFS2_I(mapping->host);
  475. struct gfs2_holder gh;
  476. int error;
  477. unlock_page(page);
  478. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  479. error = gfs2_glock_nq(&gh);
  480. if (unlikely(error))
  481. goto out;
  482. error = AOP_TRUNCATED_PAGE;
  483. lock_page(page);
  484. if (page->mapping == mapping && !PageUptodate(page))
  485. error = __gfs2_readpage(file, page);
  486. else
  487. unlock_page(page);
  488. gfs2_glock_dq(&gh);
  489. out:
  490. gfs2_holder_uninit(&gh);
  491. if (error && error != AOP_TRUNCATED_PAGE)
  492. lock_page(page);
  493. return error;
  494. }
  495. /**
  496. * gfs2_internal_read - read an internal file
  497. * @ip: The gfs2 inode
  498. * @buf: The buffer to fill
  499. * @pos: The file position
  500. * @size: The amount to read
  501. *
  502. */
  503. int gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
  504. unsigned size)
  505. {
  506. struct address_space *mapping = ip->i_inode.i_mapping;
  507. unsigned long index = *pos / PAGE_SIZE;
  508. unsigned offset = *pos & (PAGE_SIZE - 1);
  509. unsigned copied = 0;
  510. unsigned amt;
  511. struct page *page;
  512. void *p;
  513. do {
  514. amt = size - copied;
  515. if (offset + size > PAGE_SIZE)
  516. amt = PAGE_SIZE - offset;
  517. page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
  518. if (IS_ERR(page))
  519. return PTR_ERR(page);
  520. p = kmap_atomic(page);
  521. memcpy(buf + copied, p + offset, amt);
  522. kunmap_atomic(p);
  523. put_page(page);
  524. copied += amt;
  525. index++;
  526. offset = 0;
  527. } while(copied < size);
  528. (*pos) += size;
  529. return size;
  530. }
  531. /**
  532. * gfs2_readpages - Read a bunch of pages at once
  533. * @file: The file to read from
  534. * @mapping: Address space info
  535. * @pages: List of pages to read
  536. * @nr_pages: Number of pages to read
  537. *
  538. * Some notes:
  539. * 1. This is only for readahead, so we can simply ignore any things
  540. * which are slightly inconvenient (such as locking conflicts between
  541. * the page lock and the glock) and return having done no I/O. Its
  542. * obviously not something we'd want to do on too regular a basis.
  543. * Any I/O we ignore at this time will be done via readpage later.
  544. * 2. We don't handle stuffed files here we let readpage do the honours.
  545. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  546. * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
  547. */
  548. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  549. struct list_head *pages, unsigned nr_pages)
  550. {
  551. struct inode *inode = mapping->host;
  552. struct gfs2_inode *ip = GFS2_I(inode);
  553. struct gfs2_sbd *sdp = GFS2_SB(inode);
  554. struct gfs2_holder gh;
  555. int ret;
  556. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  557. ret = gfs2_glock_nq(&gh);
  558. if (unlikely(ret))
  559. goto out_uninit;
  560. if (!gfs2_is_stuffed(ip))
  561. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
  562. gfs2_glock_dq(&gh);
  563. out_uninit:
  564. gfs2_holder_uninit(&gh);
  565. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  566. ret = -EIO;
  567. return ret;
  568. }
  569. /**
  570. * gfs2_write_begin - Begin to write to a file
  571. * @file: The file to write to
  572. * @mapping: The mapping in which to write
  573. * @pos: The file offset at which to start writing
  574. * @len: Length of the write
  575. * @flags: Various flags
  576. * @pagep: Pointer to return the page
  577. * @fsdata: Pointer to return fs data (unused by GFS2)
  578. *
  579. * Returns: errno
  580. */
  581. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  582. loff_t pos, unsigned len, unsigned flags,
  583. struct page **pagep, void **fsdata)
  584. {
  585. struct gfs2_inode *ip = GFS2_I(mapping->host);
  586. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  587. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  588. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  589. unsigned requested = 0;
  590. int alloc_required;
  591. int error = 0;
  592. pgoff_t index = pos >> PAGE_SHIFT;
  593. unsigned from = pos & (PAGE_SIZE - 1);
  594. struct page *page;
  595. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  596. error = gfs2_glock_nq(&ip->i_gh);
  597. if (unlikely(error))
  598. goto out_uninit;
  599. if (&ip->i_inode == sdp->sd_rindex) {
  600. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE,
  601. GL_NOCACHE, &m_ip->i_gh);
  602. if (unlikely(error)) {
  603. gfs2_glock_dq(&ip->i_gh);
  604. goto out_uninit;
  605. }
  606. }
  607. alloc_required = gfs2_write_alloc_required(ip, pos, len);
  608. if (alloc_required || gfs2_is_jdata(ip))
  609. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  610. if (alloc_required) {
  611. struct gfs2_alloc_parms ap = { .aflags = 0, };
  612. requested = data_blocks + ind_blocks;
  613. ap.target = requested;
  614. error = gfs2_quota_lock_check(ip, &ap);
  615. if (error)
  616. goto out_unlock;
  617. error = gfs2_inplace_reserve(ip, &ap);
  618. if (error)
  619. goto out_qunlock;
  620. }
  621. rblocks = RES_DINODE + ind_blocks;
  622. if (gfs2_is_jdata(ip))
  623. rblocks += data_blocks ? data_blocks : 1;
  624. if (ind_blocks || data_blocks)
  625. rblocks += RES_STATFS + RES_QUOTA;
  626. if (&ip->i_inode == sdp->sd_rindex)
  627. rblocks += 2 * RES_STATFS;
  628. if (alloc_required)
  629. rblocks += gfs2_rg_blocks(ip, requested);
  630. error = gfs2_trans_begin(sdp, rblocks,
  631. PAGE_SIZE/sdp->sd_sb.sb_bsize);
  632. if (error)
  633. goto out_trans_fail;
  634. error = -ENOMEM;
  635. flags |= AOP_FLAG_NOFS;
  636. page = grab_cache_page_write_begin(mapping, index, flags);
  637. *pagep = page;
  638. if (unlikely(!page))
  639. goto out_endtrans;
  640. if (gfs2_is_stuffed(ip)) {
  641. error = 0;
  642. if (pos + len > gfs2_max_stuffed_size(ip)) {
  643. error = gfs2_unstuff_dinode(ip, page);
  644. if (error == 0)
  645. goto prepare_write;
  646. } else if (!PageUptodate(page)) {
  647. error = stuffed_readpage(ip, page);
  648. }
  649. goto out;
  650. }
  651. prepare_write:
  652. error = __block_write_begin(page, from, len, gfs2_block_map);
  653. out:
  654. if (error == 0)
  655. return 0;
  656. unlock_page(page);
  657. put_page(page);
  658. gfs2_trans_end(sdp);
  659. if (pos + len > ip->i_inode.i_size)
  660. gfs2_trim_blocks(&ip->i_inode);
  661. goto out_trans_fail;
  662. out_endtrans:
  663. gfs2_trans_end(sdp);
  664. out_trans_fail:
  665. if (alloc_required) {
  666. gfs2_inplace_release(ip);
  667. out_qunlock:
  668. gfs2_quota_unlock(ip);
  669. }
  670. out_unlock:
  671. if (&ip->i_inode == sdp->sd_rindex) {
  672. gfs2_glock_dq(&m_ip->i_gh);
  673. gfs2_holder_uninit(&m_ip->i_gh);
  674. }
  675. gfs2_glock_dq(&ip->i_gh);
  676. out_uninit:
  677. gfs2_holder_uninit(&ip->i_gh);
  678. return error;
  679. }
  680. /**
  681. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  682. * @inode: the rindex inode
  683. */
  684. static void adjust_fs_space(struct inode *inode)
  685. {
  686. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  687. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  688. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  689. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  690. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  691. struct buffer_head *m_bh, *l_bh;
  692. u64 fs_total, new_free;
  693. /* Total up the file system space, according to the latest rindex. */
  694. fs_total = gfs2_ri_total(sdp);
  695. if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
  696. return;
  697. spin_lock(&sdp->sd_statfs_spin);
  698. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  699. sizeof(struct gfs2_dinode));
  700. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  701. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  702. else
  703. new_free = 0;
  704. spin_unlock(&sdp->sd_statfs_spin);
  705. fs_warn(sdp, "File system extended by %llu blocks.\n",
  706. (unsigned long long)new_free);
  707. gfs2_statfs_change(sdp, new_free, new_free, 0);
  708. if (gfs2_meta_inode_buffer(l_ip, &l_bh) != 0)
  709. goto out;
  710. update_statfs(sdp, m_bh, l_bh);
  711. brelse(l_bh);
  712. out:
  713. brelse(m_bh);
  714. }
  715. /**
  716. * gfs2_stuffed_write_end - Write end for stuffed files
  717. * @inode: The inode
  718. * @dibh: The buffer_head containing the on-disk inode
  719. * @pos: The file position
  720. * @len: The length of the write
  721. * @copied: How much was actually copied by the VFS
  722. * @page: The page
  723. *
  724. * This copies the data from the page into the inode block after
  725. * the inode data structure itself.
  726. *
  727. * Returns: errno
  728. */
  729. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  730. loff_t pos, unsigned len, unsigned copied,
  731. struct page *page)
  732. {
  733. struct gfs2_inode *ip = GFS2_I(inode);
  734. struct gfs2_sbd *sdp = GFS2_SB(inode);
  735. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  736. u64 to = pos + copied;
  737. void *kaddr;
  738. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  739. BUG_ON(pos + len > gfs2_max_stuffed_size(ip));
  740. kaddr = kmap_atomic(page);
  741. memcpy(buf + pos, kaddr + pos, copied);
  742. flush_dcache_page(page);
  743. kunmap_atomic(kaddr);
  744. WARN_ON(!PageUptodate(page));
  745. unlock_page(page);
  746. put_page(page);
  747. if (copied) {
  748. if (inode->i_size < to)
  749. i_size_write(inode, to);
  750. mark_inode_dirty(inode);
  751. }
  752. if (inode == sdp->sd_rindex) {
  753. adjust_fs_space(inode);
  754. sdp->sd_rindex_uptodate = 0;
  755. }
  756. brelse(dibh);
  757. gfs2_trans_end(sdp);
  758. if (inode == sdp->sd_rindex) {
  759. gfs2_glock_dq(&m_ip->i_gh);
  760. gfs2_holder_uninit(&m_ip->i_gh);
  761. }
  762. gfs2_glock_dq(&ip->i_gh);
  763. gfs2_holder_uninit(&ip->i_gh);
  764. return copied;
  765. }
  766. /**
  767. * gfs2_write_end
  768. * @file: The file to write to
  769. * @mapping: The address space to write to
  770. * @pos: The file position
  771. * @len: The length of the data
  772. * @copied: How much was actually copied by the VFS
  773. * @page: The page that has been written
  774. * @fsdata: The fsdata (unused in GFS2)
  775. *
  776. * The main write_end function for GFS2. We have a separate one for
  777. * stuffed files as they are slightly different, otherwise we just
  778. * put our locking around the VFS provided functions.
  779. *
  780. * Returns: errno
  781. */
  782. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  783. loff_t pos, unsigned len, unsigned copied,
  784. struct page *page, void *fsdata)
  785. {
  786. struct inode *inode = page->mapping->host;
  787. struct gfs2_inode *ip = GFS2_I(inode);
  788. struct gfs2_sbd *sdp = GFS2_SB(inode);
  789. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  790. struct buffer_head *dibh;
  791. int ret;
  792. struct gfs2_trans *tr = current->journal_info;
  793. BUG_ON(!tr);
  794. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
  795. ret = gfs2_meta_inode_buffer(ip, &dibh);
  796. if (unlikely(ret)) {
  797. unlock_page(page);
  798. put_page(page);
  799. goto failed;
  800. }
  801. if (gfs2_is_stuffed(ip))
  802. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  803. if (!gfs2_is_writeback(ip))
  804. gfs2_page_add_databufs(ip, page, pos & ~PAGE_MASK, len);
  805. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  806. if (tr->tr_num_buf_new)
  807. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  808. else
  809. gfs2_trans_add_meta(ip->i_gl, dibh);
  810. if (inode == sdp->sd_rindex) {
  811. adjust_fs_space(inode);
  812. sdp->sd_rindex_uptodate = 0;
  813. }
  814. brelse(dibh);
  815. failed:
  816. gfs2_trans_end(sdp);
  817. gfs2_inplace_release(ip);
  818. if (ip->i_qadata && ip->i_qadata->qa_qd_num)
  819. gfs2_quota_unlock(ip);
  820. if (inode == sdp->sd_rindex) {
  821. gfs2_glock_dq(&m_ip->i_gh);
  822. gfs2_holder_uninit(&m_ip->i_gh);
  823. }
  824. gfs2_glock_dq(&ip->i_gh);
  825. gfs2_holder_uninit(&ip->i_gh);
  826. return ret;
  827. }
  828. /**
  829. * jdata_set_page_dirty - Page dirtying function
  830. * @page: The page to dirty
  831. *
  832. * Returns: 1 if it dirtyed the page, or 0 otherwise
  833. */
  834. static int jdata_set_page_dirty(struct page *page)
  835. {
  836. SetPageChecked(page);
  837. return __set_page_dirty_buffers(page);
  838. }
  839. /**
  840. * gfs2_bmap - Block map function
  841. * @mapping: Address space info
  842. * @lblock: The block to map
  843. *
  844. * Returns: The disk address for the block or 0 on hole or error
  845. */
  846. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  847. {
  848. struct gfs2_inode *ip = GFS2_I(mapping->host);
  849. struct gfs2_holder i_gh;
  850. sector_t dblock = 0;
  851. int error;
  852. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  853. if (error)
  854. return 0;
  855. if (!gfs2_is_stuffed(ip))
  856. dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
  857. gfs2_glock_dq_uninit(&i_gh);
  858. return dblock;
  859. }
  860. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  861. {
  862. struct gfs2_bufdata *bd;
  863. lock_buffer(bh);
  864. gfs2_log_lock(sdp);
  865. clear_buffer_dirty(bh);
  866. bd = bh->b_private;
  867. if (bd) {
  868. if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
  869. list_del_init(&bd->bd_list);
  870. else
  871. gfs2_remove_from_journal(bh, REMOVE_JDATA);
  872. }
  873. bh->b_bdev = NULL;
  874. clear_buffer_mapped(bh);
  875. clear_buffer_req(bh);
  876. clear_buffer_new(bh);
  877. gfs2_log_unlock(sdp);
  878. unlock_buffer(bh);
  879. }
  880. static void gfs2_invalidatepage(struct page *page, unsigned int offset,
  881. unsigned int length)
  882. {
  883. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  884. unsigned int stop = offset + length;
  885. int partial_page = (offset || length < PAGE_SIZE);
  886. struct buffer_head *bh, *head;
  887. unsigned long pos = 0;
  888. BUG_ON(!PageLocked(page));
  889. if (!partial_page)
  890. ClearPageChecked(page);
  891. if (!page_has_buffers(page))
  892. goto out;
  893. bh = head = page_buffers(page);
  894. do {
  895. if (pos + bh->b_size > stop)
  896. return;
  897. if (offset <= pos)
  898. gfs2_discard(sdp, bh);
  899. pos += bh->b_size;
  900. bh = bh->b_this_page;
  901. } while (bh != head);
  902. out:
  903. if (!partial_page)
  904. try_to_release_page(page, 0);
  905. }
  906. /**
  907. * gfs2_ok_for_dio - check that dio is valid on this file
  908. * @ip: The inode
  909. * @offset: The offset at which we are reading or writing
  910. *
  911. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  912. * 1 (to accept the i/o request)
  913. */
  914. static int gfs2_ok_for_dio(struct gfs2_inode *ip, loff_t offset)
  915. {
  916. /*
  917. * Should we return an error here? I can't see that O_DIRECT for
  918. * a stuffed file makes any sense. For now we'll silently fall
  919. * back to buffered I/O
  920. */
  921. if (gfs2_is_stuffed(ip))
  922. return 0;
  923. if (offset >= i_size_read(&ip->i_inode))
  924. return 0;
  925. return 1;
  926. }
  927. static ssize_t gfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  928. {
  929. struct file *file = iocb->ki_filp;
  930. struct inode *inode = file->f_mapping->host;
  931. struct address_space *mapping = inode->i_mapping;
  932. struct gfs2_inode *ip = GFS2_I(inode);
  933. loff_t offset = iocb->ki_pos;
  934. struct gfs2_holder gh;
  935. int rv;
  936. /*
  937. * Deferred lock, even if its a write, since we do no allocation
  938. * on this path. All we need change is atime, and this lock mode
  939. * ensures that other nodes have flushed their buffered read caches
  940. * (i.e. their page cache entries for this inode). We do not,
  941. * unfortunately have the option of only flushing a range like
  942. * the VFS does.
  943. */
  944. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
  945. rv = gfs2_glock_nq(&gh);
  946. if (rv)
  947. goto out_uninit;
  948. rv = gfs2_ok_for_dio(ip, offset);
  949. if (rv != 1)
  950. goto out; /* dio not valid, fall back to buffered i/o */
  951. /*
  952. * Now since we are holding a deferred (CW) lock at this point, you
  953. * might be wondering why this is ever needed. There is a case however
  954. * where we've granted a deferred local lock against a cached exclusive
  955. * glock. That is ok provided all granted local locks are deferred, but
  956. * it also means that it is possible to encounter pages which are
  957. * cached and possibly also mapped. So here we check for that and sort
  958. * them out ahead of the dio. The glock state machine will take care of
  959. * everything else.
  960. *
  961. * If in fact the cached glock state (gl->gl_state) is deferred (CW) in
  962. * the first place, mapping->nr_pages will always be zero.
  963. */
  964. if (mapping->nrpages) {
  965. loff_t lstart = offset & ~(PAGE_SIZE - 1);
  966. loff_t len = iov_iter_count(iter);
  967. loff_t end = PAGE_ALIGN(offset + len) - 1;
  968. rv = 0;
  969. if (len == 0)
  970. goto out;
  971. if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
  972. unmap_shared_mapping_range(ip->i_inode.i_mapping, offset, len);
  973. rv = filemap_write_and_wait_range(mapping, lstart, end);
  974. if (rv)
  975. goto out;
  976. if (iov_iter_rw(iter) == WRITE)
  977. truncate_inode_pages_range(mapping, lstart, end);
  978. }
  979. rv = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
  980. gfs2_get_block_direct, NULL, NULL, 0);
  981. out:
  982. gfs2_glock_dq(&gh);
  983. out_uninit:
  984. gfs2_holder_uninit(&gh);
  985. return rv;
  986. }
  987. /**
  988. * gfs2_releasepage - free the metadata associated with a page
  989. * @page: the page that's being released
  990. * @gfp_mask: passed from Linux VFS, ignored by us
  991. *
  992. * Call try_to_free_buffers() if the buffers in this page can be
  993. * released.
  994. *
  995. * Returns: 0
  996. */
  997. int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
  998. {
  999. struct address_space *mapping = page->mapping;
  1000. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  1001. struct buffer_head *bh, *head;
  1002. struct gfs2_bufdata *bd;
  1003. if (!page_has_buffers(page))
  1004. return 0;
  1005. /*
  1006. * From xfs_vm_releasepage: mm accommodates an old ext3 case where
  1007. * clean pages might not have had the dirty bit cleared. Thus, it can
  1008. * send actual dirty pages to ->releasepage() via shrink_active_list().
  1009. *
  1010. * As a workaround, we skip pages that contain dirty buffers below.
  1011. * Once ->releasepage isn't called on dirty pages anymore, we can warn
  1012. * on dirty buffers like we used to here again.
  1013. */
  1014. gfs2_log_lock(sdp);
  1015. spin_lock(&sdp->sd_ail_lock);
  1016. head = bh = page_buffers(page);
  1017. do {
  1018. if (atomic_read(&bh->b_count))
  1019. goto cannot_release;
  1020. bd = bh->b_private;
  1021. if (bd && bd->bd_tr)
  1022. goto cannot_release;
  1023. if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
  1024. goto cannot_release;
  1025. bh = bh->b_this_page;
  1026. } while(bh != head);
  1027. spin_unlock(&sdp->sd_ail_lock);
  1028. head = bh = page_buffers(page);
  1029. do {
  1030. bd = bh->b_private;
  1031. if (bd) {
  1032. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  1033. if (!list_empty(&bd->bd_list))
  1034. list_del_init(&bd->bd_list);
  1035. bd->bd_bh = NULL;
  1036. bh->b_private = NULL;
  1037. kmem_cache_free(gfs2_bufdata_cachep, bd);
  1038. }
  1039. bh = bh->b_this_page;
  1040. } while (bh != head);
  1041. gfs2_log_unlock(sdp);
  1042. return try_to_free_buffers(page);
  1043. cannot_release:
  1044. spin_unlock(&sdp->sd_ail_lock);
  1045. gfs2_log_unlock(sdp);
  1046. return 0;
  1047. }
  1048. static const struct address_space_operations gfs2_writeback_aops = {
  1049. .writepage = gfs2_writepage,
  1050. .writepages = gfs2_writepages,
  1051. .readpage = gfs2_readpage,
  1052. .readpages = gfs2_readpages,
  1053. .write_begin = gfs2_write_begin,
  1054. .write_end = gfs2_write_end,
  1055. .bmap = gfs2_bmap,
  1056. .invalidatepage = gfs2_invalidatepage,
  1057. .releasepage = gfs2_releasepage,
  1058. .direct_IO = gfs2_direct_IO,
  1059. .migratepage = buffer_migrate_page,
  1060. .is_partially_uptodate = block_is_partially_uptodate,
  1061. .error_remove_page = generic_error_remove_page,
  1062. };
  1063. static const struct address_space_operations gfs2_ordered_aops = {
  1064. .writepage = gfs2_writepage,
  1065. .writepages = gfs2_writepages,
  1066. .readpage = gfs2_readpage,
  1067. .readpages = gfs2_readpages,
  1068. .write_begin = gfs2_write_begin,
  1069. .write_end = gfs2_write_end,
  1070. .set_page_dirty = __set_page_dirty_buffers,
  1071. .bmap = gfs2_bmap,
  1072. .invalidatepage = gfs2_invalidatepage,
  1073. .releasepage = gfs2_releasepage,
  1074. .direct_IO = gfs2_direct_IO,
  1075. .migratepage = buffer_migrate_page,
  1076. .is_partially_uptodate = block_is_partially_uptodate,
  1077. .error_remove_page = generic_error_remove_page,
  1078. };
  1079. static const struct address_space_operations gfs2_jdata_aops = {
  1080. .writepage = gfs2_jdata_writepage,
  1081. .writepages = gfs2_jdata_writepages,
  1082. .readpage = gfs2_readpage,
  1083. .readpages = gfs2_readpages,
  1084. .write_begin = gfs2_write_begin,
  1085. .write_end = gfs2_write_end,
  1086. .set_page_dirty = jdata_set_page_dirty,
  1087. .bmap = gfs2_bmap,
  1088. .invalidatepage = gfs2_invalidatepage,
  1089. .releasepage = gfs2_releasepage,
  1090. .is_partially_uptodate = block_is_partially_uptodate,
  1091. .error_remove_page = generic_error_remove_page,
  1092. };
  1093. void gfs2_set_aops(struct inode *inode)
  1094. {
  1095. struct gfs2_inode *ip = GFS2_I(inode);
  1096. if (gfs2_is_writeback(ip))
  1097. inode->i_mapping->a_ops = &gfs2_writeback_aops;
  1098. else if (gfs2_is_ordered(ip))
  1099. inode->i_mapping->a_ops = &gfs2_ordered_aops;
  1100. else if (gfs2_is_jdata(ip))
  1101. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  1102. else
  1103. BUG();
  1104. }