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