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