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