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