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