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