Chao Yu | 7c1a000 | 2018-09-12 09:16:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3 | * fs/f2fs/node.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/f2fs_fs.h> |
| 10 | #include <linux/mpage.h> |
| 11 | #include <linux/backing-dev.h> |
| 12 | #include <linux/blkdev.h> |
| 13 | #include <linux/pagevec.h> |
| 14 | #include <linux/swap.h> |
| 15 | |
| 16 | #include "f2fs.h" |
| 17 | #include "node.h" |
| 18 | #include "segment.h" |
Yunlei He | 8790568 | 2017-07-18 09:48:12 +0800 | [diff] [blame] | 19 | #include "xattr.h" |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 20 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 21 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 22 | #define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock) |
Gu Zheng | f978f5a | 2014-02-21 18:08:29 +0800 | [diff] [blame] | 23 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 24 | static struct kmem_cache *nat_entry_slab; |
| 25 | static struct kmem_cache *free_nid_slab; |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 26 | static struct kmem_cache *nat_entry_set_slab; |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 27 | static struct kmem_cache *fsync_node_entry_slab; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 28 | |
Jaegeuk Kim | a4f843b | 2018-04-23 23:02:31 -0600 | [diff] [blame] | 29 | /* |
| 30 | * Check whether the given nid is within node id range. |
| 31 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 32 | int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid) |
Jaegeuk Kim | a4f843b | 2018-04-23 23:02:31 -0600 | [diff] [blame] | 33 | { |
| 34 | if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) { |
| 35 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 36 | f2fs_warn(sbi, "%s: out-of-range nid=%x, run fsck to fix.", |
| 37 | __func__, nid); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 38 | return -EFSCORRUPTED; |
Jaegeuk Kim | a4f843b | 2018-04-23 23:02:31 -0600 | [diff] [blame] | 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 43 | bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type) |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 44 | { |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 45 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Sahitya Tummala | 5286112 | 2021-03-16 14:59:18 +0530 | [diff] [blame] | 46 | struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 47 | struct sysinfo val; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 48 | unsigned long avail_ram; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 49 | unsigned long mem_size = 0; |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 50 | bool res = false; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 51 | |
Sahitya Tummala | 5286112 | 2021-03-16 14:59:18 +0530 | [diff] [blame] | 52 | if (!nm_i) |
| 53 | return true; |
| 54 | |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 55 | si_meminfo(&val); |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 56 | |
| 57 | /* only uses low memory */ |
| 58 | avail_ram = val.totalram - val.totalhigh; |
| 59 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 60 | /* |
| 61 | * give 25%, 25%, 50%, 50%, 50% memory for each components respectively |
| 62 | */ |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 63 | if (type == FREE_NIDS) { |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 64 | mem_size = (nm_i->nid_cnt[FREE_NID] * |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 65 | sizeof(struct free_nid)) >> PAGE_SHIFT; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 66 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2); |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 67 | } else if (type == NAT_ENTRIES) { |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 68 | mem_size = (nm_i->nat_cnt[TOTAL_NAT] * |
| 69 | sizeof(struct nat_entry)) >> PAGE_SHIFT; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 70 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2); |
Jaegeuk Kim | e589c2c | 2016-06-02 15:24:24 -0700 | [diff] [blame] | 71 | if (excess_cached_nats(sbi)) |
| 72 | res = false; |
Jaegeuk Kim | a125702 | 2015-10-08 10:40:07 -0700 | [diff] [blame] | 73 | } else if (type == DIRTY_DENTS) { |
| 74 | if (sbi->sb->s_bdi->wb.dirty_exceeded) |
| 75 | return false; |
| 76 | mem_size = get_pages(sbi, F2FS_DIRTY_DENTS); |
| 77 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 78 | } else if (type == INO_ENTRIES) { |
| 79 | int i; |
| 80 | |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 81 | for (i = 0; i < MAX_INO_ENTRY; i++) |
Kinglong Mee | 8f73cbb | 2017-03-18 09:26:13 +0800 | [diff] [blame] | 82 | mem_size += sbi->im[i].ino_num * |
| 83 | sizeof(struct ino_entry); |
| 84 | mem_size >>= PAGE_SHIFT; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 85 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 86 | } else if (type == EXTENT_CACHE) { |
Jaegeuk Kim | 7441cce | 2015-12-21 19:20:15 -0800 | [diff] [blame] | 87 | mem_size = (atomic_read(&sbi->total_ext_tree) * |
| 88 | sizeof(struct extent_tree) + |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 89 | atomic_read(&sbi->total_ext_node) * |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 90 | sizeof(struct extent_node)) >> PAGE_SHIFT; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 91 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 92 | } else if (type == INMEM_PAGES) { |
| 93 | /* it allows 20% / total_ram for inmemory pages */ |
| 94 | mem_size = get_pages(sbi, F2FS_INMEM_PAGES); |
| 95 | res = mem_size < (val.totalram / 5); |
Sahitya Tummala | 5286112 | 2021-03-16 14:59:18 +0530 | [diff] [blame] | 96 | } else if (type == DISCARD_CACHE) { |
| 97 | mem_size = (atomic_read(&dcc->discard_cmd_cnt) * |
| 98 | sizeof(struct discard_cmd)) >> PAGE_SHIFT; |
| 99 | res = mem_size < (avail_ram * nm_i->ram_thresh / 100); |
Chao Yu | ec3ea14 | 2021-05-20 19:51:50 +0800 | [diff] [blame] | 100 | } else if (type == COMPRESS_PAGE) { |
| 101 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
| 102 | unsigned long free_ram = val.freeram; |
| 103 | |
| 104 | /* |
| 105 | * free memory is lower than watermark or cached page count |
| 106 | * exceed threshold, deny caching compress page. |
| 107 | */ |
| 108 | res = (free_ram > avail_ram * sbi->compress_watermark / 100) && |
| 109 | (COMPRESS_MAPPING(sbi)->nrpages < |
| 110 | free_ram * sbi->compress_percent / 100); |
| 111 | #else |
| 112 | res = false; |
| 113 | #endif |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 114 | } else { |
Jaegeuk Kim | 1663cae | 2016-01-09 16:14:08 -0800 | [diff] [blame] | 115 | if (!sbi->sb->s_bdi->wb.dirty_exceeded) |
| 116 | return true; |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 117 | } |
| 118 | return res; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 121 | static void clear_node_page_dirty(struct page *page) |
| 122 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 123 | if (PageDirty(page)) { |
Matthew Wilcox | 5ec2d99 | 2017-12-04 20:25:25 -0500 | [diff] [blame] | 124 | f2fs_clear_page_cache_dirty_tag(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 125 | clear_page_dirty_for_io(page); |
Chao Yu | aec2f72 | 2018-05-26 18:03:35 +0800 | [diff] [blame] | 126 | dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 127 | } |
| 128 | ClearPageUptodate(page); |
| 129 | } |
| 130 | |
| 131 | static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid) |
| 132 | { |
Jaegeuk Kim | 2838d73 | 2020-10-25 07:35:47 -0700 | [diff] [blame] | 133 | return f2fs_get_meta_page_retry(sbi, current_nat_addr(sbi, nid)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid) |
| 137 | { |
| 138 | struct page *src_page; |
| 139 | struct page *dst_page; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 140 | pgoff_t dst_off; |
| 141 | void *src_addr; |
| 142 | void *dst_addr; |
| 143 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 144 | |
Chao Yu | 80551d1 | 2018-07-17 20:41:46 +0800 | [diff] [blame] | 145 | dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 146 | |
| 147 | /* get current nat block page with lock */ |
Chao Yu | 80551d1 | 2018-07-17 20:41:46 +0800 | [diff] [blame] | 148 | src_page = get_current_nat_page(sbi, nid); |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 149 | if (IS_ERR(src_page)) |
| 150 | return src_page; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 151 | dst_page = f2fs_grab_meta_page(sbi, dst_off); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 152 | f2fs_bug_on(sbi, PageDirty(src_page)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 153 | |
| 154 | src_addr = page_address(src_page); |
| 155 | dst_addr = page_address(dst_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 156 | memcpy(dst_addr, src_addr, PAGE_SIZE); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 157 | set_page_dirty(dst_page); |
| 158 | f2fs_put_page(src_page, 1); |
| 159 | |
| 160 | set_to_next_nat(nm_i, nid); |
| 161 | |
| 162 | return dst_page; |
| 163 | } |
| 164 | |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 165 | static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail) |
| 166 | { |
| 167 | struct nat_entry *new; |
| 168 | |
| 169 | if (no_fail) |
Chao Yu | 2882d34 | 2018-01-25 18:57:25 +0800 | [diff] [blame] | 170 | new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO); |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 171 | else |
Chao Yu | 2882d34 | 2018-01-25 18:57:25 +0800 | [diff] [blame] | 172 | new = kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO); |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 173 | if (new) { |
| 174 | nat_set_nid(new, nid); |
| 175 | nat_reset_flag(new); |
| 176 | } |
| 177 | return new; |
| 178 | } |
| 179 | |
| 180 | static void __free_nat_entry(struct nat_entry *e) |
| 181 | { |
| 182 | kmem_cache_free(nat_entry_slab, e); |
| 183 | } |
| 184 | |
| 185 | /* must be locked by nat_tree_lock */ |
| 186 | static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i, |
| 187 | struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail) |
| 188 | { |
| 189 | if (no_fail) |
| 190 | f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne); |
| 191 | else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne)) |
| 192 | return NULL; |
| 193 | |
| 194 | if (raw_ne) |
| 195 | node_info_from_raw_nat(&ne->ni, raw_ne); |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 196 | |
| 197 | spin_lock(&nm_i->nat_list_lock); |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 198 | list_add_tail(&ne->list, &nm_i->nat_entries); |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 199 | spin_unlock(&nm_i->nat_list_lock); |
| 200 | |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 201 | nm_i->nat_cnt[TOTAL_NAT]++; |
| 202 | nm_i->nat_cnt[RECLAIMABLE_NAT]++; |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 203 | return ne; |
| 204 | } |
| 205 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 206 | static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n) |
| 207 | { |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 208 | struct nat_entry *ne; |
| 209 | |
| 210 | ne = radix_tree_lookup(&nm_i->nat_root, n); |
| 211 | |
| 212 | /* for recent accessed nat entry, move it to tail of lru list */ |
| 213 | if (ne && !get_nat_flag(ne, IS_DIRTY)) { |
| 214 | spin_lock(&nm_i->nat_list_lock); |
| 215 | if (!list_empty(&ne->list)) |
| 216 | list_move_tail(&ne->list, &nm_i->nat_entries); |
| 217 | spin_unlock(&nm_i->nat_list_lock); |
| 218 | } |
| 219 | |
| 220 | return ne; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i, |
| 224 | nid_t start, unsigned int nr, struct nat_entry **ep) |
| 225 | { |
| 226 | return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr); |
| 227 | } |
| 228 | |
| 229 | static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e) |
| 230 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 231 | radix_tree_delete(&nm_i->nat_root, nat_get_nid(e)); |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 232 | nm_i->nat_cnt[TOTAL_NAT]--; |
| 233 | nm_i->nat_cnt[RECLAIMABLE_NAT]--; |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 234 | __free_nat_entry(e); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 235 | } |
| 236 | |
Chao Yu | 780de47 | 2018-03-20 23:08:30 +0800 | [diff] [blame] | 237 | static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i, |
| 238 | struct nat_entry *ne) |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 239 | { |
| 240 | nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid); |
| 241 | struct nat_entry_set *head; |
| 242 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 243 | head = radix_tree_lookup(&nm_i->nat_set_root, set); |
| 244 | if (!head) { |
Jaegeuk Kim | 80c5450 | 2015-08-20 08:51:56 -0700 | [diff] [blame] | 245 | head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 246 | |
| 247 | INIT_LIST_HEAD(&head->entry_list); |
| 248 | INIT_LIST_HEAD(&head->set_list); |
| 249 | head->set = set; |
| 250 | head->entry_cnt = 0; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 251 | f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 252 | } |
Chao Yu | 780de47 | 2018-03-20 23:08:30 +0800 | [diff] [blame] | 253 | return head; |
| 254 | } |
| 255 | |
| 256 | static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i, |
| 257 | struct nat_entry *ne) |
| 258 | { |
| 259 | struct nat_entry_set *head; |
| 260 | bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR; |
| 261 | |
| 262 | if (!new_ne) |
| 263 | head = __grab_nat_entry_set(nm_i, ne); |
| 264 | |
| 265 | /* |
| 266 | * update entry_cnt in below condition: |
| 267 | * 1. update NEW_ADDR to valid block address; |
| 268 | * 2. update old block address to new one; |
| 269 | */ |
| 270 | if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) || |
| 271 | !get_nat_flag(ne, IS_DIRTY))) |
| 272 | head->entry_cnt++; |
| 273 | |
| 274 | set_nat_flag(ne, IS_PREALLOC, new_ne); |
Chao Yu | febeca6 | 2017-06-05 18:29:08 +0800 | [diff] [blame] | 275 | |
| 276 | if (get_nat_flag(ne, IS_DIRTY)) |
| 277 | goto refresh_list; |
| 278 | |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 279 | nm_i->nat_cnt[DIRTY_NAT]++; |
| 280 | nm_i->nat_cnt[RECLAIMABLE_NAT]--; |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 281 | set_nat_flag(ne, IS_DIRTY, true); |
Chao Yu | febeca6 | 2017-06-05 18:29:08 +0800 | [diff] [blame] | 282 | refresh_list: |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 283 | spin_lock(&nm_i->nat_list_lock); |
Chao Yu | 780de47 | 2018-03-20 23:08:30 +0800 | [diff] [blame] | 284 | if (new_ne) |
Chao Yu | febeca6 | 2017-06-05 18:29:08 +0800 | [diff] [blame] | 285 | list_del_init(&ne->list); |
| 286 | else |
| 287 | list_move_tail(&ne->list, &head->entry_list); |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 288 | spin_unlock(&nm_i->nat_list_lock); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i, |
Kinglong Mee | 0b28b71 | 2017-02-28 21:34:47 +0800 | [diff] [blame] | 292 | struct nat_entry_set *set, struct nat_entry *ne) |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 293 | { |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 294 | spin_lock(&nm_i->nat_list_lock); |
Kinglong Mee | 0b28b71 | 2017-02-28 21:34:47 +0800 | [diff] [blame] | 295 | list_move_tail(&ne->list, &nm_i->nat_entries); |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 296 | spin_unlock(&nm_i->nat_list_lock); |
| 297 | |
Kinglong Mee | 0b28b71 | 2017-02-28 21:34:47 +0800 | [diff] [blame] | 298 | set_nat_flag(ne, IS_DIRTY, false); |
| 299 | set->entry_cnt--; |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 300 | nm_i->nat_cnt[DIRTY_NAT]--; |
| 301 | nm_i->nat_cnt[RECLAIMABLE_NAT]++; |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i, |
| 305 | nid_t start, unsigned int nr, struct nat_entry_set **ep) |
| 306 | { |
| 307 | return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep, |
| 308 | start, nr); |
| 309 | } |
| 310 | |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 311 | bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page) |
| 312 | { |
| 313 | return NODE_MAPPING(sbi) == page->mapping && |
| 314 | IS_DNODE(page) && is_cold_node(page); |
| 315 | } |
| 316 | |
| 317 | void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi) |
| 318 | { |
| 319 | spin_lock_init(&sbi->fsync_node_lock); |
| 320 | INIT_LIST_HEAD(&sbi->fsync_node_list); |
| 321 | sbi->fsync_seg_id = 0; |
| 322 | sbi->fsync_node_num = 0; |
| 323 | } |
| 324 | |
| 325 | static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi, |
| 326 | struct page *page) |
| 327 | { |
| 328 | struct fsync_node_entry *fn; |
| 329 | unsigned long flags; |
| 330 | unsigned int seq_id; |
| 331 | |
| 332 | fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS); |
| 333 | |
| 334 | get_page(page); |
| 335 | fn->page = page; |
| 336 | INIT_LIST_HEAD(&fn->list); |
| 337 | |
| 338 | spin_lock_irqsave(&sbi->fsync_node_lock, flags); |
| 339 | list_add_tail(&fn->list, &sbi->fsync_node_list); |
| 340 | fn->seq_id = sbi->fsync_seg_id++; |
| 341 | seq_id = fn->seq_id; |
| 342 | sbi->fsync_node_num++; |
| 343 | spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); |
| 344 | |
| 345 | return seq_id; |
| 346 | } |
| 347 | |
| 348 | void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page) |
| 349 | { |
| 350 | struct fsync_node_entry *fn; |
| 351 | unsigned long flags; |
| 352 | |
| 353 | spin_lock_irqsave(&sbi->fsync_node_lock, flags); |
| 354 | list_for_each_entry(fn, &sbi->fsync_node_list, list) { |
| 355 | if (fn->page == page) { |
| 356 | list_del(&fn->list); |
| 357 | sbi->fsync_node_num--; |
| 358 | spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); |
| 359 | kmem_cache_free(fsync_node_entry_slab, fn); |
| 360 | put_page(page); |
| 361 | return; |
| 362 | } |
| 363 | } |
| 364 | spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); |
| 365 | f2fs_bug_on(sbi, 1); |
| 366 | } |
| 367 | |
| 368 | void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi) |
| 369 | { |
| 370 | unsigned long flags; |
| 371 | |
| 372 | spin_lock_irqsave(&sbi->fsync_node_lock, flags); |
| 373 | sbi->fsync_seg_id = 0; |
| 374 | spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); |
| 375 | } |
| 376 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 377 | int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid) |
Jaegeuk Kim | 2dcf51a | 2015-04-29 18:31:19 -0700 | [diff] [blame] | 378 | { |
| 379 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 380 | struct nat_entry *e; |
| 381 | bool need = false; |
| 382 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 383 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 2dcf51a | 2015-04-29 18:31:19 -0700 | [diff] [blame] | 384 | e = __lookup_nat_cache(nm_i, nid); |
| 385 | if (e) { |
| 386 | if (!get_nat_flag(e, IS_CHECKPOINTED) && |
| 387 | !get_nat_flag(e, HAS_FSYNCED_INODE)) |
| 388 | need = true; |
| 389 | } |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 390 | up_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 2dcf51a | 2015-04-29 18:31:19 -0700 | [diff] [blame] | 391 | return need; |
| 392 | } |
| 393 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 394 | bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 395 | { |
| 396 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 397 | struct nat_entry *e; |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 398 | bool is_cp = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 399 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 400 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 401 | e = __lookup_nat_cache(nm_i, nid); |
Jaegeuk Kim | 7ef35e3 | 2014-09-15 12:07:13 -0700 | [diff] [blame] | 402 | if (e && !get_nat_flag(e, IS_CHECKPOINTED)) |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 403 | is_cp = false; |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 404 | up_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 405 | return is_cp; |
| 406 | } |
| 407 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 408 | bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino) |
Jaegeuk Kim | b6fe587 | 2014-06-04 00:39:42 +0900 | [diff] [blame] | 409 | { |
| 410 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 411 | struct nat_entry *e; |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 412 | bool need_update = true; |
Jaegeuk Kim | b6fe587 | 2014-06-04 00:39:42 +0900 | [diff] [blame] | 413 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 414 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 415 | e = __lookup_nat_cache(nm_i, ino); |
| 416 | if (e && get_nat_flag(e, HAS_LAST_FSYNC) && |
| 417 | (get_nat_flag(e, IS_CHECKPOINTED) || |
| 418 | get_nat_flag(e, HAS_FSYNCED_INODE))) |
| 419 | need_update = false; |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 420 | up_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 421 | return need_update; |
Jaegeuk Kim | b6fe587 | 2014-06-04 00:39:42 +0900 | [diff] [blame] | 422 | } |
| 423 | |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 424 | /* must be locked by nat_tree_lock */ |
Chao Yu | 1515aef | 2016-02-19 18:12:28 +0800 | [diff] [blame] | 425 | static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid, |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 426 | struct f2fs_nat_entry *ne) |
| 427 | { |
Chao Yu | 1515aef | 2016-02-19 18:12:28 +0800 | [diff] [blame] | 428 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 429 | struct nat_entry *new, *e; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 430 | |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 431 | new = __alloc_nat_entry(nid, false); |
| 432 | if (!new) |
| 433 | return; |
| 434 | |
| 435 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 436 | e = __lookup_nat_cache(nm_i, nid); |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 437 | if (!e) |
| 438 | e = __init_nat_entry(nm_i, new, ne, false); |
| 439 | else |
Eric Biggers | 0c0b471 | 2016-10-11 10:36:12 -0700 | [diff] [blame] | 440 | f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) || |
| 441 | nat_get_blkaddr(e) != |
| 442 | le32_to_cpu(ne->block_addr) || |
Chao Yu | 1515aef | 2016-02-19 18:12:28 +0800 | [diff] [blame] | 443 | nat_get_version(e) != ne->version); |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 444 | up_write(&nm_i->nat_tree_lock); |
| 445 | if (e != new) |
| 446 | __free_nat_entry(new); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni, |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 450 | block_t new_blkaddr, bool fsync_done) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 451 | { |
| 452 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 453 | struct nat_entry *e; |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 454 | struct nat_entry *new = __alloc_nat_entry(ni->nid, true); |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 455 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 456 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 457 | e = __lookup_nat_cache(nm_i, ni->nid); |
| 458 | if (!e) { |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 459 | e = __init_nat_entry(nm_i, new, NULL, true); |
Chao Yu | 5c27f4e | 2014-12-18 17:37:21 +0800 | [diff] [blame] | 460 | copy_node_info(&e->ni, ni); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 461 | f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 462 | } else if (new_blkaddr == NEW_ADDR) { |
| 463 | /* |
| 464 | * when nid is reallocated, |
| 465 | * previous nat entry can be remained in nat cache. |
| 466 | * So, reinitialize it with new information. |
| 467 | */ |
Chao Yu | 5c27f4e | 2014-12-18 17:37:21 +0800 | [diff] [blame] | 468 | copy_node_info(&e->ni, ni); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 469 | f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 470 | } |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 471 | /* let's free early to reduce memory consumption */ |
| 472 | if (e != new) |
| 473 | __free_nat_entry(new); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 474 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 475 | /* sanity check */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 476 | f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr); |
| 477 | f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR && |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 478 | new_blkaddr == NULL_ADDR); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 479 | f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR && |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 480 | new_blkaddr == NEW_ADDR); |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 481 | f2fs_bug_on(sbi, __is_valid_data_blkaddr(nat_get_blkaddr(e)) && |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 482 | new_blkaddr == NEW_ADDR); |
| 483 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 484 | /* increment version no as node is removed */ |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 485 | if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) { |
| 486 | unsigned char version = nat_get_version(e); |
Yi Zhuang | f10ea3c | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 487 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 488 | nat_set_version(e, inc_node_version(version)); |
| 489 | } |
| 490 | |
| 491 | /* change address */ |
| 492 | nat_set_blkaddr(e, new_blkaddr); |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 493 | if (!__is_valid_data_blkaddr(new_blkaddr)) |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 494 | set_nat_flag(e, IS_CHECKPOINTED, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 495 | __set_nat_cache_dirty(nm_i, e); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 496 | |
| 497 | /* update fsync_mark if its inode nat entry is still alive */ |
Chao Yu | d5b692b | 2015-04-30 18:35:50 +0800 | [diff] [blame] | 498 | if (ni->nid != ni->ino) |
| 499 | e = __lookup_nat_cache(nm_i, ni->ino); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 500 | if (e) { |
| 501 | if (fsync_done && ni->nid == ni->ino) |
| 502 | set_nat_flag(e, HAS_FSYNCED_INODE, true); |
| 503 | set_nat_flag(e, HAS_LAST_FSYNC, fsync_done); |
| 504 | } |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 505 | up_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 506 | } |
| 507 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 508 | int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 509 | { |
| 510 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 511 | int nr = nr_shrink; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 512 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 513 | if (!down_write_trylock(&nm_i->nat_tree_lock)) |
| 514 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 515 | |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 516 | spin_lock(&nm_i->nat_list_lock); |
| 517 | while (nr_shrink) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 518 | struct nat_entry *ne; |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 519 | |
| 520 | if (list_empty(&nm_i->nat_entries)) |
| 521 | break; |
| 522 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 523 | ne = list_first_entry(&nm_i->nat_entries, |
| 524 | struct nat_entry, list); |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 525 | list_del(&ne->list); |
| 526 | spin_unlock(&nm_i->nat_list_lock); |
| 527 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 528 | __del_from_nat_cache(nm_i, ne); |
| 529 | nr_shrink--; |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 530 | |
| 531 | spin_lock(&nm_i->nat_list_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 532 | } |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 533 | spin_unlock(&nm_i->nat_list_lock); |
| 534 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 535 | up_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 1b38dc8 | 2015-06-19 15:36:07 -0700 | [diff] [blame] | 536 | return nr - nr_shrink; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 537 | } |
| 538 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 539 | int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid, |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 540 | struct node_info *ni) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 541 | { |
| 542 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 543 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 544 | struct f2fs_journal *journal = curseg->journal; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 545 | nid_t start_nid = START_NID(nid); |
| 546 | struct f2fs_nat_block *nat_blk; |
| 547 | struct page *page = NULL; |
| 548 | struct f2fs_nat_entry ne; |
| 549 | struct nat_entry *e; |
Yunlei He | 66a82d1 | 2017-04-22 18:06:26 +0800 | [diff] [blame] | 550 | pgoff_t index; |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 551 | block_t blkaddr; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 552 | int i; |
| 553 | |
| 554 | ni->nid = nid; |
Jaegeuk Kim | fc2d64e | 2021-07-22 10:30:58 -0700 | [diff] [blame] | 555 | retry: |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 556 | /* Check nat cache */ |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 557 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 558 | e = __lookup_nat_cache(nm_i, nid); |
| 559 | if (e) { |
| 560 | ni->ino = nat_get_ino(e); |
| 561 | ni->blk_addr = nat_get_blkaddr(e); |
| 562 | ni->version = nat_get_version(e); |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 563 | up_read(&nm_i->nat_tree_lock); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 564 | return 0; |
Chao Yu | 1515aef | 2016-02-19 18:12:28 +0800 | [diff] [blame] | 565 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 566 | |
Jaegeuk Kim | fc2d64e | 2021-07-22 10:30:58 -0700 | [diff] [blame] | 567 | /* |
| 568 | * Check current segment summary by trying to grab journal_rwsem first. |
| 569 | * This sem is on the critical path on the checkpoint requiring the above |
| 570 | * nat_tree_lock. Therefore, we should retry, if we failed to grab here |
| 571 | * while not bothering checkpoint. |
| 572 | */ |
| 573 | if (!rwsem_is_locked(&sbi->cp_global_sem)) { |
| 574 | down_read(&curseg->journal_rwsem); |
| 575 | } else if (!down_read_trylock(&curseg->journal_rwsem)) { |
| 576 | up_read(&nm_i->nat_tree_lock); |
| 577 | goto retry; |
| 578 | } |
Jaegeuk Kim | 3547ea9 | 2014-12-30 23:08:26 -0800 | [diff] [blame] | 579 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 580 | i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 581 | if (i >= 0) { |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 582 | ne = nat_in_journal(journal, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 583 | node_info_from_raw_nat(ni, &ne); |
| 584 | } |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 585 | up_read(&curseg->journal_rwsem); |
Yunlei He | 66a82d1 | 2017-04-22 18:06:26 +0800 | [diff] [blame] | 586 | if (i >= 0) { |
| 587 | up_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 588 | goto cache; |
Yunlei He | 66a82d1 | 2017-04-22 18:06:26 +0800 | [diff] [blame] | 589 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 590 | |
| 591 | /* Fill node_info from nat page */ |
Yunlei He | 66a82d1 | 2017-04-22 18:06:26 +0800 | [diff] [blame] | 592 | index = current_nat_addr(sbi, nid); |
| 593 | up_read(&nm_i->nat_tree_lock); |
| 594 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 595 | page = f2fs_get_meta_page(sbi, index); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 596 | if (IS_ERR(page)) |
| 597 | return PTR_ERR(page); |
| 598 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 599 | nat_blk = (struct f2fs_nat_block *)page_address(page); |
| 600 | ne = nat_blk->entries[nid - start_nid]; |
| 601 | node_info_from_raw_nat(ni, &ne); |
| 602 | f2fs_put_page(page, 1); |
| 603 | cache: |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 604 | blkaddr = le32_to_cpu(ne.block_addr); |
| 605 | if (__is_valid_data_blkaddr(blkaddr) && |
| 606 | !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) |
| 607 | return -EFAULT; |
| 608 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 609 | /* cache nat entry */ |
Chao Yu | 1515aef | 2016-02-19 18:12:28 +0800 | [diff] [blame] | 610 | cache_nat_entry(sbi, nid, &ne); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 611 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 612 | } |
| 613 | |
Jaegeuk Kim | 79344ef | 2016-05-06 16:19:43 -0700 | [diff] [blame] | 614 | /* |
| 615 | * readahead MAX_RA_NODE number of node pages. |
| 616 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 617 | static void f2fs_ra_node_pages(struct page *parent, int start, int n) |
Jaegeuk Kim | 79344ef | 2016-05-06 16:19:43 -0700 | [diff] [blame] | 618 | { |
| 619 | struct f2fs_sb_info *sbi = F2FS_P_SB(parent); |
| 620 | struct blk_plug plug; |
| 621 | int i, end; |
| 622 | nid_t nid; |
| 623 | |
| 624 | blk_start_plug(&plug); |
| 625 | |
| 626 | /* Then, try readahead for siblings of the desired node */ |
| 627 | end = start + n; |
| 628 | end = min(end, NIDS_PER_BLOCK); |
| 629 | for (i = start; i < end; i++) { |
| 630 | nid = get_nid(parent, i, false); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 631 | f2fs_ra_node_page(sbi, nid); |
Jaegeuk Kim | 79344ef | 2016-05-06 16:19:43 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | blk_finish_plug(&plug); |
| 635 | } |
| 636 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 637 | pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs) |
Chao Yu | 3cf4574 | 2016-01-26 15:40:44 +0800 | [diff] [blame] | 638 | { |
| 639 | const long direct_index = ADDRS_PER_INODE(dn->inode); |
Chao Yu | d02a6e6 | 2019-03-25 21:08:19 +0800 | [diff] [blame] | 640 | const long direct_blks = ADDRS_PER_BLOCK(dn->inode); |
| 641 | const long indirect_blks = ADDRS_PER_BLOCK(dn->inode) * NIDS_PER_BLOCK; |
| 642 | unsigned int skipped_unit = ADDRS_PER_BLOCK(dn->inode); |
Chao Yu | 3cf4574 | 2016-01-26 15:40:44 +0800 | [diff] [blame] | 643 | int cur_level = dn->cur_level; |
| 644 | int max_level = dn->max_level; |
| 645 | pgoff_t base = 0; |
| 646 | |
| 647 | if (!dn->max_level) |
| 648 | return pgofs + 1; |
| 649 | |
| 650 | while (max_level-- > cur_level) |
| 651 | skipped_unit *= NIDS_PER_BLOCK; |
| 652 | |
| 653 | switch (dn->max_level) { |
| 654 | case 3: |
| 655 | base += 2 * indirect_blks; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 656 | fallthrough; |
Chao Yu | 3cf4574 | 2016-01-26 15:40:44 +0800 | [diff] [blame] | 657 | case 2: |
| 658 | base += 2 * direct_blks; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 659 | fallthrough; |
Chao Yu | 3cf4574 | 2016-01-26 15:40:44 +0800 | [diff] [blame] | 660 | case 1: |
| 661 | base += direct_index; |
| 662 | break; |
| 663 | default: |
| 664 | f2fs_bug_on(F2FS_I_SB(dn->inode), 1); |
| 665 | } |
| 666 | |
| 667 | return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base; |
| 668 | } |
| 669 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 670 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 671 | * The maximum depth is four. |
| 672 | * Offset[0] will have raw inode offset. |
| 673 | */ |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 674 | static int get_node_path(struct inode *inode, long block, |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 675 | int offset[4], unsigned int noffset[4]) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 676 | { |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 677 | const long direct_index = ADDRS_PER_INODE(inode); |
Chao Yu | d02a6e6 | 2019-03-25 21:08:19 +0800 | [diff] [blame] | 678 | const long direct_blks = ADDRS_PER_BLOCK(inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 679 | const long dptrs_per_blk = NIDS_PER_BLOCK; |
Chao Yu | d02a6e6 | 2019-03-25 21:08:19 +0800 | [diff] [blame] | 680 | const long indirect_blks = ADDRS_PER_BLOCK(inode) * NIDS_PER_BLOCK; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 681 | const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK; |
| 682 | int n = 0; |
| 683 | int level = 0; |
| 684 | |
| 685 | noffset[0] = 0; |
| 686 | |
| 687 | if (block < direct_index) { |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 688 | offset[n] = block; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 689 | goto got; |
| 690 | } |
| 691 | block -= direct_index; |
| 692 | if (block < direct_blks) { |
| 693 | offset[n++] = NODE_DIR1_BLOCK; |
| 694 | noffset[n] = 1; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 695 | offset[n] = block; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 696 | level = 1; |
| 697 | goto got; |
| 698 | } |
| 699 | block -= direct_blks; |
| 700 | if (block < direct_blks) { |
| 701 | offset[n++] = NODE_DIR2_BLOCK; |
| 702 | noffset[n] = 2; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 703 | offset[n] = block; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 704 | level = 1; |
| 705 | goto got; |
| 706 | } |
| 707 | block -= direct_blks; |
| 708 | if (block < indirect_blks) { |
| 709 | offset[n++] = NODE_IND1_BLOCK; |
| 710 | noffset[n] = 3; |
| 711 | offset[n++] = block / direct_blks; |
| 712 | noffset[n] = 4 + offset[n - 1]; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 713 | offset[n] = block % direct_blks; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 714 | level = 2; |
| 715 | goto got; |
| 716 | } |
| 717 | block -= indirect_blks; |
| 718 | if (block < indirect_blks) { |
| 719 | offset[n++] = NODE_IND2_BLOCK; |
| 720 | noffset[n] = 4 + dptrs_per_blk; |
| 721 | offset[n++] = block / direct_blks; |
| 722 | noffset[n] = 5 + dptrs_per_blk + offset[n - 1]; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 723 | offset[n] = block % direct_blks; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 724 | level = 2; |
| 725 | goto got; |
| 726 | } |
| 727 | block -= indirect_blks; |
| 728 | if (block < dindirect_blks) { |
| 729 | offset[n++] = NODE_DIND_BLOCK; |
| 730 | noffset[n] = 5 + (dptrs_per_blk * 2); |
| 731 | offset[n++] = block / indirect_blks; |
| 732 | noffset[n] = 6 + (dptrs_per_blk * 2) + |
| 733 | offset[n - 1] * (dptrs_per_blk + 1); |
| 734 | offset[n++] = (block / direct_blks) % dptrs_per_blk; |
| 735 | noffset[n] = 7 + (dptrs_per_blk * 2) + |
| 736 | offset[n - 2] * (dptrs_per_blk + 1) + |
| 737 | offset[n - 1]; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 738 | offset[n] = block % direct_blks; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 739 | level = 3; |
| 740 | goto got; |
| 741 | } else { |
Jaegeuk Kim | adb6dc19 | 2017-08-21 13:51:32 -0700 | [diff] [blame] | 742 | return -E2BIG; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 743 | } |
| 744 | got: |
| 745 | return level; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Caller should call f2fs_put_dnode(dn). |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 750 | * Also, it should grab and release a rwsem by calling f2fs_lock_op() and |
Chao Yu | 7a88ddb | 2020-02-27 19:30:05 +0800 | [diff] [blame] | 751 | * f2fs_unlock_op() only if mode is set with ALLOC_NODE. |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 752 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 753 | int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 754 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 755 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 756 | struct page *npage[4]; |
Jaegeuk Kim | f1a3b98 | 2015-02-11 11:25:11 -0800 | [diff] [blame] | 757 | struct page *parent = NULL; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 758 | int offset[4]; |
| 759 | unsigned int noffset[4]; |
| 760 | nid_t nids[4]; |
Chao Yu | 3cf4574 | 2016-01-26 15:40:44 +0800 | [diff] [blame] | 761 | int level, i = 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 762 | int err = 0; |
| 763 | |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 764 | level = get_node_path(dn->inode, index, offset, noffset); |
Jaegeuk Kim | adb6dc19 | 2017-08-21 13:51:32 -0700 | [diff] [blame] | 765 | if (level < 0) |
| 766 | return level; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 767 | |
| 768 | nids[0] = dn->inode->i_ino; |
Jaegeuk Kim | 1646cfa | 2013-05-20 09:42:28 +0900 | [diff] [blame] | 769 | npage[0] = dn->inode_page; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 770 | |
Jaegeuk Kim | 1646cfa | 2013-05-20 09:42:28 +0900 | [diff] [blame] | 771 | if (!npage[0]) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 772 | npage[0] = f2fs_get_node_page(sbi, nids[0]); |
Jaegeuk Kim | 1646cfa | 2013-05-20 09:42:28 +0900 | [diff] [blame] | 773 | if (IS_ERR(npage[0])) |
| 774 | return PTR_ERR(npage[0]); |
| 775 | } |
Jaegeuk Kim | f1a3b98 | 2015-02-11 11:25:11 -0800 | [diff] [blame] | 776 | |
| 777 | /* if inline_data is set, should not report any block indices */ |
| 778 | if (f2fs_has_inline_data(dn->inode) && index) { |
Jaegeuk Kim | 7662916 | 2015-03-02 16:28:16 -0800 | [diff] [blame] | 779 | err = -ENOENT; |
Jaegeuk Kim | f1a3b98 | 2015-02-11 11:25:11 -0800 | [diff] [blame] | 780 | f2fs_put_page(npage[0], 1); |
| 781 | goto release_out; |
| 782 | } |
| 783 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 784 | parent = npage[0]; |
Changman Lee | 52c2db3 | 2013-02-20 07:47:06 +0900 | [diff] [blame] | 785 | if (level != 0) |
| 786 | nids[1] = get_nid(parent, offset[0], true); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 787 | dn->inode_page = npage[0]; |
| 788 | dn->inode_page_locked = true; |
| 789 | |
| 790 | /* get indirect or direct nodes */ |
| 791 | for (i = 1; i <= level; i++) { |
| 792 | bool done = false; |
| 793 | |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 794 | if (!nids[i] && mode == ALLOC_NODE) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 795 | /* alloc new node */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 796 | if (!f2fs_alloc_nid(sbi, &(nids[i]))) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 797 | err = -ENOSPC; |
| 798 | goto release_pages; |
| 799 | } |
| 800 | |
| 801 | dn->nid = nids[i]; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 802 | npage[i] = f2fs_new_node_page(dn, noffset[i]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 803 | if (IS_ERR(npage[i])) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 804 | f2fs_alloc_nid_failed(sbi, nids[i]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 805 | err = PTR_ERR(npage[i]); |
| 806 | goto release_pages; |
| 807 | } |
| 808 | |
| 809 | set_nid(parent, offset[i - 1], nids[i], i == 1); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 810 | f2fs_alloc_nid_done(sbi, nids[i]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 811 | done = true; |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 812 | } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 813 | npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 814 | if (IS_ERR(npage[i])) { |
| 815 | err = PTR_ERR(npage[i]); |
| 816 | goto release_pages; |
| 817 | } |
| 818 | done = true; |
| 819 | } |
| 820 | if (i == 1) { |
| 821 | dn->inode_page_locked = false; |
| 822 | unlock_page(parent); |
| 823 | } else { |
| 824 | f2fs_put_page(parent, 1); |
| 825 | } |
| 826 | |
| 827 | if (!done) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 828 | npage[i] = f2fs_get_node_page(sbi, nids[i]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 829 | if (IS_ERR(npage[i])) { |
| 830 | err = PTR_ERR(npage[i]); |
| 831 | f2fs_put_page(npage[0], 0); |
| 832 | goto release_out; |
| 833 | } |
| 834 | } |
| 835 | if (i < level) { |
| 836 | parent = npage[i]; |
| 837 | nids[i + 1] = get_nid(parent, offset[i], false); |
| 838 | } |
| 839 | } |
| 840 | dn->nid = nids[level]; |
| 841 | dn->ofs_in_node = offset[level]; |
| 842 | dn->node_page = npage[level]; |
Chao Yu | a2ced1c | 2020-02-14 17:44:10 +0800 | [diff] [blame] | 843 | dn->data_blkaddr = f2fs_data_blkaddr(dn); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 844 | return 0; |
| 845 | |
| 846 | release_pages: |
| 847 | f2fs_put_page(parent, 1); |
| 848 | if (i > 1) |
| 849 | f2fs_put_page(npage[0], 0); |
| 850 | release_out: |
| 851 | dn->inode_page = NULL; |
| 852 | dn->node_page = NULL; |
Chao Yu | 3cf4574 | 2016-01-26 15:40:44 +0800 | [diff] [blame] | 853 | if (err == -ENOENT) { |
| 854 | dn->cur_level = i; |
| 855 | dn->max_level = level; |
Jaegeuk Kim | 0a2aa8f | 2016-07-08 17:42:21 -0700 | [diff] [blame] | 856 | dn->ofs_in_node = offset[level]; |
Chao Yu | 3cf4574 | 2016-01-26 15:40:44 +0800 | [diff] [blame] | 857 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 858 | return err; |
| 859 | } |
| 860 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 861 | static int truncate_node(struct dnode_of_data *dn) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 862 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 863 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 864 | struct node_info ni; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 865 | int err; |
Pan Bian | 0ea295d | 2018-11-22 18:58:46 +0800 | [diff] [blame] | 866 | pgoff_t index; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 867 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 868 | err = f2fs_get_node_info(sbi, dn->nid, &ni); |
| 869 | if (err) |
| 870 | return err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 871 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 872 | /* Deallocate node address */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 873 | f2fs_invalidate_blocks(sbi, ni.blk_addr); |
Chao Yu | 000519f | 2017-07-06 01:11:31 +0800 | [diff] [blame] | 874 | dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 875 | set_node_addr(sbi, &ni, NULL_ADDR, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 876 | |
| 877 | if (dn->nid == dn->inode->i_ino) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 878 | f2fs_remove_orphan_inode(sbi, dn->nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 879 | dec_valid_inode_count(sbi); |
Jaegeuk Kim | 0f18b46 | 2016-05-20 11:10:10 -0700 | [diff] [blame] | 880 | f2fs_inode_synced(dn->inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 881 | } |
Chao Yu | 000519f | 2017-07-06 01:11:31 +0800 | [diff] [blame] | 882 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 883 | clear_node_page_dirty(dn->node_page); |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 884 | set_sbi_flag(sbi, SBI_IS_DIRTY); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 885 | |
Pan Bian | 0ea295d | 2018-11-22 18:58:46 +0800 | [diff] [blame] | 886 | index = dn->node_page->index; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 887 | f2fs_put_page(dn->node_page, 1); |
Jaegeuk Kim | bf39c00 | 2014-01-22 20:41:57 +0900 | [diff] [blame] | 888 | |
| 889 | invalidate_mapping_pages(NODE_MAPPING(sbi), |
Pan Bian | 0ea295d | 2018-11-22 18:58:46 +0800 | [diff] [blame] | 890 | index, index); |
Jaegeuk Kim | bf39c00 | 2014-01-22 20:41:57 +0900 | [diff] [blame] | 891 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 892 | dn->node_page = NULL; |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 893 | trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 894 | |
| 895 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | static int truncate_dnode(struct dnode_of_data *dn) |
| 899 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 900 | struct page *page; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 901 | int err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 902 | |
| 903 | if (dn->nid == 0) |
| 904 | return 1; |
| 905 | |
| 906 | /* get direct node */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 907 | page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid); |
Masahiro Yamada | 45586c7 | 2020-02-03 17:37:45 -0800 | [diff] [blame] | 908 | if (PTR_ERR(page) == -ENOENT) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 909 | return 1; |
| 910 | else if (IS_ERR(page)) |
| 911 | return PTR_ERR(page); |
| 912 | |
| 913 | /* Make dnode_of_data for parameter */ |
| 914 | dn->node_page = page; |
| 915 | dn->ofs_in_node = 0; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 916 | f2fs_truncate_data_blocks(dn); |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 917 | err = truncate_node(dn); |
| 918 | if (err) |
| 919 | return err; |
| 920 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 921 | return 1; |
| 922 | } |
| 923 | |
| 924 | static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs, |
| 925 | int ofs, int depth) |
| 926 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 927 | struct dnode_of_data rdn = *dn; |
| 928 | struct page *page; |
| 929 | struct f2fs_node *rn; |
| 930 | nid_t child_nid; |
| 931 | unsigned int child_nofs; |
| 932 | int freed = 0; |
| 933 | int i, ret; |
| 934 | |
| 935 | if (dn->nid == 0) |
| 936 | return NIDS_PER_BLOCK + 1; |
| 937 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 938 | trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr); |
| 939 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 940 | page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 941 | if (IS_ERR(page)) { |
| 942 | trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 943 | return PTR_ERR(page); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 944 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 945 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 946 | f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK); |
Jaegeuk Kim | 79344ef | 2016-05-06 16:19:43 -0700 | [diff] [blame] | 947 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 948 | rn = F2FS_NODE(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 949 | if (depth < 3) { |
| 950 | for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) { |
| 951 | child_nid = le32_to_cpu(rn->in.nid[i]); |
| 952 | if (child_nid == 0) |
| 953 | continue; |
| 954 | rdn.nid = child_nid; |
| 955 | ret = truncate_dnode(&rdn); |
| 956 | if (ret < 0) |
| 957 | goto out_err; |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 958 | if (set_nid(page, i, 0, false)) |
| 959 | dn->node_changed = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 960 | } |
| 961 | } else { |
| 962 | child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1; |
| 963 | for (i = ofs; i < NIDS_PER_BLOCK; i++) { |
| 964 | child_nid = le32_to_cpu(rn->in.nid[i]); |
| 965 | if (child_nid == 0) { |
| 966 | child_nofs += NIDS_PER_BLOCK + 1; |
| 967 | continue; |
| 968 | } |
| 969 | rdn.nid = child_nid; |
| 970 | ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1); |
| 971 | if (ret == (NIDS_PER_BLOCK + 1)) { |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 972 | if (set_nid(page, i, 0, false)) |
| 973 | dn->node_changed = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 974 | child_nofs += ret; |
| 975 | } else if (ret < 0 && ret != -ENOENT) { |
| 976 | goto out_err; |
| 977 | } |
| 978 | } |
| 979 | freed = child_nofs; |
| 980 | } |
| 981 | |
| 982 | if (!ofs) { |
| 983 | /* remove current indirect node */ |
| 984 | dn->node_page = page; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 985 | ret = truncate_node(dn); |
| 986 | if (ret) |
| 987 | goto out_err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 988 | freed++; |
| 989 | } else { |
| 990 | f2fs_put_page(page, 1); |
| 991 | } |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 992 | trace_f2fs_truncate_nodes_exit(dn->inode, freed); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 993 | return freed; |
| 994 | |
| 995 | out_err: |
| 996 | f2fs_put_page(page, 1); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 997 | trace_f2fs_truncate_nodes_exit(dn->inode, ret); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 998 | return ret; |
| 999 | } |
| 1000 | |
| 1001 | static int truncate_partial_nodes(struct dnode_of_data *dn, |
| 1002 | struct f2fs_inode *ri, int *offset, int depth) |
| 1003 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1004 | struct page *pages[2]; |
| 1005 | nid_t nid[3]; |
| 1006 | nid_t child_nid; |
| 1007 | int err = 0; |
| 1008 | int i; |
| 1009 | int idx = depth - 2; |
| 1010 | |
| 1011 | nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]); |
| 1012 | if (!nid[0]) |
| 1013 | return 0; |
| 1014 | |
| 1015 | /* get indirect nodes in the path */ |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 1016 | for (i = 0; i < idx + 1; i++) { |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1017 | /* reference count'll be increased */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1018 | pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1019 | if (IS_ERR(pages[i])) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1020 | err = PTR_ERR(pages[i]); |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 1021 | idx = i - 1; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1022 | goto fail; |
| 1023 | } |
| 1024 | nid[i + 1] = get_nid(pages[i], offset[i + 1], false); |
| 1025 | } |
| 1026 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1027 | f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK); |
Jaegeuk Kim | 79344ef | 2016-05-06 16:19:43 -0700 | [diff] [blame] | 1028 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1029 | /* free direct nodes linked to a partial indirect node */ |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 1030 | for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1031 | child_nid = get_nid(pages[idx], i, false); |
| 1032 | if (!child_nid) |
| 1033 | continue; |
| 1034 | dn->nid = child_nid; |
| 1035 | err = truncate_dnode(dn); |
| 1036 | if (err < 0) |
| 1037 | goto fail; |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 1038 | if (set_nid(pages[idx], i, 0, false)) |
| 1039 | dn->node_changed = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1040 | } |
| 1041 | |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 1042 | if (offset[idx + 1] == 0) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1043 | dn->node_page = pages[idx]; |
| 1044 | dn->nid = nid[idx]; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1045 | err = truncate_node(dn); |
| 1046 | if (err) |
| 1047 | goto fail; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1048 | } else { |
| 1049 | f2fs_put_page(pages[idx], 1); |
| 1050 | } |
| 1051 | offset[idx]++; |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 1052 | offset[idx + 1] = 0; |
| 1053 | idx--; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1054 | fail: |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 1055 | for (i = idx; i >= 0; i--) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1056 | f2fs_put_page(pages[i], 1); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 1057 | |
| 1058 | trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err); |
| 1059 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1060 | return err; |
| 1061 | } |
| 1062 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1063 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1064 | * All the block addresses of data and nodes should be nullified. |
| 1065 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1066 | int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1067 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1068 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1069 | int err = 0, cont = 1; |
| 1070 | int level, offset[4], noffset[4]; |
Jaegeuk Kim | 7dd690c | 2013-02-12 07:28:55 +0900 | [diff] [blame] | 1071 | unsigned int nofs = 0; |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1072 | struct f2fs_inode *ri; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1073 | struct dnode_of_data dn; |
| 1074 | struct page *page; |
| 1075 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 1076 | trace_f2fs_truncate_inode_blocks_enter(inode, from); |
| 1077 | |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 1078 | level = get_node_path(inode, from, offset, noffset); |
Yubo Feng | 9039d83 | 2020-06-20 16:39:43 +0800 | [diff] [blame] | 1079 | if (level < 0) { |
| 1080 | trace_f2fs_truncate_inode_blocks_exit(inode, level); |
Jaegeuk Kim | adb6dc19 | 2017-08-21 13:51:32 -0700 | [diff] [blame] | 1081 | return level; |
Yubo Feng | 9039d83 | 2020-06-20 16:39:43 +0800 | [diff] [blame] | 1082 | } |
Jaegeuk Kim | ff37355 | 2016-03-29 16:13:45 -0700 | [diff] [blame] | 1083 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1084 | page = f2fs_get_node_page(sbi, inode->i_ino); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 1085 | if (IS_ERR(page)) { |
| 1086 | trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1087 | return PTR_ERR(page); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 1088 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1089 | |
| 1090 | set_new_dnode(&dn, inode, page, NULL, 0); |
| 1091 | unlock_page(page); |
| 1092 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1093 | ri = F2FS_INODE(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1094 | switch (level) { |
| 1095 | case 0: |
| 1096 | case 1: |
| 1097 | nofs = noffset[1]; |
| 1098 | break; |
| 1099 | case 2: |
| 1100 | nofs = noffset[1]; |
| 1101 | if (!offset[level - 1]) |
| 1102 | goto skip_partial; |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1103 | err = truncate_partial_nodes(&dn, ri, offset, level); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1104 | if (err < 0 && err != -ENOENT) |
| 1105 | goto fail; |
| 1106 | nofs += 1 + NIDS_PER_BLOCK; |
| 1107 | break; |
| 1108 | case 3: |
| 1109 | nofs = 5 + 2 * NIDS_PER_BLOCK; |
| 1110 | if (!offset[level - 1]) |
| 1111 | goto skip_partial; |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1112 | err = truncate_partial_nodes(&dn, ri, offset, level); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1113 | if (err < 0 && err != -ENOENT) |
| 1114 | goto fail; |
| 1115 | break; |
| 1116 | default: |
| 1117 | BUG(); |
| 1118 | } |
| 1119 | |
| 1120 | skip_partial: |
| 1121 | while (cont) { |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1122 | dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1123 | switch (offset[0]) { |
| 1124 | case NODE_DIR1_BLOCK: |
| 1125 | case NODE_DIR2_BLOCK: |
| 1126 | err = truncate_dnode(&dn); |
| 1127 | break; |
| 1128 | |
| 1129 | case NODE_IND1_BLOCK: |
| 1130 | case NODE_IND2_BLOCK: |
| 1131 | err = truncate_nodes(&dn, nofs, offset[1], 2); |
| 1132 | break; |
| 1133 | |
| 1134 | case NODE_DIND_BLOCK: |
| 1135 | err = truncate_nodes(&dn, nofs, offset[1], 3); |
| 1136 | cont = 0; |
| 1137 | break; |
| 1138 | |
| 1139 | default: |
| 1140 | BUG(); |
| 1141 | } |
| 1142 | if (err < 0 && err != -ENOENT) |
| 1143 | goto fail; |
| 1144 | if (offset[1] == 0 && |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1145 | ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1146 | lock_page(page); |
Jaegeuk Kim | ff37355 | 2016-03-29 16:13:45 -0700 | [diff] [blame] | 1147 | BUG_ON(page->mapping != NODE_MAPPING(sbi)); |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 1148 | f2fs_wait_on_page_writeback(page, NODE, true, true); |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1149 | ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1150 | set_page_dirty(page); |
| 1151 | unlock_page(page); |
| 1152 | } |
| 1153 | offset[1] = 0; |
| 1154 | offset[0]++; |
| 1155 | nofs += err; |
| 1156 | } |
| 1157 | fail: |
| 1158 | f2fs_put_page(page, 0); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 1159 | trace_f2fs_truncate_inode_blocks_exit(inode, err); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1160 | return err > 0 ? 0 : err; |
| 1161 | } |
| 1162 | |
Jaegeuk Kim | 9c77f75 | 2017-10-19 11:48:57 -0700 | [diff] [blame] | 1163 | /* caller must lock inode page */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1164 | int f2fs_truncate_xattr_node(struct inode *inode) |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 1165 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1166 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 1167 | nid_t nid = F2FS_I(inode)->i_xattr_nid; |
| 1168 | struct dnode_of_data dn; |
| 1169 | struct page *npage; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1170 | int err; |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 1171 | |
| 1172 | if (!nid) |
| 1173 | return 0; |
| 1174 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1175 | npage = f2fs_get_node_page(sbi, nid); |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 1176 | if (IS_ERR(npage)) |
| 1177 | return PTR_ERR(npage); |
| 1178 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1179 | set_new_dnode(&dn, inode, NULL, npage, nid); |
| 1180 | err = truncate_node(&dn); |
| 1181 | if (err) { |
| 1182 | f2fs_put_page(npage, 1); |
| 1183 | return err; |
| 1184 | } |
| 1185 | |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 1186 | f2fs_i_xnid_write(inode, 0); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 1187 | |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 1188 | return 0; |
| 1189 | } |
| 1190 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1191 | /* |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 1192 | * Caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 1193 | * f2fs_unlock_op(). |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1194 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1195 | int f2fs_remove_inode_page(struct inode *inode) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1196 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1197 | struct dnode_of_data dn; |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 1198 | int err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1199 | |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 1200 | set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1201 | err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE); |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 1202 | if (err) |
| 1203 | return err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1204 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1205 | err = f2fs_truncate_xattr_node(inode); |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 1206 | if (err) { |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 1207 | f2fs_put_dnode(&dn); |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 1208 | return err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1209 | } |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 1210 | |
| 1211 | /* remove potential inline_data blocks */ |
| 1212 | if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 1213 | S_ISLNK(inode->i_mode)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1214 | f2fs_truncate_data_blocks_range(&dn, 1); |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 1215 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1216 | /* 0 is possible, after f2fs_new_inode() has failed */ |
Jaegeuk Kim | 8d714f8 | 2018-07-31 09:09:01 -0700 | [diff] [blame] | 1217 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) { |
| 1218 | f2fs_put_dnode(&dn); |
| 1219 | return -EIO; |
| 1220 | } |
Chao Yu | 8b6810f | 2019-04-15 15:28:34 +0800 | [diff] [blame] | 1221 | |
| 1222 | if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) { |
Chao Yu | 097a768 | 2020-02-24 19:20:19 +0800 | [diff] [blame] | 1223 | f2fs_warn(F2FS_I_SB(inode), |
| 1224 | "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu", |
| 1225 | inode->i_ino, (unsigned long long)inode->i_blocks); |
Chao Yu | 8b6810f | 2019-04-15 15:28:34 +0800 | [diff] [blame] | 1226 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); |
| 1227 | } |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 1228 | |
| 1229 | /* will put inode & node pages */ |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1230 | err = truncate_node(&dn); |
| 1231 | if (err) { |
| 1232 | f2fs_put_dnode(&dn); |
| 1233 | return err; |
| 1234 | } |
Chao Yu | 13ec729 | 2015-08-24 17:40:45 +0800 | [diff] [blame] | 1235 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1236 | } |
| 1237 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1238 | struct page *f2fs_new_inode_page(struct inode *inode) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1239 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1240 | struct dnode_of_data dn; |
| 1241 | |
| 1242 | /* allocate inode page for new inode */ |
| 1243 | set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 1244 | |
| 1245 | /* caller should f2fs_put_page(page, 1); */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1246 | return f2fs_new_node_page(&dn, 0); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1247 | } |
| 1248 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1249 | struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1250 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1251 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | 25cc5d3 | 2017-02-13 17:02:44 -0800 | [diff] [blame] | 1252 | struct node_info new_ni; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1253 | struct page *page; |
| 1254 | int err; |
| 1255 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 1256 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1257 | return ERR_PTR(-EPERM); |
| 1258 | |
Jaegeuk Kim | 300e129 | 2016-04-29 16:11:53 -0700 | [diff] [blame] | 1259 | page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1260 | if (!page) |
| 1261 | return ERR_PTR(-ENOMEM); |
| 1262 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1263 | if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs)))) |
Jaegeuk Kim | 9c02740 | 2013-08-12 16:00:46 +0900 | [diff] [blame] | 1264 | goto fail; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 1265 | |
Jaegeuk Kim | 25cc5d3 | 2017-02-13 17:02:44 -0800 | [diff] [blame] | 1266 | #ifdef CONFIG_F2FS_CHECK_FS |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1267 | err = f2fs_get_node_info(sbi, dn->nid, &new_ni); |
| 1268 | if (err) { |
| 1269 | dec_valid_node_count(sbi, dn->inode, !ofs); |
| 1270 | goto fail; |
| 1271 | } |
Jaegeuk Kim | 25cc5d3 | 2017-02-13 17:02:44 -0800 | [diff] [blame] | 1272 | f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR); |
| 1273 | #endif |
| 1274 | new_ni.nid = dn->nid; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1275 | new_ni.ino = dn->inode->i_ino; |
Jaegeuk Kim | 25cc5d3 | 2017-02-13 17:02:44 -0800 | [diff] [blame] | 1276 | new_ni.blk_addr = NULL_ADDR; |
| 1277 | new_ni.flag = 0; |
| 1278 | new_ni.version = 0; |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 1279 | set_node_addr(sbi, &new_ni, NEW_ADDR, false); |
Jaegeuk Kim | 9c02740 | 2013-08-12 16:00:46 +0900 | [diff] [blame] | 1280 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 1281 | f2fs_wait_on_page_writeback(page, NODE, true, true); |
Jaegeuk Kim | 9c02740 | 2013-08-12 16:00:46 +0900 | [diff] [blame] | 1282 | fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true); |
Chao Yu | c566757 | 2018-03-09 14:24:22 +0800 | [diff] [blame] | 1283 | set_cold_node(page, S_ISDIR(dn->inode->i_mode)); |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 1284 | if (!PageUptodate(page)) |
| 1285 | SetPageUptodate(page); |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 1286 | if (set_page_dirty(page)) |
| 1287 | dn->node_changed = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1288 | |
Chao Yu | 4bc8e9b | 2014-03-17 16:35:06 +0800 | [diff] [blame] | 1289 | if (f2fs_has_xattr_block(ofs)) |
Jaegeuk Kim | 205b982 | 2016-05-20 09:52:20 -0700 | [diff] [blame] | 1290 | f2fs_i_xnid_write(dn->inode, dn->nid); |
Jaegeuk Kim | 479bd73 | 2013-08-12 16:04:53 +0900 | [diff] [blame] | 1291 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1292 | if (ofs == 0) |
| 1293 | inc_valid_inode_count(sbi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1294 | return page; |
| 1295 | |
| 1296 | fail: |
Jaegeuk Kim | 71e9fec | 2012-12-20 15:10:06 +0900 | [diff] [blame] | 1297 | clear_node_page_dirty(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1298 | f2fs_put_page(page, 1); |
| 1299 | return ERR_PTR(err); |
| 1300 | } |
| 1301 | |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1302 | /* |
| 1303 | * Caller should do after getting the following values. |
| 1304 | * 0: f2fs_put_page(page, 0) |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 1305 | * LOCKED_PAGE or error: f2fs_put_page(page, 1) |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1306 | */ |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 1307 | static int read_node_page(struct page *page, int op_flags) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1308 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1309 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1310 | struct node_info ni; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1311 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1312 | .sbi = sbi, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1313 | .type = NODE, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 1314 | .op = REQ_OP_READ, |
| 1315 | .op_flags = op_flags, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1316 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1317 | .encrypted_page = NULL, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1318 | }; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1319 | int err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1320 | |
Weichao Guo | 54c55c4 | 2018-03-09 23:10:21 +0800 | [diff] [blame] | 1321 | if (PageUptodate(page)) { |
Chao Yu | b42b179 | 2019-04-15 15:28:35 +0800 | [diff] [blame] | 1322 | if (!f2fs_inode_chksum_verify(sbi, page)) { |
| 1323 | ClearPageUptodate(page); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 1324 | return -EFSBADCRC; |
Chao Yu | b42b179 | 2019-04-15 15:28:35 +0800 | [diff] [blame] | 1325 | } |
Jaegeuk Kim | 3bdad3c | 2016-06-30 19:04:16 -0700 | [diff] [blame] | 1326 | return LOCKED_PAGE; |
Weichao Guo | 54c55c4 | 2018-03-09 23:10:21 +0800 | [diff] [blame] | 1327 | } |
Jaegeuk Kim | 3bdad3c | 2016-06-30 19:04:16 -0700 | [diff] [blame] | 1328 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1329 | err = f2fs_get_node_info(sbi, page->index, &ni); |
| 1330 | if (err) |
| 1331 | return err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1332 | |
Jaegeuk Kim | 83a3bfd | 2018-06-21 13:46:23 -0700 | [diff] [blame] | 1333 | if (unlikely(ni.blk_addr == NULL_ADDR) || |
| 1334 | is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) { |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1335 | ClearPageUptodate(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1336 | return -ENOENT; |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1337 | } |
| 1338 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 1339 | fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr; |
Chao Yu | 8b83ac8 | 2020-04-16 18:16:56 +0800 | [diff] [blame] | 1340 | |
| 1341 | err = f2fs_submit_page_bio(&fio); |
| 1342 | |
| 1343 | if (!err) |
| 1344 | f2fs_update_iostat(sbi, FS_NODE_READ_IO, F2FS_BLKSIZE); |
| 1345 | |
| 1346 | return err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1347 | } |
| 1348 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1349 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1350 | * Readahead a node page |
| 1351 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1352 | void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1353 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1354 | struct page *apage; |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1355 | int err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1356 | |
Chao Yu | e845872 | 2016-01-08 20:13:37 +0800 | [diff] [blame] | 1357 | if (!nid) |
| 1358 | return; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1359 | if (f2fs_check_nid_range(sbi, nid)) |
Jaegeuk Kim | a4f843b | 2018-04-23 23:02:31 -0600 | [diff] [blame] | 1360 | return; |
Chao Yu | e845872 | 2016-01-08 20:13:37 +0800 | [diff] [blame] | 1361 | |
Matthew Wilcox | 5ec2d99 | 2017-12-04 20:25:25 -0500 | [diff] [blame] | 1362 | apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid); |
Fan Li | 999270d | 2016-02-29 14:29:51 +0800 | [diff] [blame] | 1363 | if (apage) |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1364 | return; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1365 | |
Jaegeuk Kim | 300e129 | 2016-04-29 16:11:53 -0700 | [diff] [blame] | 1366 | apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1367 | if (!apage) |
| 1368 | return; |
| 1369 | |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 1370 | err = read_node_page(apage, REQ_RAHEAD); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 1371 | f2fs_put_page(apage, err ? 1 : 0); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1372 | } |
| 1373 | |
Jaegeuk Kim | 17a0ee5 | 2016-03-08 09:04:35 -0800 | [diff] [blame] | 1374 | static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid, |
Chao Yu | 0e022ea | 2016-01-05 16:52:46 +0800 | [diff] [blame] | 1375 | struct page *parent, int start) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1376 | { |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1377 | struct page *page; |
| 1378 | int err; |
Jaegeuk Kim | 4aa69d5 | 2015-12-23 14:17:47 -0800 | [diff] [blame] | 1379 | |
| 1380 | if (!nid) |
| 1381 | return ERR_PTR(-ENOENT); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1382 | if (f2fs_check_nid_range(sbi, nid)) |
Jaegeuk Kim | a4f843b | 2018-04-23 23:02:31 -0600 | [diff] [blame] | 1383 | return ERR_PTR(-EINVAL); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1384 | repeat: |
Jaegeuk Kim | 300e129 | 2016-04-29 16:11:53 -0700 | [diff] [blame] | 1385 | page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1386 | if (!page) |
| 1387 | return ERR_PTR(-ENOMEM); |
| 1388 | |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 1389 | err = read_node_page(page, 0); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 1390 | if (err < 0) { |
| 1391 | f2fs_put_page(page, 1); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1392 | return ERR_PTR(err); |
Chao Yu | e1c51b9f | 2015-12-11 16:08:22 +0800 | [diff] [blame] | 1393 | } else if (err == LOCKED_PAGE) { |
Chao Yu | 1f258ec | 2017-06-07 11:17:35 +0800 | [diff] [blame] | 1394 | err = 0; |
Chao Yu | e1c51b9f | 2015-12-11 16:08:22 +0800 | [diff] [blame] | 1395 | goto page_hit; |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 1396 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1397 | |
Chao Yu | 0e022ea | 2016-01-05 16:52:46 +0800 | [diff] [blame] | 1398 | if (parent) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1399 | f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE); |
Chao Yu | 0e022ea | 2016-01-05 16:52:46 +0800 | [diff] [blame] | 1400 | |
Chao Yu | e1c51b9f | 2015-12-11 16:08:22 +0800 | [diff] [blame] | 1401 | lock_page(page); |
| 1402 | |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1403 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1404 | f2fs_put_page(page, 1); |
| 1405 | goto repeat; |
| 1406 | } |
Chao Yu | 1563ac7 | 2016-07-03 22:05:12 +0800 | [diff] [blame] | 1407 | |
Chao Yu | 1f258ec | 2017-06-07 11:17:35 +0800 | [diff] [blame] | 1408 | if (unlikely(!PageUptodate(page))) { |
| 1409 | err = -EIO; |
Chao Yu | 1563ac7 | 2016-07-03 22:05:12 +0800 | [diff] [blame] | 1410 | goto out_err; |
Chao Yu | 1f258ec | 2017-06-07 11:17:35 +0800 | [diff] [blame] | 1411 | } |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 1412 | |
| 1413 | if (!f2fs_inode_chksum_verify(sbi, page)) { |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 1414 | err = -EFSBADCRC; |
Chao Yu | 704956e | 2017-07-31 20:19:09 +0800 | [diff] [blame] | 1415 | goto out_err; |
| 1416 | } |
Chao Yu | e1c51b9f | 2015-12-11 16:08:22 +0800 | [diff] [blame] | 1417 | page_hit: |
Yi Zhuang | f10ea3c | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 1418 | if (unlikely(nid != nid_of_node(page))) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 1419 | f2fs_warn(sbi, "inconsistent node block, nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]", |
| 1420 | nid, nid_of_node(page), ino_of_node(page), |
| 1421 | ofs_of_node(page), cpver_of_node(page), |
| 1422 | next_blkaddr_of_node(page)); |
Weichao Guo | 22423c9 | 2021-09-18 20:46:36 +0800 | [diff] [blame] | 1423 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
Chao Yu | 1f258ec | 2017-06-07 11:17:35 +0800 | [diff] [blame] | 1424 | err = -EINVAL; |
Yunlong Song | 0c9df7f | 2016-05-26 19:40:29 +0800 | [diff] [blame] | 1425 | out_err: |
Jaegeuk Kim | ee60523 | 2017-08-31 16:54:51 -0700 | [diff] [blame] | 1426 | ClearPageUptodate(page); |
Yunlong Song | 0c9df7f | 2016-05-26 19:40:29 +0800 | [diff] [blame] | 1427 | f2fs_put_page(page, 1); |
Chao Yu | 1f258ec | 2017-06-07 11:17:35 +0800 | [diff] [blame] | 1428 | return ERR_PTR(err); |
Yunlong Song | 0c9df7f | 2016-05-26 19:40:29 +0800 | [diff] [blame] | 1429 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1430 | return page; |
| 1431 | } |
| 1432 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1433 | struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid) |
Chao Yu | 0e022ea | 2016-01-05 16:52:46 +0800 | [diff] [blame] | 1434 | { |
| 1435 | return __get_node_page(sbi, nid, NULL, 0); |
| 1436 | } |
| 1437 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1438 | struct page *f2fs_get_node_page_ra(struct page *parent, int start) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1439 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1440 | struct f2fs_sb_info *sbi = F2FS_P_SB(parent); |
Chao Yu | 0e022ea | 2016-01-05 16:52:46 +0800 | [diff] [blame] | 1441 | nid_t nid = get_nid(parent, start, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1442 | |
Chao Yu | 0e022ea | 2016-01-05 16:52:46 +0800 | [diff] [blame] | 1443 | return __get_node_page(sbi, nid, parent, start); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1444 | } |
| 1445 | |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 1446 | static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino) |
| 1447 | { |
| 1448 | struct inode *inode; |
| 1449 | struct page *page; |
Chao Yu | 0f3311a | 2016-05-21 00:11:09 +0800 | [diff] [blame] | 1450 | int ret; |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 1451 | |
| 1452 | /* should flush inline_data before evict_inode */ |
| 1453 | inode = ilookup(sbi->sb, ino); |
| 1454 | if (!inode) |
| 1455 | return; |
| 1456 | |
Chao Yu | 01eccef | 2017-10-28 16:52:30 +0800 | [diff] [blame] | 1457 | page = f2fs_pagecache_get_page(inode->i_mapping, 0, |
| 1458 | FGP_LOCK|FGP_NOWAIT, 0); |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 1459 | if (!page) |
| 1460 | goto iput_out; |
| 1461 | |
| 1462 | if (!PageUptodate(page)) |
| 1463 | goto page_out; |
| 1464 | |
| 1465 | if (!PageDirty(page)) |
| 1466 | goto page_out; |
| 1467 | |
| 1468 | if (!clear_page_dirty_for_io(page)) |
| 1469 | goto page_out; |
| 1470 | |
Chao Yu | 0f3311a | 2016-05-21 00:11:09 +0800 | [diff] [blame] | 1471 | ret = f2fs_write_inline_data(inode, page); |
| 1472 | inode_dec_dirty_pages(inode); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1473 | f2fs_remove_dirty_inode(inode); |
Chao Yu | 0f3311a | 2016-05-21 00:11:09 +0800 | [diff] [blame] | 1474 | if (ret) |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 1475 | set_page_dirty(page); |
| 1476 | page_out: |
Jaegeuk Kim | 4a6de50 | 2016-03-30 11:25:31 -0700 | [diff] [blame] | 1477 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 1478 | iput_out: |
| 1479 | iput(inode); |
| 1480 | } |
| 1481 | |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1482 | static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1483 | { |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1484 | pgoff_t index; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1485 | struct pagevec pvec; |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1486 | struct page *last_page = NULL; |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1487 | int nr_pages; |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1488 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 1489 | pagevec_init(&pvec); |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1490 | index = 0; |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1491 | |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1492 | while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index, |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 1493 | PAGECACHE_TAG_DIRTY))) { |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1494 | int i; |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1495 | |
| 1496 | for (i = 0; i < nr_pages; i++) { |
| 1497 | struct page *page = pvec.pages[i]; |
| 1498 | |
| 1499 | if (unlikely(f2fs_cp_error(sbi))) { |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1500 | f2fs_put_page(last_page, 0); |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1501 | pagevec_release(&pvec); |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1502 | return ERR_PTR(-EIO); |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | if (!IS_DNODE(page) || !is_cold_node(page)) |
| 1506 | continue; |
| 1507 | if (ino_of_node(page) != ino) |
| 1508 | continue; |
| 1509 | |
| 1510 | lock_page(page); |
| 1511 | |
| 1512 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
| 1513 | continue_unlock: |
| 1514 | unlock_page(page); |
| 1515 | continue; |
| 1516 | } |
| 1517 | if (ino_of_node(page) != ino) |
| 1518 | goto continue_unlock; |
| 1519 | |
| 1520 | if (!PageDirty(page)) { |
| 1521 | /* someone wrote it for us */ |
| 1522 | goto continue_unlock; |
| 1523 | } |
| 1524 | |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1525 | if (last_page) |
| 1526 | f2fs_put_page(last_page, 0); |
| 1527 | |
| 1528 | get_page(page); |
| 1529 | last_page = page; |
| 1530 | unlock_page(page); |
| 1531 | } |
| 1532 | pagevec_release(&pvec); |
| 1533 | cond_resched(); |
| 1534 | } |
| 1535 | return last_page; |
| 1536 | } |
| 1537 | |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1538 | static int __write_node_page(struct page *page, bool atomic, bool *submitted, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1539 | struct writeback_control *wbc, bool do_balance, |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 1540 | enum iostat_type io_type, unsigned int *seq_id) |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1541 | { |
| 1542 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
| 1543 | nid_t nid; |
| 1544 | struct node_info ni; |
| 1545 | struct f2fs_io_info fio = { |
| 1546 | .sbi = sbi, |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 1547 | .ino = ino_of_node(page), |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1548 | .type = NODE, |
| 1549 | .op = REQ_OP_WRITE, |
| 1550 | .op_flags = wbc_to_write_flags(wbc), |
| 1551 | .page = page, |
| 1552 | .encrypted_page = NULL, |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1553 | .submitted = false, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1554 | .io_type = io_type, |
Yufen Yu | 578c647 | 2018-01-09 19:33:39 +0800 | [diff] [blame] | 1555 | .io_wbc = wbc, |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1556 | }; |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 1557 | unsigned int seq; |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1558 | |
| 1559 | trace_f2fs_writepage(page, NODE); |
| 1560 | |
Jaegeuk Kim | 6d7c865 | 2020-05-18 18:00:33 -0700 | [diff] [blame] | 1561 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1562 | if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) { |
| 1563 | ClearPageUptodate(page); |
| 1564 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1565 | unlock_page(page); |
| 1566 | return 0; |
| 1567 | } |
Jaegeuk Kim | 868de61 | 2018-05-04 18:04:22 -0700 | [diff] [blame] | 1568 | goto redirty_out; |
Jaegeuk Kim | 6d7c865 | 2020-05-18 18:00:33 -0700 | [diff] [blame] | 1569 | } |
Chao Yu | db198ae | 2018-01-18 17:29:10 +0800 | [diff] [blame] | 1570 | |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1571 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1572 | goto redirty_out; |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1573 | |
Jaegeuk Kim | 100c065 | 2019-08-28 19:58:26 -0700 | [diff] [blame] | 1574 | if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
| 1575 | wbc->sync_mode == WB_SYNC_NONE && |
Chao Yu | fd8c8ca | 2018-07-25 19:16:21 +0800 | [diff] [blame] | 1576 | IS_DNODE(page) && is_cold_node(page)) |
| 1577 | goto redirty_out; |
| 1578 | |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1579 | /* get old block addr of this node page */ |
| 1580 | nid = nid_of_node(page); |
| 1581 | f2fs_bug_on(sbi, page->index != nid); |
| 1582 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1583 | if (f2fs_get_node_info(sbi, nid, &ni)) |
| 1584 | goto redirty_out; |
| 1585 | |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1586 | if (wbc->for_reclaim) { |
| 1587 | if (!down_read_trylock(&sbi->node_write)) |
| 1588 | goto redirty_out; |
| 1589 | } else { |
| 1590 | down_read(&sbi->node_write); |
| 1591 | } |
| 1592 | |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1593 | /* This page is already truncated */ |
| 1594 | if (unlikely(ni.blk_addr == NULL_ADDR)) { |
| 1595 | ClearPageUptodate(page); |
| 1596 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1597 | up_read(&sbi->node_write); |
| 1598 | unlock_page(page); |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
Chao Yu | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 1602 | if (__is_valid_data_blkaddr(ni.blk_addr) && |
Chao Yu | 93770ab | 2019-04-15 15:26:32 +0800 | [diff] [blame] | 1603 | !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, |
| 1604 | DATA_GENERIC_ENHANCE)) { |
Jaegeuk Kim | 89d13c3 | 2018-09-27 22:15:31 -0700 | [diff] [blame] | 1605 | up_read(&sbi->node_write); |
Chao Yu | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 1606 | goto redirty_out; |
Jaegeuk Kim | 89d13c3 | 2018-09-27 22:15:31 -0700 | [diff] [blame] | 1607 | } |
Chao Yu | c9b6078 | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 1608 | |
Jaegeuk Kim | e7c75ab | 2017-02-02 18:18:06 -0800 | [diff] [blame] | 1609 | if (atomic && !test_opt(sbi, NOBARRIER)) |
| 1610 | fio.op_flags |= REQ_PREFLUSH | REQ_FUA; |
| 1611 | |
Chao Yu | dc5a941 | 2020-02-14 17:45:12 +0800 | [diff] [blame] | 1612 | /* should add to global list before clearing PAGECACHE status */ |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 1613 | if (f2fs_in_warm_node_list(sbi, page)) { |
| 1614 | seq = f2fs_add_fsync_node_entry(sbi, page); |
| 1615 | if (seq_id) |
| 1616 | *seq_id = seq; |
| 1617 | } |
| 1618 | |
Chao Yu | dc5a941 | 2020-02-14 17:45:12 +0800 | [diff] [blame] | 1619 | set_page_writeback(page); |
| 1620 | ClearPageError(page); |
| 1621 | |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1622 | fio.old_blkaddr = ni.blk_addr; |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1623 | f2fs_do_write_node_page(nid, &fio); |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1624 | set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page)); |
| 1625 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1626 | up_read(&sbi->node_write); |
| 1627 | |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1628 | if (wbc->for_reclaim) { |
Chao Yu | bab475c | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 1629 | f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE); |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1630 | submitted = NULL; |
| 1631 | } |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1632 | |
| 1633 | unlock_page(page); |
| 1634 | |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1635 | if (unlikely(f2fs_cp_error(sbi))) { |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 1636 | f2fs_submit_merged_write(sbi, NODE); |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1637 | submitted = NULL; |
| 1638 | } |
| 1639 | if (submitted) |
| 1640 | *submitted = fio.submitted; |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1641 | |
Yunlong Song | 401db79 | 2017-07-27 20:11:00 +0800 | [diff] [blame] | 1642 | if (do_balance) |
| 1643 | f2fs_balance_fs(sbi, false); |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1644 | return 0; |
| 1645 | |
| 1646 | redirty_out: |
| 1647 | redirty_page_for_writepage(wbc, page); |
| 1648 | return AOP_WRITEPAGE_ACTIVATE; |
| 1649 | } |
| 1650 | |
Chao Yu | 48018b4 | 2018-09-13 07:40:53 +0800 | [diff] [blame] | 1651 | int f2fs_move_node_page(struct page *node_page, int gc_type) |
Yunlei He | f15194f | 2017-10-30 14:18:55 +0800 | [diff] [blame] | 1652 | { |
Chao Yu | 48018b4 | 2018-09-13 07:40:53 +0800 | [diff] [blame] | 1653 | int err = 0; |
| 1654 | |
Yunlei He | f15194f | 2017-10-30 14:18:55 +0800 | [diff] [blame] | 1655 | if (gc_type == FG_GC) { |
| 1656 | struct writeback_control wbc = { |
| 1657 | .sync_mode = WB_SYNC_ALL, |
| 1658 | .nr_to_write = 1, |
| 1659 | .for_reclaim = 0, |
| 1660 | }; |
| 1661 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 1662 | f2fs_wait_on_page_writeback(node_page, NODE, true, true); |
Chao Yu | 8d64d36 | 2018-12-12 18:12:30 +0800 | [diff] [blame] | 1663 | |
| 1664 | set_page_dirty(node_page); |
| 1665 | |
Chao Yu | 48018b4 | 2018-09-13 07:40:53 +0800 | [diff] [blame] | 1666 | if (!clear_page_dirty_for_io(node_page)) { |
| 1667 | err = -EAGAIN; |
Yunlei He | f15194f | 2017-10-30 14:18:55 +0800 | [diff] [blame] | 1668 | goto out_page; |
Chao Yu | 48018b4 | 2018-09-13 07:40:53 +0800 | [diff] [blame] | 1669 | } |
Yunlei He | f15194f | 2017-10-30 14:18:55 +0800 | [diff] [blame] | 1670 | |
| 1671 | if (__write_node_page(node_page, false, NULL, |
Chao Yu | 48018b4 | 2018-09-13 07:40:53 +0800 | [diff] [blame] | 1672 | &wbc, false, FS_GC_NODE_IO, NULL)) { |
| 1673 | err = -EAGAIN; |
Yunlei He | f15194f | 2017-10-30 14:18:55 +0800 | [diff] [blame] | 1674 | unlock_page(node_page); |
Chao Yu | 48018b4 | 2018-09-13 07:40:53 +0800 | [diff] [blame] | 1675 | } |
Yunlei He | f15194f | 2017-10-30 14:18:55 +0800 | [diff] [blame] | 1676 | goto release_page; |
| 1677 | } else { |
| 1678 | /* set page dirty and write it */ |
| 1679 | if (!PageWriteback(node_page)) |
| 1680 | set_page_dirty(node_page); |
| 1681 | } |
| 1682 | out_page: |
| 1683 | unlock_page(node_page); |
| 1684 | release_page: |
| 1685 | f2fs_put_page(node_page, 0); |
Chao Yu | 48018b4 | 2018-09-13 07:40:53 +0800 | [diff] [blame] | 1686 | return err; |
Yunlei He | f15194f | 2017-10-30 14:18:55 +0800 | [diff] [blame] | 1687 | } |
| 1688 | |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1689 | static int f2fs_write_node_page(struct page *page, |
| 1690 | struct writeback_control *wbc) |
| 1691 | { |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 1692 | return __write_node_page(page, false, NULL, wbc, false, |
| 1693 | FS_NODE_IO, NULL); |
Jaegeuk Kim | faa2489 | 2017-02-02 18:27:17 -0800 | [diff] [blame] | 1694 | } |
| 1695 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1696 | int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode, |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 1697 | struct writeback_control *wbc, bool atomic, |
| 1698 | unsigned int *seq_id) |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1699 | { |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1700 | pgoff_t index; |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1701 | struct pagevec pvec; |
| 1702 | int ret = 0; |
| 1703 | struct page *last_page = NULL; |
| 1704 | bool marked = false; |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 1705 | nid_t ino = inode->i_ino; |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1706 | int nr_pages; |
Chao Yu | bab475c | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 1707 | int nwritten = 0; |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1708 | |
| 1709 | if (atomic) { |
| 1710 | last_page = last_fsync_dnode(sbi, ino); |
| 1711 | if (IS_ERR_OR_NULL(last_page)) |
| 1712 | return PTR_ERR_OR_ZERO(last_page); |
| 1713 | } |
| 1714 | retry: |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 1715 | pagevec_init(&pvec); |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1716 | index = 0; |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1717 | |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1718 | while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index, |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 1719 | PAGECACHE_TAG_DIRTY))) { |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1720 | int i; |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1721 | |
| 1722 | for (i = 0; i < nr_pages; i++) { |
| 1723 | struct page *page = pvec.pages[i]; |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1724 | bool submitted = false; |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1725 | |
| 1726 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1727 | f2fs_put_page(last_page, 0); |
| 1728 | pagevec_release(&pvec); |
Chao Yu | 9de6927 | 2016-10-11 22:57:06 +0800 | [diff] [blame] | 1729 | ret = -EIO; |
| 1730 | goto out; |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | if (!IS_DNODE(page) || !is_cold_node(page)) |
| 1734 | continue; |
| 1735 | if (ino_of_node(page) != ino) |
| 1736 | continue; |
| 1737 | |
| 1738 | lock_page(page); |
| 1739 | |
| 1740 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
| 1741 | continue_unlock: |
| 1742 | unlock_page(page); |
| 1743 | continue; |
| 1744 | } |
| 1745 | if (ino_of_node(page) != ino) |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1746 | goto continue_unlock; |
| 1747 | |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1748 | if (!PageDirty(page) && page != last_page) { |
| 1749 | /* someone wrote it for us */ |
| 1750 | goto continue_unlock; |
| 1751 | } |
| 1752 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 1753 | f2fs_wait_on_page_writeback(page, NODE, true, true); |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1754 | |
Jaegeuk Kim | d29fd17 | 2017-04-12 12:02:00 -0700 | [diff] [blame] | 1755 | set_fsync_mark(page, 0); |
| 1756 | set_dentry_mark(page, 0); |
| 1757 | |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1758 | if (!atomic || page == last_page) { |
| 1759 | set_fsync_mark(page, 1); |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 1760 | if (IS_INODE(page)) { |
| 1761 | if (is_inode_flag_set(inode, |
| 1762 | FI_DIRTY_INODE)) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1763 | f2fs_update_inode(inode, page); |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1764 | set_dentry_mark(page, |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1765 | f2fs_need_dentry_mark(sbi, ino)); |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 1766 | } |
Jack Qiu | a87aff1 | 2020-07-24 16:55:28 +0800 | [diff] [blame] | 1767 | /* may be written by other thread */ |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1768 | if (!PageDirty(page)) |
| 1769 | set_page_dirty(page); |
| 1770 | } |
| 1771 | |
| 1772 | if (!clear_page_dirty_for_io(page)) |
| 1773 | goto continue_unlock; |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1774 | |
Jaegeuk Kim | e7c75ab | 2017-02-02 18:18:06 -0800 | [diff] [blame] | 1775 | ret = __write_node_page(page, atomic && |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1776 | page == last_page, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1777 | &submitted, wbc, true, |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 1778 | FS_NODE_IO, seq_id); |
Jaegeuk Kim | c267ec1 | 2016-04-15 09:25:04 -0700 | [diff] [blame] | 1779 | if (ret) { |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1780 | unlock_page(page); |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1781 | f2fs_put_page(last_page, 0); |
| 1782 | break; |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1783 | } else if (submitted) { |
Chao Yu | bab475c | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 1784 | nwritten++; |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1785 | } |
Chao Yu | 3f5f495 | 2016-09-29 18:50:10 +0800 | [diff] [blame] | 1786 | |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1787 | if (page == last_page) { |
| 1788 | f2fs_put_page(page, 0); |
| 1789 | marked = true; |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1790 | break; |
Jaegeuk Kim | c267ec1 | 2016-04-15 09:25:04 -0700 | [diff] [blame] | 1791 | } |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1792 | } |
| 1793 | pagevec_release(&pvec); |
| 1794 | cond_resched(); |
| 1795 | |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1796 | if (ret || marked) |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1797 | break; |
| 1798 | } |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1799 | if (!ret && atomic && !marked) { |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 1800 | f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx", |
| 1801 | ino, last_page->index); |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1802 | lock_page(last_page); |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 1803 | f2fs_wait_on_page_writeback(last_page, NODE, true, true); |
Jaegeuk Kim | 608514d | 2016-04-15 09:43:17 -0700 | [diff] [blame] | 1804 | set_page_dirty(last_page); |
| 1805 | unlock_page(last_page); |
| 1806 | goto retry; |
| 1807 | } |
Chao Yu | 9de6927 | 2016-10-11 22:57:06 +0800 | [diff] [blame] | 1808 | out: |
Chao Yu | bab475c | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 1809 | if (nwritten) |
| 1810 | f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE); |
Yi Zhuang | f10ea3c | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 1811 | return ret ? -EIO : 0; |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Chao Yu | 052a82d | 2019-08-22 20:17:56 +0800 | [diff] [blame] | 1814 | static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data) |
| 1815 | { |
| 1816 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1817 | bool clean; |
| 1818 | |
| 1819 | if (inode->i_ino != ino) |
| 1820 | return 0; |
| 1821 | |
| 1822 | if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) |
| 1823 | return 0; |
| 1824 | |
| 1825 | spin_lock(&sbi->inode_lock[DIRTY_META]); |
| 1826 | clean = list_empty(&F2FS_I(inode)->gdirty_list); |
| 1827 | spin_unlock(&sbi->inode_lock[DIRTY_META]); |
| 1828 | |
| 1829 | if (clean) |
| 1830 | return 0; |
| 1831 | |
| 1832 | inode = igrab(inode); |
| 1833 | if (!inode) |
| 1834 | return 0; |
| 1835 | return 1; |
| 1836 | } |
| 1837 | |
| 1838 | static bool flush_dirty_inode(struct page *page) |
| 1839 | { |
| 1840 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
| 1841 | struct inode *inode; |
| 1842 | nid_t ino = ino_of_node(page); |
| 1843 | |
| 1844 | inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL); |
| 1845 | if (!inode) |
| 1846 | return false; |
| 1847 | |
| 1848 | f2fs_update_inode(inode, page); |
| 1849 | unlock_page(page); |
| 1850 | |
| 1851 | iput(inode); |
| 1852 | return true; |
| 1853 | } |
| 1854 | |
Jia Yang | 68e79ba | 2020-07-21 11:49:14 +0800 | [diff] [blame] | 1855 | void f2fs_flush_inline_data(struct f2fs_sb_info *sbi) |
Sayali Lokhande | 34c061a | 2020-04-30 16:28:29 +0530 | [diff] [blame] | 1856 | { |
| 1857 | pgoff_t index = 0; |
| 1858 | struct pagevec pvec; |
| 1859 | int nr_pages; |
Sayali Lokhande | 34c061a | 2020-04-30 16:28:29 +0530 | [diff] [blame] | 1860 | |
| 1861 | pagevec_init(&pvec); |
| 1862 | |
| 1863 | while ((nr_pages = pagevec_lookup_tag(&pvec, |
| 1864 | NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) { |
| 1865 | int i; |
| 1866 | |
| 1867 | for (i = 0; i < nr_pages; i++) { |
| 1868 | struct page *page = pvec.pages[i]; |
| 1869 | |
| 1870 | if (!IS_DNODE(page)) |
| 1871 | continue; |
| 1872 | |
| 1873 | lock_page(page); |
| 1874 | |
| 1875 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
| 1876 | continue_unlock: |
| 1877 | unlock_page(page); |
| 1878 | continue; |
| 1879 | } |
| 1880 | |
| 1881 | if (!PageDirty(page)) { |
| 1882 | /* someone wrote it for us */ |
| 1883 | goto continue_unlock; |
| 1884 | } |
| 1885 | |
| 1886 | /* flush inline_data, if it's async context. */ |
Chao Yu | bdc14e1 | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 1887 | if (page_private_inline(page)) { |
| 1888 | clear_page_private_inline(page); |
Sayali Lokhande | 34c061a | 2020-04-30 16:28:29 +0530 | [diff] [blame] | 1889 | unlock_page(page); |
| 1890 | flush_inline_data(sbi, ino_of_node(page)); |
| 1891 | continue; |
| 1892 | } |
| 1893 | unlock_page(page); |
| 1894 | } |
| 1895 | pagevec_release(&pvec); |
| 1896 | cond_resched(); |
| 1897 | } |
Sayali Lokhande | 34c061a | 2020-04-30 16:28:29 +0530 | [diff] [blame] | 1898 | } |
| 1899 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1900 | int f2fs_sync_node_pages(struct f2fs_sb_info *sbi, |
| 1901 | struct writeback_control *wbc, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1902 | bool do_balance, enum iostat_type io_type) |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1903 | { |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1904 | pgoff_t index; |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1905 | struct pagevec pvec; |
| 1906 | int step = 0; |
Jaegeuk Kim | 12bb0a8 | 2016-03-11 15:33:22 -0800 | [diff] [blame] | 1907 | int nwritten = 0; |
Chao Yu | 3f5f495 | 2016-09-29 18:50:10 +0800 | [diff] [blame] | 1908 | int ret = 0; |
Chao Yu | c29fd0c | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 1909 | int nr_pages, done = 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1910 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 1911 | pagevec_init(&pvec); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1912 | |
| 1913 | next_step: |
| 1914 | index = 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1915 | |
Chao Yu | c29fd0c | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 1916 | while (!done && (nr_pages = pagevec_lookup_tag(&pvec, |
| 1917 | NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) { |
Jan Kara | 028a63a | 2017-11-15 17:34:51 -0800 | [diff] [blame] | 1918 | int i; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1919 | |
| 1920 | for (i = 0; i < nr_pages; i++) { |
| 1921 | struct page *page = pvec.pages[i]; |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1922 | bool submitted = false; |
Chao Yu | 052a82d | 2019-08-22 20:17:56 +0800 | [diff] [blame] | 1923 | bool may_dirty = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1924 | |
Chao Yu | c29fd0c | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 1925 | /* give a priority to WB_SYNC threads */ |
| 1926 | if (atomic_read(&sbi->wb_sync_req[NODE]) && |
| 1927 | wbc->sync_mode == WB_SYNC_NONE) { |
| 1928 | done = 1; |
| 1929 | break; |
| 1930 | } |
| 1931 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1932 | /* |
| 1933 | * flushing sequence with step: |
| 1934 | * 0. indirect nodes |
| 1935 | * 1. dentry dnodes |
| 1936 | * 2. file dnodes |
| 1937 | */ |
| 1938 | if (step == 0 && IS_DNODE(page)) |
| 1939 | continue; |
| 1940 | if (step == 1 && (!IS_DNODE(page) || |
| 1941 | is_cold_node(page))) |
| 1942 | continue; |
| 1943 | if (step == 2 && (!IS_DNODE(page) || |
| 1944 | !is_cold_node(page))) |
| 1945 | continue; |
Chao Yu | 9a4cbc9 | 2016-02-22 18:35:46 +0800 | [diff] [blame] | 1946 | lock_node: |
Chao Yu | 4b270a8 | 2018-07-04 18:04:10 +0800 | [diff] [blame] | 1947 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 1948 | lock_page(page); |
| 1949 | else if (!trylock_page(page)) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1950 | continue; |
| 1951 | |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1952 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1953 | continue_unlock: |
| 1954 | unlock_page(page); |
| 1955 | continue; |
| 1956 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1957 | |
| 1958 | if (!PageDirty(page)) { |
| 1959 | /* someone wrote it for us */ |
| 1960 | goto continue_unlock; |
| 1961 | } |
| 1962 | |
Jaegeuk Kim | b0f3b87 | 2020-07-16 09:57:03 -0700 | [diff] [blame] | 1963 | /* flush inline_data/inode, if it's async context. */ |
| 1964 | if (!do_balance) |
| 1965 | goto write_node; |
| 1966 | |
| 1967 | /* flush inline_data */ |
Chao Yu | bdc14e1 | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 1968 | if (page_private_inline(page)) { |
| 1969 | clear_page_private_inline(page); |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 1970 | unlock_page(page); |
| 1971 | flush_inline_data(sbi, ino_of_node(page)); |
Chao Yu | 9a4cbc9 | 2016-02-22 18:35:46 +0800 | [diff] [blame] | 1972 | goto lock_node; |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
Chao Yu | 052a82d | 2019-08-22 20:17:56 +0800 | [diff] [blame] | 1975 | /* flush dirty inode */ |
| 1976 | if (IS_INODE(page) && may_dirty) { |
| 1977 | may_dirty = false; |
| 1978 | if (flush_dirty_inode(page)) |
| 1979 | goto lock_node; |
| 1980 | } |
Jaegeuk Kim | b0f3b87 | 2020-07-16 09:57:03 -0700 | [diff] [blame] | 1981 | write_node: |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 1982 | f2fs_wait_on_page_writeback(page, NODE, true, true); |
Jaegeuk Kim | fa3d2bd | 2016-01-28 11:48:52 -0800 | [diff] [blame] | 1983 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1984 | if (!clear_page_dirty_for_io(page)) |
| 1985 | goto continue_unlock; |
| 1986 | |
Jaegeuk Kim | 5268137 | 2016-04-13 16:24:44 -0700 | [diff] [blame] | 1987 | set_fsync_mark(page, 0); |
| 1988 | set_dentry_mark(page, 0); |
Jaegeuk Kim | 5274651 | 2014-08-11 18:18:36 -0700 | [diff] [blame] | 1989 | |
Yunlong Song | 401db79 | 2017-07-27 20:11:00 +0800 | [diff] [blame] | 1990 | ret = __write_node_page(page, false, &submitted, |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 1991 | wbc, do_balance, io_type, NULL); |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1992 | if (ret) |
Jaegeuk Kim | 5274651 | 2014-08-11 18:18:36 -0700 | [diff] [blame] | 1993 | unlock_page(page); |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1994 | else if (submitted) |
Chao Yu | 3f5f495 | 2016-09-29 18:50:10 +0800 | [diff] [blame] | 1995 | nwritten++; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1996 | |
| 1997 | if (--wbc->nr_to_write == 0) |
| 1998 | break; |
| 1999 | } |
| 2000 | pagevec_release(&pvec); |
| 2001 | cond_resched(); |
| 2002 | |
| 2003 | if (wbc->nr_to_write == 0) { |
| 2004 | step = 2; |
| 2005 | break; |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | if (step < 2) { |
Jaegeuk Kim | 100c065 | 2019-08-28 19:58:26 -0700 | [diff] [blame] | 2010 | if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
| 2011 | wbc->sync_mode == WB_SYNC_NONE && step == 1) |
Chao Yu | fd8c8ca | 2018-07-25 19:16:21 +0800 | [diff] [blame] | 2012 | goto out; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2013 | step++; |
| 2014 | goto next_step; |
| 2015 | } |
Chao Yu | fd8c8ca | 2018-07-25 19:16:21 +0800 | [diff] [blame] | 2016 | out: |
Chao Yu | 3f5f495 | 2016-09-29 18:50:10 +0800 | [diff] [blame] | 2017 | if (nwritten) |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 2018 | f2fs_submit_merged_write(sbi, NODE); |
Chao Yu | db198ae | 2018-01-18 17:29:10 +0800 | [diff] [blame] | 2019 | |
| 2020 | if (unlikely(f2fs_cp_error(sbi))) |
| 2021 | return -EIO; |
Chao Yu | 3f5f495 | 2016-09-29 18:50:10 +0800 | [diff] [blame] | 2022 | return ret; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2023 | } |
| 2024 | |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 2025 | int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, |
| 2026 | unsigned int seq_id) |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 2027 | { |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 2028 | struct fsync_node_entry *fn; |
| 2029 | struct page *page; |
| 2030 | struct list_head *head = &sbi->fsync_node_list; |
| 2031 | unsigned long flags; |
| 2032 | unsigned int cur_seq_id = 0; |
Miklos Szeredi | 280db3c | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 2033 | int ret2, ret = 0; |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 2034 | |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 2035 | while (seq_id && cur_seq_id < seq_id) { |
| 2036 | spin_lock_irqsave(&sbi->fsync_node_lock, flags); |
| 2037 | if (list_empty(head)) { |
| 2038 | spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); |
| 2039 | break; |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 2040 | } |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 2041 | fn = list_first_entry(head, struct fsync_node_entry, list); |
| 2042 | if (fn->seq_id > seq_id) { |
| 2043 | spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); |
| 2044 | break; |
| 2045 | } |
| 2046 | cur_seq_id = fn->seq_id; |
| 2047 | page = fn->page; |
| 2048 | get_page(page); |
| 2049 | spin_unlock_irqrestore(&sbi->fsync_node_lock, flags); |
| 2050 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 2051 | f2fs_wait_on_page_writeback(page, NODE, true, false); |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 2052 | if (TestClearPageError(page)) |
| 2053 | ret = -EIO; |
| 2054 | |
| 2055 | put_page(page); |
| 2056 | |
| 2057 | if (ret) |
| 2058 | break; |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 2059 | } |
| 2060 | |
Miklos Szeredi | 280db3c | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 2061 | ret2 = filemap_check_errors(NODE_MAPPING(sbi)); |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 2062 | if (!ret) |
| 2063 | ret = ret2; |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 2064 | |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 2065 | return ret; |
| 2066 | } |
| 2067 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2068 | static int f2fs_write_node_pages(struct address_space *mapping, |
| 2069 | struct writeback_control *wbc) |
| 2070 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 2071 | struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 2072 | struct blk_plug plug; |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 2073 | long diff; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2074 | |
Chao Yu | 0771fcc | 2017-06-29 23:20:45 +0800 | [diff] [blame] | 2075 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 2076 | goto skip_write; |
| 2077 | |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 2078 | /* balancing f2fs's metadata in background */ |
Chao Yu | 7bcd0cf | 2020-03-19 19:57:58 +0800 | [diff] [blame] | 2079 | f2fs_balance_fs_bg(sbi, true); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2080 | |
Jaegeuk Kim | a7fdffb | 2013-01-18 14:54:13 +0900 | [diff] [blame] | 2081 | /* collect a number of dirty node pages and write together */ |
Jaegeuk Kim | 812a959 | 2019-01-22 14:04:33 -0800 | [diff] [blame] | 2082 | if (wbc->sync_mode != WB_SYNC_ALL && |
| 2083 | get_pages(sbi, F2FS_DIRTY_NODES) < |
| 2084 | nr_pages_to_skip(sbi, NODE)) |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 2085 | goto skip_write; |
Jaegeuk Kim | a7fdffb | 2013-01-18 14:54:13 +0900 | [diff] [blame] | 2086 | |
Chao Yu | c29fd0c | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 2087 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 2088 | atomic_inc(&sbi->wb_sync_req[NODE]); |
| 2089 | else if (atomic_read(&sbi->wb_sync_req[NODE])) |
| 2090 | goto skip_write; |
| 2091 | |
Yunlei He | d31c7c3 | 2016-02-04 16:14:00 +0800 | [diff] [blame] | 2092 | trace_f2fs_writepages(mapping->host, wbc, NODE); |
| 2093 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 2094 | diff = nr_pages_to_write(sbi, NODE, wbc); |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 2095 | blk_start_plug(&plug); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2096 | f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO); |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 2097 | blk_finish_plug(&plug); |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 2098 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); |
Chao Yu | c29fd0c | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 2099 | |
| 2100 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 2101 | atomic_dec(&sbi->wb_sync_req[NODE]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2102 | return 0; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 2103 | |
| 2104 | skip_write: |
| 2105 | wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES); |
Yunlei He | d31c7c3 | 2016-02-04 16:14:00 +0800 | [diff] [blame] | 2106 | trace_f2fs_writepages(mapping->host, wbc, NODE); |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 2107 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | static int f2fs_set_node_page_dirty(struct page *page) |
| 2111 | { |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 2112 | trace_f2fs_set_page_dirty(page, NODE); |
| 2113 | |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 2114 | if (!PageUptodate(page)) |
| 2115 | SetPageUptodate(page); |
Weichao Guo | 54c55c4 | 2018-03-09 23:10:21 +0800 | [diff] [blame] | 2116 | #ifdef CONFIG_F2FS_CHECK_FS |
| 2117 | if (IS_INODE(page)) |
| 2118 | f2fs_inode_chksum_set(F2FS_P_SB(page), page); |
| 2119 | #endif |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2120 | if (!PageDirty(page)) { |
Jaegeuk Kim | b87078a | 2018-04-20 19:29:52 -0700 | [diff] [blame] | 2121 | __set_page_dirty_nobuffers(page); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 2122 | inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES); |
Chao Yu | bdc14e1 | 2021-04-28 17:20:31 +0800 | [diff] [blame] | 2123 | set_page_private_reference(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2124 | return 1; |
| 2125 | } |
| 2126 | return 0; |
| 2127 | } |
| 2128 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2129 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2130 | * Structure of the f2fs node operations |
| 2131 | */ |
| 2132 | const struct address_space_operations f2fs_node_aops = { |
| 2133 | .writepage = f2fs_write_node_page, |
| 2134 | .writepages = f2fs_write_node_pages, |
| 2135 | .set_page_dirty = f2fs_set_node_page_dirty, |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2136 | .invalidatepage = f2fs_invalidate_page, |
| 2137 | .releasepage = f2fs_release_page, |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2138 | #ifdef CONFIG_MIGRATION |
Jack Qiu | a87aff1 | 2020-07-24 16:55:28 +0800 | [diff] [blame] | 2139 | .migratepage = f2fs_migrate_page, |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2140 | #endif |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2141 | }; |
| 2142 | |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 2143 | static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i, |
| 2144 | nid_t n) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2145 | { |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 2146 | return radix_tree_lookup(&nm_i->free_nid_root, n); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2147 | } |
| 2148 | |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2149 | static int __insert_free_nid(struct f2fs_sb_info *sbi, |
Liu Song | b815bdc | 2020-06-28 21:48:13 +0800 | [diff] [blame] | 2150 | struct free_nid *i) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2151 | { |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2152 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2153 | int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i); |
Yi Zhuang | f10ea3c | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 2154 | |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2155 | if (err) |
| 2156 | return err; |
Jaegeuk Kim | eb0aa4b | 2016-10-12 10:09:59 -0700 | [diff] [blame] | 2157 | |
Liu Song | b815bdc | 2020-06-28 21:48:13 +0800 | [diff] [blame] | 2158 | nm_i->nid_cnt[FREE_NID]++; |
| 2159 | list_add_tail(&i->list, &nm_i->free_nid_list); |
Jaegeuk Kim | eb0aa4b | 2016-10-12 10:09:59 -0700 | [diff] [blame] | 2160 | return 0; |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2161 | } |
| 2162 | |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2163 | static void __remove_free_nid(struct f2fs_sb_info *sbi, |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2164 | struct free_nid *i, enum nid_state state) |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2165 | { |
| 2166 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2167 | |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2168 | f2fs_bug_on(sbi, state != i->state); |
| 2169 | nm_i->nid_cnt[state]--; |
| 2170 | if (state == FREE_NID) |
| 2171 | list_del(&i->list); |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2172 | radix_tree_delete(&nm_i->free_nid_root, i->nid); |
| 2173 | } |
| 2174 | |
| 2175 | static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i, |
| 2176 | enum nid_state org_state, enum nid_state dst_state) |
| 2177 | { |
| 2178 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2179 | |
| 2180 | f2fs_bug_on(sbi, org_state != i->state); |
| 2181 | i->state = dst_state; |
| 2182 | nm_i->nid_cnt[org_state]--; |
| 2183 | nm_i->nid_cnt[dst_state]++; |
| 2184 | |
| 2185 | switch (dst_state) { |
| 2186 | case PREALLOC_NID: |
| 2187 | list_del(&i->list); |
| 2188 | break; |
| 2189 | case FREE_NID: |
| 2190 | list_add_tail(&i->list, &nm_i->free_nid_list); |
| 2191 | break; |
| 2192 | default: |
| 2193 | BUG_ON(1); |
| 2194 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2195 | } |
| 2196 | |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2197 | static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid, |
| 2198 | bool set, bool build) |
| 2199 | { |
| 2200 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2201 | unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid); |
| 2202 | unsigned int nid_ofs = nid - START_NID(nid); |
| 2203 | |
| 2204 | if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap)) |
| 2205 | return; |
| 2206 | |
| 2207 | if (set) { |
| 2208 | if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs])) |
| 2209 | return; |
| 2210 | __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]); |
| 2211 | nm_i->free_nid_count[nat_ofs]++; |
| 2212 | } else { |
| 2213 | if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs])) |
| 2214 | return; |
| 2215 | __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]); |
| 2216 | if (!build) |
| 2217 | nm_i->free_nid_count[nat_ofs]--; |
| 2218 | } |
| 2219 | } |
| 2220 | |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2221 | /* return if the nid is recognized as free */ |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2222 | static bool add_free_nid(struct f2fs_sb_info *sbi, |
| 2223 | nid_t nid, bool build, bool update) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2224 | { |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 2225 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2226 | struct free_nid *i, *e; |
Jaegeuk Kim | 59bbd47 | 2013-05-07 20:47:40 +0900 | [diff] [blame] | 2227 | struct nat_entry *ne; |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2228 | int err = -EINVAL; |
| 2229 | bool ret = false; |
Jaegeuk Kim | 9198ace | 2013-04-25 13:21:12 +0900 | [diff] [blame] | 2230 | |
| 2231 | /* 0 nid should not be used */ |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 2232 | if (unlikely(nid == 0)) |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2233 | return false; |
Jaegeuk Kim | 59bbd47 | 2013-05-07 20:47:40 +0900 | [diff] [blame] | 2234 | |
Chao Yu | 626bcf2 | 2019-04-15 15:28:36 +0800 | [diff] [blame] | 2235 | if (unlikely(f2fs_check_nid_range(sbi, nid))) |
| 2236 | return false; |
| 2237 | |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 2238 | i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2239 | i->nid = nid; |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2240 | i->state = FREE_NID; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2241 | |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2242 | radix_tree_preload(GFP_NOFS | __GFP_NOFAIL); |
Jaegeuk Kim | 769ec6e | 2014-12-03 20:47:26 -0800 | [diff] [blame] | 2243 | |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2244 | spin_lock(&nm_i->nid_list_lock); |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2245 | |
| 2246 | if (build) { |
| 2247 | /* |
| 2248 | * Thread A Thread B |
| 2249 | * - f2fs_create |
| 2250 | * - f2fs_new_inode |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2251 | * - f2fs_alloc_nid |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2252 | * - __insert_nid_to_list(PREALLOC_NID) |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2253 | * - f2fs_balance_fs_bg |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2254 | * - f2fs_build_free_nids |
| 2255 | * - __f2fs_build_free_nids |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2256 | * - scan_nat_page |
| 2257 | * - add_free_nid |
| 2258 | * - __lookup_nat_cache |
| 2259 | * - f2fs_add_link |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2260 | * - f2fs_init_inode_metadata |
| 2261 | * - f2fs_new_inode_page |
| 2262 | * - f2fs_new_node_page |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2263 | * - set_node_addr |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2264 | * - f2fs_alloc_nid_done |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2265 | * - __remove_nid_from_list(PREALLOC_NID) |
| 2266 | * - __insert_nid_to_list(FREE_NID) |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2267 | */ |
| 2268 | ne = __lookup_nat_cache(nm_i, nid); |
| 2269 | if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) || |
| 2270 | nat_get_blkaddr(ne) != NULL_ADDR)) |
| 2271 | goto err_out; |
| 2272 | |
| 2273 | e = __lookup_free_nid_list(nm_i, nid); |
| 2274 | if (e) { |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2275 | if (e->state == FREE_NID) |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2276 | ret = true; |
| 2277 | goto err_out; |
| 2278 | } |
| 2279 | } |
| 2280 | ret = true; |
Liu Song | b815bdc | 2020-06-28 21:48:13 +0800 | [diff] [blame] | 2281 | err = __insert_free_nid(sbi, i); |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2282 | err_out: |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2283 | if (update) { |
| 2284 | update_free_nid_bitmap(sbi, nid, ret, build); |
| 2285 | if (!build) |
| 2286 | nm_i->available_nids++; |
| 2287 | } |
Jaegeuk Kim | eb0aa4b | 2016-10-12 10:09:59 -0700 | [diff] [blame] | 2288 | spin_unlock(&nm_i->nid_list_lock); |
| 2289 | radix_tree_preload_end(); |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2290 | |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2291 | if (err) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2292 | kmem_cache_free(free_nid_slab, i); |
Chao Yu | 30a61dd | 2017-03-22 14:45:05 +0800 | [diff] [blame] | 2293 | return ret; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2294 | } |
| 2295 | |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2296 | static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2297 | { |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2298 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2299 | struct free_nid *i; |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 2300 | bool need_free = false; |
| 2301 | |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2302 | spin_lock(&nm_i->nid_list_lock); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 2303 | i = __lookup_free_nid_list(nm_i, nid); |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2304 | if (i && i->state == FREE_NID) { |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2305 | __remove_free_nid(sbi, i, FREE_NID); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 2306 | need_free = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2307 | } |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2308 | spin_unlock(&nm_i->nid_list_lock); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 2309 | |
| 2310 | if (need_free) |
| 2311 | kmem_cache_free(free_nid_slab, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2312 | } |
| 2313 | |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2314 | static int scan_nat_page(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2315 | struct page *nat_page, nid_t start_nid) |
| 2316 | { |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 2317 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2318 | struct f2fs_nat_block *nat_blk = page_address(nat_page); |
| 2319 | block_t blk_addr; |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2320 | unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2321 | int i; |
| 2322 | |
Jaegeuk Kim | 23380b8 | 2017-03-07 14:11:06 -0800 | [diff] [blame] | 2323 | __set_bit_le(nat_ofs, nm_i->nat_block_bitmap); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2324 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2325 | i = start_nid % NAT_ENTRY_PER_BLOCK; |
| 2326 | |
| 2327 | for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) { |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 2328 | if (unlikely(start_nid >= nm_i->max_nid)) |
Jaegeuk Kim | 04431c4 | 2013-03-16 08:34:37 +0900 | [diff] [blame] | 2329 | break; |
Haicheng Li | 23d3884 | 2013-05-06 23:15:43 +0800 | [diff] [blame] | 2330 | |
| 2331 | blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr); |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2332 | |
| 2333 | if (blk_addr == NEW_ADDR) |
| 2334 | return -EINVAL; |
| 2335 | |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2336 | if (blk_addr == NULL_ADDR) { |
| 2337 | add_free_nid(sbi, start_nid, true, true); |
| 2338 | } else { |
| 2339 | spin_lock(&NM_I(sbi)->nid_list_lock); |
| 2340 | update_free_nid_bitmap(sbi, start_nid, false, true); |
| 2341 | spin_unlock(&NM_I(sbi)->nid_list_lock); |
| 2342 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2343 | } |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2344 | |
| 2345 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2346 | } |
| 2347 | |
Chao Yu | 2fbaa25 | 2017-11-08 17:47:36 +0800 | [diff] [blame] | 2348 | static void scan_curseg_cache(struct f2fs_sb_info *sbi) |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2349 | { |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2350 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
| 2351 | struct f2fs_journal *journal = curseg->journal; |
Chao Yu | 2fbaa25 | 2017-11-08 17:47:36 +0800 | [diff] [blame] | 2352 | int i; |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2353 | |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2354 | down_read(&curseg->journal_rwsem); |
| 2355 | for (i = 0; i < nats_in_cursum(journal); i++) { |
| 2356 | block_t addr; |
| 2357 | nid_t nid; |
| 2358 | |
| 2359 | addr = le32_to_cpu(nat_in_journal(journal, i).block_addr); |
| 2360 | nid = le32_to_cpu(nid_in_journal(journal, i)); |
| 2361 | if (addr == NULL_ADDR) |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2362 | add_free_nid(sbi, nid, true, false); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2363 | else |
| 2364 | remove_free_nid(sbi, nid); |
| 2365 | } |
| 2366 | up_read(&curseg->journal_rwsem); |
Chao Yu | 2fbaa25 | 2017-11-08 17:47:36 +0800 | [diff] [blame] | 2367 | } |
| 2368 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2369 | static void scan_free_nid_bits(struct f2fs_sb_info *sbi) |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2370 | { |
| 2371 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2372 | unsigned int i, idx; |
Fan Li | 9745657 | 2017-11-07 19:14:24 +0800 | [diff] [blame] | 2373 | nid_t nid; |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2374 | |
| 2375 | down_read(&nm_i->nat_tree_lock); |
| 2376 | |
| 2377 | for (i = 0; i < nm_i->nat_blocks; i++) { |
| 2378 | if (!test_bit_le(i, nm_i->nat_block_bitmap)) |
| 2379 | continue; |
| 2380 | if (!nm_i->free_nid_count[i]) |
| 2381 | continue; |
| 2382 | for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) { |
Fan Li | 9745657 | 2017-11-07 19:14:24 +0800 | [diff] [blame] | 2383 | idx = find_next_bit_le(nm_i->free_nid_bitmap[i], |
| 2384 | NAT_ENTRY_PER_BLOCK, idx); |
| 2385 | if (idx >= NAT_ENTRY_PER_BLOCK) |
| 2386 | break; |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2387 | |
| 2388 | nid = i * NAT_ENTRY_PER_BLOCK + idx; |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2389 | add_free_nid(sbi, nid, true, false); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2390 | |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2391 | if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS) |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2392 | goto out; |
| 2393 | } |
| 2394 | } |
| 2395 | out: |
Chao Yu | 2fbaa25 | 2017-11-08 17:47:36 +0800 | [diff] [blame] | 2396 | scan_curseg_cache(sbi); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2397 | |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2398 | up_read(&nm_i->nat_tree_lock); |
| 2399 | } |
| 2400 | |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2401 | static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi, |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2402 | bool sync, bool mount) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2403 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2404 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2405 | int i = 0, ret; |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2406 | nid_t nid = nm_i->next_scan_nid; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2407 | |
Yunlei He | e9cdd30 | 2017-04-26 15:56:52 +0800 | [diff] [blame] | 2408 | if (unlikely(nid >= nm_i->max_nid)) |
| 2409 | nid = 0; |
| 2410 | |
Sahitya Tummala | e2cab03 | 2020-08-18 15:40:14 +0530 | [diff] [blame] | 2411 | if (unlikely(nid % NAT_ENTRY_PER_BLOCK)) |
| 2412 | nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK; |
| 2413 | |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2414 | /* Enough entries */ |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2415 | if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK) |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2416 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2417 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2418 | if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS)) |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2419 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2420 | |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2421 | if (!mount) { |
| 2422 | /* try to find free nids in free_nid_bitmap */ |
| 2423 | scan_free_nid_bits(sbi); |
| 2424 | |
Fan Li | 7498621 | 2017-11-07 11:04:33 +0800 | [diff] [blame] | 2425 | if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK) |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2426 | return 0; |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2427 | } |
| 2428 | |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2429 | /* readahead nat pages to be scanned */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2430 | f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, |
Chao Yu | 26879fb | 2015-10-12 17:05:59 +0800 | [diff] [blame] | 2431 | META_NAT, true); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2432 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 2433 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | a513119 | 2016-01-02 09:19:41 -0800 | [diff] [blame] | 2434 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2435 | while (1) { |
Yunlei He | 66e8336 | 2017-11-17 16:13:38 +0800 | [diff] [blame] | 2436 | if (!test_bit_le(NAT_BLOCK_OFFSET(nid), |
| 2437 | nm_i->nat_block_bitmap)) { |
| 2438 | struct page *page = get_current_nat_page(sbi, nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2439 | |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 2440 | if (IS_ERR(page)) { |
| 2441 | ret = PTR_ERR(page); |
| 2442 | } else { |
| 2443 | ret = scan_nat_page(sbi, page, nid); |
| 2444 | f2fs_put_page(page, 1); |
| 2445 | } |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2446 | |
| 2447 | if (ret) { |
| 2448 | up_read(&nm_i->nat_tree_lock); |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 2449 | f2fs_err(sbi, "NAT is corrupt, run fsck to fix it"); |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 2450 | return ret; |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2451 | } |
Yunlei He | 66e8336 | 2017-11-17 16:13:38 +0800 | [diff] [blame] | 2452 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2453 | |
| 2454 | nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK)); |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 2455 | if (unlikely(nid >= nm_i->max_nid)) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2456 | nid = 0; |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2457 | |
Chao Yu | a6d494b | 2015-07-24 18:26:26 +0800 | [diff] [blame] | 2458 | if (++i >= FREE_NID_PAGES) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2459 | break; |
| 2460 | } |
| 2461 | |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2462 | /* go to the next free nat pages to find free nids abundantly */ |
| 2463 | nm_i->next_scan_nid = nid; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2464 | |
| 2465 | /* find free nids from current sum_pages */ |
Chao Yu | 2fbaa25 | 2017-11-08 17:47:36 +0800 | [diff] [blame] | 2466 | scan_curseg_cache(sbi); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2467 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 2468 | up_read(&nm_i->nat_tree_lock); |
Chao Yu | 2db2388 | 2015-10-12 17:07:33 +0800 | [diff] [blame] | 2469 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2470 | f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid), |
Chao Yu | ea1a29a0 | 2015-10-12 17:08:48 +0800 | [diff] [blame] | 2471 | nm_i->ra_nid_pages, META_NAT, false); |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2472 | |
| 2473 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2474 | } |
| 2475 | |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2476 | int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount) |
Chao Yu | 2411cf5 | 2016-10-11 22:31:34 +0800 | [diff] [blame] | 2477 | { |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2478 | int ret; |
| 2479 | |
Chao Yu | 2411cf5 | 2016-10-11 22:31:34 +0800 | [diff] [blame] | 2480 | mutex_lock(&NM_I(sbi)->build_lock); |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2481 | ret = __f2fs_build_free_nids(sbi, sync, mount); |
Chao Yu | 2411cf5 | 2016-10-11 22:31:34 +0800 | [diff] [blame] | 2482 | mutex_unlock(&NM_I(sbi)->build_lock); |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 2483 | |
| 2484 | return ret; |
Chao Yu | 2411cf5 | 2016-10-11 22:31:34 +0800 | [diff] [blame] | 2485 | } |
| 2486 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2487 | /* |
| 2488 | * If this function returns success, caller can obtain a new nid |
| 2489 | * from second parameter of this function. |
| 2490 | * The returned nid could be used ino as well as nid when inode is created. |
| 2491 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2492 | bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2493 | { |
| 2494 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2495 | struct free_nid *i = NULL; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2496 | retry: |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 2497 | if (time_to_inject(sbi, FAULT_ALLOC_NID)) { |
Chao Yu | c45d600 | 2019-11-01 17:53:23 +0800 | [diff] [blame] | 2498 | f2fs_show_injection_info(sbi, FAULT_ALLOC_NID); |
Jaegeuk Kim | cb78942 | 2016-04-29 16:29:22 -0700 | [diff] [blame] | 2499 | return false; |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 2500 | } |
Arnd Bergmann | 7fa750a | 2018-08-13 23:38:06 +0200 | [diff] [blame] | 2501 | |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2502 | spin_lock(&nm_i->nid_list_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2503 | |
Chao Yu | 04d47e6 | 2016-11-17 20:53:11 +0800 | [diff] [blame] | 2504 | if (unlikely(nm_i->available_nids == 0)) { |
| 2505 | spin_unlock(&nm_i->nid_list_lock); |
| 2506 | return false; |
| 2507 | } |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2508 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2509 | /* We should not use stale free nids created by f2fs_build_free_nids */ |
| 2510 | if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) { |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2511 | f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list)); |
| 2512 | i = list_first_entry(&nm_i->free_nid_list, |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2513 | struct free_nid, list); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2514 | *nid = i->nid; |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2515 | |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2516 | __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID); |
Chao Yu | 04d47e6 | 2016-11-17 20:53:11 +0800 | [diff] [blame] | 2517 | nm_i->available_nids--; |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2518 | |
Chao Yu | 346fe75 | 2017-03-13 20:10:41 +0800 | [diff] [blame] | 2519 | update_free_nid_bitmap(sbi, *nid, false, false); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2520 | |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2521 | spin_unlock(&nm_i->nid_list_lock); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2522 | return true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2523 | } |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2524 | spin_unlock(&nm_i->nid_list_lock); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 2525 | |
| 2526 | /* Let's scan nat pages and its caches to get free nids */ |
Jaegeuk Kim | f84262b | 2018-09-19 15:45:19 -0700 | [diff] [blame] | 2527 | if (!f2fs_build_free_nids(sbi, true, false)) |
| 2528 | goto retry; |
| 2529 | return false; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2530 | } |
| 2531 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2532 | /* |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2533 | * f2fs_alloc_nid() should be called prior to this function. |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2534 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2535 | void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2536 | { |
| 2537 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2538 | struct free_nid *i; |
| 2539 | |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2540 | spin_lock(&nm_i->nid_list_lock); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 2541 | i = __lookup_free_nid_list(nm_i, nid); |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2542 | f2fs_bug_on(sbi, !i); |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2543 | __remove_free_nid(sbi, i, PREALLOC_NID); |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2544 | spin_unlock(&nm_i->nid_list_lock); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 2545 | |
| 2546 | kmem_cache_free(free_nid_slab, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2547 | } |
| 2548 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2549 | /* |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2550 | * f2fs_alloc_nid() should be called prior to this function. |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2551 | */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2552 | void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2553 | { |
Jaegeuk Kim | 49952fa | 2013-04-03 22:19:03 +0900 | [diff] [blame] | 2554 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2555 | struct free_nid *i; |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 2556 | bool need_free = false; |
Jaegeuk Kim | 49952fa | 2013-04-03 22:19:03 +0900 | [diff] [blame] | 2557 | |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 2558 | if (!nid) |
| 2559 | return; |
| 2560 | |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2561 | spin_lock(&nm_i->nid_list_lock); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 2562 | i = __lookup_free_nid_list(nm_i, nid); |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2563 | f2fs_bug_on(sbi, !i); |
| 2564 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2565 | if (!f2fs_available_free_memory(sbi, FREE_NIDS)) { |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2566 | __remove_free_nid(sbi, i, PREALLOC_NID); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 2567 | need_free = true; |
Haicheng Li | 95630cb | 2013-05-06 23:15:41 +0800 | [diff] [blame] | 2568 | } else { |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 2569 | __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID); |
Haicheng Li | 95630cb | 2013-05-06 23:15:41 +0800 | [diff] [blame] | 2570 | } |
Chao Yu | 04d47e6 | 2016-11-17 20:53:11 +0800 | [diff] [blame] | 2571 | |
| 2572 | nm_i->available_nids++; |
| 2573 | |
Chao Yu | 346fe75 | 2017-03-13 20:10:41 +0800 | [diff] [blame] | 2574 | update_free_nid_bitmap(sbi, nid, true, false); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2575 | |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2576 | spin_unlock(&nm_i->nid_list_lock); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 2577 | |
| 2578 | if (need_free) |
| 2579 | kmem_cache_free(free_nid_slab, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2580 | } |
| 2581 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2582 | int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink) |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 2583 | { |
| 2584 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 2585 | int nr = nr_shrink; |
| 2586 | |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 2587 | if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS) |
Jaegeuk Kim | ad4edb8 | 2016-06-16 16:41:49 -0700 | [diff] [blame] | 2588 | return 0; |
| 2589 | |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 2590 | if (!mutex_trylock(&nm_i->build_lock)) |
| 2591 | return 0; |
| 2592 | |
Chao Yu | 042be37 | 2020-05-08 17:50:20 +0800 | [diff] [blame] | 2593 | while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) { |
| 2594 | struct free_nid *i, *next; |
| 2595 | unsigned int batch = SHRINK_NID_BATCH_SIZE; |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2596 | |
Chao Yu | 042be37 | 2020-05-08 17:50:20 +0800 | [diff] [blame] | 2597 | spin_lock(&nm_i->nid_list_lock); |
| 2598 | list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) { |
| 2599 | if (!nr_shrink || !batch || |
| 2600 | nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS) |
| 2601 | break; |
| 2602 | __remove_free_nid(sbi, i, FREE_NID); |
| 2603 | kmem_cache_free(free_nid_slab, i); |
| 2604 | nr_shrink--; |
| 2605 | batch--; |
| 2606 | } |
| 2607 | spin_unlock(&nm_i->nid_list_lock); |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 2608 | } |
Chao Yu | 042be37 | 2020-05-08 17:50:20 +0800 | [diff] [blame] | 2609 | |
Chao Yu | 3169658 | 2015-07-28 18:33:46 +0800 | [diff] [blame] | 2610 | mutex_unlock(&nm_i->build_lock); |
| 2611 | |
| 2612 | return nr - nr_shrink; |
| 2613 | } |
| 2614 | |
Chao Yu | 9627a7b | 2020-07-06 18:23:36 +0800 | [diff] [blame] | 2615 | int f2fs_recover_inline_xattr(struct inode *inode, struct page *page) |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 2616 | { |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 2617 | void *src_addr, *dst_addr; |
| 2618 | size_t inline_size; |
| 2619 | struct page *ipage; |
| 2620 | struct f2fs_inode *ri; |
| 2621 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2622 | ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Chao Yu | 9627a7b | 2020-07-06 18:23:36 +0800 | [diff] [blame] | 2623 | if (IS_ERR(ipage)) |
| 2624 | return PTR_ERR(ipage); |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 2625 | |
Jaegeuk Kim | e3b4d43 | 2014-08-07 23:45:42 -0700 | [diff] [blame] | 2626 | ri = F2FS_INODE(page); |
Yunlei He | 1eca05a | 2018-01-03 18:03:04 +0800 | [diff] [blame] | 2627 | if (ri->i_inline & F2FS_INLINE_XATTR) { |
Chao Yu | b91c682 | 2020-12-07 17:54:41 +0800 | [diff] [blame] | 2628 | if (!f2fs_has_inline_xattr(inode)) { |
| 2629 | set_inode_flag(inode, FI_INLINE_XATTR); |
| 2630 | stat_inc_inline_xattr(inode); |
| 2631 | } |
Yunlei He | 1eca05a | 2018-01-03 18:03:04 +0800 | [diff] [blame] | 2632 | } else { |
Chao Yu | b91c682 | 2020-12-07 17:54:41 +0800 | [diff] [blame] | 2633 | if (f2fs_has_inline_xattr(inode)) { |
| 2634 | stat_dec_inline_xattr(inode); |
| 2635 | clear_inode_flag(inode, FI_INLINE_XATTR); |
| 2636 | } |
Jaegeuk Kim | e3b4d43 | 2014-08-07 23:45:42 -0700 | [diff] [blame] | 2637 | goto update_inode; |
| 2638 | } |
| 2639 | |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 2640 | dst_addr = inline_xattr_addr(inode, ipage); |
| 2641 | src_addr = inline_xattr_addr(inode, page); |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 2642 | inline_size = inline_xattr_size(inode); |
| 2643 | |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 2644 | f2fs_wait_on_page_writeback(ipage, NODE, true, true); |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 2645 | memcpy(dst_addr, src_addr, inline_size); |
Jaegeuk Kim | e3b4d43 | 2014-08-07 23:45:42 -0700 | [diff] [blame] | 2646 | update_inode: |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2647 | f2fs_update_inode(inode, ipage); |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 2648 | f2fs_put_page(ipage, 1); |
Chao Yu | 9627a7b | 2020-07-06 18:23:36 +0800 | [diff] [blame] | 2649 | return 0; |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 2650 | } |
| 2651 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2652 | int f2fs_recover_xattr_data(struct inode *inode, struct page *page) |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2653 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 2654 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2655 | nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid; |
Yunlei He | 8790568 | 2017-07-18 09:48:12 +0800 | [diff] [blame] | 2656 | nid_t new_xnid; |
| 2657 | struct dnode_of_data dn; |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2658 | struct node_info ni; |
Chao Yu | d260081 | 2017-02-08 17:39:45 +0800 | [diff] [blame] | 2659 | struct page *xpage; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 2660 | int err; |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2661 | |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2662 | if (!prev_xnid) |
| 2663 | goto recover_xnid; |
| 2664 | |
Chao Yu | d260081 | 2017-02-08 17:39:45 +0800 | [diff] [blame] | 2665 | /* 1: invalidate the previous xattr nid */ |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 2666 | err = f2fs_get_node_info(sbi, prev_xnid, &ni); |
| 2667 | if (err) |
| 2668 | return err; |
| 2669 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2670 | f2fs_invalidate_blocks(sbi, ni.blk_addr); |
Chao Yu | 000519f | 2017-07-06 01:11:31 +0800 | [diff] [blame] | 2671 | dec_valid_node_count(sbi, inode, false); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 2672 | set_node_addr(sbi, &ni, NULL_ADDR, false); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2673 | |
| 2674 | recover_xnid: |
Chao Yu | d260081 | 2017-02-08 17:39:45 +0800 | [diff] [blame] | 2675 | /* 2: update xattr nid in inode */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2676 | if (!f2fs_alloc_nid(sbi, &new_xnid)) |
Yunlei He | 8790568 | 2017-07-18 09:48:12 +0800 | [diff] [blame] | 2677 | return -ENOSPC; |
| 2678 | |
| 2679 | set_new_dnode(&dn, inode, NULL, NULL, new_xnid); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2680 | xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET); |
Yunlei He | 8790568 | 2017-07-18 09:48:12 +0800 | [diff] [blame] | 2681 | if (IS_ERR(xpage)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2682 | f2fs_alloc_nid_failed(sbi, new_xnid); |
Yunlei He | 8790568 | 2017-07-18 09:48:12 +0800 | [diff] [blame] | 2683 | return PTR_ERR(xpage); |
| 2684 | } |
| 2685 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2686 | f2fs_alloc_nid_done(sbi, new_xnid); |
| 2687 | f2fs_update_inode_page(inode); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2688 | |
Chao Yu | d260081 | 2017-02-08 17:39:45 +0800 | [diff] [blame] | 2689 | /* 3: update and set xattr node page dirty */ |
Yunlei He | 8790568 | 2017-07-18 09:48:12 +0800 | [diff] [blame] | 2690 | memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE); |
Chao Yu | d260081 | 2017-02-08 17:39:45 +0800 | [diff] [blame] | 2691 | |
Chao Yu | d260081 | 2017-02-08 17:39:45 +0800 | [diff] [blame] | 2692 | set_page_dirty(xpage); |
| 2693 | f2fs_put_page(xpage, 1); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2694 | |
Chao Yu | d260081 | 2017-02-08 17:39:45 +0800 | [diff] [blame] | 2695 | return 0; |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 2696 | } |
| 2697 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2698 | int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2699 | { |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 2700 | struct f2fs_inode *src, *dst; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2701 | nid_t ino = ino_of_node(page); |
| 2702 | struct node_info old_ni, new_ni; |
| 2703 | struct page *ipage; |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 2704 | int err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2705 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 2706 | err = f2fs_get_node_info(sbi, ino, &old_ni); |
| 2707 | if (err) |
| 2708 | return err; |
Jaegeuk Kim | e8271fa | 2014-04-18 15:21:04 +0900 | [diff] [blame] | 2709 | |
| 2710 | if (unlikely(old_ni.blk_addr != NULL_ADDR)) |
| 2711 | return -EINVAL; |
Jaegeuk Kim | e8ea9b3 | 2016-09-09 16:59:39 -0700 | [diff] [blame] | 2712 | retry: |
Jaegeuk Kim | 300e129 | 2016-04-29 16:11:53 -0700 | [diff] [blame] | 2713 | ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false); |
Jaegeuk Kim | e8ea9b3 | 2016-09-09 16:59:39 -0700 | [diff] [blame] | 2714 | if (!ipage) { |
Chao Yu | 5df7731f | 2020-02-17 17:45:44 +0800 | [diff] [blame] | 2715 | congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT); |
Jaegeuk Kim | e8ea9b3 | 2016-09-09 16:59:39 -0700 | [diff] [blame] | 2716 | goto retry; |
| 2717 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2718 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 2719 | /* Should not use this inode from free nid list */ |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 2720 | remove_free_nid(sbi, ino); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2721 | |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 2722 | if (!PageUptodate(ipage)) |
| 2723 | SetPageUptodate(ipage); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2724 | fill_node_footer(ipage, ino, ino, 0, true); |
Chao Yu | ef2a007 | 2018-10-03 22:32:44 +0800 | [diff] [blame] | 2725 | set_cold_node(ipage, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2726 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 2727 | src = F2FS_INODE(page); |
| 2728 | dst = F2FS_INODE(ipage); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2729 | |
Zheng Yongjun | 14e6dc5 | 2020-12-22 21:34:15 +0800 | [diff] [blame] | 2730 | memcpy(dst, src, offsetof(struct f2fs_inode, i_ext)); |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 2731 | dst->i_size = 0; |
| 2732 | dst->i_blocks = cpu_to_le64(1); |
| 2733 | dst->i_links = cpu_to_le32(1); |
| 2734 | dst->i_xattr_nid = 0; |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 2735 | dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR); |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 2736 | if (dst->i_inline & F2FS_EXTRA_ATTR) { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 2737 | dst->i_extra_isize = src->i_extra_isize; |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 2738 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 2739 | if (f2fs_sb_has_flexible_inline_xattr(sbi) && |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 2740 | F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize), |
| 2741 | i_inline_xattr_size)) |
| 2742 | dst->i_inline_xattr_size = src->i_inline_xattr_size; |
| 2743 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 2744 | if (f2fs_sb_has_project_quota(sbi) && |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 2745 | F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize), |
| 2746 | i_projid)) |
| 2747 | dst->i_projid = src->i_projid; |
Chao Yu | 5cd1f38 | 2018-09-25 15:36:01 +0800 | [diff] [blame] | 2748 | |
Chao Yu | 7beb01f | 2018-10-24 18:34:26 +0800 | [diff] [blame] | 2749 | if (f2fs_sb_has_inode_crtime(sbi) && |
Chao Yu | 5cd1f38 | 2018-09-25 15:36:01 +0800 | [diff] [blame] | 2750 | F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize), |
| 2751 | i_crtime_nsec)) { |
| 2752 | dst->i_crtime = src->i_crtime; |
| 2753 | dst->i_crtime_nsec = src->i_crtime_nsec; |
| 2754 | } |
Chao Yu | 5c57132 | 2017-07-26 00:01:41 +0800 | [diff] [blame] | 2755 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2756 | |
| 2757 | new_ni = old_ni; |
| 2758 | new_ni.ino = ino; |
| 2759 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 2760 | if (unlikely(inc_valid_node_count(sbi, NULL, true))) |
Jaegeuk Kim | 65e5cd0 | 2013-05-14 15:47:43 +0900 | [diff] [blame] | 2761 | WARN_ON(1); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 2762 | set_node_addr(sbi, &new_ni, NEW_ADDR, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2763 | inc_valid_inode_count(sbi); |
Jaegeuk Kim | 617deb8 | 2014-08-07 17:04:24 -0700 | [diff] [blame] | 2764 | set_page_dirty(ipage); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2765 | f2fs_put_page(ipage, 1); |
| 2766 | return 0; |
| 2767 | } |
| 2768 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 2769 | int f2fs_restore_node_summary(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2770 | unsigned int segno, struct f2fs_summary_block *sum) |
| 2771 | { |
| 2772 | struct f2fs_node *rn; |
| 2773 | struct f2fs_summary *sum_entry; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2774 | block_t addr; |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 2775 | int i, idx, last_offset, nrpages; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2776 | |
| 2777 | /* scan the node segment */ |
| 2778 | last_offset = sbi->blocks_per_seg; |
| 2779 | addr = START_BLOCK(sbi, segno); |
| 2780 | sum_entry = &sum->entries[0]; |
| 2781 | |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 2782 | for (i = 0; i < last_offset; i += nrpages, addr += nrpages) { |
Jaegeuk Kim | 664ba97 | 2016-10-18 11:07:45 -0700 | [diff] [blame] | 2783 | nrpages = min(last_offset - i, BIO_MAX_PAGES); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 2784 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 2785 | /* readahead node pages */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2786 | f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 2787 | |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 2788 | for (idx = addr; idx < addr + nrpages; idx++) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2789 | struct page *page = f2fs_get_tmp_page(sbi, idx); |
Chao Yu | 9af0ff1 | 2013-11-22 15:48:54 +0800 | [diff] [blame] | 2790 | |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 2791 | if (IS_ERR(page)) |
| 2792 | return PTR_ERR(page); |
| 2793 | |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 2794 | rn = F2FS_NODE(page); |
| 2795 | sum_entry->nid = rn->footer.nid; |
| 2796 | sum_entry->version = 0; |
| 2797 | sum_entry->ofs_in_node = 0; |
| 2798 | sum_entry++; |
| 2799 | f2fs_put_page(page, 1); |
Chao Yu | 9af0ff1 | 2013-11-22 15:48:54 +0800 | [diff] [blame] | 2800 | } |
Chao Yu | bac4eef | 2014-05-27 08:41:07 +0800 | [diff] [blame] | 2801 | |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 2802 | invalidate_mapping_pages(META_MAPPING(sbi), addr, |
Chao Yu | bac4eef | 2014-05-27 08:41:07 +0800 | [diff] [blame] | 2803 | addr + nrpages); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2804 | } |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 2805 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2806 | } |
| 2807 | |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 2808 | static void remove_nats_in_journal(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2809 | { |
| 2810 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2811 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2812 | struct f2fs_journal *journal = curseg->journal; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2813 | int i; |
| 2814 | |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2815 | down_write(&curseg->journal_rwsem); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2816 | for (i = 0; i < nats_in_cursum(journal); i++) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2817 | struct nat_entry *ne; |
| 2818 | struct f2fs_nat_entry raw_ne; |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2819 | nid_t nid = le32_to_cpu(nid_in_journal(journal, i)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2820 | |
Chao Yu | 0d36b38 | 2021-03-22 19:47:30 +0800 | [diff] [blame] | 2821 | if (f2fs_check_nid_range(sbi, nid)) |
| 2822 | continue; |
| 2823 | |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2824 | raw_ne = nat_in_journal(journal, i); |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 2825 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2826 | ne = __lookup_nat_cache(nm_i, nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2827 | if (!ne) { |
Yunlei He | 12f9ef3 | 2017-11-10 13:36:51 -0800 | [diff] [blame] | 2828 | ne = __alloc_nat_entry(nid, true); |
| 2829 | __init_nat_entry(nm_i, ne, &raw_ne, true); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2830 | } |
Chao Yu | 04d47e6 | 2016-11-17 20:53:11 +0800 | [diff] [blame] | 2831 | |
| 2832 | /* |
| 2833 | * if a free nat in journal has not been used after last |
| 2834 | * checkpoint, we should remove it from available nids, |
| 2835 | * since later we will add it again. |
| 2836 | */ |
| 2837 | if (!get_nat_flag(ne, IS_DIRTY) && |
| 2838 | le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) { |
| 2839 | spin_lock(&nm_i->nid_list_lock); |
| 2840 | nm_i->available_nids--; |
| 2841 | spin_unlock(&nm_i->nid_list_lock); |
| 2842 | } |
| 2843 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2844 | __set_nat_cache_dirty(nm_i, ne); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2845 | } |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2846 | update_nats_in_cursum(journal, -i); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2847 | up_write(&curseg->journal_rwsem); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2848 | } |
| 2849 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2850 | static void __adjust_nat_entry_set(struct nat_entry_set *nes, |
| 2851 | struct list_head *head, int max) |
| 2852 | { |
| 2853 | struct nat_entry_set *cur; |
| 2854 | |
| 2855 | if (nes->entry_cnt >= max) |
| 2856 | goto add_out; |
| 2857 | |
| 2858 | list_for_each_entry(cur, head, set_list) { |
| 2859 | if (cur->entry_cnt >= nes->entry_cnt) { |
| 2860 | list_add(&nes->set_list, cur->set_list.prev); |
| 2861 | return; |
| 2862 | } |
| 2863 | } |
| 2864 | add_out: |
| 2865 | list_add_tail(&nes->set_list, head); |
| 2866 | } |
| 2867 | |
Jaegeuk Kim | 9f7e4a2 | 2017-03-10 09:39:57 -0800 | [diff] [blame] | 2868 | static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid, |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2869 | struct page *page) |
| 2870 | { |
| 2871 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2872 | unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK; |
| 2873 | struct f2fs_nat_block *nat_blk = page_address(page); |
| 2874 | int valid = 0; |
Fan Li | 37a0ab2 | 2017-10-30 15:19:48 +0800 | [diff] [blame] | 2875 | int i = 0; |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2876 | |
| 2877 | if (!enabled_nat_bits(sbi, NULL)) |
| 2878 | return; |
| 2879 | |
Fan Li | 37a0ab2 | 2017-10-30 15:19:48 +0800 | [diff] [blame] | 2880 | if (nat_index == 0) { |
| 2881 | valid = 1; |
| 2882 | i = 1; |
| 2883 | } |
| 2884 | for (; i < NAT_ENTRY_PER_BLOCK; i++) { |
Chao Yu | 36af5f4 | 2019-05-28 17:23:33 +0800 | [diff] [blame] | 2885 | if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR) |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2886 | valid++; |
| 2887 | } |
| 2888 | if (valid == 0) { |
Jaegeuk Kim | 23380b8 | 2017-03-07 14:11:06 -0800 | [diff] [blame] | 2889 | __set_bit_le(nat_index, nm_i->empty_nat_bits); |
| 2890 | __clear_bit_le(nat_index, nm_i->full_nat_bits); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2891 | return; |
| 2892 | } |
| 2893 | |
Jaegeuk Kim | 23380b8 | 2017-03-07 14:11:06 -0800 | [diff] [blame] | 2894 | __clear_bit_le(nat_index, nm_i->empty_nat_bits); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2895 | if (valid == NAT_ENTRY_PER_BLOCK) |
Jaegeuk Kim | 23380b8 | 2017-03-07 14:11:06 -0800 | [diff] [blame] | 2896 | __set_bit_le(nat_index, nm_i->full_nat_bits); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2897 | else |
Jaegeuk Kim | 23380b8 | 2017-03-07 14:11:06 -0800 | [diff] [blame] | 2898 | __clear_bit_le(nat_index, nm_i->full_nat_bits); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2899 | } |
| 2900 | |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 2901 | static int __flush_nat_entry_set(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2902 | struct nat_entry_set *set, struct cp_control *cpc) |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2903 | { |
| 2904 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2905 | struct f2fs_journal *journal = curseg->journal; |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2906 | nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK; |
| 2907 | bool to_journal = true; |
| 2908 | struct f2fs_nat_block *nat_blk; |
| 2909 | struct nat_entry *ne, *cur; |
| 2910 | struct page *page = NULL; |
| 2911 | |
| 2912 | /* |
| 2913 | * there are two steps to flush nat entries: |
| 2914 | * #1, flush nat entries to journal in current hot data summary block. |
| 2915 | * #2, flush nat entries to nat page. |
| 2916 | */ |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2917 | if (enabled_nat_bits(sbi, cpc) || |
| 2918 | !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL)) |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2919 | to_journal = false; |
| 2920 | |
| 2921 | if (to_journal) { |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2922 | down_write(&curseg->journal_rwsem); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2923 | } else { |
| 2924 | page = get_next_nat_page(sbi, start_nid); |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 2925 | if (IS_ERR(page)) |
| 2926 | return PTR_ERR(page); |
| 2927 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2928 | nat_blk = page_address(page); |
| 2929 | f2fs_bug_on(sbi, !nat_blk); |
| 2930 | } |
| 2931 | |
| 2932 | /* flush dirty nats in nat entry set */ |
| 2933 | list_for_each_entry_safe(ne, cur, &set->entry_list, list) { |
| 2934 | struct f2fs_nat_entry *raw_ne; |
| 2935 | nid_t nid = nat_get_nid(ne); |
| 2936 | int offset; |
| 2937 | |
Chao Yu | febeca6 | 2017-06-05 18:29:08 +0800 | [diff] [blame] | 2938 | f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2939 | |
| 2940 | if (to_journal) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2941 | offset = f2fs_lookup_journal_in_cursum(journal, |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2942 | NAT_JOURNAL, nid, 1); |
| 2943 | f2fs_bug_on(sbi, offset < 0); |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 2944 | raw_ne = &nat_in_journal(journal, offset); |
| 2945 | nid_in_journal(journal, offset) = cpu_to_le32(nid); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2946 | } else { |
| 2947 | raw_ne = &nat_blk->entries[nid - start_nid]; |
| 2948 | } |
| 2949 | raw_nat_from_node_info(raw_ne, &ne->ni); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2950 | nat_reset_flag(ne); |
Kinglong Mee | 0b28b71 | 2017-02-28 21:34:47 +0800 | [diff] [blame] | 2951 | __clear_nat_cache_dirty(NM_I(sbi), set, ne); |
Chao Yu | 04d47e6 | 2016-11-17 20:53:11 +0800 | [diff] [blame] | 2952 | if (nat_get_blkaddr(ne) == NULL_ADDR) { |
LiFan | 5921aaa | 2017-11-22 16:07:23 +0800 | [diff] [blame] | 2953 | add_free_nid(sbi, nid, false, true); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 2954 | } else { |
| 2955 | spin_lock(&NM_I(sbi)->nid_list_lock); |
Chao Yu | 346fe75 | 2017-03-13 20:10:41 +0800 | [diff] [blame] | 2956 | update_free_nid_bitmap(sbi, nid, false, false); |
Chao Yu | 04d47e6 | 2016-11-17 20:53:11 +0800 | [diff] [blame] | 2957 | spin_unlock(&NM_I(sbi)->nid_list_lock); |
| 2958 | } |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2961 | if (to_journal) { |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2962 | up_write(&curseg->journal_rwsem); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2963 | } else { |
| 2964 | __update_nat_bits(sbi, start_nid, page); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2965 | f2fs_put_page(page, 1); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 2966 | } |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2967 | |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 2968 | /* Allow dirty nats by node block allocation in write_begin */ |
| 2969 | if (!set->entry_cnt) { |
| 2970 | radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set); |
| 2971 | kmem_cache_free(nat_entry_set_slab, set); |
| 2972 | } |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 2973 | return 0; |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2976 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2977 | * This function is called during the checkpointing process. |
| 2978 | */ |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 2979 | int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2980 | { |
| 2981 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 2982 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
Chao Yu | b7ad751 | 2016-02-19 18:08:46 +0800 | [diff] [blame] | 2983 | struct f2fs_journal *journal = curseg->journal; |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 2984 | struct nat_entry_set *setvec[SETVEC_SIZE]; |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 2985 | struct nat_entry_set *set, *tmp; |
| 2986 | unsigned int found; |
| 2987 | nid_t set_idx = 0; |
| 2988 | LIST_HEAD(sets); |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 2989 | int err = 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2990 | |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 2991 | /* |
| 2992 | * during unmount, let's flush nat_bits before checking |
| 2993 | * nat_cnt[DIRTY_NAT]. |
| 2994 | */ |
Jaegeuk Kim | 7f2ecdd | 2018-06-28 19:34:40 -0700 | [diff] [blame] | 2995 | if (enabled_nat_bits(sbi, cpc)) { |
| 2996 | down_write(&nm_i->nat_tree_lock); |
| 2997 | remove_nats_in_journal(sbi); |
| 2998 | up_write(&nm_i->nat_tree_lock); |
| 2999 | } |
| 3000 | |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 3001 | if (!nm_i->nat_cnt[DIRTY_NAT]) |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 3002 | return 0; |
Jaegeuk Kim | a513119 | 2016-01-02 09:19:41 -0800 | [diff] [blame] | 3003 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 3004 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | a513119 | 2016-01-02 09:19:41 -0800 | [diff] [blame] | 3005 | |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3006 | /* |
| 3007 | * if there are no enough space in journal to store dirty nat |
| 3008 | * entries, remove all entries from journal and merge them |
| 3009 | * into nat entry set. |
| 3010 | */ |
Jaegeuk Kim | 900f736 | 2017-02-27 21:28:53 -0800 | [diff] [blame] | 3011 | if (enabled_nat_bits(sbi, cpc) || |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 3012 | !__has_cursum_space(journal, |
| 3013 | nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL)) |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3014 | remove_nats_in_journal(sbi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3015 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 3016 | while ((found = __gang_lookup_nat_set(nm_i, |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 3017 | set_idx, SETVEC_SIZE, setvec))) { |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 3018 | unsigned idx; |
Yi Zhuang | f10ea3c | 2021-04-06 09:47:35 +0800 | [diff] [blame] | 3019 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 3020 | set_idx = setvec[found - 1]->set + 1; |
| 3021 | for (idx = 0; idx < found; idx++) |
| 3022 | __adjust_nat_entry_set(setvec[idx], &sets, |
Chao Yu | dfc08a1 | 2016-02-14 18:50:40 +0800 | [diff] [blame] | 3023 | MAX_NAT_JENTRIES(journal)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3024 | } |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3025 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 3026 | /* flush dirty nats in nat entry set */ |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 3027 | list_for_each_entry_safe(set, tmp, &sets, set_list) { |
| 3028 | err = __flush_nat_entry_set(sbi, set, cpc); |
| 3029 | if (err) |
| 3030 | break; |
| 3031 | } |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 3032 | |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 3033 | up_write(&nm_i->nat_tree_lock); |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 3034 | /* Allow dirty nats by node block allocation in write_begin */ |
Jaegeuk Kim | edc55aa | 2018-09-17 17:36:06 -0700 | [diff] [blame] | 3035 | |
| 3036 | return err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3037 | } |
| 3038 | |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3039 | static int __get_nat_bitmaps(struct f2fs_sb_info *sbi) |
| 3040 | { |
| 3041 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); |
| 3042 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 3043 | unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE; |
| 3044 | unsigned int i; |
| 3045 | __u64 cp_ver = cur_cp_version(ckpt); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3046 | block_t nat_bits_addr; |
| 3047 | |
| 3048 | if (!enabled_nat_bits(sbi, NULL)) |
| 3049 | return 0; |
| 3050 | |
Chao Yu | df033ca | 2018-03-20 23:08:29 +0800 | [diff] [blame] | 3051 | nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8); |
Eric Biggers | 0b6d4ca | 2020-06-04 21:57:48 -0700 | [diff] [blame] | 3052 | nm_i->nat_bits = f2fs_kvzalloc(sbi, |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3053 | nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3054 | if (!nm_i->nat_bits) |
| 3055 | return -ENOMEM; |
| 3056 | |
| 3057 | nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg - |
| 3058 | nm_i->nat_bits_blocks; |
| 3059 | for (i = 0; i < nm_i->nat_bits_blocks; i++) { |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3060 | struct page *page; |
| 3061 | |
| 3062 | page = f2fs_get_meta_page(sbi, nat_bits_addr++); |
Chao Yu | 3b30eb1 | 2018-10-04 11:15:20 +0800 | [diff] [blame] | 3063 | if (IS_ERR(page)) |
Chao Yu | 7735730 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 3064 | return PTR_ERR(page); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3065 | |
| 3066 | memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS), |
| 3067 | page_address(page), F2FS_BLKSIZE); |
| 3068 | f2fs_put_page(page, 1); |
| 3069 | } |
| 3070 | |
Kinglong Mee | ced2c7e | 2017-02-25 19:53:39 +0800 | [diff] [blame] | 3071 | cp_ver |= (cur_cp_crc(ckpt) << 32); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3072 | if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) { |
| 3073 | disable_nat_bits(sbi, true); |
| 3074 | return 0; |
| 3075 | } |
| 3076 | |
| 3077 | nm_i->full_nat_bits = nm_i->nat_bits + 8; |
| 3078 | nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes; |
| 3079 | |
Joe Perches | dcbb4c1 | 2019-06-18 17:48:42 +0800 | [diff] [blame] | 3080 | f2fs_notice(sbi, "Found nat_bits in checkpoint"); |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3081 | return 0; |
| 3082 | } |
| 3083 | |
Hou Pengyang | bd80a4b | 2017-05-17 02:48:48 +0000 | [diff] [blame] | 3084 | static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi) |
Chao Yu | 7041d5d | 2017-03-08 20:07:49 +0800 | [diff] [blame] | 3085 | { |
| 3086 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 3087 | unsigned int i = 0; |
| 3088 | nid_t nid, last_nid; |
| 3089 | |
| 3090 | if (!enabled_nat_bits(sbi, NULL)) |
| 3091 | return; |
| 3092 | |
| 3093 | for (i = 0; i < nm_i->nat_blocks; i++) { |
| 3094 | i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i); |
| 3095 | if (i >= nm_i->nat_blocks) |
| 3096 | break; |
| 3097 | |
| 3098 | __set_bit_le(i, nm_i->nat_block_bitmap); |
| 3099 | |
| 3100 | nid = i * NAT_ENTRY_PER_BLOCK; |
Fan Li | f6986ed | 2017-11-02 11:02:52 +0800 | [diff] [blame] | 3101 | last_nid = nid + NAT_ENTRY_PER_BLOCK; |
Chao Yu | 7041d5d | 2017-03-08 20:07:49 +0800 | [diff] [blame] | 3102 | |
Chao Yu | 346fe75 | 2017-03-13 20:10:41 +0800 | [diff] [blame] | 3103 | spin_lock(&NM_I(sbi)->nid_list_lock); |
Chao Yu | 7041d5d | 2017-03-08 20:07:49 +0800 | [diff] [blame] | 3104 | for (; nid < last_nid; nid++) |
Chao Yu | 346fe75 | 2017-03-13 20:10:41 +0800 | [diff] [blame] | 3105 | update_free_nid_bitmap(sbi, nid, true, true); |
| 3106 | spin_unlock(&NM_I(sbi)->nid_list_lock); |
Chao Yu | 7041d5d | 2017-03-08 20:07:49 +0800 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | for (i = 0; i < nm_i->nat_blocks; i++) { |
| 3110 | i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i); |
| 3111 | if (i >= nm_i->nat_blocks) |
| 3112 | break; |
| 3113 | |
| 3114 | __set_bit_le(i, nm_i->nat_block_bitmap); |
| 3115 | } |
| 3116 | } |
| 3117 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3118 | static int init_node_manager(struct f2fs_sb_info *sbi) |
| 3119 | { |
| 3120 | struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi); |
| 3121 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 3122 | unsigned char *version_bitmap; |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3123 | unsigned int nat_segs; |
| 3124 | int err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3125 | |
| 3126 | nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr); |
| 3127 | |
| 3128 | /* segment_count_nat includes pair segment so divide to 2. */ |
| 3129 | nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1; |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3130 | nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg); |
| 3131 | nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks; |
Jaegeuk Kim | 7ee0eea | 2014-04-18 11:14:37 +0900 | [diff] [blame] | 3132 | |
Jaegeuk Kim | b63da15 | 2014-02-17 12:44:20 +0900 | [diff] [blame] | 3133 | /* not used nids: 0, node, meta, (and root counted as valid node) */ |
Chao Yu | 04d47e6 | 2016-11-17 20:53:11 +0800 | [diff] [blame] | 3134 | nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count - |
Chao Yu | 27cae0b | 2019-08-05 18:27:25 +0800 | [diff] [blame] | 3135 | F2FS_RESERVED_NODE_NUM; |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 3136 | nm_i->nid_cnt[FREE_NID] = 0; |
| 3137 | nm_i->nid_cnt[PREALLOC_NID] = 0; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 3138 | nm_i->ram_thresh = DEF_RAM_THRESHOLD; |
Chao Yu | ea1a29a0 | 2015-10-12 17:08:48 +0800 | [diff] [blame] | 3139 | nm_i->ra_nid_pages = DEF_RA_NID_PAGES; |
Chao Yu | 2304cb0 | 2016-01-18 18:32:58 +0800 | [diff] [blame] | 3140 | nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3141 | |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 3142 | INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC); |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 3143 | INIT_LIST_HEAD(&nm_i->free_nid_list); |
Jaegeuk Kim | 769ec6e | 2014-12-03 20:47:26 -0800 | [diff] [blame] | 3144 | INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO); |
| 3145 | INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3146 | INIT_LIST_HEAD(&nm_i->nat_entries); |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 3147 | spin_lock_init(&nm_i->nat_list_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3148 | |
| 3149 | mutex_init(&nm_i->build_lock); |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 3150 | spin_lock_init(&nm_i->nid_list_lock); |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 3151 | init_rwsem(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3152 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3153 | nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid); |
Alexandru Gheorghiu | 79b5793 | 2013-03-28 02:24:53 +0200 | [diff] [blame] | 3154 | nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3155 | version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP); |
Alexandru Gheorghiu | 79b5793 | 2013-03-28 02:24:53 +0200 | [diff] [blame] | 3156 | nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size, |
| 3157 | GFP_KERNEL); |
| 3158 | if (!nm_i->nat_bitmap) |
| 3159 | return -ENOMEM; |
Chao Yu | 599a09b | 2017-01-07 18:52:01 +0800 | [diff] [blame] | 3160 | |
Jaegeuk Kim | 22ad0b6 | 2017-02-09 10:38:09 -0800 | [diff] [blame] | 3161 | err = __get_nat_bitmaps(sbi); |
| 3162 | if (err) |
| 3163 | return err; |
| 3164 | |
Chao Yu | 599a09b | 2017-01-07 18:52:01 +0800 | [diff] [blame] | 3165 | #ifdef CONFIG_F2FS_CHECK_FS |
| 3166 | nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size, |
| 3167 | GFP_KERNEL); |
| 3168 | if (!nm_i->nat_bitmap_mir) |
| 3169 | return -ENOMEM; |
| 3170 | #endif |
| 3171 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3172 | return 0; |
| 3173 | } |
| 3174 | |
Jaegeuk Kim | 9f7e4a2 | 2017-03-10 09:39:57 -0800 | [diff] [blame] | 3175 | static int init_free_nid_cache(struct f2fs_sb_info *sbi) |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 3176 | { |
| 3177 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Jaegeuk Kim | bb1105e | 2018-03-09 17:42:28 -0800 | [diff] [blame] | 3178 | int i; |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 3179 | |
Kees Cook | 026f050 | 2018-06-12 14:28:23 -0700 | [diff] [blame] | 3180 | nm_i->free_nid_bitmap = |
Eric Biggers | 0b6d4ca | 2020-06-04 21:57:48 -0700 | [diff] [blame] | 3181 | f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *), |
| 3182 | nm_i->nat_blocks), |
| 3183 | GFP_KERNEL); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 3184 | if (!nm_i->free_nid_bitmap) |
| 3185 | return -ENOMEM; |
| 3186 | |
Jaegeuk Kim | bb1105e | 2018-03-09 17:42:28 -0800 | [diff] [blame] | 3187 | for (i = 0; i < nm_i->nat_blocks; i++) { |
| 3188 | nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi, |
Yunlei He | e15d54d | 2018-06-27 14:46:21 +0800 | [diff] [blame] | 3189 | f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL); |
Yunlei He | 68c43a2 | 2018-07-02 10:40:19 +0800 | [diff] [blame] | 3190 | if (!nm_i->free_nid_bitmap[i]) |
Jaegeuk Kim | bb1105e | 2018-03-09 17:42:28 -0800 | [diff] [blame] | 3191 | return -ENOMEM; |
| 3192 | } |
| 3193 | |
Chao Yu | 628b3d1 | 2017-11-30 19:28:18 +0800 | [diff] [blame] | 3194 | nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8, |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 3195 | GFP_KERNEL); |
| 3196 | if (!nm_i->nat_block_bitmap) |
| 3197 | return -ENOMEM; |
Chao Yu | 586d149 | 2017-03-01 17:09:07 +0800 | [diff] [blame] | 3198 | |
Kees Cook | 9d2a789 | 2018-06-12 14:28:35 -0700 | [diff] [blame] | 3199 | nm_i->free_nid_count = |
| 3200 | f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short), |
| 3201 | nm_i->nat_blocks), |
| 3202 | GFP_KERNEL); |
Chao Yu | 586d149 | 2017-03-01 17:09:07 +0800 | [diff] [blame] | 3203 | if (!nm_i->free_nid_count) |
| 3204 | return -ENOMEM; |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 3205 | return 0; |
| 3206 | } |
| 3207 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3208 | int f2fs_build_node_manager(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3209 | { |
| 3210 | int err; |
| 3211 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 3212 | sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info), |
| 3213 | GFP_KERNEL); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3214 | if (!sbi->nm_info) |
| 3215 | return -ENOMEM; |
| 3216 | |
| 3217 | err = init_node_manager(sbi); |
| 3218 | if (err) |
| 3219 | return err; |
| 3220 | |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 3221 | err = init_free_nid_cache(sbi); |
| 3222 | if (err) |
| 3223 | return err; |
| 3224 | |
Chao Yu | 7041d5d | 2017-03-08 20:07:49 +0800 | [diff] [blame] | 3225 | /* load free nid status from nat_bits table */ |
| 3226 | load_free_nid_bitmap(sbi); |
| 3227 | |
Chao Yu | e237401 | 2018-06-15 14:45:57 +0800 | [diff] [blame] | 3228 | return f2fs_build_free_nids(sbi, true, true); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3229 | } |
| 3230 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3231 | void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3232 | { |
| 3233 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 3234 | struct free_nid *i, *next_i; |
| 3235 | struct nat_entry *natvec[NATVEC_SIZE]; |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 3236 | struct nat_entry_set *setvec[SETVEC_SIZE]; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3237 | nid_t nid = 0; |
| 3238 | unsigned int found; |
| 3239 | |
| 3240 | if (!nm_i) |
| 3241 | return; |
| 3242 | |
| 3243 | /* destroy free nid list */ |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 3244 | spin_lock(&nm_i->nid_list_lock); |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 3245 | list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) { |
Fan Li | a0761f6 | 2017-10-28 19:03:37 +0800 | [diff] [blame] | 3246 | __remove_free_nid(sbi, i, FREE_NID); |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 3247 | spin_unlock(&nm_i->nid_list_lock); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 3248 | kmem_cache_free(free_nid_slab, i); |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 3249 | spin_lock(&nm_i->nid_list_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3250 | } |
Chao Yu | 9a4ffdf | 2017-09-29 13:59:35 +0800 | [diff] [blame] | 3251 | f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]); |
| 3252 | f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]); |
| 3253 | f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list)); |
Chao Yu | b8559dc | 2016-10-12 19:28:29 +0800 | [diff] [blame] | 3254 | spin_unlock(&nm_i->nid_list_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3255 | |
| 3256 | /* destroy nat cache */ |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 3257 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3258 | while ((found = __gang_lookup_nat_cache(nm_i, |
| 3259 | nid, NATVEC_SIZE, natvec))) { |
| 3260 | unsigned idx; |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 3261 | |
Gu Zheng | b6ce391 | 2014-03-07 18:43:24 +0800 | [diff] [blame] | 3262 | nid = nat_get_nid(natvec[found - 1]) + 1; |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 3263 | for (idx = 0; idx < found; idx++) { |
| 3264 | spin_lock(&nm_i->nat_list_lock); |
| 3265 | list_del(&natvec[idx]->list); |
| 3266 | spin_unlock(&nm_i->nat_list_lock); |
| 3267 | |
Gu Zheng | b6ce391 | 2014-03-07 18:43:24 +0800 | [diff] [blame] | 3268 | __del_from_nat_cache(nm_i, natvec[idx]); |
Chao Yu | 2296915 | 2018-08-05 23:08:59 +0800 | [diff] [blame] | 3269 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3270 | } |
Jaegeuk Kim | 317c4f5 | 2020-11-06 13:22:05 -0800 | [diff] [blame] | 3271 | f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]); |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 3272 | |
| 3273 | /* destroy nat set cache */ |
| 3274 | nid = 0; |
| 3275 | while ((found = __gang_lookup_nat_set(nm_i, |
| 3276 | nid, SETVEC_SIZE, setvec))) { |
| 3277 | unsigned idx; |
| 3278 | |
| 3279 | nid = setvec[found - 1]->set + 1; |
| 3280 | for (idx = 0; idx < found; idx++) { |
| 3281 | /* entry_cnt is not zero, when cp_error was occurred */ |
| 3282 | f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list)); |
| 3283 | radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set); |
| 3284 | kmem_cache_free(nat_entry_set_slab, setvec[idx]); |
| 3285 | } |
| 3286 | } |
Jaegeuk Kim | b873b79 | 2016-08-04 11:38:25 -0700 | [diff] [blame] | 3287 | up_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3288 | |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 3289 | kvfree(nm_i->nat_block_bitmap); |
Jaegeuk Kim | bb1105e | 2018-03-09 17:42:28 -0800 | [diff] [blame] | 3290 | if (nm_i->free_nid_bitmap) { |
| 3291 | int i; |
| 3292 | |
| 3293 | for (i = 0; i < nm_i->nat_blocks; i++) |
| 3294 | kvfree(nm_i->free_nid_bitmap[i]); |
Jaegeuk Kim | 5222595 | 2018-12-13 18:38:33 -0800 | [diff] [blame] | 3295 | kvfree(nm_i->free_nid_bitmap); |
Jaegeuk Kim | bb1105e | 2018-03-09 17:42:28 -0800 | [diff] [blame] | 3296 | } |
Chao Yu | 586d149 | 2017-03-01 17:09:07 +0800 | [diff] [blame] | 3297 | kvfree(nm_i->free_nid_count); |
Chao Yu | 4ac9124 | 2017-02-23 10:53:49 +0800 | [diff] [blame] | 3298 | |
Jaegeuk Kim | 5222595 | 2018-12-13 18:38:33 -0800 | [diff] [blame] | 3299 | kvfree(nm_i->nat_bitmap); |
| 3300 | kvfree(nm_i->nat_bits); |
Chao Yu | 599a09b | 2017-01-07 18:52:01 +0800 | [diff] [blame] | 3301 | #ifdef CONFIG_F2FS_CHECK_FS |
Jaegeuk Kim | 5222595 | 2018-12-13 18:38:33 -0800 | [diff] [blame] | 3302 | kvfree(nm_i->nat_bitmap_mir); |
Chao Yu | 599a09b | 2017-01-07 18:52:01 +0800 | [diff] [blame] | 3303 | #endif |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3304 | sbi->nm_info = NULL; |
Chao Yu | c8eb702 | 2020-09-14 16:47:00 +0800 | [diff] [blame] | 3305 | kfree(nm_i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3306 | } |
| 3307 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3308 | int __init f2fs_create_node_manager_caches(void) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3309 | { |
Chao Yu | 9851000 | 2020-02-17 17:46:20 +0800 | [diff] [blame] | 3310 | nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry", |
Gu Zheng | e8512d2 | 2014-03-07 18:43:28 +0800 | [diff] [blame] | 3311 | sizeof(struct nat_entry)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3312 | if (!nat_entry_slab) |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3313 | goto fail; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3314 | |
Chao Yu | 9851000 | 2020-02-17 17:46:20 +0800 | [diff] [blame] | 3315 | free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid", |
Gu Zheng | e8512d2 | 2014-03-07 18:43:28 +0800 | [diff] [blame] | 3316 | sizeof(struct free_nid)); |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3317 | if (!free_nid_slab) |
Markus Elfring | ce3e6d2 | 2014-11-24 15:52:00 +0100 | [diff] [blame] | 3318 | goto destroy_nat_entry; |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3319 | |
Chao Yu | 9851000 | 2020-02-17 17:46:20 +0800 | [diff] [blame] | 3320 | nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set", |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3321 | sizeof(struct nat_entry_set)); |
| 3322 | if (!nat_entry_set_slab) |
Markus Elfring | ce3e6d2 | 2014-11-24 15:52:00 +0100 | [diff] [blame] | 3323 | goto destroy_free_nid; |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 3324 | |
Chao Yu | 9851000 | 2020-02-17 17:46:20 +0800 | [diff] [blame] | 3325 | fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry", |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 3326 | sizeof(struct fsync_node_entry)); |
| 3327 | if (!fsync_node_entry_slab) |
| 3328 | goto destroy_nat_entry_set; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3329 | return 0; |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3330 | |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 3331 | destroy_nat_entry_set: |
| 3332 | kmem_cache_destroy(nat_entry_set_slab); |
Markus Elfring | ce3e6d2 | 2014-11-24 15:52:00 +0100 | [diff] [blame] | 3333 | destroy_free_nid: |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3334 | kmem_cache_destroy(free_nid_slab); |
Markus Elfring | ce3e6d2 | 2014-11-24 15:52:00 +0100 | [diff] [blame] | 3335 | destroy_nat_entry: |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3336 | kmem_cache_destroy(nat_entry_slab); |
| 3337 | fail: |
| 3338 | return -ENOMEM; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3339 | } |
| 3340 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 3341 | void f2fs_destroy_node_manager_caches(void) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3342 | { |
Chao Yu | 50fa53e | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 3343 | kmem_cache_destroy(fsync_node_entry_slab); |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 3344 | kmem_cache_destroy(nat_entry_set_slab); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 3345 | kmem_cache_destroy(free_nid_slab); |
| 3346 | kmem_cache_destroy(nat_entry_slab); |
| 3347 | } |