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