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