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