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