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