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