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