blob: 0dc8ee88b18bcbed86b754b067deb897578d8f19 [file] [log] [blame]
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001/*
2 * fs/f2fs/segment.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/bio.h>
14#include <linux/blkdev.h>
15#include <linux/prefetch.h>
Evan McClainf3f030d2014-07-17 21:16:35 -040016#include <linux/kthread.h>
Linus Torvalds8005ecc2012-12-20 13:54:51 -080017#include <linux/vmalloc.h>
Changman Leeb1a94e82013-11-15 10:42:51 +090018#include <linux/swap.h>
Linus Torvalds8005ecc2012-12-20 13:54:51 -080019
20#include "f2fs.h"
21#include "segment.h"
22#include "node.h"
Jaegeuk Kime0cea842015-02-18 20:43:11 -060023#include "trace.h"
Linus Torvalds8005ecc2012-12-20 13:54:51 -080024#include <trace/events/f2fs.h>
25
Changman Leeb1a94e82013-11-15 10:42:51 +090026#define __reverse_ffz(x) __reverse_ffs(~(x))
27
28static struct kmem_cache *discard_entry_slab;
Jaegeuk Kime0cea842015-02-18 20:43:11 -060029static struct kmem_cache *sit_entry_set_slab;
30static struct kmem_cache *inmem_entry_slab;
31
32/**
33 * Copied from latest lib/llist.c
34 * llist_for_each_entry_safe - iterate over some deleted entries of
35 * lock-less list of given type
36 * safe against removal of list entry
37 * @pos: the type * to use as a loop cursor.
38 * @n: another type * to use as temporary storage
39 * @node: the first entry of deleted list entries.
40 * @member: the name of the llist_node with the struct.
41 *
42 * In general, some entries of the lock-less list can be traversed
43 * safely only after being removed from list, so start with an entry
44 * instead of list head.
45 *
46 * If being used on entries deleted from lock-less list directly, the
47 * traverse order is from the newest to the oldest added entry. If
48 * you want to traverse from the oldest to the newest, you must
49 * reverse the order by yourself before traversing.
50 */
51#define llist_for_each_entry_safe(pos, n, node, member) \
52 for (pos = llist_entry((node), typeof(*pos), member); \
53 &pos->member != NULL && \
54 (n = llist_entry(pos->member.next, typeof(*n), member), true); \
55 pos = n)
56
57/**
58 * Copied from latest lib/llist.c
59 * llist_reverse_order - reverse order of a llist chain
60 * @head: first item of the list to be reversed
61 *
62 * Reverse the order of a chain of llist entries and return the
63 * new first entry.
64 */
65struct llist_node *llist_reverse_order(struct llist_node *head)
66{
67 struct llist_node *new_head = NULL;
68
69 while (head) {
70 struct llist_node *tmp = head;
71 head = head->next;
72 tmp->next = new_head;
73 new_head = tmp;
74 }
75
76 return new_head;
77}
78
79/**
80 * Copied from latest linux/list.h
81 * list_last_entry - get the last element from a list
82 * @ptr: the list head to take the element from.
83 * @type: the type of the struct this is embedded in.
84 * @member: the name of the list_struct within the struct.
85 *
86 * Note, that list is expected to be not empty.
87 */
88#define list_last_entry(ptr, type, member) \
89 list_entry((ptr)->prev, type, member)
Changman Leeb1a94e82013-11-15 10:42:51 +090090
91/*
92 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
93 * MSB and LSB are reversed in a byte by f2fs_set_bit.
94 */
95static inline unsigned long __reverse_ffs(unsigned long word)
96{
97 int num = 0;
98
99#if BITS_PER_LONG == 64
100 if ((word & 0xffffffff) == 0) {
101 num += 32;
102 word >>= 32;
103 }
104#endif
105 if ((word & 0xffff) == 0) {
106 num += 16;
107 word >>= 16;
108 }
109 if ((word & 0xff) == 0) {
110 num += 8;
111 word >>= 8;
112 }
113 if ((word & 0xf0) == 0)
114 num += 4;
115 else
116 word >>= 4;
117 if ((word & 0xc) == 0)
118 num += 2;
119 else
120 word >>= 2;
121 if ((word & 0x2) == 0)
122 num += 1;
123 return num;
124}
125
126/*
arter97f4081402014-08-06 23:22:50 +0900127 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
Changman Leeb1a94e82013-11-15 10:42:51 +0900128 * f2fs_set_bit makes MSB and LSB reversed in a byte.
129 * Example:
130 * LSB <--> MSB
131 * f2fs_set_bit(0, bitmap) => 0000 0001
132 * f2fs_set_bit(7, bitmap) => 1000 0000
133 */
134static unsigned long __find_rev_next_bit(const unsigned long *addr,
135 unsigned long size, unsigned long offset)
136{
137 const unsigned long *p = addr + BIT_WORD(offset);
138 unsigned long result = offset & ~(BITS_PER_LONG - 1);
139 unsigned long tmp;
140 unsigned long mask, submask;
141 unsigned long quot, rest;
142
143 if (offset >= size)
144 return size;
145
146 size -= result;
147 offset %= BITS_PER_LONG;
148 if (!offset)
149 goto aligned;
150
151 tmp = *(p++);
152 quot = (offset >> 3) << 3;
153 rest = offset & 0x7;
154 mask = ~0UL << quot;
155 submask = (unsigned char)(0xff << rest) >> rest;
156 submask <<= quot;
157 mask &= submask;
158 tmp &= mask;
159 if (size < BITS_PER_LONG)
160 goto found_first;
161 if (tmp)
162 goto found_middle;
163
164 size -= BITS_PER_LONG;
165 result += BITS_PER_LONG;
166aligned:
167 while (size & ~(BITS_PER_LONG-1)) {
168 tmp = *(p++);
169 if (tmp)
170 goto found_middle;
171 result += BITS_PER_LONG;
172 size -= BITS_PER_LONG;
173 }
174 if (!size)
175 return result;
176 tmp = *p;
177found_first:
178 tmp &= (~0UL >> (BITS_PER_LONG - size));
179 if (tmp == 0UL) /* Are any bits set? */
180 return result + size; /* Nope. */
181found_middle:
182 return result + __reverse_ffs(tmp);
183}
184
185static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
186 unsigned long size, unsigned long offset)
187{
188 const unsigned long *p = addr + BIT_WORD(offset);
189 unsigned long result = offset & ~(BITS_PER_LONG - 1);
190 unsigned long tmp;
191 unsigned long mask, submask;
192 unsigned long quot, rest;
193
194 if (offset >= size)
195 return size;
196
197 size -= result;
198 offset %= BITS_PER_LONG;
199 if (!offset)
200 goto aligned;
201
202 tmp = *(p++);
203 quot = (offset >> 3) << 3;
204 rest = offset & 0x7;
205 mask = ~(~0UL << quot);
206 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
207 submask <<= quot;
208 mask += submask;
209 tmp |= mask;
210 if (size < BITS_PER_LONG)
211 goto found_first;
212 if (~tmp)
213 goto found_middle;
214
215 size -= BITS_PER_LONG;
216 result += BITS_PER_LONG;
217aligned:
218 while (size & ~(BITS_PER_LONG - 1)) {
219 tmp = *(p++);
220 if (~tmp)
221 goto found_middle;
222 result += BITS_PER_LONG;
223 size -= BITS_PER_LONG;
224 }
225 if (!size)
226 return result;
227 tmp = *p;
228
229found_first:
230 tmp |= ~0UL << size;
231 if (tmp == ~0UL) /* Are any bits zero? */
232 return result + size; /* Nope. */
233found_middle:
234 return result + __reverse_ffz(tmp);
235}
236
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600237void register_inmem_page(struct inode *inode, struct page *page)
238{
239 struct f2fs_inode_info *fi = F2FS_I(inode);
240 struct inmem_pages *new;
241 int err;
242
243 SetPagePrivate(page);
244 f2fs_trace_pid(page);
245
246 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
247
248 /* add atomic page indices to the list */
249 new->page = page;
250 INIT_LIST_HEAD(&new->list);
251retry:
252 /* increase reference count with clean state */
253 mutex_lock(&fi->inmem_lock);
254 err = radix_tree_insert(&fi->inmem_root, page->index, new);
255 if (err == -EEXIST) {
256 mutex_unlock(&fi->inmem_lock);
257 kmem_cache_free(inmem_entry_slab, new);
258 return;
259 } else if (err) {
260 mutex_unlock(&fi->inmem_lock);
261 goto retry;
262 }
263 get_page(page);
264 list_add_tail(&new->list, &fi->inmem_pages);
265 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
266 mutex_unlock(&fi->inmem_lock);
267}
268
269void commit_inmem_pages(struct inode *inode, bool abort)
270{
271 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
272 struct f2fs_inode_info *fi = F2FS_I(inode);
273 struct inmem_pages *cur, *tmp;
274 bool submit_bio = false;
275 struct f2fs_io_info fio = {
276 .type = DATA,
277 .rw = WRITE_SYNC | REQ_PRIO,
278 };
279
280 /*
281 * The abort is true only when f2fs_evict_inode is called.
282 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
283 * that we don't need to call f2fs_balance_fs.
284 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
285 * inode becomes free by iget_locked in f2fs_iget.
286 */
287 if (!abort) {
288 f2fs_balance_fs(sbi);
289 f2fs_lock_op(sbi);
290 }
291
292 mutex_lock(&fi->inmem_lock);
293 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
294 if (!abort) {
295 lock_page(cur->page);
296 if (cur->page->mapping == inode->i_mapping) {
297 f2fs_wait_on_page_writeback(cur->page, DATA);
298 if (clear_page_dirty_for_io(cur->page))
299 inode_dec_dirty_pages(inode);
300 do_write_data_page(cur->page, &fio);
301 submit_bio = true;
302 }
303 f2fs_put_page(cur->page, 1);
304 } else {
305 put_page(cur->page);
306 }
307 radix_tree_delete(&fi->inmem_root, cur->page->index);
308 list_del(&cur->list);
309 kmem_cache_free(inmem_entry_slab, cur);
310 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
311 }
312 mutex_unlock(&fi->inmem_lock);
313
314 if (!abort) {
315 f2fs_unlock_op(sbi);
316 if (submit_bio)
317 f2fs_submit_merged_bio(sbi, DATA, WRITE);
318 }
319}
320
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800321/*
322 * This function balances dirty node and dentry pages.
323 * In addition, it controls garbage collection.
324 */
325void f2fs_balance_fs(struct f2fs_sb_info *sbi)
326{
327 /*
328 * We should do GC or end up with checkpoint, if there are so many dirty
329 * dir/node pages without enough free segments.
330 */
331 if (has_not_enough_free_secs(sbi, 0)) {
332 mutex_lock(&sbi->gc_mutex);
333 f2fs_gc(sbi);
334 }
335}
336
337void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
338{
339 /* check the # of cached NAT entries and prefree segments */
340 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600341 excess_prefree_segs(sbi) ||
342 !available_free_memory(sbi, INO_ENTRIES))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800343 f2fs_sync_fs(sbi->sb, true);
344}
345
Evan McClainf3f030d2014-07-17 21:16:35 -0400346struct __submit_bio_ret {
347 struct completion event;
348 int error;
349};
350
351static void __submit_bio_wait_endio(struct bio *bio, int error)
352{
353 struct __submit_bio_ret *ret = bio->bi_private;
354
355 ret->error = error;
356 complete(&ret->event);
357}
358
359static int __submit_bio_wait(int rw, struct bio *bio)
360{
361 struct __submit_bio_ret ret;
362
363 rw |= REQ_SYNC;
364 init_completion(&ret.event);
365 bio->bi_private = &ret;
366 bio->bi_end_io = __submit_bio_wait_endio;
367 submit_bio(rw, bio);
368 wait_for_completion(&ret.event);
369
370 return ret.error;
371}
372
373static int issue_flush_thread(void *data)
374{
375 struct f2fs_sb_info *sbi = data;
376 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
377 wait_queue_head_t *q = &fcc->flush_wait_queue;
378repeat:
379 if (kthread_should_stop())
380 return 0;
381
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600382 if (!llist_empty(&fcc->issue_list)) {
Evan McClainf3f030d2014-07-17 21:16:35 -0400383 struct bio *bio = bio_alloc(GFP_NOIO, 0);
384 struct flush_cmd *cmd, *next;
385 int ret;
386
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600387 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
388 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
389
Evan McClainf3f030d2014-07-17 21:16:35 -0400390 bio->bi_bdev = sbi->sb->s_bdev;
391 ret = __submit_bio_wait(WRITE_FLUSH, bio);
392
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600393 llist_for_each_entry_safe(cmd, next,
394 fcc->dispatch_list, llnode) {
Evan McClainf3f030d2014-07-17 21:16:35 -0400395 cmd->ret = ret;
Evan McClainf3f030d2014-07-17 21:16:35 -0400396 complete(&cmd->wait);
397 }
398 bio_put(bio);
399 fcc->dispatch_list = NULL;
400 }
401
402 wait_event_interruptible(*q,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600403 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Evan McClainf3f030d2014-07-17 21:16:35 -0400404 goto repeat;
405}
406
407int f2fs_issue_flush(struct f2fs_sb_info *sbi)
408{
409 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
410 struct flush_cmd cmd;
411
Jaegeuk Kim90421d22014-07-25 17:46:10 -0700412 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
413 test_opt(sbi, FLUSH_MERGE));
414
Jaegeuk Kim6f6541b2014-07-23 09:57:31 -0700415 if (test_opt(sbi, NOBARRIER))
416 return 0;
417
Evan McClainf3f030d2014-07-17 21:16:35 -0400418 if (!test_opt(sbi, FLUSH_MERGE))
419 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
420
421 init_completion(&cmd.wait);
Evan McClainf3f030d2014-07-17 21:16:35 -0400422
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600423 llist_add(&cmd.llnode, &fcc->issue_list);
Evan McClainf3f030d2014-07-17 21:16:35 -0400424
425 if (!fcc->dispatch_list)
426 wake_up(&fcc->flush_wait_queue);
427
428 wait_for_completion(&cmd.wait);
429
430 return cmd.ret;
431}
432
433int create_flush_cmd_control(struct f2fs_sb_info *sbi)
434{
435 dev_t dev = sbi->sb->s_bdev->bd_dev;
436 struct flush_cmd_control *fcc;
437 int err = 0;
438
439 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
440 if (!fcc)
441 return -ENOMEM;
Evan McClainf3f030d2014-07-17 21:16:35 -0400442 init_waitqueue_head(&fcc->flush_wait_queue);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600443 init_llist_head(&fcc->issue_list);
Evan McClainf3f030d2014-07-17 21:16:35 -0400444 SM_I(sbi)->cmd_control_info = fcc;
445 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
446 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
447 if (IS_ERR(fcc->f2fs_issue_flush)) {
448 err = PTR_ERR(fcc->f2fs_issue_flush);
449 kfree(fcc);
450 SM_I(sbi)->cmd_control_info = NULL;
451 return err;
452 }
453
454 return err;
455}
456
457void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
458{
459 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
460
461 if (fcc && fcc->f2fs_issue_flush)
462 kthread_stop(fcc->f2fs_issue_flush);
463 kfree(fcc);
464 SM_I(sbi)->cmd_control_info = NULL;
465}
466
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800467static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
468 enum dirty_type dirty_type)
469{
470 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
471
472 /* need not be added */
473 if (IS_CURSEG(sbi, segno))
474 return;
475
476 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
477 dirty_i->nr_dirty[dirty_type]++;
478
479 if (dirty_type == DIRTY) {
480 struct seg_entry *sentry = get_seg_entry(sbi, segno);
481 enum dirty_type t = sentry->type;
482
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600483 if (unlikely(t >= DIRTY)) {
484 f2fs_bug_on(sbi, 1);
485 return;
486 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800487 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
488 dirty_i->nr_dirty[t]++;
489 }
490}
491
492static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
493 enum dirty_type dirty_type)
494{
495 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
496
497 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
498 dirty_i->nr_dirty[dirty_type]--;
499
500 if (dirty_type == DIRTY) {
501 struct seg_entry *sentry = get_seg_entry(sbi, segno);
502 enum dirty_type t = sentry->type;
503
504 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
505 dirty_i->nr_dirty[t]--;
506
507 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
508 clear_bit(GET_SECNO(sbi, segno),
509 dirty_i->victim_secmap);
510 }
511}
512
513/*
514 * Should not occur error such as -ENOMEM.
515 * Adding dirty entry into seglist is not critical operation.
516 * If a given segment is one of current working segments, it won't be added.
517 */
518static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
519{
520 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
521 unsigned short valid_blocks;
522
523 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
524 return;
525
526 mutex_lock(&dirty_i->seglist_lock);
527
528 valid_blocks = get_valid_blocks(sbi, segno, 0);
529
530 if (valid_blocks == 0) {
531 __locate_dirty_segment(sbi, segno, PRE);
532 __remove_dirty_segment(sbi, segno, DIRTY);
533 } else if (valid_blocks < sbi->blocks_per_seg) {
534 __locate_dirty_segment(sbi, segno, DIRTY);
535 } else {
536 /* Recovery routine with SSR needs this */
537 __remove_dirty_segment(sbi, segno, DIRTY);
538 }
539
540 mutex_unlock(&dirty_i->seglist_lock);
541}
542
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900543static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Changman Leeb1a94e82013-11-15 10:42:51 +0900544 block_t blkstart, block_t blklen)
545{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600546 sector_t start = SECTOR_FROM_BLOCK(blkstart);
547 sector_t len = SECTOR_FROM_BLOCK(blklen);
Changman Leeb1a94e82013-11-15 10:42:51 +0900548 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900549 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
550}
551
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600552void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900553{
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900554 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
555 struct page *page = grab_meta_page(sbi, blkaddr);
556 /* zero-filled page */
557 set_page_dirty(page);
558 f2fs_put_page(page, 1);
559 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900560}
561
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600562static void __add_discard_entry(struct f2fs_sb_info *sbi,
563 struct cp_control *cpc, unsigned int start, unsigned int end)
Changman Leeb1a94e82013-11-15 10:42:51 +0900564{
565 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600566 struct discard_entry *new, *last;
567
568 if (!list_empty(head)) {
569 last = list_last_entry(head, struct discard_entry, list);
570 if (START_BLOCK(sbi, cpc->trim_start) + start ==
571 last->blkaddr + last->len) {
572 last->len += end - start;
573 goto done;
574 }
575 }
576
577 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
578 INIT_LIST_HEAD(&new->list);
579 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
580 new->len = end - start;
581 list_add_tail(&new->list, head);
582done:
583 SM_I(sbi)->nr_discards += end - start;
584 cpc->trimmed += end - start;
585}
586
587static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
588{
Changman Leeb1a94e82013-11-15 10:42:51 +0900589 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
590 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600591 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Changman Leeb1a94e82013-11-15 10:42:51 +0900592 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
593 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600594 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Changman Leeb1a94e82013-11-15 10:42:51 +0900595 unsigned int start = 0, end = -1;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600596 bool force = (cpc->reason == CP_DISCARD);
Changman Leeb1a94e82013-11-15 10:42:51 +0900597 int i;
598
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600599 if (!force && (!test_opt(sbi, DISCARD) ||
600 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards))
Changman Leeb1a94e82013-11-15 10:42:51 +0900601 return;
602
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600603 if (force && !se->valid_blocks) {
604 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
605 /*
606 * if this segment is registered in the prefree list, then
607 * we should skip adding a discard candidate, and let the
608 * checkpoint do that later.
609 */
610 mutex_lock(&dirty_i->seglist_lock);
611 if (test_bit(cpc->trim_start, dirty_i->dirty_segmap[PRE])) {
612 mutex_unlock(&dirty_i->seglist_lock);
613 cpc->trimmed += sbi->blocks_per_seg;
614 return;
615 }
616 mutex_unlock(&dirty_i->seglist_lock);
617
618 __add_discard_entry(sbi, cpc, 0, sbi->blocks_per_seg);
619 return;
620 }
621
Changman Leeb1a94e82013-11-15 10:42:51 +0900622 /* zero block will be discarded through the prefree list */
623 if (!se->valid_blocks || se->valid_blocks == max_blocks)
624 return;
625
626 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
627 for (i = 0; i < entries; i++)
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600628 dmap[i] = force ? ~ckpt_map[i] :
629 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Changman Leeb1a94e82013-11-15 10:42:51 +0900630
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600631 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Changman Leeb1a94e82013-11-15 10:42:51 +0900632 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
633 if (start >= max_blocks)
634 break;
635
636 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
637
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600638 if (end - start < cpc->trim_minlen)
639 continue;
Changman Leeb1a94e82013-11-15 10:42:51 +0900640
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600641 __add_discard_entry(sbi, cpc, start, end);
642 }
643}
644
645void release_discard_addrs(struct f2fs_sb_info *sbi)
646{
647 struct list_head *head = &(SM_I(sbi)->discard_list);
648 struct discard_entry *entry, *this;
649
650 /* drop caches */
651 list_for_each_entry_safe(entry, this, head, list) {
652 list_del(&entry->list);
653 kmem_cache_free(discard_entry_slab, entry);
Changman Leeb1a94e82013-11-15 10:42:51 +0900654 }
655}
656
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800657/*
658 * Should call clear_prefree_segments after checkpoint is done.
659 */
660static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
661{
662 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yu00ebaff2014-08-04 10:10:07 +0800663 unsigned int segno;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800664
665 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600666 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800667 __set_test_and_free(sbi, segno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800668 mutex_unlock(&dirty_i->seglist_lock);
669}
670
671void clear_prefree_segments(struct f2fs_sb_info *sbi)
672{
Changman Leeb1a94e82013-11-15 10:42:51 +0900673 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu48c561a2014-03-29 11:33:17 +0800674 struct discard_entry *entry, *this;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800675 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
676 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800677 unsigned int start = 0, end = -1;
678
679 mutex_lock(&dirty_i->seglist_lock);
680
681 while (1) {
682 int i;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600683 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
684 if (start >= MAIN_SEGS(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800685 break;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600686 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
687 start + 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800688
689 for (i = start; i < end; i++)
690 clear_bit(i, prefree_map);
691
692 dirty_i->nr_dirty[PRE] -= end - start;
693
694 if (!test_opt(sbi, DISCARD))
695 continue;
696
Changman Leeb1a94e82013-11-15 10:42:51 +0900697 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
698 (end - start) << sbi->log_blocks_per_seg);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800699 }
700 mutex_unlock(&dirty_i->seglist_lock);
Changman Leeb1a94e82013-11-15 10:42:51 +0900701
702 /* send small discards */
Chao Yu48c561a2014-03-29 11:33:17 +0800703 list_for_each_entry_safe(entry, this, head, list) {
Changman Leeb1a94e82013-11-15 10:42:51 +0900704 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
705 list_del(&entry->list);
706 SM_I(sbi)->nr_discards -= entry->len;
707 kmem_cache_free(discard_entry_slab, entry);
708 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800709}
710
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600711static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800712{
713 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600714
715 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800716 sit_i->dirty_sentries++;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600717 return false;
718 }
719
720 return true;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800721}
722
723static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
724 unsigned int segno, int modified)
725{
726 struct seg_entry *se = get_seg_entry(sbi, segno);
727 se->type = type;
728 if (modified)
729 __mark_sit_entry_dirty(sbi, segno);
730}
731
732static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
733{
734 struct seg_entry *se;
735 unsigned int segno, offset;
736 long int new_vblocks;
737
738 segno = GET_SEGNO(sbi, blkaddr);
739
740 se = get_seg_entry(sbi, segno);
741 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim26d12822014-02-04 13:01:10 +0900742 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800743
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600744 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800745 (new_vblocks > sbi->blocks_per_seg)));
746
747 se->valid_blocks = new_vblocks;
748 se->mtime = get_mtime(sbi);
749 SIT_I(sbi)->max_mtime = se->mtime;
750
751 /* Update valid block bitmap */
752 if (del > 0) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600753 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
754 f2fs_bug_on(sbi, 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800755 } else {
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600756 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
757 f2fs_bug_on(sbi, 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800758 }
759 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
760 se->ckpt_valid_blocks += del;
761
762 __mark_sit_entry_dirty(sbi, segno);
763
764 /* update total number of valid blocks to be written in ckpt area */
765 SIT_I(sbi)->written_valid_blocks += del;
766
767 if (sbi->segs_per_sec > 1)
768 get_sec_entry(sbi, segno)->valid_blocks += del;
769}
770
Jaegeuk Kim655d2c12014-01-28 12:22:14 +0900771void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800772{
Jaegeuk Kim655d2c12014-01-28 12:22:14 +0900773 update_sit_entry(sbi, new, 1);
774 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
775 update_sit_entry(sbi, old, -1);
776
777 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
778 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800779}
780
781void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
782{
783 unsigned int segno = GET_SEGNO(sbi, addr);
784 struct sit_info *sit_i = SIT_I(sbi);
785
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600786 f2fs_bug_on(sbi, addr == NULL_ADDR);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800787 if (addr == NEW_ADDR)
788 return;
789
790 /* add it into sit main buffer */
791 mutex_lock(&sit_i->sentry_lock);
792
793 update_sit_entry(sbi, addr, -1);
794
795 /* add it into dirty seglist */
796 locate_dirty_segment(sbi, segno);
797
798 mutex_unlock(&sit_i->sentry_lock);
799}
800
801/*
802 * This function should be resided under the curseg_mutex lock
803 */
804static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
805 struct f2fs_summary *sum)
806{
807 struct curseg_info *curseg = CURSEG_I(sbi, type);
808 void *addr = curseg->sum_blk;
809 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
810 memcpy(addr, sum, sizeof(struct f2fs_summary));
811}
812
813/*
814 * Calculate the number of current summary pages for writing
815 */
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600816int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800817{
818 int valid_sum_count = 0;
819 int i, sum_in_page;
820
821 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
822 if (sbi->ckpt->alloc_type[i] == SSR)
823 valid_sum_count += sbi->blocks_per_seg;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600824 else {
825 if (for_ra)
826 valid_sum_count += le16_to_cpu(
827 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
828 else
829 valid_sum_count += curseg_blkoff(sbi, i);
830 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800831 }
832
833 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
834 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
835 if (valid_sum_count <= sum_in_page)
836 return 1;
837 else if ((valid_sum_count - sum_in_page) <=
838 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
839 return 2;
840 return 3;
841}
842
843/*
844 * Caller should put this summary page
845 */
846struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
847{
848 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
849}
850
851static void write_sum_page(struct f2fs_sb_info *sbi,
852 struct f2fs_summary_block *sum_blk, block_t blk_addr)
853{
854 struct page *page = grab_meta_page(sbi, blk_addr);
855 void *kaddr = page_address(page);
856 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
857 set_page_dirty(page);
858 f2fs_put_page(page, 1);
859}
860
861static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
862{
863 struct curseg_info *curseg = CURSEG_I(sbi, type);
864 unsigned int segno = curseg->segno + 1;
865 struct free_segmap_info *free_i = FREE_I(sbi);
866
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600867 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800868 return !test_bit(segno, free_i->free_segmap);
869 return 0;
870}
871
872/*
873 * Find a new segment from the free segments bitmap to right order
874 * This function should be returned with success, otherwise BUG
875 */
876static void get_new_segment(struct f2fs_sb_info *sbi,
877 unsigned int *newseg, bool new_sec, int dir)
878{
879 struct free_segmap_info *free_i = FREE_I(sbi);
880 unsigned int segno, secno, zoneno;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600881 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800882 unsigned int hint = *newseg / sbi->segs_per_sec;
883 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
884 unsigned int left_start = hint;
885 bool init = true;
886 int go_left = 0;
887 int i;
888
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600889 spin_lock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800890
891 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
892 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600893 MAIN_SEGS(sbi), *newseg + 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800894 if (segno - *newseg < sbi->segs_per_sec -
895 (*newseg % sbi->segs_per_sec))
896 goto got_it;
897 }
898find_other_zone:
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600899 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
900 if (secno >= MAIN_SECS(sbi)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800901 if (dir == ALLOC_RIGHT) {
902 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600903 MAIN_SECS(sbi), 0);
904 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800905 } else {
906 go_left = 1;
907 left_start = hint - 1;
908 }
909 }
910 if (go_left == 0)
911 goto skip_left;
912
913 while (test_bit(left_start, free_i->free_secmap)) {
914 if (left_start > 0) {
915 left_start--;
916 continue;
917 }
918 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600919 MAIN_SECS(sbi), 0);
920 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800921 break;
922 }
923 secno = left_start;
924skip_left:
925 hint = secno;
926 segno = secno * sbi->segs_per_sec;
927 zoneno = secno / sbi->secs_per_zone;
928
929 /* give up on finding another zone */
930 if (!init)
931 goto got_it;
932 if (sbi->secs_per_zone == 1)
933 goto got_it;
934 if (zoneno == old_zoneno)
935 goto got_it;
936 if (dir == ALLOC_LEFT) {
937 if (!go_left && zoneno + 1 >= total_zones)
938 goto got_it;
939 if (go_left && zoneno == 0)
940 goto got_it;
941 }
942 for (i = 0; i < NR_CURSEG_TYPE; i++)
943 if (CURSEG_I(sbi, i)->zone == zoneno)
944 break;
945
946 if (i < NR_CURSEG_TYPE) {
947 /* zone is in user, try another */
948 if (go_left)
949 hint = zoneno * sbi->secs_per_zone - 1;
950 else if (zoneno + 1 >= total_zones)
951 hint = 0;
952 else
953 hint = (zoneno + 1) * sbi->secs_per_zone;
954 init = false;
955 goto find_other_zone;
956 }
957got_it:
958 /* set it as dirty segment in free segmap */
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600959 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800960 __set_inuse(sbi, segno);
961 *newseg = segno;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600962 spin_unlock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800963}
964
965static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
966{
967 struct curseg_info *curseg = CURSEG_I(sbi, type);
968 struct summary_footer *sum_footer;
969
970 curseg->segno = curseg->next_segno;
971 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
972 curseg->next_blkoff = 0;
973 curseg->next_segno = NULL_SEGNO;
974
975 sum_footer = &(curseg->sum_blk->footer);
976 memset(sum_footer, 0, sizeof(struct summary_footer));
977 if (IS_DATASEG(type))
978 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
979 if (IS_NODESEG(type))
980 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
981 __set_sit_entry_type(sbi, type, curseg->segno, modified);
982}
983
984/*
985 * Allocate a current working segment.
986 * This function always allocates a free segment in LFS manner.
987 */
988static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
989{
990 struct curseg_info *curseg = CURSEG_I(sbi, type);
991 unsigned int segno = curseg->segno;
992 int dir = ALLOC_LEFT;
993
994 write_sum_page(sbi, curseg->sum_blk,
995 GET_SUM_BLOCK(sbi, segno));
996 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
997 dir = ALLOC_RIGHT;
998
999 if (test_opt(sbi, NOHEAP))
1000 dir = ALLOC_RIGHT;
1001
1002 get_new_segment(sbi, &segno, new_sec, dir);
1003 curseg->next_segno = segno;
1004 reset_curseg(sbi, type, 1);
1005 curseg->alloc_type = LFS;
1006}
1007
1008static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1009 struct curseg_info *seg, block_t start)
1010{
1011 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leeb1a94e82013-11-15 10:42:51 +09001012 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001013 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leeb1a94e82013-11-15 10:42:51 +09001014 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1015 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1016 int i, pos;
1017
1018 for (i = 0; i < entries; i++)
1019 target_map[i] = ckpt_map[i] | cur_map[i];
1020
1021 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1022
1023 seg->next_blkoff = pos;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001024}
1025
1026/*
1027 * If a segment is written by LFS manner, next block offset is just obtained
1028 * by increasing the current block offset. However, if a segment is written by
1029 * SSR manner, next block offset obtained by calling __next_free_blkoff
1030 */
1031static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1032 struct curseg_info *seg)
1033{
1034 if (seg->alloc_type == SSR)
1035 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1036 else
1037 seg->next_blkoff++;
1038}
1039
1040/*
arter97f4081402014-08-06 23:22:50 +09001041 * This function always allocates a used segment(from dirty seglist) by SSR
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001042 * manner, so it should recover the existing segment information of valid blocks
1043 */
1044static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1045{
1046 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1047 struct curseg_info *curseg = CURSEG_I(sbi, type);
1048 unsigned int new_segno = curseg->next_segno;
1049 struct f2fs_summary_block *sum_node;
1050 struct page *sum_page;
1051
1052 write_sum_page(sbi, curseg->sum_blk,
1053 GET_SUM_BLOCK(sbi, curseg->segno));
1054 __set_test_and_inuse(sbi, new_segno);
1055
1056 mutex_lock(&dirty_i->seglist_lock);
1057 __remove_dirty_segment(sbi, new_segno, PRE);
1058 __remove_dirty_segment(sbi, new_segno, DIRTY);
1059 mutex_unlock(&dirty_i->seglist_lock);
1060
1061 reset_curseg(sbi, type, 1);
1062 curseg->alloc_type = SSR;
1063 __next_free_blkoff(sbi, curseg, 0);
1064
1065 if (reuse) {
1066 sum_page = get_sum_page(sbi, new_segno);
1067 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1068 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1069 f2fs_put_page(sum_page, 1);
1070 }
1071}
1072
1073static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1074{
1075 struct curseg_info *curseg = CURSEG_I(sbi, type);
1076 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1077
1078 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
1079 return v_ops->get_victim(sbi,
1080 &(curseg)->next_segno, BG_GC, type, SSR);
1081
1082 /* For data segments, let's do SSR more intensively */
1083 for (; type >= CURSEG_HOT_DATA; type--)
1084 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1085 BG_GC, type, SSR))
1086 return 1;
1087 return 0;
1088}
1089
1090/*
1091 * flush out current segment and replace it with new segment
1092 * This function should be returned with success, otherwise BUG
1093 */
1094static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1095 int type, bool force)
1096{
1097 struct curseg_info *curseg = CURSEG_I(sbi, type);
1098
1099 if (force)
1100 new_curseg(sbi, type, true);
1101 else if (type == CURSEG_WARM_NODE)
1102 new_curseg(sbi, type, false);
1103 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1104 new_curseg(sbi, type, false);
1105 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1106 change_curseg(sbi, type, true);
1107 else
1108 new_curseg(sbi, type, false);
1109
1110 stat_inc_seg_type(sbi, curseg);
1111}
1112
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001113static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1114{
1115 struct curseg_info *curseg = CURSEG_I(sbi, type);
1116 unsigned int old_segno;
1117
1118 old_segno = curseg->segno;
1119 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1120 locate_dirty_segment(sbi, old_segno);
1121}
1122
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001123void allocate_new_segments(struct f2fs_sb_info *sbi)
1124{
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001125 int i;
1126
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001127 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1128 __allocate_new_segments(sbi, i);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001129}
1130
1131static const struct segment_allocation default_salloc_ops = {
1132 .allocate_segment = allocate_segment_by_default,
1133};
1134
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001135int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1136{
1137 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1138 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
1139 unsigned int start_segno, end_segno;
1140 struct cp_control cpc;
1141
1142 if (range->minlen > SEGMENT_SIZE(sbi) || start >= MAX_BLKADDR(sbi) ||
1143 range->len < sbi->blocksize)
1144 return -EINVAL;
1145
1146 cpc.trimmed = 0;
1147 if (end <= MAIN_BLKADDR(sbi))
1148 goto out;
1149
1150 /* start/end segment number in main_area */
1151 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1152 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1153 GET_SEGNO(sbi, end);
1154 cpc.reason = CP_DISCARD;
1155 cpc.trim_minlen = F2FS_BYTES_TO_BLK(range->minlen);
1156
1157 /* do checkpoint to issue discard commands safely */
1158 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1159 cpc.trim_start = start_segno;
1160 cpc.trim_end = min_t(unsigned int, rounddown(start_segno +
1161 BATCHED_TRIM_SEGMENTS(sbi),
1162 sbi->segs_per_sec) - 1, end_segno);
1163
1164 mutex_lock(&sbi->gc_mutex);
1165 write_checkpoint(sbi, &cpc);
1166 mutex_unlock(&sbi->gc_mutex);
1167 }
1168out:
1169 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
1170 return 0;
1171}
1172
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001173static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1174{
1175 struct curseg_info *curseg = CURSEG_I(sbi, type);
1176 if (curseg->next_blkoff < sbi->blocks_per_seg)
1177 return true;
1178 return false;
1179}
1180
1181static int __get_segment_type_2(struct page *page, enum page_type p_type)
1182{
1183 if (p_type == DATA)
1184 return CURSEG_HOT_DATA;
1185 else
1186 return CURSEG_HOT_NODE;
1187}
1188
1189static int __get_segment_type_4(struct page *page, enum page_type p_type)
1190{
1191 if (p_type == DATA) {
1192 struct inode *inode = page->mapping->host;
1193
1194 if (S_ISDIR(inode->i_mode))
1195 return CURSEG_HOT_DATA;
1196 else
1197 return CURSEG_COLD_DATA;
1198 } else {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001199 if (IS_DNODE(page) && is_cold_node(page))
1200 return CURSEG_WARM_NODE;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001201 else
1202 return CURSEG_COLD_NODE;
1203 }
1204}
1205
1206static int __get_segment_type_6(struct page *page, enum page_type p_type)
1207{
1208 if (p_type == DATA) {
1209 struct inode *inode = page->mapping->host;
1210
1211 if (S_ISDIR(inode->i_mode))
1212 return CURSEG_HOT_DATA;
1213 else if (is_cold_data(page) || file_is_cold(inode))
1214 return CURSEG_COLD_DATA;
1215 else
1216 return CURSEG_WARM_DATA;
1217 } else {
1218 if (IS_DNODE(page))
1219 return is_cold_node(page) ? CURSEG_WARM_NODE :
1220 CURSEG_HOT_NODE;
1221 else
1222 return CURSEG_COLD_NODE;
1223 }
1224}
1225
1226static int __get_segment_type(struct page *page, enum page_type p_type)
1227{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001228 switch (F2FS_P_SB(page)->active_logs) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001229 case 2:
1230 return __get_segment_type_2(page, p_type);
1231 case 4:
1232 return __get_segment_type_4(page, p_type);
1233 }
1234 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001235 f2fs_bug_on(F2FS_P_SB(page),
1236 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001237 return __get_segment_type_6(page, p_type);
1238}
1239
Changman Leeb1a94e82013-11-15 10:42:51 +09001240void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1241 block_t old_blkaddr, block_t *new_blkaddr,
1242 struct f2fs_summary *sum, int type)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001243{
1244 struct sit_info *sit_i = SIT_I(sbi);
1245 struct curseg_info *curseg;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001246 bool direct_io = (type == CURSEG_DIRECT_IO);
1247
1248 type = direct_io ? CURSEG_WARM_DATA : type;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001249
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001250 curseg = CURSEG_I(sbi, type);
1251
1252 mutex_lock(&curseg->curseg_mutex);
1253
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001254 /* direct_io'ed data is aligned to the segment for better performance */
1255 if (direct_io && curseg->next_blkoff)
1256 __allocate_new_segments(sbi, type);
1257
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001258 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001259
1260 /*
1261 * __add_sum_entry should be resided under the curseg_mutex
1262 * because, this function updates a summary entry in the
1263 * current summary block.
1264 */
1265 __add_sum_entry(sbi, type, sum);
1266
1267 mutex_lock(&sit_i->sentry_lock);
1268 __refresh_next_blkoff(sbi, curseg);
1269
1270 stat_inc_block_count(sbi, curseg);
1271
Jaegeuk Kim655d2c12014-01-28 12:22:14 +09001272 if (!__has_curseg_space(sbi, type))
1273 sit_i->s_ops->allocate_segment(sbi, type, false);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001274 /*
1275 * SIT information should be updated before segment allocation,
1276 * since SSR needs latest valid block information.
1277 */
1278 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim655d2c12014-01-28 12:22:14 +09001279
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001280 mutex_unlock(&sit_i->sentry_lock);
1281
Changman Leeb1a94e82013-11-15 10:42:51 +09001282 if (page && IS_NODESEG(type))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001283 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1284
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001285 mutex_unlock(&curseg->curseg_mutex);
1286}
1287
Changman Leeb1a94e82013-11-15 10:42:51 +09001288static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001289 struct f2fs_summary *sum,
1290 struct f2fs_io_info *fio)
Changman Leeb1a94e82013-11-15 10:42:51 +09001291{
1292 int type = __get_segment_type(page, fio->type);
1293
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001294 allocate_data_block(sbi, page, fio->blk_addr, &fio->blk_addr, sum, type);
Changman Leeb1a94e82013-11-15 10:42:51 +09001295
1296 /* writeout dirty page into bdev */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001297 f2fs_submit_page_mbio(sbi, page, fio);
Changman Leeb1a94e82013-11-15 10:42:51 +09001298}
1299
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001300void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1301{
Changman Leeb1a94e82013-11-15 10:42:51 +09001302 struct f2fs_io_info fio = {
1303 .type = META,
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001304 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1305 .blk_addr = page->index,
Changman Leeb1a94e82013-11-15 10:42:51 +09001306 };
1307
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001308 set_page_writeback(page);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001309 f2fs_submit_page_mbio(sbi, page, &fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001310}
1311
1312void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001313 unsigned int nid, struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001314{
1315 struct f2fs_summary sum;
1316 set_summary(&sum, nid, 0, 0);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001317 do_write_page(sbi, page, &sum, fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001318}
1319
Changman Leeb1a94e82013-11-15 10:42:51 +09001320void write_data_page(struct page *page, struct dnode_of_data *dn,
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001321 struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001322{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001323 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001324 struct f2fs_summary sum;
1325 struct node_info ni;
1326
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001327 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001328 get_node_info(sbi, dn->nid, &ni);
1329 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001330 do_write_page(sbi, page, &sum, fio);
1331 dn->data_blkaddr = fio->blk_addr;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001332}
1333
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001334void rewrite_data_page(struct page *page, struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001335{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001336 stat_inc_inplace_blocks(F2FS_P_SB(page));
1337 f2fs_submit_page_mbio(F2FS_P_SB(page), page, fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001338}
1339
1340void recover_data_page(struct f2fs_sb_info *sbi,
1341 struct page *page, struct f2fs_summary *sum,
1342 block_t old_blkaddr, block_t new_blkaddr)
1343{
1344 struct sit_info *sit_i = SIT_I(sbi);
1345 struct curseg_info *curseg;
1346 unsigned int segno, old_cursegno;
1347 struct seg_entry *se;
1348 int type;
1349
1350 segno = GET_SEGNO(sbi, new_blkaddr);
1351 se = get_seg_entry(sbi, segno);
1352 type = se->type;
1353
1354 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1355 if (old_blkaddr == NULL_ADDR)
1356 type = CURSEG_COLD_DATA;
1357 else
1358 type = CURSEG_WARM_DATA;
1359 }
1360 curseg = CURSEG_I(sbi, type);
1361
1362 mutex_lock(&curseg->curseg_mutex);
1363 mutex_lock(&sit_i->sentry_lock);
1364
1365 old_cursegno = curseg->segno;
1366
1367 /* change the current segment */
1368 if (segno != curseg->segno) {
1369 curseg->next_segno = segno;
1370 change_curseg(sbi, type, true);
1371 }
1372
Jaegeuk Kim26d12822014-02-04 13:01:10 +09001373 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001374 __add_sum_entry(sbi, type, sum);
1375
1376 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001377 locate_dirty_segment(sbi, old_cursegno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001378
1379 mutex_unlock(&sit_i->sentry_lock);
1380 mutex_unlock(&curseg->curseg_mutex);
1381}
1382
Evan McClainf3f030d2014-07-17 21:16:35 -04001383static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1384 struct page *page, enum page_type type)
1385{
1386 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1387 struct f2fs_bio_info *io = &sbi->write_io[btype];
1388 struct bio_vec *bvec;
1389 int i;
1390
1391 down_read(&io->io_rwsem);
1392 if (!io->bio)
1393 goto out;
1394
1395 __bio_for_each_segment(bvec, io->bio, i, 0) {
1396 if (page == bvec->bv_page) {
1397 up_read(&io->io_rwsem);
1398 return true;
1399 }
1400 }
1401
1402out:
1403 up_read(&io->io_rwsem);
1404 return false;
1405}
1406
Changman Leeb1a94e82013-11-15 10:42:51 +09001407void f2fs_wait_on_page_writeback(struct page *page,
1408 enum page_type type)
1409{
Changman Leeb1a94e82013-11-15 10:42:51 +09001410 if (PageWriteback(page)) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001411 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1412
Evan McClainf3f030d2014-07-17 21:16:35 -04001413 if (is_merged_page(sbi, page, type))
1414 f2fs_submit_merged_bio(sbi, type, WRITE);
Changman Leeb1a94e82013-11-15 10:42:51 +09001415 wait_on_page_writeback(page);
1416 }
1417}
1418
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001419static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1420{
1421 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1422 struct curseg_info *seg_i;
1423 unsigned char *kaddr;
1424 struct page *page;
1425 block_t start;
1426 int i, j, offset;
1427
1428 start = start_sum_block(sbi);
1429
1430 page = get_meta_page(sbi, start++);
1431 kaddr = (unsigned char *)page_address(page);
1432
1433 /* Step 1: restore nat cache */
1434 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1435 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1436
1437 /* Step 2: restore sit cache */
1438 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1439 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1440 SUM_JOURNAL_SIZE);
1441 offset = 2 * SUM_JOURNAL_SIZE;
1442
1443 /* Step 3: restore summary entries */
1444 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1445 unsigned short blk_off;
1446 unsigned int segno;
1447
1448 seg_i = CURSEG_I(sbi, i);
1449 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1450 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1451 seg_i->next_segno = segno;
1452 reset_curseg(sbi, i, 0);
1453 seg_i->alloc_type = ckpt->alloc_type[i];
1454 seg_i->next_blkoff = blk_off;
1455
1456 if (seg_i->alloc_type == SSR)
1457 blk_off = sbi->blocks_per_seg;
1458
1459 for (j = 0; j < blk_off; j++) {
1460 struct f2fs_summary *s;
1461 s = (struct f2fs_summary *)(kaddr + offset);
1462 seg_i->sum_blk->entries[j] = *s;
1463 offset += SUMMARY_SIZE;
1464 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1465 SUM_FOOTER_SIZE)
1466 continue;
1467
1468 f2fs_put_page(page, 1);
1469 page = NULL;
1470
1471 page = get_meta_page(sbi, start++);
1472 kaddr = (unsigned char *)page_address(page);
1473 offset = 0;
1474 }
1475 }
1476 f2fs_put_page(page, 1);
1477 return 0;
1478}
1479
1480static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1481{
1482 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1483 struct f2fs_summary_block *sum;
1484 struct curseg_info *curseg;
1485 struct page *new;
1486 unsigned short blk_off;
1487 unsigned int segno = 0;
1488 block_t blk_addr = 0;
1489
1490 /* get segment number and block addr */
1491 if (IS_DATASEG(type)) {
1492 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1493 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1494 CURSEG_HOT_DATA]);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001495 if (__exist_node_summaries(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001496 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1497 else
1498 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1499 } else {
1500 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1501 CURSEG_HOT_NODE]);
1502 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1503 CURSEG_HOT_NODE]);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001504 if (__exist_node_summaries(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001505 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1506 type - CURSEG_HOT_NODE);
1507 else
1508 blk_addr = GET_SUM_BLOCK(sbi, segno);
1509 }
1510
1511 new = get_meta_page(sbi, blk_addr);
1512 sum = (struct f2fs_summary_block *)page_address(new);
1513
1514 if (IS_NODESEG(type)) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001515 if (__exist_node_summaries(sbi)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001516 struct f2fs_summary *ns = &sum->entries[0];
1517 int i;
1518 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1519 ns->version = 0;
1520 ns->ofs_in_node = 0;
1521 }
1522 } else {
Gu Zheng12e374b2014-03-07 18:43:36 +08001523 int err;
1524
1525 err = restore_node_summary(sbi, segno, sum);
1526 if (err) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001527 f2fs_put_page(new, 1);
Gu Zheng12e374b2014-03-07 18:43:36 +08001528 return err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001529 }
1530 }
1531 }
1532
1533 /* set uncompleted segment to curseg */
1534 curseg = CURSEG_I(sbi, type);
1535 mutex_lock(&curseg->curseg_mutex);
1536 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1537 curseg->next_segno = segno;
1538 reset_curseg(sbi, type, 0);
1539 curseg->alloc_type = ckpt->alloc_type[type];
1540 curseg->next_blkoff = blk_off;
1541 mutex_unlock(&curseg->curseg_mutex);
1542 f2fs_put_page(new, 1);
1543 return 0;
1544}
1545
1546static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1547{
1548 int type = CURSEG_HOT_DATA;
Chao Yu32c234e2014-03-17 16:36:24 +08001549 int err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001550
1551 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001552 int npages = npages_for_summary_flush(sbi, true);
1553
1554 if (npages >= 2)
1555 ra_meta_pages(sbi, start_sum_block(sbi), npages,
1556 META_CP);
1557
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001558 /* restore for compacted data summary */
1559 if (read_compacted_summaries(sbi))
1560 return -EINVAL;
1561 type = CURSEG_HOT_NODE;
1562 }
1563
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001564 if (__exist_node_summaries(sbi))
1565 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
1566 NR_CURSEG_TYPE - type, META_CP);
1567
Chao Yu32c234e2014-03-17 16:36:24 +08001568 for (; type <= CURSEG_COLD_NODE; type++) {
1569 err = read_normal_summaries(sbi, type);
1570 if (err)
1571 return err;
1572 }
1573
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001574 return 0;
1575}
1576
1577static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1578{
1579 struct page *page;
1580 unsigned char *kaddr;
1581 struct f2fs_summary *summary;
1582 struct curseg_info *seg_i;
1583 int written_size = 0;
1584 int i, j;
1585
1586 page = grab_meta_page(sbi, blkaddr++);
1587 kaddr = (unsigned char *)page_address(page);
1588
1589 /* Step 1: write nat cache */
1590 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1591 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1592 written_size += SUM_JOURNAL_SIZE;
1593
1594 /* Step 2: write sit cache */
1595 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1596 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1597 SUM_JOURNAL_SIZE);
1598 written_size += SUM_JOURNAL_SIZE;
1599
1600 /* Step 3: write summary entries */
1601 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1602 unsigned short blkoff;
1603 seg_i = CURSEG_I(sbi, i);
1604 if (sbi->ckpt->alloc_type[i] == SSR)
1605 blkoff = sbi->blocks_per_seg;
1606 else
1607 blkoff = curseg_blkoff(sbi, i);
1608
1609 for (j = 0; j < blkoff; j++) {
1610 if (!page) {
1611 page = grab_meta_page(sbi, blkaddr++);
1612 kaddr = (unsigned char *)page_address(page);
1613 written_size = 0;
1614 }
1615 summary = (struct f2fs_summary *)(kaddr + written_size);
1616 *summary = seg_i->sum_blk->entries[j];
1617 written_size += SUMMARY_SIZE;
1618
1619 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1620 SUM_FOOTER_SIZE)
1621 continue;
1622
1623 set_page_dirty(page);
1624 f2fs_put_page(page, 1);
1625 page = NULL;
1626 }
1627 }
1628 if (page) {
1629 set_page_dirty(page);
1630 f2fs_put_page(page, 1);
1631 }
1632}
1633
1634static void write_normal_summaries(struct f2fs_sb_info *sbi,
1635 block_t blkaddr, int type)
1636{
1637 int i, end;
1638 if (IS_DATASEG(type))
1639 end = type + NR_CURSEG_DATA_TYPE;
1640 else
1641 end = type + NR_CURSEG_NODE_TYPE;
1642
1643 for (i = type; i < end; i++) {
1644 struct curseg_info *sum = CURSEG_I(sbi, i);
1645 mutex_lock(&sum->curseg_mutex);
1646 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1647 mutex_unlock(&sum->curseg_mutex);
1648 }
1649}
1650
1651void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1652{
1653 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1654 write_compacted_summaries(sbi, start_blk);
1655 else
1656 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1657}
1658
1659void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1660{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001661 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001662}
1663
1664int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1665 unsigned int val, int alloc)
1666{
1667 int i;
1668
1669 if (type == NAT_JOURNAL) {
1670 for (i = 0; i < nats_in_cursum(sum); i++) {
1671 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1672 return i;
1673 }
1674 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1675 return update_nats_in_cursum(sum, 1);
1676 } else if (type == SIT_JOURNAL) {
1677 for (i = 0; i < sits_in_cursum(sum); i++)
1678 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1679 return i;
1680 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1681 return update_sits_in_cursum(sum, 1);
1682 }
1683 return -1;
1684}
1685
1686static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1687 unsigned int segno)
1688{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001689 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001690}
1691
1692static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1693 unsigned int start)
1694{
1695 struct sit_info *sit_i = SIT_I(sbi);
1696 struct page *src_page, *dst_page;
1697 pgoff_t src_off, dst_off;
1698 void *src_addr, *dst_addr;
1699
1700 src_off = current_sit_addr(sbi, start);
1701 dst_off = next_sit_addr(sbi, src_off);
1702
1703 /* get current sit block page without lock */
1704 src_page = get_meta_page(sbi, src_off);
1705 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001706 f2fs_bug_on(sbi, PageDirty(src_page));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001707
1708 src_addr = page_address(src_page);
1709 dst_addr = page_address(dst_page);
1710 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1711
1712 set_page_dirty(dst_page);
1713 f2fs_put_page(src_page, 1);
1714
1715 set_to_next_sit(sit_i, start);
1716
1717 return dst_page;
1718}
1719
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001720static struct sit_entry_set *grab_sit_entry_set(void)
1721{
1722 struct sit_entry_set *ses =
1723 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC);
1724
1725 ses->entry_cnt = 0;
1726 INIT_LIST_HEAD(&ses->set_list);
1727 return ses;
1728}
1729
1730static void release_sit_entry_set(struct sit_entry_set *ses)
1731{
1732 list_del(&ses->set_list);
1733 kmem_cache_free(sit_entry_set_slab, ses);
1734}
1735
1736static void adjust_sit_entry_set(struct sit_entry_set *ses,
1737 struct list_head *head)
1738{
1739 struct sit_entry_set *next = ses;
1740
1741 if (list_is_last(&ses->set_list, head))
1742 return;
1743
1744 list_for_each_entry_continue(next, head, set_list)
1745 if (ses->entry_cnt <= next->entry_cnt)
1746 break;
1747
1748 list_move_tail(&ses->set_list, &next->set_list);
1749}
1750
1751static void add_sit_entry(unsigned int segno, struct list_head *head)
1752{
1753 struct sit_entry_set *ses;
1754 unsigned int start_segno = START_SEGNO(segno);
1755
1756 list_for_each_entry(ses, head, set_list) {
1757 if (ses->start_segno == start_segno) {
1758 ses->entry_cnt++;
1759 adjust_sit_entry_set(ses, head);
1760 return;
1761 }
1762 }
1763
1764 ses = grab_sit_entry_set();
1765
1766 ses->start_segno = start_segno;
1767 ses->entry_cnt++;
1768 list_add(&ses->set_list, head);
1769}
1770
1771static void add_sits_in_set(struct f2fs_sb_info *sbi)
1772{
1773 struct f2fs_sm_info *sm_info = SM_I(sbi);
1774 struct list_head *set_list = &sm_info->sit_entry_set;
1775 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
1776 unsigned int segno;
1777
1778 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
1779 add_sit_entry(segno, set_list);
1780}
1781
1782static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001783{
1784 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1785 struct f2fs_summary_block *sum = curseg->sum_blk;
1786 int i;
1787
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001788 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1789 unsigned int segno;
1790 bool dirtied;
1791
1792 segno = le32_to_cpu(segno_in_journal(sum, i));
1793 dirtied = __mark_sit_entry_dirty(sbi, segno);
1794
1795 if (!dirtied)
1796 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001797 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001798 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001799}
1800
1801/*
1802 * CP calls this function, which flushes SIT entries including sit_journal,
1803 * and moves prefree segs to free segs.
1804 */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001805void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001806{
1807 struct sit_info *sit_i = SIT_I(sbi);
1808 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1809 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1810 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001811 struct sit_entry_set *ses, *tmp;
1812 struct list_head *head = &SM_I(sbi)->sit_entry_set;
1813 bool to_journal = true;
1814 struct seg_entry *se;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001815
1816 mutex_lock(&curseg->curseg_mutex);
1817 mutex_lock(&sit_i->sentry_lock);
1818
1819 /*
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001820 * add and account sit entries of dirty bitmap in sit entry
1821 * set temporarily
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001822 */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001823 add_sits_in_set(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001824
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001825 /*
1826 * if there are no enough space in journal to store dirty sit
1827 * entries, remove all entries from journal and add and account
1828 * them in sit entry set.
1829 */
1830 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1831 remove_sits_in_journal(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001832
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001833 if (!sit_i->dirty_sentries)
1834 goto out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001835
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001836 /*
1837 * there are two steps to flush sit entries:
1838 * #1, flush sit entries to journal in current cold data summary block.
1839 * #2, flush sit entries to sit page.
1840 */
1841 list_for_each_entry_safe(ses, tmp, head, set_list) {
1842 struct page *page = NULL;
1843 struct f2fs_sit_block *raw_sit = NULL;
1844 unsigned int start_segno = ses->start_segno;
1845 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
1846 (unsigned long)MAIN_SEGS(sbi));
1847 unsigned int segno = start_segno;
Changman Leeb1a94e82013-11-15 10:42:51 +09001848
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001849 if (to_journal &&
1850 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
1851 to_journal = false;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001852
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001853 if (!to_journal) {
1854 page = get_next_sit_page(sbi, start_segno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001855 raw_sit = page_address(page);
1856 }
1857
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001858 /* flush dirty sit entries in region of current sit set */
1859 for_each_set_bit_from(segno, bitmap, end) {
1860 int offset, sit_offset;
1861
1862 se = get_seg_entry(sbi, segno);
1863
1864 /* add discard candidates */
1865 if (cpc->reason != CP_DISCARD) {
1866 cpc->trim_start = segno;
1867 add_discard_addrs(sbi, cpc);
1868 }
1869
1870 if (to_journal) {
1871 offset = lookup_journal_in_cursum(sum,
1872 SIT_JOURNAL, segno, 1);
1873 f2fs_bug_on(sbi, offset < 0);
1874 segno_in_journal(sum, offset) =
1875 cpu_to_le32(segno);
1876 seg_info_to_raw_sit(se,
1877 &sit_in_journal(sum, offset));
1878 } else {
1879 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1880 seg_info_to_raw_sit(se,
1881 &raw_sit->entries[sit_offset]);
1882 }
1883
1884 __clear_bit(segno, bitmap);
1885 sit_i->dirty_sentries--;
1886 ses->entry_cnt--;
1887 }
1888
1889 if (!to_journal)
1890 f2fs_put_page(page, 1);
1891
1892 f2fs_bug_on(sbi, ses->entry_cnt);
1893 release_sit_entry_set(ses);
1894 }
1895
1896 f2fs_bug_on(sbi, !list_empty(head));
1897 f2fs_bug_on(sbi, sit_i->dirty_sentries);
1898out:
1899 if (cpc->reason == CP_DISCARD) {
1900 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
1901 add_discard_addrs(sbi, cpc);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001902 }
1903 mutex_unlock(&sit_i->sentry_lock);
1904 mutex_unlock(&curseg->curseg_mutex);
1905
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001906 set_prefree_as_free_segments(sbi);
1907}
1908
1909static int build_sit_info(struct f2fs_sb_info *sbi)
1910{
1911 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1912 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1913 struct sit_info *sit_i;
1914 unsigned int sit_segs, start;
1915 char *src_bitmap, *dst_bitmap;
1916 unsigned int bitmap_size;
1917
1918 /* allocate memory for SIT information */
1919 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1920 if (!sit_i)
1921 return -ENOMEM;
1922
1923 SM_I(sbi)->sit_info = sit_i;
1924
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001925 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001926 if (!sit_i->sentries)
1927 return -ENOMEM;
1928
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001929 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001930 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1931 if (!sit_i->dirty_sentries_bitmap)
1932 return -ENOMEM;
1933
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001934 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001935 sit_i->sentries[start].cur_valid_map
1936 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1937 sit_i->sentries[start].ckpt_valid_map
1938 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1939 if (!sit_i->sentries[start].cur_valid_map
1940 || !sit_i->sentries[start].ckpt_valid_map)
1941 return -ENOMEM;
1942 }
1943
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001944 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1945 if (!sit_i->tmp_map)
1946 return -ENOMEM;
1947
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001948 if (sbi->segs_per_sec > 1) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001949 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) *
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001950 sizeof(struct sec_entry));
1951 if (!sit_i->sec_entries)
1952 return -ENOMEM;
1953 }
1954
1955 /* get information related with SIT */
1956 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1957
1958 /* setup SIT bitmap from ckeckpoint pack */
1959 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1960 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1961
1962 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1963 if (!dst_bitmap)
1964 return -ENOMEM;
1965
1966 /* init SIT information */
1967 sit_i->s_ops = &default_salloc_ops;
1968
1969 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1970 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1971 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1972 sit_i->sit_bitmap = dst_bitmap;
1973 sit_i->bitmap_size = bitmap_size;
1974 sit_i->dirty_sentries = 0;
1975 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1976 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1977 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1978 mutex_init(&sit_i->sentry_lock);
1979 return 0;
1980}
1981
1982static int build_free_segmap(struct f2fs_sb_info *sbi)
1983{
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001984 struct free_segmap_info *free_i;
1985 unsigned int bitmap_size, sec_bitmap_size;
1986
1987 /* allocate memory for free segmap information */
1988 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1989 if (!free_i)
1990 return -ENOMEM;
1991
1992 SM_I(sbi)->free_info = free_i;
1993
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001994 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001995 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1996 if (!free_i->free_segmap)
1997 return -ENOMEM;
1998
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001999 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002000 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
2001 if (!free_i->free_secmap)
2002 return -ENOMEM;
2003
2004 /* set all segments as dirty temporarily */
2005 memset(free_i->free_segmap, 0xff, bitmap_size);
2006 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2007
2008 /* init free segmap information */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002009 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002010 free_i->free_segments = 0;
2011 free_i->free_sections = 0;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002012 spin_lock_init(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002013 return 0;
2014}
2015
2016static int build_curseg(struct f2fs_sb_info *sbi)
2017{
2018 struct curseg_info *array;
2019 int i;
2020
Evan McClainf3f030d2014-07-17 21:16:35 -04002021 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002022 if (!array)
2023 return -ENOMEM;
2024
2025 SM_I(sbi)->curseg_array = array;
2026
2027 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2028 mutex_init(&array[i].curseg_mutex);
2029 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
2030 if (!array[i].sum_blk)
2031 return -ENOMEM;
2032 array[i].segno = NULL_SEGNO;
2033 array[i].next_blkoff = 0;
2034 }
2035 return restore_curseg_summaries(sbi);
2036}
2037
2038static void build_sit_entries(struct f2fs_sb_info *sbi)
2039{
2040 struct sit_info *sit_i = SIT_I(sbi);
2041 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2042 struct f2fs_summary_block *sum = curseg->sum_blk;
Changman Leeb1a94e82013-11-15 10:42:51 +09002043 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2044 unsigned int i, start, end;
2045 unsigned int readed, start_blk = 0;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002046 int nrpages = MAX_BIO_BLOCKS(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002047
Changman Leeb1a94e82013-11-15 10:42:51 +09002048 do {
Chao Yu624b14f2014-02-07 16:11:53 +08002049 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002050
Changman Leeb1a94e82013-11-15 10:42:51 +09002051 start = start_blk * sit_i->sents_per_block;
2052 end = (start_blk + readed) * sit_i->sents_per_block;
2053
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002054 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Changman Leeb1a94e82013-11-15 10:42:51 +09002055 struct seg_entry *se = &sit_i->sentries[start];
2056 struct f2fs_sit_block *sit_blk;
2057 struct f2fs_sit_entry sit;
2058 struct page *page;
2059
2060 mutex_lock(&curseg->curseg_mutex);
2061 for (i = 0; i < sits_in_cursum(sum); i++) {
2062 if (le32_to_cpu(segno_in_journal(sum, i))
2063 == start) {
2064 sit = sit_in_journal(sum, i);
2065 mutex_unlock(&curseg->curseg_mutex);
2066 goto got_it;
2067 }
2068 }
2069 mutex_unlock(&curseg->curseg_mutex);
2070
2071 page = get_current_sit_page(sbi, start);
2072 sit_blk = (struct f2fs_sit_block *)page_address(page);
2073 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2074 f2fs_put_page(page, 1);
2075got_it:
2076 check_block_count(sbi, start, &sit);
2077 seg_info_from_raw_sit(se, &sit);
2078 if (sbi->segs_per_sec > 1) {
2079 struct sec_entry *e = get_sec_entry(sbi, start);
2080 e->valid_blocks += se->valid_blocks;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002081 }
2082 }
Changman Leeb1a94e82013-11-15 10:42:51 +09002083 start_blk += readed;
2084 } while (start_blk < sit_blk_cnt);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002085}
2086
2087static void init_free_segmap(struct f2fs_sb_info *sbi)
2088{
2089 unsigned int start;
2090 int type;
2091
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002092 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002093 struct seg_entry *sentry = get_seg_entry(sbi, start);
2094 if (!sentry->valid_blocks)
2095 __set_free(sbi, start);
2096 }
2097
2098 /* set use the current segments */
2099 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2100 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2101 __set_test_and_inuse(sbi, curseg_t->segno);
2102 }
2103}
2104
2105static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2106{
2107 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2108 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002109 unsigned int segno = 0, offset = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002110 unsigned short valid_blocks;
2111
2112 while (1) {
2113 /* find dirty segment based on free segmap */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002114 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2115 if (segno >= MAIN_SEGS(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002116 break;
2117 offset = segno + 1;
2118 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002119 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002120 continue;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002121 if (valid_blocks > sbi->blocks_per_seg) {
2122 f2fs_bug_on(sbi, 1);
2123 continue;
2124 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002125 mutex_lock(&dirty_i->seglist_lock);
2126 __locate_dirty_segment(sbi, segno, DIRTY);
2127 mutex_unlock(&dirty_i->seglist_lock);
2128 }
2129}
2130
2131static int init_victim_secmap(struct f2fs_sb_info *sbi)
2132{
2133 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002134 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002135
2136 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
2137 if (!dirty_i->victim_secmap)
2138 return -ENOMEM;
2139 return 0;
2140}
2141
2142static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2143{
2144 struct dirty_seglist_info *dirty_i;
2145 unsigned int bitmap_size, i;
2146
2147 /* allocate memory for dirty segments list information */
2148 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2149 if (!dirty_i)
2150 return -ENOMEM;
2151
2152 SM_I(sbi)->dirty_info = dirty_i;
2153 mutex_init(&dirty_i->seglist_lock);
2154
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002155 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002156
2157 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2158 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
2159 if (!dirty_i->dirty_segmap[i])
2160 return -ENOMEM;
2161 }
2162
2163 init_dirty_segmap(sbi);
2164 return init_victim_secmap(sbi);
2165}
2166
2167/*
2168 * Update min, max modified time for cost-benefit GC algorithm
2169 */
2170static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2171{
2172 struct sit_info *sit_i = SIT_I(sbi);
2173 unsigned int segno;
2174
2175 mutex_lock(&sit_i->sentry_lock);
2176
2177 sit_i->min_mtime = LLONG_MAX;
2178
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002179 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002180 unsigned int i;
2181 unsigned long long mtime = 0;
2182
2183 for (i = 0; i < sbi->segs_per_sec; i++)
2184 mtime += get_seg_entry(sbi, segno + i)->mtime;
2185
2186 mtime = div_u64(mtime, sbi->segs_per_sec);
2187
2188 if (sit_i->min_mtime > mtime)
2189 sit_i->min_mtime = mtime;
2190 }
2191 sit_i->max_mtime = get_mtime(sbi);
2192 mutex_unlock(&sit_i->sentry_lock);
2193}
2194
2195int build_segment_manager(struct f2fs_sb_info *sbi)
2196{
2197 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2198 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2199 struct f2fs_sm_info *sm_info;
2200 int err;
2201
2202 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2203 if (!sm_info)
2204 return -ENOMEM;
2205
2206 /* init sm info */
2207 sbi->sm_info = sm_info;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002208 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2209 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2210 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2211 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2212 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2213 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2214 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kimf183b112014-03-19 14:17:21 +09002215 sm_info->rec_prefree_segments = sm_info->main_segments *
2216 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002217 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Changman Leeb1a94e82013-11-15 10:42:51 +09002218 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002219 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Changman Leeb1a94e82013-11-15 10:42:51 +09002220
2221 INIT_LIST_HEAD(&sm_info->discard_list);
2222 sm_info->nr_discards = 0;
2223 sm_info->max_discards = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002224
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002225 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2226
2227 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2228
Evan McClainf3f030d2014-07-17 21:16:35 -04002229 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
2230 err = create_flush_cmd_control(sbi);
2231 if (err)
2232 return err;
2233 }
2234
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002235 err = build_sit_info(sbi);
2236 if (err)
2237 return err;
2238 err = build_free_segmap(sbi);
2239 if (err)
2240 return err;
2241 err = build_curseg(sbi);
2242 if (err)
2243 return err;
2244
2245 /* reinit free segmap based on SIT */
2246 build_sit_entries(sbi);
2247
2248 init_free_segmap(sbi);
2249 err = build_dirty_segmap(sbi);
2250 if (err)
2251 return err;
2252
2253 init_min_max_mtime(sbi);
2254 return 0;
2255}
2256
2257static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2258 enum dirty_type dirty_type)
2259{
2260 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2261
2262 mutex_lock(&dirty_i->seglist_lock);
2263 kfree(dirty_i->dirty_segmap[dirty_type]);
2264 dirty_i->nr_dirty[dirty_type] = 0;
2265 mutex_unlock(&dirty_i->seglist_lock);
2266}
2267
2268static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
2269{
2270 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2271 kfree(dirty_i->victim_secmap);
2272}
2273
2274static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2275{
2276 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2277 int i;
2278
2279 if (!dirty_i)
2280 return;
2281
2282 /* discard pre-free/dirty segments list */
2283 for (i = 0; i < NR_DIRTY_TYPE; i++)
2284 discard_dirty_segmap(sbi, i);
2285
2286 destroy_victim_secmap(sbi);
2287 SM_I(sbi)->dirty_info = NULL;
2288 kfree(dirty_i);
2289}
2290
2291static void destroy_curseg(struct f2fs_sb_info *sbi)
2292{
2293 struct curseg_info *array = SM_I(sbi)->curseg_array;
2294 int i;
2295
2296 if (!array)
2297 return;
2298 SM_I(sbi)->curseg_array = NULL;
2299 for (i = 0; i < NR_CURSEG_TYPE; i++)
2300 kfree(array[i].sum_blk);
2301 kfree(array);
2302}
2303
2304static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2305{
2306 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2307 if (!free_i)
2308 return;
2309 SM_I(sbi)->free_info = NULL;
2310 kfree(free_i->free_segmap);
2311 kfree(free_i->free_secmap);
2312 kfree(free_i);
2313}
2314
2315static void destroy_sit_info(struct f2fs_sb_info *sbi)
2316{
2317 struct sit_info *sit_i = SIT_I(sbi);
2318 unsigned int start;
2319
2320 if (!sit_i)
2321 return;
2322
2323 if (sit_i->sentries) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002324 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002325 kfree(sit_i->sentries[start].cur_valid_map);
2326 kfree(sit_i->sentries[start].ckpt_valid_map);
2327 }
2328 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002329 kfree(sit_i->tmp_map);
2330
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002331 vfree(sit_i->sentries);
2332 vfree(sit_i->sec_entries);
2333 kfree(sit_i->dirty_sentries_bitmap);
2334
2335 SM_I(sbi)->sit_info = NULL;
2336 kfree(sit_i->sit_bitmap);
2337 kfree(sit_i);
2338}
2339
2340void destroy_segment_manager(struct f2fs_sb_info *sbi)
2341{
2342 struct f2fs_sm_info *sm_info = SM_I(sbi);
Evan McClainf3f030d2014-07-17 21:16:35 -04002343
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002344 if (!sm_info)
2345 return;
Evan McClainf3f030d2014-07-17 21:16:35 -04002346 destroy_flush_cmd_control(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002347 destroy_dirty_segmap(sbi);
2348 destroy_curseg(sbi);
2349 destroy_free_segmap(sbi);
2350 destroy_sit_info(sbi);
2351 sbi->sm_info = NULL;
2352 kfree(sm_info);
2353}
Changman Leeb1a94e82013-11-15 10:42:51 +09002354
2355int __init create_segment_manager_caches(void)
2356{
2357 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge33dcea2014-03-07 18:43:28 +08002358 sizeof(struct discard_entry));
Changman Leeb1a94e82013-11-15 10:42:51 +09002359 if (!discard_entry_slab)
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002360 goto fail;
2361
2362 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
2363 sizeof(struct sit_entry_set));
2364 if (!sit_entry_set_slab)
2365 goto destory_discard_entry;
2366
2367 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2368 sizeof(struct inmem_pages));
2369 if (!inmem_entry_slab)
2370 goto destroy_sit_entry_set;
Changman Leeb1a94e82013-11-15 10:42:51 +09002371 return 0;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002372
2373destroy_sit_entry_set:
2374 kmem_cache_destroy(sit_entry_set_slab);
2375destory_discard_entry:
2376 kmem_cache_destroy(discard_entry_slab);
2377fail:
2378 return -ENOMEM;
Changman Leeb1a94e82013-11-15 10:42:51 +09002379}
2380
2381void destroy_segment_manager_caches(void)
2382{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002383 kmem_cache_destroy(sit_entry_set_slab);
Changman Leeb1a94e82013-11-15 10:42:51 +09002384 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002385 kmem_cache_destroy(inmem_entry_slab);
Changman Leeb1a94e82013-11-15 10:42:51 +09002386}