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