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