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