]>
Commit | Line | Data |
---|---|---|
0a8165d7 | 1 | /* |
351df4b2 JK |
2 | * fs/f2fs/segment.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/bio.h> | |
14 | #include <linux/blkdev.h> | |
690e4a3e | 15 | #include <linux/prefetch.h> |
6b4afdd7 | 16 | #include <linux/kthread.h> |
351df4b2 | 17 | #include <linux/vmalloc.h> |
74de593a | 18 | #include <linux/swap.h> |
351df4b2 JK |
19 | |
20 | #include "f2fs.h" | |
21 | #include "segment.h" | |
22 | #include "node.h" | |
6ec178da | 23 | #include <trace/events/f2fs.h> |
351df4b2 | 24 | |
9a7f143a CL |
25 | #define __reverse_ffz(x) __reverse_ffs(~(x)) |
26 | ||
7fd9e544 | 27 | static struct kmem_cache *discard_entry_slab; |
6b4afdd7 | 28 | static struct kmem_cache *flush_cmd_slab; |
7fd9e544 | 29 | |
9a7f143a CL |
30 | /* |
31 | * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since | |
32 | * MSB and LSB are reversed in a byte by f2fs_set_bit. | |
33 | */ | |
34 | static inline unsigned long __reverse_ffs(unsigned long word) | |
35 | { | |
36 | int num = 0; | |
37 | ||
38 | #if BITS_PER_LONG == 64 | |
39 | if ((word & 0xffffffff) == 0) { | |
40 | num += 32; | |
41 | word >>= 32; | |
42 | } | |
43 | #endif | |
44 | if ((word & 0xffff) == 0) { | |
45 | num += 16; | |
46 | word >>= 16; | |
47 | } | |
48 | if ((word & 0xff) == 0) { | |
49 | num += 8; | |
50 | word >>= 8; | |
51 | } | |
52 | if ((word & 0xf0) == 0) | |
53 | num += 4; | |
54 | else | |
55 | word >>= 4; | |
56 | if ((word & 0xc) == 0) | |
57 | num += 2; | |
58 | else | |
59 | word >>= 2; | |
60 | if ((word & 0x2) == 0) | |
61 | num += 1; | |
62 | return num; | |
63 | } | |
64 | ||
65 | /* | |
66 | * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue | |
67 | * f2fs_set_bit makes MSB and LSB reversed in a byte. | |
68 | * Example: | |
69 | * LSB <--> MSB | |
70 | * f2fs_set_bit(0, bitmap) => 0000 0001 | |
71 | * f2fs_set_bit(7, bitmap) => 1000 0000 | |
72 | */ | |
73 | static unsigned long __find_rev_next_bit(const unsigned long *addr, | |
74 | unsigned long size, unsigned long offset) | |
75 | { | |
76 | const unsigned long *p = addr + BIT_WORD(offset); | |
77 | unsigned long result = offset & ~(BITS_PER_LONG - 1); | |
78 | unsigned long tmp; | |
79 | unsigned long mask, submask; | |
80 | unsigned long quot, rest; | |
81 | ||
82 | if (offset >= size) | |
83 | return size; | |
84 | ||
85 | size -= result; | |
86 | offset %= BITS_PER_LONG; | |
87 | if (!offset) | |
88 | goto aligned; | |
89 | ||
90 | tmp = *(p++); | |
91 | quot = (offset >> 3) << 3; | |
92 | rest = offset & 0x7; | |
93 | mask = ~0UL << quot; | |
94 | submask = (unsigned char)(0xff << rest) >> rest; | |
95 | submask <<= quot; | |
96 | mask &= submask; | |
97 | tmp &= mask; | |
98 | if (size < BITS_PER_LONG) | |
99 | goto found_first; | |
100 | if (tmp) | |
101 | goto found_middle; | |
102 | ||
103 | size -= BITS_PER_LONG; | |
104 | result += BITS_PER_LONG; | |
105 | aligned: | |
106 | while (size & ~(BITS_PER_LONG-1)) { | |
107 | tmp = *(p++); | |
108 | if (tmp) | |
109 | goto found_middle; | |
110 | result += BITS_PER_LONG; | |
111 | size -= BITS_PER_LONG; | |
112 | } | |
113 | if (!size) | |
114 | return result; | |
115 | tmp = *p; | |
116 | found_first: | |
117 | tmp &= (~0UL >> (BITS_PER_LONG - size)); | |
118 | if (tmp == 0UL) /* Are any bits set? */ | |
119 | return result + size; /* Nope. */ | |
120 | found_middle: | |
121 | return result + __reverse_ffs(tmp); | |
122 | } | |
123 | ||
124 | static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, | |
125 | unsigned long size, unsigned long offset) | |
126 | { | |
127 | const unsigned long *p = addr + BIT_WORD(offset); | |
128 | unsigned long result = offset & ~(BITS_PER_LONG - 1); | |
129 | unsigned long tmp; | |
130 | unsigned long mask, submask; | |
131 | unsigned long quot, rest; | |
132 | ||
133 | if (offset >= size) | |
134 | return size; | |
135 | ||
136 | size -= result; | |
137 | offset %= BITS_PER_LONG; | |
138 | if (!offset) | |
139 | goto aligned; | |
140 | ||
141 | tmp = *(p++); | |
142 | quot = (offset >> 3) << 3; | |
143 | rest = offset & 0x7; | |
144 | mask = ~(~0UL << quot); | |
145 | submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest); | |
146 | submask <<= quot; | |
147 | mask += submask; | |
148 | tmp |= mask; | |
149 | if (size < BITS_PER_LONG) | |
150 | goto found_first; | |
151 | if (~tmp) | |
152 | goto found_middle; | |
153 | ||
154 | size -= BITS_PER_LONG; | |
155 | result += BITS_PER_LONG; | |
156 | aligned: | |
157 | while (size & ~(BITS_PER_LONG - 1)) { | |
158 | tmp = *(p++); | |
159 | if (~tmp) | |
160 | goto found_middle; | |
161 | result += BITS_PER_LONG; | |
162 | size -= BITS_PER_LONG; | |
163 | } | |
164 | if (!size) | |
165 | return result; | |
166 | tmp = *p; | |
167 | ||
168 | found_first: | |
169 | tmp |= ~0UL << size; | |
170 | if (tmp == ~0UL) /* Are any bits zero? */ | |
171 | return result + size; /* Nope. */ | |
172 | found_middle: | |
173 | return result + __reverse_ffz(tmp); | |
174 | } | |
175 | ||
0a8165d7 | 176 | /* |
351df4b2 JK |
177 | * This function balances dirty node and dentry pages. |
178 | * In addition, it controls garbage collection. | |
179 | */ | |
180 | void f2fs_balance_fs(struct f2fs_sb_info *sbi) | |
181 | { | |
351df4b2 | 182 | /* |
029cd28c JK |
183 | * We should do GC or end up with checkpoint, if there are so many dirty |
184 | * dir/node pages without enough free segments. | |
351df4b2 | 185 | */ |
43727527 | 186 | if (has_not_enough_free_secs(sbi, 0)) { |
351df4b2 | 187 | mutex_lock(&sbi->gc_mutex); |
408e9375 | 188 | f2fs_gc(sbi); |
351df4b2 JK |
189 | } |
190 | } | |
191 | ||
4660f9c0 JK |
192 | void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi) |
193 | { | |
194 | /* check the # of cached NAT entries and prefree segments */ | |
195 | if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) || | |
196 | excess_prefree_segs(sbi)) | |
197 | f2fs_sync_fs(sbi->sb, true); | |
198 | } | |
199 | ||
6b4afdd7 JK |
200 | static int issue_flush_thread(void *data) |
201 | { | |
202 | struct f2fs_sb_info *sbi = data; | |
203 | struct f2fs_sm_info *sm_i = SM_I(sbi); | |
204 | wait_queue_head_t *q = &sm_i->flush_wait_queue; | |
205 | repeat: | |
206 | if (kthread_should_stop()) | |
207 | return 0; | |
208 | ||
209 | spin_lock(&sm_i->issue_lock); | |
210 | if (sm_i->issue_list) { | |
211 | sm_i->dispatch_list = sm_i->issue_list; | |
212 | sm_i->issue_list = sm_i->issue_tail = NULL; | |
213 | } | |
214 | spin_unlock(&sm_i->issue_lock); | |
215 | ||
216 | if (sm_i->dispatch_list) { | |
217 | struct bio *bio = bio_alloc(GFP_NOIO, 0); | |
218 | struct flush_cmd *cmd, *next; | |
219 | int ret; | |
220 | ||
221 | bio->bi_bdev = sbi->sb->s_bdev; | |
222 | ret = submit_bio_wait(WRITE_FLUSH, bio); | |
223 | ||
224 | for (cmd = sm_i->dispatch_list; cmd; cmd = next) { | |
225 | cmd->ret = ret; | |
226 | next = cmd->next; | |
227 | complete(&cmd->wait); | |
228 | } | |
a4ed23f2 | 229 | bio_put(bio); |
6b4afdd7 JK |
230 | sm_i->dispatch_list = NULL; |
231 | } | |
232 | ||
233 | wait_event_interruptible(*q, kthread_should_stop() || sm_i->issue_list); | |
234 | goto repeat; | |
235 | } | |
236 | ||
237 | int f2fs_issue_flush(struct f2fs_sb_info *sbi) | |
238 | { | |
239 | struct f2fs_sm_info *sm_i = SM_I(sbi); | |
240 | struct flush_cmd *cmd; | |
241 | int ret; | |
242 | ||
243 | if (!test_opt(sbi, FLUSH_MERGE)) | |
244 | return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL); | |
245 | ||
197d4647 | 246 | cmd = f2fs_kmem_cache_alloc(flush_cmd_slab, GFP_ATOMIC | __GFP_ZERO); |
6b4afdd7 JK |
247 | init_completion(&cmd->wait); |
248 | ||
249 | spin_lock(&sm_i->issue_lock); | |
250 | if (sm_i->issue_list) | |
251 | sm_i->issue_tail->next = cmd; | |
252 | else | |
253 | sm_i->issue_list = cmd; | |
254 | sm_i->issue_tail = cmd; | |
255 | spin_unlock(&sm_i->issue_lock); | |
256 | ||
257 | if (!sm_i->dispatch_list) | |
258 | wake_up(&sm_i->flush_wait_queue); | |
259 | ||
260 | wait_for_completion(&cmd->wait); | |
261 | ret = cmd->ret; | |
262 | kmem_cache_free(flush_cmd_slab, cmd); | |
263 | return ret; | |
264 | } | |
265 | ||
351df4b2 JK |
266 | static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, |
267 | enum dirty_type dirty_type) | |
268 | { | |
269 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
270 | ||
271 | /* need not be added */ | |
272 | if (IS_CURSEG(sbi, segno)) | |
273 | return; | |
274 | ||
275 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type])) | |
276 | dirty_i->nr_dirty[dirty_type]++; | |
277 | ||
278 | if (dirty_type == DIRTY) { | |
279 | struct seg_entry *sentry = get_seg_entry(sbi, segno); | |
4625d6aa | 280 | enum dirty_type t = sentry->type; |
b2f2c390 | 281 | |
4625d6aa CL |
282 | if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t])) |
283 | dirty_i->nr_dirty[t]++; | |
351df4b2 JK |
284 | } |
285 | } | |
286 | ||
287 | static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, | |
288 | enum dirty_type dirty_type) | |
289 | { | |
290 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
291 | ||
292 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type])) | |
293 | dirty_i->nr_dirty[dirty_type]--; | |
294 | ||
295 | if (dirty_type == DIRTY) { | |
4625d6aa CL |
296 | struct seg_entry *sentry = get_seg_entry(sbi, segno); |
297 | enum dirty_type t = sentry->type; | |
298 | ||
299 | if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t])) | |
300 | dirty_i->nr_dirty[t]--; | |
b2f2c390 | 301 | |
5ec4e49f JK |
302 | if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0) |
303 | clear_bit(GET_SECNO(sbi, segno), | |
304 | dirty_i->victim_secmap); | |
351df4b2 JK |
305 | } |
306 | } | |
307 | ||
0a8165d7 | 308 | /* |
351df4b2 JK |
309 | * Should not occur error such as -ENOMEM. |
310 | * Adding dirty entry into seglist is not critical operation. | |
311 | * If a given segment is one of current working segments, it won't be added. | |
312 | */ | |
8d8451af | 313 | static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno) |
351df4b2 JK |
314 | { |
315 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
316 | unsigned short valid_blocks; | |
317 | ||
318 | if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno)) | |
319 | return; | |
320 | ||
321 | mutex_lock(&dirty_i->seglist_lock); | |
322 | ||
323 | valid_blocks = get_valid_blocks(sbi, segno, 0); | |
324 | ||
325 | if (valid_blocks == 0) { | |
326 | __locate_dirty_segment(sbi, segno, PRE); | |
327 | __remove_dirty_segment(sbi, segno, DIRTY); | |
328 | } else if (valid_blocks < sbi->blocks_per_seg) { | |
329 | __locate_dirty_segment(sbi, segno, DIRTY); | |
330 | } else { | |
331 | /* Recovery routine with SSR needs this */ | |
332 | __remove_dirty_segment(sbi, segno, DIRTY); | |
333 | } | |
334 | ||
335 | mutex_unlock(&dirty_i->seglist_lock); | |
351df4b2 JK |
336 | } |
337 | ||
37208879 JK |
338 | static void f2fs_issue_discard(struct f2fs_sb_info *sbi, |
339 | block_t blkstart, block_t blklen) | |
340 | { | |
f9a4e6df JK |
341 | sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart); |
342 | sector_t len = SECTOR_FROM_BLOCK(sbi, blklen); | |
37208879 | 343 | blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0); |
1661d07c | 344 | trace_f2fs_issue_discard(sbi->sb, blkstart, blklen); |
37208879 JK |
345 | } |
346 | ||
b2955550 JK |
347 | static void add_discard_addrs(struct f2fs_sb_info *sbi, |
348 | unsigned int segno, struct seg_entry *se) | |
349 | { | |
350 | struct list_head *head = &SM_I(sbi)->discard_list; | |
351 | struct discard_entry *new; | |
352 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); | |
353 | int max_blocks = sbi->blocks_per_seg; | |
354 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; | |
355 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; | |
356 | unsigned long dmap[entries]; | |
357 | unsigned int start = 0, end = -1; | |
358 | int i; | |
359 | ||
360 | if (!test_opt(sbi, DISCARD)) | |
361 | return; | |
362 | ||
363 | /* zero block will be discarded through the prefree list */ | |
364 | if (!se->valid_blocks || se->valid_blocks == max_blocks) | |
365 | return; | |
366 | ||
367 | /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ | |
368 | for (i = 0; i < entries; i++) | |
369 | dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; | |
370 | ||
371 | while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) { | |
372 | start = __find_rev_next_bit(dmap, max_blocks, end + 1); | |
373 | if (start >= max_blocks) | |
374 | break; | |
375 | ||
376 | end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1); | |
377 | ||
378 | new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS); | |
379 | INIT_LIST_HEAD(&new->list); | |
380 | new->blkaddr = START_BLOCK(sbi, segno) + start; | |
381 | new->len = end - start; | |
382 | ||
383 | list_add_tail(&new->list, head); | |
384 | SM_I(sbi)->nr_discards += end - start; | |
385 | } | |
386 | } | |
387 | ||
0a8165d7 | 388 | /* |
351df4b2 JK |
389 | * Should call clear_prefree_segments after checkpoint is done. |
390 | */ | |
391 | static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) | |
392 | { | |
393 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
81fb5e87 | 394 | unsigned int segno = -1; |
351df4b2 JK |
395 | unsigned int total_segs = TOTAL_SEGS(sbi); |
396 | ||
397 | mutex_lock(&dirty_i->seglist_lock); | |
398 | while (1) { | |
399 | segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs, | |
81fb5e87 | 400 | segno + 1); |
351df4b2 JK |
401 | if (segno >= total_segs) |
402 | break; | |
403 | __set_test_and_free(sbi, segno); | |
351df4b2 JK |
404 | } |
405 | mutex_unlock(&dirty_i->seglist_lock); | |
406 | } | |
407 | ||
408 | void clear_prefree_segments(struct f2fs_sb_info *sbi) | |
409 | { | |
b2955550 | 410 | struct list_head *head = &(SM_I(sbi)->discard_list); |
2d7b822a | 411 | struct discard_entry *entry, *this; |
351df4b2 | 412 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); |
29e59c14 | 413 | unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; |
351df4b2 | 414 | unsigned int total_segs = TOTAL_SEGS(sbi); |
29e59c14 | 415 | unsigned int start = 0, end = -1; |
351df4b2 JK |
416 | |
417 | mutex_lock(&dirty_i->seglist_lock); | |
29e59c14 | 418 | |
351df4b2 | 419 | while (1) { |
29e59c14 CL |
420 | int i; |
421 | start = find_next_bit(prefree_map, total_segs, end + 1); | |
422 | if (start >= total_segs) | |
351df4b2 | 423 | break; |
29e59c14 CL |
424 | end = find_next_zero_bit(prefree_map, total_segs, start + 1); |
425 | ||
426 | for (i = start; i < end; i++) | |
427 | clear_bit(i, prefree_map); | |
428 | ||
429 | dirty_i->nr_dirty[PRE] -= end - start; | |
430 | ||
431 | if (!test_opt(sbi, DISCARD)) | |
432 | continue; | |
351df4b2 | 433 | |
37208879 JK |
434 | f2fs_issue_discard(sbi, START_BLOCK(sbi, start), |
435 | (end - start) << sbi->log_blocks_per_seg); | |
351df4b2 JK |
436 | } |
437 | mutex_unlock(&dirty_i->seglist_lock); | |
b2955550 JK |
438 | |
439 | /* send small discards */ | |
2d7b822a | 440 | list_for_each_entry_safe(entry, this, head, list) { |
37208879 | 441 | f2fs_issue_discard(sbi, entry->blkaddr, entry->len); |
b2955550 JK |
442 | list_del(&entry->list); |
443 | SM_I(sbi)->nr_discards -= entry->len; | |
444 | kmem_cache_free(discard_entry_slab, entry); | |
445 | } | |
351df4b2 JK |
446 | } |
447 | ||
448 | static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno) | |
449 | { | |
450 | struct sit_info *sit_i = SIT_I(sbi); | |
451 | if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) | |
452 | sit_i->dirty_sentries++; | |
453 | } | |
454 | ||
455 | static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, | |
456 | unsigned int segno, int modified) | |
457 | { | |
458 | struct seg_entry *se = get_seg_entry(sbi, segno); | |
459 | se->type = type; | |
460 | if (modified) | |
461 | __mark_sit_entry_dirty(sbi, segno); | |
462 | } | |
463 | ||
464 | static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) | |
465 | { | |
466 | struct seg_entry *se; | |
467 | unsigned int segno, offset; | |
468 | long int new_vblocks; | |
469 | ||
470 | segno = GET_SEGNO(sbi, blkaddr); | |
471 | ||
472 | se = get_seg_entry(sbi, segno); | |
473 | new_vblocks = se->valid_blocks + del; | |
491c0854 | 474 | offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); |
351df4b2 | 475 | |
5d56b671 | 476 | f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) || |
351df4b2 JK |
477 | (new_vblocks > sbi->blocks_per_seg))); |
478 | ||
479 | se->valid_blocks = new_vblocks; | |
480 | se->mtime = get_mtime(sbi); | |
481 | SIT_I(sbi)->max_mtime = se->mtime; | |
482 | ||
483 | /* Update valid block bitmap */ | |
484 | if (del > 0) { | |
485 | if (f2fs_set_bit(offset, se->cur_valid_map)) | |
486 | BUG(); | |
487 | } else { | |
488 | if (!f2fs_clear_bit(offset, se->cur_valid_map)) | |
489 | BUG(); | |
490 | } | |
491 | if (!f2fs_test_bit(offset, se->ckpt_valid_map)) | |
492 | se->ckpt_valid_blocks += del; | |
493 | ||
494 | __mark_sit_entry_dirty(sbi, segno); | |
495 | ||
496 | /* update total number of valid blocks to be written in ckpt area */ | |
497 | SIT_I(sbi)->written_valid_blocks += del; | |
498 | ||
499 | if (sbi->segs_per_sec > 1) | |
500 | get_sec_entry(sbi, segno)->valid_blocks += del; | |
501 | } | |
502 | ||
5e443818 | 503 | void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new) |
351df4b2 | 504 | { |
5e443818 JK |
505 | update_sit_entry(sbi, new, 1); |
506 | if (GET_SEGNO(sbi, old) != NULL_SEGNO) | |
507 | update_sit_entry(sbi, old, -1); | |
508 | ||
509 | locate_dirty_segment(sbi, GET_SEGNO(sbi, old)); | |
510 | locate_dirty_segment(sbi, GET_SEGNO(sbi, new)); | |
351df4b2 JK |
511 | } |
512 | ||
513 | void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr) | |
514 | { | |
515 | unsigned int segno = GET_SEGNO(sbi, addr); | |
516 | struct sit_info *sit_i = SIT_I(sbi); | |
517 | ||
5d56b671 | 518 | f2fs_bug_on(addr == NULL_ADDR); |
351df4b2 JK |
519 | if (addr == NEW_ADDR) |
520 | return; | |
521 | ||
522 | /* add it into sit main buffer */ | |
523 | mutex_lock(&sit_i->sentry_lock); | |
524 | ||
525 | update_sit_entry(sbi, addr, -1); | |
526 | ||
527 | /* add it into dirty seglist */ | |
528 | locate_dirty_segment(sbi, segno); | |
529 | ||
530 | mutex_unlock(&sit_i->sentry_lock); | |
531 | } | |
532 | ||
0a8165d7 | 533 | /* |
351df4b2 JK |
534 | * This function should be resided under the curseg_mutex lock |
535 | */ | |
536 | static void __add_sum_entry(struct f2fs_sb_info *sbi, int type, | |
e79efe3b | 537 | struct f2fs_summary *sum) |
351df4b2 JK |
538 | { |
539 | struct curseg_info *curseg = CURSEG_I(sbi, type); | |
540 | void *addr = curseg->sum_blk; | |
e79efe3b | 541 | addr += curseg->next_blkoff * sizeof(struct f2fs_summary); |
351df4b2 | 542 | memcpy(addr, sum, sizeof(struct f2fs_summary)); |
351df4b2 JK |
543 | } |
544 | ||
0a8165d7 | 545 | /* |
351df4b2 JK |
546 | * Calculate the number of current summary pages for writing |
547 | */ | |
548 | int npages_for_summary_flush(struct f2fs_sb_info *sbi) | |
549 | { | |
351df4b2 | 550 | int valid_sum_count = 0; |
9a47938b | 551 | int i, sum_in_page; |
351df4b2 JK |
552 | |
553 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { | |
554 | if (sbi->ckpt->alloc_type[i] == SSR) | |
555 | valid_sum_count += sbi->blocks_per_seg; | |
556 | else | |
557 | valid_sum_count += curseg_blkoff(sbi, i); | |
558 | } | |
559 | ||
9a47938b FL |
560 | sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE - |
561 | SUM_FOOTER_SIZE) / SUMMARY_SIZE; | |
562 | if (valid_sum_count <= sum_in_page) | |
351df4b2 | 563 | return 1; |
9a47938b FL |
564 | else if ((valid_sum_count - sum_in_page) <= |
565 | (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE) | |
351df4b2 JK |
566 | return 2; |
567 | return 3; | |
568 | } | |
569 | ||
0a8165d7 | 570 | /* |
351df4b2 JK |
571 | * Caller should put this summary page |
572 | */ | |
573 | struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno) | |
574 | { | |
575 | return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno)); | |
576 | } | |
577 | ||
578 | static void write_sum_page(struct f2fs_sb_info *sbi, | |
579 | struct f2fs_summary_block *sum_blk, block_t blk_addr) | |
580 | { | |
581 | struct page *page = grab_meta_page(sbi, blk_addr); | |
582 | void *kaddr = page_address(page); | |
583 | memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE); | |
584 | set_page_dirty(page); | |
585 | f2fs_put_page(page, 1); | |
586 | } | |
587 | ||
60374688 JK |
588 | static int is_next_segment_free(struct f2fs_sb_info *sbi, int type) |
589 | { | |
590 | struct curseg_info *curseg = CURSEG_I(sbi, type); | |
81fb5e87 | 591 | unsigned int segno = curseg->segno + 1; |
60374688 JK |
592 | struct free_segmap_info *free_i = FREE_I(sbi); |
593 | ||
81fb5e87 HL |
594 | if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec) |
595 | return !test_bit(segno, free_i->free_segmap); | |
60374688 JK |
596 | return 0; |
597 | } | |
598 | ||
0a8165d7 | 599 | /* |
351df4b2 JK |
600 | * Find a new segment from the free segments bitmap to right order |
601 | * This function should be returned with success, otherwise BUG | |
602 | */ | |
603 | static void get_new_segment(struct f2fs_sb_info *sbi, | |
604 | unsigned int *newseg, bool new_sec, int dir) | |
605 | { | |
606 | struct free_segmap_info *free_i = FREE_I(sbi); | |
351df4b2 | 607 | unsigned int segno, secno, zoneno; |
53cf9522 | 608 | unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone; |
351df4b2 JK |
609 | unsigned int hint = *newseg / sbi->segs_per_sec; |
610 | unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg); | |
611 | unsigned int left_start = hint; | |
612 | bool init = true; | |
613 | int go_left = 0; | |
614 | int i; | |
615 | ||
616 | write_lock(&free_i->segmap_lock); | |
617 | ||
618 | if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) { | |
619 | segno = find_next_zero_bit(free_i->free_segmap, | |
620 | TOTAL_SEGS(sbi), *newseg + 1); | |
33afa7fd JK |
621 | if (segno - *newseg < sbi->segs_per_sec - |
622 | (*newseg % sbi->segs_per_sec)) | |
351df4b2 JK |
623 | goto got_it; |
624 | } | |
625 | find_other_zone: | |
53cf9522 JK |
626 | secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint); |
627 | if (secno >= TOTAL_SECS(sbi)) { | |
351df4b2 JK |
628 | if (dir == ALLOC_RIGHT) { |
629 | secno = find_next_zero_bit(free_i->free_secmap, | |
53cf9522 | 630 | TOTAL_SECS(sbi), 0); |
5d56b671 | 631 | f2fs_bug_on(secno >= TOTAL_SECS(sbi)); |
351df4b2 JK |
632 | } else { |
633 | go_left = 1; | |
634 | left_start = hint - 1; | |
635 | } | |
636 | } | |
637 | if (go_left == 0) | |
638 | goto skip_left; | |
639 | ||
640 | while (test_bit(left_start, free_i->free_secmap)) { | |
641 | if (left_start > 0) { | |
642 | left_start--; | |
643 | continue; | |
644 | } | |
645 | left_start = find_next_zero_bit(free_i->free_secmap, | |
53cf9522 | 646 | TOTAL_SECS(sbi), 0); |
5d56b671 | 647 | f2fs_bug_on(left_start >= TOTAL_SECS(sbi)); |
351df4b2 JK |
648 | break; |
649 | } | |
650 | secno = left_start; | |
651 | skip_left: | |
652 | hint = secno; | |
653 | segno = secno * sbi->segs_per_sec; | |
654 | zoneno = secno / sbi->secs_per_zone; | |
655 | ||
656 | /* give up on finding another zone */ | |
657 | if (!init) | |
658 | goto got_it; | |
659 | if (sbi->secs_per_zone == 1) | |
660 | goto got_it; | |
661 | if (zoneno == old_zoneno) | |
662 | goto got_it; | |
663 | if (dir == ALLOC_LEFT) { | |
664 | if (!go_left && zoneno + 1 >= total_zones) | |
665 | goto got_it; | |
666 | if (go_left && zoneno == 0) | |
667 | goto got_it; | |
668 | } | |
669 | for (i = 0; i < NR_CURSEG_TYPE; i++) | |
670 | if (CURSEG_I(sbi, i)->zone == zoneno) | |
671 | break; | |
672 | ||
673 | if (i < NR_CURSEG_TYPE) { | |
674 | /* zone is in user, try another */ | |
675 | if (go_left) | |
676 | hint = zoneno * sbi->secs_per_zone - 1; | |
677 | else if (zoneno + 1 >= total_zones) | |
678 | hint = 0; | |
679 | else | |
680 | hint = (zoneno + 1) * sbi->secs_per_zone; | |
681 | init = false; | |
682 | goto find_other_zone; | |
683 | } | |
684 | got_it: | |
685 | /* set it as dirty segment in free segmap */ | |
5d56b671 | 686 | f2fs_bug_on(test_bit(segno, free_i->free_segmap)); |
351df4b2 JK |
687 | __set_inuse(sbi, segno); |
688 | *newseg = segno; | |
689 | write_unlock(&free_i->segmap_lock); | |
690 | } | |
691 | ||
692 | static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) | |
693 | { | |
694 | struct curseg_info *curseg = CURSEG_I(sbi, type); | |
695 | struct summary_footer *sum_footer; | |
696 | ||
697 | curseg->segno = curseg->next_segno; | |
698 | curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno); | |
699 | curseg->next_blkoff = 0; | |
700 | curseg->next_segno = NULL_SEGNO; | |
701 | ||
702 | sum_footer = &(curseg->sum_blk->footer); | |
703 | memset(sum_footer, 0, sizeof(struct summary_footer)); | |
704 | if (IS_DATASEG(type)) | |
705 | SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); | |
706 | if (IS_NODESEG(type)) | |
707 | SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); | |
708 | __set_sit_entry_type(sbi, type, curseg->segno, modified); | |
709 | } | |
710 | ||
0a8165d7 | 711 | /* |
351df4b2 JK |
712 | * Allocate a current working segment. |
713 | * This function always allocates a free segment in LFS manner. | |
714 | */ | |
715 | static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) | |
716 | { | |
717 | struct curseg_info *curseg = CURSEG_I(sbi, type); | |
718 | unsigned int segno = curseg->segno; | |
719 | int dir = ALLOC_LEFT; | |
720 | ||
721 | write_sum_page(sbi, curseg->sum_blk, | |
81fb5e87 | 722 | GET_SUM_BLOCK(sbi, segno)); |
351df4b2 JK |
723 | if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA) |
724 | dir = ALLOC_RIGHT; | |
725 | ||
726 | if (test_opt(sbi, NOHEAP)) | |
727 | dir = ALLOC_RIGHT; | |
728 | ||
729 | get_new_segment(sbi, &segno, new_sec, dir); | |
730 | curseg->next_segno = segno; | |
731 | reset_curseg(sbi, type, 1); | |
732 | curseg->alloc_type = LFS; | |
733 | } | |
734 | ||
735 | static void __next_free_blkoff(struct f2fs_sb_info *sbi, | |
736 | struct curseg_info *seg, block_t start) | |
737 | { | |
738 | struct seg_entry *se = get_seg_entry(sbi, seg->segno); | |
e81c93cf CL |
739 | int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); |
740 | unsigned long target_map[entries]; | |
741 | unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; | |
742 | unsigned long *cur_map = (unsigned long *)se->cur_valid_map; | |
743 | int i, pos; | |
744 | ||
745 | for (i = 0; i < entries; i++) | |
746 | target_map[i] = ckpt_map[i] | cur_map[i]; | |
747 | ||
748 | pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start); | |
749 | ||
750 | seg->next_blkoff = pos; | |
351df4b2 JK |
751 | } |
752 | ||
0a8165d7 | 753 | /* |
351df4b2 JK |
754 | * If a segment is written by LFS manner, next block offset is just obtained |
755 | * by increasing the current block offset. However, if a segment is written by | |
756 | * SSR manner, next block offset obtained by calling __next_free_blkoff | |
757 | */ | |
758 | static void __refresh_next_blkoff(struct f2fs_sb_info *sbi, | |
759 | struct curseg_info *seg) | |
760 | { | |
761 | if (seg->alloc_type == SSR) | |
762 | __next_free_blkoff(sbi, seg, seg->next_blkoff + 1); | |
763 | else | |
764 | seg->next_blkoff++; | |
765 | } | |
766 | ||
0a8165d7 | 767 | /* |
351df4b2 JK |
768 | * This function always allocates a used segment (from dirty seglist) by SSR |
769 | * manner, so it should recover the existing segment information of valid blocks | |
770 | */ | |
771 | static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse) | |
772 | { | |
773 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
774 | struct curseg_info *curseg = CURSEG_I(sbi, type); | |
775 | unsigned int new_segno = curseg->next_segno; | |
776 | struct f2fs_summary_block *sum_node; | |
777 | struct page *sum_page; | |
778 | ||
779 | write_sum_page(sbi, curseg->sum_blk, | |
780 | GET_SUM_BLOCK(sbi, curseg->segno)); | |
781 | __set_test_and_inuse(sbi, new_segno); | |
782 | ||
783 | mutex_lock(&dirty_i->seglist_lock); | |
784 | __remove_dirty_segment(sbi, new_segno, PRE); | |
785 | __remove_dirty_segment(sbi, new_segno, DIRTY); | |
786 | mutex_unlock(&dirty_i->seglist_lock); | |
787 | ||
788 | reset_curseg(sbi, type, 1); | |
789 | curseg->alloc_type = SSR; | |
790 | __next_free_blkoff(sbi, curseg, 0); | |
791 | ||
792 | if (reuse) { | |
793 | sum_page = get_sum_page(sbi, new_segno); | |
794 | sum_node = (struct f2fs_summary_block *)page_address(sum_page); | |
795 | memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); | |
796 | f2fs_put_page(sum_page, 1); | |
797 | } | |
798 | } | |
799 | ||
43727527 JK |
800 | static int get_ssr_segment(struct f2fs_sb_info *sbi, int type) |
801 | { | |
802 | struct curseg_info *curseg = CURSEG_I(sbi, type); | |
803 | const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops; | |
804 | ||
805 | if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0)) | |
806 | return v_ops->get_victim(sbi, | |
807 | &(curseg)->next_segno, BG_GC, type, SSR); | |
808 | ||
809 | /* For data segments, let's do SSR more intensively */ | |
810 | for (; type >= CURSEG_HOT_DATA; type--) | |
811 | if (v_ops->get_victim(sbi, &(curseg)->next_segno, | |
812 | BG_GC, type, SSR)) | |
813 | return 1; | |
814 | return 0; | |
815 | } | |
816 | ||
351df4b2 JK |
817 | /* |
818 | * flush out current segment and replace it with new segment | |
819 | * This function should be returned with success, otherwise BUG | |
820 | */ | |
821 | static void allocate_segment_by_default(struct f2fs_sb_info *sbi, | |
822 | int type, bool force) | |
823 | { | |
824 | struct curseg_info *curseg = CURSEG_I(sbi, type); | |
351df4b2 | 825 | |
7b405275 | 826 | if (force) |
351df4b2 | 827 | new_curseg(sbi, type, true); |
7b405275 | 828 | else if (type == CURSEG_WARM_NODE) |
351df4b2 | 829 | new_curseg(sbi, type, false); |
60374688 JK |
830 | else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type)) |
831 | new_curseg(sbi, type, false); | |
351df4b2 JK |
832 | else if (need_SSR(sbi) && get_ssr_segment(sbi, type)) |
833 | change_curseg(sbi, type, true); | |
834 | else | |
835 | new_curseg(sbi, type, false); | |
dcdfff65 JK |
836 | |
837 | stat_inc_seg_type(sbi, curseg); | |
351df4b2 JK |
838 | } |
839 | ||
840 | void allocate_new_segments(struct f2fs_sb_info *sbi) | |
841 | { | |
842 | struct curseg_info *curseg; | |
843 | unsigned int old_curseg; | |
844 | int i; | |
845 | ||
846 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { | |
847 | curseg = CURSEG_I(sbi, i); | |
848 | old_curseg = curseg->segno; | |
849 | SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true); | |
850 | locate_dirty_segment(sbi, old_curseg); | |
851 | } | |
852 | } | |
853 | ||
854 | static const struct segment_allocation default_salloc_ops = { | |
855 | .allocate_segment = allocate_segment_by_default, | |
856 | }; | |
857 | ||
351df4b2 JK |
858 | static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type) |
859 | { | |
860 | struct curseg_info *curseg = CURSEG_I(sbi, type); | |
861 | if (curseg->next_blkoff < sbi->blocks_per_seg) | |
862 | return true; | |
863 | return false; | |
864 | } | |
865 | ||
866 | static int __get_segment_type_2(struct page *page, enum page_type p_type) | |
867 | { | |
868 | if (p_type == DATA) | |
869 | return CURSEG_HOT_DATA; | |
870 | else | |
871 | return CURSEG_HOT_NODE; | |
872 | } | |
873 | ||
874 | static int __get_segment_type_4(struct page *page, enum page_type p_type) | |
875 | { | |
876 | if (p_type == DATA) { | |
877 | struct inode *inode = page->mapping->host; | |
878 | ||
879 | if (S_ISDIR(inode->i_mode)) | |
880 | return CURSEG_HOT_DATA; | |
881 | else | |
882 | return CURSEG_COLD_DATA; | |
883 | } else { | |
884 | if (IS_DNODE(page) && !is_cold_node(page)) | |
885 | return CURSEG_HOT_NODE; | |
886 | else | |
887 | return CURSEG_COLD_NODE; | |
888 | } | |
889 | } | |
890 | ||
891 | static int __get_segment_type_6(struct page *page, enum page_type p_type) | |
892 | { | |
893 | if (p_type == DATA) { | |
894 | struct inode *inode = page->mapping->host; | |
895 | ||
896 | if (S_ISDIR(inode->i_mode)) | |
897 | return CURSEG_HOT_DATA; | |
354a3399 | 898 | else if (is_cold_data(page) || file_is_cold(inode)) |
351df4b2 JK |
899 | return CURSEG_COLD_DATA; |
900 | else | |
901 | return CURSEG_WARM_DATA; | |
902 | } else { | |
903 | if (IS_DNODE(page)) | |
904 | return is_cold_node(page) ? CURSEG_WARM_NODE : | |
905 | CURSEG_HOT_NODE; | |
906 | else | |
907 | return CURSEG_COLD_NODE; | |
908 | } | |
909 | } | |
910 | ||
911 | static int __get_segment_type(struct page *page, enum page_type p_type) | |
912 | { | |
913 | struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb); | |
914 | switch (sbi->active_logs) { | |
915 | case 2: | |
916 | return __get_segment_type_2(page, p_type); | |
917 | case 4: | |
918 | return __get_segment_type_4(page, p_type); | |
351df4b2 | 919 | } |
12a67146 | 920 | /* NR_CURSEG_TYPE(6) logs by default */ |
5d56b671 | 921 | f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE); |
12a67146 | 922 | return __get_segment_type_6(page, p_type); |
351df4b2 JK |
923 | } |
924 | ||
bfad7c2d JK |
925 | void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, |
926 | block_t old_blkaddr, block_t *new_blkaddr, | |
927 | struct f2fs_summary *sum, int type) | |
351df4b2 JK |
928 | { |
929 | struct sit_info *sit_i = SIT_I(sbi); | |
930 | struct curseg_info *curseg; | |
931 | unsigned int old_cursegno; | |
351df4b2 | 932 | |
351df4b2 JK |
933 | curseg = CURSEG_I(sbi, type); |
934 | ||
935 | mutex_lock(&curseg->curseg_mutex); | |
936 | ||
937 | *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); | |
938 | old_cursegno = curseg->segno; | |
939 | ||
940 | /* | |
941 | * __add_sum_entry should be resided under the curseg_mutex | |
942 | * because, this function updates a summary entry in the | |
943 | * current summary block. | |
944 | */ | |
e79efe3b | 945 | __add_sum_entry(sbi, type, sum); |
351df4b2 JK |
946 | |
947 | mutex_lock(&sit_i->sentry_lock); | |
948 | __refresh_next_blkoff(sbi, curseg); | |
dcdfff65 JK |
949 | |
950 | stat_inc_block_count(sbi, curseg); | |
351df4b2 | 951 | |
5e443818 JK |
952 | if (!__has_curseg_space(sbi, type)) |
953 | sit_i->s_ops->allocate_segment(sbi, type, false); | |
351df4b2 JK |
954 | /* |
955 | * SIT information should be updated before segment allocation, | |
956 | * since SSR needs latest valid block information. | |
957 | */ | |
958 | refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr); | |
351df4b2 | 959 | locate_dirty_segment(sbi, old_cursegno); |
5e443818 | 960 | |
351df4b2 JK |
961 | mutex_unlock(&sit_i->sentry_lock); |
962 | ||
bfad7c2d | 963 | if (page && IS_NODESEG(type)) |
351df4b2 JK |
964 | fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); |
965 | ||
bfad7c2d JK |
966 | mutex_unlock(&curseg->curseg_mutex); |
967 | } | |
968 | ||
969 | static void do_write_page(struct f2fs_sb_info *sbi, struct page *page, | |
970 | block_t old_blkaddr, block_t *new_blkaddr, | |
971 | struct f2fs_summary *sum, struct f2fs_io_info *fio) | |
972 | { | |
973 | int type = __get_segment_type(page, fio->type); | |
974 | ||
975 | allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type); | |
976 | ||
351df4b2 | 977 | /* writeout dirty page into bdev */ |
458e6197 | 978 | f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio); |
351df4b2 JK |
979 | } |
980 | ||
577e3495 | 981 | void write_meta_page(struct f2fs_sb_info *sbi, struct page *page) |
351df4b2 | 982 | { |
458e6197 JK |
983 | struct f2fs_io_info fio = { |
984 | .type = META, | |
7e8f2308 | 985 | .rw = WRITE_SYNC | REQ_META | REQ_PRIO |
458e6197 JK |
986 | }; |
987 | ||
351df4b2 | 988 | set_page_writeback(page); |
458e6197 | 989 | f2fs_submit_page_mbio(sbi, page, page->index, &fio); |
351df4b2 JK |
990 | } |
991 | ||
992 | void write_node_page(struct f2fs_sb_info *sbi, struct page *page, | |
fb5566da | 993 | struct f2fs_io_info *fio, |
351df4b2 JK |
994 | unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr) |
995 | { | |
996 | struct f2fs_summary sum; | |
997 | set_summary(&sum, nid, 0, 0); | |
fb5566da | 998 | do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio); |
351df4b2 JK |
999 | } |
1000 | ||
458e6197 JK |
1001 | void write_data_page(struct page *page, struct dnode_of_data *dn, |
1002 | block_t *new_blkaddr, struct f2fs_io_info *fio) | |
351df4b2 | 1003 | { |
458e6197 | 1004 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
351df4b2 JK |
1005 | struct f2fs_summary sum; |
1006 | struct node_info ni; | |
1007 | ||
458e6197 | 1008 | f2fs_bug_on(dn->data_blkaddr == NULL_ADDR); |
351df4b2 JK |
1009 | get_node_info(sbi, dn->nid, &ni); |
1010 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); | |
1011 | ||
458e6197 | 1012 | do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio); |
351df4b2 JK |
1013 | } |
1014 | ||
6c311ec6 CF |
1015 | void rewrite_data_page(struct page *page, block_t old_blkaddr, |
1016 | struct f2fs_io_info *fio) | |
351df4b2 | 1017 | { |
458e6197 JK |
1018 | struct inode *inode = page->mapping->host; |
1019 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); | |
1020 | f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio); | |
351df4b2 JK |
1021 | } |
1022 | ||
1023 | void recover_data_page(struct f2fs_sb_info *sbi, | |
1024 | struct page *page, struct f2fs_summary *sum, | |
1025 | block_t old_blkaddr, block_t new_blkaddr) | |
1026 | { | |
1027 | struct sit_info *sit_i = SIT_I(sbi); | |
1028 | struct curseg_info *curseg; | |
1029 | unsigned int segno, old_cursegno; | |
1030 | struct seg_entry *se; | |
1031 | int type; | |
1032 | ||
1033 | segno = GET_SEGNO(sbi, new_blkaddr); | |
1034 | se = get_seg_entry(sbi, segno); | |
1035 | type = se->type; | |
1036 | ||
1037 | if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) { | |
1038 | if (old_blkaddr == NULL_ADDR) | |
1039 | type = CURSEG_COLD_DATA; | |
1040 | else | |
1041 | type = CURSEG_WARM_DATA; | |
1042 | } | |
1043 | curseg = CURSEG_I(sbi, type); | |
1044 | ||
1045 | mutex_lock(&curseg->curseg_mutex); | |
1046 | mutex_lock(&sit_i->sentry_lock); | |
1047 | ||
1048 | old_cursegno = curseg->segno; | |
1049 | ||
1050 | /* change the current segment */ | |
1051 | if (segno != curseg->segno) { | |
1052 | curseg->next_segno = segno; | |
1053 | change_curseg(sbi, type, true); | |
1054 | } | |
1055 | ||
491c0854 | 1056 | curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); |
e79efe3b | 1057 | __add_sum_entry(sbi, type, sum); |
351df4b2 JK |
1058 | |
1059 | refresh_sit_entry(sbi, old_blkaddr, new_blkaddr); | |
351df4b2 | 1060 | locate_dirty_segment(sbi, old_cursegno); |
351df4b2 JK |
1061 | |
1062 | mutex_unlock(&sit_i->sentry_lock); | |
1063 | mutex_unlock(&curseg->curseg_mutex); | |
1064 | } | |
1065 | ||
1066 | void rewrite_node_page(struct f2fs_sb_info *sbi, | |
1067 | struct page *page, struct f2fs_summary *sum, | |
1068 | block_t old_blkaddr, block_t new_blkaddr) | |
1069 | { | |
1070 | struct sit_info *sit_i = SIT_I(sbi); | |
1071 | int type = CURSEG_WARM_NODE; | |
1072 | struct curseg_info *curseg; | |
1073 | unsigned int segno, old_cursegno; | |
1074 | block_t next_blkaddr = next_blkaddr_of_node(page); | |
1075 | unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr); | |
458e6197 JK |
1076 | struct f2fs_io_info fio = { |
1077 | .type = NODE, | |
1078 | .rw = WRITE_SYNC, | |
458e6197 | 1079 | }; |
351df4b2 JK |
1080 | |
1081 | curseg = CURSEG_I(sbi, type); | |
1082 | ||
1083 | mutex_lock(&curseg->curseg_mutex); | |
1084 | mutex_lock(&sit_i->sentry_lock); | |
1085 | ||
1086 | segno = GET_SEGNO(sbi, new_blkaddr); | |
1087 | old_cursegno = curseg->segno; | |
1088 | ||
1089 | /* change the current segment */ | |
1090 | if (segno != curseg->segno) { | |
1091 | curseg->next_segno = segno; | |
1092 | change_curseg(sbi, type, true); | |
1093 | } | |
491c0854 | 1094 | curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); |
e79efe3b | 1095 | __add_sum_entry(sbi, type, sum); |
351df4b2 JK |
1096 | |
1097 | /* change the current log to the next block addr in advance */ | |
1098 | if (next_segno != segno) { | |
1099 | curseg->next_segno = next_segno; | |
1100 | change_curseg(sbi, type, true); | |
1101 | } | |
491c0854 | 1102 | curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr); |
351df4b2 JK |
1103 | |
1104 | /* rewrite node page */ | |
1105 | set_page_writeback(page); | |
458e6197 JK |
1106 | f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio); |
1107 | f2fs_submit_merged_bio(sbi, NODE, WRITE); | |
351df4b2 | 1108 | refresh_sit_entry(sbi, old_blkaddr, new_blkaddr); |
351df4b2 | 1109 | locate_dirty_segment(sbi, old_cursegno); |
351df4b2 JK |
1110 | |
1111 | mutex_unlock(&sit_i->sentry_lock); | |
1112 | mutex_unlock(&curseg->curseg_mutex); | |
1113 | } | |
1114 | ||
df0f8dc0 CY |
1115 | static inline bool is_merged_page(struct f2fs_sb_info *sbi, |
1116 | struct page *page, enum page_type type) | |
1117 | { | |
1118 | enum page_type btype = PAGE_TYPE_OF_BIO(type); | |
1119 | struct f2fs_bio_info *io = &sbi->write_io[btype]; | |
df0f8dc0 CY |
1120 | struct bio_vec *bvec; |
1121 | int i; | |
1122 | ||
1123 | down_read(&io->io_rwsem); | |
ce23447f | 1124 | if (!io->bio) |
df0f8dc0 CY |
1125 | goto out; |
1126 | ||
ce23447f | 1127 | bio_for_each_segment_all(bvec, io->bio, i) { |
df0f8dc0 CY |
1128 | if (page == bvec->bv_page) { |
1129 | up_read(&io->io_rwsem); | |
1130 | return true; | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | out: | |
1135 | up_read(&io->io_rwsem); | |
1136 | return false; | |
1137 | } | |
1138 | ||
93dfe2ac | 1139 | void f2fs_wait_on_page_writeback(struct page *page, |
5514f0aa | 1140 | enum page_type type) |
93dfe2ac JK |
1141 | { |
1142 | struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb); | |
1143 | if (PageWriteback(page)) { | |
df0f8dc0 CY |
1144 | if (is_merged_page(sbi, page, type)) |
1145 | f2fs_submit_merged_bio(sbi, type, WRITE); | |
93dfe2ac JK |
1146 | wait_on_page_writeback(page); |
1147 | } | |
1148 | } | |
1149 | ||
351df4b2 JK |
1150 | static int read_compacted_summaries(struct f2fs_sb_info *sbi) |
1151 | { | |
1152 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
1153 | struct curseg_info *seg_i; | |
1154 | unsigned char *kaddr; | |
1155 | struct page *page; | |
1156 | block_t start; | |
1157 | int i, j, offset; | |
1158 | ||
1159 | start = start_sum_block(sbi); | |
1160 | ||
1161 | page = get_meta_page(sbi, start++); | |
1162 | kaddr = (unsigned char *)page_address(page); | |
1163 | ||
1164 | /* Step 1: restore nat cache */ | |
1165 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); | |
1166 | memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE); | |
1167 | ||
1168 | /* Step 2: restore sit cache */ | |
1169 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); | |
1170 | memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE, | |
1171 | SUM_JOURNAL_SIZE); | |
1172 | offset = 2 * SUM_JOURNAL_SIZE; | |
1173 | ||
1174 | /* Step 3: restore summary entries */ | |
1175 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { | |
1176 | unsigned short blk_off; | |
1177 | unsigned int segno; | |
1178 | ||
1179 | seg_i = CURSEG_I(sbi, i); | |
1180 | segno = le32_to_cpu(ckpt->cur_data_segno[i]); | |
1181 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); | |
1182 | seg_i->next_segno = segno; | |
1183 | reset_curseg(sbi, i, 0); | |
1184 | seg_i->alloc_type = ckpt->alloc_type[i]; | |
1185 | seg_i->next_blkoff = blk_off; | |
1186 | ||
1187 | if (seg_i->alloc_type == SSR) | |
1188 | blk_off = sbi->blocks_per_seg; | |
1189 | ||
1190 | for (j = 0; j < blk_off; j++) { | |
1191 | struct f2fs_summary *s; | |
1192 | s = (struct f2fs_summary *)(kaddr + offset); | |
1193 | seg_i->sum_blk->entries[j] = *s; | |
1194 | offset += SUMMARY_SIZE; | |
1195 | if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE - | |
1196 | SUM_FOOTER_SIZE) | |
1197 | continue; | |
1198 | ||
1199 | f2fs_put_page(page, 1); | |
1200 | page = NULL; | |
1201 | ||
1202 | page = get_meta_page(sbi, start++); | |
1203 | kaddr = (unsigned char *)page_address(page); | |
1204 | offset = 0; | |
1205 | } | |
1206 | } | |
1207 | f2fs_put_page(page, 1); | |
1208 | return 0; | |
1209 | } | |
1210 | ||
1211 | static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) | |
1212 | { | |
1213 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
1214 | struct f2fs_summary_block *sum; | |
1215 | struct curseg_info *curseg; | |
1216 | struct page *new; | |
1217 | unsigned short blk_off; | |
1218 | unsigned int segno = 0; | |
1219 | block_t blk_addr = 0; | |
1220 | ||
1221 | /* get segment number and block addr */ | |
1222 | if (IS_DATASEG(type)) { | |
1223 | segno = le32_to_cpu(ckpt->cur_data_segno[type]); | |
1224 | blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - | |
1225 | CURSEG_HOT_DATA]); | |
25ca923b | 1226 | if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) |
351df4b2 JK |
1227 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); |
1228 | else | |
1229 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); | |
1230 | } else { | |
1231 | segno = le32_to_cpu(ckpt->cur_node_segno[type - | |
1232 | CURSEG_HOT_NODE]); | |
1233 | blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - | |
1234 | CURSEG_HOT_NODE]); | |
25ca923b | 1235 | if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) |
351df4b2 JK |
1236 | blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, |
1237 | type - CURSEG_HOT_NODE); | |
1238 | else | |
1239 | blk_addr = GET_SUM_BLOCK(sbi, segno); | |
1240 | } | |
1241 | ||
1242 | new = get_meta_page(sbi, blk_addr); | |
1243 | sum = (struct f2fs_summary_block *)page_address(new); | |
1244 | ||
1245 | if (IS_NODESEG(type)) { | |
25ca923b | 1246 | if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) { |
351df4b2 JK |
1247 | struct f2fs_summary *ns = &sum->entries[0]; |
1248 | int i; | |
1249 | for (i = 0; i < sbi->blocks_per_seg; i++, ns++) { | |
1250 | ns->version = 0; | |
1251 | ns->ofs_in_node = 0; | |
1252 | } | |
1253 | } else { | |
d653788a GZ |
1254 | int err; |
1255 | ||
1256 | err = restore_node_summary(sbi, segno, sum); | |
1257 | if (err) { | |
351df4b2 | 1258 | f2fs_put_page(new, 1); |
d653788a | 1259 | return err; |
351df4b2 JK |
1260 | } |
1261 | } | |
1262 | } | |
1263 | ||
1264 | /* set uncompleted segment to curseg */ | |
1265 | curseg = CURSEG_I(sbi, type); | |
1266 | mutex_lock(&curseg->curseg_mutex); | |
1267 | memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE); | |
1268 | curseg->next_segno = segno; | |
1269 | reset_curseg(sbi, type, 0); | |
1270 | curseg->alloc_type = ckpt->alloc_type[type]; | |
1271 | curseg->next_blkoff = blk_off; | |
1272 | mutex_unlock(&curseg->curseg_mutex); | |
1273 | f2fs_put_page(new, 1); | |
1274 | return 0; | |
1275 | } | |
1276 | ||
1277 | static int restore_curseg_summaries(struct f2fs_sb_info *sbi) | |
1278 | { | |
1279 | int type = CURSEG_HOT_DATA; | |
e4fc5fbf | 1280 | int err; |
351df4b2 | 1281 | |
25ca923b | 1282 | if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) { |
351df4b2 JK |
1283 | /* restore for compacted data summary */ |
1284 | if (read_compacted_summaries(sbi)) | |
1285 | return -EINVAL; | |
1286 | type = CURSEG_HOT_NODE; | |
1287 | } | |
1288 | ||
e4fc5fbf CY |
1289 | for (; type <= CURSEG_COLD_NODE; type++) { |
1290 | err = read_normal_summaries(sbi, type); | |
1291 | if (err) | |
1292 | return err; | |
1293 | } | |
1294 | ||
351df4b2 JK |
1295 | return 0; |
1296 | } | |
1297 | ||
1298 | static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) | |
1299 | { | |
1300 | struct page *page; | |
1301 | unsigned char *kaddr; | |
1302 | struct f2fs_summary *summary; | |
1303 | struct curseg_info *seg_i; | |
1304 | int written_size = 0; | |
1305 | int i, j; | |
1306 | ||
1307 | page = grab_meta_page(sbi, blkaddr++); | |
1308 | kaddr = (unsigned char *)page_address(page); | |
1309 | ||
1310 | /* Step 1: write nat cache */ | |
1311 | seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); | |
1312 | memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE); | |
1313 | written_size += SUM_JOURNAL_SIZE; | |
1314 | ||
1315 | /* Step 2: write sit cache */ | |
1316 | seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); | |
1317 | memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits, | |
1318 | SUM_JOURNAL_SIZE); | |
1319 | written_size += SUM_JOURNAL_SIZE; | |
1320 | ||
351df4b2 JK |
1321 | /* Step 3: write summary entries */ |
1322 | for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { | |
1323 | unsigned short blkoff; | |
1324 | seg_i = CURSEG_I(sbi, i); | |
1325 | if (sbi->ckpt->alloc_type[i] == SSR) | |
1326 | blkoff = sbi->blocks_per_seg; | |
1327 | else | |
1328 | blkoff = curseg_blkoff(sbi, i); | |
1329 | ||
1330 | for (j = 0; j < blkoff; j++) { | |
1331 | if (!page) { | |
1332 | page = grab_meta_page(sbi, blkaddr++); | |
1333 | kaddr = (unsigned char *)page_address(page); | |
1334 | written_size = 0; | |
1335 | } | |
1336 | summary = (struct f2fs_summary *)(kaddr + written_size); | |
1337 | *summary = seg_i->sum_blk->entries[j]; | |
1338 | written_size += SUMMARY_SIZE; | |
351df4b2 JK |
1339 | |
1340 | if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE - | |
1341 | SUM_FOOTER_SIZE) | |
1342 | continue; | |
1343 | ||
e8d61a74 | 1344 | set_page_dirty(page); |
351df4b2 JK |
1345 | f2fs_put_page(page, 1); |
1346 | page = NULL; | |
1347 | } | |
1348 | } | |
e8d61a74 CY |
1349 | if (page) { |
1350 | set_page_dirty(page); | |
351df4b2 | 1351 | f2fs_put_page(page, 1); |
e8d61a74 | 1352 | } |
351df4b2 JK |
1353 | } |
1354 | ||
1355 | static void write_normal_summaries(struct f2fs_sb_info *sbi, | |
1356 | block_t blkaddr, int type) | |
1357 | { | |
1358 | int i, end; | |
1359 | if (IS_DATASEG(type)) | |
1360 | end = type + NR_CURSEG_DATA_TYPE; | |
1361 | else | |
1362 | end = type + NR_CURSEG_NODE_TYPE; | |
1363 | ||
1364 | for (i = type; i < end; i++) { | |
1365 | struct curseg_info *sum = CURSEG_I(sbi, i); | |
1366 | mutex_lock(&sum->curseg_mutex); | |
1367 | write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type)); | |
1368 | mutex_unlock(&sum->curseg_mutex); | |
1369 | } | |
1370 | } | |
1371 | ||
1372 | void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk) | |
1373 | { | |
25ca923b | 1374 | if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) |
351df4b2 JK |
1375 | write_compacted_summaries(sbi, start_blk); |
1376 | else | |
1377 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); | |
1378 | } | |
1379 | ||
1380 | void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk) | |
1381 | { | |
25ca923b | 1382 | if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG)) |
351df4b2 | 1383 | write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); |
351df4b2 JK |
1384 | } |
1385 | ||
1386 | int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type, | |
1387 | unsigned int val, int alloc) | |
1388 | { | |
1389 | int i; | |
1390 | ||
1391 | if (type == NAT_JOURNAL) { | |
1392 | for (i = 0; i < nats_in_cursum(sum); i++) { | |
1393 | if (le32_to_cpu(nid_in_journal(sum, i)) == val) | |
1394 | return i; | |
1395 | } | |
1396 | if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) | |
1397 | return update_nats_in_cursum(sum, 1); | |
1398 | } else if (type == SIT_JOURNAL) { | |
1399 | for (i = 0; i < sits_in_cursum(sum); i++) | |
1400 | if (le32_to_cpu(segno_in_journal(sum, i)) == val) | |
1401 | return i; | |
1402 | if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES) | |
1403 | return update_sits_in_cursum(sum, 1); | |
1404 | } | |
1405 | return -1; | |
1406 | } | |
1407 | ||
1408 | static struct page *get_current_sit_page(struct f2fs_sb_info *sbi, | |
1409 | unsigned int segno) | |
1410 | { | |
1411 | struct sit_info *sit_i = SIT_I(sbi); | |
1412 | unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno); | |
1413 | block_t blk_addr = sit_i->sit_base_addr + offset; | |
1414 | ||
1415 | check_seg_range(sbi, segno); | |
1416 | ||
1417 | /* calculate sit block address */ | |
1418 | if (f2fs_test_bit(offset, sit_i->sit_bitmap)) | |
1419 | blk_addr += sit_i->sit_blocks; | |
1420 | ||
1421 | return get_meta_page(sbi, blk_addr); | |
1422 | } | |
1423 | ||
1424 | static struct page *get_next_sit_page(struct f2fs_sb_info *sbi, | |
1425 | unsigned int start) | |
1426 | { | |
1427 | struct sit_info *sit_i = SIT_I(sbi); | |
1428 | struct page *src_page, *dst_page; | |
1429 | pgoff_t src_off, dst_off; | |
1430 | void *src_addr, *dst_addr; | |
1431 | ||
1432 | src_off = current_sit_addr(sbi, start); | |
1433 | dst_off = next_sit_addr(sbi, src_off); | |
1434 | ||
1435 | /* get current sit block page without lock */ | |
1436 | src_page = get_meta_page(sbi, src_off); | |
1437 | dst_page = grab_meta_page(sbi, dst_off); | |
5d56b671 | 1438 | f2fs_bug_on(PageDirty(src_page)); |
351df4b2 JK |
1439 | |
1440 | src_addr = page_address(src_page); | |
1441 | dst_addr = page_address(dst_page); | |
1442 | memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE); | |
1443 | ||
1444 | set_page_dirty(dst_page); | |
1445 | f2fs_put_page(src_page, 1); | |
1446 | ||
1447 | set_to_next_sit(sit_i, start); | |
1448 | ||
1449 | return dst_page; | |
1450 | } | |
1451 | ||
1452 | static bool flush_sits_in_journal(struct f2fs_sb_info *sbi) | |
1453 | { | |
1454 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); | |
1455 | struct f2fs_summary_block *sum = curseg->sum_blk; | |
1456 | int i; | |
1457 | ||
1458 | /* | |
1459 | * If the journal area in the current summary is full of sit entries, | |
1460 | * all the sit entries will be flushed. Otherwise the sit entries | |
1461 | * are not able to replace with newly hot sit entries. | |
1462 | */ | |
1463 | if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) { | |
1464 | for (i = sits_in_cursum(sum) - 1; i >= 0; i--) { | |
1465 | unsigned int segno; | |
1466 | segno = le32_to_cpu(segno_in_journal(sum, i)); | |
1467 | __mark_sit_entry_dirty(sbi, segno); | |
1468 | } | |
1469 | update_sits_in_cursum(sum, -sits_in_cursum(sum)); | |
cffbfa66 | 1470 | return true; |
351df4b2 | 1471 | } |
cffbfa66 | 1472 | return false; |
351df4b2 JK |
1473 | } |
1474 | ||
0a8165d7 | 1475 | /* |
351df4b2 JK |
1476 | * CP calls this function, which flushes SIT entries including sit_journal, |
1477 | * and moves prefree segs to free segs. | |
1478 | */ | |
1479 | void flush_sit_entries(struct f2fs_sb_info *sbi) | |
1480 | { | |
1481 | struct sit_info *sit_i = SIT_I(sbi); | |
1482 | unsigned long *bitmap = sit_i->dirty_sentries_bitmap; | |
1483 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); | |
1484 | struct f2fs_summary_block *sum = curseg->sum_blk; | |
1485 | unsigned long nsegs = TOTAL_SEGS(sbi); | |
1486 | struct page *page = NULL; | |
1487 | struct f2fs_sit_block *raw_sit = NULL; | |
1488 | unsigned int start = 0, end = 0; | |
1489 | unsigned int segno = -1; | |
1490 | bool flushed; | |
1491 | ||
1492 | mutex_lock(&curseg->curseg_mutex); | |
1493 | mutex_lock(&sit_i->sentry_lock); | |
1494 | ||
1495 | /* | |
1496 | * "flushed" indicates whether sit entries in journal are flushed | |
1497 | * to the SIT area or not. | |
1498 | */ | |
1499 | flushed = flush_sits_in_journal(sbi); | |
1500 | ||
1501 | while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) { | |
1502 | struct seg_entry *se = get_seg_entry(sbi, segno); | |
1503 | int sit_offset, offset; | |
1504 | ||
1505 | sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); | |
1506 | ||
b2955550 JK |
1507 | /* add discard candidates */ |
1508 | if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards) | |
1509 | add_discard_addrs(sbi, segno, se); | |
1510 | ||
351df4b2 JK |
1511 | if (flushed) |
1512 | goto to_sit_page; | |
1513 | ||
1514 | offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1); | |
1515 | if (offset >= 0) { | |
1516 | segno_in_journal(sum, offset) = cpu_to_le32(segno); | |
1517 | seg_info_to_raw_sit(se, &sit_in_journal(sum, offset)); | |
1518 | goto flush_done; | |
1519 | } | |
1520 | to_sit_page: | |
1521 | if (!page || (start > segno) || (segno > end)) { | |
1522 | if (page) { | |
1523 | f2fs_put_page(page, 1); | |
1524 | page = NULL; | |
1525 | } | |
1526 | ||
1527 | start = START_SEGNO(sit_i, segno); | |
1528 | end = start + SIT_ENTRY_PER_BLOCK - 1; | |
1529 | ||
1530 | /* read sit block that will be updated */ | |
1531 | page = get_next_sit_page(sbi, start); | |
1532 | raw_sit = page_address(page); | |
1533 | } | |
1534 | ||
1535 | /* udpate entry in SIT block */ | |
1536 | seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]); | |
1537 | flush_done: | |
1538 | __clear_bit(segno, bitmap); | |
1539 | sit_i->dirty_sentries--; | |
1540 | } | |
1541 | mutex_unlock(&sit_i->sentry_lock); | |
1542 | mutex_unlock(&curseg->curseg_mutex); | |
1543 | ||
1544 | /* writeout last modified SIT block */ | |
1545 | f2fs_put_page(page, 1); | |
1546 | ||
1547 | set_prefree_as_free_segments(sbi); | |
1548 | } | |
1549 | ||
1550 | static int build_sit_info(struct f2fs_sb_info *sbi) | |
1551 | { | |
1552 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); | |
1553 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
1554 | struct sit_info *sit_i; | |
1555 | unsigned int sit_segs, start; | |
1556 | char *src_bitmap, *dst_bitmap; | |
1557 | unsigned int bitmap_size; | |
1558 | ||
1559 | /* allocate memory for SIT information */ | |
1560 | sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL); | |
1561 | if (!sit_i) | |
1562 | return -ENOMEM; | |
1563 | ||
1564 | SM_I(sbi)->sit_info = sit_i; | |
1565 | ||
1566 | sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry)); | |
1567 | if (!sit_i->sentries) | |
1568 | return -ENOMEM; | |
1569 | ||
1570 | bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi)); | |
1571 | sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL); | |
1572 | if (!sit_i->dirty_sentries_bitmap) | |
1573 | return -ENOMEM; | |
1574 | ||
1575 | for (start = 0; start < TOTAL_SEGS(sbi); start++) { | |
1576 | sit_i->sentries[start].cur_valid_map | |
1577 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); | |
1578 | sit_i->sentries[start].ckpt_valid_map | |
1579 | = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); | |
1580 | if (!sit_i->sentries[start].cur_valid_map | |
1581 | || !sit_i->sentries[start].ckpt_valid_map) | |
1582 | return -ENOMEM; | |
1583 | } | |
1584 | ||
1585 | if (sbi->segs_per_sec > 1) { | |
53cf9522 | 1586 | sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) * |
351df4b2 JK |
1587 | sizeof(struct sec_entry)); |
1588 | if (!sit_i->sec_entries) | |
1589 | return -ENOMEM; | |
1590 | } | |
1591 | ||
1592 | /* get information related with SIT */ | |
1593 | sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1; | |
1594 | ||
1595 | /* setup SIT bitmap from ckeckpoint pack */ | |
1596 | bitmap_size = __bitmap_size(sbi, SIT_BITMAP); | |
1597 | src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); | |
1598 | ||
79b5793b | 1599 | dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); |
351df4b2 JK |
1600 | if (!dst_bitmap) |
1601 | return -ENOMEM; | |
351df4b2 JK |
1602 | |
1603 | /* init SIT information */ | |
1604 | sit_i->s_ops = &default_salloc_ops; | |
1605 | ||
1606 | sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr); | |
1607 | sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; | |
1608 | sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count); | |
1609 | sit_i->sit_bitmap = dst_bitmap; | |
1610 | sit_i->bitmap_size = bitmap_size; | |
1611 | sit_i->dirty_sentries = 0; | |
1612 | sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; | |
1613 | sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); | |
1614 | sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec; | |
1615 | mutex_init(&sit_i->sentry_lock); | |
1616 | return 0; | |
1617 | } | |
1618 | ||
1619 | static int build_free_segmap(struct f2fs_sb_info *sbi) | |
1620 | { | |
1621 | struct f2fs_sm_info *sm_info = SM_I(sbi); | |
1622 | struct free_segmap_info *free_i; | |
1623 | unsigned int bitmap_size, sec_bitmap_size; | |
1624 | ||
1625 | /* allocate memory for free segmap information */ | |
1626 | free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL); | |
1627 | if (!free_i) | |
1628 | return -ENOMEM; | |
1629 | ||
1630 | SM_I(sbi)->free_info = free_i; | |
1631 | ||
1632 | bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi)); | |
1633 | free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL); | |
1634 | if (!free_i->free_segmap) | |
1635 | return -ENOMEM; | |
1636 | ||
53cf9522 | 1637 | sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi)); |
351df4b2 JK |
1638 | free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL); |
1639 | if (!free_i->free_secmap) | |
1640 | return -ENOMEM; | |
1641 | ||
1642 | /* set all segments as dirty temporarily */ | |
1643 | memset(free_i->free_segmap, 0xff, bitmap_size); | |
1644 | memset(free_i->free_secmap, 0xff, sec_bitmap_size); | |
1645 | ||
1646 | /* init free segmap information */ | |
1647 | free_i->start_segno = | |
1648 | (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr); | |
1649 | free_i->free_segments = 0; | |
1650 | free_i->free_sections = 0; | |
1651 | rwlock_init(&free_i->segmap_lock); | |
1652 | return 0; | |
1653 | } | |
1654 | ||
1655 | static int build_curseg(struct f2fs_sb_info *sbi) | |
1656 | { | |
1042d60f | 1657 | struct curseg_info *array; |
351df4b2 JK |
1658 | int i; |
1659 | ||
1660 | array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL); | |
1661 | if (!array) | |
1662 | return -ENOMEM; | |
1663 | ||
1664 | SM_I(sbi)->curseg_array = array; | |
1665 | ||
1666 | for (i = 0; i < NR_CURSEG_TYPE; i++) { | |
1667 | mutex_init(&array[i].curseg_mutex); | |
1668 | array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL); | |
1669 | if (!array[i].sum_blk) | |
1670 | return -ENOMEM; | |
1671 | array[i].segno = NULL_SEGNO; | |
1672 | array[i].next_blkoff = 0; | |
1673 | } | |
1674 | return restore_curseg_summaries(sbi); | |
1675 | } | |
1676 | ||
1677 | static void build_sit_entries(struct f2fs_sb_info *sbi) | |
1678 | { | |
1679 | struct sit_info *sit_i = SIT_I(sbi); | |
1680 | struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); | |
1681 | struct f2fs_summary_block *sum = curseg->sum_blk; | |
74de593a CY |
1682 | int sit_blk_cnt = SIT_BLK_CNT(sbi); |
1683 | unsigned int i, start, end; | |
1684 | unsigned int readed, start_blk = 0; | |
1685 | int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi)); | |
351df4b2 | 1686 | |
74de593a | 1687 | do { |
662befda | 1688 | readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT); |
74de593a CY |
1689 | |
1690 | start = start_blk * sit_i->sents_per_block; | |
1691 | end = (start_blk + readed) * sit_i->sents_per_block; | |
1692 | ||
1693 | for (; start < end && start < TOTAL_SEGS(sbi); start++) { | |
1694 | struct seg_entry *se = &sit_i->sentries[start]; | |
1695 | struct f2fs_sit_block *sit_blk; | |
1696 | struct f2fs_sit_entry sit; | |
1697 | struct page *page; | |
1698 | ||
1699 | mutex_lock(&curseg->curseg_mutex); | |
1700 | for (i = 0; i < sits_in_cursum(sum); i++) { | |
6c311ec6 CF |
1701 | if (le32_to_cpu(segno_in_journal(sum, i)) |
1702 | == start) { | |
74de593a CY |
1703 | sit = sit_in_journal(sum, i); |
1704 | mutex_unlock(&curseg->curseg_mutex); | |
1705 | goto got_it; | |
1706 | } | |
351df4b2 | 1707 | } |
74de593a CY |
1708 | mutex_unlock(&curseg->curseg_mutex); |
1709 | ||
1710 | page = get_current_sit_page(sbi, start); | |
1711 | sit_blk = (struct f2fs_sit_block *)page_address(page); | |
1712 | sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; | |
1713 | f2fs_put_page(page, 1); | |
351df4b2 | 1714 | got_it: |
74de593a CY |
1715 | check_block_count(sbi, start, &sit); |
1716 | seg_info_from_raw_sit(se, &sit); | |
1717 | if (sbi->segs_per_sec > 1) { | |
1718 | struct sec_entry *e = get_sec_entry(sbi, start); | |
1719 | e->valid_blocks += se->valid_blocks; | |
1720 | } | |
351df4b2 | 1721 | } |
74de593a CY |
1722 | start_blk += readed; |
1723 | } while (start_blk < sit_blk_cnt); | |
351df4b2 JK |
1724 | } |
1725 | ||
1726 | static void init_free_segmap(struct f2fs_sb_info *sbi) | |
1727 | { | |
1728 | unsigned int start; | |
1729 | int type; | |
1730 | ||
1731 | for (start = 0; start < TOTAL_SEGS(sbi); start++) { | |
1732 | struct seg_entry *sentry = get_seg_entry(sbi, start); | |
1733 | if (!sentry->valid_blocks) | |
1734 | __set_free(sbi, start); | |
1735 | } | |
1736 | ||
1737 | /* set use the current segments */ | |
1738 | for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) { | |
1739 | struct curseg_info *curseg_t = CURSEG_I(sbi, type); | |
1740 | __set_test_and_inuse(sbi, curseg_t->segno); | |
1741 | } | |
1742 | } | |
1743 | ||
1744 | static void init_dirty_segmap(struct f2fs_sb_info *sbi) | |
1745 | { | |
1746 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
1747 | struct free_segmap_info *free_i = FREE_I(sbi); | |
8736fbf0 | 1748 | unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi); |
351df4b2 JK |
1749 | unsigned short valid_blocks; |
1750 | ||
8736fbf0 | 1751 | while (1) { |
351df4b2 | 1752 | /* find dirty segment based on free segmap */ |
8736fbf0 NJ |
1753 | segno = find_next_inuse(free_i, total_segs, offset); |
1754 | if (segno >= total_segs) | |
351df4b2 JK |
1755 | break; |
1756 | offset = segno + 1; | |
1757 | valid_blocks = get_valid_blocks(sbi, segno, 0); | |
1758 | if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks) | |
1759 | continue; | |
1760 | mutex_lock(&dirty_i->seglist_lock); | |
1761 | __locate_dirty_segment(sbi, segno, DIRTY); | |
1762 | mutex_unlock(&dirty_i->seglist_lock); | |
1763 | } | |
1764 | } | |
1765 | ||
5ec4e49f | 1766 | static int init_victim_secmap(struct f2fs_sb_info *sbi) |
351df4b2 JK |
1767 | { |
1768 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
5ec4e49f | 1769 | unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi)); |
351df4b2 | 1770 | |
5ec4e49f JK |
1771 | dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL); |
1772 | if (!dirty_i->victim_secmap) | |
351df4b2 JK |
1773 | return -ENOMEM; |
1774 | return 0; | |
1775 | } | |
1776 | ||
1777 | static int build_dirty_segmap(struct f2fs_sb_info *sbi) | |
1778 | { | |
1779 | struct dirty_seglist_info *dirty_i; | |
1780 | unsigned int bitmap_size, i; | |
1781 | ||
1782 | /* allocate memory for dirty segments list information */ | |
1783 | dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL); | |
1784 | if (!dirty_i) | |
1785 | return -ENOMEM; | |
1786 | ||
1787 | SM_I(sbi)->dirty_info = dirty_i; | |
1788 | mutex_init(&dirty_i->seglist_lock); | |
1789 | ||
1790 | bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi)); | |
1791 | ||
1792 | for (i = 0; i < NR_DIRTY_TYPE; i++) { | |
1793 | dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL); | |
351df4b2 JK |
1794 | if (!dirty_i->dirty_segmap[i]) |
1795 | return -ENOMEM; | |
1796 | } | |
1797 | ||
1798 | init_dirty_segmap(sbi); | |
5ec4e49f | 1799 | return init_victim_secmap(sbi); |
351df4b2 JK |
1800 | } |
1801 | ||
0a8165d7 | 1802 | /* |
351df4b2 JK |
1803 | * Update min, max modified time for cost-benefit GC algorithm |
1804 | */ | |
1805 | static void init_min_max_mtime(struct f2fs_sb_info *sbi) | |
1806 | { | |
1807 | struct sit_info *sit_i = SIT_I(sbi); | |
1808 | unsigned int segno; | |
1809 | ||
1810 | mutex_lock(&sit_i->sentry_lock); | |
1811 | ||
1812 | sit_i->min_mtime = LLONG_MAX; | |
1813 | ||
1814 | for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) { | |
1815 | unsigned int i; | |
1816 | unsigned long long mtime = 0; | |
1817 | ||
1818 | for (i = 0; i < sbi->segs_per_sec; i++) | |
1819 | mtime += get_seg_entry(sbi, segno + i)->mtime; | |
1820 | ||
1821 | mtime = div_u64(mtime, sbi->segs_per_sec); | |
1822 | ||
1823 | if (sit_i->min_mtime > mtime) | |
1824 | sit_i->min_mtime = mtime; | |
1825 | } | |
1826 | sit_i->max_mtime = get_mtime(sbi); | |
1827 | mutex_unlock(&sit_i->sentry_lock); | |
1828 | } | |
1829 | ||
1830 | int build_segment_manager(struct f2fs_sb_info *sbi) | |
1831 | { | |
1832 | struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); | |
1833 | struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); | |
6b4afdd7 | 1834 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
1042d60f | 1835 | struct f2fs_sm_info *sm_info; |
351df4b2 JK |
1836 | int err; |
1837 | ||
1838 | sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL); | |
1839 | if (!sm_info) | |
1840 | return -ENOMEM; | |
1841 | ||
1842 | /* init sm info */ | |
1843 | sbi->sm_info = sm_info; | |
1844 | INIT_LIST_HEAD(&sm_info->wblist_head); | |
1845 | spin_lock_init(&sm_info->wblist_lock); | |
1846 | sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); | |
1847 | sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); | |
1848 | sm_info->segment_count = le32_to_cpu(raw_super->segment_count); | |
1849 | sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); | |
1850 | sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); | |
1851 | sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); | |
1852 | sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); | |
58c41035 JK |
1853 | sm_info->rec_prefree_segments = sm_info->main_segments * |
1854 | DEF_RECLAIM_PREFREE_SEGMENTS / 100; | |
216fbd64 JK |
1855 | sm_info->ipu_policy = F2FS_IPU_DISABLE; |
1856 | sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; | |
351df4b2 | 1857 | |
7fd9e544 JK |
1858 | INIT_LIST_HEAD(&sm_info->discard_list); |
1859 | sm_info->nr_discards = 0; | |
1860 | sm_info->max_discards = 0; | |
1861 | ||
6b4afdd7 JK |
1862 | if (test_opt(sbi, FLUSH_MERGE)) { |
1863 | spin_lock_init(&sm_info->issue_lock); | |
1864 | init_waitqueue_head(&sm_info->flush_wait_queue); | |
1865 | ||
1866 | sm_info->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, | |
1867 | "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev)); | |
1868 | if (IS_ERR(sm_info->f2fs_issue_flush)) | |
1869 | return PTR_ERR(sm_info->f2fs_issue_flush); | |
1870 | } | |
1871 | ||
351df4b2 JK |
1872 | err = build_sit_info(sbi); |
1873 | if (err) | |
1874 | return err; | |
1875 | err = build_free_segmap(sbi); | |
1876 | if (err) | |
1877 | return err; | |
1878 | err = build_curseg(sbi); | |
1879 | if (err) | |
1880 | return err; | |
1881 | ||
1882 | /* reinit free segmap based on SIT */ | |
1883 | build_sit_entries(sbi); | |
1884 | ||
1885 | init_free_segmap(sbi); | |
1886 | err = build_dirty_segmap(sbi); | |
1887 | if (err) | |
1888 | return err; | |
1889 | ||
1890 | init_min_max_mtime(sbi); | |
1891 | return 0; | |
1892 | } | |
1893 | ||
1894 | static void discard_dirty_segmap(struct f2fs_sb_info *sbi, | |
1895 | enum dirty_type dirty_type) | |
1896 | { | |
1897 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
1898 | ||
1899 | mutex_lock(&dirty_i->seglist_lock); | |
1900 | kfree(dirty_i->dirty_segmap[dirty_type]); | |
1901 | dirty_i->nr_dirty[dirty_type] = 0; | |
1902 | mutex_unlock(&dirty_i->seglist_lock); | |
1903 | } | |
1904 | ||
5ec4e49f | 1905 | static void destroy_victim_secmap(struct f2fs_sb_info *sbi) |
351df4b2 JK |
1906 | { |
1907 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
5ec4e49f | 1908 | kfree(dirty_i->victim_secmap); |
351df4b2 JK |
1909 | } |
1910 | ||
1911 | static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) | |
1912 | { | |
1913 | struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); | |
1914 | int i; | |
1915 | ||
1916 | if (!dirty_i) | |
1917 | return; | |
1918 | ||
1919 | /* discard pre-free/dirty segments list */ | |
1920 | for (i = 0; i < NR_DIRTY_TYPE; i++) | |
1921 | discard_dirty_segmap(sbi, i); | |
1922 | ||
5ec4e49f | 1923 | destroy_victim_secmap(sbi); |
351df4b2 JK |
1924 | SM_I(sbi)->dirty_info = NULL; |
1925 | kfree(dirty_i); | |
1926 | } | |
1927 | ||
1928 | static void destroy_curseg(struct f2fs_sb_info *sbi) | |
1929 | { | |
1930 | struct curseg_info *array = SM_I(sbi)->curseg_array; | |
1931 | int i; | |
1932 | ||
1933 | if (!array) | |
1934 | return; | |
1935 | SM_I(sbi)->curseg_array = NULL; | |
1936 | for (i = 0; i < NR_CURSEG_TYPE; i++) | |
1937 | kfree(array[i].sum_blk); | |
1938 | kfree(array); | |
1939 | } | |
1940 | ||
1941 | static void destroy_free_segmap(struct f2fs_sb_info *sbi) | |
1942 | { | |
1943 | struct free_segmap_info *free_i = SM_I(sbi)->free_info; | |
1944 | if (!free_i) | |
1945 | return; | |
1946 | SM_I(sbi)->free_info = NULL; | |
1947 | kfree(free_i->free_segmap); | |
1948 | kfree(free_i->free_secmap); | |
1949 | kfree(free_i); | |
1950 | } | |
1951 | ||
1952 | static void destroy_sit_info(struct f2fs_sb_info *sbi) | |
1953 | { | |
1954 | struct sit_info *sit_i = SIT_I(sbi); | |
1955 | unsigned int start; | |
1956 | ||
1957 | if (!sit_i) | |
1958 | return; | |
1959 | ||
1960 | if (sit_i->sentries) { | |
1961 | for (start = 0; start < TOTAL_SEGS(sbi); start++) { | |
1962 | kfree(sit_i->sentries[start].cur_valid_map); | |
1963 | kfree(sit_i->sentries[start].ckpt_valid_map); | |
1964 | } | |
1965 | } | |
1966 | vfree(sit_i->sentries); | |
1967 | vfree(sit_i->sec_entries); | |
1968 | kfree(sit_i->dirty_sentries_bitmap); | |
1969 | ||
1970 | SM_I(sbi)->sit_info = NULL; | |
1971 | kfree(sit_i->sit_bitmap); | |
1972 | kfree(sit_i); | |
1973 | } | |
1974 | ||
1975 | void destroy_segment_manager(struct f2fs_sb_info *sbi) | |
1976 | { | |
1977 | struct f2fs_sm_info *sm_info = SM_I(sbi); | |
3b03f724 CY |
1978 | if (!sm_info) |
1979 | return; | |
6b4afdd7 JK |
1980 | if (sm_info->f2fs_issue_flush) |
1981 | kthread_stop(sm_info->f2fs_issue_flush); | |
351df4b2 JK |
1982 | destroy_dirty_segmap(sbi); |
1983 | destroy_curseg(sbi); | |
1984 | destroy_free_segmap(sbi); | |
1985 | destroy_sit_info(sbi); | |
1986 | sbi->sm_info = NULL; | |
1987 | kfree(sm_info); | |
1988 | } | |
7fd9e544 JK |
1989 | |
1990 | int __init create_segment_manager_caches(void) | |
1991 | { | |
1992 | discard_entry_slab = f2fs_kmem_cache_create("discard_entry", | |
e8512d2e | 1993 | sizeof(struct discard_entry)); |
7fd9e544 JK |
1994 | if (!discard_entry_slab) |
1995 | return -ENOMEM; | |
6b4afdd7 JK |
1996 | flush_cmd_slab = f2fs_kmem_cache_create("flush_command", |
1997 | sizeof(struct flush_cmd)); | |
1998 | if (!flush_cmd_slab) { | |
1999 | kmem_cache_destroy(discard_entry_slab); | |
2000 | return -ENOMEM; | |
2001 | } | |
7fd9e544 JK |
2002 | return 0; |
2003 | } | |
2004 | ||
2005 | void destroy_segment_manager_caches(void) | |
2006 | { | |
2007 | kmem_cache_destroy(discard_entry_slab); | |
6b4afdd7 | 2008 | kmem_cache_destroy(flush_cmd_slab); |
7fd9e544 | 2009 | } |