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