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