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