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" |
Jaegeuk Kim | 9e4ded3 | 2014-12-17 19:58:58 -0800 | [diff] [blame] | 22 | #include "trace.h" |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 23 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 24 | |
Gu Zheng | f978f5a | 2014-02-21 18:08:29 +0800 | [diff] [blame] | 25 | #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock) |
| 26 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 27 | static struct kmem_cache *nat_entry_slab; |
| 28 | static struct kmem_cache *free_nid_slab; |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 29 | static struct kmem_cache *nat_entry_set_slab; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 30 | |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 31 | bool available_free_memory(struct f2fs_sb_info *sbi, int type) |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 32 | { |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 33 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 34 | struct sysinfo val; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 35 | unsigned long avail_ram; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 36 | unsigned long mem_size = 0; |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 37 | bool res = false; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 38 | |
| 39 | si_meminfo(&val); |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 40 | |
| 41 | /* only uses low memory */ |
| 42 | avail_ram = val.totalram - val.totalhigh; |
| 43 | |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame^] | 44 | /* |
| 45 | * give 25%, 25%, 50%, 50%, 50% memory for each components respectively |
| 46 | */ |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 47 | if (type == FREE_NIDS) { |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 48 | mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >> |
| 49 | PAGE_CACHE_SHIFT; |
| 50 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2); |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 51 | } else if (type == NAT_ENTRIES) { |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 52 | mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> |
| 53 | PAGE_CACHE_SHIFT; |
| 54 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2); |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 55 | } else if (type == DIRTY_DENTS) { |
Jaegeuk Kim | 2743f86 | 2014-06-28 01:00:41 +0900 | [diff] [blame] | 56 | if (sbi->sb->s_bdi->dirty_exceeded) |
| 57 | return false; |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 58 | mem_size = get_pages(sbi, F2FS_DIRTY_DENTS); |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 59 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); |
| 60 | } else if (type == INO_ENTRIES) { |
| 61 | int i; |
| 62 | |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 63 | for (i = 0; i <= UPDATE_INO; i++) |
Chao Yu | 6729880 | 2014-11-18 11:18:36 +0800 | [diff] [blame] | 64 | mem_size += (sbi->im[i].ino_num * |
| 65 | sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT; |
Jaegeuk Kim | e5e7ea3 | 2014-11-06 15:24:46 -0800 | [diff] [blame] | 66 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame^] | 67 | } else if (type == EXTENT_CACHE) { |
| 68 | mem_size = (sbi->total_ext_tree * sizeof(struct extent_tree) + |
| 69 | atomic_read(&sbi->total_ext_node) * |
| 70 | sizeof(struct extent_node)) >> PAGE_CACHE_SHIFT; |
| 71 | res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1); |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 72 | } else { |
| 73 | if (sbi->sb->s_bdi->dirty_exceeded) |
| 74 | return false; |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 75 | } |
| 76 | return res; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 77 | } |
| 78 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 79 | static void clear_node_page_dirty(struct page *page) |
| 80 | { |
| 81 | struct address_space *mapping = page->mapping; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 82 | unsigned int long flags; |
| 83 | |
| 84 | if (PageDirty(page)) { |
| 85 | spin_lock_irqsave(&mapping->tree_lock, flags); |
| 86 | radix_tree_tag_clear(&mapping->page_tree, |
| 87 | page_index(page), |
| 88 | PAGECACHE_TAG_DIRTY); |
| 89 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
| 90 | |
| 91 | clear_page_dirty_for_io(page); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 92 | dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 93 | } |
| 94 | ClearPageUptodate(page); |
| 95 | } |
| 96 | |
| 97 | static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid) |
| 98 | { |
| 99 | pgoff_t index = current_nat_addr(sbi, nid); |
| 100 | return get_meta_page(sbi, index); |
| 101 | } |
| 102 | |
| 103 | static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid) |
| 104 | { |
| 105 | struct page *src_page; |
| 106 | struct page *dst_page; |
| 107 | pgoff_t src_off; |
| 108 | pgoff_t dst_off; |
| 109 | void *src_addr; |
| 110 | void *dst_addr; |
| 111 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 112 | |
| 113 | src_off = current_nat_addr(sbi, nid); |
| 114 | dst_off = next_nat_addr(sbi, src_off); |
| 115 | |
| 116 | /* get current nat block page with lock */ |
| 117 | src_page = get_meta_page(sbi, src_off); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 118 | dst_page = grab_meta_page(sbi, dst_off); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 119 | f2fs_bug_on(sbi, PageDirty(src_page)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 120 | |
| 121 | src_addr = page_address(src_page); |
| 122 | dst_addr = page_address(dst_page); |
| 123 | memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE); |
| 124 | set_page_dirty(dst_page); |
| 125 | f2fs_put_page(src_page, 1); |
| 126 | |
| 127 | set_to_next_nat(nm_i, nid); |
| 128 | |
| 129 | return dst_page; |
| 130 | } |
| 131 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 132 | static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n) |
| 133 | { |
| 134 | return radix_tree_lookup(&nm_i->nat_root, n); |
| 135 | } |
| 136 | |
| 137 | static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i, |
| 138 | nid_t start, unsigned int nr, struct nat_entry **ep) |
| 139 | { |
| 140 | return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr); |
| 141 | } |
| 142 | |
| 143 | static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e) |
| 144 | { |
| 145 | list_del(&e->list); |
| 146 | radix_tree_delete(&nm_i->nat_root, nat_get_nid(e)); |
| 147 | nm_i->nat_cnt--; |
| 148 | kmem_cache_free(nat_entry_slab, e); |
| 149 | } |
| 150 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 151 | static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i, |
| 152 | struct nat_entry *ne) |
| 153 | { |
| 154 | nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid); |
| 155 | struct nat_entry_set *head; |
| 156 | |
| 157 | if (get_nat_flag(ne, IS_DIRTY)) |
| 158 | return; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 159 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 160 | head = radix_tree_lookup(&nm_i->nat_set_root, set); |
| 161 | if (!head) { |
| 162 | head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC); |
| 163 | |
| 164 | INIT_LIST_HEAD(&head->entry_list); |
| 165 | INIT_LIST_HEAD(&head->set_list); |
| 166 | head->set = set; |
| 167 | head->entry_cnt = 0; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 168 | f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 169 | } |
| 170 | list_move_tail(&ne->list, &head->entry_list); |
| 171 | nm_i->dirty_nat_cnt++; |
| 172 | head->entry_cnt++; |
| 173 | set_nat_flag(ne, IS_DIRTY, true); |
| 174 | } |
| 175 | |
| 176 | static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i, |
| 177 | struct nat_entry *ne) |
| 178 | { |
Changman Lee | 20d047c | 2014-11-25 12:44:23 +0900 | [diff] [blame] | 179 | nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 180 | struct nat_entry_set *head; |
| 181 | |
| 182 | head = radix_tree_lookup(&nm_i->nat_set_root, set); |
| 183 | if (head) { |
| 184 | list_move_tail(&ne->list, &nm_i->nat_entries); |
| 185 | set_nat_flag(ne, IS_DIRTY, false); |
| 186 | head->entry_cnt--; |
| 187 | nm_i->dirty_nat_cnt--; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i, |
| 192 | nid_t start, unsigned int nr, struct nat_entry_set **ep) |
| 193 | { |
| 194 | return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep, |
| 195 | start, nr); |
| 196 | } |
| 197 | |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 198 | bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 199 | { |
| 200 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 201 | struct nat_entry *e; |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 202 | bool is_cp = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 203 | |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 204 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 205 | e = __lookup_nat_cache(nm_i, nid); |
Jaegeuk Kim | 7ef35e3 | 2014-09-15 12:07:13 -0700 | [diff] [blame] | 206 | if (e && !get_nat_flag(e, IS_CHECKPOINTED)) |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 207 | is_cp = false; |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 208 | up_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 209 | return is_cp; |
| 210 | } |
| 211 | |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 212 | bool has_fsynced_inode(struct f2fs_sb_info *sbi, nid_t ino) |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 213 | { |
| 214 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 215 | struct nat_entry *e; |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 216 | bool fsynced = false; |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 217 | |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 218 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 219 | e = __lookup_nat_cache(nm_i, ino); |
| 220 | if (e && get_nat_flag(e, HAS_FSYNCED_INODE)) |
| 221 | fsynced = true; |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 222 | up_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 223 | return fsynced; |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 224 | } |
| 225 | |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 226 | 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] | 227 | { |
| 228 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 229 | struct nat_entry *e; |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 230 | bool need_update = true; |
Jaegeuk Kim | b6fe587 | 2014-06-04 00:39:42 +0900 | [diff] [blame] | 231 | |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 232 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 233 | e = __lookup_nat_cache(nm_i, ino); |
| 234 | if (e && get_nat_flag(e, HAS_LAST_FSYNC) && |
| 235 | (get_nat_flag(e, IS_CHECKPOINTED) || |
| 236 | get_nat_flag(e, HAS_FSYNCED_INODE))) |
| 237 | need_update = false; |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 238 | up_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 239 | return need_update; |
Jaegeuk Kim | b6fe587 | 2014-06-04 00:39:42 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 242 | static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid) |
| 243 | { |
| 244 | struct nat_entry *new; |
| 245 | |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 246 | new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC); |
| 247 | f2fs_radix_tree_insert(&nm_i->nat_root, nid, new); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 248 | memset(new, 0, sizeof(struct nat_entry)); |
| 249 | nat_set_nid(new, nid); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 250 | nat_reset_flag(new); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 251 | list_add_tail(&new->list, &nm_i->nat_entries); |
| 252 | nm_i->nat_cnt++; |
| 253 | return new; |
| 254 | } |
| 255 | |
| 256 | static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid, |
| 257 | struct f2fs_nat_entry *ne) |
| 258 | { |
| 259 | struct nat_entry *e; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 260 | |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 261 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 262 | e = __lookup_nat_cache(nm_i, nid); |
| 263 | if (!e) { |
| 264 | e = grab_nat_entry(nm_i, nid); |
Chao Yu | 94dac22 | 2014-04-17 10:51:05 +0800 | [diff] [blame] | 265 | node_info_from_raw_nat(&e->ni, ne); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 266 | } |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 267 | up_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | 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] | 271 | block_t new_blkaddr, bool fsync_done) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 272 | { |
| 273 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 274 | struct nat_entry *e; |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 275 | |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 276 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 277 | e = __lookup_nat_cache(nm_i, ni->nid); |
| 278 | if (!e) { |
| 279 | e = grab_nat_entry(nm_i, ni->nid); |
Chao Yu | 5c27f4e | 2014-12-18 17:37:21 +0800 | [diff] [blame] | 280 | copy_node_info(&e->ni, ni); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 281 | f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 282 | } else if (new_blkaddr == NEW_ADDR) { |
| 283 | /* |
| 284 | * when nid is reallocated, |
| 285 | * previous nat entry can be remained in nat cache. |
| 286 | * So, reinitialize it with new information. |
| 287 | */ |
Chao Yu | 5c27f4e | 2014-12-18 17:37:21 +0800 | [diff] [blame] | 288 | copy_node_info(&e->ni, ni); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 289 | f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 290 | } |
| 291 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 292 | /* sanity check */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 293 | f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr); |
| 294 | f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR && |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 295 | new_blkaddr == NULL_ADDR); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 296 | f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR && |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 297 | new_blkaddr == NEW_ADDR); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 298 | f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR && |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 299 | nat_get_blkaddr(e) != NULL_ADDR && |
| 300 | new_blkaddr == NEW_ADDR); |
| 301 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 302 | /* increment version no as node is removed */ |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 303 | if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) { |
| 304 | unsigned char version = nat_get_version(e); |
| 305 | nat_set_version(e, inc_node_version(version)); |
| 306 | } |
| 307 | |
| 308 | /* change address */ |
| 309 | nat_set_blkaddr(e, new_blkaddr); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 310 | if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR) |
| 311 | set_nat_flag(e, IS_CHECKPOINTED, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 312 | __set_nat_cache_dirty(nm_i, e); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 313 | |
| 314 | /* update fsync_mark if its inode nat entry is still alive */ |
| 315 | e = __lookup_nat_cache(nm_i, ni->ino); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 316 | if (e) { |
| 317 | if (fsync_done && ni->nid == ni->ino) |
| 318 | set_nat_flag(e, HAS_FSYNCED_INODE, true); |
| 319 | set_nat_flag(e, HAS_LAST_FSYNC, fsync_done); |
| 320 | } |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 321 | up_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 322 | } |
| 323 | |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 324 | 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] | 325 | { |
| 326 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 327 | |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 328 | if (available_free_memory(sbi, NAT_ENTRIES)) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 329 | return 0; |
| 330 | |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 331 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 332 | while (nr_shrink && !list_empty(&nm_i->nat_entries)) { |
| 333 | struct nat_entry *ne; |
| 334 | ne = list_first_entry(&nm_i->nat_entries, |
| 335 | struct nat_entry, list); |
| 336 | __del_from_nat_cache(nm_i, ne); |
| 337 | nr_shrink--; |
| 338 | } |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 339 | up_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 340 | return nr_shrink; |
| 341 | } |
| 342 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 343 | /* |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 344 | * This function always returns success |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 345 | */ |
| 346 | void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni) |
| 347 | { |
| 348 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 349 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
| 350 | struct f2fs_summary_block *sum = curseg->sum_blk; |
| 351 | nid_t start_nid = START_NID(nid); |
| 352 | struct f2fs_nat_block *nat_blk; |
| 353 | struct page *page = NULL; |
| 354 | struct f2fs_nat_entry ne; |
| 355 | struct nat_entry *e; |
| 356 | int i; |
| 357 | |
| 358 | ni->nid = nid; |
| 359 | |
| 360 | /* Check nat cache */ |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 361 | down_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 362 | e = __lookup_nat_cache(nm_i, nid); |
| 363 | if (e) { |
| 364 | ni->ino = nat_get_ino(e); |
| 365 | ni->blk_addr = nat_get_blkaddr(e); |
| 366 | ni->version = nat_get_version(e); |
| 367 | } |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 368 | up_read(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 369 | if (e) |
| 370 | return; |
| 371 | |
Jaegeuk Kim | 3547ea9 | 2014-12-30 23:08:26 -0800 | [diff] [blame] | 372 | memset(&ne, 0, sizeof(struct f2fs_nat_entry)); |
| 373 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 374 | /* Check current segment summary */ |
| 375 | mutex_lock(&curseg->curseg_mutex); |
| 376 | i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0); |
| 377 | if (i >= 0) { |
| 378 | ne = nat_in_journal(sum, i); |
| 379 | node_info_from_raw_nat(ni, &ne); |
| 380 | } |
| 381 | mutex_unlock(&curseg->curseg_mutex); |
| 382 | if (i >= 0) |
| 383 | goto cache; |
| 384 | |
| 385 | /* Fill node_info from nat page */ |
| 386 | page = get_current_nat_page(sbi, start_nid); |
| 387 | nat_blk = (struct f2fs_nat_block *)page_address(page); |
| 388 | ne = nat_blk->entries[nid - start_nid]; |
| 389 | node_info_from_raw_nat(ni, &ne); |
| 390 | f2fs_put_page(page, 1); |
| 391 | cache: |
| 392 | /* cache nat entry */ |
| 393 | cache_nat_entry(NM_I(sbi), nid, &ne); |
| 394 | } |
| 395 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 396 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 397 | * The maximum depth is four. |
| 398 | * Offset[0] will have raw inode offset. |
| 399 | */ |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 400 | static int get_node_path(struct f2fs_inode_info *fi, long block, |
| 401 | int offset[4], unsigned int noffset[4]) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 402 | { |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 403 | const long direct_index = ADDRS_PER_INODE(fi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 404 | const long direct_blks = ADDRS_PER_BLOCK; |
| 405 | const long dptrs_per_blk = NIDS_PER_BLOCK; |
| 406 | const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK; |
| 407 | const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK; |
| 408 | int n = 0; |
| 409 | int level = 0; |
| 410 | |
| 411 | noffset[0] = 0; |
| 412 | |
| 413 | if (block < direct_index) { |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 414 | offset[n] = block; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 415 | goto got; |
| 416 | } |
| 417 | block -= direct_index; |
| 418 | if (block < direct_blks) { |
| 419 | offset[n++] = NODE_DIR1_BLOCK; |
| 420 | noffset[n] = 1; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 421 | offset[n] = block; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 422 | level = 1; |
| 423 | goto got; |
| 424 | } |
| 425 | block -= direct_blks; |
| 426 | if (block < direct_blks) { |
| 427 | offset[n++] = NODE_DIR2_BLOCK; |
| 428 | noffset[n] = 2; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 429 | offset[n] = block; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 430 | level = 1; |
| 431 | goto got; |
| 432 | } |
| 433 | block -= direct_blks; |
| 434 | if (block < indirect_blks) { |
| 435 | offset[n++] = NODE_IND1_BLOCK; |
| 436 | noffset[n] = 3; |
| 437 | offset[n++] = block / direct_blks; |
| 438 | noffset[n] = 4 + offset[n - 1]; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 439 | offset[n] = block % direct_blks; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 440 | level = 2; |
| 441 | goto got; |
| 442 | } |
| 443 | block -= indirect_blks; |
| 444 | if (block < indirect_blks) { |
| 445 | offset[n++] = NODE_IND2_BLOCK; |
| 446 | noffset[n] = 4 + dptrs_per_blk; |
| 447 | offset[n++] = block / direct_blks; |
| 448 | noffset[n] = 5 + dptrs_per_blk + offset[n - 1]; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 449 | offset[n] = block % direct_blks; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 450 | level = 2; |
| 451 | goto got; |
| 452 | } |
| 453 | block -= indirect_blks; |
| 454 | if (block < dindirect_blks) { |
| 455 | offset[n++] = NODE_DIND_BLOCK; |
| 456 | noffset[n] = 5 + (dptrs_per_blk * 2); |
| 457 | offset[n++] = block / indirect_blks; |
| 458 | noffset[n] = 6 + (dptrs_per_blk * 2) + |
| 459 | offset[n - 1] * (dptrs_per_blk + 1); |
| 460 | offset[n++] = (block / direct_blks) % dptrs_per_blk; |
| 461 | noffset[n] = 7 + (dptrs_per_blk * 2) + |
| 462 | offset[n - 2] * (dptrs_per_blk + 1) + |
| 463 | offset[n - 1]; |
Namjae Jeon | 25c0a6e | 2013-03-02 12:41:31 +0900 | [diff] [blame] | 464 | offset[n] = block % direct_blks; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 465 | level = 3; |
| 466 | goto got; |
| 467 | } else { |
| 468 | BUG(); |
| 469 | } |
| 470 | got: |
| 471 | return level; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Caller should call f2fs_put_dnode(dn). |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 476 | * Also, it should grab and release a rwsem by calling f2fs_lock_op() and |
| 477 | * f2fs_unlock_op() only if ro is not set RDONLY_NODE. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 478 | * 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] | 479 | */ |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 480 | 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] | 481 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 482 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 483 | struct page *npage[4]; |
Jaegeuk Kim | f1a3b98 | 2015-02-11 11:25:11 -0800 | [diff] [blame] | 484 | struct page *parent = NULL; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 485 | int offset[4]; |
| 486 | unsigned int noffset[4]; |
| 487 | nid_t nids[4]; |
| 488 | int level, i; |
| 489 | int err = 0; |
| 490 | |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 491 | level = get_node_path(F2FS_I(dn->inode), index, offset, noffset); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 492 | |
| 493 | nids[0] = dn->inode->i_ino; |
Jaegeuk Kim | 1646cfa | 2013-05-20 09:42:28 +0900 | [diff] [blame] | 494 | npage[0] = dn->inode_page; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 495 | |
Jaegeuk Kim | 1646cfa | 2013-05-20 09:42:28 +0900 | [diff] [blame] | 496 | if (!npage[0]) { |
| 497 | npage[0] = get_node_page(sbi, nids[0]); |
| 498 | if (IS_ERR(npage[0])) |
| 499 | return PTR_ERR(npage[0]); |
| 500 | } |
Jaegeuk Kim | f1a3b98 | 2015-02-11 11:25:11 -0800 | [diff] [blame] | 501 | |
| 502 | /* if inline_data is set, should not report any block indices */ |
| 503 | if (f2fs_has_inline_data(dn->inode) && index) { |
| 504 | err = -EINVAL; |
| 505 | f2fs_put_page(npage[0], 1); |
| 506 | goto release_out; |
| 507 | } |
| 508 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 509 | parent = npage[0]; |
Changman Lee | 52c2db3 | 2013-02-20 07:47:06 +0900 | [diff] [blame] | 510 | if (level != 0) |
| 511 | nids[1] = get_nid(parent, offset[0], true); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 512 | dn->inode_page = npage[0]; |
| 513 | dn->inode_page_locked = true; |
| 514 | |
| 515 | /* get indirect or direct nodes */ |
| 516 | for (i = 1; i <= level; i++) { |
| 517 | bool done = false; |
| 518 | |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 519 | if (!nids[i] && mode == ALLOC_NODE) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 520 | /* alloc new node */ |
| 521 | if (!alloc_nid(sbi, &(nids[i]))) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 522 | err = -ENOSPC; |
| 523 | goto release_pages; |
| 524 | } |
| 525 | |
| 526 | dn->nid = nids[i]; |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 527 | npage[i] = new_node_page(dn, noffset[i], NULL); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 528 | if (IS_ERR(npage[i])) { |
| 529 | alloc_nid_failed(sbi, nids[i]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 530 | err = PTR_ERR(npage[i]); |
| 531 | goto release_pages; |
| 532 | } |
| 533 | |
| 534 | set_nid(parent, offset[i - 1], nids[i], i == 1); |
| 535 | alloc_nid_done(sbi, nids[i]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 536 | done = true; |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 537 | } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 538 | npage[i] = get_node_page_ra(parent, offset[i - 1]); |
| 539 | if (IS_ERR(npage[i])) { |
| 540 | err = PTR_ERR(npage[i]); |
| 541 | goto release_pages; |
| 542 | } |
| 543 | done = true; |
| 544 | } |
| 545 | if (i == 1) { |
| 546 | dn->inode_page_locked = false; |
| 547 | unlock_page(parent); |
| 548 | } else { |
| 549 | f2fs_put_page(parent, 1); |
| 550 | } |
| 551 | |
| 552 | if (!done) { |
| 553 | npage[i] = get_node_page(sbi, nids[i]); |
| 554 | if (IS_ERR(npage[i])) { |
| 555 | err = PTR_ERR(npage[i]); |
| 556 | f2fs_put_page(npage[0], 0); |
| 557 | goto release_out; |
| 558 | } |
| 559 | } |
| 560 | if (i < level) { |
| 561 | parent = npage[i]; |
| 562 | nids[i + 1] = get_nid(parent, offset[i], false); |
| 563 | } |
| 564 | } |
| 565 | dn->nid = nids[level]; |
| 566 | dn->ofs_in_node = offset[level]; |
| 567 | dn->node_page = npage[level]; |
| 568 | dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node); |
| 569 | return 0; |
| 570 | |
| 571 | release_pages: |
| 572 | f2fs_put_page(parent, 1); |
| 573 | if (i > 1) |
| 574 | f2fs_put_page(npage[0], 0); |
| 575 | release_out: |
| 576 | dn->inode_page = NULL; |
| 577 | dn->node_page = NULL; |
| 578 | return err; |
| 579 | } |
| 580 | |
| 581 | static void truncate_node(struct dnode_of_data *dn) |
| 582 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 583 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 584 | struct node_info ni; |
| 585 | |
| 586 | get_node_info(sbi, dn->nid, &ni); |
Jaegeuk Kim | 71e9fec | 2012-12-20 15:10:06 +0900 | [diff] [blame] | 587 | if (dn->inode->i_blocks == 0) { |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 588 | f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR); |
Jaegeuk Kim | 71e9fec | 2012-12-20 15:10:06 +0900 | [diff] [blame] | 589 | goto invalidate; |
| 590 | } |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 591 | f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 592 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 593 | /* Deallocate node address */ |
Jaegeuk Kim | 71e9fec | 2012-12-20 15:10:06 +0900 | [diff] [blame] | 594 | invalidate_blocks(sbi, ni.blk_addr); |
Gu Zheng | ef86d70 | 2013-11-19 18:03:38 +0800 | [diff] [blame] | 595 | dec_valid_node_count(sbi, dn->inode); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 596 | set_node_addr(sbi, &ni, NULL_ADDR, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 597 | |
| 598 | if (dn->nid == dn->inode->i_ino) { |
| 599 | remove_orphan_inode(sbi, dn->nid); |
| 600 | dec_valid_inode_count(sbi); |
| 601 | } else { |
| 602 | sync_inode_page(dn); |
| 603 | } |
Jaegeuk Kim | 71e9fec | 2012-12-20 15:10:06 +0900 | [diff] [blame] | 604 | invalidate: |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 605 | clear_node_page_dirty(dn->node_page); |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 606 | set_sbi_flag(sbi, SBI_IS_DIRTY); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 607 | |
| 608 | f2fs_put_page(dn->node_page, 1); |
Jaegeuk Kim | bf39c00 | 2014-01-22 20:41:57 +0900 | [diff] [blame] | 609 | |
| 610 | invalidate_mapping_pages(NODE_MAPPING(sbi), |
| 611 | dn->node_page->index, dn->node_page->index); |
| 612 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 613 | dn->node_page = NULL; |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 614 | trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static int truncate_dnode(struct dnode_of_data *dn) |
| 618 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 619 | struct page *page; |
| 620 | |
| 621 | if (dn->nid == 0) |
| 622 | return 1; |
| 623 | |
| 624 | /* get direct node */ |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 625 | page = get_node_page(F2FS_I_SB(dn->inode), dn->nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 626 | if (IS_ERR(page) && PTR_ERR(page) == -ENOENT) |
| 627 | return 1; |
| 628 | else if (IS_ERR(page)) |
| 629 | return PTR_ERR(page); |
| 630 | |
| 631 | /* Make dnode_of_data for parameter */ |
| 632 | dn->node_page = page; |
| 633 | dn->ofs_in_node = 0; |
| 634 | truncate_data_blocks(dn); |
| 635 | truncate_node(dn); |
| 636 | return 1; |
| 637 | } |
| 638 | |
| 639 | static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs, |
| 640 | int ofs, int depth) |
| 641 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 642 | struct dnode_of_data rdn = *dn; |
| 643 | struct page *page; |
| 644 | struct f2fs_node *rn; |
| 645 | nid_t child_nid; |
| 646 | unsigned int child_nofs; |
| 647 | int freed = 0; |
| 648 | int i, ret; |
| 649 | |
| 650 | if (dn->nid == 0) |
| 651 | return NIDS_PER_BLOCK + 1; |
| 652 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 653 | trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr); |
| 654 | |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 655 | page = get_node_page(F2FS_I_SB(dn->inode), dn->nid); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 656 | if (IS_ERR(page)) { |
| 657 | trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 658 | return PTR_ERR(page); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 659 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 660 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 661 | rn = F2FS_NODE(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 662 | if (depth < 3) { |
| 663 | for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) { |
| 664 | child_nid = le32_to_cpu(rn->in.nid[i]); |
| 665 | if (child_nid == 0) |
| 666 | continue; |
| 667 | rdn.nid = child_nid; |
| 668 | ret = truncate_dnode(&rdn); |
| 669 | if (ret < 0) |
| 670 | goto out_err; |
| 671 | set_nid(page, i, 0, false); |
| 672 | } |
| 673 | } else { |
| 674 | child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1; |
| 675 | for (i = ofs; i < NIDS_PER_BLOCK; i++) { |
| 676 | child_nid = le32_to_cpu(rn->in.nid[i]); |
| 677 | if (child_nid == 0) { |
| 678 | child_nofs += NIDS_PER_BLOCK + 1; |
| 679 | continue; |
| 680 | } |
| 681 | rdn.nid = child_nid; |
| 682 | ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1); |
| 683 | if (ret == (NIDS_PER_BLOCK + 1)) { |
| 684 | set_nid(page, i, 0, false); |
| 685 | child_nofs += ret; |
| 686 | } else if (ret < 0 && ret != -ENOENT) { |
| 687 | goto out_err; |
| 688 | } |
| 689 | } |
| 690 | freed = child_nofs; |
| 691 | } |
| 692 | |
| 693 | if (!ofs) { |
| 694 | /* remove current indirect node */ |
| 695 | dn->node_page = page; |
| 696 | truncate_node(dn); |
| 697 | freed++; |
| 698 | } else { |
| 699 | f2fs_put_page(page, 1); |
| 700 | } |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 701 | trace_f2fs_truncate_nodes_exit(dn->inode, freed); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 702 | return freed; |
| 703 | |
| 704 | out_err: |
| 705 | f2fs_put_page(page, 1); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 706 | trace_f2fs_truncate_nodes_exit(dn->inode, ret); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | static int truncate_partial_nodes(struct dnode_of_data *dn, |
| 711 | struct f2fs_inode *ri, int *offset, int depth) |
| 712 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 713 | struct page *pages[2]; |
| 714 | nid_t nid[3]; |
| 715 | nid_t child_nid; |
| 716 | int err = 0; |
| 717 | int i; |
| 718 | int idx = depth - 2; |
| 719 | |
| 720 | nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]); |
| 721 | if (!nid[0]) |
| 722 | return 0; |
| 723 | |
| 724 | /* get indirect nodes in the path */ |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 725 | for (i = 0; i < idx + 1; i++) { |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 726 | /* reference count'll be increased */ |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 727 | pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 728 | if (IS_ERR(pages[i])) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 729 | err = PTR_ERR(pages[i]); |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 730 | idx = i - 1; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 731 | goto fail; |
| 732 | } |
| 733 | nid[i + 1] = get_nid(pages[i], offset[i + 1], false); |
| 734 | } |
| 735 | |
| 736 | /* free direct nodes linked to a partial indirect node */ |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 737 | for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 738 | child_nid = get_nid(pages[idx], i, false); |
| 739 | if (!child_nid) |
| 740 | continue; |
| 741 | dn->nid = child_nid; |
| 742 | err = truncate_dnode(dn); |
| 743 | if (err < 0) |
| 744 | goto fail; |
| 745 | set_nid(pages[idx], i, 0, false); |
| 746 | } |
| 747 | |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 748 | if (offset[idx + 1] == 0) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 749 | dn->node_page = pages[idx]; |
| 750 | dn->nid = nid[idx]; |
| 751 | truncate_node(dn); |
| 752 | } else { |
| 753 | f2fs_put_page(pages[idx], 1); |
| 754 | } |
| 755 | offset[idx]++; |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 756 | offset[idx + 1] = 0; |
| 757 | idx--; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 758 | fail: |
shifei10.ge | a225dca | 2013-10-29 15:32:34 +0800 | [diff] [blame] | 759 | for (i = idx; i >= 0; i--) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 760 | f2fs_put_page(pages[i], 1); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 761 | |
| 762 | trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err); |
| 763 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 764 | return err; |
| 765 | } |
| 766 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 767 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 768 | * All the block addresses of data and nodes should be nullified. |
| 769 | */ |
| 770 | int truncate_inode_blocks(struct inode *inode, pgoff_t from) |
| 771 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 772 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 773 | int err = 0, cont = 1; |
| 774 | int level, offset[4], noffset[4]; |
Jaegeuk Kim | 7dd690c | 2013-02-12 07:28:55 +0900 | [diff] [blame] | 775 | unsigned int nofs = 0; |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 776 | struct f2fs_inode *ri; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 777 | struct dnode_of_data dn; |
| 778 | struct page *page; |
| 779 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 780 | trace_f2fs_truncate_inode_blocks_enter(inode, from); |
| 781 | |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 782 | level = get_node_path(F2FS_I(inode), from, offset, noffset); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 783 | restart: |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 784 | page = get_node_page(sbi, inode->i_ino); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 785 | if (IS_ERR(page)) { |
| 786 | trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 787 | return PTR_ERR(page); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 788 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 789 | |
| 790 | set_new_dnode(&dn, inode, page, NULL, 0); |
| 791 | unlock_page(page); |
| 792 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 793 | ri = F2FS_INODE(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 794 | switch (level) { |
| 795 | case 0: |
| 796 | case 1: |
| 797 | nofs = noffset[1]; |
| 798 | break; |
| 799 | case 2: |
| 800 | nofs = noffset[1]; |
| 801 | if (!offset[level - 1]) |
| 802 | goto skip_partial; |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 803 | err = truncate_partial_nodes(&dn, ri, offset, level); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 804 | if (err < 0 && err != -ENOENT) |
| 805 | goto fail; |
| 806 | nofs += 1 + NIDS_PER_BLOCK; |
| 807 | break; |
| 808 | case 3: |
| 809 | nofs = 5 + 2 * NIDS_PER_BLOCK; |
| 810 | if (!offset[level - 1]) |
| 811 | goto skip_partial; |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 812 | err = truncate_partial_nodes(&dn, ri, offset, level); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 813 | if (err < 0 && err != -ENOENT) |
| 814 | goto fail; |
| 815 | break; |
| 816 | default: |
| 817 | BUG(); |
| 818 | } |
| 819 | |
| 820 | skip_partial: |
| 821 | while (cont) { |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 822 | 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] | 823 | switch (offset[0]) { |
| 824 | case NODE_DIR1_BLOCK: |
| 825 | case NODE_DIR2_BLOCK: |
| 826 | err = truncate_dnode(&dn); |
| 827 | break; |
| 828 | |
| 829 | case NODE_IND1_BLOCK: |
| 830 | case NODE_IND2_BLOCK: |
| 831 | err = truncate_nodes(&dn, nofs, offset[1], 2); |
| 832 | break; |
| 833 | |
| 834 | case NODE_DIND_BLOCK: |
| 835 | err = truncate_nodes(&dn, nofs, offset[1], 3); |
| 836 | cont = 0; |
| 837 | break; |
| 838 | |
| 839 | default: |
| 840 | BUG(); |
| 841 | } |
| 842 | if (err < 0 && err != -ENOENT) |
| 843 | goto fail; |
| 844 | if (offset[1] == 0 && |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 845 | ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 846 | lock_page(page); |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 847 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 848 | f2fs_put_page(page, 1); |
| 849 | goto restart; |
| 850 | } |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 851 | f2fs_wait_on_page_writeback(page, NODE); |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 852 | ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 853 | set_page_dirty(page); |
| 854 | unlock_page(page); |
| 855 | } |
| 856 | offset[1] = 0; |
| 857 | offset[0]++; |
| 858 | nofs += err; |
| 859 | } |
| 860 | fail: |
| 861 | f2fs_put_page(page, 0); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 862 | trace_f2fs_truncate_inode_blocks_exit(inode, err); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 863 | return err > 0 ? 0 : err; |
| 864 | } |
| 865 | |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 866 | int truncate_xattr_node(struct inode *inode, struct page *page) |
| 867 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 868 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 869 | nid_t nid = F2FS_I(inode)->i_xattr_nid; |
| 870 | struct dnode_of_data dn; |
| 871 | struct page *npage; |
| 872 | |
| 873 | if (!nid) |
| 874 | return 0; |
| 875 | |
| 876 | npage = get_node_page(sbi, nid); |
| 877 | if (IS_ERR(npage)) |
| 878 | return PTR_ERR(npage); |
| 879 | |
| 880 | F2FS_I(inode)->i_xattr_nid = 0; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 881 | |
| 882 | /* need to do checkpoint during fsync */ |
| 883 | F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi)); |
| 884 | |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 885 | set_new_dnode(&dn, inode, page, npage, nid); |
| 886 | |
| 887 | if (page) |
Chao Yu | 01d2d1a | 2013-11-28 15:43:07 +0800 | [diff] [blame] | 888 | dn.inode_page_locked = true; |
Jaegeuk Kim | 4f16fb0 | 2013-08-14 20:40:06 +0900 | [diff] [blame] | 889 | truncate_node(&dn); |
| 890 | return 0; |
| 891 | } |
| 892 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 893 | /* |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 894 | * Caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 895 | * f2fs_unlock_op(). |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 896 | */ |
Gu Zheng | 58e674d | 2013-11-19 18:03:18 +0800 | [diff] [blame] | 897 | void remove_inode_page(struct inode *inode) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 898 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 899 | struct dnode_of_data dn; |
| 900 | |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 901 | set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); |
| 902 | if (get_dnode_of_data(&dn, 0, LOOKUP_NODE)) |
Gu Zheng | 58e674d | 2013-11-19 18:03:18 +0800 | [diff] [blame] | 903 | return; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 904 | |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 905 | if (truncate_xattr_node(inode, dn.inode_page)) { |
| 906 | f2fs_put_dnode(&dn); |
Gu Zheng | 58e674d | 2013-11-19 18:03:18 +0800 | [diff] [blame] | 907 | return; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 908 | } |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 909 | |
| 910 | /* remove potential inline_data blocks */ |
| 911 | if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 912 | S_ISLNK(inode->i_mode)) |
| 913 | truncate_data_blocks_range(&dn, 1); |
| 914 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 915 | /* 0 is possible, after f2fs_new_inode() has failed */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 916 | f2fs_bug_on(F2FS_I_SB(inode), |
| 917 | inode->i_blocks != 0 && inode->i_blocks != 1); |
Jaegeuk Kim | c2e6958 | 2014-08-25 14:45:59 -0700 | [diff] [blame] | 918 | |
| 919 | /* will put inode & node pages */ |
Jaegeuk Kim | 71e9fec | 2012-12-20 15:10:06 +0900 | [diff] [blame] | 920 | truncate_node(&dn); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 921 | } |
| 922 | |
Jaegeuk Kim | a014e03 | 2014-06-20 21:44:02 -0700 | [diff] [blame] | 923 | struct page *new_inode_page(struct inode *inode) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 924 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 925 | struct dnode_of_data dn; |
| 926 | |
| 927 | /* allocate inode page for new inode */ |
| 928 | set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino); |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 929 | |
| 930 | /* caller should f2fs_put_page(page, 1); */ |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 931 | return new_node_page(&dn, 0, NULL); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 932 | } |
| 933 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 934 | struct page *new_node_page(struct dnode_of_data *dn, |
| 935 | unsigned int ofs, struct page *ipage) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 936 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 937 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 938 | struct node_info old_ni, new_ni; |
| 939 | struct page *page; |
| 940 | int err; |
| 941 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 942 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 943 | return ERR_PTR(-EPERM); |
| 944 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 945 | page = grab_cache_page(NODE_MAPPING(sbi), dn->nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 946 | if (!page) |
| 947 | return ERR_PTR(-ENOMEM); |
| 948 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 949 | if (unlikely(!inc_valid_node_count(sbi, dn->inode))) { |
Jaegeuk Kim | 9c02740 | 2013-08-12 16:00:46 +0900 | [diff] [blame] | 950 | err = -ENOSPC; |
| 951 | goto fail; |
| 952 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 953 | |
Jaegeuk Kim | 9c02740 | 2013-08-12 16:00:46 +0900 | [diff] [blame] | 954 | get_node_info(sbi, dn->nid, &old_ni); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 955 | |
| 956 | /* Reinitialize old_ni with new node page */ |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 957 | f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 958 | new_ni = old_ni; |
| 959 | new_ni.ino = dn->inode->i_ino; |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 960 | set_node_addr(sbi, &new_ni, NEW_ADDR, false); |
Jaegeuk Kim | 9c02740 | 2013-08-12 16:00:46 +0900 | [diff] [blame] | 961 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 962 | f2fs_wait_on_page_writeback(page, NODE); |
Jaegeuk Kim | 9c02740 | 2013-08-12 16:00:46 +0900 | [diff] [blame] | 963 | fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true); |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 964 | set_cold_node(dn->inode, page); |
Jaegeuk Kim | 9c02740 | 2013-08-12 16:00:46 +0900 | [diff] [blame] | 965 | SetPageUptodate(page); |
| 966 | set_page_dirty(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 967 | |
Chao Yu | 4bc8e9b | 2014-03-17 16:35:06 +0800 | [diff] [blame] | 968 | if (f2fs_has_xattr_block(ofs)) |
Jaegeuk Kim | 479bd73 | 2013-08-12 16:04:53 +0900 | [diff] [blame] | 969 | F2FS_I(dn->inode)->i_xattr_nid = dn->nid; |
| 970 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 971 | dn->node_page = page; |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 972 | if (ipage) |
| 973 | update_inode(dn->inode, ipage); |
| 974 | else |
| 975 | sync_inode_page(dn); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 976 | if (ofs == 0) |
| 977 | inc_valid_inode_count(sbi); |
| 978 | |
| 979 | return page; |
| 980 | |
| 981 | fail: |
Jaegeuk Kim | 71e9fec | 2012-12-20 15:10:06 +0900 | [diff] [blame] | 982 | clear_node_page_dirty(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 983 | f2fs_put_page(page, 1); |
| 984 | return ERR_PTR(err); |
| 985 | } |
| 986 | |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 987 | /* |
| 988 | * Caller should do after getting the following values. |
| 989 | * 0: f2fs_put_page(page, 0) |
| 990 | * LOCKED_PAGE: f2fs_put_page(page, 1) |
| 991 | * error: nothing |
| 992 | */ |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 993 | static int read_node_page(struct page *page, int rw) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 994 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 995 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 996 | struct node_info ni; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 997 | struct f2fs_io_info fio = { |
| 998 | .type = NODE, |
| 999 | .rw = rw, |
| 1000 | }; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1001 | |
| 1002 | get_node_info(sbi, page->index, &ni); |
| 1003 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1004 | if (unlikely(ni.blk_addr == NULL_ADDR)) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1005 | f2fs_put_page(page, 1); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1006 | return -ENOENT; |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1007 | } |
| 1008 | |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1009 | if (PageUptodate(page)) |
| 1010 | return LOCKED_PAGE; |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1011 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1012 | fio.blk_addr = ni.blk_addr; |
| 1013 | return f2fs_submit_page_bio(sbi, page, &fio); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1014 | } |
| 1015 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1016 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1017 | * Readahead a node page |
| 1018 | */ |
| 1019 | void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid) |
| 1020 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1021 | struct page *apage; |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1022 | int err; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1023 | |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1024 | apage = find_get_page(NODE_MAPPING(sbi), nid); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1025 | if (apage && PageUptodate(apage)) { |
| 1026 | f2fs_put_page(apage, 0); |
| 1027 | return; |
| 1028 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1029 | f2fs_put_page(apage, 0); |
| 1030 | |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1031 | apage = grab_cache_page(NODE_MAPPING(sbi), nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1032 | if (!apage) |
| 1033 | return; |
| 1034 | |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1035 | err = read_node_page(apage, READA); |
| 1036 | if (err == 0) |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1037 | f2fs_put_page(apage, 0); |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1038 | else if (err == LOCKED_PAGE) |
| 1039 | f2fs_put_page(apage, 1); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid) |
| 1043 | { |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1044 | struct page *page; |
| 1045 | int err; |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1046 | repeat: |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 1047 | page = grab_cache_page(NODE_MAPPING(sbi), nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1048 | if (!page) |
| 1049 | return ERR_PTR(-ENOMEM); |
| 1050 | |
| 1051 | err = read_node_page(page, READ_SYNC); |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1052 | if (err < 0) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1053 | return ERR_PTR(err); |
Jaegeuk Kim | aaf9607 | 2015-02-06 18:53:45 -0800 | [diff] [blame] | 1054 | else if (err != LOCKED_PAGE) |
| 1055 | lock_page(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1056 | |
Jaegeuk Kim | 3bb5e2c | 2014-04-01 17:38:26 +0900 | [diff] [blame] | 1057 | if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) { |
Jaegeuk Kim | aaf9607 | 2015-02-06 18:53:45 -0800 | [diff] [blame] | 1058 | ClearPageUptodate(page); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1059 | f2fs_put_page(page, 1); |
| 1060 | return ERR_PTR(-EIO); |
| 1061 | } |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1062 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1063 | f2fs_put_page(page, 1); |
| 1064 | goto repeat; |
| 1065 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1066 | return page; |
| 1067 | } |
| 1068 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1069 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1070 | * Return a locked page for the desired node page. |
| 1071 | * And, readahead MAX_RA_NODE number of node pages. |
| 1072 | */ |
| 1073 | struct page *get_node_page_ra(struct page *parent, int start) |
| 1074 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1075 | struct f2fs_sb_info *sbi = F2FS_P_SB(parent); |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 1076 | struct blk_plug plug; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1077 | struct page *page; |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1078 | int err, i, end; |
| 1079 | nid_t nid; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1080 | |
| 1081 | /* First, try getting the desired direct node. */ |
| 1082 | nid = get_nid(parent, start, false); |
| 1083 | if (!nid) |
| 1084 | return ERR_PTR(-ENOENT); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1085 | repeat: |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1086 | page = grab_cache_page(NODE_MAPPING(sbi), nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1087 | if (!page) |
| 1088 | return ERR_PTR(-ENOMEM); |
| 1089 | |
Jaegeuk Kim | 66d36a2 | 2013-02-26 12:43:46 +0900 | [diff] [blame] | 1090 | err = read_node_page(page, READ_SYNC); |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1091 | if (err < 0) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1092 | return ERR_PTR(err); |
Jaegeuk Kim | 56ae674 | 2013-03-31 12:47:20 +0900 | [diff] [blame] | 1093 | else if (err == LOCKED_PAGE) |
| 1094 | goto page_hit; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1095 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 1096 | blk_start_plug(&plug); |
| 1097 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1098 | /* Then, try readahead for siblings of the desired node */ |
| 1099 | end = start + MAX_RA_NODE; |
| 1100 | end = min(end, NIDS_PER_BLOCK); |
| 1101 | for (i = start + 1; i < end; i++) { |
| 1102 | nid = get_nid(parent, i, false); |
| 1103 | if (!nid) |
| 1104 | continue; |
| 1105 | ra_node_page(sbi, nid); |
| 1106 | } |
| 1107 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 1108 | blk_finish_plug(&plug); |
| 1109 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1110 | lock_page(page); |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1111 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1112 | f2fs_put_page(page, 1); |
| 1113 | goto repeat; |
| 1114 | } |
Namjae Jeon | e0f56cb | 2013-02-02 23:51:51 +0900 | [diff] [blame] | 1115 | page_hit: |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1116 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1117 | f2fs_put_page(page, 1); |
| 1118 | return ERR_PTR(-EIO); |
| 1119 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1120 | return page; |
| 1121 | } |
| 1122 | |
| 1123 | void sync_inode_page(struct dnode_of_data *dn) |
| 1124 | { |
| 1125 | if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) { |
| 1126 | update_inode(dn->inode, dn->node_page); |
| 1127 | } else if (dn->inode_page) { |
| 1128 | if (!dn->inode_page_locked) |
| 1129 | lock_page(dn->inode_page); |
| 1130 | update_inode(dn->inode, dn->inode_page); |
| 1131 | if (!dn->inode_page_locked) |
| 1132 | unlock_page(dn->inode_page); |
| 1133 | } else { |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1134 | update_inode_page(dn->inode); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino, |
| 1139 | struct writeback_control *wbc) |
| 1140 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1141 | pgoff_t index, end; |
| 1142 | struct pagevec pvec; |
| 1143 | int step = ino ? 2 : 0; |
| 1144 | int nwritten = 0, wrote = 0; |
| 1145 | |
| 1146 | pagevec_init(&pvec, 0); |
| 1147 | |
| 1148 | next_step: |
| 1149 | index = 0; |
| 1150 | end = LONG_MAX; |
| 1151 | |
| 1152 | while (index <= end) { |
| 1153 | int i, nr_pages; |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1154 | nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index, |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1155 | PAGECACHE_TAG_DIRTY, |
| 1156 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); |
| 1157 | if (nr_pages == 0) |
| 1158 | break; |
| 1159 | |
| 1160 | for (i = 0; i < nr_pages; i++) { |
| 1161 | struct page *page = pvec.pages[i]; |
| 1162 | |
| 1163 | /* |
| 1164 | * flushing sequence with step: |
| 1165 | * 0. indirect nodes |
| 1166 | * 1. dentry dnodes |
| 1167 | * 2. file dnodes |
| 1168 | */ |
| 1169 | if (step == 0 && IS_DNODE(page)) |
| 1170 | continue; |
| 1171 | if (step == 1 && (!IS_DNODE(page) || |
| 1172 | is_cold_node(page))) |
| 1173 | continue; |
| 1174 | if (step == 2 && (!IS_DNODE(page) || |
| 1175 | !is_cold_node(page))) |
| 1176 | continue; |
| 1177 | |
| 1178 | /* |
| 1179 | * If an fsync mode, |
| 1180 | * we should not skip writing node pages. |
| 1181 | */ |
| 1182 | if (ino && ino_of_node(page) == ino) |
| 1183 | lock_page(page); |
| 1184 | else if (!trylock_page(page)) |
| 1185 | continue; |
| 1186 | |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1187 | if (unlikely(page->mapping != NODE_MAPPING(sbi))) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1188 | continue_unlock: |
| 1189 | unlock_page(page); |
| 1190 | continue; |
| 1191 | } |
| 1192 | if (ino && ino_of_node(page) != ino) |
| 1193 | goto continue_unlock; |
| 1194 | |
| 1195 | if (!PageDirty(page)) { |
| 1196 | /* someone wrote it for us */ |
| 1197 | goto continue_unlock; |
| 1198 | } |
| 1199 | |
| 1200 | if (!clear_page_dirty_for_io(page)) |
| 1201 | goto continue_unlock; |
| 1202 | |
| 1203 | /* called by fsync() */ |
| 1204 | if (ino && IS_DNODE(page)) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1205 | set_fsync_mark(page, 1); |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 1206 | if (IS_INODE(page)) { |
| 1207 | if (!is_checkpointed_node(sbi, ino) && |
| 1208 | !has_fsynced_inode(sbi, ino)) |
| 1209 | set_dentry_mark(page, 1); |
| 1210 | else |
| 1211 | set_dentry_mark(page, 0); |
| 1212 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1213 | nwritten++; |
| 1214 | } else { |
| 1215 | set_fsync_mark(page, 0); |
| 1216 | set_dentry_mark(page, 0); |
| 1217 | } |
Jaegeuk Kim | 5274651 | 2014-08-11 18:18:36 -0700 | [diff] [blame] | 1218 | |
| 1219 | if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc)) |
| 1220 | unlock_page(page); |
| 1221 | else |
| 1222 | wrote++; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1223 | |
| 1224 | if (--wbc->nr_to_write == 0) |
| 1225 | break; |
| 1226 | } |
| 1227 | pagevec_release(&pvec); |
| 1228 | cond_resched(); |
| 1229 | |
| 1230 | if (wbc->nr_to_write == 0) { |
| 1231 | step = 2; |
| 1232 | break; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | if (step < 2) { |
| 1237 | step++; |
| 1238 | goto next_step; |
| 1239 | } |
| 1240 | |
| 1241 | if (wrote) |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1242 | f2fs_submit_merged_bio(sbi, NODE, WRITE); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1243 | return nwritten; |
| 1244 | } |
| 1245 | |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 1246 | int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino) |
| 1247 | { |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 1248 | pgoff_t index = 0, end = LONG_MAX; |
| 1249 | struct pagevec pvec; |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 1250 | int ret2 = 0, ret = 0; |
| 1251 | |
| 1252 | pagevec_init(&pvec, 0); |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1253 | |
| 1254 | while (index <= end) { |
| 1255 | int i, nr_pages; |
| 1256 | nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index, |
| 1257 | PAGECACHE_TAG_WRITEBACK, |
| 1258 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); |
| 1259 | if (nr_pages == 0) |
| 1260 | break; |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 1261 | |
| 1262 | for (i = 0; i < nr_pages; i++) { |
| 1263 | struct page *page = pvec.pages[i]; |
| 1264 | |
| 1265 | /* until radix tree lookup accepts end_index */ |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 1266 | if (unlikely(page->index > end)) |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 1267 | continue; |
| 1268 | |
Chao Yu | 4bf08ff | 2013-11-04 10:28:33 +0800 | [diff] [blame] | 1269 | if (ino && ino_of_node(page) == ino) { |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 1270 | f2fs_wait_on_page_writeback(page, NODE); |
Chao Yu | 4bf08ff | 2013-11-04 10:28:33 +0800 | [diff] [blame] | 1271 | if (TestClearPageError(page)) |
| 1272 | ret = -EIO; |
| 1273 | } |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 1274 | } |
| 1275 | pagevec_release(&pvec); |
| 1276 | cond_resched(); |
| 1277 | } |
| 1278 | |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1279 | if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags))) |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 1280 | ret2 = -ENOSPC; |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1281 | if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags))) |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 1282 | ret2 = -EIO; |
| 1283 | if (!ret) |
| 1284 | ret = ret2; |
| 1285 | return ret; |
| 1286 | } |
| 1287 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1288 | static int f2fs_write_node_page(struct page *page, |
| 1289 | struct writeback_control *wbc) |
| 1290 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1291 | struct f2fs_sb_info *sbi = F2FS_P_SB(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1292 | nid_t nid; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1293 | struct node_info ni; |
Jaegeuk Kim | fb5566d | 2014-01-08 10:09:51 +0900 | [diff] [blame] | 1294 | struct f2fs_io_info fio = { |
| 1295 | .type = NODE, |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 1296 | .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE, |
Jaegeuk Kim | fb5566d | 2014-01-08 10:09:51 +0900 | [diff] [blame] | 1297 | }; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1298 | |
Chao Yu | ecda0de | 2014-05-06 16:48:26 +0800 | [diff] [blame] | 1299 | trace_f2fs_writepage(page, NODE); |
| 1300 | |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 1301 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
Jaegeuk Kim | 87a9bd2 | 2013-10-16 15:09:26 +0900 | [diff] [blame] | 1302 | goto redirty_out; |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1303 | if (unlikely(f2fs_cp_error(sbi))) |
| 1304 | goto redirty_out; |
Jaegeuk Kim | 87a9bd2 | 2013-10-16 15:09:26 +0900 | [diff] [blame] | 1305 | |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 1306 | f2fs_wait_on_page_writeback(page, NODE); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1307 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1308 | /* get old block addr of this node page */ |
| 1309 | nid = nid_of_node(page); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1310 | f2fs_bug_on(sbi, page->index != nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1311 | |
| 1312 | get_node_info(sbi, nid, &ni); |
| 1313 | |
| 1314 | /* This page is already truncated */ |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1315 | if (unlikely(ni.blk_addr == NULL_ADDR)) { |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1316 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1317 | unlock_page(page); |
| 1318 | return 0; |
| 1319 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1320 | |
Jaegeuk Kim | 2f97c32 | 2014-11-06 17:21:24 -0800 | [diff] [blame] | 1321 | if (wbc->for_reclaim) { |
| 1322 | if (!down_read_trylock(&sbi->node_write)) |
| 1323 | goto redirty_out; |
| 1324 | } else { |
| 1325 | down_read(&sbi->node_write); |
| 1326 | } |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1327 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1328 | set_page_writeback(page); |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1329 | fio.blk_addr = ni.blk_addr; |
| 1330 | write_node_page(sbi, page, nid, &fio); |
| 1331 | set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1332 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
Chao Yu | b3582c6 | 2014-07-03 18:58:39 +0800 | [diff] [blame] | 1333 | up_read(&sbi->node_write); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1334 | unlock_page(page); |
Jaegeuk Kim | 27c6bd6 | 2014-11-19 10:54:48 -0800 | [diff] [blame] | 1335 | |
| 1336 | if (wbc->for_reclaim) |
| 1337 | f2fs_submit_merged_bio(sbi, NODE, WRITE); |
| 1338 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1339 | return 0; |
Jaegeuk Kim | 87a9bd2 | 2013-10-16 15:09:26 +0900 | [diff] [blame] | 1340 | |
| 1341 | redirty_out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1342 | redirty_page_for_writepage(wbc, page); |
Jaegeuk Kim | 87a9bd2 | 2013-10-16 15:09:26 +0900 | [diff] [blame] | 1343 | return AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | static int f2fs_write_node_pages(struct address_space *mapping, |
| 1347 | struct writeback_control *wbc) |
| 1348 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1349 | struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1350 | long diff; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1351 | |
Chao Yu | e574843 | 2014-05-06 16:51:24 +0800 | [diff] [blame] | 1352 | trace_f2fs_writepages(mapping->host, wbc, NODE); |
| 1353 | |
Jaegeuk Kim | 4660f9c | 2013-10-24 14:19:18 +0900 | [diff] [blame] | 1354 | /* balancing f2fs's metadata in background */ |
| 1355 | f2fs_balance_fs_bg(sbi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1356 | |
Jaegeuk Kim | a7fdffb | 2013-01-18 14:54:13 +0900 | [diff] [blame] | 1357 | /* collect a number of dirty node pages and write together */ |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 1358 | 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] | 1359 | goto skip_write; |
Jaegeuk Kim | a7fdffb | 2013-01-18 14:54:13 +0900 | [diff] [blame] | 1360 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1361 | diff = nr_pages_to_write(sbi, NODE, wbc); |
Jaegeuk Kim | fb5566d | 2014-01-08 10:09:51 +0900 | [diff] [blame] | 1362 | wbc->sync_mode = WB_SYNC_NONE; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1363 | sync_node_pages(sbi, 0, wbc); |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1364 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1365 | return 0; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1366 | |
| 1367 | skip_write: |
| 1368 | wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES); |
| 1369 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | static int f2fs_set_node_page_dirty(struct page *page) |
| 1373 | { |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 1374 | trace_f2fs_set_page_dirty(page, NODE); |
| 1375 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1376 | SetPageUptodate(page); |
| 1377 | if (!PageDirty(page)) { |
| 1378 | __set_page_dirty_nobuffers(page); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1379 | inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1380 | SetPagePrivate(page); |
Jaegeuk Kim | 9e4ded3 | 2014-12-17 19:58:58 -0800 | [diff] [blame] | 1381 | f2fs_trace_pid(page); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1382 | return 1; |
| 1383 | } |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1387 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1388 | * Structure of the f2fs node operations |
| 1389 | */ |
| 1390 | const struct address_space_operations f2fs_node_aops = { |
| 1391 | .writepage = f2fs_write_node_page, |
| 1392 | .writepages = f2fs_write_node_pages, |
| 1393 | .set_page_dirty = f2fs_set_node_page_dirty, |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1394 | .invalidatepage = f2fs_invalidate_page, |
| 1395 | .releasepage = f2fs_release_page, |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1396 | }; |
| 1397 | |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1398 | static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i, |
| 1399 | nid_t n) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1400 | { |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1401 | return radix_tree_lookup(&nm_i->free_nid_root, n); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1402 | } |
| 1403 | |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1404 | static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i, |
| 1405 | struct free_nid *i) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1406 | { |
| 1407 | list_del(&i->list); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1408 | radix_tree_delete(&nm_i->free_nid_root, i->nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1409 | } |
| 1410 | |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1411 | static int 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] | 1412 | { |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1413 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1414 | struct free_nid *i; |
Jaegeuk Kim | 59bbd47 | 2013-05-07 20:47:40 +0900 | [diff] [blame] | 1415 | struct nat_entry *ne; |
| 1416 | bool allocated = false; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1417 | |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1418 | if (!available_free_memory(sbi, FREE_NIDS)) |
Haicheng Li | 23d3884 | 2013-05-06 23:15:43 +0800 | [diff] [blame] | 1419 | return -1; |
Jaegeuk Kim | 9198ace | 2013-04-25 13:21:12 +0900 | [diff] [blame] | 1420 | |
| 1421 | /* 0 nid should not be used */ |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 1422 | if (unlikely(nid == 0)) |
Jaegeuk Kim | 9198ace | 2013-04-25 13:21:12 +0900 | [diff] [blame] | 1423 | return 0; |
Jaegeuk Kim | 59bbd47 | 2013-05-07 20:47:40 +0900 | [diff] [blame] | 1424 | |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 1425 | if (build) { |
| 1426 | /* do not add allocated nids */ |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 1427 | down_read(&nm_i->nat_tree_lock); |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 1428 | ne = __lookup_nat_cache(nm_i, nid); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1429 | if (ne && |
Jaegeuk Kim | 7ef35e3 | 2014-09-15 12:07:13 -0700 | [diff] [blame] | 1430 | (!get_nat_flag(ne, IS_CHECKPOINTED) || |
| 1431 | nat_get_blkaddr(ne) != NULL_ADDR)) |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 1432 | allocated = true; |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 1433 | up_read(&nm_i->nat_tree_lock); |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 1434 | if (allocated) |
| 1435 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1436 | } |
Gu Zheng | 7bd5938 | 2013-10-22 14:52:26 +0800 | [diff] [blame] | 1437 | |
| 1438 | i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1439 | i->nid = nid; |
| 1440 | i->state = NID_NEW; |
| 1441 | |
Jaegeuk Kim | 769ec6e | 2014-12-03 20:47:26 -0800 | [diff] [blame] | 1442 | if (radix_tree_preload(GFP_NOFS)) { |
| 1443 | kmem_cache_free(free_nid_slab, i); |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1447 | spin_lock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1448 | if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1449 | spin_unlock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 769ec6e | 2014-12-03 20:47:26 -0800 | [diff] [blame] | 1450 | radix_tree_preload_end(); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1451 | kmem_cache_free(free_nid_slab, i); |
| 1452 | return 0; |
| 1453 | } |
| 1454 | list_add_tail(&i->list, &nm_i->free_nid_list); |
| 1455 | nm_i->fcnt++; |
| 1456 | spin_unlock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 769ec6e | 2014-12-03 20:47:26 -0800 | [diff] [blame] | 1457 | radix_tree_preload_end(); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1458 | return 1; |
| 1459 | } |
| 1460 | |
| 1461 | static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid) |
| 1462 | { |
| 1463 | struct free_nid *i; |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 1464 | bool need_free = false; |
| 1465 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1466 | spin_lock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1467 | i = __lookup_free_nid_list(nm_i, nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1468 | if (i && i->state == NID_NEW) { |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1469 | __del_from_free_nid_list(nm_i, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1470 | nm_i->fcnt--; |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 1471 | need_free = true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1472 | } |
| 1473 | spin_unlock(&nm_i->free_nid_list_lock); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 1474 | |
| 1475 | if (need_free) |
| 1476 | kmem_cache_free(free_nid_slab, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1477 | } |
| 1478 | |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1479 | static void scan_nat_page(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1480 | struct page *nat_page, nid_t start_nid) |
| 1481 | { |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1482 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1483 | struct f2fs_nat_block *nat_blk = page_address(nat_page); |
| 1484 | block_t blk_addr; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1485 | int i; |
| 1486 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1487 | i = start_nid % NAT_ENTRY_PER_BLOCK; |
| 1488 | |
| 1489 | for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) { |
Haicheng Li | 23d3884 | 2013-05-06 23:15:43 +0800 | [diff] [blame] | 1490 | |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 1491 | if (unlikely(start_nid >= nm_i->max_nid)) |
Jaegeuk Kim | 04431c4 | 2013-03-16 08:34:37 +0900 | [diff] [blame] | 1492 | break; |
Haicheng Li | 23d3884 | 2013-05-06 23:15:43 +0800 | [diff] [blame] | 1493 | |
| 1494 | blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1495 | f2fs_bug_on(sbi, blk_addr == NEW_ADDR); |
Haicheng Li | 23d3884 | 2013-05-06 23:15:43 +0800 | [diff] [blame] | 1496 | if (blk_addr == NULL_ADDR) { |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1497 | if (add_free_nid(sbi, start_nid, true) < 0) |
Haicheng Li | 23d3884 | 2013-05-06 23:15:43 +0800 | [diff] [blame] | 1498 | break; |
| 1499 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1500 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | static void build_free_nids(struct f2fs_sb_info *sbi) |
| 1504 | { |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1505 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 1506 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
| 1507 | struct f2fs_summary_block *sum = curseg->sum_blk; |
Haicheng Li | 8760952 | 2013-05-06 23:15:42 +0800 | [diff] [blame] | 1508 | int i = 0; |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1509 | nid_t nid = nm_i->next_scan_nid; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1510 | |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1511 | /* Enough entries */ |
| 1512 | if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK) |
| 1513 | return; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1514 | |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1515 | /* readahead nat pages to be scanned */ |
Chao Yu | 662befd | 2014-02-07 16:11:53 +0800 | [diff] [blame] | 1516 | ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1517 | |
| 1518 | while (1) { |
| 1519 | struct page *page = get_current_nat_page(sbi, nid); |
| 1520 | |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1521 | scan_nat_page(sbi, page, nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1522 | f2fs_put_page(page, 1); |
| 1523 | |
| 1524 | nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK)); |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 1525 | if (unlikely(nid >= nm_i->max_nid)) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1526 | nid = 0; |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1527 | |
| 1528 | if (i++ == FREE_NID_PAGES) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1529 | break; |
| 1530 | } |
| 1531 | |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1532 | /* go to the next free nat pages to find free nids abundantly */ |
| 1533 | nm_i->next_scan_nid = nid; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1534 | |
| 1535 | /* find free nids from current sum_pages */ |
| 1536 | mutex_lock(&curseg->curseg_mutex); |
| 1537 | for (i = 0; i < nats_in_cursum(sum); i++) { |
| 1538 | block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr); |
| 1539 | nid = le32_to_cpu(nid_in_journal(sum, i)); |
| 1540 | if (addr == NULL_ADDR) |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1541 | add_free_nid(sbi, nid, true); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1542 | else |
| 1543 | remove_free_nid(nm_i, nid); |
| 1544 | } |
| 1545 | mutex_unlock(&curseg->curseg_mutex); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * If this function returns success, caller can obtain a new nid |
| 1550 | * from second parameter of this function. |
| 1551 | * The returned nid could be used ino as well as nid when inode is created. |
| 1552 | */ |
| 1553 | bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid) |
| 1554 | { |
| 1555 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 1556 | struct free_nid *i = NULL; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1557 | retry: |
Jaegeuk Kim | 7ee0eea | 2014-04-18 11:14:37 +0900 | [diff] [blame] | 1558 | if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids)) |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1559 | return false; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1560 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1561 | spin_lock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1562 | |
| 1563 | /* We should not use stale free nids created by build_free_nids */ |
Gu Zheng | f978f5a | 2014-02-21 18:08:29 +0800 | [diff] [blame] | 1564 | if (nm_i->fcnt && !on_build_free_nids(nm_i)) { |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1565 | f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list)); |
Chao Yu | 2d7b822 | 2014-03-29 11:33:17 +0800 | [diff] [blame] | 1566 | list_for_each_entry(i, &nm_i->free_nid_list, list) |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1567 | if (i->state == NID_NEW) |
| 1568 | break; |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1569 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1570 | f2fs_bug_on(sbi, i->state != NID_NEW); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1571 | *nid = i->nid; |
| 1572 | i->state = NID_ALLOC; |
| 1573 | nm_i->fcnt--; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1574 | spin_unlock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1575 | return true; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1576 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1577 | spin_unlock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1578 | |
| 1579 | /* Let's scan nat pages and its caches to get free nids */ |
| 1580 | mutex_lock(&nm_i->build_lock); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1581 | build_free_nids(sbi); |
Jaegeuk Kim | 55008d8 | 2013-04-25 16:05:51 +0900 | [diff] [blame] | 1582 | mutex_unlock(&nm_i->build_lock); |
| 1583 | goto retry; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1584 | } |
| 1585 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1586 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1587 | * alloc_nid() should be called prior to this function. |
| 1588 | */ |
| 1589 | void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid) |
| 1590 | { |
| 1591 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 1592 | struct free_nid *i; |
| 1593 | |
| 1594 | spin_lock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1595 | i = __lookup_free_nid_list(nm_i, nid); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1596 | f2fs_bug_on(sbi, !i || i->state != NID_ALLOC); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1597 | __del_from_free_nid_list(nm_i, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1598 | spin_unlock(&nm_i->free_nid_list_lock); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 1599 | |
| 1600 | kmem_cache_free(free_nid_slab, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1601 | } |
| 1602 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1603 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1604 | * alloc_nid() should be called prior to this function. |
| 1605 | */ |
| 1606 | void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid) |
| 1607 | { |
Jaegeuk Kim | 49952fa | 2013-04-03 22:19:03 +0900 | [diff] [blame] | 1608 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 1609 | struct free_nid *i; |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 1610 | bool need_free = false; |
Jaegeuk Kim | 49952fa | 2013-04-03 22:19:03 +0900 | [diff] [blame] | 1611 | |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 1612 | if (!nid) |
| 1613 | return; |
| 1614 | |
Jaegeuk Kim | 49952fa | 2013-04-03 22:19:03 +0900 | [diff] [blame] | 1615 | spin_lock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1616 | i = __lookup_free_nid_list(nm_i, nid); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1617 | f2fs_bug_on(sbi, !i || i->state != NID_ALLOC); |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 1618 | if (!available_free_memory(sbi, FREE_NIDS)) { |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1619 | __del_from_free_nid_list(nm_i, i); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 1620 | need_free = true; |
Haicheng Li | 95630cb | 2013-05-06 23:15:41 +0800 | [diff] [blame] | 1621 | } else { |
| 1622 | i->state = NID_NEW; |
| 1623 | nm_i->fcnt++; |
| 1624 | } |
Jaegeuk Kim | 49952fa | 2013-04-03 22:19:03 +0900 | [diff] [blame] | 1625 | spin_unlock(&nm_i->free_nid_list_lock); |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 1626 | |
| 1627 | if (need_free) |
| 1628 | kmem_cache_free(free_nid_slab, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1629 | } |
| 1630 | |
Chao Yu | 70cfed8 | 2014-08-02 15:26:04 +0800 | [diff] [blame] | 1631 | void recover_inline_xattr(struct inode *inode, struct page *page) |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 1632 | { |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 1633 | void *src_addr, *dst_addr; |
| 1634 | size_t inline_size; |
| 1635 | struct page *ipage; |
| 1636 | struct f2fs_inode *ri; |
| 1637 | |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1638 | ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1639 | f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage)); |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 1640 | |
Jaegeuk Kim | e3b4d43 | 2014-08-07 23:45:42 -0700 | [diff] [blame] | 1641 | ri = F2FS_INODE(page); |
| 1642 | if (!(ri->i_inline & F2FS_INLINE_XATTR)) { |
| 1643 | clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR); |
| 1644 | goto update_inode; |
| 1645 | } |
| 1646 | |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 1647 | dst_addr = inline_xattr_addr(ipage); |
| 1648 | src_addr = inline_xattr_addr(page); |
| 1649 | inline_size = inline_xattr_size(inode); |
| 1650 | |
Jaegeuk Kim | 54b591d | 2014-04-29 17:28:32 +0900 | [diff] [blame] | 1651 | f2fs_wait_on_page_writeback(ipage, NODE); |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 1652 | memcpy(dst_addr, src_addr, inline_size); |
Jaegeuk Kim | e3b4d43 | 2014-08-07 23:45:42 -0700 | [diff] [blame] | 1653 | update_inode: |
Chao Yu | 28cdce0 | 2014-03-11 13:37:38 +0800 | [diff] [blame] | 1654 | update_inode(inode, ipage); |
| 1655 | f2fs_put_page(ipage, 1); |
| 1656 | } |
| 1657 | |
Jaegeuk Kim | 1c35a90 | 2014-08-07 23:49:17 -0700 | [diff] [blame] | 1658 | void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr) |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1659 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1660 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1661 | nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid; |
| 1662 | nid_t new_xnid = nid_of_node(page); |
| 1663 | struct node_info ni; |
| 1664 | |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1665 | /* 1: invalidate the previous xattr nid */ |
| 1666 | if (!prev_xnid) |
| 1667 | goto recover_xnid; |
| 1668 | |
| 1669 | /* Deallocate node address */ |
| 1670 | get_node_info(sbi, prev_xnid, &ni); |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1671 | f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1672 | invalidate_blocks(sbi, ni.blk_addr); |
| 1673 | dec_valid_node_count(sbi, inode); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 1674 | set_node_addr(sbi, &ni, NULL_ADDR, false); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1675 | |
| 1676 | recover_xnid: |
| 1677 | /* 2: allocate new xattr nid */ |
| 1678 | if (unlikely(!inc_valid_node_count(sbi, inode))) |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1679 | f2fs_bug_on(sbi, 1); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1680 | |
| 1681 | remove_free_nid(NM_I(sbi), new_xnid); |
| 1682 | get_node_info(sbi, new_xnid, &ni); |
| 1683 | ni.ino = inode->i_ino; |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 1684 | set_node_addr(sbi, &ni, NEW_ADDR, false); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1685 | F2FS_I(inode)->i_xattr_nid = new_xnid; |
| 1686 | |
| 1687 | /* 3: update xattr blkaddr */ |
| 1688 | refresh_sit_entry(sbi, NEW_ADDR, blkaddr); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 1689 | set_node_addr(sbi, &ni, blkaddr, false); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1690 | |
| 1691 | update_inode_page(inode); |
Jaegeuk Kim | abb2366 | 2014-01-28 12:25:06 +0900 | [diff] [blame] | 1692 | } |
| 1693 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1694 | int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page) |
| 1695 | { |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1696 | struct f2fs_inode *src, *dst; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1697 | nid_t ino = ino_of_node(page); |
| 1698 | struct node_info old_ni, new_ni; |
| 1699 | struct page *ipage; |
| 1700 | |
Jaegeuk Kim | e8271fa | 2014-04-18 15:21:04 +0900 | [diff] [blame] | 1701 | get_node_info(sbi, ino, &old_ni); |
| 1702 | |
| 1703 | if (unlikely(old_ni.blk_addr != NULL_ADDR)) |
| 1704 | return -EINVAL; |
| 1705 | |
Jaegeuk Kim | 4ef51a8 | 2014-01-21 18:51:16 +0900 | [diff] [blame] | 1706 | ipage = grab_cache_page(NODE_MAPPING(sbi), ino); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1707 | if (!ipage) |
| 1708 | return -ENOMEM; |
| 1709 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1710 | /* Should not use this inode from free nid list */ |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1711 | remove_free_nid(NM_I(sbi), ino); |
| 1712 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1713 | SetPageUptodate(ipage); |
| 1714 | fill_node_footer(ipage, ino, ino, 0, true); |
| 1715 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1716 | src = F2FS_INODE(page); |
| 1717 | dst = F2FS_INODE(ipage); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1718 | |
Jaegeuk Kim | 58bfaf4 | 2013-12-26 16:30:41 +0900 | [diff] [blame] | 1719 | memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src); |
| 1720 | dst->i_size = 0; |
| 1721 | dst->i_blocks = cpu_to_le64(1); |
| 1722 | dst->i_links = cpu_to_le32(1); |
| 1723 | dst->i_xattr_nid = 0; |
Jaegeuk Kim | 617deb8 | 2014-08-07 17:04:24 -0700 | [diff] [blame] | 1724 | dst->i_inline = src->i_inline & F2FS_INLINE_XATTR; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1725 | |
| 1726 | new_ni = old_ni; |
| 1727 | new_ni.ino = ino; |
| 1728 | |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 1729 | if (unlikely(!inc_valid_node_count(sbi, NULL))) |
Jaegeuk Kim | 65e5cd0 | 2013-05-14 15:47:43 +0900 | [diff] [blame] | 1730 | WARN_ON(1); |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 1731 | set_node_addr(sbi, &new_ni, NEW_ADDR, false); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1732 | inc_valid_inode_count(sbi); |
Jaegeuk Kim | 617deb8 | 2014-08-07 17:04:24 -0700 | [diff] [blame] | 1733 | set_page_dirty(ipage); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1734 | f2fs_put_page(ipage, 1); |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | int restore_node_summary(struct f2fs_sb_info *sbi, |
| 1739 | unsigned int segno, struct f2fs_summary_block *sum) |
| 1740 | { |
| 1741 | struct f2fs_node *rn; |
| 1742 | struct f2fs_summary *sum_entry; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1743 | block_t addr; |
Jaegeuk Kim | 90a893c | 2014-09-22 16:21:07 -0700 | [diff] [blame] | 1744 | int bio_blocks = MAX_BIO_BLOCKS(sbi); |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 1745 | int i, idx, last_offset, nrpages; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1746 | |
| 1747 | /* scan the node segment */ |
| 1748 | last_offset = sbi->blocks_per_seg; |
| 1749 | addr = START_BLOCK(sbi, segno); |
| 1750 | sum_entry = &sum->entries[0]; |
| 1751 | |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 1752 | for (i = 0; i < last_offset; i += nrpages, addr += nrpages) { |
Chao Yu | 9af0ff1 | 2013-11-22 15:48:54 +0800 | [diff] [blame] | 1753 | nrpages = min(last_offset - i, bio_blocks); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1754 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1755 | /* readahead node pages */ |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 1756 | ra_meta_pages(sbi, addr, nrpages, META_POR); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1757 | |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 1758 | for (idx = addr; idx < addr + nrpages; idx++) { |
| 1759 | struct page *page = get_meta_page(sbi, idx); |
Chao Yu | 9af0ff1 | 2013-11-22 15:48:54 +0800 | [diff] [blame] | 1760 | |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 1761 | rn = F2FS_NODE(page); |
| 1762 | sum_entry->nid = rn->footer.nid; |
| 1763 | sum_entry->version = 0; |
| 1764 | sum_entry->ofs_in_node = 0; |
| 1765 | sum_entry++; |
| 1766 | f2fs_put_page(page, 1); |
Chao Yu | 9af0ff1 | 2013-11-22 15:48:54 +0800 | [diff] [blame] | 1767 | } |
Chao Yu | bac4eef | 2014-05-27 08:41:07 +0800 | [diff] [blame] | 1768 | |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 1769 | invalidate_mapping_pages(META_MAPPING(sbi), addr, |
Chao Yu | bac4eef | 2014-05-27 08:41:07 +0800 | [diff] [blame] | 1770 | addr + nrpages); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1771 | } |
Chao Yu | 9ecf4b8 | 2014-12-18 18:29:05 +0800 | [diff] [blame] | 1772 | return 0; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1773 | } |
| 1774 | |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 1775 | static void remove_nats_in_journal(struct f2fs_sb_info *sbi) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1776 | { |
| 1777 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 1778 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
| 1779 | struct f2fs_summary_block *sum = curseg->sum_blk; |
| 1780 | int i; |
| 1781 | |
| 1782 | mutex_lock(&curseg->curseg_mutex); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1783 | for (i = 0; i < nats_in_cursum(sum); i++) { |
| 1784 | struct nat_entry *ne; |
| 1785 | struct f2fs_nat_entry raw_ne; |
| 1786 | nid_t nid = le32_to_cpu(nid_in_journal(sum, i)); |
| 1787 | |
| 1788 | raw_ne = nat_in_journal(sum, i); |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 1789 | |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 1790 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1791 | ne = __lookup_nat_cache(nm_i, nid); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1792 | if (!ne) { |
Jaegeuk Kim | 9be32d7 | 2014-12-05 10:39:49 -0800 | [diff] [blame] | 1793 | ne = grab_nat_entry(nm_i, nid); |
| 1794 | node_info_from_raw_nat(&ne->ni, &raw_ne); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1795 | } |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1796 | __set_nat_cache_dirty(nm_i, ne); |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 1797 | up_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1798 | } |
| 1799 | update_nats_in_cursum(sum, -i); |
| 1800 | mutex_unlock(&curseg->curseg_mutex); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1801 | } |
| 1802 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1803 | static void __adjust_nat_entry_set(struct nat_entry_set *nes, |
| 1804 | struct list_head *head, int max) |
| 1805 | { |
| 1806 | struct nat_entry_set *cur; |
| 1807 | |
| 1808 | if (nes->entry_cnt >= max) |
| 1809 | goto add_out; |
| 1810 | |
| 1811 | list_for_each_entry(cur, head, set_list) { |
| 1812 | if (cur->entry_cnt >= nes->entry_cnt) { |
| 1813 | list_add(&nes->set_list, cur->set_list.prev); |
| 1814 | return; |
| 1815 | } |
| 1816 | } |
| 1817 | add_out: |
| 1818 | list_add_tail(&nes->set_list, head); |
| 1819 | } |
| 1820 | |
| 1821 | static void __flush_nat_entry_set(struct f2fs_sb_info *sbi, |
| 1822 | struct nat_entry_set *set) |
| 1823 | { |
| 1824 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
| 1825 | struct f2fs_summary_block *sum = curseg->sum_blk; |
| 1826 | nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK; |
| 1827 | bool to_journal = true; |
| 1828 | struct f2fs_nat_block *nat_blk; |
| 1829 | struct nat_entry *ne, *cur; |
| 1830 | struct page *page = NULL; |
| 1831 | |
| 1832 | /* |
| 1833 | * there are two steps to flush nat entries: |
| 1834 | * #1, flush nat entries to journal in current hot data summary block. |
| 1835 | * #2, flush nat entries to nat page. |
| 1836 | */ |
| 1837 | if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL)) |
| 1838 | to_journal = false; |
| 1839 | |
| 1840 | if (to_journal) { |
| 1841 | mutex_lock(&curseg->curseg_mutex); |
| 1842 | } else { |
| 1843 | page = get_next_nat_page(sbi, start_nid); |
| 1844 | nat_blk = page_address(page); |
| 1845 | f2fs_bug_on(sbi, !nat_blk); |
| 1846 | } |
| 1847 | |
| 1848 | /* flush dirty nats in nat entry set */ |
| 1849 | list_for_each_entry_safe(ne, cur, &set->entry_list, list) { |
| 1850 | struct f2fs_nat_entry *raw_ne; |
| 1851 | nid_t nid = nat_get_nid(ne); |
| 1852 | int offset; |
| 1853 | |
| 1854 | if (nat_get_blkaddr(ne) == NEW_ADDR) |
| 1855 | continue; |
| 1856 | |
| 1857 | if (to_journal) { |
| 1858 | offset = lookup_journal_in_cursum(sum, |
| 1859 | NAT_JOURNAL, nid, 1); |
| 1860 | f2fs_bug_on(sbi, offset < 0); |
| 1861 | raw_ne = &nat_in_journal(sum, offset); |
| 1862 | nid_in_journal(sum, offset) = cpu_to_le32(nid); |
| 1863 | } else { |
| 1864 | raw_ne = &nat_blk->entries[nid - start_nid]; |
| 1865 | } |
| 1866 | raw_nat_from_node_info(raw_ne, &ne->ni); |
| 1867 | |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 1868 | down_write(&NM_I(sbi)->nat_tree_lock); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1869 | nat_reset_flag(ne); |
| 1870 | __clear_nat_cache_dirty(NM_I(sbi), ne); |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 1871 | up_write(&NM_I(sbi)->nat_tree_lock); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1872 | |
| 1873 | if (nat_get_blkaddr(ne) == NULL_ADDR) |
| 1874 | add_free_nid(sbi, nid, false); |
| 1875 | } |
| 1876 | |
| 1877 | if (to_journal) |
| 1878 | mutex_unlock(&curseg->curseg_mutex); |
| 1879 | else |
| 1880 | f2fs_put_page(page, 1); |
| 1881 | |
Changman Lee | 80ec2e9 | 2014-11-25 12:44:24 +0900 | [diff] [blame] | 1882 | f2fs_bug_on(sbi, set->entry_cnt); |
| 1883 | |
| 1884 | radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set); |
| 1885 | kmem_cache_free(nat_entry_set_slab, set); |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1888 | /* |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1889 | * This function is called during the checkpointing process. |
| 1890 | */ |
| 1891 | void flush_nat_entries(struct f2fs_sb_info *sbi) |
| 1892 | { |
| 1893 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 1894 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); |
| 1895 | struct f2fs_summary_block *sum = curseg->sum_blk; |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 1896 | struct nat_entry_set *setvec[SETVEC_SIZE]; |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1897 | struct nat_entry_set *set, *tmp; |
| 1898 | unsigned int found; |
| 1899 | nid_t set_idx = 0; |
| 1900 | LIST_HEAD(sets); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1901 | |
Changman Lee | 20d047c | 2014-11-25 12:44:23 +0900 | [diff] [blame] | 1902 | if (!nm_i->dirty_nat_cnt) |
| 1903 | return; |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 1904 | /* |
| 1905 | * if there are no enough space in journal to store dirty nat |
| 1906 | * entries, remove all entries from journal and merge them |
| 1907 | * into nat entry set. |
| 1908 | */ |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1909 | if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL)) |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 1910 | remove_nats_in_journal(sbi); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1911 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1912 | while ((found = __gang_lookup_nat_set(nm_i, |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 1913 | set_idx, SETVEC_SIZE, setvec))) { |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1914 | unsigned idx; |
| 1915 | set_idx = setvec[found - 1]->set + 1; |
| 1916 | for (idx = 0; idx < found; idx++) |
| 1917 | __adjust_nat_entry_set(setvec[idx], &sets, |
| 1918 | MAX_NAT_JENTRIES(sum)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1919 | } |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 1920 | |
Jaegeuk Kim | 309cc2b | 2014-09-22 11:40:48 -0700 | [diff] [blame] | 1921 | /* flush dirty nats in nat entry set */ |
| 1922 | list_for_each_entry_safe(set, tmp, &sets, set_list) |
| 1923 | __flush_nat_entry_set(sbi, set); |
| 1924 | |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 1925 | f2fs_bug_on(sbi, nm_i->dirty_nat_cnt); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | static int init_node_manager(struct f2fs_sb_info *sbi) |
| 1929 | { |
| 1930 | struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi); |
| 1931 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 1932 | unsigned char *version_bitmap; |
| 1933 | unsigned int nat_segs, nat_blocks; |
| 1934 | |
| 1935 | nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr); |
| 1936 | |
| 1937 | /* segment_count_nat includes pair segment so divide to 2. */ |
| 1938 | nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1; |
| 1939 | nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg); |
Jaegeuk Kim | b63da15 | 2014-02-17 12:44:20 +0900 | [diff] [blame] | 1940 | |
Jaegeuk Kim | 7ee0eea | 2014-04-18 11:14:37 +0900 | [diff] [blame] | 1941 | nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks; |
| 1942 | |
Jaegeuk Kim | b63da15 | 2014-02-17 12:44:20 +0900 | [diff] [blame] | 1943 | /* not used nids: 0, node, meta, (and root counted as valid node) */ |
Chao Yu | c200b1a | 2014-08-20 18:36:46 +0800 | [diff] [blame] | 1944 | nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1945 | nm_i->fcnt = 0; |
| 1946 | nm_i->nat_cnt = 0; |
Jaegeuk Kim | cdfc41c | 2014-03-19 13:31:37 +0900 | [diff] [blame] | 1947 | nm_i->ram_thresh = DEF_RAM_THRESHOLD; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1948 | |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 1949 | INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1950 | INIT_LIST_HEAD(&nm_i->free_nid_list); |
Jaegeuk Kim | 769ec6e | 2014-12-03 20:47:26 -0800 | [diff] [blame] | 1951 | INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO); |
| 1952 | INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1953 | INIT_LIST_HEAD(&nm_i->nat_entries); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1954 | |
| 1955 | mutex_init(&nm_i->build_lock); |
| 1956 | spin_lock_init(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 1957 | init_rwsem(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1958 | |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1959 | 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] | 1960 | nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1961 | version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP); |
| 1962 | if (!version_bitmap) |
| 1963 | return -EFAULT; |
| 1964 | |
Alexandru Gheorghiu | 79b5793 | 2013-03-28 02:24:53 +0200 | [diff] [blame] | 1965 | nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size, |
| 1966 | GFP_KERNEL); |
| 1967 | if (!nm_i->nat_bitmap) |
| 1968 | return -ENOMEM; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1969 | return 0; |
| 1970 | } |
| 1971 | |
| 1972 | int build_node_manager(struct f2fs_sb_info *sbi) |
| 1973 | { |
| 1974 | int err; |
| 1975 | |
| 1976 | sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL); |
| 1977 | if (!sbi->nm_info) |
| 1978 | return -ENOMEM; |
| 1979 | |
| 1980 | err = init_node_manager(sbi); |
| 1981 | if (err) |
| 1982 | return err; |
| 1983 | |
| 1984 | build_free_nids(sbi); |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
| 1988 | void destroy_node_manager(struct f2fs_sb_info *sbi) |
| 1989 | { |
| 1990 | struct f2fs_nm_info *nm_i = NM_I(sbi); |
| 1991 | struct free_nid *i, *next_i; |
| 1992 | struct nat_entry *natvec[NATVEC_SIZE]; |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 1993 | struct nat_entry_set *setvec[SETVEC_SIZE]; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 1994 | nid_t nid = 0; |
| 1995 | unsigned int found; |
| 1996 | |
| 1997 | if (!nm_i) |
| 1998 | return; |
| 1999 | |
| 2000 | /* destroy free nid list */ |
| 2001 | spin_lock(&nm_i->free_nid_list_lock); |
| 2002 | list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) { |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2003 | f2fs_bug_on(sbi, i->state == NID_ALLOC); |
Jaegeuk Kim | 8a7ed66 | 2014-02-21 14:29:35 +0900 | [diff] [blame] | 2004 | __del_from_free_nid_list(nm_i, i); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2005 | nm_i->fcnt--; |
Chao Yu | cf0ee0f | 2014-04-02 08:55:00 +0800 | [diff] [blame] | 2006 | spin_unlock(&nm_i->free_nid_list_lock); |
| 2007 | kmem_cache_free(free_nid_slab, i); |
| 2008 | spin_lock(&nm_i->free_nid_list_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2009 | } |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2010 | f2fs_bug_on(sbi, nm_i->fcnt); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2011 | spin_unlock(&nm_i->free_nid_list_lock); |
| 2012 | |
| 2013 | /* destroy nat cache */ |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 2014 | down_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2015 | while ((found = __gang_lookup_nat_cache(nm_i, |
| 2016 | nid, NATVEC_SIZE, natvec))) { |
| 2017 | unsigned idx; |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 2018 | |
Gu Zheng | b6ce391 | 2014-03-07 18:43:24 +0800 | [diff] [blame] | 2019 | nid = nat_get_nid(natvec[found - 1]) + 1; |
| 2020 | for (idx = 0; idx < found; idx++) |
| 2021 | __del_from_nat_cache(nm_i, natvec[idx]); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2022 | } |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 2023 | f2fs_bug_on(sbi, nm_i->nat_cnt); |
Jaegeuk Kim | 7aed0d4 | 2015-01-07 10:47:57 -0800 | [diff] [blame] | 2024 | |
| 2025 | /* destroy nat set cache */ |
| 2026 | nid = 0; |
| 2027 | while ((found = __gang_lookup_nat_set(nm_i, |
| 2028 | nid, SETVEC_SIZE, setvec))) { |
| 2029 | unsigned idx; |
| 2030 | |
| 2031 | nid = setvec[found - 1]->set + 1; |
| 2032 | for (idx = 0; idx < found; idx++) { |
| 2033 | /* entry_cnt is not zero, when cp_error was occurred */ |
| 2034 | f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list)); |
| 2035 | radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set); |
| 2036 | kmem_cache_free(nat_entry_set_slab, setvec[idx]); |
| 2037 | } |
| 2038 | } |
Jaegeuk Kim | 8b26ef9 | 2014-12-03 21:15:10 -0800 | [diff] [blame] | 2039 | up_write(&nm_i->nat_tree_lock); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2040 | |
| 2041 | kfree(nm_i->nat_bitmap); |
| 2042 | sbi->nm_info = NULL; |
| 2043 | kfree(nm_i); |
| 2044 | } |
| 2045 | |
Namjae Jeon | 6e6093a | 2013-01-17 00:08:30 +0900 | [diff] [blame] | 2046 | int __init create_node_manager_caches(void) |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2047 | { |
| 2048 | nat_entry_slab = f2fs_kmem_cache_create("nat_entry", |
Gu Zheng | e8512d2 | 2014-03-07 18:43:28 +0800 | [diff] [blame] | 2049 | sizeof(struct nat_entry)); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2050 | if (!nat_entry_slab) |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 2051 | goto fail; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2052 | |
| 2053 | free_nid_slab = f2fs_kmem_cache_create("free_nid", |
Gu Zheng | e8512d2 | 2014-03-07 18:43:28 +0800 | [diff] [blame] | 2054 | sizeof(struct free_nid)); |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 2055 | if (!free_nid_slab) |
Markus Elfring | ce3e6d2 | 2014-11-24 15:52:00 +0100 | [diff] [blame] | 2056 | goto destroy_nat_entry; |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 2057 | |
| 2058 | nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set", |
| 2059 | sizeof(struct nat_entry_set)); |
| 2060 | if (!nat_entry_set_slab) |
Markus Elfring | ce3e6d2 | 2014-11-24 15:52:00 +0100 | [diff] [blame] | 2061 | goto destroy_free_nid; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2062 | return 0; |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 2063 | |
Markus Elfring | ce3e6d2 | 2014-11-24 15:52:00 +0100 | [diff] [blame] | 2064 | destroy_free_nid: |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 2065 | kmem_cache_destroy(free_nid_slab); |
Markus Elfring | ce3e6d2 | 2014-11-24 15:52:00 +0100 | [diff] [blame] | 2066 | destroy_nat_entry: |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 2067 | kmem_cache_destroy(nat_entry_slab); |
| 2068 | fail: |
| 2069 | return -ENOMEM; |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | void destroy_node_manager_caches(void) |
| 2073 | { |
Chao Yu | aec7138 | 2014-06-24 09:18:20 +0800 | [diff] [blame] | 2074 | kmem_cache_destroy(nat_entry_set_slab); |
Jaegeuk Kim | e05df3b | 2012-11-02 17:08:50 +0900 | [diff] [blame] | 2075 | kmem_cache_destroy(free_nid_slab); |
| 2076 | kmem_cache_destroy(nat_entry_slab); |
| 2077 | } |