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