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