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