aops.c 19 KB

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