ae98043f5f7fa (Ryusuke Konishi 2018-09-04 15:46:30 -0700 1) // SPDX-License-Identifier: GPL-2.0+
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 2) /*
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 3) * gcinode.c - dummy inodes to buffer blocks for garbage collection
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 4) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 5) * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 6) *
4b420ab4eedc7 (Ryusuke Konishi 2016-05-23 16:23:09 -0700 7) * Written by Seiji Kihara, Amagai Yoshiji, and Ryusuke Konishi.
4b420ab4eedc7 (Ryusuke Konishi 2016-05-23 16:23:09 -0700 8) * Revised by Ryusuke Konishi.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 9) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 10) */
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 11) /*
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 12) * This file adds the cache of on-disk blocks to be moved in garbage
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 13) * collection. The disk blocks are held with dummy inodes (called
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 14) * gcinodes), and this file provides lookup function of the dummy
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 15) * inodes and their buffer read function.
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 16) *
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 17) * Buffers and pages held by the dummy inodes will be released each
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 18) * time after they are copied to a new log. Dirty blocks made on the
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 19) * current generation and the blocks to be moved by GC never overlap
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 20) * because the dirty blocks make a new generation; they rather must be
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 21) * written individually.
047180f2d7bf9 (Ryusuke Konishi 2009-04-06 19:01:45 -0700 22) */
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 23)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 24) #include <linux/buffer_head.h>
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 25) #include <linux/mpage.h>
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 26) #include <linux/hash.h>
5a0e3ad6af866 (Tejun Heo 2010-03-24 17:04:11 +0900 27) #include <linux/slab.h>
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 28) #include <linux/swap.h>
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 29) #include "nilfs.h"
05d0e94b66dbd (Ryusuke Konishi 2010-07-10 20:52:09 +0900 30) #include "btree.h"
05d0e94b66dbd (Ryusuke Konishi 2010-07-10 20:52:09 +0900 31) #include "btnode.h"
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 32) #include "page.h"
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 33) #include "mdt.h"
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 34) #include "dat.h"
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 35) #include "ifile.h"
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 36)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 37) /*
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 38) * nilfs_gccache_submit_read_data() - add data buffer and submit read request
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 39) * @inode - gc inode
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 40) * @blkoff - dummy offset treated as the key for the page cache
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 41) * @pbn - physical block number of the block
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 42) * @vbn - virtual block number of the block, 0 for non-virtual block
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 43) * @out_bh - indirect pointer to a buffer_head struct to receive the results
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 44) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 45) * Description: nilfs_gccache_submit_read_data() registers the data buffer
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 46) * specified by @pbn to the GC pagecache with the key @blkoff.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 47) * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 48) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 49) * Return Value: On success, 0 is returned. On Error, one of the following
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 50) * negative error code is returned.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 51) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 52) * %-EIO - I/O error.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 53) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 54) * %-ENOMEM - Insufficient amount of memory available.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 55) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 56) * %-ENOENT - The block specified with @pbn does not exist.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 57) */
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 58) int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 59) sector_t pbn, __u64 vbn,
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 60) struct buffer_head **out_bh)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 61) {
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 62) struct buffer_head *bh;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 63) int err;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 64)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 65) bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 66) if (unlikely(!bh))
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 67) return -ENOMEM;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 68)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 69) if (buffer_uptodate(bh))
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 70) goto out;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 71)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 72) if (pbn == 0) {
0ef28f9aec4dc (Ryusuke Konishi 2011-05-05 12:56:51 +0900 73) struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
0ef28f9aec4dc (Ryusuke Konishi 2011-05-05 12:56:51 +0900 74)
0ef28f9aec4dc (Ryusuke Konishi 2011-05-05 12:56:51 +0900 75) err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 76) if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 77) brelse(bh);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 78) goto failed;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 79) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 80) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 81)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 82) lock_buffer(bh);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 83) if (buffer_uptodate(bh)) {
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 84) unlock_buffer(bh);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 85) goto out;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 86) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 87)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 88) if (!buffer_mapped(bh)) {
0ef28f9aec4dc (Ryusuke Konishi 2011-05-05 12:56:51 +0900 89) bh->b_bdev = inode->i_sb->s_bdev;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 90) set_buffer_mapped(bh);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 91) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 92) bh->b_blocknr = pbn;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 93) bh->b_end_io = end_buffer_read_sync;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 94) get_bh(bh);
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 95) submit_bh(REQ_OP_READ, 0, bh);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 96) if (vbn)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 97) bh->b_blocknr = vbn;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 98) out:
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 99) err = 0;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 100) *out_bh = bh;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 101)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 102) failed:
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 103) unlock_page(bh->b_page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 104) put_page(bh->b_page);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 105) return err;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 106) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 107)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 108) /*
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 109) * nilfs_gccache_submit_read_node() - add node buffer and submit read request
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 110) * @inode - gc inode
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 111) * @pbn - physical block number for the block
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 112) * @vbn - virtual block number for the block
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 113) * @out_bh - indirect pointer to a buffer_head struct to receive the results
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 114) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 115) * Description: nilfs_gccache_submit_read_node() registers the node buffer
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 116) * specified by @vbn to the GC pagecache. @pbn can be supplied by the
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 117) * caller to avoid translation of the disk block address.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 118) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 119) * Return Value: On success, 0 is returned. On Error, one of the following
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 120) * negative error code is returned.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 121) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 122) * %-EIO - I/O error.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 123) *
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 124) * %-ENOMEM - Insufficient amount of memory available.
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 125) */
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 126) int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 127) __u64 vbn, struct buffer_head **out_bh)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 128) {
26dfdd8e29f28 (Ryusuke Konishi 2010-07-18 10:42:23 +0900 129) int ret;
26dfdd8e29f28 (Ryusuke Konishi 2010-07-18 10:42:23 +0900 130)
26dfdd8e29f28 (Ryusuke Konishi 2010-07-18 10:42:23 +0900 131) ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 132) vbn ? : pbn, pbn, REQ_OP_READ, 0,
2a222ca992c35 (Mike Christie 2016-06-05 14:31:43 -0500 133) out_bh, &pbn);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 134) if (ret == -EEXIST) /* internal code (cache hit) */
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 135) ret = 0;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 136) return ret;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 137) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 138)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 139) int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 140) {
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 141) wait_on_buffer(bh);
39a9dcca61a3d (Ryusuke Konishi 2016-08-02 14:05:17 -0700 142) if (!buffer_uptodate(bh)) {
39a9dcca61a3d (Ryusuke Konishi 2016-08-02 14:05:17 -0700 143) struct inode *inode = bh->b_page->mapping->host;
39a9dcca61a3d (Ryusuke Konishi 2016-08-02 14:05:17 -0700 144)
a1d0747a393a0 (Joe Perches 2020-08-11 18:35:49 -0700 145) nilfs_err(inode->i_sb,
39a9dcca61a3d (Ryusuke Konishi 2016-08-02 14:05:17 -0700 146) "I/O error reading %s block for GC (ino=%lu, vblocknr=%llu)",
39a9dcca61a3d (Ryusuke Konishi 2016-08-02 14:05:17 -0700 147) buffer_nilfs_node(bh) ? "node" : "data",
39a9dcca61a3d (Ryusuke Konishi 2016-08-02 14:05:17 -0700 148) inode->i_ino, (unsigned long long)bh->b_blocknr);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 149) return -EIO;
39a9dcca61a3d (Ryusuke Konishi 2016-08-02 14:05:17 -0700 150) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 151) if (buffer_dirty(bh))
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 152) return -EEXIST;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 153)
5fc7b14177b1a (Ryusuke Konishi 2011-05-05 12:56:51 +0900 154) if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
5fc7b14177b1a (Ryusuke Konishi 2011-05-05 12:56:51 +0900 155) clear_buffer_uptodate(bh);
5fc7b14177b1a (Ryusuke Konishi 2011-05-05 12:56:51 +0900 156) return -EIO;
1d5385b9f30ae (Ryusuke Konishi 2010-07-16 23:52:40 +0900 157) }
5fc7b14177b1a (Ryusuke Konishi 2011-05-05 12:56:51 +0900 158) mark_buffer_dirty(bh);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 159) return 0;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 160) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 161)
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 162) int nilfs_init_gcinode(struct inode *inode)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 163) {
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 164) struct nilfs_inode_info *ii = NILFS_I(inode);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 165)
adbb39b5485b7 (Ryusuke Konishi 2010-09-05 10:14:43 +0900 166) inode->i_mode = S_IFREG;
adbb39b5485b7 (Ryusuke Konishi 2010-09-05 10:14:43 +0900 167) mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
293ce0ed8c2d2 (Ryusuke Konishi 2011-05-05 12:56:51 +0900 168) inode->i_mapping->a_ops = &empty_aops;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 169)
adbb39b5485b7 (Ryusuke Konishi 2010-09-05 10:14:43 +0900 170) ii->i_flags = 0;
adbb39b5485b7 (Ryusuke Konishi 2010-09-05 10:14:43 +0900 171) nilfs_bmap_init_gc(ii->i_bmap);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 172)
adbb39b5485b7 (Ryusuke Konishi 2010-09-05 10:14:43 +0900 173) return 0;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 174) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 175)
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 176) /**
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 177) * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 178) */
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 179) void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 180) {
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 181) struct list_head *head = &nilfs->ns_gc_inodes;
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 182) struct nilfs_inode_info *ii;
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 183)
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 184) while (!list_empty(head)) {
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 185) ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 186) list_del_init(&ii->i_dirty);
fbb24a3a915f1 (Ryusuke Konishi 2012-06-20 12:52:57 -0700 187) truncate_inode_pages(&ii->vfs_inode.i_data, 0);
fbb24a3a915f1 (Ryusuke Konishi 2012-06-20 12:52:57 -0700 188) nilfs_btnode_cache_clear(&ii->i_btnode_cache);
263d90cefc7d8 (Ryusuke Konishi 2010-08-20 19:06:11 +0900 189) iput(&ii->vfs_inode);
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 190) }
a3d93f709e893 (Ryusuke Konishi 2009-04-06 19:01:40 -0700 191) }