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