blob: bc431d4568a93aea73861310f30dab5c86718140 [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>
Changman Leeb1a94e82013-11-15 10:42:51 +090017#include <linux/swap.h>
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -060018#include <linux/timer.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
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -060082 * @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.
Jaegeuk Kime0cea842015-02-18 20:43:11 -060085 *
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{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600137 while (!f2fs_test_bit(offset, (unsigned char *)addr))
138 offset++;
139
140 if (offset > size)
141 offset = size;
142
143 return offset;
144#if 0
Changman Leeb1a94e82013-11-15 10:42:51 +0900145 const unsigned long *p = addr + BIT_WORD(offset);
146 unsigned long result = offset & ~(BITS_PER_LONG - 1);
147 unsigned long tmp;
148 unsigned long mask, submask;
149 unsigned long quot, rest;
150
151 if (offset >= size)
152 return size;
153
154 size -= result;
155 offset %= BITS_PER_LONG;
156 if (!offset)
157 goto aligned;
158
159 tmp = *(p++);
160 quot = (offset >> 3) << 3;
161 rest = offset & 0x7;
162 mask = ~0UL << quot;
163 submask = (unsigned char)(0xff << rest) >> rest;
164 submask <<= quot;
165 mask &= submask;
166 tmp &= mask;
167 if (size < BITS_PER_LONG)
168 goto found_first;
169 if (tmp)
170 goto found_middle;
171
172 size -= BITS_PER_LONG;
173 result += BITS_PER_LONG;
174aligned:
175 while (size & ~(BITS_PER_LONG-1)) {
176 tmp = *(p++);
177 if (tmp)
178 goto found_middle;
179 result += BITS_PER_LONG;
180 size -= BITS_PER_LONG;
181 }
182 if (!size)
183 return result;
184 tmp = *p;
185found_first:
186 tmp &= (~0UL >> (BITS_PER_LONG - size));
187 if (tmp == 0UL) /* Are any bits set? */
188 return result + size; /* Nope. */
189found_middle:
190 return result + __reverse_ffs(tmp);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600191#endif
Changman Leeb1a94e82013-11-15 10:42:51 +0900192}
193
194static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
195 unsigned long size, unsigned long offset)
196{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600197 while (f2fs_test_bit(offset, (unsigned char *)addr))
198 offset++;
199
200 if (offset > size)
201 offset = size;
202
203 return offset;
204#if 0
Changman Leeb1a94e82013-11-15 10:42:51 +0900205 const unsigned long *p = addr + BIT_WORD(offset);
206 unsigned long result = offset & ~(BITS_PER_LONG - 1);
207 unsigned long tmp;
208 unsigned long mask, submask;
209 unsigned long quot, rest;
210
211 if (offset >= size)
212 return size;
213
214 size -= result;
215 offset %= BITS_PER_LONG;
216 if (!offset)
217 goto aligned;
218
219 tmp = *(p++);
220 quot = (offset >> 3) << 3;
221 rest = offset & 0x7;
222 mask = ~(~0UL << quot);
223 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
224 submask <<= quot;
225 mask += submask;
226 tmp |= mask;
227 if (size < BITS_PER_LONG)
228 goto found_first;
229 if (~tmp)
230 goto found_middle;
231
232 size -= BITS_PER_LONG;
233 result += BITS_PER_LONG;
234aligned:
235 while (size & ~(BITS_PER_LONG - 1)) {
236 tmp = *(p++);
237 if (~tmp)
238 goto found_middle;
239 result += BITS_PER_LONG;
240 size -= BITS_PER_LONG;
241 }
242 if (!size)
243 return result;
244 tmp = *p;
245
246found_first:
247 tmp |= ~0UL << size;
248 if (tmp == ~0UL) /* Are any bits zero? */
249 return result + size; /* Nope. */
250found_middle:
251 return result + __reverse_ffz(tmp);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600252#endif
Changman Leeb1a94e82013-11-15 10:42:51 +0900253}
254
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600255void register_inmem_page(struct inode *inode, struct page *page)
256{
257 struct f2fs_inode_info *fi = F2FS_I(inode);
258 struct inmem_pages *new;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600259
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600260 f2fs_trace_pid(page);
261
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600262 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
263 SetPagePrivate(page);
264
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600265 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
266
267 /* add atomic page indices to the list */
268 new->page = page;
269 INIT_LIST_HEAD(&new->list);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600270
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600271 /* increase reference count with clean state */
272 mutex_lock(&fi->inmem_lock);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600273 get_page(page);
274 list_add_tail(&new->list, &fi->inmem_pages);
275 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
276 mutex_unlock(&fi->inmem_lock);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600277
278 trace_f2fs_register_inmem_page(page, INMEM);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600279}
280
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600281int commit_inmem_pages(struct inode *inode, bool abort)
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600282{
283 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
284 struct f2fs_inode_info *fi = F2FS_I(inode);
285 struct inmem_pages *cur, *tmp;
286 bool submit_bio = false;
287 struct f2fs_io_info fio = {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600288 .sbi = sbi,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600289 .type = DATA,
290 .rw = WRITE_SYNC | REQ_PRIO,
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600291 .encrypted_page = NULL,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600292 };
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600293 int err = 0;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600294
295 /*
296 * The abort is true only when f2fs_evict_inode is called.
297 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
298 * that we don't need to call f2fs_balance_fs.
299 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
300 * inode becomes free by iget_locked in f2fs_iget.
301 */
302 if (!abort) {
303 f2fs_balance_fs(sbi);
304 f2fs_lock_op(sbi);
305 }
306
307 mutex_lock(&fi->inmem_lock);
308 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600309 lock_page(cur->page);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600310 if (!abort) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600311 if (cur->page->mapping == inode->i_mapping) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600312 set_page_dirty(cur->page);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600313 f2fs_wait_on_page_writeback(cur->page, DATA);
314 if (clear_page_dirty_for_io(cur->page))
315 inode_dec_dirty_pages(inode);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600316 trace_f2fs_commit_inmem_page(cur->page, INMEM);
317 fio.page = cur->page;
318 err = do_write_data_page(&fio);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600319 submit_bio = true;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600320 if (err) {
321 unlock_page(cur->page);
322 break;
323 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600324 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600325 } else {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600326 trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600327 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600328 set_page_private(cur->page, 0);
329 ClearPagePrivate(cur->page);
330 f2fs_put_page(cur->page, 1);
331
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600332 list_del(&cur->list);
333 kmem_cache_free(inmem_entry_slab, cur);
334 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
335 }
336 mutex_unlock(&fi->inmem_lock);
337
338 if (!abort) {
339 f2fs_unlock_op(sbi);
340 if (submit_bio)
341 f2fs_submit_merged_bio(sbi, DATA, WRITE);
342 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600343 return err;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600344}
345
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800346/*
347 * This function balances dirty node and dentry pages.
348 * In addition, it controls garbage collection.
349 */
350void f2fs_balance_fs(struct f2fs_sb_info *sbi)
351{
352 /*
353 * We should do GC or end up with checkpoint, if there are so many dirty
354 * dir/node pages without enough free segments.
355 */
356 if (has_not_enough_free_secs(sbi, 0)) {
357 mutex_lock(&sbi->gc_mutex);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600358 f2fs_gc(sbi, false);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800359 }
360}
361
362void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
363{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600364 /* try to shrink extent cache when there is no enough memory */
365 if (!available_free_memory(sbi, EXTENT_CACHE))
366 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
367
368 /* check the # of cached NAT entries */
369 if (!available_free_memory(sbi, NAT_ENTRIES))
370 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
371
372 if (!available_free_memory(sbi, FREE_NIDS))
373 try_to_free_nids(sbi, NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES);
374
375 /* checkpoint is the only way to shrink partial cached entries */
376 if (!available_free_memory(sbi, NAT_ENTRIES) ||
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600377 excess_prefree_segs(sbi) ||
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600378 !available_free_memory(sbi, INO_ENTRIES) ||
379 jiffies > sbi->cp_expires)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800380 f2fs_sync_fs(sbi->sb, true);
381}
382
Evan McClainf3f030d2014-07-17 21:16:35 -0400383struct __submit_bio_ret {
384 struct completion event;
385 int error;
386};
387
388static void __submit_bio_wait_endio(struct bio *bio, int error)
389{
390 struct __submit_bio_ret *ret = bio->bi_private;
391
392 ret->error = error;
393 complete(&ret->event);
394}
395
396static int __submit_bio_wait(int rw, struct bio *bio)
397{
398 struct __submit_bio_ret ret;
399
400 rw |= REQ_SYNC;
401 init_completion(&ret.event);
402 bio->bi_private = &ret;
403 bio->bi_end_io = __submit_bio_wait_endio;
404 submit_bio(rw, bio);
405 wait_for_completion(&ret.event);
406
407 return ret.error;
408}
409
410static int issue_flush_thread(void *data)
411{
412 struct f2fs_sb_info *sbi = data;
413 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
414 wait_queue_head_t *q = &fcc->flush_wait_queue;
415repeat:
416 if (kthread_should_stop())
417 return 0;
418
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600419 if (!llist_empty(&fcc->issue_list)) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600420 struct bio *bio;
Evan McClainf3f030d2014-07-17 21:16:35 -0400421 struct flush_cmd *cmd, *next;
422 int ret;
423
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600424 bio = f2fs_bio_alloc(0);
425
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600426 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
427 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
428
Evan McClainf3f030d2014-07-17 21:16:35 -0400429 bio->bi_bdev = sbi->sb->s_bdev;
430 ret = __submit_bio_wait(WRITE_FLUSH, bio);
431
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600432 llist_for_each_entry_safe(cmd, next,
433 fcc->dispatch_list, llnode) {
Evan McClainf3f030d2014-07-17 21:16:35 -0400434 cmd->ret = ret;
Evan McClainf3f030d2014-07-17 21:16:35 -0400435 complete(&cmd->wait);
436 }
437 bio_put(bio);
438 fcc->dispatch_list = NULL;
439 }
440
441 wait_event_interruptible(*q,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600442 kthread_should_stop() || !llist_empty(&fcc->issue_list));
Evan McClainf3f030d2014-07-17 21:16:35 -0400443 goto repeat;
444}
445
446int f2fs_issue_flush(struct f2fs_sb_info *sbi)
447{
448 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
449 struct flush_cmd cmd;
450
Jaegeuk Kim90421d22014-07-25 17:46:10 -0700451 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
452 test_opt(sbi, FLUSH_MERGE));
453
Jaegeuk Kim6f6541b2014-07-23 09:57:31 -0700454 if (test_opt(sbi, NOBARRIER))
455 return 0;
456
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600457 if (!test_opt(sbi, FLUSH_MERGE)) {
458 struct bio *bio = f2fs_bio_alloc(0);
459 int ret;
460
461 bio->bi_bdev = sbi->sb->s_bdev;
462 ret = __submit_bio_wait(WRITE_FLUSH, bio);
463 bio_put(bio);
464 return ret;
465 }
Evan McClainf3f030d2014-07-17 21:16:35 -0400466
467 init_completion(&cmd.wait);
Evan McClainf3f030d2014-07-17 21:16:35 -0400468
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600469 llist_add(&cmd.llnode, &fcc->issue_list);
Evan McClainf3f030d2014-07-17 21:16:35 -0400470
471 if (!fcc->dispatch_list)
472 wake_up(&fcc->flush_wait_queue);
473
474 wait_for_completion(&cmd.wait);
475
476 return cmd.ret;
477}
478
479int create_flush_cmd_control(struct f2fs_sb_info *sbi)
480{
481 dev_t dev = sbi->sb->s_bdev->bd_dev;
482 struct flush_cmd_control *fcc;
483 int err = 0;
484
485 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
486 if (!fcc)
487 return -ENOMEM;
Evan McClainf3f030d2014-07-17 21:16:35 -0400488 init_waitqueue_head(&fcc->flush_wait_queue);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600489 init_llist_head(&fcc->issue_list);
Evan McClainf3f030d2014-07-17 21:16:35 -0400490 SM_I(sbi)->cmd_control_info = fcc;
491 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
492 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
493 if (IS_ERR(fcc->f2fs_issue_flush)) {
494 err = PTR_ERR(fcc->f2fs_issue_flush);
495 kfree(fcc);
496 SM_I(sbi)->cmd_control_info = NULL;
497 return err;
498 }
499
500 return err;
501}
502
503void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
504{
505 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
506
507 if (fcc && fcc->f2fs_issue_flush)
508 kthread_stop(fcc->f2fs_issue_flush);
509 kfree(fcc);
510 SM_I(sbi)->cmd_control_info = NULL;
511}
512
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800513static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
514 enum dirty_type dirty_type)
515{
516 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
517
518 /* need not be added */
519 if (IS_CURSEG(sbi, segno))
520 return;
521
522 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
523 dirty_i->nr_dirty[dirty_type]++;
524
525 if (dirty_type == DIRTY) {
526 struct seg_entry *sentry = get_seg_entry(sbi, segno);
527 enum dirty_type t = sentry->type;
528
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600529 if (unlikely(t >= DIRTY)) {
530 f2fs_bug_on(sbi, 1);
531 return;
532 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800533 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
534 dirty_i->nr_dirty[t]++;
535 }
536}
537
538static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
539 enum dirty_type dirty_type)
540{
541 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
542
543 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
544 dirty_i->nr_dirty[dirty_type]--;
545
546 if (dirty_type == DIRTY) {
547 struct seg_entry *sentry = get_seg_entry(sbi, segno);
548 enum dirty_type t = sentry->type;
549
550 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
551 dirty_i->nr_dirty[t]--;
552
553 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
554 clear_bit(GET_SECNO(sbi, segno),
555 dirty_i->victim_secmap);
556 }
557}
558
559/*
560 * Should not occur error such as -ENOMEM.
561 * Adding dirty entry into seglist is not critical operation.
562 * If a given segment is one of current working segments, it won't be added.
563 */
564static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
565{
566 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
567 unsigned short valid_blocks;
568
569 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
570 return;
571
572 mutex_lock(&dirty_i->seglist_lock);
573
574 valid_blocks = get_valid_blocks(sbi, segno, 0);
575
576 if (valid_blocks == 0) {
577 __locate_dirty_segment(sbi, segno, PRE);
578 __remove_dirty_segment(sbi, segno, DIRTY);
579 } else if (valid_blocks < sbi->blocks_per_seg) {
580 __locate_dirty_segment(sbi, segno, DIRTY);
581 } else {
582 /* Recovery routine with SSR needs this */
583 __remove_dirty_segment(sbi, segno, DIRTY);
584 }
585
586 mutex_unlock(&dirty_i->seglist_lock);
587}
588
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900589static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
Changman Leeb1a94e82013-11-15 10:42:51 +0900590 block_t blkstart, block_t blklen)
591{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600592 sector_t start = SECTOR_FROM_BLOCK(blkstart);
593 sector_t len = SECTOR_FROM_BLOCK(blklen);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600594 struct seg_entry *se;
595 unsigned int offset;
596 block_t i;
597
598 for (i = blkstart; i < blkstart + blklen; i++) {
599 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
600 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
601
602 if (!f2fs_test_and_set_bit(offset, se->discard_map))
603 sbi->discard_blks--;
604 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900605 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900606 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
607}
608
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600609bool discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr)
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900610{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600611 int err = -ENOTSUPP;
612
613 if (test_opt(sbi, DISCARD)) {
614 struct seg_entry *se = get_seg_entry(sbi,
615 GET_SEGNO(sbi, blkaddr));
616 unsigned int offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
617
618 if (f2fs_test_bit(offset, se->discard_map))
619 return false;
620
621 err = f2fs_issue_discard(sbi, blkaddr, 1);
Jaegeuk Kimf8ff1412014-04-15 13:57:55 +0900622 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600623
624 if (err) {
625 update_meta_page(sbi, NULL, blkaddr);
626 return true;
627 }
628 return false;
Changman Leeb1a94e82013-11-15 10:42:51 +0900629}
630
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600631static void __add_discard_entry(struct f2fs_sb_info *sbi,
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600632 struct cp_control *cpc, struct seg_entry *se,
633 unsigned int start, unsigned int end)
Changman Leeb1a94e82013-11-15 10:42:51 +0900634{
635 struct list_head *head = &SM_I(sbi)->discard_list;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600636 struct discard_entry *new, *last;
637
638 if (!list_empty(head)) {
639 last = list_last_entry(head, struct discard_entry, list);
640 if (START_BLOCK(sbi, cpc->trim_start) + start ==
641 last->blkaddr + last->len) {
642 last->len += end - start;
643 goto done;
644 }
645 }
646
647 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
648 INIT_LIST_HEAD(&new->list);
649 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
650 new->len = end - start;
651 list_add_tail(&new->list, head);
652done:
653 SM_I(sbi)->nr_discards += end - start;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600654}
655
656static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
657{
Changman Leeb1a94e82013-11-15 10:42:51 +0900658 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
659 int max_blocks = sbi->blocks_per_seg;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600660 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
Changman Leeb1a94e82013-11-15 10:42:51 +0900661 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
662 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600663 unsigned long *discard_map = (unsigned long *)se->discard_map;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600664 unsigned long *dmap = SIT_I(sbi)->tmp_map;
Changman Leeb1a94e82013-11-15 10:42:51 +0900665 unsigned int start = 0, end = -1;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600666 bool force = (cpc->reason == CP_DISCARD);
Changman Leeb1a94e82013-11-15 10:42:51 +0900667 int i;
668
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600669 if (se->valid_blocks == max_blocks)
Changman Leeb1a94e82013-11-15 10:42:51 +0900670 return;
671
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600672 if (!force) {
673 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
674 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600675 return;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600676 }
677
Changman Leeb1a94e82013-11-15 10:42:51 +0900678 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
679 for (i = 0; i < entries; i++)
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600680 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600681 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
Changman Leeb1a94e82013-11-15 10:42:51 +0900682
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600683 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
Changman Leeb1a94e82013-11-15 10:42:51 +0900684 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
685 if (start >= max_blocks)
686 break;
687
688 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600689 __add_discard_entry(sbi, cpc, se, start, end);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600690 }
691}
692
693void release_discard_addrs(struct f2fs_sb_info *sbi)
694{
695 struct list_head *head = &(SM_I(sbi)->discard_list);
696 struct discard_entry *entry, *this;
697
698 /* drop caches */
699 list_for_each_entry_safe(entry, this, head, list) {
700 list_del(&entry->list);
701 kmem_cache_free(discard_entry_slab, entry);
Changman Leeb1a94e82013-11-15 10:42:51 +0900702 }
703}
704
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800705/*
706 * Should call clear_prefree_segments after checkpoint is done.
707 */
708static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
709{
710 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Chao Yu00ebaff2014-08-04 10:10:07 +0800711 unsigned int segno;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800712
713 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600714 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800715 __set_test_and_free(sbi, segno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800716 mutex_unlock(&dirty_i->seglist_lock);
717}
718
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600719void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800720{
Changman Leeb1a94e82013-11-15 10:42:51 +0900721 struct list_head *head = &(SM_I(sbi)->discard_list);
Chao Yu48c561a2014-03-29 11:33:17 +0800722 struct discard_entry *entry, *this;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800723 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
724 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800725 unsigned int start = 0, end = -1;
726
727 mutex_lock(&dirty_i->seglist_lock);
728
729 while (1) {
730 int i;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600731 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
732 if (start >= MAIN_SEGS(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800733 break;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600734 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
735 start + 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800736
737 for (i = start; i < end; i++)
738 clear_bit(i, prefree_map);
739
740 dirty_i->nr_dirty[PRE] -= end - start;
741
742 if (!test_opt(sbi, DISCARD))
743 continue;
744
Changman Leeb1a94e82013-11-15 10:42:51 +0900745 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
746 (end - start) << sbi->log_blocks_per_seg);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800747 }
748 mutex_unlock(&dirty_i->seglist_lock);
Changman Leeb1a94e82013-11-15 10:42:51 +0900749
750 /* send small discards */
Chao Yu48c561a2014-03-29 11:33:17 +0800751 list_for_each_entry_safe(entry, this, head, list) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600752 if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen)
753 goto skip;
Changman Leeb1a94e82013-11-15 10:42:51 +0900754 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600755 cpc->trimmed += entry->len;
756skip:
Changman Leeb1a94e82013-11-15 10:42:51 +0900757 list_del(&entry->list);
758 SM_I(sbi)->nr_discards -= entry->len;
759 kmem_cache_free(discard_entry_slab, entry);
760 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800761}
762
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600763static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800764{
765 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600766
767 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800768 sit_i->dirty_sentries++;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600769 return false;
770 }
771
772 return true;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800773}
774
775static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
776 unsigned int segno, int modified)
777{
778 struct seg_entry *se = get_seg_entry(sbi, segno);
779 se->type = type;
780 if (modified)
781 __mark_sit_entry_dirty(sbi, segno);
782}
783
784static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
785{
786 struct seg_entry *se;
787 unsigned int segno, offset;
788 long int new_vblocks;
789
790 segno = GET_SEGNO(sbi, blkaddr);
791
792 se = get_seg_entry(sbi, segno);
793 new_vblocks = se->valid_blocks + del;
Jaegeuk Kim26d12822014-02-04 13:01:10 +0900794 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800795
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600796 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800797 (new_vblocks > sbi->blocks_per_seg)));
798
799 se->valid_blocks = new_vblocks;
800 se->mtime = get_mtime(sbi);
801 SIT_I(sbi)->max_mtime = se->mtime;
802
803 /* Update valid block bitmap */
804 if (del > 0) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600805 if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
806 f2fs_bug_on(sbi, 1);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600807 if (!f2fs_test_and_set_bit(offset, se->discard_map))
808 sbi->discard_blks--;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800809 } else {
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600810 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
811 f2fs_bug_on(sbi, 1);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600812 if (f2fs_test_and_clear_bit(offset, se->discard_map))
813 sbi->discard_blks++;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800814 }
815 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
816 se->ckpt_valid_blocks += del;
817
818 __mark_sit_entry_dirty(sbi, segno);
819
820 /* update total number of valid blocks to be written in ckpt area */
821 SIT_I(sbi)->written_valid_blocks += del;
822
823 if (sbi->segs_per_sec > 1)
824 get_sec_entry(sbi, segno)->valid_blocks += del;
825}
826
Jaegeuk Kim655d2c12014-01-28 12:22:14 +0900827void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800828{
Jaegeuk Kim655d2c12014-01-28 12:22:14 +0900829 update_sit_entry(sbi, new, 1);
830 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
831 update_sit_entry(sbi, old, -1);
832
833 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
834 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800835}
836
837void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
838{
839 unsigned int segno = GET_SEGNO(sbi, addr);
840 struct sit_info *sit_i = SIT_I(sbi);
841
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600842 f2fs_bug_on(sbi, addr == NULL_ADDR);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800843 if (addr == NEW_ADDR)
844 return;
845
846 /* add it into sit main buffer */
847 mutex_lock(&sit_i->sentry_lock);
848
849 update_sit_entry(sbi, addr, -1);
850
851 /* add it into dirty seglist */
852 locate_dirty_segment(sbi, segno);
853
854 mutex_unlock(&sit_i->sentry_lock);
855}
856
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600857bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
858{
859 struct sit_info *sit_i = SIT_I(sbi);
860 unsigned int segno, offset;
861 struct seg_entry *se;
862 bool is_cp = false;
863
864 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
865 return true;
866
867 mutex_lock(&sit_i->sentry_lock);
868
869 segno = GET_SEGNO(sbi, blkaddr);
870 se = get_seg_entry(sbi, segno);
871 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
872
873 if (f2fs_test_bit(offset, se->ckpt_valid_map))
874 is_cp = true;
875
876 mutex_unlock(&sit_i->sentry_lock);
877
878 return is_cp;
879}
880
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800881/*
882 * This function should be resided under the curseg_mutex lock
883 */
884static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
885 struct f2fs_summary *sum)
886{
887 struct curseg_info *curseg = CURSEG_I(sbi, type);
888 void *addr = curseg->sum_blk;
889 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
890 memcpy(addr, sum, sizeof(struct f2fs_summary));
891}
892
893/*
894 * Calculate the number of current summary pages for writing
895 */
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600896int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800897{
898 int valid_sum_count = 0;
899 int i, sum_in_page;
900
901 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
902 if (sbi->ckpt->alloc_type[i] == SSR)
903 valid_sum_count += sbi->blocks_per_seg;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600904 else {
905 if (for_ra)
906 valid_sum_count += le16_to_cpu(
907 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
908 else
909 valid_sum_count += curseg_blkoff(sbi, i);
910 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800911 }
912
913 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
914 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
915 if (valid_sum_count <= sum_in_page)
916 return 1;
917 else if ((valid_sum_count - sum_in_page) <=
918 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
919 return 2;
920 return 3;
921}
922
923/*
924 * Caller should put this summary page
925 */
926struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
927{
928 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
929}
930
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600931void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
932{
933 struct page *page = grab_meta_page(sbi, blk_addr);
934 void *dst = page_address(page);
935
936 if (src)
937 memcpy(dst, src, PAGE_CACHE_SIZE);
938 else
939 memset(dst, 0, PAGE_CACHE_SIZE);
940 set_page_dirty(page);
941 f2fs_put_page(page, 1);
942}
943
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800944static void write_sum_page(struct f2fs_sb_info *sbi,
945 struct f2fs_summary_block *sum_blk, block_t blk_addr)
946{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600947 update_meta_page(sbi, (void *)sum_blk, blk_addr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800948}
949
950static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
951{
952 struct curseg_info *curseg = CURSEG_I(sbi, type);
953 unsigned int segno = curseg->segno + 1;
954 struct free_segmap_info *free_i = FREE_I(sbi);
955
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600956 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800957 return !test_bit(segno, free_i->free_segmap);
958 return 0;
959}
960
961/*
962 * Find a new segment from the free segments bitmap to right order
963 * This function should be returned with success, otherwise BUG
964 */
965static void get_new_segment(struct f2fs_sb_info *sbi,
966 unsigned int *newseg, bool new_sec, int dir)
967{
968 struct free_segmap_info *free_i = FREE_I(sbi);
969 unsigned int segno, secno, zoneno;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600970 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800971 unsigned int hint = *newseg / sbi->segs_per_sec;
972 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
973 unsigned int left_start = hint;
974 bool init = true;
975 int go_left = 0;
976 int i;
977
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600978 spin_lock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800979
980 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
981 segno = find_next_zero_bit(free_i->free_segmap,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600982 MAIN_SEGS(sbi), *newseg + 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800983 if (segno - *newseg < sbi->segs_per_sec -
984 (*newseg % sbi->segs_per_sec))
985 goto got_it;
986 }
987find_other_zone:
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600988 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
989 if (secno >= MAIN_SECS(sbi)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800990 if (dir == ALLOC_RIGHT) {
991 secno = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600992 MAIN_SECS(sbi), 0);
993 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800994 } else {
995 go_left = 1;
996 left_start = hint - 1;
997 }
998 }
999 if (go_left == 0)
1000 goto skip_left;
1001
1002 while (test_bit(left_start, free_i->free_secmap)) {
1003 if (left_start > 0) {
1004 left_start--;
1005 continue;
1006 }
1007 left_start = find_next_zero_bit(free_i->free_secmap,
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001008 MAIN_SECS(sbi), 0);
1009 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001010 break;
1011 }
1012 secno = left_start;
1013skip_left:
1014 hint = secno;
1015 segno = secno * sbi->segs_per_sec;
1016 zoneno = secno / sbi->secs_per_zone;
1017
1018 /* give up on finding another zone */
1019 if (!init)
1020 goto got_it;
1021 if (sbi->secs_per_zone == 1)
1022 goto got_it;
1023 if (zoneno == old_zoneno)
1024 goto got_it;
1025 if (dir == ALLOC_LEFT) {
1026 if (!go_left && zoneno + 1 >= total_zones)
1027 goto got_it;
1028 if (go_left && zoneno == 0)
1029 goto got_it;
1030 }
1031 for (i = 0; i < NR_CURSEG_TYPE; i++)
1032 if (CURSEG_I(sbi, i)->zone == zoneno)
1033 break;
1034
1035 if (i < NR_CURSEG_TYPE) {
1036 /* zone is in user, try another */
1037 if (go_left)
1038 hint = zoneno * sbi->secs_per_zone - 1;
1039 else if (zoneno + 1 >= total_zones)
1040 hint = 0;
1041 else
1042 hint = (zoneno + 1) * sbi->secs_per_zone;
1043 init = false;
1044 goto find_other_zone;
1045 }
1046got_it:
1047 /* set it as dirty segment in free segmap */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001048 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001049 __set_inuse(sbi, segno);
1050 *newseg = segno;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001051 spin_unlock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001052}
1053
1054static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1055{
1056 struct curseg_info *curseg = CURSEG_I(sbi, type);
1057 struct summary_footer *sum_footer;
1058
1059 curseg->segno = curseg->next_segno;
1060 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
1061 curseg->next_blkoff = 0;
1062 curseg->next_segno = NULL_SEGNO;
1063
1064 sum_footer = &(curseg->sum_blk->footer);
1065 memset(sum_footer, 0, sizeof(struct summary_footer));
1066 if (IS_DATASEG(type))
1067 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1068 if (IS_NODESEG(type))
1069 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1070 __set_sit_entry_type(sbi, type, curseg->segno, modified);
1071}
1072
1073/*
1074 * Allocate a current working segment.
1075 * This function always allocates a free segment in LFS manner.
1076 */
1077static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
1078{
1079 struct curseg_info *curseg = CURSEG_I(sbi, type);
1080 unsigned int segno = curseg->segno;
1081 int dir = ALLOC_LEFT;
1082
1083 write_sum_page(sbi, curseg->sum_blk,
1084 GET_SUM_BLOCK(sbi, segno));
1085 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1086 dir = ALLOC_RIGHT;
1087
1088 if (test_opt(sbi, NOHEAP))
1089 dir = ALLOC_RIGHT;
1090
1091 get_new_segment(sbi, &segno, new_sec, dir);
1092 curseg->next_segno = segno;
1093 reset_curseg(sbi, type, 1);
1094 curseg->alloc_type = LFS;
1095}
1096
1097static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1098 struct curseg_info *seg, block_t start)
1099{
1100 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
Changman Leeb1a94e82013-11-15 10:42:51 +09001101 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001102 unsigned long *target_map = SIT_I(sbi)->tmp_map;
Changman Leeb1a94e82013-11-15 10:42:51 +09001103 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1104 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1105 int i, pos;
1106
1107 for (i = 0; i < entries; i++)
1108 target_map[i] = ckpt_map[i] | cur_map[i];
1109
1110 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1111
1112 seg->next_blkoff = pos;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001113}
1114
1115/*
1116 * If a segment is written by LFS manner, next block offset is just obtained
1117 * by increasing the current block offset. However, if a segment is written by
1118 * SSR manner, next block offset obtained by calling __next_free_blkoff
1119 */
1120static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1121 struct curseg_info *seg)
1122{
1123 if (seg->alloc_type == SSR)
1124 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1125 else
1126 seg->next_blkoff++;
1127}
1128
1129/*
arter97f4081402014-08-06 23:22:50 +09001130 * This function always allocates a used segment(from dirty seglist) by SSR
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001131 * manner, so it should recover the existing segment information of valid blocks
1132 */
1133static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1134{
1135 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1136 struct curseg_info *curseg = CURSEG_I(sbi, type);
1137 unsigned int new_segno = curseg->next_segno;
1138 struct f2fs_summary_block *sum_node;
1139 struct page *sum_page;
1140
1141 write_sum_page(sbi, curseg->sum_blk,
1142 GET_SUM_BLOCK(sbi, curseg->segno));
1143 __set_test_and_inuse(sbi, new_segno);
1144
1145 mutex_lock(&dirty_i->seglist_lock);
1146 __remove_dirty_segment(sbi, new_segno, PRE);
1147 __remove_dirty_segment(sbi, new_segno, DIRTY);
1148 mutex_unlock(&dirty_i->seglist_lock);
1149
1150 reset_curseg(sbi, type, 1);
1151 curseg->alloc_type = SSR;
1152 __next_free_blkoff(sbi, curseg, 0);
1153
1154 if (reuse) {
1155 sum_page = get_sum_page(sbi, new_segno);
1156 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1157 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1158 f2fs_put_page(sum_page, 1);
1159 }
1160}
1161
1162static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1163{
1164 struct curseg_info *curseg = CURSEG_I(sbi, type);
1165 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1166
1167 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
1168 return v_ops->get_victim(sbi,
1169 &(curseg)->next_segno, BG_GC, type, SSR);
1170
1171 /* For data segments, let's do SSR more intensively */
1172 for (; type >= CURSEG_HOT_DATA; type--)
1173 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1174 BG_GC, type, SSR))
1175 return 1;
1176 return 0;
1177}
1178
1179/*
1180 * flush out current segment and replace it with new segment
1181 * This function should be returned with success, otherwise BUG
1182 */
1183static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1184 int type, bool force)
1185{
1186 struct curseg_info *curseg = CURSEG_I(sbi, type);
1187
1188 if (force)
1189 new_curseg(sbi, type, true);
1190 else if (type == CURSEG_WARM_NODE)
1191 new_curseg(sbi, type, false);
1192 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
1193 new_curseg(sbi, type, false);
1194 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1195 change_curseg(sbi, type, true);
1196 else
1197 new_curseg(sbi, type, false);
1198
1199 stat_inc_seg_type(sbi, curseg);
1200}
1201
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001202static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
1203{
1204 struct curseg_info *curseg = CURSEG_I(sbi, type);
1205 unsigned int old_segno;
1206
1207 old_segno = curseg->segno;
1208 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
1209 locate_dirty_segment(sbi, old_segno);
1210}
1211
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001212void allocate_new_segments(struct f2fs_sb_info *sbi)
1213{
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001214 int i;
1215
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001216 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
1217 __allocate_new_segments(sbi, i);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001218}
1219
1220static const struct segment_allocation default_salloc_ops = {
1221 .allocate_segment = allocate_segment_by_default,
1222};
1223
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001224int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1225{
1226 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1227 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
1228 unsigned int start_segno, end_segno;
1229 struct cp_control cpc;
1230
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001231 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001232 return -EINVAL;
1233
1234 cpc.trimmed = 0;
1235 if (end <= MAIN_BLKADDR(sbi))
1236 goto out;
1237
1238 /* start/end segment number in main_area */
1239 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1240 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1241 GET_SEGNO(sbi, end);
1242 cpc.reason = CP_DISCARD;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001243 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001244
1245 /* do checkpoint to issue discard commands safely */
1246 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1247 cpc.trim_start = start_segno;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001248
1249 if (sbi->discard_blks == 0)
1250 break;
1251 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1252 cpc.trim_end = end_segno;
1253 else
1254 cpc.trim_end = min_t(unsigned int,
1255 rounddown(start_segno +
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001256 BATCHED_TRIM_SEGMENTS(sbi),
1257 sbi->segs_per_sec) - 1, end_segno);
1258
1259 mutex_lock(&sbi->gc_mutex);
1260 write_checkpoint(sbi, &cpc);
1261 mutex_unlock(&sbi->gc_mutex);
1262 }
1263out:
1264 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
1265 return 0;
1266}
1267
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001268static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1269{
1270 struct curseg_info *curseg = CURSEG_I(sbi, type);
1271 if (curseg->next_blkoff < sbi->blocks_per_seg)
1272 return true;
1273 return false;
1274}
1275
1276static int __get_segment_type_2(struct page *page, enum page_type p_type)
1277{
1278 if (p_type == DATA)
1279 return CURSEG_HOT_DATA;
1280 else
1281 return CURSEG_HOT_NODE;
1282}
1283
1284static int __get_segment_type_4(struct page *page, enum page_type p_type)
1285{
1286 if (p_type == DATA) {
1287 struct inode *inode = page->mapping->host;
1288
1289 if (S_ISDIR(inode->i_mode))
1290 return CURSEG_HOT_DATA;
1291 else
1292 return CURSEG_COLD_DATA;
1293 } else {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001294 if (IS_DNODE(page) && is_cold_node(page))
1295 return CURSEG_WARM_NODE;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001296 else
1297 return CURSEG_COLD_NODE;
1298 }
1299}
1300
1301static int __get_segment_type_6(struct page *page, enum page_type p_type)
1302{
1303 if (p_type == DATA) {
1304 struct inode *inode = page->mapping->host;
1305
1306 if (S_ISDIR(inode->i_mode))
1307 return CURSEG_HOT_DATA;
1308 else if (is_cold_data(page) || file_is_cold(inode))
1309 return CURSEG_COLD_DATA;
1310 else
1311 return CURSEG_WARM_DATA;
1312 } else {
1313 if (IS_DNODE(page))
1314 return is_cold_node(page) ? CURSEG_WARM_NODE :
1315 CURSEG_HOT_NODE;
1316 else
1317 return CURSEG_COLD_NODE;
1318 }
1319}
1320
1321static int __get_segment_type(struct page *page, enum page_type p_type)
1322{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001323 switch (F2FS_P_SB(page)->active_logs) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001324 case 2:
1325 return __get_segment_type_2(page, p_type);
1326 case 4:
1327 return __get_segment_type_4(page, p_type);
1328 }
1329 /* NR_CURSEG_TYPE(6) logs by default */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001330 f2fs_bug_on(F2FS_P_SB(page),
1331 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001332 return __get_segment_type_6(page, p_type);
1333}
1334
Changman Leeb1a94e82013-11-15 10:42:51 +09001335void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1336 block_t old_blkaddr, block_t *new_blkaddr,
1337 struct f2fs_summary *sum, int type)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001338{
1339 struct sit_info *sit_i = SIT_I(sbi);
1340 struct curseg_info *curseg;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001341 bool direct_io = (type == CURSEG_DIRECT_IO);
1342
1343 type = direct_io ? CURSEG_WARM_DATA : type;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001344
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001345 curseg = CURSEG_I(sbi, type);
1346
1347 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001348 mutex_lock(&sit_i->sentry_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001349
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001350 /* direct_io'ed data is aligned to the segment for better performance */
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001351 if (direct_io && curseg->next_blkoff &&
1352 !has_not_enough_free_secs(sbi, 0))
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001353 __allocate_new_segments(sbi, type);
1354
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001355 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001356
1357 /*
1358 * __add_sum_entry should be resided under the curseg_mutex
1359 * because, this function updates a summary entry in the
1360 * current summary block.
1361 */
1362 __add_sum_entry(sbi, type, sum);
1363
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001364 __refresh_next_blkoff(sbi, curseg);
1365
1366 stat_inc_block_count(sbi, curseg);
1367
Jaegeuk Kim655d2c12014-01-28 12:22:14 +09001368 if (!__has_curseg_space(sbi, type))
1369 sit_i->s_ops->allocate_segment(sbi, type, false);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001370 /*
1371 * SIT information should be updated before segment allocation,
1372 * since SSR needs latest valid block information.
1373 */
1374 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
Jaegeuk Kim655d2c12014-01-28 12:22:14 +09001375
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001376 mutex_unlock(&sit_i->sentry_lock);
1377
Changman Leeb1a94e82013-11-15 10:42:51 +09001378 if (page && IS_NODESEG(type))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001379 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1380
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001381 mutex_unlock(&curseg->curseg_mutex);
1382}
1383
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001384static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
Changman Leeb1a94e82013-11-15 10:42:51 +09001385{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001386 int type = __get_segment_type(fio->page, fio->type);
Changman Leeb1a94e82013-11-15 10:42:51 +09001387
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001388 allocate_data_block(fio->sbi, fio->page, fio->blk_addr,
1389 &fio->blk_addr, sum, type);
Changman Leeb1a94e82013-11-15 10:42:51 +09001390
1391 /* writeout dirty page into bdev */
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001392 f2fs_submit_page_mbio(fio);
Changman Leeb1a94e82013-11-15 10:42:51 +09001393}
1394
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001395void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1396{
Changman Leeb1a94e82013-11-15 10:42:51 +09001397 struct f2fs_io_info fio = {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001398 .sbi = sbi,
Changman Leeb1a94e82013-11-15 10:42:51 +09001399 .type = META,
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001400 .rw = WRITE_SYNC | REQ_META | REQ_PRIO,
1401 .blk_addr = page->index,
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001402 .page = page,
1403 .encrypted_page = NULL,
Changman Leeb1a94e82013-11-15 10:42:51 +09001404 };
1405
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001406 set_page_writeback(page);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001407 f2fs_submit_page_mbio(&fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001408}
1409
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001410void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001411{
1412 struct f2fs_summary sum;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001413
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001414 set_summary(&sum, nid, 0, 0);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001415 do_write_page(&sum, fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001416}
1417
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001418void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001419{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001420 struct f2fs_sb_info *sbi = fio->sbi;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001421 struct f2fs_summary sum;
1422 struct node_info ni;
1423
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001424 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001425 get_node_info(sbi, dn->nid, &ni);
1426 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001427 do_write_page(&sum, fio);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001428 dn->data_blkaddr = fio->blk_addr;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001429}
1430
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001431void rewrite_data_page(struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001432{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001433 stat_inc_inplace_blocks(fio->sbi);
1434 f2fs_submit_page_mbio(fio);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001435}
1436
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001437static void __f2fs_replace_block(struct f2fs_sb_info *sbi,
1438 struct f2fs_summary *sum,
1439 block_t old_blkaddr, block_t new_blkaddr,
1440 bool recover_curseg)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001441{
1442 struct sit_info *sit_i = SIT_I(sbi);
1443 struct curseg_info *curseg;
1444 unsigned int segno, old_cursegno;
1445 struct seg_entry *se;
1446 int type;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001447 unsigned short old_blkoff;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001448
1449 segno = GET_SEGNO(sbi, new_blkaddr);
1450 se = get_seg_entry(sbi, segno);
1451 type = se->type;
1452
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001453 if (!recover_curseg) {
1454 /* for recovery flow */
1455 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1456 if (old_blkaddr == NULL_ADDR)
1457 type = CURSEG_COLD_DATA;
1458 else
1459 type = CURSEG_WARM_DATA;
1460 }
1461 } else {
1462 if (!IS_CURSEG(sbi, segno))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001463 type = CURSEG_WARM_DATA;
1464 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001465
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001466 curseg = CURSEG_I(sbi, type);
1467
1468 mutex_lock(&curseg->curseg_mutex);
1469 mutex_lock(&sit_i->sentry_lock);
1470
1471 old_cursegno = curseg->segno;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001472 old_blkoff = curseg->next_blkoff;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001473
1474 /* change the current segment */
1475 if (segno != curseg->segno) {
1476 curseg->next_segno = segno;
1477 change_curseg(sbi, type, true);
1478 }
1479
Jaegeuk Kim26d12822014-02-04 13:01:10 +09001480 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001481 __add_sum_entry(sbi, type, sum);
1482
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001483 if (!recover_curseg)
1484 update_sit_entry(sbi, new_blkaddr, 1);
1485 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1486 update_sit_entry(sbi, old_blkaddr, -1);
1487
1488 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1489 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
1490
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001491 locate_dirty_segment(sbi, old_cursegno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001492
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001493 if (recover_curseg) {
1494 if (old_cursegno != curseg->segno) {
1495 curseg->next_segno = old_cursegno;
1496 change_curseg(sbi, type, true);
1497 }
1498 curseg->next_blkoff = old_blkoff;
1499 }
1500
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001501 mutex_unlock(&sit_i->sentry_lock);
1502 mutex_unlock(&curseg->curseg_mutex);
1503}
1504
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001505void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
1506 block_t old_addr, block_t new_addr,
1507 unsigned char version, bool recover_curseg)
1508{
1509 struct f2fs_summary sum;
1510
1511 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
1512
1513 __f2fs_replace_block(sbi, &sum, old_addr, new_addr, recover_curseg);
1514
1515 dn->data_blkaddr = new_addr;
1516 set_data_blkaddr(dn);
1517 f2fs_update_extent_cache(dn);
1518}
1519
Evan McClainf3f030d2014-07-17 21:16:35 -04001520static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1521 struct page *page, enum page_type type)
1522{
1523 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1524 struct f2fs_bio_info *io = &sbi->write_io[btype];
1525 struct bio_vec *bvec;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001526 struct page *target;
Evan McClainf3f030d2014-07-17 21:16:35 -04001527 int i;
1528
1529 down_read(&io->io_rwsem);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001530 if (!io->bio) {
1531 up_read(&io->io_rwsem);
1532 return false;
1533 }
Evan McClainf3f030d2014-07-17 21:16:35 -04001534
1535 __bio_for_each_segment(bvec, io->bio, i, 0) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001536
1537 if (bvec->bv_page->mapping) {
1538 target = bvec->bv_page;
1539 } else {
1540 struct f2fs_crypto_ctx *ctx;
1541
1542 /* encrypted page */
1543 ctx = (struct f2fs_crypto_ctx *)page_private(
1544 bvec->bv_page);
1545 target = ctx->w.control_page;
1546 }
1547
1548 if (page == target) {
Evan McClainf3f030d2014-07-17 21:16:35 -04001549 up_read(&io->io_rwsem);
1550 return true;
1551 }
1552 }
1553
Evan McClainf3f030d2014-07-17 21:16:35 -04001554 up_read(&io->io_rwsem);
1555 return false;
1556}
1557
Changman Leeb1a94e82013-11-15 10:42:51 +09001558void f2fs_wait_on_page_writeback(struct page *page,
1559 enum page_type type)
1560{
Changman Leeb1a94e82013-11-15 10:42:51 +09001561 if (PageWriteback(page)) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001562 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1563
Evan McClainf3f030d2014-07-17 21:16:35 -04001564 if (is_merged_page(sbi, page, type))
1565 f2fs_submit_merged_bio(sbi, type, WRITE);
Changman Leeb1a94e82013-11-15 10:42:51 +09001566 wait_on_page_writeback(page);
1567 }
1568}
1569
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001570static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1571{
1572 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1573 struct curseg_info *seg_i;
1574 unsigned char *kaddr;
1575 struct page *page;
1576 block_t start;
1577 int i, j, offset;
1578
1579 start = start_sum_block(sbi);
1580
1581 page = get_meta_page(sbi, start++);
1582 kaddr = (unsigned char *)page_address(page);
1583
1584 /* Step 1: restore nat cache */
1585 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1586 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1587
1588 /* Step 2: restore sit cache */
1589 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1590 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1591 SUM_JOURNAL_SIZE);
1592 offset = 2 * SUM_JOURNAL_SIZE;
1593
1594 /* Step 3: restore summary entries */
1595 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1596 unsigned short blk_off;
1597 unsigned int segno;
1598
1599 seg_i = CURSEG_I(sbi, i);
1600 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1601 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1602 seg_i->next_segno = segno;
1603 reset_curseg(sbi, i, 0);
1604 seg_i->alloc_type = ckpt->alloc_type[i];
1605 seg_i->next_blkoff = blk_off;
1606
1607 if (seg_i->alloc_type == SSR)
1608 blk_off = sbi->blocks_per_seg;
1609
1610 for (j = 0; j < blk_off; j++) {
1611 struct f2fs_summary *s;
1612 s = (struct f2fs_summary *)(kaddr + offset);
1613 seg_i->sum_blk->entries[j] = *s;
1614 offset += SUMMARY_SIZE;
1615 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1616 SUM_FOOTER_SIZE)
1617 continue;
1618
1619 f2fs_put_page(page, 1);
1620 page = NULL;
1621
1622 page = get_meta_page(sbi, start++);
1623 kaddr = (unsigned char *)page_address(page);
1624 offset = 0;
1625 }
1626 }
1627 f2fs_put_page(page, 1);
1628 return 0;
1629}
1630
1631static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1632{
1633 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1634 struct f2fs_summary_block *sum;
1635 struct curseg_info *curseg;
1636 struct page *new;
1637 unsigned short blk_off;
1638 unsigned int segno = 0;
1639 block_t blk_addr = 0;
1640
1641 /* get segment number and block addr */
1642 if (IS_DATASEG(type)) {
1643 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1644 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1645 CURSEG_HOT_DATA]);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001646 if (__exist_node_summaries(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001647 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1648 else
1649 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1650 } else {
1651 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1652 CURSEG_HOT_NODE]);
1653 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1654 CURSEG_HOT_NODE]);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001655 if (__exist_node_summaries(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001656 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1657 type - CURSEG_HOT_NODE);
1658 else
1659 blk_addr = GET_SUM_BLOCK(sbi, segno);
1660 }
1661
1662 new = get_meta_page(sbi, blk_addr);
1663 sum = (struct f2fs_summary_block *)page_address(new);
1664
1665 if (IS_NODESEG(type)) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001666 if (__exist_node_summaries(sbi)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001667 struct f2fs_summary *ns = &sum->entries[0];
1668 int i;
1669 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1670 ns->version = 0;
1671 ns->ofs_in_node = 0;
1672 }
1673 } else {
Gu Zheng12e374b2014-03-07 18:43:36 +08001674 int err;
1675
1676 err = restore_node_summary(sbi, segno, sum);
1677 if (err) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001678 f2fs_put_page(new, 1);
Gu Zheng12e374b2014-03-07 18:43:36 +08001679 return err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001680 }
1681 }
1682 }
1683
1684 /* set uncompleted segment to curseg */
1685 curseg = CURSEG_I(sbi, type);
1686 mutex_lock(&curseg->curseg_mutex);
1687 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1688 curseg->next_segno = segno;
1689 reset_curseg(sbi, type, 0);
1690 curseg->alloc_type = ckpt->alloc_type[type];
1691 curseg->next_blkoff = blk_off;
1692 mutex_unlock(&curseg->curseg_mutex);
1693 f2fs_put_page(new, 1);
1694 return 0;
1695}
1696
1697static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1698{
1699 int type = CURSEG_HOT_DATA;
Chao Yu32c234e2014-03-17 16:36:24 +08001700 int err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001701
1702 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001703 int npages = npages_for_summary_flush(sbi, true);
1704
1705 if (npages >= 2)
1706 ra_meta_pages(sbi, start_sum_block(sbi), npages,
1707 META_CP);
1708
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001709 /* restore for compacted data summary */
1710 if (read_compacted_summaries(sbi))
1711 return -EINVAL;
1712 type = CURSEG_HOT_NODE;
1713 }
1714
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001715 if (__exist_node_summaries(sbi))
1716 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
1717 NR_CURSEG_TYPE - type, META_CP);
1718
Chao Yu32c234e2014-03-17 16:36:24 +08001719 for (; type <= CURSEG_COLD_NODE; type++) {
1720 err = read_normal_summaries(sbi, type);
1721 if (err)
1722 return err;
1723 }
1724
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001725 return 0;
1726}
1727
1728static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1729{
1730 struct page *page;
1731 unsigned char *kaddr;
1732 struct f2fs_summary *summary;
1733 struct curseg_info *seg_i;
1734 int written_size = 0;
1735 int i, j;
1736
1737 page = grab_meta_page(sbi, blkaddr++);
1738 kaddr = (unsigned char *)page_address(page);
1739
1740 /* Step 1: write nat cache */
1741 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1742 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1743 written_size += SUM_JOURNAL_SIZE;
1744
1745 /* Step 2: write sit cache */
1746 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1747 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1748 SUM_JOURNAL_SIZE);
1749 written_size += SUM_JOURNAL_SIZE;
1750
1751 /* Step 3: write summary entries */
1752 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1753 unsigned short blkoff;
1754 seg_i = CURSEG_I(sbi, i);
1755 if (sbi->ckpt->alloc_type[i] == SSR)
1756 blkoff = sbi->blocks_per_seg;
1757 else
1758 blkoff = curseg_blkoff(sbi, i);
1759
1760 for (j = 0; j < blkoff; j++) {
1761 if (!page) {
1762 page = grab_meta_page(sbi, blkaddr++);
1763 kaddr = (unsigned char *)page_address(page);
1764 written_size = 0;
1765 }
1766 summary = (struct f2fs_summary *)(kaddr + written_size);
1767 *summary = seg_i->sum_blk->entries[j];
1768 written_size += SUMMARY_SIZE;
1769
1770 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1771 SUM_FOOTER_SIZE)
1772 continue;
1773
1774 set_page_dirty(page);
1775 f2fs_put_page(page, 1);
1776 page = NULL;
1777 }
1778 }
1779 if (page) {
1780 set_page_dirty(page);
1781 f2fs_put_page(page, 1);
1782 }
1783}
1784
1785static void write_normal_summaries(struct f2fs_sb_info *sbi,
1786 block_t blkaddr, int type)
1787{
1788 int i, end;
1789 if (IS_DATASEG(type))
1790 end = type + NR_CURSEG_DATA_TYPE;
1791 else
1792 end = type + NR_CURSEG_NODE_TYPE;
1793
1794 for (i = type; i < end; i++) {
1795 struct curseg_info *sum = CURSEG_I(sbi, i);
1796 mutex_lock(&sum->curseg_mutex);
1797 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1798 mutex_unlock(&sum->curseg_mutex);
1799 }
1800}
1801
1802void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1803{
1804 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1805 write_compacted_summaries(sbi, start_blk);
1806 else
1807 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1808}
1809
1810void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1811{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001812 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001813}
1814
1815int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1816 unsigned int val, int alloc)
1817{
1818 int i;
1819
1820 if (type == NAT_JOURNAL) {
1821 for (i = 0; i < nats_in_cursum(sum); i++) {
1822 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1823 return i;
1824 }
1825 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1826 return update_nats_in_cursum(sum, 1);
1827 } else if (type == SIT_JOURNAL) {
1828 for (i = 0; i < sits_in_cursum(sum); i++)
1829 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1830 return i;
1831 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1832 return update_sits_in_cursum(sum, 1);
1833 }
1834 return -1;
1835}
1836
1837static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1838 unsigned int segno)
1839{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001840 return get_meta_page(sbi, current_sit_addr(sbi, segno));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001841}
1842
1843static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1844 unsigned int start)
1845{
1846 struct sit_info *sit_i = SIT_I(sbi);
1847 struct page *src_page, *dst_page;
1848 pgoff_t src_off, dst_off;
1849 void *src_addr, *dst_addr;
1850
1851 src_off = current_sit_addr(sbi, start);
1852 dst_off = next_sit_addr(sbi, src_off);
1853
1854 /* get current sit block page without lock */
1855 src_page = get_meta_page(sbi, src_off);
1856 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001857 f2fs_bug_on(sbi, PageDirty(src_page));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001858
1859 src_addr = page_address(src_page);
1860 dst_addr = page_address(dst_page);
1861 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1862
1863 set_page_dirty(dst_page);
1864 f2fs_put_page(src_page, 1);
1865
1866 set_to_next_sit(sit_i, start);
1867
1868 return dst_page;
1869}
1870
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001871static struct sit_entry_set *grab_sit_entry_set(void)
1872{
1873 struct sit_entry_set *ses =
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001874 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001875
1876 ses->entry_cnt = 0;
1877 INIT_LIST_HEAD(&ses->set_list);
1878 return ses;
1879}
1880
1881static void release_sit_entry_set(struct sit_entry_set *ses)
1882{
1883 list_del(&ses->set_list);
1884 kmem_cache_free(sit_entry_set_slab, ses);
1885}
1886
1887static void adjust_sit_entry_set(struct sit_entry_set *ses,
1888 struct list_head *head)
1889{
1890 struct sit_entry_set *next = ses;
1891
1892 if (list_is_last(&ses->set_list, head))
1893 return;
1894
1895 list_for_each_entry_continue(next, head, set_list)
1896 if (ses->entry_cnt <= next->entry_cnt)
1897 break;
1898
1899 list_move_tail(&ses->set_list, &next->set_list);
1900}
1901
1902static void add_sit_entry(unsigned int segno, struct list_head *head)
1903{
1904 struct sit_entry_set *ses;
1905 unsigned int start_segno = START_SEGNO(segno);
1906
1907 list_for_each_entry(ses, head, set_list) {
1908 if (ses->start_segno == start_segno) {
1909 ses->entry_cnt++;
1910 adjust_sit_entry_set(ses, head);
1911 return;
1912 }
1913 }
1914
1915 ses = grab_sit_entry_set();
1916
1917 ses->start_segno = start_segno;
1918 ses->entry_cnt++;
1919 list_add(&ses->set_list, head);
1920}
1921
1922static void add_sits_in_set(struct f2fs_sb_info *sbi)
1923{
1924 struct f2fs_sm_info *sm_info = SM_I(sbi);
1925 struct list_head *set_list = &sm_info->sit_entry_set;
1926 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
1927 unsigned int segno;
1928
1929 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
1930 add_sit_entry(segno, set_list);
1931}
1932
1933static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001934{
1935 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1936 struct f2fs_summary_block *sum = curseg->sum_blk;
1937 int i;
1938
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001939 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1940 unsigned int segno;
1941 bool dirtied;
1942
1943 segno = le32_to_cpu(segno_in_journal(sum, i));
1944 dirtied = __mark_sit_entry_dirty(sbi, segno);
1945
1946 if (!dirtied)
1947 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001948 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001949 update_sits_in_cursum(sum, -sits_in_cursum(sum));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001950}
1951
1952/*
1953 * CP calls this function, which flushes SIT entries including sit_journal,
1954 * and moves prefree segs to free segs.
1955 */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001956void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001957{
1958 struct sit_info *sit_i = SIT_I(sbi);
1959 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1960 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1961 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001962 struct sit_entry_set *ses, *tmp;
1963 struct list_head *head = &SM_I(sbi)->sit_entry_set;
1964 bool to_journal = true;
1965 struct seg_entry *se;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001966
1967 mutex_lock(&curseg->curseg_mutex);
1968 mutex_lock(&sit_i->sentry_lock);
1969
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06001970 if (!sit_i->dirty_sentries)
1971 goto out;
1972
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001973 /*
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001974 * add and account sit entries of dirty bitmap in sit entry
1975 * set temporarily
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001976 */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001977 add_sits_in_set(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001978
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001979 /*
1980 * if there are no enough space in journal to store dirty sit
1981 * entries, remove all entries from journal and add and account
1982 * them in sit entry set.
1983 */
1984 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL))
1985 remove_sits_in_journal(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001986
Jaegeuk Kime0cea842015-02-18 20:43:11 -06001987 /*
1988 * there are two steps to flush sit entries:
1989 * #1, flush sit entries to journal in current cold data summary block.
1990 * #2, flush sit entries to sit page.
1991 */
1992 list_for_each_entry_safe(ses, tmp, head, set_list) {
1993 struct page *page = NULL;
1994 struct f2fs_sit_block *raw_sit = NULL;
1995 unsigned int start_segno = ses->start_segno;
1996 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
1997 (unsigned long)MAIN_SEGS(sbi));
1998 unsigned int segno = start_segno;
Changman Leeb1a94e82013-11-15 10:42:51 +09001999
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002000 if (to_journal &&
2001 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL))
2002 to_journal = false;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002003
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002004 if (!to_journal) {
2005 page = get_next_sit_page(sbi, start_segno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002006 raw_sit = page_address(page);
2007 }
2008
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002009 /* flush dirty sit entries in region of current sit set */
2010 for_each_set_bit_from(segno, bitmap, end) {
2011 int offset, sit_offset;
2012
2013 se = get_seg_entry(sbi, segno);
2014
2015 /* add discard candidates */
2016 if (cpc->reason != CP_DISCARD) {
2017 cpc->trim_start = segno;
2018 add_discard_addrs(sbi, cpc);
2019 }
2020
2021 if (to_journal) {
2022 offset = lookup_journal_in_cursum(sum,
2023 SIT_JOURNAL, segno, 1);
2024 f2fs_bug_on(sbi, offset < 0);
2025 segno_in_journal(sum, offset) =
2026 cpu_to_le32(segno);
2027 seg_info_to_raw_sit(se,
2028 &sit_in_journal(sum, offset));
2029 } else {
2030 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
2031 seg_info_to_raw_sit(se,
2032 &raw_sit->entries[sit_offset]);
2033 }
2034
2035 __clear_bit(segno, bitmap);
2036 sit_i->dirty_sentries--;
2037 ses->entry_cnt--;
2038 }
2039
2040 if (!to_journal)
2041 f2fs_put_page(page, 1);
2042
2043 f2fs_bug_on(sbi, ses->entry_cnt);
2044 release_sit_entry_set(ses);
2045 }
2046
2047 f2fs_bug_on(sbi, !list_empty(head));
2048 f2fs_bug_on(sbi, sit_i->dirty_sentries);
2049out:
2050 if (cpc->reason == CP_DISCARD) {
2051 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
2052 add_discard_addrs(sbi, cpc);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002053 }
2054 mutex_unlock(&sit_i->sentry_lock);
2055 mutex_unlock(&curseg->curseg_mutex);
2056
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002057 set_prefree_as_free_segments(sbi);
2058}
2059
2060static int build_sit_info(struct f2fs_sb_info *sbi)
2061{
2062 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2063 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2064 struct sit_info *sit_i;
2065 unsigned int sit_segs, start;
2066 char *src_bitmap, *dst_bitmap;
2067 unsigned int bitmap_size;
2068
2069 /* allocate memory for SIT information */
2070 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
2071 if (!sit_i)
2072 return -ENOMEM;
2073
2074 SM_I(sbi)->sit_info = sit_i;
2075
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002076 sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
2077 sizeof(struct seg_entry), GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002078 if (!sit_i->sentries)
2079 return -ENOMEM;
2080
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002081 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002082 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002083 if (!sit_i->dirty_sentries_bitmap)
2084 return -ENOMEM;
2085
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002086 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002087 sit_i->sentries[start].cur_valid_map
2088 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2089 sit_i->sentries[start].ckpt_valid_map
2090 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002091 sit_i->sentries[start].discard_map
2092 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2093 if (!sit_i->sentries[start].cur_valid_map ||
2094 !sit_i->sentries[start].ckpt_valid_map ||
2095 !sit_i->sentries[start].discard_map)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002096 return -ENOMEM;
2097 }
2098
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002099 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2100 if (!sit_i->tmp_map)
2101 return -ENOMEM;
2102
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002103 if (sbi->segs_per_sec > 1) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002104 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
2105 sizeof(struct sec_entry), GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002106 if (!sit_i->sec_entries)
2107 return -ENOMEM;
2108 }
2109
2110 /* get information related with SIT */
2111 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
2112
2113 /* setup SIT bitmap from ckeckpoint pack */
2114 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
2115 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
2116
2117 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2118 if (!dst_bitmap)
2119 return -ENOMEM;
2120
2121 /* init SIT information */
2122 sit_i->s_ops = &default_salloc_ops;
2123
2124 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
2125 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
2126 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
2127 sit_i->sit_bitmap = dst_bitmap;
2128 sit_i->bitmap_size = bitmap_size;
2129 sit_i->dirty_sentries = 0;
2130 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
2131 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
2132 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
2133 mutex_init(&sit_i->sentry_lock);
2134 return 0;
2135}
2136
2137static int build_free_segmap(struct f2fs_sb_info *sbi)
2138{
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002139 struct free_segmap_info *free_i;
2140 unsigned int bitmap_size, sec_bitmap_size;
2141
2142 /* allocate memory for free segmap information */
2143 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2144 if (!free_i)
2145 return -ENOMEM;
2146
2147 SM_I(sbi)->free_info = free_i;
2148
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002149 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002150 free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002151 if (!free_i->free_segmap)
2152 return -ENOMEM;
2153
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002154 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002155 free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002156 if (!free_i->free_secmap)
2157 return -ENOMEM;
2158
2159 /* set all segments as dirty temporarily */
2160 memset(free_i->free_segmap, 0xff, bitmap_size);
2161 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2162
2163 /* init free segmap information */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002164 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002165 free_i->free_segments = 0;
2166 free_i->free_sections = 0;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002167 spin_lock_init(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002168 return 0;
2169}
2170
2171static int build_curseg(struct f2fs_sb_info *sbi)
2172{
2173 struct curseg_info *array;
2174 int i;
2175
Evan McClainf3f030d2014-07-17 21:16:35 -04002176 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002177 if (!array)
2178 return -ENOMEM;
2179
2180 SM_I(sbi)->curseg_array = array;
2181
2182 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2183 mutex_init(&array[i].curseg_mutex);
2184 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
2185 if (!array[i].sum_blk)
2186 return -ENOMEM;
2187 array[i].segno = NULL_SEGNO;
2188 array[i].next_blkoff = 0;
2189 }
2190 return restore_curseg_summaries(sbi);
2191}
2192
2193static void build_sit_entries(struct f2fs_sb_info *sbi)
2194{
2195 struct sit_info *sit_i = SIT_I(sbi);
2196 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2197 struct f2fs_summary_block *sum = curseg->sum_blk;
Changman Leeb1a94e82013-11-15 10:42:51 +09002198 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2199 unsigned int i, start, end;
2200 unsigned int readed, start_blk = 0;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002201 int nrpages = MAX_BIO_BLOCKS(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002202
Changman Leeb1a94e82013-11-15 10:42:51 +09002203 do {
Chao Yu624b14f2014-02-07 16:11:53 +08002204 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002205
Changman Leeb1a94e82013-11-15 10:42:51 +09002206 start = start_blk * sit_i->sents_per_block;
2207 end = (start_blk + readed) * sit_i->sents_per_block;
2208
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002209 for (; start < end && start < MAIN_SEGS(sbi); start++) {
Changman Leeb1a94e82013-11-15 10:42:51 +09002210 struct seg_entry *se = &sit_i->sentries[start];
2211 struct f2fs_sit_block *sit_blk;
2212 struct f2fs_sit_entry sit;
2213 struct page *page;
2214
2215 mutex_lock(&curseg->curseg_mutex);
2216 for (i = 0; i < sits_in_cursum(sum); i++) {
2217 if (le32_to_cpu(segno_in_journal(sum, i))
2218 == start) {
2219 sit = sit_in_journal(sum, i);
2220 mutex_unlock(&curseg->curseg_mutex);
2221 goto got_it;
2222 }
2223 }
2224 mutex_unlock(&curseg->curseg_mutex);
2225
2226 page = get_current_sit_page(sbi, start);
2227 sit_blk = (struct f2fs_sit_block *)page_address(page);
2228 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2229 f2fs_put_page(page, 1);
2230got_it:
2231 check_block_count(sbi, start, &sit);
2232 seg_info_from_raw_sit(se, &sit);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002233
2234 /* build discard map only one time */
2235 memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2236 sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks;
2237
Changman Leeb1a94e82013-11-15 10:42:51 +09002238 if (sbi->segs_per_sec > 1) {
2239 struct sec_entry *e = get_sec_entry(sbi, start);
2240 e->valid_blocks += se->valid_blocks;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002241 }
2242 }
Changman Leeb1a94e82013-11-15 10:42:51 +09002243 start_blk += readed;
2244 } while (start_blk < sit_blk_cnt);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002245}
2246
2247static void init_free_segmap(struct f2fs_sb_info *sbi)
2248{
2249 unsigned int start;
2250 int type;
2251
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002252 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002253 struct seg_entry *sentry = get_seg_entry(sbi, start);
2254 if (!sentry->valid_blocks)
2255 __set_free(sbi, start);
2256 }
2257
2258 /* set use the current segments */
2259 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2260 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2261 __set_test_and_inuse(sbi, curseg_t->segno);
2262 }
2263}
2264
2265static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2266{
2267 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2268 struct free_segmap_info *free_i = FREE_I(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002269 unsigned int segno = 0, offset = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002270 unsigned short valid_blocks;
2271
2272 while (1) {
2273 /* find dirty segment based on free segmap */
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002274 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2275 if (segno >= MAIN_SEGS(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002276 break;
2277 offset = segno + 1;
2278 valid_blocks = get_valid_blocks(sbi, segno, 0);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002279 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002280 continue;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002281 if (valid_blocks > sbi->blocks_per_seg) {
2282 f2fs_bug_on(sbi, 1);
2283 continue;
2284 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002285 mutex_lock(&dirty_i->seglist_lock);
2286 __locate_dirty_segment(sbi, segno, DIRTY);
2287 mutex_unlock(&dirty_i->seglist_lock);
2288 }
2289}
2290
2291static int init_victim_secmap(struct f2fs_sb_info *sbi)
2292{
2293 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002294 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002295
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002296 dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002297 if (!dirty_i->victim_secmap)
2298 return -ENOMEM;
2299 return 0;
2300}
2301
2302static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2303{
2304 struct dirty_seglist_info *dirty_i;
2305 unsigned int bitmap_size, i;
2306
2307 /* allocate memory for dirty segments list information */
2308 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2309 if (!dirty_i)
2310 return -ENOMEM;
2311
2312 SM_I(sbi)->dirty_info = dirty_i;
2313 mutex_init(&dirty_i->seglist_lock);
2314
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002315 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002316
2317 for (i = 0; i < NR_DIRTY_TYPE; i++) {
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002318 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002319 if (!dirty_i->dirty_segmap[i])
2320 return -ENOMEM;
2321 }
2322
2323 init_dirty_segmap(sbi);
2324 return init_victim_secmap(sbi);
2325}
2326
2327/*
2328 * Update min, max modified time for cost-benefit GC algorithm
2329 */
2330static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2331{
2332 struct sit_info *sit_i = SIT_I(sbi);
2333 unsigned int segno;
2334
2335 mutex_lock(&sit_i->sentry_lock);
2336
2337 sit_i->min_mtime = LLONG_MAX;
2338
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002339 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002340 unsigned int i;
2341 unsigned long long mtime = 0;
2342
2343 for (i = 0; i < sbi->segs_per_sec; i++)
2344 mtime += get_seg_entry(sbi, segno + i)->mtime;
2345
2346 mtime = div_u64(mtime, sbi->segs_per_sec);
2347
2348 if (sit_i->min_mtime > mtime)
2349 sit_i->min_mtime = mtime;
2350 }
2351 sit_i->max_mtime = get_mtime(sbi);
2352 mutex_unlock(&sit_i->sentry_lock);
2353}
2354
2355int build_segment_manager(struct f2fs_sb_info *sbi)
2356{
2357 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2358 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2359 struct f2fs_sm_info *sm_info;
2360 int err;
2361
2362 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2363 if (!sm_info)
2364 return -ENOMEM;
2365
2366 /* init sm info */
2367 sbi->sm_info = sm_info;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002368 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2369 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2370 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2371 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2372 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2373 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2374 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
Jaegeuk Kimf183b112014-03-19 14:17:21 +09002375 sm_info->rec_prefree_segments = sm_info->main_segments *
2376 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002377 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
Changman Leeb1a94e82013-11-15 10:42:51 +09002378 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002379 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
Changman Leeb1a94e82013-11-15 10:42:51 +09002380
2381 INIT_LIST_HEAD(&sm_info->discard_list);
2382 sm_info->nr_discards = 0;
2383 sm_info->max_discards = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002384
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002385 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2386
2387 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2388
Evan McClainf3f030d2014-07-17 21:16:35 -04002389 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
2390 err = create_flush_cmd_control(sbi);
2391 if (err)
2392 return err;
2393 }
2394
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002395 err = build_sit_info(sbi);
2396 if (err)
2397 return err;
2398 err = build_free_segmap(sbi);
2399 if (err)
2400 return err;
2401 err = build_curseg(sbi);
2402 if (err)
2403 return err;
2404
2405 /* reinit free segmap based on SIT */
2406 build_sit_entries(sbi);
2407
2408 init_free_segmap(sbi);
2409 err = build_dirty_segmap(sbi);
2410 if (err)
2411 return err;
2412
2413 init_min_max_mtime(sbi);
2414 return 0;
2415}
2416
2417static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2418 enum dirty_type dirty_type)
2419{
2420 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2421
2422 mutex_lock(&dirty_i->seglist_lock);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002423 kvfree(dirty_i->dirty_segmap[dirty_type]);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002424 dirty_i->nr_dirty[dirty_type] = 0;
2425 mutex_unlock(&dirty_i->seglist_lock);
2426}
2427
2428static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
2429{
2430 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002431 kvfree(dirty_i->victim_secmap);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002432}
2433
2434static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2435{
2436 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2437 int i;
2438
2439 if (!dirty_i)
2440 return;
2441
2442 /* discard pre-free/dirty segments list */
2443 for (i = 0; i < NR_DIRTY_TYPE; i++)
2444 discard_dirty_segmap(sbi, i);
2445
2446 destroy_victim_secmap(sbi);
2447 SM_I(sbi)->dirty_info = NULL;
2448 kfree(dirty_i);
2449}
2450
2451static void destroy_curseg(struct f2fs_sb_info *sbi)
2452{
2453 struct curseg_info *array = SM_I(sbi)->curseg_array;
2454 int i;
2455
2456 if (!array)
2457 return;
2458 SM_I(sbi)->curseg_array = NULL;
2459 for (i = 0; i < NR_CURSEG_TYPE; i++)
2460 kfree(array[i].sum_blk);
2461 kfree(array);
2462}
2463
2464static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2465{
2466 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2467 if (!free_i)
2468 return;
2469 SM_I(sbi)->free_info = NULL;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002470 kvfree(free_i->free_segmap);
2471 kvfree(free_i->free_secmap);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002472 kfree(free_i);
2473}
2474
2475static void destroy_sit_info(struct f2fs_sb_info *sbi)
2476{
2477 struct sit_info *sit_i = SIT_I(sbi);
2478 unsigned int start;
2479
2480 if (!sit_i)
2481 return;
2482
2483 if (sit_i->sentries) {
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002484 for (start = 0; start < MAIN_SEGS(sbi); start++) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002485 kfree(sit_i->sentries[start].cur_valid_map);
2486 kfree(sit_i->sentries[start].ckpt_valid_map);
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002487 kfree(sit_i->sentries[start].discard_map);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002488 }
2489 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002490 kfree(sit_i->tmp_map);
2491
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -06002492 kvfree(sit_i->sentries);
2493 kvfree(sit_i->sec_entries);
2494 kvfree(sit_i->dirty_sentries_bitmap);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002495
2496 SM_I(sbi)->sit_info = NULL;
2497 kfree(sit_i->sit_bitmap);
2498 kfree(sit_i);
2499}
2500
2501void destroy_segment_manager(struct f2fs_sb_info *sbi)
2502{
2503 struct f2fs_sm_info *sm_info = SM_I(sbi);
Evan McClainf3f030d2014-07-17 21:16:35 -04002504
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002505 if (!sm_info)
2506 return;
Evan McClainf3f030d2014-07-17 21:16:35 -04002507 destroy_flush_cmd_control(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08002508 destroy_dirty_segmap(sbi);
2509 destroy_curseg(sbi);
2510 destroy_free_segmap(sbi);
2511 destroy_sit_info(sbi);
2512 sbi->sm_info = NULL;
2513 kfree(sm_info);
2514}
Changman Leeb1a94e82013-11-15 10:42:51 +09002515
2516int __init create_segment_manager_caches(void)
2517{
2518 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
Gu Zhenge33dcea2014-03-07 18:43:28 +08002519 sizeof(struct discard_entry));
Changman Leeb1a94e82013-11-15 10:42:51 +09002520 if (!discard_entry_slab)
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002521 goto fail;
2522
2523 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
2524 sizeof(struct sit_entry_set));
2525 if (!sit_entry_set_slab)
2526 goto destory_discard_entry;
2527
2528 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2529 sizeof(struct inmem_pages));
2530 if (!inmem_entry_slab)
2531 goto destroy_sit_entry_set;
Changman Leeb1a94e82013-11-15 10:42:51 +09002532 return 0;
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002533
2534destroy_sit_entry_set:
2535 kmem_cache_destroy(sit_entry_set_slab);
2536destory_discard_entry:
2537 kmem_cache_destroy(discard_entry_slab);
2538fail:
2539 return -ENOMEM;
Changman Leeb1a94e82013-11-15 10:42:51 +09002540}
2541
2542void destroy_segment_manager_caches(void)
2543{
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002544 kmem_cache_destroy(sit_entry_set_slab);
Changman Leeb1a94e82013-11-15 10:42:51 +09002545 kmem_cache_destroy(discard_entry_slab);
Jaegeuk Kime0cea842015-02-18 20:43:11 -06002546 kmem_cache_destroy(inmem_entry_slab);
Changman Leeb1a94e82013-11-15 10:42:51 +09002547}