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 |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 145 | * stripe size (sbi->s_stripe), we try to search for contiguous 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, |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 626 | void *buddy, ext4_grpblk_t first, ext4_grpblk_t len, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 627 | struct ext4_group_info *grp) |
| 628 | { |
| 629 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 630 | ext4_grpblk_t min; |
| 631 | ext4_grpblk_t max; |
| 632 | ext4_grpblk_t chunk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 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); |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 666 | ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb); |
| 667 | ext4_grpblk_t i = 0; |
| 668 | ext4_grpblk_t first; |
| 669 | ext4_grpblk_t len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 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 | |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 913 | static noinline_for_stack |
| 914 | int ext4_mb_init_group(struct super_block *sb, ext4_group_t group) |
| 915 | { |
| 916 | |
| 917 | int ret = 0; |
| 918 | void *bitmap; |
| 919 | int blocks_per_page; |
| 920 | int block, pnum, poff; |
| 921 | int num_grp_locked = 0; |
| 922 | struct ext4_group_info *this_grp; |
| 923 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 924 | struct inode *inode = sbi->s_buddy_cache; |
| 925 | struct page *page = NULL, *bitmap_page = NULL; |
| 926 | |
| 927 | mb_debug(1, "init group %u\n", group); |
| 928 | blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; |
| 929 | this_grp = ext4_get_group_info(sb, group); |
| 930 | /* |
Aneesh Kumar K.V | 08c3a81 | 2009-09-09 23:50:17 -0400 | [diff] [blame] | 931 | * This ensures that we don't reinit the buddy cache |
| 932 | * page which map to the group from which we are already |
| 933 | * allocating. If we are looking at the buddy cache we would |
| 934 | * have taken a reference using ext4_mb_load_buddy and that |
| 935 | * would have taken the alloc_sem lock. |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 936 | */ |
| 937 | num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group); |
| 938 | if (!EXT4_MB_GRP_NEED_INIT(this_grp)) { |
| 939 | /* |
| 940 | * somebody initialized the group |
| 941 | * return without doing anything |
| 942 | */ |
| 943 | ret = 0; |
| 944 | goto err; |
| 945 | } |
| 946 | /* |
| 947 | * the buddy cache inode stores the block bitmap |
| 948 | * and buddy information in consecutive blocks. |
| 949 | * So for each group we need two blocks. |
| 950 | */ |
| 951 | block = group * 2; |
| 952 | pnum = block / blocks_per_page; |
| 953 | poff = block % blocks_per_page; |
| 954 | page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); |
| 955 | if (page) { |
| 956 | BUG_ON(page->mapping != inode->i_mapping); |
| 957 | ret = ext4_mb_init_cache(page, NULL); |
| 958 | if (ret) { |
| 959 | unlock_page(page); |
| 960 | goto err; |
| 961 | } |
| 962 | unlock_page(page); |
| 963 | } |
| 964 | if (page == NULL || !PageUptodate(page)) { |
| 965 | ret = -EIO; |
| 966 | goto err; |
| 967 | } |
| 968 | mark_page_accessed(page); |
| 969 | bitmap_page = page; |
| 970 | bitmap = page_address(page) + (poff * sb->s_blocksize); |
| 971 | |
| 972 | /* init buddy cache */ |
| 973 | block++; |
| 974 | pnum = block / blocks_per_page; |
| 975 | poff = block % blocks_per_page; |
| 976 | page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); |
| 977 | if (page == bitmap_page) { |
| 978 | /* |
| 979 | * If both the bitmap and buddy are in |
| 980 | * the same page we don't need to force |
| 981 | * init the buddy |
| 982 | */ |
| 983 | unlock_page(page); |
| 984 | } else if (page) { |
| 985 | BUG_ON(page->mapping != inode->i_mapping); |
| 986 | ret = ext4_mb_init_cache(page, bitmap); |
| 987 | if (ret) { |
| 988 | unlock_page(page); |
| 989 | goto err; |
| 990 | } |
| 991 | unlock_page(page); |
| 992 | } |
| 993 | if (page == NULL || !PageUptodate(page)) { |
| 994 | ret = -EIO; |
| 995 | goto err; |
| 996 | } |
| 997 | mark_page_accessed(page); |
| 998 | err: |
| 999 | ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked); |
| 1000 | if (bitmap_page) |
| 1001 | page_cache_release(bitmap_page); |
| 1002 | if (page) |
| 1003 | page_cache_release(page); |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1007 | static noinline_for_stack int |
| 1008 | ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group, |
| 1009 | struct ext4_buddy *e4b) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1010 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1011 | int blocks_per_page; |
| 1012 | int block; |
| 1013 | int pnum; |
| 1014 | int poff; |
| 1015 | struct page *page; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1016 | int ret; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1017 | struct ext4_group_info *grp; |
| 1018 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 1019 | struct inode *inode = sbi->s_buddy_cache; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1020 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 1021 | mb_debug(1, "load group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1022 | |
| 1023 | blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1024 | grp = ext4_get_group_info(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1025 | |
| 1026 | e4b->bd_blkbits = sb->s_blocksize_bits; |
| 1027 | e4b->bd_info = ext4_get_group_info(sb, group); |
| 1028 | e4b->bd_sb = sb; |
| 1029 | e4b->bd_group = group; |
| 1030 | e4b->bd_buddy_page = NULL; |
| 1031 | e4b->bd_bitmap_page = NULL; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1032 | e4b->alloc_semp = &grp->alloc_sem; |
| 1033 | |
| 1034 | /* Take the read lock on the group alloc |
| 1035 | * sem. This would make sure a parallel |
| 1036 | * ext4_mb_init_group happening on other |
| 1037 | * groups mapped by the page is blocked |
| 1038 | * till we are done with allocation |
| 1039 | */ |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1040 | repeat_load_buddy: |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1041 | down_read(e4b->alloc_semp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1042 | |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1043 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { |
| 1044 | /* we need to check for group need init flag |
| 1045 | * with alloc_semp held so that we can be sure |
| 1046 | * that new blocks didn't get added to the group |
| 1047 | * when we are loading the buddy cache |
| 1048 | */ |
| 1049 | up_read(e4b->alloc_semp); |
| 1050 | /* |
| 1051 | * we need full data about the group |
| 1052 | * to make a good selection |
| 1053 | */ |
| 1054 | ret = ext4_mb_init_group(sb, group); |
| 1055 | if (ret) |
| 1056 | return ret; |
| 1057 | goto repeat_load_buddy; |
| 1058 | } |
| 1059 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1060 | /* |
| 1061 | * the buddy cache inode stores the block bitmap |
| 1062 | * and buddy information in consecutive blocks. |
| 1063 | * So for each group we need two blocks. |
| 1064 | */ |
| 1065 | block = group * 2; |
| 1066 | pnum = block / blocks_per_page; |
| 1067 | poff = block % blocks_per_page; |
| 1068 | |
| 1069 | /* we could use find_or_create_page(), but it locks page |
| 1070 | * what we'd like to avoid in fast path ... */ |
| 1071 | page = find_get_page(inode->i_mapping, pnum); |
| 1072 | if (page == NULL || !PageUptodate(page)) { |
| 1073 | if (page) |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1074 | /* |
| 1075 | * drop the page reference and try |
| 1076 | * to get the page with lock. If we |
| 1077 | * are not uptodate that implies |
| 1078 | * somebody just created the page but |
| 1079 | * is yet to initialize the same. So |
| 1080 | * wait for it to initialize. |
| 1081 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1082 | page_cache_release(page); |
| 1083 | page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); |
| 1084 | if (page) { |
| 1085 | BUG_ON(page->mapping != inode->i_mapping); |
| 1086 | if (!PageUptodate(page)) { |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1087 | ret = ext4_mb_init_cache(page, NULL); |
| 1088 | if (ret) { |
| 1089 | unlock_page(page); |
| 1090 | goto err; |
| 1091 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1092 | mb_cmp_bitmaps(e4b, page_address(page) + |
| 1093 | (poff * sb->s_blocksize)); |
| 1094 | } |
| 1095 | unlock_page(page); |
| 1096 | } |
| 1097 | } |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1098 | if (page == NULL || !PageUptodate(page)) { |
| 1099 | ret = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1100 | goto err; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1101 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1102 | e4b->bd_bitmap_page = page; |
| 1103 | e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize); |
| 1104 | mark_page_accessed(page); |
| 1105 | |
| 1106 | block++; |
| 1107 | pnum = block / blocks_per_page; |
| 1108 | poff = block % blocks_per_page; |
| 1109 | |
| 1110 | page = find_get_page(inode->i_mapping, pnum); |
| 1111 | if (page == NULL || !PageUptodate(page)) { |
| 1112 | if (page) |
| 1113 | page_cache_release(page); |
| 1114 | page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS); |
| 1115 | if (page) { |
| 1116 | BUG_ON(page->mapping != inode->i_mapping); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1117 | if (!PageUptodate(page)) { |
| 1118 | ret = ext4_mb_init_cache(page, e4b->bd_bitmap); |
| 1119 | if (ret) { |
| 1120 | unlock_page(page); |
| 1121 | goto err; |
| 1122 | } |
| 1123 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1124 | unlock_page(page); |
| 1125 | } |
| 1126 | } |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1127 | if (page == NULL || !PageUptodate(page)) { |
| 1128 | ret = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1129 | goto err; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1130 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1131 | e4b->bd_buddy_page = page; |
| 1132 | e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize); |
| 1133 | mark_page_accessed(page); |
| 1134 | |
| 1135 | BUG_ON(e4b->bd_bitmap_page == NULL); |
| 1136 | BUG_ON(e4b->bd_buddy_page == NULL); |
| 1137 | |
| 1138 | return 0; |
| 1139 | |
| 1140 | err: |
| 1141 | if (e4b->bd_bitmap_page) |
| 1142 | page_cache_release(e4b->bd_bitmap_page); |
| 1143 | if (e4b->bd_buddy_page) |
| 1144 | page_cache_release(e4b->bd_buddy_page); |
| 1145 | e4b->bd_buddy = NULL; |
| 1146 | e4b->bd_bitmap = NULL; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1147 | |
| 1148 | /* Done with the buddy cache */ |
| 1149 | up_read(e4b->alloc_semp); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1150 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static void ext4_mb_release_desc(struct ext4_buddy *e4b) |
| 1154 | { |
| 1155 | if (e4b->bd_bitmap_page) |
| 1156 | page_cache_release(e4b->bd_bitmap_page); |
| 1157 | if (e4b->bd_buddy_page) |
| 1158 | page_cache_release(e4b->bd_buddy_page); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1159 | /* Done with the buddy cache */ |
Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 1160 | if (e4b->alloc_semp) |
| 1161 | up_read(e4b->alloc_semp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | |
| 1165 | static int mb_find_order_for_block(struct ext4_buddy *e4b, int block) |
| 1166 | { |
| 1167 | int order = 1; |
| 1168 | void *bb; |
| 1169 | |
| 1170 | BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b)); |
| 1171 | BUG_ON(block >= (1 << (e4b->bd_blkbits + 3))); |
| 1172 | |
| 1173 | bb = EXT4_MB_BUDDY(e4b); |
| 1174 | while (order <= e4b->bd_blkbits + 1) { |
| 1175 | block = block >> 1; |
| 1176 | if (!mb_test_bit(block, bb)) { |
| 1177 | /* this block is part of buddy of order 'order' */ |
| 1178 | return order; |
| 1179 | } |
| 1180 | bb += 1 << (e4b->bd_blkbits - order); |
| 1181 | order++; |
| 1182 | } |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1186 | static void mb_clear_bits(void *bm, int cur, int len) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1187 | { |
| 1188 | __u32 *addr; |
| 1189 | |
| 1190 | len = cur + len; |
| 1191 | while (cur < len) { |
| 1192 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1193 | /* fast path: clear whole word at once */ |
| 1194 | addr = bm + (cur >> 3); |
| 1195 | *addr = 0; |
| 1196 | cur += 32; |
| 1197 | continue; |
| 1198 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1199 | mb_clear_bit(cur, bm); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1200 | cur++; |
| 1201 | } |
| 1202 | } |
| 1203 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1204 | static void mb_set_bits(void *bm, int cur, int len) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1205 | { |
| 1206 | __u32 *addr; |
| 1207 | |
| 1208 | len = cur + len; |
| 1209 | while (cur < len) { |
| 1210 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1211 | /* fast path: set whole word at once */ |
| 1212 | addr = bm + (cur >> 3); |
| 1213 | *addr = 0xffffffff; |
| 1214 | cur += 32; |
| 1215 | continue; |
| 1216 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1217 | mb_set_bit(cur, bm); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1218 | cur++; |
| 1219 | } |
| 1220 | } |
| 1221 | |
Shen Feng | 7e5a8cd | 2008-07-13 21:03:31 -0400 | [diff] [blame] | 1222 | static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1223 | int first, int count) |
| 1224 | { |
| 1225 | int block = 0; |
| 1226 | int max = 0; |
| 1227 | int order; |
| 1228 | void *buddy; |
| 1229 | void *buddy2; |
| 1230 | struct super_block *sb = e4b->bd_sb; |
| 1231 | |
| 1232 | BUG_ON(first + count > (sb->s_blocksize << 3)); |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1233 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1234 | mb_check_buddy(e4b); |
| 1235 | mb_free_blocks_double(inode, e4b, first, count); |
| 1236 | |
| 1237 | e4b->bd_info->bb_free += count; |
| 1238 | if (first < e4b->bd_info->bb_first_free) |
| 1239 | e4b->bd_info->bb_first_free = first; |
| 1240 | |
| 1241 | /* let's maintain fragments counter */ |
| 1242 | if (first != 0) |
| 1243 | block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b)); |
| 1244 | if (first + count < EXT4_SB(sb)->s_mb_maxs[0]) |
| 1245 | max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b)); |
| 1246 | if (block && max) |
| 1247 | e4b->bd_info->bb_fragments--; |
| 1248 | else if (!block && !max) |
| 1249 | e4b->bd_info->bb_fragments++; |
| 1250 | |
| 1251 | /* let's maintain buddy itself */ |
| 1252 | while (count-- > 0) { |
| 1253 | block = first++; |
| 1254 | order = 0; |
| 1255 | |
| 1256 | if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) { |
| 1257 | ext4_fsblk_t blocknr; |
| 1258 | blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb); |
| 1259 | blocknr += block; |
| 1260 | blocknr += |
| 1261 | 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] | 1262 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 1263 | __func__, "double-free of inode" |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 1264 | " %lu's block %llu(bit %u in group %u)", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1265 | inode ? inode->i_ino : 0, blocknr, block, |
| 1266 | e4b->bd_group); |
| 1267 | } |
| 1268 | mb_clear_bit(block, EXT4_MB_BITMAP(e4b)); |
| 1269 | e4b->bd_info->bb_counters[order]++; |
| 1270 | |
| 1271 | /* start of the buddy */ |
| 1272 | buddy = mb_find_buddy(e4b, order, &max); |
| 1273 | |
| 1274 | do { |
| 1275 | block &= ~1UL; |
| 1276 | if (mb_test_bit(block, buddy) || |
| 1277 | mb_test_bit(block + 1, buddy)) |
| 1278 | break; |
| 1279 | |
| 1280 | /* both the buddies are free, try to coalesce them */ |
| 1281 | buddy2 = mb_find_buddy(e4b, order + 1, &max); |
| 1282 | |
| 1283 | if (!buddy2) |
| 1284 | break; |
| 1285 | |
| 1286 | if (order > 0) { |
| 1287 | /* for special purposes, we don't set |
| 1288 | * free bits in bitmap */ |
| 1289 | mb_set_bit(block, buddy); |
| 1290 | mb_set_bit(block + 1, buddy); |
| 1291 | } |
| 1292 | e4b->bd_info->bb_counters[order]--; |
| 1293 | e4b->bd_info->bb_counters[order]--; |
| 1294 | |
| 1295 | block = block >> 1; |
| 1296 | order++; |
| 1297 | e4b->bd_info->bb_counters[order]++; |
| 1298 | |
| 1299 | mb_clear_bit(block, buddy2); |
| 1300 | buddy = buddy2; |
| 1301 | } while (1); |
| 1302 | } |
| 1303 | mb_check_buddy(e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | static int mb_find_extent(struct ext4_buddy *e4b, int order, int block, |
| 1307 | int needed, struct ext4_free_extent *ex) |
| 1308 | { |
| 1309 | int next = block; |
| 1310 | int max; |
| 1311 | int ord; |
| 1312 | void *buddy; |
| 1313 | |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1314 | 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] | 1315 | BUG_ON(ex == NULL); |
| 1316 | |
| 1317 | buddy = mb_find_buddy(e4b, order, &max); |
| 1318 | BUG_ON(buddy == NULL); |
| 1319 | BUG_ON(block >= max); |
| 1320 | if (mb_test_bit(block, buddy)) { |
| 1321 | ex->fe_len = 0; |
| 1322 | ex->fe_start = 0; |
| 1323 | ex->fe_group = 0; |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | /* FIXME dorp order completely ? */ |
| 1328 | if (likely(order == 0)) { |
| 1329 | /* find actual order */ |
| 1330 | order = mb_find_order_for_block(e4b, block); |
| 1331 | block = block >> order; |
| 1332 | } |
| 1333 | |
| 1334 | ex->fe_len = 1 << order; |
| 1335 | ex->fe_start = block << order; |
| 1336 | ex->fe_group = e4b->bd_group; |
| 1337 | |
| 1338 | /* calc difference from given start */ |
| 1339 | next = next - ex->fe_start; |
| 1340 | ex->fe_len -= next; |
| 1341 | ex->fe_start += next; |
| 1342 | |
| 1343 | while (needed > ex->fe_len && |
| 1344 | (buddy = mb_find_buddy(e4b, order, &max))) { |
| 1345 | |
| 1346 | if (block + 1 >= max) |
| 1347 | break; |
| 1348 | |
| 1349 | next = (block + 1) * (1 << order); |
| 1350 | if (mb_test_bit(next, EXT4_MB_BITMAP(e4b))) |
| 1351 | break; |
| 1352 | |
| 1353 | ord = mb_find_order_for_block(e4b, next); |
| 1354 | |
| 1355 | order = ord; |
| 1356 | block = next >> order; |
| 1357 | ex->fe_len += 1 << order; |
| 1358 | } |
| 1359 | |
| 1360 | BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3))); |
| 1361 | return ex->fe_len; |
| 1362 | } |
| 1363 | |
| 1364 | static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) |
| 1365 | { |
| 1366 | int ord; |
| 1367 | int mlen = 0; |
| 1368 | int max = 0; |
| 1369 | int cur; |
| 1370 | int start = ex->fe_start; |
| 1371 | int len = ex->fe_len; |
| 1372 | unsigned ret = 0; |
| 1373 | int len0 = len; |
| 1374 | void *buddy; |
| 1375 | |
| 1376 | BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3)); |
| 1377 | BUG_ON(e4b->bd_group != ex->fe_group); |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1378 | 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] | 1379 | mb_check_buddy(e4b); |
| 1380 | mb_mark_used_double(e4b, start, len); |
| 1381 | |
| 1382 | e4b->bd_info->bb_free -= len; |
| 1383 | if (e4b->bd_info->bb_first_free == start) |
| 1384 | e4b->bd_info->bb_first_free += len; |
| 1385 | |
| 1386 | /* let's maintain fragments counter */ |
| 1387 | if (start != 0) |
| 1388 | mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b)); |
| 1389 | if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0]) |
| 1390 | max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b)); |
| 1391 | if (mlen && max) |
| 1392 | e4b->bd_info->bb_fragments++; |
| 1393 | else if (!mlen && !max) |
| 1394 | e4b->bd_info->bb_fragments--; |
| 1395 | |
| 1396 | /* let's maintain buddy itself */ |
| 1397 | while (len) { |
| 1398 | ord = mb_find_order_for_block(e4b, start); |
| 1399 | |
| 1400 | if (((start >> ord) << ord) == start && len >= (1 << ord)) { |
| 1401 | /* the whole chunk may be allocated at once! */ |
| 1402 | mlen = 1 << ord; |
| 1403 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1404 | BUG_ON((start >> ord) >= max); |
| 1405 | mb_set_bit(start >> ord, buddy); |
| 1406 | e4b->bd_info->bb_counters[ord]--; |
| 1407 | start += mlen; |
| 1408 | len -= mlen; |
| 1409 | BUG_ON(len < 0); |
| 1410 | continue; |
| 1411 | } |
| 1412 | |
| 1413 | /* store for history */ |
| 1414 | if (ret == 0) |
| 1415 | ret = len | (ord << 16); |
| 1416 | |
| 1417 | /* we have to split large buddy */ |
| 1418 | BUG_ON(ord <= 0); |
| 1419 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1420 | mb_set_bit(start >> ord, buddy); |
| 1421 | e4b->bd_info->bb_counters[ord]--; |
| 1422 | |
| 1423 | ord--; |
| 1424 | cur = (start >> ord) & ~1U; |
| 1425 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1426 | mb_clear_bit(cur, buddy); |
| 1427 | mb_clear_bit(cur + 1, buddy); |
| 1428 | e4b->bd_info->bb_counters[ord]++; |
| 1429 | e4b->bd_info->bb_counters[ord]++; |
| 1430 | } |
| 1431 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1432 | mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1433 | mb_check_buddy(e4b); |
| 1434 | |
| 1435 | return ret; |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | * Must be called under group lock! |
| 1440 | */ |
| 1441 | static void ext4_mb_use_best_found(struct ext4_allocation_context *ac, |
| 1442 | struct ext4_buddy *e4b) |
| 1443 | { |
| 1444 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 1445 | int ret; |
| 1446 | |
| 1447 | BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group); |
| 1448 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
| 1449 | |
| 1450 | ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len); |
| 1451 | ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical; |
| 1452 | ret = mb_mark_used(e4b, &ac->ac_b_ex); |
| 1453 | |
| 1454 | /* preallocation can change ac_b_ex, thus we store actually |
| 1455 | * allocated blocks for history */ |
| 1456 | ac->ac_f_ex = ac->ac_b_ex; |
| 1457 | |
| 1458 | ac->ac_status = AC_STATUS_FOUND; |
| 1459 | ac->ac_tail = ret & 0xffff; |
| 1460 | ac->ac_buddy = ret >> 16; |
| 1461 | |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 1462 | /* |
| 1463 | * take the page reference. We want the page to be pinned |
| 1464 | * so that we don't get a ext4_mb_init_cache_call for this |
| 1465 | * group until we update the bitmap. That would mean we |
| 1466 | * double allocate blocks. The reference is dropped |
| 1467 | * in ext4_mb_release_context |
| 1468 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1469 | ac->ac_bitmap_page = e4b->bd_bitmap_page; |
| 1470 | get_page(ac->ac_bitmap_page); |
| 1471 | ac->ac_buddy_page = e4b->bd_buddy_page; |
| 1472 | get_page(ac->ac_buddy_page); |
Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 1473 | /* on allocation we use ac to track the held semaphore */ |
| 1474 | ac->alloc_semp = e4b->alloc_semp; |
| 1475 | e4b->alloc_semp = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1476 | /* store last allocated for subsequent stream allocation */ |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 1477 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1478 | spin_lock(&sbi->s_md_lock); |
| 1479 | sbi->s_mb_last_group = ac->ac_f_ex.fe_group; |
| 1480 | sbi->s_mb_last_start = ac->ac_f_ex.fe_start; |
| 1481 | spin_unlock(&sbi->s_md_lock); |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * regular allocator, for general purposes allocation |
| 1487 | */ |
| 1488 | |
| 1489 | static void ext4_mb_check_limits(struct ext4_allocation_context *ac, |
| 1490 | struct ext4_buddy *e4b, |
| 1491 | int finish_group) |
| 1492 | { |
| 1493 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 1494 | struct ext4_free_extent *bex = &ac->ac_b_ex; |
| 1495 | struct ext4_free_extent *gex = &ac->ac_g_ex; |
| 1496 | struct ext4_free_extent ex; |
| 1497 | int max; |
| 1498 | |
Aneesh Kumar K.V | 032115f | 2009-01-05 21:34:30 -0500 | [diff] [blame] | 1499 | if (ac->ac_status == AC_STATUS_FOUND) |
| 1500 | return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1501 | /* |
| 1502 | * We don't want to scan for a whole year |
| 1503 | */ |
| 1504 | if (ac->ac_found > sbi->s_mb_max_to_scan && |
| 1505 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 1506 | ac->ac_status = AC_STATUS_BREAK; |
| 1507 | return; |
| 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * Haven't found good chunk so far, let's continue |
| 1512 | */ |
| 1513 | if (bex->fe_len < gex->fe_len) |
| 1514 | return; |
| 1515 | |
| 1516 | if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan) |
| 1517 | && bex->fe_group == e4b->bd_group) { |
| 1518 | /* recheck chunk's availability - we don't know |
| 1519 | * when it was found (within this lock-unlock |
| 1520 | * period or not) */ |
| 1521 | max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex); |
| 1522 | if (max >= gex->fe_len) { |
| 1523 | ext4_mb_use_best_found(ac, e4b); |
| 1524 | return; |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | /* |
| 1530 | * The routine checks whether found extent is good enough. If it is, |
| 1531 | * then the extent gets marked used and flag is set to the context |
| 1532 | * to stop scanning. Otherwise, the extent is compared with the |
| 1533 | * previous found extent and if new one is better, then it's stored |
| 1534 | * in the context. Later, the best found extent will be used, if |
| 1535 | * mballoc can't find good enough extent. |
| 1536 | * |
| 1537 | * FIXME: real allocation policy is to be designed yet! |
| 1538 | */ |
| 1539 | static void ext4_mb_measure_extent(struct ext4_allocation_context *ac, |
| 1540 | struct ext4_free_extent *ex, |
| 1541 | struct ext4_buddy *e4b) |
| 1542 | { |
| 1543 | struct ext4_free_extent *bex = &ac->ac_b_ex; |
| 1544 | struct ext4_free_extent *gex = &ac->ac_g_ex; |
| 1545 | |
| 1546 | BUG_ON(ex->fe_len <= 0); |
Eric Sandeen | 8d03c7a | 2009-03-14 11:51:46 -0400 | [diff] [blame] | 1547 | BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1548 | BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
| 1549 | BUG_ON(ac->ac_status != AC_STATUS_CONTINUE); |
| 1550 | |
| 1551 | ac->ac_found++; |
| 1552 | |
| 1553 | /* |
| 1554 | * The special case - take what you catch first |
| 1555 | */ |
| 1556 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 1557 | *bex = *ex; |
| 1558 | ext4_mb_use_best_found(ac, e4b); |
| 1559 | return; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * Let's check whether the chuck is good enough |
| 1564 | */ |
| 1565 | if (ex->fe_len == gex->fe_len) { |
| 1566 | *bex = *ex; |
| 1567 | ext4_mb_use_best_found(ac, e4b); |
| 1568 | return; |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * If this is first found extent, just store it in the context |
| 1573 | */ |
| 1574 | if (bex->fe_len == 0) { |
| 1575 | *bex = *ex; |
| 1576 | return; |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * If new found extent is better, store it in the context |
| 1581 | */ |
| 1582 | if (bex->fe_len < gex->fe_len) { |
| 1583 | /* if the request isn't satisfied, any found extent |
| 1584 | * larger than previous best one is better */ |
| 1585 | if (ex->fe_len > bex->fe_len) |
| 1586 | *bex = *ex; |
| 1587 | } else if (ex->fe_len > gex->fe_len) { |
| 1588 | /* if the request is satisfied, then we try to find |
| 1589 | * an extent that still satisfy the request, but is |
| 1590 | * smaller than previous one */ |
| 1591 | if (ex->fe_len < bex->fe_len) |
| 1592 | *bex = *ex; |
| 1593 | } |
| 1594 | |
| 1595 | ext4_mb_check_limits(ac, e4b, 0); |
| 1596 | } |
| 1597 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1598 | static noinline_for_stack |
| 1599 | int ext4_mb_try_best_found(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1600 | struct ext4_buddy *e4b) |
| 1601 | { |
| 1602 | struct ext4_free_extent ex = ac->ac_b_ex; |
| 1603 | ext4_group_t group = ex.fe_group; |
| 1604 | int max; |
| 1605 | int err; |
| 1606 | |
| 1607 | BUG_ON(ex.fe_len <= 0); |
| 1608 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); |
| 1609 | if (err) |
| 1610 | return err; |
| 1611 | |
| 1612 | ext4_lock_group(ac->ac_sb, group); |
| 1613 | max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex); |
| 1614 | |
| 1615 | if (max > 0) { |
| 1616 | ac->ac_b_ex = ex; |
| 1617 | ext4_mb_use_best_found(ac, e4b); |
| 1618 | } |
| 1619 | |
| 1620 | ext4_unlock_group(ac->ac_sb, group); |
| 1621 | ext4_mb_release_desc(e4b); |
| 1622 | |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1626 | static noinline_for_stack |
| 1627 | int ext4_mb_find_by_goal(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1628 | struct ext4_buddy *e4b) |
| 1629 | { |
| 1630 | ext4_group_t group = ac->ac_g_ex.fe_group; |
| 1631 | int max; |
| 1632 | int err; |
| 1633 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 1634 | struct ext4_super_block *es = sbi->s_es; |
| 1635 | struct ext4_free_extent ex; |
| 1636 | |
| 1637 | if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL)) |
| 1638 | return 0; |
| 1639 | |
| 1640 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); |
| 1641 | if (err) |
| 1642 | return err; |
| 1643 | |
| 1644 | ext4_lock_group(ac->ac_sb, group); |
| 1645 | max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start, |
| 1646 | ac->ac_g_ex.fe_len, &ex); |
| 1647 | |
| 1648 | if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) { |
| 1649 | ext4_fsblk_t start; |
| 1650 | |
| 1651 | start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) + |
| 1652 | ex.fe_start + le32_to_cpu(es->s_first_data_block); |
| 1653 | /* use do_div to get remainder (would be 64-bit modulo) */ |
| 1654 | if (do_div(start, sbi->s_stripe) == 0) { |
| 1655 | ac->ac_found++; |
| 1656 | ac->ac_b_ex = ex; |
| 1657 | ext4_mb_use_best_found(ac, e4b); |
| 1658 | } |
| 1659 | } else if (max >= ac->ac_g_ex.fe_len) { |
| 1660 | BUG_ON(ex.fe_len <= 0); |
| 1661 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); |
| 1662 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); |
| 1663 | ac->ac_found++; |
| 1664 | ac->ac_b_ex = ex; |
| 1665 | ext4_mb_use_best_found(ac, e4b); |
| 1666 | } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) { |
| 1667 | /* Sometimes, caller may want to merge even small |
| 1668 | * number of blocks to an existing extent */ |
| 1669 | BUG_ON(ex.fe_len <= 0); |
| 1670 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); |
| 1671 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); |
| 1672 | ac->ac_found++; |
| 1673 | ac->ac_b_ex = ex; |
| 1674 | ext4_mb_use_best_found(ac, e4b); |
| 1675 | } |
| 1676 | ext4_unlock_group(ac->ac_sb, group); |
| 1677 | ext4_mb_release_desc(e4b); |
| 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
| 1683 | * The routine scans buddy structures (not bitmap!) from given order |
| 1684 | * to max order and tries to find big enough chunk to satisfy the req |
| 1685 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1686 | static noinline_for_stack |
| 1687 | void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1688 | struct ext4_buddy *e4b) |
| 1689 | { |
| 1690 | struct super_block *sb = ac->ac_sb; |
| 1691 | struct ext4_group_info *grp = e4b->bd_info; |
| 1692 | void *buddy; |
| 1693 | int i; |
| 1694 | int k; |
| 1695 | int max; |
| 1696 | |
| 1697 | BUG_ON(ac->ac_2order <= 0); |
| 1698 | for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) { |
| 1699 | if (grp->bb_counters[i] == 0) |
| 1700 | continue; |
| 1701 | |
| 1702 | buddy = mb_find_buddy(e4b, i, &max); |
| 1703 | BUG_ON(buddy == NULL); |
| 1704 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 1705 | k = mb_find_next_zero_bit(buddy, max, 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1706 | BUG_ON(k >= max); |
| 1707 | |
| 1708 | ac->ac_found++; |
| 1709 | |
| 1710 | ac->ac_b_ex.fe_len = 1 << i; |
| 1711 | ac->ac_b_ex.fe_start = k << i; |
| 1712 | ac->ac_b_ex.fe_group = e4b->bd_group; |
| 1713 | |
| 1714 | ext4_mb_use_best_found(ac, e4b); |
| 1715 | |
| 1716 | BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len); |
| 1717 | |
| 1718 | if (EXT4_SB(sb)->s_mb_stats) |
| 1719 | atomic_inc(&EXT4_SB(sb)->s_bal_2orders); |
| 1720 | |
| 1721 | break; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * The routine scans the group and measures all found extents. |
| 1727 | * In order to optimize scanning, caller must pass number of |
| 1728 | * free blocks in the group, so the routine can know upper limit. |
| 1729 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1730 | static noinline_for_stack |
| 1731 | void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1732 | struct ext4_buddy *e4b) |
| 1733 | { |
| 1734 | struct super_block *sb = ac->ac_sb; |
| 1735 | void *bitmap = EXT4_MB_BITMAP(e4b); |
| 1736 | struct ext4_free_extent ex; |
| 1737 | int i; |
| 1738 | int free; |
| 1739 | |
| 1740 | free = e4b->bd_info->bb_free; |
| 1741 | BUG_ON(free <= 0); |
| 1742 | |
| 1743 | i = e4b->bd_info->bb_first_free; |
| 1744 | |
| 1745 | while (free && ac->ac_status == AC_STATUS_CONTINUE) { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 1746 | i = mb_find_next_zero_bit(bitmap, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1747 | EXT4_BLOCKS_PER_GROUP(sb), i); |
| 1748 | if (i >= EXT4_BLOCKS_PER_GROUP(sb)) { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1749 | /* |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 1750 | * IF we have corrupt bitmap, we won't find any |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1751 | * free blocks even though group info says we |
| 1752 | * we have free blocks |
| 1753 | */ |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 1754 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 1755 | __func__, "%d free blocks as per " |
Theodore Ts'o | fde4d95 | 2009-01-05 22:17:35 -0500 | [diff] [blame] | 1756 | "group info. But bitmap says 0", |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1757 | free); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1758 | break; |
| 1759 | } |
| 1760 | |
| 1761 | mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex); |
| 1762 | BUG_ON(ex.fe_len <= 0); |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1763 | if (free < ex.fe_len) { |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 1764 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 1765 | __func__, "%d free blocks as per " |
Theodore Ts'o | fde4d95 | 2009-01-05 22:17:35 -0500 | [diff] [blame] | 1766 | "group info. But got %d blocks", |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1767 | free, ex.fe_len); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 1768 | /* |
| 1769 | * The number of free blocks differs. This mostly |
| 1770 | * indicate that the bitmap is corrupt. So exit |
| 1771 | * without claiming the space. |
| 1772 | */ |
| 1773 | break; |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 1774 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1775 | |
| 1776 | ext4_mb_measure_extent(ac, &ex, e4b); |
| 1777 | |
| 1778 | i += ex.fe_len; |
| 1779 | free -= ex.fe_len; |
| 1780 | } |
| 1781 | |
| 1782 | ext4_mb_check_limits(ac, e4b, 1); |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * This is a special case for storages like raid5 |
| 1787 | * we try to find stripe-aligned chunks for stripe-size requests |
| 1788 | * XXX should do so at least for multiples of stripe size as well |
| 1789 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1790 | static noinline_for_stack |
| 1791 | void ext4_mb_scan_aligned(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1792 | struct ext4_buddy *e4b) |
| 1793 | { |
| 1794 | struct super_block *sb = ac->ac_sb; |
| 1795 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 1796 | void *bitmap = EXT4_MB_BITMAP(e4b); |
| 1797 | struct ext4_free_extent ex; |
| 1798 | ext4_fsblk_t first_group_block; |
| 1799 | ext4_fsblk_t a; |
| 1800 | ext4_grpblk_t i; |
| 1801 | int max; |
| 1802 | |
| 1803 | BUG_ON(sbi->s_stripe == 0); |
| 1804 | |
| 1805 | /* find first stripe-aligned block in group */ |
| 1806 | first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb) |
| 1807 | + le32_to_cpu(sbi->s_es->s_first_data_block); |
| 1808 | a = first_group_block + sbi->s_stripe - 1; |
| 1809 | do_div(a, sbi->s_stripe); |
| 1810 | i = (a * sbi->s_stripe) - first_group_block; |
| 1811 | |
| 1812 | while (i < EXT4_BLOCKS_PER_GROUP(sb)) { |
| 1813 | if (!mb_test_bit(i, bitmap)) { |
| 1814 | max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex); |
| 1815 | if (max >= sbi->s_stripe) { |
| 1816 | ac->ac_found++; |
| 1817 | ac->ac_b_ex = ex; |
| 1818 | ext4_mb_use_best_found(ac, e4b); |
| 1819 | break; |
| 1820 | } |
| 1821 | } |
| 1822 | i += sbi->s_stripe; |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | static int ext4_mb_good_group(struct ext4_allocation_context *ac, |
| 1827 | ext4_group_t group, int cr) |
| 1828 | { |
| 1829 | unsigned free, fragments; |
| 1830 | unsigned i, bits; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 1831 | int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1832 | struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); |
| 1833 | |
| 1834 | BUG_ON(cr < 0 || cr >= 4); |
| 1835 | BUG_ON(EXT4_MB_GRP_NEED_INIT(grp)); |
| 1836 | |
| 1837 | free = grp->bb_free; |
| 1838 | fragments = grp->bb_fragments; |
| 1839 | if (free == 0) |
| 1840 | return 0; |
| 1841 | if (fragments == 0) |
| 1842 | return 0; |
| 1843 | |
| 1844 | switch (cr) { |
| 1845 | case 0: |
| 1846 | BUG_ON(ac->ac_2order == 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1847 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 1848 | /* Avoid using the first bg of a flexgroup for data files */ |
| 1849 | if ((ac->ac_flags & EXT4_MB_HINT_DATA) && |
| 1850 | (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) && |
| 1851 | ((group % flex_size) == 0)) |
| 1852 | return 0; |
| 1853 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1854 | bits = ac->ac_sb->s_blocksize_bits + 1; |
| 1855 | for (i = ac->ac_2order; i <= bits; i++) |
| 1856 | if (grp->bb_counters[i] > 0) |
| 1857 | return 1; |
| 1858 | break; |
| 1859 | case 1: |
| 1860 | if ((free / fragments) >= ac->ac_g_ex.fe_len) |
| 1861 | return 1; |
| 1862 | break; |
| 1863 | case 2: |
| 1864 | if (free >= ac->ac_g_ex.fe_len) |
| 1865 | return 1; |
| 1866 | break; |
| 1867 | case 3: |
| 1868 | return 1; |
| 1869 | default: |
| 1870 | BUG(); |
| 1871 | } |
| 1872 | |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1876 | /* |
| 1877 | * lock the group_info alloc_sem of all the groups |
| 1878 | * belonging to the same buddy cache page. This |
| 1879 | * make sure other parallel operation on the buddy |
| 1880 | * cache doesn't happen whild holding the buddy cache |
| 1881 | * lock |
| 1882 | */ |
| 1883 | int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group) |
| 1884 | { |
| 1885 | int i; |
| 1886 | int block, pnum; |
| 1887 | int blocks_per_page; |
| 1888 | int groups_per_page; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1889 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1890 | ext4_group_t first_group; |
| 1891 | struct ext4_group_info *grp; |
| 1892 | |
| 1893 | blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; |
| 1894 | /* |
| 1895 | * the buddy cache inode stores the block bitmap |
| 1896 | * and buddy information in consecutive blocks. |
| 1897 | * So for each group we need two blocks. |
| 1898 | */ |
| 1899 | block = group * 2; |
| 1900 | pnum = block / blocks_per_page; |
| 1901 | first_group = pnum * blocks_per_page / 2; |
| 1902 | |
| 1903 | groups_per_page = blocks_per_page >> 1; |
| 1904 | if (groups_per_page == 0) |
| 1905 | groups_per_page = 1; |
| 1906 | /* read all groups the page covers into the cache */ |
| 1907 | for (i = 0; i < groups_per_page; i++) { |
| 1908 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1909 | if ((first_group + i) >= ngroups) |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1910 | break; |
| 1911 | grp = ext4_get_group_info(sb, first_group + i); |
| 1912 | /* take all groups write allocation |
| 1913 | * semaphore. This make sure there is |
| 1914 | * no block allocation going on in any |
| 1915 | * of that groups |
| 1916 | */ |
Aneesh Kumar K.V | b7be019 | 2008-11-23 23:51:53 -0500 | [diff] [blame] | 1917 | down_write_nested(&grp->alloc_sem, i); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1918 | } |
| 1919 | return i; |
| 1920 | } |
| 1921 | |
| 1922 | void ext4_mb_put_buddy_cache_lock(struct super_block *sb, |
| 1923 | ext4_group_t group, int locked_group) |
| 1924 | { |
| 1925 | int i; |
| 1926 | int block, pnum; |
| 1927 | int blocks_per_page; |
| 1928 | ext4_group_t first_group; |
| 1929 | struct ext4_group_info *grp; |
| 1930 | |
| 1931 | blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; |
| 1932 | /* |
| 1933 | * the buddy cache inode stores the block bitmap |
| 1934 | * and buddy information in consecutive blocks. |
| 1935 | * So for each group we need two blocks. |
| 1936 | */ |
| 1937 | block = group * 2; |
| 1938 | pnum = block / blocks_per_page; |
| 1939 | first_group = pnum * blocks_per_page / 2; |
| 1940 | /* release locks on all the groups */ |
| 1941 | for (i = 0; i < locked_group; i++) { |
| 1942 | |
| 1943 | grp = ext4_get_group_info(sb, first_group + i); |
| 1944 | /* take all groups write allocation |
| 1945 | * semaphore. This make sure there is |
| 1946 | * no block allocation going on in any |
| 1947 | * of that groups |
| 1948 | */ |
| 1949 | up_write(&grp->alloc_sem); |
| 1950 | } |
| 1951 | |
| 1952 | } |
| 1953 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1954 | static noinline_for_stack int |
| 1955 | ext4_mb_regular_allocator(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1956 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1957 | ext4_group_t ngroups, group, i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1958 | int cr; |
| 1959 | int err = 0; |
| 1960 | int bsbits; |
| 1961 | struct ext4_sb_info *sbi; |
| 1962 | struct super_block *sb; |
| 1963 | struct ext4_buddy e4b; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1964 | |
| 1965 | sb = ac->ac_sb; |
| 1966 | sbi = EXT4_SB(sb); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1967 | ngroups = ext4_get_groups_count(sb); |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 1968 | /* non-extent files are limited to low blocks/groups */ |
| 1969 | if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL)) |
| 1970 | ngroups = sbi->s_blockfile_groups; |
| 1971 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1972 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
| 1973 | |
| 1974 | /* first, try the goal */ |
| 1975 | err = ext4_mb_find_by_goal(ac, &e4b); |
| 1976 | if (err || ac->ac_status == AC_STATUS_FOUND) |
| 1977 | goto out; |
| 1978 | |
| 1979 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 1980 | goto out; |
| 1981 | |
| 1982 | /* |
| 1983 | * ac->ac2_order is set only if the fe_len is a power of 2 |
| 1984 | * if ac2_order is set we also set criteria to 0 so that we |
| 1985 | * try exact allocation using buddy. |
| 1986 | */ |
| 1987 | i = fls(ac->ac_g_ex.fe_len); |
| 1988 | ac->ac_2order = 0; |
| 1989 | /* |
| 1990 | * We search using buddy data only if the order of the request |
| 1991 | * is greater than equal to the sbi_s_mb_order2_reqs |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 1992 | * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1993 | */ |
| 1994 | if (i >= sbi->s_mb_order2_reqs) { |
| 1995 | /* |
| 1996 | * This should tell if fe_len is exactly power of 2 |
| 1997 | */ |
| 1998 | if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0) |
| 1999 | ac->ac_2order = i - 1; |
| 2000 | } |
| 2001 | |
| 2002 | bsbits = ac->ac_sb->s_blocksize_bits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2003 | |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 2004 | /* if stream allocation is enabled, use global goal */ |
| 2005 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2006 | /* TBD: may be hot point */ |
| 2007 | spin_lock(&sbi->s_md_lock); |
| 2008 | ac->ac_g_ex.fe_group = sbi->s_mb_last_group; |
| 2009 | ac->ac_g_ex.fe_start = sbi->s_mb_last_start; |
| 2010 | spin_unlock(&sbi->s_md_lock); |
| 2011 | } |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 2012 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2013 | /* Let's just scan groups to find more-less suitable blocks */ |
| 2014 | cr = ac->ac_2order ? 0 : 1; |
| 2015 | /* |
| 2016 | * cr == 0 try to get exact allocation, |
| 2017 | * cr == 3 try to get anything |
| 2018 | */ |
| 2019 | repeat: |
| 2020 | for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) { |
| 2021 | ac->ac_criteria = cr; |
Aneesh Kumar K.V | ed8f9c7 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2022 | /* |
| 2023 | * searching for the right group start |
| 2024 | * from the goal value specified |
| 2025 | */ |
| 2026 | group = ac->ac_g_ex.fe_group; |
| 2027 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2028 | for (i = 0; i < ngroups; group++, i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2029 | struct ext4_group_info *grp; |
| 2030 | struct ext4_group_desc *desc; |
| 2031 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2032 | if (group == ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2033 | group = 0; |
| 2034 | |
| 2035 | /* quick check to skip empty groups */ |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 2036 | grp = ext4_get_group_info(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2037 | if (grp->bb_free == 0) |
| 2038 | continue; |
| 2039 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2040 | err = ext4_mb_load_buddy(sb, group, &e4b); |
| 2041 | if (err) |
| 2042 | goto out; |
| 2043 | |
| 2044 | ext4_lock_group(sb, group); |
| 2045 | if (!ext4_mb_good_group(ac, group, cr)) { |
| 2046 | /* someone did allocation from this group */ |
| 2047 | ext4_unlock_group(sb, group); |
| 2048 | ext4_mb_release_desc(&e4b); |
| 2049 | continue; |
| 2050 | } |
| 2051 | |
| 2052 | ac->ac_groups_scanned++; |
| 2053 | desc = ext4_get_group_desc(sb, group, NULL); |
Theodore Ts'o | 75507ef | 2009-05-01 12:58:36 -0400 | [diff] [blame] | 2054 | if (cr == 0) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2055 | ext4_mb_simple_scan_group(ac, &e4b); |
| 2056 | else if (cr == 1 && |
| 2057 | ac->ac_g_ex.fe_len == sbi->s_stripe) |
| 2058 | ext4_mb_scan_aligned(ac, &e4b); |
| 2059 | else |
| 2060 | ext4_mb_complex_scan_group(ac, &e4b); |
| 2061 | |
| 2062 | ext4_unlock_group(sb, group); |
| 2063 | ext4_mb_release_desc(&e4b); |
| 2064 | |
| 2065 | if (ac->ac_status != AC_STATUS_CONTINUE) |
| 2066 | break; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND && |
| 2071 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 2072 | /* |
| 2073 | * We've been searching too long. Let's try to allocate |
| 2074 | * the best chunk we've found so far |
| 2075 | */ |
| 2076 | |
| 2077 | ext4_mb_try_best_found(ac, &e4b); |
| 2078 | if (ac->ac_status != AC_STATUS_FOUND) { |
| 2079 | /* |
| 2080 | * Someone more lucky has already allocated it. |
| 2081 | * The only thing we can do is just take first |
| 2082 | * found block(s) |
| 2083 | printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n"); |
| 2084 | */ |
| 2085 | ac->ac_b_ex.fe_group = 0; |
| 2086 | ac->ac_b_ex.fe_start = 0; |
| 2087 | ac->ac_b_ex.fe_len = 0; |
| 2088 | ac->ac_status = AC_STATUS_CONTINUE; |
| 2089 | ac->ac_flags |= EXT4_MB_HINT_FIRST; |
| 2090 | cr = 3; |
| 2091 | atomic_inc(&sbi->s_mb_lost_chunks); |
| 2092 | goto repeat; |
| 2093 | } |
| 2094 | } |
| 2095 | out: |
| 2096 | return err; |
| 2097 | } |
| 2098 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2099 | static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos) |
| 2100 | { |
| 2101 | struct super_block *sb = seq->private; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2102 | ext4_group_t group; |
| 2103 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2104 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2105 | return NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2106 | group = *pos + 1; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2107 | return (void *) ((unsigned long) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2111 | { |
| 2112 | struct super_block *sb = seq->private; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2113 | ext4_group_t group; |
| 2114 | |
| 2115 | ++*pos; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2116 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2117 | return NULL; |
| 2118 | group = *pos + 1; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2119 | return (void *) ((unsigned long) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v) |
| 2123 | { |
| 2124 | struct super_block *sb = seq->private; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2125 | ext4_group_t group = (ext4_group_t) ((unsigned long) v); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2126 | int i; |
| 2127 | int err; |
| 2128 | struct ext4_buddy e4b; |
| 2129 | struct sg { |
| 2130 | struct ext4_group_info info; |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 2131 | ext4_grpblk_t counters[16]; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2132 | } sg; |
| 2133 | |
| 2134 | group--; |
| 2135 | if (group == 0) |
| 2136 | seq_printf(seq, "#%-5s: %-5s %-5s %-5s " |
| 2137 | "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s " |
| 2138 | "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n", |
| 2139 | "group", "free", "frags", "first", |
| 2140 | "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6", |
| 2141 | "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13"); |
| 2142 | |
| 2143 | i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) + |
| 2144 | sizeof(struct ext4_group_info); |
| 2145 | err = ext4_mb_load_buddy(sb, group, &e4b); |
| 2146 | if (err) { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2147 | seq_printf(seq, "#%-5u: I/O error\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2148 | return 0; |
| 2149 | } |
| 2150 | ext4_lock_group(sb, group); |
| 2151 | memcpy(&sg, ext4_get_group_info(sb, group), i); |
| 2152 | ext4_unlock_group(sb, group); |
| 2153 | ext4_mb_release_desc(&e4b); |
| 2154 | |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2155 | seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2156 | sg.info.bb_fragments, sg.info.bb_first_free); |
| 2157 | for (i = 0; i <= 13; i++) |
| 2158 | seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ? |
| 2159 | sg.info.bb_counters[i] : 0); |
| 2160 | seq_printf(seq, " ]\n"); |
| 2161 | |
| 2162 | return 0; |
| 2163 | } |
| 2164 | |
| 2165 | static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v) |
| 2166 | { |
| 2167 | } |
| 2168 | |
Tobias Klauser | 7f1346a | 2009-09-05 09:28:54 -0400 | [diff] [blame] | 2169 | static const struct seq_operations ext4_mb_seq_groups_ops = { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2170 | .start = ext4_mb_seq_groups_start, |
| 2171 | .next = ext4_mb_seq_groups_next, |
| 2172 | .stop = ext4_mb_seq_groups_stop, |
| 2173 | .show = ext4_mb_seq_groups_show, |
| 2174 | }; |
| 2175 | |
| 2176 | static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file) |
| 2177 | { |
| 2178 | struct super_block *sb = PDE(inode)->data; |
| 2179 | int rc; |
| 2180 | |
| 2181 | rc = seq_open(file, &ext4_mb_seq_groups_ops); |
| 2182 | if (rc == 0) { |
| 2183 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 2184 | m->private = sb; |
| 2185 | } |
| 2186 | return rc; |
| 2187 | |
| 2188 | } |
| 2189 | |
Tobias Klauser | 7f1346a | 2009-09-05 09:28:54 -0400 | [diff] [blame] | 2190 | static const struct file_operations ext4_mb_seq_groups_fops = { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2191 | .owner = THIS_MODULE, |
| 2192 | .open = ext4_mb_seq_groups_open, |
| 2193 | .read = seq_read, |
| 2194 | .llseek = seq_lseek, |
| 2195 | .release = seq_release, |
| 2196 | }; |
| 2197 | |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2198 | |
| 2199 | /* 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] | 2200 | 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] | 2201 | struct ext4_group_desc *desc) |
| 2202 | { |
| 2203 | int i, len; |
| 2204 | int metalen = 0; |
| 2205 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2206 | struct ext4_group_info **meta_group_info; |
| 2207 | |
| 2208 | /* |
| 2209 | * First check if this group is the first of a reserved block. |
| 2210 | * If it's true, we have to allocate a new table of pointers |
| 2211 | * to ext4_group_info structures |
| 2212 | */ |
| 2213 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { |
| 2214 | metalen = sizeof(*meta_group_info) << |
| 2215 | EXT4_DESC_PER_BLOCK_BITS(sb); |
| 2216 | meta_group_info = kmalloc(metalen, GFP_KERNEL); |
| 2217 | if (meta_group_info == NULL) { |
| 2218 | printk(KERN_ERR "EXT4-fs: can't allocate mem for a " |
| 2219 | "buddy group\n"); |
| 2220 | goto exit_meta_group_info; |
| 2221 | } |
| 2222 | sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = |
| 2223 | meta_group_info; |
| 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * calculate needed size. if change bb_counters size, |
| 2228 | * don't forget about ext4_mb_generate_buddy() |
| 2229 | */ |
| 2230 | len = offsetof(typeof(**meta_group_info), |
| 2231 | bb_counters[sb->s_blocksize_bits + 2]); |
| 2232 | |
| 2233 | meta_group_info = |
| 2234 | sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]; |
| 2235 | i = group & (EXT4_DESC_PER_BLOCK(sb) - 1); |
| 2236 | |
| 2237 | meta_group_info[i] = kzalloc(len, GFP_KERNEL); |
| 2238 | if (meta_group_info[i] == NULL) { |
| 2239 | printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n"); |
| 2240 | goto exit_group_info; |
| 2241 | } |
| 2242 | set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, |
| 2243 | &(meta_group_info[i]->bb_state)); |
| 2244 | |
| 2245 | /* |
| 2246 | * initialize bb_free to be able to skip |
| 2247 | * empty groups without initialization |
| 2248 | */ |
| 2249 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 2250 | meta_group_info[i]->bb_free = |
| 2251 | ext4_free_blocks_after_init(sb, group, desc); |
| 2252 | } else { |
| 2253 | meta_group_info[i]->bb_free = |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 2254 | ext4_free_blks_count(sb, desc); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 2258 | init_rwsem(&meta_group_info[i]->alloc_sem); |
Joe Perches | 5a4a798 | 2009-07-05 22:33:08 -0400 | [diff] [blame] | 2259 | meta_group_info[i]->bb_free_root.rb_node = NULL; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2260 | |
| 2261 | #ifdef DOUBLE_CHECK |
| 2262 | { |
| 2263 | struct buffer_head *bh; |
| 2264 | meta_group_info[i]->bb_bitmap = |
| 2265 | kmalloc(sb->s_blocksize, GFP_KERNEL); |
| 2266 | BUG_ON(meta_group_info[i]->bb_bitmap == NULL); |
| 2267 | bh = ext4_read_block_bitmap(sb, group); |
| 2268 | BUG_ON(bh == NULL); |
| 2269 | memcpy(meta_group_info[i]->bb_bitmap, bh->b_data, |
| 2270 | sb->s_blocksize); |
| 2271 | put_bh(bh); |
| 2272 | } |
| 2273 | #endif |
| 2274 | |
| 2275 | return 0; |
| 2276 | |
| 2277 | exit_group_info: |
| 2278 | /* If a meta_group_info table has been allocated, release it now */ |
| 2279 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) |
| 2280 | kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]); |
| 2281 | exit_meta_group_info: |
| 2282 | return -ENOMEM; |
| 2283 | } /* ext4_mb_add_groupinfo */ |
| 2284 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2285 | static int ext4_mb_init_backend(struct super_block *sb) |
| 2286 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2287 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2288 | ext4_group_t i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2289 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2290 | struct ext4_super_block *es = sbi->s_es; |
| 2291 | int num_meta_group_infos; |
| 2292 | int num_meta_group_infos_max; |
| 2293 | int array_size; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2294 | struct ext4_group_desc *desc; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2295 | |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2296 | /* This is the number of blocks used by GDT */ |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2297 | num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) - |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2298 | 1) >> EXT4_DESC_PER_BLOCK_BITS(sb); |
| 2299 | |
| 2300 | /* |
| 2301 | * This is the total number of blocks used by GDT including |
| 2302 | * the number of reserved blocks for GDT. |
| 2303 | * The s_group_info array is allocated with this value |
| 2304 | * to allow a clean online resize without a complex |
| 2305 | * manipulation of pointer. |
| 2306 | * The drawback is the unused memory when no resize |
| 2307 | * occurs but it's very low in terms of pages |
| 2308 | * (see comments below) |
| 2309 | * Need to handle this properly when META_BG resizing is allowed |
| 2310 | */ |
| 2311 | num_meta_group_infos_max = num_meta_group_infos + |
| 2312 | le16_to_cpu(es->s_reserved_gdt_blocks); |
| 2313 | |
| 2314 | /* |
| 2315 | * array_size is the size of s_group_info array. We round it |
| 2316 | * to the next power of two because this approximation is done |
| 2317 | * internally by kmalloc so we can have some more memory |
| 2318 | * for free here (e.g. may be used for META_BG resize). |
| 2319 | */ |
| 2320 | array_size = 1; |
| 2321 | while (array_size < sizeof(*sbi->s_group_info) * |
| 2322 | num_meta_group_infos_max) |
| 2323 | array_size = array_size << 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2324 | /* An 8TB filesystem with 64-bit pointers requires a 4096 byte |
| 2325 | * kmalloc. A 128kb malloc should suffice for a 256TB filesystem. |
| 2326 | * So a two level scheme suffices for now. */ |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2327 | sbi->s_group_info = kmalloc(array_size, GFP_KERNEL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2328 | if (sbi->s_group_info == NULL) { |
| 2329 | printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n"); |
| 2330 | return -ENOMEM; |
| 2331 | } |
| 2332 | sbi->s_buddy_cache = new_inode(sb); |
| 2333 | if (sbi->s_buddy_cache == NULL) { |
| 2334 | printk(KERN_ERR "EXT4-fs: can't get new inode\n"); |
| 2335 | goto err_freesgi; |
| 2336 | } |
| 2337 | EXT4_I(sbi->s_buddy_cache)->i_disksize = 0; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2338 | for (i = 0; i < ngroups; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2339 | desc = ext4_get_group_desc(sb, i, NULL); |
| 2340 | if (desc == NULL) { |
| 2341 | printk(KERN_ERR |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2342 | "EXT4-fs: can't read descriptor %u\n", i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2343 | goto err_freebuddy; |
| 2344 | } |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2345 | if (ext4_mb_add_groupinfo(sb, i, desc) != 0) |
| 2346 | goto err_freebuddy; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | return 0; |
| 2350 | |
| 2351 | err_freebuddy: |
Roel Kluin | f1fa334 | 2008-04-29 22:01:15 -0400 | [diff] [blame] | 2352 | while (i-- > 0) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2353 | kfree(ext4_get_group_info(sb, i)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2354 | i = num_meta_group_infos; |
Roel Kluin | f1fa334 | 2008-04-29 22:01:15 -0400 | [diff] [blame] | 2355 | while (i-- > 0) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2356 | kfree(sbi->s_group_info[i]); |
| 2357 | iput(sbi->s_buddy_cache); |
| 2358 | err_freesgi: |
| 2359 | kfree(sbi->s_group_info); |
| 2360 | return -ENOMEM; |
| 2361 | } |
| 2362 | |
| 2363 | int ext4_mb_init(struct super_block *sb, int needs_recovery) |
| 2364 | { |
| 2365 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 2366 | unsigned i, j; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2367 | unsigned offset; |
| 2368 | unsigned max; |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2369 | int ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2370 | |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame] | 2371 | i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2372 | |
| 2373 | sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL); |
| 2374 | if (sbi->s_mb_offsets == NULL) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2375 | return -ENOMEM; |
| 2376 | } |
Yasunori Goto | ff7ef32 | 2008-12-17 00:48:39 -0500 | [diff] [blame] | 2377 | |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame] | 2378 | i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2379 | sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL); |
| 2380 | if (sbi->s_mb_maxs == NULL) { |
Dan Carpenter | a7b1944 | 2009-03-27 19:42:54 -0400 | [diff] [blame] | 2381 | kfree(sbi->s_mb_offsets); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2382 | return -ENOMEM; |
| 2383 | } |
| 2384 | |
| 2385 | /* order 0 is regular bitmap */ |
| 2386 | sbi->s_mb_maxs[0] = sb->s_blocksize << 3; |
| 2387 | sbi->s_mb_offsets[0] = 0; |
| 2388 | |
| 2389 | i = 1; |
| 2390 | offset = 0; |
| 2391 | max = sb->s_blocksize << 2; |
| 2392 | do { |
| 2393 | sbi->s_mb_offsets[i] = offset; |
| 2394 | sbi->s_mb_maxs[i] = max; |
| 2395 | offset += 1 << (sb->s_blocksize_bits - i); |
| 2396 | max = max >> 1; |
| 2397 | i++; |
| 2398 | } while (i <= sb->s_blocksize_bits + 1); |
| 2399 | |
| 2400 | /* init file for buddy data */ |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2401 | ret = ext4_mb_init_backend(sb); |
| 2402 | if (ret != 0) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2403 | kfree(sbi->s_mb_offsets); |
| 2404 | kfree(sbi->s_mb_maxs); |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2405 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | spin_lock_init(&sbi->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2409 | spin_lock_init(&sbi->s_bal_lock); |
| 2410 | |
| 2411 | sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN; |
| 2412 | sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN; |
| 2413 | sbi->s_mb_stats = MB_DEFAULT_STATS; |
| 2414 | sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD; |
| 2415 | sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2416 | sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC; |
| 2417 | |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2418 | sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2419 | if (sbi->s_locality_groups == NULL) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2420 | kfree(sbi->s_mb_offsets); |
| 2421 | kfree(sbi->s_mb_maxs); |
| 2422 | return -ENOMEM; |
| 2423 | } |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2424 | for_each_possible_cpu(i) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2425 | struct ext4_locality_group *lg; |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2426 | lg = per_cpu_ptr(sbi->s_locality_groups, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2427 | mutex_init(&lg->lg_mutex); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 2428 | for (j = 0; j < PREALLOC_TB_SIZE; j++) |
| 2429 | INIT_LIST_HEAD(&lg->lg_prealloc_list[j]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2430 | spin_lock_init(&lg->lg_prealloc_lock); |
| 2431 | } |
| 2432 | |
Theodore Ts'o | 296c355 | 2009-09-30 00:32:42 -0400 | [diff] [blame] | 2433 | if (sbi->s_proc) |
| 2434 | proc_create_data("mb_groups", S_IRUGO, sbi->s_proc, |
| 2435 | &ext4_mb_seq_groups_fops, sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2436 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2437 | if (sbi->s_journal) |
| 2438 | sbi->s_journal->j_commit_callback = release_blocks_on_commit; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2439 | return 0; |
| 2440 | } |
| 2441 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 2442 | /* need to called with the ext4 group lock held */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2443 | static void ext4_mb_cleanup_pa(struct ext4_group_info *grp) |
| 2444 | { |
| 2445 | struct ext4_prealloc_space *pa; |
| 2446 | struct list_head *cur, *tmp; |
| 2447 | int count = 0; |
| 2448 | |
| 2449 | list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) { |
| 2450 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 2451 | list_del(&pa->pa_group_list); |
| 2452 | count++; |
Aneesh Kumar K.V | 688f05a | 2008-10-13 12:14:14 -0400 | [diff] [blame] | 2453 | kmem_cache_free(ext4_pspace_cachep, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2454 | } |
| 2455 | if (count) |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2456 | mb_debug(1, "mballoc: %u PAs left\n", count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2457 | |
| 2458 | } |
| 2459 | |
| 2460 | int ext4_mb_release(struct super_block *sb) |
| 2461 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2462 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2463 | ext4_group_t i; |
| 2464 | int num_meta_group_infos; |
| 2465 | struct ext4_group_info *grinfo; |
| 2466 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2467 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2468 | if (sbi->s_group_info) { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2469 | for (i = 0; i < ngroups; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2470 | grinfo = ext4_get_group_info(sb, i); |
| 2471 | #ifdef DOUBLE_CHECK |
| 2472 | kfree(grinfo->bb_bitmap); |
| 2473 | #endif |
| 2474 | ext4_lock_group(sb, i); |
| 2475 | ext4_mb_cleanup_pa(grinfo); |
| 2476 | ext4_unlock_group(sb, i); |
| 2477 | kfree(grinfo); |
| 2478 | } |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2479 | num_meta_group_infos = (ngroups + |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2480 | EXT4_DESC_PER_BLOCK(sb) - 1) >> |
| 2481 | EXT4_DESC_PER_BLOCK_BITS(sb); |
| 2482 | for (i = 0; i < num_meta_group_infos; i++) |
| 2483 | kfree(sbi->s_group_info[i]); |
| 2484 | kfree(sbi->s_group_info); |
| 2485 | } |
| 2486 | kfree(sbi->s_mb_offsets); |
| 2487 | kfree(sbi->s_mb_maxs); |
| 2488 | if (sbi->s_buddy_cache) |
| 2489 | iput(sbi->s_buddy_cache); |
| 2490 | if (sbi->s_mb_stats) { |
| 2491 | printk(KERN_INFO |
| 2492 | "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n", |
| 2493 | atomic_read(&sbi->s_bal_allocated), |
| 2494 | atomic_read(&sbi->s_bal_reqs), |
| 2495 | atomic_read(&sbi->s_bal_success)); |
| 2496 | printk(KERN_INFO |
| 2497 | "EXT4-fs: mballoc: %u extents scanned, %u goal hits, " |
| 2498 | "%u 2^N hits, %u breaks, %u lost\n", |
| 2499 | atomic_read(&sbi->s_bal_ex_scanned), |
| 2500 | atomic_read(&sbi->s_bal_goals), |
| 2501 | atomic_read(&sbi->s_bal_2orders), |
| 2502 | atomic_read(&sbi->s_bal_breaks), |
| 2503 | atomic_read(&sbi->s_mb_lost_chunks)); |
| 2504 | printk(KERN_INFO |
| 2505 | "EXT4-fs: mballoc: %lu generated and it took %Lu\n", |
| 2506 | sbi->s_mb_buddies_generated++, |
| 2507 | sbi->s_mb_generation_time); |
| 2508 | printk(KERN_INFO |
| 2509 | "EXT4-fs: mballoc: %u preallocated, %u discarded\n", |
| 2510 | atomic_read(&sbi->s_mb_preallocated), |
| 2511 | atomic_read(&sbi->s_mb_discarded)); |
| 2512 | } |
| 2513 | |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2514 | free_percpu(sbi->s_locality_groups); |
Theodore Ts'o | 296c355 | 2009-09-30 00:32:42 -0400 | [diff] [blame] | 2515 | if (sbi->s_proc) |
| 2516 | remove_proc_entry("mb_groups", sbi->s_proc); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2517 | |
| 2518 | return 0; |
| 2519 | } |
| 2520 | |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2521 | /* |
| 2522 | * This function is called by the jbd2 layer once the commit has finished, |
| 2523 | * so we know we can free the blocks that were released with that commit. |
| 2524 | */ |
| 2525 | static void release_blocks_on_commit(journal_t *journal, transaction_t *txn) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2526 | { |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2527 | struct super_block *sb = journal->j_private; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2528 | struct ext4_buddy e4b; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2529 | struct ext4_group_info *db; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2530 | int err, count = 0, count2 = 0; |
| 2531 | struct ext4_free_data *entry; |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2532 | struct list_head *l, *ltmp; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2533 | |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2534 | list_for_each_safe(l, ltmp, &txn->t_private_list) { |
| 2535 | entry = list_entry(l, struct ext4_free_data, list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2536 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2537 | 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] | 2538 | entry->count, entry->group, entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2539 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2540 | err = ext4_mb_load_buddy(sb, entry->group, &e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2541 | /* we expect to find existing buddy because it's pinned */ |
| 2542 | BUG_ON(err != 0); |
| 2543 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2544 | db = e4b.bd_info; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2545 | /* 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] | 2546 | count += entry->count; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2547 | count2++; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2548 | ext4_lock_group(sb, entry->group); |
| 2549 | /* Take it out of per group rb tree */ |
| 2550 | rb_erase(&entry->node, &(db->bb_free_root)); |
| 2551 | mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count); |
| 2552 | |
| 2553 | if (!db->bb_free_root.rb_node) { |
| 2554 | /* No more items in the per group rb tree |
| 2555 | * balance refcounts from ext4_mb_free_metadata() |
| 2556 | */ |
| 2557 | page_cache_release(e4b.bd_buddy_page); |
| 2558 | page_cache_release(e4b.bd_bitmap_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2559 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2560 | ext4_unlock_group(sb, entry->group); |
Eric Sandeen | 5328e63 | 2009-11-19 14:25:42 -0500 | [diff] [blame] | 2561 | if (test_opt(sb, DISCARD)) { |
| 2562 | ext4_fsblk_t discard_block; |
| 2563 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2564 | |
Eric Sandeen | 5328e63 | 2009-11-19 14:25:42 -0500 | [diff] [blame] | 2565 | discard_block = (ext4_fsblk_t)entry->group * |
| 2566 | EXT4_BLOCKS_PER_GROUP(sb) |
| 2567 | + entry->start_blk |
| 2568 | + le32_to_cpu(es->s_first_data_block); |
| 2569 | trace_ext4_discard_blocks(sb, |
| 2570 | (unsigned long long)discard_block, |
| 2571 | entry->count); |
| 2572 | sb_issue_discard(sb, discard_block, entry->count); |
| 2573 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2574 | kmem_cache_free(ext4_free_ext_cachep, entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2575 | ext4_mb_release_desc(&e4b); |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 2576 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2577 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2578 | mb_debug(1, "freed %u blocks in %u structures\n", count, count2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2579 | } |
| 2580 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2581 | #ifdef CONFIG_EXT4_DEBUG |
| 2582 | u8 mb_enable_debug __read_mostly; |
| 2583 | |
| 2584 | static struct dentry *debugfs_dir; |
| 2585 | static struct dentry *debugfs_debug; |
| 2586 | |
| 2587 | static void __init ext4_create_debugfs_entry(void) |
| 2588 | { |
| 2589 | debugfs_dir = debugfs_create_dir("ext4", NULL); |
| 2590 | if (debugfs_dir) |
| 2591 | debugfs_debug = debugfs_create_u8("mballoc-debug", |
| 2592 | S_IRUGO | S_IWUSR, |
| 2593 | debugfs_dir, |
| 2594 | &mb_enable_debug); |
| 2595 | } |
| 2596 | |
| 2597 | static void ext4_remove_debugfs_entry(void) |
| 2598 | { |
| 2599 | debugfs_remove(debugfs_debug); |
| 2600 | debugfs_remove(debugfs_dir); |
| 2601 | } |
| 2602 | |
| 2603 | #else |
| 2604 | |
| 2605 | static void __init ext4_create_debugfs_entry(void) |
| 2606 | { |
| 2607 | } |
| 2608 | |
| 2609 | static void ext4_remove_debugfs_entry(void) |
| 2610 | { |
| 2611 | } |
| 2612 | |
| 2613 | #endif |
| 2614 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2615 | int __init init_ext4_mballoc(void) |
| 2616 | { |
| 2617 | ext4_pspace_cachep = |
| 2618 | kmem_cache_create("ext4_prealloc_space", |
| 2619 | sizeof(struct ext4_prealloc_space), |
| 2620 | 0, SLAB_RECLAIM_ACCOUNT, NULL); |
| 2621 | if (ext4_pspace_cachep == NULL) |
| 2622 | return -ENOMEM; |
| 2623 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 2624 | ext4_ac_cachep = |
| 2625 | kmem_cache_create("ext4_alloc_context", |
| 2626 | sizeof(struct ext4_allocation_context), |
| 2627 | 0, SLAB_RECLAIM_ACCOUNT, NULL); |
| 2628 | if (ext4_ac_cachep == NULL) { |
| 2629 | kmem_cache_destroy(ext4_pspace_cachep); |
| 2630 | return -ENOMEM; |
| 2631 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2632 | |
| 2633 | ext4_free_ext_cachep = |
| 2634 | kmem_cache_create("ext4_free_block_extents", |
| 2635 | sizeof(struct ext4_free_data), |
| 2636 | 0, SLAB_RECLAIM_ACCOUNT, NULL); |
| 2637 | if (ext4_free_ext_cachep == NULL) { |
| 2638 | kmem_cache_destroy(ext4_pspace_cachep); |
| 2639 | kmem_cache_destroy(ext4_ac_cachep); |
| 2640 | return -ENOMEM; |
| 2641 | } |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2642 | ext4_create_debugfs_entry(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2643 | return 0; |
| 2644 | } |
| 2645 | |
| 2646 | void exit_ext4_mballoc(void) |
| 2647 | { |
Jesper Dangaard Brouer | 3e03f9c | 2009-07-05 22:29:27 -0400 | [diff] [blame] | 2648 | /* |
| 2649 | * Wait for completion of call_rcu()'s on ext4_pspace_cachep |
| 2650 | * before destroying the slab cache. |
| 2651 | */ |
| 2652 | rcu_barrier(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2653 | kmem_cache_destroy(ext4_pspace_cachep); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 2654 | kmem_cache_destroy(ext4_ac_cachep); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 2655 | kmem_cache_destroy(ext4_free_ext_cachep); |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2656 | ext4_remove_debugfs_entry(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | |
| 2660 | /* |
| 2661 | * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps |
| 2662 | * Returns 0 if success or error code |
| 2663 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2664 | static noinline_for_stack int |
| 2665 | ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2666 | handle_t *handle, unsigned int reserv_blks) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2667 | { |
| 2668 | struct buffer_head *bitmap_bh = NULL; |
| 2669 | struct ext4_super_block *es; |
| 2670 | struct ext4_group_desc *gdp; |
| 2671 | struct buffer_head *gdp_bh; |
| 2672 | struct ext4_sb_info *sbi; |
| 2673 | struct super_block *sb; |
| 2674 | ext4_fsblk_t block; |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 2675 | int err, len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2676 | |
| 2677 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 2678 | BUG_ON(ac->ac_b_ex.fe_len <= 0); |
| 2679 | |
| 2680 | sb = ac->ac_sb; |
| 2681 | sbi = EXT4_SB(sb); |
| 2682 | es = sbi->s_es; |
| 2683 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2684 | |
| 2685 | err = -EIO; |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2686 | 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] | 2687 | if (!bitmap_bh) |
| 2688 | goto out_err; |
| 2689 | |
| 2690 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
| 2691 | if (err) |
| 2692 | goto out_err; |
| 2693 | |
| 2694 | err = -EIO; |
| 2695 | gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh); |
| 2696 | if (!gdp) |
| 2697 | goto out_err; |
| 2698 | |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2699 | 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] | 2700 | ext4_free_blks_count(sb, gdp)); |
Aneesh Kumar K.V | 03cddb8 | 2008-06-05 20:59:29 -0400 | [diff] [blame] | 2701 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2702 | err = ext4_journal_get_write_access(handle, gdp_bh); |
| 2703 | if (err) |
| 2704 | goto out_err; |
| 2705 | |
| 2706 | block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb) |
| 2707 | + ac->ac_b_ex.fe_start |
| 2708 | + le32_to_cpu(es->s_first_data_block); |
| 2709 | |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 2710 | len = ac->ac_b_ex.fe_len; |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 2711 | if (!ext4_data_block_valid(sbi, block, len)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame^] | 2712 | ext4_error(sb, "Allocating blocks %llu-%llu which overlap " |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 2713 | "fs metadata\n", block, block+len); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 2714 | /* File system mounted not to panic on error |
| 2715 | * Fix the bitmap and repeat the block allocation |
| 2716 | * We leak some of the blocks here. |
| 2717 | */ |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 2718 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
| 2719 | mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, |
| 2720 | ac->ac_b_ex.fe_len); |
| 2721 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2722 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 2723 | if (!err) |
| 2724 | err = -EAGAIN; |
| 2725 | goto out_err; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2726 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 2727 | |
| 2728 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2729 | #ifdef AGGRESSIVE_CHECK |
| 2730 | { |
| 2731 | int i; |
| 2732 | for (i = 0; i < ac->ac_b_ex.fe_len; i++) { |
| 2733 | BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i, |
| 2734 | bitmap_bh->b_data)); |
| 2735 | } |
| 2736 | } |
| 2737 | #endif |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 2738 | 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] | 2739 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 2740 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 2741 | ext4_free_blks_set(sb, gdp, |
| 2742 | ext4_free_blocks_after_init(sb, |
| 2743 | ac->ac_b_ex.fe_group, gdp)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2744 | } |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 2745 | len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len; |
| 2746 | ext4_free_blks_set(sb, gdp, len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2747 | 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] | 2748 | |
| 2749 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 2750 | 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] | 2751 | /* |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 2752 | * Now reduce the dirty block count also. Should not go negative |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2753 | */ |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 2754 | if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED)) |
| 2755 | /* release all the reserved blocks if non delalloc */ |
| 2756 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2757 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2758 | if (sbi->s_log_groups_per_flex) { |
| 2759 | ext4_group_t flex_group = ext4_flex_group(sbi, |
| 2760 | ac->ac_b_ex.fe_group); |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 2761 | atomic_sub(ac->ac_b_ex.fe_len, |
| 2762 | &sbi->s_flex_groups[flex_group].free_blocks); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2763 | } |
| 2764 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2765 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2766 | if (err) |
| 2767 | goto out_err; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2768 | err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2769 | |
| 2770 | out_err: |
| 2771 | sb->s_dirt = 1; |
Aneesh Kumar K.V | 42a10ad | 2008-02-10 01:07:28 -0500 | [diff] [blame] | 2772 | brelse(bitmap_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2773 | return err; |
| 2774 | } |
| 2775 | |
| 2776 | /* |
| 2777 | * here we normalize request for locality group |
| 2778 | * Group request are normalized to s_strip size if we set the same via mount |
| 2779 | * 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] | 2780 | * /sys/fs/ext4/<partition>/mb_group_prealloc |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2781 | * |
| 2782 | * XXX: should we try to preallocate more than the group has now? |
| 2783 | */ |
| 2784 | static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac) |
| 2785 | { |
| 2786 | struct super_block *sb = ac->ac_sb; |
| 2787 | struct ext4_locality_group *lg = ac->ac_lg; |
| 2788 | |
| 2789 | BUG_ON(lg == NULL); |
| 2790 | if (EXT4_SB(sb)->s_stripe) |
| 2791 | ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe; |
| 2792 | else |
| 2793 | 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] | 2794 | mb_debug(1, "#%u: goal %u blocks for locality group\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2795 | current->pid, ac->ac_g_ex.fe_len); |
| 2796 | } |
| 2797 | |
| 2798 | /* |
| 2799 | * Normalization means making request better in terms of |
| 2800 | * size and alignment |
| 2801 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2802 | static noinline_for_stack void |
| 2803 | ext4_mb_normalize_request(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2804 | struct ext4_allocation_request *ar) |
| 2805 | { |
| 2806 | int bsbits, max; |
| 2807 | ext4_lblk_t end; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2808 | loff_t size, orig_size, start_off; |
| 2809 | ext4_lblk_t start, orig_start; |
| 2810 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2811 | struct ext4_prealloc_space *pa; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2812 | |
| 2813 | /* do normalize only data requests, metadata requests |
| 2814 | do not need preallocation */ |
| 2815 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 2816 | return; |
| 2817 | |
| 2818 | /* sometime caller may want exact blocks */ |
| 2819 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 2820 | return; |
| 2821 | |
| 2822 | /* caller may indicate that preallocation isn't |
| 2823 | * required (it's a tail, for example) */ |
| 2824 | if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) |
| 2825 | return; |
| 2826 | |
| 2827 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) { |
| 2828 | ext4_mb_normalize_group_request(ac); |
| 2829 | return ; |
| 2830 | } |
| 2831 | |
| 2832 | bsbits = ac->ac_sb->s_blocksize_bits; |
| 2833 | |
| 2834 | /* first, let's learn actual file size |
| 2835 | * given current request is allocated */ |
| 2836 | size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len; |
| 2837 | size = size << bsbits; |
| 2838 | if (size < i_size_read(ac->ac_inode)) |
| 2839 | size = i_size_read(ac->ac_inode); |
| 2840 | |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 2841 | /* max size of free chunks */ |
| 2842 | max = 2 << bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2843 | |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 2844 | #define NRL_CHECK_SIZE(req, size, max, chunk_size) \ |
| 2845 | (req <= (size) || max <= (chunk_size)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2846 | |
| 2847 | /* first, try to predict filesize */ |
| 2848 | /* XXX: should this table be tunable? */ |
| 2849 | start_off = 0; |
| 2850 | if (size <= 16 * 1024) { |
| 2851 | size = 16 * 1024; |
| 2852 | } else if (size <= 32 * 1024) { |
| 2853 | size = 32 * 1024; |
| 2854 | } else if (size <= 64 * 1024) { |
| 2855 | size = 64 * 1024; |
| 2856 | } else if (size <= 128 * 1024) { |
| 2857 | size = 128 * 1024; |
| 2858 | } else if (size <= 256 * 1024) { |
| 2859 | size = 256 * 1024; |
| 2860 | } else if (size <= 512 * 1024) { |
| 2861 | size = 512 * 1024; |
| 2862 | } else if (size <= 1024 * 1024) { |
| 2863 | size = 1024 * 1024; |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 2864 | } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2865 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 2866 | (21 - bsbits)) << 21; |
| 2867 | size = 2 * 1024 * 1024; |
| 2868 | } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2869 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
| 2870 | (22 - bsbits)) << 22; |
| 2871 | size = 4 * 1024 * 1024; |
| 2872 | } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len, |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 2873 | (8<<20)>>bsbits, max, 8 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2874 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
| 2875 | (23 - bsbits)) << 23; |
| 2876 | size = 8 * 1024 * 1024; |
| 2877 | } else { |
| 2878 | start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits; |
| 2879 | size = ac->ac_o_ex.fe_len << bsbits; |
| 2880 | } |
| 2881 | orig_size = size = size >> bsbits; |
| 2882 | orig_start = start = start_off >> bsbits; |
| 2883 | |
| 2884 | /* don't cover already allocated blocks in selected range */ |
| 2885 | if (ar->pleft && start <= ar->lleft) { |
| 2886 | size -= ar->lleft + 1 - start; |
| 2887 | start = ar->lleft + 1; |
| 2888 | } |
| 2889 | if (ar->pright && start + size - 1 >= ar->lright) |
| 2890 | size -= start + size - ar->lright; |
| 2891 | |
| 2892 | end = start + size; |
| 2893 | |
| 2894 | /* check we don't cross already preallocated blocks */ |
| 2895 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2896 | 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] | 2897 | ext4_lblk_t pa_end; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2898 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2899 | if (pa->pa_deleted) |
| 2900 | continue; |
| 2901 | spin_lock(&pa->pa_lock); |
| 2902 | if (pa->pa_deleted) { |
| 2903 | spin_unlock(&pa->pa_lock); |
| 2904 | continue; |
| 2905 | } |
| 2906 | |
| 2907 | pa_end = pa->pa_lstart + pa->pa_len; |
| 2908 | |
| 2909 | /* PA must not overlap original request */ |
| 2910 | BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end || |
| 2911 | ac->ac_o_ex.fe_logical < pa->pa_lstart)); |
| 2912 | |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 2913 | /* skip PAs this normalized request doesn't overlap with */ |
| 2914 | if (pa->pa_lstart >= end || pa_end <= start) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2915 | spin_unlock(&pa->pa_lock); |
| 2916 | continue; |
| 2917 | } |
| 2918 | BUG_ON(pa->pa_lstart <= start && pa_end >= end); |
| 2919 | |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 2920 | /* adjust start or end to be adjacent to this pa */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2921 | if (pa_end <= ac->ac_o_ex.fe_logical) { |
| 2922 | BUG_ON(pa_end < start); |
| 2923 | start = pa_end; |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 2924 | } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2925 | BUG_ON(pa->pa_lstart > end); |
| 2926 | end = pa->pa_lstart; |
| 2927 | } |
| 2928 | spin_unlock(&pa->pa_lock); |
| 2929 | } |
| 2930 | rcu_read_unlock(); |
| 2931 | size = end - start; |
| 2932 | |
| 2933 | /* XXX: extra loop to check we really don't overlap preallocations */ |
| 2934 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2935 | 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] | 2936 | ext4_lblk_t pa_end; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2937 | spin_lock(&pa->pa_lock); |
| 2938 | if (pa->pa_deleted == 0) { |
| 2939 | pa_end = pa->pa_lstart + pa->pa_len; |
| 2940 | BUG_ON(!(start >= pa_end || end <= pa->pa_lstart)); |
| 2941 | } |
| 2942 | spin_unlock(&pa->pa_lock); |
| 2943 | } |
| 2944 | rcu_read_unlock(); |
| 2945 | |
| 2946 | if (start + size <= ac->ac_o_ex.fe_logical && |
| 2947 | start > ac->ac_o_ex.fe_logical) { |
| 2948 | printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n", |
| 2949 | (unsigned long) start, (unsigned long) size, |
| 2950 | (unsigned long) ac->ac_o_ex.fe_logical); |
| 2951 | } |
| 2952 | BUG_ON(start + size <= ac->ac_o_ex.fe_logical && |
| 2953 | start > ac->ac_o_ex.fe_logical); |
Eric Sandeen | 8d03c7a | 2009-03-14 11:51:46 -0400 | [diff] [blame] | 2954 | BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2955 | |
| 2956 | /* now prepare goal request */ |
| 2957 | |
| 2958 | /* XXX: is it better to align blocks WRT to logical |
| 2959 | * placement or satisfy big request as is */ |
| 2960 | ac->ac_g_ex.fe_logical = start; |
| 2961 | ac->ac_g_ex.fe_len = size; |
| 2962 | |
| 2963 | /* define goal start in order to merge */ |
| 2964 | if (ar->pright && (ar->lright == (start + size))) { |
| 2965 | /* merge to the right */ |
| 2966 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size, |
| 2967 | &ac->ac_f_ex.fe_group, |
| 2968 | &ac->ac_f_ex.fe_start); |
| 2969 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; |
| 2970 | } |
| 2971 | if (ar->pleft && (ar->lleft + 1 == start)) { |
| 2972 | /* merge to the left */ |
| 2973 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1, |
| 2974 | &ac->ac_f_ex.fe_group, |
| 2975 | &ac->ac_f_ex.fe_start); |
| 2976 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; |
| 2977 | } |
| 2978 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 2979 | 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] | 2980 | (unsigned) orig_size, (unsigned) start); |
| 2981 | } |
| 2982 | |
| 2983 | static void ext4_mb_collect_stats(struct ext4_allocation_context *ac) |
| 2984 | { |
| 2985 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 2986 | |
| 2987 | if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) { |
| 2988 | atomic_inc(&sbi->s_bal_reqs); |
| 2989 | atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated); |
| 2990 | if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len) |
| 2991 | atomic_inc(&sbi->s_bal_success); |
| 2992 | atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned); |
| 2993 | if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start && |
| 2994 | ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group) |
| 2995 | atomic_inc(&sbi->s_bal_goals); |
| 2996 | if (ac->ac_found > sbi->s_mb_max_to_scan) |
| 2997 | atomic_inc(&sbi->s_bal_breaks); |
| 2998 | } |
| 2999 | |
Theodore Ts'o | 296c355 | 2009-09-30 00:32:42 -0400 | [diff] [blame] | 3000 | if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) |
| 3001 | trace_ext4_mballoc_alloc(ac); |
| 3002 | else |
| 3003 | trace_ext4_mballoc_prealloc(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | /* |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 3007 | * Called on failure; free up any blocks from the inode PA for this |
| 3008 | * context. We don't need this for MB_GROUP_PA because we only change |
| 3009 | * pa_free in ext4_mb_release_context(), but on failure, we've already |
| 3010 | * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed. |
| 3011 | */ |
| 3012 | static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac) |
| 3013 | { |
| 3014 | struct ext4_prealloc_space *pa = ac->ac_pa; |
| 3015 | int len; |
| 3016 | |
| 3017 | if (pa && pa->pa_type == MB_INODE_PA) { |
| 3018 | len = ac->ac_b_ex.fe_len; |
| 3019 | pa->pa_free += len; |
| 3020 | } |
| 3021 | |
| 3022 | } |
| 3023 | |
| 3024 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3025 | * use blocks preallocated to inode |
| 3026 | */ |
| 3027 | static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac, |
| 3028 | struct ext4_prealloc_space *pa) |
| 3029 | { |
| 3030 | ext4_fsblk_t start; |
| 3031 | ext4_fsblk_t end; |
| 3032 | int len; |
| 3033 | |
| 3034 | /* found preallocated blocks, use them */ |
| 3035 | start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart); |
| 3036 | end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len); |
| 3037 | len = end - start; |
| 3038 | ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group, |
| 3039 | &ac->ac_b_ex.fe_start); |
| 3040 | ac->ac_b_ex.fe_len = len; |
| 3041 | ac->ac_status = AC_STATUS_FOUND; |
| 3042 | ac->ac_pa = pa; |
| 3043 | |
| 3044 | BUG_ON(start < pa->pa_pstart); |
| 3045 | BUG_ON(start + len > pa->pa_pstart + pa->pa_len); |
| 3046 | BUG_ON(pa->pa_free < len); |
| 3047 | pa->pa_free -= len; |
| 3048 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3049 | 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] | 3050 | } |
| 3051 | |
| 3052 | /* |
| 3053 | * use blocks preallocated to locality group |
| 3054 | */ |
| 3055 | static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac, |
| 3056 | struct ext4_prealloc_space *pa) |
| 3057 | { |
Aneesh Kumar K.V | 03cddb8 | 2008-06-05 20:59:29 -0400 | [diff] [blame] | 3058 | unsigned int len = ac->ac_o_ex.fe_len; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3059 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3060 | ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart, |
| 3061 | &ac->ac_b_ex.fe_group, |
| 3062 | &ac->ac_b_ex.fe_start); |
| 3063 | ac->ac_b_ex.fe_len = len; |
| 3064 | ac->ac_status = AC_STATUS_FOUND; |
| 3065 | ac->ac_pa = pa; |
| 3066 | |
| 3067 | /* 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] | 3068 | * possible race when the group is being loaded concurrently |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3069 | * instead we correct pa later, after blocks are marked |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 3070 | * in on-disk bitmap -- see ext4_mb_release_context() |
| 3071 | * Other CPUs are prevented from allocating from this pa by lg_mutex |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3072 | */ |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3073 | 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] | 3074 | } |
| 3075 | |
| 3076 | /* |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3077 | * Return the prealloc space that have minimal distance |
| 3078 | * from the goal block. @cpa is the prealloc |
| 3079 | * space that is having currently known minimal distance |
| 3080 | * from the goal block. |
| 3081 | */ |
| 3082 | static struct ext4_prealloc_space * |
| 3083 | ext4_mb_check_group_pa(ext4_fsblk_t goal_block, |
| 3084 | struct ext4_prealloc_space *pa, |
| 3085 | struct ext4_prealloc_space *cpa) |
| 3086 | { |
| 3087 | ext4_fsblk_t cur_distance, new_distance; |
| 3088 | |
| 3089 | if (cpa == NULL) { |
| 3090 | atomic_inc(&pa->pa_count); |
| 3091 | return pa; |
| 3092 | } |
| 3093 | cur_distance = abs(goal_block - cpa->pa_pstart); |
| 3094 | new_distance = abs(goal_block - pa->pa_pstart); |
| 3095 | |
| 3096 | if (cur_distance < new_distance) |
| 3097 | return cpa; |
| 3098 | |
| 3099 | /* drop the previous reference */ |
| 3100 | atomic_dec(&cpa->pa_count); |
| 3101 | atomic_inc(&pa->pa_count); |
| 3102 | return pa; |
| 3103 | } |
| 3104 | |
| 3105 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3106 | * search goal blocks in preallocated space |
| 3107 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3108 | static noinline_for_stack int |
| 3109 | ext4_mb_use_preallocated(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3110 | { |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3111 | int order, i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3112 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
| 3113 | struct ext4_locality_group *lg; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3114 | struct ext4_prealloc_space *pa, *cpa = NULL; |
| 3115 | ext4_fsblk_t goal_block; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3116 | |
| 3117 | /* only data can be preallocated */ |
| 3118 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 3119 | return 0; |
| 3120 | |
| 3121 | /* first, try per-file preallocation */ |
| 3122 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3123 | 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] | 3124 | |
| 3125 | /* all fields in this condition don't change, |
| 3126 | * so we can skip locking for them */ |
| 3127 | if (ac->ac_o_ex.fe_logical < pa->pa_lstart || |
| 3128 | ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len) |
| 3129 | continue; |
| 3130 | |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 3131 | /* non-extent files can't have physical blocks past 2^32 */ |
| 3132 | if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL) && |
| 3133 | pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS) |
| 3134 | continue; |
| 3135 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3136 | /* found preallocated blocks, use them */ |
| 3137 | spin_lock(&pa->pa_lock); |
| 3138 | if (pa->pa_deleted == 0 && pa->pa_free) { |
| 3139 | atomic_inc(&pa->pa_count); |
| 3140 | ext4_mb_use_inode_pa(ac, pa); |
| 3141 | spin_unlock(&pa->pa_lock); |
| 3142 | ac->ac_criteria = 10; |
| 3143 | rcu_read_unlock(); |
| 3144 | return 1; |
| 3145 | } |
| 3146 | spin_unlock(&pa->pa_lock); |
| 3147 | } |
| 3148 | rcu_read_unlock(); |
| 3149 | |
| 3150 | /* can we use group allocation? */ |
| 3151 | if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)) |
| 3152 | return 0; |
| 3153 | |
| 3154 | /* inode may have no locality group for some reason */ |
| 3155 | lg = ac->ac_lg; |
| 3156 | if (lg == NULL) |
| 3157 | return 0; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3158 | order = fls(ac->ac_o_ex.fe_len) - 1; |
| 3159 | if (order > PREALLOC_TB_SIZE - 1) |
| 3160 | /* The max size of hash table is PREALLOC_TB_SIZE */ |
| 3161 | order = PREALLOC_TB_SIZE - 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3162 | |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3163 | goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) + |
| 3164 | ac->ac_g_ex.fe_start + |
| 3165 | le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block); |
| 3166 | /* |
| 3167 | * search for the prealloc space that is having |
| 3168 | * minimal distance from the goal block. |
| 3169 | */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3170 | for (i = order; i < PREALLOC_TB_SIZE; i++) { |
| 3171 | rcu_read_lock(); |
| 3172 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i], |
| 3173 | pa_inode_list) { |
| 3174 | spin_lock(&pa->pa_lock); |
| 3175 | if (pa->pa_deleted == 0 && |
| 3176 | pa->pa_free >= ac->ac_o_ex.fe_len) { |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3177 | |
| 3178 | cpa = ext4_mb_check_group_pa(goal_block, |
| 3179 | pa, cpa); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3180 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3181 | spin_unlock(&pa->pa_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3182 | } |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3183 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3184 | } |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3185 | if (cpa) { |
| 3186 | ext4_mb_use_group_pa(ac, cpa); |
| 3187 | ac->ac_criteria = 20; |
| 3188 | return 1; |
| 3189 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3190 | return 0; |
| 3191 | } |
| 3192 | |
| 3193 | /* |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 3194 | * the function goes through all block freed in the group |
| 3195 | * but not yet committed and marks them used in in-core bitmap. |
| 3196 | * buddy must be generated from this bitmap |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3197 | * Need to be called with the ext4 group lock held |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 3198 | */ |
| 3199 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, |
| 3200 | ext4_group_t group) |
| 3201 | { |
| 3202 | struct rb_node *n; |
| 3203 | struct ext4_group_info *grp; |
| 3204 | struct ext4_free_data *entry; |
| 3205 | |
| 3206 | grp = ext4_get_group_info(sb, group); |
| 3207 | n = rb_first(&(grp->bb_free_root)); |
| 3208 | |
| 3209 | while (n) { |
| 3210 | entry = rb_entry(n, struct ext4_free_data, node); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3211 | mb_set_bits(bitmap, entry->start_blk, entry->count); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 3212 | n = rb_next(n); |
| 3213 | } |
| 3214 | return; |
| 3215 | } |
| 3216 | |
| 3217 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3218 | * the function goes through all preallocation in this group and marks them |
| 3219 | * 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] | 3220 | * Need to be called with ext4 group lock held |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3221 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 3222 | static noinline_for_stack |
| 3223 | void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3224 | ext4_group_t group) |
| 3225 | { |
| 3226 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 3227 | struct ext4_prealloc_space *pa; |
| 3228 | struct list_head *cur; |
| 3229 | ext4_group_t groupnr; |
| 3230 | ext4_grpblk_t start; |
| 3231 | int preallocated = 0; |
| 3232 | int count = 0; |
| 3233 | int len; |
| 3234 | |
| 3235 | /* all form of preallocation discards first load group, |
| 3236 | * so the only competing code is preallocation use. |
| 3237 | * we don't need any locking here |
| 3238 | * notice we do NOT ignore preallocations with pa_deleted |
| 3239 | * otherwise we could leave used blocks available for |
| 3240 | * allocation in buddy when concurrent ext4_mb_put_pa() |
| 3241 | * is dropping preallocation |
| 3242 | */ |
| 3243 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 3244 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 3245 | spin_lock(&pa->pa_lock); |
| 3246 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, |
| 3247 | &groupnr, &start); |
| 3248 | len = pa->pa_len; |
| 3249 | spin_unlock(&pa->pa_lock); |
| 3250 | if (unlikely(len == 0)) |
| 3251 | continue; |
| 3252 | BUG_ON(groupnr != group); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3253 | mb_set_bits(bitmap, start, len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3254 | preallocated += len; |
| 3255 | count++; |
| 3256 | } |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3257 | mb_debug(1, "prellocated %u for group %u\n", preallocated, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | static void ext4_mb_pa_callback(struct rcu_head *head) |
| 3261 | { |
| 3262 | struct ext4_prealloc_space *pa; |
| 3263 | pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu); |
| 3264 | kmem_cache_free(ext4_pspace_cachep, pa); |
| 3265 | } |
| 3266 | |
| 3267 | /* |
| 3268 | * drops a reference to preallocated space descriptor |
| 3269 | * if this was the last reference and the space is consumed |
| 3270 | */ |
| 3271 | static void ext4_mb_put_pa(struct ext4_allocation_context *ac, |
| 3272 | struct super_block *sb, struct ext4_prealloc_space *pa) |
| 3273 | { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 3274 | ext4_group_t grp; |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3275 | ext4_fsblk_t grp_blk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3276 | |
| 3277 | if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) |
| 3278 | return; |
| 3279 | |
| 3280 | /* in this short window concurrent discard can set pa_deleted */ |
| 3281 | spin_lock(&pa->pa_lock); |
| 3282 | if (pa->pa_deleted == 1) { |
| 3283 | spin_unlock(&pa->pa_lock); |
| 3284 | return; |
| 3285 | } |
| 3286 | |
| 3287 | pa->pa_deleted = 1; |
| 3288 | spin_unlock(&pa->pa_lock); |
| 3289 | |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3290 | grp_blk = pa->pa_pstart; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3291 | /* |
| 3292 | * If doing group-based preallocation, pa_pstart may be in the |
| 3293 | * next group when pa is used up |
| 3294 | */ |
| 3295 | if (pa->pa_type == MB_GROUP_PA) |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3296 | grp_blk--; |
| 3297 | |
| 3298 | ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3299 | |
| 3300 | /* |
| 3301 | * possible race: |
| 3302 | * |
| 3303 | * P1 (buddy init) P2 (regular allocation) |
| 3304 | * find block B in PA |
| 3305 | * copy on-disk bitmap to buddy |
| 3306 | * mark B in on-disk bitmap |
| 3307 | * drop PA from group |
| 3308 | * mark all PAs in buddy |
| 3309 | * |
| 3310 | * thus, P1 initializes buddy with B available. to prevent this |
| 3311 | * we make "copy" and "mark all PAs" atomic and serialize "drop PA" |
| 3312 | * against that pair |
| 3313 | */ |
| 3314 | ext4_lock_group(sb, grp); |
| 3315 | list_del(&pa->pa_group_list); |
| 3316 | ext4_unlock_group(sb, grp); |
| 3317 | |
| 3318 | spin_lock(pa->pa_obj_lock); |
| 3319 | list_del_rcu(&pa->pa_inode_list); |
| 3320 | spin_unlock(pa->pa_obj_lock); |
| 3321 | |
| 3322 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 3323 | } |
| 3324 | |
| 3325 | /* |
| 3326 | * creates new preallocated space for given inode |
| 3327 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3328 | static noinline_for_stack int |
| 3329 | ext4_mb_new_inode_pa(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3330 | { |
| 3331 | struct super_block *sb = ac->ac_sb; |
| 3332 | struct ext4_prealloc_space *pa; |
| 3333 | struct ext4_group_info *grp; |
| 3334 | struct ext4_inode_info *ei; |
| 3335 | |
| 3336 | /* preallocate only when found space is larger then requested */ |
| 3337 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); |
| 3338 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 3339 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); |
| 3340 | |
| 3341 | pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS); |
| 3342 | if (pa == NULL) |
| 3343 | return -ENOMEM; |
| 3344 | |
| 3345 | if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) { |
| 3346 | int winl; |
| 3347 | int wins; |
| 3348 | int win; |
| 3349 | int offs; |
| 3350 | |
| 3351 | /* we can't allocate as much as normalizer wants. |
| 3352 | * so, found space must get proper lstart |
| 3353 | * to cover original request */ |
| 3354 | BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical); |
| 3355 | BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len); |
| 3356 | |
| 3357 | /* we're limited by original request in that |
| 3358 | * logical block must be covered any way |
| 3359 | * winl is window we can move our chunk within */ |
| 3360 | winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical; |
| 3361 | |
| 3362 | /* also, we should cover whole original request */ |
| 3363 | wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len; |
| 3364 | |
| 3365 | /* the smallest one defines real window */ |
| 3366 | win = min(winl, wins); |
| 3367 | |
| 3368 | offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len; |
| 3369 | if (offs && offs < win) |
| 3370 | win = offs; |
| 3371 | |
| 3372 | ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win; |
| 3373 | BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical); |
| 3374 | BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len); |
| 3375 | } |
| 3376 | |
| 3377 | /* preallocation can change ac_b_ex, thus we store actually |
| 3378 | * allocated blocks for history */ |
| 3379 | ac->ac_f_ex = ac->ac_b_ex; |
| 3380 | |
| 3381 | pa->pa_lstart = ac->ac_b_ex.fe_logical; |
| 3382 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 3383 | pa->pa_len = ac->ac_b_ex.fe_len; |
| 3384 | pa->pa_free = pa->pa_len; |
| 3385 | atomic_set(&pa->pa_count, 1); |
| 3386 | spin_lock_init(&pa->pa_lock); |
Aneesh Kumar K.V | d794bf8 | 2009-02-14 10:31:16 -0500 | [diff] [blame] | 3387 | INIT_LIST_HEAD(&pa->pa_inode_list); |
| 3388 | INIT_LIST_HEAD(&pa->pa_group_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3389 | pa->pa_deleted = 0; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3390 | pa->pa_type = MB_INODE_PA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3391 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3392 | 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] | 3393 | pa->pa_pstart, pa->pa_len, pa->pa_lstart); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3394 | trace_ext4_mb_new_inode_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3395 | |
| 3396 | ext4_mb_use_inode_pa(ac, pa); |
| 3397 | atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated); |
| 3398 | |
| 3399 | ei = EXT4_I(ac->ac_inode); |
| 3400 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); |
| 3401 | |
| 3402 | pa->pa_obj_lock = &ei->i_prealloc_lock; |
| 3403 | pa->pa_inode = ac->ac_inode; |
| 3404 | |
| 3405 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
| 3406 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); |
| 3407 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
| 3408 | |
| 3409 | spin_lock(pa->pa_obj_lock); |
| 3410 | list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list); |
| 3411 | spin_unlock(pa->pa_obj_lock); |
| 3412 | |
| 3413 | return 0; |
| 3414 | } |
| 3415 | |
| 3416 | /* |
| 3417 | * creates new preallocated space for locality group inodes belongs to |
| 3418 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3419 | static noinline_for_stack int |
| 3420 | ext4_mb_new_group_pa(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3421 | { |
| 3422 | struct super_block *sb = ac->ac_sb; |
| 3423 | struct ext4_locality_group *lg; |
| 3424 | struct ext4_prealloc_space *pa; |
| 3425 | struct ext4_group_info *grp; |
| 3426 | |
| 3427 | /* preallocate only when found space is larger then requested */ |
| 3428 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); |
| 3429 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 3430 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); |
| 3431 | |
| 3432 | BUG_ON(ext4_pspace_cachep == NULL); |
| 3433 | pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS); |
| 3434 | if (pa == NULL) |
| 3435 | return -ENOMEM; |
| 3436 | |
| 3437 | /* preallocation can change ac_b_ex, thus we store actually |
| 3438 | * allocated blocks for history */ |
| 3439 | ac->ac_f_ex = ac->ac_b_ex; |
| 3440 | |
| 3441 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 3442 | pa->pa_lstart = pa->pa_pstart; |
| 3443 | pa->pa_len = ac->ac_b_ex.fe_len; |
| 3444 | pa->pa_free = pa->pa_len; |
| 3445 | atomic_set(&pa->pa_count, 1); |
| 3446 | spin_lock_init(&pa->pa_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3447 | INIT_LIST_HEAD(&pa->pa_inode_list); |
Aneesh Kumar K.V | d794bf8 | 2009-02-14 10:31:16 -0500 | [diff] [blame] | 3448 | INIT_LIST_HEAD(&pa->pa_group_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3449 | pa->pa_deleted = 0; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3450 | pa->pa_type = MB_GROUP_PA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3451 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3452 | 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] | 3453 | pa->pa_pstart, pa->pa_len, pa->pa_lstart); |
| 3454 | trace_ext4_mb_new_group_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3455 | |
| 3456 | ext4_mb_use_group_pa(ac, pa); |
| 3457 | atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated); |
| 3458 | |
| 3459 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); |
| 3460 | lg = ac->ac_lg; |
| 3461 | BUG_ON(lg == NULL); |
| 3462 | |
| 3463 | pa->pa_obj_lock = &lg->lg_prealloc_lock; |
| 3464 | pa->pa_inode = NULL; |
| 3465 | |
| 3466 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
| 3467 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); |
| 3468 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
| 3469 | |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3470 | /* |
| 3471 | * We will later add the new pa to the right bucket |
| 3472 | * after updating the pa_free in ext4_mb_release_context |
| 3473 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3474 | return 0; |
| 3475 | } |
| 3476 | |
| 3477 | static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac) |
| 3478 | { |
| 3479 | int err; |
| 3480 | |
| 3481 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
| 3482 | err = ext4_mb_new_group_pa(ac); |
| 3483 | else |
| 3484 | err = ext4_mb_new_inode_pa(ac); |
| 3485 | return err; |
| 3486 | } |
| 3487 | |
| 3488 | /* |
| 3489 | * finds all unused blocks in on-disk bitmap, frees them in |
| 3490 | * in-core bitmap and buddy. |
| 3491 | * @pa must be unlinked from inode and group lists, so that |
| 3492 | * nobody else can find/use it. |
| 3493 | * the caller MUST hold group/inode locks. |
| 3494 | * TODO: optimize the case when there are no in-core structures yet |
| 3495 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3496 | static noinline_for_stack int |
| 3497 | 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] | 3498 | struct ext4_prealloc_space *pa, |
| 3499 | struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3500 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3501 | struct super_block *sb = e4b->bd_sb; |
| 3502 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3503 | unsigned int end; |
| 3504 | unsigned int next; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3505 | ext4_group_t group; |
| 3506 | ext4_grpblk_t bit; |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 3507 | unsigned long long grp_blk_start; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3508 | sector_t start; |
| 3509 | int err = 0; |
| 3510 | int free = 0; |
| 3511 | |
| 3512 | BUG_ON(pa->pa_deleted == 0); |
| 3513 | 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] | 3514 | grp_blk_start = pa->pa_pstart - bit; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3515 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
| 3516 | end = bit + pa->pa_len; |
| 3517 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3518 | if (ac) { |
| 3519 | ac->ac_sb = sb; |
| 3520 | ac->ac_inode = pa->pa_inode; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3521 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3522 | |
| 3523 | while (bit < end) { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 3524 | bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3525 | if (bit >= end) |
| 3526 | break; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 3527 | next = mb_find_next_bit(bitmap_bh->b_data, end, bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3528 | start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit + |
| 3529 | le32_to_cpu(sbi->s_es->s_first_data_block); |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3530 | mb_debug(1, " free preallocated %u/%u in group %u\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3531 | (unsigned) start, (unsigned) next - bit, |
| 3532 | (unsigned) group); |
| 3533 | free += next - bit; |
| 3534 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3535 | if (ac) { |
| 3536 | ac->ac_b_ex.fe_group = group; |
| 3537 | ac->ac_b_ex.fe_start = bit; |
| 3538 | ac->ac_b_ex.fe_len = next - bit; |
| 3539 | ac->ac_b_ex.fe_logical = 0; |
Theodore Ts'o | 296c355 | 2009-09-30 00:32:42 -0400 | [diff] [blame] | 3540 | trace_ext4_mballoc_discard(ac); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3541 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3542 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3543 | trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit, |
| 3544 | next - bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3545 | mb_free_blocks(pa->pa_inode, e4b, bit, next - bit); |
| 3546 | bit = next + 1; |
| 3547 | } |
| 3548 | if (free != pa->pa_free) { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 3549 | printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3550 | pa, (unsigned long) pa->pa_lstart, |
| 3551 | (unsigned long) pa->pa_pstart, |
| 3552 | (unsigned long) pa->pa_len); |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 3553 | ext4_grp_locked_error(sb, group, |
| 3554 | __func__, "free %u, pa_free %u", |
| 3555 | free, pa->pa_free); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 3556 | /* |
| 3557 | * pa is already deleted so we use the value obtained |
| 3558 | * from the bitmap and continue. |
| 3559 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3560 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3561 | atomic_add(free, &sbi->s_mb_discarded); |
| 3562 | |
| 3563 | return err; |
| 3564 | } |
| 3565 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3566 | static noinline_for_stack int |
| 3567 | ext4_mb_release_group_pa(struct ext4_buddy *e4b, |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3568 | struct ext4_prealloc_space *pa, |
| 3569 | struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3570 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3571 | struct super_block *sb = e4b->bd_sb; |
| 3572 | ext4_group_t group; |
| 3573 | ext4_grpblk_t bit; |
| 3574 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3575 | trace_ext4_mb_release_group_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3576 | BUG_ON(pa->pa_deleted == 0); |
| 3577 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); |
| 3578 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
| 3579 | mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len); |
| 3580 | atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded); |
| 3581 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3582 | if (ac) { |
| 3583 | ac->ac_sb = sb; |
| 3584 | ac->ac_inode = NULL; |
| 3585 | ac->ac_b_ex.fe_group = group; |
| 3586 | ac->ac_b_ex.fe_start = bit; |
| 3587 | ac->ac_b_ex.fe_len = pa->pa_len; |
| 3588 | ac->ac_b_ex.fe_logical = 0; |
Theodore Ts'o | 296c355 | 2009-09-30 00:32:42 -0400 | [diff] [blame] | 3589 | trace_ext4_mballoc_discard(ac); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3590 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3591 | |
| 3592 | return 0; |
| 3593 | } |
| 3594 | |
| 3595 | /* |
| 3596 | * releases all preallocations in given group |
| 3597 | * |
| 3598 | * first, we need to decide discard policy: |
| 3599 | * - when do we discard |
| 3600 | * 1) ENOSPC |
| 3601 | * - how many do we discard |
| 3602 | * 1) how many requested |
| 3603 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3604 | static noinline_for_stack int |
| 3605 | ext4_mb_discard_group_preallocations(struct super_block *sb, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3606 | ext4_group_t group, int needed) |
| 3607 | { |
| 3608 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 3609 | struct buffer_head *bitmap_bh = NULL; |
| 3610 | struct ext4_prealloc_space *pa, *tmp; |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3611 | struct ext4_allocation_context *ac; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3612 | struct list_head list; |
| 3613 | struct ext4_buddy e4b; |
| 3614 | int err; |
| 3615 | int busy = 0; |
| 3616 | int free = 0; |
| 3617 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3618 | mb_debug(1, "discard preallocation for group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3619 | |
| 3620 | if (list_empty(&grp->bb_prealloc_list)) |
| 3621 | return 0; |
| 3622 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3623 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3624 | if (bitmap_bh == NULL) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame^] | 3625 | ext4_error(sb, "Error reading block bitmap for %u", group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3626 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | err = ext4_mb_load_buddy(sb, group, &e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3630 | if (err) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame^] | 3631 | ext4_error(sb, "Error loading buddy information for %u", group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3632 | put_bh(bitmap_bh); |
| 3633 | return 0; |
| 3634 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3635 | |
| 3636 | if (needed == 0) |
| 3637 | needed = EXT4_BLOCKS_PER_GROUP(sb) + 1; |
| 3638 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3639 | INIT_LIST_HEAD(&list); |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3640 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3641 | if (ac) |
| 3642 | ac->ac_sb = sb; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3643 | repeat: |
| 3644 | ext4_lock_group(sb, group); |
| 3645 | list_for_each_entry_safe(pa, tmp, |
| 3646 | &grp->bb_prealloc_list, pa_group_list) { |
| 3647 | spin_lock(&pa->pa_lock); |
| 3648 | if (atomic_read(&pa->pa_count)) { |
| 3649 | spin_unlock(&pa->pa_lock); |
| 3650 | busy = 1; |
| 3651 | continue; |
| 3652 | } |
| 3653 | if (pa->pa_deleted) { |
| 3654 | spin_unlock(&pa->pa_lock); |
| 3655 | continue; |
| 3656 | } |
| 3657 | |
| 3658 | /* seems this one can be freed ... */ |
| 3659 | pa->pa_deleted = 1; |
| 3660 | |
| 3661 | /* we can trust pa_free ... */ |
| 3662 | free += pa->pa_free; |
| 3663 | |
| 3664 | spin_unlock(&pa->pa_lock); |
| 3665 | |
| 3666 | list_del(&pa->pa_group_list); |
| 3667 | list_add(&pa->u.pa_tmp_list, &list); |
| 3668 | } |
| 3669 | |
| 3670 | /* if we still need more blocks and some PAs were used, try again */ |
| 3671 | if (free < needed && busy) { |
| 3672 | busy = 0; |
| 3673 | ext4_unlock_group(sb, group); |
| 3674 | /* |
| 3675 | * Yield the CPU here so that we don't get soft lockup |
| 3676 | * in non preempt case. |
| 3677 | */ |
| 3678 | yield(); |
| 3679 | goto repeat; |
| 3680 | } |
| 3681 | |
| 3682 | /* found anything to free? */ |
| 3683 | if (list_empty(&list)) { |
| 3684 | BUG_ON(free != 0); |
| 3685 | goto out; |
| 3686 | } |
| 3687 | |
| 3688 | /* now free all selected PAs */ |
| 3689 | list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { |
| 3690 | |
| 3691 | /* remove from object (inode or locality group) */ |
| 3692 | spin_lock(pa->pa_obj_lock); |
| 3693 | list_del_rcu(&pa->pa_inode_list); |
| 3694 | spin_unlock(pa->pa_obj_lock); |
| 3695 | |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3696 | if (pa->pa_type == MB_GROUP_PA) |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3697 | ext4_mb_release_group_pa(&e4b, pa, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3698 | else |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3699 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3700 | |
| 3701 | list_del(&pa->u.pa_tmp_list); |
| 3702 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 3703 | } |
| 3704 | |
| 3705 | out: |
| 3706 | ext4_unlock_group(sb, group); |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3707 | if (ac) |
| 3708 | kmem_cache_free(ext4_ac_cachep, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3709 | ext4_mb_release_desc(&e4b); |
| 3710 | put_bh(bitmap_bh); |
| 3711 | return free; |
| 3712 | } |
| 3713 | |
| 3714 | /* |
| 3715 | * releases all non-used preallocated blocks for given inode |
| 3716 | * |
| 3717 | * It's important to discard preallocations under i_data_sem |
| 3718 | * We don't want another block to be served from the prealloc |
| 3719 | * space when we are discarding the inode prealloc space. |
| 3720 | * |
| 3721 | * FIXME!! Make sure it is valid at all the call sites |
| 3722 | */ |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3723 | void ext4_discard_preallocations(struct inode *inode) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3724 | { |
| 3725 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 3726 | struct super_block *sb = inode->i_sb; |
| 3727 | struct buffer_head *bitmap_bh = NULL; |
| 3728 | struct ext4_prealloc_space *pa, *tmp; |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3729 | struct ext4_allocation_context *ac; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3730 | ext4_group_t group = 0; |
| 3731 | struct list_head list; |
| 3732 | struct ext4_buddy e4b; |
| 3733 | int err; |
| 3734 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3735 | if (!S_ISREG(inode->i_mode)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3736 | /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/ |
| 3737 | return; |
| 3738 | } |
| 3739 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3740 | 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] | 3741 | trace_ext4_discard_preallocations(inode); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3742 | |
| 3743 | INIT_LIST_HEAD(&list); |
| 3744 | |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3745 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3746 | if (ac) { |
| 3747 | ac->ac_sb = sb; |
| 3748 | ac->ac_inode = inode; |
| 3749 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3750 | repeat: |
| 3751 | /* first, collect all pa's in the inode */ |
| 3752 | spin_lock(&ei->i_prealloc_lock); |
| 3753 | while (!list_empty(&ei->i_prealloc_list)) { |
| 3754 | pa = list_entry(ei->i_prealloc_list.next, |
| 3755 | struct ext4_prealloc_space, pa_inode_list); |
| 3756 | BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock); |
| 3757 | spin_lock(&pa->pa_lock); |
| 3758 | if (atomic_read(&pa->pa_count)) { |
| 3759 | /* this shouldn't happen often - nobody should |
| 3760 | * use preallocation while we're discarding it */ |
| 3761 | spin_unlock(&pa->pa_lock); |
| 3762 | spin_unlock(&ei->i_prealloc_lock); |
| 3763 | printk(KERN_ERR "uh-oh! used pa while discarding\n"); |
| 3764 | WARN_ON(1); |
| 3765 | schedule_timeout_uninterruptible(HZ); |
| 3766 | goto repeat; |
| 3767 | |
| 3768 | } |
| 3769 | if (pa->pa_deleted == 0) { |
| 3770 | pa->pa_deleted = 1; |
| 3771 | spin_unlock(&pa->pa_lock); |
| 3772 | list_del_rcu(&pa->pa_inode_list); |
| 3773 | list_add(&pa->u.pa_tmp_list, &list); |
| 3774 | continue; |
| 3775 | } |
| 3776 | |
| 3777 | /* someone is deleting pa right now */ |
| 3778 | spin_unlock(&pa->pa_lock); |
| 3779 | spin_unlock(&ei->i_prealloc_lock); |
| 3780 | |
| 3781 | /* we have to wait here because pa_deleted |
| 3782 | * doesn't mean pa is already unlinked from |
| 3783 | * the list. as we might be called from |
| 3784 | * ->clear_inode() the inode will get freed |
| 3785 | * and concurrent thread which is unlinking |
| 3786 | * pa from inode's list may access already |
| 3787 | * freed memory, bad-bad-bad */ |
| 3788 | |
| 3789 | /* XXX: if this happens too often, we can |
| 3790 | * add a flag to force wait only in case |
| 3791 | * of ->clear_inode(), but not in case of |
| 3792 | * regular truncate */ |
| 3793 | schedule_timeout_uninterruptible(HZ); |
| 3794 | goto repeat; |
| 3795 | } |
| 3796 | spin_unlock(&ei->i_prealloc_lock); |
| 3797 | |
| 3798 | 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] | 3799 | BUG_ON(pa->pa_type != MB_INODE_PA); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3800 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL); |
| 3801 | |
| 3802 | err = ext4_mb_load_buddy(sb, group, &e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3803 | if (err) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame^] | 3804 | ext4_error(sb, "Error loading buddy information for %u", |
| 3805 | group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3806 | continue; |
| 3807 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3808 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3809 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3810 | if (bitmap_bh == NULL) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame^] | 3811 | ext4_error(sb, "Error reading block bitmap for %u", |
| 3812 | group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3813 | ext4_mb_release_desc(&e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 3814 | continue; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3815 | } |
| 3816 | |
| 3817 | ext4_lock_group(sb, group); |
| 3818 | list_del(&pa->pa_group_list); |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3819 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3820 | ext4_unlock_group(sb, group); |
| 3821 | |
| 3822 | ext4_mb_release_desc(&e4b); |
| 3823 | put_bh(bitmap_bh); |
| 3824 | |
| 3825 | list_del(&pa->u.pa_tmp_list); |
| 3826 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 3827 | } |
Aneesh Kumar K.V | c83617d | 2008-04-29 22:00:47 -0400 | [diff] [blame] | 3828 | if (ac) |
| 3829 | kmem_cache_free(ext4_ac_cachep, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | /* |
| 3833 | * finds all preallocated spaces and return blocks being freed to them |
| 3834 | * if preallocated space becomes full (no block is used from the space) |
| 3835 | * then the function frees space in buddy |
| 3836 | * XXX: at the moment, truncate (which is the only way to free blocks) |
| 3837 | * discards all preallocations |
| 3838 | */ |
| 3839 | static void ext4_mb_return_to_preallocation(struct inode *inode, |
| 3840 | struct ext4_buddy *e4b, |
| 3841 | sector_t block, int count) |
| 3842 | { |
| 3843 | BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list)); |
| 3844 | } |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 3845 | #ifdef CONFIG_EXT4_DEBUG |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3846 | static void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
| 3847 | { |
| 3848 | struct super_block *sb = ac->ac_sb; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3849 | ext4_group_t ngroups, i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3850 | |
| 3851 | printk(KERN_ERR "EXT4-fs: Can't allocate:" |
| 3852 | " Allocation context details:\n"); |
| 3853 | printk(KERN_ERR "EXT4-fs: status %d flags %d\n", |
| 3854 | ac->ac_status, ac->ac_flags); |
| 3855 | printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, " |
| 3856 | "best %lu/%lu/%lu@%lu cr %d\n", |
| 3857 | (unsigned long)ac->ac_o_ex.fe_group, |
| 3858 | (unsigned long)ac->ac_o_ex.fe_start, |
| 3859 | (unsigned long)ac->ac_o_ex.fe_len, |
| 3860 | (unsigned long)ac->ac_o_ex.fe_logical, |
| 3861 | (unsigned long)ac->ac_g_ex.fe_group, |
| 3862 | (unsigned long)ac->ac_g_ex.fe_start, |
| 3863 | (unsigned long)ac->ac_g_ex.fe_len, |
| 3864 | (unsigned long)ac->ac_g_ex.fe_logical, |
| 3865 | (unsigned long)ac->ac_b_ex.fe_group, |
| 3866 | (unsigned long)ac->ac_b_ex.fe_start, |
| 3867 | (unsigned long)ac->ac_b_ex.fe_len, |
| 3868 | (unsigned long)ac->ac_b_ex.fe_logical, |
| 3869 | (int)ac->ac_criteria); |
| 3870 | printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned, |
| 3871 | ac->ac_found); |
| 3872 | printk(KERN_ERR "EXT4-fs: groups: \n"); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3873 | ngroups = ext4_get_groups_count(sb); |
| 3874 | for (i = 0; i < ngroups; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3875 | struct ext4_group_info *grp = ext4_get_group_info(sb, i); |
| 3876 | struct ext4_prealloc_space *pa; |
| 3877 | ext4_grpblk_t start; |
| 3878 | struct list_head *cur; |
| 3879 | ext4_lock_group(sb, i); |
| 3880 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 3881 | pa = list_entry(cur, struct ext4_prealloc_space, |
| 3882 | pa_group_list); |
| 3883 | spin_lock(&pa->pa_lock); |
| 3884 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, |
| 3885 | NULL, &start); |
| 3886 | spin_unlock(&pa->pa_lock); |
Akira Fujita | 1c71850 | 2009-07-05 23:04:36 -0400 | [diff] [blame] | 3887 | printk(KERN_ERR "PA:%u:%d:%u \n", i, |
| 3888 | start, pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3889 | } |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 3890 | ext4_unlock_group(sb, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3891 | |
| 3892 | if (grp->bb_free == 0) |
| 3893 | continue; |
Akira Fujita | 1c71850 | 2009-07-05 23:04:36 -0400 | [diff] [blame] | 3894 | printk(KERN_ERR "%u: %d/%d \n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3895 | i, grp->bb_free, grp->bb_fragments); |
| 3896 | } |
| 3897 | printk(KERN_ERR "\n"); |
| 3898 | } |
| 3899 | #else |
| 3900 | static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
| 3901 | { |
| 3902 | return; |
| 3903 | } |
| 3904 | #endif |
| 3905 | |
| 3906 | /* |
| 3907 | * We use locality group preallocation for small size file. The size of the |
| 3908 | * file is determined by the current size or the resulting size after |
| 3909 | * allocation which ever is larger |
| 3910 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 3911 | * 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] | 3912 | */ |
| 3913 | static void ext4_mb_group_or_file(struct ext4_allocation_context *ac) |
| 3914 | { |
| 3915 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 3916 | int bsbits = ac->ac_sb->s_blocksize_bits; |
| 3917 | loff_t size, isize; |
| 3918 | |
| 3919 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 3920 | return; |
| 3921 | |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 3922 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 3923 | return; |
| 3924 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3925 | 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] | 3926 | isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1) |
| 3927 | >> bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3928 | |
Theodore Ts'o | 5079748 | 2009-09-18 13:34:02 -0400 | [diff] [blame] | 3929 | if ((size == isize) && |
| 3930 | !ext4_fs_is_busy(sbi) && |
| 3931 | (atomic_read(&ac->ac_inode->i_writecount) == 0)) { |
| 3932 | ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC; |
| 3933 | return; |
| 3934 | } |
| 3935 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3936 | /* don't use group allocation for large files */ |
Theodore Ts'o | 7178057 | 2009-09-28 00:06:20 -0400 | [diff] [blame] | 3937 | size = max(size, isize); |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 3938 | if (size >= sbi->s_mb_stream_request) { |
| 3939 | ac->ac_flags |= EXT4_MB_STREAM_ALLOC; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3940 | return; |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 3941 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3942 | |
| 3943 | BUG_ON(ac->ac_lg != NULL); |
| 3944 | /* |
| 3945 | * locality group prealloc space are per cpu. The reason for having |
| 3946 | * per cpu locality group is to reduce the contention between block |
| 3947 | * request from multiple CPUs. |
| 3948 | */ |
Christoph Lameter | ca0c958 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 3949 | ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3950 | |
| 3951 | /* we're going to use group allocation */ |
| 3952 | ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC; |
| 3953 | |
| 3954 | /* serialize all allocations in the group */ |
| 3955 | mutex_lock(&ac->ac_lg->lg_mutex); |
| 3956 | } |
| 3957 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3958 | static noinline_for_stack int |
| 3959 | ext4_mb_initialize_context(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3960 | struct ext4_allocation_request *ar) |
| 3961 | { |
| 3962 | struct super_block *sb = ar->inode->i_sb; |
| 3963 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3964 | struct ext4_super_block *es = sbi->s_es; |
| 3965 | ext4_group_t group; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3966 | unsigned int len; |
| 3967 | ext4_fsblk_t goal; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3968 | ext4_grpblk_t block; |
| 3969 | |
| 3970 | /* we can't allocate > group size */ |
| 3971 | len = ar->len; |
| 3972 | |
| 3973 | /* just a dirty hack to filter too big requests */ |
| 3974 | if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10) |
| 3975 | len = EXT4_BLOCKS_PER_GROUP(sb) - 10; |
| 3976 | |
| 3977 | /* start searching from the goal */ |
| 3978 | goal = ar->goal; |
| 3979 | if (goal < le32_to_cpu(es->s_first_data_block) || |
| 3980 | goal >= ext4_blocks_count(es)) |
| 3981 | goal = le32_to_cpu(es->s_first_data_block); |
| 3982 | ext4_get_group_no_and_offset(sb, goal, &group, &block); |
| 3983 | |
| 3984 | /* set up allocation goals */ |
Theodore Ts'o | 833576b | 2009-07-13 09:45:52 -0400 | [diff] [blame] | 3985 | memset(ac, 0, sizeof(struct ext4_allocation_context)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3986 | ac->ac_b_ex.fe_logical = ar->logical; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3987 | ac->ac_status = AC_STATUS_CONTINUE; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3988 | ac->ac_sb = sb; |
| 3989 | ac->ac_inode = ar->inode; |
| 3990 | ac->ac_o_ex.fe_logical = ar->logical; |
| 3991 | ac->ac_o_ex.fe_group = group; |
| 3992 | ac->ac_o_ex.fe_start = block; |
| 3993 | ac->ac_o_ex.fe_len = len; |
| 3994 | ac->ac_g_ex.fe_logical = ar->logical; |
| 3995 | ac->ac_g_ex.fe_group = group; |
| 3996 | ac->ac_g_ex.fe_start = block; |
| 3997 | ac->ac_g_ex.fe_len = len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3998 | ac->ac_flags = ar->flags; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3999 | |
| 4000 | /* we have to define context: we'll we work with a file or |
| 4001 | * locality group. this is a policy, actually */ |
| 4002 | ext4_mb_group_or_file(ac); |
| 4003 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 4004 | 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] | 4005 | "left: %u/%u, right %u/%u to %swritable\n", |
| 4006 | (unsigned) ar->len, (unsigned) ar->logical, |
| 4007 | (unsigned) ar->goal, ac->ac_flags, ac->ac_2order, |
| 4008 | (unsigned) ar->lleft, (unsigned) ar->pleft, |
| 4009 | (unsigned) ar->lright, (unsigned) ar->pright, |
| 4010 | atomic_read(&ar->inode->i_writecount) ? "" : "non-"); |
| 4011 | return 0; |
| 4012 | |
| 4013 | } |
| 4014 | |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4015 | static noinline_for_stack void |
| 4016 | ext4_mb_discard_lg_preallocations(struct super_block *sb, |
| 4017 | struct ext4_locality_group *lg, |
| 4018 | int order, int total_entries) |
| 4019 | { |
| 4020 | ext4_group_t group = 0; |
| 4021 | struct ext4_buddy e4b; |
| 4022 | struct list_head discard_list; |
| 4023 | struct ext4_prealloc_space *pa, *tmp; |
| 4024 | struct ext4_allocation_context *ac; |
| 4025 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 4026 | mb_debug(1, "discard locality group preallocation\n"); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4027 | |
| 4028 | INIT_LIST_HEAD(&discard_list); |
| 4029 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4030 | if (ac) |
| 4031 | ac->ac_sb = sb; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4032 | |
| 4033 | spin_lock(&lg->lg_prealloc_lock); |
| 4034 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order], |
| 4035 | pa_inode_list) { |
| 4036 | spin_lock(&pa->pa_lock); |
| 4037 | if (atomic_read(&pa->pa_count)) { |
| 4038 | /* |
| 4039 | * This is the pa that we just used |
| 4040 | * for block allocation. So don't |
| 4041 | * free that |
| 4042 | */ |
| 4043 | spin_unlock(&pa->pa_lock); |
| 4044 | continue; |
| 4045 | } |
| 4046 | if (pa->pa_deleted) { |
| 4047 | spin_unlock(&pa->pa_lock); |
| 4048 | continue; |
| 4049 | } |
| 4050 | /* only lg prealloc space */ |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4051 | BUG_ON(pa->pa_type != MB_GROUP_PA); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4052 | |
| 4053 | /* seems this one can be freed ... */ |
| 4054 | pa->pa_deleted = 1; |
| 4055 | spin_unlock(&pa->pa_lock); |
| 4056 | |
| 4057 | list_del_rcu(&pa->pa_inode_list); |
| 4058 | list_add(&pa->u.pa_tmp_list, &discard_list); |
| 4059 | |
| 4060 | total_entries--; |
| 4061 | if (total_entries <= 5) { |
| 4062 | /* |
| 4063 | * we want to keep only 5 entries |
| 4064 | * allowing it to grow to 8. This |
| 4065 | * mak sure we don't call discard |
| 4066 | * soon for this list. |
| 4067 | */ |
| 4068 | break; |
| 4069 | } |
| 4070 | } |
| 4071 | spin_unlock(&lg->lg_prealloc_lock); |
| 4072 | |
| 4073 | list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) { |
| 4074 | |
| 4075 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL); |
| 4076 | if (ext4_mb_load_buddy(sb, group, &e4b)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame^] | 4077 | ext4_error(sb, "Error loading buddy information for %u", |
| 4078 | group); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4079 | continue; |
| 4080 | } |
| 4081 | ext4_lock_group(sb, group); |
| 4082 | list_del(&pa->pa_group_list); |
| 4083 | ext4_mb_release_group_pa(&e4b, pa, ac); |
| 4084 | ext4_unlock_group(sb, group); |
| 4085 | |
| 4086 | ext4_mb_release_desc(&e4b); |
| 4087 | list_del(&pa->u.pa_tmp_list); |
| 4088 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4089 | } |
| 4090 | if (ac) |
| 4091 | kmem_cache_free(ext4_ac_cachep, ac); |
| 4092 | } |
| 4093 | |
| 4094 | /* |
| 4095 | * We have incremented pa_count. So it cannot be freed at this |
| 4096 | * point. Also we hold lg_mutex. So no parallel allocation is |
| 4097 | * possible from this lg. That means pa_free cannot be updated. |
| 4098 | * |
| 4099 | * A parallel ext4_mb_discard_group_preallocations is possible. |
| 4100 | * which can cause the lg_prealloc_list to be updated. |
| 4101 | */ |
| 4102 | |
| 4103 | static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac) |
| 4104 | { |
| 4105 | int order, added = 0, lg_prealloc_count = 1; |
| 4106 | struct super_block *sb = ac->ac_sb; |
| 4107 | struct ext4_locality_group *lg = ac->ac_lg; |
| 4108 | struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa; |
| 4109 | |
| 4110 | order = fls(pa->pa_free) - 1; |
| 4111 | if (order > PREALLOC_TB_SIZE - 1) |
| 4112 | /* The max size of hash table is PREALLOC_TB_SIZE */ |
| 4113 | order = PREALLOC_TB_SIZE - 1; |
| 4114 | /* Add the prealloc space to lg */ |
| 4115 | rcu_read_lock(); |
| 4116 | list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order], |
| 4117 | pa_inode_list) { |
| 4118 | spin_lock(&tmp_pa->pa_lock); |
| 4119 | if (tmp_pa->pa_deleted) { |
Theodore Ts'o | e7c9e3e | 2009-03-27 19:43:21 -0400 | [diff] [blame] | 4120 | spin_unlock(&tmp_pa->pa_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4121 | continue; |
| 4122 | } |
| 4123 | if (!added && pa->pa_free < tmp_pa->pa_free) { |
| 4124 | /* Add to the tail of the previous entry */ |
| 4125 | list_add_tail_rcu(&pa->pa_inode_list, |
| 4126 | &tmp_pa->pa_inode_list); |
| 4127 | added = 1; |
| 4128 | /* |
| 4129 | * we want to count the total |
| 4130 | * number of entries in the list |
| 4131 | */ |
| 4132 | } |
| 4133 | spin_unlock(&tmp_pa->pa_lock); |
| 4134 | lg_prealloc_count++; |
| 4135 | } |
| 4136 | if (!added) |
| 4137 | list_add_tail_rcu(&pa->pa_inode_list, |
| 4138 | &lg->lg_prealloc_list[order]); |
| 4139 | rcu_read_unlock(); |
| 4140 | |
| 4141 | /* Now trim the list to be not more than 8 elements */ |
| 4142 | if (lg_prealloc_count > 8) { |
| 4143 | ext4_mb_discard_lg_preallocations(sb, lg, |
| 4144 | order, lg_prealloc_count); |
| 4145 | return; |
| 4146 | } |
| 4147 | return ; |
| 4148 | } |
| 4149 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4150 | /* |
| 4151 | * release all resource we used in allocation |
| 4152 | */ |
| 4153 | static int ext4_mb_release_context(struct ext4_allocation_context *ac) |
| 4154 | { |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4155 | struct ext4_prealloc_space *pa = ac->ac_pa; |
| 4156 | if (pa) { |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4157 | if (pa->pa_type == MB_GROUP_PA) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4158 | /* see comment in ext4_mb_use_group_pa() */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4159 | spin_lock(&pa->pa_lock); |
| 4160 | pa->pa_pstart += ac->ac_b_ex.fe_len; |
| 4161 | pa->pa_lstart += ac->ac_b_ex.fe_len; |
| 4162 | pa->pa_free -= ac->ac_b_ex.fe_len; |
| 4163 | pa->pa_len -= ac->ac_b_ex.fe_len; |
| 4164 | spin_unlock(&pa->pa_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4165 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4166 | } |
Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 4167 | if (ac->alloc_semp) |
| 4168 | up_read(ac->alloc_semp); |
Aneesh Kumar K.V | ba44391 | 2009-02-10 11:14:34 -0500 | [diff] [blame] | 4169 | if (pa) { |
| 4170 | /* |
| 4171 | * We want to add the pa to the right bucket. |
| 4172 | * Remove it from the list and while adding |
| 4173 | * make sure the list to which we are adding |
| 4174 | * doesn't grow big. We need to release |
| 4175 | * alloc_semp before calling ext4_mb_add_n_trim() |
| 4176 | */ |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4177 | 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] | 4178 | spin_lock(pa->pa_obj_lock); |
| 4179 | list_del_rcu(&pa->pa_inode_list); |
| 4180 | spin_unlock(pa->pa_obj_lock); |
| 4181 | ext4_mb_add_n_trim(ac); |
| 4182 | } |
| 4183 | ext4_mb_put_pa(ac, ac->ac_sb, pa); |
| 4184 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4185 | if (ac->ac_bitmap_page) |
| 4186 | page_cache_release(ac->ac_bitmap_page); |
| 4187 | if (ac->ac_buddy_page) |
| 4188 | page_cache_release(ac->ac_buddy_page); |
| 4189 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
| 4190 | mutex_unlock(&ac->ac_lg->lg_mutex); |
| 4191 | ext4_mb_collect_stats(ac); |
| 4192 | return 0; |
| 4193 | } |
| 4194 | |
| 4195 | static int ext4_mb_discard_preallocations(struct super_block *sb, int needed) |
| 4196 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4197 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4198 | int ret; |
| 4199 | int freed = 0; |
| 4200 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4201 | trace_ext4_mb_discard_preallocations(sb, needed); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4202 | for (i = 0; i < ngroups && needed > 0; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4203 | ret = ext4_mb_discard_group_preallocations(sb, i, needed); |
| 4204 | freed += ret; |
| 4205 | needed -= ret; |
| 4206 | } |
| 4207 | |
| 4208 | return freed; |
| 4209 | } |
| 4210 | |
| 4211 | /* |
| 4212 | * Main entry point into mballoc to allocate blocks |
| 4213 | * it tries to use preallocation first, then falls back |
| 4214 | * to usual allocation |
| 4215 | */ |
| 4216 | ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, |
| 4217 | struct ext4_allocation_request *ar, int *errp) |
| 4218 | { |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 4219 | int freed; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4220 | struct ext4_allocation_context *ac = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4221 | struct ext4_sb_info *sbi; |
| 4222 | struct super_block *sb; |
| 4223 | ext4_fsblk_t block = 0; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4224 | unsigned int inquota = 0; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4225 | unsigned int reserv_blks = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4226 | |
| 4227 | sb = ar->inode->i_sb; |
| 4228 | sbi = EXT4_SB(sb); |
| 4229 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4230 | trace_ext4_request_blocks(ar); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 4231 | |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4232 | /* |
| 4233 | * For delayed allocation, we could skip the ENOSPC and |
| 4234 | * EDQUOT check, as blocks and quotas have been already |
| 4235 | * reserved when data being copied into pagecache. |
| 4236 | */ |
| 4237 | if (EXT4_I(ar->inode)->i_delalloc_reserved_flag) |
| 4238 | ar->flags |= EXT4_MB_DELALLOC_RESERVED; |
| 4239 | else { |
| 4240 | /* Without delayed allocation we need to verify |
| 4241 | * there is enough free blocks to do block allocation |
| 4242 | * and verify allocation doesn't exceed the quota limits. |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4243 | */ |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 4244 | while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) { |
| 4245 | /* let others to free the space */ |
| 4246 | yield(); |
| 4247 | ar->len = ar->len >> 1; |
| 4248 | } |
| 4249 | if (!ar->len) { |
Aneesh Kumar K.V | a30d542a | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 4250 | *errp = -ENOSPC; |
| 4251 | return 0; |
| 4252 | } |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 4253 | reserv_blks = ar->len; |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 4254 | while (ar->len && vfs_dq_alloc_block(ar->inode, ar->len)) { |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4255 | ar->flags |= EXT4_MB_HINT_NOPREALLOC; |
| 4256 | ar->len--; |
| 4257 | } |
| 4258 | inquota = ar->len; |
| 4259 | if (ar->len == 0) { |
| 4260 | *errp = -EDQUOT; |
| 4261 | goto out3; |
| 4262 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4263 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4264 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4265 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 833576b | 2009-07-13 09:45:52 -0400 | [diff] [blame] | 4266 | if (!ac) { |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4267 | ar->len = 0; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4268 | *errp = -ENOMEM; |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4269 | goto out1; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4270 | } |
| 4271 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4272 | *errp = ext4_mb_initialize_context(ac, ar); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4273 | if (*errp) { |
| 4274 | ar->len = 0; |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4275 | goto out2; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4276 | } |
| 4277 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4278 | ac->ac_op = EXT4_MB_HISTORY_PREALLOC; |
| 4279 | if (!ext4_mb_use_preallocated(ac)) { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4280 | ac->ac_op = EXT4_MB_HISTORY_ALLOC; |
| 4281 | ext4_mb_normalize_request(ac, ar); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4282 | repeat: |
| 4283 | /* allocate space in core */ |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4284 | ext4_mb_regular_allocator(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4285 | |
| 4286 | /* as we've just preallocated more space than |
| 4287 | * user requested orinally, we store allocated |
| 4288 | * space in a special descriptor */ |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4289 | if (ac->ac_status == AC_STATUS_FOUND && |
| 4290 | ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) |
| 4291 | ext4_mb_new_preallocation(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4292 | } |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4293 | if (likely(ac->ac_status == AC_STATUS_FOUND)) { |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 4294 | *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 4295 | if (*errp == -EAGAIN) { |
Aneesh Kumar K.V | 8556e8f | 2009-01-05 21:46:55 -0500 | [diff] [blame] | 4296 | /* |
| 4297 | * drop the reference that we took |
| 4298 | * in ext4_mb_use_best_found |
| 4299 | */ |
| 4300 | ext4_mb_release_context(ac); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 4301 | ac->ac_b_ex.fe_group = 0; |
| 4302 | ac->ac_b_ex.fe_start = 0; |
| 4303 | ac->ac_b_ex.fe_len = 0; |
| 4304 | ac->ac_status = AC_STATUS_CONTINUE; |
| 4305 | goto repeat; |
| 4306 | } else if (*errp) { |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 4307 | ext4_discard_allocated_blocks(ac); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 4308 | ac->ac_b_ex.fe_len = 0; |
| 4309 | ar->len = 0; |
| 4310 | ext4_mb_show_ac(ac); |
| 4311 | } else { |
| 4312 | block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 4313 | ar->len = ac->ac_b_ex.fe_len; |
| 4314 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4315 | } else { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4316 | freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4317 | if (freed) |
| 4318 | goto repeat; |
| 4319 | *errp = -ENOSPC; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4320 | ac->ac_b_ex.fe_len = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4321 | ar->len = 0; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4322 | ext4_mb_show_ac(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4323 | } |
| 4324 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4325 | ext4_mb_release_context(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4326 | |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4327 | out2: |
| 4328 | kmem_cache_free(ext4_ac_cachep, ac); |
| 4329 | out1: |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4330 | if (inquota && ar->len < inquota) |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 4331 | vfs_dq_free_block(ar->inode, inquota - ar->len); |
Aneesh Kumar K.V | 0087d9f | 2009-01-05 21:49:12 -0500 | [diff] [blame] | 4332 | out3: |
| 4333 | if (!ar->len) { |
| 4334 | if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag) |
| 4335 | /* release all the reserved blocks if non delalloc */ |
| 4336 | percpu_counter_sub(&sbi->s_dirtyblocks_counter, |
| 4337 | reserv_blks); |
| 4338 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4339 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4340 | trace_ext4_allocate_blocks(ar, (unsigned long long)block); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 4341 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4342 | return block; |
| 4343 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4344 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4345 | /* |
| 4346 | * We can merge two free data extents only if the physical blocks |
| 4347 | * are contiguous, AND the extents were freed by the same transaction, |
| 4348 | * AND the blocks are associated with the same group. |
| 4349 | */ |
| 4350 | static int can_merge(struct ext4_free_data *entry1, |
| 4351 | struct ext4_free_data *entry2) |
| 4352 | { |
| 4353 | if ((entry1->t_tid == entry2->t_tid) && |
| 4354 | (entry1->group == entry2->group) && |
| 4355 | ((entry1->start_blk + entry1->count) == entry2->start_blk)) |
| 4356 | return 1; |
| 4357 | return 0; |
| 4358 | } |
| 4359 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4360 | static noinline_for_stack int |
| 4361 | 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] | 4362 | struct ext4_free_data *new_entry) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4363 | { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4364 | ext4_grpblk_t block; |
| 4365 | struct ext4_free_data *entry; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4366 | struct ext4_group_info *db = e4b->bd_info; |
| 4367 | struct super_block *sb = e4b->bd_sb; |
| 4368 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4369 | struct rb_node **n = &db->bb_free_root.rb_node, *node; |
| 4370 | struct rb_node *parent = NULL, *new_node; |
| 4371 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4372 | BUG_ON(!ext4_handle_valid(handle)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4373 | BUG_ON(e4b->bd_bitmap_page == NULL); |
| 4374 | BUG_ON(e4b->bd_buddy_page == NULL); |
| 4375 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4376 | new_node = &new_entry->node; |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4377 | block = new_entry->start_blk; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4378 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4379 | if (!*n) { |
| 4380 | /* first free block exent. We need to |
| 4381 | protect buddy cache from being freed, |
| 4382 | * otherwise we'll refresh it from |
| 4383 | * on-disk bitmap and lose not-yet-available |
| 4384 | * blocks */ |
| 4385 | page_cache_get(e4b->bd_buddy_page); |
| 4386 | page_cache_get(e4b->bd_bitmap_page); |
| 4387 | } |
| 4388 | while (*n) { |
| 4389 | parent = *n; |
| 4390 | entry = rb_entry(parent, struct ext4_free_data, node); |
| 4391 | if (block < entry->start_blk) |
| 4392 | n = &(*n)->rb_left; |
| 4393 | else if (block >= (entry->start_blk + entry->count)) |
| 4394 | n = &(*n)->rb_right; |
| 4395 | else { |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 4396 | ext4_grp_locked_error(sb, e4b->bd_group, __func__, |
| 4397 | "Double free of blocks %d (%d %d)", |
| 4398 | block, entry->start_blk, entry->count); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4399 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4400 | } |
| 4401 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4402 | |
| 4403 | rb_link_node(new_node, parent, n); |
| 4404 | rb_insert_color(new_node, &db->bb_free_root); |
| 4405 | |
| 4406 | /* Now try to see the extent can be merged to left and right */ |
| 4407 | node = rb_prev(new_node); |
| 4408 | if (node) { |
| 4409 | entry = rb_entry(node, struct ext4_free_data, node); |
| 4410 | if (can_merge(entry, new_entry)) { |
| 4411 | new_entry->start_blk = entry->start_blk; |
| 4412 | new_entry->count += entry->count; |
| 4413 | rb_erase(node, &(db->bb_free_root)); |
| 4414 | spin_lock(&sbi->s_md_lock); |
| 4415 | list_del(&entry->list); |
| 4416 | spin_unlock(&sbi->s_md_lock); |
| 4417 | kmem_cache_free(ext4_free_ext_cachep, entry); |
| 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | node = rb_next(new_node); |
| 4422 | if (node) { |
| 4423 | entry = rb_entry(node, struct ext4_free_data, node); |
| 4424 | if (can_merge(new_entry, entry)) { |
| 4425 | new_entry->count += entry->count; |
| 4426 | rb_erase(node, &(db->bb_free_root)); |
| 4427 | spin_lock(&sbi->s_md_lock); |
| 4428 | list_del(&entry->list); |
| 4429 | spin_unlock(&sbi->s_md_lock); |
| 4430 | kmem_cache_free(ext4_free_ext_cachep, entry); |
| 4431 | } |
| 4432 | } |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 4433 | /* Add the extent to transaction's private list */ |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4434 | spin_lock(&sbi->s_md_lock); |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 4435 | 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] | 4436 | spin_unlock(&sbi->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4437 | return 0; |
| 4438 | } |
| 4439 | |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 4440 | /** |
| 4441 | * ext4_free_blocks() -- Free given blocks and update quota |
| 4442 | * @handle: handle for this transaction |
| 4443 | * @inode: inode |
| 4444 | * @block: start physical block to free |
| 4445 | * @count: number of blocks to count |
| 4446 | * @metadata: Are these metadata blocks |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4447 | */ |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 4448 | void ext4_free_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 4449 | struct buffer_head *bh, ext4_fsblk_t block, |
| 4450 | unsigned long count, int flags) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4451 | { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 4452 | struct buffer_head *bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4453 | struct super_block *sb = inode->i_sb; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4454 | struct ext4_allocation_context *ac = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4455 | struct ext4_group_desc *gdp; |
| 4456 | struct ext4_super_block *es; |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 4457 | unsigned long freed = 0; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4458 | unsigned int overflow; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4459 | ext4_grpblk_t bit; |
| 4460 | struct buffer_head *gd_bh; |
| 4461 | ext4_group_t block_group; |
| 4462 | struct ext4_sb_info *sbi; |
| 4463 | struct ext4_buddy e4b; |
| 4464 | int err = 0; |
| 4465 | int ret; |
| 4466 | |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 4467 | if (bh) { |
| 4468 | if (block) |
| 4469 | BUG_ON(block != bh->b_blocknr); |
| 4470 | else |
| 4471 | block = bh->b_blocknr; |
| 4472 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4473 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4474 | sbi = EXT4_SB(sb); |
| 4475 | es = EXT4_SB(sb)->s_es; |
Theodore Ts'o | 1f2acb6 | 2010-01-22 17:40:42 -0500 | [diff] [blame] | 4476 | if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) && |
| 4477 | !ext4_data_block_valid(sbi, block, count)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame^] | 4478 | ext4_error(sb, "Freeing blocks not in datazone - " |
Theodore Ts'o | 1f2acb6 | 2010-01-22 17:40:42 -0500 | [diff] [blame] | 4479 | "block = %llu, count = %lu", block, count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4480 | goto error_return; |
| 4481 | } |
| 4482 | |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 4483 | ext4_debug("freeing block %llu\n", block); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 4484 | trace_ext4_free_blocks(inode, block, count, flags); |
| 4485 | |
| 4486 | if (flags & EXT4_FREE_BLOCKS_FORGET) { |
| 4487 | struct buffer_head *tbh = bh; |
| 4488 | int i; |
| 4489 | |
| 4490 | BUG_ON(bh && (count > 1)); |
| 4491 | |
| 4492 | for (i = 0; i < count; i++) { |
| 4493 | if (!bh) |
| 4494 | tbh = sb_find_get_block(inode->i_sb, |
| 4495 | block + i); |
| 4496 | ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA, |
| 4497 | inode, tbh, block + i); |
| 4498 | } |
| 4499 | } |
| 4500 | |
| 4501 | /* |
| 4502 | * We need to make sure we don't reuse the freed block until |
| 4503 | * after the transaction is committed, which we can do by |
| 4504 | * treating the block as metadata, below. We make an |
| 4505 | * exception if the inode is to be written in writeback mode |
| 4506 | * since writeback mode has weak data consistency guarantees. |
| 4507 | */ |
| 4508 | if (!ext4_should_writeback_data(inode)) |
| 4509 | flags |= EXT4_FREE_BLOCKS_METADATA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4510 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4511 | ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); |
| 4512 | if (ac) { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4513 | ac->ac_inode = inode; |
| 4514 | ac->ac_sb = sb; |
| 4515 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4516 | |
| 4517 | do_more: |
| 4518 | overflow = 0; |
| 4519 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
| 4520 | |
| 4521 | /* |
| 4522 | * Check to see if we are freeing blocks across a group |
| 4523 | * boundary. |
| 4524 | */ |
| 4525 | if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) { |
| 4526 | overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb); |
| 4527 | count -= overflow; |
| 4528 | } |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4529 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4530 | if (!bitmap_bh) { |
| 4531 | err = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4532 | goto error_return; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4533 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4534 | gdp = ext4_get_group_desc(sb, block_group, &gd_bh); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4535 | if (!gdp) { |
| 4536 | err = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4537 | goto error_return; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4538 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4539 | |
| 4540 | if (in_range(ext4_block_bitmap(sb, gdp), block, count) || |
| 4541 | in_range(ext4_inode_bitmap(sb, gdp), block, count) || |
| 4542 | in_range(block, ext4_inode_table(sb, gdp), |
| 4543 | EXT4_SB(sb)->s_itb_per_group) || |
| 4544 | in_range(block + count - 1, ext4_inode_table(sb, gdp), |
| 4545 | EXT4_SB(sb)->s_itb_per_group)) { |
| 4546 | |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame^] | 4547 | ext4_error(sb, "Freeing blocks in system zone - " |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 4548 | "Block = %llu, count = %lu", block, count); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 4549 | /* err = 0. ext4_std_error should be a no op */ |
| 4550 | goto error_return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
| 4554 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
| 4555 | if (err) |
| 4556 | goto error_return; |
| 4557 | |
| 4558 | /* |
| 4559 | * We are about to modify some metadata. Call the journal APIs |
| 4560 | * to unshare ->b_data if a currently-committing transaction is |
| 4561 | * using it |
| 4562 | */ |
| 4563 | BUFFER_TRACE(gd_bh, "get_write_access"); |
| 4564 | err = ext4_journal_get_write_access(handle, gd_bh); |
| 4565 | if (err) |
| 4566 | goto error_return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4567 | #ifdef AGGRESSIVE_CHECK |
| 4568 | { |
| 4569 | int i; |
| 4570 | for (i = 0; i < count; i++) |
| 4571 | BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data)); |
| 4572 | } |
| 4573 | #endif |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4574 | if (ac) { |
| 4575 | ac->ac_b_ex.fe_group = block_group; |
| 4576 | ac->ac_b_ex.fe_start = bit; |
| 4577 | ac->ac_b_ex.fe_len = count; |
Theodore Ts'o | 296c355 | 2009-09-30 00:32:42 -0400 | [diff] [blame] | 4578 | trace_ext4_mballoc_free(ac); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4579 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4580 | |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 4581 | err = ext4_mb_load_buddy(sb, block_group, &e4b); |
| 4582 | if (err) |
| 4583 | goto error_return; |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 4584 | |
| 4585 | if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4586 | struct ext4_free_data *new_entry; |
| 4587 | /* |
| 4588 | * blocks being freed are metadata. these blocks shouldn't |
| 4589 | * be used until this transaction is committed |
| 4590 | */ |
| 4591 | new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS); |
| 4592 | new_entry->start_blk = bit; |
| 4593 | new_entry->group = block_group; |
| 4594 | new_entry->count = count; |
| 4595 | new_entry->t_tid = handle->h_transaction->t_tid; |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 4596 | |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4597 | ext4_lock_group(sb, block_group); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 4598 | mb_clear_bits(bitmap_bh->b_data, bit, count); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4599 | ext4_mb_free_metadata(handle, &e4b, new_entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4600 | } else { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4601 | /* need to update group_info->bb_free and bitmap |
| 4602 | * with group lock held. generate_buddy look at |
| 4603 | * them with group lock_held |
| 4604 | */ |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 4605 | ext4_lock_group(sb, block_group); |
| 4606 | mb_clear_bits(bitmap_bh->b_data, bit, count); |
Shen Feng | 7e5a8cd | 2008-07-13 21:03:31 -0400 | [diff] [blame] | 4607 | mb_free_blocks(inode, &e4b, bit, count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4608 | ext4_mb_return_to_preallocation(inode, &e4b, block, count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4609 | } |
| 4610 | |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 4611 | ret = ext4_free_blks_count(sb, gdp) + count; |
| 4612 | ext4_free_blks_set(sb, gdp, ret); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4613 | 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] | 4614 | ext4_unlock_group(sb, block_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4615 | percpu_counter_add(&sbi->s_freeblocks_counter, count); |
| 4616 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4617 | if (sbi->s_log_groups_per_flex) { |
| 4618 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 4619 | atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4620 | } |
| 4621 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4622 | ext4_mb_release_desc(&e4b); |
| 4623 | |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 4624 | freed += count; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4625 | |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4626 | /* We dirtied the bitmap block */ |
| 4627 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
| 4628 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
| 4629 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4630 | /* And the group descriptor block */ |
| 4631 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4632 | ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4633 | if (!err) |
| 4634 | err = ret; |
| 4635 | |
| 4636 | if (overflow && !err) { |
| 4637 | block += count; |
| 4638 | count = overflow; |
| 4639 | put_bh(bitmap_bh); |
| 4640 | goto do_more; |
| 4641 | } |
| 4642 | sb->s_dirt = 1; |
| 4643 | error_return: |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 4644 | if (freed) |
| 4645 | vfs_dq_free_block(inode, freed); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4646 | brelse(bitmap_bh); |
| 4647 | ext4_std_error(sb, err); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4648 | if (ac) |
| 4649 | kmem_cache_free(ext4_ac_cachep, ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4650 | return; |
| 4651 | } |