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