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