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