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