aops.c 32 KB

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