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