gcinode.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Dummy inodes to buffer blocks for garbage collection
  4. *
  5. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Seiji Kihara, Amagai Yoshiji, and Ryusuke Konishi.
  8. * Revised by Ryusuke Konishi.
  9. *
  10. */
  11. /*
  12. * This file adds the cache of on-disk blocks to be moved in garbage
  13. * collection. The disk blocks are held with dummy inodes (called
  14. * gcinodes), and this file provides lookup function of the dummy
  15. * inodes and their buffer read function.
  16. *
  17. * Buffers and pages held by the dummy inodes will be released each
  18. * time after they are copied to a new log. Dirty blocks made on the
  19. * current generation and the blocks to be moved by GC never overlap
  20. * because the dirty blocks make a new generation; they rather must be
  21. * written individually.
  22. */
  23. #include <linux/buffer_head.h>
  24. #include <linux/mpage.h>
  25. #include <linux/hash.h>
  26. #include <linux/slab.h>
  27. #include <linux/swap.h>
  28. #include "nilfs.h"
  29. #include "btree.h"
  30. #include "btnode.h"
  31. #include "page.h"
  32. #include "mdt.h"
  33. #include "dat.h"
  34. #include "ifile.h"
  35. /*
  36. * nilfs_gccache_submit_read_data() - add data buffer and submit read request
  37. * @inode - gc inode
  38. * @blkoff - dummy offset treated as the key for the page cache
  39. * @pbn - physical block number of the block
  40. * @vbn - virtual block number of the block, 0 for non-virtual block
  41. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  42. *
  43. * Description: nilfs_gccache_submit_read_data() registers the data buffer
  44. * specified by @pbn to the GC pagecache with the key @blkoff.
  45. * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
  46. *
  47. * Return: 0 on success, or one of the following negative error codes on
  48. * failure:
  49. * * %-EIO - I/O error (including metadata corruption).
  50. * * %-ENOENT - The block specified with @pbn does not exist.
  51. * * %-ENOMEM - Insufficient memory available.
  52. */
  53. int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  54. sector_t pbn, __u64 vbn,
  55. struct buffer_head **out_bh)
  56. {
  57. struct buffer_head *bh;
  58. int err;
  59. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  60. if (unlikely(!bh))
  61. return -ENOMEM;
  62. if (buffer_uptodate(bh))
  63. goto out;
  64. if (pbn == 0) {
  65. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  66. err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
  67. if (unlikely(err)) /* -EIO, -ENOMEM, -ENOENT */
  68. goto failed;
  69. }
  70. lock_buffer(bh);
  71. if (buffer_uptodate(bh)) {
  72. unlock_buffer(bh);
  73. goto out;
  74. }
  75. if (!buffer_mapped(bh))
  76. set_buffer_mapped(bh);
  77. bh->b_blocknr = pbn;
  78. bh->b_end_io = end_buffer_read_sync;
  79. get_bh(bh);
  80. submit_bh(REQ_OP_READ, bh);
  81. if (vbn)
  82. bh->b_blocknr = vbn;
  83. out:
  84. err = 0;
  85. *out_bh = bh;
  86. failed:
  87. folio_unlock(bh->b_folio);
  88. folio_put(bh->b_folio);
  89. if (unlikely(err))
  90. brelse(bh);
  91. return err;
  92. }
  93. /*
  94. * nilfs_gccache_submit_read_node() - add node buffer and submit read request
  95. * @inode - gc inode
  96. * @pbn - physical block number for the block
  97. * @vbn - virtual block number for the block
  98. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  99. *
  100. * Description: nilfs_gccache_submit_read_node() registers the node buffer
  101. * specified by @vbn to the GC pagecache. @pbn can be supplied by the
  102. * caller to avoid translation of the disk block address.
  103. *
  104. * Return: 0 on success, or one of the following negative error codes on
  105. * failure:
  106. * * %-EIO - I/O error (including metadata corruption).
  107. * * %-ENOENT - Invalid virtual block address.
  108. * * %-ENOMEM - Insufficient memory available.
  109. */
  110. int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
  111. __u64 vbn, struct buffer_head **out_bh)
  112. {
  113. struct inode *btnc_inode = NILFS_I(inode)->i_assoc_inode;
  114. int ret;
  115. ret = nilfs_btnode_submit_block(btnc_inode->i_mapping, vbn ? : pbn, pbn,
  116. REQ_OP_READ, out_bh, &pbn);
  117. if (ret == -EEXIST) /* internal code (cache hit) */
  118. ret = 0;
  119. return ret;
  120. }
  121. int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
  122. {
  123. wait_on_buffer(bh);
  124. if (!buffer_uptodate(bh)) {
  125. struct inode *inode = bh->b_folio->mapping->host;
  126. nilfs_err(inode->i_sb,
  127. "I/O error reading %s block for GC (ino=%lu, vblocknr=%llu)",
  128. buffer_nilfs_node(bh) ? "node" : "data",
  129. inode->i_ino, (unsigned long long)bh->b_blocknr);
  130. return -EIO;
  131. }
  132. if (buffer_dirty(bh))
  133. return -EEXIST;
  134. if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
  135. clear_buffer_uptodate(bh);
  136. return -EIO;
  137. }
  138. mark_buffer_dirty(bh);
  139. return 0;
  140. }
  141. int nilfs_init_gcinode(struct inode *inode)
  142. {
  143. struct nilfs_inode_info *ii = NILFS_I(inode);
  144. inode->i_mode = S_IFREG;
  145. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  146. inode->i_mapping->a_ops = &nilfs_buffer_cache_aops;
  147. ii->i_flags = 0;
  148. nilfs_bmap_init_gc(ii->i_bmap);
  149. return nilfs_attach_btree_node_cache(inode);
  150. }
  151. /**
  152. * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
  153. * @nilfs: NILFS filesystem instance
  154. */
  155. void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
  156. {
  157. struct list_head *head = &nilfs->ns_gc_inodes;
  158. struct nilfs_inode_info *ii;
  159. while (!list_empty(head)) {
  160. ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
  161. list_del_init(&ii->i_dirty);
  162. truncate_inode_pages(&ii->vfs_inode.i_data, 0);
  163. nilfs_btnode_cache_clear(ii->i_assoc_inode->i_mapping);
  164. iput(&ii->vfs_inode);
  165. }
  166. }