aops.c 31 KB

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