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