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