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