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