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