gcinode.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * gcinode.c - 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. brelse(bh);
  72. goto failed;
  73. }
  74. }
  75. lock_buffer(bh);
  76. if (buffer_uptodate(bh)) {
  77. unlock_buffer(bh);
  78. goto out;
  79. }
  80. if (!buffer_mapped(bh)) {
  81. bh->b_bdev = inode->i_sb->s_bdev;
  82. set_buffer_mapped(bh);
  83. }
  84. bh->b_blocknr = pbn;
  85. bh->b_end_io = end_buffer_read_sync;
  86. get_bh(bh);
  87. submit_bh(REQ_OP_READ, 0, bh);
  88. if (vbn)
  89. bh->b_blocknr = vbn;
  90. out:
  91. err = 0;
  92. *out_bh = bh;
  93. failed:
  94. unlock_page(bh->b_page);
  95. put_page(bh->b_page);
  96. return err;
  97. }
  98. /*
  99. * nilfs_gccache_submit_read_node() - add node buffer and submit read request
  100. * @inode - gc inode
  101. * @pbn - physical block number for the block
  102. * @vbn - virtual block number for the block
  103. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  104. *
  105. * Description: nilfs_gccache_submit_read_node() registers the node buffer
  106. * specified by @vbn to the GC pagecache. @pbn can be supplied by the
  107. * caller to avoid translation of the disk block address.
  108. *
  109. * Return Value: On success, 0 is returned. On Error, one of the following
  110. * negative error code is returned.
  111. *
  112. * %-EIO - I/O error.
  113. *
  114. * %-ENOMEM - Insufficient amount of memory available.
  115. */
  116. int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
  117. __u64 vbn, struct buffer_head **out_bh)
  118. {
  119. int ret;
  120. ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
  121. vbn ? : pbn, pbn, REQ_OP_READ, 0,
  122. out_bh, &pbn);
  123. if (ret == -EEXIST) /* internal code (cache hit) */
  124. ret = 0;
  125. return ret;
  126. }
  127. int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
  128. {
  129. wait_on_buffer(bh);
  130. if (!buffer_uptodate(bh)) {
  131. struct inode *inode = bh->b_page->mapping->host;
  132. nilfs_err(inode->i_sb,
  133. "I/O error reading %s block for GC (ino=%lu, vblocknr=%llu)",
  134. buffer_nilfs_node(bh) ? "node" : "data",
  135. inode->i_ino, (unsigned long long)bh->b_blocknr);
  136. return -EIO;
  137. }
  138. if (buffer_dirty(bh))
  139. return -EEXIST;
  140. if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
  141. clear_buffer_uptodate(bh);
  142. return -EIO;
  143. }
  144. mark_buffer_dirty(bh);
  145. return 0;
  146. }
  147. int nilfs_init_gcinode(struct inode *inode)
  148. {
  149. struct nilfs_inode_info *ii = NILFS_I(inode);
  150. inode->i_mode = S_IFREG;
  151. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  152. inode->i_mapping->a_ops = &empty_aops;
  153. ii->i_flags = 0;
  154. nilfs_bmap_init_gc(ii->i_bmap);
  155. return 0;
  156. }
  157. /**
  158. * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
  159. */
  160. void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
  161. {
  162. struct list_head *head = &nilfs->ns_gc_inodes;
  163. struct nilfs_inode_info *ii;
  164. while (!list_empty(head)) {
  165. ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
  166. list_del_init(&ii->i_dirty);
  167. truncate_inode_pages(&ii->vfs_inode.i_data, 0);
  168. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  169. iput(&ii->vfs_inode);
  170. }
  171. }