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