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