gcinode.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Value: On success, 0 is returned. On Error, one of the following
  48. * negative error code is returned.
  49. *
  50. * %-EIO - I/O error.
  51. *
  52. * %-ENOMEM - Insufficient amount of memory available.
  53. *
  54. * %-ENOENT - The block specified with @pbn does not exist.
  55. */
  56. int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  57. sector_t pbn, __u64 vbn,
  58. struct buffer_head **out_bh)
  59. {
  60. struct buffer_head *bh;
  61. int err;
  62. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  63. if (unlikely(!bh))
  64. return -ENOMEM;
  65. if (buffer_uptodate(bh))
  66. goto out;
  67. if (pbn == 0) {
  68. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  69. err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
  70. if (unlikely(err)) /* -EIO, -ENOMEM, -ENOENT */
  71. goto failed;
  72. }
  73. lock_buffer(bh);
  74. if (buffer_uptodate(bh)) {
  75. unlock_buffer(bh);
  76. goto out;
  77. }
  78. if (!buffer_mapped(bh))
  79. set_buffer_mapped(bh);
  80. bh->b_blocknr = pbn;
  81. bh->b_end_io = end_buffer_read_sync;
  82. get_bh(bh);
  83. submit_bh(REQ_OP_READ, bh);
  84. if (vbn)
  85. bh->b_blocknr = vbn;
  86. out:
  87. err = 0;
  88. *out_bh = bh;
  89. failed:
  90. folio_unlock(bh->b_folio);
  91. folio_put(bh->b_folio);
  92. if (unlikely(err))
  93. brelse(bh);
  94. return err;
  95. }
  96. /*
  97. * nilfs_gccache_submit_read_node() - add node buffer and submit read request
  98. * @inode - gc inode
  99. * @pbn - physical block number for the block
  100. * @vbn - virtual block number for the block
  101. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  102. *
  103. * Description: nilfs_gccache_submit_read_node() registers the node buffer
  104. * specified by @vbn to the GC pagecache. @pbn can be supplied by the
  105. * caller to avoid translation of the disk block address.
  106. *
  107. * Return Value: On success, 0 is returned. On Error, one of the following
  108. * negative error code is returned.
  109. *
  110. * %-EIO - I/O error.
  111. *
  112. * %-ENOMEM - Insufficient amount of memory available.
  113. */
  114. int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
  115. __u64 vbn, struct buffer_head **out_bh)
  116. {
  117. struct inode *btnc_inode = NILFS_I(inode)->i_assoc_inode;
  118. int ret;
  119. ret = nilfs_btnode_submit_block(btnc_inode->i_mapping, vbn ? : pbn, pbn,
  120. REQ_OP_READ, out_bh, &pbn);
  121. if (ret == -EEXIST) /* internal code (cache hit) */
  122. ret = 0;
  123. return ret;
  124. }
  125. int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
  126. {
  127. wait_on_buffer(bh);
  128. if (!buffer_uptodate(bh)) {
  129. struct inode *inode = bh->b_folio->mapping->host;
  130. nilfs_err(inode->i_sb,
  131. "I/O error reading %s block for GC (ino=%lu, vblocknr=%llu)",
  132. buffer_nilfs_node(bh) ? "node" : "data",
  133. inode->i_ino, (unsigned long long)bh->b_blocknr);
  134. return -EIO;
  135. }
  136. if (buffer_dirty(bh))
  137. return -EEXIST;
  138. if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
  139. clear_buffer_uptodate(bh);
  140. return -EIO;
  141. }
  142. mark_buffer_dirty(bh);
  143. return 0;
  144. }
  145. int nilfs_init_gcinode(struct inode *inode)
  146. {
  147. struct nilfs_inode_info *ii = NILFS_I(inode);
  148. inode->i_mode = S_IFREG;
  149. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  150. inode->i_mapping->a_ops = &nilfs_buffer_cache_aops;
  151. ii->i_flags = 0;
  152. nilfs_bmap_init_gc(ii->i_bmap);
  153. return nilfs_attach_btree_node_cache(inode);
  154. }
  155. /**
  156. * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
  157. * @nilfs: NILFS filesystem instance
  158. */
  159. void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
  160. {
  161. struct list_head *head = &nilfs->ns_gc_inodes;
  162. struct nilfs_inode_info *ii;
  163. while (!list_empty(head)) {
  164. ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
  165. list_del_init(&ii->i_dirty);
  166. truncate_inode_pages(&ii->vfs_inode.i_data, 0);
  167. nilfs_btnode_cache_clear(ii->i_assoc_inode->i_mapping);
  168. iput(&ii->vfs_inode);
  169. }
  170. }