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