blob: aa3c328ee189c4409c1c4fe497677b2278c12553 [file] [log] [blame]
Ryusuke Konishiae980432018-09-04 15:46:30 -07001// SPDX-License-Identifier: GPL-2.0+
Ryusuke Konishia3d93f72009-04-06 19:01:40 -07002/*
Ryusuke Konishi047180f2009-04-06 19:01:45 -07003 * gcinode.c - dummy inodes to buffer blocks for garbage collection
Ryusuke Konishia3d93f72009-04-06 19:01:40 -07004 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -07007 * Written by Seiji Kihara, Amagai Yoshiji, and Ryusuke Konishi.
8 * Revised by Ryusuke Konishi.
Ryusuke Konishia3d93f72009-04-06 19:01:40 -07009 *
10 */
Ryusuke Konishi047180f2009-04-06 19:01:45 -070011/*
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 *
Ryusuke Konishi047180f2009-04-06 19:01:45 -070017 * 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 */
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070023
24#include <linux/buffer_head.h>
25#include <linux/mpage.h>
26#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070028#include <linux/swap.h>
29#include "nilfs.h"
Ryusuke Konishi05d0e942010-07-10 20:52:09 +090030#include "btree.h"
31#include "btnode.h"
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070032#include "page.h"
33#include "mdt.h"
34#include "dat.h"
35#include "ifile.h"
36
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070037/*
38 * nilfs_gccache_submit_read_data() - add data buffer and submit read request
39 * @inode - gc inode
40 * @blkoff - dummy offset treated as the key for the page cache
41 * @pbn - physical block number of the block
42 * @vbn - virtual block number of the block, 0 for non-virtual block
43 * @out_bh - indirect pointer to a buffer_head struct to receive the results
44 *
45 * Description: nilfs_gccache_submit_read_data() registers the data buffer
46 * specified by @pbn to the GC pagecache with the key @blkoff.
47 * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
48 *
49 * Return Value: On success, 0 is returned. On Error, one of the following
50 * negative error code is returned.
51 *
52 * %-EIO - I/O error.
53 *
54 * %-ENOMEM - Insufficient amount of memory available.
55 *
56 * %-ENOENT - The block specified with @pbn does not exist.
57 */
58int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
59 sector_t pbn, __u64 vbn,
60 struct buffer_head **out_bh)
61{
62 struct buffer_head *bh;
63 int err;
64
65 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
66 if (unlikely(!bh))
67 return -ENOMEM;
68
69 if (buffer_uptodate(bh))
70 goto out;
71
72 if (pbn == 0) {
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090073 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
74
75 err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070076 if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
77 brelse(bh);
78 goto failed;
79 }
80 }
81
82 lock_buffer(bh);
83 if (buffer_uptodate(bh)) {
84 unlock_buffer(bh);
85 goto out;
86 }
87
88 if (!buffer_mapped(bh)) {
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090089 bh->b_bdev = inode->i_sb->s_bdev;
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070090 set_buffer_mapped(bh);
91 }
92 bh->b_blocknr = pbn;
93 bh->b_end_io = end_buffer_read_sync;
94 get_bh(bh);
Mike Christie2a222ca2016-06-05 14:31:43 -050095 submit_bh(REQ_OP_READ, 0, bh);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070096 if (vbn)
97 bh->b_blocknr = vbn;
98 out:
99 err = 0;
100 *out_bh = bh;
101
102 failed:
103 unlock_page(bh->b_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300104 put_page(bh->b_page);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700105 return err;
106}
107
108/*
109 * nilfs_gccache_submit_read_node() - add node buffer and submit read request
110 * @inode - gc inode
111 * @pbn - physical block number for the block
112 * @vbn - virtual block number for the block
113 * @out_bh - indirect pointer to a buffer_head struct to receive the results
114 *
115 * Description: nilfs_gccache_submit_read_node() registers the node buffer
116 * specified by @vbn to the GC pagecache. @pbn can be supplied by the
117 * caller to avoid translation of the disk block address.
118 *
119 * Return Value: On success, 0 is returned. On Error, one of the following
120 * negative error code is returned.
121 *
122 * %-EIO - I/O error.
123 *
124 * %-ENOMEM - Insufficient amount of memory available.
125 */
126int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
127 __u64 vbn, struct buffer_head **out_bh)
128{
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900129 int ret;
130
131 ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
Mike Christie2a222ca2016-06-05 14:31:43 -0500132 vbn ? : pbn, pbn, REQ_OP_READ, 0,
133 out_bh, &pbn);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700134 if (ret == -EEXIST) /* internal code (cache hit) */
135 ret = 0;
136 return ret;
137}
138
139int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
140{
141 wait_on_buffer(bh);
Ryusuke Konishi39a9dcc2016-08-02 14:05:17 -0700142 if (!buffer_uptodate(bh)) {
143 struct inode *inode = bh->b_page->mapping->host;
144
145 nilfs_msg(inode->i_sb, KERN_ERR,
146 "I/O error reading %s block for GC (ino=%lu, vblocknr=%llu)",
147 buffer_nilfs_node(bh) ? "node" : "data",
148 inode->i_ino, (unsigned long long)bh->b_blocknr);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700149 return -EIO;
Ryusuke Konishi39a9dcc2016-08-02 14:05:17 -0700150 }
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700151 if (buffer_dirty(bh))
152 return -EEXIST;
153
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900154 if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
155 clear_buffer_uptodate(bh);
156 return -EIO;
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900157 }
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900158 mark_buffer_dirty(bh);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700159 return 0;
160}
161
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900162int nilfs_init_gcinode(struct inode *inode)
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700163{
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900164 struct nilfs_inode_info *ii = NILFS_I(inode);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700165
Ryusuke Konishiadbb39b2010-09-05 10:14:43 +0900166 inode->i_mode = S_IFREG;
167 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
Ryusuke Konishi293ce0e2011-05-05 12:56:51 +0900168 inode->i_mapping->a_ops = &empty_aops;
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700169
Ryusuke Konishiadbb39b2010-09-05 10:14:43 +0900170 ii->i_flags = 0;
171 nilfs_bmap_init_gc(ii->i_bmap);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700172
Ryusuke Konishiadbb39b2010-09-05 10:14:43 +0900173 return 0;
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700174}
175
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900176/**
177 * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700178 */
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900179void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700180{
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900181 struct list_head *head = &nilfs->ns_gc_inodes;
182 struct nilfs_inode_info *ii;
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700183
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900184 while (!list_empty(head)) {
185 ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
186 list_del_init(&ii->i_dirty);
Ryusuke Konishifbb24a32012-06-20 12:52:57 -0700187 truncate_inode_pages(&ii->vfs_inode.i_data, 0);
188 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900189 iput(&ii->vfs_inode);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700190 }
191}