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