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