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