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