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> |
Jeremy Cline | 1a5d5e5 | 2018-08-02 00:03:40 -0400 | [diff] [blame] | 17 | #include <linux/nospec.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 18 | #include <linux/backing-dev.h> |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 19 | #include <trace/events/ext4.h> |
| 20 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 21 | /* |
| 22 | * MUSTDO: |
| 23 | * - test ext4_ext_search_left() and ext4_ext_search_right() |
| 24 | * - search for metadata in few groups |
| 25 | * |
| 26 | * TODO v4: |
| 27 | * - normalization should take into account whether file is still open |
| 28 | * - discard preallocations if no free space left (policy?) |
| 29 | * - don't normalize tails |
| 30 | * - quota |
| 31 | * - reservation for superuser |
| 32 | * |
| 33 | * TODO v3: |
| 34 | * - bitmap read-ahead (proposed by Oleg Drokin aka green) |
| 35 | * - track min/max extents in each group for better group selection |
| 36 | * - mb_mark_used() may allocate chunk right after splitting buddy |
| 37 | * - tree of groups sorted by number of free blocks |
| 38 | * - error handling |
| 39 | */ |
| 40 | |
| 41 | /* |
| 42 | * The allocation request involve request for multiple number of blocks |
| 43 | * near to the goal(block) value specified. |
| 44 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 45 | * During initialization phase of the allocator we decide to use the |
| 46 | * group preallocation or inode preallocation depending on the size of |
| 47 | * the file. The size of the file could be the resulting file size we |
| 48 | * would have after allocation, or the current file size, which ever |
| 49 | * is larger. If the size is less than sbi->s_mb_stream_request we |
| 50 | * select to use the group preallocation. The default value of |
| 51 | * s_mb_stream_request is 16 blocks. This can also be tuned via |
| 52 | * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in |
| 53 | * terms of number of blocks. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 54 | * |
| 55 | * 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] | 56 | * ensure that we have small files closer together on the disk. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 57 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 58 | * First stage the allocator looks at the inode prealloc list, |
| 59 | * ext4_inode_info->i_prealloc_list, which contains list of prealloc |
| 60 | * spaces for this particular inode. The inode prealloc space is |
| 61 | * represented as: |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 62 | * |
| 63 | * pa_lstart -> the logical start block for this prealloc space |
| 64 | * pa_pstart -> the physical start block for this prealloc space |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 65 | * pa_len -> length for this prealloc space (in clusters) |
| 66 | * pa_free -> free space available in this prealloc space (in clusters) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 67 | * |
| 68 | * The inode preallocation space is used looking at the _logical_ start |
| 69 | * 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] | 70 | * space we will consume the particular prealloc space. This makes sure that |
| 71 | * we have contiguous physical blocks representing the file blocks |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 72 | * |
| 73 | * The important thing to be noted in case of inode prealloc space is that |
| 74 | * we don't modify the values associated to inode prealloc space except |
| 75 | * pa_free. |
| 76 | * |
| 77 | * If we are not able to find blocks in the inode prealloc space and if we |
| 78 | * 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] | 79 | * prealloc space. These are per CPU prealloc list represented as |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 80 | * |
| 81 | * ext4_sb_info.s_locality_groups[smp_processor_id()] |
| 82 | * |
| 83 | * The reason for having a per cpu locality group is to reduce the contention |
| 84 | * between CPUs. It is possible to get scheduled at this point. |
| 85 | * |
| 86 | * 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] | 87 | * enough free space (pa_free) within the prealloc space. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 88 | * |
| 89 | * If we can't allocate blocks via inode prealloc or/and locality group |
| 90 | * prealloc then we look at the buddy cache. The buddy cache is represented |
| 91 | * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets |
| 92 | * mapped to the buddy and bitmap information regarding different |
| 93 | * groups. The buddy information is attached to buddy cache inode so that |
| 94 | * we can access them through the page cache. The information regarding |
| 95 | * each group is loaded via ext4_mb_load_buddy. The information involve |
| 96 | * block bitmap and buddy information. The information are stored in the |
| 97 | * inode as: |
| 98 | * |
| 99 | * { page } |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 100 | * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 101 | * |
| 102 | * |
| 103 | * 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] | 104 | * 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] | 105 | * blocksize) blocks. So it can have information regarding groups_per_page |
| 106 | * which is blocks_per_page/2 |
| 107 | * |
| 108 | * The buddy cache inode is not stored on disk. The inode is thrown |
| 109 | * away when the filesystem is unmounted. |
| 110 | * |
| 111 | * We look for count number of blocks in the buddy cache. If we were able |
| 112 | * to locate that many free blocks we return with additional information |
| 113 | * regarding rest of the contiguous physical block available |
| 114 | * |
| 115 | * Before allocating blocks via buddy cache we normalize the request |
| 116 | * blocks. This ensure we ask for more blocks that we needed. The extra |
| 117 | * blocks that we get after allocation is added to the respective prealloc |
| 118 | * list. In case of inode preallocation we follow a list of heuristics |
| 119 | * based on file size. This can be found in ext4_mb_normalize_request. If |
| 120 | * 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] | 121 | * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is |
| 122 | * dependent on the cluster size; for non-bigalloc file systems, it is |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 123 | * 512 blocks. This can be tuned via |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 124 | * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 125 | * terms of number of blocks. If we have mounted the file system with -O |
| 126 | * stripe=<value> option the group prealloc request is normalized to the |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 127 | * the smallest multiple of the stripe value (sbi->s_stripe) which is |
| 128 | * greater than the default mb_group_prealloc. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 129 | * |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 130 | * The regular allocator (using the buddy cache) supports a few tunables. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 131 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 132 | * /sys/fs/ext4/<partition>/mb_min_to_scan |
| 133 | * /sys/fs/ext4/<partition>/mb_max_to_scan |
| 134 | * /sys/fs/ext4/<partition>/mb_order2_req |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 135 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 136 | * 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] | 137 | * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The |
| 138 | * value of s_mb_order2_reqs can be tuned via |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 139 | * /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] | 140 | * 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] | 141 | * stripe size. This should result in better allocation on RAID setups. If |
| 142 | * not, we search in the specific group using bitmap for best extents. The |
| 143 | * tunable min_to_scan and max_to_scan control the behaviour here. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 144 | * 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] | 145 | * 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] | 146 | * best extent in the found extents. Searching for the blocks starts with |
| 147 | * the group specified as the goal value in allocation context via |
| 148 | * 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] | 149 | * 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] | 150 | * checked. |
| 151 | * |
| 152 | * Both the prealloc space are getting populated as above. So for the first |
| 153 | * request we will hit the buddy cache which will result in this prealloc |
| 154 | * space getting filled. The prealloc space is then later used for the |
| 155 | * subsequent request. |
| 156 | */ |
| 157 | |
| 158 | /* |
| 159 | * mballoc operates on the following data: |
| 160 | * - on-disk bitmap |
| 161 | * - in-core buddy (actually includes buddy and bitmap) |
| 162 | * - preallocation descriptors (PAs) |
| 163 | * |
| 164 | * there are two types of preallocations: |
| 165 | * - inode |
| 166 | * assiged to specific inode and can be used for this inode only. |
| 167 | * it describes part of inode's space preallocated to specific |
| 168 | * physical blocks. any block from that preallocated can be used |
| 169 | * independent. the descriptor just tracks number of blocks left |
| 170 | * unused. so, before taking some block from descriptor, one must |
| 171 | * make sure corresponded logical block isn't allocated yet. this |
| 172 | * also means that freeing any block within descriptor's range |
| 173 | * must discard all preallocated blocks. |
| 174 | * - locality group |
| 175 | * assigned to specific locality group which does not translate to |
| 176 | * permanent set of inodes: inode can join and leave group. space |
| 177 | * from this type of preallocation can be used for any inode. thus |
| 178 | * it's consumed from the beginning to the end. |
| 179 | * |
| 180 | * relation between them can be expressed as: |
| 181 | * in-core buddy = on-disk bitmap + preallocation descriptors |
| 182 | * |
| 183 | * this mean blocks mballoc considers used are: |
| 184 | * - allocated blocks (persistent) |
| 185 | * - preallocated blocks (non-persistent) |
| 186 | * |
| 187 | * consistency in mballoc world means that at any time a block is either |
| 188 | * free or used in ALL structures. notice: "any time" should not be read |
| 189 | * literally -- time is discrete and delimited by locks. |
| 190 | * |
| 191 | * to keep it simple, we don't use block numbers, instead we count number of |
| 192 | * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA. |
| 193 | * |
| 194 | * all operations can be expressed as: |
| 195 | * - init buddy: buddy = on-disk + PAs |
| 196 | * - new PA: buddy += N; PA = N |
| 197 | * - use inode PA: on-disk += N; PA -= N |
| 198 | * - discard inode PA buddy -= on-disk - PA; PA = 0 |
| 199 | * - use locality group PA on-disk += N; PA -= N |
| 200 | * - discard locality group PA buddy -= PA; PA = 0 |
| 201 | * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap |
| 202 | * is used in real operation because we can't know actual used |
| 203 | * bits from PA, only from on-disk bitmap |
| 204 | * |
| 205 | * if we follow this strict logic, then all operations above should be atomic. |
| 206 | * given some of them can block, we'd have to use something like semaphores |
| 207 | * killing performance on high-end SMP hardware. let's try to relax it using |
| 208 | * the following knowledge: |
| 209 | * 1) if buddy is referenced, it's already initialized |
| 210 | * 2) while block is used in buddy and the buddy is referenced, |
| 211 | * nobody can re-allocate that block |
| 212 | * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has |
| 213 | * bit set and PA claims same block, it's OK. IOW, one can set bit in |
| 214 | * on-disk bitmap if buddy has same bit set or/and PA covers corresponded |
| 215 | * block |
| 216 | * |
| 217 | * so, now we're building a concurrency table: |
| 218 | * - init buddy vs. |
| 219 | * - new PA |
| 220 | * blocks for PA are allocated in the buddy, buddy must be referenced |
| 221 | * until PA is linked to allocation group to avoid concurrent buddy init |
| 222 | * - use inode PA |
| 223 | * we need to make sure that either on-disk bitmap or PA has uptodate data |
| 224 | * given (3) we care that PA-=N operation doesn't interfere with init |
| 225 | * - discard inode PA |
| 226 | * the simplest way would be to have buddy initialized by the discard |
| 227 | * - use locality group PA |
| 228 | * again PA-=N must be serialized with init |
| 229 | * - discard locality group PA |
| 230 | * the simplest way would be to have buddy initialized by the discard |
| 231 | * - new PA vs. |
| 232 | * - use inode PA |
| 233 | * i_data_sem serializes them |
| 234 | * - discard inode PA |
| 235 | * discard process must wait until PA isn't used by another process |
| 236 | * - use locality group PA |
| 237 | * some mutex should serialize them |
| 238 | * - discard locality group PA |
| 239 | * discard process must wait until PA isn't used by another process |
| 240 | * - use inode PA |
| 241 | * - use inode PA |
| 242 | * i_data_sem or another mutex should serializes them |
| 243 | * - discard inode PA |
| 244 | * discard process must wait until PA isn't used by another process |
| 245 | * - use locality group PA |
| 246 | * nothing wrong here -- they're different PAs covering different blocks |
| 247 | * - discard locality group PA |
| 248 | * discard process must wait until PA isn't used by another process |
| 249 | * |
| 250 | * now we're ready to make few consequences: |
| 251 | * - PA is referenced and while it is no discard is possible |
| 252 | * - PA is referenced until block isn't marked in on-disk bitmap |
| 253 | * - PA changes only after on-disk bitmap |
| 254 | * - discard must not compete with init. either init is done before |
| 255 | * any discard or they're serialized somehow |
| 256 | * - buddy init as sum of on-disk bitmap and PAs is done atomically |
| 257 | * |
| 258 | * a special case when we've used PA to emptiness. no need to modify buddy |
| 259 | * in this case, but we should care about concurrent init |
| 260 | * |
| 261 | */ |
| 262 | |
| 263 | /* |
| 264 | * Logic in few words: |
| 265 | * |
| 266 | * - allocation: |
| 267 | * load group |
| 268 | * find blocks |
| 269 | * mark bits in on-disk bitmap |
| 270 | * release group |
| 271 | * |
| 272 | * - use preallocation: |
| 273 | * find proper PA (per-inode or group) |
| 274 | * load group |
| 275 | * mark bits in on-disk bitmap |
| 276 | * release group |
| 277 | * release PA |
| 278 | * |
| 279 | * - free: |
| 280 | * load group |
| 281 | * mark bits in on-disk bitmap |
| 282 | * release group |
| 283 | * |
| 284 | * - discard preallocations in group: |
| 285 | * mark PAs deleted |
| 286 | * move them onto local list |
| 287 | * load on-disk bitmap |
| 288 | * load group |
| 289 | * remove PA from object (inode or locality group) |
| 290 | * mark free blocks in-core |
| 291 | * |
| 292 | * - discard inode's preallocations: |
| 293 | */ |
| 294 | |
| 295 | /* |
| 296 | * Locking rules |
| 297 | * |
| 298 | * Locks: |
| 299 | * - bitlock on a group (group) |
| 300 | * - object (inode/locality) (object) |
| 301 | * - per-pa lock (pa) |
| 302 | * |
| 303 | * Paths: |
| 304 | * - new pa |
| 305 | * object |
| 306 | * group |
| 307 | * |
| 308 | * - find and use pa: |
| 309 | * pa |
| 310 | * |
| 311 | * - release consumed pa: |
| 312 | * pa |
| 313 | * group |
| 314 | * object |
| 315 | * |
| 316 | * - generate in-core bitmap: |
| 317 | * group |
| 318 | * pa |
| 319 | * |
| 320 | * - discard all for given object (inode, locality group): |
| 321 | * object |
| 322 | * pa |
| 323 | * group |
| 324 | * |
| 325 | * - discard all for given group: |
| 326 | * group |
| 327 | * pa |
| 328 | * group |
| 329 | * object |
| 330 | * |
| 331 | */ |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 332 | static struct kmem_cache *ext4_pspace_cachep; |
| 333 | static struct kmem_cache *ext4_ac_cachep; |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 334 | static struct kmem_cache *ext4_free_data_cachep; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 335 | |
| 336 | /* We create slab caches for groupinfo data structures based on the |
| 337 | * superblock block size. There will be one per mounted filesystem for |
| 338 | * each unique s_blocksize_bits */ |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 339 | #define NR_GRPINFO_CACHES 8 |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 340 | static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES]; |
| 341 | |
Eric Biggers | d600618 | 2017-04-29 23:47:50 -0400 | [diff] [blame] | 342 | static const char * const ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = { |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 343 | "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k", |
| 344 | "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k", |
| 345 | "ext4_groupinfo_64k", "ext4_groupinfo_128k" |
| 346 | }; |
| 347 | |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 348 | static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
| 349 | ext4_group_t group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 350 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, |
| 351 | ext4_group_t group); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 352 | static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac); |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 353 | |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 354 | /* |
| 355 | * The algorithm using this percpu seq counter goes below: |
| 356 | * 1. We sample the percpu discard_pa_seq counter before trying for block |
| 357 | * allocation in ext4_mb_new_blocks(). |
| 358 | * 2. We increment this percpu discard_pa_seq counter when we either allocate |
| 359 | * or free these blocks i.e. while marking those blocks as used/free in |
| 360 | * mb_mark_used()/mb_free_blocks(). |
| 361 | * 3. We also increment this percpu seq counter when we successfully identify |
| 362 | * that the bb_prealloc_list is not empty and hence proceed for discarding |
| 363 | * of those PAs inside ext4_mb_discard_group_preallocations(). |
| 364 | * |
| 365 | * Now to make sure that the regular fast path of block allocation is not |
| 366 | * affected, as a small optimization we only sample the percpu seq counter |
| 367 | * on that cpu. Only when the block allocation fails and when freed blocks |
| 368 | * found were 0, that is when we sample percpu seq counter for all cpus using |
| 369 | * below function ext4_get_discard_pa_seq_sum(). This happens after making |
| 370 | * sure that all the PAs on grp->bb_prealloc_list got freed or if it's empty. |
| 371 | */ |
| 372 | static DEFINE_PER_CPU(u64, discard_pa_seq); |
| 373 | static inline u64 ext4_get_discard_pa_seq_sum(void) |
| 374 | { |
| 375 | int __cpu; |
| 376 | u64 __seq = 0; |
| 377 | |
| 378 | for_each_possible_cpu(__cpu) |
| 379 | __seq += per_cpu(discard_pa_seq, __cpu); |
| 380 | return __seq; |
| 381 | } |
| 382 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 383 | static inline void *mb_correct_addr_and_bit(int *bit, void *addr) |
| 384 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 385 | #if BITS_PER_LONG == 64 |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 386 | *bit += ((unsigned long) addr & 7UL) << 3; |
| 387 | addr = (void *) ((unsigned long) addr & ~7UL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 388 | #elif BITS_PER_LONG == 32 |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 389 | *bit += ((unsigned long) addr & 3UL) << 3; |
| 390 | addr = (void *) ((unsigned long) addr & ~3UL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 391 | #else |
| 392 | #error "how many bits you are?!" |
| 393 | #endif |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 394 | return addr; |
| 395 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 396 | |
| 397 | static inline int mb_test_bit(int bit, void *addr) |
| 398 | { |
| 399 | /* |
| 400 | * ext4_test_bit on architecture like powerpc |
| 401 | * needs unsigned long aligned address |
| 402 | */ |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 403 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 404 | return ext4_test_bit(bit, addr); |
| 405 | } |
| 406 | |
| 407 | static inline void mb_set_bit(int bit, void *addr) |
| 408 | { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 409 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 410 | ext4_set_bit(bit, addr); |
| 411 | } |
| 412 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 413 | static inline void mb_clear_bit(int bit, void *addr) |
| 414 | { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 415 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 416 | ext4_clear_bit(bit, addr); |
| 417 | } |
| 418 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 419 | static inline int mb_test_and_clear_bit(int bit, void *addr) |
| 420 | { |
| 421 | addr = mb_correct_addr_and_bit(&bit, addr); |
| 422 | return ext4_test_and_clear_bit(bit, addr); |
| 423 | } |
| 424 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 425 | static inline int mb_find_next_zero_bit(void *addr, int max, int start) |
| 426 | { |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 427 | int fix = 0, ret, tmpmax; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 428 | addr = mb_correct_addr_and_bit(&fix, addr); |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 429 | tmpmax = max + fix; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 430 | start += fix; |
| 431 | |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 432 | ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix; |
| 433 | if (ret > max) |
| 434 | return max; |
| 435 | return ret; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static inline int mb_find_next_bit(void *addr, int max, int start) |
| 439 | { |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 440 | int fix = 0, ret, tmpmax; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 441 | addr = mb_correct_addr_and_bit(&fix, addr); |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 442 | tmpmax = max + fix; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 443 | start += fix; |
| 444 | |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 445 | ret = ext4_find_next_bit(addr, tmpmax, start) - fix; |
| 446 | if (ret > max) |
| 447 | return max; |
| 448 | return ret; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 449 | } |
| 450 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 451 | static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max) |
| 452 | { |
| 453 | char *bb; |
| 454 | |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 455 | BUG_ON(e4b->bd_bitmap == e4b->bd_buddy); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 456 | BUG_ON(max == NULL); |
| 457 | |
| 458 | if (order > e4b->bd_blkbits + 1) { |
| 459 | *max = 0; |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | /* at order 0 we see each particular block */ |
Coly Li | 84b775a | 2011-02-24 12:51:59 -0500 | [diff] [blame] | 464 | if (order == 0) { |
| 465 | *max = 1 << (e4b->bd_blkbits + 3); |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 466 | return e4b->bd_bitmap; |
Coly Li | 84b775a | 2011-02-24 12:51:59 -0500 | [diff] [blame] | 467 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 468 | |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 469 | 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] | 470 | *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order]; |
| 471 | |
| 472 | return bb; |
| 473 | } |
| 474 | |
| 475 | #ifdef DOUBLE_CHECK |
| 476 | static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b, |
| 477 | int first, int count) |
| 478 | { |
| 479 | int i; |
| 480 | struct super_block *sb = e4b->bd_sb; |
| 481 | |
| 482 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) |
| 483 | return; |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 484 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 485 | for (i = 0; i < count; i++) { |
| 486 | if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) { |
| 487 | ext4_fsblk_t blocknr; |
Akinobu Mita | 5661bd6 | 2010-03-03 23:53:39 -0500 | [diff] [blame] | 488 | |
| 489 | blocknr = ext4_group_first_block_no(sb, e4b->bd_group); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 490 | blocknr += EXT4_C2B(EXT4_SB(sb), first + i); |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 491 | ext4_grp_locked_error(sb, e4b->bd_group, |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 492 | inode ? inode->i_ino : 0, |
| 493 | blocknr, |
| 494 | "freeing block already freed " |
| 495 | "(bit %u)", |
| 496 | first + i); |
Wang Shilong | 736dedb | 2018-05-12 12:37:58 -0400 | [diff] [blame] | 497 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
| 498 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 499 | } |
| 500 | mb_clear_bit(first + i, e4b->bd_info->bb_bitmap); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count) |
| 505 | { |
| 506 | int i; |
| 507 | |
| 508 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) |
| 509 | return; |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 510 | 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] | 511 | for (i = 0; i < count; i++) { |
| 512 | BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap)); |
| 513 | mb_set_bit(first + i, e4b->bd_info->bb_bitmap); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) |
| 518 | { |
Ritesh Harjani | eb2b8eb | 2020-05-10 11:54:49 +0530 | [diff] [blame] | 519 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) |
| 520 | return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 521 | if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) { |
| 522 | unsigned char *b1, *b2; |
| 523 | int i; |
| 524 | b1 = (unsigned char *) e4b->bd_info->bb_bitmap; |
| 525 | b2 = (unsigned char *) bitmap; |
| 526 | for (i = 0; i < e4b->bd_sb->s_blocksize; i++) { |
| 527 | if (b1[i] != b2[i]) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 528 | ext4_msg(e4b->bd_sb, KERN_ERR, |
| 529 | "corruption in group %u " |
| 530 | "at byte %u(%u): %x in copy != %x " |
| 531 | "on disk/prealloc", |
| 532 | e4b->bd_group, i, i * 8, b1[i], b2[i]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 533 | BUG(); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 539 | static void mb_group_bb_bitmap_alloc(struct super_block *sb, |
| 540 | struct ext4_group_info *grp, ext4_group_t group) |
| 541 | { |
| 542 | struct buffer_head *bh; |
| 543 | |
| 544 | grp->bb_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS); |
Ritesh Harjani | eb2b8eb | 2020-05-10 11:54:49 +0530 | [diff] [blame] | 545 | if (!grp->bb_bitmap) |
| 546 | return; |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 547 | |
| 548 | bh = ext4_read_block_bitmap(sb, group); |
Ritesh Harjani | eb2b8eb | 2020-05-10 11:54:49 +0530 | [diff] [blame] | 549 | if (IS_ERR_OR_NULL(bh)) { |
| 550 | kfree(grp->bb_bitmap); |
| 551 | grp->bb_bitmap = NULL; |
| 552 | return; |
| 553 | } |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 554 | |
| 555 | memcpy(grp->bb_bitmap, bh->b_data, sb->s_blocksize); |
| 556 | put_bh(bh); |
| 557 | } |
| 558 | |
| 559 | static void mb_group_bb_bitmap_free(struct ext4_group_info *grp) |
| 560 | { |
| 561 | kfree(grp->bb_bitmap); |
| 562 | } |
| 563 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 564 | #else |
| 565 | static inline void mb_free_blocks_double(struct inode *inode, |
| 566 | struct ext4_buddy *e4b, int first, int count) |
| 567 | { |
| 568 | return; |
| 569 | } |
| 570 | static inline void mb_mark_used_double(struct ext4_buddy *e4b, |
| 571 | int first, int count) |
| 572 | { |
| 573 | return; |
| 574 | } |
| 575 | static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) |
| 576 | { |
| 577 | return; |
| 578 | } |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 579 | |
| 580 | static inline void mb_group_bb_bitmap_alloc(struct super_block *sb, |
| 581 | struct ext4_group_info *grp, ext4_group_t group) |
| 582 | { |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | static inline void mb_group_bb_bitmap_free(struct ext4_group_info *grp) |
| 587 | { |
| 588 | return; |
| 589 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 590 | #endif |
| 591 | |
| 592 | #ifdef AGGRESSIVE_CHECK |
| 593 | |
| 594 | #define MB_CHECK_ASSERT(assert) \ |
| 595 | do { \ |
| 596 | if (!(assert)) { \ |
| 597 | printk(KERN_EMERG \ |
| 598 | "Assertion failure in %s() at %s:%d: \"%s\"\n", \ |
| 599 | function, file, line, # assert); \ |
| 600 | BUG(); \ |
| 601 | } \ |
| 602 | } while (0) |
| 603 | |
| 604 | static int __mb_check_buddy(struct ext4_buddy *e4b, char *file, |
| 605 | const char *function, int line) |
| 606 | { |
| 607 | struct super_block *sb = e4b->bd_sb; |
| 608 | int order = e4b->bd_blkbits + 1; |
| 609 | int max; |
| 610 | int max2; |
| 611 | int i; |
| 612 | int j; |
| 613 | int k; |
| 614 | int count; |
| 615 | struct ext4_group_info *grp; |
| 616 | int fragments = 0; |
| 617 | int fstart; |
| 618 | struct list_head *cur; |
| 619 | void *buddy; |
| 620 | void *buddy2; |
| 621 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 622 | { |
| 623 | static int mb_check_counter; |
| 624 | if (mb_check_counter++ % 100 != 0) |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | while (order > 1) { |
| 629 | buddy = mb_find_buddy(e4b, order, &max); |
| 630 | MB_CHECK_ASSERT(buddy); |
| 631 | buddy2 = mb_find_buddy(e4b, order - 1, &max2); |
| 632 | MB_CHECK_ASSERT(buddy2); |
| 633 | MB_CHECK_ASSERT(buddy != buddy2); |
| 634 | MB_CHECK_ASSERT(max * 2 == max2); |
| 635 | |
| 636 | count = 0; |
| 637 | for (i = 0; i < max; i++) { |
| 638 | |
| 639 | if (mb_test_bit(i, buddy)) { |
| 640 | /* only single bit in buddy2 may be 1 */ |
| 641 | if (!mb_test_bit(i << 1, buddy2)) { |
| 642 | MB_CHECK_ASSERT( |
| 643 | mb_test_bit((i<<1)+1, buddy2)); |
| 644 | } else if (!mb_test_bit((i << 1) + 1, buddy2)) { |
| 645 | MB_CHECK_ASSERT( |
| 646 | mb_test_bit(i << 1, buddy2)); |
| 647 | } |
| 648 | continue; |
| 649 | } |
| 650 | |
Robin Dong | 0a10da7 | 2011-10-26 08:48:54 -0400 | [diff] [blame] | 651 | /* both bits in buddy2 must be 1 */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 652 | MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2)); |
| 653 | MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2)); |
| 654 | |
| 655 | for (j = 0; j < (1 << order); j++) { |
| 656 | k = (i * (1 << order)) + j; |
| 657 | MB_CHECK_ASSERT( |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 658 | !mb_test_bit(k, e4b->bd_bitmap)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 659 | } |
| 660 | count++; |
| 661 | } |
| 662 | MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count); |
| 663 | order--; |
| 664 | } |
| 665 | |
| 666 | fstart = -1; |
| 667 | buddy = mb_find_buddy(e4b, 0, &max); |
| 668 | for (i = 0; i < max; i++) { |
| 669 | if (!mb_test_bit(i, buddy)) { |
| 670 | MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free); |
| 671 | if (fstart == -1) { |
| 672 | fragments++; |
| 673 | fstart = i; |
| 674 | } |
| 675 | continue; |
| 676 | } |
| 677 | fstart = -1; |
| 678 | /* check used bits only */ |
| 679 | for (j = 0; j < e4b->bd_blkbits + 1; j++) { |
| 680 | buddy2 = mb_find_buddy(e4b, j, &max2); |
| 681 | k = i >> j; |
| 682 | MB_CHECK_ASSERT(k < max2); |
| 683 | MB_CHECK_ASSERT(mb_test_bit(k, buddy2)); |
| 684 | } |
| 685 | } |
| 686 | MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info)); |
| 687 | MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments); |
| 688 | |
| 689 | grp = ext4_get_group_info(sb, e4b->bd_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 690 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 691 | ext4_group_t groupnr; |
| 692 | struct ext4_prealloc_space *pa; |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 693 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 694 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 695 | MB_CHECK_ASSERT(groupnr == e4b->bd_group); |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 696 | for (i = 0; i < pa->pa_len; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 697 | MB_CHECK_ASSERT(mb_test_bit(k + i, buddy)); |
| 698 | } |
| 699 | return 0; |
| 700 | } |
| 701 | #undef MB_CHECK_ASSERT |
| 702 | #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \ |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 703 | __FILE__, __func__, __LINE__) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 704 | #else |
| 705 | #define mb_check_buddy(e4b) |
| 706 | #endif |
| 707 | |
Coly Li | 7c78605 | 2011-02-24 13:24:25 -0500 | [diff] [blame] | 708 | /* |
| 709 | * Divide blocks started from @first with length @len into |
| 710 | * smaller chunks with power of 2 blocks. |
| 711 | * Clear the bits in bitmap which the blocks of the chunk(s) covered, |
| 712 | * then increase bb_counters[] for corresponded chunk size. |
| 713 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 714 | static void ext4_mb_mark_free_simple(struct super_block *sb, |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 715 | void *buddy, ext4_grpblk_t first, ext4_grpblk_t len, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 716 | struct ext4_group_info *grp) |
| 717 | { |
| 718 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 719 | ext4_grpblk_t min; |
| 720 | ext4_grpblk_t max; |
| 721 | ext4_grpblk_t chunk; |
Chandan Rajendra | 69e43e8 | 2016-11-14 21:04:37 -0500 | [diff] [blame] | 722 | unsigned int border; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 723 | |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 724 | BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 725 | |
| 726 | border = 2 << sb->s_blocksize_bits; |
| 727 | |
| 728 | while (len > 0) { |
| 729 | /* find how many blocks can be covered since this position */ |
| 730 | max = ffs(first | border) - 1; |
| 731 | |
| 732 | /* find how many blocks of power 2 we need to mark */ |
| 733 | min = fls(len) - 1; |
| 734 | |
| 735 | if (max < min) |
| 736 | min = max; |
| 737 | chunk = 1 << min; |
| 738 | |
| 739 | /* mark multiblock chunks only */ |
| 740 | grp->bb_counters[min]++; |
| 741 | if (min > 0) |
| 742 | mb_clear_bit(first >> min, |
| 743 | buddy + sbi->s_mb_offsets[min]); |
| 744 | |
| 745 | len -= chunk; |
| 746 | first += chunk; |
| 747 | } |
| 748 | } |
| 749 | |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 750 | /* |
| 751 | * Cache the order of the largest free extent we have available in this block |
| 752 | * group. |
| 753 | */ |
| 754 | static void |
| 755 | mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp) |
| 756 | { |
| 757 | int i; |
| 758 | int bits; |
| 759 | |
| 760 | grp->bb_largest_free_order = -1; /* uninit */ |
| 761 | |
| 762 | bits = sb->s_blocksize_bits + 1; |
| 763 | for (i = bits; i >= 0; i--) { |
| 764 | if (grp->bb_counters[i] > 0) { |
| 765 | grp->bb_largest_free_order = i; |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 771 | static noinline_for_stack |
| 772 | void ext4_mb_generate_buddy(struct super_block *sb, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 773 | void *buddy, void *bitmap, ext4_group_t group) |
| 774 | { |
| 775 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
Namjae Jeon | e43bb4e | 2014-06-26 10:11:53 -0400 | [diff] [blame] | 776 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 777 | ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb); |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 778 | ext4_grpblk_t i = 0; |
| 779 | ext4_grpblk_t first; |
| 780 | ext4_grpblk_t len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 781 | unsigned free = 0; |
| 782 | unsigned fragments = 0; |
| 783 | unsigned long long period = get_cycles(); |
| 784 | |
| 785 | /* initialize buddy from bitmap which is aggregation |
| 786 | * of on-disk bitmap and preallocations */ |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 787 | i = mb_find_next_zero_bit(bitmap, max, 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 788 | grp->bb_first_free = i; |
| 789 | while (i < max) { |
| 790 | fragments++; |
| 791 | first = i; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 792 | i = mb_find_next_bit(bitmap, max, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 793 | len = i - first; |
| 794 | free += len; |
| 795 | if (len > 1) |
| 796 | ext4_mb_mark_free_simple(sb, buddy, first, len, grp); |
| 797 | else |
| 798 | grp->bb_counters[0]++; |
| 799 | if (i < max) |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 800 | i = mb_find_next_zero_bit(bitmap, max, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 801 | } |
| 802 | grp->bb_fragments = fragments; |
| 803 | |
| 804 | if (free != grp->bb_free) { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 805 | ext4_grp_locked_error(sb, group, 0, 0, |
Theodore Ts'o | 94d4c06 | 2014-07-05 19:15:50 -0400 | [diff] [blame] | 806 | "block bitmap and bg descriptor " |
| 807 | "inconsistent: %u vs %u free clusters", |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 808 | free, grp->bb_free); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 809 | /* |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 810 | * If we intend to continue, we consider group descriptor |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 811 | * corrupt and update bb_free using bitmap value |
| 812 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 813 | grp->bb_free = free; |
Wang Shilong | db79e6d | 2018-05-12 11:39:40 -0400 | [diff] [blame] | 814 | ext4_mark_group_bitmap_corrupted(sb, group, |
| 815 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 816 | } |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 817 | mb_set_largest_free_order(sb, grp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 818 | |
| 819 | clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state)); |
| 820 | |
| 821 | period = get_cycles() - period; |
Jun Piao | 49598e0 | 2018-01-11 13:17:49 -0500 | [diff] [blame] | 822 | spin_lock(&sbi->s_bal_lock); |
| 823 | sbi->s_mb_buddies_generated++; |
| 824 | sbi->s_mb_generation_time += period; |
| 825 | spin_unlock(&sbi->s_bal_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 826 | } |
| 827 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 828 | static void mb_regenerate_buddy(struct ext4_buddy *e4b) |
| 829 | { |
| 830 | int count; |
| 831 | int order = 1; |
| 832 | void *buddy; |
| 833 | |
| 834 | while ((buddy = mb_find_buddy(e4b, order++, &count))) { |
| 835 | ext4_set_bits(buddy, 0, count); |
| 836 | } |
| 837 | e4b->bd_info->bb_fragments = 0; |
| 838 | memset(e4b->bd_info->bb_counters, 0, |
| 839 | sizeof(*e4b->bd_info->bb_counters) * |
| 840 | (e4b->bd_sb->s_blocksize_bits + 2)); |
| 841 | |
| 842 | ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy, |
| 843 | e4b->bd_bitmap, e4b->bd_group); |
| 844 | } |
| 845 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 846 | /* The buddy information is attached the buddy cache inode |
| 847 | * for convenience. The information regarding each group |
| 848 | * is loaded via ext4_mb_load_buddy. The information involve |
| 849 | * block bitmap and buddy information. The information are |
| 850 | * stored in the inode as |
| 851 | * |
| 852 | * { page } |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 853 | * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 854 | * |
| 855 | * |
| 856 | * one block each for bitmap and buddy information. |
| 857 | * 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] | 858 | * contain blocks_per_page (PAGE_SIZE / blocksize) blocks. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 859 | * So it can have information regarding groups_per_page which |
| 860 | * is blocks_per_page/2 |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 861 | * |
| 862 | * Locking note: This routine takes the block group lock of all groups |
| 863 | * for this page; do not hold this lock when calling this routine! |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 864 | */ |
| 865 | |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 866 | 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] | 867 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 868 | ext4_group_t ngroups; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 869 | int blocksize; |
| 870 | int blocks_per_page; |
| 871 | int groups_per_page; |
| 872 | int err = 0; |
| 873 | int i; |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 874 | ext4_group_t first_group, group; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 875 | int first_block; |
| 876 | struct super_block *sb; |
| 877 | struct buffer_head *bhs; |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 878 | struct buffer_head **bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 879 | struct inode *inode; |
| 880 | char *data; |
| 881 | char *bitmap; |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 882 | struct ext4_group_info *grinfo; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 883 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 884 | inode = page->mapping->host; |
| 885 | sb = inode->i_sb; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 886 | ngroups = ext4_get_groups_count(sb); |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 887 | blocksize = i_blocksize(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 888 | blocks_per_page = PAGE_SIZE / blocksize; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 889 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 890 | mb_debug(sb, "init page %lu\n", page->index); |
| 891 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 892 | groups_per_page = blocks_per_page >> 1; |
| 893 | if (groups_per_page == 0) |
| 894 | groups_per_page = 1; |
| 895 | |
| 896 | /* allocate buffer_heads to read bitmaps */ |
| 897 | if (groups_per_page > 1) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 898 | i = sizeof(struct buffer_head *) * groups_per_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 899 | bh = kzalloc(i, gfp); |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 900 | if (bh == NULL) { |
| 901 | err = -ENOMEM; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 902 | goto out; |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 903 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 904 | } else |
| 905 | bh = &bhs; |
| 906 | |
| 907 | first_group = page->index * blocks_per_page / 2; |
| 908 | |
| 909 | /* read all groups the page covers into the cache */ |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 910 | for (i = 0, group = first_group; i < groups_per_page; i++, group++) { |
| 911 | if (group >= ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 912 | break; |
| 913 | |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 914 | grinfo = ext4_get_group_info(sb, group); |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 915 | /* |
| 916 | * If page is uptodate then we came here after online resize |
| 917 | * which added some new uninitialized group info structs, so |
| 918 | * we must skip all initialized uptodate buddies on the page, |
| 919 | * which may be currently in use by an allocating task. |
| 920 | */ |
| 921 | if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) { |
| 922 | bh[i] = NULL; |
| 923 | continue; |
| 924 | } |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 925 | bh[i] = ext4_read_block_bitmap_nowait(sb, group, false); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 926 | if (IS_ERR(bh[i])) { |
| 927 | err = PTR_ERR(bh[i]); |
| 928 | bh[i] = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 929 | goto out; |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 930 | } |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 931 | mb_debug(sb, "read bitmap for group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* wait for I/O completion */ |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 935 | 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] | 936 | int err2; |
| 937 | |
| 938 | if (!bh[i]) |
| 939 | continue; |
| 940 | err2 = ext4_wait_block_bitmap(sb, group, bh[i]); |
| 941 | if (!err) |
| 942 | err = err2; |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 943 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 944 | |
| 945 | first_block = page->index * blocks_per_page; |
| 946 | for (i = 0; i < blocks_per_page; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 947 | group = (first_block + i) >> 1; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 948 | if (group >= ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 949 | break; |
| 950 | |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 951 | if (!bh[group - first_group]) |
| 952 | /* skip initialized uptodate buddy */ |
| 953 | continue; |
| 954 | |
Lukas Czerner | bbdc322 | 2015-06-08 11:38:37 -0400 | [diff] [blame] | 955 | if (!buffer_verified(bh[group - first_group])) |
| 956 | /* Skip faulty bitmaps */ |
| 957 | continue; |
| 958 | err = 0; |
| 959 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 960 | /* |
| 961 | * data carry information regarding this |
| 962 | * particular group in the format specified |
| 963 | * above |
| 964 | * |
| 965 | */ |
| 966 | data = page_address(page) + (i * blocksize); |
| 967 | bitmap = bh[group - first_group]->b_data; |
| 968 | |
| 969 | /* |
| 970 | * We place the buddy block and bitmap block |
| 971 | * close together |
| 972 | */ |
| 973 | if ((first_block + i) & 1) { |
| 974 | /* this is block of buddy */ |
| 975 | BUG_ON(incore == NULL); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 976 | mb_debug(sb, "put buddy for group %u in page %lu/%x\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 977 | group, page->index, i * blocksize); |
Theodore Ts'o | f307333 | 2010-05-17 03:00:00 -0400 | [diff] [blame] | 978 | trace_ext4_mb_buddy_bitmap_load(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 979 | grinfo = ext4_get_group_info(sb, group); |
| 980 | grinfo->bb_fragments = 0; |
| 981 | memset(grinfo->bb_counters, 0, |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame] | 982 | sizeof(*grinfo->bb_counters) * |
| 983 | (sb->s_blocksize_bits+2)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 984 | /* |
| 985 | * incore got set to the group block bitmap below |
| 986 | */ |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 987 | ext4_lock_group(sb, group); |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 988 | /* init the buddy */ |
| 989 | memset(data, 0xff, blocksize); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 990 | ext4_mb_generate_buddy(sb, data, incore, group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 991 | ext4_unlock_group(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 992 | incore = NULL; |
| 993 | } else { |
| 994 | /* this is block of bitmap */ |
| 995 | BUG_ON(incore != NULL); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 996 | mb_debug(sb, "put bitmap for group %u in page %lu/%x\n", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 997 | group, page->index, i * blocksize); |
Theodore Ts'o | f307333 | 2010-05-17 03:00:00 -0400 | [diff] [blame] | 998 | trace_ext4_mb_bitmap_load(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 999 | |
| 1000 | /* see comments in ext4_mb_put_pa() */ |
| 1001 | ext4_lock_group(sb, group); |
| 1002 | memcpy(data, bitmap, blocksize); |
| 1003 | |
| 1004 | /* mark all preallocated blks used in in-core bitmap */ |
| 1005 | ext4_mb_generate_from_pa(sb, data, group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 1006 | ext4_mb_generate_from_freelist(sb, data, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1007 | ext4_unlock_group(sb, group); |
| 1008 | |
| 1009 | /* set incore so that the buddy information can be |
| 1010 | * generated using this |
| 1011 | */ |
| 1012 | incore = data; |
| 1013 | } |
| 1014 | } |
| 1015 | SetPageUptodate(page); |
| 1016 | |
| 1017 | out: |
| 1018 | if (bh) { |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 1019 | for (i = 0; i < groups_per_page; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1020 | brelse(bh[i]); |
| 1021 | if (bh != &bhs) |
| 1022 | kfree(bh); |
| 1023 | } |
| 1024 | return err; |
| 1025 | } |
| 1026 | |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1027 | /* |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1028 | * Lock the buddy and bitmap pages. This make sure other parallel init_group |
| 1029 | * on the same buddy page doesn't happen whild holding the buddy page lock. |
| 1030 | * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap |
| 1031 | * 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] | 1032 | */ |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1033 | static int ext4_mb_get_buddy_page_lock(struct super_block *sb, |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1034 | ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp) |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1035 | { |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1036 | struct inode *inode = EXT4_SB(sb)->s_buddy_cache; |
| 1037 | int block, pnum, poff; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1038 | int blocks_per_page; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1039 | struct page *page; |
| 1040 | |
| 1041 | e4b->bd_buddy_page = NULL; |
| 1042 | e4b->bd_bitmap_page = NULL; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1043 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1044 | blocks_per_page = PAGE_SIZE / sb->s_blocksize; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1045 | /* |
| 1046 | * the buddy cache inode stores the block bitmap |
| 1047 | * and buddy information in consecutive blocks. |
| 1048 | * So for each group we need two blocks. |
| 1049 | */ |
| 1050 | block = group * 2; |
| 1051 | pnum = block / blocks_per_page; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1052 | poff = block % blocks_per_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1053 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1054 | if (!page) |
Younger Liu | c57ab39 | 2014-04-10 23:03:43 -0400 | [diff] [blame] | 1055 | return -ENOMEM; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1056 | BUG_ON(page->mapping != inode->i_mapping); |
| 1057 | e4b->bd_bitmap_page = page; |
| 1058 | e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize); |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1059 | |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1060 | if (blocks_per_page >= 2) { |
| 1061 | /* buddy and bitmap are on the same page */ |
| 1062 | return 0; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1063 | } |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1064 | |
| 1065 | block++; |
| 1066 | pnum = block / blocks_per_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1067 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1068 | if (!page) |
Younger Liu | c57ab39 | 2014-04-10 23:03:43 -0400 | [diff] [blame] | 1069 | return -ENOMEM; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1070 | BUG_ON(page->mapping != inode->i_mapping); |
| 1071 | e4b->bd_buddy_page = page; |
| 1072 | return 0; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1073 | } |
| 1074 | |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1075 | static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b) |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1076 | { |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1077 | if (e4b->bd_bitmap_page) { |
| 1078 | unlock_page(e4b->bd_bitmap_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1079 | put_page(e4b->bd_bitmap_page); |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1080 | } |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1081 | if (e4b->bd_buddy_page) { |
| 1082 | unlock_page(e4b->bd_buddy_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1083 | put_page(e4b->bd_buddy_page); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1084 | } |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1088 | * Locking note: This routine calls ext4_mb_init_cache(), which takes the |
| 1089 | * block group lock of all groups for this page; do not hold the BG lock when |
| 1090 | * calling this routine! |
| 1091 | */ |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1092 | static noinline_for_stack |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1093 | 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] | 1094 | { |
| 1095 | |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1096 | struct ext4_group_info *this_grp; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1097 | struct ext4_buddy e4b; |
| 1098 | struct page *page; |
| 1099 | int ret = 0; |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1100 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 1101 | might_sleep(); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 1102 | mb_debug(sb, "init group %u\n", group); |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1103 | this_grp = ext4_get_group_info(sb, group); |
| 1104 | /* |
Aneesh Kumar K.V | 08c3a81 | 2009-09-09 23:50:17 -0400 | [diff] [blame] | 1105 | * This ensures that we don't reinit the buddy cache |
| 1106 | * page which map to the group from which we are already |
| 1107 | * allocating. If we are looking at the buddy cache we would |
| 1108 | * have taken a reference using ext4_mb_load_buddy and that |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1109 | * would have pinned buddy page to page cache. |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1110 | * The call to ext4_mb_get_buddy_page_lock will mark the |
| 1111 | * page accessed. |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1112 | */ |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1113 | ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1114 | if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) { |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1115 | /* |
| 1116 | * somebody initialized the group |
| 1117 | * return without doing anything |
| 1118 | */ |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1119 | goto err; |
| 1120 | } |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1121 | |
| 1122 | page = e4b.bd_bitmap_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1123 | ret = ext4_mb_init_cache(page, NULL, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1124 | if (ret) |
| 1125 | goto err; |
| 1126 | if (!PageUptodate(page)) { |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1127 | ret = -EIO; |
| 1128 | goto err; |
| 1129 | } |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1130 | |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1131 | if (e4b.bd_buddy_page == NULL) { |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1132 | /* |
| 1133 | * If both the bitmap and buddy are in |
| 1134 | * the same page we don't need to force |
| 1135 | * init the buddy |
| 1136 | */ |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1137 | ret = 0; |
| 1138 | goto err; |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1139 | } |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1140 | /* init buddy cache */ |
| 1141 | page = e4b.bd_buddy_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1142 | ret = ext4_mb_init_cache(page, e4b.bd_bitmap, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1143 | if (ret) |
| 1144 | goto err; |
| 1145 | if (!PageUptodate(page)) { |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1146 | ret = -EIO; |
| 1147 | goto err; |
| 1148 | } |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1149 | err: |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1150 | ext4_mb_put_buddy_page_lock(&e4b); |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1151 | return ret; |
| 1152 | } |
| 1153 | |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1154 | /* |
| 1155 | * Locking note: This routine calls ext4_mb_init_cache(), which takes the |
| 1156 | * block group lock of all groups for this page; do not hold the BG lock when |
| 1157 | * calling this routine! |
| 1158 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1159 | static noinline_for_stack int |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1160 | ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group, |
| 1161 | struct ext4_buddy *e4b, gfp_t gfp) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1162 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1163 | int blocks_per_page; |
| 1164 | int block; |
| 1165 | int pnum; |
| 1166 | int poff; |
| 1167 | struct page *page; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1168 | int ret; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1169 | struct ext4_group_info *grp; |
| 1170 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 1171 | struct inode *inode = sbi->s_buddy_cache; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1172 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 1173 | might_sleep(); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 1174 | mb_debug(sb, "load group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1175 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1176 | blocks_per_page = PAGE_SIZE / sb->s_blocksize; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1177 | grp = ext4_get_group_info(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1178 | |
| 1179 | e4b->bd_blkbits = sb->s_blocksize_bits; |
Tao Ma | 529da70 | 2011-07-23 16:07:26 -0400 | [diff] [blame] | 1180 | e4b->bd_info = grp; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1181 | e4b->bd_sb = sb; |
| 1182 | e4b->bd_group = group; |
| 1183 | e4b->bd_buddy_page = NULL; |
| 1184 | e4b->bd_bitmap_page = NULL; |
| 1185 | |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1186 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1187 | /* |
| 1188 | * we need full data about the group |
| 1189 | * to make a good selection |
| 1190 | */ |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1191 | ret = ext4_mb_init_group(sb, group, gfp); |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1192 | if (ret) |
| 1193 | return ret; |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1194 | } |
| 1195 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1196 | /* |
| 1197 | * the buddy cache inode stores the block bitmap |
| 1198 | * and buddy information in consecutive blocks. |
| 1199 | * So for each group we need two blocks. |
| 1200 | */ |
| 1201 | block = group * 2; |
| 1202 | pnum = block / blocks_per_page; |
| 1203 | poff = block % blocks_per_page; |
| 1204 | |
| 1205 | /* we could use find_or_create_page(), but it locks page |
| 1206 | * what we'd like to avoid in fast path ... */ |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1207 | page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1208 | if (page == NULL || !PageUptodate(page)) { |
| 1209 | if (page) |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1210 | /* |
| 1211 | * drop the page reference and try |
| 1212 | * to get the page with lock. If we |
| 1213 | * are not uptodate that implies |
| 1214 | * somebody just created the page but |
| 1215 | * is yet to initialize the same. So |
| 1216 | * wait for it to initialize. |
| 1217 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1218 | put_page(page); |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1219 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1220 | if (page) { |
| 1221 | BUG_ON(page->mapping != inode->i_mapping); |
| 1222 | if (!PageUptodate(page)) { |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1223 | ret = ext4_mb_init_cache(page, NULL, gfp); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1224 | if (ret) { |
| 1225 | unlock_page(page); |
| 1226 | goto err; |
| 1227 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1228 | mb_cmp_bitmaps(e4b, page_address(page) + |
| 1229 | (poff * sb->s_blocksize)); |
| 1230 | } |
| 1231 | unlock_page(page); |
| 1232 | } |
| 1233 | } |
Younger Liu | c57ab39 | 2014-04-10 23:03:43 -0400 | [diff] [blame] | 1234 | if (page == NULL) { |
| 1235 | ret = -ENOMEM; |
| 1236 | goto err; |
| 1237 | } |
| 1238 | if (!PageUptodate(page)) { |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1239 | ret = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1240 | goto err; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1241 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1242 | |
| 1243 | /* Pages marked accessed already */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1244 | e4b->bd_bitmap_page = page; |
| 1245 | e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1246 | |
| 1247 | block++; |
| 1248 | pnum = block / blocks_per_page; |
| 1249 | poff = block % blocks_per_page; |
| 1250 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1251 | page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1252 | if (page == NULL || !PageUptodate(page)) { |
| 1253 | if (page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1254 | put_page(page); |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1255 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1256 | if (page) { |
| 1257 | BUG_ON(page->mapping != inode->i_mapping); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1258 | if (!PageUptodate(page)) { |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1259 | ret = ext4_mb_init_cache(page, e4b->bd_bitmap, |
| 1260 | gfp); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1261 | if (ret) { |
| 1262 | unlock_page(page); |
| 1263 | goto err; |
| 1264 | } |
| 1265 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1266 | unlock_page(page); |
| 1267 | } |
| 1268 | } |
Younger Liu | c57ab39 | 2014-04-10 23:03:43 -0400 | [diff] [blame] | 1269 | if (page == NULL) { |
| 1270 | ret = -ENOMEM; |
| 1271 | goto err; |
| 1272 | } |
| 1273 | if (!PageUptodate(page)) { |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1274 | ret = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1275 | goto err; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1276 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1277 | |
| 1278 | /* Pages marked accessed already */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1279 | e4b->bd_buddy_page = page; |
| 1280 | e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1281 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1282 | return 0; |
| 1283 | |
| 1284 | err: |
Yang Ruirui | 26626f11 | 2011-04-16 19:17:48 -0400 | [diff] [blame] | 1285 | if (page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1286 | put_page(page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1287 | if (e4b->bd_bitmap_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1288 | put_page(e4b->bd_bitmap_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1289 | if (e4b->bd_buddy_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1290 | put_page(e4b->bd_buddy_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1291 | e4b->bd_buddy = NULL; |
| 1292 | e4b->bd_bitmap = NULL; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1293 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1294 | } |
| 1295 | |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1296 | static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group, |
| 1297 | struct ext4_buddy *e4b) |
| 1298 | { |
| 1299 | return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS); |
| 1300 | } |
| 1301 | |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 1302 | static void ext4_mb_unload_buddy(struct ext4_buddy *e4b) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1303 | { |
| 1304 | if (e4b->bd_bitmap_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1305 | put_page(e4b->bd_bitmap_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1306 | if (e4b->bd_buddy_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1307 | put_page(e4b->bd_buddy_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | static int mb_find_order_for_block(struct ext4_buddy *e4b, int block) |
| 1312 | { |
| 1313 | int order = 1; |
Nicolai Stange | b5cb316 | 2016-05-05 17:38:03 -0400 | [diff] [blame] | 1314 | int bb_incr = 1 << (e4b->bd_blkbits - 1); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1315 | void *bb; |
| 1316 | |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1317 | BUG_ON(e4b->bd_bitmap == e4b->bd_buddy); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1318 | BUG_ON(block >= (1 << (e4b->bd_blkbits + 3))); |
| 1319 | |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1320 | bb = e4b->bd_buddy; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1321 | while (order <= e4b->bd_blkbits + 1) { |
| 1322 | block = block >> 1; |
| 1323 | if (!mb_test_bit(block, bb)) { |
| 1324 | /* this block is part of buddy of order 'order' */ |
| 1325 | return order; |
| 1326 | } |
Nicolai Stange | b5cb316 | 2016-05-05 17:38:03 -0400 | [diff] [blame] | 1327 | bb += bb_incr; |
| 1328 | bb_incr >>= 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1329 | order++; |
| 1330 | } |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1334 | static void mb_clear_bits(void *bm, int cur, int len) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1335 | { |
| 1336 | __u32 *addr; |
| 1337 | |
| 1338 | len = cur + len; |
| 1339 | while (cur < len) { |
| 1340 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1341 | /* fast path: clear whole word at once */ |
| 1342 | addr = bm + (cur >> 3); |
| 1343 | *addr = 0; |
| 1344 | cur += 32; |
| 1345 | continue; |
| 1346 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1347 | mb_clear_bit(cur, bm); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1348 | cur++; |
| 1349 | } |
| 1350 | } |
| 1351 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1352 | /* clear bits in given range |
| 1353 | * will return first found zero bit if any, -1 otherwise |
| 1354 | */ |
| 1355 | static int mb_test_and_clear_bits(void *bm, int cur, int len) |
| 1356 | { |
| 1357 | __u32 *addr; |
| 1358 | int zero_bit = -1; |
| 1359 | |
| 1360 | len = cur + len; |
| 1361 | while (cur < len) { |
| 1362 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1363 | /* fast path: clear whole word at once */ |
| 1364 | addr = bm + (cur >> 3); |
| 1365 | if (*addr != (__u32)(-1) && zero_bit == -1) |
| 1366 | zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0); |
| 1367 | *addr = 0; |
| 1368 | cur += 32; |
| 1369 | continue; |
| 1370 | } |
| 1371 | if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1) |
| 1372 | zero_bit = cur; |
| 1373 | cur++; |
| 1374 | } |
| 1375 | |
| 1376 | return zero_bit; |
| 1377 | } |
| 1378 | |
Yongqiang Yang | c3e94d1 | 2011-07-26 22:05:53 -0400 | [diff] [blame] | 1379 | void ext4_set_bits(void *bm, int cur, int len) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1380 | { |
| 1381 | __u32 *addr; |
| 1382 | |
| 1383 | len = cur + len; |
| 1384 | while (cur < len) { |
| 1385 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1386 | /* fast path: set whole word at once */ |
| 1387 | addr = bm + (cur >> 3); |
| 1388 | *addr = 0xffffffff; |
| 1389 | cur += 32; |
| 1390 | continue; |
| 1391 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1392 | mb_set_bit(cur, bm); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1393 | cur++; |
| 1394 | } |
| 1395 | } |
| 1396 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1397 | /* |
| 1398 | * _________________________________________________________________ */ |
| 1399 | |
| 1400 | 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] | 1401 | { |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1402 | if (mb_test_bit(*bit + side, bitmap)) { |
| 1403 | mb_clear_bit(*bit, bitmap); |
| 1404 | (*bit) -= side; |
| 1405 | return 1; |
| 1406 | } |
| 1407 | else { |
| 1408 | (*bit) += side; |
| 1409 | mb_set_bit(*bit, bitmap); |
| 1410 | return -1; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last) |
| 1415 | { |
| 1416 | int max; |
| 1417 | int order = 1; |
| 1418 | void *buddy = mb_find_buddy(e4b, order, &max); |
| 1419 | |
| 1420 | while (buddy) { |
| 1421 | void *buddy2; |
| 1422 | |
| 1423 | /* Bits in range [first; last] are known to be set since |
| 1424 | * corresponding blocks were allocated. Bits in range |
| 1425 | * (first; last) will stay set because they form buddies on |
| 1426 | * upper layer. We just deal with borders if they don't |
| 1427 | * align with upper layer and then go up. |
| 1428 | * Releasing entire group is all about clearing |
| 1429 | * single bit of highest order buddy. |
| 1430 | */ |
| 1431 | |
| 1432 | /* Example: |
| 1433 | * --------------------------------- |
| 1434 | * | 1 | 1 | 1 | 1 | |
| 1435 | * --------------------------------- |
| 1436 | * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | |
| 1437 | * --------------------------------- |
| 1438 | * 0 1 2 3 4 5 6 7 |
| 1439 | * \_____________________/ |
| 1440 | * |
| 1441 | * Neither [1] nor [6] is aligned to above layer. |
| 1442 | * Left neighbour [0] is free, so mark it busy, |
| 1443 | * decrease bb_counters and extend range to |
| 1444 | * [0; 6] |
| 1445 | * Right neighbour [7] is busy. It can't be coaleasced with [6], so |
| 1446 | * mark [6] free, increase bb_counters and shrink range to |
| 1447 | * [0; 5]. |
| 1448 | * Then shift range to [0; 2], go up and do the same. |
| 1449 | */ |
| 1450 | |
| 1451 | |
| 1452 | if (first & 1) |
| 1453 | e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1); |
| 1454 | if (!(last & 1)) |
| 1455 | e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1); |
| 1456 | if (first > last) |
| 1457 | break; |
| 1458 | order++; |
| 1459 | |
| 1460 | if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) { |
| 1461 | mb_clear_bits(buddy, first, last - first + 1); |
| 1462 | e4b->bd_info->bb_counters[order - 1] += last - first + 1; |
| 1463 | break; |
| 1464 | } |
| 1465 | first >>= 1; |
| 1466 | last >>= 1; |
| 1467 | buddy = buddy2; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b, |
| 1472 | int first, int count) |
| 1473 | { |
| 1474 | int left_is_free = 0; |
| 1475 | int right_is_free = 0; |
| 1476 | int block; |
| 1477 | int last = first + count - 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1478 | struct super_block *sb = e4b->bd_sb; |
| 1479 | |
Theodore Ts'o | c99d1e6 | 2014-08-23 17:47:28 -0400 | [diff] [blame] | 1480 | if (WARN_ON(count == 0)) |
| 1481 | return; |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1482 | BUG_ON(last >= (sb->s_blocksize << 3)); |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1483 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 1484 | /* Don't bother if the block group is corrupt. */ |
| 1485 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) |
| 1486 | return; |
| 1487 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1488 | mb_check_buddy(e4b); |
| 1489 | mb_free_blocks_double(inode, e4b, first, count); |
| 1490 | |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 1491 | this_cpu_inc(discard_pa_seq); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1492 | e4b->bd_info->bb_free += count; |
| 1493 | if (first < e4b->bd_info->bb_first_free) |
| 1494 | e4b->bd_info->bb_first_free = first; |
| 1495 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1496 | /* access memory sequentially: check left neighbour, |
| 1497 | * clear range and then check right neighbour |
| 1498 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1499 | if (first != 0) |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1500 | left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap); |
| 1501 | block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count); |
| 1502 | if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0]) |
| 1503 | right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap); |
| 1504 | |
| 1505 | if (unlikely(block != -1)) { |
Namjae Jeon | e43bb4e | 2014-06-26 10:11:53 -0400 | [diff] [blame] | 1506 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1507 | ext4_fsblk_t blocknr; |
| 1508 | |
| 1509 | blocknr = ext4_group_first_block_no(sb, e4b->bd_group); |
Jun Piao | 49598e0 | 2018-01-11 13:17:49 -0500 | [diff] [blame] | 1510 | blocknr += EXT4_C2B(sbi, block); |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1511 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 1512 | inode ? inode->i_ino : 0, |
| 1513 | blocknr, |
| 1514 | "freeing already freed block " |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 1515 | "(bit %u); block bitmap corrupt.", |
| 1516 | block); |
Wang Shilong | db79e6d | 2018-05-12 11:39:40 -0400 | [diff] [blame] | 1517 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
| 1518 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1519 | mb_regenerate_buddy(e4b); |
| 1520 | goto done; |
| 1521 | } |
| 1522 | |
| 1523 | /* let's maintain fragments counter */ |
| 1524 | if (left_is_free && right_is_free) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1525 | e4b->bd_info->bb_fragments--; |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1526 | else if (!left_is_free && !right_is_free) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1527 | e4b->bd_info->bb_fragments++; |
| 1528 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1529 | /* buddy[0] == bd_bitmap is a special case, so handle |
| 1530 | * it right away and let mb_buddy_mark_free stay free of |
| 1531 | * zero order checks. |
| 1532 | * Check if neighbours are to be coaleasced, |
| 1533 | * adjust bitmap bb_counters and borders appropriately. |
| 1534 | */ |
| 1535 | if (first & 1) { |
| 1536 | first += !left_is_free; |
| 1537 | e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1538 | } |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1539 | if (!(last & 1)) { |
| 1540 | last -= !right_is_free; |
| 1541 | e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1; |
| 1542 | } |
| 1543 | |
| 1544 | if (first <= last) |
| 1545 | mb_buddy_mark_free(e4b, first >> 1, last >> 1); |
| 1546 | |
| 1547 | done: |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1548 | mb_set_largest_free_order(sb, e4b->bd_info); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1549 | mb_check_buddy(e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1550 | } |
| 1551 | |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1552 | static int mb_find_extent(struct ext4_buddy *e4b, int block, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1553 | int needed, struct ext4_free_extent *ex) |
| 1554 | { |
| 1555 | int next = block; |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1556 | int max, order; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1557 | void *buddy; |
| 1558 | |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1559 | 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] | 1560 | BUG_ON(ex == NULL); |
| 1561 | |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1562 | buddy = mb_find_buddy(e4b, 0, &max); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1563 | BUG_ON(buddy == NULL); |
| 1564 | BUG_ON(block >= max); |
| 1565 | if (mb_test_bit(block, buddy)) { |
| 1566 | ex->fe_len = 0; |
| 1567 | ex->fe_start = 0; |
| 1568 | ex->fe_group = 0; |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1572 | /* find actual order */ |
| 1573 | order = mb_find_order_for_block(e4b, block); |
| 1574 | block = block >> order; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1575 | |
| 1576 | ex->fe_len = 1 << order; |
| 1577 | ex->fe_start = block << order; |
| 1578 | ex->fe_group = e4b->bd_group; |
| 1579 | |
| 1580 | /* calc difference from given start */ |
| 1581 | next = next - ex->fe_start; |
| 1582 | ex->fe_len -= next; |
| 1583 | ex->fe_start += next; |
| 1584 | |
| 1585 | while (needed > ex->fe_len && |
Alan Cox | d8ec0c3 | 2012-11-08 12:19:58 -0500 | [diff] [blame] | 1586 | mb_find_buddy(e4b, order, &max)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1587 | |
| 1588 | if (block + 1 >= max) |
| 1589 | break; |
| 1590 | |
| 1591 | next = (block + 1) * (1 << order); |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1592 | if (mb_test_bit(next, e4b->bd_bitmap)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1593 | break; |
| 1594 | |
Robin Dong | b051d8d | 2011-10-26 05:30:30 -0400 | [diff] [blame] | 1595 | order = mb_find_order_for_block(e4b, next); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1596 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1597 | block = next >> order; |
| 1598 | ex->fe_len += 1 << order; |
| 1599 | } |
| 1600 | |
Jan Kara | 31562b9 | 2019-04-06 18:33:06 -0400 | [diff] [blame] | 1601 | if (ex->fe_start + ex->fe_len > EXT4_CLUSTERS_PER_GROUP(e4b->bd_sb)) { |
Theodore Ts'o | 43c7322 | 2017-01-22 19:35:52 -0500 | [diff] [blame] | 1602 | /* Should never happen! (but apparently sometimes does?!?) */ |
| 1603 | WARN_ON(1); |
| 1604 | ext4_error(e4b->bd_sb, "corruption or bug in mb_find_extent " |
| 1605 | "block=%d, order=%d needed=%d ex=%u/%d/%d@%u", |
| 1606 | block, order, needed, ex->fe_group, ex->fe_start, |
| 1607 | ex->fe_len, ex->fe_logical); |
| 1608 | ex->fe_len = 0; |
| 1609 | ex->fe_start = 0; |
| 1610 | ex->fe_group = 0; |
| 1611 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1612 | return ex->fe_len; |
| 1613 | } |
| 1614 | |
| 1615 | static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) |
| 1616 | { |
| 1617 | int ord; |
| 1618 | int mlen = 0; |
| 1619 | int max = 0; |
| 1620 | int cur; |
| 1621 | int start = ex->fe_start; |
| 1622 | int len = ex->fe_len; |
| 1623 | unsigned ret = 0; |
| 1624 | int len0 = len; |
| 1625 | void *buddy; |
| 1626 | |
| 1627 | BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3)); |
| 1628 | BUG_ON(e4b->bd_group != ex->fe_group); |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1629 | 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] | 1630 | mb_check_buddy(e4b); |
| 1631 | mb_mark_used_double(e4b, start, len); |
| 1632 | |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 1633 | this_cpu_inc(discard_pa_seq); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1634 | e4b->bd_info->bb_free -= len; |
| 1635 | if (e4b->bd_info->bb_first_free == start) |
| 1636 | e4b->bd_info->bb_first_free += len; |
| 1637 | |
| 1638 | /* let's maintain fragments counter */ |
| 1639 | if (start != 0) |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1640 | mlen = !mb_test_bit(start - 1, e4b->bd_bitmap); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1641 | 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] | 1642 | max = !mb_test_bit(start + len, e4b->bd_bitmap); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1643 | if (mlen && max) |
| 1644 | e4b->bd_info->bb_fragments++; |
| 1645 | else if (!mlen && !max) |
| 1646 | e4b->bd_info->bb_fragments--; |
| 1647 | |
| 1648 | /* let's maintain buddy itself */ |
| 1649 | while (len) { |
| 1650 | ord = mb_find_order_for_block(e4b, start); |
| 1651 | |
| 1652 | if (((start >> ord) << ord) == start && len >= (1 << ord)) { |
| 1653 | /* the whole chunk may be allocated at once! */ |
| 1654 | mlen = 1 << ord; |
| 1655 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1656 | BUG_ON((start >> ord) >= max); |
| 1657 | mb_set_bit(start >> ord, buddy); |
| 1658 | e4b->bd_info->bb_counters[ord]--; |
| 1659 | start += mlen; |
| 1660 | len -= mlen; |
| 1661 | BUG_ON(len < 0); |
| 1662 | continue; |
| 1663 | } |
| 1664 | |
| 1665 | /* store for history */ |
| 1666 | if (ret == 0) |
| 1667 | ret = len | (ord << 16); |
| 1668 | |
| 1669 | /* we have to split large buddy */ |
| 1670 | BUG_ON(ord <= 0); |
| 1671 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1672 | mb_set_bit(start >> ord, buddy); |
| 1673 | e4b->bd_info->bb_counters[ord]--; |
| 1674 | |
| 1675 | ord--; |
| 1676 | cur = (start >> ord) & ~1U; |
| 1677 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1678 | mb_clear_bit(cur, buddy); |
| 1679 | mb_clear_bit(cur + 1, buddy); |
| 1680 | e4b->bd_info->bb_counters[ord]++; |
| 1681 | e4b->bd_info->bb_counters[ord]++; |
| 1682 | } |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1683 | mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1684 | |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1685 | ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1686 | mb_check_buddy(e4b); |
| 1687 | |
| 1688 | return ret; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Must be called under group lock! |
| 1693 | */ |
| 1694 | static void ext4_mb_use_best_found(struct ext4_allocation_context *ac, |
| 1695 | struct ext4_buddy *e4b) |
| 1696 | { |
| 1697 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 1698 | int ret; |
| 1699 | |
| 1700 | BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group); |
| 1701 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
| 1702 | |
| 1703 | ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len); |
| 1704 | ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical; |
| 1705 | ret = mb_mark_used(e4b, &ac->ac_b_ex); |
| 1706 | |
| 1707 | /* preallocation can change ac_b_ex, thus we store actually |
| 1708 | * allocated blocks for history */ |
| 1709 | ac->ac_f_ex = ac->ac_b_ex; |
| 1710 | |
| 1711 | ac->ac_status = AC_STATUS_FOUND; |
| 1712 | ac->ac_tail = ret & 0xffff; |
| 1713 | ac->ac_buddy = ret >> 16; |
| 1714 | |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 1715 | /* |
| 1716 | * take the page reference. We want the page to be pinned |
| 1717 | * so that we don't get a ext4_mb_init_cache_call for this |
| 1718 | * group until we update the bitmap. That would mean we |
| 1719 | * double allocate blocks. The reference is dropped |
| 1720 | * in ext4_mb_release_context |
| 1721 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1722 | ac->ac_bitmap_page = e4b->bd_bitmap_page; |
| 1723 | get_page(ac->ac_bitmap_page); |
| 1724 | ac->ac_buddy_page = e4b->bd_buddy_page; |
| 1725 | get_page(ac->ac_buddy_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1726 | /* store last allocated for subsequent stream allocation */ |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 1727 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1728 | spin_lock(&sbi->s_md_lock); |
| 1729 | sbi->s_mb_last_group = ac->ac_f_ex.fe_group; |
| 1730 | sbi->s_mb_last_start = ac->ac_f_ex.fe_start; |
| 1731 | spin_unlock(&sbi->s_md_lock); |
| 1732 | } |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 1733 | /* |
| 1734 | * As we've just preallocated more space than |
| 1735 | * user requested originally, we store allocated |
| 1736 | * space in a special descriptor. |
| 1737 | */ |
| 1738 | if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) |
| 1739 | ext4_mb_new_preallocation(ac); |
| 1740 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1741 | } |
| 1742 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1743 | static void ext4_mb_check_limits(struct ext4_allocation_context *ac, |
| 1744 | struct ext4_buddy *e4b, |
| 1745 | int finish_group) |
| 1746 | { |
| 1747 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 1748 | struct ext4_free_extent *bex = &ac->ac_b_ex; |
| 1749 | struct ext4_free_extent *gex = &ac->ac_g_ex; |
| 1750 | struct ext4_free_extent ex; |
| 1751 | int max; |
| 1752 | |
Aneesh Kumar K.V | 032115f | 2009-01-05 21:34:30 -0500 | [diff] [blame] | 1753 | if (ac->ac_status == AC_STATUS_FOUND) |
| 1754 | return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1755 | /* |
| 1756 | * We don't want to scan for a whole year |
| 1757 | */ |
| 1758 | if (ac->ac_found > sbi->s_mb_max_to_scan && |
| 1759 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 1760 | ac->ac_status = AC_STATUS_BREAK; |
| 1761 | return; |
| 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | * Haven't found good chunk so far, let's continue |
| 1766 | */ |
| 1767 | if (bex->fe_len < gex->fe_len) |
| 1768 | return; |
| 1769 | |
| 1770 | if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan) |
| 1771 | && bex->fe_group == e4b->bd_group) { |
| 1772 | /* recheck chunk's availability - we don't know |
| 1773 | * when it was found (within this lock-unlock |
| 1774 | * period or not) */ |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1775 | max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1776 | if (max >= gex->fe_len) { |
| 1777 | ext4_mb_use_best_found(ac, e4b); |
| 1778 | return; |
| 1779 | } |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * The routine checks whether found extent is good enough. If it is, |
| 1785 | * then the extent gets marked used and flag is set to the context |
| 1786 | * to stop scanning. Otherwise, the extent is compared with the |
| 1787 | * previous found extent and if new one is better, then it's stored |
| 1788 | * in the context. Later, the best found extent will be used, if |
| 1789 | * mballoc can't find good enough extent. |
| 1790 | * |
| 1791 | * FIXME: real allocation policy is to be designed yet! |
| 1792 | */ |
| 1793 | static void ext4_mb_measure_extent(struct ext4_allocation_context *ac, |
| 1794 | struct ext4_free_extent *ex, |
| 1795 | struct ext4_buddy *e4b) |
| 1796 | { |
| 1797 | struct ext4_free_extent *bex = &ac->ac_b_ex; |
| 1798 | struct ext4_free_extent *gex = &ac->ac_g_ex; |
| 1799 | |
| 1800 | BUG_ON(ex->fe_len <= 0); |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 1801 | BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb)); |
| 1802 | BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1803 | BUG_ON(ac->ac_status != AC_STATUS_CONTINUE); |
| 1804 | |
| 1805 | ac->ac_found++; |
| 1806 | |
| 1807 | /* |
| 1808 | * The special case - take what you catch first |
| 1809 | */ |
| 1810 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 1811 | *bex = *ex; |
| 1812 | ext4_mb_use_best_found(ac, e4b); |
| 1813 | return; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * Let's check whether the chuck is good enough |
| 1818 | */ |
| 1819 | if (ex->fe_len == gex->fe_len) { |
| 1820 | *bex = *ex; |
| 1821 | ext4_mb_use_best_found(ac, e4b); |
| 1822 | return; |
| 1823 | } |
| 1824 | |
| 1825 | /* |
| 1826 | * If this is first found extent, just store it in the context |
| 1827 | */ |
| 1828 | if (bex->fe_len == 0) { |
| 1829 | *bex = *ex; |
| 1830 | return; |
| 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * If new found extent is better, store it in the context |
| 1835 | */ |
| 1836 | if (bex->fe_len < gex->fe_len) { |
| 1837 | /* if the request isn't satisfied, any found extent |
| 1838 | * larger than previous best one is better */ |
| 1839 | if (ex->fe_len > bex->fe_len) |
| 1840 | *bex = *ex; |
| 1841 | } else if (ex->fe_len > gex->fe_len) { |
| 1842 | /* if the request is satisfied, then we try to find |
| 1843 | * an extent that still satisfy the request, but is |
| 1844 | * smaller than previous one */ |
| 1845 | if (ex->fe_len < bex->fe_len) |
| 1846 | *bex = *ex; |
| 1847 | } |
| 1848 | |
| 1849 | ext4_mb_check_limits(ac, e4b, 0); |
| 1850 | } |
| 1851 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1852 | static noinline_for_stack |
| 1853 | int ext4_mb_try_best_found(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1854 | struct ext4_buddy *e4b) |
| 1855 | { |
| 1856 | struct ext4_free_extent ex = ac->ac_b_ex; |
| 1857 | ext4_group_t group = ex.fe_group; |
| 1858 | int max; |
| 1859 | int err; |
| 1860 | |
| 1861 | BUG_ON(ex.fe_len <= 0); |
| 1862 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); |
| 1863 | if (err) |
| 1864 | return err; |
| 1865 | |
| 1866 | ext4_lock_group(ac->ac_sb, group); |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1867 | max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1868 | |
| 1869 | if (max > 0) { |
| 1870 | ac->ac_b_ex = ex; |
| 1871 | ext4_mb_use_best_found(ac, e4b); |
| 1872 | } |
| 1873 | |
| 1874 | ext4_unlock_group(ac->ac_sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 1875 | ext4_mb_unload_buddy(e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1876 | |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1880 | static noinline_for_stack |
| 1881 | int ext4_mb_find_by_goal(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1882 | struct ext4_buddy *e4b) |
| 1883 | { |
| 1884 | ext4_group_t group = ac->ac_g_ex.fe_group; |
| 1885 | int max; |
| 1886 | int err; |
| 1887 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Yongqiang Yang | 838cd0c | 2012-09-23 23:10:51 -0400 | [diff] [blame] | 1888 | 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] | 1889 | struct ext4_free_extent ex; |
| 1890 | |
| 1891 | if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL)) |
| 1892 | return 0; |
Yongqiang Yang | 838cd0c | 2012-09-23 23:10:51 -0400 | [diff] [blame] | 1893 | if (grp->bb_free == 0) |
| 1894 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1895 | |
| 1896 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); |
| 1897 | if (err) |
| 1898 | return err; |
| 1899 | |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 1900 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) { |
| 1901 | ext4_mb_unload_buddy(e4b); |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1905 | ext4_lock_group(ac->ac_sb, group); |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1906 | max = mb_find_extent(e4b, ac->ac_g_ex.fe_start, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1907 | ac->ac_g_ex.fe_len, &ex); |
Theodore Ts'o | ab0c00f | 2014-02-20 00:36:41 -0500 | [diff] [blame] | 1908 | ex.fe_logical = 0xDEADFA11; /* debug value */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1909 | |
| 1910 | if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) { |
| 1911 | ext4_fsblk_t start; |
| 1912 | |
Akinobu Mita | 5661bd6 | 2010-03-03 23:53:39 -0500 | [diff] [blame] | 1913 | start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) + |
| 1914 | ex.fe_start; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1915 | /* use do_div to get remainder (would be 64-bit modulo) */ |
| 1916 | if (do_div(start, sbi->s_stripe) == 0) { |
| 1917 | ac->ac_found++; |
| 1918 | ac->ac_b_ex = ex; |
| 1919 | ext4_mb_use_best_found(ac, e4b); |
| 1920 | } |
| 1921 | } else if (max >= ac->ac_g_ex.fe_len) { |
| 1922 | BUG_ON(ex.fe_len <= 0); |
| 1923 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); |
| 1924 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); |
| 1925 | ac->ac_found++; |
| 1926 | ac->ac_b_ex = ex; |
| 1927 | ext4_mb_use_best_found(ac, e4b); |
| 1928 | } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) { |
| 1929 | /* Sometimes, caller may want to merge even small |
| 1930 | * number of blocks to an existing extent */ |
| 1931 | BUG_ON(ex.fe_len <= 0); |
| 1932 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); |
| 1933 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); |
| 1934 | ac->ac_found++; |
| 1935 | ac->ac_b_ex = ex; |
| 1936 | ext4_mb_use_best_found(ac, e4b); |
| 1937 | } |
| 1938 | ext4_unlock_group(ac->ac_sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 1939 | ext4_mb_unload_buddy(e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1940 | |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | * The routine scans buddy structures (not bitmap!) from given order |
| 1946 | * to max order and tries to find big enough chunk to satisfy the req |
| 1947 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1948 | static noinline_for_stack |
| 1949 | void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1950 | struct ext4_buddy *e4b) |
| 1951 | { |
| 1952 | struct super_block *sb = ac->ac_sb; |
| 1953 | struct ext4_group_info *grp = e4b->bd_info; |
| 1954 | void *buddy; |
| 1955 | int i; |
| 1956 | int k; |
| 1957 | int max; |
| 1958 | |
| 1959 | BUG_ON(ac->ac_2order <= 0); |
| 1960 | for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) { |
| 1961 | if (grp->bb_counters[i] == 0) |
| 1962 | continue; |
| 1963 | |
| 1964 | buddy = mb_find_buddy(e4b, i, &max); |
| 1965 | BUG_ON(buddy == NULL); |
| 1966 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 1967 | k = mb_find_next_zero_bit(buddy, max, 0); |
Dmitry Monakhov | eb57608 | 2020-03-10 15:01:56 +0000 | [diff] [blame] | 1968 | if (k >= max) { |
| 1969 | ext4_grp_locked_error(ac->ac_sb, e4b->bd_group, 0, 0, |
| 1970 | "%d free clusters of order %d. But found 0", |
| 1971 | grp->bb_counters[i], i); |
| 1972 | ext4_mark_group_bitmap_corrupted(ac->ac_sb, |
| 1973 | e4b->bd_group, |
| 1974 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
| 1975 | break; |
| 1976 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1977 | ac->ac_found++; |
| 1978 | |
| 1979 | ac->ac_b_ex.fe_len = 1 << i; |
| 1980 | ac->ac_b_ex.fe_start = k << i; |
| 1981 | ac->ac_b_ex.fe_group = e4b->bd_group; |
| 1982 | |
| 1983 | ext4_mb_use_best_found(ac, e4b); |
| 1984 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 1985 | BUG_ON(ac->ac_f_ex.fe_len != ac->ac_g_ex.fe_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1986 | |
| 1987 | if (EXT4_SB(sb)->s_mb_stats) |
| 1988 | atomic_inc(&EXT4_SB(sb)->s_bal_2orders); |
| 1989 | |
| 1990 | break; |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | /* |
| 1995 | * The routine scans the group and measures all found extents. |
| 1996 | * In order to optimize scanning, caller must pass number of |
| 1997 | * free blocks in the group, so the routine can know upper limit. |
| 1998 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1999 | static noinline_for_stack |
| 2000 | void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2001 | struct ext4_buddy *e4b) |
| 2002 | { |
| 2003 | struct super_block *sb = ac->ac_sb; |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 2004 | void *bitmap = e4b->bd_bitmap; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2005 | struct ext4_free_extent ex; |
| 2006 | int i; |
| 2007 | int free; |
| 2008 | |
| 2009 | free = e4b->bd_info->bb_free; |
Theodore Ts'o | 907ea52 | 2020-04-13 23:33:05 -0400 | [diff] [blame] | 2010 | if (WARN_ON(free <= 0)) |
| 2011 | return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2012 | |
| 2013 | i = e4b->bd_info->bb_first_free; |
| 2014 | |
| 2015 | while (free && ac->ac_status == AC_STATUS_CONTINUE) { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 2016 | i = mb_find_next_zero_bit(bitmap, |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 2017 | EXT4_CLUSTERS_PER_GROUP(sb), i); |
| 2018 | if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2019 | /* |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 2020 | * IF we have corrupt bitmap, we won't find any |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2021 | * free blocks even though group info says we |
| 2022 | * we have free blocks |
| 2023 | */ |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 2024 | ext4_grp_locked_error(sb, e4b->bd_group, 0, 0, |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 2025 | "%d free clusters as per " |
Theodore Ts'o | fde4d95 | 2009-01-05 22:17:35 -0500 | [diff] [blame] | 2026 | "group info. But bitmap says 0", |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2027 | free); |
Wang Shilong | 736dedb | 2018-05-12 12:37:58 -0400 | [diff] [blame] | 2028 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
| 2029 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2030 | break; |
| 2031 | } |
| 2032 | |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 2033 | mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex); |
Theodore Ts'o | 907ea52 | 2020-04-13 23:33:05 -0400 | [diff] [blame] | 2034 | if (WARN_ON(ex.fe_len <= 0)) |
| 2035 | break; |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2036 | if (free < ex.fe_len) { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 2037 | ext4_grp_locked_error(sb, e4b->bd_group, 0, 0, |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 2038 | "%d free clusters as per " |
Theodore Ts'o | fde4d95 | 2009-01-05 22:17:35 -0500 | [diff] [blame] | 2039 | "group info. But got %d blocks", |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2040 | free, ex.fe_len); |
Wang Shilong | 736dedb | 2018-05-12 12:37:58 -0400 | [diff] [blame] | 2041 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
| 2042 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 2043 | /* |
| 2044 | * The number of free blocks differs. This mostly |
| 2045 | * indicate that the bitmap is corrupt. So exit |
| 2046 | * without claiming the space. |
| 2047 | */ |
| 2048 | break; |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2049 | } |
Theodore Ts'o | ab0c00f | 2014-02-20 00:36:41 -0500 | [diff] [blame] | 2050 | ex.fe_logical = 0xDEADC0DE; /* debug value */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2051 | ext4_mb_measure_extent(ac, &ex, e4b); |
| 2052 | |
| 2053 | i += ex.fe_len; |
| 2054 | free -= ex.fe_len; |
| 2055 | } |
| 2056 | |
| 2057 | ext4_mb_check_limits(ac, e4b, 1); |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * This is a special case for storages like raid5 |
Eric Sandeen | 506bf2d | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 2062 | * we try to find stripe-aligned chunks for stripe-size-multiple requests |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2063 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 2064 | static noinline_for_stack |
| 2065 | void ext4_mb_scan_aligned(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2066 | struct ext4_buddy *e4b) |
| 2067 | { |
| 2068 | struct super_block *sb = ac->ac_sb; |
| 2069 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 2070 | void *bitmap = e4b->bd_bitmap; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2071 | struct ext4_free_extent ex; |
| 2072 | ext4_fsblk_t first_group_block; |
| 2073 | ext4_fsblk_t a; |
| 2074 | ext4_grpblk_t i; |
| 2075 | int max; |
| 2076 | |
| 2077 | BUG_ON(sbi->s_stripe == 0); |
| 2078 | |
| 2079 | /* find first stripe-aligned block in group */ |
Akinobu Mita | 5661bd6 | 2010-03-03 23:53:39 -0500 | [diff] [blame] | 2080 | first_group_block = ext4_group_first_block_no(sb, e4b->bd_group); |
| 2081 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2082 | a = first_group_block + sbi->s_stripe - 1; |
| 2083 | do_div(a, sbi->s_stripe); |
| 2084 | i = (a * sbi->s_stripe) - first_group_block; |
| 2085 | |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 2086 | while (i < EXT4_CLUSTERS_PER_GROUP(sb)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2087 | if (!mb_test_bit(i, bitmap)) { |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 2088 | max = mb_find_extent(e4b, i, sbi->s_stripe, &ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2089 | if (max >= sbi->s_stripe) { |
| 2090 | ac->ac_found++; |
Theodore Ts'o | ab0c00f | 2014-02-20 00:36:41 -0500 | [diff] [blame] | 2091 | ex.fe_logical = 0xDEADF00D; /* debug value */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2092 | ac->ac_b_ex = ex; |
| 2093 | ext4_mb_use_best_found(ac, e4b); |
| 2094 | break; |
| 2095 | } |
| 2096 | } |
| 2097 | i += sbi->s_stripe; |
| 2098 | } |
| 2099 | } |
| 2100 | |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2101 | /* |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2102 | * This is also called BEFORE we load the buddy bitmap. |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2103 | * Returns either 1 or 0 indicating that the group is either suitable |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2104 | * for the allocation or not. |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2105 | */ |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2106 | static bool ext4_mb_good_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2107 | ext4_group_t group, int cr) |
| 2108 | { |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2109 | ext4_grpblk_t free, fragments; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 2110 | int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2111 | struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); |
| 2112 | |
| 2113 | BUG_ON(cr < 0 || cr >= 4); |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 2114 | |
brookxu | dddcd2f | 2020-08-07 22:01:39 +0800 | [diff] [blame] | 2115 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2116 | return false; |
Theodore Ts'o | 01fc48e | 2012-08-17 09:46:17 -0400 | [diff] [blame] | 2117 | |
brookxu | dddcd2f | 2020-08-07 22:01:39 +0800 | [diff] [blame] | 2118 | free = grp->bb_free; |
| 2119 | if (free == 0) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2120 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2121 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2122 | fragments = grp->bb_fragments; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2123 | if (fragments == 0) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2124 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2125 | |
| 2126 | switch (cr) { |
| 2127 | case 0: |
| 2128 | BUG_ON(ac->ac_2order == 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2129 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 2130 | /* Avoid using the first bg of a flexgroup for data files */ |
| 2131 | if ((ac->ac_flags & EXT4_MB_HINT_DATA) && |
| 2132 | (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) && |
| 2133 | ((group % flex_size) == 0)) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2134 | return false; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 2135 | |
brookxu | dddcd2f | 2020-08-07 22:01:39 +0800 | [diff] [blame] | 2136 | if (free < ac->ac_g_ex.fe_len) |
| 2137 | return false; |
| 2138 | |
| 2139 | if (ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2140 | return true; |
Theodore Ts'o | 40ae348 | 2013-02-04 15:08:40 -0500 | [diff] [blame] | 2141 | |
| 2142 | if (grp->bb_largest_free_order < ac->ac_2order) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2143 | return false; |
Theodore Ts'o | 40ae348 | 2013-02-04 15:08:40 -0500 | [diff] [blame] | 2144 | |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2145 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2146 | case 1: |
| 2147 | if ((free / fragments) >= ac->ac_g_ex.fe_len) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2148 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2149 | break; |
| 2150 | case 2: |
| 2151 | if (free >= ac->ac_g_ex.fe_len) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2152 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2153 | break; |
| 2154 | case 3: |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2155 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2156 | default: |
| 2157 | BUG(); |
| 2158 | } |
| 2159 | |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2160 | return false; |
| 2161 | } |
| 2162 | |
| 2163 | /* |
| 2164 | * This could return negative error code if something goes wrong |
| 2165 | * during ext4_mb_init_group(). This should not be called with |
| 2166 | * ext4_lock_group() held. |
| 2167 | */ |
| 2168 | static int ext4_mb_good_group_nolock(struct ext4_allocation_context *ac, |
| 2169 | ext4_group_t group, int cr) |
| 2170 | { |
| 2171 | struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2172 | struct super_block *sb = ac->ac_sb; |
Alex Zhuravlev | c1d2c7d | 2020-06-19 22:08:56 -0400 | [diff] [blame] | 2173 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2174 | bool should_lock = ac->ac_flags & EXT4_MB_STRICT_CHECK; |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2175 | ext4_grpblk_t free; |
| 2176 | int ret = 0; |
| 2177 | |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2178 | if (should_lock) |
| 2179 | ext4_lock_group(sb, group); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2180 | free = grp->bb_free; |
| 2181 | if (free == 0) |
| 2182 | goto out; |
| 2183 | if (cr <= 2 && free < ac->ac_g_ex.fe_len) |
| 2184 | goto out; |
| 2185 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) |
| 2186 | goto out; |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2187 | if (should_lock) |
| 2188 | ext4_unlock_group(sb, group); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2189 | |
| 2190 | /* We only do this if the grp has never been initialized */ |
| 2191 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { |
Alex Zhuravlev | c1d2c7d | 2020-06-19 22:08:56 -0400 | [diff] [blame] | 2192 | struct ext4_group_desc *gdp = |
| 2193 | ext4_get_group_desc(sb, group, NULL); |
| 2194 | int ret; |
| 2195 | |
| 2196 | /* cr=0/1 is a very optimistic search to find large |
| 2197 | * good chunks almost for free. If buddy data is not |
| 2198 | * ready, then this optimization makes no sense. But |
| 2199 | * we never skip the first block group in a flex_bg, |
| 2200 | * since this gets used for metadata block allocation, |
| 2201 | * and we want to make sure we locate metadata blocks |
| 2202 | * in the first block group in the flex_bg if possible. |
| 2203 | */ |
| 2204 | if (cr < 2 && |
| 2205 | (!sbi->s_log_groups_per_flex || |
| 2206 | ((group & ((1 << sbi->s_log_groups_per_flex) - 1)) != 0)) && |
| 2207 | !(ext4_has_group_desc_csum(sb) && |
| 2208 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) |
| 2209 | return 0; |
| 2210 | ret = ext4_mb_init_group(sb, group, GFP_NOFS); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2211 | if (ret) |
| 2212 | return ret; |
| 2213 | } |
| 2214 | |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2215 | if (should_lock) |
| 2216 | ext4_lock_group(sb, group); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2217 | ret = ext4_mb_good_group(ac, group, cr); |
| 2218 | out: |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2219 | if (should_lock) |
| 2220 | ext4_unlock_group(sb, group); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2221 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2222 | } |
| 2223 | |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2224 | /* |
| 2225 | * Start prefetching @nr block bitmaps starting at @group. |
| 2226 | * Return the next group which needs to be prefetched. |
| 2227 | */ |
Theodore Ts'o | 3d392b2 | 2020-07-17 00:14:40 -0400 | [diff] [blame] | 2228 | ext4_group_t ext4_mb_prefetch(struct super_block *sb, ext4_group_t group, |
| 2229 | unsigned int nr, int *cnt) |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2230 | { |
| 2231 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
| 2232 | struct buffer_head *bh; |
| 2233 | struct blk_plug plug; |
| 2234 | |
| 2235 | blk_start_plug(&plug); |
| 2236 | while (nr-- > 0) { |
| 2237 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, |
| 2238 | NULL); |
| 2239 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 2240 | |
| 2241 | /* |
| 2242 | * Prefetch block groups with free blocks; but don't |
| 2243 | * bother if it is marked uninitialized on disk, since |
| 2244 | * it won't require I/O to read. Also only try to |
| 2245 | * prefetch once, so we avoid getblk() call, which can |
| 2246 | * be expensive. |
| 2247 | */ |
| 2248 | if (!EXT4_MB_GRP_TEST_AND_SET_READ(grp) && |
| 2249 | EXT4_MB_GRP_NEED_INIT(grp) && |
| 2250 | ext4_free_group_clusters(sb, gdp) > 0 && |
| 2251 | !(ext4_has_group_desc_csum(sb) && |
| 2252 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) { |
| 2253 | bh = ext4_read_block_bitmap_nowait(sb, group, true); |
| 2254 | if (bh && !IS_ERR(bh)) { |
| 2255 | if (!buffer_uptodate(bh) && cnt) |
| 2256 | (*cnt)++; |
| 2257 | brelse(bh); |
| 2258 | } |
| 2259 | } |
| 2260 | if (++group >= ngroups) |
| 2261 | group = 0; |
| 2262 | } |
| 2263 | blk_finish_plug(&plug); |
| 2264 | return group; |
| 2265 | } |
| 2266 | |
| 2267 | /* |
| 2268 | * Prefetching reads the block bitmap into the buffer cache; but we |
| 2269 | * need to make sure that the buddy bitmap in the page cache has been |
| 2270 | * initialized. Note that ext4_mb_init_group() will block if the I/O |
| 2271 | * is not yet completed, or indeed if it was not initiated by |
| 2272 | * ext4_mb_prefetch did not start the I/O. |
| 2273 | * |
| 2274 | * TODO: We should actually kick off the buddy bitmap setup in a work |
| 2275 | * queue when the buffer I/O is completed, so that we don't block |
| 2276 | * waiting for the block allocation bitmap read to finish when |
| 2277 | * ext4_mb_prefetch_fini is called from ext4_mb_regular_allocator(). |
| 2278 | */ |
Theodore Ts'o | 3d392b2 | 2020-07-17 00:14:40 -0400 | [diff] [blame] | 2279 | void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group, |
| 2280 | unsigned int nr) |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2281 | { |
| 2282 | while (nr-- > 0) { |
| 2283 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, |
| 2284 | NULL); |
| 2285 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 2286 | |
| 2287 | if (!group) |
| 2288 | group = ext4_get_groups_count(sb); |
| 2289 | group--; |
| 2290 | grp = ext4_get_group_info(sb, group); |
| 2291 | |
| 2292 | if (EXT4_MB_GRP_NEED_INIT(grp) && |
| 2293 | ext4_free_group_clusters(sb, gdp) > 0 && |
| 2294 | !(ext4_has_group_desc_csum(sb) && |
| 2295 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) { |
| 2296 | if (ext4_mb_init_group(sb, group, GFP_NOFS)) |
| 2297 | break; |
| 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2302 | static noinline_for_stack int |
| 2303 | ext4_mb_regular_allocator(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2304 | { |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2305 | ext4_group_t prefetch_grp = 0, ngroups, group, i; |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 2306 | int cr = -1; |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2307 | int err = 0, first_err = 0; |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2308 | unsigned int nr = 0, prefetch_ios = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2309 | struct ext4_sb_info *sbi; |
| 2310 | struct super_block *sb; |
| 2311 | struct ext4_buddy e4b; |
brookxu | 66d5e02 | 2020-08-17 15:36:06 +0800 | [diff] [blame] | 2312 | int lost; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2313 | |
| 2314 | sb = ac->ac_sb; |
| 2315 | sbi = EXT4_SB(sb); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2316 | ngroups = ext4_get_groups_count(sb); |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2317 | /* non-extent files are limited to low blocks/groups */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 2318 | if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2319 | ngroups = sbi->s_blockfile_groups; |
| 2320 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2321 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
| 2322 | |
| 2323 | /* first, try the goal */ |
| 2324 | err = ext4_mb_find_by_goal(ac, &e4b); |
| 2325 | if (err || ac->ac_status == AC_STATUS_FOUND) |
| 2326 | goto out; |
| 2327 | |
| 2328 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 2329 | goto out; |
| 2330 | |
| 2331 | /* |
brookxu | e9a3cd4 | 2020-08-07 22:01:23 +0800 | [diff] [blame] | 2332 | * ac->ac_2order is set only if the fe_len is a power of 2 |
| 2333 | * if ac->ac_2order is set we also set criteria to 0 so that we |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2334 | * try exact allocation using buddy. |
| 2335 | */ |
| 2336 | i = fls(ac->ac_g_ex.fe_len); |
| 2337 | ac->ac_2order = 0; |
| 2338 | /* |
| 2339 | * We search using buddy data only if the order of the request |
| 2340 | * is greater than equal to the sbi_s_mb_order2_reqs |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 2341 | * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req |
Jan Kara | d9b22cf | 2017-02-10 00:50:56 -0500 | [diff] [blame] | 2342 | * We also support searching for power-of-two requests only for |
| 2343 | * requests upto maximum buddy size we have constructed. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2344 | */ |
Jan Kara | d9b22cf | 2017-02-10 00:50:56 -0500 | [diff] [blame] | 2345 | 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] | 2346 | /* |
| 2347 | * This should tell if fe_len is exactly power of 2 |
| 2348 | */ |
| 2349 | if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0) |
Jeremy Cline | 1a5d5e5 | 2018-08-02 00:03:40 -0400 | [diff] [blame] | 2350 | ac->ac_2order = array_index_nospec(i - 1, |
| 2351 | sb->s_blocksize_bits + 2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2352 | } |
| 2353 | |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 2354 | /* if stream allocation is enabled, use global goal */ |
| 2355 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2356 | /* TBD: may be hot point */ |
| 2357 | spin_lock(&sbi->s_md_lock); |
| 2358 | ac->ac_g_ex.fe_group = sbi->s_mb_last_group; |
| 2359 | ac->ac_g_ex.fe_start = sbi->s_mb_last_start; |
| 2360 | spin_unlock(&sbi->s_md_lock); |
| 2361 | } |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 2362 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2363 | /* Let's just scan groups to find more-less suitable blocks */ |
| 2364 | cr = ac->ac_2order ? 0 : 1; |
| 2365 | /* |
| 2366 | * cr == 0 try to get exact allocation, |
| 2367 | * cr == 3 try to get anything |
| 2368 | */ |
| 2369 | repeat: |
| 2370 | for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) { |
| 2371 | ac->ac_criteria = cr; |
Aneesh Kumar K.V | ed8f9c7 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2372 | /* |
| 2373 | * searching for the right group start |
| 2374 | * from the goal value specified |
| 2375 | */ |
| 2376 | group = ac->ac_g_ex.fe_group; |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2377 | prefetch_grp = group; |
Aneesh Kumar K.V | ed8f9c7 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2378 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2379 | for (i = 0; i < ngroups; group++, i++) { |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2380 | int ret = 0; |
Theodore Ts'o | 2ed5724 | 2013-06-12 11:43:02 -0400 | [diff] [blame] | 2381 | cond_resched(); |
Lachlan McIlroy | e615573 | 2013-05-05 23:10:00 -0400 | [diff] [blame] | 2382 | /* |
| 2383 | * Artificially restricted ngroups for non-extent |
| 2384 | * files makes group > ngroups possible on first loop. |
| 2385 | */ |
| 2386 | if (group >= ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2387 | group = 0; |
| 2388 | |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2389 | /* |
| 2390 | * Batch reads of the block allocation bitmaps |
| 2391 | * to get multiple READs in flight; limit |
| 2392 | * prefetching at cr=0/1, otherwise mballoc can |
| 2393 | * spend a lot of time loading imperfect groups |
| 2394 | */ |
| 2395 | if ((prefetch_grp == group) && |
| 2396 | (cr > 1 || |
| 2397 | prefetch_ios < sbi->s_mb_prefetch_limit)) { |
| 2398 | unsigned int curr_ios = prefetch_ios; |
| 2399 | |
| 2400 | nr = sbi->s_mb_prefetch; |
| 2401 | if (ext4_has_feature_flex_bg(sb)) { |
| 2402 | nr = (group / sbi->s_mb_prefetch) * |
| 2403 | sbi->s_mb_prefetch; |
| 2404 | nr = nr + sbi->s_mb_prefetch - group; |
| 2405 | } |
| 2406 | prefetch_grp = ext4_mb_prefetch(sb, group, |
| 2407 | nr, &prefetch_ios); |
| 2408 | if (prefetch_ios == curr_ios) |
| 2409 | nr = 0; |
| 2410 | } |
| 2411 | |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 2412 | /* This now checks without needing the buddy page */ |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2413 | ret = ext4_mb_good_group_nolock(ac, group, cr); |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2414 | if (ret <= 0) { |
| 2415 | if (!first_err) |
| 2416 | first_err = ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2417 | continue; |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2418 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2419 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2420 | err = ext4_mb_load_buddy(sb, group, &e4b); |
| 2421 | if (err) |
| 2422 | goto out; |
| 2423 | |
| 2424 | ext4_lock_group(sb, group); |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 2425 | |
| 2426 | /* |
| 2427 | * We need to check again after locking the |
| 2428 | * block group |
| 2429 | */ |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2430 | ret = ext4_mb_good_group(ac, group, cr); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2431 | if (ret == 0) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2432 | ext4_unlock_group(sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 2433 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2434 | continue; |
| 2435 | } |
| 2436 | |
| 2437 | ac->ac_groups_scanned++; |
Jan Kara | d9b22cf | 2017-02-10 00:50:56 -0500 | [diff] [blame] | 2438 | if (cr == 0) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2439 | ext4_mb_simple_scan_group(ac, &e4b); |
Eric Sandeen | 506bf2d | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 2440 | else if (cr == 1 && sbi->s_stripe && |
| 2441 | !(ac->ac_g_ex.fe_len % sbi->s_stripe)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2442 | ext4_mb_scan_aligned(ac, &e4b); |
| 2443 | else |
| 2444 | ext4_mb_complex_scan_group(ac, &e4b); |
| 2445 | |
| 2446 | ext4_unlock_group(sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 2447 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2448 | |
| 2449 | if (ac->ac_status != AC_STATUS_CONTINUE) |
| 2450 | break; |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND && |
| 2455 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 2456 | /* |
| 2457 | * We've been searching too long. Let's try to allocate |
| 2458 | * the best chunk we've found so far |
| 2459 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2460 | ext4_mb_try_best_found(ac, &e4b); |
| 2461 | if (ac->ac_status != AC_STATUS_FOUND) { |
| 2462 | /* |
| 2463 | * Someone more lucky has already allocated it. |
| 2464 | * The only thing we can do is just take first |
| 2465 | * found block(s) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2466 | */ |
brookxu | 66d5e02 | 2020-08-17 15:36:06 +0800 | [diff] [blame] | 2467 | lost = atomic_inc_return(&sbi->s_mb_lost_chunks); |
| 2468 | mb_debug(sb, "lost chunk, group: %u, start: %d, len: %d, lost: %d\n", |
brookxu | c55ee7d | 2020-08-15 08:10:44 +0800 | [diff] [blame] | 2469 | ac->ac_b_ex.fe_group, ac->ac_b_ex.fe_start, |
| 2470 | ac->ac_b_ex.fe_len, lost); |
| 2471 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2472 | ac->ac_b_ex.fe_group = 0; |
| 2473 | ac->ac_b_ex.fe_start = 0; |
| 2474 | ac->ac_b_ex.fe_len = 0; |
| 2475 | ac->ac_status = AC_STATUS_CONTINUE; |
| 2476 | ac->ac_flags |= EXT4_MB_HINT_FIRST; |
| 2477 | cr = 3; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2478 | goto repeat; |
| 2479 | } |
| 2480 | } |
| 2481 | out: |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2482 | if (!err && ac->ac_status != AC_STATUS_FOUND && first_err) |
| 2483 | err = first_err; |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 2484 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 2485 | mb_debug(sb, "Best len %d, origin len %d, ac_status %u, ac_flags 0x%x, cr %d ret %d\n", |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 2486 | ac->ac_b_ex.fe_len, ac->ac_o_ex.fe_len, ac->ac_status, |
| 2487 | ac->ac_flags, cr, err); |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2488 | |
| 2489 | if (nr) |
| 2490 | ext4_mb_prefetch_fini(sb, prefetch_grp, nr); |
| 2491 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2492 | return err; |
| 2493 | } |
| 2494 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2495 | static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos) |
| 2496 | { |
Christoph Hellwig | 247dbed | 2018-04-11 11:37:23 +0200 | [diff] [blame] | 2497 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2498 | ext4_group_t group; |
| 2499 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2500 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2501 | return NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2502 | group = *pos + 1; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2503 | return (void *) ((unsigned long) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2507 | { |
Christoph Hellwig | 247dbed | 2018-04-11 11:37:23 +0200 | [diff] [blame] | 2508 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2509 | ext4_group_t group; |
| 2510 | |
| 2511 | ++*pos; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2512 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2513 | return NULL; |
| 2514 | group = *pos + 1; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2515 | return (void *) ((unsigned long) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v) |
| 2519 | { |
Christoph Hellwig | 247dbed | 2018-04-11 11:37:23 +0200 | [diff] [blame] | 2520 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2521 | ext4_group_t group = (ext4_group_t) ((unsigned long) v); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2522 | int i; |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2523 | int err, buddy_loaded = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2524 | struct ext4_buddy e4b; |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2525 | struct ext4_group_info *grinfo; |
Arnd Bergmann | 2df2c34 | 2017-08-05 21:57:46 -0400 | [diff] [blame] | 2526 | unsigned char blocksize_bits = min_t(unsigned char, |
| 2527 | sb->s_blocksize_bits, |
| 2528 | EXT4_MAX_BLOCK_LOG_SIZE); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2529 | struct sg { |
| 2530 | struct ext4_group_info info; |
Theodore Ts'o | b80b32b | 2017-08-14 08:29:18 -0400 | [diff] [blame] | 2531 | ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2]; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2532 | } sg; |
| 2533 | |
| 2534 | group--; |
| 2535 | if (group == 0) |
Rasmus Villemoes | 97b4af2 | 2015-06-15 00:32:58 -0400 | [diff] [blame] | 2536 | seq_puts(seq, "#group: free frags first [" |
| 2537 | " 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] | 2538 | " 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] | 2539 | |
Theodore Ts'o | b80b32b | 2017-08-14 08:29:18 -0400 | [diff] [blame] | 2540 | i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) + |
| 2541 | sizeof(struct ext4_group_info); |
| 2542 | |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2543 | grinfo = ext4_get_group_info(sb, group); |
| 2544 | /* Load the group info in memory only if not already loaded. */ |
| 2545 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) { |
| 2546 | err = ext4_mb_load_buddy(sb, group, &e4b); |
| 2547 | if (err) { |
| 2548 | seq_printf(seq, "#%-5u: I/O error\n", group); |
| 2549 | return 0; |
| 2550 | } |
| 2551 | buddy_loaded = 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2552 | } |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2553 | |
Theodore Ts'o | b80b32b | 2017-08-14 08:29:18 -0400 | [diff] [blame] | 2554 | memcpy(&sg, ext4_get_group_info(sb, group), i); |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2555 | |
| 2556 | if (buddy_loaded) |
| 2557 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2558 | |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2559 | seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2560 | sg.info.bb_fragments, sg.info.bb_first_free); |
| 2561 | for (i = 0; i <= 13; i++) |
Arnd Bergmann | 2df2c34 | 2017-08-05 21:57:46 -0400 | [diff] [blame] | 2562 | seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ? |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2563 | sg.info.bb_counters[i] : 0); |
Xu Wang | e0d438c | 2020-08-10 02:21:58 +0000 | [diff] [blame] | 2564 | seq_puts(seq, " ]\n"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2565 | |
| 2566 | return 0; |
| 2567 | } |
| 2568 | |
| 2569 | static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v) |
| 2570 | { |
| 2571 | } |
| 2572 | |
Christoph Hellwig | 247dbed | 2018-04-11 11:37:23 +0200 | [diff] [blame] | 2573 | const struct seq_operations ext4_mb_seq_groups_ops = { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2574 | .start = ext4_mb_seq_groups_start, |
| 2575 | .next = ext4_mb_seq_groups_next, |
| 2576 | .stop = ext4_mb_seq_groups_stop, |
| 2577 | .show = ext4_mb_seq_groups_show, |
| 2578 | }; |
| 2579 | |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2580 | static struct kmem_cache *get_groupinfo_cache(int blocksize_bits) |
| 2581 | { |
| 2582 | int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE; |
| 2583 | struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index]; |
| 2584 | |
| 2585 | BUG_ON(!cachep); |
| 2586 | return cachep; |
| 2587 | } |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2588 | |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2589 | /* |
| 2590 | * Allocate the top-level s_group_info array for the specified number |
| 2591 | * of groups |
| 2592 | */ |
| 2593 | int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups) |
| 2594 | { |
| 2595 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2596 | unsigned size; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2597 | struct ext4_group_info ***old_groupinfo, ***new_groupinfo; |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2598 | |
| 2599 | size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >> |
| 2600 | EXT4_DESC_PER_BLOCK_BITS(sb); |
| 2601 | if (size <= sbi->s_group_info_size) |
| 2602 | return 0; |
| 2603 | |
| 2604 | size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size); |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 2605 | new_groupinfo = kvzalloc(size, GFP_KERNEL); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2606 | if (!new_groupinfo) { |
| 2607 | ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group"); |
| 2608 | return -ENOMEM; |
| 2609 | } |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2610 | rcu_read_lock(); |
| 2611 | old_groupinfo = rcu_dereference(sbi->s_group_info); |
| 2612 | if (old_groupinfo) |
| 2613 | memcpy(new_groupinfo, old_groupinfo, |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2614 | sbi->s_group_info_size * sizeof(*sbi->s_group_info)); |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2615 | rcu_read_unlock(); |
| 2616 | rcu_assign_pointer(sbi->s_group_info, new_groupinfo); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2617 | sbi->s_group_info_size = size / sizeof(*sbi->s_group_info); |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2618 | if (old_groupinfo) |
| 2619 | ext4_kvfree_array_rcu(old_groupinfo); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2620 | ext4_debug("allocated s_groupinfo array for %d meta_bg's\n", |
| 2621 | sbi->s_group_info_size); |
| 2622 | return 0; |
| 2623 | } |
| 2624 | |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2625 | /* 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] | 2626 | 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] | 2627 | struct ext4_group_desc *desc) |
| 2628 | { |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2629 | int i; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2630 | int metalen = 0; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2631 | int idx = group >> EXT4_DESC_PER_BLOCK_BITS(sb); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2632 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2633 | struct ext4_group_info **meta_group_info; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2634 | struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2635 | |
| 2636 | /* |
| 2637 | * First check if this group is the first of a reserved block. |
| 2638 | * If it's true, we have to allocate a new table of pointers |
| 2639 | * to ext4_group_info structures |
| 2640 | */ |
| 2641 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { |
| 2642 | metalen = sizeof(*meta_group_info) << |
| 2643 | EXT4_DESC_PER_BLOCK_BITS(sb); |
Dmitry Monakhov | 4fdb554 | 2014-11-25 13:08:04 -0500 | [diff] [blame] | 2644 | meta_group_info = kmalloc(metalen, GFP_NOFS); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2645 | if (meta_group_info == NULL) { |
Joe Perches | 7f6a11e | 2012-03-19 23:09:43 -0400 | [diff] [blame] | 2646 | ext4_msg(sb, KERN_ERR, "can't allocate mem " |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 2647 | "for a buddy group"); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2648 | goto exit_meta_group_info; |
| 2649 | } |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2650 | rcu_read_lock(); |
| 2651 | rcu_dereference(sbi->s_group_info)[idx] = meta_group_info; |
| 2652 | rcu_read_unlock(); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2653 | } |
| 2654 | |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2655 | meta_group_info = sbi_array_rcu_deref(sbi, s_group_info, idx); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2656 | i = group & (EXT4_DESC_PER_BLOCK(sb) - 1); |
| 2657 | |
Dmitry Monakhov | 4fdb554 | 2014-11-25 13:08:04 -0500 | [diff] [blame] | 2658 | meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2659 | if (meta_group_info[i] == NULL) { |
Joe Perches | 7f6a11e | 2012-03-19 23:09:43 -0400 | [diff] [blame] | 2660 | ext4_msg(sb, KERN_ERR, "can't allocate buddy mem"); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2661 | goto exit_group_info; |
| 2662 | } |
| 2663 | set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, |
| 2664 | &(meta_group_info[i]->bb_state)); |
| 2665 | |
| 2666 | /* |
| 2667 | * initialize bb_free to be able to skip |
| 2668 | * empty groups without initialization |
| 2669 | */ |
Theodore Ts'o | 8844618 | 2018-06-14 00:58:00 -0400 | [diff] [blame] | 2670 | if (ext4_has_group_desc_csum(sb) && |
| 2671 | (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2672 | meta_group_info[i]->bb_free = |
Theodore Ts'o | cff1dfd7 | 2011-09-09 19:12:51 -0400 | [diff] [blame] | 2673 | ext4_free_clusters_after_init(sb, group, desc); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2674 | } else { |
| 2675 | meta_group_info[i]->bb_free = |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 2676 | ext4_free_group_clusters(sb, desc); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 2680 | init_rwsem(&meta_group_info[i]->alloc_sem); |
Venkatesh Pallipadi | 64e290e | 2010-03-04 22:25:21 -0500 | [diff] [blame] | 2681 | meta_group_info[i]->bb_free_root = RB_ROOT; |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 2682 | meta_group_info[i]->bb_largest_free_order = -1; /* uninit */ |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2683 | |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 2684 | mb_group_bb_bitmap_alloc(sb, meta_group_info[i], group); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2685 | return 0; |
| 2686 | |
| 2687 | exit_group_info: |
| 2688 | /* If a meta_group_info table has been allocated, release it now */ |
Tao Ma | caaf7a2 | 2011-07-11 18:42:42 -0400 | [diff] [blame] | 2689 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2690 | struct ext4_group_info ***group_info; |
| 2691 | |
| 2692 | rcu_read_lock(); |
| 2693 | group_info = rcu_dereference(sbi->s_group_info); |
| 2694 | kfree(group_info[idx]); |
| 2695 | group_info[idx] = NULL; |
| 2696 | rcu_read_unlock(); |
Tao Ma | caaf7a2 | 2011-07-11 18:42:42 -0400 | [diff] [blame] | 2697 | } |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2698 | exit_meta_group_info: |
| 2699 | return -ENOMEM; |
| 2700 | } /* ext4_mb_add_groupinfo */ |
| 2701 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2702 | static int ext4_mb_init_backend(struct super_block *sb) |
| 2703 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2704 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2705 | ext4_group_t i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2706 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2707 | int err; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2708 | struct ext4_group_desc *desc; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2709 | struct ext4_group_info ***group_info; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2710 | struct kmem_cache *cachep; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2711 | |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2712 | err = ext4_mb_alloc_groupinfo(sb, ngroups); |
| 2713 | if (err) |
| 2714 | return err; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2715 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2716 | sbi->s_buddy_cache = new_inode(sb); |
| 2717 | if (sbi->s_buddy_cache == NULL) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 2718 | ext4_msg(sb, KERN_ERR, "can't get new inode"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2719 | goto err_freesgi; |
| 2720 | } |
Yu Jian | 48e6061 | 2011-08-01 17:41:39 -0400 | [diff] [blame] | 2721 | /* To avoid potentially colliding with an valid on-disk inode number, |
| 2722 | * use EXT4_BAD_INO for the buddy cache inode number. This inode is |
| 2723 | * not in the inode hash, so it should never be found by iget(), but |
| 2724 | * this will avoid confusion if it ever shows up during debugging. */ |
| 2725 | sbi->s_buddy_cache->i_ino = EXT4_BAD_INO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2726 | EXT4_I(sbi->s_buddy_cache)->i_disksize = 0; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2727 | for (i = 0; i < ngroups; i++) { |
Khazhismel Kumykov | 4b99faa | 2019-04-25 12:58:01 -0400 | [diff] [blame] | 2728 | cond_resched(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2729 | desc = ext4_get_group_desc(sb, i, NULL); |
| 2730 | if (desc == NULL) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 2731 | ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2732 | goto err_freebuddy; |
| 2733 | } |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2734 | if (ext4_mb_add_groupinfo(sb, i, desc) != 0) |
| 2735 | goto err_freebuddy; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2736 | } |
| 2737 | |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2738 | if (ext4_has_feature_flex_bg(sb)) { |
| 2739 | /* a single flex group is supposed to be read by a single IO */ |
| 2740 | sbi->s_mb_prefetch = 1 << sbi->s_es->s_log_groups_per_flex; |
| 2741 | sbi->s_mb_prefetch *= 8; /* 8 prefetch IOs in flight at most */ |
| 2742 | } else { |
| 2743 | sbi->s_mb_prefetch = 32; |
| 2744 | } |
| 2745 | if (sbi->s_mb_prefetch > ext4_get_groups_count(sb)) |
| 2746 | sbi->s_mb_prefetch = ext4_get_groups_count(sb); |
| 2747 | /* now many real IOs to prefetch within a single allocation at cr=0 |
| 2748 | * given cr=0 is an CPU-related optimization we shouldn't try to |
| 2749 | * load too many groups, at some point we should start to use what |
| 2750 | * we've got in memory. |
| 2751 | * with an average random access time 5ms, it'd take a second to get |
| 2752 | * 200 groups (* N with flex_bg), so let's make this limit 4 |
| 2753 | */ |
| 2754 | sbi->s_mb_prefetch_limit = sbi->s_mb_prefetch * 4; |
| 2755 | if (sbi->s_mb_prefetch_limit > ext4_get_groups_count(sb)) |
| 2756 | sbi->s_mb_prefetch_limit = ext4_get_groups_count(sb); |
| 2757 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2758 | return 0; |
| 2759 | |
| 2760 | err_freebuddy: |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2761 | cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
Roel Kluin | f1fa334 | 2008-04-29 22:01:15 -0400 | [diff] [blame] | 2762 | while (i-- > 0) |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2763 | kmem_cache_free(cachep, ext4_get_group_info(sb, i)); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 2764 | i = sbi->s_group_info_size; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2765 | rcu_read_lock(); |
| 2766 | group_info = rcu_dereference(sbi->s_group_info); |
Roel Kluin | f1fa334 | 2008-04-29 22:01:15 -0400 | [diff] [blame] | 2767 | while (i-- > 0) |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2768 | kfree(group_info[i]); |
| 2769 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2770 | iput(sbi->s_buddy_cache); |
| 2771 | err_freesgi: |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2772 | rcu_read_lock(); |
| 2773 | kvfree(rcu_dereference(sbi->s_group_info)); |
| 2774 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2775 | return -ENOMEM; |
| 2776 | } |
| 2777 | |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 2778 | static void ext4_groupinfo_destroy_slabs(void) |
| 2779 | { |
| 2780 | int i; |
| 2781 | |
| 2782 | for (i = 0; i < NR_GRPINFO_CACHES; i++) { |
Sean Fu | 21c580d | 2018-05-20 22:44:13 -0400 | [diff] [blame] | 2783 | kmem_cache_destroy(ext4_groupinfo_caches[i]); |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 2784 | ext4_groupinfo_caches[i] = NULL; |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | static int ext4_groupinfo_create_slab(size_t size) |
| 2789 | { |
| 2790 | static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex); |
| 2791 | int slab_size; |
| 2792 | int blocksize_bits = order_base_2(size); |
| 2793 | int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE; |
| 2794 | struct kmem_cache *cachep; |
| 2795 | |
| 2796 | if (cache_index >= NR_GRPINFO_CACHES) |
| 2797 | return -EINVAL; |
| 2798 | |
| 2799 | if (unlikely(cache_index < 0)) |
| 2800 | cache_index = 0; |
| 2801 | |
| 2802 | mutex_lock(&ext4_grpinfo_slab_create_mutex); |
| 2803 | if (ext4_groupinfo_caches[cache_index]) { |
| 2804 | mutex_unlock(&ext4_grpinfo_slab_create_mutex); |
| 2805 | return 0; /* Already created */ |
| 2806 | } |
| 2807 | |
| 2808 | slab_size = offsetof(struct ext4_group_info, |
| 2809 | bb_counters[blocksize_bits + 2]); |
| 2810 | |
| 2811 | cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index], |
| 2812 | slab_size, 0, SLAB_RECLAIM_ACCOUNT, |
| 2813 | NULL); |
| 2814 | |
Tao Ma | 823ba01 | 2011-07-11 18:26:01 -0400 | [diff] [blame] | 2815 | ext4_groupinfo_caches[cache_index] = cachep; |
| 2816 | |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 2817 | mutex_unlock(&ext4_grpinfo_slab_create_mutex); |
| 2818 | if (!cachep) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 2819 | printk(KERN_EMERG |
| 2820 | "EXT4-fs: no memory for groupinfo slab cache\n"); |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 2821 | return -ENOMEM; |
| 2822 | } |
| 2823 | |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 2824 | return 0; |
| 2825 | } |
| 2826 | |
Akira Fujita | 9d99012 | 2012-05-28 14:19:25 -0400 | [diff] [blame] | 2827 | int ext4_mb_init(struct super_block *sb) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2828 | { |
| 2829 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 2830 | unsigned i, j; |
Nicolai Stange | 935244c | 2016-05-05 19:46:19 -0400 | [diff] [blame] | 2831 | unsigned offset, offset_incr; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2832 | unsigned max; |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2833 | int ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2834 | |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame] | 2835 | i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2836 | |
| 2837 | sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL); |
| 2838 | if (sbi->s_mb_offsets == NULL) { |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2839 | ret = -ENOMEM; |
| 2840 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2841 | } |
Yasunori Goto | ff7ef32 | 2008-12-17 00:48:39 -0500 | [diff] [blame] | 2842 | |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame] | 2843 | i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2844 | sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL); |
| 2845 | if (sbi->s_mb_maxs == NULL) { |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2846 | ret = -ENOMEM; |
| 2847 | goto out; |
| 2848 | } |
| 2849 | |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 2850 | ret = ext4_groupinfo_create_slab(sb->s_blocksize); |
| 2851 | if (ret < 0) |
| 2852 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2853 | |
| 2854 | /* order 0 is regular bitmap */ |
| 2855 | sbi->s_mb_maxs[0] = sb->s_blocksize << 3; |
| 2856 | sbi->s_mb_offsets[0] = 0; |
| 2857 | |
| 2858 | i = 1; |
| 2859 | offset = 0; |
Nicolai Stange | 935244c | 2016-05-05 19:46:19 -0400 | [diff] [blame] | 2860 | offset_incr = 1 << (sb->s_blocksize_bits - 1); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2861 | max = sb->s_blocksize << 2; |
| 2862 | do { |
| 2863 | sbi->s_mb_offsets[i] = offset; |
| 2864 | sbi->s_mb_maxs[i] = max; |
Nicolai Stange | 935244c | 2016-05-05 19:46:19 -0400 | [diff] [blame] | 2865 | offset += offset_incr; |
| 2866 | offset_incr = offset_incr >> 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2867 | max = max >> 1; |
| 2868 | i++; |
| 2869 | } while (i <= sb->s_blocksize_bits + 1); |
| 2870 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2871 | spin_lock_init(&sbi->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2872 | spin_lock_init(&sbi->s_bal_lock); |
Theodore Ts'o | d08854f | 2016-06-26 18:24:01 -0400 | [diff] [blame] | 2873 | sbi->s_mb_free_pending = 0; |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 2874 | INIT_LIST_HEAD(&sbi->s_freed_data_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2875 | |
| 2876 | sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN; |
| 2877 | sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN; |
| 2878 | sbi->s_mb_stats = MB_DEFAULT_STATS; |
| 2879 | sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD; |
| 2880 | sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS; |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 2881 | sbi->s_mb_max_inode_prealloc = MB_DEFAULT_MAX_INODE_PREALLOC; |
Theodore Ts'o | 27baebb | 2011-09-09 19:02:51 -0400 | [diff] [blame] | 2882 | /* |
| 2883 | * The default group preallocation is 512, which for 4k block |
| 2884 | * sizes translates to 2 megabytes. However for bigalloc file |
| 2885 | * systems, this is probably too big (i.e, if the cluster size |
| 2886 | * is 1 megabyte, then group preallocation size becomes half a |
| 2887 | * gigabyte!). As a default, we will keep a two megabyte |
| 2888 | * group pralloc size for cluster sizes up to 64k, and after |
| 2889 | * that, we will force a minimum group preallocation size of |
| 2890 | * 32 clusters. This translates to 8 megs when the cluster |
| 2891 | * size is 256k, and 32 megs when the cluster size is 1 meg, |
| 2892 | * which seems reasonable as a default. |
| 2893 | */ |
| 2894 | sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >> |
| 2895 | sbi->s_cluster_bits, 32); |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 2896 | /* |
| 2897 | * If there is a s_stripe > 1, then we set the s_mb_group_prealloc |
| 2898 | * to the lowest multiple of s_stripe which is bigger than |
| 2899 | * the s_mb_group_prealloc as determined above. We want |
| 2900 | * the preallocation size to be an exact multiple of the |
| 2901 | * RAID stripe size so that preallocations don't fragment |
| 2902 | * the stripes. |
| 2903 | */ |
| 2904 | if (sbi->s_stripe > 1) { |
| 2905 | sbi->s_mb_group_prealloc = roundup( |
| 2906 | sbi->s_mb_group_prealloc, sbi->s_stripe); |
| 2907 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2908 | |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2909 | sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2910 | if (sbi->s_locality_groups == NULL) { |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2911 | ret = -ENOMEM; |
Andrey Tsyvarev | 029b10c | 2014-05-12 12:34:21 -0400 | [diff] [blame] | 2912 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2913 | } |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2914 | for_each_possible_cpu(i) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2915 | struct ext4_locality_group *lg; |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 2916 | lg = per_cpu_ptr(sbi->s_locality_groups, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2917 | mutex_init(&lg->lg_mutex); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 2918 | for (j = 0; j < PREALLOC_TB_SIZE; j++) |
| 2919 | INIT_LIST_HEAD(&lg->lg_prealloc_list[j]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2920 | spin_lock_init(&lg->lg_prealloc_lock); |
| 2921 | } |
| 2922 | |
Yu Jian | 79a77c5 | 2011-08-01 17:41:46 -0400 | [diff] [blame] | 2923 | /* init file for buddy data */ |
| 2924 | ret = ext4_mb_init_backend(sb); |
Tao Ma | 7aa0bae | 2011-10-06 10:22:28 -0400 | [diff] [blame] | 2925 | if (ret != 0) |
| 2926 | goto out_free_locality_groups; |
Yu Jian | 79a77c5 | 2011-08-01 17:41:46 -0400 | [diff] [blame] | 2927 | |
Tao Ma | 7aa0bae | 2011-10-06 10:22:28 -0400 | [diff] [blame] | 2928 | return 0; |
| 2929 | |
| 2930 | out_free_locality_groups: |
| 2931 | free_percpu(sbi->s_locality_groups); |
| 2932 | sbi->s_locality_groups = NULL; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2933 | out: |
Tao Ma | 7aa0bae | 2011-10-06 10:22:28 -0400 | [diff] [blame] | 2934 | kfree(sbi->s_mb_offsets); |
| 2935 | sbi->s_mb_offsets = NULL; |
| 2936 | kfree(sbi->s_mb_maxs); |
| 2937 | sbi->s_mb_maxs = NULL; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2938 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2939 | } |
| 2940 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 2941 | /* need to called with the ext4 group lock held */ |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 2942 | static int ext4_mb_cleanup_pa(struct ext4_group_info *grp) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2943 | { |
| 2944 | struct ext4_prealloc_space *pa; |
| 2945 | struct list_head *cur, *tmp; |
| 2946 | int count = 0; |
| 2947 | |
| 2948 | list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) { |
| 2949 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 2950 | list_del(&pa->pa_group_list); |
| 2951 | count++; |
Aneesh Kumar K.V | 688f05a | 2008-10-13 12:14:14 -0400 | [diff] [blame] | 2952 | kmem_cache_free(ext4_pspace_cachep, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2953 | } |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 2954 | return count; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | int ext4_mb_release(struct super_block *sb) |
| 2958 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2959 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2960 | ext4_group_t i; |
| 2961 | int num_meta_group_infos; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2962 | struct ext4_group_info *grinfo, ***group_info; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2963 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2964 | struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 2965 | int count; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2966 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2967 | if (sbi->s_group_info) { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2968 | for (i = 0; i < ngroups; i++) { |
Khazhismel Kumykov | 4b99faa | 2019-04-25 12:58:01 -0400 | [diff] [blame] | 2969 | cond_resched(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2970 | grinfo = ext4_get_group_info(sb, i); |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 2971 | mb_group_bb_bitmap_free(grinfo); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2972 | ext4_lock_group(sb, i); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 2973 | count = ext4_mb_cleanup_pa(grinfo); |
| 2974 | if (count) |
| 2975 | mb_debug(sb, "mballoc: %d PAs left\n", |
| 2976 | count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2977 | ext4_unlock_group(sb, i); |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 2978 | kmem_cache_free(cachep, grinfo); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2979 | } |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2980 | num_meta_group_infos = (ngroups + |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2981 | EXT4_DESC_PER_BLOCK(sb) - 1) >> |
| 2982 | EXT4_DESC_PER_BLOCK_BITS(sb); |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2983 | rcu_read_lock(); |
| 2984 | group_info = rcu_dereference(sbi->s_group_info); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2985 | for (i = 0; i < num_meta_group_infos; i++) |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 2986 | kfree(group_info[i]); |
| 2987 | kvfree(group_info); |
| 2988 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2989 | } |
| 2990 | kfree(sbi->s_mb_offsets); |
| 2991 | kfree(sbi->s_mb_maxs); |
Markus Elfring | bfcba2d | 2014-11-25 20:01:37 -0500 | [diff] [blame] | 2992 | iput(sbi->s_buddy_cache); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2993 | if (sbi->s_mb_stats) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 2994 | ext4_msg(sb, KERN_INFO, |
| 2995 | "mballoc: %u blocks %u reqs (%u success)", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2996 | atomic_read(&sbi->s_bal_allocated), |
| 2997 | atomic_read(&sbi->s_bal_reqs), |
| 2998 | atomic_read(&sbi->s_bal_success)); |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 2999 | ext4_msg(sb, KERN_INFO, |
| 3000 | "mballoc: %u extents scanned, %u goal hits, " |
| 3001 | "%u 2^N hits, %u breaks, %u lost", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3002 | atomic_read(&sbi->s_bal_ex_scanned), |
| 3003 | atomic_read(&sbi->s_bal_goals), |
| 3004 | atomic_read(&sbi->s_bal_2orders), |
| 3005 | atomic_read(&sbi->s_bal_breaks), |
| 3006 | atomic_read(&sbi->s_mb_lost_chunks)); |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3007 | ext4_msg(sb, KERN_INFO, |
| 3008 | "mballoc: %lu generated and it took %Lu", |
Tao Ma | ced156e | 2011-07-23 16:18:05 -0400 | [diff] [blame] | 3009 | sbi->s_mb_buddies_generated, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3010 | sbi->s_mb_generation_time); |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3011 | ext4_msg(sb, KERN_INFO, |
| 3012 | "mballoc: %u preallocated, %u discarded", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3013 | atomic_read(&sbi->s_mb_preallocated), |
| 3014 | atomic_read(&sbi->s_mb_discarded)); |
| 3015 | } |
| 3016 | |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 3017 | free_percpu(sbi->s_locality_groups); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3018 | |
| 3019 | return 0; |
| 3020 | } |
| 3021 | |
Lukas Czerner | 77ca6cd | 2010-10-27 21:30:11 -0400 | [diff] [blame] | 3022 | static inline int ext4_issue_discard(struct super_block *sb, |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3023 | ext4_group_t block_group, ext4_grpblk_t cluster, int count, |
| 3024 | struct bio **biop) |
Jiaying Zhang | 5c52183 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 3025 | { |
Jiaying Zhang | 5c52183 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 3026 | ext4_fsblk_t discard_block; |
| 3027 | |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 3028 | discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) + |
| 3029 | ext4_group_first_block_no(sb, block_group)); |
| 3030 | count = EXT4_C2B(EXT4_SB(sb), count); |
Jiaying Zhang | 5c52183 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 3031 | trace_ext4_discard_blocks(sb, |
| 3032 | (unsigned long long) discard_block, count); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3033 | if (biop) { |
| 3034 | return __blkdev_issue_discard(sb->s_bdev, |
| 3035 | (sector_t)discard_block << (sb->s_blocksize_bits - 9), |
| 3036 | (sector_t)count << (sb->s_blocksize_bits - 9), |
| 3037 | GFP_NOFS, 0, biop); |
| 3038 | } else |
| 3039 | return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0); |
Jiaying Zhang | 5c52183 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 3040 | } |
| 3041 | |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3042 | static void ext4_free_data_in_buddy(struct super_block *sb, |
| 3043 | struct ext4_free_data *entry) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3044 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3045 | struct ext4_buddy e4b; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 3046 | struct ext4_group_info *db; |
Theodore Ts'o | d9f3450 | 2011-04-30 13:47:24 -0400 | [diff] [blame] | 3047 | int err, count = 0, count2 = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3048 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3049 | mb_debug(sb, "gonna free %u blocks in group %u (0x%p):", |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3050 | entry->efd_count, entry->efd_group, entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3051 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3052 | err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b); |
| 3053 | /* we expect to find existing buddy because it's pinned */ |
| 3054 | BUG_ON(err != 0); |
Theodore Ts'o | b90f687 | 2010-04-20 16:51:59 -0400 | [diff] [blame] | 3055 | |
Theodore Ts'o | d08854f | 2016-06-26 18:24:01 -0400 | [diff] [blame] | 3056 | spin_lock(&EXT4_SB(sb)->s_md_lock); |
| 3057 | EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count; |
| 3058 | spin_unlock(&EXT4_SB(sb)->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3059 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3060 | db = e4b.bd_info; |
| 3061 | /* there are blocks to put in buddy to make them really free */ |
| 3062 | count += entry->efd_count; |
| 3063 | count2++; |
| 3064 | ext4_lock_group(sb, entry->efd_group); |
| 3065 | /* Take it out of per group rb tree */ |
| 3066 | rb_erase(&entry->efd_node, &(db->bb_free_root)); |
| 3067 | 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] | 3068 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3069 | /* |
| 3070 | * Clear the trimmed flag for the group so that the next |
| 3071 | * ext4_trim_fs can trim it. |
| 3072 | * If the volume is mounted with -o discard, online discard |
| 3073 | * is supported and the free blocks will be trimmed online. |
| 3074 | */ |
| 3075 | if (!test_opt(sb, DISCARD)) |
| 3076 | EXT4_MB_GRP_CLEAR_TRIMMED(db); |
| 3077 | |
| 3078 | if (!db->bb_free_root.rb_node) { |
| 3079 | /* No more items in the per group rb tree |
| 3080 | * balance refcounts from ext4_mb_free_metadata() |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 3081 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3082 | put_page(e4b.bd_buddy_page); |
| 3083 | put_page(e4b.bd_bitmap_page); |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 3084 | } |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3085 | ext4_unlock_group(sb, entry->efd_group); |
| 3086 | kmem_cache_free(ext4_free_data_cachep, entry); |
| 3087 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3088 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3089 | mb_debug(sb, "freed %d blocks in %d structures\n", count, |
| 3090 | count2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3091 | } |
| 3092 | |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3093 | /* |
| 3094 | * This function is called by the jbd2 layer once the commit has finished, |
| 3095 | * so we know we can free the blocks that were released with that commit. |
| 3096 | */ |
| 3097 | void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid) |
| 3098 | { |
| 3099 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3100 | struct ext4_free_data *entry, *tmp; |
| 3101 | struct bio *discard_bio = NULL; |
| 3102 | struct list_head freed_data_list; |
| 3103 | struct list_head *cut_pos = NULL; |
| 3104 | int err; |
| 3105 | |
| 3106 | INIT_LIST_HEAD(&freed_data_list); |
| 3107 | |
| 3108 | spin_lock(&sbi->s_md_lock); |
| 3109 | list_for_each_entry(entry, &sbi->s_freed_data_list, efd_list) { |
| 3110 | if (entry->efd_tid != commit_tid) |
| 3111 | break; |
| 3112 | cut_pos = &entry->efd_list; |
| 3113 | } |
| 3114 | if (cut_pos) |
| 3115 | list_cut_position(&freed_data_list, &sbi->s_freed_data_list, |
| 3116 | cut_pos); |
| 3117 | spin_unlock(&sbi->s_md_lock); |
| 3118 | |
| 3119 | if (test_opt(sb, DISCARD)) { |
| 3120 | list_for_each_entry(entry, &freed_data_list, efd_list) { |
| 3121 | err = ext4_issue_discard(sb, entry->efd_group, |
| 3122 | entry->efd_start_cluster, |
| 3123 | entry->efd_count, |
| 3124 | &discard_bio); |
| 3125 | if (err && err != -EOPNOTSUPP) { |
| 3126 | ext4_msg(sb, KERN_WARNING, "discard request in" |
| 3127 | " group:%d block:%d count:%d failed" |
| 3128 | " with %d", entry->efd_group, |
| 3129 | entry->efd_start_cluster, |
| 3130 | entry->efd_count, err); |
| 3131 | } else if (err == -EOPNOTSUPP) |
| 3132 | break; |
| 3133 | } |
| 3134 | |
Daeho Jeong | e451057 | 2017-08-05 13:11:57 -0400 | [diff] [blame] | 3135 | if (discard_bio) { |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3136 | submit_bio_wait(discard_bio); |
Daeho Jeong | e451057 | 2017-08-05 13:11:57 -0400 | [diff] [blame] | 3137 | bio_put(discard_bio); |
| 3138 | } |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list) |
| 3142 | ext4_free_data_in_buddy(sb, entry); |
| 3143 | } |
| 3144 | |
Theodore Ts'o | 5dabfc7 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3145 | int __init ext4_init_mballoc(void) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3146 | { |
Theodore Ts'o | 1682808 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 3147 | ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space, |
| 3148 | SLAB_RECLAIM_ACCOUNT); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3149 | if (ext4_pspace_cachep == NULL) |
Ritesh Harjani | f283529 | 2020-05-10 11:54:46 +0530 | [diff] [blame] | 3150 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3151 | |
Theodore Ts'o | 1682808 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 3152 | ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context, |
| 3153 | SLAB_RECLAIM_ACCOUNT); |
Ritesh Harjani | f283529 | 2020-05-10 11:54:46 +0530 | [diff] [blame] | 3154 | if (ext4_ac_cachep == NULL) |
| 3155 | goto out_pa_free; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 3156 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3157 | ext4_free_data_cachep = KMEM_CACHE(ext4_free_data, |
| 3158 | SLAB_RECLAIM_ACCOUNT); |
Ritesh Harjani | f283529 | 2020-05-10 11:54:46 +0530 | [diff] [blame] | 3159 | if (ext4_free_data_cachep == NULL) |
| 3160 | goto out_ac_free; |
| 3161 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3162 | return 0; |
Ritesh Harjani | f283529 | 2020-05-10 11:54:46 +0530 | [diff] [blame] | 3163 | |
| 3164 | out_ac_free: |
| 3165 | kmem_cache_destroy(ext4_ac_cachep); |
| 3166 | out_pa_free: |
| 3167 | kmem_cache_destroy(ext4_pspace_cachep); |
| 3168 | out: |
| 3169 | return -ENOMEM; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3170 | } |
| 3171 | |
Theodore Ts'o | 5dabfc7 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3172 | void ext4_exit_mballoc(void) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3173 | { |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 3174 | /* |
Jesper Dangaard Brouer | 3e03f9c | 2009-07-05 22:29:27 -0400 | [diff] [blame] | 3175 | * Wait for completion of call_rcu()'s on ext4_pspace_cachep |
| 3176 | * before destroying the slab cache. |
| 3177 | */ |
| 3178 | rcu_barrier(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3179 | kmem_cache_destroy(ext4_pspace_cachep); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3180 | kmem_cache_destroy(ext4_ac_cachep); |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3181 | kmem_cache_destroy(ext4_free_data_cachep); |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 3182 | ext4_groupinfo_destroy_slabs(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | |
| 3186 | /* |
Uwe Kleine-König | 73b2c71 | 2010-07-30 21:02:47 +0200 | [diff] [blame] | 3187 | * 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] | 3188 | * Returns 0 if success or error code |
| 3189 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3190 | static noinline_for_stack int |
| 3191 | ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3192 | handle_t *handle, unsigned int reserv_clstrs) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3193 | { |
| 3194 | struct buffer_head *bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3195 | struct ext4_group_desc *gdp; |
| 3196 | struct buffer_head *gdp_bh; |
| 3197 | struct ext4_sb_info *sbi; |
| 3198 | struct super_block *sb; |
| 3199 | ext4_fsblk_t block; |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3200 | int err, len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3201 | |
| 3202 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 3203 | BUG_ON(ac->ac_b_ex.fe_len <= 0); |
| 3204 | |
| 3205 | sb = ac->ac_sb; |
| 3206 | sbi = EXT4_SB(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3207 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3208 | 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] | 3209 | if (IS_ERR(bitmap_bh)) { |
| 3210 | err = PTR_ERR(bitmap_bh); |
| 3211 | bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3212 | goto out_err; |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 3213 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3214 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 3215 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3216 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
| 3217 | if (err) |
| 3218 | goto out_err; |
| 3219 | |
| 3220 | err = -EIO; |
| 3221 | gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh); |
| 3222 | if (!gdp) |
| 3223 | goto out_err; |
| 3224 | |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 3225 | 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] | 3226 | ext4_free_group_clusters(sb, gdp)); |
Aneesh Kumar K.V | 03cddb8 | 2008-06-05 20:59:29 -0400 | [diff] [blame] | 3227 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 3228 | BUFFER_TRACE(gdp_bh, "get_write_access"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3229 | err = ext4_journal_get_write_access(handle, gdp_bh); |
| 3230 | if (err) |
| 3231 | goto out_err; |
| 3232 | |
Akinobu Mita | bda00de | 2010-03-03 23:53:25 -0500 | [diff] [blame] | 3233 | block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3234 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3235 | len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len); |
Jan Kara | ce9f24c | 2020-07-28 15:04:34 +0200 | [diff] [blame] | 3236 | if (!ext4_inode_block_valid(ac->ac_inode, block, len)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 3237 | ext4_error(sb, "Allocating blocks %llu-%llu which overlap " |
Theodore Ts'o | 1084f25 | 2012-03-19 23:13:43 -0400 | [diff] [blame] | 3238 | "fs metadata", block, block+len); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3239 | /* File system mounted not to panic on error |
Vegard Nossum | 554a5cc | 2016-07-14 23:02:47 -0400 | [diff] [blame] | 3240 | * Fix the bitmap and return EFSCORRUPTED |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3241 | * We leak some of the blocks here. |
| 3242 | */ |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3243 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
Yongqiang Yang | c3e94d1 | 2011-07-26 22:05:53 -0400 | [diff] [blame] | 3244 | ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, |
| 3245 | ac->ac_b_ex.fe_len); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3246 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3247 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3248 | if (!err) |
Vegard Nossum | 554a5cc | 2016-07-14 23:02:47 -0400 | [diff] [blame] | 3249 | err = -EFSCORRUPTED; |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3250 | goto out_err; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3251 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3252 | |
| 3253 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3254 | #ifdef AGGRESSIVE_CHECK |
| 3255 | { |
| 3256 | int i; |
| 3257 | for (i = 0; i < ac->ac_b_ex.fe_len; i++) { |
| 3258 | BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i, |
| 3259 | bitmap_bh->b_data)); |
| 3260 | } |
| 3261 | } |
| 3262 | #endif |
Yongqiang Yang | c3e94d1 | 2011-07-26 22:05:53 -0400 | [diff] [blame] | 3263 | ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, |
| 3264 | ac->ac_b_ex.fe_len); |
Theodore Ts'o | 8844618 | 2018-06-14 00:58:00 -0400 | [diff] [blame] | 3265 | if (ext4_has_group_desc_csum(sb) && |
| 3266 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3267 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 3268 | ext4_free_group_clusters_set(sb, gdp, |
Theodore Ts'o | cff1dfd7 | 2011-09-09 19:12:51 -0400 | [diff] [blame] | 3269 | ext4_free_clusters_after_init(sb, |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 3270 | ac->ac_b_ex.fe_group, gdp)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3271 | } |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 3272 | len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len; |
| 3273 | ext4_free_group_clusters_set(sb, gdp, len); |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 3274 | 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] | 3275 | 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] | 3276 | |
| 3277 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
Theodore Ts'o | 5704265 | 2011-09-09 18:56:51 -0400 | [diff] [blame] | 3278 | 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] | 3279 | /* |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 3280 | * Now reduce the dirty block count also. Should not go negative |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3281 | */ |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 3282 | if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED)) |
| 3283 | /* release all the reserved blocks if non delalloc */ |
Theodore Ts'o | 5704265 | 2011-09-09 18:56:51 -0400 | [diff] [blame] | 3284 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
| 3285 | reserv_clstrs); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3286 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3287 | if (sbi->s_log_groups_per_flex) { |
| 3288 | ext4_group_t flex_group = ext4_flex_group(sbi, |
| 3289 | ac->ac_b_ex.fe_group); |
Theodore Ts'o | 90ba983 | 2013-03-11 23:39:59 -0400 | [diff] [blame] | 3290 | atomic64_sub(ac->ac_b_ex.fe_len, |
Suraj Jitindar Singh | 7c99072 | 2020-02-18 19:08:51 -0800 | [diff] [blame] | 3291 | &sbi_array_rcu_deref(sbi, s_flex_groups, |
| 3292 | flex_group)->free_clusters); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3293 | } |
| 3294 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3295 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3296 | if (err) |
| 3297 | goto out_err; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3298 | err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3299 | |
| 3300 | out_err: |
Aneesh Kumar K.V | 42a10ad | 2008-02-10 01:07:28 -0500 | [diff] [blame] | 3301 | brelse(bitmap_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3302 | return err; |
| 3303 | } |
| 3304 | |
| 3305 | /* |
| 3306 | * here we normalize request for locality group |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 3307 | * Group request are normalized to s_mb_group_prealloc, which goes to |
| 3308 | * s_strip if we set the same via mount option. |
| 3309 | * s_mb_group_prealloc can be configured via |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 3310 | * /sys/fs/ext4/<partition>/mb_group_prealloc |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3311 | * |
| 3312 | * XXX: should we try to preallocate more than the group has now? |
| 3313 | */ |
| 3314 | static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac) |
| 3315 | { |
| 3316 | struct super_block *sb = ac->ac_sb; |
| 3317 | struct ext4_locality_group *lg = ac->ac_lg; |
| 3318 | |
| 3319 | BUG_ON(lg == NULL); |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 3320 | ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc; |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3321 | mb_debug(sb, "goal %u blocks for locality group\n", ac->ac_g_ex.fe_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | /* |
| 3325 | * Normalization means making request better in terms of |
| 3326 | * size and alignment |
| 3327 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3328 | static noinline_for_stack void |
| 3329 | ext4_mb_normalize_request(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3330 | struct ext4_allocation_request *ar) |
| 3331 | { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3332 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3333 | int bsbits, max; |
| 3334 | ext4_lblk_t end; |
Curt Wohlgemuth | 1592d2c | 2012-02-20 17:53:03 -0500 | [diff] [blame] | 3335 | loff_t size, start_off; |
| 3336 | loff_t orig_size __maybe_unused; |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 3337 | ext4_lblk_t start; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3338 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3339 | struct ext4_prealloc_space *pa; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3340 | |
| 3341 | /* do normalize only data requests, metadata requests |
| 3342 | do not need preallocation */ |
| 3343 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 3344 | return; |
| 3345 | |
| 3346 | /* sometime caller may want exact blocks */ |
| 3347 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 3348 | return; |
| 3349 | |
| 3350 | /* caller may indicate that preallocation isn't |
| 3351 | * required (it's a tail, for example) */ |
| 3352 | if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) |
| 3353 | return; |
| 3354 | |
| 3355 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) { |
| 3356 | ext4_mb_normalize_group_request(ac); |
| 3357 | return ; |
| 3358 | } |
| 3359 | |
| 3360 | bsbits = ac->ac_sb->s_blocksize_bits; |
| 3361 | |
| 3362 | /* first, let's learn actual file size |
| 3363 | * given current request is allocated */ |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3364 | 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] | 3365 | size = size << bsbits; |
| 3366 | if (size < i_size_read(ac->ac_inode)) |
| 3367 | size = i_size_read(ac->ac_inode); |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 3368 | orig_size = size; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3369 | |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3370 | /* max size of free chunks */ |
| 3371 | max = 2 << bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3372 | |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3373 | #define NRL_CHECK_SIZE(req, size, max, chunk_size) \ |
| 3374 | (req <= (size) || max <= (chunk_size)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3375 | |
| 3376 | /* first, try to predict filesize */ |
| 3377 | /* XXX: should this table be tunable? */ |
| 3378 | start_off = 0; |
| 3379 | if (size <= 16 * 1024) { |
| 3380 | size = 16 * 1024; |
| 3381 | } else if (size <= 32 * 1024) { |
| 3382 | size = 32 * 1024; |
| 3383 | } else if (size <= 64 * 1024) { |
| 3384 | size = 64 * 1024; |
| 3385 | } else if (size <= 128 * 1024) { |
| 3386 | size = 128 * 1024; |
| 3387 | } else if (size <= 256 * 1024) { |
| 3388 | size = 256 * 1024; |
| 3389 | } else if (size <= 512 * 1024) { |
| 3390 | size = 512 * 1024; |
| 3391 | } else if (size <= 1024 * 1024) { |
| 3392 | size = 1024 * 1024; |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3393 | } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3394 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3395 | (21 - bsbits)) << 21; |
| 3396 | size = 2 * 1024 * 1024; |
| 3397 | } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3398 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
| 3399 | (22 - bsbits)) << 22; |
| 3400 | size = 4 * 1024 * 1024; |
| 3401 | } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len, |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3402 | (8<<20)>>bsbits, max, 8 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3403 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
| 3404 | (23 - bsbits)) << 23; |
| 3405 | size = 8 * 1024 * 1024; |
| 3406 | } else { |
Xiaoguang Wang | b27b153 | 2014-07-27 22:26:36 -0400 | [diff] [blame] | 3407 | start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits; |
| 3408 | size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb), |
| 3409 | ac->ac_o_ex.fe_len) << bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3410 | } |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 3411 | size = size >> bsbits; |
| 3412 | start = start_off >> bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3413 | |
| 3414 | /* don't cover already allocated blocks in selected range */ |
| 3415 | if (ar->pleft && start <= ar->lleft) { |
| 3416 | size -= ar->lleft + 1 - start; |
| 3417 | start = ar->lleft + 1; |
| 3418 | } |
| 3419 | if (ar->pright && start + size - 1 >= ar->lright) |
| 3420 | size -= start + size - ar->lright; |
| 3421 | |
Jan Kara | cd648b8 | 2017-01-27 14:34:30 -0500 | [diff] [blame] | 3422 | /* |
| 3423 | * Trim allocation request for filesystems with artificially small |
| 3424 | * groups. |
| 3425 | */ |
| 3426 | if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) |
| 3427 | size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb); |
| 3428 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3429 | end = start + size; |
| 3430 | |
| 3431 | /* check we don't cross already preallocated blocks */ |
| 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) { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3434 | ext4_lblk_t pa_end; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3435 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3436 | if (pa->pa_deleted) |
| 3437 | continue; |
| 3438 | spin_lock(&pa->pa_lock); |
| 3439 | if (pa->pa_deleted) { |
| 3440 | spin_unlock(&pa->pa_lock); |
| 3441 | continue; |
| 3442 | } |
| 3443 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3444 | pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb), |
| 3445 | pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3446 | |
| 3447 | /* PA must not overlap original request */ |
| 3448 | BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end || |
| 3449 | ac->ac_o_ex.fe_logical < pa->pa_lstart)); |
| 3450 | |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 3451 | /* skip PAs this normalized request doesn't overlap with */ |
| 3452 | if (pa->pa_lstart >= end || pa_end <= start) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3453 | spin_unlock(&pa->pa_lock); |
| 3454 | continue; |
| 3455 | } |
| 3456 | BUG_ON(pa->pa_lstart <= start && pa_end >= end); |
| 3457 | |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 3458 | /* adjust start or end to be adjacent to this pa */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3459 | if (pa_end <= ac->ac_o_ex.fe_logical) { |
| 3460 | BUG_ON(pa_end < start); |
| 3461 | start = pa_end; |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 3462 | } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3463 | BUG_ON(pa->pa_lstart > end); |
| 3464 | end = pa->pa_lstart; |
| 3465 | } |
| 3466 | spin_unlock(&pa->pa_lock); |
| 3467 | } |
| 3468 | rcu_read_unlock(); |
| 3469 | size = end - start; |
| 3470 | |
| 3471 | /* XXX: extra loop to check we really don't overlap preallocations */ |
| 3472 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3473 | 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] | 3474 | ext4_lblk_t pa_end; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3475 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3476 | spin_lock(&pa->pa_lock); |
| 3477 | if (pa->pa_deleted == 0) { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3478 | pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb), |
| 3479 | pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3480 | BUG_ON(!(start >= pa_end || end <= pa->pa_lstart)); |
| 3481 | } |
| 3482 | spin_unlock(&pa->pa_lock); |
| 3483 | } |
| 3484 | rcu_read_unlock(); |
| 3485 | |
| 3486 | if (start + size <= ac->ac_o_ex.fe_logical && |
| 3487 | start > ac->ac_o_ex.fe_logical) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3488 | ext4_msg(ac->ac_sb, KERN_ERR, |
| 3489 | "start %lu, size %lu, fe_logical %lu", |
| 3490 | (unsigned long) start, (unsigned long) size, |
| 3491 | (unsigned long) ac->ac_o_ex.fe_logical); |
Dmitry Monakhov | dfe076c | 2014-10-01 22:26:17 -0400 | [diff] [blame] | 3492 | BUG(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3493 | } |
Maurizio Lombardi | b5b6077 | 2014-05-27 12:48:56 -0400 | [diff] [blame] | 3494 | BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3495 | |
| 3496 | /* now prepare goal request */ |
| 3497 | |
| 3498 | /* XXX: is it better to align blocks WRT to logical |
| 3499 | * placement or satisfy big request as is */ |
| 3500 | ac->ac_g_ex.fe_logical = start; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3501 | ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3502 | |
| 3503 | /* define goal start in order to merge */ |
| 3504 | if (ar->pright && (ar->lright == (start + size))) { |
| 3505 | /* merge to the right */ |
| 3506 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size, |
| 3507 | &ac->ac_f_ex.fe_group, |
| 3508 | &ac->ac_f_ex.fe_start); |
| 3509 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; |
| 3510 | } |
| 3511 | if (ar->pleft && (ar->lleft + 1 == start)) { |
| 3512 | /* merge to the left */ |
| 3513 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1, |
| 3514 | &ac->ac_f_ex.fe_group, |
| 3515 | &ac->ac_f_ex.fe_start); |
| 3516 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; |
| 3517 | } |
| 3518 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3519 | mb_debug(ac->ac_sb, "goal: %lld(was %lld) blocks at %u\n", size, |
| 3520 | orig_size, start); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | static void ext4_mb_collect_stats(struct ext4_allocation_context *ac) |
| 3524 | { |
| 3525 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 3526 | |
| 3527 | if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) { |
| 3528 | atomic_inc(&sbi->s_bal_reqs); |
| 3529 | atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated); |
Curt Wohlgemuth | 291dae4 | 2010-05-16 16:00:00 -0400 | [diff] [blame] | 3530 | 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] | 3531 | atomic_inc(&sbi->s_bal_success); |
| 3532 | atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned); |
| 3533 | if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start && |
| 3534 | ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group) |
| 3535 | atomic_inc(&sbi->s_bal_goals); |
| 3536 | if (ac->ac_found > sbi->s_mb_max_to_scan) |
| 3537 | atomic_inc(&sbi->s_bal_breaks); |
| 3538 | } |
| 3539 | |
Theodore Ts'o | 296c355 | 2009-09-30 00:32:42 -0400 | [diff] [blame] | 3540 | if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) |
| 3541 | trace_ext4_mballoc_alloc(ac); |
| 3542 | else |
| 3543 | trace_ext4_mballoc_prealloc(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | /* |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 3547 | * Called on failure; free up any blocks from the inode PA for this |
| 3548 | * context. We don't need this for MB_GROUP_PA because we only change |
| 3549 | * pa_free in ext4_mb_release_context(), but on failure, we've already |
| 3550 | * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed. |
| 3551 | */ |
| 3552 | static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac) |
| 3553 | { |
| 3554 | struct ext4_prealloc_space *pa = ac->ac_pa; |
Theodore Ts'o | 86f0afd | 2014-07-30 22:17:17 -0400 | [diff] [blame] | 3555 | struct ext4_buddy e4b; |
| 3556 | int err; |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 3557 | |
Theodore Ts'o | 86f0afd | 2014-07-30 22:17:17 -0400 | [diff] [blame] | 3558 | if (pa == NULL) { |
Theodore Ts'o | c99d1e6 | 2014-08-23 17:47:28 -0400 | [diff] [blame] | 3559 | if (ac->ac_f_ex.fe_len == 0) |
| 3560 | return; |
Theodore Ts'o | 86f0afd | 2014-07-30 22:17:17 -0400 | [diff] [blame] | 3561 | err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b); |
| 3562 | if (err) { |
| 3563 | /* |
| 3564 | * This should never happen since we pin the |
| 3565 | * pages in the ext4_allocation_context so |
| 3566 | * ext4_mb_load_buddy() should never fail. |
| 3567 | */ |
| 3568 | WARN(1, "mb_load_buddy failed (%d)", err); |
| 3569 | return; |
| 3570 | } |
| 3571 | ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group); |
| 3572 | mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start, |
| 3573 | ac->ac_f_ex.fe_len); |
| 3574 | 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] | 3575 | ext4_mb_unload_buddy(&e4b); |
Theodore Ts'o | 86f0afd | 2014-07-30 22:17:17 -0400 | [diff] [blame] | 3576 | return; |
| 3577 | } |
| 3578 | if (pa->pa_type == MB_INODE_PA) |
Zheng Liu | 400db9d | 2012-05-28 17:53:53 -0400 | [diff] [blame] | 3579 | pa->pa_free += ac->ac_b_ex.fe_len; |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3583 | * use blocks preallocated to inode |
| 3584 | */ |
| 3585 | static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac, |
| 3586 | struct ext4_prealloc_space *pa) |
| 3587 | { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3588 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3589 | ext4_fsblk_t start; |
| 3590 | ext4_fsblk_t end; |
| 3591 | int len; |
| 3592 | |
| 3593 | /* found preallocated blocks, use them */ |
| 3594 | 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] | 3595 | end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len), |
| 3596 | start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len)); |
| 3597 | len = EXT4_NUM_B2C(sbi, end - start); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3598 | ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group, |
| 3599 | &ac->ac_b_ex.fe_start); |
| 3600 | ac->ac_b_ex.fe_len = len; |
| 3601 | ac->ac_status = AC_STATUS_FOUND; |
| 3602 | ac->ac_pa = pa; |
| 3603 | |
| 3604 | BUG_ON(start < pa->pa_pstart); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3605 | BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3606 | BUG_ON(pa->pa_free < len); |
| 3607 | pa->pa_free -= len; |
| 3608 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3609 | mb_debug(ac->ac_sb, "use %llu/%d from inode pa %p\n", start, len, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | /* |
| 3613 | * use blocks preallocated to locality group |
| 3614 | */ |
| 3615 | static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac, |
| 3616 | struct ext4_prealloc_space *pa) |
| 3617 | { |
Aneesh Kumar K.V | 03cddb8 | 2008-06-05 20:59:29 -0400 | [diff] [blame] | 3618 | unsigned int len = ac->ac_o_ex.fe_len; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3619 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3620 | ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart, |
| 3621 | &ac->ac_b_ex.fe_group, |
| 3622 | &ac->ac_b_ex.fe_start); |
| 3623 | ac->ac_b_ex.fe_len = len; |
| 3624 | ac->ac_status = AC_STATUS_FOUND; |
| 3625 | ac->ac_pa = pa; |
| 3626 | |
| 3627 | /* 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] | 3628 | * possible race when the group is being loaded concurrently |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3629 | * instead we correct pa later, after blocks are marked |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 3630 | * in on-disk bitmap -- see ext4_mb_release_context() |
| 3631 | * Other CPUs are prevented from allocating from this pa by lg_mutex |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3632 | */ |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3633 | mb_debug(ac->ac_sb, "use %u/%u from group pa %p\n", |
| 3634 | pa->pa_lstart-len, len, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | /* |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3638 | * Return the prealloc space that have minimal distance |
| 3639 | * from the goal block. @cpa is the prealloc |
| 3640 | * space that is having currently known minimal distance |
| 3641 | * from the goal block. |
| 3642 | */ |
| 3643 | static struct ext4_prealloc_space * |
| 3644 | ext4_mb_check_group_pa(ext4_fsblk_t goal_block, |
| 3645 | struct ext4_prealloc_space *pa, |
| 3646 | struct ext4_prealloc_space *cpa) |
| 3647 | { |
| 3648 | ext4_fsblk_t cur_distance, new_distance; |
| 3649 | |
| 3650 | if (cpa == NULL) { |
| 3651 | atomic_inc(&pa->pa_count); |
| 3652 | return pa; |
| 3653 | } |
Andrew Morton | 79211c8 | 2015-11-09 14:58:13 -0800 | [diff] [blame] | 3654 | cur_distance = abs(goal_block - cpa->pa_pstart); |
| 3655 | new_distance = abs(goal_block - pa->pa_pstart); |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3656 | |
Coly Li | 5a54b2f | 2011-02-24 14:10:05 -0500 | [diff] [blame] | 3657 | if (cur_distance <= new_distance) |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3658 | return cpa; |
| 3659 | |
| 3660 | /* drop the previous reference */ |
| 3661 | atomic_dec(&cpa->pa_count); |
| 3662 | atomic_inc(&pa->pa_count); |
| 3663 | return pa; |
| 3664 | } |
| 3665 | |
| 3666 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3667 | * search goal blocks in preallocated space |
| 3668 | */ |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 3669 | static noinline_for_stack bool |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3670 | ext4_mb_use_preallocated(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3671 | { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3672 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3673 | int order, i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3674 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
| 3675 | struct ext4_locality_group *lg; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3676 | struct ext4_prealloc_space *pa, *cpa = NULL; |
| 3677 | ext4_fsblk_t goal_block; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3678 | |
| 3679 | /* only data can be preallocated */ |
| 3680 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 3681 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3682 | |
| 3683 | /* first, try per-file preallocation */ |
| 3684 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3685 | 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] | 3686 | |
| 3687 | /* all fields in this condition don't change, |
| 3688 | * so we can skip locking for them */ |
| 3689 | if (ac->ac_o_ex.fe_logical < pa->pa_lstart || |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3690 | ac->ac_o_ex.fe_logical >= (pa->pa_lstart + |
| 3691 | EXT4_C2B(sbi, pa->pa_len))) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3692 | continue; |
| 3693 | |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 3694 | /* non-extent files can't have physical blocks past 2^32 */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 3695 | if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) && |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3696 | (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) > |
| 3697 | EXT4_MAX_BLOCK_FILE_PHYS)) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 3698 | continue; |
| 3699 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3700 | /* found preallocated blocks, use them */ |
| 3701 | spin_lock(&pa->pa_lock); |
| 3702 | if (pa->pa_deleted == 0 && pa->pa_free) { |
| 3703 | atomic_inc(&pa->pa_count); |
| 3704 | ext4_mb_use_inode_pa(ac, pa); |
| 3705 | spin_unlock(&pa->pa_lock); |
| 3706 | ac->ac_criteria = 10; |
| 3707 | rcu_read_unlock(); |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 3708 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3709 | } |
| 3710 | spin_unlock(&pa->pa_lock); |
| 3711 | } |
| 3712 | rcu_read_unlock(); |
| 3713 | |
| 3714 | /* can we use group allocation? */ |
| 3715 | if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)) |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 3716 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3717 | |
| 3718 | /* inode may have no locality group for some reason */ |
| 3719 | lg = ac->ac_lg; |
| 3720 | if (lg == NULL) |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 3721 | return false; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3722 | order = fls(ac->ac_o_ex.fe_len) - 1; |
| 3723 | if (order > PREALLOC_TB_SIZE - 1) |
| 3724 | /* The max size of hash table is PREALLOC_TB_SIZE */ |
| 3725 | order = PREALLOC_TB_SIZE - 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3726 | |
Akinobu Mita | bda00de | 2010-03-03 23:53:25 -0500 | [diff] [blame] | 3727 | 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] | 3728 | /* |
| 3729 | * search for the prealloc space that is having |
| 3730 | * minimal distance from the goal block. |
| 3731 | */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3732 | for (i = order; i < PREALLOC_TB_SIZE; i++) { |
| 3733 | rcu_read_lock(); |
| 3734 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i], |
| 3735 | pa_inode_list) { |
| 3736 | spin_lock(&pa->pa_lock); |
| 3737 | if (pa->pa_deleted == 0 && |
| 3738 | pa->pa_free >= ac->ac_o_ex.fe_len) { |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3739 | |
| 3740 | cpa = ext4_mb_check_group_pa(goal_block, |
| 3741 | pa, cpa); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3742 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3743 | spin_unlock(&pa->pa_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3744 | } |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3745 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3746 | } |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3747 | if (cpa) { |
| 3748 | ext4_mb_use_group_pa(ac, cpa); |
| 3749 | ac->ac_criteria = 20; |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 3750 | return true; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 3751 | } |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 3752 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | /* |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 3756 | * the function goes through all block freed in the group |
| 3757 | * but not yet committed and marks them used in in-core bitmap. |
| 3758 | * buddy must be generated from this bitmap |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3759 | * Need to be called with the ext4 group lock held |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 3760 | */ |
| 3761 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, |
| 3762 | ext4_group_t group) |
| 3763 | { |
| 3764 | struct rb_node *n; |
| 3765 | struct ext4_group_info *grp; |
| 3766 | struct ext4_free_data *entry; |
| 3767 | |
| 3768 | grp = ext4_get_group_info(sb, group); |
| 3769 | n = rb_first(&(grp->bb_free_root)); |
| 3770 | |
| 3771 | while (n) { |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3772 | entry = rb_entry(n, struct ext4_free_data, efd_node); |
| 3773 | 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] | 3774 | n = rb_next(n); |
| 3775 | } |
| 3776 | return; |
| 3777 | } |
| 3778 | |
| 3779 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3780 | * the function goes through all preallocation in this group and marks them |
| 3781 | * 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] | 3782 | * Need to be called with ext4 group lock held |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3783 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 3784 | static noinline_for_stack |
| 3785 | void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3786 | ext4_group_t group) |
| 3787 | { |
| 3788 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 3789 | struct ext4_prealloc_space *pa; |
| 3790 | struct list_head *cur; |
| 3791 | ext4_group_t groupnr; |
| 3792 | ext4_grpblk_t start; |
| 3793 | int preallocated = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3794 | int len; |
| 3795 | |
| 3796 | /* all form of preallocation discards first load group, |
| 3797 | * so the only competing code is preallocation use. |
| 3798 | * we don't need any locking here |
| 3799 | * notice we do NOT ignore preallocations with pa_deleted |
| 3800 | * otherwise we could leave used blocks available for |
| 3801 | * allocation in buddy when concurrent ext4_mb_put_pa() |
| 3802 | * is dropping preallocation |
| 3803 | */ |
| 3804 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 3805 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 3806 | spin_lock(&pa->pa_lock); |
| 3807 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, |
| 3808 | &groupnr, &start); |
| 3809 | len = pa->pa_len; |
| 3810 | spin_unlock(&pa->pa_lock); |
| 3811 | if (unlikely(len == 0)) |
| 3812 | continue; |
| 3813 | BUG_ON(groupnr != group); |
Yongqiang Yang | c3e94d1 | 2011-07-26 22:05:53 -0400 | [diff] [blame] | 3814 | ext4_set_bits(bitmap, start, len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3815 | preallocated += len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3816 | } |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3817 | mb_debug(sb, "preallocated %d for group %u\n", preallocated, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3818 | } |
| 3819 | |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 3820 | static void ext4_mb_mark_pa_deleted(struct super_block *sb, |
| 3821 | struct ext4_prealloc_space *pa) |
| 3822 | { |
| 3823 | struct ext4_inode_info *ei; |
| 3824 | |
| 3825 | if (pa->pa_deleted) { |
| 3826 | ext4_warning(sb, "deleted pa, type:%d, pblk:%llu, lblk:%u, len:%d\n", |
| 3827 | pa->pa_type, pa->pa_pstart, pa->pa_lstart, |
| 3828 | pa->pa_len); |
| 3829 | return; |
| 3830 | } |
| 3831 | |
| 3832 | pa->pa_deleted = 1; |
| 3833 | |
| 3834 | if (pa->pa_type == MB_INODE_PA) { |
| 3835 | ei = EXT4_I(pa->pa_inode); |
| 3836 | atomic_dec(&ei->i_prealloc_active); |
| 3837 | } |
| 3838 | } |
| 3839 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3840 | static void ext4_mb_pa_callback(struct rcu_head *head) |
| 3841 | { |
| 3842 | struct ext4_prealloc_space *pa; |
| 3843 | pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu); |
Junho Ryu | 4e8d213 | 2013-12-03 18:10:28 -0500 | [diff] [blame] | 3844 | |
| 3845 | BUG_ON(atomic_read(&pa->pa_count)); |
| 3846 | BUG_ON(pa->pa_deleted == 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3847 | kmem_cache_free(ext4_pspace_cachep, pa); |
| 3848 | } |
| 3849 | |
| 3850 | /* |
| 3851 | * drops a reference to preallocated space descriptor |
| 3852 | * if this was the last reference and the space is consumed |
| 3853 | */ |
| 3854 | static void ext4_mb_put_pa(struct ext4_allocation_context *ac, |
| 3855 | struct super_block *sb, struct ext4_prealloc_space *pa) |
| 3856 | { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 3857 | ext4_group_t grp; |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3858 | ext4_fsblk_t grp_blk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3859 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3860 | /* in this short window concurrent discard can set pa_deleted */ |
| 3861 | spin_lock(&pa->pa_lock); |
Junho Ryu | 4e8d213 | 2013-12-03 18:10:28 -0500 | [diff] [blame] | 3862 | if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) { |
| 3863 | spin_unlock(&pa->pa_lock); |
| 3864 | return; |
| 3865 | } |
| 3866 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3867 | if (pa->pa_deleted == 1) { |
| 3868 | spin_unlock(&pa->pa_lock); |
| 3869 | return; |
| 3870 | } |
| 3871 | |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 3872 | ext4_mb_mark_pa_deleted(sb, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3873 | spin_unlock(&pa->pa_lock); |
| 3874 | |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3875 | grp_blk = pa->pa_pstart; |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 3876 | /* |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3877 | * If doing group-based preallocation, pa_pstart may be in the |
| 3878 | * next group when pa is used up |
| 3879 | */ |
| 3880 | if (pa->pa_type == MB_GROUP_PA) |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 3881 | grp_blk--; |
| 3882 | |
Lukas Czerner | bd86298 | 2013-04-03 23:32:34 -0400 | [diff] [blame] | 3883 | grp = ext4_get_group_number(sb, grp_blk); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3884 | |
| 3885 | /* |
| 3886 | * possible race: |
| 3887 | * |
| 3888 | * P1 (buddy init) P2 (regular allocation) |
| 3889 | * find block B in PA |
| 3890 | * copy on-disk bitmap to buddy |
| 3891 | * mark B in on-disk bitmap |
| 3892 | * drop PA from group |
| 3893 | * mark all PAs in buddy |
| 3894 | * |
| 3895 | * thus, P1 initializes buddy with B available. to prevent this |
| 3896 | * we make "copy" and "mark all PAs" atomic and serialize "drop PA" |
| 3897 | * against that pair |
| 3898 | */ |
| 3899 | ext4_lock_group(sb, grp); |
| 3900 | list_del(&pa->pa_group_list); |
| 3901 | ext4_unlock_group(sb, grp); |
| 3902 | |
| 3903 | spin_lock(pa->pa_obj_lock); |
| 3904 | list_del_rcu(&pa->pa_inode_list); |
| 3905 | spin_unlock(pa->pa_obj_lock); |
| 3906 | |
| 3907 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 3908 | } |
| 3909 | |
| 3910 | /* |
| 3911 | * creates new preallocated space for given inode |
| 3912 | */ |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 3913 | static noinline_for_stack void |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3914 | ext4_mb_new_inode_pa(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3915 | { |
| 3916 | struct super_block *sb = ac->ac_sb; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3917 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3918 | struct ext4_prealloc_space *pa; |
| 3919 | struct ext4_group_info *grp; |
| 3920 | struct ext4_inode_info *ei; |
| 3921 | |
| 3922 | /* preallocate only when found space is larger then requested */ |
| 3923 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); |
| 3924 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 3925 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 3926 | BUG_ON(ac->ac_pa == NULL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3927 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 3928 | pa = ac->ac_pa; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3929 | |
| 3930 | if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) { |
| 3931 | int winl; |
| 3932 | int wins; |
| 3933 | int win; |
| 3934 | int offs; |
| 3935 | |
| 3936 | /* we can't allocate as much as normalizer wants. |
| 3937 | * so, found space must get proper lstart |
| 3938 | * to cover original request */ |
| 3939 | BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical); |
| 3940 | BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len); |
| 3941 | |
| 3942 | /* we're limited by original request in that |
| 3943 | * logical block must be covered any way |
| 3944 | * winl is window we can move our chunk within */ |
| 3945 | winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical; |
| 3946 | |
| 3947 | /* also, we should cover whole original request */ |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3948 | 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] | 3949 | |
| 3950 | /* the smallest one defines real window */ |
| 3951 | win = min(winl, wins); |
| 3952 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3953 | offs = ac->ac_o_ex.fe_logical % |
| 3954 | EXT4_C2B(sbi, ac->ac_b_ex.fe_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3955 | if (offs && offs < win) |
| 3956 | win = offs; |
| 3957 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3958 | ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - |
Lukas Czerner | 810da24 | 2013-03-02 17:18:58 -0500 | [diff] [blame] | 3959 | EXT4_NUM_B2C(sbi, win); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3960 | BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical); |
| 3961 | BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len); |
| 3962 | } |
| 3963 | |
| 3964 | /* preallocation can change ac_b_ex, thus we store actually |
| 3965 | * allocated blocks for history */ |
| 3966 | ac->ac_f_ex = ac->ac_b_ex; |
| 3967 | |
| 3968 | pa->pa_lstart = ac->ac_b_ex.fe_logical; |
| 3969 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 3970 | pa->pa_len = ac->ac_b_ex.fe_len; |
| 3971 | pa->pa_free = pa->pa_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3972 | spin_lock_init(&pa->pa_lock); |
Aneesh Kumar K.V | d794bf8 | 2009-02-14 10:31:16 -0500 | [diff] [blame] | 3973 | INIT_LIST_HEAD(&pa->pa_inode_list); |
| 3974 | INIT_LIST_HEAD(&pa->pa_group_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3975 | pa->pa_deleted = 0; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 3976 | pa->pa_type = MB_INODE_PA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3977 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3978 | mb_debug(sb, "new inode pa %p: %llu/%d for %u\n", pa, pa->pa_pstart, |
| 3979 | pa->pa_len, pa->pa_lstart); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 3980 | trace_ext4_mb_new_inode_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3981 | |
| 3982 | ext4_mb_use_inode_pa(ac, pa); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3983 | atomic_add(pa->pa_free, &sbi->s_mb_preallocated); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3984 | |
| 3985 | ei = EXT4_I(ac->ac_inode); |
| 3986 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); |
| 3987 | |
| 3988 | pa->pa_obj_lock = &ei->i_prealloc_lock; |
| 3989 | pa->pa_inode = ac->ac_inode; |
| 3990 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3991 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3992 | |
| 3993 | spin_lock(pa->pa_obj_lock); |
| 3994 | list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list); |
| 3995 | spin_unlock(pa->pa_obj_lock); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 3996 | atomic_inc(&ei->i_prealloc_active); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | /* |
| 4000 | * creates new preallocated space for locality group inodes belongs to |
| 4001 | */ |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4002 | static noinline_for_stack void |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4003 | ext4_mb_new_group_pa(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4004 | { |
| 4005 | struct super_block *sb = ac->ac_sb; |
| 4006 | struct ext4_locality_group *lg; |
| 4007 | struct ext4_prealloc_space *pa; |
| 4008 | struct ext4_group_info *grp; |
| 4009 | |
| 4010 | /* preallocate only when found space is larger then requested */ |
| 4011 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); |
| 4012 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 4013 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4014 | BUG_ON(ac->ac_pa == NULL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4015 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4016 | pa = ac->ac_pa; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4017 | |
| 4018 | /* preallocation can change ac_b_ex, thus we store actually |
| 4019 | * allocated blocks for history */ |
| 4020 | ac->ac_f_ex = ac->ac_b_ex; |
| 4021 | |
| 4022 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 4023 | pa->pa_lstart = pa->pa_pstart; |
| 4024 | pa->pa_len = ac->ac_b_ex.fe_len; |
| 4025 | pa->pa_free = pa->pa_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4026 | spin_lock_init(&pa->pa_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4027 | INIT_LIST_HEAD(&pa->pa_inode_list); |
Aneesh Kumar K.V | d794bf8 | 2009-02-14 10:31:16 -0500 | [diff] [blame] | 4028 | INIT_LIST_HEAD(&pa->pa_group_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4029 | pa->pa_deleted = 0; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4030 | pa->pa_type = MB_GROUP_PA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4031 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4032 | mb_debug(sb, "new group pa %p: %llu/%d for %u\n", pa, pa->pa_pstart, |
| 4033 | pa->pa_len, pa->pa_lstart); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4034 | trace_ext4_mb_new_group_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4035 | |
| 4036 | ext4_mb_use_group_pa(ac, pa); |
| 4037 | atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated); |
| 4038 | |
| 4039 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); |
| 4040 | lg = ac->ac_lg; |
| 4041 | BUG_ON(lg == NULL); |
| 4042 | |
| 4043 | pa->pa_obj_lock = &lg->lg_prealloc_lock; |
| 4044 | pa->pa_inode = NULL; |
| 4045 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4046 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4047 | |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4048 | /* |
| 4049 | * We will later add the new pa to the right bucket |
| 4050 | * after updating the pa_free in ext4_mb_release_context |
| 4051 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4052 | } |
| 4053 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4054 | static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4055 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4056 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4057 | ext4_mb_new_group_pa(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4058 | else |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4059 | ext4_mb_new_inode_pa(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | /* |
| 4063 | * finds all unused blocks in on-disk bitmap, frees them in |
| 4064 | * in-core bitmap and buddy. |
| 4065 | * @pa must be unlinked from inode and group lists, so that |
| 4066 | * nobody else can find/use it. |
| 4067 | * the caller MUST hold group/inode locks. |
| 4068 | * TODO: optimize the case when there are no in-core structures yet |
| 4069 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4070 | static noinline_for_stack int |
| 4071 | 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] | 4072 | struct ext4_prealloc_space *pa) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4073 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4074 | struct super_block *sb = e4b->bd_sb; |
| 4075 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4076 | unsigned int end; |
| 4077 | unsigned int next; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4078 | ext4_group_t group; |
| 4079 | ext4_grpblk_t bit; |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 4080 | unsigned long long grp_blk_start; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4081 | int free = 0; |
| 4082 | |
| 4083 | BUG_ON(pa->pa_deleted == 0); |
| 4084 | 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] | 4085 | grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4086 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
| 4087 | end = bit + pa->pa_len; |
| 4088 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4089 | while (bit < end) { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 4090 | bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4091 | if (bit >= end) |
| 4092 | break; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 4093 | next = mb_find_next_bit(bitmap_bh->b_data, end, bit); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4094 | mb_debug(sb, "free preallocated %u/%u in group %u\n", |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 4095 | (unsigned) ext4_group_first_block_no(sb, group) + bit, |
| 4096 | (unsigned) next - bit, (unsigned) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4097 | free += next - bit; |
| 4098 | |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4099 | trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4100 | trace_ext4_mb_release_inode_pa(pa, (grp_blk_start + |
| 4101 | EXT4_C2B(sbi, bit)), |
Lukas Czerner | a9c667f | 2011-06-06 09:51:52 -0400 | [diff] [blame] | 4102 | next - bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4103 | mb_free_blocks(pa->pa_inode, e4b, bit, next - bit); |
| 4104 | bit = next + 1; |
| 4105 | } |
| 4106 | if (free != pa->pa_free) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 4107 | ext4_msg(e4b->bd_sb, KERN_CRIT, |
Ritesh Harjani | 36bad42 | 2020-05-10 11:54:44 +0530 | [diff] [blame] | 4108 | "pa %p: logic %lu, phys. %lu, len %d", |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 4109 | pa, (unsigned long) pa->pa_lstart, |
| 4110 | (unsigned long) pa->pa_pstart, |
Ritesh Harjani | 36bad42 | 2020-05-10 11:54:44 +0530 | [diff] [blame] | 4111 | pa->pa_len); |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 4112 | 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] | 4113 | free, pa->pa_free); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 4114 | /* |
| 4115 | * pa is already deleted so we use the value obtained |
| 4116 | * from the bitmap and continue. |
| 4117 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4118 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4119 | atomic_add(free, &sbi->s_mb_discarded); |
| 4120 | |
zhong jiang | 863c37f | 2018-08-04 17:34:07 -0400 | [diff] [blame] | 4121 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4122 | } |
| 4123 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4124 | static noinline_for_stack int |
| 4125 | ext4_mb_release_group_pa(struct ext4_buddy *e4b, |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4126 | struct ext4_prealloc_space *pa) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4127 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4128 | struct super_block *sb = e4b->bd_sb; |
| 4129 | ext4_group_t group; |
| 4130 | ext4_grpblk_t bit; |
| 4131 | |
Yongqiang Yang | 60e07cf | 2011-12-18 15:49:54 -0500 | [diff] [blame] | 4132 | trace_ext4_mb_release_group_pa(sb, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4133 | BUG_ON(pa->pa_deleted == 0); |
| 4134 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); |
| 4135 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
| 4136 | mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len); |
| 4137 | atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded); |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4138 | trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4139 | |
| 4140 | return 0; |
| 4141 | } |
| 4142 | |
| 4143 | /* |
| 4144 | * releases all preallocations in given group |
| 4145 | * |
| 4146 | * first, we need to decide discard policy: |
| 4147 | * - when do we discard |
| 4148 | * 1) ENOSPC |
| 4149 | * - how many do we discard |
| 4150 | * 1) how many requested |
| 4151 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4152 | static noinline_for_stack int |
| 4153 | ext4_mb_discard_group_preallocations(struct super_block *sb, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4154 | ext4_group_t group, int needed) |
| 4155 | { |
| 4156 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 4157 | struct buffer_head *bitmap_bh = NULL; |
| 4158 | struct ext4_prealloc_space *pa, *tmp; |
| 4159 | struct list_head list; |
| 4160 | struct ext4_buddy e4b; |
| 4161 | int err; |
| 4162 | int busy = 0; |
| 4163 | int free = 0; |
| 4164 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4165 | mb_debug(sb, "discard preallocation for group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4166 | if (list_empty(&grp->bb_prealloc_list)) |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4167 | goto out_dbg; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4168 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4169 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 4170 | if (IS_ERR(bitmap_bh)) { |
| 4171 | err = PTR_ERR(bitmap_bh); |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 4172 | ext4_error_err(sb, -err, |
| 4173 | "Error %d reading block bitmap for %u", |
| 4174 | err, group); |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4175 | goto out_dbg; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | err = ext4_mb_load_buddy(sb, group, &e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4179 | if (err) { |
Konstantin Khlebnikov | 9651e6b | 2017-05-21 22:35:23 -0400 | [diff] [blame] | 4180 | ext4_warning(sb, "Error %d loading buddy information for %u", |
| 4181 | err, group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4182 | put_bh(bitmap_bh); |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4183 | goto out_dbg; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4184 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4185 | |
| 4186 | if (needed == 0) |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 4187 | needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4188 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4189 | INIT_LIST_HEAD(&list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4190 | repeat: |
| 4191 | ext4_lock_group(sb, group); |
| 4192 | list_for_each_entry_safe(pa, tmp, |
| 4193 | &grp->bb_prealloc_list, pa_group_list) { |
| 4194 | spin_lock(&pa->pa_lock); |
| 4195 | if (atomic_read(&pa->pa_count)) { |
| 4196 | spin_unlock(&pa->pa_lock); |
| 4197 | busy = 1; |
| 4198 | continue; |
| 4199 | } |
| 4200 | if (pa->pa_deleted) { |
| 4201 | spin_unlock(&pa->pa_lock); |
| 4202 | continue; |
| 4203 | } |
| 4204 | |
| 4205 | /* seems this one can be freed ... */ |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4206 | ext4_mb_mark_pa_deleted(sb, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4207 | |
Ye Bin | 70022da | 2020-09-16 19:38:59 +0800 | [diff] [blame^] | 4208 | if (!free) |
| 4209 | this_cpu_inc(discard_pa_seq); |
| 4210 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4211 | /* we can trust pa_free ... */ |
| 4212 | free += pa->pa_free; |
| 4213 | |
| 4214 | spin_unlock(&pa->pa_lock); |
| 4215 | |
| 4216 | list_del(&pa->pa_group_list); |
| 4217 | list_add(&pa->u.pa_tmp_list, &list); |
| 4218 | } |
| 4219 | |
| 4220 | /* if we still need more blocks and some PAs were used, try again */ |
| 4221 | if (free < needed && busy) { |
| 4222 | busy = 0; |
| 4223 | ext4_unlock_group(sb, group); |
Lukas Czerner | bb8b20e | 2013-03-10 22:28:09 -0400 | [diff] [blame] | 4224 | cond_resched(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4225 | goto repeat; |
| 4226 | } |
| 4227 | |
| 4228 | /* found anything to free? */ |
| 4229 | if (list_empty(&list)) { |
| 4230 | BUG_ON(free != 0); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4231 | mb_debug(sb, "Someone else may have freed PA for this group %u\n", |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4232 | group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4233 | goto out; |
| 4234 | } |
| 4235 | |
| 4236 | /* now free all selected PAs */ |
| 4237 | list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { |
| 4238 | |
| 4239 | /* remove from object (inode or locality group) */ |
| 4240 | spin_lock(pa->pa_obj_lock); |
| 4241 | list_del_rcu(&pa->pa_inode_list); |
| 4242 | spin_unlock(pa->pa_obj_lock); |
| 4243 | |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4244 | if (pa->pa_type == MB_GROUP_PA) |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4245 | ext4_mb_release_group_pa(&e4b, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4246 | else |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4247 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4248 | |
| 4249 | list_del(&pa->u.pa_tmp_list); |
| 4250 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4251 | } |
| 4252 | |
| 4253 | out: |
| 4254 | ext4_unlock_group(sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 4255 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4256 | put_bh(bitmap_bh); |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4257 | out_dbg: |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4258 | mb_debug(sb, "discarded (%d) blocks preallocated for group %u bb_free (%d)\n", |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4259 | free, group, grp->bb_free); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4260 | return free; |
| 4261 | } |
| 4262 | |
| 4263 | /* |
| 4264 | * releases all non-used preallocated blocks for given inode |
| 4265 | * |
| 4266 | * It's important to discard preallocations under i_data_sem |
| 4267 | * We don't want another block to be served from the prealloc |
| 4268 | * space when we are discarding the inode prealloc space. |
| 4269 | * |
| 4270 | * FIXME!! Make sure it is valid at all the call sites |
| 4271 | */ |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4272 | void ext4_discard_preallocations(struct inode *inode, unsigned int needed) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4273 | { |
| 4274 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 4275 | struct super_block *sb = inode->i_sb; |
| 4276 | struct buffer_head *bitmap_bh = NULL; |
| 4277 | struct ext4_prealloc_space *pa, *tmp; |
| 4278 | ext4_group_t group = 0; |
| 4279 | struct list_head list; |
| 4280 | struct ext4_buddy e4b; |
| 4281 | int err; |
| 4282 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4283 | if (!S_ISREG(inode->i_mode)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4284 | /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/ |
| 4285 | return; |
| 4286 | } |
| 4287 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4288 | mb_debug(sb, "discard preallocation for inode %lu\n", |
| 4289 | inode->i_ino); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4290 | trace_ext4_discard_preallocations(inode, |
| 4291 | atomic_read(&ei->i_prealloc_active), needed); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4292 | |
| 4293 | INIT_LIST_HEAD(&list); |
| 4294 | |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4295 | if (needed == 0) |
| 4296 | needed = UINT_MAX; |
| 4297 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4298 | repeat: |
| 4299 | /* first, collect all pa's in the inode */ |
| 4300 | spin_lock(&ei->i_prealloc_lock); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4301 | while (!list_empty(&ei->i_prealloc_list) && needed) { |
| 4302 | pa = list_entry(ei->i_prealloc_list.prev, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4303 | struct ext4_prealloc_space, pa_inode_list); |
| 4304 | BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock); |
| 4305 | spin_lock(&pa->pa_lock); |
| 4306 | if (atomic_read(&pa->pa_count)) { |
| 4307 | /* this shouldn't happen often - nobody should |
| 4308 | * use preallocation while we're discarding it */ |
| 4309 | spin_unlock(&pa->pa_lock); |
| 4310 | spin_unlock(&ei->i_prealloc_lock); |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 4311 | ext4_msg(sb, KERN_ERR, |
| 4312 | "uh-oh! used pa while discarding"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4313 | WARN_ON(1); |
| 4314 | schedule_timeout_uninterruptible(HZ); |
| 4315 | goto repeat; |
| 4316 | |
| 4317 | } |
| 4318 | if (pa->pa_deleted == 0) { |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4319 | ext4_mb_mark_pa_deleted(sb, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4320 | spin_unlock(&pa->pa_lock); |
| 4321 | list_del_rcu(&pa->pa_inode_list); |
| 4322 | list_add(&pa->u.pa_tmp_list, &list); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4323 | needed--; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4324 | continue; |
| 4325 | } |
| 4326 | |
| 4327 | /* someone is deleting pa right now */ |
| 4328 | spin_unlock(&pa->pa_lock); |
| 4329 | spin_unlock(&ei->i_prealloc_lock); |
| 4330 | |
| 4331 | /* we have to wait here because pa_deleted |
| 4332 | * doesn't mean pa is already unlinked from |
| 4333 | * the list. as we might be called from |
| 4334 | * ->clear_inode() the inode will get freed |
| 4335 | * and concurrent thread which is unlinking |
| 4336 | * pa from inode's list may access already |
| 4337 | * freed memory, bad-bad-bad */ |
| 4338 | |
| 4339 | /* XXX: if this happens too often, we can |
| 4340 | * add a flag to force wait only in case |
| 4341 | * of ->clear_inode(), but not in case of |
| 4342 | * regular truncate */ |
| 4343 | schedule_timeout_uninterruptible(HZ); |
| 4344 | goto repeat; |
| 4345 | } |
| 4346 | spin_unlock(&ei->i_prealloc_lock); |
| 4347 | |
| 4348 | 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] | 4349 | BUG_ON(pa->pa_type != MB_INODE_PA); |
Lukas Czerner | bd86298 | 2013-04-03 23:32:34 -0400 | [diff] [blame] | 4350 | group = ext4_get_group_number(sb, pa->pa_pstart); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4351 | |
Konstantin Khlebnikov | 9651e6b | 2017-05-21 22:35:23 -0400 | [diff] [blame] | 4352 | err = ext4_mb_load_buddy_gfp(sb, group, &e4b, |
| 4353 | GFP_NOFS|__GFP_NOFAIL); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4354 | if (err) { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 4355 | ext4_error_err(sb, -err, "Error %d loading buddy information for %u", |
| 4356 | err, group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4357 | continue; |
| 4358 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4359 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4360 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 4361 | if (IS_ERR(bitmap_bh)) { |
| 4362 | err = PTR_ERR(bitmap_bh); |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 4363 | ext4_error_err(sb, -err, "Error %d reading block bitmap for %u", |
| 4364 | err, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 4365 | ext4_mb_unload_buddy(&e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4366 | continue; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | ext4_lock_group(sb, group); |
| 4370 | list_del(&pa->pa_group_list); |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4371 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4372 | ext4_unlock_group(sb, group); |
| 4373 | |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 4374 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4375 | put_bh(bitmap_bh); |
| 4376 | |
| 4377 | list_del(&pa->u.pa_tmp_list); |
| 4378 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4379 | } |
| 4380 | } |
| 4381 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4382 | static int ext4_mb_pa_alloc(struct ext4_allocation_context *ac) |
| 4383 | { |
| 4384 | struct ext4_prealloc_space *pa; |
| 4385 | |
| 4386 | BUG_ON(ext4_pspace_cachep == NULL); |
| 4387 | pa = kmem_cache_zalloc(ext4_pspace_cachep, GFP_NOFS); |
| 4388 | if (!pa) |
| 4389 | return -ENOMEM; |
| 4390 | atomic_set(&pa->pa_count, 1); |
| 4391 | ac->ac_pa = pa; |
| 4392 | return 0; |
| 4393 | } |
| 4394 | |
| 4395 | static void ext4_mb_pa_free(struct ext4_allocation_context *ac) |
| 4396 | { |
| 4397 | struct ext4_prealloc_space *pa = ac->ac_pa; |
| 4398 | |
| 4399 | BUG_ON(!pa); |
| 4400 | ac->ac_pa = NULL; |
| 4401 | WARN_ON(!atomic_dec_and_test(&pa->pa_count)); |
| 4402 | kmem_cache_free(ext4_pspace_cachep, pa); |
| 4403 | } |
| 4404 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 4405 | #ifdef CONFIG_EXT4_DEBUG |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4406 | static inline void ext4_mb_show_pa(struct super_block *sb) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4407 | { |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4408 | ext4_group_t i, ngroups; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4409 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4410 | if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED) |
Eric Sandeen | e357063 | 2010-07-27 11:56:08 -0400 | [diff] [blame] | 4411 | return; |
| 4412 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4413 | ngroups = ext4_get_groups_count(sb); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4414 | mb_debug(sb, "groups: "); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4415 | for (i = 0; i < ngroups; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4416 | struct ext4_group_info *grp = ext4_get_group_info(sb, i); |
| 4417 | struct ext4_prealloc_space *pa; |
| 4418 | ext4_grpblk_t start; |
| 4419 | struct list_head *cur; |
| 4420 | ext4_lock_group(sb, i); |
| 4421 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 4422 | pa = list_entry(cur, struct ext4_prealloc_space, |
| 4423 | pa_group_list); |
| 4424 | spin_lock(&pa->pa_lock); |
| 4425 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, |
| 4426 | NULL, &start); |
| 4427 | spin_unlock(&pa->pa_lock); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4428 | mb_debug(sb, "PA:%u:%d:%d\n", i, start, |
| 4429 | pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4430 | } |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 4431 | ext4_unlock_group(sb, i); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4432 | mb_debug(sb, "%u: %d/%d\n", i, grp->bb_free, |
| 4433 | grp->bb_fragments); |
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 | } |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4436 | |
| 4437 | static void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
| 4438 | { |
| 4439 | struct super_block *sb = ac->ac_sb; |
| 4440 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4441 | if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED) |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4442 | return; |
| 4443 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4444 | mb_debug(sb, "Can't allocate:" |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4445 | " Allocation context details:"); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4446 | mb_debug(sb, "status %u flags 0x%x", |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4447 | ac->ac_status, ac->ac_flags); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4448 | mb_debug(sb, "orig %lu/%lu/%lu@%lu, " |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4449 | "goal %lu/%lu/%lu@%lu, " |
| 4450 | "best %lu/%lu/%lu@%lu cr %d", |
| 4451 | (unsigned long)ac->ac_o_ex.fe_group, |
| 4452 | (unsigned long)ac->ac_o_ex.fe_start, |
| 4453 | (unsigned long)ac->ac_o_ex.fe_len, |
| 4454 | (unsigned long)ac->ac_o_ex.fe_logical, |
| 4455 | (unsigned long)ac->ac_g_ex.fe_group, |
| 4456 | (unsigned long)ac->ac_g_ex.fe_start, |
| 4457 | (unsigned long)ac->ac_g_ex.fe_len, |
| 4458 | (unsigned long)ac->ac_g_ex.fe_logical, |
| 4459 | (unsigned long)ac->ac_b_ex.fe_group, |
| 4460 | (unsigned long)ac->ac_b_ex.fe_start, |
| 4461 | (unsigned long)ac->ac_b_ex.fe_len, |
| 4462 | (unsigned long)ac->ac_b_ex.fe_logical, |
| 4463 | (int)ac->ac_criteria); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4464 | mb_debug(sb, "%u found", ac->ac_found); |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4465 | ext4_mb_show_pa(sb); |
| 4466 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4467 | #else |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4468 | static inline void ext4_mb_show_pa(struct super_block *sb) |
| 4469 | { |
| 4470 | return; |
| 4471 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4472 | static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
| 4473 | { |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4474 | ext4_mb_show_pa(ac->ac_sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4475 | return; |
| 4476 | } |
| 4477 | #endif |
| 4478 | |
| 4479 | /* |
| 4480 | * We use locality group preallocation for small size file. The size of the |
| 4481 | * file is determined by the current size or the resulting size after |
| 4482 | * allocation which ever is larger |
| 4483 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 4484 | * 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] | 4485 | */ |
| 4486 | static void ext4_mb_group_or_file(struct ext4_allocation_context *ac) |
| 4487 | { |
| 4488 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 4489 | int bsbits = ac->ac_sb->s_blocksize_bits; |
| 4490 | loff_t size, isize; |
| 4491 | |
| 4492 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 4493 | return; |
| 4494 | |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 4495 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 4496 | return; |
| 4497 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4498 | 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] | 4499 | isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1) |
| 4500 | >> bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4501 | |
Nikolay Borisov | 82dd124 | 2019-02-10 23:04:16 -0500 | [diff] [blame] | 4502 | if ((size == isize) && !ext4_fs_is_busy(sbi) && |
| 4503 | !inode_is_open_for_write(ac->ac_inode)) { |
Theodore Ts'o | 5079748 | 2009-09-18 13:34:02 -0400 | [diff] [blame] | 4504 | ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC; |
| 4505 | return; |
| 4506 | } |
| 4507 | |
Robin Dong | ebbe027 | 2011-10-26 05:14:27 -0400 | [diff] [blame] | 4508 | if (sbi->s_mb_group_prealloc <= 0) { |
| 4509 | ac->ac_flags |= EXT4_MB_STREAM_ALLOC; |
| 4510 | return; |
| 4511 | } |
| 4512 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4513 | /* don't use group allocation for large files */ |
Theodore Ts'o | 7178057 | 2009-09-28 00:06:20 -0400 | [diff] [blame] | 4514 | size = max(size, isize); |
Tao Ma | cc483f1 | 2010-03-01 19:06:35 -0500 | [diff] [blame] | 4515 | if (size > sbi->s_mb_stream_request) { |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 4516 | ac->ac_flags |= EXT4_MB_STREAM_ALLOC; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4517 | return; |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 4518 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4519 | |
| 4520 | BUG_ON(ac->ac_lg != NULL); |
| 4521 | /* |
| 4522 | * locality group prealloc space are per cpu. The reason for having |
| 4523 | * per cpu locality group is to reduce the contention between block |
| 4524 | * request from multiple CPUs. |
| 4525 | */ |
Christoph Lameter | a0b6bc6 | 2014-08-17 12:30:28 -0500 | [diff] [blame] | 4526 | ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4527 | |
| 4528 | /* we're going to use group allocation */ |
| 4529 | ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC; |
| 4530 | |
| 4531 | /* serialize all allocations in the group */ |
| 4532 | mutex_lock(&ac->ac_lg->lg_mutex); |
| 4533 | } |
| 4534 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4535 | static noinline_for_stack int |
| 4536 | ext4_mb_initialize_context(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4537 | struct ext4_allocation_request *ar) |
| 4538 | { |
| 4539 | struct super_block *sb = ar->inode->i_sb; |
| 4540 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 4541 | struct ext4_super_block *es = sbi->s_es; |
| 4542 | ext4_group_t group; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4543 | unsigned int len; |
| 4544 | ext4_fsblk_t goal; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4545 | ext4_grpblk_t block; |
| 4546 | |
| 4547 | /* we can't allocate > group size */ |
| 4548 | len = ar->len; |
| 4549 | |
| 4550 | /* just a dirty hack to filter too big requests */ |
Theodore Ts'o | 40ae348 | 2013-02-04 15:08:40 -0500 | [diff] [blame] | 4551 | if (len >= EXT4_CLUSTERS_PER_GROUP(sb)) |
| 4552 | len = EXT4_CLUSTERS_PER_GROUP(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4553 | |
| 4554 | /* start searching from the goal */ |
| 4555 | goal = ar->goal; |
| 4556 | if (goal < le32_to_cpu(es->s_first_data_block) || |
| 4557 | goal >= ext4_blocks_count(es)) |
| 4558 | goal = le32_to_cpu(es->s_first_data_block); |
| 4559 | ext4_get_group_no_and_offset(sb, goal, &group, &block); |
| 4560 | |
| 4561 | /* set up allocation goals */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4562 | ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4563 | ac->ac_status = AC_STATUS_CONTINUE; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4564 | ac->ac_sb = sb; |
| 4565 | ac->ac_inode = ar->inode; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4566 | ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4567 | ac->ac_o_ex.fe_group = group; |
| 4568 | ac->ac_o_ex.fe_start = block; |
| 4569 | ac->ac_o_ex.fe_len = len; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4570 | ac->ac_g_ex = ac->ac_o_ex; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4571 | ac->ac_flags = ar->flags; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4572 | |
brookxu | 3cb77bd | 2020-07-15 11:00:44 +0800 | [diff] [blame] | 4573 | /* we have to define context: we'll work with a file or |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4574 | * locality group. this is a policy, actually */ |
| 4575 | ext4_mb_group_or_file(ac); |
| 4576 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4577 | mb_debug(sb, "init ac: %u blocks @ %u, goal %u, flags 0x%x, 2^%d, " |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4578 | "left: %u/%u, right %u/%u to %swritable\n", |
| 4579 | (unsigned) ar->len, (unsigned) ar->logical, |
| 4580 | (unsigned) ar->goal, ac->ac_flags, ac->ac_2order, |
| 4581 | (unsigned) ar->lleft, (unsigned) ar->pleft, |
| 4582 | (unsigned) ar->lright, (unsigned) ar->pright, |
Nikolay Borisov | 82dd124 | 2019-02-10 23:04:16 -0500 | [diff] [blame] | 4583 | inode_is_open_for_write(ar->inode) ? "" : "non-"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4584 | return 0; |
| 4585 | |
| 4586 | } |
| 4587 | |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4588 | static noinline_for_stack void |
| 4589 | ext4_mb_discard_lg_preallocations(struct super_block *sb, |
| 4590 | struct ext4_locality_group *lg, |
| 4591 | int order, int total_entries) |
| 4592 | { |
| 4593 | ext4_group_t group = 0; |
| 4594 | struct ext4_buddy e4b; |
| 4595 | struct list_head discard_list; |
| 4596 | struct ext4_prealloc_space *pa, *tmp; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4597 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4598 | mb_debug(sb, "discard locality group preallocation\n"); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4599 | |
| 4600 | INIT_LIST_HEAD(&discard_list); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4601 | |
| 4602 | spin_lock(&lg->lg_prealloc_lock); |
| 4603 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order], |
Madhuparna Bhowmik | 92e9c58 | 2020-02-13 20:55:58 +0530 | [diff] [blame] | 4604 | pa_inode_list, |
| 4605 | lockdep_is_held(&lg->lg_prealloc_lock)) { |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4606 | spin_lock(&pa->pa_lock); |
| 4607 | if (atomic_read(&pa->pa_count)) { |
| 4608 | /* |
| 4609 | * This is the pa that we just used |
| 4610 | * for block allocation. So don't |
| 4611 | * free that |
| 4612 | */ |
| 4613 | spin_unlock(&pa->pa_lock); |
| 4614 | continue; |
| 4615 | } |
| 4616 | if (pa->pa_deleted) { |
| 4617 | spin_unlock(&pa->pa_lock); |
| 4618 | continue; |
| 4619 | } |
| 4620 | /* only lg prealloc space */ |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4621 | BUG_ON(pa->pa_type != MB_GROUP_PA); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4622 | |
| 4623 | /* seems this one can be freed ... */ |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4624 | ext4_mb_mark_pa_deleted(sb, pa); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4625 | spin_unlock(&pa->pa_lock); |
| 4626 | |
| 4627 | list_del_rcu(&pa->pa_inode_list); |
| 4628 | list_add(&pa->u.pa_tmp_list, &discard_list); |
| 4629 | |
| 4630 | total_entries--; |
| 4631 | if (total_entries <= 5) { |
| 4632 | /* |
| 4633 | * we want to keep only 5 entries |
| 4634 | * allowing it to grow to 8. This |
| 4635 | * mak sure we don't call discard |
| 4636 | * soon for this list. |
| 4637 | */ |
| 4638 | break; |
| 4639 | } |
| 4640 | } |
| 4641 | spin_unlock(&lg->lg_prealloc_lock); |
| 4642 | |
| 4643 | 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] | 4644 | int err; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4645 | |
Lukas Czerner | bd86298 | 2013-04-03 23:32:34 -0400 | [diff] [blame] | 4646 | group = ext4_get_group_number(sb, pa->pa_pstart); |
Konstantin Khlebnikov | 9651e6b | 2017-05-21 22:35:23 -0400 | [diff] [blame] | 4647 | err = ext4_mb_load_buddy_gfp(sb, group, &e4b, |
| 4648 | GFP_NOFS|__GFP_NOFAIL); |
| 4649 | if (err) { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 4650 | ext4_error_err(sb, -err, "Error %d loading buddy information for %u", |
| 4651 | err, group); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4652 | continue; |
| 4653 | } |
| 4654 | ext4_lock_group(sb, group); |
| 4655 | list_del(&pa->pa_group_list); |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4656 | ext4_mb_release_group_pa(&e4b, pa); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4657 | ext4_unlock_group(sb, group); |
| 4658 | |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 4659 | ext4_mb_unload_buddy(&e4b); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4660 | list_del(&pa->u.pa_tmp_list); |
| 4661 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4662 | } |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | /* |
| 4666 | * We have incremented pa_count. So it cannot be freed at this |
| 4667 | * point. Also we hold lg_mutex. So no parallel allocation is |
| 4668 | * possible from this lg. That means pa_free cannot be updated. |
| 4669 | * |
| 4670 | * A parallel ext4_mb_discard_group_preallocations is possible. |
| 4671 | * which can cause the lg_prealloc_list to be updated. |
| 4672 | */ |
| 4673 | |
| 4674 | static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac) |
| 4675 | { |
| 4676 | int order, added = 0, lg_prealloc_count = 1; |
| 4677 | struct super_block *sb = ac->ac_sb; |
| 4678 | struct ext4_locality_group *lg = ac->ac_lg; |
| 4679 | struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa; |
| 4680 | |
| 4681 | order = fls(pa->pa_free) - 1; |
| 4682 | if (order > PREALLOC_TB_SIZE - 1) |
| 4683 | /* The max size of hash table is PREALLOC_TB_SIZE */ |
| 4684 | order = PREALLOC_TB_SIZE - 1; |
| 4685 | /* Add the prealloc space to lg */ |
Niu Yawei | f116700 | 2013-02-01 21:31:27 -0500 | [diff] [blame] | 4686 | spin_lock(&lg->lg_prealloc_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4687 | list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order], |
Madhuparna Bhowmik | 92e9c58 | 2020-02-13 20:55:58 +0530 | [diff] [blame] | 4688 | pa_inode_list, |
| 4689 | lockdep_is_held(&lg->lg_prealloc_lock)) { |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4690 | spin_lock(&tmp_pa->pa_lock); |
| 4691 | if (tmp_pa->pa_deleted) { |
Theodore Ts'o | e7c9e3e | 2009-03-27 19:43:21 -0400 | [diff] [blame] | 4692 | spin_unlock(&tmp_pa->pa_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4693 | continue; |
| 4694 | } |
| 4695 | if (!added && pa->pa_free < tmp_pa->pa_free) { |
| 4696 | /* Add to the tail of the previous entry */ |
| 4697 | list_add_tail_rcu(&pa->pa_inode_list, |
| 4698 | &tmp_pa->pa_inode_list); |
| 4699 | added = 1; |
| 4700 | /* |
| 4701 | * we want to count the total |
| 4702 | * number of entries in the list |
| 4703 | */ |
| 4704 | } |
| 4705 | spin_unlock(&tmp_pa->pa_lock); |
| 4706 | lg_prealloc_count++; |
| 4707 | } |
| 4708 | if (!added) |
| 4709 | list_add_tail_rcu(&pa->pa_inode_list, |
| 4710 | &lg->lg_prealloc_list[order]); |
Niu Yawei | f116700 | 2013-02-01 21:31:27 -0500 | [diff] [blame] | 4711 | spin_unlock(&lg->lg_prealloc_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4712 | |
| 4713 | /* Now trim the list to be not more than 8 elements */ |
| 4714 | if (lg_prealloc_count > 8) { |
| 4715 | ext4_mb_discard_lg_preallocations(sb, lg, |
Niu Yawei | f116700 | 2013-02-01 21:31:27 -0500 | [diff] [blame] | 4716 | order, lg_prealloc_count); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4717 | return; |
| 4718 | } |
| 4719 | return ; |
| 4720 | } |
| 4721 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4722 | /* |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4723 | * if per-inode prealloc list is too long, trim some PA |
| 4724 | */ |
| 4725 | static void ext4_mb_trim_inode_pa(struct inode *inode) |
| 4726 | { |
| 4727 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 4728 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 4729 | int count, delta; |
| 4730 | |
| 4731 | count = atomic_read(&ei->i_prealloc_active); |
| 4732 | delta = (sbi->s_mb_max_inode_prealloc >> 2) + 1; |
| 4733 | if (count > sbi->s_mb_max_inode_prealloc + delta) { |
| 4734 | count -= sbi->s_mb_max_inode_prealloc; |
| 4735 | ext4_discard_preallocations(inode, count); |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4740 | * release all resource we used in allocation |
| 4741 | */ |
| 4742 | static int ext4_mb_release_context(struct ext4_allocation_context *ac) |
| 4743 | { |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4744 | struct inode *inode = ac->ac_inode; |
| 4745 | struct ext4_inode_info *ei = EXT4_I(inode); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4746 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4747 | struct ext4_prealloc_space *pa = ac->ac_pa; |
| 4748 | if (pa) { |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4749 | if (pa->pa_type == MB_GROUP_PA) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4750 | /* see comment in ext4_mb_use_group_pa() */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4751 | spin_lock(&pa->pa_lock); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4752 | pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len); |
| 4753 | 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] | 4754 | pa->pa_free -= ac->ac_b_ex.fe_len; |
| 4755 | pa->pa_len -= ac->ac_b_ex.fe_len; |
| 4756 | spin_unlock(&pa->pa_lock); |
brookxu | 66d5e02 | 2020-08-17 15:36:06 +0800 | [diff] [blame] | 4757 | |
| 4758 | /* |
| 4759 | * We want to add the pa to the right bucket. |
| 4760 | * Remove it from the list and while adding |
| 4761 | * make sure the list to which we are adding |
| 4762 | * doesn't grow big. |
| 4763 | */ |
| 4764 | if (likely(pa->pa_free)) { |
| 4765 | spin_lock(pa->pa_obj_lock); |
| 4766 | list_del_rcu(&pa->pa_inode_list); |
| 4767 | spin_unlock(pa->pa_obj_lock); |
| 4768 | ext4_mb_add_n_trim(ac); |
| 4769 | } |
Aneesh Kumar K.V | ba44391 | 2009-02-10 11:14:34 -0500 | [diff] [blame] | 4770 | } |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4771 | |
| 4772 | if (pa->pa_type == MB_INODE_PA) { |
| 4773 | /* |
| 4774 | * treat per-inode prealloc list as a lru list, then try |
| 4775 | * to trim the least recently used PA. |
| 4776 | */ |
| 4777 | spin_lock(pa->pa_obj_lock); |
| 4778 | list_move(&pa->pa_inode_list, &ei->i_prealloc_list); |
| 4779 | spin_unlock(pa->pa_obj_lock); |
| 4780 | } |
| 4781 | |
Aneesh Kumar K.V | ba44391 | 2009-02-10 11:14:34 -0500 | [diff] [blame] | 4782 | ext4_mb_put_pa(ac, ac->ac_sb, pa); |
| 4783 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4784 | if (ac->ac_bitmap_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4785 | put_page(ac->ac_bitmap_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4786 | if (ac->ac_buddy_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4787 | put_page(ac->ac_buddy_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4788 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
| 4789 | mutex_unlock(&ac->ac_lg->lg_mutex); |
| 4790 | ext4_mb_collect_stats(ac); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4791 | ext4_mb_trim_inode_pa(inode); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4792 | return 0; |
| 4793 | } |
| 4794 | |
| 4795 | static int ext4_mb_discard_preallocations(struct super_block *sb, int needed) |
| 4796 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4797 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4798 | int ret; |
| 4799 | int freed = 0; |
| 4800 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4801 | trace_ext4_mb_discard_preallocations(sb, needed); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 4802 | for (i = 0; i < ngroups && needed > 0; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4803 | ret = ext4_mb_discard_group_preallocations(sb, i, needed); |
| 4804 | freed += ret; |
| 4805 | needed -= ret; |
| 4806 | } |
| 4807 | |
| 4808 | return freed; |
| 4809 | } |
| 4810 | |
Ritesh Harjani | cf5e2ca | 2020-05-20 12:10:33 +0530 | [diff] [blame] | 4811 | static bool ext4_mb_discard_preallocations_should_retry(struct super_block *sb, |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 4812 | struct ext4_allocation_context *ac, u64 *seq) |
Ritesh Harjani | cf5e2ca | 2020-05-20 12:10:33 +0530 | [diff] [blame] | 4813 | { |
| 4814 | int freed; |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 4815 | u64 seq_retry = 0; |
| 4816 | bool ret = false; |
Ritesh Harjani | cf5e2ca | 2020-05-20 12:10:33 +0530 | [diff] [blame] | 4817 | |
| 4818 | freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len); |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 4819 | if (freed) { |
| 4820 | ret = true; |
| 4821 | goto out_dbg; |
| 4822 | } |
| 4823 | seq_retry = ext4_get_discard_pa_seq_sum(); |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 4824 | if (!(ac->ac_flags & EXT4_MB_STRICT_CHECK) || seq_retry != *seq) { |
| 4825 | ac->ac_flags |= EXT4_MB_STRICT_CHECK; |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 4826 | *seq = seq_retry; |
| 4827 | ret = true; |
| 4828 | } |
| 4829 | |
| 4830 | out_dbg: |
| 4831 | mb_debug(sb, "freed %d, retry ? %s\n", freed, ret ? "yes" : "no"); |
| 4832 | return ret; |
Ritesh Harjani | cf5e2ca | 2020-05-20 12:10:33 +0530 | [diff] [blame] | 4833 | } |
| 4834 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4835 | /* |
| 4836 | * Main entry point into mballoc to allocate blocks |
| 4837 | * it tries to use preallocation first, then falls back |
| 4838 | * to usual allocation |
| 4839 | */ |
| 4840 | ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4841 | struct ext4_allocation_request *ar, int *errp) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4842 | { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4843 | struct ext4_allocation_context *ac = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4844 | struct ext4_sb_info *sbi; |
| 4845 | struct super_block *sb; |
| 4846 | ext4_fsblk_t block = 0; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4847 | unsigned int inquota = 0; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4848 | unsigned int reserv_clstrs = 0; |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 4849 | u64 seq; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4850 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 4851 | might_sleep(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4852 | sb = ar->inode->i_sb; |
| 4853 | sbi = EXT4_SB(sb); |
| 4854 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4855 | trace_ext4_request_blocks(ar); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 4856 | |
Dmitry Monakhov | 45dc63e | 2011-10-20 20:07:23 -0400 | [diff] [blame] | 4857 | /* Allow to use superuser reservation for quota file */ |
Tahsin Erdogan | 02749a4 | 2017-06-22 11:31:25 -0400 | [diff] [blame] | 4858 | if (ext4_is_quota_file(ar->inode)) |
Dmitry Monakhov | 45dc63e | 2011-10-20 20:07:23 -0400 | [diff] [blame] | 4859 | ar->flags |= EXT4_MB_USE_ROOT_BLOCKS; |
| 4860 | |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 4861 | if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) { |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4862 | /* Without delayed allocation we need to verify |
| 4863 | * there is enough free blocks to do block allocation |
| 4864 | * and verify allocation doesn't exceed the quota limits. |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4865 | */ |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 4866 | while (ar->len && |
Theodore Ts'o | e7d5f31 | 2011-09-09 19:14:51 -0400 | [diff] [blame] | 4867 | ext4_claim_free_clusters(sbi, ar->len, ar->flags)) { |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 4868 | |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 4869 | /* let others to free the space */ |
Lukas Czerner | bb8b20e | 2013-03-10 22:28:09 -0400 | [diff] [blame] | 4870 | cond_resched(); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 4871 | ar->len = ar->len >> 1; |
| 4872 | } |
| 4873 | if (!ar->len) { |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4874 | ext4_mb_show_pa(sb); |
Aneesh Kumar K.V | a30d542a | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 4875 | *errp = -ENOSPC; |
| 4876 | return 0; |
| 4877 | } |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4878 | reserv_clstrs = ar->len; |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 4879 | if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4880 | dquot_alloc_block_nofail(ar->inode, |
| 4881 | EXT4_C2B(sbi, ar->len)); |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 4882 | } else { |
| 4883 | while (ar->len && |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4884 | dquot_alloc_block(ar->inode, |
| 4885 | EXT4_C2B(sbi, ar->len))) { |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 4886 | |
| 4887 | ar->flags |= EXT4_MB_HINT_NOPREALLOC; |
| 4888 | ar->len--; |
| 4889 | } |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4890 | } |
| 4891 | inquota = ar->len; |
| 4892 | if (ar->len == 0) { |
| 4893 | *errp = -EDQUOT; |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4894 | goto out; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4895 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4896 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 4897 | |
Wei Yongjun | 85556c9 | 2012-09-26 20:43:37 -0400 | [diff] [blame] | 4898 | ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 833576b | 2009-07-13 09:45:52 -0400 | [diff] [blame] | 4899 | if (!ac) { |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4900 | ar->len = 0; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4901 | *errp = -ENOMEM; |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4902 | goto out; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4903 | } |
| 4904 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4905 | *errp = ext4_mb_initialize_context(ac, ar); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4906 | if (*errp) { |
| 4907 | ar->len = 0; |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4908 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4909 | } |
| 4910 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4911 | ac->ac_op = EXT4_MB_HISTORY_PREALLOC; |
Ritesh Harjani | 8119853 | 2020-06-09 16:23:10 +0530 | [diff] [blame] | 4912 | seq = this_cpu_read(discard_pa_seq); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4913 | if (!ext4_mb_use_preallocated(ac)) { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4914 | ac->ac_op = EXT4_MB_HISTORY_ALLOC; |
| 4915 | ext4_mb_normalize_request(ac, ar); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4916 | |
| 4917 | *errp = ext4_mb_pa_alloc(ac); |
| 4918 | if (*errp) |
| 4919 | goto errout; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4920 | repeat: |
| 4921 | /* allocate space in core */ |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4922 | *errp = ext4_mb_regular_allocator(ac); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4923 | /* |
| 4924 | * pa allocated above is added to grp->bb_prealloc_list only |
| 4925 | * when we were able to allocate some block i.e. when |
| 4926 | * ac->ac_status == AC_STATUS_FOUND. |
| 4927 | * And error from above mean ac->ac_status != AC_STATUS_FOUND |
| 4928 | * So we have to free this pa here itself. |
| 4929 | */ |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 4930 | if (*errp) { |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4931 | ext4_mb_pa_free(ac); |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 4932 | ext4_discard_allocated_blocks(ac); |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4933 | goto errout; |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 4934 | } |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4935 | if (ac->ac_status == AC_STATUS_FOUND && |
| 4936 | ac->ac_o_ex.fe_len >= ac->ac_f_ex.fe_len) |
| 4937 | ext4_mb_pa_free(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4938 | } |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4939 | if (likely(ac->ac_status == AC_STATUS_FOUND)) { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4940 | *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs); |
Vegard Nossum | 554a5cc | 2016-07-14 23:02:47 -0400 | [diff] [blame] | 4941 | if (*errp) { |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 4942 | ext4_discard_allocated_blocks(ac); |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 4943 | goto errout; |
| 4944 | } else { |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 4945 | block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 4946 | ar->len = ac->ac_b_ex.fe_len; |
| 4947 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4948 | } else { |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 4949 | if (ext4_mb_discard_preallocations_should_retry(sb, ac, &seq)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4950 | goto repeat; |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4951 | /* |
| 4952 | * If block allocation fails then the pa allocated above |
| 4953 | * needs to be freed here itself. |
| 4954 | */ |
| 4955 | ext4_mb_pa_free(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4956 | *errp = -ENOSPC; |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4957 | } |
| 4958 | |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 4959 | errout: |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4960 | if (*errp) { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4961 | ac->ac_b_ex.fe_len = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4962 | ar->len = 0; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4963 | ext4_mb_show_ac(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4964 | } |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 4965 | ext4_mb_release_context(ac); |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 4966 | out: |
| 4967 | if (ac) |
| 4968 | kmem_cache_free(ext4_ac_cachep, ac); |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 4969 | if (inquota && ar->len < inquota) |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4970 | 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] | 4971 | if (!ar->len) { |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 4972 | if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) |
Aneesh Kumar K.V | 0087d9f | 2009-01-05 21:49:12 -0500 | [diff] [blame] | 4973 | /* release all the reserved blocks if non delalloc */ |
Theodore Ts'o | 5704265 | 2011-09-09 18:56:51 -0400 | [diff] [blame] | 4974 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4975 | reserv_clstrs); |
Aneesh Kumar K.V | 0087d9f | 2009-01-05 21:49:12 -0500 | [diff] [blame] | 4976 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4977 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4978 | trace_ext4_allocate_blocks(ar, (unsigned long long)block); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 4979 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4980 | return block; |
| 4981 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4982 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4983 | /* |
| 4984 | * We can merge two free data extents only if the physical blocks |
| 4985 | * are contiguous, AND the extents were freed by the same transaction, |
| 4986 | * AND the blocks are associated with the same group. |
| 4987 | */ |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 4988 | static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi, |
| 4989 | struct ext4_free_data *entry, |
| 4990 | struct ext4_free_data *new_entry, |
| 4991 | struct rb_root *entry_rb_root) |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 4992 | { |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 4993 | if ((entry->efd_tid != new_entry->efd_tid) || |
| 4994 | (entry->efd_group != new_entry->efd_group)) |
| 4995 | return; |
| 4996 | if (entry->efd_start_cluster + entry->efd_count == |
| 4997 | new_entry->efd_start_cluster) { |
| 4998 | new_entry->efd_start_cluster = entry->efd_start_cluster; |
| 4999 | new_entry->efd_count += entry->efd_count; |
| 5000 | } else if (new_entry->efd_start_cluster + new_entry->efd_count == |
| 5001 | entry->efd_start_cluster) { |
| 5002 | new_entry->efd_count += entry->efd_count; |
| 5003 | } else |
| 5004 | return; |
| 5005 | spin_lock(&sbi->s_md_lock); |
| 5006 | list_del(&entry->efd_list); |
| 5007 | spin_unlock(&sbi->s_md_lock); |
| 5008 | rb_erase(&entry->efd_node, entry_rb_root); |
| 5009 | kmem_cache_free(ext4_free_data_cachep, entry); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5010 | } |
| 5011 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 5012 | static noinline_for_stack int |
| 5013 | 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] | 5014 | struct ext4_free_data *new_entry) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5015 | { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 5016 | ext4_group_t group = e4b->bd_group; |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5017 | ext4_grpblk_t cluster; |
Theodore Ts'o | d08854f | 2016-06-26 18:24:01 -0400 | [diff] [blame] | 5018 | ext4_grpblk_t clusters = new_entry->efd_count; |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5019 | struct ext4_free_data *entry; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5020 | struct ext4_group_info *db = e4b->bd_info; |
| 5021 | struct super_block *sb = e4b->bd_sb; |
| 5022 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5023 | struct rb_node **n = &db->bb_free_root.rb_node, *node; |
| 5024 | struct rb_node *parent = NULL, *new_node; |
| 5025 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5026 | BUG_ON(!ext4_handle_valid(handle)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5027 | BUG_ON(e4b->bd_bitmap_page == NULL); |
| 5028 | BUG_ON(e4b->bd_buddy_page == NULL); |
| 5029 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5030 | new_node = &new_entry->efd_node; |
| 5031 | cluster = new_entry->efd_start_cluster; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5032 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5033 | if (!*n) { |
| 5034 | /* first free block exent. We need to |
| 5035 | protect buddy cache from being freed, |
| 5036 | * otherwise we'll refresh it from |
| 5037 | * on-disk bitmap and lose not-yet-available |
| 5038 | * blocks */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5039 | get_page(e4b->bd_buddy_page); |
| 5040 | get_page(e4b->bd_bitmap_page); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5041 | } |
| 5042 | while (*n) { |
| 5043 | parent = *n; |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5044 | entry = rb_entry(parent, struct ext4_free_data, efd_node); |
| 5045 | if (cluster < entry->efd_start_cluster) |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5046 | n = &(*n)->rb_left; |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5047 | else if (cluster >= (entry->efd_start_cluster + entry->efd_count)) |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5048 | n = &(*n)->rb_right; |
| 5049 | else { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 5050 | ext4_grp_locked_error(sb, group, 0, |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5051 | ext4_group_first_block_no(sb, group) + |
| 5052 | EXT4_C2B(sbi, cluster), |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 5053 | "Block already on to-be-freed list"); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5054 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5055 | } |
| 5056 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5057 | |
| 5058 | rb_link_node(new_node, parent, n); |
| 5059 | rb_insert_color(new_node, &db->bb_free_root); |
| 5060 | |
| 5061 | /* Now try to see the extent can be merged to left and right */ |
| 5062 | node = rb_prev(new_node); |
| 5063 | if (node) { |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5064 | entry = rb_entry(node, struct ext4_free_data, efd_node); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5065 | ext4_try_merge_freed_extent(sbi, entry, new_entry, |
| 5066 | &(db->bb_free_root)); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | node = rb_next(new_node); |
| 5070 | if (node) { |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5071 | entry = rb_entry(node, struct ext4_free_data, efd_node); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5072 | ext4_try_merge_freed_extent(sbi, entry, new_entry, |
| 5073 | &(db->bb_free_root)); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5074 | } |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5075 | |
Theodore Ts'o | d08854f | 2016-06-26 18:24:01 -0400 | [diff] [blame] | 5076 | spin_lock(&sbi->s_md_lock); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5077 | 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] | 5078 | sbi->s_mb_free_pending += clusters; |
| 5079 | spin_unlock(&sbi->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5080 | return 0; |
| 5081 | } |
| 5082 | |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 5083 | /** |
| 5084 | * ext4_free_blocks() -- Free given blocks and update quota |
| 5085 | * @handle: handle for this transaction |
| 5086 | * @inode: inode |
Theodore Ts'o | c60990b | 2019-06-19 16:30:03 -0400 | [diff] [blame] | 5087 | * @bh: optional buffer of the block to be freed |
| 5088 | * @block: starting physical block to be freed |
| 5089 | * @count: number of blocks to be freed |
Yongqiang Yang | 5def136 | 2011-06-05 23:26:40 -0400 | [diff] [blame] | 5090 | * @flags: flags used by ext4_free_blocks |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5091 | */ |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 5092 | void ext4_free_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5093 | struct buffer_head *bh, ext4_fsblk_t block, |
| 5094 | unsigned long count, int flags) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5095 | { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 5096 | struct buffer_head *bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5097 | struct super_block *sb = inode->i_sb; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5098 | struct ext4_group_desc *gdp; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 5099 | unsigned int overflow; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5100 | ext4_grpblk_t bit; |
| 5101 | struct buffer_head *gd_bh; |
| 5102 | ext4_group_t block_group; |
| 5103 | struct ext4_sb_info *sbi; |
| 5104 | struct ext4_buddy e4b; |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5105 | unsigned int count_clusters; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5106 | int err = 0; |
| 5107 | int ret; |
| 5108 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 5109 | might_sleep(); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5110 | if (bh) { |
| 5111 | if (block) |
| 5112 | BUG_ON(block != bh->b_blocknr); |
| 5113 | else |
| 5114 | block = bh->b_blocknr; |
| 5115 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5116 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5117 | sbi = EXT4_SB(sb); |
Theodore Ts'o | 1f2acb6 | 2010-01-22 17:40:42 -0500 | [diff] [blame] | 5118 | if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) && |
Jan Kara | ce9f24c | 2020-07-28 15:04:34 +0200 | [diff] [blame] | 5119 | !ext4_inode_block_valid(inode, block, count)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 5120 | ext4_error(sb, "Freeing blocks not in datazone - " |
Theodore Ts'o | 1f2acb6 | 2010-01-22 17:40:42 -0500 | [diff] [blame] | 5121 | "block = %llu, count = %lu", block, count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5122 | goto error_return; |
| 5123 | } |
| 5124 | |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 5125 | ext4_debug("freeing block %llu\n", block); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5126 | trace_ext4_free_blocks(inode, block, count, flags); |
| 5127 | |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5128 | if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) { |
| 5129 | BUG_ON(count > 1); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5130 | |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5131 | ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA, |
| 5132 | inode, bh, block); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5133 | } |
| 5134 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 5135 | /* |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5136 | * If the extent to be freed does not begin on a cluster |
| 5137 | * boundary, we need to deal with partial clusters at the |
| 5138 | * beginning and end of the extent. Normally we will free |
| 5139 | * blocks at the beginning or the end unless we are explicitly |
| 5140 | * requested to avoid doing so. |
| 5141 | */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 5142 | overflow = EXT4_PBLK_COFF(sbi, block); |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5143 | if (overflow) { |
| 5144 | if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) { |
| 5145 | overflow = sbi->s_cluster_ratio - overflow; |
| 5146 | block += overflow; |
| 5147 | if (count > overflow) |
| 5148 | count -= overflow; |
| 5149 | else |
| 5150 | return; |
| 5151 | } else { |
| 5152 | block -= overflow; |
| 5153 | count += overflow; |
| 5154 | } |
| 5155 | } |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 5156 | overflow = EXT4_LBLK_COFF(sbi, count); |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5157 | if (overflow) { |
| 5158 | if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) { |
| 5159 | if (count > overflow) |
| 5160 | count -= overflow; |
| 5161 | else |
| 5162 | return; |
| 5163 | } else |
| 5164 | count += sbi->s_cluster_ratio - overflow; |
| 5165 | } |
| 5166 | |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5167 | if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) { |
| 5168 | int i; |
Daeho Jeong | f96c450 | 2016-02-21 18:31:41 -0500 | [diff] [blame] | 5169 | int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA; |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5170 | |
| 5171 | for (i = 0; i < count; i++) { |
| 5172 | cond_resched(); |
Daeho Jeong | f96c450 | 2016-02-21 18:31:41 -0500 | [diff] [blame] | 5173 | if (is_metadata) |
| 5174 | bh = sb_find_get_block(inode->i_sb, block + i); |
| 5175 | ext4_forget(handle, is_metadata, inode, bh, block + i); |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5176 | } |
| 5177 | } |
| 5178 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5179 | do_more: |
| 5180 | overflow = 0; |
| 5181 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
| 5182 | |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 5183 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT( |
| 5184 | ext4_get_group_info(sb, block_group)))) |
| 5185 | return; |
| 5186 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5187 | /* |
| 5188 | * Check to see if we are freeing blocks across a group |
| 5189 | * boundary. |
| 5190 | */ |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5191 | if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) { |
| 5192 | overflow = EXT4_C2B(sbi, bit) + count - |
| 5193 | EXT4_BLOCKS_PER_GROUP(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5194 | count -= overflow; |
| 5195 | } |
Lukas Czerner | 810da24 | 2013-03-02 17:18:58 -0500 | [diff] [blame] | 5196 | count_clusters = EXT4_NUM_B2C(sbi, count); |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5197 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 5198 | if (IS_ERR(bitmap_bh)) { |
| 5199 | err = PTR_ERR(bitmap_bh); |
| 5200 | bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5201 | goto error_return; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 5202 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5203 | gdp = ext4_get_group_desc(sb, block_group, &gd_bh); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 5204 | if (!gdp) { |
| 5205 | err = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5206 | goto error_return; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 5207 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5208 | |
| 5209 | if (in_range(ext4_block_bitmap(sb, gdp), block, count) || |
| 5210 | in_range(ext4_inode_bitmap(sb, gdp), block, count) || |
| 5211 | in_range(block, ext4_inode_table(sb, gdp), |
Jun Piao | 49598e0 | 2018-01-11 13:17:49 -0500 | [diff] [blame] | 5212 | sbi->s_itb_per_group) || |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5213 | in_range(block + count - 1, ext4_inode_table(sb, gdp), |
Jun Piao | 49598e0 | 2018-01-11 13:17:49 -0500 | [diff] [blame] | 5214 | sbi->s_itb_per_group)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5215 | |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 5216 | ext4_error(sb, "Freeing blocks in system zone - " |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 5217 | "Block = %llu, count = %lu", block, count); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 5218 | /* err = 0. ext4_std_error should be a no op */ |
| 5219 | goto error_return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
| 5223 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
| 5224 | if (err) |
| 5225 | goto error_return; |
| 5226 | |
| 5227 | /* |
| 5228 | * We are about to modify some metadata. Call the journal APIs |
| 5229 | * to unshare ->b_data if a currently-committing transaction is |
| 5230 | * using it |
| 5231 | */ |
| 5232 | BUFFER_TRACE(gd_bh, "get_write_access"); |
| 5233 | err = ext4_journal_get_write_access(handle, gd_bh); |
| 5234 | if (err) |
| 5235 | goto error_return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5236 | #ifdef AGGRESSIVE_CHECK |
| 5237 | { |
| 5238 | int i; |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5239 | for (i = 0; i < count_clusters; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5240 | BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data)); |
| 5241 | } |
| 5242 | #endif |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5243 | trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5244 | |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 5245 | /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */ |
| 5246 | err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b, |
| 5247 | GFP_NOFS|__GFP_NOFAIL); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 5248 | if (err) |
| 5249 | goto error_return; |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5250 | |
Daeho Jeong | f96c450 | 2016-02-21 18:31:41 -0500 | [diff] [blame] | 5251 | /* |
| 5252 | * We need to make sure we don't reuse the freed block until after the |
| 5253 | * transaction is committed. We make an exception if the inode is to be |
| 5254 | * written in writeback mode since writeback mode has weak data |
| 5255 | * consistency guarantees. |
| 5256 | */ |
| 5257 | if (ext4_handle_valid(handle) && |
| 5258 | ((flags & EXT4_FREE_BLOCKS_METADATA) || |
| 5259 | !ext4_should_writeback_data(inode))) { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5260 | struct ext4_free_data *new_entry; |
| 5261 | /* |
Michal Hocko | 7444a07 | 2015-07-05 12:33:44 -0400 | [diff] [blame] | 5262 | * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed |
| 5263 | * to fail. |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5264 | */ |
Michal Hocko | 7444a07 | 2015-07-05 12:33:44 -0400 | [diff] [blame] | 5265 | new_entry = kmem_cache_alloc(ext4_free_data_cachep, |
| 5266 | GFP_NOFS|__GFP_NOFAIL); |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5267 | new_entry->efd_start_cluster = bit; |
| 5268 | new_entry->efd_group = block_group; |
| 5269 | new_entry->efd_count = count_clusters; |
| 5270 | new_entry->efd_tid = handle->h_transaction->t_tid; |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 5271 | |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5272 | ext4_lock_group(sb, block_group); |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5273 | mb_clear_bits(bitmap_bh->b_data, bit, count_clusters); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5274 | ext4_mb_free_metadata(handle, &e4b, new_entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5275 | } else { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5276 | /* need to update group_info->bb_free and bitmap |
| 5277 | * with group lock held. generate_buddy look at |
| 5278 | * them with group lock_held |
| 5279 | */ |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5280 | if (test_opt(sb, DISCARD)) { |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5281 | err = ext4_issue_discard(sb, block_group, bit, count, |
| 5282 | NULL); |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5283 | if (err && err != -EOPNOTSUPP) |
| 5284 | ext4_msg(sb, KERN_WARNING, "discard request in" |
| 5285 | " group:%d block:%d count:%lu failed" |
| 5286 | " with %d", block_group, bit, count, |
| 5287 | err); |
Lukas Czerner | 8f9ff18 | 2013-10-30 11:10:52 -0400 | [diff] [blame] | 5288 | } else |
| 5289 | EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info); |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5290 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 5291 | ext4_lock_group(sb, block_group); |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5292 | mb_clear_bits(bitmap_bh->b_data, bit, count_clusters); |
| 5293 | mb_free_blocks(inode, &e4b, bit, count_clusters); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5294 | } |
| 5295 | |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 5296 | ret = ext4_free_group_clusters(sb, gdp) + count_clusters; |
| 5297 | ext4_free_group_clusters_set(sb, gdp, ret); |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 5298 | ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh); |
Darrick J. Wong | feb0ab3 | 2012-04-29 18:45:10 -0400 | [diff] [blame] | 5299 | ext4_group_desc_csum_set(sb, block_group, gdp); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 5300 | ext4_unlock_group(sb, block_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5301 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5302 | if (sbi->s_log_groups_per_flex) { |
| 5303 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); |
Theodore Ts'o | 90ba983 | 2013-03-11 23:39:59 -0400 | [diff] [blame] | 5304 | atomic64_add(count_clusters, |
Suraj Jitindar Singh | 7c99072 | 2020-02-18 19:08:51 -0800 | [diff] [blame] | 5305 | &sbi_array_rcu_deref(sbi, s_flex_groups, |
| 5306 | flex_group)->free_clusters); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5307 | } |
| 5308 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 5309 | /* |
| 5310 | * on a bigalloc file system, defer the s_freeclusters_counter |
| 5311 | * update to the caller (ext4_remove_space and friends) so they |
| 5312 | * can determine if a cluster freed here should be rereserved |
| 5313 | */ |
| 5314 | if (!(flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)) { |
| 5315 | if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE)) |
| 5316 | dquot_free_block(inode, EXT4_C2B(sbi, count_clusters)); |
| 5317 | percpu_counter_add(&sbi->s_freeclusters_counter, |
| 5318 | count_clusters); |
| 5319 | } |
Jan Kara | 7d73453 | 2013-08-17 09:36:54 -0400 | [diff] [blame] | 5320 | |
| 5321 | ext4_mb_unload_buddy(&e4b); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 5322 | |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5323 | /* We dirtied the bitmap block */ |
| 5324 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
| 5325 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
| 5326 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5327 | /* And the group descriptor block */ |
| 5328 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5329 | ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5330 | if (!err) |
| 5331 | err = ret; |
| 5332 | |
| 5333 | if (overflow && !err) { |
| 5334 | block += count; |
| 5335 | count = overflow; |
| 5336 | put_bh(bitmap_bh); |
| 5337 | goto do_more; |
| 5338 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5339 | error_return: |
| 5340 | brelse(bitmap_bh); |
| 5341 | ext4_std_error(sb, err); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5342 | return; |
| 5343 | } |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5344 | |
| 5345 | /** |
Yongqiang Yang | 0529155 | 2011-07-26 21:43:56 -0400 | [diff] [blame] | 5346 | * ext4_group_add_blocks() -- Add given blocks to an existing group |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5347 | * @handle: handle to this transaction |
| 5348 | * @sb: super block |
Anatol Pomozov | 4907cb7 | 2012-09-01 10:31:09 -0700 | [diff] [blame] | 5349 | * @block: start physical block to add to the block group |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5350 | * @count: number of blocks to free |
| 5351 | * |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 5352 | * This marks the blocks as free in the bitmap and buddy. |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5353 | */ |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 5354 | int ext4_group_add_blocks(handle_t *handle, struct super_block *sb, |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5355 | ext4_fsblk_t block, unsigned long count) |
| 5356 | { |
| 5357 | struct buffer_head *bitmap_bh = NULL; |
| 5358 | struct buffer_head *gd_bh; |
| 5359 | ext4_group_t block_group; |
| 5360 | ext4_grpblk_t bit; |
| 5361 | unsigned int i; |
| 5362 | struct ext4_group_desc *desc; |
| 5363 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 5364 | struct ext4_buddy e4b; |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 5365 | int err = 0, ret, free_clusters_count; |
| 5366 | ext4_grpblk_t clusters_freed; |
| 5367 | ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block); |
| 5368 | ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1); |
| 5369 | unsigned long cluster_count = last_cluster - first_cluster + 1; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5370 | |
| 5371 | ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1); |
| 5372 | |
Yongqiang Yang | 4740b83 | 2011-07-26 21:51:08 -0400 | [diff] [blame] | 5373 | if (count == 0) |
| 5374 | return 0; |
| 5375 | |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5376 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5377 | /* |
| 5378 | * Check to see if we are freeing blocks across a group |
| 5379 | * boundary. |
| 5380 | */ |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 5381 | if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) { |
| 5382 | ext4_warning(sb, "too many blocks added to group %u", |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 5383 | block_group); |
| 5384 | err = -EINVAL; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5385 | goto error_return; |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 5386 | } |
Theodore Ts'o | 2cd05cc | 2011-05-09 10:58:45 -0400 | [diff] [blame] | 5387 | |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5388 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 5389 | if (IS_ERR(bitmap_bh)) { |
| 5390 | err = PTR_ERR(bitmap_bh); |
| 5391 | bitmap_bh = NULL; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5392 | goto error_return; |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 5393 | } |
| 5394 | |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5395 | desc = ext4_get_group_desc(sb, block_group, &gd_bh); |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 5396 | if (!desc) { |
| 5397 | err = -EIO; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5398 | goto error_return; |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 5399 | } |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5400 | |
| 5401 | if (in_range(ext4_block_bitmap(sb, desc), block, count) || |
| 5402 | in_range(ext4_inode_bitmap(sb, desc), block, count) || |
| 5403 | in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) || |
| 5404 | in_range(block + count - 1, ext4_inode_table(sb, desc), |
| 5405 | sbi->s_itb_per_group)) { |
| 5406 | ext4_error(sb, "Adding blocks in system zones - " |
| 5407 | "Block = %llu, count = %lu", |
| 5408 | block, count); |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 5409 | err = -EINVAL; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5410 | goto error_return; |
| 5411 | } |
| 5412 | |
Theodore Ts'o | 2cd05cc | 2011-05-09 10:58:45 -0400 | [diff] [blame] | 5413 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
| 5414 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5415 | if (err) |
| 5416 | goto error_return; |
| 5417 | |
| 5418 | /* |
| 5419 | * We are about to modify some metadata. Call the journal APIs |
| 5420 | * to unshare ->b_data if a currently-committing transaction is |
| 5421 | * using it |
| 5422 | */ |
| 5423 | BUFFER_TRACE(gd_bh, "get_write_access"); |
| 5424 | err = ext4_journal_get_write_access(handle, gd_bh); |
| 5425 | if (err) |
| 5426 | goto error_return; |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 5427 | |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 5428 | for (i = 0, clusters_freed = 0; i < cluster_count; i++) { |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5429 | BUFFER_TRACE(bitmap_bh, "clear bit"); |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 5430 | if (!mb_test_bit(bit + i, bitmap_bh->b_data)) { |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5431 | ext4_error(sb, "bit already cleared for block %llu", |
| 5432 | (ext4_fsblk_t)(block + i)); |
| 5433 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); |
| 5434 | } else { |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 5435 | clusters_freed++; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5436 | } |
| 5437 | } |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 5438 | |
| 5439 | err = ext4_mb_load_buddy(sb, block_group, &e4b); |
| 5440 | if (err) |
| 5441 | goto error_return; |
| 5442 | |
| 5443 | /* |
| 5444 | * need to update group_info->bb_free and bitmap |
| 5445 | * with group lock held. generate_buddy look at |
| 5446 | * them with group lock_held |
| 5447 | */ |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5448 | ext4_lock_group(sb, block_group); |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 5449 | mb_clear_bits(bitmap_bh->b_data, bit, cluster_count); |
| 5450 | mb_free_blocks(NULL, &e4b, bit, cluster_count); |
| 5451 | free_clusters_count = clusters_freed + |
| 5452 | ext4_free_group_clusters(sb, desc); |
| 5453 | ext4_free_group_clusters_set(sb, desc, free_clusters_count); |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 5454 | ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh); |
Darrick J. Wong | feb0ab3 | 2012-04-29 18:45:10 -0400 | [diff] [blame] | 5455 | ext4_group_desc_csum_set(sb, block_group, desc); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5456 | ext4_unlock_group(sb, block_group); |
Theodore Ts'o | 5704265 | 2011-09-09 18:56:51 -0400 | [diff] [blame] | 5457 | percpu_counter_add(&sbi->s_freeclusters_counter, |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 5458 | clusters_freed); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5459 | |
| 5460 | if (sbi->s_log_groups_per_flex) { |
| 5461 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 5462 | atomic64_add(clusters_freed, |
Suraj Jitindar Singh | 7c99072 | 2020-02-18 19:08:51 -0800 | [diff] [blame] | 5463 | &sbi_array_rcu_deref(sbi, s_flex_groups, |
| 5464 | flex_group)->free_clusters); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5465 | } |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 5466 | |
| 5467 | ext4_mb_unload_buddy(&e4b); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5468 | |
| 5469 | /* We dirtied the bitmap block */ |
| 5470 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
| 5471 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
| 5472 | |
| 5473 | /* And the group descriptor block */ |
| 5474 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
| 5475 | ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh); |
| 5476 | if (!err) |
| 5477 | err = ret; |
| 5478 | |
| 5479 | error_return: |
| 5480 | brelse(bitmap_bh); |
| 5481 | ext4_std_error(sb, err); |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 5482 | return err; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | /** |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5486 | * ext4_trim_extent -- function to TRIM one single free extent in the group |
| 5487 | * @sb: super block for the file system |
| 5488 | * @start: starting block of the free extent in the alloc. group |
| 5489 | * @count: number of blocks to TRIM |
| 5490 | * @group: alloc. group we are working with |
| 5491 | * @e4b: ext4 buddy for the group |
| 5492 | * |
| 5493 | * Trim "count" blocks starting at "start" in the "group". To assure that no |
| 5494 | * one will allocate those blocks, mark it as used in buddy bitmap. This must |
| 5495 | * be called with under the group lock. |
| 5496 | */ |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5497 | 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] | 5498 | ext4_group_t group, struct ext4_buddy *e4b) |
jon ernst | e2cbd58 | 2014-04-12 23:01:28 -0400 | [diff] [blame] | 5499 | __releases(bitlock) |
| 5500 | __acquires(bitlock) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5501 | { |
| 5502 | struct ext4_free_extent ex; |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5503 | int ret = 0; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5504 | |
Tao Ma | b3d4c2b | 2011-07-11 00:01:52 -0400 | [diff] [blame] | 5505 | trace_ext4_trim_extent(sb, group, start, count); |
| 5506 | |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5507 | assert_spin_locked(ext4_group_lock_ptr(sb, group)); |
| 5508 | |
| 5509 | ex.fe_start = start; |
| 5510 | ex.fe_group = group; |
| 5511 | ex.fe_len = count; |
| 5512 | |
| 5513 | /* |
| 5514 | * Mark blocks used, so no one can reuse them while |
| 5515 | * being trimmed. |
| 5516 | */ |
| 5517 | mb_mark_used(e4b, &ex); |
| 5518 | ext4_unlock_group(sb, group); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5519 | ret = ext4_issue_discard(sb, group, start, count, NULL); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5520 | ext4_lock_group(sb, group); |
| 5521 | mb_free_blocks(NULL, e4b, start, ex.fe_len); |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5522 | return ret; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | /** |
| 5526 | * ext4_trim_all_free -- function to trim all free space in alloc. group |
| 5527 | * @sb: super block for file system |
Tao Ma | 2261228 | 2011-07-11 00:04:34 -0400 | [diff] [blame] | 5528 | * @group: group to be trimmed |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5529 | * @start: first group block to examine |
| 5530 | * @max: last group block to examine |
| 5531 | * @minblocks: minimum extent block count |
| 5532 | * |
| 5533 | * ext4_trim_all_free walks through group's buddy bitmap searching for free |
| 5534 | * extents. When the free block is found, ext4_trim_extent is called to TRIM |
| 5535 | * the extent. |
| 5536 | * |
| 5537 | * |
| 5538 | * ext4_trim_all_free walks through group's block bitmap searching for free |
| 5539 | * extents. When the free extent is found, mark it as used in group buddy |
| 5540 | * bitmap. Then issue a TRIM command on this extent and free the extent in |
| 5541 | * the group buddy bitmap. This is done until whole group is scanned. |
| 5542 | */ |
Lukas Czerner | 0b75a84 | 2011-02-23 12:22:49 -0500 | [diff] [blame] | 5543 | static ext4_grpblk_t |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5544 | ext4_trim_all_free(struct super_block *sb, ext4_group_t group, |
| 5545 | ext4_grpblk_t start, ext4_grpblk_t max, |
| 5546 | ext4_grpblk_t minblocks) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5547 | { |
| 5548 | void *bitmap; |
Tao Ma | 169ddc3 | 2011-07-11 00:00:07 -0400 | [diff] [blame] | 5549 | ext4_grpblk_t next, count = 0, free_count = 0; |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5550 | struct ext4_buddy e4b; |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5551 | int ret = 0; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5552 | |
Tao Ma | b3d4c2b | 2011-07-11 00:01:52 -0400 | [diff] [blame] | 5553 | trace_ext4_trim_all_free(sb, group, start, max); |
| 5554 | |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5555 | ret = ext4_mb_load_buddy(sb, group, &e4b); |
| 5556 | if (ret) { |
Konstantin Khlebnikov | 9651e6b | 2017-05-21 22:35:23 -0400 | [diff] [blame] | 5557 | ext4_warning(sb, "Error %d loading buddy information for %u", |
| 5558 | ret, group); |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5559 | return ret; |
| 5560 | } |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5561 | bitmap = e4b.bd_bitmap; |
Lukas Czerner | 28739ee | 2011-05-24 18:28:07 -0400 | [diff] [blame] | 5562 | |
| 5563 | ext4_lock_group(sb, group); |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 5564 | if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) && |
| 5565 | minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks)) |
| 5566 | goto out; |
| 5567 | |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5568 | start = (e4b.bd_info->bb_first_free > start) ? |
| 5569 | e4b.bd_info->bb_first_free : start; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5570 | |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5571 | while (start <= max) { |
| 5572 | start = mb_find_next_zero_bit(bitmap, max + 1, start); |
| 5573 | if (start > max) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5574 | break; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5575 | next = mb_find_next_bit(bitmap, max + 1, start); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5576 | |
| 5577 | if ((next - start) >= minblocks) { |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5578 | ret = ext4_trim_extent(sb, start, |
| 5579 | next - start, group, &e4b); |
| 5580 | if (ret && ret != -EOPNOTSUPP) |
| 5581 | break; |
| 5582 | ret = 0; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5583 | count += next - start; |
| 5584 | } |
Tao Ma | 169ddc3 | 2011-07-11 00:00:07 -0400 | [diff] [blame] | 5585 | free_count += next - start; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5586 | start = next + 1; |
| 5587 | |
| 5588 | if (fatal_signal_pending(current)) { |
| 5589 | count = -ERESTARTSYS; |
| 5590 | break; |
| 5591 | } |
| 5592 | |
| 5593 | if (need_resched()) { |
| 5594 | ext4_unlock_group(sb, group); |
| 5595 | cond_resched(); |
| 5596 | ext4_lock_group(sb, group); |
| 5597 | } |
| 5598 | |
Tao Ma | 169ddc3 | 2011-07-11 00:00:07 -0400 | [diff] [blame] | 5599 | if ((e4b.bd_info->bb_free - free_count) < minblocks) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5600 | break; |
| 5601 | } |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 5602 | |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5603 | if (!ret) { |
| 5604 | ret = count; |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 5605 | EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info); |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5606 | } |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 5607 | out: |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5608 | ext4_unlock_group(sb, group); |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5609 | ext4_mb_unload_buddy(&e4b); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5610 | |
| 5611 | ext4_debug("trimmed %d blocks in the group %d\n", |
| 5612 | count, group); |
| 5613 | |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5614 | return ret; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | /** |
| 5618 | * ext4_trim_fs() -- trim ioctl handle function |
| 5619 | * @sb: superblock for filesystem |
| 5620 | * @range: fstrim_range structure |
| 5621 | * |
| 5622 | * start: First Byte to trim |
| 5623 | * len: number of Bytes to trim from start |
| 5624 | * minlen: minimum extent length in Bytes |
| 5625 | * ext4_trim_fs goes through all allocation groups containing Bytes from |
| 5626 | * start to start+len. For each such a group ext4_trim_all_free function |
| 5627 | * is invoked to trim all free space. |
| 5628 | */ |
| 5629 | int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) |
| 5630 | { |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5631 | struct ext4_group_info *grp; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5632 | ext4_group_t group, first_group, last_group; |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 5633 | ext4_grpblk_t cnt = 0, first_cluster, last_cluster; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5634 | uint64_t start, end, minlen, trimmed = 0; |
Jan Kara | 0f0a25b | 2011-01-11 15:16:31 -0500 | [diff] [blame] | 5635 | ext4_fsblk_t first_data_blk = |
| 5636 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5637 | 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] | 5638 | int ret = 0; |
| 5639 | |
| 5640 | start = range->start >> sb->s_blocksize_bits; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5641 | end = start + (range->len >> sb->s_blocksize_bits) - 1; |
Lukas Czerner | aaf7d73 | 2012-09-26 22:21:21 -0400 | [diff] [blame] | 5642 | minlen = EXT4_NUM_B2C(EXT4_SB(sb), |
| 5643 | range->minlen >> sb->s_blocksize_bits); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5644 | |
Lukas Czerner | 5de35e8 | 2012-10-22 18:01:19 -0400 | [diff] [blame] | 5645 | if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) || |
| 5646 | start >= max_blks || |
| 5647 | range->len < sb->s_blocksize) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5648 | return -EINVAL; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5649 | if (end >= max_blks) |
| 5650 | end = max_blks - 1; |
| 5651 | if (end <= first_data_blk) |
Tao Ma | 22f1045 | 2011-07-10 23:52:37 -0400 | [diff] [blame] | 5652 | goto out; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5653 | if (start < first_data_blk) |
Jan Kara | 0f0a25b | 2011-01-11 15:16:31 -0500 | [diff] [blame] | 5654 | start = first_data_blk; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5655 | |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5656 | /* Determine first and last group to examine based on start and end */ |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5657 | ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start, |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 5658 | &first_group, &first_cluster); |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5659 | ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end, |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 5660 | &last_group, &last_cluster); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5661 | |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5662 | /* end now represents the last cluster to discard in this group */ |
| 5663 | end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5664 | |
| 5665 | for (group = first_group; group <= last_group; group++) { |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5666 | grp = ext4_get_group_info(sb, group); |
| 5667 | /* We only do this if the grp has never been initialized */ |
| 5668 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 5669 | ret = ext4_mb_init_group(sb, group, GFP_NOFS); |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5670 | if (ret) |
| 5671 | break; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5672 | } |
| 5673 | |
Tao Ma | 0ba0851 | 2011-03-23 15:48:11 -0400 | [diff] [blame] | 5674 | /* |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5675 | * For all the groups except the last one, last cluster will |
| 5676 | * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to |
| 5677 | * change it for the last group, note that last_cluster is |
| 5678 | * already computed earlier by ext4_get_group_no_and_offset() |
Tao Ma | 0ba0851 | 2011-03-23 15:48:11 -0400 | [diff] [blame] | 5679 | */ |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5680 | if (group == last_group) |
| 5681 | end = last_cluster; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5682 | |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 5683 | if (grp->bb_free >= minlen) { |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 5684 | cnt = ext4_trim_all_free(sb, group, first_cluster, |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5685 | end, minlen); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5686 | if (cnt < 0) { |
| 5687 | ret = cnt; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5688 | break; |
| 5689 | } |
Lukas Czerner | 21e7fd2 | 2012-03-21 21:24:22 -0400 | [diff] [blame] | 5690 | trimmed += cnt; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5691 | } |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 5692 | |
| 5693 | /* |
| 5694 | * For every group except the first one, we are sure |
| 5695 | * that the first cluster to discard will be cluster #0. |
| 5696 | */ |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 5697 | first_cluster = 0; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5698 | } |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5699 | |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 5700 | if (!ret) |
| 5701 | atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen); |
| 5702 | |
Tao Ma | 22f1045 | 2011-07-10 23:52:37 -0400 | [diff] [blame] | 5703 | out: |
Lukas Czerner | aaf7d73 | 2012-09-26 22:21:21 -0400 | [diff] [blame] | 5704 | range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 5705 | return ret; |
| 5706 | } |
Darrick J. Wong | 0c9ec4b | 2017-04-30 00:36:53 -0400 | [diff] [blame] | 5707 | |
| 5708 | /* Iterate all the free extents in the group. */ |
| 5709 | int |
| 5710 | ext4_mballoc_query_range( |
| 5711 | struct super_block *sb, |
| 5712 | ext4_group_t group, |
| 5713 | ext4_grpblk_t start, |
| 5714 | ext4_grpblk_t end, |
| 5715 | ext4_mballoc_query_range_fn formatter, |
| 5716 | void *priv) |
| 5717 | { |
| 5718 | void *bitmap; |
| 5719 | ext4_grpblk_t next; |
| 5720 | struct ext4_buddy e4b; |
| 5721 | int error; |
| 5722 | |
| 5723 | error = ext4_mb_load_buddy(sb, group, &e4b); |
| 5724 | if (error) |
| 5725 | return error; |
| 5726 | bitmap = e4b.bd_bitmap; |
| 5727 | |
| 5728 | ext4_lock_group(sb, group); |
| 5729 | |
| 5730 | start = (e4b.bd_info->bb_first_free > start) ? |
| 5731 | e4b.bd_info->bb_first_free : start; |
| 5732 | if (end >= EXT4_CLUSTERS_PER_GROUP(sb)) |
| 5733 | end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; |
| 5734 | |
| 5735 | while (start <= end) { |
| 5736 | start = mb_find_next_zero_bit(bitmap, end + 1, start); |
| 5737 | if (start > end) |
| 5738 | break; |
| 5739 | next = mb_find_next_bit(bitmap, end + 1, start); |
| 5740 | |
| 5741 | ext4_unlock_group(sb, group); |
| 5742 | error = formatter(sb, group, start, next - start, priv); |
| 5743 | if (error) |
| 5744 | goto out_unload; |
| 5745 | ext4_lock_group(sb, group); |
| 5746 | |
| 5747 | start = next + 1; |
| 5748 | } |
| 5749 | |
| 5750 | ext4_unlock_group(sb, group); |
| 5751 | out_unload: |
| 5752 | ext4_mb_unload_buddy(&e4b); |
| 5753 | |
| 5754 | return error; |
| 5755 | } |