Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com |
| 3 | * Written by Alex Tomas <alex@clusterfs.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public Licens |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | /* |
| 21 | * mballoc.c contains the multiblocks allocation routines |
| 22 | */ |
| 23 | |
Mingming Cao | 8f6e39a | 2008-04-29 22:01:31 -0400 | [diff] [blame] | 24 | #include "mballoc.h" |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 25 | #include <linux/debugfs.h> |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 26 | #include <trace/events/ext4.h> |
| 27 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 28 | /* |
| 29 | * MUSTDO: |
| 30 | * - test ext4_ext_search_left() and ext4_ext_search_right() |
| 31 | * - search for metadata in few groups |
| 32 | * |
| 33 | * TODO v4: |
| 34 | * - normalization should take into account whether file is still open |
| 35 | * - discard preallocations if no free space left (policy?) |
| 36 | * - don't normalize tails |
| 37 | * - quota |
| 38 | * - reservation for superuser |
| 39 | * |
| 40 | * TODO v3: |
| 41 | * - bitmap read-ahead (proposed by Oleg Drokin aka green) |
| 42 | * - track min/max extents in each group for better group selection |
| 43 | * - mb_mark_used() may allocate chunk right after splitting buddy |
| 44 | * - tree of groups sorted by number of free blocks |
| 45 | * - error handling |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * The allocation request involve request for multiple number of blocks |
| 50 | * near to the goal(block) value specified. |
| 51 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 52 | * During initialization phase of the allocator we decide to use the |
| 53 | * group preallocation or inode preallocation depending on the size of |
| 54 | * the file. The size of the file could be the resulting file size we |
| 55 | * would have after allocation, or the current file size, which ever |
| 56 | * is larger. If the size is less than sbi->s_mb_stream_request we |
| 57 | * select to use the group preallocation. The default value of |
| 58 | * s_mb_stream_request is 16 blocks. This can also be tuned via |
| 59 | * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in |
| 60 | * terms of number of blocks. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 61 | * |
| 62 | * The main motivation for having small file use group preallocation is to |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 63 | * ensure that we have small files closer together on the disk. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 64 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 65 | * First stage the allocator looks at the inode prealloc list, |
| 66 | * ext4_inode_info->i_prealloc_list, which contains list of prealloc |
| 67 | * spaces for this particular inode. The inode prealloc space is |
| 68 | * represented as: |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 69 | * |
| 70 | * pa_lstart -> the logical start block for this prealloc space |
| 71 | * pa_pstart -> the physical start block for this prealloc space |
| 72 | * pa_len -> lenght for this prealloc space |
| 73 | * pa_free -> free space available in this prealloc space |
| 74 | * |
| 75 | * The inode preallocation space is used looking at the _logical_ start |
| 76 | * block. If only the logical file block falls within the range of prealloc |
| 77 | * space we will consume the particular prealloc space. This make sure that |
| 78 | * that the we have contiguous physical blocks representing the file blocks |
| 79 | * |
| 80 | * The important thing to be noted in case of inode prealloc space is that |
| 81 | * we don't modify the values associated to inode prealloc space except |
| 82 | * pa_free. |
| 83 | * |
| 84 | * If we are not able to find blocks in the inode prealloc space and if we |
| 85 | * have the group allocation flag set then we look at the locality group |
| 86 | * prealloc space. These are per CPU prealloc list repreasented as |
| 87 | * |
| 88 | * ext4_sb_info.s_locality_groups[smp_processor_id()] |
| 89 | * |
| 90 | * The reason for having a per cpu locality group is to reduce the contention |
| 91 | * between CPUs. It is possible to get scheduled at this point. |
| 92 | * |
| 93 | * The locality group prealloc space is used looking at whether we have |
| 94 | * enough free space (pa_free) withing the prealloc space. |
| 95 | * |
| 96 | * If we can't allocate blocks via inode prealloc or/and locality group |
| 97 | * prealloc then we look at the buddy cache. The buddy cache is represented |
| 98 | * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets |
| 99 | * mapped to the buddy and bitmap information regarding different |
| 100 | * groups. The buddy information is attached to buddy cache inode so that |
| 101 | * we can access them through the page cache. The information regarding |
| 102 | * each group is loaded via ext4_mb_load_buddy. The information involve |
| 103 | * block bitmap and buddy information. The information are stored in the |
| 104 | * inode as: |
| 105 | * |
| 106 | * { page } |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 107 | * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 108 | * |
| 109 | * |
| 110 | * one block each for bitmap and buddy information. So for each group we |
| 111 | * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE / |
| 112 | * blocksize) blocks. So it can have information regarding groups_per_page |
| 113 | * which is blocks_per_page/2 |
| 114 | * |
| 115 | * The buddy cache inode is not stored on disk. The inode is thrown |
| 116 | * away when the filesystem is unmounted. |
| 117 | * |
| 118 | * We look for count number of blocks in the buddy cache. If we were able |
| 119 | * to locate that many free blocks we return with additional information |
| 120 | * regarding rest of the contiguous physical block available |
| 121 | * |
| 122 | * Before allocating blocks via buddy cache we normalize the request |
| 123 | * blocks. This ensure we ask for more blocks that we needed. The extra |
| 124 | * blocks that we get after allocation is added to the respective prealloc |
| 125 | * list. In case of inode preallocation we follow a list of heuristics |
| 126 | * based on file size. This can be found in ext4_mb_normalize_request. If |
| 127 | * we are doing a group prealloc we try to normalize the request to |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 128 | * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 129 | * 512 blocks. This can be tuned via |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 130 | * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 131 | * terms of number of blocks. If we have mounted the file system with -O |
| 132 | * stripe=<value> option the group prealloc request is normalized to the |
| 133 | * stripe value (sbi->s_stripe) |
| 134 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 135 | * The regular allocator(using the buddy cache) supports few tunables. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 136 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 137 | * /sys/fs/ext4/<partition>/mb_min_to_scan |
| 138 | * /sys/fs/ext4/<partition>/mb_max_to_scan |
| 139 | * /sys/fs/ext4/<partition>/mb_order2_req |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 140 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 141 | * The regular allocator uses buddy scan only if the request len is power of |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 142 | * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The |
| 143 | * value of s_mb_order2_reqs can be tuned via |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 144 | * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 145 | * stripe size (sbi->s_stripe), we try to search for contigous block in |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 146 | * stripe size. This should result in better allocation on RAID setups. If |
| 147 | * not, we search in the specific group using bitmap for best extents. The |
| 148 | * tunable min_to_scan and max_to_scan control the behaviour here. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 149 | * min_to_scan indicate how long the mballoc __must__ look for a best |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 150 | * extent and max_to_scan indicates how long the mballoc __can__ look for a |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 151 | * best extent in the found extents. Searching for the blocks starts with |
| 152 | * the group specified as the goal value in allocation context via |
| 153 | * ac_g_ex. Each group is first checked based on the criteria whether it |
| 154 | * can used for allocation. ext4_mb_good_group explains how the groups are |
| 155 | * checked. |
| 156 | * |
| 157 | * Both the prealloc space are getting populated as above. So for the first |
| 158 | * request we will hit the buddy cache which will result in this prealloc |
| 159 | * space getting filled. The prealloc space is then later used for the |
| 160 | * subsequent request. |
| 161 | */ |
| 162 | |
| 163 | /* |
| 164 | * mballoc operates on the following data: |
| 165 | * - on-disk bitmap |
| 166 | * - in-core buddy (actually includes buddy and bitmap) |
| 167 | * - preallocation descriptors (PAs) |
| 168 | * |
| 169 | * there are two types of preallocations: |
| 170 | * - inode |
| 171 | * assiged to specific inode and can be used for this inode only. |
| 172 | * it describes part of inode's space preallocated to specific |
| 173 | * physical blocks. any block from that preallocated can be used |
| 174 | * independent. the descriptor just tracks number of blocks left |
| 175 | * unused. so, before taking some block from descriptor, one must |
| 176 | * make sure corresponded logical block isn't allocated yet. this |
| 177 | * also means that freeing any block within descriptor's range |
| 178 | * must discard all preallocated blocks. |
| 179 | * - locality group |
| 180 | * assigned to specific locality group which does not translate to |
| 181 | * permanent set of inodes: inode can join and leave group. space |
| 182 | * from this type of preallocation can be used for any inode. thus |
| 183 | * it's consumed from the beginning to the end. |
| 184 | * |
| 185 | * relation between them can be expressed as: |
| 186 | * in-core buddy = on-disk bitmap + preallocation descriptors |
| 187 | * |
| 188 | * this mean blocks mballoc considers used are: |
| 189 | * - allocated blocks (persistent) |
| 190 | * - preallocated blocks (non-persistent) |
| 191 | * |
| 192 | * consistency in mballoc world means that at any time a block is either |
| 193 | * free or used in ALL structures. notice: "any time" should not be read |
| 194 | * literally -- time is discrete and delimited by locks. |
| 195 | * |
| 196 | * to keep it simple, we don't use block numbers, instead we count number of |
| 197 | * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA. |
| 198 | * |
| 199 | * all operations can be expressed as: |
| 200 | * - init buddy: buddy = on-disk + PAs |
| 201 | * - new PA: buddy += N; PA = N |
| 202 | * - use inode PA: on-disk += N; PA -= N |
| 203 | * - discard inode PA buddy -= on-disk - PA; PA = 0 |
| 204 | * - use locality group PA on-disk += N; PA -= N |
| 205 | * - discard locality group PA buddy -= PA; PA = 0 |
| 206 | * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap |
| 207 | * is used in real operation because we can't know actual used |
| 208 | * bits from PA, only from on-disk bitmap |
| 209 | * |
| 210 | * if we follow this strict logic, then all operations above should be atomic. |
| 211 | * given some of them can block, we'd have to use something like semaphores |
| 212 | * killing performance on high-end SMP hardware. let's try to relax it using |
| 213 | * the following knowledge: |
| 214 | * 1) if buddy is referenced, it's already initialized |
| 215 | * 2) while block is used in buddy and the buddy is referenced, |
| 216 | * nobody can re-allocate that block |
| 217 | * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has |
| 218 | * bit set and PA claims same block, it's OK. IOW, one can set bit in |
| 219 | * on-disk bitmap if buddy has same bit set or/and PA covers corresponded |
| 220 | * block |
| 221 | * |
| 222 | * so, now we're building a concurrency table: |
| 223 | * - init buddy vs. |
| 224 | * - new PA |
| 225 | * blocks for PA are allocated in the buddy, buddy must be referenced |
| 226 | * until PA is linked to allocation group to avoid concurrent buddy init |
| 227 | * - use inode PA |
| 228 | * we need to make sure that either on-disk bitmap or PA has uptodate data |
| 229 | * given (3) we care that PA-=N operation doesn't interfere with init |
| 230 | * - discard inode PA |
| 231 | * the simplest way would be to have buddy initialized by the discard |
| 232 | * - use locality group PA |
| 233 | * again PA-=N must be serialized with init |
| 234 | * - discard locality group PA |
| 235 | * the simplest way would be to have buddy initialized by the discard |
| 236 | * - new PA vs. |
| 237 | * - use inode PA |
| 238 | * i_data_sem serializes them |
| 239 | * - discard inode PA |
| 240 | * discard process must wait until PA isn't used by another process |
| 241 | * - use locality group PA |
| 242 | * some mutex should serialize them |
| 243 | * - discard locality group PA |
| 244 | * discard process must wait until PA isn't used by another process |
| 245 | * - use inode PA |
| 246 | * - use inode PA |
| 247 | * i_data_sem or another mutex should serializes them |
| 248 | * - discard inode PA |
| 249 | * discard process must wait until PA isn't used by another process |
| 250 | * - use locality group PA |
| 251 | * nothing wrong here -- they're different PAs covering different blocks |
| 252 | * - discard locality group PA |
| 253 | * discard process must wait until PA isn't used by another process |
| 254 | * |
| 255 | * now we're ready to make few consequences: |
| 256 | * - PA is referenced and while it is no discard is possible |
| 257 | * - PA is referenced until block isn't marked in on-disk bitmap |
| 258 | * - PA changes only after on-disk bitmap |
| 259 | * - discard must not compete with init. either init is done before |
| 260 | * any discard or they're serialized somehow |
| 261 | * - buddy init as sum of on-disk bitmap and PAs is done atomically |
| 262 | * |
| 263 | * a special case when we've used PA to emptiness. no need to modify buddy |
| 264 | * in this case, but we should care about concurrent init |
| 265 | * |
| 266 | */ |
| 267 | |
| 268 | /* |
| 269 | * Logic in few words: |
| 270 | * |
| 271 | * - allocation: |
| 272 | * load group |
| 273 | * find blocks |
| 274 | * mark bits in on-disk bitmap |
| 275 | * release group |
| 276 | * |
| 277 | * - use preallocation: |
| 278 | * find proper PA (per-inode or group) |
| 279 | * load group |
| 280 | * mark bits in on-disk bitmap |
| 281 | * release group |
| 282 | * release PA |
| 283 | * |
| 284 | * - free: |
| 285 | * load group |
| 286 | * mark bits in on-disk bitmap |
| 287 | * release group |
| 288 | * |
| 289 | * - discard preallocations in group: |
| 290 | * mark PAs deleted |
| 291 | * move them onto local list |
| 292 | * load on-disk bitmap |
| 293 | * load group |
| 294 | * remove PA from object (inode or locality group) |
| 295 | * mark free blocks in-core |
| 296 | * |
| 297 | * - discard inode's preallocations: |
| 298 | */ |
| 299 | |
| 300 | /* |
| 301 | * Locking rules |
| 302 | * |
| 303 | * Locks: |
| 304 | * - bitlock on a group (group) |
| 305 | * - object (inode/locality) (object) |
| 306 | * - per-pa lock (pa) |
| 307 | * |
| 308 | * Paths: |
| 309 | * - new pa |
| 310 | * object |
| 311 | * group |
| 312 | * |
| 313 | * - find and use pa: |
| 314 | * pa |
| 315 | * |
| 316 | * - release consumed pa: |
| 317 | * pa |
| 318 | * group |
| 319 | * object |
| 320 | * |
| 321 | * - generate in-core bitmap: |
| 322 | * group |
| 323 | * pa |
| 324 | * |
| 325 | * - discard all for given object (inode, locality group): |
| 326 | * object |
| 327 | * pa |
| 328 | * group |
| 329 | * |
| 330 | * - discard all for given group: |
| 331 | * group |
| 332 | * pa |
| 333 | * group |
| 334 | * object |
| 335 | * |
| 336 | */ |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 337 | static struct kmem_cache *ext4_pspace_cachep; |
| 338 | static struct kmem_cache *ext4_ac_cachep; |
| 339 | static struct kmem_cache *ext4_free_ext_cachep; |
| 340 | static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
| 341 | ext4_group_t group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 342 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, |
| 343 | ext4_group_t group); |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 344 | static void release_blocks_on_commit(journal_t *journal, transaction_t *txn); |
| 345 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 346 | static inline void *mb_correct_addr_and_bit(int *bit, void *addr) |
| 347 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 348 | #if BITS_PER_LONG == 64 |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 349 | *bit += ((unsigned long) addr & 7UL) << 3; |
| 350 | addr = (void *) ((unsigned long) addr & ~7UL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 351 | #elif BITS_PER_LONG == 32 |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 352 | *bit += ((unsigned long) addr & 3UL) << 3; |
| 353 | addr = (void *) ((unsigned long) addr & ~3UL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 354 | #else |
| 355 | #error "how many bits you are?!" |
| 356 | #endif |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 357 | return addr; |
| 358 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 359 | |
| 360 | static inline int mb_test_bit(int bit, void *addr) |
| 361 | { |
| 362 | /* |
| 363 | * ext4_test_bit on architecture like powerpc |
| 364 | * needs unsigned long aligned address |
| 365 | */ |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 366 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 367 | return ext4_test_bit(bit, addr); |
| 368 | } |
| 369 | |
| 370 | static inline void mb_set_bit(int bit, void *addr) |
| 371 | { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 372 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 373 | ext4_set_bit(bit, addr); |
| 374 | } |
| 375 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 376 | static inline void mb_clear_bit(int bit, void *addr) |
| 377 | { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 378 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 379 | ext4_clear_bit(bit, addr); |
| 380 | } |
| 381 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 382 | static inline int mb_find_next_zero_bit(void *addr, int max, int start) |
| 383 | { |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 384 | int fix = 0, ret, tmpmax; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 385 | addr = mb_correct_addr_and_bit(&fix, addr); |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 386 | tmpmax = max + fix; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 387 | start += fix; |
| 388 | |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 389 | ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix; |
| 390 | if (ret > max) |
| 391 | return max; |
| 392 | return ret; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static inline int mb_find_next_bit(void *addr, int max, int start) |
| 396 | { |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 397 | int fix = 0, ret, tmpmax; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 398 | addr = mb_correct_addr_and_bit(&fix, addr); |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 399 | tmpmax = max + fix; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 400 | start += fix; |
| 401 | |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 402 | ret = ext4_find_next_bit(addr, tmpmax, start) - fix; |
| 403 | if (ret > max) |
| 404 | return max; |
| 405 | return ret; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 408 | static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max) |
| 409 | { |
| 410 | char *bb; |
| 411 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 412 | BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b)); |
| 413 | BUG_ON(max == NULL); |
| 414 | |
| 415 | if (order > e4b->bd_blkbits + 1) { |
| 416 | *max = 0; |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | /* at order 0 we see each particular block */ |
| 421 | *max = 1 << (e4b->bd_blkbits + 3); |
| 422 | if (order == 0) |
| 423 | return EXT4_MB_BITMAP(e4b); |
| 424 | |
| 425 | bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order]; |
| 426 | *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order]; |
| 427 | |
| 428 | return bb; |
| 429 | } |
| 430 | |
| 431 | #ifdef DOUBLE_CHECK |
| 432 | static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b, |
| 433 | int first, int count) |
| 434 | { |
| 435 | int i; |
| 436 | struct super_block *sb = e4b->bd_sb; |
| 437 | |
| 438 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) |
| 439 | return; |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 440 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 441 | for (i = 0; i < count; i++) { |
| 442 | if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) { |
| 443 | ext4_fsblk_t blocknr; |
| 444 | blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb); |
| 445 | blocknr += first + i; |
| 446 | blocknr += |
| 447 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 448 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 449 | __func__, "double-free of inode" |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 450 | " %lu's block %llu(bit %u in group %u)", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 451 | inode ? inode->i_ino : 0, blocknr, |
| 452 | first + i, e4b->bd_group); |
| 453 | } |
| 454 | mb_clear_bit(first + i, e4b->bd_info->bb_bitmap); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count) |
| 459 | { |
| 460 | int i; |
| 461 | |
| 462 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) |
| 463 | return; |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 464 | assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 465 | for (i = 0; i < count; i++) { |
| 466 | BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap)); |
| 467 | mb_set_bit(first + i, e4b->bd_info->bb_bitmap); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) |
| 472 | { |
| 473 | if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) { |
| 474 | unsigned char *b1, *b2; |
| 475 | int i; |
| 476 | b1 = (unsigned char *) e4b->bd_info->bb_bitmap; |
| 477 | b2 = (unsigned char *) bitmap; |
| 478 | for (i = 0; i < e4b->bd_sb->s_blocksize; i++) { |
| 479 | if (b1[i] != b2[i]) { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 480 | printk(KERN_ERR "corruption in group %u " |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 481 | "at byte %u(%u): %x in copy != %x " |
| 482 | "on disk/prealloc\n", |
| 483 | e4b->bd_group, i, i * 8, b1[i], b2[i]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 484 | BUG(); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | #else |
| 491 | static inline void mb_free_blocks_double(struct inode *inode, |
| 492 | struct ext4_buddy *e4b, int first, int count) |
| 493 | { |
| 494 | return; |
| 495 | } |
| 496 | static inline void mb_mark_used_double(struct ext4_buddy *e4b, |
| 497 | int first, int count) |
| 498 | { |
| 499 | return; |
| 500 | } |
| 501 | static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) |
| 502 | { |
| 503 | return; |
| 504 | } |
| 505 | #endif |
| 506 | |
| 507 | #ifdef AGGRESSIVE_CHECK |
| 508 | |
| 509 | #define MB_CHECK_ASSERT(assert) \ |
| 510 | do { \ |
| 511 | if (!(assert)) { \ |
| 512 | printk(KERN_EMERG \ |
| 513 | "Assertion failure in %s() at %s:%d: \"%s\"\n", \ |
| 514 | function, file, line, # assert); \ |
| 515 | BUG(); \ |
| 516 | } \ |
| 517 | } while (0) |
| 518 | |
| 519 | static int __mb_check_buddy(struct ext4_buddy *e4b, char *file, |
| 520 | const char *function, int line) |
| 521 | { |
| 522 | struct super_block *sb = e4b->bd_sb; |
| 523 | int order = e4b->bd_blkbits + 1; |
| 524 | int max; |
| 525 | int max2; |
| 526 | int i; |
| 527 | int j; |
| 528 | int k; |
| 529 | int count; |
| 530 | struct ext4_group_info *grp; |
| 531 | int fragments = 0; |
| 532 | int fstart; |
| 533 | struct list_head *cur; |
| 534 | void *buddy; |
| 535 | void *buddy2; |
| 536 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 537 | { |
| 538 | static int mb_check_counter; |
| 539 | if (mb_check_counter++ % 100 != 0) |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | while (order > 1) { |
| 544 | buddy = mb_find_buddy(e4b, order, &max); |
| 545 | MB_CHECK_ASSERT(buddy); |
| 546 | buddy2 = mb_find_buddy(e4b, order - 1, &max2); |
| 547 | MB_CHECK_ASSERT(buddy2); |
| 548 | MB_CHECK_ASSERT(buddy != buddy2); |
| 549 | MB_CHECK_ASSERT(max * 2 == max2); |
| 550 | |
| 551 | count = 0; |
| 552 | for (i = 0; i < max; i++) { |
| 553 | |
| 554 | if (mb_test_bit(i, buddy)) { |
| 555 | /* only single bit in buddy2 may be 1 */ |
| 556 | if (!mb_test_bit(i << 1, buddy2)) { |
| 557 | MB_CHECK_ASSERT( |
| 558 | mb_test_bit((i<<1)+1, buddy2)); |
| 559 | } else if (!mb_test_bit((i << 1) + 1, buddy2)) { |
| 560 | MB_CHECK_ASSERT( |
| 561 | mb_test_bit(i << 1, buddy2)); |
| 562 | } |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | /* both bits in buddy2 must be 0 */ |
| 567 | MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2)); |
| 568 | MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2)); |
| 569 | |
| 570 | for (j = 0; j < (1 << order); j++) { |
| 571 | k = (i * (1 << order)) + j; |
| 572 | MB_CHECK_ASSERT( |
| 573 | !mb_test_bit(k, EXT4_MB_BITMAP(e4b))); |
| 574 | } |
| 575 | count++; |
| 576 | } |
| 577 | MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count); |
| 578 | order--; |
| 579 | } |
| 580 | |
| 581 | fstart = -1; |
| 582 | buddy = mb_find_buddy(e4b, 0, &max); |
| 583 | for (i = 0; i < max; i++) { |
| 584 | if (!mb_test_bit(i, buddy)) { |
| 585 | MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free); |
| 586 | if (fstart == -1) { |
| 587 | fragments++; |
| 588 | fstart = i; |
| 589 | } |
| 590 | continue; |
| 591 | } |
| 592 | fstart = -1; |
| 593 | /* check used bits only */ |
| 594 | for (j = 0; j < e4b->bd_blkbits + 1; j++) { |
| 595 | buddy2 = mb_find_buddy(e4b, j, &max2); |
| 596 | k = i >> j; |
| 597 | MB_CHECK_ASSERT(k < max2); |
| 598 | MB_CHECK_ASSERT(mb_test_bit(k, buddy2)); |
| 599 | } |
| 600 | } |
| 601 | MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info)); |
| 602 | MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments); |
| 603 | |
| 604 | grp = ext4_get_group_info(sb, e4b->bd_group); |
| 605 | buddy = mb_find_buddy(e4b, 0, &max); |
| 606 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 607 | ext4_group_t groupnr; |
| 608 | struct ext4_prealloc_space *pa; |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 609 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 610 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 611 | MB_CHECK_ASSERT(groupnr == e4b->bd_group); |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 612 | for (i = 0; i < pa->pa_len; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 613 | MB_CHECK_ASSERT(mb_test_bit(k + i, buddy)); |
| 614 | } |
| 615 | return 0; |
| 616 | } |
| 617 | #undef MB_CHECK_ASSERT |
| 618 | #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \ |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 619 | __FILE__, __func__, __LINE__) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 620 | #else |
| 621 | #define mb_check_buddy(e4b) |
| 622 | #endif |
| 623 | |
| 624 | /* FIXME!! need more doc */ |
| 625 | static void ext4_mb_mark_free_simple(struct super_block *sb, |
| 626 | void *buddy, unsigned first, int len, |
| 627 | struct ext4_group_info *grp) |
| 628 | { |
| 629 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 630 | unsigned short min; |
| 631 | unsigned short max; |
| 632 | unsigned short chunk; |
| 633 | unsigned short border; |
| 634 | |
Valerie Clement | b73fce6 | 2008-02-15 13:48:51 -0500 | [diff] [blame] | 635 | BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 636 | |
| 637 | border = 2 << sb->s_blocksize_bits; |
| 638 | |
| 639 | while (len > 0) { |
| 640 | /* find how many blocks can be covered since this position */ |
| 641 | max = ffs(first | border) - 1; |
| 642 | |
| 643 | /* find how many blocks of power 2 we need to mark */ |
| 644 | min = fls(len) - 1; |
| 645 | |
| 646 | if (max < min) |
| 647 | min = max; |
| 648 | chunk = 1 << min; |
| 649 | |
| 650 | /* mark multiblock chunks only */ |
| 651 | grp->bb_counters[min]++; |
| 652 | if (min > 0) |
| 653 | mb_clear_bit(first >> min, |
| 654 | buddy + sbi->s_mb_offsets[min]); |
| 655 | |
| 656 | len -= chunk; |
| 657 | first += chunk; |
| 658 | } |
| 659 | } |
| 660 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 661 | static noinline_for_stack |
| 662 | void ext4_mb_generate_buddy(struct super_block *sb, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 663 | void *buddy, void *bitmap, ext4_group_t group) |
| 664 | { |
| 665 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 666 | unsigned short max = EXT4_BLOCKS_PER_GROUP(sb); |
| 667 | unsigned short i = 0; |
| 668 | unsigned short first; |
| 669 | unsigned short len; |
| 670 | unsigned free = 0; |
| 671 | unsigned fragments = 0; |
| 672 | unsigned long long period = get_cycles(); |
| 673 | |
| 674 | /* initialize buddy from bitmap which is aggregation |
| 675 | * of on-disk bitmap and preallocations */ |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 676 | i = mb_find_next_zero_bit(bitmap, max, 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 677 | grp->bb_first_free = i; |
| 678 | while (i < max) { |
| 679 | fragments++; |
| 680 | first = i; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 681 | i = mb_find_next_bit(bitmap, max, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 682 | len = i - first; |
| 683 | free += len; |
| 684 | if (len > 1) |
| 685 | ext4_mb_mark_free_simple(sb, buddy, first, len, grp); |
| 686 | else |
| 687 | grp->bb_counters[0]++; |
| 688 | if (i < max) |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 689 | i = mb_find_next_zero_bit(bitmap, max, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 690 | } |
| 691 | grp->bb_fragments = fragments; |
| 692 | |
| 693 | if (free != grp->bb_free) { |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 694 | ext4_grp_locked_error(sb, group, __func__, |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 695 | "EXT4-fs: group %u: %u blocks in bitmap, %u in gd", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 696 | group, free, grp->bb_free); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 697 | /* |
| 698 | * If we intent to continue, we consider group descritor |
| 699 | * corrupt and update bb_free using bitmap value |
| 700 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 701 | grp->bb_free = free; |
| 702 | } |
| 703 | |
| 704 | clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state)); |
| 705 | |
| 706 | period = get_cycles() - period; |
| 707 | spin_lock(&EXT4_SB(sb)->s_bal_lock); |
| 708 | EXT4_SB(sb)->s_mb_buddies_generated++; |
| 709 | EXT4_SB(sb)->s_mb_generation_time += period; |
| 710 | spin_unlock(&EXT4_SB(sb)->s_bal_lock); |
| 711 | } |
| 712 | |
| 713 | /* The buddy information is attached the buddy cache inode |
| 714 | * for convenience. The information regarding each group |
| 715 | * is loaded via ext4_mb_load_buddy. The information involve |
| 716 | * block bitmap and buddy information. The information are |
| 717 | * stored in the inode as |
| 718 | * |
| 719 | * { page } |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 720 | * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 721 | * |
| 722 | * |
| 723 | * one block each for bitmap and buddy information. |
| 724 | * So for each group we take up 2 blocks. A page can |
| 725 | * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks. |
| 726 | * So it can have information regarding groups_per_page which |
| 727 | * is blocks_per_page/2 |
| 728 | */ |
| 729 | |
| 730 | static int ext4_mb_init_cache(struct page *page, char *incore) |
| 731 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 732 | ext4_group_t ngroups; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 733 | int blocksize; |
| 734 | int blocks_per_page; |
| 735 | int groups_per_page; |
| 736 | int err = 0; |
| 737 | int i; |
| 738 | ext4_group_t first_group; |
| 739 | int first_block; |
| 740 | struct super_block *sb; |
| 741 | struct buffer_head *bhs; |
| 742 | struct buffer_head **bh; |
| 743 | struct inode *inode; |
| 744 | char *data; |
| 745 | char *bitmap; |
| 746 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 747 | mb_debug(1, "init page %lu\n", page->index); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 748 | |
| 749 | inode = page->mapping->host; |
| 750 | sb = inode->i_sb; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 751 | ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 752 | blocksize = 1 << inode->i_blkbits; |
| 753 | blocks_per_page = PAGE_CACHE_SIZE / blocksize; |
| 754 | |
| 755 | groups_per_page = blocks_per_page >> 1; |
| 756 | if (groups_per_page == 0) |
| 757 | groups_per_page = 1; |
| 758 | |
| 759 | /* allocate buffer_heads to read bitmaps */ |
| 760 | if (groups_per_page > 1) { |
| 761 | err = -ENOMEM; |
| 762 | i = sizeof(struct buffer_head *) * groups_per_page; |
| 763 | bh = kzalloc(i, GFP_NOFS); |
| 764 | if (bh == NULL) |
| 765 | goto out; |
| 766 | } else |
| 767 | bh = &bhs; |
| 768 | |
| 769 | first_group = page->index * blocks_per_page / 2; |
| 770 | |
| 771 | /* read all groups the page covers into the cache */ |
| 772 | for (i = 0; i < groups_per_page; i++) { |
| 773 | struct ext4_group_desc *desc; |
| 774 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 775 | if (first_group + i >= ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 776 | break; |
| 777 | |
| 778 | err = -EIO; |
| 779 | desc = ext4_get_group_desc(sb, first_group + i, NULL); |
| 780 | if (desc == NULL) |
| 781 | goto out; |
| 782 | |
| 783 | err = -ENOMEM; |
| 784 | bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc)); |
| 785 | if (bh[i] == NULL) |
| 786 | goto out; |
| 787 | |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 788 | if (bitmap_uptodate(bh[i])) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 789 | continue; |
| 790 | |
Frederic Bohe | c806e68 | 2008-10-10 08:09:18 -0400 | [diff] [blame] | 791 | lock_buffer(bh[i]); |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 792 | if (bitmap_uptodate(bh[i])) { |
| 793 | unlock_buffer(bh[i]); |
| 794 | continue; |
| 795 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 796 | ext4_lock_group(sb, first_group + i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 797 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 798 | ext4_init_block_bitmap(sb, bh[i], |
| 799 | first_group + i, desc); |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 800 | set_bitmap_uptodate(bh[i]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 801 | set_buffer_uptodate(bh[i]); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 802 | ext4_unlock_group(sb, first_group + i); |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 803 | unlock_buffer(bh[i]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 804 | continue; |
| 805 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 806 | ext4_unlock_group(sb, first_group + i); |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 807 | if (buffer_uptodate(bh[i])) { |
| 808 | /* |
| 809 | * if not uninit if bh is uptodate, |
| 810 | * bitmap is also uptodate |
| 811 | */ |
| 812 | set_bitmap_uptodate(bh[i]); |
| 813 | unlock_buffer(bh[i]); |
| 814 | continue; |
| 815 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 816 | get_bh(bh[i]); |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 817 | /* |
| 818 | * submit the buffer_head for read. We can |
| 819 | * safely mark the bitmap as uptodate now. |
| 820 | * We do it here so the bitmap uptodate bit |
| 821 | * get set with buffer lock held. |
| 822 | */ |
| 823 | set_bitmap_uptodate(bh[i]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 824 | bh[i]->b_end_io = end_buffer_read_sync; |
| 825 | submit_bh(READ, bh[i]); |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 826 | mb_debug(1, "read bitmap for group %u\n", first_group + i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* wait for I/O completion */ |
| 830 | for (i = 0; i < groups_per_page && bh[i]; i++) |
| 831 | wait_on_buffer(bh[i]); |
| 832 | |
| 833 | err = -EIO; |
| 834 | for (i = 0; i < groups_per_page && bh[i]; i++) |
| 835 | if (!buffer_uptodate(bh[i])) |
| 836 | goto out; |
| 837 | |
Mingming Cao | 31b481d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 838 | err = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 839 | first_block = page->index * blocks_per_page; |
Aneesh Kumar K.V | 29eaf02 | 2009-01-05 21:48:56 -0500 | [diff] [blame] | 840 | /* init the page */ |
| 841 | memset(page_address(page), 0xff, PAGE_CACHE_SIZE); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 842 | for (i = 0; i < blocks_per_page; i++) { |
| 843 | int group; |
| 844 | struct ext4_group_info *grinfo; |
| 845 | |
| 846 | group = (first_block + i) >> 1; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 847 | if (group >= ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 848 | break; |
| 849 | |
| 850 | /* |
| 851 | * data carry information regarding this |
| 852 | * particular group in the format specified |
| 853 | * above |
| 854 | * |
| 855 | */ |
| 856 | data = page_address(page) + (i * blocksize); |
| 857 | bitmap = bh[group - first_group]->b_data; |
| 858 | |
| 859 | /* |
| 860 | * We place the buddy block and bitmap block |
| 861 | * close together |
| 862 | */ |
| 863 | if ((first_block + i) & 1) { |
| 864 | /* this is block of buddy */ |
| 865 | BUG_ON(incore == NULL); |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 866 | mb_debug(1, "put buddy for group %u in page %lu/%x\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 867 | group, page->index, i * blocksize); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 868 | grinfo = ext4_get_group_info(sb, group); |
| 869 | grinfo->bb_fragments = 0; |
| 870 | memset(grinfo->bb_counters, 0, |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame^] | 871 | sizeof(*grinfo->bb_counters) * |
| 872 | (sb->s_blocksize_bits+2)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 873 | /* |
| 874 | * incore got set to the group block bitmap below |
| 875 | */ |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 876 | ext4_lock_group(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 877 | ext4_mb_generate_buddy(sb, data, incore, group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 878 | ext4_unlock_group(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 879 | incore = NULL; |
| 880 | } else { |
| 881 | /* this is block of bitmap */ |
| 882 | BUG_ON(incore != NULL); |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 883 | mb_debug(1, "put bitmap for group %u in page %lu/%x\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 884 | group, page->index, i * blocksize); |
| 885 | |
| 886 | /* see comments in ext4_mb_put_pa() */ |
| 887 | ext4_lock_group(sb, group); |
| 888 | memcpy(data, bitmap, blocksize); |
| 889 | |
| 890 | /* mark all preallocated blks used in in-core bitmap */ |
| 891 | ext4_mb_generate_from_pa(sb, data, group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 892 | ext4_mb_generate_from_freelist(sb, data, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 893 | ext4_unlock_group(sb, group); |
| 894 | |
| 895 | /* set incore so that the buddy information can be |
| 896 | * generated using this |
| 897 | */ |
| 898 | incore = data; |
| 899 | } |
| 900 | } |
| 901 | SetPageUptodate(page); |
| 902 | |
| 903 | out: |
| 904 | if (bh) { |
| 905 | for (i = 0; i < groups_per_page && bh[i]; i++) |
| 906 | brelse(bh[i]); |
| 907 | if (bh != &bhs) |
| 908 | kfree(bh); |
| 909 | } |
| 910 | return err; |
| 911 | } |
| 912 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 913 | static noinline_for_stack int |
| 914 | ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group, |
| 915 | struct ext4_buddy *e4b) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 916 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 917 | int blocks_per_page; |
| 918 | int block; |
| 919 | int pnum; |
| 920 | int poff; |
| 921 | struct page *page; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 922 | int ret; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 923 | struct ext4_group_info *grp; |
| 924 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 925 | struct inode *inode = sbi->s_buddy_cache; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 926 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 927 | mb_debug(1, "load group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 928 | |
| 929 | blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 930 | grp = ext4_get_group_info(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 931 | |
| 932 | e4b->bd_blkbits = sb->s_blocksize_bits; |
| 933 | e4b->bd_info = ext4_get_group_info(sb, group); |
| 934 | e4b->bd_sb = sb; |
| 935 | e4b->bd_group = group; |
| 936 | e4b->bd_buddy_page = NULL; |
| 937 | e4b->bd_bitmap_page = NULL; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 938 | e4b->alloc_semp = &grp->alloc_sem; |
| 939 | |
| 940 | /* Take the read lock on the group alloc |
| 941 | * sem. This would make sure a parallel |
| 942 | * ext4_mb_init_group happening on other |
| 943 | * groups mapped by the page is blocked |
| 944 | * till we are done with allocation |
| 945 | */ |
| 946 | down_read(e4b->alloc_semp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 947 | |
| 948 | /* |
| 949 | * the buddy cache inode stores the block bitmap |
| 950 | * and buddy information in consecutive blocks. |
| 951 | * So for each group we need two blocks. |
| 952 | */ |
| 953 | block = group * 2; |
| 954 | pnum = block / blocks_per_page; |
| 955 | poff = block % blocks_per_page; |
| 956 | |
| 957 | /* we could use find_or_create_page(), but it locks page |
| 958 | * what we'd like to avoid in fast path ... */ |
| 959 | page = find_get_page(inode->i_mapping, pnum); |
| 960 | if (page == NULL || !PageUptodate(page)) { |
| 961 | if (page) |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 962 | /* |
| 963 | * drop the page reference and try |
| 964 | * to get the page with lock. If we |
| 965 | * are not uptodate that implies |
| 966 | * somebody just created the page but |
| 967 | * is yet to initialize the same. So |
| 968 | * wait for it to initialize. |
| 969 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 970 | page_cache_release(page); |
| 971 | page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); |
| 972 | if (page) { |
| 973 | BUG_ON(page->mapping != inode->i_mapping); |
| 974 | if (!PageUptodate(page)) { |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 975 | ret = ext4_mb_init_cache(page, NULL); |
| 976 | if (ret) { |
| 977 | unlock_page(page); |
| 978 | goto err; |
| 979 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 980 | mb_cmp_bitmaps(e4b, page_address(page) + |
| 981 | (poff * sb->s_blocksize)); |
| 982 | } |
| 983 | unlock_page(page); |
| 984 | } |
| 985 | } |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 986 | if (page == NULL || !PageUptodate(page)) { |
| 987 | ret = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 988 | goto err; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 989 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 990 | e4b->bd_bitmap_page = page; |
| 991 | e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize); |
| 992 | mark_page_accessed(page); |
| 993 | |
| 994 | block++; |
| 995 | pnum = block / blocks_per_page; |
| 996 | poff = block % blocks_per_page; |
| 997 | |
| 998 | page = find_get_page(inode->i_mapping, pnum); |
| 999 | if (page == NULL || !PageUptodate(page)) { |
| 1000 | if (page) |
| 1001 | page_cache_release(page); |
| 1002 | page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); |
| 1003 | if (page) { |
| 1004 | BUG_ON(page->mapping != inode->i_mapping); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1005 | if (!PageUptodate(page)) { |
| 1006 | ret = ext4_mb_init_cache(page, e4b->bd_bitmap); |
| 1007 | if (ret) { |
| 1008 | unlock_page(page); |
| 1009 | goto err; |
| 1010 | } |
| 1011 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1012 | unlock_page(page); |
| 1013 | } |
| 1014 | } |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1015 | if (page == NULL || !PageUptodate(page)) { |
| 1016 | ret = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1017 | goto err; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1018 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1019 | e4b->bd_buddy_page = page; |
| 1020 | e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize); |
| 1021 | mark_page_accessed(page); |
| 1022 | |
| 1023 | BUG_ON(e4b->bd_bitmap_page == NULL); |
| 1024 | BUG_ON(e4b->bd_buddy_page == NULL); |
| 1025 | |
| 1026 | return 0; |
| 1027 | |
| 1028 | err: |
| 1029 | if (e4b->bd_bitmap_page) |
| 1030 | page_cache_release(e4b->bd_bitmap_page); |
| 1031 | if (e4b->bd_buddy_page) |
| 1032 | page_cache_release(e4b->bd_buddy_page); |
| 1033 | e4b->bd_buddy = NULL; |
| 1034 | e4b->bd_bitmap = NULL; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1035 | |
| 1036 | /* Done with the buddy cache */ |
| 1037 | up_read(e4b->alloc_semp); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1038 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | static void ext4_mb_release_desc(struct ext4_buddy *e4b) |
| 1042 | { |
| 1043 | if (e4b->bd_bitmap_page) |
| 1044 | page_cache_release(e4b->bd_bitmap_page); |
| 1045 | if (e4b->bd_buddy_page) |
| 1046 | page_cache_release(e4b->bd_buddy_page); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1047 | /* Done with the buddy cache */ |
Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 1048 | if (e4b->alloc_semp) |
| 1049 | up_read(e4b->alloc_semp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | static int mb_find_order_for_block(struct ext4_buddy *e4b, int block) |
| 1054 | { |
| 1055 | int order = 1; |
| 1056 | void *bb; |
| 1057 | |
| 1058 | BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b)); |
| 1059 | BUG_ON(block >= (1 << (e4b->bd_blkbits + 3))); |
| 1060 | |
| 1061 | bb = EXT4_MB_BUDDY(e4b); |
| 1062 | while (order <= e4b->bd_blkbits + 1) { |
| 1063 | block = block >> 1; |
| 1064 | if (!mb_test_bit(block, bb)) { |
| 1065 | /* this block is part of buddy of order 'order' */ |
| 1066 | return order; |
| 1067 | } |
| 1068 | bb += 1 << (e4b->bd_blkbits - order); |
| 1069 | order++; |
| 1070 | } |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1074 | static void mb_clear_bits(void *bm, int cur, int len) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1075 | { |
| 1076 | __u32 *addr; |
| 1077 | |
| 1078 | len = cur + len; |
| 1079 | while (cur < len) { |
| 1080 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1081 | /* fast path: clear whole word at once */ |
| 1082 | addr = bm + (cur >> 3); |
| 1083 | *addr = 0; |
| 1084 | cur += 32; |
| 1085 | continue; |
| 1086 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1087 | mb_clear_bit(cur, bm); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1088 | cur++; |
| 1089 | } |
| 1090 | } |
| 1091 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1092 | static void mb_set_bits(void *bm, int cur, int len) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1093 | { |
| 1094 | __u32 *addr; |
| 1095 | |
| 1096 | len = cur + len; |
| 1097 | while (cur < len) { |
| 1098 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1099 | /* fast path: set whole word at once */ |
| 1100 | addr = bm + (cur >> 3); |
| 1101 | *addr = 0xffffffff; |
| 1102 | cur += 32; |
| 1103 | continue; |
| 1104 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1105 | mb_set_bit(cur, bm); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1106 | cur++; |
| 1107 | } |
| 1108 | } |
| 1109 | |
Shen Feng | 7e5a8cd | 2008-07-13 21:03:31 -0400 | [diff] [blame] | 1110 | static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1111 | int first, int count) |
| 1112 | { |
| 1113 | int block = 0; |
| 1114 | int max = 0; |
| 1115 | int order; |
| 1116 | void *buddy; |
| 1117 | void *buddy2; |
| 1118 | struct super_block *sb = e4b->bd_sb; |
| 1119 | |
| 1120 | BUG_ON(first + count > (sb->s_blocksize << 3)); |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1121 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1122 | mb_check_buddy(e4b); |
| 1123 | mb_free_blocks_double(inode, e4b, first, count); |
| 1124 | |
| 1125 | e4b->bd_info->bb_free += count; |
| 1126 | if (first < e4b->bd_info->bb_first_free) |
| 1127 | e4b->bd_info->bb_first_free = first; |
| 1128 | |
| 1129 | /* let's maintain fragments counter */ |
| 1130 | if (first != 0) |
| 1131 | block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b)); |
| 1132 | if (first + count < EXT4_SB(sb)->s_mb_maxs[0]) |
| 1133 | max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b)); |
| 1134 | if (block && max) |
| 1135 | e4b->bd_info->bb_fragments--; |
| 1136 | else if (!block && !max) |
| 1137 | e4b->bd_info->bb_fragments++; |
| 1138 | |
| 1139 | /* let's maintain buddy itself */ |
| 1140 | while (count-- > 0) { |
| 1141 | block = first++; |
| 1142 | order = 0; |
| 1143 | |
| 1144 | if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) { |
| 1145 | ext4_fsblk_t blocknr; |
| 1146 | blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb); |
| 1147 | blocknr += block; |
| 1148 | blocknr += |
| 1149 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 1150 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 1151 | __func__, "double-free of inode" |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 1152 | " %lu's block %llu(bit %u in group %u)", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1153 | inode ? inode->i_ino : 0, blocknr, block, |
| 1154 | e4b->bd_group); |
| 1155 | } |
| 1156 | mb_clear_bit(block, EXT4_MB_BITMAP(e4b)); |
| 1157 | e4b->bd_info->bb_counters[order]++; |
| 1158 | |
| 1159 | /* start of the buddy */ |
| 1160 | buddy = mb_find_buddy(e4b, order, &max); |
| 1161 | |
| 1162 | do { |
| 1163 | block &= ~1UL; |
| 1164 | if (mb_test_bit(block, buddy) || |
| 1165 | mb_test_bit(block + 1, buddy)) |
| 1166 | break; |
| 1167 | |
| 1168 | /* both the buddies are free, try to coalesce them */ |
| 1169 | buddy2 = mb_find_buddy(e4b, order + 1, &max); |
| 1170 | |
| 1171 | if (!buddy2) |
| 1172 | break; |
| 1173 | |
| 1174 | if (order > 0) { |
| 1175 | /* for special purposes, we don't set |
| 1176 | * free bits in bitmap */ |
| 1177 | mb_set_bit(block, buddy); |
| 1178 | mb_set_bit(block + 1, buddy); |
| 1179 | } |
| 1180 | e4b->bd_info->bb_counters[order]--; |
| 1181 | e4b->bd_info->bb_counters[order]--; |
| 1182 | |
| 1183 | block = block >> 1; |
| 1184 | order++; |
| 1185 | e4b->bd_info->bb_counters[order]++; |
| 1186 | |
| 1187 | mb_clear_bit(block, buddy2); |
| 1188 | buddy = buddy2; |
| 1189 | } while (1); |
| 1190 | } |
| 1191 | mb_check_buddy(e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | static int mb_find_extent(struct ext4_buddy *e4b, int order, int block, |
| 1195 | int needed, struct ext4_free_extent *ex) |
| 1196 | { |
| 1197 | int next = block; |
| 1198 | int max; |
| 1199 | int ord; |
| 1200 | void *buddy; |
| 1201 | |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1202 | assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1203 | BUG_ON(ex == NULL); |
| 1204 | |
| 1205 | buddy = mb_find_buddy(e4b, order, &max); |
| 1206 | BUG_ON(buddy == NULL); |
| 1207 | BUG_ON(block >= max); |
| 1208 | if (mb_test_bit(block, buddy)) { |
| 1209 | ex->fe_len = 0; |
| 1210 | ex->fe_start = 0; |
| 1211 | ex->fe_group = 0; |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | /* FIXME dorp order completely ? */ |
| 1216 | if (likely(order == 0)) { |
| 1217 | /* find actual order */ |
| 1218 | order = mb_find_order_for_block(e4b, block); |
| 1219 | block = block >> order; |
| 1220 | } |
| 1221 | |
| 1222 | ex->fe_len = 1 << order; |
| 1223 | ex->fe_start = block << order; |
| 1224 | ex->fe_group = e4b->bd_group; |
| 1225 | |
| 1226 | /* calc difference from given start */ |
| 1227 | next = next - ex->fe_start; |
| 1228 | ex->fe_len -= next; |
| 1229 | ex->fe_start += next; |
| 1230 | |
| 1231 | while (needed > ex->fe_len && |
| 1232 | (buddy = mb_find_buddy(e4b, order, &max))) { |
| 1233 | |
| 1234 | if (block + 1 >= max) |
| 1235 | break; |
| 1236 | |
| 1237 | next = (block + 1) * (1 << order); |
| 1238 | if (mb_test_bit(next, EXT4_MB_BITMAP(e4b))) |
| 1239 | break; |
| 1240 | |
| 1241 | ord = mb_find_order_for_block(e4b, next); |
| 1242 | |
| 1243 | order = ord; |
| 1244 | block = next >> order; |
| 1245 | ex->fe_len += 1 << order; |
| 1246 | } |
| 1247 | |
| 1248 | BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3))); |
| 1249 | return ex->fe_len; |
| 1250 | } |
| 1251 | |
| 1252 | static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) |
| 1253 | { |
| 1254 | int ord; |
| 1255 | int mlen = 0; |
| 1256 | int max = 0; |
| 1257 | int cur; |
| 1258 | int start = ex->fe_start; |
| 1259 | int len = ex->fe_len; |
| 1260 | unsigned ret = 0; |
| 1261 | int len0 = len; |
| 1262 | void *buddy; |
| 1263 | |
| 1264 | BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3)); |
| 1265 | BUG_ON(e4b->bd_group != ex->fe_group); |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1266 | assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1267 | mb_check_buddy(e4b); |
| 1268 | mb_mark_used_double(e4b, start, len); |
| 1269 | |
| 1270 | e4b->bd_info->bb_free -= len; |
| 1271 | if (e4b->bd_info->bb_first_free == start) |
| 1272 | e4b->bd_info->bb_first_free += len; |
| 1273 | |
| 1274 | /* let's maintain fragments counter */ |
| 1275 | if (start != 0) |
| 1276 | mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b)); |
| 1277 | if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0]) |
| 1278 | max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b)); |
| 1279 | if (mlen && max) |
| 1280 | e4b->bd_info->bb_fragments++; |
| 1281 | else if (!mlen && !max) |
| 1282 | e4b->bd_info->bb_fragments--; |
| 1283 | |
| 1284 | /* let's maintain buddy itself */ |
| 1285 | while (len) { |
| 1286 | ord = mb_find_order_for_block(e4b, start); |
| 1287 | |
| 1288 | if (((start >> ord) << ord) == start && len >= (1 << ord)) { |
| 1289 | /* the whole chunk may be allocated at once! */ |
| 1290 | mlen = 1 << ord; |
| 1291 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1292 | BUG_ON((start >> ord) >= max); |
| 1293 | mb_set_bit(start >> ord, buddy); |
| 1294 | e4b->bd_info->bb_counters[ord]--; |
| 1295 | start += mlen; |
| 1296 | len -= mlen; |
| 1297 | BUG_ON(len < 0); |
| 1298 | continue; |
| 1299 | } |
| 1300 | |
| 1301 | /* store for history */ |
| 1302 | if (ret == 0) |
| 1303 | ret = len | (ord << 16); |
| 1304 | |
| 1305 | /* we have to split large buddy */ |
| 1306 | BUG_ON(ord <= 0); |
| 1307 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1308 | mb_set_bit(start >> ord, buddy); |
| 1309 | e4b->bd_info->bb_counters[ord]--; |
| 1310 | |
| 1311 | ord--; |
| 1312 | cur = (start >> ord) & ~1U; |
| 1313 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1314 | mb_clear_bit(cur, buddy); |
| 1315 | mb_clear_bit(cur + 1, buddy); |
| 1316 | e4b->bd_info->bb_counters[ord]++; |
| 1317 | e4b->bd_info->bb_counters[ord]++; |
| 1318 | } |
| 1319 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1320 | mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1321 | mb_check_buddy(e4b); |
| 1322 | |
| 1323 | return ret; |
| 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * Must be called under group lock! |
| 1328 | */ |
| 1329 | static void ext4_mb_use_best_found(struct ext4_allocation_context *ac, |
| 1330 | struct ext4_buddy *e4b) |
| 1331 | { |
| 1332 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 1333 | int ret; |
| 1334 | |
| 1335 | BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group); |
| 1336 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
| 1337 | |
| 1338 | ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len); |
| 1339 | ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical; |
| 1340 | ret = mb_mark_used(e4b, &ac->ac_b_ex); |
| 1341 | |
| 1342 | /* preallocation can change ac_b_ex, thus we store actually |
| 1343 | * allocated blocks for history */ |
| 1344 | ac->ac_f_ex = ac->ac_b_ex; |
| 1345 | |
| 1346 | ac->ac_status = AC_STATUS_FOUND; |
| 1347 | ac->ac_tail = ret & 0xffff; |
| 1348 | ac->ac_buddy = ret >> 16; |
| 1349 | |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 1350 | /* |
| 1351 | * take the page reference. We want the page to be pinned |
| 1352 | * so that we don't get a ext4_mb_init_cache_call for this |
| 1353 | * group until we update the bitmap. That would mean we |
| 1354 | * double allocate blocks. The reference is dropped |
| 1355 | * in ext4_mb_release_context |
| 1356 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1357 | ac->ac_bitmap_page = e4b->bd_bitmap_page; |
| 1358 | get_page(ac->ac_bitmap_page); |
| 1359 | ac->ac_buddy_page = e4b->bd_buddy_page; |
| 1360 | get_page(ac->ac_buddy_page); |
Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 1361 | /* on allocation we use ac to track the held semaphore */ |
| 1362 | ac->alloc_semp = e4b->alloc_semp; |
| 1363 | e4b->alloc_semp = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1364 | /* store last allocated for subsequent stream allocation */ |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 1365 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1366 | spin_lock(&sbi->s_md_lock); |
| 1367 | sbi->s_mb_last_group = ac->ac_f_ex.fe_group; |
| 1368 | sbi->s_mb_last_start = ac->ac_f_ex.fe_start; |
| 1369 | spin_unlock(&sbi->s_md_lock); |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * regular allocator, for general purposes allocation |
| 1375 | */ |
| 1376 | |
| 1377 | static void ext4_mb_check_limits(struct ext4_allocation_context *ac, |
| 1378 | struct ext4_buddy *e4b, |
| 1379 | int finish_group) |
| 1380 | { |
| 1381 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 1382 | struct ext4_free_extent *bex = &ac->ac_b_ex; |
| 1383 | struct ext4_free_extent *gex = &ac->ac_g_ex; |
| 1384 | struct ext4_free_extent ex; |
| 1385 | int max; |
| 1386 | |
Aneesh Kumar K.V | 032115f | 2009-01-05 21:34:30 -0500 | [diff] [blame] | 1387 | if (ac->ac_status == AC_STATUS_FOUND) |
| 1388 | return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1389 | /* |
| 1390 | * We don't want to scan for a whole year |
| 1391 | */ |
| 1392 | if (ac->ac_found > sbi->s_mb_max_to_scan && |
| 1393 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 1394 | ac->ac_status = AC_STATUS_BREAK; |
| 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | /* |
| 1399 | * Haven't found good chunk so far, let's continue |
| 1400 | */ |
| 1401 | if (bex->fe_len < gex->fe_len) |
| 1402 | return; |
| 1403 | |
| 1404 | if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan) |
| 1405 | && bex->fe_group == e4b->bd_group) { |
| 1406 | /* recheck chunk's availability - we don't know |
| 1407 | * when it was found (within this lock-unlock |
| 1408 | * period or not) */ |
| 1409 | max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex); |
| 1410 | if (max >= gex->fe_len) { |
| 1411 | ext4_mb_use_best_found(ac, e4b); |
| 1412 | return; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * The routine checks whether found extent is good enough. If it is, |
| 1419 | * then the extent gets marked used and flag is set to the context |
| 1420 | * to stop scanning. Otherwise, the extent is compared with the |
| 1421 | * previous found extent and if new one is better, then it's stored |
| 1422 | * in the context. Later, the best found extent will be used, if |
| 1423 | * mballoc can't find good enough extent. |
| 1424 | * |
| 1425 | * FIXME: real allocation policy is to be designed yet! |
| 1426 | */ |
| 1427 | static void ext4_mb_measure_extent(struct ext4_allocation_context *ac, |
| 1428 | struct ext4_free_extent *ex, |
| 1429 | struct ext4_buddy *e4b) |
| 1430 | { |
| 1431 | struct ext4_free_extent *bex = &ac->ac_b_ex; |
| 1432 | struct ext4_free_extent *gex = &ac->ac_g_ex; |
| 1433 | |
| 1434 | BUG_ON(ex->fe_len <= 0); |
Eric Sandeen | 8d03c7a | 2009-03-14 11:51:46 -0400 | [diff] [blame] | 1435 | BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1436 | BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
| 1437 | BUG_ON(ac->ac_status != AC_STATUS_CONTINUE); |
| 1438 | |
| 1439 | ac->ac_found++; |
| 1440 | |
| 1441 | /* |
| 1442 | * The special case - take what you catch first |
| 1443 | */ |
| 1444 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 1445 | *bex = *ex; |
| 1446 | ext4_mb_use_best_found(ac, e4b); |
| 1447 | return; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Let's check whether the chuck is good enough |
| 1452 | */ |
| 1453 | if (ex->fe_len == gex->fe_len) { |
| 1454 | *bex = *ex; |
| 1455 | ext4_mb_use_best_found(ac, e4b); |
| 1456 | return; |
| 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | * If this is first found extent, just store it in the context |
| 1461 | */ |
| 1462 | if (bex->fe_len == 0) { |
| 1463 | *bex = *ex; |
| 1464 | return; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * If new found extent is better, store it in the context |
| 1469 | */ |
| 1470 | if (bex->fe_len < gex->fe_len) { |
| 1471 | /* if the request isn't satisfied, any found extent |
| 1472 | * larger than previous best one is better */ |
| 1473 | if (ex->fe_len > bex->fe_len) |
| 1474 | *bex = *ex; |
| 1475 | } else if (ex->fe_len > gex->fe_len) { |
| 1476 | /* if the request is satisfied, then we try to find |
| 1477 | * an extent that still satisfy the request, but is |
| 1478 | * smaller than previous one */ |
| 1479 | if (ex->fe_len < bex->fe_len) |
| 1480 | *bex = *ex; |
| 1481 | } |
| 1482 | |
| 1483 | ext4_mb_check_limits(ac, e4b, 0); |
| 1484 | } |
| 1485 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1486 | static noinline_for_stack |
| 1487 | int ext4_mb_try_best_found(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1488 | struct ext4_buddy *e4b) |
| 1489 | { |
| 1490 | struct ext4_free_extent ex = ac->ac_b_ex; |
| 1491 | ext4_group_t group = ex.fe_group; |
| 1492 | int max; |
| 1493 | int err; |
| 1494 | |
| 1495 | BUG_ON(ex.fe_len <= 0); |
| 1496 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); |
| 1497 | if (err) |
| 1498 | return err; |
| 1499 | |
| 1500 | ext4_lock_group(ac->ac_sb, group); |
| 1501 | max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex); |
| 1502 | |
| 1503 | if (max > 0) { |
| 1504 | ac->ac_b_ex = ex; |
| 1505 | ext4_mb_use_best_found(ac, e4b); |
| 1506 | } |
| 1507 | |
| 1508 | ext4_unlock_group(ac->ac_sb, group); |
| 1509 | ext4_mb_release_desc(e4b); |
| 1510 | |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1514 | static noinline_for_stack |
| 1515 | int ext4_mb_find_by_goal(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1516 | struct ext4_buddy *e4b) |
| 1517 | { |
| 1518 | ext4_group_t group = ac->ac_g_ex.fe_group; |
| 1519 | int max; |
| 1520 | int err; |
| 1521 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 1522 | struct ext4_super_block *es = sbi->s_es; |
| 1523 | struct ext4_free_extent ex; |
| 1524 | |
| 1525 | if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL)) |
| 1526 | return 0; |
| 1527 | |
| 1528 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); |
| 1529 | if (err) |
| 1530 | return err; |
| 1531 | |
| 1532 | ext4_lock_group(ac->ac_sb, group); |
| 1533 | max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start, |
| 1534 | ac->ac_g_ex.fe_len, &ex); |
| 1535 | |
| 1536 | if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) { |
| 1537 | ext4_fsblk_t start; |
| 1538 | |
| 1539 | start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) + |
| 1540 | ex.fe_start + le32_to_cpu(es->s_first_data_block); |
| 1541 | /* use do_div to get remainder (would be 64-bit modulo) */ |
| 1542 | if (do_div(start, sbi->s_stripe) == 0) { |
| 1543 | ac->ac_found++; |
| 1544 | ac->ac_b_ex = ex; |
| 1545 | ext4_mb_use_best_found(ac, e4b); |
| 1546 | } |
| 1547 | } else if (max >= ac->ac_g_ex.fe_len) { |
| 1548 | BUG_ON(ex.fe_len <= 0); |
| 1549 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); |
| 1550 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); |
| 1551 | ac->ac_found++; |
| 1552 | ac->ac_b_ex = ex; |
| 1553 | ext4_mb_use_best_found(ac, e4b); |
| 1554 | } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) { |
| 1555 | /* Sometimes, caller may want to merge even small |
| 1556 | * number of blocks to an existing extent */ |
| 1557 | BUG_ON(ex.fe_len <= 0); |
| 1558 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); |
| 1559 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); |
| 1560 | ac->ac_found++; |
| 1561 | ac->ac_b_ex = ex; |
| 1562 | ext4_mb_use_best_found(ac, e4b); |
| 1563 | } |
| 1564 | ext4_unlock_group(ac->ac_sb, group); |
| 1565 | ext4_mb_release_desc(e4b); |
| 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * The routine scans buddy structures (not bitmap!) from given order |
| 1572 | * to max order and tries to find big enough chunk to satisfy the req |
| 1573 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1574 | static noinline_for_stack |
| 1575 | void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1576 | struct ext4_buddy *e4b) |
| 1577 | { |
| 1578 | struct super_block *sb = ac->ac_sb; |
| 1579 | struct ext4_group_info *grp = e4b->bd_info; |
| 1580 | void *buddy; |
| 1581 | int i; |
| 1582 | int k; |
| 1583 | int max; |
| 1584 | |
| 1585 | BUG_ON(ac->ac_2order <= 0); |
| 1586 | for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) { |
| 1587 | if (grp->bb_counters[i] == 0) |
| 1588 | continue; |
| 1589 | |
| 1590 | buddy = mb_find_buddy(e4b, i, &max); |
| 1591 | BUG_ON(buddy == NULL); |
| 1592 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 1593 | k = mb_find_next_zero_bit(buddy, max, 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1594 | BUG_ON(k >= max); |
| 1595 | |
| 1596 | ac->ac_found++; |
| 1597 | |
| 1598 | ac->ac_b_ex.fe_len = 1 << i; |
| 1599 | ac->ac_b_ex.fe_start = k << i; |
| 1600 | ac->ac_b_ex.fe_group = e4b->bd_group; |
| 1601 | |
| 1602 | ext4_mb_use_best_found(ac, e4b); |
| 1603 | |
| 1604 | BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len); |
| 1605 | |
| 1606 | if (EXT4_SB(sb)->s_mb_stats) |
| 1607 | atomic_inc(&EXT4_SB(sb)->s_bal_2orders); |
| 1608 | |
| 1609 | break; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * The routine scans the group and measures all found extents. |
| 1615 | * In order to optimize scanning, caller must pass number of |
| 1616 | * free blocks in the group, so the routine can know upper limit. |
| 1617 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1618 | static noinline_for_stack |
| 1619 | void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1620 | struct ext4_buddy *e4b) |
| 1621 | { |
| 1622 | struct super_block *sb = ac->ac_sb; |
| 1623 | void *bitmap = EXT4_MB_BITMAP(e4b); |
| 1624 | struct ext4_free_extent ex; |
| 1625 | int i; |
| 1626 | int free; |
| 1627 | |
| 1628 | free = e4b->bd_info->bb_free; |
| 1629 | BUG_ON(free <= 0); |
| 1630 | |
| 1631 | i = e4b->bd_info->bb_first_free; |
| 1632 | |
| 1633 | while (free && ac->ac_status == AC_STATUS_CONTINUE) { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 1634 | i = mb_find_next_zero_bit(bitmap, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1635 | EXT4_BLOCKS_PER_GROUP(sb), i); |
| 1636 | if (i >= EXT4_BLOCKS_PER_GROUP(sb)) { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1637 | /* |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 1638 | * IF we have corrupt bitmap, we won't find any |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1639 | * free blocks even though group info says we |
| 1640 | * we have free blocks |
| 1641 | */ |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 1642 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 1643 | __func__, "%d free blocks as per " |
Theodore Ts'o | fde4d95 | 2009-01-05 22:17:35 -0500 | [diff] [blame] | 1644 | "group info. But bitmap says 0", |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1645 | free); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1646 | break; |
| 1647 | } |
| 1648 | |
| 1649 | mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex); |
| 1650 | BUG_ON(ex.fe_len <= 0); |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1651 | if (free < ex.fe_len) { |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 1652 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 1653 | __func__, "%d free blocks as per " |
Theodore Ts'o | fde4d95 | 2009-01-05 22:17:35 -0500 | [diff] [blame] | 1654 | "group info. But got %d blocks", |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1655 | free, ex.fe_len); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 1656 | /* |
| 1657 | * The number of free blocks differs. This mostly |
| 1658 | * indicate that the bitmap is corrupt. So exit |
| 1659 | * without claiming the space. |
| 1660 | */ |
| 1661 | break; |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1662 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1663 | |
| 1664 | ext4_mb_measure_extent(ac, &ex, e4b); |
| 1665 | |
| 1666 | i += ex.fe_len; |
| 1667 | free -= ex.fe_len; |
| 1668 | } |
| 1669 | |
| 1670 | ext4_mb_check_limits(ac, e4b, 1); |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * This is a special case for storages like raid5 |
| 1675 | * we try to find stripe-aligned chunks for stripe-size requests |
| 1676 | * XXX should do so at least for multiples of stripe size as well |
| 1677 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1678 | static noinline_for_stack |
| 1679 | void ext4_mb_scan_aligned(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1680 | struct ext4_buddy *e4b) |
| 1681 | { |
| 1682 | struct super_block *sb = ac->ac_sb; |
| 1683 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 1684 | void *bitmap = EXT4_MB_BITMAP(e4b); |
| 1685 | struct ext4_free_extent ex; |
| 1686 | ext4_fsblk_t first_group_block; |
| 1687 | ext4_fsblk_t a; |
| 1688 | ext4_grpblk_t i; |
| 1689 | int max; |
| 1690 | |
| 1691 | BUG_ON(sbi->s_stripe == 0); |
| 1692 | |
| 1693 | /* find first stripe-aligned block in group */ |
| 1694 | first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb) |
| 1695 | + le32_to_cpu(sbi->s_es->s_first_data_block); |
| 1696 | a = first_group_block + sbi->s_stripe - 1; |
| 1697 | do_div(a, sbi->s_stripe); |
| 1698 | i = (a * sbi->s_stripe) - first_group_block; |
| 1699 | |
| 1700 | while (i < EXT4_BLOCKS_PER_GROUP(sb)) { |
| 1701 | if (!mb_test_bit(i, bitmap)) { |
| 1702 | max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex); |
| 1703 | if (max >= sbi->s_stripe) { |
| 1704 | ac->ac_found++; |
| 1705 | ac->ac_b_ex = ex; |
| 1706 | ext4_mb_use_best_found(ac, e4b); |
| 1707 | break; |
| 1708 | } |
| 1709 | } |
| 1710 | i += sbi->s_stripe; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | static int ext4_mb_good_group(struct ext4_allocation_context *ac, |
| 1715 | ext4_group_t group, int cr) |
| 1716 | { |
| 1717 | unsigned free, fragments; |
| 1718 | unsigned i, bits; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 1719 | int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1720 | struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); |
| 1721 | |
| 1722 | BUG_ON(cr < 0 || cr >= 4); |
| 1723 | BUG_ON(EXT4_MB_GRP_NEED_INIT(grp)); |
| 1724 | |
| 1725 | free = grp->bb_free; |
| 1726 | fragments = grp->bb_fragments; |
| 1727 | if (free == 0) |
| 1728 | return 0; |
| 1729 | if (fragments == 0) |
| 1730 | return 0; |
| 1731 | |
| 1732 | switch (cr) { |
| 1733 | case 0: |
| 1734 | BUG_ON(ac->ac_2order == 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1735 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 1736 | /* Avoid using the first bg of a flexgroup for data files */ |
| 1737 | if ((ac->ac_flags & EXT4_MB_HINT_DATA) && |
| 1738 | (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) && |
| 1739 | ((group % flex_size) == 0)) |
| 1740 | return 0; |
| 1741 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1742 | bits = ac->ac_sb->s_blocksize_bits + 1; |
| 1743 | for (i = ac->ac_2order; i <= bits; i++) |
| 1744 | if (grp->bb_counters[i] > 0) |
| 1745 | return 1; |
| 1746 | break; |
| 1747 | case 1: |
| 1748 | if ((free / fragments) >= ac->ac_g_ex.fe_len) |
| 1749 | return 1; |
| 1750 | break; |
| 1751 | case 2: |
| 1752 | if (free >= ac->ac_g_ex.fe_len) |
| 1753 | return 1; |
| 1754 | break; |
| 1755 | case 3: |
| 1756 | return 1; |
| 1757 | default: |
| 1758 | BUG(); |
| 1759 | } |
| 1760 | |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1764 | /* |
| 1765 | * lock the group_info alloc_sem of all the groups |
| 1766 | * belonging to the same buddy cache page. This |
| 1767 | * make sure other parallel operation on the buddy |
| 1768 | * cache doesn't happen whild holding the buddy cache |
| 1769 | * lock |
| 1770 | */ |
| 1771 | int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group) |
| 1772 | { |
| 1773 | int i; |
| 1774 | int block, pnum; |
| 1775 | int blocks_per_page; |
| 1776 | int groups_per_page; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1777 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1778 | ext4_group_t first_group; |
| 1779 | struct ext4_group_info *grp; |
| 1780 | |
| 1781 | blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; |
| 1782 | /* |
| 1783 | * the buddy cache inode stores the block bitmap |
| 1784 | * and buddy information in consecutive blocks. |
| 1785 | * So for each group we need two blocks. |
| 1786 | */ |
| 1787 | block = group * 2; |
| 1788 | pnum = block / blocks_per_page; |
| 1789 | first_group = pnum * blocks_per_page / 2; |
| 1790 | |
| 1791 | groups_per_page = blocks_per_page >> 1; |
| 1792 | if (groups_per_page == 0) |
| 1793 | groups_per_page = 1; |
| 1794 | /* read all groups the page covers into the cache */ |
| 1795 | for (i = 0; i < groups_per_page; i++) { |
| 1796 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1797 | if ((first_group + i) >= ngroups) |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1798 | break; |
| 1799 | grp = ext4_get_group_info(sb, first_group + i); |
| 1800 | /* take all groups write allocation |
| 1801 | * semaphore. This make sure there is |
| 1802 | * no block allocation going on in any |
| 1803 | * of that groups |
| 1804 | */ |
Aneesh Kumar K.V | b7be019 | 2008-11-23 23:51:53 -0500 | [diff] [blame] | 1805 | down_write_nested(&grp->alloc_sem, i); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1806 | } |
| 1807 | return i; |
| 1808 | } |
| 1809 | |
| 1810 | void ext4_mb_put_buddy_cache_lock(struct super_block *sb, |
| 1811 | ext4_group_t group, int locked_group) |
| 1812 | { |
| 1813 | int i; |
| 1814 | int block, pnum; |
| 1815 | int blocks_per_page; |
| 1816 | ext4_group_t first_group; |
| 1817 | struct ext4_group_info *grp; |
| 1818 | |
| 1819 | blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; |
| 1820 | /* |
| 1821 | * the buddy cache inode stores the block bitmap |
| 1822 | * and buddy information in consecutive blocks. |
| 1823 | * So for each group we need two blocks. |
| 1824 | */ |
| 1825 | block = group * 2; |
| 1826 | pnum = block / blocks_per_page; |
| 1827 | first_group = pnum * blocks_per_page / 2; |
| 1828 | /* release locks on all the groups */ |
| 1829 | for (i = 0; i < locked_group; i++) { |
| 1830 | |
| 1831 | grp = ext4_get_group_info(sb, first_group + i); |
| 1832 | /* take all groups write allocation |
| 1833 | * semaphore. This make sure there is |
| 1834 | * no block allocation going on in any |
| 1835 | * of that groups |
| 1836 | */ |
| 1837 | up_write(&grp->alloc_sem); |
| 1838 | } |
| 1839 | |
| 1840 | } |
| 1841 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1842 | static noinline_for_stack |
| 1843 | int ext4_mb_init_group(struct super_block *sb, ext4_group_t group) |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1844 | { |
| 1845 | |
| 1846 | int ret; |
| 1847 | void *bitmap; |
| 1848 | int blocks_per_page; |
| 1849 | int block, pnum, poff; |
| 1850 | int num_grp_locked = 0; |
| 1851 | struct ext4_group_info *this_grp; |
| 1852 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 1853 | struct inode *inode = sbi->s_buddy_cache; |
| 1854 | struct page *page = NULL, *bitmap_page = NULL; |
| 1855 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 1856 | mb_debug(1, "init group %u\n", group); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1857 | blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; |
| 1858 | this_grp = ext4_get_group_info(sb, group); |
| 1859 | /* |
| 1860 | * This ensures we don't add group |
| 1861 | * to this buddy cache via resize |
| 1862 | */ |
| 1863 | num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group); |
| 1864 | if (!EXT4_MB_GRP_NEED_INIT(this_grp)) { |
| 1865 | /* |
| 1866 | * somebody initialized the group |
| 1867 | * return without doing anything |
| 1868 | */ |
| 1869 | ret = 0; |
| 1870 | goto err; |
| 1871 | } |
| 1872 | /* |
| 1873 | * the buddy cache inode stores the block bitmap |
| 1874 | * and buddy information in consecutive blocks. |
| 1875 | * So for each group we need two blocks. |
| 1876 | */ |
| 1877 | block = group * 2; |
| 1878 | pnum = block / blocks_per_page; |
| 1879 | poff = block % blocks_per_page; |
| 1880 | page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); |
| 1881 | if (page) { |
| 1882 | BUG_ON(page->mapping != inode->i_mapping); |
| 1883 | ret = ext4_mb_init_cache(page, NULL); |
| 1884 | if (ret) { |
| 1885 | unlock_page(page); |
| 1886 | goto err; |
| 1887 | } |
| 1888 | unlock_page(page); |
| 1889 | } |
| 1890 | if (page == NULL || !PageUptodate(page)) { |
| 1891 | ret = -EIO; |
| 1892 | goto err; |
| 1893 | } |
| 1894 | mark_page_accessed(page); |
| 1895 | bitmap_page = page; |
| 1896 | bitmap = page_address(page) + (poff * sb->s_blocksize); |
| 1897 | |
| 1898 | /* init buddy cache */ |
| 1899 | block++; |
| 1900 | pnum = block / blocks_per_page; |
| 1901 | poff = block % blocks_per_page; |
| 1902 | page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); |
| 1903 | if (page == bitmap_page) { |
| 1904 | /* |
| 1905 | * If both the bitmap and buddy are in |
| 1906 | * the same page we don't need to force |
| 1907 | * init the buddy |
| 1908 | */ |
| 1909 | unlock_page(page); |
| 1910 | } else if (page) { |
| 1911 | BUG_ON(page->mapping != inode->i_mapping); |
| 1912 | ret = ext4_mb_init_cache(page, bitmap); |
| 1913 | if (ret) { |
| 1914 | unlock_page(page); |
| 1915 | goto err; |
| 1916 | } |
| 1917 | unlock_page(page); |
| 1918 | } |
| 1919 | if (page == NULL || !PageUptodate(page)) { |
| 1920 | ret = -EIO; |
| 1921 | goto err; |
| 1922 | } |
| 1923 | mark_page_accessed(page); |
| 1924 | err: |
| 1925 | ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked); |
| 1926 | if (bitmap_page) |
| 1927 | page_cache_release(bitmap_page); |
| 1928 | if (page) |
| 1929 | page_cache_release(page); |
| 1930 | return ret; |
| 1931 | } |
| 1932 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1933 | static noinline_for_stack int |
| 1934 | ext4_mb_regular_allocator(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1935 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1936 | ext4_group_t ngroups, group, i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1937 | int cr; |
| 1938 | int err = 0; |
| 1939 | int bsbits; |
| 1940 | struct ext4_sb_info *sbi; |
| 1941 | struct super_block *sb; |
| 1942 | struct ext4_buddy e4b; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1943 | |
| 1944 | sb = ac->ac_sb; |
| 1945 | sbi = EXT4_SB(sb); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1946 | ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1947 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
| 1948 | |
| 1949 | /* first, try the goal */ |
| 1950 | err = ext4_mb_find_by_goal(ac, &e4b); |
| 1951 | if (err || ac->ac_status == AC_STATUS_FOUND) |
| 1952 | goto out; |
| 1953 | |
| 1954 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 1955 | goto out; |
| 1956 | |
| 1957 | /* |
| 1958 | * ac->ac2_order is set only if the fe_len is a power of 2 |
| 1959 | * if ac2_order is set we also set criteria to 0 so that we |
| 1960 | * try exact allocation using buddy. |
| 1961 | */ |
| 1962 | i = fls(ac->ac_g_ex.fe_len); |
| 1963 | ac->ac_2order = 0; |
| 1964 | /* |
| 1965 | * We search using buddy data only if the order of the request |
| 1966 | * is greater than equal to the sbi_s_mb_order2_reqs |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 1967 | * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1968 | */ |
| 1969 | if (i >= sbi->s_mb_order2_reqs) { |
| 1970 | /* |
| 1971 | * This should tell if fe_len is exactly power of 2 |
| 1972 | */ |
| 1973 | if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0) |
| 1974 | ac->ac_2order = i - 1; |
| 1975 | } |
| 1976 | |
| 1977 | bsbits = ac->ac_sb->s_blocksize_bits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1978 | |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 1979 | /* if stream allocation is enabled, use global goal */ |
| 1980 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1981 | /* TBD: may be hot point */ |
| 1982 | spin_lock(&sbi->s_md_lock); |
| 1983 | ac->ac_g_ex.fe_group = sbi->s_mb_last_group; |
| 1984 | ac->ac_g_ex.fe_start = sbi->s_mb_last_start; |
| 1985 | spin_unlock(&sbi->s_md_lock); |
| 1986 | } |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 1987 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1988 | /* Let's just scan groups to find more-less suitable blocks */ |
| 1989 | cr = ac->ac_2order ? 0 : 1; |
| 1990 | /* |
| 1991 | * cr == 0 try to get exact allocation, |
| 1992 | * cr == 3 try to get anything |
| 1993 | */ |
| 1994 | repeat: |
| 1995 | for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) { |
| 1996 | ac->ac_criteria = cr; |
Aneesh Kumar K.V | ed8f9c7 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1997 | /* |
| 1998 | * searching for the right group start |
| 1999 | * from the goal value specified |
| 2000 | */ |
| 2001 | group = ac->ac_g_ex.fe_group; |
| 2002 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2003 | for (i = 0; i < ngroups; group++, i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2004 | struct ext4_group_info *grp; |
| 2005 | struct ext4_group_desc *desc; |
| 2006 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2007 | if (group == ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2008 | group = 0; |
| 2009 | |
| 2010 | /* quick check to skip empty groups */ |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 2011 | grp = ext4_get_group_info(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2012 | if (grp->bb_free == 0) |
| 2013 | continue; |
| 2014 | |
| 2015 | /* |
| 2016 | * if the group is already init we check whether it is |
| 2017 | * a good group and if not we don't load the buddy |
| 2018 | */ |
| 2019 | if (EXT4_MB_GRP_NEED_INIT(grp)) { |
| 2020 | /* |
| 2021 | * we need full data about the group |
| 2022 | * to make a good selection |
| 2023 | */ |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 2024 | err = ext4_mb_init_group(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2025 | if (err) |
| 2026 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | /* |
| 2030 | * If the particular group doesn't satisfy our |
| 2031 | * criteria we continue with the next group |
| 2032 | */ |
| 2033 | if (!ext4_mb_good_group(ac, group, cr)) |
| 2034 | continue; |
| 2035 | |
| 2036 | err = ext4_mb_load_buddy(sb, group, &e4b); |
| 2037 | if (err) |
| 2038 | goto out; |
| 2039 | |
| 2040 | ext4_lock_group(sb, group); |
| 2041 | if (!ext4_mb_good_group(ac, group, cr)) { |
| 2042 | /* someone did allocation from this group */ |
| 2043 | ext4_unlock_group(sb, group); |
| 2044 | ext4_mb_release_desc(&e4b); |
| 2045 | continue; |
| 2046 | } |
| 2047 | |
| 2048 | ac->ac_groups_scanned++; |
| 2049 | desc = ext4_get_group_desc(sb, group, NULL); |
Theodore Ts'o | 75507ef | 2009-05-01 12:58:36 -0400 | [diff] [blame] | 2050 | if (cr == 0) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2051 | ext4_mb_simple_scan_group(ac, &e4b); |
| 2052 | else if (cr == 1 && |
| 2053 | ac->ac_g_ex.fe_len == sbi->s_stripe) |
| 2054 | ext4_mb_scan_aligned(ac, &e4b); |
| 2055 | else |
| 2056 | ext4_mb_complex_scan_group(ac, &e4b); |
| 2057 | |
| 2058 | ext4_unlock_group(sb, group); |
| 2059 | ext4_mb_release_desc(&e4b); |
| 2060 | |
| 2061 | if (ac->ac_status != AC_STATUS_CONTINUE) |
| 2062 | break; |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND && |
| 2067 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 2068 | /* |
| 2069 | * We've been searching too long. Let's try to allocate |
| 2070 | * the best chunk we've found so far |
| 2071 | */ |
| 2072 | |
| 2073 | ext4_mb_try_best_found(ac, &e4b); |
| 2074 | if (ac->ac_status != AC_STATUS_FOUND) { |
| 2075 | /* |
| 2076 | * Someone more lucky has already allocated it. |
| 2077 | * The only thing we can do is just take first |
| 2078 | * found block(s) |
| 2079 | printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n"); |
| 2080 | */ |
| 2081 | ac->ac_b_ex.fe_group = 0; |
| 2082 | ac->ac_b_ex.fe_start = 0; |
| 2083 | ac->ac_b_ex.fe_len = 0; |
| 2084 | ac->ac_status = AC_STATUS_CONTINUE; |
| 2085 | ac->ac_flags |= EXT4_MB_HINT_FIRST; |
| 2086 | cr = 3; |
| 2087 | atomic_inc(&sbi->s_mb_lost_chunks); |
| 2088 | goto repeat; |
| 2089 | } |
| 2090 | } |
| 2091 | out: |
| 2092 | return err; |
| 2093 | } |
| 2094 | |
| 2095 | #ifdef EXT4_MB_HISTORY |
| 2096 | struct ext4_mb_proc_session { |
| 2097 | struct ext4_mb_history *history; |
| 2098 | struct super_block *sb; |
| 2099 | int start; |
| 2100 | int max; |
| 2101 | }; |
| 2102 | |
| 2103 | static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s, |
| 2104 | struct ext4_mb_history *hs, |
| 2105 | int first) |
| 2106 | { |
| 2107 | if (hs == s->history + s->max) |
| 2108 | hs = s->history; |
| 2109 | if (!first && hs == s->history + s->start) |
| 2110 | return NULL; |
| 2111 | while (hs->orig.fe_len == 0) { |
| 2112 | hs++; |
| 2113 | if (hs == s->history + s->max) |
| 2114 | hs = s->history; |
| 2115 | if (hs == s->history + s->start) |
| 2116 | return NULL; |
| 2117 | } |
| 2118 | return hs; |
| 2119 | } |
| 2120 | |
| 2121 | static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos) |
| 2122 | { |
| 2123 | struct ext4_mb_proc_session *s = seq->private; |
| 2124 | struct ext4_mb_history *hs; |
| 2125 | int l = *pos; |
| 2126 | |
| 2127 | if (l == 0) |
| 2128 | return SEQ_START_TOKEN; |
| 2129 | hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1); |
| 2130 | if (!hs) |
| 2131 | return NULL; |
| 2132 | while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL); |
| 2133 | return hs; |
| 2134 | } |
| 2135 | |
| 2136 | static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v, |
| 2137 | loff_t *pos) |
| 2138 | { |
| 2139 | struct ext4_mb_proc_session *s = seq->private; |
| 2140 | struct ext4_mb_history *hs = v; |
| 2141 | |
| 2142 | ++*pos; |
| 2143 | if (v == SEQ_START_TOKEN) |
| 2144 | return ext4_mb_history_skip_empty(s, s->history + s->start, 1); |
| 2145 | else |
| 2146 | return ext4_mb_history_skip_empty(s, ++hs, 0); |
| 2147 | } |
| 2148 | |
| 2149 | static int ext4_mb_seq_history_show(struct seq_file *seq, void *v) |
| 2150 | { |
| 2151 | char buf[25], buf2[25], buf3[25], *fmt; |
| 2152 | struct ext4_mb_history *hs = v; |
| 2153 | |
| 2154 | if (v == SEQ_START_TOKEN) { |
| 2155 | seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s " |
Theodore Ts'o | 0ef90db | 2009-08-09 16:46:13 -0400 | [diff] [blame] | 2156 | "%-5s %-2s %-6s %-5s %-5s %-6s\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2157 | "pid", "inode", "original", "goal", "result", "found", |
| 2158 | "grps", "cr", "flags", "merge", "tail", "broken"); |
| 2159 | return 0; |
| 2160 | } |
| 2161 | |
| 2162 | if (hs->op == EXT4_MB_HISTORY_ALLOC) { |
| 2163 | fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u " |
Theodore Ts'o | 0ef90db | 2009-08-09 16:46:13 -0400 | [diff] [blame] | 2164 | "0x%04x %-5s %-5u %-6u\n"; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2165 | sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2166 | hs->result.fe_start, hs->result.fe_len, |
| 2167 | hs->result.fe_logical); |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2168 | sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2169 | hs->orig.fe_start, hs->orig.fe_len, |
| 2170 | hs->orig.fe_logical); |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2171 | sprintf(buf3, "%u/%d/%u@%u", hs->goal.fe_group, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2172 | hs->goal.fe_start, hs->goal.fe_len, |
| 2173 | hs->goal.fe_logical); |
| 2174 | seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2, |
| 2175 | hs->found, hs->groups, hs->cr, hs->flags, |
| 2176 | hs->merged ? "M" : "", hs->tail, |
| 2177 | hs->buddy ? 1 << hs->buddy : 0); |
| 2178 | } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) { |
| 2179 | fmt = "%-5u %-8u %-23s %-23s %-23s\n"; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2180 | sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2181 | hs->result.fe_start, hs->result.fe_len, |
| 2182 | hs->result.fe_logical); |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2183 | sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2184 | hs->orig.fe_start, hs->orig.fe_len, |
| 2185 | hs->orig.fe_logical); |
| 2186 | seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2); |
| 2187 | } else if (hs->op == EXT4_MB_HISTORY_DISCARD) { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2188 | sprintf(buf2, "%u/%d/%u", hs->result.fe_group, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2189 | hs->result.fe_start, hs->result.fe_len); |
| 2190 | seq_printf(seq, "%-5u %-8u %-23s discard\n", |
| 2191 | hs->pid, hs->ino, buf2); |
| 2192 | } else if (hs->op == EXT4_MB_HISTORY_FREE) { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2193 | sprintf(buf2, "%u/%d/%u", hs->result.fe_group, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2194 | hs->result.fe_start, hs->result.fe_len); |
| 2195 | seq_printf(seq, "%-5u %-8u %-23s free\n", |
| 2196 | hs->pid, hs->ino, buf2); |
| 2197 | } |
| 2198 | return 0; |
| 2199 | } |
| 2200 | |
| 2201 | static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v) |
| 2202 | { |
| 2203 | } |
| 2204 | |
| 2205 | static struct seq_operations ext4_mb_seq_history_ops = { |
| 2206 | .start = ext4_mb_seq_history_start, |
| 2207 | .next = ext4_mb_seq_history_next, |
| 2208 | .stop = ext4_mb_seq_history_stop, |
| 2209 | .show = ext4_mb_seq_history_show, |
| 2210 | }; |
| 2211 | |
| 2212 | static int ext4_mb_seq_history_open(struct inode *inode, struct file *file) |
| 2213 | { |
| 2214 | struct super_block *sb = PDE(inode)->data; |
| 2215 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2216 | struct ext4_mb_proc_session *s; |
| 2217 | int rc; |
| 2218 | int size; |
| 2219 | |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2220 | if (unlikely(sbi->s_mb_history == NULL)) |
| 2221 | return -ENOMEM; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2222 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 2223 | if (s == NULL) |
| 2224 | return -ENOMEM; |
| 2225 | s->sb = sb; |
| 2226 | size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max; |
| 2227 | s->history = kmalloc(size, GFP_KERNEL); |
| 2228 | if (s->history == NULL) { |
| 2229 | kfree(s); |
| 2230 | return -ENOMEM; |
| 2231 | } |
| 2232 | |
| 2233 | spin_lock(&sbi->s_mb_history_lock); |
| 2234 | memcpy(s->history, sbi->s_mb_history, size); |
| 2235 | s->max = sbi->s_mb_history_max; |
| 2236 | s->start = sbi->s_mb_history_cur % s->max; |
| 2237 | spin_unlock(&sbi->s_mb_history_lock); |
| 2238 | |
| 2239 | rc = seq_open(file, &ext4_mb_seq_history_ops); |
| 2240 | if (rc == 0) { |
| 2241 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 2242 | m->private = s; |
| 2243 | } else { |
| 2244 | kfree(s->history); |
| 2245 | kfree(s); |
| 2246 | } |
| 2247 | return rc; |
| 2248 | |
| 2249 | } |
| 2250 | |
| 2251 | static int ext4_mb_seq_history_release(struct inode *inode, struct file *file) |
| 2252 | { |
| 2253 | struct seq_file *seq = (struct seq_file *)file->private_data; |
| 2254 | struct ext4_mb_proc_session *s = seq->private; |
| 2255 | kfree(s->history); |
| 2256 | kfree(s); |
| 2257 | return seq_release(inode, file); |
| 2258 | } |
| 2259 | |
| 2260 | static ssize_t ext4_mb_seq_history_write(struct file *file, |
| 2261 | const char __user *buffer, |
| 2262 | size_t count, loff_t *ppos) |
| 2263 | { |
| 2264 | struct seq_file *seq = (struct seq_file *)file->private_data; |
| 2265 | struct ext4_mb_proc_session *s = seq->private; |
| 2266 | struct super_block *sb = s->sb; |
| 2267 | char str[32]; |
| 2268 | int value; |
| 2269 | |
| 2270 | if (count >= sizeof(str)) { |
| 2271 | printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n", |
| 2272 | "mb_history", (int)sizeof(str)); |
| 2273 | return -EOVERFLOW; |
| 2274 | } |
| 2275 | |
| 2276 | if (copy_from_user(str, buffer, count)) |
| 2277 | return -EFAULT; |
| 2278 | |
| 2279 | value = simple_strtol(str, NULL, 0); |
| 2280 | if (value < 0) |
| 2281 | return -ERANGE; |
| 2282 | EXT4_SB(sb)->s_mb_history_filter = value; |
| 2283 | |
| 2284 | return count; |
| 2285 | } |
| 2286 | |
| 2287 | static struct file_operations ext4_mb_seq_history_fops = { |
| 2288 | .owner = THIS_MODULE, |
| 2289 | .open = ext4_mb_seq_history_open, |
| 2290 | .read = seq_read, |
| 2291 | .write = ext4_mb_seq_history_write, |
| 2292 | .llseek = seq_lseek, |
| 2293 | .release = ext4_mb_seq_history_release, |
| 2294 | }; |
| 2295 | |
| 2296 | static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos) |
| 2297 | { |
| 2298 | struct super_block *sb = seq->private; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2299 | ext4_group_t group; |
| 2300 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2301 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2302 | return NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2303 | group = *pos + 1; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2304 | return (void *) ((unsigned long) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2308 | { |
| 2309 | struct super_block *sb = seq->private; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2310 | ext4_group_t group; |
| 2311 | |
| 2312 | ++*pos; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2313 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2314 | return NULL; |
| 2315 | group = *pos + 1; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2316 | return (void *) ((unsigned long) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v) |
| 2320 | { |
| 2321 | struct super_block *sb = seq->private; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2322 | ext4_group_t group = (ext4_group_t) ((unsigned long) v); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2323 | int i; |
| 2324 | int err; |
| 2325 | struct ext4_buddy e4b; |
| 2326 | struct sg { |
| 2327 | struct ext4_group_info info; |
| 2328 | unsigned short counters[16]; |
| 2329 | } sg; |
| 2330 | |
| 2331 | group--; |
| 2332 | if (group == 0) |
| 2333 | seq_printf(seq, "#%-5s: %-5s %-5s %-5s " |
| 2334 | "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s " |
| 2335 | "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n", |
| 2336 | "group", "free", "frags", "first", |
| 2337 | "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6", |
| 2338 | "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13"); |
| 2339 | |
| 2340 | i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) + |
| 2341 | sizeof(struct ext4_group_info); |
| 2342 | err = ext4_mb_load_buddy(sb, group, &e4b); |
| 2343 | if (err) { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2344 | seq_printf(seq, "#%-5u: I/O error\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2345 | return 0; |
| 2346 | } |
| 2347 | ext4_lock_group(sb, group); |
| 2348 | memcpy(&sg, ext4_get_group_info(sb, group), i); |
| 2349 | ext4_unlock_group(sb, group); |
| 2350 | ext4_mb_release_desc(&e4b); |
| 2351 | |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2352 | seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2353 | sg.info.bb_fragments, sg.info.bb_first_free); |
| 2354 | for (i = 0; i <= 13; i++) |
| 2355 | seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ? |
| 2356 | sg.info.bb_counters[i] : 0); |
| 2357 | seq_printf(seq, " ]\n"); |
| 2358 | |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
| 2362 | static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v) |
| 2363 | { |
| 2364 | } |
| 2365 | |
| 2366 | static struct seq_operations ext4_mb_seq_groups_ops = { |
| 2367 | .start = ext4_mb_seq_groups_start, |
| 2368 | .next = ext4_mb_seq_groups_next, |
| 2369 | .stop = ext4_mb_seq_groups_stop, |
| 2370 | .show = ext4_mb_seq_groups_show, |
| 2371 | }; |
| 2372 | |
| 2373 | static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file) |
| 2374 | { |
| 2375 | struct super_block *sb = PDE(inode)->data; |
| 2376 | int rc; |
| 2377 | |
| 2378 | rc = seq_open(file, &ext4_mb_seq_groups_ops); |
| 2379 | if (rc == 0) { |
| 2380 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 2381 | m->private = sb; |
| 2382 | } |
| 2383 | return rc; |
| 2384 | |
| 2385 | } |
| 2386 | |
| 2387 | static struct file_operations ext4_mb_seq_groups_fops = { |
| 2388 | .owner = THIS_MODULE, |
| 2389 | .open = ext4_mb_seq_groups_open, |
| 2390 | .read = seq_read, |
| 2391 | .llseek = seq_lseek, |
| 2392 | .release = seq_release, |
| 2393 | }; |
| 2394 | |
| 2395 | static void ext4_mb_history_release(struct super_block *sb) |
| 2396 | { |
| 2397 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2398 | |
Theodore Ts'o | 9f6200b | 2008-09-23 09:18:24 -0400 | [diff] [blame] | 2399 | if (sbi->s_proc != NULL) { |
| 2400 | remove_proc_entry("mb_groups", sbi->s_proc); |
Curt Wohlgemuth | f403390 | 2009-05-01 20:27:20 -0400 | [diff] [blame] | 2401 | if (sbi->s_mb_history_max) |
| 2402 | remove_proc_entry("mb_history", sbi->s_proc); |
Theodore Ts'o | 9f6200b | 2008-09-23 09:18:24 -0400 | [diff] [blame] | 2403 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2404 | kfree(sbi->s_mb_history); |
| 2405 | } |
| 2406 | |
| 2407 | static void ext4_mb_history_init(struct super_block *sb) |
| 2408 | { |
| 2409 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2410 | int i; |
| 2411 | |
Theodore Ts'o | 9f6200b | 2008-09-23 09:18:24 -0400 | [diff] [blame] | 2412 | if (sbi->s_proc != NULL) { |
Curt Wohlgemuth | f403390 | 2009-05-01 20:27:20 -0400 | [diff] [blame] | 2413 | if (sbi->s_mb_history_max) |
| 2414 | proc_create_data("mb_history", S_IRUGO, sbi->s_proc, |
| 2415 | &ext4_mb_seq_history_fops, sb); |
Theodore Ts'o | 9f6200b | 2008-09-23 09:18:24 -0400 | [diff] [blame] | 2416 | proc_create_data("mb_groups", S_IRUGO, sbi->s_proc, |
Denis V. Lunev | 46fe74f | 2008-04-29 01:02:08 -0700 | [diff] [blame] | 2417 | &ext4_mb_seq_groups_fops, sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2418 | } |
| 2419 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2420 | sbi->s_mb_history_cur = 0; |
| 2421 | spin_lock_init(&sbi->s_mb_history_lock); |
| 2422 | i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history); |
Curt Wohlgemuth | f403390 | 2009-05-01 20:27:20 -0400 | [diff] [blame] | 2423 | sbi->s_mb_history = i ? kzalloc(i, GFP_KERNEL) : NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2424 | /* if we can't allocate history, then we simple won't use it */ |
| 2425 | } |
| 2426 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2427 | static noinline_for_stack void |
| 2428 | ext4_mb_store_history(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2429 | { |
| 2430 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 2431 | struct ext4_mb_history h; |
| 2432 | |
Curt Wohlgemuth | f403390 | 2009-05-01 20:27:20 -0400 | [diff] [blame] | 2433 | if (sbi->s_mb_history == NULL) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2434 | return; |
| 2435 | |
| 2436 | if (!(ac->ac_op & sbi->s_mb_history_filter)) |
| 2437 | return; |
| 2438 | |
| 2439 | h.op = ac->ac_op; |
| 2440 | h.pid = current->pid; |
| 2441 | h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0; |
| 2442 | h.orig = ac->ac_o_ex; |
| 2443 | h.result = ac->ac_b_ex; |
| 2444 | h.flags = ac->ac_flags; |
| 2445 | h.found = ac->ac_found; |
| 2446 | h.groups = ac->ac_groups_scanned; |
| 2447 | h.cr = ac->ac_criteria; |
| 2448 | h.tail = ac->ac_tail; |
| 2449 | h.buddy = ac->ac_buddy; |
| 2450 | h.merged = 0; |
| 2451 | if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) { |
| 2452 | if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start && |
| 2453 | ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group) |
| 2454 | h.merged = 1; |
| 2455 | h.goal = ac->ac_g_ex; |
| 2456 | h.result = ac->ac_f_ex; |
| 2457 | } |
| 2458 | |
| 2459 | spin_lock(&sbi->s_mb_history_lock); |
| 2460 | memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h)); |
| 2461 | if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max) |
| 2462 | sbi->s_mb_history_cur = 0; |
| 2463 | spin_unlock(&sbi->s_mb_history_lock); |
| 2464 | } |
| 2465 | |
| 2466 | #else |
| 2467 | #define ext4_mb_history_release(sb) |
| 2468 | #define ext4_mb_history_init(sb) |
| 2469 | #endif |
| 2470 | |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2471 | |
| 2472 | /* Create and initialize ext4_group_info data for the given group. */ |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 2473 | int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group, |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2474 | struct ext4_group_desc *desc) |
| 2475 | { |
| 2476 | int i, len; |
| 2477 | int metalen = 0; |
| 2478 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2479 | struct ext4_group_info **meta_group_info; |
| 2480 | |
| 2481 | /* |
| 2482 | * First check if this group is the first of a reserved block. |
| 2483 | * If it's true, we have to allocate a new table of pointers |
| 2484 | * to ext4_group_info structures |
| 2485 | */ |
| 2486 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { |
| 2487 | metalen = sizeof(*meta_group_info) << |
| 2488 | EXT4_DESC_PER_BLOCK_BITS(sb); |
| 2489 | meta_group_info = kmalloc(metalen, GFP_KERNEL); |
| 2490 | if (meta_group_info == NULL) { |
| 2491 | printk(KERN_ERR "EXT4-fs: can't allocate mem for a " |
| 2492 | "buddy group\n"); |
| 2493 | goto exit_meta_group_info; |
| 2494 | } |
| 2495 | sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = |
| 2496 | meta_group_info; |
| 2497 | } |
| 2498 | |
| 2499 | /* |
| 2500 | * calculate needed size. if change bb_counters size, |
| 2501 | * don't forget about ext4_mb_generate_buddy() |
| 2502 | */ |
| 2503 | len = offsetof(typeof(**meta_group_info), |
| 2504 | bb_counters[sb->s_blocksize_bits + 2]); |
| 2505 | |
| 2506 | meta_group_info = |
| 2507 | sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]; |
| 2508 | i = group & (EXT4_DESC_PER_BLOCK(sb) - 1); |
| 2509 | |
| 2510 | meta_group_info[i] = kzalloc(len, GFP_KERNEL); |
| 2511 | if (meta_group_info[i] == NULL) { |
| 2512 | printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n"); |
| 2513 | goto exit_group_info; |
| 2514 | } |
| 2515 | set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, |
| 2516 | &(meta_group_info[i]->bb_state)); |
| 2517 | |
| 2518 | /* |
| 2519 | * initialize bb_free to be able to skip |
| 2520 | * empty groups without initialization |
| 2521 | */ |
| 2522 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 2523 | meta_group_info[i]->bb_free = |
| 2524 | ext4_free_blocks_after_init(sb, group, desc); |
| 2525 | } else { |
| 2526 | meta_group_info[i]->bb_free = |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 2527 | ext4_free_blks_count(sb, desc); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 2531 | init_rwsem(&meta_group_info[i]->alloc_sem); |
Joe Perches | 5a4a798 | 2009-07-05 22:33:08 -0400 | [diff] [blame] | 2532 | meta_group_info[i]->bb_free_root.rb_node = NULL; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2533 | |
| 2534 | #ifdef DOUBLE_CHECK |
| 2535 | { |
| 2536 | struct buffer_head *bh; |
| 2537 | meta_group_info[i]->bb_bitmap = |
| 2538 | kmalloc(sb->s_blocksize, GFP_KERNEL); |
| 2539 | BUG_ON(meta_group_info[i]->bb_bitmap == NULL); |
| 2540 | bh = ext4_read_block_bitmap(sb, group); |
| 2541 | BUG_ON(bh == NULL); |
| 2542 | memcpy(meta_group_info[i]->bb_bitmap, bh->b_data, |
| 2543 | sb->s_blocksize); |
| 2544 | put_bh(bh); |
| 2545 | } |
| 2546 | #endif |
| 2547 | |
| 2548 | return 0; |
| 2549 | |
| 2550 | exit_group_info: |
| 2551 | /* If a meta_group_info table has been allocated, release it now */ |
| 2552 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) |
| 2553 | kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]); |
| 2554 | exit_meta_group_info: |
| 2555 | return -ENOMEM; |
| 2556 | } /* ext4_mb_add_groupinfo */ |
| 2557 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2558 | static int ext4_mb_init_backend(struct super_block *sb) |
| 2559 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2560 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2561 | ext4_group_t i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2562 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2563 | struct ext4_super_block *es = sbi->s_es; |
| 2564 | int num_meta_group_infos; |
| 2565 | int num_meta_group_infos_max; |
| 2566 | int array_size; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2567 | struct ext4_group_desc *desc; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2568 | |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2569 | /* This is the number of blocks used by GDT */ |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2570 | num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) - |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2571 | 1) >> EXT4_DESC_PER_BLOCK_BITS(sb); |
| 2572 | |
| 2573 | /* |
| 2574 | * This is the total number of blocks used by GDT including |
| 2575 | * the number of reserved blocks for GDT. |
| 2576 | * The s_group_info array is allocated with this value |
| 2577 | * to allow a clean online resize without a complex |
| 2578 | * manipulation of pointer. |
| 2579 | * The drawback is the unused memory when no resize |
| 2580 | * occurs but it's very low in terms of pages |
| 2581 | * (see comments below) |
| 2582 | * Need to handle this properly when META_BG resizing is allowed |
| 2583 | */ |
| 2584 | num_meta_group_infos_max = num_meta_group_infos + |
| 2585 | le16_to_cpu(es->s_reserved_gdt_blocks); |
| 2586 | |
| 2587 | /* |
| 2588 | * array_size is the size of s_group_info array. We round it |
| 2589 | * to the next power of two because this approximation is done |
| 2590 | * internally by kmalloc so we can have some more memory |
| 2591 | * for free here (e.g. may be used for META_BG resize). |
| 2592 | */ |
| 2593 | array_size = 1; |
| 2594 | while (array_size < sizeof(*sbi->s_group_info) * |
| 2595 | num_meta_group_infos_max) |
| 2596 | array_size = array_size << 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2597 | /* An 8TB filesystem with 64-bit pointers requires a 4096 byte |
| 2598 | * kmalloc. A 128kb malloc should suffice for a 256TB filesystem. |
| 2599 | * So a two level scheme suffices for now. */ |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2600 | sbi->s_group_info = kmalloc(array_size, GFP_KERNEL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2601 | if (sbi->s_group_info == NULL) { |
| 2602 | printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n"); |
| 2603 | return -ENOMEM; |
| 2604 | } |
| 2605 | sbi->s_buddy_cache = new_inode(sb); |
| 2606 | if (sbi->s_buddy_cache == NULL) { |
| 2607 | printk(KERN_ERR "EXT4-fs: can't get new inode\n"); |
| 2608 | goto err_freesgi; |
| 2609 | } |
| 2610 | EXT4_I(sbi->s_buddy_cache)->i_disksize = 0; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2611 | for (i = 0; i < ngroups; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2612 | desc = ext4_get_group_desc(sb, i, NULL); |
| 2613 | if (desc == NULL) { |
| 2614 | printk(KERN_ERR |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2615 | "EXT4-fs: can't read descriptor %u\n", i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2616 | goto err_freebuddy; |
| 2617 | } |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2618 | if (ext4_mb_add_groupinfo(sb, i, desc) != 0) |
| 2619 | goto err_freebuddy; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | return 0; |
| 2623 | |
| 2624 | err_freebuddy: |
Roel Kluin | f1fa334 | 2008-04-29 22:01:15 -0400 | [diff] [blame] | 2625 | while (i-- > 0) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2626 | kfree(ext4_get_group_info(sb, i)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2627 | i = num_meta_group_infos; |
Roel Kluin | f1fa334 | 2008-04-29 22:01:15 -0400 | [diff] [blame] | 2628 | while (i-- > 0) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2629 | kfree(sbi->s_group_info[i]); |
| 2630 | iput(sbi->s_buddy_cache); |
| 2631 | err_freesgi: |
| 2632 | kfree(sbi->s_group_info); |
| 2633 | return -ENOMEM; |
| 2634 | } |
| 2635 | |
| 2636 | int ext4_mb_init(struct super_block *sb, int needs_recovery) |
| 2637 | { |
| 2638 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 2639 | unsigned i, j; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2640 | unsigned offset; |
| 2641 | unsigned max; |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2642 | int ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2643 | |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame^] | 2644 | i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2645 | |
| 2646 | sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL); |
| 2647 | if (sbi->s_mb_offsets == NULL) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2648 | return -ENOMEM; |
| 2649 | } |
Yasunori Goto | ff7ef32 | 2008-12-17 00:48:39 -0500 | [diff] [blame] | 2650 | |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame^] | 2651 | i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2652 | sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL); |
| 2653 | if (sbi->s_mb_maxs == NULL) { |
Dan Carpenter | a7b1944 | 2009-03-27 19:42:54 -0400 | [diff] [blame] | 2654 | kfree(sbi->s_mb_offsets); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2655 | return -ENOMEM; |
| 2656 | } |
| 2657 | |
| 2658 | /* order 0 is regular bitmap */ |
| 2659 | sbi->s_mb_maxs[0] = sb->s_blocksize << 3; |
| 2660 | sbi->s_mb_offsets[0] = 0; |
| 2661 | |
| 2662 | i = 1; |
| 2663 | offset = 0; |
| 2664 | max = sb->s_blocksize << 2; |
| 2665 | do { |
| 2666 | sbi->s_mb_offsets[i] = offset; |
| 2667 | sbi->s_mb_maxs[i] = max; |
| 2668 | offset += 1 << (sb->s_blocksize_bits - i); |
| 2669 | max = max >> 1; |
| 2670 | i++; |
| 2671 | } while (i <= sb->s_blocksize_bits + 1); |
| 2672 | |
| 2673 | /* init file for buddy data */ |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2674 | ret = ext4_mb_init_backend(sb); |
| 2675 | if (ret != 0) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2676 | kfree(sbi->s_mb_offsets); |
| 2677 | kfree(sbi->s_mb_maxs); |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2678 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | spin_lock_init(&sbi->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2682 | spin_lock_init(&sbi->s_bal_lock); |
| 2683 | |
| 2684 | sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN; |
| 2685 | sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN; |
| 2686 | sbi->s_mb_stats = MB_DEFAULT_STATS; |
| 2687 | sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD; |
| 2688 | sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS; |
| 2689 | sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT; |
| 2690 | sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC; |
| 2691 | |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2692 | sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2693 | if (sbi->s_locality_groups == NULL) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2694 | kfree(sbi->s_mb_offsets); |
| 2695 | kfree(sbi->s_mb_maxs); |
| 2696 | return -ENOMEM; |
| 2697 | } |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2698 | for_each_possible_cpu(i) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2699 | struct ext4_locality_group *lg; |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2700 | lg = per_cpu_ptr(sbi->s_locality_groups, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2701 | mutex_init(&lg->lg_mutex); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 2702 | for (j = 0; j < PREALLOC_TB_SIZE; j++) |
| 2703 | INIT_LIST_HEAD(&lg->lg_prealloc_list[j]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2704 | spin_lock_init(&lg->lg_prealloc_lock); |
| 2705 | } |
| 2706 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2707 | ext4_mb_history_init(sb); |
| 2708 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2709 | if (sbi->s_journal) |
| 2710 | sbi->s_journal->j_commit_callback = release_blocks_on_commit; |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2711 | |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2712 | printk(KERN_INFO "EXT4-fs: mballoc enabled\n"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2713 | return 0; |
| 2714 | } |
| 2715 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 2716 | /* need to called with the ext4 group lock held */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2717 | static void ext4_mb_cleanup_pa(struct ext4_group_info *grp) |
| 2718 | { |
| 2719 | struct ext4_prealloc_space *pa; |
| 2720 | struct list_head *cur, *tmp; |
| 2721 | int count = 0; |
| 2722 | |
| 2723 | list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) { |
| 2724 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 2725 | list_del(&pa->pa_group_list); |
| 2726 | count++; |
Aneesh Kumar K.V | 688f05a | 2008-10-13 12:14:14 -0400 | [diff] [blame] | 2727 | kmem_cache_free(ext4_pspace_cachep, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2728 | } |
| 2729 | if (count) |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2730 | mb_debug(1, "mballoc: %u PAs left\n", count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2731 | |
| 2732 | } |
| 2733 | |
| 2734 | int ext4_mb_release(struct super_block *sb) |
| 2735 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2736 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2737 | ext4_group_t i; |
| 2738 | int num_meta_group_infos; |
| 2739 | struct ext4_group_info *grinfo; |
| 2740 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2741 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2742 | if (sbi->s_group_info) { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2743 | for (i = 0; i < ngroups; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2744 | grinfo = ext4_get_group_info(sb, i); |
| 2745 | #ifdef DOUBLE_CHECK |
| 2746 | kfree(grinfo->bb_bitmap); |
| 2747 | #endif |
| 2748 | ext4_lock_group(sb, i); |
| 2749 | ext4_mb_cleanup_pa(grinfo); |
| 2750 | ext4_unlock_group(sb, i); |
| 2751 | kfree(grinfo); |
| 2752 | } |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2753 | num_meta_group_infos = (ngroups + |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2754 | EXT4_DESC_PER_BLOCK(sb) - 1) >> |
| 2755 | EXT4_DESC_PER_BLOCK_BITS(sb); |
| 2756 | for (i = 0; i < num_meta_group_infos; i++) |
| 2757 | kfree(sbi->s_group_info[i]); |
| 2758 | kfree(sbi->s_group_info); |
| 2759 | } |
| 2760 | kfree(sbi->s_mb_offsets); |
| 2761 | kfree(sbi->s_mb_maxs); |
| 2762 | if (sbi->s_buddy_cache) |
| 2763 | iput(sbi->s_buddy_cache); |
| 2764 | if (sbi->s_mb_stats) { |
| 2765 | printk(KERN_INFO |
| 2766 | "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n", |
| 2767 | atomic_read(&sbi->s_bal_allocated), |
| 2768 | atomic_read(&sbi->s_bal_reqs), |
| 2769 | atomic_read(&sbi->s_bal_success)); |
| 2770 | printk(KERN_INFO |
| 2771 | "EXT4-fs: mballoc: %u extents scanned, %u goal hits, " |
| 2772 | "%u 2^N hits, %u breaks, %u lost\n", |
| 2773 | atomic_read(&sbi->s_bal_ex_scanned), |
| 2774 | atomic_read(&sbi->s_bal_goals), |
| 2775 | atomic_read(&sbi->s_bal_2orders), |
| 2776 | atomic_read(&sbi->s_bal_breaks), |
| 2777 | atomic_read(&sbi->s_mb_lost_chunks)); |
| 2778 | printk(KERN_INFO |
| 2779 | "EXT4-fs: mballoc: %lu generated and it took %Lu\n", |
| 2780 | sbi->s_mb_buddies_generated++, |
| 2781 | sbi->s_mb_generation_time); |
| 2782 | printk(KERN_INFO |
| 2783 | "EXT4-fs: mballoc: %u preallocated, %u discarded\n", |
| 2784 | atomic_read(&sbi->s_mb_preallocated), |
| 2785 | atomic_read(&sbi->s_mb_discarded)); |
| 2786 | } |
| 2787 | |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2788 | free_percpu(sbi->s_locality_groups); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2789 | ext4_mb_history_release(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2790 | |
| 2791 | return 0; |
| 2792 | } |
| 2793 | |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2794 | /* |
| 2795 | * This function is called by the jbd2 layer once the commit has finished, |
| 2796 | * so we know we can free the blocks that were released with that commit. |
| 2797 | */ |
| 2798 | static void release_blocks_on_commit(journal_t *journal, transaction_t *txn) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2799 | { |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2800 | struct super_block *sb = journal->j_private; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2801 | struct ext4_buddy e4b; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2802 | struct ext4_group_info *db; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2803 | int err, count = 0, count2 = 0; |
| 2804 | struct ext4_free_data *entry; |
Theodore Ts'o | 8a0aba7 | 2008-10-16 10:06:27 -0400 | [diff] [blame] | 2805 | ext4_fsblk_t discard_block; |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2806 | struct list_head *l, *ltmp; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2807 | |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2808 | list_for_each_safe(l, ltmp, &txn->t_private_list) { |
| 2809 | entry = list_entry(l, struct ext4_free_data, list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2810 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2811 | mb_debug(1, "gonna free %u blocks in group %u (0x%p):", |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2812 | entry->count, entry->group, entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2813 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2814 | err = ext4_mb_load_buddy(sb, entry->group, &e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2815 | /* we expect to find existing buddy because it's pinned */ |
| 2816 | BUG_ON(err != 0); |
| 2817 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2818 | db = e4b.bd_info; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2819 | /* there are blocks to put in buddy to make them really free */ |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2820 | count += entry->count; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2821 | count2++; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2822 | ext4_lock_group(sb, entry->group); |
| 2823 | /* Take it out of per group rb tree */ |
| 2824 | rb_erase(&entry->node, &(db->bb_free_root)); |
| 2825 | mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count); |
| 2826 | |
| 2827 | if (!db->bb_free_root.rb_node) { |
| 2828 | /* No more items in the per group rb tree |
| 2829 | * balance refcounts from ext4_mb_free_metadata() |
| 2830 | */ |
| 2831 | page_cache_release(e4b.bd_buddy_page); |
| 2832 | page_cache_release(e4b.bd_bitmap_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2833 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2834 | ext4_unlock_group(sb, entry->group); |
Theodore Ts'o | 8a0aba7 | 2008-10-16 10:06:27 -0400 | [diff] [blame] | 2835 | discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb) |
| 2836 | + entry->start_blk |
| 2837 | + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 2838 | trace_ext4_discard_blocks(sb, (unsigned long long)discard_block, |
| 2839 | entry->count); |
Theodore Ts'o | 8a0aba7 | 2008-10-16 10:06:27 -0400 | [diff] [blame] | 2840 | sb_issue_discard(sb, discard_block, entry->count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2841 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2842 | kmem_cache_free(ext4_free_ext_cachep, entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2843 | ext4_mb_release_desc(&e4b); |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2844 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2845 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2846 | mb_debug(1, "freed %u blocks in %u structures\n", count, count2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2847 | } |
| 2848 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2849 | #ifdef CONFIG_EXT4_DEBUG |
| 2850 | u8 mb_enable_debug __read_mostly; |
| 2851 | |
| 2852 | static struct dentry *debugfs_dir; |
| 2853 | static struct dentry *debugfs_debug; |
| 2854 | |
| 2855 | static void __init ext4_create_debugfs_entry(void) |
| 2856 | { |
| 2857 | debugfs_dir = debugfs_create_dir("ext4", NULL); |
| 2858 | if (debugfs_dir) |
| 2859 | debugfs_debug = debugfs_create_u8("mballoc-debug", |
| 2860 | S_IRUGO | S_IWUSR, |
| 2861 | debugfs_dir, |
| 2862 | &mb_enable_debug); |
| 2863 | } |
| 2864 | |
| 2865 | static void ext4_remove_debugfs_entry(void) |
| 2866 | { |
| 2867 | debugfs_remove(debugfs_debug); |
| 2868 | debugfs_remove(debugfs_dir); |
| 2869 | } |
| 2870 | |
| 2871 | #else |
| 2872 | |
| 2873 | static void __init ext4_create_debugfs_entry(void) |
| 2874 | { |
| 2875 | } |
| 2876 | |
| 2877 | static void ext4_remove_debugfs_entry(void) |
| 2878 | { |
| 2879 | } |
| 2880 | |
| 2881 | #endif |
| 2882 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2883 | int __init init_ext4_mballoc(void) |
| 2884 | { |
| 2885 | ext4_pspace_cachep = |
| 2886 | kmem_cache_create("ext4_prealloc_space", |
| 2887 | sizeof(struct ext4_prealloc_space), |
| 2888 | 0, SLAB_RECLAIM_ACCOUNT, NULL); |
| 2889 | if (ext4_pspace_cachep == NULL) |
| 2890 | return -ENOMEM; |
| 2891 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 2892 | ext4_ac_cachep = |
| 2893 | kmem_cache_create("ext4_alloc_context", |
| 2894 | sizeof(struct ext4_allocation_context), |
| 2895 | 0, SLAB_RECLAIM_ACCOUNT, NULL); |
| 2896 | if (ext4_ac_cachep == NULL) { |
| 2897 | kmem_cache_destroy(ext4_pspace_cachep); |
| 2898 | return -ENOMEM; |
| 2899 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2900 | |
| 2901 | ext4_free_ext_cachep = |
| 2902 | kmem_cache_create("ext4_free_block_extents", |
| 2903 | sizeof(struct ext4_free_data), |
| 2904 | 0, SLAB_RECLAIM_ACCOUNT, NULL); |
| 2905 | if (ext4_free_ext_cachep == NULL) { |
| 2906 | kmem_cache_destroy(ext4_pspace_cachep); |
| 2907 | kmem_cache_destroy(ext4_ac_cachep); |
| 2908 | return -ENOMEM; |
| 2909 | } |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2910 | ext4_create_debugfs_entry(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2911 | return 0; |
| 2912 | } |
| 2913 | |
| 2914 | void exit_ext4_mballoc(void) |
| 2915 | { |
Jesper Dangaard Brouer | 3e03f9c | 2009-07-05 22:29:27 -0400 | [diff] [blame] | 2916 | /* |
| 2917 | * Wait for completion of call_rcu()'s on ext4_pspace_cachep |
| 2918 | * before destroying the slab cache. |
| 2919 | */ |
| 2920 | rcu_barrier(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2921 | kmem_cache_destroy(ext4_pspace_cachep); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 2922 | kmem_cache_destroy(ext4_ac_cachep); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2923 | kmem_cache_destroy(ext4_free_ext_cachep); |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2924 | ext4_remove_debugfs_entry(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | |
| 2928 | /* |
| 2929 | * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps |
| 2930 | * Returns 0 if success or error code |
| 2931 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2932 | static noinline_for_stack int |
| 2933 | ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2934 | handle_t *handle, unsigned int reserv_blks) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2935 | { |
| 2936 | struct buffer_head *bitmap_bh = NULL; |
| 2937 | struct ext4_super_block *es; |
| 2938 | struct ext4_group_desc *gdp; |
| 2939 | struct buffer_head *gdp_bh; |
| 2940 | struct ext4_sb_info *sbi; |
| 2941 | struct super_block *sb; |
| 2942 | ext4_fsblk_t block; |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 2943 | int err, len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2944 | |
| 2945 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 2946 | BUG_ON(ac->ac_b_ex.fe_len <= 0); |
| 2947 | |
| 2948 | sb = ac->ac_sb; |
| 2949 | sbi = EXT4_SB(sb); |
| 2950 | es = sbi->s_es; |
| 2951 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2952 | |
| 2953 | err = -EIO; |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2954 | bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2955 | if (!bitmap_bh) |
| 2956 | goto out_err; |
| 2957 | |
| 2958 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
| 2959 | if (err) |
| 2960 | goto out_err; |
| 2961 | |
| 2962 | err = -EIO; |
| 2963 | gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh); |
| 2964 | if (!gdp) |
| 2965 | goto out_err; |
| 2966 | |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2967 | ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group, |
Thadeu Lima de Souza Cascardo | 9fd9784 | 2009-01-26 19:26:26 -0500 | [diff] [blame] | 2968 | ext4_free_blks_count(sb, gdp)); |
Aneesh Kumar K.V | 03cddb8 | 2008-06-05 20:59:29 -0400 | [diff] [blame] | 2969 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2970 | err = ext4_journal_get_write_access(handle, gdp_bh); |
| 2971 | if (err) |
| 2972 | goto out_err; |
| 2973 | |
| 2974 | block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb) |
| 2975 | + ac->ac_b_ex.fe_start |
| 2976 | + le32_to_cpu(es->s_first_data_block); |
| 2977 | |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 2978 | len = ac->ac_b_ex.fe_len; |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 2979 | if (!ext4_data_block_valid(sbi, block, len)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2980 | ext4_error(sb, __func__, |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 2981 | "Allocating blocks %llu-%llu which overlap " |
| 2982 | "fs metadata\n", block, block+len); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 2983 | /* File system mounted not to panic on error |
| 2984 | * Fix the bitmap and repeat the block allocation |
| 2985 | * We leak some of the blocks here. |
| 2986 | */ |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 2987 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
| 2988 | mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, |
| 2989 | ac->ac_b_ex.fe_len); |
| 2990 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2991 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 2992 | if (!err) |
| 2993 | err = -EAGAIN; |
| 2994 | goto out_err; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2995 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 2996 | |
| 2997 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2998 | #ifdef AGGRESSIVE_CHECK |
| 2999 | { |
| 3000 | int i; |
| 3001 | for (i = 0; i < ac->ac_b_ex.fe_len; i++) { |
| 3002 | BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i, |
| 3003 | bitmap_bh->b_data)); |
| 3004 | } |
| 3005 | } |
| 3006 | #endif |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3007 | mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3008 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 3009 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 3010 | ext4_free_blks_set(sb, gdp, |
| 3011 | ext4_free_blocks_after_init(sb, |
| 3012 | ac->ac_b_ex.fe_group, gdp)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3013 | } |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 3014 | len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len; |
| 3015 | ext4_free_blks_set(sb, gdp, len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3016 | gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3017 | |
| 3018 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 3019 | percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3020 | /* |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 3021 | * Now reduce the dirty block count also. Should not go negative |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3022 | */ |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 3023 | if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED)) |
| 3024 | /* release all the reserved blocks if non delalloc */ |
| 3025 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks); |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 3026 | else { |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 3027 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, |
| 3028 | ac->ac_b_ex.fe_len); |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 3029 | /* convert reserved quota blocks to real quota blocks */ |
| 3030 | vfs_dq_claim_block(ac->ac_inode, ac->ac_b_ex.fe_len); |
| 3031 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3032 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3033 | if (sbi->s_log_groups_per_flex) { |
| 3034 | ext4_group_t flex_group = ext4_flex_group(sbi, |
| 3035 | ac->ac_b_ex.fe_group); |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 3036 | atomic_sub(ac->ac_b_ex.fe_len, |
| 3037 | &sbi->s_flex_groups[flex_group].free_blocks); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3038 | } |
| 3039 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3040 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3041 | if (err) |
| 3042 | goto out_err; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3043 | err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3044 | |
| 3045 | out_err: |
| 3046 | sb->s_dirt = 1; |
Aneesh Kumar K.V | 42a10ad | 2008-02-10 01:07:28 -0500 | [diff] [blame] | 3047 | brelse(bitmap_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3048 | return err; |
| 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * here we normalize request for locality group |
| 3053 | * Group request are normalized to s_strip size if we set the same via mount |
| 3054 | * option. If not we set it to s_mb_group_prealloc which can be configured via |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 3055 | * /sys/fs/ext4/<partition>/mb_group_prealloc |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3056 | * |
| 3057 | * XXX: should we try to preallocate more than the group has now? |
| 3058 | */ |
| 3059 | static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac) |
| 3060 | { |
| 3061 | struct super_block *sb = ac->ac_sb; |
| 3062 | struct ext4_locality_group *lg = ac->ac_lg; |
| 3063 | |
| 3064 | BUG_ON(lg == NULL); |
| 3065 | if (EXT4_SB(sb)->s_stripe) |
| 3066 | ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe; |
| 3067 | else |
| 3068 | ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc; |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3069 | mb_debug(1, "#%u: goal %u blocks for locality group\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3070 | current->pid, ac->ac_g_ex.fe_len); |
| 3071 | } |
| 3072 | |
| 3073 | /* |
| 3074 | * Normalization means making request better in terms of |
| 3075 | * size and alignment |
| 3076 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3077 | static noinline_for_stack void |
| 3078 | ext4_mb_normalize_request(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3079 | struct ext4_allocation_request *ar) |
| 3080 | { |
| 3081 | int bsbits, max; |
| 3082 | ext4_lblk_t end; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3083 | loff_t size, orig_size, start_off; |
| 3084 | ext4_lblk_t start, orig_start; |
| 3085 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3086 | struct ext4_prealloc_space *pa; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3087 | |
| 3088 | /* do normalize only data requests, metadata requests |
| 3089 | do not need preallocation */ |
| 3090 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 3091 | return; |
| 3092 | |
| 3093 | /* sometime caller may want exact blocks */ |
| 3094 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 3095 | return; |
| 3096 | |
| 3097 | /* caller may indicate that preallocation isn't |
| 3098 | * required (it's a tail, for example) */ |
| 3099 | if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) |
| 3100 | return; |
| 3101 | |
| 3102 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) { |
| 3103 | ext4_mb_normalize_group_request(ac); |
| 3104 | return ; |
| 3105 | } |
| 3106 | |
| 3107 | bsbits = ac->ac_sb->s_blocksize_bits; |
| 3108 | |
| 3109 | /* first, let's learn actual file size |
| 3110 | * given current request is allocated */ |
| 3111 | size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len; |
| 3112 | size = size << bsbits; |
| 3113 | if (size < i_size_read(ac->ac_inode)) |
| 3114 | size = i_size_read(ac->ac_inode); |
| 3115 | |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3116 | /* max size of free chunks */ |
| 3117 | max = 2 << bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3118 | |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3119 | #define NRL_CHECK_SIZE(req, size, max, chunk_size) \ |
| 3120 | (req <= (size) || max <= (chunk_size)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3121 | |
| 3122 | /* first, try to predict filesize */ |
| 3123 | /* XXX: should this table be tunable? */ |
| 3124 | start_off = 0; |
| 3125 | if (size <= 16 * 1024) { |
| 3126 | size = 16 * 1024; |
| 3127 | } else if (size <= 32 * 1024) { |
| 3128 | size = 32 * 1024; |
| 3129 | } else if (size <= 64 * 1024) { |
| 3130 | size = 64 * 1024; |
| 3131 | } else if (size <= 128 * 1024) { |
| 3132 | size = 128 * 1024; |
| 3133 | } else if (size <= 256 * 1024) { |
| 3134 | size = 256 * 1024; |
| 3135 | } else if (size <= 512 * 1024) { |
| 3136 | size = 512 * 1024; |
| 3137 | } else if (size <= 1024 * 1024) { |
| 3138 | size = 1024 * 1024; |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3139 | } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3140 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3141 | (21 - bsbits)) << 21; |
| 3142 | size = 2 * 1024 * 1024; |
| 3143 | } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3144 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
| 3145 | (22 - bsbits)) << 22; |
| 3146 | size = 4 * 1024 * 1024; |
| 3147 | } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len, |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3148 | (8<<20)>>bsbits, max, 8 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3149 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
| 3150 | (23 - bsbits)) << 23; |
| 3151 | size = 8 * 1024 * 1024; |
| 3152 | } else { |
| 3153 | start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits; |
| 3154 | size = ac->ac_o_ex.fe_len << bsbits; |
| 3155 | } |
| 3156 | orig_size = size = size >> bsbits; |
| 3157 | orig_start = start = start_off >> bsbits; |
| 3158 | |
| 3159 | /* don't cover already allocated blocks in selected range */ |
| 3160 | if (ar->pleft && start <= ar->lleft) { |
| 3161 | size -= ar->lleft + 1 - start; |
| 3162 | start = ar->lleft + 1; |
| 3163 | } |
| 3164 | if (ar->pright && start + size - 1 >= ar->lright) |
| 3165 | size -= start + size - ar->lright; |
| 3166 | |
| 3167 | end = start + size; |
| 3168 | |
| 3169 | /* check we don't cross already preallocated blocks */ |
| 3170 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3171 | list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3172 | ext4_lblk_t pa_end; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3173 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3174 | if (pa->pa_deleted) |
| 3175 | continue; |
| 3176 | spin_lock(&pa->pa_lock); |
| 3177 | if (pa->pa_deleted) { |
| 3178 | spin_unlock(&pa->pa_lock); |
| 3179 | continue; |
| 3180 | } |
| 3181 | |
| 3182 | pa_end = pa->pa_lstart + pa->pa_len; |
| 3183 | |
| 3184 | /* PA must not overlap original request */ |
| 3185 | BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end || |
| 3186 | ac->ac_o_ex.fe_logical < pa->pa_lstart)); |
| 3187 | |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 3188 | /* skip PAs this normalized request doesn't overlap with */ |
| 3189 | if (pa->pa_lstart >= end || pa_end <= start) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3190 | spin_unlock(&pa->pa_lock); |
| 3191 | continue; |
| 3192 | } |
| 3193 | BUG_ON(pa->pa_lstart <= start && pa_end >= end); |
| 3194 | |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 3195 | /* adjust start or end to be adjacent to this pa */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3196 | if (pa_end <= ac->ac_o_ex.fe_logical) { |
| 3197 | BUG_ON(pa_end < start); |
| 3198 | start = pa_end; |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 3199 | } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3200 | BUG_ON(pa->pa_lstart > end); |
| 3201 | end = pa->pa_lstart; |
| 3202 | } |
| 3203 | spin_unlock(&pa->pa_lock); |
| 3204 | } |
| 3205 | rcu_read_unlock(); |
| 3206 | size = end - start; |
| 3207 | |
| 3208 | /* XXX: extra loop to check we really don't overlap preallocations */ |
| 3209 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3210 | list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3211 | ext4_lblk_t pa_end; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3212 | spin_lock(&pa->pa_lock); |
| 3213 | if (pa->pa_deleted == 0) { |
| 3214 | pa_end = pa->pa_lstart + pa->pa_len; |
| 3215 | BUG_ON(!(start >= pa_end || end <= pa->pa_lstart)); |
| 3216 | } |
| 3217 | spin_unlock(&pa->pa_lock); |
| 3218 | } |
| 3219 | rcu_read_unlock(); |
| 3220 | |
| 3221 | if (start + size <= ac->ac_o_ex.fe_logical && |
| 3222 | start > ac->ac_o_ex.fe_logical) { |
| 3223 | printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n", |
| 3224 | (unsigned long) start, (unsigned long) size, |
| 3225 | (unsigned long) ac->ac_o_ex.fe_logical); |
| 3226 | } |
| 3227 | BUG_ON(start + size <= ac->ac_o_ex.fe_logical && |
| 3228 | start > ac->ac_o_ex.fe_logical); |
Eric Sandeen | 8d03c7a | 2009-03-14 11:51:46 -0400 | [diff] [blame] | 3229 | BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3230 | |
| 3231 | /* now prepare goal request */ |
| 3232 | |
| 3233 | /* XXX: is it better to align blocks WRT to logical |
| 3234 | * placement or satisfy big request as is */ |
| 3235 | ac->ac_g_ex.fe_logical = start; |
| 3236 | ac->ac_g_ex.fe_len = size; |
| 3237 | |
| 3238 | /* define goal start in order to merge */ |
| 3239 | if (ar->pright && (ar->lright == (start + size))) { |
| 3240 | /* merge to the right */ |
| 3241 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size, |
| 3242 | &ac->ac_f_ex.fe_group, |
| 3243 | &ac->ac_f_ex.fe_start); |
| 3244 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; |
| 3245 | } |
| 3246 | if (ar->pleft && (ar->lleft + 1 == start)) { |
| 3247 | /* merge to the left */ |
| 3248 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1, |
| 3249 | &ac->ac_f_ex.fe_group, |
| 3250 | &ac->ac_f_ex.fe_start); |
| 3251 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; |
| 3252 | } |
| 3253 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3254 | mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3255 | (unsigned) orig_size, (unsigned) start); |
| 3256 | } |
| 3257 | |
| 3258 | static void ext4_mb_collect_stats(struct ext4_allocation_context *ac) |
| 3259 | { |
| 3260 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 3261 | |
| 3262 | if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) { |
| 3263 | atomic_inc(&sbi->s_bal_reqs); |
| 3264 | atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated); |
| 3265 | if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len) |
| 3266 | atomic_inc(&sbi->s_bal_success); |
| 3267 | atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned); |
| 3268 | if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start && |
| 3269 | ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group) |
| 3270 | atomic_inc(&sbi->s_bal_goals); |
| 3271 | if (ac->ac_found > sbi->s_mb_max_to_scan) |
| 3272 | atomic_inc(&sbi->s_bal_breaks); |
| 3273 | } |
| 3274 | |
| 3275 | ext4_mb_store_history(ac); |
| 3276 | } |
| 3277 | |
| 3278 | /* |
| 3279 | * use blocks preallocated to inode |
| 3280 | */ |
| 3281 | static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac, |
| 3282 | struct ext4_prealloc_space *pa) |
| 3283 | { |
| 3284 | ext4_fsblk_t start; |
| 3285 | ext4_fsblk_t end; |
| 3286 | int len; |
| 3287 | |
| 3288 | /* found preallocated blocks, use them */ |
| 3289 | start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart); |
| 3290 | end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len); |
| 3291 | len = end - start; |
| 3292 | ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group, |
| 3293 | &ac->ac_b_ex.fe_start); |
| 3294 | ac->ac_b_ex.fe_len = len; |
| 3295 | ac->ac_status = AC_STATUS_FOUND; |
| 3296 | ac->ac_pa = pa; |
| 3297 | |
| 3298 | BUG_ON(start < pa->pa_pstart); |
| 3299 | BUG_ON(start + len > pa->pa_pstart + pa->pa_len); |
| 3300 | BUG_ON(pa->pa_free < len); |
| 3301 | pa->pa_free -= len; |
| 3302 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3303 | mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | /* |
| 3307 | * use blocks preallocated to locality group |
| 3308 | */ |
| 3309 | static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac, |
| 3310 | struct ext4_prealloc_space *pa) |
| 3311 | { |
Aneesh Kumar K.V | 03cddb8 | 2008-06-05 20:59:29 -0400 | [diff] [blame] | 3312 | unsigned int len = ac->ac_o_ex.fe_len; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3313 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3314 | ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart, |
| 3315 | &ac->ac_b_ex.fe_group, |
| 3316 | &ac->ac_b_ex.fe_start); |
| 3317 | ac->ac_b_ex.fe_len = len; |
| 3318 | ac->ac_status = AC_STATUS_FOUND; |
| 3319 | ac->ac_pa = pa; |
| 3320 | |
| 3321 | /* we don't correct pa_pstart or pa_plen here to avoid |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 3322 | * possible race when the group is being loaded concurrently |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3323 | * instead we correct pa later, after blocks are marked |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 3324 | * in on-disk bitmap -- see ext4_mb_release_context() |
| 3325 | * Other CPUs are prevented from allocating from this pa by lg_mutex |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3326 | */ |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3327 | mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | /* |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3331 | * Return the prealloc space that have minimal distance |
| 3332 | * from the goal block. @cpa is the prealloc |
| 3333 | * space that is having currently known minimal distance |
| 3334 | * from the goal block. |
| 3335 | */ |
| 3336 | static struct ext4_prealloc_space * |
| 3337 | ext4_mb_check_group_pa(ext4_fsblk_t goal_block, |
| 3338 | struct ext4_prealloc_space *pa, |
| 3339 | struct ext4_prealloc_space *cpa) |
| 3340 | { |
| 3341 | ext4_fsblk_t cur_distance, new_distance; |
| 3342 | |
| 3343 | if (cpa == NULL) { |
| 3344 | atomic_inc(&pa->pa_count); |
| 3345 | return pa; |
| 3346 | } |
| 3347 | cur_distance = abs(goal_block - cpa->pa_pstart); |
| 3348 | new_distance = abs(goal_block - pa->pa_pstart); |
| 3349 | |
| 3350 | if (cur_distance < new_distance) |
| 3351 | return cpa; |
| 3352 | |
| 3353 | /* drop the previous reference */ |
| 3354 | atomic_dec(&cpa->pa_count); |
| 3355 | atomic_inc(&pa->pa_count); |
| 3356 | return pa; |
| 3357 | } |
| 3358 | |
| 3359 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3360 | * search goal blocks in preallocated space |
| 3361 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3362 | static noinline_for_stack int |
| 3363 | ext4_mb_use_preallocated(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3364 | { |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3365 | int order, i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3366 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
| 3367 | struct ext4_locality_group *lg; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3368 | struct ext4_prealloc_space *pa, *cpa = NULL; |
| 3369 | ext4_fsblk_t goal_block; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3370 | |
| 3371 | /* only data can be preallocated */ |
| 3372 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 3373 | return 0; |
| 3374 | |
| 3375 | /* first, try per-file preallocation */ |
| 3376 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3377 | list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3378 | |
| 3379 | /* all fields in this condition don't change, |
| 3380 | * so we can skip locking for them */ |
| 3381 | if (ac->ac_o_ex.fe_logical < pa->pa_lstart || |
| 3382 | ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len) |
| 3383 | continue; |
| 3384 | |
| 3385 | /* found preallocated blocks, use them */ |
| 3386 | spin_lock(&pa->pa_lock); |
| 3387 | if (pa->pa_deleted == 0 && pa->pa_free) { |
| 3388 | atomic_inc(&pa->pa_count); |
| 3389 | ext4_mb_use_inode_pa(ac, pa); |
| 3390 | spin_unlock(&pa->pa_lock); |
| 3391 | ac->ac_criteria = 10; |
| 3392 | rcu_read_unlock(); |
| 3393 | return 1; |
| 3394 | } |
| 3395 | spin_unlock(&pa->pa_lock); |
| 3396 | } |
| 3397 | rcu_read_unlock(); |
| 3398 | |
| 3399 | /* can we use group allocation? */ |
| 3400 | if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)) |
| 3401 | return 0; |
| 3402 | |
| 3403 | /* inode may have no locality group for some reason */ |
| 3404 | lg = ac->ac_lg; |
| 3405 | if (lg == NULL) |
| 3406 | return 0; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3407 | order = fls(ac->ac_o_ex.fe_len) - 1; |
| 3408 | if (order > PREALLOC_TB_SIZE - 1) |
| 3409 | /* The max size of hash table is PREALLOC_TB_SIZE */ |
| 3410 | order = PREALLOC_TB_SIZE - 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3411 | |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3412 | goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) + |
| 3413 | ac->ac_g_ex.fe_start + |
| 3414 | le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block); |
| 3415 | /* |
| 3416 | * search for the prealloc space that is having |
| 3417 | * minimal distance from the goal block. |
| 3418 | */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3419 | for (i = order; i < PREALLOC_TB_SIZE; i++) { |
| 3420 | rcu_read_lock(); |
| 3421 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i], |
| 3422 | pa_inode_list) { |
| 3423 | spin_lock(&pa->pa_lock); |
| 3424 | if (pa->pa_deleted == 0 && |
| 3425 | pa->pa_free >= ac->ac_o_ex.fe_len) { |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3426 | |
| 3427 | cpa = ext4_mb_check_group_pa(goal_block, |
| 3428 | pa, cpa); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3429 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3430 | spin_unlock(&pa->pa_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3431 | } |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3432 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3433 | } |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3434 | if (cpa) { |
| 3435 | ext4_mb_use_group_pa(ac, cpa); |
| 3436 | ac->ac_criteria = 20; |
| 3437 | return 1; |
| 3438 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3439 | return 0; |
| 3440 | } |
| 3441 | |
| 3442 | /* |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 3443 | * the function goes through all block freed in the group |
| 3444 | * but not yet committed and marks them used in in-core bitmap. |
| 3445 | * buddy must be generated from this bitmap |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3446 | * Need to be called with the ext4 group lock held |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 3447 | */ |
| 3448 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, |
| 3449 | ext4_group_t group) |
| 3450 | { |
| 3451 | struct rb_node *n; |
| 3452 | struct ext4_group_info *grp; |
| 3453 | struct ext4_free_data *entry; |
| 3454 | |
| 3455 | grp = ext4_get_group_info(sb, group); |
| 3456 | n = rb_first(&(grp->bb_free_root)); |
| 3457 | |
| 3458 | while (n) { |
| 3459 | entry = rb_entry(n, struct ext4_free_data, node); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3460 | mb_set_bits(bitmap, entry->start_blk, entry->count); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 3461 | n = rb_next(n); |
| 3462 | } |
| 3463 | return; |
| 3464 | } |
| 3465 | |
| 3466 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3467 | * the function goes through all preallocation in this group and marks them |
| 3468 | * used in in-core bitmap. buddy must be generated from this bitmap |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3469 | * Need to be called with ext4 group lock held |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3470 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 3471 | static noinline_for_stack |
| 3472 | void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3473 | ext4_group_t group) |
| 3474 | { |
| 3475 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 3476 | struct ext4_prealloc_space *pa; |
| 3477 | struct list_head *cur; |
| 3478 | ext4_group_t groupnr; |
| 3479 | ext4_grpblk_t start; |
| 3480 | int preallocated = 0; |
| 3481 | int count = 0; |
| 3482 | int len; |
| 3483 | |
| 3484 | /* all form of preallocation discards first load group, |
| 3485 | * so the only competing code is preallocation use. |
| 3486 | * we don't need any locking here |
| 3487 | * notice we do NOT ignore preallocations with pa_deleted |
| 3488 | * otherwise we could leave used blocks available for |
| 3489 | * allocation in buddy when concurrent ext4_mb_put_pa() |
| 3490 | * is dropping preallocation |
| 3491 | */ |
| 3492 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 3493 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 3494 | spin_lock(&pa->pa_lock); |
| 3495 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, |
| 3496 | &groupnr, &start); |
| 3497 | len = pa->pa_len; |
| 3498 | spin_unlock(&pa->pa_lock); |
| 3499 | if (unlikely(len == 0)) |
| 3500 | continue; |
| 3501 | BUG_ON(groupnr != group); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3502 | mb_set_bits(bitmap, start, len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3503 | preallocated += len; |
| 3504 | count++; |
| 3505 | } |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3506 | mb_debug(1, "prellocated %u for group %u\n", preallocated, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | static void ext4_mb_pa_callback(struct rcu_head *head) |
| 3510 | { |
| 3511 | struct ext4_prealloc_space *pa; |
| 3512 | pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu); |
| 3513 | kmem_cache_free(ext4_pspace_cachep, pa); |
| 3514 | } |
| 3515 | |
| 3516 | /* |
| 3517 | * drops a reference to preallocated space descriptor |
| 3518 | * if this was the last reference and the space is consumed |
| 3519 | */ |
| 3520 | static void ext4_mb_put_pa(struct ext4_allocation_context *ac, |
| 3521 | struct super_block *sb, struct ext4_prealloc_space *pa) |
| 3522 | { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 3523 | ext4_group_t grp; |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3524 | ext4_fsblk_t grp_blk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3525 | |
| 3526 | if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) |
| 3527 | return; |
| 3528 | |
| 3529 | /* in this short window concurrent discard can set pa_deleted */ |
| 3530 | spin_lock(&pa->pa_lock); |
| 3531 | if (pa->pa_deleted == 1) { |
| 3532 | spin_unlock(&pa->pa_lock); |
| 3533 | return; |
| 3534 | } |
| 3535 | |
| 3536 | pa->pa_deleted = 1; |
| 3537 | spin_unlock(&pa->pa_lock); |
| 3538 | |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3539 | grp_blk = pa->pa_pstart; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3540 | /* |
| 3541 | * If doing group-based preallocation, pa_pstart may be in the |
| 3542 | * next group when pa is used up |
| 3543 | */ |
| 3544 | if (pa->pa_type == MB_GROUP_PA) |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3545 | grp_blk--; |
| 3546 | |
| 3547 | ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3548 | |
| 3549 | /* |
| 3550 | * possible race: |
| 3551 | * |
| 3552 | * P1 (buddy init) P2 (regular allocation) |
| 3553 | * find block B in PA |
| 3554 | * copy on-disk bitmap to buddy |
| 3555 | * mark B in on-disk bitmap |
| 3556 | * drop PA from group |
| 3557 | * mark all PAs in buddy |
| 3558 | * |
| 3559 | * thus, P1 initializes buddy with B available. to prevent this |
| 3560 | * we make "copy" and "mark all PAs" atomic and serialize "drop PA" |
| 3561 | * against that pair |
| 3562 | */ |
| 3563 | ext4_lock_group(sb, grp); |
| 3564 | list_del(&pa->pa_group_list); |
| 3565 | ext4_unlock_group(sb, grp); |
| 3566 | |
| 3567 | spin_lock(pa->pa_obj_lock); |
| 3568 | list_del_rcu(&pa->pa_inode_list); |
| 3569 | spin_unlock(pa->pa_obj_lock); |
| 3570 | |
| 3571 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 3572 | } |
| 3573 | |
| 3574 | /* |
| 3575 | * creates new preallocated space for given inode |
| 3576 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3577 | static noinline_for_stack int |
| 3578 | ext4_mb_new_inode_pa(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3579 | { |
| 3580 | struct super_block *sb = ac->ac_sb; |
| 3581 | struct ext4_prealloc_space *pa; |
| 3582 | struct ext4_group_info *grp; |
| 3583 | struct ext4_inode_info *ei; |
| 3584 | |
| 3585 | /* preallocate only when found space is larger then requested */ |
| 3586 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); |
| 3587 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 3588 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); |
| 3589 | |
| 3590 | pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS); |
| 3591 | if (pa == NULL) |
| 3592 | return -ENOMEM; |
| 3593 | |
| 3594 | if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) { |
| 3595 | int winl; |
| 3596 | int wins; |
| 3597 | int win; |
| 3598 | int offs; |
| 3599 | |
| 3600 | /* we can't allocate as much as normalizer wants. |
| 3601 | * so, found space must get proper lstart |
| 3602 | * to cover original request */ |
| 3603 | BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical); |
| 3604 | BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len); |
| 3605 | |
| 3606 | /* we're limited by original request in that |
| 3607 | * logical block must be covered any way |
| 3608 | * winl is window we can move our chunk within */ |
| 3609 | winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical; |
| 3610 | |
| 3611 | /* also, we should cover whole original request */ |
| 3612 | wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len; |
| 3613 | |
| 3614 | /* the smallest one defines real window */ |
| 3615 | win = min(winl, wins); |
| 3616 | |
| 3617 | offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len; |
| 3618 | if (offs && offs < win) |
| 3619 | win = offs; |
| 3620 | |
| 3621 | ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win; |
| 3622 | BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical); |
| 3623 | BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len); |
| 3624 | } |
| 3625 | |
| 3626 | /* preallocation can change ac_b_ex, thus we store actually |
| 3627 | * allocated blocks for history */ |
| 3628 | ac->ac_f_ex = ac->ac_b_ex; |
| 3629 | |
| 3630 | pa->pa_lstart = ac->ac_b_ex.fe_logical; |
| 3631 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 3632 | pa->pa_len = ac->ac_b_ex.fe_len; |
| 3633 | pa->pa_free = pa->pa_len; |
| 3634 | atomic_set(&pa->pa_count, 1); |
| 3635 | spin_lock_init(&pa->pa_lock); |
Aneesh Kumar K.V | d794bf8 | 2009-02-14 10:31:16 -0500 | [diff] [blame] | 3636 | INIT_LIST_HEAD(&pa->pa_inode_list); |
| 3637 | INIT_LIST_HEAD(&pa->pa_group_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3638 | pa->pa_deleted = 0; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3639 | pa->pa_type = MB_INODE_PA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3640 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3641 | mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3642 | pa->pa_pstart, pa->pa_len, pa->pa_lstart); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3643 | trace_ext4_mb_new_inode_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3644 | |
| 3645 | ext4_mb_use_inode_pa(ac, pa); |
| 3646 | atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated); |
| 3647 | |
| 3648 | ei = EXT4_I(ac->ac_inode); |
| 3649 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); |
| 3650 | |
| 3651 | pa->pa_obj_lock = &ei->i_prealloc_lock; |
| 3652 | pa->pa_inode = ac->ac_inode; |
| 3653 | |
| 3654 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
| 3655 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); |
| 3656 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
| 3657 | |
| 3658 | spin_lock(pa->pa_obj_lock); |
| 3659 | list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list); |
| 3660 | spin_unlock(pa->pa_obj_lock); |
| 3661 | |
| 3662 | return 0; |
| 3663 | } |
| 3664 | |
| 3665 | /* |
| 3666 | * creates new preallocated space for locality group inodes belongs to |
| 3667 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3668 | static noinline_for_stack int |
| 3669 | ext4_mb_new_group_pa(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3670 | { |
| 3671 | struct super_block *sb = ac->ac_sb; |
| 3672 | struct ext4_locality_group *lg; |
| 3673 | struct ext4_prealloc_space *pa; |
| 3674 | struct ext4_group_info *grp; |
| 3675 | |
| 3676 | /* preallocate only when found space is larger then requested */ |
| 3677 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); |
| 3678 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 3679 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); |
| 3680 | |
| 3681 | BUG_ON(ext4_pspace_cachep == NULL); |
| 3682 | pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS); |
| 3683 | if (pa == NULL) |
| 3684 | return -ENOMEM; |
| 3685 | |
| 3686 | /* preallocation can change ac_b_ex, thus we store actually |
| 3687 | * allocated blocks for history */ |
| 3688 | ac->ac_f_ex = ac->ac_b_ex; |
| 3689 | |
| 3690 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 3691 | pa->pa_lstart = pa->pa_pstart; |
| 3692 | pa->pa_len = ac->ac_b_ex.fe_len; |
| 3693 | pa->pa_free = pa->pa_len; |
| 3694 | atomic_set(&pa->pa_count, 1); |
| 3695 | spin_lock_init(&pa->pa_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3696 | INIT_LIST_HEAD(&pa->pa_inode_list); |
Aneesh Kumar K.V | d794bf8 | 2009-02-14 10:31:16 -0500 | [diff] [blame] | 3697 | INIT_LIST_HEAD(&pa->pa_group_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3698 | pa->pa_deleted = 0; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3699 | pa->pa_type = MB_GROUP_PA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3700 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3701 | mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa, |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3702 | pa->pa_pstart, pa->pa_len, pa->pa_lstart); |
| 3703 | trace_ext4_mb_new_group_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3704 | |
| 3705 | ext4_mb_use_group_pa(ac, pa); |
| 3706 | atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated); |
| 3707 | |
| 3708 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); |
| 3709 | lg = ac->ac_lg; |
| 3710 | BUG_ON(lg == NULL); |
| 3711 | |
| 3712 | pa->pa_obj_lock = &lg->lg_prealloc_lock; |
| 3713 | pa->pa_inode = NULL; |
| 3714 | |
| 3715 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
| 3716 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); |
| 3717 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
| 3718 | |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3719 | /* |
| 3720 | * We will later add the new pa to the right bucket |
| 3721 | * after updating the pa_free in ext4_mb_release_context |
| 3722 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3723 | return 0; |
| 3724 | } |
| 3725 | |
| 3726 | static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac) |
| 3727 | { |
| 3728 | int err; |
| 3729 | |
| 3730 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
| 3731 | err = ext4_mb_new_group_pa(ac); |
| 3732 | else |
| 3733 | err = ext4_mb_new_inode_pa(ac); |
| 3734 | return err; |
| 3735 | } |
| 3736 | |
| 3737 | /* |
| 3738 | * finds all unused blocks in on-disk bitmap, frees them in |
| 3739 | * in-core bitmap and buddy. |
| 3740 | * @pa must be unlinked from inode and group lists, so that |
| 3741 | * nobody else can find/use it. |
| 3742 | * the caller MUST hold group/inode locks. |
| 3743 | * TODO: optimize the case when there are no in-core structures yet |
| 3744 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3745 | static noinline_for_stack int |
| 3746 | ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh, |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3747 | struct ext4_prealloc_space *pa, |
| 3748 | struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3749 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3750 | struct super_block *sb = e4b->bd_sb; |
| 3751 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3752 | unsigned int end; |
| 3753 | unsigned int next; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3754 | ext4_group_t group; |
| 3755 | ext4_grpblk_t bit; |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 3756 | unsigned long long grp_blk_start; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3757 | sector_t start; |
| 3758 | int err = 0; |
| 3759 | int free = 0; |
| 3760 | |
| 3761 | BUG_ON(pa->pa_deleted == 0); |
| 3762 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 3763 | grp_blk_start = pa->pa_pstart - bit; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3764 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
| 3765 | end = bit + pa->pa_len; |
| 3766 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3767 | if (ac) { |
| 3768 | ac->ac_sb = sb; |
| 3769 | ac->ac_inode = pa->pa_inode; |
| 3770 | ac->ac_op = EXT4_MB_HISTORY_DISCARD; |
| 3771 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3772 | |
| 3773 | while (bit < end) { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 3774 | bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3775 | if (bit >= end) |
| 3776 | break; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 3777 | next = mb_find_next_bit(bitmap_bh->b_data, end, bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3778 | start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit + |
| 3779 | le32_to_cpu(sbi->s_es->s_first_data_block); |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3780 | mb_debug(1, " free preallocated %u/%u in group %u\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3781 | (unsigned) start, (unsigned) next - bit, |
| 3782 | (unsigned) group); |
| 3783 | free += next - bit; |
| 3784 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3785 | if (ac) { |
| 3786 | ac->ac_b_ex.fe_group = group; |
| 3787 | ac->ac_b_ex.fe_start = bit; |
| 3788 | ac->ac_b_ex.fe_len = next - bit; |
| 3789 | ac->ac_b_ex.fe_logical = 0; |
| 3790 | ext4_mb_store_history(ac); |
| 3791 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3792 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3793 | trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit, |
| 3794 | next - bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3795 | mb_free_blocks(pa->pa_inode, e4b, bit, next - bit); |
| 3796 | bit = next + 1; |
| 3797 | } |
| 3798 | if (free != pa->pa_free) { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 3799 | printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3800 | pa, (unsigned long) pa->pa_lstart, |
| 3801 | (unsigned long) pa->pa_pstart, |
| 3802 | (unsigned long) pa->pa_len); |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 3803 | ext4_grp_locked_error(sb, group, |
| 3804 | __func__, "free %u, pa_free %u", |
| 3805 | free, pa->pa_free); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 3806 | /* |
| 3807 | * pa is already deleted so we use the value obtained |
| 3808 | * from the bitmap and continue. |
| 3809 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3810 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3811 | atomic_add(free, &sbi->s_mb_discarded); |
| 3812 | |
| 3813 | return err; |
| 3814 | } |
| 3815 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3816 | static noinline_for_stack int |
| 3817 | ext4_mb_release_group_pa(struct ext4_buddy *e4b, |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3818 | struct ext4_prealloc_space *pa, |
| 3819 | struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3820 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3821 | struct super_block *sb = e4b->bd_sb; |
| 3822 | ext4_group_t group; |
| 3823 | ext4_grpblk_t bit; |
| 3824 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3825 | if (ac) |
| 3826 | ac->ac_op = EXT4_MB_HISTORY_DISCARD; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3827 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3828 | trace_ext4_mb_release_group_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3829 | BUG_ON(pa->pa_deleted == 0); |
| 3830 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); |
| 3831 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
| 3832 | mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len); |
| 3833 | atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded); |
| 3834 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3835 | if (ac) { |
| 3836 | ac->ac_sb = sb; |
| 3837 | ac->ac_inode = NULL; |
| 3838 | ac->ac_b_ex.fe_group = group; |
| 3839 | ac->ac_b_ex.fe_start = bit; |
| 3840 | ac->ac_b_ex.fe_len = pa->pa_len; |
| 3841 | ac->ac_b_ex.fe_logical = 0; |
| 3842 | ext4_mb_store_history(ac); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3843 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3844 | |
| 3845 | return 0; |
| 3846 | } |
| 3847 | |
| 3848 | /* |
| 3849 | * releases all preallocations in given group |
| 3850 | * |
| 3851 | * first, we need to decide discard policy: |
| 3852 | * - when do we discard |
| 3853 | * 1) ENOSPC |
| 3854 | * - how many do we discard |
| 3855 | * 1) how many requested |
| 3856 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3857 | static noinline_for_stack int |
| 3858 | ext4_mb_discard_group_preallocations(struct super_block *sb, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3859 | ext4_group_t group, int needed) |
| 3860 | { |
| 3861 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 3862 | struct buffer_head *bitmap_bh = NULL; |
| 3863 | struct ext4_prealloc_space *pa, *tmp; |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3864 | struct ext4_allocation_context *ac; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3865 | struct list_head list; |
| 3866 | struct ext4_buddy e4b; |
| 3867 | int err; |
| 3868 | int busy = 0; |
| 3869 | int free = 0; |
| 3870 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3871 | mb_debug(1, "discard preallocation for group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3872 | |
| 3873 | if (list_empty(&grp->bb_prealloc_list)) |
| 3874 | return 0; |
| 3875 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3876 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3877 | if (bitmap_bh == NULL) { |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3878 | ext4_error(sb, __func__, "Error in reading block " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 3879 | "bitmap for %u", group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3880 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | err = ext4_mb_load_buddy(sb, group, &e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3884 | if (err) { |
| 3885 | ext4_error(sb, __func__, "Error in loading buddy " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 3886 | "information for %u", group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3887 | put_bh(bitmap_bh); |
| 3888 | return 0; |
| 3889 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3890 | |
| 3891 | if (needed == 0) |
| 3892 | needed = EXT4_BLOCKS_PER_GROUP(sb) + 1; |
| 3893 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3894 | INIT_LIST_HEAD(&list); |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3895 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3896 | if (ac) |
| 3897 | ac->ac_sb = sb; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3898 | repeat: |
| 3899 | ext4_lock_group(sb, group); |
| 3900 | list_for_each_entry_safe(pa, tmp, |
| 3901 | &grp->bb_prealloc_list, pa_group_list) { |
| 3902 | spin_lock(&pa->pa_lock); |
| 3903 | if (atomic_read(&pa->pa_count)) { |
| 3904 | spin_unlock(&pa->pa_lock); |
| 3905 | busy = 1; |
| 3906 | continue; |
| 3907 | } |
| 3908 | if (pa->pa_deleted) { |
| 3909 | spin_unlock(&pa->pa_lock); |
| 3910 | continue; |
| 3911 | } |
| 3912 | |
| 3913 | /* seems this one can be freed ... */ |
| 3914 | pa->pa_deleted = 1; |
| 3915 | |
| 3916 | /* we can trust pa_free ... */ |
| 3917 | free += pa->pa_free; |
| 3918 | |
| 3919 | spin_unlock(&pa->pa_lock); |
| 3920 | |
| 3921 | list_del(&pa->pa_group_list); |
| 3922 | list_add(&pa->u.pa_tmp_list, &list); |
| 3923 | } |
| 3924 | |
| 3925 | /* if we still need more blocks and some PAs were used, try again */ |
| 3926 | if (free < needed && busy) { |
| 3927 | busy = 0; |
| 3928 | ext4_unlock_group(sb, group); |
| 3929 | /* |
| 3930 | * Yield the CPU here so that we don't get soft lockup |
| 3931 | * in non preempt case. |
| 3932 | */ |
| 3933 | yield(); |
| 3934 | goto repeat; |
| 3935 | } |
| 3936 | |
| 3937 | /* found anything to free? */ |
| 3938 | if (list_empty(&list)) { |
| 3939 | BUG_ON(free != 0); |
| 3940 | goto out; |
| 3941 | } |
| 3942 | |
| 3943 | /* now free all selected PAs */ |
| 3944 | list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { |
| 3945 | |
| 3946 | /* remove from object (inode or locality group) */ |
| 3947 | spin_lock(pa->pa_obj_lock); |
| 3948 | list_del_rcu(&pa->pa_inode_list); |
| 3949 | spin_unlock(pa->pa_obj_lock); |
| 3950 | |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3951 | if (pa->pa_type == MB_GROUP_PA) |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3952 | ext4_mb_release_group_pa(&e4b, pa, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3953 | else |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3954 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3955 | |
| 3956 | list_del(&pa->u.pa_tmp_list); |
| 3957 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 3958 | } |
| 3959 | |
| 3960 | out: |
| 3961 | ext4_unlock_group(sb, group); |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3962 | if (ac) |
| 3963 | kmem_cache_free(ext4_ac_cachep, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3964 | ext4_mb_release_desc(&e4b); |
| 3965 | put_bh(bitmap_bh); |
| 3966 | return free; |
| 3967 | } |
| 3968 | |
| 3969 | /* |
| 3970 | * releases all non-used preallocated blocks for given inode |
| 3971 | * |
| 3972 | * It's important to discard preallocations under i_data_sem |
| 3973 | * We don't want another block to be served from the prealloc |
| 3974 | * space when we are discarding the inode prealloc space. |
| 3975 | * |
| 3976 | * FIXME!! Make sure it is valid at all the call sites |
| 3977 | */ |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3978 | void ext4_discard_preallocations(struct inode *inode) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3979 | { |
| 3980 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 3981 | struct super_block *sb = inode->i_sb; |
| 3982 | struct buffer_head *bitmap_bh = NULL; |
| 3983 | struct ext4_prealloc_space *pa, *tmp; |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3984 | struct ext4_allocation_context *ac; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3985 | ext4_group_t group = 0; |
| 3986 | struct list_head list; |
| 3987 | struct ext4_buddy e4b; |
| 3988 | int err; |
| 3989 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3990 | if (!S_ISREG(inode->i_mode)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3991 | /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/ |
| 3992 | return; |
| 3993 | } |
| 3994 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3995 | mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3996 | trace_ext4_discard_preallocations(inode); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3997 | |
| 3998 | INIT_LIST_HEAD(&list); |
| 3999 | |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 4000 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4001 | if (ac) { |
| 4002 | ac->ac_sb = sb; |
| 4003 | ac->ac_inode = inode; |
| 4004 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4005 | repeat: |
| 4006 | /* first, collect all pa's in the inode */ |
| 4007 | spin_lock(&ei->i_prealloc_lock); |
| 4008 | while (!list_empty(&ei->i_prealloc_list)) { |
| 4009 | pa = list_entry(ei->i_prealloc_list.next, |
| 4010 | struct ext4_prealloc_space, pa_inode_list); |
| 4011 | BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock); |
| 4012 | spin_lock(&pa->pa_lock); |
| 4013 | if (atomic_read(&pa->pa_count)) { |
| 4014 | /* this shouldn't happen often - nobody should |
| 4015 | * use preallocation while we're discarding it */ |
| 4016 | spin_unlock(&pa->pa_lock); |
| 4017 | spin_unlock(&ei->i_prealloc_lock); |
| 4018 | printk(KERN_ERR "uh-oh! used pa while discarding\n"); |
| 4019 | WARN_ON(1); |
| 4020 | schedule_timeout_uninterruptible(HZ); |
| 4021 | goto repeat; |
| 4022 | |
| 4023 | } |
| 4024 | if (pa->pa_deleted == 0) { |
| 4025 | pa->pa_deleted = 1; |
| 4026 | spin_unlock(&pa->pa_lock); |
| 4027 | list_del_rcu(&pa->pa_inode_list); |
| 4028 | list_add(&pa->u.pa_tmp_list, &list); |
| 4029 | continue; |
| 4030 | } |
| 4031 | |
| 4032 | /* someone is deleting pa right now */ |
| 4033 | spin_unlock(&pa->pa_lock); |
| 4034 | spin_unlock(&ei->i_prealloc_lock); |
| 4035 | |
| 4036 | /* we have to wait here because pa_deleted |
| 4037 | * doesn't mean pa is already unlinked from |
| 4038 | * the list. as we might be called from |
| 4039 | * ->clear_inode() the inode will get freed |
| 4040 | * and concurrent thread which is unlinking |
| 4041 | * pa from inode's list may access already |
| 4042 | * freed memory, bad-bad-bad */ |
| 4043 | |
| 4044 | /* XXX: if this happens too often, we can |
| 4045 | * add a flag to force wait only in case |
| 4046 | * of ->clear_inode(), but not in case of |
| 4047 | * regular truncate */ |
| 4048 | schedule_timeout_uninterruptible(HZ); |
| 4049 | goto repeat; |
| 4050 | } |
| 4051 | spin_unlock(&ei->i_prealloc_lock); |
| 4052 | |
| 4053 | list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4054 | BUG_ON(pa->pa_type != MB_INODE_PA); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4055 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL); |
| 4056 | |
| 4057 | err = ext4_mb_load_buddy(sb, group, &e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4058 | if (err) { |
| 4059 | ext4_error(sb, __func__, "Error in loading buddy " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 4060 | "information for %u", group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4061 | continue; |
| 4062 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4063 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4064 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4065 | if (bitmap_bh == NULL) { |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4066 | ext4_error(sb, __func__, "Error in reading block " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 4067 | "bitmap for %u", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4068 | ext4_mb_release_desc(&e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4069 | continue; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | ext4_lock_group(sb, group); |
| 4073 | list_del(&pa->pa_group_list); |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 4074 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4075 | ext4_unlock_group(sb, group); |
| 4076 | |
| 4077 | ext4_mb_release_desc(&e4b); |
| 4078 | put_bh(bitmap_bh); |
| 4079 | |
| 4080 | list_del(&pa->u.pa_tmp_list); |
| 4081 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4082 | } |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 4083 | if (ac) |
| 4084 | kmem_cache_free(ext4_ac_cachep, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4085 | } |
| 4086 | |
| 4087 | /* |
| 4088 | * finds all preallocated spaces and return blocks being freed to them |
| 4089 | * if preallocated space becomes full (no block is used from the space) |
| 4090 | * then the function frees space in buddy |
| 4091 | * XXX: at the moment, truncate (which is the only way to free blocks) |
| 4092 | * discards all preallocations |
| 4093 | */ |
| 4094 | static void ext4_mb_return_to_preallocation(struct inode *inode, |
| 4095 | struct ext4_buddy *e4b, |
| 4096 | sector_t block, int count) |
| 4097 | { |
| 4098 | BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list)); |
| 4099 | } |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 4100 | #ifdef CONFIG_EXT4_DEBUG |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4101 | static void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
| 4102 | { |
| 4103 | struct super_block *sb = ac->ac_sb; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4104 | ext4_group_t ngroups, i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4105 | |
| 4106 | printk(KERN_ERR "EXT4-fs: Can't allocate:" |
| 4107 | " Allocation context details:\n"); |
| 4108 | printk(KERN_ERR "EXT4-fs: status %d flags %d\n", |
| 4109 | ac->ac_status, ac->ac_flags); |
| 4110 | printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, " |
| 4111 | "best %lu/%lu/%lu@%lu cr %d\n", |
| 4112 | (unsigned long)ac->ac_o_ex.fe_group, |
| 4113 | (unsigned long)ac->ac_o_ex.fe_start, |
| 4114 | (unsigned long)ac->ac_o_ex.fe_len, |
| 4115 | (unsigned long)ac->ac_o_ex.fe_logical, |
| 4116 | (unsigned long)ac->ac_g_ex.fe_group, |
| 4117 | (unsigned long)ac->ac_g_ex.fe_start, |
| 4118 | (unsigned long)ac->ac_g_ex.fe_len, |
| 4119 | (unsigned long)ac->ac_g_ex.fe_logical, |
| 4120 | (unsigned long)ac->ac_b_ex.fe_group, |
| 4121 | (unsigned long)ac->ac_b_ex.fe_start, |
| 4122 | (unsigned long)ac->ac_b_ex.fe_len, |
| 4123 | (unsigned long)ac->ac_b_ex.fe_logical, |
| 4124 | (int)ac->ac_criteria); |
| 4125 | printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned, |
| 4126 | ac->ac_found); |
| 4127 | printk(KERN_ERR "EXT4-fs: groups: \n"); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4128 | ngroups = ext4_get_groups_count(sb); |
| 4129 | for (i = 0; i < ngroups; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4130 | struct ext4_group_info *grp = ext4_get_group_info(sb, i); |
| 4131 | struct ext4_prealloc_space *pa; |
| 4132 | ext4_grpblk_t start; |
| 4133 | struct list_head *cur; |
| 4134 | ext4_lock_group(sb, i); |
| 4135 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 4136 | pa = list_entry(cur, struct ext4_prealloc_space, |
| 4137 | pa_group_list); |
| 4138 | spin_lock(&pa->pa_lock); |
| 4139 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, |
| 4140 | NULL, &start); |
| 4141 | spin_unlock(&pa->pa_lock); |
Akira Fujita | 1c71850 | 2009-07-05 23:04:36 -0400 | [diff] [blame] | 4142 | printk(KERN_ERR "PA:%u:%d:%u \n", i, |
| 4143 | start, pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4144 | } |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 4145 | ext4_unlock_group(sb, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4146 | |
| 4147 | if (grp->bb_free == 0) |
| 4148 | continue; |
Akira Fujita | 1c71850 | 2009-07-05 23:04:36 -0400 | [diff] [blame] | 4149 | printk(KERN_ERR "%u: %d/%d \n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4150 | i, grp->bb_free, grp->bb_fragments); |
| 4151 | } |
| 4152 | printk(KERN_ERR "\n"); |
| 4153 | } |
| 4154 | #else |
| 4155 | static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
| 4156 | { |
| 4157 | return; |
| 4158 | } |
| 4159 | #endif |
| 4160 | |
| 4161 | /* |
| 4162 | * We use locality group preallocation for small size file. The size of the |
| 4163 | * file is determined by the current size or the resulting size after |
| 4164 | * allocation which ever is larger |
| 4165 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 4166 | * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4167 | */ |
| 4168 | static void ext4_mb_group_or_file(struct ext4_allocation_context *ac) |
| 4169 | { |
| 4170 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 4171 | int bsbits = ac->ac_sb->s_blocksize_bits; |
| 4172 | loff_t size, isize; |
| 4173 | |
| 4174 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 4175 | return; |
| 4176 | |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 4177 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 4178 | return; |
| 4179 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4180 | size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len; |
Theodore Ts'o | 5079748 | 2009-09-18 13:34:02 -0400 | [diff] [blame] | 4181 | isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1) |
| 4182 | >> bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4183 | size = max(size, isize); |
| 4184 | |
Theodore Ts'o | 5079748 | 2009-09-18 13:34:02 -0400 | [diff] [blame] | 4185 | if ((size == isize) && |
| 4186 | !ext4_fs_is_busy(sbi) && |
| 4187 | (atomic_read(&ac->ac_inode->i_writecount) == 0)) { |
| 4188 | ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC; |
| 4189 | return; |
| 4190 | } |
| 4191 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4192 | /* don't use group allocation for large files */ |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 4193 | if (size >= sbi->s_mb_stream_request) { |
| 4194 | ac->ac_flags |= EXT4_MB_STREAM_ALLOC; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4195 | return; |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 4196 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4197 | |
| 4198 | BUG_ON(ac->ac_lg != NULL); |
| 4199 | /* |
| 4200 | * locality group prealloc space are per cpu. The reason for having |
| 4201 | * per cpu locality group is to reduce the contention between block |
| 4202 | * request from multiple CPUs. |
| 4203 | */ |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 4204 | ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id()); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4205 | |
| 4206 | /* we're going to use group allocation */ |
| 4207 | ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC; |
| 4208 | |
| 4209 | /* serialize all allocations in the group */ |
| 4210 | mutex_lock(&ac->ac_lg->lg_mutex); |
| 4211 | } |
| 4212 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4213 | static noinline_for_stack int |
| 4214 | ext4_mb_initialize_context(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4215 | struct ext4_allocation_request *ar) |
| 4216 | { |
| 4217 | struct super_block *sb = ar->inode->i_sb; |
| 4218 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 4219 | struct ext4_super_block *es = sbi->s_es; |
| 4220 | ext4_group_t group; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4221 | unsigned int len; |
| 4222 | ext4_fsblk_t goal; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4223 | ext4_grpblk_t block; |
| 4224 | |
| 4225 | /* we can't allocate > group size */ |
| 4226 | len = ar->len; |
| 4227 | |
| 4228 | /* just a dirty hack to filter too big requests */ |
| 4229 | if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10) |
| 4230 | len = EXT4_BLOCKS_PER_GROUP(sb) - 10; |
| 4231 | |
| 4232 | /* start searching from the goal */ |
| 4233 | goal = ar->goal; |
| 4234 | if (goal < le32_to_cpu(es->s_first_data_block) || |
| 4235 | goal >= ext4_blocks_count(es)) |
| 4236 | goal = le32_to_cpu(es->s_first_data_block); |
| 4237 | ext4_get_group_no_and_offset(sb, goal, &group, &block); |
| 4238 | |
| 4239 | /* set up allocation goals */ |
Theodore Ts'o | 833576b | 2009-07-13 09:45:52 -0400 | [diff] [blame] | 4240 | memset(ac, 0, sizeof(struct ext4_allocation_context)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4241 | ac->ac_b_ex.fe_logical = ar->logical; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4242 | ac->ac_status = AC_STATUS_CONTINUE; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4243 | ac->ac_sb = sb; |
| 4244 | ac->ac_inode = ar->inode; |
| 4245 | ac->ac_o_ex.fe_logical = ar->logical; |
| 4246 | ac->ac_o_ex.fe_group = group; |
| 4247 | ac->ac_o_ex.fe_start = block; |
| 4248 | ac->ac_o_ex.fe_len = len; |
| 4249 | ac->ac_g_ex.fe_logical = ar->logical; |
| 4250 | ac->ac_g_ex.fe_group = group; |
| 4251 | ac->ac_g_ex.fe_start = block; |
| 4252 | ac->ac_g_ex.fe_len = len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4253 | ac->ac_flags = ar->flags; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4254 | |
| 4255 | /* we have to define context: we'll we work with a file or |
| 4256 | * locality group. this is a policy, actually */ |
| 4257 | ext4_mb_group_or_file(ac); |
| 4258 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 4259 | mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, " |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4260 | "left: %u/%u, right %u/%u to %swritable\n", |
| 4261 | (unsigned) ar->len, (unsigned) ar->logical, |
| 4262 | (unsigned) ar->goal, ac->ac_flags, ac->ac_2order, |
| 4263 | (unsigned) ar->lleft, (unsigned) ar->pleft, |
| 4264 | (unsigned) ar->lright, (unsigned) ar->pright, |
| 4265 | atomic_read(&ar->inode->i_writecount) ? "" : "non-"); |
| 4266 | return 0; |
| 4267 | |
| 4268 | } |
| 4269 | |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4270 | static noinline_for_stack void |
| 4271 | ext4_mb_discard_lg_preallocations(struct super_block *sb, |
| 4272 | struct ext4_locality_group *lg, |
| 4273 | int order, int total_entries) |
| 4274 | { |
| 4275 | ext4_group_t group = 0; |
| 4276 | struct ext4_buddy e4b; |
| 4277 | struct list_head discard_list; |
| 4278 | struct ext4_prealloc_space *pa, *tmp; |
| 4279 | struct ext4_allocation_context *ac; |
| 4280 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 4281 | mb_debug(1, "discard locality group preallocation\n"); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4282 | |
| 4283 | INIT_LIST_HEAD(&discard_list); |
| 4284 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4285 | if (ac) |
| 4286 | ac->ac_sb = sb; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4287 | |
| 4288 | spin_lock(&lg->lg_prealloc_lock); |
| 4289 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order], |
| 4290 | pa_inode_list) { |
| 4291 | spin_lock(&pa->pa_lock); |
| 4292 | if (atomic_read(&pa->pa_count)) { |
| 4293 | /* |
| 4294 | * This is the pa that we just used |
| 4295 | * for block allocation. So don't |
| 4296 | * free that |
| 4297 | */ |
| 4298 | spin_unlock(&pa->pa_lock); |
| 4299 | continue; |
| 4300 | } |
| 4301 | if (pa->pa_deleted) { |
| 4302 | spin_unlock(&pa->pa_lock); |
| 4303 | continue; |
| 4304 | } |
| 4305 | /* only lg prealloc space */ |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4306 | BUG_ON(pa->pa_type != MB_GROUP_PA); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4307 | |
| 4308 | /* seems this one can be freed ... */ |
| 4309 | pa->pa_deleted = 1; |
| 4310 | spin_unlock(&pa->pa_lock); |
| 4311 | |
| 4312 | list_del_rcu(&pa->pa_inode_list); |
| 4313 | list_add(&pa->u.pa_tmp_list, &discard_list); |
| 4314 | |
| 4315 | total_entries--; |
| 4316 | if (total_entries <= 5) { |
| 4317 | /* |
| 4318 | * we want to keep only 5 entries |
| 4319 | * allowing it to grow to 8. This |
| 4320 | * mak sure we don't call discard |
| 4321 | * soon for this list. |
| 4322 | */ |
| 4323 | break; |
| 4324 | } |
| 4325 | } |
| 4326 | spin_unlock(&lg->lg_prealloc_lock); |
| 4327 | |
| 4328 | list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) { |
| 4329 | |
| 4330 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL); |
| 4331 | if (ext4_mb_load_buddy(sb, group, &e4b)) { |
| 4332 | ext4_error(sb, __func__, "Error in loading buddy " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 4333 | "information for %u", group); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4334 | continue; |
| 4335 | } |
| 4336 | ext4_lock_group(sb, group); |
| 4337 | list_del(&pa->pa_group_list); |
| 4338 | ext4_mb_release_group_pa(&e4b, pa, ac); |
| 4339 | ext4_unlock_group(sb, group); |
| 4340 | |
| 4341 | ext4_mb_release_desc(&e4b); |
| 4342 | list_del(&pa->u.pa_tmp_list); |
| 4343 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4344 | } |
| 4345 | if (ac) |
| 4346 | kmem_cache_free(ext4_ac_cachep, ac); |
| 4347 | } |
| 4348 | |
| 4349 | /* |
| 4350 | * We have incremented pa_count. So it cannot be freed at this |
| 4351 | * point. Also we hold lg_mutex. So no parallel allocation is |
| 4352 | * possible from this lg. That means pa_free cannot be updated. |
| 4353 | * |
| 4354 | * A parallel ext4_mb_discard_group_preallocations is possible. |
| 4355 | * which can cause the lg_prealloc_list to be updated. |
| 4356 | */ |
| 4357 | |
| 4358 | static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac) |
| 4359 | { |
| 4360 | int order, added = 0, lg_prealloc_count = 1; |
| 4361 | struct super_block *sb = ac->ac_sb; |
| 4362 | struct ext4_locality_group *lg = ac->ac_lg; |
| 4363 | struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa; |
| 4364 | |
| 4365 | order = fls(pa->pa_free) - 1; |
| 4366 | if (order > PREALLOC_TB_SIZE - 1) |
| 4367 | /* The max size of hash table is PREALLOC_TB_SIZE */ |
| 4368 | order = PREALLOC_TB_SIZE - 1; |
| 4369 | /* Add the prealloc space to lg */ |
| 4370 | rcu_read_lock(); |
| 4371 | list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order], |
| 4372 | pa_inode_list) { |
| 4373 | spin_lock(&tmp_pa->pa_lock); |
| 4374 | if (tmp_pa->pa_deleted) { |
Theodore Ts'o | e7c9e3e | 2009-03-27 19:43:21 -0400 | [diff] [blame] | 4375 | spin_unlock(&tmp_pa->pa_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4376 | continue; |
| 4377 | } |
| 4378 | if (!added && pa->pa_free < tmp_pa->pa_free) { |
| 4379 | /* Add to the tail of the previous entry */ |
| 4380 | list_add_tail_rcu(&pa->pa_inode_list, |
| 4381 | &tmp_pa->pa_inode_list); |
| 4382 | added = 1; |
| 4383 | /* |
| 4384 | * we want to count the total |
| 4385 | * number of entries in the list |
| 4386 | */ |
| 4387 | } |
| 4388 | spin_unlock(&tmp_pa->pa_lock); |
| 4389 | lg_prealloc_count++; |
| 4390 | } |
| 4391 | if (!added) |
| 4392 | list_add_tail_rcu(&pa->pa_inode_list, |
| 4393 | &lg->lg_prealloc_list[order]); |
| 4394 | rcu_read_unlock(); |
| 4395 | |
| 4396 | /* Now trim the list to be not more than 8 elements */ |
| 4397 | if (lg_prealloc_count > 8) { |
| 4398 | ext4_mb_discard_lg_preallocations(sb, lg, |
| 4399 | order, lg_prealloc_count); |
| 4400 | return; |
| 4401 | } |
| 4402 | return ; |
| 4403 | } |
| 4404 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4405 | /* |
| 4406 | * release all resource we used in allocation |
| 4407 | */ |
| 4408 | static int ext4_mb_release_context(struct ext4_allocation_context *ac) |
| 4409 | { |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4410 | struct ext4_prealloc_space *pa = ac->ac_pa; |
| 4411 | if (pa) { |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4412 | if (pa->pa_type == MB_GROUP_PA) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4413 | /* see comment in ext4_mb_use_group_pa() */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4414 | spin_lock(&pa->pa_lock); |
| 4415 | pa->pa_pstart += ac->ac_b_ex.fe_len; |
| 4416 | pa->pa_lstart += ac->ac_b_ex.fe_len; |
| 4417 | pa->pa_free -= ac->ac_b_ex.fe_len; |
| 4418 | pa->pa_len -= ac->ac_b_ex.fe_len; |
| 4419 | spin_unlock(&pa->pa_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4420 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4421 | } |
Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 4422 | if (ac->alloc_semp) |
| 4423 | up_read(ac->alloc_semp); |
Aneesh Kumar K.V | ba44391 | 2009-02-10 11:14:34 -0500 | [diff] [blame] | 4424 | if (pa) { |
| 4425 | /* |
| 4426 | * We want to add the pa to the right bucket. |
| 4427 | * Remove it from the list and while adding |
| 4428 | * make sure the list to which we are adding |
| 4429 | * doesn't grow big. We need to release |
| 4430 | * alloc_semp before calling ext4_mb_add_n_trim() |
| 4431 | */ |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4432 | if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) { |
Aneesh Kumar K.V | ba44391 | 2009-02-10 11:14:34 -0500 | [diff] [blame] | 4433 | spin_lock(pa->pa_obj_lock); |
| 4434 | list_del_rcu(&pa->pa_inode_list); |
| 4435 | spin_unlock(pa->pa_obj_lock); |
| 4436 | ext4_mb_add_n_trim(ac); |
| 4437 | } |
| 4438 | ext4_mb_put_pa(ac, ac->ac_sb, pa); |
| 4439 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4440 | if (ac->ac_bitmap_page) |
| 4441 | page_cache_release(ac->ac_bitmap_page); |
| 4442 | if (ac->ac_buddy_page) |
| 4443 | page_cache_release(ac->ac_buddy_page); |
| 4444 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
| 4445 | mutex_unlock(&ac->ac_lg->lg_mutex); |
| 4446 | ext4_mb_collect_stats(ac); |
| 4447 | return 0; |
| 4448 | } |
| 4449 | |
| 4450 | static int ext4_mb_discard_preallocations(struct super_block *sb, int needed) |
| 4451 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4452 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4453 | int ret; |
| 4454 | int freed = 0; |
| 4455 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4456 | trace_ext4_mb_discard_preallocations(sb, needed); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4457 | for (i = 0; i < ngroups && needed > 0; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4458 | ret = ext4_mb_discard_group_preallocations(sb, i, needed); |
| 4459 | freed += ret; |
| 4460 | needed -= ret; |
| 4461 | } |
| 4462 | |
| 4463 | return freed; |
| 4464 | } |
| 4465 | |
| 4466 | /* |
| 4467 | * Main entry point into mballoc to allocate blocks |
| 4468 | * it tries to use preallocation first, then falls back |
| 4469 | * to usual allocation |
| 4470 | */ |
| 4471 | ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, |
| 4472 | struct ext4_allocation_request *ar, int *errp) |
| 4473 | { |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 4474 | int freed; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4475 | struct ext4_allocation_context *ac = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4476 | struct ext4_sb_info *sbi; |
| 4477 | struct super_block *sb; |
| 4478 | ext4_fsblk_t block = 0; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4479 | unsigned int inquota = 0; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4480 | unsigned int reserv_blks = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4481 | |
| 4482 | sb = ar->inode->i_sb; |
| 4483 | sbi = EXT4_SB(sb); |
| 4484 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4485 | trace_ext4_request_blocks(ar); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 4486 | |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4487 | /* |
| 4488 | * For delayed allocation, we could skip the ENOSPC and |
| 4489 | * EDQUOT check, as blocks and quotas have been already |
| 4490 | * reserved when data being copied into pagecache. |
| 4491 | */ |
| 4492 | if (EXT4_I(ar->inode)->i_delalloc_reserved_flag) |
| 4493 | ar->flags |= EXT4_MB_DELALLOC_RESERVED; |
| 4494 | else { |
| 4495 | /* Without delayed allocation we need to verify |
| 4496 | * there is enough free blocks to do block allocation |
| 4497 | * and verify allocation doesn't exceed the quota limits. |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4498 | */ |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 4499 | while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) { |
| 4500 | /* let others to free the space */ |
| 4501 | yield(); |
| 4502 | ar->len = ar->len >> 1; |
| 4503 | } |
| 4504 | if (!ar->len) { |
Aneesh Kumar K.V | a30d542a | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 4505 | *errp = -ENOSPC; |
| 4506 | return 0; |
| 4507 | } |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 4508 | reserv_blks = ar->len; |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 4509 | while (ar->len && vfs_dq_alloc_block(ar->inode, ar->len)) { |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4510 | ar->flags |= EXT4_MB_HINT_NOPREALLOC; |
| 4511 | ar->len--; |
| 4512 | } |
| 4513 | inquota = ar->len; |
| 4514 | if (ar->len == 0) { |
| 4515 | *errp = -EDQUOT; |
| 4516 | goto out3; |
| 4517 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4518 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4519 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4520 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 833576b | 2009-07-13 09:45:52 -0400 | [diff] [blame] | 4521 | if (!ac) { |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4522 | ar->len = 0; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4523 | *errp = -ENOMEM; |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4524 | goto out1; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4525 | } |
| 4526 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4527 | *errp = ext4_mb_initialize_context(ac, ar); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4528 | if (*errp) { |
| 4529 | ar->len = 0; |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4530 | goto out2; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4531 | } |
| 4532 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4533 | ac->ac_op = EXT4_MB_HISTORY_PREALLOC; |
| 4534 | if (!ext4_mb_use_preallocated(ac)) { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4535 | ac->ac_op = EXT4_MB_HISTORY_ALLOC; |
| 4536 | ext4_mb_normalize_request(ac, ar); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4537 | repeat: |
| 4538 | /* allocate space in core */ |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4539 | ext4_mb_regular_allocator(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4540 | |
| 4541 | /* as we've just preallocated more space than |
| 4542 | * user requested orinally, we store allocated |
| 4543 | * space in a special descriptor */ |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4544 | if (ac->ac_status == AC_STATUS_FOUND && |
| 4545 | ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) |
| 4546 | ext4_mb_new_preallocation(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4547 | } |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4548 | if (likely(ac->ac_status == AC_STATUS_FOUND)) { |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 4549 | *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 4550 | if (*errp == -EAGAIN) { |
Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 4551 | /* |
| 4552 | * drop the reference that we took |
| 4553 | * in ext4_mb_use_best_found |
| 4554 | */ |
| 4555 | ext4_mb_release_context(ac); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 4556 | ac->ac_b_ex.fe_group = 0; |
| 4557 | ac->ac_b_ex.fe_start = 0; |
| 4558 | ac->ac_b_ex.fe_len = 0; |
| 4559 | ac->ac_status = AC_STATUS_CONTINUE; |
| 4560 | goto repeat; |
| 4561 | } else if (*errp) { |
| 4562 | ac->ac_b_ex.fe_len = 0; |
| 4563 | ar->len = 0; |
| 4564 | ext4_mb_show_ac(ac); |
| 4565 | } else { |
| 4566 | block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 4567 | ar->len = ac->ac_b_ex.fe_len; |
| 4568 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4569 | } else { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4570 | freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4571 | if (freed) |
| 4572 | goto repeat; |
| 4573 | *errp = -ENOSPC; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4574 | ac->ac_b_ex.fe_len = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4575 | ar->len = 0; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4576 | ext4_mb_show_ac(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4577 | } |
| 4578 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4579 | ext4_mb_release_context(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4580 | |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4581 | out2: |
| 4582 | kmem_cache_free(ext4_ac_cachep, ac); |
| 4583 | out1: |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4584 | if (inquota && ar->len < inquota) |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 4585 | vfs_dq_free_block(ar->inode, inquota - ar->len); |
Aneesh Kumar K.V | 0087d9f | 2009-01-05 21:49:12 -0500 | [diff] [blame] | 4586 | out3: |
| 4587 | if (!ar->len) { |
| 4588 | if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag) |
| 4589 | /* release all the reserved blocks if non delalloc */ |
| 4590 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, |
| 4591 | reserv_blks); |
| 4592 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4593 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4594 | trace_ext4_allocate_blocks(ar, (unsigned long long)block); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 4595 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4596 | return block; |
| 4597 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4598 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4599 | /* |
| 4600 | * We can merge two free data extents only if the physical blocks |
| 4601 | * are contiguous, AND the extents were freed by the same transaction, |
| 4602 | * AND the blocks are associated with the same group. |
| 4603 | */ |
| 4604 | static int can_merge(struct ext4_free_data *entry1, |
| 4605 | struct ext4_free_data *entry2) |
| 4606 | { |
| 4607 | if ((entry1->t_tid == entry2->t_tid) && |
| 4608 | (entry1->group == entry2->group) && |
| 4609 | ((entry1->start_blk + entry1->count) == entry2->start_blk)) |
| 4610 | return 1; |
| 4611 | return 0; |
| 4612 | } |
| 4613 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4614 | static noinline_for_stack int |
| 4615 | ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b, |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4616 | struct ext4_free_data *new_entry) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4617 | { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4618 | ext4_grpblk_t block; |
| 4619 | struct ext4_free_data *entry; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4620 | struct ext4_group_info *db = e4b->bd_info; |
| 4621 | struct super_block *sb = e4b->bd_sb; |
| 4622 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4623 | struct rb_node **n = &db->bb_free_root.rb_node, *node; |
| 4624 | struct rb_node *parent = NULL, *new_node; |
| 4625 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4626 | BUG_ON(!ext4_handle_valid(handle)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4627 | BUG_ON(e4b->bd_bitmap_page == NULL); |
| 4628 | BUG_ON(e4b->bd_buddy_page == NULL); |
| 4629 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4630 | new_node = &new_entry->node; |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4631 | block = new_entry->start_blk; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4632 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4633 | if (!*n) { |
| 4634 | /* first free block exent. We need to |
| 4635 | protect buddy cache from being freed, |
| 4636 | * otherwise we'll refresh it from |
| 4637 | * on-disk bitmap and lose not-yet-available |
| 4638 | * blocks */ |
| 4639 | page_cache_get(e4b->bd_buddy_page); |
| 4640 | page_cache_get(e4b->bd_bitmap_page); |
| 4641 | } |
| 4642 | while (*n) { |
| 4643 | parent = *n; |
| 4644 | entry = rb_entry(parent, struct ext4_free_data, node); |
| 4645 | if (block < entry->start_blk) |
| 4646 | n = &(*n)->rb_left; |
| 4647 | else if (block >= (entry->start_blk + entry->count)) |
| 4648 | n = &(*n)->rb_right; |
| 4649 | else { |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 4650 | ext4_grp_locked_error(sb, e4b->bd_group, __func__, |
| 4651 | "Double free of blocks %d (%d %d)", |
| 4652 | block, entry->start_blk, entry->count); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4653 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4654 | } |
| 4655 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4656 | |
| 4657 | rb_link_node(new_node, parent, n); |
| 4658 | rb_insert_color(new_node, &db->bb_free_root); |
| 4659 | |
| 4660 | /* Now try to see the extent can be merged to left and right */ |
| 4661 | node = rb_prev(new_node); |
| 4662 | if (node) { |
| 4663 | entry = rb_entry(node, struct ext4_free_data, node); |
| 4664 | if (can_merge(entry, new_entry)) { |
| 4665 | new_entry->start_blk = entry->start_blk; |
| 4666 | new_entry->count += entry->count; |
| 4667 | rb_erase(node, &(db->bb_free_root)); |
| 4668 | spin_lock(&sbi->s_md_lock); |
| 4669 | list_del(&entry->list); |
| 4670 | spin_unlock(&sbi->s_md_lock); |
| 4671 | kmem_cache_free(ext4_free_ext_cachep, entry); |
| 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | node = rb_next(new_node); |
| 4676 | if (node) { |
| 4677 | entry = rb_entry(node, struct ext4_free_data, node); |
| 4678 | if (can_merge(new_entry, entry)) { |
| 4679 | new_entry->count += entry->count; |
| 4680 | rb_erase(node, &(db->bb_free_root)); |
| 4681 | spin_lock(&sbi->s_md_lock); |
| 4682 | list_del(&entry->list); |
| 4683 | spin_unlock(&sbi->s_md_lock); |
| 4684 | kmem_cache_free(ext4_free_ext_cachep, entry); |
| 4685 | } |
| 4686 | } |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 4687 | /* Add the extent to transaction's private list */ |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4688 | spin_lock(&sbi->s_md_lock); |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 4689 | list_add(&new_entry->list, &handle->h_transaction->t_private_list); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4690 | spin_unlock(&sbi->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4691 | return 0; |
| 4692 | } |
| 4693 | |
| 4694 | /* |
| 4695 | * Main entry point into mballoc to free blocks |
| 4696 | */ |
| 4697 | void ext4_mb_free_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 4698 | ext4_fsblk_t block, unsigned long count, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4699 | int metadata, unsigned long *freed) |
| 4700 | { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 4701 | struct buffer_head *bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4702 | struct super_block *sb = inode->i_sb; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4703 | struct ext4_allocation_context *ac = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4704 | struct ext4_group_desc *gdp; |
| 4705 | struct ext4_super_block *es; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4706 | unsigned int overflow; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4707 | ext4_grpblk_t bit; |
| 4708 | struct buffer_head *gd_bh; |
| 4709 | ext4_group_t block_group; |
| 4710 | struct ext4_sb_info *sbi; |
| 4711 | struct ext4_buddy e4b; |
| 4712 | int err = 0; |
| 4713 | int ret; |
| 4714 | |
| 4715 | *freed = 0; |
| 4716 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4717 | sbi = EXT4_SB(sb); |
| 4718 | es = EXT4_SB(sb)->s_es; |
| 4719 | if (block < le32_to_cpu(es->s_first_data_block) || |
| 4720 | block + count < block || |
| 4721 | block + count > ext4_blocks_count(es)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 4722 | ext4_error(sb, __func__, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4723 | "Freeing blocks not in datazone - " |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 4724 | "block = %llu, count = %lu", block, count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4725 | goto error_return; |
| 4726 | } |
| 4727 | |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 4728 | ext4_debug("freeing block %llu\n", block); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4729 | trace_ext4_free_blocks(inode, block, count, metadata); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4730 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4731 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
| 4732 | if (ac) { |
| 4733 | ac->ac_op = EXT4_MB_HISTORY_FREE; |
| 4734 | ac->ac_inode = inode; |
| 4735 | ac->ac_sb = sb; |
| 4736 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4737 | |
| 4738 | do_more: |
| 4739 | overflow = 0; |
| 4740 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
| 4741 | |
| 4742 | /* |
| 4743 | * Check to see if we are freeing blocks across a group |
| 4744 | * boundary. |
| 4745 | */ |
| 4746 | if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) { |
| 4747 | overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb); |
| 4748 | count -= overflow; |
| 4749 | } |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4750 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4751 | if (!bitmap_bh) { |
| 4752 | err = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4753 | goto error_return; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4754 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4755 | gdp = ext4_get_group_desc(sb, block_group, &gd_bh); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4756 | if (!gdp) { |
| 4757 | err = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4758 | goto error_return; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4759 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4760 | |
| 4761 | if (in_range(ext4_block_bitmap(sb, gdp), block, count) || |
| 4762 | in_range(ext4_inode_bitmap(sb, gdp), block, count) || |
| 4763 | in_range(block, ext4_inode_table(sb, gdp), |
| 4764 | EXT4_SB(sb)->s_itb_per_group) || |
| 4765 | in_range(block + count - 1, ext4_inode_table(sb, gdp), |
| 4766 | EXT4_SB(sb)->s_itb_per_group)) { |
| 4767 | |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 4768 | ext4_error(sb, __func__, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4769 | "Freeing blocks in system zone - " |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 4770 | "Block = %llu, count = %lu", block, count); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 4771 | /* err = 0. ext4_std_error should be a no op */ |
| 4772 | goto error_return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
| 4776 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
| 4777 | if (err) |
| 4778 | goto error_return; |
| 4779 | |
| 4780 | /* |
| 4781 | * We are about to modify some metadata. Call the journal APIs |
| 4782 | * to unshare ->b_data if a currently-committing transaction is |
| 4783 | * using it |
| 4784 | */ |
| 4785 | BUFFER_TRACE(gd_bh, "get_write_access"); |
| 4786 | err = ext4_journal_get_write_access(handle, gd_bh); |
| 4787 | if (err) |
| 4788 | goto error_return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4789 | #ifdef AGGRESSIVE_CHECK |
| 4790 | { |
| 4791 | int i; |
| 4792 | for (i = 0; i < count; i++) |
| 4793 | BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data)); |
| 4794 | } |
| 4795 | #endif |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4796 | if (ac) { |
| 4797 | ac->ac_b_ex.fe_group = block_group; |
| 4798 | ac->ac_b_ex.fe_start = bit; |
| 4799 | ac->ac_b_ex.fe_len = count; |
| 4800 | ext4_mb_store_history(ac); |
| 4801 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4802 | |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 4803 | err = ext4_mb_load_buddy(sb, block_group, &e4b); |
| 4804 | if (err) |
| 4805 | goto error_return; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4806 | if (metadata && ext4_handle_valid(handle)) { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4807 | struct ext4_free_data *new_entry; |
| 4808 | /* |
| 4809 | * blocks being freed are metadata. these blocks shouldn't |
| 4810 | * be used until this transaction is committed |
| 4811 | */ |
| 4812 | new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS); |
| 4813 | new_entry->start_blk = bit; |
| 4814 | new_entry->group = block_group; |
| 4815 | new_entry->count = count; |
| 4816 | new_entry->t_tid = handle->h_transaction->t_tid; |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 4817 | |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4818 | ext4_lock_group(sb, block_group); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 4819 | mb_clear_bits(bitmap_bh->b_data, bit, count); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4820 | ext4_mb_free_metadata(handle, &e4b, new_entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4821 | } else { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4822 | /* need to update group_info->bb_free and bitmap |
| 4823 | * with group lock held. generate_buddy look at |
| 4824 | * them with group lock_held |
| 4825 | */ |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 4826 | ext4_lock_group(sb, block_group); |
| 4827 | mb_clear_bits(bitmap_bh->b_data, bit, count); |
Shen Feng | 7e5a8cd | 2008-07-13 21:03:31 -0400 | [diff] [blame] | 4828 | mb_free_blocks(inode, &e4b, bit, count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4829 | ext4_mb_return_to_preallocation(inode, &e4b, block, count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4830 | } |
| 4831 | |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 4832 | ret = ext4_free_blks_count(sb, gdp) + count; |
| 4833 | ext4_free_blks_set(sb, gdp, ret); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4834 | gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 4835 | ext4_unlock_group(sb, block_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4836 | percpu_counter_add(&sbi->s_freeblocks_counter, count); |
| 4837 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4838 | if (sbi->s_log_groups_per_flex) { |
| 4839 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 4840 | atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4841 | } |
| 4842 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4843 | ext4_mb_release_desc(&e4b); |
| 4844 | |
| 4845 | *freed += count; |
| 4846 | |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4847 | /* We dirtied the bitmap block */ |
| 4848 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
| 4849 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
| 4850 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4851 | /* And the group descriptor block */ |
| 4852 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4853 | ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4854 | if (!err) |
| 4855 | err = ret; |
| 4856 | |
| 4857 | if (overflow && !err) { |
| 4858 | block += count; |
| 4859 | count = overflow; |
| 4860 | put_bh(bitmap_bh); |
| 4861 | goto do_more; |
| 4862 | } |
| 4863 | sb->s_dirt = 1; |
| 4864 | error_return: |
| 4865 | brelse(bitmap_bh); |
| 4866 | ext4_std_error(sb, err); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4867 | if (ac) |
| 4868 | kmem_cache_free(ext4_ac_cachep, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4869 | return; |
| 4870 | } |