blob: c5eeb44ace613d60f1f1deb8403b3227b6e2bc1b [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
Jaegeuk Kim6f6541b2014-07-23 09:57:31 -0700269 if (test_opt(sbi, NOBARRIER))
270 return 0;
271
Evan McClainf3f030d2014-07-17 21:16:35 -0400272 if (!test_opt(sbi, FLUSH_MERGE))
273 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
274
275 init_completion(&cmd.wait);
276 cmd.next = NULL;
277
278 spin_lock(&fcc->issue_lock);
279 if (fcc->issue_list)
280 fcc->issue_tail->next = &cmd;
281 else
282 fcc->issue_list = &cmd;
283 fcc->issue_tail = &cmd;
284 spin_unlock(&fcc->issue_lock);
285
286 if (!fcc->dispatch_list)
287 wake_up(&fcc->flush_wait_queue);
288
289 wait_for_completion(&cmd.wait);
290
291 return cmd.ret;
292}
293
294int create_flush_cmd_control(struct f2fs_sb_info *sbi)
295{
296 dev_t dev = sbi->sb->s_bdev->bd_dev;
297 struct flush_cmd_control *fcc;
298 int err = 0;
299
300 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
301 if (!fcc)
302 return -ENOMEM;
303 spin_lock_init(&fcc->issue_lock);
304 init_waitqueue_head(&fcc->flush_wait_queue);
305 SM_I(sbi)->cmd_control_info = fcc;
306 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
307 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
308 if (IS_ERR(fcc->f2fs_issue_flush)) {
309 err = PTR_ERR(fcc->f2fs_issue_flush);
310 kfree(fcc);
311 SM_I(sbi)->cmd_control_info = NULL;
312 return err;
313 }
314
315 return err;
316}
317
318void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
319{
320 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
321
322 if (fcc && fcc->f2fs_issue_flush)
323 kthread_stop(fcc->f2fs_issue_flush);
324 kfree(fcc);
325 SM_I(sbi)->cmd_control_info = NULL;
326}
327
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800328static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
329 enum dirty_type dirty_type)
330{
331 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
332
333 /* need not be added */
334 if (IS_CURSEG(sbi, segno))
335 return;
336
337 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
338 dirty_i->nr_dirty[dirty_type]++;
339
340 if (dirty_type == DIRTY) {
341 struct seg_entry *sentry = get_seg_entry(sbi, segno);
342 enum dirty_type t = sentry->type;
343
344 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
345 dirty_i->nr_dirty[t]++;
346 }
347}
348
349static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
350 enum dirty_type dirty_type)
351{
352 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
353
354 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
355 dirty_i->nr_dirty[dirty_type]--;
356
357 if (dirty_type == DIRTY) {
358 struct seg_entry *sentry = get_seg_entry(sbi, segno);
359 enum dirty_type t = sentry->type;
360
361 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
362 dirty_i->nr_dirty[t]--;
363
364 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
365 clear_bit(GET_SECNO(sbi, segno),
366 dirty_i->victim_secmap);
367 }
368}
369
370/*
371 * Should not occur error such as -ENOMEM.
372 * Adding dirty entry into seglist is not critical operation.
373 * If a given segment is one of current working segments, it won't be added.
374 */
375static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
376{
377 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
378 unsigned short valid_blocks;
379
380 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
381 return;
382
383 mutex_lock(&dirty_i->seglist_lock);
384
385 valid_blocks = get_valid_blocks(sbi, segno, 0);
386
387 if (valid_blocks == 0) {
388 __locate_dirty_segment(sbi, segno, PRE);
389 __remove_dirty_segment(sbi, segno, DIRTY);
390 } else if (valid_blocks < sbi->blocks_per_seg) {
391 __locate_dirty_segment(sbi, segno, DIRTY);
392 } else {
393 /* Recovery routine with SSR needs this */
394 __remove_dirty_segment(sbi, segno, DIRTY);
395 }
396
397 mutex_unlock(&dirty_i->seglist_lock);
398}
399
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900400static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Changman Leeb1a94e82013-11-15 10:42:51 +0900401 block_t blkstart, block_t blklen)
402{
403 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
404 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
Changman Leeb1a94e82013-11-15 10:42:51 +0900405 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900406 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
407}
408
Dan Pasanen97e8a342014-08-25 19:11:31 -0500409void discard_next_dnode(struct f2fs_sb_info *sbi)
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900410{
Dan Pasanen97e8a342014-08-25 19:11:31 -0500411 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
412 block_t blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
413
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900414 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
415 struct page *page = grab_meta_page(sbi, blkaddr);
416 /* zero-filled page */
417 set_page_dirty(page);
418 f2fs_put_page(page, 1);
419 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900420}
421
422static void add_discard_addrs(struct f2fs_sb_info *sbi,
423 unsigned int segno, struct seg_entry *se)
424{
425 struct list_head *head = &SM_I(sbi)->discard_list;
426 struct discard_entry *new;
427 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
428 int max_blocks = sbi->blocks_per_seg;
429 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
430 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
431 unsigned long dmap[entries];
432 unsigned int start = 0, end = -1;
433 int i;
434
435 if (!test_opt(sbi, DISCARD))
436 return;
437
438 /* zero block will be discarded through the prefree list */
439 if (!se->valid_blocks || se->valid_blocks == max_blocks)
440 return;
441
442 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
443 for (i = 0; i < entries; i++)
444 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
445
446 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
447 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
448 if (start >= max_blocks)
449 break;
450
451 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
452
453 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
454 INIT_LIST_HEAD(&new->list);
455 new->blkaddr = START_BLOCK(sbi, segno) + start;
456 new->len = end - start;
457
458 list_add_tail(&new->list, head);
459 SM_I(sbi)->nr_discards += end - start;
460 }
461}
462
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800463/*
464 * Should call clear_prefree_segments after checkpoint is done.
465 */
466static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
467{
468 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Dan Pasanen97e8a342014-08-25 19:11:31 -0500469 unsigned int segno = -1;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800470 unsigned int total_segs = TOTAL_SEGS(sbi);
471
472 mutex_lock(&dirty_i->seglist_lock);
Dan Pasanen97e8a342014-08-25 19:11:31 -0500473 while (1) {
474 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
475 segno + 1);
476 if (segno >= total_segs)
477 break;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800478 __set_test_and_free(sbi, segno);
Dan Pasanen97e8a342014-08-25 19:11:31 -0500479 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800480 mutex_unlock(&dirty_i->seglist_lock);
481}
482
483void clear_prefree_segments(struct f2fs_sb_info *sbi)
484{
Changman Leeb1a94e82013-11-15 10:42:51 +0900485 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu48c561a2014-03-29 11:33:17 +0800486 struct discard_entry *entry, *this;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800487 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
488 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
489 unsigned int total_segs = TOTAL_SEGS(sbi);
490 unsigned int start = 0, end = -1;
491
492 mutex_lock(&dirty_i->seglist_lock);
493
494 while (1) {
495 int i;
496 start = find_next_bit(prefree_map, total_segs, end + 1);
497 if (start >= total_segs)
498 break;
499 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
500
501 for (i = start; i < end; i++)
502 clear_bit(i, prefree_map);
503
504 dirty_i->nr_dirty[PRE] -= end - start;
505
506 if (!test_opt(sbi, DISCARD))
507 continue;
508
Changman Leeb1a94e82013-11-15 10:42:51 +0900509 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
510 (end - start) << sbi->log_blocks_per_seg);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800511 }
512 mutex_unlock(&dirty_i->seglist_lock);
Changman Leeb1a94e82013-11-15 10:42:51 +0900513
514 /* send small discards */
Chao Yu48c561a2014-03-29 11:33:17 +0800515 list_for_each_entry_safe(entry, this, head, list) {
Changman Leeb1a94e82013-11-15 10:42:51 +0900516 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
517 list_del(&entry->list);
518 SM_I(sbi)->nr_discards -= entry->len;
519 kmem_cache_free(discard_entry_slab, entry);
520 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800521}
522
523static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
524{
525 struct sit_info *sit_i = SIT_I(sbi);
526 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
527 sit_i->dirty_sentries++;
528}
529
530static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
531 unsigned int segno, int modified)
532{
533 struct seg_entry *se = get_seg_entry(sbi, segno);
534 se->type = type;
535 if (modified)
536 __mark_sit_entry_dirty(sbi, segno);
537}
538
539static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
540{
541 struct seg_entry *se;
542 unsigned int segno, offset;
543 long int new_vblocks;
544
545 segno = GET_SEGNO(sbi, blkaddr);
546
547 se = get_seg_entry(sbi, segno);
548 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim26d12822014-02-04 13:01:10 +0900549 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800550
551 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
552 (new_vblocks > sbi->blocks_per_seg)));
553
554 se->valid_blocks = new_vblocks;
555 se->mtime = get_mtime(sbi);
556 SIT_I(sbi)->max_mtime = se->mtime;
557
558 /* Update valid block bitmap */
559 if (del > 0) {
560 if (f2fs_set_bit(offset, se->cur_valid_map))
561 BUG();
562 } else {
563 if (!f2fs_clear_bit(offset, se->cur_valid_map))
564 BUG();
565 }
566 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
567 se->ckpt_valid_blocks += del;
568
569 __mark_sit_entry_dirty(sbi, segno);
570
571 /* update total number of valid blocks to be written in ckpt area */
572 SIT_I(sbi)->written_valid_blocks += del;
573
574 if (sbi->segs_per_sec > 1)
575 get_sec_entry(sbi, segno)->valid_blocks += del;
576}
577
Jaegeuk Kim655d2c12014-01-28 12:22:14 +0900578void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800579{
Jaegeuk Kim655d2c12014-01-28 12:22:14 +0900580 update_sit_entry(sbi, new, 1);
581 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
582 update_sit_entry(sbi, old, -1);
583
584 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
585 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800586}
587
588void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
589{
590 unsigned int segno = GET_SEGNO(sbi, addr);
591 struct sit_info *sit_i = SIT_I(sbi);
592
593 f2fs_bug_on(addr == NULL_ADDR);
594 if (addr == NEW_ADDR)
595 return;
596
597 /* add it into sit main buffer */
598 mutex_lock(&sit_i->sentry_lock);
599
600 update_sit_entry(sbi, addr, -1);
601
602 /* add it into dirty seglist */
603 locate_dirty_segment(sbi, segno);
604
605 mutex_unlock(&sit_i->sentry_lock);
606}
607
608/*
609 * This function should be resided under the curseg_mutex lock
610 */
611static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
612 struct f2fs_summary *sum)
613{
614 struct curseg_info *curseg = CURSEG_I(sbi, type);
615 void *addr = curseg->sum_blk;
616 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
617 memcpy(addr, sum, sizeof(struct f2fs_summary));
618}
619
620/*
621 * Calculate the number of current summary pages for writing
622 */
623int npages_for_summary_flush(struct f2fs_sb_info *sbi)
624{
625 int valid_sum_count = 0;
626 int i, sum_in_page;
627
628 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
629 if (sbi->ckpt->alloc_type[i] == SSR)
630 valid_sum_count += sbi->blocks_per_seg;
631 else
632 valid_sum_count += curseg_blkoff(sbi, i);
633 }
634
635 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
636 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
637 if (valid_sum_count <= sum_in_page)
638 return 1;
639 else if ((valid_sum_count - sum_in_page) <=
640 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
641 return 2;
642 return 3;
643}
644
645/*
646 * Caller should put this summary page
647 */
648struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
649{
650 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
651}
652
653static void write_sum_page(struct f2fs_sb_info *sbi,
654 struct f2fs_summary_block *sum_blk, block_t blk_addr)
655{
656 struct page *page = grab_meta_page(sbi, blk_addr);
657 void *kaddr = page_address(page);
658 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
659 set_page_dirty(page);
660 f2fs_put_page(page, 1);
661}
662
663static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
664{
665 struct curseg_info *curseg = CURSEG_I(sbi, type);
666 unsigned int segno = curseg->segno + 1;
667 struct free_segmap_info *free_i = FREE_I(sbi);
668
669 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
670 return !test_bit(segno, free_i->free_segmap);
671 return 0;
672}
673
674/*
675 * Find a new segment from the free segments bitmap to right order
676 * This function should be returned with success, otherwise BUG
677 */
678static void get_new_segment(struct f2fs_sb_info *sbi,
679 unsigned int *newseg, bool new_sec, int dir)
680{
681 struct free_segmap_info *free_i = FREE_I(sbi);
682 unsigned int segno, secno, zoneno;
683 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
684 unsigned int hint = *newseg / sbi->segs_per_sec;
685 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
686 unsigned int left_start = hint;
687 bool init = true;
688 int go_left = 0;
689 int i;
690
691 write_lock(&free_i->segmap_lock);
692
693 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
694 segno = find_next_zero_bit(free_i->free_segmap,
695 TOTAL_SEGS(sbi), *newseg + 1);
696 if (segno - *newseg < sbi->segs_per_sec -
697 (*newseg % sbi->segs_per_sec))
698 goto got_it;
699 }
700find_other_zone:
701 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
702 if (secno >= TOTAL_SECS(sbi)) {
703 if (dir == ALLOC_RIGHT) {
704 secno = find_next_zero_bit(free_i->free_secmap,
705 TOTAL_SECS(sbi), 0);
706 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
707 } else {
708 go_left = 1;
709 left_start = hint - 1;
710 }
711 }
712 if (go_left == 0)
713 goto skip_left;
714
715 while (test_bit(left_start, free_i->free_secmap)) {
716 if (left_start > 0) {
717 left_start--;
718 continue;
719 }
720 left_start = find_next_zero_bit(free_i->free_secmap,
721 TOTAL_SECS(sbi), 0);
722 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
723 break;
724 }
725 secno = left_start;
726skip_left:
727 hint = secno;
728 segno = secno * sbi->segs_per_sec;
729 zoneno = secno / sbi->secs_per_zone;
730
731 /* give up on finding another zone */
732 if (!init)
733 goto got_it;
734 if (sbi->secs_per_zone == 1)
735 goto got_it;
736 if (zoneno == old_zoneno)
737 goto got_it;
738 if (dir == ALLOC_LEFT) {
739 if (!go_left && zoneno + 1 >= total_zones)
740 goto got_it;
741 if (go_left && zoneno == 0)
742 goto got_it;
743 }
744 for (i = 0; i < NR_CURSEG_TYPE; i++)
745 if (CURSEG_I(sbi, i)->zone == zoneno)
746 break;
747
748 if (i < NR_CURSEG_TYPE) {
749 /* zone is in user, try another */
750 if (go_left)
751 hint = zoneno * sbi->secs_per_zone - 1;
752 else if (zoneno + 1 >= total_zones)
753 hint = 0;
754 else
755 hint = (zoneno + 1) * sbi->secs_per_zone;
756 init = false;
757 goto find_other_zone;
758 }
759got_it:
760 /* set it as dirty segment in free segmap */
761 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
762 __set_inuse(sbi, segno);
763 *newseg = segno;
764 write_unlock(&free_i->segmap_lock);
765}
766
767static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
768{
769 struct curseg_info *curseg = CURSEG_I(sbi, type);
770 struct summary_footer *sum_footer;
771
772 curseg->segno = curseg->next_segno;
773 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
774 curseg->next_blkoff = 0;
775 curseg->next_segno = NULL_SEGNO;
776
777 sum_footer = &(curseg->sum_blk->footer);
778 memset(sum_footer, 0, sizeof(struct summary_footer));
779 if (IS_DATASEG(type))
780 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
781 if (IS_NODESEG(type))
782 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
783 __set_sit_entry_type(sbi, type, curseg->segno, modified);
784}
785
786/*
787 * Allocate a current working segment.
788 * This function always allocates a free segment in LFS manner.
789 */
790static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
791{
792 struct curseg_info *curseg = CURSEG_I(sbi, type);
793 unsigned int segno = curseg->segno;
794 int dir = ALLOC_LEFT;
795
796 write_sum_page(sbi, curseg->sum_blk,
797 GET_SUM_BLOCK(sbi, segno));
798 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
799 dir = ALLOC_RIGHT;
800
801 if (test_opt(sbi, NOHEAP))
802 dir = ALLOC_RIGHT;
803
804 get_new_segment(sbi, &segno, new_sec, dir);
805 curseg->next_segno = segno;
806 reset_curseg(sbi, type, 1);
807 curseg->alloc_type = LFS;
808}
809
810static void __next_free_blkoff(struct f2fs_sb_info *sbi,
811 struct curseg_info *seg, block_t start)
812{
813 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leeb1a94e82013-11-15 10:42:51 +0900814 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
815 unsigned long target_map[entries];
816 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
817 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
818 int i, pos;
819
820 for (i = 0; i < entries; i++)
821 target_map[i] = ckpt_map[i] | cur_map[i];
822
823 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
824
825 seg->next_blkoff = pos;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800826}
827
828/*
829 * If a segment is written by LFS manner, next block offset is just obtained
830 * by increasing the current block offset. However, if a segment is written by
831 * SSR manner, next block offset obtained by calling __next_free_blkoff
832 */
833static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
834 struct curseg_info *seg)
835{
836 if (seg->alloc_type == SSR)
837 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
838 else
839 seg->next_blkoff++;
840}
841
842/*
843 * This function always allocates a used segment (from dirty seglist) by SSR
844 * manner, so it should recover the existing segment information of valid blocks
845 */
846static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
847{
848 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
849 struct curseg_info *curseg = CURSEG_I(sbi, type);
850 unsigned int new_segno = curseg->next_segno;
851 struct f2fs_summary_block *sum_node;
852 struct page *sum_page;
853
854 write_sum_page(sbi, curseg->sum_blk,
855 GET_SUM_BLOCK(sbi, curseg->segno));
856 __set_test_and_inuse(sbi, new_segno);
857
858 mutex_lock(&dirty_i->seglist_lock);
859 __remove_dirty_segment(sbi, new_segno, PRE);
860 __remove_dirty_segment(sbi, new_segno, DIRTY);
861 mutex_unlock(&dirty_i->seglist_lock);
862
863 reset_curseg(sbi, type, 1);
864 curseg->alloc_type = SSR;
865 __next_free_blkoff(sbi, curseg, 0);
866
867 if (reuse) {
868 sum_page = get_sum_page(sbi, new_segno);
869 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
870 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
871 f2fs_put_page(sum_page, 1);
872 }
873}
874
875static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
876{
877 struct curseg_info *curseg = CURSEG_I(sbi, type);
878 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
879
880 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
881 return v_ops->get_victim(sbi,
882 &(curseg)->next_segno, BG_GC, type, SSR);
883
884 /* For data segments, let's do SSR more intensively */
885 for (; type >= CURSEG_HOT_DATA; type--)
886 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
887 BG_GC, type, SSR))
888 return 1;
889 return 0;
890}
891
892/*
893 * flush out current segment and replace it with new segment
894 * This function should be returned with success, otherwise BUG
895 */
896static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
897 int type, bool force)
898{
899 struct curseg_info *curseg = CURSEG_I(sbi, type);
900
901 if (force)
902 new_curseg(sbi, type, true);
903 else if (type == CURSEG_WARM_NODE)
904 new_curseg(sbi, type, false);
905 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
906 new_curseg(sbi, type, false);
907 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
908 change_curseg(sbi, type, true);
909 else
910 new_curseg(sbi, type, false);
911
912 stat_inc_seg_type(sbi, curseg);
913}
914
915void allocate_new_segments(struct f2fs_sb_info *sbi)
916{
917 struct curseg_info *curseg;
918 unsigned int old_curseg;
919 int i;
920
921 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
922 curseg = CURSEG_I(sbi, i);
923 old_curseg = curseg->segno;
924 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
925 locate_dirty_segment(sbi, old_curseg);
926 }
927}
928
929static const struct segment_allocation default_salloc_ops = {
930 .allocate_segment = allocate_segment_by_default,
931};
932
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800933static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
934{
935 struct curseg_info *curseg = CURSEG_I(sbi, type);
936 if (curseg->next_blkoff < sbi->blocks_per_seg)
937 return true;
938 return false;
939}
940
941static int __get_segment_type_2(struct page *page, enum page_type p_type)
942{
943 if (p_type == DATA)
944 return CURSEG_HOT_DATA;
945 else
946 return CURSEG_HOT_NODE;
947}
948
949static int __get_segment_type_4(struct page *page, enum page_type p_type)
950{
951 if (p_type == DATA) {
952 struct inode *inode = page->mapping->host;
953
954 if (S_ISDIR(inode->i_mode))
955 return CURSEG_HOT_DATA;
956 else
957 return CURSEG_COLD_DATA;
958 } else {
959 if (IS_DNODE(page) && !is_cold_node(page))
960 return CURSEG_HOT_NODE;
961 else
962 return CURSEG_COLD_NODE;
963 }
964}
965
966static int __get_segment_type_6(struct page *page, enum page_type p_type)
967{
968 if (p_type == DATA) {
969 struct inode *inode = page->mapping->host;
970
971 if (S_ISDIR(inode->i_mode))
972 return CURSEG_HOT_DATA;
973 else if (is_cold_data(page) || file_is_cold(inode))
974 return CURSEG_COLD_DATA;
975 else
976 return CURSEG_WARM_DATA;
977 } else {
978 if (IS_DNODE(page))
979 return is_cold_node(page) ? CURSEG_WARM_NODE :
980 CURSEG_HOT_NODE;
981 else
982 return CURSEG_COLD_NODE;
983 }
984}
985
986static int __get_segment_type(struct page *page, enum page_type p_type)
987{
988 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
989 switch (sbi->active_logs) {
990 case 2:
991 return __get_segment_type_2(page, p_type);
992 case 4:
993 return __get_segment_type_4(page, p_type);
994 }
995 /* NR_CURSEG_TYPE(6) logs by default */
996 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
997 return __get_segment_type_6(page, p_type);
998}
999
Changman Leeb1a94e82013-11-15 10:42:51 +09001000void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1001 block_t old_blkaddr, block_t *new_blkaddr,
1002 struct f2fs_summary *sum, int type)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001003{
1004 struct sit_info *sit_i = SIT_I(sbi);
1005 struct curseg_info *curseg;
Dan Pasanen97e8a342014-08-25 19:11:31 -05001006 unsigned int old_cursegno;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001007
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001008 curseg = CURSEG_I(sbi, type);
1009
1010 mutex_lock(&curseg->curseg_mutex);
1011
1012 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Dan Pasanen97e8a342014-08-25 19:11:31 -05001013 old_cursegno = curseg->segno;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001014
1015 /*
1016 * __add_sum_entry should be resided under the curseg_mutex
1017 * because, this function updates a summary entry in the
1018 * current summary block.
1019 */
1020 __add_sum_entry(sbi, type, sum);
1021
1022 mutex_lock(&sit_i->sentry_lock);
1023 __refresh_next_blkoff(sbi, curseg);
1024
1025 stat_inc_block_count(sbi, curseg);
1026
Jaegeuk Kim655d2c12014-01-28 12:22:14 +09001027 if (!__has_curseg_space(sbi, type))
1028 sit_i->s_ops->allocate_segment(sbi, type, false);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001029 /*
1030 * SIT information should be updated before segment allocation,
1031 * since SSR needs latest valid block information.
1032 */
1033 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Dan Pasanen97e8a342014-08-25 19:11:31 -05001034 locate_dirty_segment(sbi, old_cursegno);
Jaegeuk Kim655d2c12014-01-28 12:22:14 +09001035
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001036 mutex_unlock(&sit_i->sentry_lock);
1037
Changman Leeb1a94e82013-11-15 10:42:51 +09001038 if (page && IS_NODESEG(type))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001039 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1040
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001041 mutex_unlock(&curseg->curseg_mutex);
1042}
1043
Changman Leeb1a94e82013-11-15 10:42:51 +09001044static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1045 block_t old_blkaddr, block_t *new_blkaddr,
1046 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1047{
1048 int type = __get_segment_type(page, fio->type);
1049
1050 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1051
1052 /* writeout dirty page into bdev */
1053 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
1054}
1055
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001056void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1057{
Changman Leeb1a94e82013-11-15 10:42:51 +09001058 struct f2fs_io_info fio = {
1059 .type = META,
1060 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
1061 };
1062
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001063 set_page_writeback(page);
Changman Leeb1a94e82013-11-15 10:42:51 +09001064 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001065}
1066
1067void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Changman Leeb1a94e82013-11-15 10:42:51 +09001068 struct f2fs_io_info *fio,
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001069 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1070{
1071 struct f2fs_summary sum;
1072 set_summary(&sum, nid, 0, 0);
Changman Leeb1a94e82013-11-15 10:42:51 +09001073 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001074}
1075
Changman Leeb1a94e82013-11-15 10:42:51 +09001076void write_data_page(struct page *page, struct dnode_of_data *dn,
1077 block_t *new_blkaddr, struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001078{
Changman Leeb1a94e82013-11-15 10:42:51 +09001079 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001080 struct f2fs_summary sum;
1081 struct node_info ni;
1082
Changman Leeb1a94e82013-11-15 10:42:51 +09001083 f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001084 get_node_info(sbi, dn->nid, &ni);
1085 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1086
Changman Leeb1a94e82013-11-15 10:42:51 +09001087 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001088}
1089
Changman Leeb1a94e82013-11-15 10:42:51 +09001090void rewrite_data_page(struct page *page, block_t old_blkaddr,
1091 struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001092{
Changman Leeb1a94e82013-11-15 10:42:51 +09001093 struct inode *inode = page->mapping->host;
1094 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1095 f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001096}
1097
1098void recover_data_page(struct f2fs_sb_info *sbi,
1099 struct page *page, struct f2fs_summary *sum,
1100 block_t old_blkaddr, block_t new_blkaddr)
1101{
1102 struct sit_info *sit_i = SIT_I(sbi);
1103 struct curseg_info *curseg;
1104 unsigned int segno, old_cursegno;
1105 struct seg_entry *se;
1106 int type;
1107
1108 segno = GET_SEGNO(sbi, new_blkaddr);
1109 se = get_seg_entry(sbi, segno);
1110 type = se->type;
1111
1112 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1113 if (old_blkaddr == NULL_ADDR)
1114 type = CURSEG_COLD_DATA;
1115 else
1116 type = CURSEG_WARM_DATA;
1117 }
1118 curseg = CURSEG_I(sbi, type);
1119
1120 mutex_lock(&curseg->curseg_mutex);
1121 mutex_lock(&sit_i->sentry_lock);
1122
1123 old_cursegno = curseg->segno;
1124
1125 /* change the current segment */
1126 if (segno != curseg->segno) {
1127 curseg->next_segno = segno;
1128 change_curseg(sbi, type, true);
1129 }
1130
Jaegeuk Kim26d12822014-02-04 13:01:10 +09001131 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001132 __add_sum_entry(sbi, type, sum);
1133
1134 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001135 locate_dirty_segment(sbi, old_cursegno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001136
1137 mutex_unlock(&sit_i->sentry_lock);
1138 mutex_unlock(&curseg->curseg_mutex);
1139}
1140
1141void rewrite_node_page(struct f2fs_sb_info *sbi,
1142 struct page *page, struct f2fs_summary *sum,
1143 block_t old_blkaddr, block_t new_blkaddr)
1144{
1145 struct sit_info *sit_i = SIT_I(sbi);
1146 int type = CURSEG_WARM_NODE;
1147 struct curseg_info *curseg;
1148 unsigned int segno, old_cursegno;
1149 block_t next_blkaddr = next_blkaddr_of_node(page);
1150 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
Changman Leeb1a94e82013-11-15 10:42:51 +09001151 struct f2fs_io_info fio = {
1152 .type = NODE,
1153 .rw = WRITE_SYNC,
1154 };
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001155
1156 curseg = CURSEG_I(sbi, type);
1157
1158 mutex_lock(&curseg->curseg_mutex);
1159 mutex_lock(&sit_i->sentry_lock);
1160
1161 segno = GET_SEGNO(sbi, new_blkaddr);
1162 old_cursegno = curseg->segno;
1163
1164 /* change the current segment */
1165 if (segno != curseg->segno) {
1166 curseg->next_segno = segno;
1167 change_curseg(sbi, type, true);
1168 }
Jaegeuk Kim26d12822014-02-04 13:01:10 +09001169 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001170 __add_sum_entry(sbi, type, sum);
1171
1172 /* change the current log to the next block addr in advance */
1173 if (next_segno != segno) {
1174 curseg->next_segno = next_segno;
1175 change_curseg(sbi, type, true);
1176 }
Jaegeuk Kim26d12822014-02-04 13:01:10 +09001177 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001178
1179 /* rewrite node page */
1180 set_page_writeback(page);
Changman Leeb1a94e82013-11-15 10:42:51 +09001181 f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
1182 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001183 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001184 locate_dirty_segment(sbi, old_cursegno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001185
1186 mutex_unlock(&sit_i->sentry_lock);
1187 mutex_unlock(&curseg->curseg_mutex);
1188}
1189
Evan McClainf3f030d2014-07-17 21:16:35 -04001190static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1191 struct page *page, enum page_type type)
1192{
1193 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1194 struct f2fs_bio_info *io = &sbi->write_io[btype];
1195 struct bio_vec *bvec;
1196 int i;
1197
1198 down_read(&io->io_rwsem);
1199 if (!io->bio)
1200 goto out;
1201
1202 __bio_for_each_segment(bvec, io->bio, i, 0) {
1203 if (page == bvec->bv_page) {
1204 up_read(&io->io_rwsem);
1205 return true;
1206 }
1207 }
1208
1209out:
1210 up_read(&io->io_rwsem);
1211 return false;
1212}
1213
Changman Leeb1a94e82013-11-15 10:42:51 +09001214void f2fs_wait_on_page_writeback(struct page *page,
1215 enum page_type type)
1216{
1217 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1218 if (PageWriteback(page)) {
Evan McClainf3f030d2014-07-17 21:16:35 -04001219 if (is_merged_page(sbi, page, type))
1220 f2fs_submit_merged_bio(sbi, type, WRITE);
Changman Leeb1a94e82013-11-15 10:42:51 +09001221 wait_on_page_writeback(page);
1222 }
1223}
1224
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001225static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1226{
1227 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1228 struct curseg_info *seg_i;
1229 unsigned char *kaddr;
1230 struct page *page;
1231 block_t start;
1232 int i, j, offset;
1233
1234 start = start_sum_block(sbi);
1235
1236 page = get_meta_page(sbi, start++);
1237 kaddr = (unsigned char *)page_address(page);
1238
1239 /* Step 1: restore nat cache */
1240 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1241 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1242
1243 /* Step 2: restore sit cache */
1244 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1245 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1246 SUM_JOURNAL_SIZE);
1247 offset = 2 * SUM_JOURNAL_SIZE;
1248
1249 /* Step 3: restore summary entries */
1250 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1251 unsigned short blk_off;
1252 unsigned int segno;
1253
1254 seg_i = CURSEG_I(sbi, i);
1255 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1256 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1257 seg_i->next_segno = segno;
1258 reset_curseg(sbi, i, 0);
1259 seg_i->alloc_type = ckpt->alloc_type[i];
1260 seg_i->next_blkoff = blk_off;
1261
1262 if (seg_i->alloc_type == SSR)
1263 blk_off = sbi->blocks_per_seg;
1264
1265 for (j = 0; j < blk_off; j++) {
1266 struct f2fs_summary *s;
1267 s = (struct f2fs_summary *)(kaddr + offset);
1268 seg_i->sum_blk->entries[j] = *s;
1269 offset += SUMMARY_SIZE;
1270 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1271 SUM_FOOTER_SIZE)
1272 continue;
1273
1274 f2fs_put_page(page, 1);
1275 page = NULL;
1276
1277 page = get_meta_page(sbi, start++);
1278 kaddr = (unsigned char *)page_address(page);
1279 offset = 0;
1280 }
1281 }
1282 f2fs_put_page(page, 1);
1283 return 0;
1284}
1285
1286static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1287{
1288 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1289 struct f2fs_summary_block *sum;
1290 struct curseg_info *curseg;
1291 struct page *new;
1292 unsigned short blk_off;
1293 unsigned int segno = 0;
1294 block_t blk_addr = 0;
1295
1296 /* get segment number and block addr */
1297 if (IS_DATASEG(type)) {
1298 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1299 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1300 CURSEG_HOT_DATA]);
1301 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1302 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1303 else
1304 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1305 } else {
1306 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1307 CURSEG_HOT_NODE]);
1308 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1309 CURSEG_HOT_NODE]);
1310 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1311 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1312 type - CURSEG_HOT_NODE);
1313 else
1314 blk_addr = GET_SUM_BLOCK(sbi, segno);
1315 }
1316
1317 new = get_meta_page(sbi, blk_addr);
1318 sum = (struct f2fs_summary_block *)page_address(new);
1319
1320 if (IS_NODESEG(type)) {
1321 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1322 struct f2fs_summary *ns = &sum->entries[0];
1323 int i;
1324 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1325 ns->version = 0;
1326 ns->ofs_in_node = 0;
1327 }
1328 } else {
Gu Zheng12e374b2014-03-07 18:43:36 +08001329 int err;
1330
1331 err = restore_node_summary(sbi, segno, sum);
1332 if (err) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001333 f2fs_put_page(new, 1);
Gu Zheng12e374b2014-03-07 18:43:36 +08001334 return err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001335 }
1336 }
1337 }
1338
1339 /* set uncompleted segment to curseg */
1340 curseg = CURSEG_I(sbi, type);
1341 mutex_lock(&curseg->curseg_mutex);
1342 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1343 curseg->next_segno = segno;
1344 reset_curseg(sbi, type, 0);
1345 curseg->alloc_type = ckpt->alloc_type[type];
1346 curseg->next_blkoff = blk_off;
1347 mutex_unlock(&curseg->curseg_mutex);
1348 f2fs_put_page(new, 1);
1349 return 0;
1350}
1351
1352static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1353{
1354 int type = CURSEG_HOT_DATA;
Chao Yu32c234e2014-03-17 16:36:24 +08001355 int err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001356
1357 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1358 /* restore for compacted data summary */
1359 if (read_compacted_summaries(sbi))
1360 return -EINVAL;
1361 type = CURSEG_HOT_NODE;
1362 }
1363
Chao Yu32c234e2014-03-17 16:36:24 +08001364 for (; type <= CURSEG_COLD_NODE; type++) {
1365 err = read_normal_summaries(sbi, type);
1366 if (err)
1367 return err;
1368 }
1369
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001370 return 0;
1371}
1372
1373static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1374{
1375 struct page *page;
1376 unsigned char *kaddr;
1377 struct f2fs_summary *summary;
1378 struct curseg_info *seg_i;
1379 int written_size = 0;
1380 int i, j;
1381
1382 page = grab_meta_page(sbi, blkaddr++);
1383 kaddr = (unsigned char *)page_address(page);
1384
1385 /* Step 1: write nat cache */
1386 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1387 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1388 written_size += SUM_JOURNAL_SIZE;
1389
1390 /* Step 2: write sit cache */
1391 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1392 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1393 SUM_JOURNAL_SIZE);
1394 written_size += SUM_JOURNAL_SIZE;
1395
1396 /* Step 3: write summary entries */
1397 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1398 unsigned short blkoff;
1399 seg_i = CURSEG_I(sbi, i);
1400 if (sbi->ckpt->alloc_type[i] == SSR)
1401 blkoff = sbi->blocks_per_seg;
1402 else
1403 blkoff = curseg_blkoff(sbi, i);
1404
1405 for (j = 0; j < blkoff; j++) {
1406 if (!page) {
1407 page = grab_meta_page(sbi, blkaddr++);
1408 kaddr = (unsigned char *)page_address(page);
1409 written_size = 0;
1410 }
1411 summary = (struct f2fs_summary *)(kaddr + written_size);
1412 *summary = seg_i->sum_blk->entries[j];
1413 written_size += SUMMARY_SIZE;
1414
1415 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1416 SUM_FOOTER_SIZE)
1417 continue;
1418
1419 set_page_dirty(page);
1420 f2fs_put_page(page, 1);
1421 page = NULL;
1422 }
1423 }
1424 if (page) {
1425 set_page_dirty(page);
1426 f2fs_put_page(page, 1);
1427 }
1428}
1429
1430static void write_normal_summaries(struct f2fs_sb_info *sbi,
1431 block_t blkaddr, int type)
1432{
1433 int i, end;
1434 if (IS_DATASEG(type))
1435 end = type + NR_CURSEG_DATA_TYPE;
1436 else
1437 end = type + NR_CURSEG_NODE_TYPE;
1438
1439 for (i = type; i < end; i++) {
1440 struct curseg_info *sum = CURSEG_I(sbi, i);
1441 mutex_lock(&sum->curseg_mutex);
1442 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1443 mutex_unlock(&sum->curseg_mutex);
1444 }
1445}
1446
1447void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1448{
1449 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1450 write_compacted_summaries(sbi, start_blk);
1451 else
1452 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1453}
1454
1455void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1456{
1457 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1458 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1459}
1460
1461int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1462 unsigned int val, int alloc)
1463{
1464 int i;
1465
1466 if (type == NAT_JOURNAL) {
1467 for (i = 0; i < nats_in_cursum(sum); i++) {
1468 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1469 return i;
1470 }
1471 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1472 return update_nats_in_cursum(sum, 1);
1473 } else if (type == SIT_JOURNAL) {
1474 for (i = 0; i < sits_in_cursum(sum); i++)
1475 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1476 return i;
1477 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1478 return update_sits_in_cursum(sum, 1);
1479 }
1480 return -1;
1481}
1482
1483static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1484 unsigned int segno)
1485{
1486 struct sit_info *sit_i = SIT_I(sbi);
1487 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1488 block_t blk_addr = sit_i->sit_base_addr + offset;
1489
1490 check_seg_range(sbi, segno);
1491
1492 /* calculate sit block address */
1493 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1494 blk_addr += sit_i->sit_blocks;
1495
1496 return get_meta_page(sbi, blk_addr);
1497}
1498
1499static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1500 unsigned int start)
1501{
1502 struct sit_info *sit_i = SIT_I(sbi);
1503 struct page *src_page, *dst_page;
1504 pgoff_t src_off, dst_off;
1505 void *src_addr, *dst_addr;
1506
1507 src_off = current_sit_addr(sbi, start);
1508 dst_off = next_sit_addr(sbi, src_off);
1509
1510 /* get current sit block page without lock */
1511 src_page = get_meta_page(sbi, src_off);
1512 dst_page = grab_meta_page(sbi, dst_off);
1513 f2fs_bug_on(PageDirty(src_page));
1514
1515 src_addr = page_address(src_page);
1516 dst_addr = page_address(dst_page);
1517 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1518
1519 set_page_dirty(dst_page);
1520 f2fs_put_page(src_page, 1);
1521
1522 set_to_next_sit(sit_i, start);
1523
1524 return dst_page;
1525}
1526
1527static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1528{
1529 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1530 struct f2fs_summary_block *sum = curseg->sum_blk;
1531 int i;
1532
1533 /*
1534 * If the journal area in the current summary is full of sit entries,
1535 * all the sit entries will be flushed. Otherwise the sit entries
1536 * are not able to replace with newly hot sit entries.
1537 */
1538 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1539 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1540 unsigned int segno;
1541 segno = le32_to_cpu(segno_in_journal(sum, i));
1542 __mark_sit_entry_dirty(sbi, segno);
1543 }
1544 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1545 return true;
1546 }
1547 return false;
1548}
1549
1550/*
1551 * CP calls this function, which flushes SIT entries including sit_journal,
1552 * and moves prefree segs to free segs.
1553 */
1554void flush_sit_entries(struct f2fs_sb_info *sbi)
1555{
1556 struct sit_info *sit_i = SIT_I(sbi);
1557 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1558 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1559 struct f2fs_summary_block *sum = curseg->sum_blk;
1560 unsigned long nsegs = TOTAL_SEGS(sbi);
1561 struct page *page = NULL;
1562 struct f2fs_sit_block *raw_sit = NULL;
1563 unsigned int start = 0, end = 0;
Dan Pasanen97e8a342014-08-25 19:11:31 -05001564 unsigned int segno = -1;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001565 bool flushed;
1566
1567 mutex_lock(&curseg->curseg_mutex);
1568 mutex_lock(&sit_i->sentry_lock);
1569
1570 /*
1571 * "flushed" indicates whether sit entries in journal are flushed
1572 * to the SIT area or not.
1573 */
1574 flushed = flush_sits_in_journal(sbi);
1575
Dan Pasanen97e8a342014-08-25 19:11:31 -05001576 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001577 struct seg_entry *se = get_seg_entry(sbi, segno);
1578 int sit_offset, offset;
1579
1580 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1581
Changman Leeb1a94e82013-11-15 10:42:51 +09001582 /* add discard candidates */
1583 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1584 add_discard_addrs(sbi, segno, se);
1585
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001586 if (flushed)
1587 goto to_sit_page;
1588
1589 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1590 if (offset >= 0) {
1591 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1592 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1593 goto flush_done;
1594 }
1595to_sit_page:
1596 if (!page || (start > segno) || (segno > end)) {
1597 if (page) {
1598 f2fs_put_page(page, 1);
1599 page = NULL;
1600 }
1601
1602 start = START_SEGNO(sit_i, segno);
1603 end = start + SIT_ENTRY_PER_BLOCK - 1;
1604
1605 /* read sit block that will be updated */
1606 page = get_next_sit_page(sbi, start);
1607 raw_sit = page_address(page);
1608 }
1609
1610 /* udpate entry in SIT block */
1611 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1612flush_done:
1613 __clear_bit(segno, bitmap);
1614 sit_i->dirty_sentries--;
1615 }
1616 mutex_unlock(&sit_i->sentry_lock);
1617 mutex_unlock(&curseg->curseg_mutex);
1618
1619 /* writeout last modified SIT block */
1620 f2fs_put_page(page, 1);
1621
1622 set_prefree_as_free_segments(sbi);
1623}
1624
1625static int build_sit_info(struct f2fs_sb_info *sbi)
1626{
1627 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1628 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1629 struct sit_info *sit_i;
1630 unsigned int sit_segs, start;
1631 char *src_bitmap, *dst_bitmap;
1632 unsigned int bitmap_size;
1633
1634 /* allocate memory for SIT information */
1635 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1636 if (!sit_i)
1637 return -ENOMEM;
1638
1639 SM_I(sbi)->sit_info = sit_i;
1640
1641 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1642 if (!sit_i->sentries)
1643 return -ENOMEM;
1644
1645 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1646 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1647 if (!sit_i->dirty_sentries_bitmap)
1648 return -ENOMEM;
1649
1650 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1651 sit_i->sentries[start].cur_valid_map
1652 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1653 sit_i->sentries[start].ckpt_valid_map
1654 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1655 if (!sit_i->sentries[start].cur_valid_map
1656 || !sit_i->sentries[start].ckpt_valid_map)
1657 return -ENOMEM;
1658 }
1659
1660 if (sbi->segs_per_sec > 1) {
1661 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1662 sizeof(struct sec_entry));
1663 if (!sit_i->sec_entries)
1664 return -ENOMEM;
1665 }
1666
1667 /* get information related with SIT */
1668 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1669
1670 /* setup SIT bitmap from ckeckpoint pack */
1671 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1672 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1673
1674 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1675 if (!dst_bitmap)
1676 return -ENOMEM;
1677
1678 /* init SIT information */
1679 sit_i->s_ops = &default_salloc_ops;
1680
1681 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1682 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1683 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1684 sit_i->sit_bitmap = dst_bitmap;
1685 sit_i->bitmap_size = bitmap_size;
1686 sit_i->dirty_sentries = 0;
1687 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1688 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1689 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1690 mutex_init(&sit_i->sentry_lock);
1691 return 0;
1692}
1693
1694static int build_free_segmap(struct f2fs_sb_info *sbi)
1695{
1696 struct f2fs_sm_info *sm_info = SM_I(sbi);
1697 struct free_segmap_info *free_i;
1698 unsigned int bitmap_size, sec_bitmap_size;
1699
1700 /* allocate memory for free segmap information */
1701 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1702 if (!free_i)
1703 return -ENOMEM;
1704
1705 SM_I(sbi)->free_info = free_i;
1706
1707 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1708 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1709 if (!free_i->free_segmap)
1710 return -ENOMEM;
1711
1712 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1713 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1714 if (!free_i->free_secmap)
1715 return -ENOMEM;
1716
1717 /* set all segments as dirty temporarily */
1718 memset(free_i->free_segmap, 0xff, bitmap_size);
1719 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1720
1721 /* init free segmap information */
1722 free_i->start_segno =
1723 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1724 free_i->free_segments = 0;
1725 free_i->free_sections = 0;
1726 rwlock_init(&free_i->segmap_lock);
1727 return 0;
1728}
1729
1730static int build_curseg(struct f2fs_sb_info *sbi)
1731{
1732 struct curseg_info *array;
1733 int i;
1734
Evan McClainf3f030d2014-07-17 21:16:35 -04001735 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001736 if (!array)
1737 return -ENOMEM;
1738
1739 SM_I(sbi)->curseg_array = array;
1740
1741 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1742 mutex_init(&array[i].curseg_mutex);
1743 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1744 if (!array[i].sum_blk)
1745 return -ENOMEM;
1746 array[i].segno = NULL_SEGNO;
1747 array[i].next_blkoff = 0;
1748 }
1749 return restore_curseg_summaries(sbi);
1750}
1751
1752static void build_sit_entries(struct f2fs_sb_info *sbi)
1753{
1754 struct sit_info *sit_i = SIT_I(sbi);
1755 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1756 struct f2fs_summary_block *sum = curseg->sum_blk;
Changman Leeb1a94e82013-11-15 10:42:51 +09001757 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1758 unsigned int i, start, end;
1759 unsigned int readed, start_blk = 0;
1760 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001761
Changman Leeb1a94e82013-11-15 10:42:51 +09001762 do {
Chao Yu624b14f2014-02-07 16:11:53 +08001763 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001764
Changman Leeb1a94e82013-11-15 10:42:51 +09001765 start = start_blk * sit_i->sents_per_block;
1766 end = (start_blk + readed) * sit_i->sents_per_block;
1767
1768 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1769 struct seg_entry *se = &sit_i->sentries[start];
1770 struct f2fs_sit_block *sit_blk;
1771 struct f2fs_sit_entry sit;
1772 struct page *page;
1773
1774 mutex_lock(&curseg->curseg_mutex);
1775 for (i = 0; i < sits_in_cursum(sum); i++) {
1776 if (le32_to_cpu(segno_in_journal(sum, i))
1777 == start) {
1778 sit = sit_in_journal(sum, i);
1779 mutex_unlock(&curseg->curseg_mutex);
1780 goto got_it;
1781 }
1782 }
1783 mutex_unlock(&curseg->curseg_mutex);
1784
1785 page = get_current_sit_page(sbi, start);
1786 sit_blk = (struct f2fs_sit_block *)page_address(page);
1787 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1788 f2fs_put_page(page, 1);
1789got_it:
1790 check_block_count(sbi, start, &sit);
1791 seg_info_from_raw_sit(se, &sit);
1792 if (sbi->segs_per_sec > 1) {
1793 struct sec_entry *e = get_sec_entry(sbi, start);
1794 e->valid_blocks += se->valid_blocks;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001795 }
1796 }
Changman Leeb1a94e82013-11-15 10:42:51 +09001797 start_blk += readed;
1798 } while (start_blk < sit_blk_cnt);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001799}
1800
1801static void init_free_segmap(struct f2fs_sb_info *sbi)
1802{
1803 unsigned int start;
1804 int type;
1805
1806 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1807 struct seg_entry *sentry = get_seg_entry(sbi, start);
1808 if (!sentry->valid_blocks)
1809 __set_free(sbi, start);
1810 }
1811
1812 /* set use the current segments */
1813 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1814 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1815 __set_test_and_inuse(sbi, curseg_t->segno);
1816 }
1817}
1818
1819static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1820{
1821 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1822 struct free_segmap_info *free_i = FREE_I(sbi);
1823 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1824 unsigned short valid_blocks;
1825
1826 while (1) {
1827 /* find dirty segment based on free segmap */
1828 segno = find_next_inuse(free_i, total_segs, offset);
1829 if (segno >= total_segs)
1830 break;
1831 offset = segno + 1;
1832 valid_blocks = get_valid_blocks(sbi, segno, 0);
1833 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1834 continue;
1835 mutex_lock(&dirty_i->seglist_lock);
1836 __locate_dirty_segment(sbi, segno, DIRTY);
1837 mutex_unlock(&dirty_i->seglist_lock);
1838 }
1839}
1840
1841static int init_victim_secmap(struct f2fs_sb_info *sbi)
1842{
1843 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1844 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1845
1846 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1847 if (!dirty_i->victim_secmap)
1848 return -ENOMEM;
1849 return 0;
1850}
1851
1852static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1853{
1854 struct dirty_seglist_info *dirty_i;
1855 unsigned int bitmap_size, i;
1856
1857 /* allocate memory for dirty segments list information */
1858 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1859 if (!dirty_i)
1860 return -ENOMEM;
1861
1862 SM_I(sbi)->dirty_info = dirty_i;
1863 mutex_init(&dirty_i->seglist_lock);
1864
1865 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1866
1867 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1868 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1869 if (!dirty_i->dirty_segmap[i])
1870 return -ENOMEM;
1871 }
1872
1873 init_dirty_segmap(sbi);
1874 return init_victim_secmap(sbi);
1875}
1876
1877/*
1878 * Update min, max modified time for cost-benefit GC algorithm
1879 */
1880static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1881{
1882 struct sit_info *sit_i = SIT_I(sbi);
1883 unsigned int segno;
1884
1885 mutex_lock(&sit_i->sentry_lock);
1886
1887 sit_i->min_mtime = LLONG_MAX;
1888
1889 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1890 unsigned int i;
1891 unsigned long long mtime = 0;
1892
1893 for (i = 0; i < sbi->segs_per_sec; i++)
1894 mtime += get_seg_entry(sbi, segno + i)->mtime;
1895
1896 mtime = div_u64(mtime, sbi->segs_per_sec);
1897
1898 if (sit_i->min_mtime > mtime)
1899 sit_i->min_mtime = mtime;
1900 }
1901 sit_i->max_mtime = get_mtime(sbi);
1902 mutex_unlock(&sit_i->sentry_lock);
1903}
1904
1905int build_segment_manager(struct f2fs_sb_info *sbi)
1906{
1907 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1908 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1909 struct f2fs_sm_info *sm_info;
1910 int err;
1911
1912 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1913 if (!sm_info)
1914 return -ENOMEM;
1915
1916 /* init sm info */
1917 sbi->sm_info = sm_info;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001918 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1919 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1920 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1921 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1922 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1923 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1924 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kimf183b112014-03-19 14:17:21 +09001925 sm_info->rec_prefree_segments = sm_info->main_segments *
1926 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Changman Leeb1a94e82013-11-15 10:42:51 +09001927 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1928 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
1929
1930 INIT_LIST_HEAD(&sm_info->discard_list);
1931 sm_info->nr_discards = 0;
1932 sm_info->max_discards = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001933
Evan McClainf3f030d2014-07-17 21:16:35 -04001934 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
1935 err = create_flush_cmd_control(sbi);
1936 if (err)
1937 return err;
1938 }
1939
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001940 err = build_sit_info(sbi);
1941 if (err)
1942 return err;
1943 err = build_free_segmap(sbi);
1944 if (err)
1945 return err;
1946 err = build_curseg(sbi);
1947 if (err)
1948 return err;
1949
1950 /* reinit free segmap based on SIT */
1951 build_sit_entries(sbi);
1952
1953 init_free_segmap(sbi);
1954 err = build_dirty_segmap(sbi);
1955 if (err)
1956 return err;
1957
1958 init_min_max_mtime(sbi);
1959 return 0;
1960}
1961
1962static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1963 enum dirty_type dirty_type)
1964{
1965 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1966
1967 mutex_lock(&dirty_i->seglist_lock);
1968 kfree(dirty_i->dirty_segmap[dirty_type]);
1969 dirty_i->nr_dirty[dirty_type] = 0;
1970 mutex_unlock(&dirty_i->seglist_lock);
1971}
1972
1973static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1974{
1975 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1976 kfree(dirty_i->victim_secmap);
1977}
1978
1979static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1980{
1981 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1982 int i;
1983
1984 if (!dirty_i)
1985 return;
1986
1987 /* discard pre-free/dirty segments list */
1988 for (i = 0; i < NR_DIRTY_TYPE; i++)
1989 discard_dirty_segmap(sbi, i);
1990
1991 destroy_victim_secmap(sbi);
1992 SM_I(sbi)->dirty_info = NULL;
1993 kfree(dirty_i);
1994}
1995
1996static void destroy_curseg(struct f2fs_sb_info *sbi)
1997{
1998 struct curseg_info *array = SM_I(sbi)->curseg_array;
1999 int i;
2000
2001 if (!array)
2002 return;
2003 SM_I(sbi)->curseg_array = NULL;
2004 for (i = 0; i < NR_CURSEG_TYPE; i++)
2005 kfree(array[i].sum_blk);
2006 kfree(array);
2007}
2008
2009static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2010{
2011 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2012 if (!free_i)
2013 return;
2014 SM_I(sbi)->free_info = NULL;
2015 kfree(free_i->free_segmap);
2016 kfree(free_i->free_secmap);
2017 kfree(free_i);
2018}
2019
2020static void destroy_sit_info(struct f2fs_sb_info *sbi)
2021{
2022 struct sit_info *sit_i = SIT_I(sbi);
2023 unsigned int start;
2024
2025 if (!sit_i)
2026 return;
2027
2028 if (sit_i->sentries) {
2029 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
2030 kfree(sit_i->sentries[start].cur_valid_map);
2031 kfree(sit_i->sentries[start].ckpt_valid_map);
2032 }
2033 }
2034 vfree(sit_i->sentries);
2035 vfree(sit_i->sec_entries);
2036 kfree(sit_i->dirty_sentries_bitmap);
2037
2038 SM_I(sbi)->sit_info = NULL;
2039 kfree(sit_i->sit_bitmap);
2040 kfree(sit_i);
2041}
2042
2043void destroy_segment_manager(struct f2fs_sb_info *sbi)
2044{
2045 struct f2fs_sm_info *sm_info = SM_I(sbi);
Evan McClainf3f030d2014-07-17 21:16:35 -04002046
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002047 if (!sm_info)
2048 return;
Evan McClainf3f030d2014-07-17 21:16:35 -04002049 destroy_flush_cmd_control(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002050 destroy_dirty_segmap(sbi);
2051 destroy_curseg(sbi);
2052 destroy_free_segmap(sbi);
2053 destroy_sit_info(sbi);
2054 sbi->sm_info = NULL;
2055 kfree(sm_info);
2056}
Changman Leeb1a94e82013-11-15 10:42:51 +09002057
2058int __init create_segment_manager_caches(void)
2059{
2060 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge33dcea2014-03-07 18:43:28 +08002061 sizeof(struct discard_entry));
Changman Leeb1a94e82013-11-15 10:42:51 +09002062 if (!discard_entry_slab)
2063 return -ENOMEM;
2064 return 0;
2065}
2066
2067void destroy_segment_manager_caches(void)
2068{
2069 kmem_cache_destroy(discard_entry_slab);
2070}