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