aops.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/completion.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/pagevec.h>
  13. #include <linux/mpage.h>
  14. #include <linux/fs.h>
  15. #include <linux/writeback.h>
  16. #include <linux/swap.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/backing-dev.h>
  19. #include <linux/uio.h>
  20. #include <trace/events/writeback.h>
  21. #include <linux/sched/signal.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "bmap.h"
  25. #include "glock.h"
  26. #include "inode.h"
  27. #include "log.h"
  28. #include "meta_io.h"
  29. #include "quota.h"
  30. #include "trans.h"
  31. #include "rgrp.h"
  32. #include "super.h"
  33. #include "util.h"
  34. #include "glops.h"
  35. #include "aops.h"
  36. void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  37. unsigned int from, unsigned int len)
  38. {
  39. struct buffer_head *head = page_buffers(page);
  40. unsigned int bsize = head->b_size;
  41. struct buffer_head *bh;
  42. unsigned int to = from + len;
  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)
  48. continue;
  49. if (start >= to)
  50. break;
  51. set_buffer_uptodate(bh);
  52. gfs2_trans_add_data(ip->i_gl, bh);
  53. }
  54. }
  55. /**
  56. * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  57. * @inode: The inode
  58. * @lblock: The block number to look up
  59. * @bh_result: The buffer head to return the result in
  60. * @create: Non-zero if we may add block to the file
  61. *
  62. * Returns: errno
  63. */
  64. static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
  65. struct buffer_head *bh_result, int create)
  66. {
  67. int error;
  68. error = gfs2_block_map(inode, lblock, bh_result, 0);
  69. if (error)
  70. return error;
  71. if (!buffer_mapped(bh_result))
  72. return -ENODATA;
  73. return 0;
  74. }
  75. /**
  76. * gfs2_write_jdata_page - gfs2 jdata-specific version of block_write_full_page
  77. * @page: The page to write
  78. * @wbc: The writeback control
  79. *
  80. * This is the same as calling block_write_full_page, but it also
  81. * writes pages outside of i_size
  82. */
  83. static int gfs2_write_jdata_page(struct page *page,
  84. struct writeback_control *wbc)
  85. {
  86. struct inode * const inode = page->mapping->host;
  87. loff_t i_size = i_size_read(inode);
  88. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  89. unsigned offset;
  90. /*
  91. * The page straddles i_size. It must be zeroed out on each and every
  92. * writepage invocation because it may be mmapped. "A file is mapped
  93. * in multiples of the page size. For a file that is not a multiple of
  94. * the page size, the remaining memory is zeroed when mapped, and
  95. * writes to that region are not written out to the file."
  96. */
  97. offset = i_size & (PAGE_SIZE - 1);
  98. if (page->index == end_index && offset)
  99. zero_user_segment(page, offset, PAGE_SIZE);
  100. return __block_write_full_page(inode, page, gfs2_get_block_noalloc, wbc,
  101. end_buffer_async_write);
  102. }
  103. /**
  104. * __gfs2_jdata_writepage - The core of jdata writepage
  105. * @page: The page to write
  106. * @wbc: The writeback control
  107. *
  108. * This is shared between writepage and writepages and implements the
  109. * core of the writepage operation. If a transaction is required then
  110. * PageChecked will have been set and the transaction will have
  111. * already been started before this is called.
  112. */
  113. static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  114. {
  115. struct inode *inode = page->mapping->host;
  116. struct gfs2_inode *ip = GFS2_I(inode);
  117. if (PageChecked(page)) {
  118. ClearPageChecked(page);
  119. if (!page_has_buffers(page)) {
  120. create_empty_buffers(page, inode->i_sb->s_blocksize,
  121. BIT(BH_Dirty)|BIT(BH_Uptodate));
  122. }
  123. gfs2_page_add_databufs(ip, page, 0, PAGE_SIZE);
  124. }
  125. return gfs2_write_jdata_page(page, wbc);
  126. }
  127. /**
  128. * gfs2_jdata_writepage - Write complete page
  129. * @page: Page to write
  130. * @wbc: The writeback control
  131. *
  132. * Returns: errno
  133. *
  134. */
  135. static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  136. {
  137. struct inode *inode = page->mapping->host;
  138. struct gfs2_inode *ip = GFS2_I(inode);
  139. struct gfs2_sbd *sdp = GFS2_SB(inode);
  140. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
  141. goto out;
  142. if (PageChecked(page) || current->journal_info)
  143. goto out_ignore;
  144. return __gfs2_jdata_writepage(page, wbc);
  145. out_ignore:
  146. redirty_page_for_writepage(wbc, page);
  147. out:
  148. unlock_page(page);
  149. return 0;
  150. }
  151. /**
  152. * gfs2_writepages - Write a bunch of dirty pages back to disk
  153. * @mapping: The mapping to write
  154. * @wbc: Write-back control
  155. *
  156. * Used for both ordered and writeback modes.
  157. */
  158. static int gfs2_writepages(struct address_space *mapping,
  159. struct writeback_control *wbc)
  160. {
  161. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  162. struct iomap_writepage_ctx wpc = { };
  163. int ret;
  164. /*
  165. * Even if we didn't write enough pages here, we might still be holding
  166. * dirty pages in the ail. We forcibly flush the ail because we don't
  167. * want balance_dirty_pages() to loop indefinitely trying to write out
  168. * pages held in the ail that it can't find.
  169. */
  170. ret = iomap_writepages(mapping, wbc, &wpc, &gfs2_writeback_ops);
  171. if (ret == 0 && wbc->nr_to_write > 0)
  172. set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
  173. return ret;
  174. }
  175. /**
  176. * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
  177. * @mapping: The mapping
  178. * @wbc: The writeback control
  179. * @pvec: The vector of pages
  180. * @nr_pages: The number of pages to write
  181. * @done_index: Page index
  182. *
  183. * Returns: non-zero if loop should terminate, zero otherwise
  184. */
  185. static int gfs2_write_jdata_pagevec(struct address_space *mapping,
  186. struct writeback_control *wbc,
  187. struct pagevec *pvec,
  188. int nr_pages,
  189. pgoff_t *done_index)
  190. {
  191. struct inode *inode = mapping->host;
  192. struct gfs2_sbd *sdp = GFS2_SB(inode);
  193. unsigned nrblocks = nr_pages * (PAGE_SIZE >> inode->i_blkbits);
  194. int i;
  195. int ret;
  196. ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
  197. if (ret < 0)
  198. return ret;
  199. for(i = 0; i < nr_pages; i++) {
  200. struct page *page = pvec->pages[i];
  201. *done_index = page->index;
  202. lock_page(page);
  203. if (unlikely(page->mapping != mapping)) {
  204. continue_unlock:
  205. unlock_page(page);
  206. continue;
  207. }
  208. if (!PageDirty(page)) {
  209. /* someone wrote it for us */
  210. goto continue_unlock;
  211. }
  212. if (PageWriteback(page)) {
  213. if (wbc->sync_mode != WB_SYNC_NONE)
  214. wait_on_page_writeback(page);
  215. else
  216. goto continue_unlock;
  217. }
  218. BUG_ON(PageWriteback(page));
  219. if (!clear_page_dirty_for_io(page))
  220. goto continue_unlock;
  221. trace_wbc_writepage(wbc, inode_to_bdi(inode));
  222. ret = __gfs2_jdata_writepage(page, wbc);
  223. if (unlikely(ret)) {
  224. if (ret == AOP_WRITEPAGE_ACTIVATE) {
  225. unlock_page(page);
  226. ret = 0;
  227. } else {
  228. /*
  229. * done_index is set past this page,
  230. * so media errors will not choke
  231. * background writeout for the entire
  232. * file. This has consequences for
  233. * range_cyclic semantics (ie. it may
  234. * not be suitable for data integrity
  235. * writeout).
  236. */
  237. *done_index = page->index + 1;
  238. ret = 1;
  239. break;
  240. }
  241. }
  242. /*
  243. * We stop writing back only if we are not doing
  244. * integrity sync. In case of integrity sync we have to
  245. * keep going until we have written all the pages
  246. * we tagged for writeback prior to entering this loop.
  247. */
  248. if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
  249. ret = 1;
  250. break;
  251. }
  252. }
  253. gfs2_trans_end(sdp);
  254. return ret;
  255. }
  256. /**
  257. * gfs2_write_cache_jdata - Like write_cache_pages but different
  258. * @mapping: The mapping to write
  259. * @wbc: The writeback control
  260. *
  261. * The reason that we use our own function here is that we need to
  262. * start transactions before we grab page locks. This allows us
  263. * to get the ordering right.
  264. */
  265. static int gfs2_write_cache_jdata(struct address_space *mapping,
  266. struct writeback_control *wbc)
  267. {
  268. int ret = 0;
  269. int done = 0;
  270. struct pagevec pvec;
  271. int nr_pages;
  272. pgoff_t writeback_index;
  273. pgoff_t index;
  274. pgoff_t end;
  275. pgoff_t done_index;
  276. int cycled;
  277. int range_whole = 0;
  278. xa_mark_t tag;
  279. pagevec_init(&pvec);
  280. if (wbc->range_cyclic) {
  281. writeback_index = mapping->writeback_index; /* prev offset */
  282. index = writeback_index;
  283. if (index == 0)
  284. cycled = 1;
  285. else
  286. cycled = 0;
  287. end = -1;
  288. } else {
  289. index = wbc->range_start >> PAGE_SHIFT;
  290. end = wbc->range_end >> PAGE_SHIFT;
  291. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  292. range_whole = 1;
  293. cycled = 1; /* ignore range_cyclic tests */
  294. }
  295. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  296. tag = PAGECACHE_TAG_TOWRITE;
  297. else
  298. tag = PAGECACHE_TAG_DIRTY;
  299. retry:
  300. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  301. tag_pages_for_writeback(mapping, index, end);
  302. done_index = index;
  303. while (!done && (index <= end)) {
  304. nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
  305. tag);
  306. if (nr_pages == 0)
  307. break;
  308. ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, &done_index);
  309. if (ret)
  310. done = 1;
  311. if (ret > 0)
  312. ret = 0;
  313. pagevec_release(&pvec);
  314. cond_resched();
  315. }
  316. if (!cycled && !done) {
  317. /*
  318. * range_cyclic:
  319. * We hit the last page and there is more work to be done: wrap
  320. * back to the start of the file
  321. */
  322. cycled = 1;
  323. index = 0;
  324. end = writeback_index - 1;
  325. goto retry;
  326. }
  327. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  328. mapping->writeback_index = done_index;
  329. return ret;
  330. }
  331. /**
  332. * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
  333. * @mapping: The mapping to write
  334. * @wbc: The writeback control
  335. *
  336. */
  337. static int gfs2_jdata_writepages(struct address_space *mapping,
  338. struct writeback_control *wbc)
  339. {
  340. struct gfs2_inode *ip = GFS2_I(mapping->host);
  341. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  342. int ret;
  343. ret = gfs2_write_cache_jdata(mapping, wbc);
  344. if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
  345. gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
  346. GFS2_LFC_JDATA_WPAGES);
  347. ret = gfs2_write_cache_jdata(mapping, wbc);
  348. }
  349. return ret;
  350. }
  351. /**
  352. * stuffed_readpage - Fill in a Linux page with stuffed file data
  353. * @ip: the inode
  354. * @page: the page
  355. *
  356. * Returns: errno
  357. */
  358. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  359. {
  360. struct buffer_head *dibh;
  361. u64 dsize = i_size_read(&ip->i_inode);
  362. void *kaddr;
  363. int error;
  364. /*
  365. * Due to the order of unstuffing files and ->fault(), we can be
  366. * asked for a zero page in the case of a stuffed file being extended,
  367. * so we need to supply one here. It doesn't happen often.
  368. */
  369. if (unlikely(page->index)) {
  370. zero_user(page, 0, PAGE_SIZE);
  371. SetPageUptodate(page);
  372. return 0;
  373. }
  374. error = gfs2_meta_inode_buffer(ip, &dibh);
  375. if (error)
  376. return error;
  377. kaddr = kmap_atomic(page);
  378. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
  379. memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
  380. kunmap_atomic(kaddr);
  381. flush_dcache_page(page);
  382. brelse(dibh);
  383. SetPageUptodate(page);
  384. return 0;
  385. }
  386. /**
  387. * gfs2_read_folio - read a folio from a file
  388. * @file: The file to read
  389. * @folio: The folio in the file
  390. */
  391. static int gfs2_read_folio(struct file *file, struct folio *folio)
  392. {
  393. struct inode *inode = folio->mapping->host;
  394. struct gfs2_inode *ip = GFS2_I(inode);
  395. struct gfs2_sbd *sdp = GFS2_SB(inode);
  396. int error;
  397. if (!gfs2_is_jdata(ip) ||
  398. (i_blocksize(inode) == PAGE_SIZE && !folio_buffers(folio))) {
  399. error = iomap_read_folio(folio, &gfs2_iomap_ops);
  400. } else if (gfs2_is_stuffed(ip)) {
  401. error = stuffed_readpage(ip, &folio->page);
  402. folio_unlock(folio);
  403. } else {
  404. error = mpage_read_folio(folio, gfs2_block_map);
  405. }
  406. if (unlikely(gfs2_withdrawn(sdp)))
  407. return -EIO;
  408. return error;
  409. }
  410. /**
  411. * gfs2_internal_read - read an internal file
  412. * @ip: The gfs2 inode
  413. * @buf: The buffer to fill
  414. * @pos: The file position
  415. * @size: The amount to read
  416. *
  417. */
  418. int gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
  419. unsigned size)
  420. {
  421. struct address_space *mapping = ip->i_inode.i_mapping;
  422. unsigned long index = *pos >> PAGE_SHIFT;
  423. unsigned offset = *pos & (PAGE_SIZE - 1);
  424. unsigned copied = 0;
  425. unsigned amt;
  426. struct page *page;
  427. void *p;
  428. do {
  429. amt = size - copied;
  430. if (offset + size > PAGE_SIZE)
  431. amt = PAGE_SIZE - offset;
  432. page = read_cache_page(mapping, index, gfs2_read_folio, NULL);
  433. if (IS_ERR(page))
  434. return PTR_ERR(page);
  435. p = kmap_atomic(page);
  436. memcpy(buf + copied, p + offset, amt);
  437. kunmap_atomic(p);
  438. put_page(page);
  439. copied += amt;
  440. index++;
  441. offset = 0;
  442. } while(copied < size);
  443. (*pos) += size;
  444. return size;
  445. }
  446. /**
  447. * gfs2_readahead - Read a bunch of pages at once
  448. * @rac: Read-ahead control structure
  449. *
  450. * Some notes:
  451. * 1. This is only for readahead, so we can simply ignore any things
  452. * which are slightly inconvenient (such as locking conflicts between
  453. * the page lock and the glock) and return having done no I/O. Its
  454. * obviously not something we'd want to do on too regular a basis.
  455. * Any I/O we ignore at this time will be done via readpage later.
  456. * 2. We don't handle stuffed files here we let readpage do the honours.
  457. * 3. mpage_readahead() does most of the heavy lifting in the common case.
  458. * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
  459. */
  460. static void gfs2_readahead(struct readahead_control *rac)
  461. {
  462. struct inode *inode = rac->mapping->host;
  463. struct gfs2_inode *ip = GFS2_I(inode);
  464. if (gfs2_is_stuffed(ip))
  465. ;
  466. else if (gfs2_is_jdata(ip))
  467. mpage_readahead(rac, gfs2_block_map);
  468. else
  469. iomap_readahead(rac, &gfs2_iomap_ops);
  470. }
  471. /**
  472. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  473. * @inode: the rindex inode
  474. */
  475. void adjust_fs_space(struct inode *inode)
  476. {
  477. struct gfs2_sbd *sdp = GFS2_SB(inode);
  478. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  479. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  480. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  481. struct buffer_head *m_bh;
  482. u64 fs_total, new_free;
  483. if (gfs2_trans_begin(sdp, 2 * RES_STATFS, 0) != 0)
  484. return;
  485. /* Total up the file system space, according to the latest rindex. */
  486. fs_total = gfs2_ri_total(sdp);
  487. if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
  488. goto out;
  489. spin_lock(&sdp->sd_statfs_spin);
  490. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  491. sizeof(struct gfs2_dinode));
  492. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  493. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  494. else
  495. new_free = 0;
  496. spin_unlock(&sdp->sd_statfs_spin);
  497. fs_warn(sdp, "File system extended by %llu blocks.\n",
  498. (unsigned long long)new_free);
  499. gfs2_statfs_change(sdp, new_free, new_free, 0);
  500. update_statfs(sdp, m_bh);
  501. brelse(m_bh);
  502. out:
  503. sdp->sd_rindex_uptodate = 0;
  504. gfs2_trans_end(sdp);
  505. }
  506. static bool jdata_dirty_folio(struct address_space *mapping,
  507. struct folio *folio)
  508. {
  509. if (current->journal_info)
  510. folio_set_checked(folio);
  511. return block_dirty_folio(mapping, folio);
  512. }
  513. /**
  514. * gfs2_bmap - Block map function
  515. * @mapping: Address space info
  516. * @lblock: The block to map
  517. *
  518. * Returns: The disk address for the block or 0 on hole or error
  519. */
  520. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  521. {
  522. struct gfs2_inode *ip = GFS2_I(mapping->host);
  523. struct gfs2_holder i_gh;
  524. sector_t dblock = 0;
  525. int error;
  526. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  527. if (error)
  528. return 0;
  529. if (!gfs2_is_stuffed(ip))
  530. dblock = iomap_bmap(mapping, lblock, &gfs2_iomap_ops);
  531. gfs2_glock_dq_uninit(&i_gh);
  532. return dblock;
  533. }
  534. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  535. {
  536. struct gfs2_bufdata *bd;
  537. lock_buffer(bh);
  538. gfs2_log_lock(sdp);
  539. clear_buffer_dirty(bh);
  540. bd = bh->b_private;
  541. if (bd) {
  542. if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
  543. list_del_init(&bd->bd_list);
  544. else {
  545. spin_lock(&sdp->sd_ail_lock);
  546. gfs2_remove_from_journal(bh, REMOVE_JDATA);
  547. spin_unlock(&sdp->sd_ail_lock);
  548. }
  549. }
  550. bh->b_bdev = NULL;
  551. clear_buffer_mapped(bh);
  552. clear_buffer_req(bh);
  553. clear_buffer_new(bh);
  554. gfs2_log_unlock(sdp);
  555. unlock_buffer(bh);
  556. }
  557. static void gfs2_invalidate_folio(struct folio *folio, size_t offset,
  558. size_t length)
  559. {
  560. struct gfs2_sbd *sdp = GFS2_SB(folio->mapping->host);
  561. size_t stop = offset + length;
  562. int partial_page = (offset || length < folio_size(folio));
  563. struct buffer_head *bh, *head;
  564. unsigned long pos = 0;
  565. BUG_ON(!folio_test_locked(folio));
  566. if (!partial_page)
  567. folio_clear_checked(folio);
  568. head = folio_buffers(folio);
  569. if (!head)
  570. goto out;
  571. bh = head;
  572. do {
  573. if (pos + bh->b_size > stop)
  574. return;
  575. if (offset <= pos)
  576. gfs2_discard(sdp, bh);
  577. pos += bh->b_size;
  578. bh = bh->b_this_page;
  579. } while (bh != head);
  580. out:
  581. if (!partial_page)
  582. filemap_release_folio(folio, 0);
  583. }
  584. /**
  585. * gfs2_release_folio - free the metadata associated with a folio
  586. * @folio: the folio that's being released
  587. * @gfp_mask: passed from Linux VFS, ignored by us
  588. *
  589. * Calls try_to_free_buffers() to free the buffers and put the folio if the
  590. * buffers can be released.
  591. *
  592. * Returns: true if the folio was put or else false
  593. */
  594. bool gfs2_release_folio(struct folio *folio, gfp_t gfp_mask)
  595. {
  596. struct address_space *mapping = folio->mapping;
  597. struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
  598. struct buffer_head *bh, *head;
  599. struct gfs2_bufdata *bd;
  600. head = folio_buffers(folio);
  601. if (!head)
  602. return false;
  603. /*
  604. * mm accommodates an old ext3 case where clean folios might
  605. * not have had the dirty bit cleared. Thus, it can send actual
  606. * dirty folios to ->release_folio() via shrink_active_list().
  607. *
  608. * As a workaround, we skip folios that contain dirty buffers
  609. * below. Once ->release_folio isn't called on dirty folios
  610. * anymore, we can warn on dirty buffers like we used to here
  611. * again.
  612. */
  613. gfs2_log_lock(sdp);
  614. bh = head;
  615. do {
  616. if (atomic_read(&bh->b_count))
  617. goto cannot_release;
  618. bd = bh->b_private;
  619. if (bd && bd->bd_tr)
  620. goto cannot_release;
  621. if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
  622. goto cannot_release;
  623. bh = bh->b_this_page;
  624. } while (bh != head);
  625. bh = head;
  626. do {
  627. bd = bh->b_private;
  628. if (bd) {
  629. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  630. bd->bd_bh = NULL;
  631. bh->b_private = NULL;
  632. /*
  633. * The bd may still be queued as a revoke, in which
  634. * case we must not dequeue nor free it.
  635. */
  636. if (!bd->bd_blkno && !list_empty(&bd->bd_list))
  637. list_del_init(&bd->bd_list);
  638. if (list_empty(&bd->bd_list))
  639. kmem_cache_free(gfs2_bufdata_cachep, bd);
  640. }
  641. bh = bh->b_this_page;
  642. } while (bh != head);
  643. gfs2_log_unlock(sdp);
  644. return try_to_free_buffers(folio);
  645. cannot_release:
  646. gfs2_log_unlock(sdp);
  647. return false;
  648. }
  649. static const struct address_space_operations gfs2_aops = {
  650. .writepages = gfs2_writepages,
  651. .read_folio = gfs2_read_folio,
  652. .readahead = gfs2_readahead,
  653. .dirty_folio = filemap_dirty_folio,
  654. .release_folio = iomap_release_folio,
  655. .invalidate_folio = iomap_invalidate_folio,
  656. .bmap = gfs2_bmap,
  657. .direct_IO = noop_direct_IO,
  658. .migrate_folio = filemap_migrate_folio,
  659. .is_partially_uptodate = iomap_is_partially_uptodate,
  660. .error_remove_page = generic_error_remove_page,
  661. };
  662. static const struct address_space_operations gfs2_jdata_aops = {
  663. .writepage = gfs2_jdata_writepage,
  664. .writepages = gfs2_jdata_writepages,
  665. .read_folio = gfs2_read_folio,
  666. .readahead = gfs2_readahead,
  667. .dirty_folio = jdata_dirty_folio,
  668. .bmap = gfs2_bmap,
  669. .invalidate_folio = gfs2_invalidate_folio,
  670. .release_folio = gfs2_release_folio,
  671. .is_partially_uptodate = block_is_partially_uptodate,
  672. .error_remove_page = generic_error_remove_page,
  673. };
  674. void gfs2_set_aops(struct inode *inode)
  675. {
  676. if (gfs2_is_jdata(GFS2_I(inode)))
  677. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  678. else
  679. inode->i_mapping->a_ops = &gfs2_aops;
  680. }