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 |
Randy Dunlap | b483bb7 | 2020-08-04 19:48:50 -0700 | [diff] [blame] | 127 | * smallest multiple of the stripe value (sbi->s_stripe) which is |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 128 | * greater than the default mb_group_prealloc. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 129 | * |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 130 | * If "mb_optimize_scan" mount option is set, we maintain in memory group info |
| 131 | * structures in two data structures: |
| 132 | * |
| 133 | * 1) Array of largest free order lists (sbi->s_mb_largest_free_orders) |
| 134 | * |
| 135 | * Locking: sbi->s_mb_largest_free_orders_locks(array of rw locks) |
| 136 | * |
| 137 | * This is an array of lists where the index in the array represents the |
| 138 | * largest free order in the buddy bitmap of the participating group infos of |
| 139 | * that list. So, there are exactly MB_NUM_ORDERS(sb) (which means total |
| 140 | * number of buddy bitmap orders possible) number of lists. Group-infos are |
| 141 | * placed in appropriate lists. |
| 142 | * |
| 143 | * 2) Average fragment size rb tree (sbi->s_mb_avg_fragment_size_root) |
| 144 | * |
| 145 | * Locking: sbi->s_mb_rb_lock (rwlock) |
| 146 | * |
| 147 | * This is a red black tree consisting of group infos and the tree is sorted |
| 148 | * by average fragment sizes (which is calculated as ext4_group_info->bb_free |
| 149 | * / ext4_group_info->bb_fragments). |
| 150 | * |
| 151 | * When "mb_optimize_scan" mount option is set, mballoc consults the above data |
| 152 | * structures to decide the order in which groups are to be traversed for |
| 153 | * fulfilling an allocation request. |
| 154 | * |
| 155 | * At CR = 0, we look for groups which have the largest_free_order >= the order |
| 156 | * of the request. We directly look at the largest free order list in the data |
| 157 | * structure (1) above where largest_free_order = order of the request. If that |
| 158 | * list is empty, we look at remaining list in the increasing order of |
| 159 | * largest_free_order. This allows us to perform CR = 0 lookup in O(1) time. |
| 160 | * |
| 161 | * At CR = 1, we only consider groups where average fragment size > request |
| 162 | * size. So, we lookup a group which has average fragment size just above or |
| 163 | * equal to request size using our rb tree (data structure 2) in O(log N) time. |
| 164 | * |
| 165 | * If "mb_optimize_scan" mount option is not set, mballoc traverses groups in |
| 166 | * linear order which requires O(N) search time for each CR 0 and CR 1 phase. |
| 167 | * |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 168 | * The regular allocator (using the buddy cache) supports a few tunables. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 169 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 170 | * /sys/fs/ext4/<partition>/mb_min_to_scan |
| 171 | * /sys/fs/ext4/<partition>/mb_max_to_scan |
| 172 | * /sys/fs/ext4/<partition>/mb_order2_req |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 173 | * /sys/fs/ext4/<partition>/mb_linear_limit |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 174 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 175 | * 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] | 176 | * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The |
| 177 | * value of s_mb_order2_reqs can be tuned via |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 178 | * /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] | 179 | * 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] | 180 | * stripe size. This should result in better allocation on RAID setups. If |
| 181 | * not, we search in the specific group using bitmap for best extents. The |
| 182 | * tunable min_to_scan and max_to_scan control the behaviour here. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 183 | * 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] | 184 | * 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] | 185 | * best extent in the found extents. Searching for the blocks starts with |
| 186 | * the group specified as the goal value in allocation context via |
| 187 | * 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] | 188 | * 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] | 189 | * checked. |
| 190 | * |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 191 | * When "mb_optimize_scan" is turned on, as mentioned above, the groups may not |
| 192 | * get traversed linearly. That may result in subsequent allocations being not |
| 193 | * close to each other. And so, the underlying device may get filled up in a |
| 194 | * non-linear fashion. While that may not matter on non-rotational devices, for |
| 195 | * rotational devices that may result in higher seek times. "mb_linear_limit" |
| 196 | * tells mballoc how many groups mballoc should search linearly before |
| 197 | * performing consulting above data structures for more efficient lookups. For |
| 198 | * non rotational devices, this value defaults to 0 and for rotational devices |
| 199 | * this is set to MB_DEFAULT_LINEAR_LIMIT. |
| 200 | * |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 201 | * Both the prealloc space are getting populated as above. So for the first |
| 202 | * request we will hit the buddy cache which will result in this prealloc |
| 203 | * space getting filled. The prealloc space is then later used for the |
| 204 | * subsequent request. |
| 205 | */ |
| 206 | |
| 207 | /* |
| 208 | * mballoc operates on the following data: |
| 209 | * - on-disk bitmap |
| 210 | * - in-core buddy (actually includes buddy and bitmap) |
| 211 | * - preallocation descriptors (PAs) |
| 212 | * |
| 213 | * there are two types of preallocations: |
| 214 | * - inode |
| 215 | * assiged to specific inode and can be used for this inode only. |
| 216 | * it describes part of inode's space preallocated to specific |
| 217 | * physical blocks. any block from that preallocated can be used |
| 218 | * independent. the descriptor just tracks number of blocks left |
| 219 | * unused. so, before taking some block from descriptor, one must |
| 220 | * make sure corresponded logical block isn't allocated yet. this |
| 221 | * also means that freeing any block within descriptor's range |
| 222 | * must discard all preallocated blocks. |
| 223 | * - locality group |
| 224 | * assigned to specific locality group which does not translate to |
| 225 | * permanent set of inodes: inode can join and leave group. space |
| 226 | * from this type of preallocation can be used for any inode. thus |
| 227 | * it's consumed from the beginning to the end. |
| 228 | * |
| 229 | * relation between them can be expressed as: |
| 230 | * in-core buddy = on-disk bitmap + preallocation descriptors |
| 231 | * |
| 232 | * this mean blocks mballoc considers used are: |
| 233 | * - allocated blocks (persistent) |
| 234 | * - preallocated blocks (non-persistent) |
| 235 | * |
| 236 | * consistency in mballoc world means that at any time a block is either |
| 237 | * free or used in ALL structures. notice: "any time" should not be read |
| 238 | * literally -- time is discrete and delimited by locks. |
| 239 | * |
| 240 | * to keep it simple, we don't use block numbers, instead we count number of |
| 241 | * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA. |
| 242 | * |
| 243 | * all operations can be expressed as: |
| 244 | * - init buddy: buddy = on-disk + PAs |
| 245 | * - new PA: buddy += N; PA = N |
| 246 | * - use inode PA: on-disk += N; PA -= N |
| 247 | * - discard inode PA buddy -= on-disk - PA; PA = 0 |
| 248 | * - use locality group PA on-disk += N; PA -= N |
| 249 | * - discard locality group PA buddy -= PA; PA = 0 |
| 250 | * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap |
| 251 | * is used in real operation because we can't know actual used |
| 252 | * bits from PA, only from on-disk bitmap |
| 253 | * |
| 254 | * if we follow this strict logic, then all operations above should be atomic. |
| 255 | * given some of them can block, we'd have to use something like semaphores |
| 256 | * killing performance on high-end SMP hardware. let's try to relax it using |
| 257 | * the following knowledge: |
| 258 | * 1) if buddy is referenced, it's already initialized |
| 259 | * 2) while block is used in buddy and the buddy is referenced, |
| 260 | * nobody can re-allocate that block |
| 261 | * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has |
| 262 | * bit set and PA claims same block, it's OK. IOW, one can set bit in |
| 263 | * on-disk bitmap if buddy has same bit set or/and PA covers corresponded |
| 264 | * block |
| 265 | * |
| 266 | * so, now we're building a concurrency table: |
| 267 | * - init buddy vs. |
| 268 | * - new PA |
| 269 | * blocks for PA are allocated in the buddy, buddy must be referenced |
| 270 | * until PA is linked to allocation group to avoid concurrent buddy init |
| 271 | * - use inode PA |
| 272 | * we need to make sure that either on-disk bitmap or PA has uptodate data |
| 273 | * given (3) we care that PA-=N operation doesn't interfere with init |
| 274 | * - discard inode PA |
| 275 | * the simplest way would be to have buddy initialized by the discard |
| 276 | * - use locality group PA |
| 277 | * again PA-=N must be serialized with init |
| 278 | * - discard locality group PA |
| 279 | * the simplest way would be to have buddy initialized by the discard |
| 280 | * - new PA vs. |
| 281 | * - use inode PA |
| 282 | * i_data_sem serializes them |
| 283 | * - discard inode PA |
| 284 | * discard process must wait until PA isn't used by another process |
| 285 | * - use locality group PA |
| 286 | * some mutex should serialize them |
| 287 | * - discard locality group PA |
| 288 | * discard process must wait until PA isn't used by another process |
| 289 | * - use inode PA |
| 290 | * - use inode PA |
| 291 | * i_data_sem or another mutex should serializes them |
| 292 | * - discard inode PA |
| 293 | * discard process must wait until PA isn't used by another process |
| 294 | * - use locality group PA |
| 295 | * nothing wrong here -- they're different PAs covering different blocks |
| 296 | * - discard locality group PA |
| 297 | * discard process must wait until PA isn't used by another process |
| 298 | * |
| 299 | * now we're ready to make few consequences: |
| 300 | * - PA is referenced and while it is no discard is possible |
| 301 | * - PA is referenced until block isn't marked in on-disk bitmap |
| 302 | * - PA changes only after on-disk bitmap |
| 303 | * - discard must not compete with init. either init is done before |
| 304 | * any discard or they're serialized somehow |
| 305 | * - buddy init as sum of on-disk bitmap and PAs is done atomically |
| 306 | * |
| 307 | * a special case when we've used PA to emptiness. no need to modify buddy |
| 308 | * in this case, but we should care about concurrent init |
| 309 | * |
| 310 | */ |
| 311 | |
| 312 | /* |
| 313 | * Logic in few words: |
| 314 | * |
| 315 | * - allocation: |
| 316 | * load group |
| 317 | * find blocks |
| 318 | * mark bits in on-disk bitmap |
| 319 | * release group |
| 320 | * |
| 321 | * - use preallocation: |
| 322 | * find proper PA (per-inode or group) |
| 323 | * load group |
| 324 | * mark bits in on-disk bitmap |
| 325 | * release group |
| 326 | * release PA |
| 327 | * |
| 328 | * - free: |
| 329 | * load group |
| 330 | * mark bits in on-disk bitmap |
| 331 | * release group |
| 332 | * |
| 333 | * - discard preallocations in group: |
| 334 | * mark PAs deleted |
| 335 | * move them onto local list |
| 336 | * load on-disk bitmap |
| 337 | * load group |
| 338 | * remove PA from object (inode or locality group) |
| 339 | * mark free blocks in-core |
| 340 | * |
| 341 | * - discard inode's preallocations: |
| 342 | */ |
| 343 | |
| 344 | /* |
| 345 | * Locking rules |
| 346 | * |
| 347 | * Locks: |
| 348 | * - bitlock on a group (group) |
| 349 | * - object (inode/locality) (object) |
| 350 | * - per-pa lock (pa) |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 351 | * - cr0 lists lock (cr0) |
| 352 | * - cr1 tree lock (cr1) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 353 | * |
| 354 | * Paths: |
| 355 | * - new pa |
| 356 | * object |
| 357 | * group |
| 358 | * |
| 359 | * - find and use pa: |
| 360 | * pa |
| 361 | * |
| 362 | * - release consumed pa: |
| 363 | * pa |
| 364 | * group |
| 365 | * object |
| 366 | * |
| 367 | * - generate in-core bitmap: |
| 368 | * group |
| 369 | * pa |
| 370 | * |
| 371 | * - discard all for given object (inode, locality group): |
| 372 | * object |
| 373 | * pa |
| 374 | * group |
| 375 | * |
| 376 | * - discard all for given group: |
| 377 | * group |
| 378 | * pa |
| 379 | * group |
| 380 | * object |
| 381 | * |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 382 | * - allocation path (ext4_mb_regular_allocator) |
| 383 | * group |
| 384 | * cr0/cr1 |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 385 | */ |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 386 | static struct kmem_cache *ext4_pspace_cachep; |
| 387 | static struct kmem_cache *ext4_ac_cachep; |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 388 | static struct kmem_cache *ext4_free_data_cachep; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 389 | |
| 390 | /* We create slab caches for groupinfo data structures based on the |
| 391 | * superblock block size. There will be one per mounted filesystem for |
| 392 | * each unique s_blocksize_bits */ |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 393 | #define NR_GRPINFO_CACHES 8 |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 394 | static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES]; |
| 395 | |
Eric Biggers | d600618 | 2017-04-29 23:47:50 -0400 | [diff] [blame] | 396 | static const char * const ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = { |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 397 | "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k", |
| 398 | "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k", |
| 399 | "ext4_groupinfo_64k", "ext4_groupinfo_128k" |
| 400 | }; |
| 401 | |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 402 | static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
| 403 | ext4_group_t group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 404 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, |
| 405 | ext4_group_t group); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 406 | 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] | 407 | |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 408 | static bool ext4_mb_good_group(struct ext4_allocation_context *ac, |
| 409 | ext4_group_t group, int cr); |
| 410 | |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 411 | /* |
| 412 | * The algorithm using this percpu seq counter goes below: |
| 413 | * 1. We sample the percpu discard_pa_seq counter before trying for block |
| 414 | * allocation in ext4_mb_new_blocks(). |
| 415 | * 2. We increment this percpu discard_pa_seq counter when we either allocate |
| 416 | * or free these blocks i.e. while marking those blocks as used/free in |
| 417 | * mb_mark_used()/mb_free_blocks(). |
| 418 | * 3. We also increment this percpu seq counter when we successfully identify |
| 419 | * that the bb_prealloc_list is not empty and hence proceed for discarding |
| 420 | * of those PAs inside ext4_mb_discard_group_preallocations(). |
| 421 | * |
| 422 | * Now to make sure that the regular fast path of block allocation is not |
| 423 | * affected, as a small optimization we only sample the percpu seq counter |
| 424 | * on that cpu. Only when the block allocation fails and when freed blocks |
| 425 | * found were 0, that is when we sample percpu seq counter for all cpus using |
| 426 | * below function ext4_get_discard_pa_seq_sum(). This happens after making |
| 427 | * sure that all the PAs on grp->bb_prealloc_list got freed or if it's empty. |
| 428 | */ |
| 429 | static DEFINE_PER_CPU(u64, discard_pa_seq); |
| 430 | static inline u64 ext4_get_discard_pa_seq_sum(void) |
| 431 | { |
| 432 | int __cpu; |
| 433 | u64 __seq = 0; |
| 434 | |
| 435 | for_each_possible_cpu(__cpu) |
| 436 | __seq += per_cpu(discard_pa_seq, __cpu); |
| 437 | return __seq; |
| 438 | } |
| 439 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 440 | static inline void *mb_correct_addr_and_bit(int *bit, void *addr) |
| 441 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 442 | #if BITS_PER_LONG == 64 |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 443 | *bit += ((unsigned long) addr & 7UL) << 3; |
| 444 | addr = (void *) ((unsigned long) addr & ~7UL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 445 | #elif BITS_PER_LONG == 32 |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 446 | *bit += ((unsigned long) addr & 3UL) << 3; |
| 447 | addr = (void *) ((unsigned long) addr & ~3UL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 448 | #else |
| 449 | #error "how many bits you are?!" |
| 450 | #endif |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 451 | return addr; |
| 452 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 453 | |
| 454 | static inline int mb_test_bit(int bit, void *addr) |
| 455 | { |
| 456 | /* |
| 457 | * ext4_test_bit on architecture like powerpc |
| 458 | * needs unsigned long aligned address |
| 459 | */ |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 460 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 461 | return ext4_test_bit(bit, addr); |
| 462 | } |
| 463 | |
| 464 | static inline void mb_set_bit(int bit, void *addr) |
| 465 | { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 466 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 467 | ext4_set_bit(bit, addr); |
| 468 | } |
| 469 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 470 | static inline void mb_clear_bit(int bit, void *addr) |
| 471 | { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 472 | addr = mb_correct_addr_and_bit(&bit, addr); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 473 | ext4_clear_bit(bit, addr); |
| 474 | } |
| 475 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 476 | static inline int mb_test_and_clear_bit(int bit, void *addr) |
| 477 | { |
| 478 | addr = mb_correct_addr_and_bit(&bit, addr); |
| 479 | return ext4_test_and_clear_bit(bit, addr); |
| 480 | } |
| 481 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 482 | static inline int mb_find_next_zero_bit(void *addr, int max, int start) |
| 483 | { |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 484 | int fix = 0, ret, tmpmax; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 485 | addr = mb_correct_addr_and_bit(&fix, addr); |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 486 | tmpmax = max + fix; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 487 | start += fix; |
| 488 | |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 489 | ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix; |
| 490 | if (ret > max) |
| 491 | return max; |
| 492 | return ret; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static inline int mb_find_next_bit(void *addr, int max, int start) |
| 496 | { |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 497 | int fix = 0, ret, tmpmax; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 498 | addr = mb_correct_addr_and_bit(&fix, addr); |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 499 | tmpmax = max + fix; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 500 | start += fix; |
| 501 | |
Aneesh Kumar K.V | e7dfb24 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 502 | ret = ext4_find_next_bit(addr, tmpmax, start) - fix; |
| 503 | if (ret > max) |
| 504 | return max; |
| 505 | return ret; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 508 | static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max) |
| 509 | { |
| 510 | char *bb; |
| 511 | |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 512 | BUG_ON(e4b->bd_bitmap == e4b->bd_buddy); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 513 | BUG_ON(max == NULL); |
| 514 | |
| 515 | if (order > e4b->bd_blkbits + 1) { |
| 516 | *max = 0; |
| 517 | return NULL; |
| 518 | } |
| 519 | |
| 520 | /* at order 0 we see each particular block */ |
Coly Li | 84b775a | 2011-02-24 12:51:59 -0500 | [diff] [blame] | 521 | if (order == 0) { |
| 522 | *max = 1 << (e4b->bd_blkbits + 3); |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 523 | return e4b->bd_bitmap; |
Coly Li | 84b775a | 2011-02-24 12:51:59 -0500 | [diff] [blame] | 524 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 525 | |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 526 | 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] | 527 | *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order]; |
| 528 | |
| 529 | return bb; |
| 530 | } |
| 531 | |
| 532 | #ifdef DOUBLE_CHECK |
| 533 | static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b, |
| 534 | int first, int count) |
| 535 | { |
| 536 | int i; |
| 537 | struct super_block *sb = e4b->bd_sb; |
| 538 | |
| 539 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) |
| 540 | return; |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 541 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 542 | for (i = 0; i < count; i++) { |
| 543 | if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) { |
| 544 | ext4_fsblk_t blocknr; |
Akinobu Mita | 5661bd6 | 2010-03-03 23:53:39 -0500 | [diff] [blame] | 545 | |
| 546 | blocknr = ext4_group_first_block_no(sb, e4b->bd_group); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 547 | blocknr += EXT4_C2B(EXT4_SB(sb), first + i); |
Aneesh Kumar K.V | 5d1b1b3 | 2009-01-05 22:19:52 -0500 | [diff] [blame] | 548 | ext4_grp_locked_error(sb, e4b->bd_group, |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 549 | inode ? inode->i_ino : 0, |
| 550 | blocknr, |
| 551 | "freeing block already freed " |
| 552 | "(bit %u)", |
| 553 | first + i); |
Wang Shilong | 736dedb | 2018-05-12 12:37:58 -0400 | [diff] [blame] | 554 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
| 555 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 556 | } |
| 557 | mb_clear_bit(first + i, e4b->bd_info->bb_bitmap); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count) |
| 562 | { |
| 563 | int i; |
| 564 | |
| 565 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) |
| 566 | return; |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 567 | 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] | 568 | for (i = 0; i < count; i++) { |
| 569 | BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap)); |
| 570 | mb_set_bit(first + i, e4b->bd_info->bb_bitmap); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) |
| 575 | { |
Ritesh Harjani | eb2b8eb | 2020-05-10 11:54:49 +0530 | [diff] [blame] | 576 | if (unlikely(e4b->bd_info->bb_bitmap == NULL)) |
| 577 | return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 578 | if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) { |
| 579 | unsigned char *b1, *b2; |
| 580 | int i; |
| 581 | b1 = (unsigned char *) e4b->bd_info->bb_bitmap; |
| 582 | b2 = (unsigned char *) bitmap; |
| 583 | for (i = 0; i < e4b->bd_sb->s_blocksize; i++) { |
| 584 | if (b1[i] != b2[i]) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 585 | ext4_msg(e4b->bd_sb, KERN_ERR, |
| 586 | "corruption in group %u " |
| 587 | "at byte %u(%u): %x in copy != %x " |
| 588 | "on disk/prealloc", |
| 589 | e4b->bd_group, i, i * 8, b1[i], b2[i]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 590 | BUG(); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 596 | static void mb_group_bb_bitmap_alloc(struct super_block *sb, |
| 597 | struct ext4_group_info *grp, ext4_group_t group) |
| 598 | { |
| 599 | struct buffer_head *bh; |
| 600 | |
| 601 | grp->bb_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS); |
Ritesh Harjani | eb2b8eb | 2020-05-10 11:54:49 +0530 | [diff] [blame] | 602 | if (!grp->bb_bitmap) |
| 603 | return; |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 604 | |
| 605 | bh = ext4_read_block_bitmap(sb, group); |
Ritesh Harjani | eb2b8eb | 2020-05-10 11:54:49 +0530 | [diff] [blame] | 606 | if (IS_ERR_OR_NULL(bh)) { |
| 607 | kfree(grp->bb_bitmap); |
| 608 | grp->bb_bitmap = NULL; |
| 609 | return; |
| 610 | } |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 611 | |
| 612 | memcpy(grp->bb_bitmap, bh->b_data, sb->s_blocksize); |
| 613 | put_bh(bh); |
| 614 | } |
| 615 | |
| 616 | static void mb_group_bb_bitmap_free(struct ext4_group_info *grp) |
| 617 | { |
| 618 | kfree(grp->bb_bitmap); |
| 619 | } |
| 620 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 621 | #else |
| 622 | static inline void mb_free_blocks_double(struct inode *inode, |
| 623 | struct ext4_buddy *e4b, int first, int count) |
| 624 | { |
| 625 | return; |
| 626 | } |
| 627 | static inline void mb_mark_used_double(struct ext4_buddy *e4b, |
| 628 | int first, int count) |
| 629 | { |
| 630 | return; |
| 631 | } |
| 632 | static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) |
| 633 | { |
| 634 | return; |
| 635 | } |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 636 | |
| 637 | static inline void mb_group_bb_bitmap_alloc(struct super_block *sb, |
| 638 | struct ext4_group_info *grp, ext4_group_t group) |
| 639 | { |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | static inline void mb_group_bb_bitmap_free(struct ext4_group_info *grp) |
| 644 | { |
| 645 | return; |
| 646 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 647 | #endif |
| 648 | |
| 649 | #ifdef AGGRESSIVE_CHECK |
| 650 | |
| 651 | #define MB_CHECK_ASSERT(assert) \ |
| 652 | do { \ |
| 653 | if (!(assert)) { \ |
| 654 | printk(KERN_EMERG \ |
| 655 | "Assertion failure in %s() at %s:%d: \"%s\"\n", \ |
| 656 | function, file, line, # assert); \ |
| 657 | BUG(); \ |
| 658 | } \ |
| 659 | } while (0) |
| 660 | |
| 661 | static int __mb_check_buddy(struct ext4_buddy *e4b, char *file, |
| 662 | const char *function, int line) |
| 663 | { |
| 664 | struct super_block *sb = e4b->bd_sb; |
| 665 | int order = e4b->bd_blkbits + 1; |
| 666 | int max; |
| 667 | int max2; |
| 668 | int i; |
| 669 | int j; |
| 670 | int k; |
| 671 | int count; |
| 672 | struct ext4_group_info *grp; |
| 673 | int fragments = 0; |
| 674 | int fstart; |
| 675 | struct list_head *cur; |
| 676 | void *buddy; |
| 677 | void *buddy2; |
| 678 | |
Chunguang Xu | addd752 | 2020-09-28 19:36:35 +0800 | [diff] [blame] | 679 | if (e4b->bd_info->bb_check_counter++ % 10) |
| 680 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 681 | |
| 682 | while (order > 1) { |
| 683 | buddy = mb_find_buddy(e4b, order, &max); |
| 684 | MB_CHECK_ASSERT(buddy); |
| 685 | buddy2 = mb_find_buddy(e4b, order - 1, &max2); |
| 686 | MB_CHECK_ASSERT(buddy2); |
| 687 | MB_CHECK_ASSERT(buddy != buddy2); |
| 688 | MB_CHECK_ASSERT(max * 2 == max2); |
| 689 | |
| 690 | count = 0; |
| 691 | for (i = 0; i < max; i++) { |
| 692 | |
| 693 | if (mb_test_bit(i, buddy)) { |
| 694 | /* only single bit in buddy2 may be 1 */ |
| 695 | if (!mb_test_bit(i << 1, buddy2)) { |
| 696 | MB_CHECK_ASSERT( |
| 697 | mb_test_bit((i<<1)+1, buddy2)); |
| 698 | } else if (!mb_test_bit((i << 1) + 1, buddy2)) { |
| 699 | MB_CHECK_ASSERT( |
| 700 | mb_test_bit(i << 1, buddy2)); |
| 701 | } |
| 702 | continue; |
| 703 | } |
| 704 | |
Robin Dong | 0a10da7 | 2011-10-26 08:48:54 -0400 | [diff] [blame] | 705 | /* both bits in buddy2 must be 1 */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 706 | MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2)); |
| 707 | MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2)); |
| 708 | |
| 709 | for (j = 0; j < (1 << order); j++) { |
| 710 | k = (i * (1 << order)) + j; |
| 711 | MB_CHECK_ASSERT( |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 712 | !mb_test_bit(k, e4b->bd_bitmap)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 713 | } |
| 714 | count++; |
| 715 | } |
| 716 | MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count); |
| 717 | order--; |
| 718 | } |
| 719 | |
| 720 | fstart = -1; |
| 721 | buddy = mb_find_buddy(e4b, 0, &max); |
| 722 | for (i = 0; i < max; i++) { |
| 723 | if (!mb_test_bit(i, buddy)) { |
| 724 | MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free); |
| 725 | if (fstart == -1) { |
| 726 | fragments++; |
| 727 | fstart = i; |
| 728 | } |
| 729 | continue; |
| 730 | } |
| 731 | fstart = -1; |
| 732 | /* check used bits only */ |
| 733 | for (j = 0; j < e4b->bd_blkbits + 1; j++) { |
| 734 | buddy2 = mb_find_buddy(e4b, j, &max2); |
| 735 | k = i >> j; |
| 736 | MB_CHECK_ASSERT(k < max2); |
| 737 | MB_CHECK_ASSERT(mb_test_bit(k, buddy2)); |
| 738 | } |
| 739 | } |
| 740 | MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info)); |
| 741 | MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments); |
| 742 | |
| 743 | grp = ext4_get_group_info(sb, e4b->bd_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 744 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 745 | ext4_group_t groupnr; |
| 746 | struct ext4_prealloc_space *pa; |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 747 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 748 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 749 | MB_CHECK_ASSERT(groupnr == e4b->bd_group); |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 750 | for (i = 0; i < pa->pa_len; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 751 | MB_CHECK_ASSERT(mb_test_bit(k + i, buddy)); |
| 752 | } |
| 753 | return 0; |
| 754 | } |
| 755 | #undef MB_CHECK_ASSERT |
| 756 | #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \ |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 757 | __FILE__, __func__, __LINE__) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 758 | #else |
| 759 | #define mb_check_buddy(e4b) |
| 760 | #endif |
| 761 | |
Coly Li | 7c78605 | 2011-02-24 13:24:25 -0500 | [diff] [blame] | 762 | /* |
| 763 | * Divide blocks started from @first with length @len into |
| 764 | * smaller chunks with power of 2 blocks. |
| 765 | * Clear the bits in bitmap which the blocks of the chunk(s) covered, |
| 766 | * then increase bb_counters[] for corresponded chunk size. |
| 767 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 768 | static void ext4_mb_mark_free_simple(struct super_block *sb, |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 769 | void *buddy, ext4_grpblk_t first, ext4_grpblk_t len, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 770 | struct ext4_group_info *grp) |
| 771 | { |
| 772 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 773 | ext4_grpblk_t min; |
| 774 | ext4_grpblk_t max; |
| 775 | ext4_grpblk_t chunk; |
Chandan Rajendra | 69e43e8 | 2016-11-14 21:04:37 -0500 | [diff] [blame] | 776 | unsigned int border; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 777 | |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 778 | BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 779 | |
| 780 | border = 2 << sb->s_blocksize_bits; |
| 781 | |
| 782 | while (len > 0) { |
| 783 | /* find how many blocks can be covered since this position */ |
| 784 | max = ffs(first | border) - 1; |
| 785 | |
| 786 | /* find how many blocks of power 2 we need to mark */ |
| 787 | min = fls(len) - 1; |
| 788 | |
| 789 | if (max < min) |
| 790 | min = max; |
| 791 | chunk = 1 << min; |
| 792 | |
| 793 | /* mark multiblock chunks only */ |
| 794 | grp->bb_counters[min]++; |
| 795 | if (min > 0) |
| 796 | mb_clear_bit(first >> min, |
| 797 | buddy + sbi->s_mb_offsets[min]); |
| 798 | |
| 799 | len -= chunk; |
| 800 | first += chunk; |
| 801 | } |
| 802 | } |
| 803 | |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 804 | static void ext4_mb_rb_insert(struct rb_root *root, struct rb_node *new, |
| 805 | int (*cmp)(struct rb_node *, struct rb_node *)) |
| 806 | { |
| 807 | struct rb_node **iter = &root->rb_node, *parent = NULL; |
| 808 | |
| 809 | while (*iter) { |
| 810 | parent = *iter; |
| 811 | if (cmp(new, *iter) > 0) |
| 812 | iter = &((*iter)->rb_left); |
| 813 | else |
| 814 | iter = &((*iter)->rb_right); |
| 815 | } |
| 816 | |
| 817 | rb_link_node(new, parent, iter); |
| 818 | rb_insert_color(new, root); |
| 819 | } |
| 820 | |
| 821 | static int |
| 822 | ext4_mb_avg_fragment_size_cmp(struct rb_node *rb1, struct rb_node *rb2) |
| 823 | { |
| 824 | struct ext4_group_info *grp1 = rb_entry(rb1, |
| 825 | struct ext4_group_info, |
| 826 | bb_avg_fragment_size_rb); |
| 827 | struct ext4_group_info *grp2 = rb_entry(rb2, |
| 828 | struct ext4_group_info, |
| 829 | bb_avg_fragment_size_rb); |
| 830 | int num_frags_1, num_frags_2; |
| 831 | |
| 832 | num_frags_1 = grp1->bb_fragments ? |
| 833 | grp1->bb_free / grp1->bb_fragments : 0; |
| 834 | num_frags_2 = grp2->bb_fragments ? |
| 835 | grp2->bb_free / grp2->bb_fragments : 0; |
| 836 | |
| 837 | return (num_frags_2 - num_frags_1); |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Reinsert grpinfo into the avg_fragment_size tree with new average |
| 842 | * fragment size. |
| 843 | */ |
| 844 | static void |
| 845 | mb_update_avg_fragment_size(struct super_block *sb, struct ext4_group_info *grp) |
| 846 | { |
| 847 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 848 | |
| 849 | if (!test_opt2(sb, MB_OPTIMIZE_SCAN) || grp->bb_free == 0) |
| 850 | return; |
| 851 | |
| 852 | write_lock(&sbi->s_mb_rb_lock); |
| 853 | if (!RB_EMPTY_NODE(&grp->bb_avg_fragment_size_rb)) { |
| 854 | rb_erase(&grp->bb_avg_fragment_size_rb, |
| 855 | &sbi->s_mb_avg_fragment_size_root); |
| 856 | RB_CLEAR_NODE(&grp->bb_avg_fragment_size_rb); |
| 857 | } |
| 858 | |
| 859 | ext4_mb_rb_insert(&sbi->s_mb_avg_fragment_size_root, |
| 860 | &grp->bb_avg_fragment_size_rb, |
| 861 | ext4_mb_avg_fragment_size_cmp); |
| 862 | write_unlock(&sbi->s_mb_rb_lock); |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | * Choose next group by traversing largest_free_order lists. Updates *new_cr if |
| 867 | * cr level needs an update. |
| 868 | */ |
| 869 | static void ext4_mb_choose_next_group_cr0(struct ext4_allocation_context *ac, |
| 870 | int *new_cr, ext4_group_t *group, ext4_group_t ngroups) |
| 871 | { |
| 872 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 873 | struct ext4_group_info *iter, *grp; |
| 874 | int i; |
| 875 | |
| 876 | if (ac->ac_status == AC_STATUS_FOUND) |
| 877 | return; |
| 878 | |
| 879 | if (unlikely(sbi->s_mb_stats && ac->ac_flags & EXT4_MB_CR0_OPTIMIZED)) |
| 880 | atomic_inc(&sbi->s_bal_cr0_bad_suggestions); |
| 881 | |
| 882 | grp = NULL; |
| 883 | for (i = ac->ac_2order; i < MB_NUM_ORDERS(ac->ac_sb); i++) { |
| 884 | if (list_empty(&sbi->s_mb_largest_free_orders[i])) |
| 885 | continue; |
| 886 | read_lock(&sbi->s_mb_largest_free_orders_locks[i]); |
| 887 | if (list_empty(&sbi->s_mb_largest_free_orders[i])) { |
| 888 | read_unlock(&sbi->s_mb_largest_free_orders_locks[i]); |
| 889 | continue; |
| 890 | } |
| 891 | grp = NULL; |
| 892 | list_for_each_entry(iter, &sbi->s_mb_largest_free_orders[i], |
| 893 | bb_largest_free_order_node) { |
| 894 | if (sbi->s_mb_stats) |
| 895 | atomic64_inc(&sbi->s_bal_cX_groups_considered[0]); |
| 896 | if (likely(ext4_mb_good_group(ac, iter->bb_group, 0))) { |
| 897 | grp = iter; |
| 898 | break; |
| 899 | } |
| 900 | } |
| 901 | read_unlock(&sbi->s_mb_largest_free_orders_locks[i]); |
| 902 | if (grp) |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | if (!grp) { |
| 907 | /* Increment cr and search again */ |
| 908 | *new_cr = 1; |
| 909 | } else { |
| 910 | *group = grp->bb_group; |
| 911 | ac->ac_last_optimal_group = *group; |
| 912 | ac->ac_flags |= EXT4_MB_CR0_OPTIMIZED; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * Choose next group by traversing average fragment size tree. Updates *new_cr |
| 918 | * if cr lvel needs an update. Sets EXT4_MB_SEARCH_NEXT_LINEAR to indicate that |
| 919 | * the linear search should continue for one iteration since there's lock |
| 920 | * contention on the rb tree lock. |
| 921 | */ |
| 922 | static void ext4_mb_choose_next_group_cr1(struct ext4_allocation_context *ac, |
| 923 | int *new_cr, ext4_group_t *group, ext4_group_t ngroups) |
| 924 | { |
| 925 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 926 | int avg_fragment_size, best_so_far; |
| 927 | struct rb_node *node, *found; |
| 928 | struct ext4_group_info *grp; |
| 929 | |
| 930 | /* |
| 931 | * If there is contention on the lock, instead of waiting for the lock |
| 932 | * to become available, just continue searching lineraly. We'll resume |
| 933 | * our rb tree search later starting at ac->ac_last_optimal_group. |
| 934 | */ |
| 935 | if (!read_trylock(&sbi->s_mb_rb_lock)) { |
| 936 | ac->ac_flags |= EXT4_MB_SEARCH_NEXT_LINEAR; |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | if (unlikely(ac->ac_flags & EXT4_MB_CR1_OPTIMIZED)) { |
| 941 | if (sbi->s_mb_stats) |
| 942 | atomic_inc(&sbi->s_bal_cr1_bad_suggestions); |
| 943 | /* We have found something at CR 1 in the past */ |
| 944 | grp = ext4_get_group_info(ac->ac_sb, ac->ac_last_optimal_group); |
| 945 | for (found = rb_next(&grp->bb_avg_fragment_size_rb); found != NULL; |
| 946 | found = rb_next(found)) { |
| 947 | grp = rb_entry(found, struct ext4_group_info, |
| 948 | bb_avg_fragment_size_rb); |
| 949 | if (sbi->s_mb_stats) |
| 950 | atomic64_inc(&sbi->s_bal_cX_groups_considered[1]); |
| 951 | if (likely(ext4_mb_good_group(ac, grp->bb_group, 1))) |
| 952 | break; |
| 953 | } |
| 954 | goto done; |
| 955 | } |
| 956 | |
| 957 | node = sbi->s_mb_avg_fragment_size_root.rb_node; |
| 958 | best_so_far = 0; |
| 959 | found = NULL; |
| 960 | |
| 961 | while (node) { |
| 962 | grp = rb_entry(node, struct ext4_group_info, |
| 963 | bb_avg_fragment_size_rb); |
| 964 | avg_fragment_size = 0; |
| 965 | if (ext4_mb_good_group(ac, grp->bb_group, 1)) { |
| 966 | avg_fragment_size = grp->bb_fragments ? |
| 967 | grp->bb_free / grp->bb_fragments : 0; |
| 968 | if (!best_so_far || avg_fragment_size < best_so_far) { |
| 969 | best_so_far = avg_fragment_size; |
| 970 | found = node; |
| 971 | } |
| 972 | } |
| 973 | if (avg_fragment_size > ac->ac_g_ex.fe_len) |
| 974 | node = node->rb_right; |
| 975 | else |
| 976 | node = node->rb_left; |
| 977 | } |
| 978 | |
| 979 | done: |
| 980 | if (found) { |
| 981 | grp = rb_entry(found, struct ext4_group_info, |
| 982 | bb_avg_fragment_size_rb); |
| 983 | *group = grp->bb_group; |
| 984 | ac->ac_flags |= EXT4_MB_CR1_OPTIMIZED; |
| 985 | } else { |
| 986 | *new_cr = 2; |
| 987 | } |
| 988 | |
| 989 | read_unlock(&sbi->s_mb_rb_lock); |
| 990 | ac->ac_last_optimal_group = *group; |
| 991 | } |
| 992 | |
| 993 | static inline int should_optimize_scan(struct ext4_allocation_context *ac) |
| 994 | { |
| 995 | if (unlikely(!test_opt2(ac->ac_sb, MB_OPTIMIZE_SCAN))) |
| 996 | return 0; |
| 997 | if (ac->ac_criteria >= 2) |
| 998 | return 0; |
| 999 | if (ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) |
| 1000 | return 0; |
| 1001 | return 1; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Return next linear group for allocation. If linear traversal should not be |
| 1006 | * performed, this function just returns the same group |
| 1007 | */ |
| 1008 | static int |
| 1009 | next_linear_group(struct ext4_allocation_context *ac, int group, int ngroups) |
| 1010 | { |
| 1011 | if (!should_optimize_scan(ac)) |
| 1012 | goto inc_and_return; |
| 1013 | |
| 1014 | if (ac->ac_groups_linear_remaining) { |
| 1015 | ac->ac_groups_linear_remaining--; |
| 1016 | goto inc_and_return; |
| 1017 | } |
| 1018 | |
| 1019 | if (ac->ac_flags & EXT4_MB_SEARCH_NEXT_LINEAR) { |
| 1020 | ac->ac_flags &= ~EXT4_MB_SEARCH_NEXT_LINEAR; |
| 1021 | goto inc_and_return; |
| 1022 | } |
| 1023 | |
| 1024 | return group; |
| 1025 | inc_and_return: |
| 1026 | /* |
| 1027 | * Artificially restricted ngroups for non-extent |
| 1028 | * files makes group > ngroups possible on first loop. |
| 1029 | */ |
| 1030 | return group + 1 >= ngroups ? 0 : group + 1; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * ext4_mb_choose_next_group: choose next group for allocation. |
| 1035 | * |
| 1036 | * @ac Allocation Context |
| 1037 | * @new_cr This is an output parameter. If the there is no good group |
| 1038 | * available at current CR level, this field is updated to indicate |
| 1039 | * the new cr level that should be used. |
| 1040 | * @group This is an input / output parameter. As an input it indicates the |
| 1041 | * next group that the allocator intends to use for allocation. As |
| 1042 | * output, this field indicates the next group that should be used as |
| 1043 | * determined by the optimization functions. |
| 1044 | * @ngroups Total number of groups |
| 1045 | */ |
| 1046 | static void ext4_mb_choose_next_group(struct ext4_allocation_context *ac, |
| 1047 | int *new_cr, ext4_group_t *group, ext4_group_t ngroups) |
| 1048 | { |
| 1049 | *new_cr = ac->ac_criteria; |
| 1050 | |
| 1051 | if (!should_optimize_scan(ac) || ac->ac_groups_linear_remaining) |
| 1052 | return; |
| 1053 | |
| 1054 | if (*new_cr == 0) { |
| 1055 | ext4_mb_choose_next_group_cr0(ac, new_cr, group, ngroups); |
| 1056 | } else if (*new_cr == 1) { |
| 1057 | ext4_mb_choose_next_group_cr1(ac, new_cr, group, ngroups); |
| 1058 | } else { |
| 1059 | /* |
| 1060 | * TODO: For CR=2, we can arrange groups in an rb tree sorted by |
| 1061 | * bb_free. But until that happens, we should never come here. |
| 1062 | */ |
| 1063 | WARN_ON(1); |
| 1064 | } |
| 1065 | } |
| 1066 | |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1067 | /* |
| 1068 | * Cache the order of the largest free extent we have available in this block |
| 1069 | * group. |
| 1070 | */ |
| 1071 | static void |
| 1072 | mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp) |
| 1073 | { |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 1074 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1075 | int i; |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1076 | |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 1077 | if (test_opt2(sb, MB_OPTIMIZE_SCAN) && grp->bb_largest_free_order >= 0) { |
| 1078 | write_lock(&sbi->s_mb_largest_free_orders_locks[ |
| 1079 | grp->bb_largest_free_order]); |
| 1080 | list_del_init(&grp->bb_largest_free_order_node); |
| 1081 | write_unlock(&sbi->s_mb_largest_free_orders_locks[ |
| 1082 | grp->bb_largest_free_order]); |
| 1083 | } |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1084 | grp->bb_largest_free_order = -1; /* uninit */ |
| 1085 | |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 1086 | for (i = MB_NUM_ORDERS(sb) - 1; i >= 0; i--) { |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1087 | if (grp->bb_counters[i] > 0) { |
| 1088 | grp->bb_largest_free_order = i; |
| 1089 | break; |
| 1090 | } |
| 1091 | } |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 1092 | if (test_opt2(sb, MB_OPTIMIZE_SCAN) && |
| 1093 | grp->bb_largest_free_order >= 0 && grp->bb_free) { |
| 1094 | write_lock(&sbi->s_mb_largest_free_orders_locks[ |
| 1095 | grp->bb_largest_free_order]); |
| 1096 | list_add_tail(&grp->bb_largest_free_order_node, |
| 1097 | &sbi->s_mb_largest_free_orders[grp->bb_largest_free_order]); |
| 1098 | write_unlock(&sbi->s_mb_largest_free_orders_locks[ |
| 1099 | grp->bb_largest_free_order]); |
| 1100 | } |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 1103 | static noinline_for_stack |
| 1104 | void ext4_mb_generate_buddy(struct super_block *sb, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1105 | void *buddy, void *bitmap, ext4_group_t group) |
| 1106 | { |
| 1107 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
Namjae Jeon | e43bb4e | 2014-06-26 10:11:53 -0400 | [diff] [blame] | 1108 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 1109 | ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb); |
Eric Sandeen | a36b449 | 2009-08-25 22:36:45 -0400 | [diff] [blame] | 1110 | ext4_grpblk_t i = 0; |
| 1111 | ext4_grpblk_t first; |
| 1112 | ext4_grpblk_t len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1113 | unsigned free = 0; |
| 1114 | unsigned fragments = 0; |
| 1115 | unsigned long long period = get_cycles(); |
| 1116 | |
| 1117 | /* initialize buddy from bitmap which is aggregation |
| 1118 | * of on-disk bitmap and preallocations */ |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 1119 | i = mb_find_next_zero_bit(bitmap, max, 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1120 | grp->bb_first_free = i; |
| 1121 | while (i < max) { |
| 1122 | fragments++; |
| 1123 | first = i; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 1124 | i = mb_find_next_bit(bitmap, max, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1125 | len = i - first; |
| 1126 | free += len; |
| 1127 | if (len > 1) |
| 1128 | ext4_mb_mark_free_simple(sb, buddy, first, len, grp); |
| 1129 | else |
| 1130 | grp->bb_counters[0]++; |
| 1131 | if (i < max) |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 1132 | i = mb_find_next_zero_bit(bitmap, max, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1133 | } |
| 1134 | grp->bb_fragments = fragments; |
| 1135 | |
| 1136 | if (free != grp->bb_free) { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 1137 | ext4_grp_locked_error(sb, group, 0, 0, |
Theodore Ts'o | 94d4c06 | 2014-07-05 19:15:50 -0400 | [diff] [blame] | 1138 | "block bitmap and bg descriptor " |
| 1139 | "inconsistent: %u vs %u free clusters", |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 1140 | free, grp->bb_free); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 1141 | /* |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 1142 | * If we intend to continue, we consider group descriptor |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 1143 | * corrupt and update bb_free using bitmap value |
| 1144 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1145 | grp->bb_free = free; |
Wang Shilong | db79e6d | 2018-05-12 11:39:40 -0400 | [diff] [blame] | 1146 | ext4_mark_group_bitmap_corrupted(sb, group, |
| 1147 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1148 | } |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1149 | mb_set_largest_free_order(sb, grp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1150 | |
| 1151 | clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state)); |
| 1152 | |
| 1153 | period = get_cycles() - period; |
Harshad Shirwadkar | 67d2518 | 2021-04-01 10:21:23 -0700 | [diff] [blame] | 1154 | atomic_inc(&sbi->s_mb_buddies_generated); |
| 1155 | atomic64_add(period, &sbi->s_mb_generation_time); |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 1156 | mb_update_avg_fragment_size(sb, grp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /* The buddy information is attached the buddy cache inode |
| 1160 | * for convenience. The information regarding each group |
| 1161 | * is loaded via ext4_mb_load_buddy. The information involve |
| 1162 | * block bitmap and buddy information. The information are |
| 1163 | * stored in the inode as |
| 1164 | * |
| 1165 | * { page } |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 1166 | * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1167 | * |
| 1168 | * |
| 1169 | * one block each for bitmap and buddy information. |
| 1170 | * 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] | 1171 | * contain blocks_per_page (PAGE_SIZE / blocksize) blocks. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1172 | * So it can have information regarding groups_per_page which |
| 1173 | * is blocks_per_page/2 |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1174 | * |
| 1175 | * Locking note: This routine takes the block group lock of all groups |
| 1176 | * for this page; do not hold this lock when calling this routine! |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1177 | */ |
| 1178 | |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1179 | 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] | 1180 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1181 | ext4_group_t ngroups; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1182 | int blocksize; |
| 1183 | int blocks_per_page; |
| 1184 | int groups_per_page; |
| 1185 | int err = 0; |
| 1186 | int i; |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 1187 | ext4_group_t first_group, group; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1188 | int first_block; |
| 1189 | struct super_block *sb; |
| 1190 | struct buffer_head *bhs; |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 1191 | struct buffer_head **bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1192 | struct inode *inode; |
| 1193 | char *data; |
| 1194 | char *bitmap; |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 1195 | struct ext4_group_info *grinfo; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1196 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1197 | inode = page->mapping->host; |
| 1198 | sb = inode->i_sb; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1199 | ngroups = ext4_get_groups_count(sb); |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 1200 | blocksize = i_blocksize(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1201 | blocks_per_page = PAGE_SIZE / blocksize; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1202 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 1203 | mb_debug(sb, "init page %lu\n", page->index); |
| 1204 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1205 | groups_per_page = blocks_per_page >> 1; |
| 1206 | if (groups_per_page == 0) |
| 1207 | groups_per_page = 1; |
| 1208 | |
| 1209 | /* allocate buffer_heads to read bitmaps */ |
| 1210 | if (groups_per_page > 1) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1211 | i = sizeof(struct buffer_head *) * groups_per_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1212 | bh = kzalloc(i, gfp); |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 1213 | if (bh == NULL) { |
| 1214 | err = -ENOMEM; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1215 | goto out; |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 1216 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1217 | } else |
| 1218 | bh = &bhs; |
| 1219 | |
| 1220 | first_group = page->index * blocks_per_page / 2; |
| 1221 | |
| 1222 | /* read all groups the page covers into the cache */ |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 1223 | for (i = 0, group = first_group; i < groups_per_page; i++, group++) { |
| 1224 | if (group >= ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1225 | break; |
| 1226 | |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 1227 | grinfo = ext4_get_group_info(sb, group); |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 1228 | /* |
| 1229 | * If page is uptodate then we came here after online resize |
| 1230 | * which added some new uninitialized group info structs, so |
| 1231 | * we must skip all initialized uptodate buddies on the page, |
| 1232 | * which may be currently in use by an allocating task. |
| 1233 | */ |
| 1234 | if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) { |
| 1235 | bh[i] = NULL; |
| 1236 | continue; |
| 1237 | } |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 1238 | bh[i] = ext4_read_block_bitmap_nowait(sb, group, false); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 1239 | if (IS_ERR(bh[i])) { |
| 1240 | err = PTR_ERR(bh[i]); |
| 1241 | bh[i] = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1242 | goto out; |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 1243 | } |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 1244 | mb_debug(sb, "read bitmap for group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /* wait for I/O completion */ |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 1248 | 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] | 1249 | int err2; |
| 1250 | |
| 1251 | if (!bh[i]) |
| 1252 | continue; |
| 1253 | err2 = ext4_wait_block_bitmap(sb, group, bh[i]); |
| 1254 | if (!err) |
| 1255 | err = err2; |
Theodore Ts'o | 813e572 | 2012-02-20 17:52:46 -0500 | [diff] [blame] | 1256 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1257 | |
| 1258 | first_block = page->index * blocks_per_page; |
| 1259 | for (i = 0; i < blocks_per_page; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1260 | group = (first_block + i) >> 1; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 1261 | if (group >= ngroups) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1262 | break; |
| 1263 | |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 1264 | if (!bh[group - first_group]) |
| 1265 | /* skip initialized uptodate buddy */ |
| 1266 | continue; |
| 1267 | |
Lukas Czerner | bbdc322 | 2015-06-08 11:38:37 -0400 | [diff] [blame] | 1268 | if (!buffer_verified(bh[group - first_group])) |
| 1269 | /* Skip faulty bitmaps */ |
| 1270 | continue; |
| 1271 | err = 0; |
| 1272 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1273 | /* |
| 1274 | * data carry information regarding this |
| 1275 | * particular group in the format specified |
| 1276 | * above |
| 1277 | * |
| 1278 | */ |
| 1279 | data = page_address(page) + (i * blocksize); |
| 1280 | bitmap = bh[group - first_group]->b_data; |
| 1281 | |
| 1282 | /* |
| 1283 | * We place the buddy block and bitmap block |
| 1284 | * close together |
| 1285 | */ |
| 1286 | if ((first_block + i) & 1) { |
| 1287 | /* this is block of buddy */ |
| 1288 | BUG_ON(incore == NULL); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 1289 | 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] | 1290 | group, page->index, i * blocksize); |
Theodore Ts'o | f307333 | 2010-05-17 03:00:00 -0400 | [diff] [blame] | 1291 | trace_ext4_mb_buddy_bitmap_load(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1292 | grinfo = ext4_get_group_info(sb, group); |
| 1293 | grinfo->bb_fragments = 0; |
| 1294 | memset(grinfo->bb_counters, 0, |
Eric Sandeen | 1927805 | 2009-08-25 22:36:25 -0400 | [diff] [blame] | 1295 | sizeof(*grinfo->bb_counters) * |
Harshad Shirwadkar | 4b68f6d | 2021-04-01 10:21:26 -0700 | [diff] [blame] | 1296 | (MB_NUM_ORDERS(sb))); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1297 | /* |
| 1298 | * incore got set to the group block bitmap below |
| 1299 | */ |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 1300 | ext4_lock_group(sb, group); |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 1301 | /* init the buddy */ |
| 1302 | memset(data, 0xff, blocksize); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1303 | ext4_mb_generate_buddy(sb, data, incore, group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 1304 | ext4_unlock_group(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1305 | incore = NULL; |
| 1306 | } else { |
| 1307 | /* this is block of bitmap */ |
| 1308 | BUG_ON(incore != NULL); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 1309 | 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] | 1310 | group, page->index, i * blocksize); |
Theodore Ts'o | f307333 | 2010-05-17 03:00:00 -0400 | [diff] [blame] | 1311 | trace_ext4_mb_bitmap_load(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1312 | |
| 1313 | /* see comments in ext4_mb_put_pa() */ |
| 1314 | ext4_lock_group(sb, group); |
| 1315 | memcpy(data, bitmap, blocksize); |
| 1316 | |
| 1317 | /* mark all preallocated blks used in in-core bitmap */ |
| 1318 | ext4_mb_generate_from_pa(sb, data, group); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 1319 | ext4_mb_generate_from_freelist(sb, data, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1320 | ext4_unlock_group(sb, group); |
| 1321 | |
| 1322 | /* set incore so that the buddy information can be |
| 1323 | * generated using this |
| 1324 | */ |
| 1325 | incore = data; |
| 1326 | } |
| 1327 | } |
| 1328 | SetPageUptodate(page); |
| 1329 | |
| 1330 | out: |
| 1331 | if (bh) { |
Amir Goldstein | 9b8b7d3 | 2011-05-09 21:49:42 -0400 | [diff] [blame] | 1332 | for (i = 0; i < groups_per_page; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1333 | brelse(bh[i]); |
| 1334 | if (bh != &bhs) |
| 1335 | kfree(bh); |
| 1336 | } |
| 1337 | return err; |
| 1338 | } |
| 1339 | |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1340 | /* |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1341 | * Lock the buddy and bitmap pages. This make sure other parallel init_group |
| 1342 | * on the same buddy page doesn't happen whild holding the buddy page lock. |
| 1343 | * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap |
| 1344 | * 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] | 1345 | */ |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1346 | static int ext4_mb_get_buddy_page_lock(struct super_block *sb, |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1347 | ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp) |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1348 | { |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1349 | struct inode *inode = EXT4_SB(sb)->s_buddy_cache; |
| 1350 | int block, pnum, poff; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1351 | int blocks_per_page; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1352 | struct page *page; |
| 1353 | |
| 1354 | e4b->bd_buddy_page = NULL; |
| 1355 | e4b->bd_bitmap_page = NULL; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1356 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1357 | blocks_per_page = PAGE_SIZE / sb->s_blocksize; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1358 | /* |
| 1359 | * the buddy cache inode stores the block bitmap |
| 1360 | * and buddy information in consecutive blocks. |
| 1361 | * So for each group we need two blocks. |
| 1362 | */ |
| 1363 | block = group * 2; |
| 1364 | pnum = block / blocks_per_page; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1365 | poff = block % blocks_per_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1366 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1367 | if (!page) |
Younger Liu | c57ab39 | 2014-04-10 23:03:43 -0400 | [diff] [blame] | 1368 | return -ENOMEM; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1369 | BUG_ON(page->mapping != inode->i_mapping); |
| 1370 | e4b->bd_bitmap_page = page; |
| 1371 | e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize); |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1372 | |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1373 | if (blocks_per_page >= 2) { |
| 1374 | /* buddy and bitmap are on the same page */ |
| 1375 | return 0; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1376 | } |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1377 | |
| 1378 | block++; |
| 1379 | pnum = block / blocks_per_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1380 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1381 | if (!page) |
Younger Liu | c57ab39 | 2014-04-10 23:03:43 -0400 | [diff] [blame] | 1382 | return -ENOMEM; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1383 | BUG_ON(page->mapping != inode->i_mapping); |
| 1384 | e4b->bd_buddy_page = page; |
| 1385 | return 0; |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1386 | } |
| 1387 | |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1388 | static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b) |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1389 | { |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1390 | if (e4b->bd_bitmap_page) { |
| 1391 | unlock_page(e4b->bd_bitmap_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1392 | put_page(e4b->bd_bitmap_page); |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1393 | } |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1394 | if (e4b->bd_buddy_page) { |
| 1395 | unlock_page(e4b->bd_buddy_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1396 | put_page(e4b->bd_buddy_page); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1397 | } |
Eric Sandeen | eee4adc | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /* |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1401 | * Locking note: This routine calls ext4_mb_init_cache(), which takes the |
| 1402 | * block group lock of all groups for this page; do not hold the BG lock when |
| 1403 | * calling this routine! |
| 1404 | */ |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1405 | static noinline_for_stack |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1406 | 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] | 1407 | { |
| 1408 | |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1409 | struct ext4_group_info *this_grp; |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1410 | struct ext4_buddy e4b; |
| 1411 | struct page *page; |
| 1412 | int ret = 0; |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1413 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 1414 | might_sleep(); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 1415 | mb_debug(sb, "init group %u\n", group); |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1416 | this_grp = ext4_get_group_info(sb, group); |
| 1417 | /* |
Aneesh Kumar K.V | 08c3a81 | 2009-09-09 23:50:17 -0400 | [diff] [blame] | 1418 | * This ensures that we don't reinit the buddy cache |
| 1419 | * page which map to the group from which we are already |
| 1420 | * allocating. If we are looking at the buddy cache we would |
| 1421 | * have taken a reference using ext4_mb_load_buddy and that |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1422 | * would have pinned buddy page to page cache. |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1423 | * The call to ext4_mb_get_buddy_page_lock will mark the |
| 1424 | * page accessed. |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1425 | */ |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1426 | ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1427 | if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) { |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1428 | /* |
| 1429 | * somebody initialized the group |
| 1430 | * return without doing anything |
| 1431 | */ |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1432 | goto err; |
| 1433 | } |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1434 | |
| 1435 | page = e4b.bd_bitmap_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1436 | ret = ext4_mb_init_cache(page, NULL, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1437 | if (ret) |
| 1438 | goto err; |
| 1439 | if (!PageUptodate(page)) { |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1440 | ret = -EIO; |
| 1441 | goto err; |
| 1442 | } |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1443 | |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1444 | if (e4b.bd_buddy_page == NULL) { |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1445 | /* |
| 1446 | * If both the bitmap and buddy are in |
| 1447 | * the same page we don't need to force |
| 1448 | * init the buddy |
| 1449 | */ |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1450 | ret = 0; |
| 1451 | goto err; |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1452 | } |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1453 | /* init buddy cache */ |
| 1454 | page = e4b.bd_buddy_page; |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1455 | ret = ext4_mb_init_cache(page, e4b.bd_bitmap, gfp); |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1456 | if (ret) |
| 1457 | goto err; |
| 1458 | if (!PageUptodate(page)) { |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1459 | ret = -EIO; |
| 1460 | goto err; |
| 1461 | } |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1462 | err: |
Amir Goldstein | 2de8807 | 2011-05-09 21:48:13 -0400 | [diff] [blame] | 1463 | ext4_mb_put_buddy_page_lock(&e4b); |
Aneesh Kumar K.V | b6a758e | 2009-09-09 23:47:46 -0400 | [diff] [blame] | 1464 | return ret; |
| 1465 | } |
| 1466 | |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1467 | /* |
| 1468 | * Locking note: This routine calls ext4_mb_init_cache(), which takes the |
| 1469 | * block group lock of all groups for this page; do not hold the BG lock when |
| 1470 | * calling this routine! |
| 1471 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1472 | static noinline_for_stack int |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1473 | ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group, |
| 1474 | struct ext4_buddy *e4b, gfp_t gfp) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1475 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1476 | int blocks_per_page; |
| 1477 | int block; |
| 1478 | int pnum; |
| 1479 | int poff; |
| 1480 | struct page *page; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1481 | int ret; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1482 | struct ext4_group_info *grp; |
| 1483 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 1484 | struct inode *inode = sbi->s_buddy_cache; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1485 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 1486 | might_sleep(); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 1487 | mb_debug(sb, "load group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1488 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1489 | blocks_per_page = PAGE_SIZE / sb->s_blocksize; |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1490 | grp = ext4_get_group_info(sb, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1491 | |
| 1492 | e4b->bd_blkbits = sb->s_blocksize_bits; |
Tao Ma | 529da70 | 2011-07-23 16:07:26 -0400 | [diff] [blame] | 1493 | e4b->bd_info = grp; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1494 | e4b->bd_sb = sb; |
| 1495 | e4b->bd_group = group; |
| 1496 | e4b->bd_buddy_page = NULL; |
| 1497 | e4b->bd_bitmap_page = NULL; |
| 1498 | |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1499 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1500 | /* |
| 1501 | * we need full data about the group |
| 1502 | * to make a good selection |
| 1503 | */ |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1504 | ret = ext4_mb_init_group(sb, group, gfp); |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1505 | if (ret) |
| 1506 | return ret; |
Aneesh Kumar K.V | f41c075 | 2009-09-09 23:34:50 -0400 | [diff] [blame] | 1507 | } |
| 1508 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1509 | /* |
| 1510 | * the buddy cache inode stores the block bitmap |
| 1511 | * and buddy information in consecutive blocks. |
| 1512 | * So for each group we need two blocks. |
| 1513 | */ |
| 1514 | block = group * 2; |
| 1515 | pnum = block / blocks_per_page; |
| 1516 | poff = block % blocks_per_page; |
| 1517 | |
| 1518 | /* we could use find_or_create_page(), but it locks page |
| 1519 | * what we'd like to avoid in fast path ... */ |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1520 | page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1521 | if (page == NULL || !PageUptodate(page)) { |
| 1522 | if (page) |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 1523 | /* |
| 1524 | * drop the page reference and try |
| 1525 | * to get the page with lock. If we |
| 1526 | * are not uptodate that implies |
| 1527 | * somebody just created the page but |
| 1528 | * is yet to initialize the same. So |
| 1529 | * wait for it to initialize. |
| 1530 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1531 | put_page(page); |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1532 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1533 | if (page) { |
| 1534 | BUG_ON(page->mapping != inode->i_mapping); |
| 1535 | if (!PageUptodate(page)) { |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1536 | ret = ext4_mb_init_cache(page, NULL, gfp); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1537 | if (ret) { |
| 1538 | unlock_page(page); |
| 1539 | goto err; |
| 1540 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1541 | mb_cmp_bitmaps(e4b, page_address(page) + |
| 1542 | (poff * sb->s_blocksize)); |
| 1543 | } |
| 1544 | unlock_page(page); |
| 1545 | } |
| 1546 | } |
Younger Liu | c57ab39 | 2014-04-10 23:03:43 -0400 | [diff] [blame] | 1547 | if (page == NULL) { |
| 1548 | ret = -ENOMEM; |
| 1549 | goto err; |
| 1550 | } |
| 1551 | if (!PageUptodate(page)) { |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1552 | ret = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1553 | goto err; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1554 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1555 | |
| 1556 | /* Pages marked accessed already */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1557 | e4b->bd_bitmap_page = page; |
| 1558 | e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1559 | |
| 1560 | block++; |
| 1561 | pnum = block / blocks_per_page; |
| 1562 | poff = block % blocks_per_page; |
| 1563 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1564 | page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1565 | if (page == NULL || !PageUptodate(page)) { |
| 1566 | if (page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1567 | put_page(page); |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1568 | page = find_or_create_page(inode->i_mapping, pnum, gfp); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1569 | if (page) { |
| 1570 | BUG_ON(page->mapping != inode->i_mapping); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1571 | if (!PageUptodate(page)) { |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1572 | ret = ext4_mb_init_cache(page, e4b->bd_bitmap, |
| 1573 | gfp); |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1574 | if (ret) { |
| 1575 | unlock_page(page); |
| 1576 | goto err; |
| 1577 | } |
| 1578 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1579 | unlock_page(page); |
| 1580 | } |
| 1581 | } |
Younger Liu | c57ab39 | 2014-04-10 23:03:43 -0400 | [diff] [blame] | 1582 | if (page == NULL) { |
| 1583 | ret = -ENOMEM; |
| 1584 | goto err; |
| 1585 | } |
| 1586 | if (!PageUptodate(page)) { |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1587 | ret = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1588 | goto err; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1589 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1590 | |
| 1591 | /* Pages marked accessed already */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1592 | e4b->bd_buddy_page = page; |
| 1593 | e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1594 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1595 | return 0; |
| 1596 | |
| 1597 | err: |
Yang Ruirui | 26626f11 | 2011-04-16 19:17:48 -0400 | [diff] [blame] | 1598 | if (page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1599 | put_page(page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1600 | if (e4b->bd_bitmap_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1601 | put_page(e4b->bd_bitmap_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1602 | if (e4b->bd_buddy_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1603 | put_page(e4b->bd_buddy_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1604 | e4b->bd_buddy = NULL; |
| 1605 | e4b->bd_bitmap = NULL; |
Shen Feng | fdf6c7a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1606 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1607 | } |
| 1608 | |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 1609 | static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group, |
| 1610 | struct ext4_buddy *e4b) |
| 1611 | { |
| 1612 | return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS); |
| 1613 | } |
| 1614 | |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 1615 | static void ext4_mb_unload_buddy(struct ext4_buddy *e4b) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1616 | { |
| 1617 | if (e4b->bd_bitmap_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1618 | put_page(e4b->bd_bitmap_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1619 | if (e4b->bd_buddy_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1620 | put_page(e4b->bd_buddy_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | static int mb_find_order_for_block(struct ext4_buddy *e4b, int block) |
| 1625 | { |
Chunguang Xu | ce3cca3 | 2020-11-07 23:58:13 +0800 | [diff] [blame] | 1626 | int order = 1, max; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1627 | void *bb; |
| 1628 | |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1629 | BUG_ON(e4b->bd_bitmap == e4b->bd_buddy); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1630 | BUG_ON(block >= (1 << (e4b->bd_blkbits + 3))); |
| 1631 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1632 | while (order <= e4b->bd_blkbits + 1) { |
Chunguang Xu | ce3cca3 | 2020-11-07 23:58:13 +0800 | [diff] [blame] | 1633 | bb = mb_find_buddy(e4b, order, &max); |
| 1634 | if (!mb_test_bit(block >> order, bb)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1635 | /* this block is part of buddy of order 'order' */ |
| 1636 | return order; |
| 1637 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1638 | order++; |
| 1639 | } |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1643 | static void mb_clear_bits(void *bm, int cur, int len) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1644 | { |
| 1645 | __u32 *addr; |
| 1646 | |
| 1647 | len = cur + len; |
| 1648 | while (cur < len) { |
| 1649 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1650 | /* fast path: clear whole word at once */ |
| 1651 | addr = bm + (cur >> 3); |
| 1652 | *addr = 0; |
| 1653 | cur += 32; |
| 1654 | continue; |
| 1655 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1656 | mb_clear_bit(cur, bm); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1657 | cur++; |
| 1658 | } |
| 1659 | } |
| 1660 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1661 | /* clear bits in given range |
| 1662 | * will return first found zero bit if any, -1 otherwise |
| 1663 | */ |
| 1664 | static int mb_test_and_clear_bits(void *bm, int cur, int len) |
| 1665 | { |
| 1666 | __u32 *addr; |
| 1667 | int zero_bit = -1; |
| 1668 | |
| 1669 | len = cur + len; |
| 1670 | while (cur < len) { |
| 1671 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1672 | /* fast path: clear whole word at once */ |
| 1673 | addr = bm + (cur >> 3); |
| 1674 | if (*addr != (__u32)(-1) && zero_bit == -1) |
| 1675 | zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0); |
| 1676 | *addr = 0; |
| 1677 | cur += 32; |
| 1678 | continue; |
| 1679 | } |
| 1680 | if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1) |
| 1681 | zero_bit = cur; |
| 1682 | cur++; |
| 1683 | } |
| 1684 | |
| 1685 | return zero_bit; |
| 1686 | } |
| 1687 | |
Yongqiang Yang | c3e94d1 | 2011-07-26 22:05:53 -0400 | [diff] [blame] | 1688 | void ext4_set_bits(void *bm, int cur, int len) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1689 | { |
| 1690 | __u32 *addr; |
| 1691 | |
| 1692 | len = cur + len; |
| 1693 | while (cur < len) { |
| 1694 | if ((cur & 31) == 0 && (len - cur) >= 32) { |
| 1695 | /* fast path: set whole word at once */ |
| 1696 | addr = bm + (cur >> 3); |
| 1697 | *addr = 0xffffffff; |
| 1698 | cur += 32; |
| 1699 | continue; |
| 1700 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 1701 | mb_set_bit(cur, bm); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1702 | cur++; |
| 1703 | } |
| 1704 | } |
| 1705 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1706 | 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] | 1707 | { |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1708 | if (mb_test_bit(*bit + side, bitmap)) { |
| 1709 | mb_clear_bit(*bit, bitmap); |
| 1710 | (*bit) -= side; |
| 1711 | return 1; |
| 1712 | } |
| 1713 | else { |
| 1714 | (*bit) += side; |
| 1715 | mb_set_bit(*bit, bitmap); |
| 1716 | return -1; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last) |
| 1721 | { |
| 1722 | int max; |
| 1723 | int order = 1; |
| 1724 | void *buddy = mb_find_buddy(e4b, order, &max); |
| 1725 | |
| 1726 | while (buddy) { |
| 1727 | void *buddy2; |
| 1728 | |
| 1729 | /* Bits in range [first; last] are known to be set since |
| 1730 | * corresponding blocks were allocated. Bits in range |
| 1731 | * (first; last) will stay set because they form buddies on |
| 1732 | * upper layer. We just deal with borders if they don't |
| 1733 | * align with upper layer and then go up. |
| 1734 | * Releasing entire group is all about clearing |
| 1735 | * single bit of highest order buddy. |
| 1736 | */ |
| 1737 | |
| 1738 | /* Example: |
| 1739 | * --------------------------------- |
| 1740 | * | 1 | 1 | 1 | 1 | |
| 1741 | * --------------------------------- |
| 1742 | * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | |
| 1743 | * --------------------------------- |
| 1744 | * 0 1 2 3 4 5 6 7 |
| 1745 | * \_____________________/ |
| 1746 | * |
| 1747 | * Neither [1] nor [6] is aligned to above layer. |
| 1748 | * Left neighbour [0] is free, so mark it busy, |
| 1749 | * decrease bb_counters and extend range to |
| 1750 | * [0; 6] |
| 1751 | * Right neighbour [7] is busy. It can't be coaleasced with [6], so |
| 1752 | * mark [6] free, increase bb_counters and shrink range to |
| 1753 | * [0; 5]. |
| 1754 | * Then shift range to [0; 2], go up and do the same. |
| 1755 | */ |
| 1756 | |
| 1757 | |
| 1758 | if (first & 1) |
| 1759 | e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1); |
| 1760 | if (!(last & 1)) |
| 1761 | e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1); |
| 1762 | if (first > last) |
| 1763 | break; |
| 1764 | order++; |
| 1765 | |
| 1766 | if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) { |
| 1767 | mb_clear_bits(buddy, first, last - first + 1); |
| 1768 | e4b->bd_info->bb_counters[order - 1] += last - first + 1; |
| 1769 | break; |
| 1770 | } |
| 1771 | first >>= 1; |
| 1772 | last >>= 1; |
| 1773 | buddy = buddy2; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b, |
| 1778 | int first, int count) |
| 1779 | { |
| 1780 | int left_is_free = 0; |
| 1781 | int right_is_free = 0; |
| 1782 | int block; |
| 1783 | int last = first + count - 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1784 | struct super_block *sb = e4b->bd_sb; |
| 1785 | |
Theodore Ts'o | c99d1e6 | 2014-08-23 17:47:28 -0400 | [diff] [blame] | 1786 | if (WARN_ON(count == 0)) |
| 1787 | return; |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1788 | BUG_ON(last >= (sb->s_blocksize << 3)); |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1789 | assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 1790 | /* Don't bother if the block group is corrupt. */ |
| 1791 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) |
| 1792 | return; |
| 1793 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1794 | mb_check_buddy(e4b); |
| 1795 | mb_free_blocks_double(inode, e4b, first, count); |
| 1796 | |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 1797 | this_cpu_inc(discard_pa_seq); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1798 | e4b->bd_info->bb_free += count; |
| 1799 | if (first < e4b->bd_info->bb_first_free) |
| 1800 | e4b->bd_info->bb_first_free = first; |
| 1801 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1802 | /* access memory sequentially: check left neighbour, |
| 1803 | * clear range and then check right neighbour |
| 1804 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1805 | if (first != 0) |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1806 | left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap); |
| 1807 | block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count); |
| 1808 | if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0]) |
| 1809 | right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap); |
| 1810 | |
| 1811 | if (unlikely(block != -1)) { |
Namjae Jeon | e43bb4e | 2014-06-26 10:11:53 -0400 | [diff] [blame] | 1812 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1813 | ext4_fsblk_t blocknr; |
| 1814 | |
| 1815 | blocknr = ext4_group_first_block_no(sb, e4b->bd_group); |
Jun Piao | 49598e0 | 2018-01-11 13:17:49 -0500 | [diff] [blame] | 1816 | blocknr += EXT4_C2B(sbi, block); |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 1817 | if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) { |
| 1818 | ext4_grp_locked_error(sb, e4b->bd_group, |
| 1819 | inode ? inode->i_ino : 0, |
| 1820 | blocknr, |
| 1821 | "freeing already freed block (bit %u); block bitmap corrupt.", |
| 1822 | block); |
| 1823 | ext4_mark_group_bitmap_corrupted( |
| 1824 | sb, e4b->bd_group, |
Wang Shilong | db79e6d | 2018-05-12 11:39:40 -0400 | [diff] [blame] | 1825 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 1826 | } |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1827 | goto done; |
| 1828 | } |
| 1829 | |
| 1830 | /* let's maintain fragments counter */ |
| 1831 | if (left_is_free && right_is_free) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1832 | e4b->bd_info->bb_fragments--; |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1833 | else if (!left_is_free && !right_is_free) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1834 | e4b->bd_info->bb_fragments++; |
| 1835 | |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1836 | /* buddy[0] == bd_bitmap is a special case, so handle |
| 1837 | * it right away and let mb_buddy_mark_free stay free of |
| 1838 | * zero order checks. |
| 1839 | * Check if neighbours are to be coaleasced, |
| 1840 | * adjust bitmap bb_counters and borders appropriately. |
| 1841 | */ |
| 1842 | if (first & 1) { |
| 1843 | first += !left_is_free; |
| 1844 | e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1845 | } |
Andrey Sidorov | eabe044 | 2013-04-09 12:22:29 -0400 | [diff] [blame] | 1846 | if (!(last & 1)) { |
| 1847 | last -= !right_is_free; |
| 1848 | e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1; |
| 1849 | } |
| 1850 | |
| 1851 | if (first <= last) |
| 1852 | mb_buddy_mark_free(e4b, first >> 1, last >> 1); |
| 1853 | |
| 1854 | done: |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1855 | mb_set_largest_free_order(sb, e4b->bd_info); |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 1856 | mb_update_avg_fragment_size(sb, e4b->bd_info); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1857 | mb_check_buddy(e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1858 | } |
| 1859 | |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1860 | static int mb_find_extent(struct ext4_buddy *e4b, int block, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1861 | int needed, struct ext4_free_extent *ex) |
| 1862 | { |
| 1863 | int next = block; |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1864 | int max, order; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1865 | void *buddy; |
| 1866 | |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1867 | 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] | 1868 | BUG_ON(ex == NULL); |
| 1869 | |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1870 | buddy = mb_find_buddy(e4b, 0, &max); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1871 | BUG_ON(buddy == NULL); |
| 1872 | BUG_ON(block >= max); |
| 1873 | if (mb_test_bit(block, buddy)) { |
| 1874 | ex->fe_len = 0; |
| 1875 | ex->fe_start = 0; |
| 1876 | ex->fe_group = 0; |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 1880 | /* find actual order */ |
| 1881 | order = mb_find_order_for_block(e4b, block); |
| 1882 | block = block >> order; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1883 | |
| 1884 | ex->fe_len = 1 << order; |
| 1885 | ex->fe_start = block << order; |
| 1886 | ex->fe_group = e4b->bd_group; |
| 1887 | |
| 1888 | /* calc difference from given start */ |
| 1889 | next = next - ex->fe_start; |
| 1890 | ex->fe_len -= next; |
| 1891 | ex->fe_start += next; |
| 1892 | |
| 1893 | while (needed > ex->fe_len && |
Alan Cox | d8ec0c3 | 2012-11-08 12:19:58 -0500 | [diff] [blame] | 1894 | mb_find_buddy(e4b, order, &max)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1895 | |
| 1896 | if (block + 1 >= max) |
| 1897 | break; |
| 1898 | |
| 1899 | next = (block + 1) * (1 << order); |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1900 | if (mb_test_bit(next, e4b->bd_bitmap)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1901 | break; |
| 1902 | |
Robin Dong | b051d8d | 2011-10-26 05:30:30 -0400 | [diff] [blame] | 1903 | order = mb_find_order_for_block(e4b, next); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1904 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1905 | block = next >> order; |
| 1906 | ex->fe_len += 1 << order; |
| 1907 | } |
| 1908 | |
Jan Kara | 31562b9 | 2019-04-06 18:33:06 -0400 | [diff] [blame] | 1909 | 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] | 1910 | /* Should never happen! (but apparently sometimes does?!?) */ |
| 1911 | WARN_ON(1); |
Stephen Brennan | cd84bbb | 2021-06-23 16:21:14 -0700 | [diff] [blame^] | 1912 | ext4_grp_locked_error(e4b->bd_sb, e4b->bd_group, 0, 0, |
| 1913 | "corruption or bug in mb_find_extent " |
| 1914 | "block=%d, order=%d needed=%d ex=%u/%d/%d@%u", |
| 1915 | block, order, needed, ex->fe_group, ex->fe_start, |
| 1916 | ex->fe_len, ex->fe_logical); |
Theodore Ts'o | 43c7322 | 2017-01-22 19:35:52 -0500 | [diff] [blame] | 1917 | ex->fe_len = 0; |
| 1918 | ex->fe_start = 0; |
| 1919 | ex->fe_group = 0; |
| 1920 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1921 | return ex->fe_len; |
| 1922 | } |
| 1923 | |
| 1924 | static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) |
| 1925 | { |
| 1926 | int ord; |
| 1927 | int mlen = 0; |
| 1928 | int max = 0; |
| 1929 | int cur; |
| 1930 | int start = ex->fe_start; |
| 1931 | int len = ex->fe_len; |
| 1932 | unsigned ret = 0; |
| 1933 | int len0 = len; |
| 1934 | void *buddy; |
| 1935 | |
| 1936 | BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3)); |
| 1937 | BUG_ON(e4b->bd_group != ex->fe_group); |
Vincent Minet | bc8e674 | 2009-05-15 08:33:18 -0400 | [diff] [blame] | 1938 | 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] | 1939 | mb_check_buddy(e4b); |
| 1940 | mb_mark_used_double(e4b, start, len); |
| 1941 | |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 1942 | this_cpu_inc(discard_pa_seq); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1943 | e4b->bd_info->bb_free -= len; |
| 1944 | if (e4b->bd_info->bb_first_free == start) |
| 1945 | e4b->bd_info->bb_first_free += len; |
| 1946 | |
| 1947 | /* let's maintain fragments counter */ |
| 1948 | if (start != 0) |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1949 | mlen = !mb_test_bit(start - 1, e4b->bd_bitmap); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1950 | 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] | 1951 | max = !mb_test_bit(start + len, e4b->bd_bitmap); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1952 | if (mlen && max) |
| 1953 | e4b->bd_info->bb_fragments++; |
| 1954 | else if (!mlen && !max) |
| 1955 | e4b->bd_info->bb_fragments--; |
| 1956 | |
| 1957 | /* let's maintain buddy itself */ |
| 1958 | while (len) { |
| 1959 | ord = mb_find_order_for_block(e4b, start); |
| 1960 | |
| 1961 | if (((start >> ord) << ord) == start && len >= (1 << ord)) { |
| 1962 | /* the whole chunk may be allocated at once! */ |
| 1963 | mlen = 1 << ord; |
| 1964 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1965 | BUG_ON((start >> ord) >= max); |
| 1966 | mb_set_bit(start >> ord, buddy); |
| 1967 | e4b->bd_info->bb_counters[ord]--; |
| 1968 | start += mlen; |
| 1969 | len -= mlen; |
| 1970 | BUG_ON(len < 0); |
| 1971 | continue; |
| 1972 | } |
| 1973 | |
| 1974 | /* store for history */ |
| 1975 | if (ret == 0) |
| 1976 | ret = len | (ord << 16); |
| 1977 | |
| 1978 | /* we have to split large buddy */ |
| 1979 | BUG_ON(ord <= 0); |
| 1980 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1981 | mb_set_bit(start >> ord, buddy); |
| 1982 | e4b->bd_info->bb_counters[ord]--; |
| 1983 | |
| 1984 | ord--; |
| 1985 | cur = (start >> ord) & ~1U; |
| 1986 | buddy = mb_find_buddy(e4b, ord, &max); |
| 1987 | mb_clear_bit(cur, buddy); |
| 1988 | mb_clear_bit(cur + 1, buddy); |
| 1989 | e4b->bd_info->bb_counters[ord]++; |
| 1990 | e4b->bd_info->bb_counters[ord]++; |
| 1991 | } |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 1992 | mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1993 | |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 1994 | mb_update_avg_fragment_size(e4b->bd_sb, e4b->bd_info); |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 1995 | ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1996 | mb_check_buddy(e4b); |
| 1997 | |
| 1998 | return ret; |
| 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | * Must be called under group lock! |
| 2003 | */ |
| 2004 | static void ext4_mb_use_best_found(struct ext4_allocation_context *ac, |
| 2005 | struct ext4_buddy *e4b) |
| 2006 | { |
| 2007 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 2008 | int ret; |
| 2009 | |
| 2010 | BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group); |
| 2011 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
| 2012 | |
| 2013 | ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len); |
| 2014 | ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical; |
| 2015 | ret = mb_mark_used(e4b, &ac->ac_b_ex); |
| 2016 | |
| 2017 | /* preallocation can change ac_b_ex, thus we store actually |
| 2018 | * allocated blocks for history */ |
| 2019 | ac->ac_f_ex = ac->ac_b_ex; |
| 2020 | |
| 2021 | ac->ac_status = AC_STATUS_FOUND; |
| 2022 | ac->ac_tail = ret & 0xffff; |
| 2023 | ac->ac_buddy = ret >> 16; |
| 2024 | |
Aneesh Kumar K.V | c3a326a | 2008-11-25 15:11:52 -0500 | [diff] [blame] | 2025 | /* |
| 2026 | * take the page reference. We want the page to be pinned |
| 2027 | * so that we don't get a ext4_mb_init_cache_call for this |
| 2028 | * group until we update the bitmap. That would mean we |
| 2029 | * double allocate blocks. The reference is dropped |
| 2030 | * in ext4_mb_release_context |
| 2031 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2032 | ac->ac_bitmap_page = e4b->bd_bitmap_page; |
| 2033 | get_page(ac->ac_bitmap_page); |
| 2034 | ac->ac_buddy_page = e4b->bd_buddy_page; |
| 2035 | get_page(ac->ac_buddy_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2036 | /* store last allocated for subsequent stream allocation */ |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 2037 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2038 | spin_lock(&sbi->s_md_lock); |
| 2039 | sbi->s_mb_last_group = ac->ac_f_ex.fe_group; |
| 2040 | sbi->s_mb_last_start = ac->ac_f_ex.fe_start; |
| 2041 | spin_unlock(&sbi->s_md_lock); |
| 2042 | } |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 2043 | /* |
| 2044 | * As we've just preallocated more space than |
| 2045 | * user requested originally, we store allocated |
| 2046 | * space in a special descriptor. |
| 2047 | */ |
| 2048 | if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) |
| 2049 | ext4_mb_new_preallocation(ac); |
| 2050 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2051 | } |
| 2052 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2053 | static void ext4_mb_check_limits(struct ext4_allocation_context *ac, |
| 2054 | struct ext4_buddy *e4b, |
| 2055 | int finish_group) |
| 2056 | { |
| 2057 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 2058 | struct ext4_free_extent *bex = &ac->ac_b_ex; |
| 2059 | struct ext4_free_extent *gex = &ac->ac_g_ex; |
| 2060 | struct ext4_free_extent ex; |
| 2061 | int max; |
| 2062 | |
Aneesh Kumar K.V | 032115f | 2009-01-05 21:34:30 -0500 | [diff] [blame] | 2063 | if (ac->ac_status == AC_STATUS_FOUND) |
| 2064 | return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2065 | /* |
| 2066 | * We don't want to scan for a whole year |
| 2067 | */ |
| 2068 | if (ac->ac_found > sbi->s_mb_max_to_scan && |
| 2069 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 2070 | ac->ac_status = AC_STATUS_BREAK; |
| 2071 | return; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * Haven't found good chunk so far, let's continue |
| 2076 | */ |
| 2077 | if (bex->fe_len < gex->fe_len) |
| 2078 | return; |
| 2079 | |
| 2080 | if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan) |
| 2081 | && bex->fe_group == e4b->bd_group) { |
| 2082 | /* recheck chunk's availability - we don't know |
| 2083 | * when it was found (within this lock-unlock |
| 2084 | * period or not) */ |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 2085 | max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2086 | if (max >= gex->fe_len) { |
| 2087 | ext4_mb_use_best_found(ac, e4b); |
| 2088 | return; |
| 2089 | } |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * The routine checks whether found extent is good enough. If it is, |
| 2095 | * then the extent gets marked used and flag is set to the context |
| 2096 | * to stop scanning. Otherwise, the extent is compared with the |
| 2097 | * previous found extent and if new one is better, then it's stored |
| 2098 | * in the context. Later, the best found extent will be used, if |
| 2099 | * mballoc can't find good enough extent. |
| 2100 | * |
| 2101 | * FIXME: real allocation policy is to be designed yet! |
| 2102 | */ |
| 2103 | static void ext4_mb_measure_extent(struct ext4_allocation_context *ac, |
| 2104 | struct ext4_free_extent *ex, |
| 2105 | struct ext4_buddy *e4b) |
| 2106 | { |
| 2107 | struct ext4_free_extent *bex = &ac->ac_b_ex; |
| 2108 | struct ext4_free_extent *gex = &ac->ac_g_ex; |
| 2109 | |
| 2110 | BUG_ON(ex->fe_len <= 0); |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 2111 | BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb)); |
| 2112 | BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2113 | BUG_ON(ac->ac_status != AC_STATUS_CONTINUE); |
| 2114 | |
| 2115 | ac->ac_found++; |
| 2116 | |
| 2117 | /* |
| 2118 | * The special case - take what you catch first |
| 2119 | */ |
| 2120 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 2121 | *bex = *ex; |
| 2122 | ext4_mb_use_best_found(ac, e4b); |
| 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * Let's check whether the chuck is good enough |
| 2128 | */ |
| 2129 | if (ex->fe_len == gex->fe_len) { |
| 2130 | *bex = *ex; |
| 2131 | ext4_mb_use_best_found(ac, e4b); |
| 2132 | return; |
| 2133 | } |
| 2134 | |
| 2135 | /* |
| 2136 | * If this is first found extent, just store it in the context |
| 2137 | */ |
| 2138 | if (bex->fe_len == 0) { |
| 2139 | *bex = *ex; |
| 2140 | return; |
| 2141 | } |
| 2142 | |
| 2143 | /* |
| 2144 | * If new found extent is better, store it in the context |
| 2145 | */ |
| 2146 | if (bex->fe_len < gex->fe_len) { |
| 2147 | /* if the request isn't satisfied, any found extent |
| 2148 | * larger than previous best one is better */ |
| 2149 | if (ex->fe_len > bex->fe_len) |
| 2150 | *bex = *ex; |
| 2151 | } else if (ex->fe_len > gex->fe_len) { |
| 2152 | /* if the request is satisfied, then we try to find |
| 2153 | * an extent that still satisfy the request, but is |
| 2154 | * smaller than previous one */ |
| 2155 | if (ex->fe_len < bex->fe_len) |
| 2156 | *bex = *ex; |
| 2157 | } |
| 2158 | |
| 2159 | ext4_mb_check_limits(ac, e4b, 0); |
| 2160 | } |
| 2161 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 2162 | static noinline_for_stack |
| 2163 | int ext4_mb_try_best_found(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2164 | struct ext4_buddy *e4b) |
| 2165 | { |
| 2166 | struct ext4_free_extent ex = ac->ac_b_ex; |
| 2167 | ext4_group_t group = ex.fe_group; |
| 2168 | int max; |
| 2169 | int err; |
| 2170 | |
| 2171 | BUG_ON(ex.fe_len <= 0); |
| 2172 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); |
| 2173 | if (err) |
| 2174 | return err; |
| 2175 | |
| 2176 | ext4_lock_group(ac->ac_sb, group); |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 2177 | max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2178 | |
| 2179 | if (max > 0) { |
| 2180 | ac->ac_b_ex = ex; |
| 2181 | ext4_mb_use_best_found(ac, e4b); |
| 2182 | } |
| 2183 | |
| 2184 | ext4_unlock_group(ac->ac_sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 2185 | ext4_mb_unload_buddy(e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2186 | |
| 2187 | return 0; |
| 2188 | } |
| 2189 | |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 2190 | static noinline_for_stack |
| 2191 | int ext4_mb_find_by_goal(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2192 | struct ext4_buddy *e4b) |
| 2193 | { |
| 2194 | ext4_group_t group = ac->ac_g_ex.fe_group; |
| 2195 | int max; |
| 2196 | int err; |
| 2197 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Yongqiang Yang | 838cd0c | 2012-09-23 23:10:51 -0400 | [diff] [blame] | 2198 | 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] | 2199 | struct ext4_free_extent ex; |
| 2200 | |
| 2201 | if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL)) |
| 2202 | return 0; |
Yongqiang Yang | 838cd0c | 2012-09-23 23:10:51 -0400 | [diff] [blame] | 2203 | if (grp->bb_free == 0) |
| 2204 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2205 | |
| 2206 | err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); |
| 2207 | if (err) |
| 2208 | return err; |
| 2209 | |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 2210 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) { |
| 2211 | ext4_mb_unload_buddy(e4b); |
| 2212 | return 0; |
| 2213 | } |
| 2214 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2215 | ext4_lock_group(ac->ac_sb, group); |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 2216 | max = mb_find_extent(e4b, ac->ac_g_ex.fe_start, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2217 | ac->ac_g_ex.fe_len, &ex); |
Theodore Ts'o | ab0c00f | 2014-02-20 00:36:41 -0500 | [diff] [blame] | 2218 | ex.fe_logical = 0xDEADFA11; /* debug value */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2219 | |
| 2220 | if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) { |
| 2221 | ext4_fsblk_t start; |
| 2222 | |
Akinobu Mita | 5661bd6 | 2010-03-03 23:53:39 -0500 | [diff] [blame] | 2223 | start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) + |
| 2224 | ex.fe_start; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2225 | /* use do_div to get remainder (would be 64-bit modulo) */ |
| 2226 | if (do_div(start, sbi->s_stripe) == 0) { |
| 2227 | ac->ac_found++; |
| 2228 | ac->ac_b_ex = ex; |
| 2229 | ext4_mb_use_best_found(ac, e4b); |
| 2230 | } |
| 2231 | } else if (max >= ac->ac_g_ex.fe_len) { |
| 2232 | BUG_ON(ex.fe_len <= 0); |
| 2233 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); |
| 2234 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); |
| 2235 | ac->ac_found++; |
| 2236 | ac->ac_b_ex = ex; |
| 2237 | ext4_mb_use_best_found(ac, e4b); |
| 2238 | } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) { |
| 2239 | /* Sometimes, caller may want to merge even small |
| 2240 | * number of blocks to an existing extent */ |
| 2241 | BUG_ON(ex.fe_len <= 0); |
| 2242 | BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); |
| 2243 | BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); |
| 2244 | ac->ac_found++; |
| 2245 | ac->ac_b_ex = ex; |
| 2246 | ext4_mb_use_best_found(ac, e4b); |
| 2247 | } |
| 2248 | ext4_unlock_group(ac->ac_sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 2249 | ext4_mb_unload_buddy(e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2250 | |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * The routine scans buddy structures (not bitmap!) from given order |
| 2256 | * to max order and tries to find big enough chunk to satisfy the req |
| 2257 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 2258 | static noinline_for_stack |
| 2259 | void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2260 | struct ext4_buddy *e4b) |
| 2261 | { |
| 2262 | struct super_block *sb = ac->ac_sb; |
| 2263 | struct ext4_group_info *grp = e4b->bd_info; |
| 2264 | void *buddy; |
| 2265 | int i; |
| 2266 | int k; |
| 2267 | int max; |
| 2268 | |
| 2269 | BUG_ON(ac->ac_2order <= 0); |
Harshad Shirwadkar | 4b68f6d | 2021-04-01 10:21:26 -0700 | [diff] [blame] | 2270 | for (i = ac->ac_2order; i < MB_NUM_ORDERS(sb); i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2271 | if (grp->bb_counters[i] == 0) |
| 2272 | continue; |
| 2273 | |
| 2274 | buddy = mb_find_buddy(e4b, i, &max); |
| 2275 | BUG_ON(buddy == NULL); |
| 2276 | |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 2277 | k = mb_find_next_zero_bit(buddy, max, 0); |
Dmitry Monakhov | eb57608 | 2020-03-10 15:01:56 +0000 | [diff] [blame] | 2278 | if (k >= max) { |
| 2279 | ext4_grp_locked_error(ac->ac_sb, e4b->bd_group, 0, 0, |
| 2280 | "%d free clusters of order %d. But found 0", |
| 2281 | grp->bb_counters[i], i); |
| 2282 | ext4_mark_group_bitmap_corrupted(ac->ac_sb, |
| 2283 | e4b->bd_group, |
| 2284 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
| 2285 | break; |
| 2286 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2287 | ac->ac_found++; |
| 2288 | |
| 2289 | ac->ac_b_ex.fe_len = 1 << i; |
| 2290 | ac->ac_b_ex.fe_start = k << i; |
| 2291 | ac->ac_b_ex.fe_group = e4b->bd_group; |
| 2292 | |
| 2293 | ext4_mb_use_best_found(ac, e4b); |
| 2294 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 2295 | 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] | 2296 | |
| 2297 | if (EXT4_SB(sb)->s_mb_stats) |
| 2298 | atomic_inc(&EXT4_SB(sb)->s_bal_2orders); |
| 2299 | |
| 2300 | break; |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | /* |
| 2305 | * The routine scans the group and measures all found extents. |
| 2306 | * In order to optimize scanning, caller must pass number of |
| 2307 | * free blocks in the group, so the routine can know upper limit. |
| 2308 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 2309 | static noinline_for_stack |
| 2310 | void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2311 | struct ext4_buddy *e4b) |
| 2312 | { |
| 2313 | struct super_block *sb = ac->ac_sb; |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 2314 | void *bitmap = e4b->bd_bitmap; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2315 | struct ext4_free_extent ex; |
| 2316 | int i; |
| 2317 | int free; |
| 2318 | |
| 2319 | free = e4b->bd_info->bb_free; |
Theodore Ts'o | 907ea52 | 2020-04-13 23:33:05 -0400 | [diff] [blame] | 2320 | if (WARN_ON(free <= 0)) |
| 2321 | return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2322 | |
| 2323 | i = e4b->bd_info->bb_first_free; |
| 2324 | |
| 2325 | while (free && ac->ac_status == AC_STATUS_CONTINUE) { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 2326 | i = mb_find_next_zero_bit(bitmap, |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 2327 | EXT4_CLUSTERS_PER_GROUP(sb), i); |
| 2328 | if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2329 | /* |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 2330 | * IF we have corrupt bitmap, we won't find any |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2331 | * free blocks even though group info says we |
Randy Dunlap | b483bb7 | 2020-08-04 19:48:50 -0700 | [diff] [blame] | 2332 | * have free blocks |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2333 | */ |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 2334 | ext4_grp_locked_error(sb, e4b->bd_group, 0, 0, |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 2335 | "%d free clusters as per " |
Theodore Ts'o | fde4d95 | 2009-01-05 22:17:35 -0500 | [diff] [blame] | 2336 | "group info. But bitmap says 0", |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2337 | free); |
Wang Shilong | 736dedb | 2018-05-12 12:37:58 -0400 | [diff] [blame] | 2338 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
| 2339 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2340 | break; |
| 2341 | } |
| 2342 | |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 2343 | 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] | 2344 | if (WARN_ON(ex.fe_len <= 0)) |
| 2345 | break; |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2346 | if (free < ex.fe_len) { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 2347 | ext4_grp_locked_error(sb, e4b->bd_group, 0, 0, |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 2348 | "%d free clusters as per " |
Theodore Ts'o | fde4d95 | 2009-01-05 22:17:35 -0500 | [diff] [blame] | 2349 | "group info. But got %d blocks", |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2350 | free, ex.fe_len); |
Wang Shilong | 736dedb | 2018-05-12 12:37:58 -0400 | [diff] [blame] | 2351 | ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, |
| 2352 | EXT4_GROUP_INFO_BBITMAP_CORRUPT); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 2353 | /* |
| 2354 | * The number of free blocks differs. This mostly |
| 2355 | * indicate that the bitmap is corrupt. So exit |
| 2356 | * without claiming the space. |
| 2357 | */ |
| 2358 | break; |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 2359 | } |
Theodore Ts'o | ab0c00f | 2014-02-20 00:36:41 -0500 | [diff] [blame] | 2360 | ex.fe_logical = 0xDEADC0DE; /* debug value */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2361 | ext4_mb_measure_extent(ac, &ex, e4b); |
| 2362 | |
| 2363 | i += ex.fe_len; |
| 2364 | free -= ex.fe_len; |
| 2365 | } |
| 2366 | |
| 2367 | ext4_mb_check_limits(ac, e4b, 1); |
| 2368 | } |
| 2369 | |
| 2370 | /* |
| 2371 | * This is a special case for storages like raid5 |
Eric Sandeen | 506bf2d | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 2372 | * we try to find stripe-aligned chunks for stripe-size-multiple requests |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2373 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 2374 | static noinline_for_stack |
| 2375 | void ext4_mb_scan_aligned(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2376 | struct ext4_buddy *e4b) |
| 2377 | { |
| 2378 | struct super_block *sb = ac->ac_sb; |
| 2379 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | c5e8f3f | 2012-02-20 17:54:06 -0500 | [diff] [blame] | 2380 | void *bitmap = e4b->bd_bitmap; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2381 | struct ext4_free_extent ex; |
| 2382 | ext4_fsblk_t first_group_block; |
| 2383 | ext4_fsblk_t a; |
| 2384 | ext4_grpblk_t i; |
| 2385 | int max; |
| 2386 | |
| 2387 | BUG_ON(sbi->s_stripe == 0); |
| 2388 | |
| 2389 | /* find first stripe-aligned block in group */ |
Akinobu Mita | 5661bd6 | 2010-03-03 23:53:39 -0500 | [diff] [blame] | 2390 | first_group_block = ext4_group_first_block_no(sb, e4b->bd_group); |
| 2391 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2392 | a = first_group_block + sbi->s_stripe - 1; |
| 2393 | do_div(a, sbi->s_stripe); |
| 2394 | i = (a * sbi->s_stripe) - first_group_block; |
| 2395 | |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 2396 | while (i < EXT4_CLUSTERS_PER_GROUP(sb)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2397 | if (!mb_test_bit(i, bitmap)) { |
Robin Dong | 15c006a | 2012-08-17 10:02:17 -0400 | [diff] [blame] | 2398 | max = mb_find_extent(e4b, i, sbi->s_stripe, &ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2399 | if (max >= sbi->s_stripe) { |
| 2400 | ac->ac_found++; |
Theodore Ts'o | ab0c00f | 2014-02-20 00:36:41 -0500 | [diff] [blame] | 2401 | ex.fe_logical = 0xDEADF00D; /* debug value */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2402 | ac->ac_b_ex = ex; |
| 2403 | ext4_mb_use_best_found(ac, e4b); |
| 2404 | break; |
| 2405 | } |
| 2406 | } |
| 2407 | i += sbi->s_stripe; |
| 2408 | } |
| 2409 | } |
| 2410 | |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2411 | /* |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2412 | * This is also called BEFORE we load the buddy bitmap. |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2413 | * Returns either 1 or 0 indicating that the group is either suitable |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2414 | * for the allocation or not. |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2415 | */ |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2416 | static bool ext4_mb_good_group(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2417 | ext4_group_t group, int cr) |
| 2418 | { |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2419 | ext4_grpblk_t free, fragments; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 2420 | int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2421 | struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); |
| 2422 | |
| 2423 | BUG_ON(cr < 0 || cr >= 4); |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 2424 | |
brookxu | dddcd2f | 2020-08-07 22:01:39 +0800 | [diff] [blame] | 2425 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2426 | return false; |
Theodore Ts'o | 01fc48e | 2012-08-17 09:46:17 -0400 | [diff] [blame] | 2427 | |
brookxu | dddcd2f | 2020-08-07 22:01:39 +0800 | [diff] [blame] | 2428 | free = grp->bb_free; |
| 2429 | if (free == 0) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2430 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2431 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2432 | fragments = grp->bb_fragments; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2433 | if (fragments == 0) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2434 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2435 | |
| 2436 | switch (cr) { |
| 2437 | case 0: |
| 2438 | BUG_ON(ac->ac_2order == 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2439 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 2440 | /* Avoid using the first bg of a flexgroup for data files */ |
| 2441 | if ((ac->ac_flags & EXT4_MB_HINT_DATA) && |
| 2442 | (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) && |
| 2443 | ((group % flex_size) == 0)) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2444 | return false; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 2445 | |
brookxu | dddcd2f | 2020-08-07 22:01:39 +0800 | [diff] [blame] | 2446 | if (free < ac->ac_g_ex.fe_len) |
| 2447 | return false; |
| 2448 | |
Harshad Shirwadkar | 4b68f6d | 2021-04-01 10:21:26 -0700 | [diff] [blame] | 2449 | if (ac->ac_2order >= MB_NUM_ORDERS(ac->ac_sb)) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2450 | return true; |
Theodore Ts'o | 40ae348 | 2013-02-04 15:08:40 -0500 | [diff] [blame] | 2451 | |
| 2452 | if (grp->bb_largest_free_order < ac->ac_2order) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2453 | return false; |
Theodore Ts'o | 40ae348 | 2013-02-04 15:08:40 -0500 | [diff] [blame] | 2454 | |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2455 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2456 | case 1: |
| 2457 | if ((free / fragments) >= ac->ac_g_ex.fe_len) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2458 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2459 | break; |
| 2460 | case 2: |
| 2461 | if (free >= ac->ac_g_ex.fe_len) |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2462 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2463 | break; |
| 2464 | case 3: |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2465 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2466 | default: |
| 2467 | BUG(); |
| 2468 | } |
| 2469 | |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2470 | return false; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * This could return negative error code if something goes wrong |
| 2475 | * during ext4_mb_init_group(). This should not be called with |
| 2476 | * ext4_lock_group() held. |
| 2477 | */ |
| 2478 | static int ext4_mb_good_group_nolock(struct ext4_allocation_context *ac, |
| 2479 | ext4_group_t group, int cr) |
| 2480 | { |
| 2481 | 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] | 2482 | struct super_block *sb = ac->ac_sb; |
Alex Zhuravlev | c1d2c7d | 2020-06-19 22:08:56 -0400 | [diff] [blame] | 2483 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2484 | bool should_lock = ac->ac_flags & EXT4_MB_STRICT_CHECK; |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2485 | ext4_grpblk_t free; |
| 2486 | int ret = 0; |
| 2487 | |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 2488 | if (sbi->s_mb_stats) |
| 2489 | atomic64_inc(&sbi->s_bal_cX_groups_considered[ac->ac_criteria]); |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2490 | if (should_lock) |
| 2491 | ext4_lock_group(sb, group); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2492 | free = grp->bb_free; |
| 2493 | if (free == 0) |
| 2494 | goto out; |
| 2495 | if (cr <= 2 && free < ac->ac_g_ex.fe_len) |
| 2496 | goto out; |
| 2497 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) |
| 2498 | goto out; |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2499 | if (should_lock) |
| 2500 | ext4_unlock_group(sb, group); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2501 | |
| 2502 | /* We only do this if the grp has never been initialized */ |
| 2503 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { |
Alex Zhuravlev | c1d2c7d | 2020-06-19 22:08:56 -0400 | [diff] [blame] | 2504 | struct ext4_group_desc *gdp = |
| 2505 | ext4_get_group_desc(sb, group, NULL); |
| 2506 | int ret; |
| 2507 | |
| 2508 | /* cr=0/1 is a very optimistic search to find large |
| 2509 | * good chunks almost for free. If buddy data is not |
| 2510 | * ready, then this optimization makes no sense. But |
| 2511 | * we never skip the first block group in a flex_bg, |
| 2512 | * since this gets used for metadata block allocation, |
| 2513 | * and we want to make sure we locate metadata blocks |
| 2514 | * in the first block group in the flex_bg if possible. |
| 2515 | */ |
| 2516 | if (cr < 2 && |
| 2517 | (!sbi->s_log_groups_per_flex || |
| 2518 | ((group & ((1 << sbi->s_log_groups_per_flex) - 1)) != 0)) && |
| 2519 | !(ext4_has_group_desc_csum(sb) && |
| 2520 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) |
| 2521 | return 0; |
| 2522 | ret = ext4_mb_init_group(sb, group, GFP_NOFS); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2523 | if (ret) |
| 2524 | return ret; |
| 2525 | } |
| 2526 | |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2527 | if (should_lock) |
| 2528 | ext4_lock_group(sb, group); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2529 | ret = ext4_mb_good_group(ac, group, cr); |
| 2530 | out: |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 2531 | if (should_lock) |
| 2532 | ext4_unlock_group(sb, group); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2533 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2534 | } |
| 2535 | |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2536 | /* |
| 2537 | * Start prefetching @nr block bitmaps starting at @group. |
| 2538 | * Return the next group which needs to be prefetched. |
| 2539 | */ |
Theodore Ts'o | 3d392b2 | 2020-07-17 00:14:40 -0400 | [diff] [blame] | 2540 | ext4_group_t ext4_mb_prefetch(struct super_block *sb, ext4_group_t group, |
| 2541 | unsigned int nr, int *cnt) |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2542 | { |
| 2543 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
| 2544 | struct buffer_head *bh; |
| 2545 | struct blk_plug plug; |
| 2546 | |
| 2547 | blk_start_plug(&plug); |
| 2548 | while (nr-- > 0) { |
| 2549 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, |
| 2550 | NULL); |
| 2551 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 2552 | |
| 2553 | /* |
| 2554 | * Prefetch block groups with free blocks; but don't |
| 2555 | * bother if it is marked uninitialized on disk, since |
| 2556 | * it won't require I/O to read. Also only try to |
| 2557 | * prefetch once, so we avoid getblk() call, which can |
| 2558 | * be expensive. |
| 2559 | */ |
| 2560 | if (!EXT4_MB_GRP_TEST_AND_SET_READ(grp) && |
| 2561 | EXT4_MB_GRP_NEED_INIT(grp) && |
| 2562 | ext4_free_group_clusters(sb, gdp) > 0 && |
| 2563 | !(ext4_has_group_desc_csum(sb) && |
| 2564 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) { |
| 2565 | bh = ext4_read_block_bitmap_nowait(sb, group, true); |
| 2566 | if (bh && !IS_ERR(bh)) { |
| 2567 | if (!buffer_uptodate(bh) && cnt) |
| 2568 | (*cnt)++; |
| 2569 | brelse(bh); |
| 2570 | } |
| 2571 | } |
| 2572 | if (++group >= ngroups) |
| 2573 | group = 0; |
| 2574 | } |
| 2575 | blk_finish_plug(&plug); |
| 2576 | return group; |
| 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | * Prefetching reads the block bitmap into the buffer cache; but we |
| 2581 | * need to make sure that the buddy bitmap in the page cache has been |
| 2582 | * initialized. Note that ext4_mb_init_group() will block if the I/O |
| 2583 | * is not yet completed, or indeed if it was not initiated by |
| 2584 | * ext4_mb_prefetch did not start the I/O. |
| 2585 | * |
| 2586 | * TODO: We should actually kick off the buddy bitmap setup in a work |
| 2587 | * queue when the buffer I/O is completed, so that we don't block |
| 2588 | * waiting for the block allocation bitmap read to finish when |
| 2589 | * ext4_mb_prefetch_fini is called from ext4_mb_regular_allocator(). |
| 2590 | */ |
Theodore Ts'o | 3d392b2 | 2020-07-17 00:14:40 -0400 | [diff] [blame] | 2591 | void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group, |
| 2592 | unsigned int nr) |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2593 | { |
| 2594 | while (nr-- > 0) { |
| 2595 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, |
| 2596 | NULL); |
| 2597 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 2598 | |
| 2599 | if (!group) |
| 2600 | group = ext4_get_groups_count(sb); |
| 2601 | group--; |
| 2602 | grp = ext4_get_group_info(sb, group); |
| 2603 | |
| 2604 | if (EXT4_MB_GRP_NEED_INIT(grp) && |
| 2605 | ext4_free_group_clusters(sb, gdp) > 0 && |
| 2606 | !(ext4_has_group_desc_csum(sb) && |
| 2607 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) { |
| 2608 | if (ext4_mb_init_group(sb, group, GFP_NOFS)) |
| 2609 | break; |
| 2610 | } |
| 2611 | } |
| 2612 | } |
| 2613 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2614 | static noinline_for_stack int |
| 2615 | ext4_mb_regular_allocator(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2616 | { |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2617 | ext4_group_t prefetch_grp = 0, ngroups, group, i; |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 2618 | int cr = -1; |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2619 | int err = 0, first_err = 0; |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2620 | unsigned int nr = 0, prefetch_ios = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2621 | struct ext4_sb_info *sbi; |
| 2622 | struct super_block *sb; |
| 2623 | struct ext4_buddy e4b; |
brookxu | 66d5e02 | 2020-08-17 15:36:06 +0800 | [diff] [blame] | 2624 | int lost; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2625 | |
| 2626 | sb = ac->ac_sb; |
| 2627 | sbi = EXT4_SB(sb); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2628 | ngroups = ext4_get_groups_count(sb); |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2629 | /* non-extent files are limited to low blocks/groups */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 2630 | if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 2631 | ngroups = sbi->s_blockfile_groups; |
| 2632 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2633 | BUG_ON(ac->ac_status == AC_STATUS_FOUND); |
| 2634 | |
| 2635 | /* first, try the goal */ |
| 2636 | err = ext4_mb_find_by_goal(ac, &e4b); |
| 2637 | if (err || ac->ac_status == AC_STATUS_FOUND) |
| 2638 | goto out; |
| 2639 | |
| 2640 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 2641 | goto out; |
| 2642 | |
| 2643 | /* |
brookxu | e9a3cd4 | 2020-08-07 22:01:23 +0800 | [diff] [blame] | 2644 | * ac->ac_2order is set only if the fe_len is a power of 2 |
| 2645 | * 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] | 2646 | * try exact allocation using buddy. |
| 2647 | */ |
| 2648 | i = fls(ac->ac_g_ex.fe_len); |
| 2649 | ac->ac_2order = 0; |
| 2650 | /* |
| 2651 | * We search using buddy data only if the order of the request |
| 2652 | * is greater than equal to the sbi_s_mb_order2_reqs |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 2653 | * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req |
Jan Kara | d9b22cf | 2017-02-10 00:50:56 -0500 | [diff] [blame] | 2654 | * We also support searching for power-of-two requests only for |
| 2655 | * requests upto maximum buddy size we have constructed. |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2656 | */ |
Harshad Shirwadkar | 4b68f6d | 2021-04-01 10:21:26 -0700 | [diff] [blame] | 2657 | if (i >= sbi->s_mb_order2_reqs && i <= MB_NUM_ORDERS(sb)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2658 | /* |
| 2659 | * This should tell if fe_len is exactly power of 2 |
| 2660 | */ |
| 2661 | if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0) |
Jeremy Cline | 1a5d5e5 | 2018-08-02 00:03:40 -0400 | [diff] [blame] | 2662 | ac->ac_2order = array_index_nospec(i - 1, |
Harshad Shirwadkar | 4b68f6d | 2021-04-01 10:21:26 -0700 | [diff] [blame] | 2663 | MB_NUM_ORDERS(sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2664 | } |
| 2665 | |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 2666 | /* if stream allocation is enabled, use global goal */ |
| 2667 | if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2668 | /* TBD: may be hot point */ |
| 2669 | spin_lock(&sbi->s_md_lock); |
| 2670 | ac->ac_g_ex.fe_group = sbi->s_mb_last_group; |
| 2671 | ac->ac_g_ex.fe_start = sbi->s_mb_last_start; |
| 2672 | spin_unlock(&sbi->s_md_lock); |
| 2673 | } |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 2674 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2675 | /* Let's just scan groups to find more-less suitable blocks */ |
| 2676 | cr = ac->ac_2order ? 0 : 1; |
| 2677 | /* |
| 2678 | * cr == 0 try to get exact allocation, |
| 2679 | * cr == 3 try to get anything |
| 2680 | */ |
| 2681 | repeat: |
| 2682 | for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) { |
| 2683 | ac->ac_criteria = cr; |
Aneesh Kumar K.V | ed8f9c7 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2684 | /* |
| 2685 | * searching for the right group start |
| 2686 | * from the goal value specified |
| 2687 | */ |
| 2688 | group = ac->ac_g_ex.fe_group; |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 2689 | ac->ac_last_optimal_group = group; |
| 2690 | ac->ac_groups_linear_remaining = sbi->s_mb_max_linear_groups; |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2691 | prefetch_grp = group; |
Aneesh Kumar K.V | ed8f9c7 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2692 | |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 2693 | for (i = 0; i < ngroups; group = next_linear_group(ac, group, ngroups), |
| 2694 | i++) { |
| 2695 | int ret = 0, new_cr; |
| 2696 | |
Theodore Ts'o | 2ed5724 | 2013-06-12 11:43:02 -0400 | [diff] [blame] | 2697 | cond_resched(); |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 2698 | |
| 2699 | ext4_mb_choose_next_group(ac, &new_cr, &group, ngroups); |
| 2700 | if (new_cr != cr) { |
| 2701 | cr = new_cr; |
| 2702 | goto repeat; |
| 2703 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2704 | |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2705 | /* |
| 2706 | * Batch reads of the block allocation bitmaps |
| 2707 | * to get multiple READs in flight; limit |
| 2708 | * prefetching at cr=0/1, otherwise mballoc can |
| 2709 | * spend a lot of time loading imperfect groups |
| 2710 | */ |
| 2711 | if ((prefetch_grp == group) && |
| 2712 | (cr > 1 || |
| 2713 | prefetch_ios < sbi->s_mb_prefetch_limit)) { |
| 2714 | unsigned int curr_ios = prefetch_ios; |
| 2715 | |
| 2716 | nr = sbi->s_mb_prefetch; |
| 2717 | if (ext4_has_feature_flex_bg(sb)) { |
Chunguang Xu | 82ef137 | 2020-12-04 11:05:43 +0800 | [diff] [blame] | 2718 | nr = 1 << sbi->s_log_groups_per_flex; |
| 2719 | nr -= group & (nr - 1); |
| 2720 | nr = min(nr, sbi->s_mb_prefetch); |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2721 | } |
| 2722 | prefetch_grp = ext4_mb_prefetch(sb, group, |
| 2723 | nr, &prefetch_ios); |
| 2724 | if (prefetch_ios == curr_ios) |
| 2725 | nr = 0; |
| 2726 | } |
| 2727 | |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 2728 | /* This now checks without needing the buddy page */ |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2729 | ret = ext4_mb_good_group_nolock(ac, group, cr); |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2730 | if (ret <= 0) { |
| 2731 | if (!first_err) |
| 2732 | first_err = ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2733 | continue; |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2734 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2735 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2736 | err = ext4_mb_load_buddy(sb, group, &e4b); |
| 2737 | if (err) |
| 2738 | goto out; |
| 2739 | |
| 2740 | ext4_lock_group(sb, group); |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 2741 | |
| 2742 | /* |
| 2743 | * We need to check again after locking the |
| 2744 | * block group |
| 2745 | */ |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2746 | ret = ext4_mb_good_group(ac, group, cr); |
Ritesh Harjani | 8ef123fe | 2020-05-20 12:10:35 +0530 | [diff] [blame] | 2747 | if (ret == 0) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2748 | ext4_unlock_group(sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 2749 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2750 | continue; |
| 2751 | } |
| 2752 | |
| 2753 | ac->ac_groups_scanned++; |
Jan Kara | d9b22cf | 2017-02-10 00:50:56 -0500 | [diff] [blame] | 2754 | if (cr == 0) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2755 | ext4_mb_simple_scan_group(ac, &e4b); |
Eric Sandeen | 506bf2d | 2010-07-27 11:56:06 -0400 | [diff] [blame] | 2756 | else if (cr == 1 && sbi->s_stripe && |
| 2757 | !(ac->ac_g_ex.fe_len % sbi->s_stripe)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2758 | ext4_mb_scan_aligned(ac, &e4b); |
| 2759 | else |
| 2760 | ext4_mb_complex_scan_group(ac, &e4b); |
| 2761 | |
| 2762 | ext4_unlock_group(sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 2763 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2764 | |
| 2765 | if (ac->ac_status != AC_STATUS_CONTINUE) |
| 2766 | break; |
| 2767 | } |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 2768 | /* Processed all groups and haven't found blocks */ |
| 2769 | if (sbi->s_mb_stats && i == ngroups) |
| 2770 | atomic64_inc(&sbi->s_bal_cX_failed[cr]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND && |
| 2774 | !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { |
| 2775 | /* |
| 2776 | * We've been searching too long. Let's try to allocate |
| 2777 | * the best chunk we've found so far |
| 2778 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2779 | ext4_mb_try_best_found(ac, &e4b); |
| 2780 | if (ac->ac_status != AC_STATUS_FOUND) { |
| 2781 | /* |
| 2782 | * Someone more lucky has already allocated it. |
| 2783 | * The only thing we can do is just take first |
| 2784 | * found block(s) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2785 | */ |
brookxu | 66d5e02 | 2020-08-17 15:36:06 +0800 | [diff] [blame] | 2786 | lost = atomic_inc_return(&sbi->s_mb_lost_chunks); |
| 2787 | 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] | 2788 | ac->ac_b_ex.fe_group, ac->ac_b_ex.fe_start, |
| 2789 | ac->ac_b_ex.fe_len, lost); |
| 2790 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2791 | ac->ac_b_ex.fe_group = 0; |
| 2792 | ac->ac_b_ex.fe_start = 0; |
| 2793 | ac->ac_b_ex.fe_len = 0; |
| 2794 | ac->ac_status = AC_STATUS_CONTINUE; |
| 2795 | ac->ac_flags |= EXT4_MB_HINT_FIRST; |
| 2796 | cr = 3; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2797 | goto repeat; |
| 2798 | } |
| 2799 | } |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 2800 | |
| 2801 | if (sbi->s_mb_stats && ac->ac_status == AC_STATUS_FOUND) |
| 2802 | atomic64_inc(&sbi->s_bal_cX_hits[ac->ac_criteria]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2803 | out: |
Lukas Czerner | 42ac184 | 2015-06-08 11:40:40 -0400 | [diff] [blame] | 2804 | if (!err && ac->ac_status != AC_STATUS_FOUND && first_err) |
| 2805 | err = first_err; |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 2806 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 2807 | 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] | 2808 | ac->ac_b_ex.fe_len, ac->ac_o_ex.fe_len, ac->ac_status, |
| 2809 | ac->ac_flags, cr, err); |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 2810 | |
| 2811 | if (nr) |
| 2812 | ext4_mb_prefetch_fini(sb, prefetch_grp, nr); |
| 2813 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2814 | return err; |
| 2815 | } |
| 2816 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2817 | static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos) |
| 2818 | { |
Christoph Hellwig | 247dbed | 2018-04-11 11:37:23 +0200 | [diff] [blame] | 2819 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2820 | ext4_group_t group; |
| 2821 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2822 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2823 | return NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2824 | group = *pos + 1; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2825 | return (void *) ((unsigned long) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2829 | { |
Christoph Hellwig | 247dbed | 2018-04-11 11:37:23 +0200 | [diff] [blame] | 2830 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2831 | ext4_group_t group; |
| 2832 | |
| 2833 | ++*pos; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 2834 | if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2835 | return NULL; |
| 2836 | group = *pos + 1; |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2837 | return (void *) ((unsigned long) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v) |
| 2841 | { |
Christoph Hellwig | 247dbed | 2018-04-11 11:37:23 +0200 | [diff] [blame] | 2842 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2843 | ext4_group_t group = (ext4_group_t) ((unsigned long) v); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2844 | int i; |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2845 | int err, buddy_loaded = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2846 | struct ext4_buddy e4b; |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2847 | struct ext4_group_info *grinfo; |
Arnd Bergmann | 2df2c34 | 2017-08-05 21:57:46 -0400 | [diff] [blame] | 2848 | unsigned char blocksize_bits = min_t(unsigned char, |
| 2849 | sb->s_blocksize_bits, |
| 2850 | EXT4_MAX_BLOCK_LOG_SIZE); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2851 | struct sg { |
| 2852 | struct ext4_group_info info; |
Theodore Ts'o | b80b32b | 2017-08-14 08:29:18 -0400 | [diff] [blame] | 2853 | ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2]; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2854 | } sg; |
| 2855 | |
| 2856 | group--; |
| 2857 | if (group == 0) |
Rasmus Villemoes | 97b4af2 | 2015-06-15 00:32:58 -0400 | [diff] [blame] | 2858 | seq_puts(seq, "#group: free frags first [" |
| 2859 | " 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] | 2860 | " 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] | 2861 | |
Theodore Ts'o | b80b32b | 2017-08-14 08:29:18 -0400 | [diff] [blame] | 2862 | i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) + |
| 2863 | sizeof(struct ext4_group_info); |
| 2864 | |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2865 | grinfo = ext4_get_group_info(sb, group); |
| 2866 | /* Load the group info in memory only if not already loaded. */ |
| 2867 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) { |
| 2868 | err = ext4_mb_load_buddy(sb, group, &e4b); |
| 2869 | if (err) { |
| 2870 | seq_printf(seq, "#%-5u: I/O error\n", group); |
| 2871 | return 0; |
| 2872 | } |
| 2873 | buddy_loaded = 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2874 | } |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2875 | |
Theodore Ts'o | b80b32b | 2017-08-14 08:29:18 -0400 | [diff] [blame] | 2876 | memcpy(&sg, ext4_get_group_info(sb, group), i); |
Aditya Kali | 1c8457c | 2012-06-30 19:10:57 -0400 | [diff] [blame] | 2877 | |
| 2878 | if (buddy_loaded) |
| 2879 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2880 | |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 2881 | seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2882 | sg.info.bb_fragments, sg.info.bb_first_free); |
| 2883 | for (i = 0; i <= 13; i++) |
Arnd Bergmann | 2df2c34 | 2017-08-05 21:57:46 -0400 | [diff] [blame] | 2884 | seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ? |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2885 | sg.info.bb_counters[i] : 0); |
Xu Wang | e0d438c | 2020-08-10 02:21:58 +0000 | [diff] [blame] | 2886 | seq_puts(seq, " ]\n"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2887 | |
| 2888 | return 0; |
| 2889 | } |
| 2890 | |
| 2891 | static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v) |
| 2892 | { |
| 2893 | } |
| 2894 | |
Christoph Hellwig | 247dbed | 2018-04-11 11:37:23 +0200 | [diff] [blame] | 2895 | const struct seq_operations ext4_mb_seq_groups_ops = { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2896 | .start = ext4_mb_seq_groups_start, |
| 2897 | .next = ext4_mb_seq_groups_next, |
| 2898 | .stop = ext4_mb_seq_groups_stop, |
| 2899 | .show = ext4_mb_seq_groups_show, |
| 2900 | }; |
| 2901 | |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 2902 | int ext4_seq_mb_stats_show(struct seq_file *seq, void *offset) |
| 2903 | { |
| 2904 | struct super_block *sb = (struct super_block *)seq->private; |
| 2905 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2906 | |
| 2907 | seq_puts(seq, "mballoc:\n"); |
| 2908 | if (!sbi->s_mb_stats) { |
| 2909 | seq_puts(seq, "\tmb stats collection turned off.\n"); |
| 2910 | seq_puts(seq, "\tTo enable, please write \"1\" to sysfs file mb_stats.\n"); |
| 2911 | return 0; |
| 2912 | } |
| 2913 | seq_printf(seq, "\treqs: %u\n", atomic_read(&sbi->s_bal_reqs)); |
| 2914 | seq_printf(seq, "\tsuccess: %u\n", atomic_read(&sbi->s_bal_success)); |
| 2915 | |
| 2916 | seq_printf(seq, "\tgroups_scanned: %u\n", atomic_read(&sbi->s_bal_groups_scanned)); |
| 2917 | |
| 2918 | seq_puts(seq, "\tcr0_stats:\n"); |
| 2919 | seq_printf(seq, "\t\thits: %llu\n", atomic64_read(&sbi->s_bal_cX_hits[0])); |
| 2920 | seq_printf(seq, "\t\tgroups_considered: %llu\n", |
| 2921 | atomic64_read(&sbi->s_bal_cX_groups_considered[0])); |
| 2922 | seq_printf(seq, "\t\tuseless_loops: %llu\n", |
| 2923 | atomic64_read(&sbi->s_bal_cX_failed[0])); |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 2924 | seq_printf(seq, "\t\tbad_suggestions: %u\n", |
| 2925 | atomic_read(&sbi->s_bal_cr0_bad_suggestions)); |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 2926 | |
| 2927 | seq_puts(seq, "\tcr1_stats:\n"); |
| 2928 | seq_printf(seq, "\t\thits: %llu\n", atomic64_read(&sbi->s_bal_cX_hits[1])); |
| 2929 | seq_printf(seq, "\t\tgroups_considered: %llu\n", |
| 2930 | atomic64_read(&sbi->s_bal_cX_groups_considered[1])); |
| 2931 | seq_printf(seq, "\t\tuseless_loops: %llu\n", |
| 2932 | atomic64_read(&sbi->s_bal_cX_failed[1])); |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 2933 | seq_printf(seq, "\t\tbad_suggestions: %u\n", |
| 2934 | atomic_read(&sbi->s_bal_cr1_bad_suggestions)); |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 2935 | |
| 2936 | seq_puts(seq, "\tcr2_stats:\n"); |
| 2937 | seq_printf(seq, "\t\thits: %llu\n", atomic64_read(&sbi->s_bal_cX_hits[2])); |
| 2938 | seq_printf(seq, "\t\tgroups_considered: %llu\n", |
| 2939 | atomic64_read(&sbi->s_bal_cX_groups_considered[2])); |
| 2940 | seq_printf(seq, "\t\tuseless_loops: %llu\n", |
| 2941 | atomic64_read(&sbi->s_bal_cX_failed[2])); |
| 2942 | |
| 2943 | seq_puts(seq, "\tcr3_stats:\n"); |
| 2944 | seq_printf(seq, "\t\thits: %llu\n", atomic64_read(&sbi->s_bal_cX_hits[3])); |
| 2945 | seq_printf(seq, "\t\tgroups_considered: %llu\n", |
| 2946 | atomic64_read(&sbi->s_bal_cX_groups_considered[3])); |
| 2947 | seq_printf(seq, "\t\tuseless_loops: %llu\n", |
| 2948 | atomic64_read(&sbi->s_bal_cX_failed[3])); |
| 2949 | seq_printf(seq, "\textents_scanned: %u\n", atomic_read(&sbi->s_bal_ex_scanned)); |
| 2950 | seq_printf(seq, "\t\tgoal_hits: %u\n", atomic_read(&sbi->s_bal_goals)); |
| 2951 | seq_printf(seq, "\t\t2^n_hits: %u\n", atomic_read(&sbi->s_bal_2orders)); |
| 2952 | seq_printf(seq, "\t\tbreaks: %u\n", atomic_read(&sbi->s_bal_breaks)); |
| 2953 | seq_printf(seq, "\t\tlost: %u\n", atomic_read(&sbi->s_mb_lost_chunks)); |
| 2954 | |
| 2955 | seq_printf(seq, "\tbuddies_generated: %u/%u\n", |
| 2956 | atomic_read(&sbi->s_mb_buddies_generated), |
| 2957 | ext4_get_groups_count(sb)); |
| 2958 | seq_printf(seq, "\tbuddies_time_used: %llu\n", |
| 2959 | atomic64_read(&sbi->s_mb_generation_time)); |
| 2960 | seq_printf(seq, "\tpreallocated: %u\n", |
| 2961 | atomic_read(&sbi->s_mb_preallocated)); |
| 2962 | seq_printf(seq, "\tdiscarded: %u\n", |
| 2963 | atomic_read(&sbi->s_mb_discarded)); |
| 2964 | return 0; |
| 2965 | } |
| 2966 | |
Harshad Shirwadkar | f68f406 | 2021-04-01 10:21:28 -0700 | [diff] [blame] | 2967 | static void *ext4_mb_seq_structs_summary_start(struct seq_file *seq, loff_t *pos) |
| 2968 | { |
| 2969 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
| 2970 | unsigned long position; |
| 2971 | |
| 2972 | read_lock(&EXT4_SB(sb)->s_mb_rb_lock); |
| 2973 | |
| 2974 | if (*pos < 0 || *pos >= MB_NUM_ORDERS(sb) + 1) |
| 2975 | return NULL; |
| 2976 | position = *pos + 1; |
| 2977 | return (void *) ((unsigned long) position); |
| 2978 | } |
| 2979 | |
| 2980 | static void *ext4_mb_seq_structs_summary_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2981 | { |
| 2982 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
| 2983 | unsigned long position; |
| 2984 | |
| 2985 | ++*pos; |
| 2986 | if (*pos < 0 || *pos >= MB_NUM_ORDERS(sb) + 1) |
| 2987 | return NULL; |
| 2988 | position = *pos + 1; |
| 2989 | return (void *) ((unsigned long) position); |
| 2990 | } |
| 2991 | |
| 2992 | static int ext4_mb_seq_structs_summary_show(struct seq_file *seq, void *v) |
| 2993 | { |
| 2994 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
| 2995 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2996 | unsigned long position = ((unsigned long) v); |
| 2997 | struct ext4_group_info *grp; |
| 2998 | struct rb_node *n; |
| 2999 | unsigned int count, min, max; |
| 3000 | |
| 3001 | position--; |
| 3002 | if (position >= MB_NUM_ORDERS(sb)) { |
| 3003 | seq_puts(seq, "fragment_size_tree:\n"); |
| 3004 | n = rb_first(&sbi->s_mb_avg_fragment_size_root); |
| 3005 | if (!n) { |
| 3006 | seq_puts(seq, "\ttree_min: 0\n\ttree_max: 0\n\ttree_nodes: 0\n"); |
| 3007 | return 0; |
| 3008 | } |
| 3009 | grp = rb_entry(n, struct ext4_group_info, bb_avg_fragment_size_rb); |
| 3010 | min = grp->bb_fragments ? grp->bb_free / grp->bb_fragments : 0; |
| 3011 | count = 1; |
| 3012 | while (rb_next(n)) { |
| 3013 | count++; |
| 3014 | n = rb_next(n); |
| 3015 | } |
| 3016 | grp = rb_entry(n, struct ext4_group_info, bb_avg_fragment_size_rb); |
| 3017 | max = grp->bb_fragments ? grp->bb_free / grp->bb_fragments : 0; |
| 3018 | |
| 3019 | seq_printf(seq, "\ttree_min: %u\n\ttree_max: %u\n\ttree_nodes: %u\n", |
| 3020 | min, max, count); |
| 3021 | return 0; |
| 3022 | } |
| 3023 | |
| 3024 | if (position == 0) { |
| 3025 | seq_printf(seq, "optimize_scan: %d\n", |
| 3026 | test_opt2(sb, MB_OPTIMIZE_SCAN) ? 1 : 0); |
| 3027 | seq_puts(seq, "max_free_order_lists:\n"); |
| 3028 | } |
| 3029 | count = 0; |
| 3030 | list_for_each_entry(grp, &sbi->s_mb_largest_free_orders[position], |
| 3031 | bb_largest_free_order_node) |
| 3032 | count++; |
| 3033 | seq_printf(seq, "\tlist_order_%u_groups: %u\n", |
| 3034 | (unsigned int)position, count); |
| 3035 | |
| 3036 | return 0; |
| 3037 | } |
| 3038 | |
| 3039 | static void ext4_mb_seq_structs_summary_stop(struct seq_file *seq, void *v) |
| 3040 | { |
| 3041 | struct super_block *sb = PDE_DATA(file_inode(seq->file)); |
| 3042 | |
| 3043 | read_unlock(&EXT4_SB(sb)->s_mb_rb_lock); |
| 3044 | } |
| 3045 | |
| 3046 | const struct seq_operations ext4_mb_seq_structs_summary_ops = { |
| 3047 | .start = ext4_mb_seq_structs_summary_start, |
| 3048 | .next = ext4_mb_seq_structs_summary_next, |
| 3049 | .stop = ext4_mb_seq_structs_summary_stop, |
| 3050 | .show = ext4_mb_seq_structs_summary_show, |
| 3051 | }; |
| 3052 | |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3053 | static struct kmem_cache *get_groupinfo_cache(int blocksize_bits) |
| 3054 | { |
| 3055 | int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE; |
| 3056 | struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index]; |
| 3057 | |
| 3058 | BUG_ON(!cachep); |
| 3059 | return cachep; |
| 3060 | } |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3061 | |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3062 | /* |
| 3063 | * Allocate the top-level s_group_info array for the specified number |
| 3064 | * of groups |
| 3065 | */ |
| 3066 | int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups) |
| 3067 | { |
| 3068 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3069 | unsigned size; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3070 | struct ext4_group_info ***old_groupinfo, ***new_groupinfo; |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3071 | |
| 3072 | size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >> |
| 3073 | EXT4_DESC_PER_BLOCK_BITS(sb); |
| 3074 | if (size <= sbi->s_group_info_size) |
| 3075 | return 0; |
| 3076 | |
| 3077 | size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size); |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 3078 | new_groupinfo = kvzalloc(size, GFP_KERNEL); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3079 | if (!new_groupinfo) { |
| 3080 | ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group"); |
| 3081 | return -ENOMEM; |
| 3082 | } |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3083 | rcu_read_lock(); |
| 3084 | old_groupinfo = rcu_dereference(sbi->s_group_info); |
| 3085 | if (old_groupinfo) |
| 3086 | memcpy(new_groupinfo, old_groupinfo, |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3087 | sbi->s_group_info_size * sizeof(*sbi->s_group_info)); |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3088 | rcu_read_unlock(); |
| 3089 | rcu_assign_pointer(sbi->s_group_info, new_groupinfo); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3090 | sbi->s_group_info_size = size / sizeof(*sbi->s_group_info); |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3091 | if (old_groupinfo) |
| 3092 | ext4_kvfree_array_rcu(old_groupinfo); |
Jack Qiu | 666245d | 2021-04-09 12:20:35 +0800 | [diff] [blame] | 3093 | ext4_debug("allocated s_groupinfo array for %d meta_bg's\n", |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3094 | sbi->s_group_info_size); |
| 3095 | return 0; |
| 3096 | } |
| 3097 | |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3098 | /* 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] | 3099 | 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] | 3100 | struct ext4_group_desc *desc) |
| 3101 | { |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3102 | int i; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3103 | int metalen = 0; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3104 | int idx = group >> EXT4_DESC_PER_BLOCK_BITS(sb); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3105 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3106 | struct ext4_group_info **meta_group_info; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3107 | struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3108 | |
| 3109 | /* |
| 3110 | * First check if this group is the first of a reserved block. |
| 3111 | * If it's true, we have to allocate a new table of pointers |
| 3112 | * to ext4_group_info structures |
| 3113 | */ |
| 3114 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { |
| 3115 | metalen = sizeof(*meta_group_info) << |
| 3116 | EXT4_DESC_PER_BLOCK_BITS(sb); |
Dmitry Monakhov | 4fdb554 | 2014-11-25 13:08:04 -0500 | [diff] [blame] | 3117 | meta_group_info = kmalloc(metalen, GFP_NOFS); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3118 | if (meta_group_info == NULL) { |
Joe Perches | 7f6a11e | 2012-03-19 23:09:43 -0400 | [diff] [blame] | 3119 | ext4_msg(sb, KERN_ERR, "can't allocate mem " |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3120 | "for a buddy group"); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3121 | goto exit_meta_group_info; |
| 3122 | } |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3123 | rcu_read_lock(); |
| 3124 | rcu_dereference(sbi->s_group_info)[idx] = meta_group_info; |
| 3125 | rcu_read_unlock(); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3126 | } |
| 3127 | |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3128 | meta_group_info = sbi_array_rcu_deref(sbi, s_group_info, idx); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3129 | i = group & (EXT4_DESC_PER_BLOCK(sb) - 1); |
| 3130 | |
Dmitry Monakhov | 4fdb554 | 2014-11-25 13:08:04 -0500 | [diff] [blame] | 3131 | meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3132 | if (meta_group_info[i] == NULL) { |
Joe Perches | 7f6a11e | 2012-03-19 23:09:43 -0400 | [diff] [blame] | 3133 | ext4_msg(sb, KERN_ERR, "can't allocate buddy mem"); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3134 | goto exit_group_info; |
| 3135 | } |
| 3136 | set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, |
| 3137 | &(meta_group_info[i]->bb_state)); |
| 3138 | |
| 3139 | /* |
| 3140 | * initialize bb_free to be able to skip |
| 3141 | * empty groups without initialization |
| 3142 | */ |
Theodore Ts'o | 8844618 | 2018-06-14 00:58:00 -0400 | [diff] [blame] | 3143 | if (ext4_has_group_desc_csum(sb) && |
| 3144 | (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3145 | meta_group_info[i]->bb_free = |
Theodore Ts'o | cff1dfd7 | 2011-09-09 19:12:51 -0400 | [diff] [blame] | 3146 | ext4_free_clusters_after_init(sb, group, desc); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3147 | } else { |
| 3148 | meta_group_info[i]->bb_free = |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 3149 | ext4_free_group_clusters(sb, desc); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 3153 | init_rwsem(&meta_group_info[i]->alloc_sem); |
Venkatesh Pallipadi | 64e290e | 2010-03-04 22:25:21 -0500 | [diff] [blame] | 3154 | meta_group_info[i]->bb_free_root = RB_ROOT; |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 3155 | INIT_LIST_HEAD(&meta_group_info[i]->bb_largest_free_order_node); |
| 3156 | RB_CLEAR_NODE(&meta_group_info[i]->bb_avg_fragment_size_rb); |
Curt Wohlgemuth | 8a57d9d | 2010-05-16 15:00:00 -0400 | [diff] [blame] | 3157 | meta_group_info[i]->bb_largest_free_order = -1; /* uninit */ |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 3158 | meta_group_info[i]->bb_group = group; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3159 | |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 3160 | mb_group_bb_bitmap_alloc(sb, meta_group_info[i], group); |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3161 | return 0; |
| 3162 | |
| 3163 | exit_group_info: |
| 3164 | /* If a meta_group_info table has been allocated, release it now */ |
Tao Ma | caaf7a2 | 2011-07-11 18:42:42 -0400 | [diff] [blame] | 3165 | if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3166 | struct ext4_group_info ***group_info; |
| 3167 | |
| 3168 | rcu_read_lock(); |
| 3169 | group_info = rcu_dereference(sbi->s_group_info); |
| 3170 | kfree(group_info[idx]); |
| 3171 | group_info[idx] = NULL; |
| 3172 | rcu_read_unlock(); |
Tao Ma | caaf7a2 | 2011-07-11 18:42:42 -0400 | [diff] [blame] | 3173 | } |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3174 | exit_meta_group_info: |
| 3175 | return -ENOMEM; |
| 3176 | } /* ext4_mb_add_groupinfo */ |
| 3177 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3178 | static int ext4_mb_init_backend(struct super_block *sb) |
| 3179 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3180 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3181 | ext4_group_t i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3182 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3183 | int err; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3184 | struct ext4_group_desc *desc; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3185 | struct ext4_group_info ***group_info; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3186 | struct kmem_cache *cachep; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3187 | |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3188 | err = ext4_mb_alloc_groupinfo(sb, ngroups); |
| 3189 | if (err) |
| 3190 | return err; |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3191 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3192 | sbi->s_buddy_cache = new_inode(sb); |
| 3193 | if (sbi->s_buddy_cache == NULL) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3194 | ext4_msg(sb, KERN_ERR, "can't get new inode"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3195 | goto err_freesgi; |
| 3196 | } |
Yu Jian | 48e6061 | 2011-08-01 17:41:39 -0400 | [diff] [blame] | 3197 | /* To avoid potentially colliding with an valid on-disk inode number, |
| 3198 | * use EXT4_BAD_INO for the buddy cache inode number. This inode is |
| 3199 | * not in the inode hash, so it should never be found by iget(), but |
| 3200 | * this will avoid confusion if it ever shows up during debugging. */ |
| 3201 | sbi->s_buddy_cache->i_ino = EXT4_BAD_INO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3202 | EXT4_I(sbi->s_buddy_cache)->i_disksize = 0; |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3203 | for (i = 0; i < ngroups; i++) { |
Khazhismel Kumykov | 4b99faa | 2019-04-25 12:58:01 -0400 | [diff] [blame] | 3204 | cond_resched(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3205 | desc = ext4_get_group_desc(sb, i, NULL); |
| 3206 | if (desc == NULL) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3207 | ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3208 | goto err_freebuddy; |
| 3209 | } |
Frederic Bohe | 5f21b0e | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3210 | if (ext4_mb_add_groupinfo(sb, i, desc) != 0) |
| 3211 | goto err_freebuddy; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3212 | } |
| 3213 | |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 3214 | if (ext4_has_feature_flex_bg(sb)) { |
Sabyrzhan Tasbolatov | f91436d | 2021-02-24 15:58:00 +0600 | [diff] [blame] | 3215 | /* a single flex group is supposed to be read by a single IO. |
| 3216 | * 2 ^ s_log_groups_per_flex != UINT_MAX as s_mb_prefetch is |
| 3217 | * unsigned integer, so the maximum shift is 32. |
| 3218 | */ |
| 3219 | if (sbi->s_es->s_log_groups_per_flex >= 32) { |
| 3220 | ext4_msg(sb, KERN_ERR, "too many log groups per flexible block group"); |
Phillip Potter | a8867f4 | 2021-04-12 08:38:37 +0100 | [diff] [blame] | 3221 | goto err_freebuddy; |
Sabyrzhan Tasbolatov | f91436d | 2021-02-24 15:58:00 +0600 | [diff] [blame] | 3222 | } |
| 3223 | sbi->s_mb_prefetch = min_t(uint, 1 << sbi->s_es->s_log_groups_per_flex, |
Chunguang Xu | 82ef137 | 2020-12-04 11:05:43 +0800 | [diff] [blame] | 3224 | BLK_MAX_SEGMENT_SIZE >> (sb->s_blocksize_bits - 9)); |
Alex Zhuravlev | cfd7323 | 2020-04-21 10:54:07 +0300 | [diff] [blame] | 3225 | sbi->s_mb_prefetch *= 8; /* 8 prefetch IOs in flight at most */ |
| 3226 | } else { |
| 3227 | sbi->s_mb_prefetch = 32; |
| 3228 | } |
| 3229 | if (sbi->s_mb_prefetch > ext4_get_groups_count(sb)) |
| 3230 | sbi->s_mb_prefetch = ext4_get_groups_count(sb); |
| 3231 | /* now many real IOs to prefetch within a single allocation at cr=0 |
| 3232 | * given cr=0 is an CPU-related optimization we shouldn't try to |
| 3233 | * load too many groups, at some point we should start to use what |
| 3234 | * we've got in memory. |
| 3235 | * with an average random access time 5ms, it'd take a second to get |
| 3236 | * 200 groups (* N with flex_bg), so let's make this limit 4 |
| 3237 | */ |
| 3238 | sbi->s_mb_prefetch_limit = sbi->s_mb_prefetch * 4; |
| 3239 | if (sbi->s_mb_prefetch_limit > ext4_get_groups_count(sb)) |
| 3240 | sbi->s_mb_prefetch_limit = ext4_get_groups_count(sb); |
| 3241 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3242 | return 0; |
| 3243 | |
| 3244 | err_freebuddy: |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3245 | cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
Roel Kluin | f1fa334 | 2008-04-29 22:01:15 -0400 | [diff] [blame] | 3246 | while (i-- > 0) |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3247 | kmem_cache_free(cachep, ext4_get_group_info(sb, i)); |
Theodore Ts'o | 28623c2 | 2012-09-05 01:31:50 -0400 | [diff] [blame] | 3248 | i = sbi->s_group_info_size; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3249 | rcu_read_lock(); |
| 3250 | group_info = rcu_dereference(sbi->s_group_info); |
Roel Kluin | f1fa334 | 2008-04-29 22:01:15 -0400 | [diff] [blame] | 3251 | while (i-- > 0) |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3252 | kfree(group_info[i]); |
| 3253 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3254 | iput(sbi->s_buddy_cache); |
| 3255 | err_freesgi: |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3256 | rcu_read_lock(); |
| 3257 | kvfree(rcu_dereference(sbi->s_group_info)); |
| 3258 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3259 | return -ENOMEM; |
| 3260 | } |
| 3261 | |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 3262 | static void ext4_groupinfo_destroy_slabs(void) |
| 3263 | { |
| 3264 | int i; |
| 3265 | |
| 3266 | for (i = 0; i < NR_GRPINFO_CACHES; i++) { |
Sean Fu | 21c580d | 2018-05-20 22:44:13 -0400 | [diff] [blame] | 3267 | kmem_cache_destroy(ext4_groupinfo_caches[i]); |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 3268 | ext4_groupinfo_caches[i] = NULL; |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | static int ext4_groupinfo_create_slab(size_t size) |
| 3273 | { |
| 3274 | static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex); |
| 3275 | int slab_size; |
| 3276 | int blocksize_bits = order_base_2(size); |
| 3277 | int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE; |
| 3278 | struct kmem_cache *cachep; |
| 3279 | |
| 3280 | if (cache_index >= NR_GRPINFO_CACHES) |
| 3281 | return -EINVAL; |
| 3282 | |
| 3283 | if (unlikely(cache_index < 0)) |
| 3284 | cache_index = 0; |
| 3285 | |
| 3286 | mutex_lock(&ext4_grpinfo_slab_create_mutex); |
| 3287 | if (ext4_groupinfo_caches[cache_index]) { |
| 3288 | mutex_unlock(&ext4_grpinfo_slab_create_mutex); |
| 3289 | return 0; /* Already created */ |
| 3290 | } |
| 3291 | |
| 3292 | slab_size = offsetof(struct ext4_group_info, |
| 3293 | bb_counters[blocksize_bits + 2]); |
| 3294 | |
| 3295 | cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index], |
| 3296 | slab_size, 0, SLAB_RECLAIM_ACCOUNT, |
| 3297 | NULL); |
| 3298 | |
Tao Ma | 823ba01 | 2011-07-11 18:26:01 -0400 | [diff] [blame] | 3299 | ext4_groupinfo_caches[cache_index] = cachep; |
| 3300 | |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 3301 | mutex_unlock(&ext4_grpinfo_slab_create_mutex); |
| 3302 | if (!cachep) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3303 | printk(KERN_EMERG |
| 3304 | "EXT4-fs: no memory for groupinfo slab cache\n"); |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 3305 | return -ENOMEM; |
| 3306 | } |
| 3307 | |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 3308 | return 0; |
| 3309 | } |
| 3310 | |
Akira Fujita | 9d99012 | 2012-05-28 14:19:25 -0400 | [diff] [blame] | 3311 | int ext4_mb_init(struct super_block *sb) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3312 | { |
| 3313 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3314 | unsigned i, j; |
Nicolai Stange | 935244c | 2016-05-05 19:46:19 -0400 | [diff] [blame] | 3315 | unsigned offset, offset_incr; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3316 | unsigned max; |
Shen Feng | 74767c5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3317 | int ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3318 | |
Harshad Shirwadkar | 4b68f6d | 2021-04-01 10:21:26 -0700 | [diff] [blame] | 3319 | i = MB_NUM_ORDERS(sb) * sizeof(*sbi->s_mb_offsets); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3320 | |
| 3321 | sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL); |
| 3322 | if (sbi->s_mb_offsets == NULL) { |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3323 | ret = -ENOMEM; |
| 3324 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3325 | } |
Yasunori Goto | ff7ef32 | 2008-12-17 00:48:39 -0500 | [diff] [blame] | 3326 | |
Harshad Shirwadkar | 4b68f6d | 2021-04-01 10:21:26 -0700 | [diff] [blame] | 3327 | i = MB_NUM_ORDERS(sb) * sizeof(*sbi->s_mb_maxs); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3328 | sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL); |
| 3329 | if (sbi->s_mb_maxs == NULL) { |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3330 | ret = -ENOMEM; |
| 3331 | goto out; |
| 3332 | } |
| 3333 | |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 3334 | ret = ext4_groupinfo_create_slab(sb->s_blocksize); |
| 3335 | if (ret < 0) |
| 3336 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3337 | |
| 3338 | /* order 0 is regular bitmap */ |
| 3339 | sbi->s_mb_maxs[0] = sb->s_blocksize << 3; |
| 3340 | sbi->s_mb_offsets[0] = 0; |
| 3341 | |
| 3342 | i = 1; |
| 3343 | offset = 0; |
Nicolai Stange | 935244c | 2016-05-05 19:46:19 -0400 | [diff] [blame] | 3344 | offset_incr = 1 << (sb->s_blocksize_bits - 1); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3345 | max = sb->s_blocksize << 2; |
| 3346 | do { |
| 3347 | sbi->s_mb_offsets[i] = offset; |
| 3348 | sbi->s_mb_maxs[i] = max; |
Nicolai Stange | 935244c | 2016-05-05 19:46:19 -0400 | [diff] [blame] | 3349 | offset += offset_incr; |
| 3350 | offset_incr = offset_incr >> 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3351 | max = max >> 1; |
| 3352 | i++; |
Harshad Shirwadkar | 4b68f6d | 2021-04-01 10:21:26 -0700 | [diff] [blame] | 3353 | } while (i < MB_NUM_ORDERS(sb)); |
| 3354 | |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 3355 | sbi->s_mb_avg_fragment_size_root = RB_ROOT; |
| 3356 | sbi->s_mb_largest_free_orders = |
| 3357 | kmalloc_array(MB_NUM_ORDERS(sb), sizeof(struct list_head), |
| 3358 | GFP_KERNEL); |
| 3359 | if (!sbi->s_mb_largest_free_orders) { |
| 3360 | ret = -ENOMEM; |
| 3361 | goto out; |
| 3362 | } |
| 3363 | sbi->s_mb_largest_free_orders_locks = |
| 3364 | kmalloc_array(MB_NUM_ORDERS(sb), sizeof(rwlock_t), |
| 3365 | GFP_KERNEL); |
| 3366 | if (!sbi->s_mb_largest_free_orders_locks) { |
| 3367 | ret = -ENOMEM; |
| 3368 | goto out; |
| 3369 | } |
| 3370 | for (i = 0; i < MB_NUM_ORDERS(sb); i++) { |
| 3371 | INIT_LIST_HEAD(&sbi->s_mb_largest_free_orders[i]); |
| 3372 | rwlock_init(&sbi->s_mb_largest_free_orders_locks[i]); |
| 3373 | } |
| 3374 | rwlock_init(&sbi->s_mb_rb_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3375 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3376 | spin_lock_init(&sbi->s_md_lock); |
Theodore Ts'o | d08854f | 2016-06-26 18:24:01 -0400 | [diff] [blame] | 3377 | sbi->s_mb_free_pending = 0; |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3378 | INIT_LIST_HEAD(&sbi->s_freed_data_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3379 | |
| 3380 | sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN; |
| 3381 | sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN; |
| 3382 | sbi->s_mb_stats = MB_DEFAULT_STATS; |
| 3383 | sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD; |
| 3384 | sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS; |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 3385 | sbi->s_mb_max_inode_prealloc = MB_DEFAULT_MAX_INODE_PREALLOC; |
Theodore Ts'o | 27baebb | 2011-09-09 19:02:51 -0400 | [diff] [blame] | 3386 | /* |
| 3387 | * The default group preallocation is 512, which for 4k block |
| 3388 | * sizes translates to 2 megabytes. However for bigalloc file |
| 3389 | * systems, this is probably too big (i.e, if the cluster size |
| 3390 | * is 1 megabyte, then group preallocation size becomes half a |
| 3391 | * gigabyte!). As a default, we will keep a two megabyte |
| 3392 | * group pralloc size for cluster sizes up to 64k, and after |
| 3393 | * that, we will force a minimum group preallocation size of |
| 3394 | * 32 clusters. This translates to 8 megs when the cluster |
| 3395 | * size is 256k, and 32 megs when the cluster size is 1 meg, |
| 3396 | * which seems reasonable as a default. |
| 3397 | */ |
| 3398 | sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >> |
| 3399 | sbi->s_cluster_bits, 32); |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 3400 | /* |
| 3401 | * If there is a s_stripe > 1, then we set the s_mb_group_prealloc |
| 3402 | * to the lowest multiple of s_stripe which is bigger than |
| 3403 | * the s_mb_group_prealloc as determined above. We want |
| 3404 | * the preallocation size to be an exact multiple of the |
| 3405 | * RAID stripe size so that preallocations don't fragment |
| 3406 | * the stripes. |
| 3407 | */ |
| 3408 | if (sbi->s_stripe > 1) { |
| 3409 | sbi->s_mb_group_prealloc = roundup( |
| 3410 | sbi->s_mb_group_prealloc, sbi->s_stripe); |
| 3411 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3412 | |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 3413 | sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3414 | if (sbi->s_locality_groups == NULL) { |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3415 | ret = -ENOMEM; |
Andrey Tsyvarev | 029b10c | 2014-05-12 12:34:21 -0400 | [diff] [blame] | 3416 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3417 | } |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 3418 | for_each_possible_cpu(i) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3419 | struct ext4_locality_group *lg; |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 3420 | lg = per_cpu_ptr(sbi->s_locality_groups, i); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3421 | mutex_init(&lg->lg_mutex); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 3422 | for (j = 0; j < PREALLOC_TB_SIZE; j++) |
| 3423 | INIT_LIST_HEAD(&lg->lg_prealloc_list[j]); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3424 | spin_lock_init(&lg->lg_prealloc_lock); |
| 3425 | } |
| 3426 | |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 3427 | if (blk_queue_nonrot(bdev_get_queue(sb->s_bdev))) |
| 3428 | sbi->s_mb_max_linear_groups = 0; |
| 3429 | else |
| 3430 | sbi->s_mb_max_linear_groups = MB_DEFAULT_LINEAR_LIMIT; |
Yu Jian | 79a77c5 | 2011-08-01 17:41:46 -0400 | [diff] [blame] | 3431 | /* init file for buddy data */ |
| 3432 | ret = ext4_mb_init_backend(sb); |
Tao Ma | 7aa0bae | 2011-10-06 10:22:28 -0400 | [diff] [blame] | 3433 | if (ret != 0) |
| 3434 | goto out_free_locality_groups; |
Yu Jian | 79a77c5 | 2011-08-01 17:41:46 -0400 | [diff] [blame] | 3435 | |
Tao Ma | 7aa0bae | 2011-10-06 10:22:28 -0400 | [diff] [blame] | 3436 | return 0; |
| 3437 | |
| 3438 | out_free_locality_groups: |
| 3439 | free_percpu(sbi->s_locality_groups); |
| 3440 | sbi->s_locality_groups = NULL; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3441 | out: |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 3442 | kfree(sbi->s_mb_largest_free_orders); |
| 3443 | kfree(sbi->s_mb_largest_free_orders_locks); |
Tao Ma | 7aa0bae | 2011-10-06 10:22:28 -0400 | [diff] [blame] | 3444 | kfree(sbi->s_mb_offsets); |
| 3445 | sbi->s_mb_offsets = NULL; |
| 3446 | kfree(sbi->s_mb_maxs); |
| 3447 | sbi->s_mb_maxs = NULL; |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3448 | return ret; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3449 | } |
| 3450 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3451 | /* need to called with the ext4 group lock held */ |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3452 | static int ext4_mb_cleanup_pa(struct ext4_group_info *grp) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3453 | { |
| 3454 | struct ext4_prealloc_space *pa; |
| 3455 | struct list_head *cur, *tmp; |
| 3456 | int count = 0; |
| 3457 | |
| 3458 | list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) { |
| 3459 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 3460 | list_del(&pa->pa_group_list); |
| 3461 | count++; |
Aneesh Kumar K.V | 688f05a | 2008-10-13 12:14:14 -0400 | [diff] [blame] | 3462 | kmem_cache_free(ext4_pspace_cachep, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3463 | } |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3464 | return count; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | int ext4_mb_release(struct super_block *sb) |
| 3468 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3469 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3470 | ext4_group_t i; |
| 3471 | int num_meta_group_infos; |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3472 | struct ext4_group_info *grinfo, ***group_info; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3473 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3474 | struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3475 | int count; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3476 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3477 | if (sbi->s_group_info) { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3478 | for (i = 0; i < ngroups; i++) { |
Khazhismel Kumykov | 4b99faa | 2019-04-25 12:58:01 -0400 | [diff] [blame] | 3479 | cond_resched(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3480 | grinfo = ext4_get_group_info(sb, i); |
Ritesh Harjani | a345021 | 2020-05-10 11:54:48 +0530 | [diff] [blame] | 3481 | mb_group_bb_bitmap_free(grinfo); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3482 | ext4_lock_group(sb, i); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3483 | count = ext4_mb_cleanup_pa(grinfo); |
| 3484 | if (count) |
| 3485 | mb_debug(sb, "mballoc: %d PAs left\n", |
| 3486 | count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3487 | ext4_unlock_group(sb, i); |
Curt Wohlgemuth | fb1813f | 2010-10-27 21:29:12 -0400 | [diff] [blame] | 3488 | kmem_cache_free(cachep, grinfo); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3489 | } |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 3490 | num_meta_group_infos = (ngroups + |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3491 | EXT4_DESC_PER_BLOCK(sb) - 1) >> |
| 3492 | EXT4_DESC_PER_BLOCK_BITS(sb); |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3493 | rcu_read_lock(); |
| 3494 | group_info = rcu_dereference(sbi->s_group_info); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3495 | for (i = 0; i < num_meta_group_infos; i++) |
Suraj Jitindar Singh | df3da4e | 2020-02-18 19:08:50 -0800 | [diff] [blame] | 3496 | kfree(group_info[i]); |
| 3497 | kvfree(group_info); |
| 3498 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3499 | } |
Harshad Shirwadkar | 196e402 | 2021-04-01 10:21:27 -0700 | [diff] [blame] | 3500 | kfree(sbi->s_mb_largest_free_orders); |
| 3501 | kfree(sbi->s_mb_largest_free_orders_locks); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3502 | kfree(sbi->s_mb_offsets); |
| 3503 | kfree(sbi->s_mb_maxs); |
Markus Elfring | bfcba2d | 2014-11-25 20:01:37 -0500 | [diff] [blame] | 3504 | iput(sbi->s_buddy_cache); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3505 | if (sbi->s_mb_stats) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3506 | ext4_msg(sb, KERN_INFO, |
| 3507 | "mballoc: %u blocks %u reqs (%u success)", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3508 | atomic_read(&sbi->s_bal_allocated), |
| 3509 | atomic_read(&sbi->s_bal_reqs), |
| 3510 | atomic_read(&sbi->s_bal_success)); |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3511 | ext4_msg(sb, KERN_INFO, |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 3512 | "mballoc: %u extents scanned, %u groups scanned, %u goal hits, " |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3513 | "%u 2^N hits, %u breaks, %u lost", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3514 | atomic_read(&sbi->s_bal_ex_scanned), |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 3515 | atomic_read(&sbi->s_bal_groups_scanned), |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3516 | atomic_read(&sbi->s_bal_goals), |
| 3517 | atomic_read(&sbi->s_bal_2orders), |
| 3518 | atomic_read(&sbi->s_bal_breaks), |
| 3519 | atomic_read(&sbi->s_mb_lost_chunks)); |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3520 | ext4_msg(sb, KERN_INFO, |
Harshad Shirwadkar | 67d2518 | 2021-04-01 10:21:23 -0700 | [diff] [blame] | 3521 | "mballoc: %u generated and it took %llu", |
| 3522 | atomic_read(&sbi->s_mb_buddies_generated), |
| 3523 | atomic64_read(&sbi->s_mb_generation_time)); |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 3524 | ext4_msg(sb, KERN_INFO, |
| 3525 | "mballoc: %u preallocated, %u discarded", |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3526 | atomic_read(&sbi->s_mb_preallocated), |
| 3527 | atomic_read(&sbi->s_mb_discarded)); |
| 3528 | } |
| 3529 | |
Eric Sandeen | 730c213 | 2008-09-13 15:23:29 -0400 | [diff] [blame] | 3530 | free_percpu(sbi->s_locality_groups); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3531 | |
| 3532 | return 0; |
| 3533 | } |
| 3534 | |
Lukas Czerner | 77ca6cd | 2010-10-27 21:30:11 -0400 | [diff] [blame] | 3535 | static inline int ext4_issue_discard(struct super_block *sb, |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3536 | ext4_group_t block_group, ext4_grpblk_t cluster, int count, |
| 3537 | struct bio **biop) |
Jiaying Zhang | 5c52183 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 3538 | { |
Jiaying Zhang | 5c52183 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 3539 | ext4_fsblk_t discard_block; |
| 3540 | |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 3541 | discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) + |
| 3542 | ext4_group_first_block_no(sb, block_group)); |
| 3543 | count = EXT4_C2B(EXT4_SB(sb), count); |
Jiaying Zhang | 5c52183 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 3544 | trace_ext4_discard_blocks(sb, |
| 3545 | (unsigned long long) discard_block, count); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3546 | if (biop) { |
| 3547 | return __blkdev_issue_discard(sb->s_bdev, |
| 3548 | (sector_t)discard_block << (sb->s_blocksize_bits - 9), |
| 3549 | (sector_t)count << (sb->s_blocksize_bits - 9), |
| 3550 | GFP_NOFS, 0, biop); |
| 3551 | } else |
| 3552 | return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0); |
Jiaying Zhang | 5c52183 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 3553 | } |
| 3554 | |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3555 | static void ext4_free_data_in_buddy(struct super_block *sb, |
| 3556 | struct ext4_free_data *entry) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3557 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3558 | struct ext4_buddy e4b; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 3559 | struct ext4_group_info *db; |
Theodore Ts'o | d9f3450 | 2011-04-30 13:47:24 -0400 | [diff] [blame] | 3560 | int err, count = 0, count2 = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3561 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3562 | mb_debug(sb, "gonna free %u blocks in group %u (0x%p):", |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3563 | entry->efd_count, entry->efd_group, entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3564 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3565 | err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b); |
| 3566 | /* we expect to find existing buddy because it's pinned */ |
| 3567 | BUG_ON(err != 0); |
Theodore Ts'o | b90f687 | 2010-04-20 16:51:59 -0400 | [diff] [blame] | 3568 | |
Theodore Ts'o | d08854f | 2016-06-26 18:24:01 -0400 | [diff] [blame] | 3569 | spin_lock(&EXT4_SB(sb)->s_md_lock); |
| 3570 | EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count; |
| 3571 | spin_unlock(&EXT4_SB(sb)->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3572 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3573 | db = e4b.bd_info; |
| 3574 | /* there are blocks to put in buddy to make them really free */ |
| 3575 | count += entry->efd_count; |
| 3576 | count2++; |
| 3577 | ext4_lock_group(sb, entry->efd_group); |
| 3578 | /* Take it out of per group rb tree */ |
| 3579 | rb_erase(&entry->efd_node, &(db->bb_free_root)); |
| 3580 | 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] | 3581 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3582 | /* |
| 3583 | * Clear the trimmed flag for the group so that the next |
| 3584 | * ext4_trim_fs can trim it. |
| 3585 | * If the volume is mounted with -o discard, online discard |
| 3586 | * is supported and the free blocks will be trimmed online. |
| 3587 | */ |
| 3588 | if (!test_opt(sb, DISCARD)) |
| 3589 | EXT4_MB_GRP_CLEAR_TRIMMED(db); |
| 3590 | |
| 3591 | if (!db->bb_free_root.rb_node) { |
| 3592 | /* No more items in the per group rb tree |
| 3593 | * balance refcounts from ext4_mb_free_metadata() |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 3594 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3595 | put_page(e4b.bd_buddy_page); |
| 3596 | put_page(e4b.bd_bitmap_page); |
Theodore Ts'o | 3e624fc | 2008-10-16 20:00:24 -0400 | [diff] [blame] | 3597 | } |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3598 | ext4_unlock_group(sb, entry->efd_group); |
| 3599 | kmem_cache_free(ext4_free_data_cachep, entry); |
| 3600 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3601 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 3602 | mb_debug(sb, "freed %d blocks in %d structures\n", count, |
| 3603 | count2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3604 | } |
| 3605 | |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3606 | /* |
| 3607 | * This function is called by the jbd2 layer once the commit has finished, |
| 3608 | * so we know we can free the blocks that were released with that commit. |
| 3609 | */ |
| 3610 | void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid) |
| 3611 | { |
| 3612 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3613 | struct ext4_free_data *entry, *tmp; |
| 3614 | struct bio *discard_bio = NULL; |
| 3615 | struct list_head freed_data_list; |
| 3616 | struct list_head *cut_pos = NULL; |
| 3617 | int err; |
| 3618 | |
| 3619 | INIT_LIST_HEAD(&freed_data_list); |
| 3620 | |
| 3621 | spin_lock(&sbi->s_md_lock); |
| 3622 | list_for_each_entry(entry, &sbi->s_freed_data_list, efd_list) { |
| 3623 | if (entry->efd_tid != commit_tid) |
| 3624 | break; |
| 3625 | cut_pos = &entry->efd_list; |
| 3626 | } |
| 3627 | if (cut_pos) |
| 3628 | list_cut_position(&freed_data_list, &sbi->s_freed_data_list, |
| 3629 | cut_pos); |
| 3630 | spin_unlock(&sbi->s_md_lock); |
| 3631 | |
| 3632 | if (test_opt(sb, DISCARD)) { |
| 3633 | list_for_each_entry(entry, &freed_data_list, efd_list) { |
| 3634 | err = ext4_issue_discard(sb, entry->efd_group, |
| 3635 | entry->efd_start_cluster, |
| 3636 | entry->efd_count, |
| 3637 | &discard_bio); |
| 3638 | if (err && err != -EOPNOTSUPP) { |
| 3639 | ext4_msg(sb, KERN_WARNING, "discard request in" |
| 3640 | " group:%d block:%d count:%d failed" |
| 3641 | " with %d", entry->efd_group, |
| 3642 | entry->efd_start_cluster, |
| 3643 | entry->efd_count, err); |
| 3644 | } else if (err == -EOPNOTSUPP) |
| 3645 | break; |
| 3646 | } |
| 3647 | |
Daeho Jeong | e451057 | 2017-08-05 13:11:57 -0400 | [diff] [blame] | 3648 | if (discard_bio) { |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3649 | submit_bio_wait(discard_bio); |
Daeho Jeong | e451057 | 2017-08-05 13:11:57 -0400 | [diff] [blame] | 3650 | bio_put(discard_bio); |
| 3651 | } |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list) |
| 3655 | ext4_free_data_in_buddy(sb, entry); |
| 3656 | } |
| 3657 | |
Theodore Ts'o | 5dabfc7 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3658 | int __init ext4_init_mballoc(void) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3659 | { |
Theodore Ts'o | 1682808 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 3660 | ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space, |
| 3661 | SLAB_RECLAIM_ACCOUNT); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3662 | if (ext4_pspace_cachep == NULL) |
Ritesh Harjani | f283529 | 2020-05-10 11:54:46 +0530 | [diff] [blame] | 3663 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3664 | |
Theodore Ts'o | 1682808 | 2010-10-27 21:30:09 -0400 | [diff] [blame] | 3665 | ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context, |
| 3666 | SLAB_RECLAIM_ACCOUNT); |
Ritesh Harjani | f283529 | 2020-05-10 11:54:46 +0530 | [diff] [blame] | 3667 | if (ext4_ac_cachep == NULL) |
| 3668 | goto out_pa_free; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 3669 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3670 | ext4_free_data_cachep = KMEM_CACHE(ext4_free_data, |
| 3671 | SLAB_RECLAIM_ACCOUNT); |
Ritesh Harjani | f283529 | 2020-05-10 11:54:46 +0530 | [diff] [blame] | 3672 | if (ext4_free_data_cachep == NULL) |
| 3673 | goto out_ac_free; |
| 3674 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3675 | return 0; |
Ritesh Harjani | f283529 | 2020-05-10 11:54:46 +0530 | [diff] [blame] | 3676 | |
| 3677 | out_ac_free: |
| 3678 | kmem_cache_destroy(ext4_ac_cachep); |
| 3679 | out_pa_free: |
| 3680 | kmem_cache_destroy(ext4_pspace_cachep); |
| 3681 | out: |
| 3682 | return -ENOMEM; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3683 | } |
| 3684 | |
Theodore Ts'o | 5dabfc7 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3685 | void ext4_exit_mballoc(void) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3686 | { |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 3687 | /* |
Jesper Dangaard Brouer | 3e03f9c | 2009-07-05 22:29:27 -0400 | [diff] [blame] | 3688 | * Wait for completion of call_rcu()'s on ext4_pspace_cachep |
| 3689 | * before destroying the slab cache. |
| 3690 | */ |
| 3691 | rcu_barrier(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3692 | kmem_cache_destroy(ext4_pspace_cachep); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 3693 | kmem_cache_destroy(ext4_ac_cachep); |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 3694 | kmem_cache_destroy(ext4_free_data_cachep); |
Eric Sandeen | 2892c15 | 2011-02-12 08:12:18 -0500 | [diff] [blame] | 3695 | ext4_groupinfo_destroy_slabs(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | |
| 3699 | /* |
Uwe Kleine-König | 73b2c71 | 2010-07-30 21:02:47 +0200 | [diff] [blame] | 3700 | * 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] | 3701 | * Returns 0 if success or error code |
| 3702 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3703 | static noinline_for_stack int |
| 3704 | ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3705 | handle_t *handle, unsigned int reserv_clstrs) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3706 | { |
| 3707 | struct buffer_head *bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3708 | struct ext4_group_desc *gdp; |
| 3709 | struct buffer_head *gdp_bh; |
| 3710 | struct ext4_sb_info *sbi; |
| 3711 | struct super_block *sb; |
| 3712 | ext4_fsblk_t block; |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3713 | int err, len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3714 | |
| 3715 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 3716 | BUG_ON(ac->ac_b_ex.fe_len <= 0); |
| 3717 | |
| 3718 | sb = ac->ac_sb; |
| 3719 | sbi = EXT4_SB(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3720 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3721 | 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] | 3722 | if (IS_ERR(bitmap_bh)) { |
| 3723 | err = PTR_ERR(bitmap_bh); |
| 3724 | bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3725 | goto out_err; |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 3726 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3727 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 3728 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3729 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
| 3730 | if (err) |
| 3731 | goto out_err; |
| 3732 | |
| 3733 | err = -EIO; |
| 3734 | gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh); |
| 3735 | if (!gdp) |
| 3736 | goto out_err; |
| 3737 | |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 3738 | 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] | 3739 | ext4_free_group_clusters(sb, gdp)); |
Aneesh Kumar K.V | 03cddb8 | 2008-06-05 20:59:29 -0400 | [diff] [blame] | 3740 | |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 3741 | BUFFER_TRACE(gdp_bh, "get_write_access"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3742 | err = ext4_journal_get_write_access(handle, gdp_bh); |
| 3743 | if (err) |
| 3744 | goto out_err; |
| 3745 | |
Akinobu Mita | bda00de | 2010-03-03 23:53:25 -0500 | [diff] [blame] | 3746 | block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3747 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3748 | len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len); |
Jan Kara | ce9f24c | 2020-07-28 15:04:34 +0200 | [diff] [blame] | 3749 | if (!ext4_inode_block_valid(ac->ac_inode, block, len)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 3750 | ext4_error(sb, "Allocating blocks %llu-%llu which overlap " |
Theodore Ts'o | 1084f25 | 2012-03-19 23:13:43 -0400 | [diff] [blame] | 3751 | "fs metadata", block, block+len); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3752 | /* File system mounted not to panic on error |
Vegard Nossum | 554a5cc | 2016-07-14 23:02:47 -0400 | [diff] [blame] | 3753 | * Fix the bitmap and return EFSCORRUPTED |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3754 | * We leak some of the blocks here. |
| 3755 | */ |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3756 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
Yongqiang Yang | c3e94d1 | 2011-07-26 22:05:53 -0400 | [diff] [blame] | 3757 | ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, |
| 3758 | ac->ac_b_ex.fe_len); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3759 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3760 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3761 | if (!err) |
Vegard Nossum | 554a5cc | 2016-07-14 23:02:47 -0400 | [diff] [blame] | 3762 | err = -EFSCORRUPTED; |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 3763 | goto out_err; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3764 | } |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 3765 | |
| 3766 | ext4_lock_group(sb, ac->ac_b_ex.fe_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3767 | #ifdef AGGRESSIVE_CHECK |
| 3768 | { |
| 3769 | int i; |
| 3770 | for (i = 0; i < ac->ac_b_ex.fe_len; i++) { |
| 3771 | BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i, |
| 3772 | bitmap_bh->b_data)); |
| 3773 | } |
| 3774 | } |
| 3775 | #endif |
Yongqiang Yang | c3e94d1 | 2011-07-26 22:05:53 -0400 | [diff] [blame] | 3776 | ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, |
| 3777 | ac->ac_b_ex.fe_len); |
Theodore Ts'o | 8844618 | 2018-06-14 00:58:00 -0400 | [diff] [blame] | 3778 | if (ext4_has_group_desc_csum(sb) && |
| 3779 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3780 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 3781 | ext4_free_group_clusters_set(sb, gdp, |
Theodore Ts'o | cff1dfd7 | 2011-09-09 19:12:51 -0400 | [diff] [blame] | 3782 | ext4_free_clusters_after_init(sb, |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 3783 | ac->ac_b_ex.fe_group, gdp)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3784 | } |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 3785 | len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len; |
| 3786 | ext4_free_group_clusters_set(sb, gdp, len); |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 3787 | 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] | 3788 | 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] | 3789 | |
| 3790 | ext4_unlock_group(sb, ac->ac_b_ex.fe_group); |
Theodore Ts'o | 5704265 | 2011-09-09 18:56:51 -0400 | [diff] [blame] | 3791 | 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] | 3792 | /* |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 3793 | * Now reduce the dirty block count also. Should not go negative |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 3794 | */ |
Aneesh Kumar K.V | 6bc6e63 | 2008-10-10 09:39:00 -0400 | [diff] [blame] | 3795 | if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED)) |
| 3796 | /* release all the reserved blocks if non delalloc */ |
Theodore Ts'o | 5704265 | 2011-09-09 18:56:51 -0400 | [diff] [blame] | 3797 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
| 3798 | reserv_clstrs); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3799 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3800 | if (sbi->s_log_groups_per_flex) { |
| 3801 | ext4_group_t flex_group = ext4_flex_group(sbi, |
| 3802 | ac->ac_b_ex.fe_group); |
Theodore Ts'o | 90ba983 | 2013-03-11 23:39:59 -0400 | [diff] [blame] | 3803 | atomic64_sub(ac->ac_b_ex.fe_len, |
Suraj Jitindar Singh | 7c99072 | 2020-02-18 19:08:51 -0800 | [diff] [blame] | 3804 | &sbi_array_rcu_deref(sbi, s_flex_groups, |
| 3805 | flex_group)->free_clusters); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3806 | } |
| 3807 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3808 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3809 | if (err) |
| 3810 | goto out_err; |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3811 | err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3812 | |
| 3813 | out_err: |
Aneesh Kumar K.V | 42a10ad | 2008-02-10 01:07:28 -0500 | [diff] [blame] | 3814 | brelse(bitmap_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3815 | return err; |
| 3816 | } |
| 3817 | |
| 3818 | /* |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 3819 | * Idempotent helper for Ext4 fast commit replay path to set the state of |
| 3820 | * blocks in bitmaps and update counters. |
| 3821 | */ |
| 3822 | void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block, |
| 3823 | int len, int state) |
| 3824 | { |
| 3825 | struct buffer_head *bitmap_bh = NULL; |
| 3826 | struct ext4_group_desc *gdp; |
| 3827 | struct buffer_head *gdp_bh; |
| 3828 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3829 | ext4_group_t group; |
| 3830 | ext4_grpblk_t blkoff; |
| 3831 | int i, clen, err; |
| 3832 | int already; |
| 3833 | |
| 3834 | clen = EXT4_B2C(sbi, len); |
| 3835 | |
| 3836 | ext4_get_group_no_and_offset(sb, block, &group, &blkoff); |
| 3837 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
| 3838 | if (IS_ERR(bitmap_bh)) { |
| 3839 | err = PTR_ERR(bitmap_bh); |
| 3840 | bitmap_bh = NULL; |
| 3841 | goto out_err; |
| 3842 | } |
| 3843 | |
| 3844 | err = -EIO; |
| 3845 | gdp = ext4_get_group_desc(sb, group, &gdp_bh); |
| 3846 | if (!gdp) |
| 3847 | goto out_err; |
| 3848 | |
| 3849 | ext4_lock_group(sb, group); |
| 3850 | already = 0; |
| 3851 | for (i = 0; i < clen; i++) |
| 3852 | if (!mb_test_bit(blkoff + i, bitmap_bh->b_data) == !state) |
| 3853 | already++; |
| 3854 | |
| 3855 | if (state) |
| 3856 | ext4_set_bits(bitmap_bh->b_data, blkoff, clen); |
| 3857 | else |
| 3858 | mb_test_and_clear_bits(bitmap_bh->b_data, blkoff, clen); |
| 3859 | if (ext4_has_group_desc_csum(sb) && |
| 3860 | (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { |
| 3861 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
| 3862 | ext4_free_group_clusters_set(sb, gdp, |
| 3863 | ext4_free_clusters_after_init(sb, |
| 3864 | group, gdp)); |
| 3865 | } |
| 3866 | if (state) |
| 3867 | clen = ext4_free_group_clusters(sb, gdp) - clen + already; |
| 3868 | else |
| 3869 | clen = ext4_free_group_clusters(sb, gdp) + clen - already; |
| 3870 | |
| 3871 | ext4_free_group_clusters_set(sb, gdp, clen); |
| 3872 | ext4_block_bitmap_csum_set(sb, group, gdp, bitmap_bh); |
| 3873 | ext4_group_desc_csum_set(sb, group, gdp); |
| 3874 | |
| 3875 | ext4_unlock_group(sb, group); |
| 3876 | |
| 3877 | if (sbi->s_log_groups_per_flex) { |
| 3878 | ext4_group_t flex_group = ext4_flex_group(sbi, group); |
| 3879 | |
| 3880 | atomic64_sub(len, |
| 3881 | &sbi_array_rcu_deref(sbi, s_flex_groups, |
| 3882 | flex_group)->free_clusters); |
| 3883 | } |
| 3884 | |
| 3885 | err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh); |
| 3886 | if (err) |
| 3887 | goto out_err; |
| 3888 | sync_dirty_buffer(bitmap_bh); |
| 3889 | err = ext4_handle_dirty_metadata(NULL, NULL, gdp_bh); |
| 3890 | sync_dirty_buffer(gdp_bh); |
| 3891 | |
| 3892 | out_err: |
| 3893 | brelse(bitmap_bh); |
| 3894 | } |
| 3895 | |
| 3896 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3897 | * here we normalize request for locality group |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 3898 | * Group request are normalized to s_mb_group_prealloc, which goes to |
| 3899 | * s_strip if we set the same via mount option. |
| 3900 | * s_mb_group_prealloc can be configured via |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 3901 | * /sys/fs/ext4/<partition>/mb_group_prealloc |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3902 | * |
| 3903 | * XXX: should we try to preallocate more than the group has now? |
| 3904 | */ |
| 3905 | static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac) |
| 3906 | { |
| 3907 | struct super_block *sb = ac->ac_sb; |
| 3908 | struct ext4_locality_group *lg = ac->ac_lg; |
| 3909 | |
| 3910 | BUG_ON(lg == NULL); |
Dan Ehrenberg | d7a1fee | 2011-07-17 21:11:30 -0400 | [diff] [blame] | 3911 | 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] | 3912 | 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] | 3913 | } |
| 3914 | |
| 3915 | /* |
| 3916 | * Normalization means making request better in terms of |
| 3917 | * size and alignment |
| 3918 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3919 | static noinline_for_stack void |
| 3920 | ext4_mb_normalize_request(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3921 | struct ext4_allocation_request *ar) |
| 3922 | { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3923 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3924 | int bsbits, max; |
| 3925 | ext4_lblk_t end; |
Curt Wohlgemuth | 1592d2c | 2012-02-20 17:53:03 -0500 | [diff] [blame] | 3926 | loff_t size, start_off; |
| 3927 | loff_t orig_size __maybe_unused; |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 3928 | ext4_lblk_t start; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3929 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3930 | struct ext4_prealloc_space *pa; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3931 | |
| 3932 | /* do normalize only data requests, metadata requests |
| 3933 | do not need preallocation */ |
| 3934 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 3935 | return; |
| 3936 | |
| 3937 | /* sometime caller may want exact blocks */ |
| 3938 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 3939 | return; |
| 3940 | |
| 3941 | /* caller may indicate that preallocation isn't |
| 3942 | * required (it's a tail, for example) */ |
| 3943 | if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) |
| 3944 | return; |
| 3945 | |
| 3946 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) { |
| 3947 | ext4_mb_normalize_group_request(ac); |
| 3948 | return ; |
| 3949 | } |
| 3950 | |
| 3951 | bsbits = ac->ac_sb->s_blocksize_bits; |
| 3952 | |
| 3953 | /* first, let's learn actual file size |
| 3954 | * given current request is allocated */ |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 3955 | 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] | 3956 | size = size << bsbits; |
| 3957 | if (size < i_size_read(ac->ac_inode)) |
| 3958 | size = i_size_read(ac->ac_inode); |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 3959 | orig_size = size; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3960 | |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3961 | /* max size of free chunks */ |
| 3962 | max = 2 << bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3963 | |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3964 | #define NRL_CHECK_SIZE(req, size, max, chunk_size) \ |
| 3965 | (req <= (size) || max <= (chunk_size)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3966 | |
| 3967 | /* first, try to predict filesize */ |
| 3968 | /* XXX: should this table be tunable? */ |
| 3969 | start_off = 0; |
| 3970 | if (size <= 16 * 1024) { |
| 3971 | size = 16 * 1024; |
| 3972 | } else if (size <= 32 * 1024) { |
| 3973 | size = 32 * 1024; |
| 3974 | } else if (size <= 64 * 1024) { |
| 3975 | size = 64 * 1024; |
| 3976 | } else if (size <= 128 * 1024) { |
| 3977 | size = 128 * 1024; |
| 3978 | } else if (size <= 256 * 1024) { |
| 3979 | size = 256 * 1024; |
| 3980 | } else if (size <= 512 * 1024) { |
| 3981 | size = 512 * 1024; |
| 3982 | } else if (size <= 1024 * 1024) { |
| 3983 | size = 1024 * 1024; |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3984 | } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3985 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3986 | (21 - bsbits)) << 21; |
| 3987 | size = 2 * 1024 * 1024; |
| 3988 | } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3989 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
| 3990 | (22 - bsbits)) << 22; |
| 3991 | size = 4 * 1024 * 1024; |
| 3992 | } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len, |
Valerie Clement | 1930479 | 2008-05-13 19:31:14 -0400 | [diff] [blame] | 3993 | (8<<20)>>bsbits, max, 8 * 1024)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3994 | start_off = ((loff_t)ac->ac_o_ex.fe_logical >> |
| 3995 | (23 - bsbits)) << 23; |
| 3996 | size = 8 * 1024 * 1024; |
| 3997 | } else { |
Xiaoguang Wang | b27b153 | 2014-07-27 22:26:36 -0400 | [diff] [blame] | 3998 | start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits; |
| 3999 | size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb), |
| 4000 | ac->ac_o_ex.fe_len) << bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4001 | } |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 4002 | size = size >> bsbits; |
| 4003 | start = start_off >> bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4004 | |
| 4005 | /* don't cover already allocated blocks in selected range */ |
| 4006 | if (ar->pleft && start <= ar->lleft) { |
| 4007 | size -= ar->lleft + 1 - start; |
| 4008 | start = ar->lleft + 1; |
| 4009 | } |
| 4010 | if (ar->pright && start + size - 1 >= ar->lright) |
| 4011 | size -= start + size - ar->lright; |
| 4012 | |
Jan Kara | cd648b8 | 2017-01-27 14:34:30 -0500 | [diff] [blame] | 4013 | /* |
| 4014 | * Trim allocation request for filesystems with artificially small |
| 4015 | * groups. |
| 4016 | */ |
| 4017 | if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) |
| 4018 | size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb); |
| 4019 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4020 | end = start + size; |
| 4021 | |
| 4022 | /* check we don't cross already preallocated blocks */ |
| 4023 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 4024 | 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] | 4025 | ext4_lblk_t pa_end; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4026 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4027 | if (pa->pa_deleted) |
| 4028 | continue; |
| 4029 | spin_lock(&pa->pa_lock); |
| 4030 | if (pa->pa_deleted) { |
| 4031 | spin_unlock(&pa->pa_lock); |
| 4032 | continue; |
| 4033 | } |
| 4034 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4035 | pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb), |
| 4036 | pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4037 | |
| 4038 | /* PA must not overlap original request */ |
| 4039 | BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end || |
| 4040 | ac->ac_o_ex.fe_logical < pa->pa_lstart)); |
| 4041 | |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 4042 | /* skip PAs this normalized request doesn't overlap with */ |
| 4043 | if (pa->pa_lstart >= end || pa_end <= start) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4044 | spin_unlock(&pa->pa_lock); |
| 4045 | continue; |
| 4046 | } |
| 4047 | BUG_ON(pa->pa_lstart <= start && pa_end >= end); |
| 4048 | |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 4049 | /* adjust start or end to be adjacent to this pa */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4050 | if (pa_end <= ac->ac_o_ex.fe_logical) { |
| 4051 | BUG_ON(pa_end < start); |
| 4052 | start = pa_end; |
Eric Sandeen | 38877f4 | 2009-08-17 23:55:24 -0400 | [diff] [blame] | 4053 | } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4054 | BUG_ON(pa->pa_lstart > end); |
| 4055 | end = pa->pa_lstart; |
| 4056 | } |
| 4057 | spin_unlock(&pa->pa_lock); |
| 4058 | } |
| 4059 | rcu_read_unlock(); |
| 4060 | size = end - start; |
| 4061 | |
| 4062 | /* XXX: extra loop to check we really don't overlap preallocations */ |
| 4063 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 4064 | 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] | 4065 | ext4_lblk_t pa_end; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4066 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4067 | spin_lock(&pa->pa_lock); |
| 4068 | if (pa->pa_deleted == 0) { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4069 | pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb), |
| 4070 | pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4071 | BUG_ON(!(start >= pa_end || end <= pa->pa_lstart)); |
| 4072 | } |
| 4073 | spin_unlock(&pa->pa_lock); |
| 4074 | } |
| 4075 | rcu_read_unlock(); |
| 4076 | |
| 4077 | if (start + size <= ac->ac_o_ex.fe_logical && |
| 4078 | start > ac->ac_o_ex.fe_logical) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 4079 | ext4_msg(ac->ac_sb, KERN_ERR, |
| 4080 | "start %lu, size %lu, fe_logical %lu", |
| 4081 | (unsigned long) start, (unsigned long) size, |
| 4082 | (unsigned long) ac->ac_o_ex.fe_logical); |
Dmitry Monakhov | dfe076c | 2014-10-01 22:26:17 -0400 | [diff] [blame] | 4083 | BUG(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4084 | } |
Maurizio Lombardi | b5b6077 | 2014-05-27 12:48:56 -0400 | [diff] [blame] | 4085 | BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4086 | |
| 4087 | /* now prepare goal request */ |
| 4088 | |
| 4089 | /* XXX: is it better to align blocks WRT to logical |
| 4090 | * placement or satisfy big request as is */ |
| 4091 | ac->ac_g_ex.fe_logical = start; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4092 | ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4093 | |
| 4094 | /* define goal start in order to merge */ |
| 4095 | if (ar->pright && (ar->lright == (start + size))) { |
| 4096 | /* merge to the right */ |
| 4097 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size, |
| 4098 | &ac->ac_f_ex.fe_group, |
| 4099 | &ac->ac_f_ex.fe_start); |
| 4100 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; |
| 4101 | } |
| 4102 | if (ar->pleft && (ar->lleft + 1 == start)) { |
| 4103 | /* merge to the left */ |
| 4104 | ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1, |
| 4105 | &ac->ac_f_ex.fe_group, |
| 4106 | &ac->ac_f_ex.fe_start); |
| 4107 | ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; |
| 4108 | } |
| 4109 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4110 | mb_debug(ac->ac_sb, "goal: %lld(was %lld) blocks at %u\n", size, |
| 4111 | orig_size, start); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | static void ext4_mb_collect_stats(struct ext4_allocation_context *ac) |
| 4115 | { |
| 4116 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 4117 | |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 4118 | if (sbi->s_mb_stats && ac->ac_g_ex.fe_len >= 1) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4119 | atomic_inc(&sbi->s_bal_reqs); |
| 4120 | atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated); |
Curt Wohlgemuth | 291dae4 | 2010-05-16 16:00:00 -0400 | [diff] [blame] | 4121 | 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] | 4122 | atomic_inc(&sbi->s_bal_success); |
| 4123 | atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned); |
Harshad Shirwadkar | a6c75ea | 2021-04-01 10:21:25 -0700 | [diff] [blame] | 4124 | atomic_add(ac->ac_groups_scanned, &sbi->s_bal_groups_scanned); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4125 | if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start && |
| 4126 | ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group) |
| 4127 | atomic_inc(&sbi->s_bal_goals); |
| 4128 | if (ac->ac_found > sbi->s_mb_max_to_scan) |
| 4129 | atomic_inc(&sbi->s_bal_breaks); |
| 4130 | } |
| 4131 | |
Theodore Ts'o | 296c355 | 2009-09-30 00:32:42 -0400 | [diff] [blame] | 4132 | if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) |
| 4133 | trace_ext4_mballoc_alloc(ac); |
| 4134 | else |
| 4135 | trace_ext4_mballoc_prealloc(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | /* |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 4139 | * Called on failure; free up any blocks from the inode PA for this |
| 4140 | * context. We don't need this for MB_GROUP_PA because we only change |
| 4141 | * pa_free in ext4_mb_release_context(), but on failure, we've already |
| 4142 | * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed. |
| 4143 | */ |
| 4144 | static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac) |
| 4145 | { |
| 4146 | struct ext4_prealloc_space *pa = ac->ac_pa; |
Theodore Ts'o | 86f0afd | 2014-07-30 22:17:17 -0400 | [diff] [blame] | 4147 | struct ext4_buddy e4b; |
| 4148 | int err; |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 4149 | |
Theodore Ts'o | 86f0afd | 2014-07-30 22:17:17 -0400 | [diff] [blame] | 4150 | if (pa == NULL) { |
Theodore Ts'o | c99d1e6 | 2014-08-23 17:47:28 -0400 | [diff] [blame] | 4151 | if (ac->ac_f_ex.fe_len == 0) |
| 4152 | return; |
Theodore Ts'o | 86f0afd | 2014-07-30 22:17:17 -0400 | [diff] [blame] | 4153 | err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b); |
| 4154 | if (err) { |
| 4155 | /* |
| 4156 | * This should never happen since we pin the |
| 4157 | * pages in the ext4_allocation_context so |
| 4158 | * ext4_mb_load_buddy() should never fail. |
| 4159 | */ |
| 4160 | WARN(1, "mb_load_buddy failed (%d)", err); |
| 4161 | return; |
| 4162 | } |
| 4163 | ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group); |
| 4164 | mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start, |
| 4165 | ac->ac_f_ex.fe_len); |
| 4166 | 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] | 4167 | ext4_mb_unload_buddy(&e4b); |
Theodore Ts'o | 86f0afd | 2014-07-30 22:17:17 -0400 | [diff] [blame] | 4168 | return; |
| 4169 | } |
| 4170 | if (pa->pa_type == MB_INODE_PA) |
Zheng Liu | 400db9d | 2012-05-28 17:53:53 -0400 | [diff] [blame] | 4171 | pa->pa_free += ac->ac_b_ex.fe_len; |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4175 | * use blocks preallocated to inode |
| 4176 | */ |
| 4177 | static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac, |
| 4178 | struct ext4_prealloc_space *pa) |
| 4179 | { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4180 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4181 | ext4_fsblk_t start; |
| 4182 | ext4_fsblk_t end; |
| 4183 | int len; |
| 4184 | |
| 4185 | /* found preallocated blocks, use them */ |
| 4186 | 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] | 4187 | end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len), |
| 4188 | start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len)); |
| 4189 | len = EXT4_NUM_B2C(sbi, end - start); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4190 | ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group, |
| 4191 | &ac->ac_b_ex.fe_start); |
| 4192 | ac->ac_b_ex.fe_len = len; |
| 4193 | ac->ac_status = AC_STATUS_FOUND; |
| 4194 | ac->ac_pa = pa; |
| 4195 | |
| 4196 | BUG_ON(start < pa->pa_pstart); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4197 | BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4198 | BUG_ON(pa->pa_free < len); |
| 4199 | pa->pa_free -= len; |
| 4200 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4201 | 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] | 4202 | } |
| 4203 | |
| 4204 | /* |
| 4205 | * use blocks preallocated to locality group |
| 4206 | */ |
| 4207 | static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac, |
| 4208 | struct ext4_prealloc_space *pa) |
| 4209 | { |
Aneesh Kumar K.V | 03cddb8 | 2008-06-05 20:59:29 -0400 | [diff] [blame] | 4210 | unsigned int len = ac->ac_o_ex.fe_len; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4211 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4212 | ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart, |
| 4213 | &ac->ac_b_ex.fe_group, |
| 4214 | &ac->ac_b_ex.fe_start); |
| 4215 | ac->ac_b_ex.fe_len = len; |
| 4216 | ac->ac_status = AC_STATUS_FOUND; |
| 4217 | ac->ac_pa = pa; |
| 4218 | |
| 4219 | /* 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] | 4220 | * possible race when the group is being loaded concurrently |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4221 | * instead we correct pa later, after blocks are marked |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 4222 | * in on-disk bitmap -- see ext4_mb_release_context() |
| 4223 | * Other CPUs are prevented from allocating from this pa by lg_mutex |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4224 | */ |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4225 | mb_debug(ac->ac_sb, "use %u/%u from group pa %p\n", |
| 4226 | pa->pa_lstart-len, len, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4227 | } |
| 4228 | |
| 4229 | /* |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 4230 | * Return the prealloc space that have minimal distance |
| 4231 | * from the goal block. @cpa is the prealloc |
| 4232 | * space that is having currently known minimal distance |
| 4233 | * from the goal block. |
| 4234 | */ |
| 4235 | static struct ext4_prealloc_space * |
| 4236 | ext4_mb_check_group_pa(ext4_fsblk_t goal_block, |
| 4237 | struct ext4_prealloc_space *pa, |
| 4238 | struct ext4_prealloc_space *cpa) |
| 4239 | { |
| 4240 | ext4_fsblk_t cur_distance, new_distance; |
| 4241 | |
| 4242 | if (cpa == NULL) { |
| 4243 | atomic_inc(&pa->pa_count); |
| 4244 | return pa; |
| 4245 | } |
Andrew Morton | 79211c8 | 2015-11-09 14:58:13 -0800 | [diff] [blame] | 4246 | cur_distance = abs(goal_block - cpa->pa_pstart); |
| 4247 | new_distance = abs(goal_block - pa->pa_pstart); |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 4248 | |
Coly Li | 5a54b2f | 2011-02-24 14:10:05 -0500 | [diff] [blame] | 4249 | if (cur_distance <= new_distance) |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 4250 | return cpa; |
| 4251 | |
| 4252 | /* drop the previous reference */ |
| 4253 | atomic_dec(&cpa->pa_count); |
| 4254 | atomic_inc(&pa->pa_count); |
| 4255 | return pa; |
| 4256 | } |
| 4257 | |
| 4258 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4259 | * search goal blocks in preallocated space |
| 4260 | */ |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 4261 | static noinline_for_stack bool |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4262 | ext4_mb_use_preallocated(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4263 | { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4264 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4265 | int order, i; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4266 | struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); |
| 4267 | struct ext4_locality_group *lg; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 4268 | struct ext4_prealloc_space *pa, *cpa = NULL; |
| 4269 | ext4_fsblk_t goal_block; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4270 | |
| 4271 | /* only data can be preallocated */ |
| 4272 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 4273 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4274 | |
| 4275 | /* first, try per-file preallocation */ |
| 4276 | rcu_read_lock(); |
Aneesh Kumar K.V | 9a0762c | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 4277 | 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] | 4278 | |
| 4279 | /* all fields in this condition don't change, |
| 4280 | * so we can skip locking for them */ |
| 4281 | if (ac->ac_o_ex.fe_logical < pa->pa_lstart || |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4282 | ac->ac_o_ex.fe_logical >= (pa->pa_lstart + |
| 4283 | EXT4_C2B(sbi, pa->pa_len))) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4284 | continue; |
| 4285 | |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 4286 | /* non-extent files can't have physical blocks past 2^32 */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 4287 | if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) && |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4288 | (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) > |
| 4289 | EXT4_MAX_BLOCK_FILE_PHYS)) |
Eric Sandeen | fb0a387 | 2009-09-16 14:45:10 -0400 | [diff] [blame] | 4290 | continue; |
| 4291 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4292 | /* found preallocated blocks, use them */ |
| 4293 | spin_lock(&pa->pa_lock); |
| 4294 | if (pa->pa_deleted == 0 && pa->pa_free) { |
| 4295 | atomic_inc(&pa->pa_count); |
| 4296 | ext4_mb_use_inode_pa(ac, pa); |
| 4297 | spin_unlock(&pa->pa_lock); |
| 4298 | ac->ac_criteria = 10; |
| 4299 | rcu_read_unlock(); |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 4300 | return true; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4301 | } |
| 4302 | spin_unlock(&pa->pa_lock); |
| 4303 | } |
| 4304 | rcu_read_unlock(); |
| 4305 | |
| 4306 | /* can we use group allocation? */ |
| 4307 | if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)) |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 4308 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4309 | |
| 4310 | /* inode may have no locality group for some reason */ |
| 4311 | lg = ac->ac_lg; |
| 4312 | if (lg == NULL) |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 4313 | return false; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4314 | order = fls(ac->ac_o_ex.fe_len) - 1; |
| 4315 | if (order > PREALLOC_TB_SIZE - 1) |
| 4316 | /* The max size of hash table is PREALLOC_TB_SIZE */ |
| 4317 | order = PREALLOC_TB_SIZE - 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4318 | |
Akinobu Mita | bda00de | 2010-03-03 23:53:25 -0500 | [diff] [blame] | 4319 | 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] | 4320 | /* |
| 4321 | * search for the prealloc space that is having |
| 4322 | * minimal distance from the goal block. |
| 4323 | */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4324 | for (i = order; i < PREALLOC_TB_SIZE; i++) { |
| 4325 | rcu_read_lock(); |
| 4326 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i], |
| 4327 | pa_inode_list) { |
| 4328 | spin_lock(&pa->pa_lock); |
| 4329 | if (pa->pa_deleted == 0 && |
| 4330 | pa->pa_free >= ac->ac_o_ex.fe_len) { |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 4331 | |
| 4332 | cpa = ext4_mb_check_group_pa(goal_block, |
| 4333 | pa, cpa); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4334 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4335 | spin_unlock(&pa->pa_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4336 | } |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4337 | rcu_read_unlock(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4338 | } |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 4339 | if (cpa) { |
| 4340 | ext4_mb_use_group_pa(ac, cpa); |
| 4341 | ac->ac_criteria = 20; |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 4342 | return true; |
Aneesh Kumar K.V | 5e745b0 | 2008-08-18 18:00:57 -0400 | [diff] [blame] | 4343 | } |
Ritesh Harjani | 4fca8f0 | 2020-05-10 11:54:47 +0530 | [diff] [blame] | 4344 | return false; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | /* |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4348 | * the function goes through all block freed in the group |
| 4349 | * but not yet committed and marks them used in in-core bitmap. |
| 4350 | * buddy must be generated from this bitmap |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 4351 | * Need to be called with the ext4 group lock held |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 4352 | */ |
| 4353 | static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap, |
| 4354 | ext4_group_t group) |
| 4355 | { |
| 4356 | struct rb_node *n; |
| 4357 | struct ext4_group_info *grp; |
| 4358 | struct ext4_free_data *entry; |
| 4359 | |
| 4360 | grp = ext4_get_group_info(sb, group); |
| 4361 | n = rb_first(&(grp->bb_free_root)); |
| 4362 | |
| 4363 | while (n) { |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 4364 | entry = rb_entry(n, struct ext4_free_data, efd_node); |
| 4365 | 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] | 4366 | n = rb_next(n); |
| 4367 | } |
| 4368 | return; |
| 4369 | } |
| 4370 | |
| 4371 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4372 | * the function goes through all preallocation in this group and marks them |
| 4373 | * 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] | 4374 | * Need to be called with ext4 group lock held |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4375 | */ |
Eric Sandeen | 089ceec | 2009-07-05 22:17:31 -0400 | [diff] [blame] | 4376 | static noinline_for_stack |
| 4377 | void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4378 | ext4_group_t group) |
| 4379 | { |
| 4380 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 4381 | struct ext4_prealloc_space *pa; |
| 4382 | struct list_head *cur; |
| 4383 | ext4_group_t groupnr; |
| 4384 | ext4_grpblk_t start; |
| 4385 | int preallocated = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4386 | int len; |
| 4387 | |
| 4388 | /* all form of preallocation discards first load group, |
| 4389 | * so the only competing code is preallocation use. |
| 4390 | * we don't need any locking here |
| 4391 | * notice we do NOT ignore preallocations with pa_deleted |
| 4392 | * otherwise we could leave used blocks available for |
| 4393 | * allocation in buddy when concurrent ext4_mb_put_pa() |
| 4394 | * is dropping preallocation |
| 4395 | */ |
| 4396 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 4397 | pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); |
| 4398 | spin_lock(&pa->pa_lock); |
| 4399 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, |
| 4400 | &groupnr, &start); |
| 4401 | len = pa->pa_len; |
| 4402 | spin_unlock(&pa->pa_lock); |
| 4403 | if (unlikely(len == 0)) |
| 4404 | continue; |
| 4405 | BUG_ON(groupnr != group); |
Yongqiang Yang | c3e94d1 | 2011-07-26 22:05:53 -0400 | [diff] [blame] | 4406 | ext4_set_bits(bitmap, start, len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4407 | preallocated += len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4408 | } |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4409 | mb_debug(sb, "preallocated %d for group %u\n", preallocated, group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4410 | } |
| 4411 | |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4412 | static void ext4_mb_mark_pa_deleted(struct super_block *sb, |
| 4413 | struct ext4_prealloc_space *pa) |
| 4414 | { |
| 4415 | struct ext4_inode_info *ei; |
| 4416 | |
| 4417 | if (pa->pa_deleted) { |
| 4418 | ext4_warning(sb, "deleted pa, type:%d, pblk:%llu, lblk:%u, len:%d\n", |
| 4419 | pa->pa_type, pa->pa_pstart, pa->pa_lstart, |
| 4420 | pa->pa_len); |
| 4421 | return; |
| 4422 | } |
| 4423 | |
| 4424 | pa->pa_deleted = 1; |
| 4425 | |
| 4426 | if (pa->pa_type == MB_INODE_PA) { |
| 4427 | ei = EXT4_I(pa->pa_inode); |
| 4428 | atomic_dec(&ei->i_prealloc_active); |
| 4429 | } |
| 4430 | } |
| 4431 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4432 | static void ext4_mb_pa_callback(struct rcu_head *head) |
| 4433 | { |
| 4434 | struct ext4_prealloc_space *pa; |
| 4435 | pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu); |
Junho Ryu | 4e8d213 | 2013-12-03 18:10:28 -0500 | [diff] [blame] | 4436 | |
| 4437 | BUG_ON(atomic_read(&pa->pa_count)); |
| 4438 | BUG_ON(pa->pa_deleted == 0); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4439 | kmem_cache_free(ext4_pspace_cachep, pa); |
| 4440 | } |
| 4441 | |
| 4442 | /* |
| 4443 | * drops a reference to preallocated space descriptor |
| 4444 | * if this was the last reference and the space is consumed |
| 4445 | */ |
| 4446 | static void ext4_mb_put_pa(struct ext4_allocation_context *ac, |
| 4447 | struct super_block *sb, struct ext4_prealloc_space *pa) |
| 4448 | { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 4449 | ext4_group_t grp; |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 4450 | ext4_fsblk_t grp_blk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4451 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4452 | /* in this short window concurrent discard can set pa_deleted */ |
| 4453 | spin_lock(&pa->pa_lock); |
Junho Ryu | 4e8d213 | 2013-12-03 18:10:28 -0500 | [diff] [blame] | 4454 | if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) { |
| 4455 | spin_unlock(&pa->pa_lock); |
| 4456 | return; |
| 4457 | } |
| 4458 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4459 | if (pa->pa_deleted == 1) { |
| 4460 | spin_unlock(&pa->pa_lock); |
| 4461 | return; |
| 4462 | } |
| 4463 | |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4464 | ext4_mb_mark_pa_deleted(sb, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4465 | spin_unlock(&pa->pa_lock); |
| 4466 | |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 4467 | grp_blk = pa->pa_pstart; |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 4468 | /* |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4469 | * If doing group-based preallocation, pa_pstart may be in the |
| 4470 | * next group when pa is used up |
| 4471 | */ |
| 4472 | if (pa->pa_type == MB_GROUP_PA) |
Eric Sandeen | d33a197 | 2009-03-16 23:25:40 -0400 | [diff] [blame] | 4473 | grp_blk--; |
| 4474 | |
Lukas Czerner | bd86298 | 2013-04-03 23:32:34 -0400 | [diff] [blame] | 4475 | grp = ext4_get_group_number(sb, grp_blk); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4476 | |
| 4477 | /* |
| 4478 | * possible race: |
| 4479 | * |
| 4480 | * P1 (buddy init) P2 (regular allocation) |
| 4481 | * find block B in PA |
| 4482 | * copy on-disk bitmap to buddy |
| 4483 | * mark B in on-disk bitmap |
| 4484 | * drop PA from group |
| 4485 | * mark all PAs in buddy |
| 4486 | * |
| 4487 | * thus, P1 initializes buddy with B available. to prevent this |
| 4488 | * we make "copy" and "mark all PAs" atomic and serialize "drop PA" |
| 4489 | * against that pair |
| 4490 | */ |
| 4491 | ext4_lock_group(sb, grp); |
| 4492 | list_del(&pa->pa_group_list); |
| 4493 | ext4_unlock_group(sb, grp); |
| 4494 | |
| 4495 | spin_lock(pa->pa_obj_lock); |
| 4496 | list_del_rcu(&pa->pa_inode_list); |
| 4497 | spin_unlock(pa->pa_obj_lock); |
| 4498 | |
| 4499 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4500 | } |
| 4501 | |
| 4502 | /* |
| 4503 | * creates new preallocated space for given inode |
| 4504 | */ |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4505 | static noinline_for_stack void |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4506 | ext4_mb_new_inode_pa(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4507 | { |
| 4508 | struct super_block *sb = ac->ac_sb; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4509 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4510 | struct ext4_prealloc_space *pa; |
| 4511 | struct ext4_group_info *grp; |
| 4512 | struct ext4_inode_info *ei; |
| 4513 | |
| 4514 | /* preallocate only when found space is larger then requested */ |
| 4515 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); |
| 4516 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 4517 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4518 | BUG_ON(ac->ac_pa == NULL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4519 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4520 | pa = ac->ac_pa; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4521 | |
| 4522 | if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) { |
| 4523 | int winl; |
| 4524 | int wins; |
| 4525 | int win; |
| 4526 | int offs; |
| 4527 | |
| 4528 | /* we can't allocate as much as normalizer wants. |
| 4529 | * so, found space must get proper lstart |
| 4530 | * to cover original request */ |
| 4531 | BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical); |
| 4532 | BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len); |
| 4533 | |
| 4534 | /* we're limited by original request in that |
| 4535 | * logical block must be covered any way |
| 4536 | * winl is window we can move our chunk within */ |
| 4537 | winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical; |
| 4538 | |
| 4539 | /* also, we should cover whole original request */ |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4540 | 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] | 4541 | |
| 4542 | /* the smallest one defines real window */ |
| 4543 | win = min(winl, wins); |
| 4544 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4545 | offs = ac->ac_o_ex.fe_logical % |
| 4546 | EXT4_C2B(sbi, ac->ac_b_ex.fe_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4547 | if (offs && offs < win) |
| 4548 | win = offs; |
| 4549 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4550 | ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - |
Lukas Czerner | 810da24 | 2013-03-02 17:18:58 -0500 | [diff] [blame] | 4551 | EXT4_NUM_B2C(sbi, win); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4552 | BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical); |
| 4553 | BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len); |
| 4554 | } |
| 4555 | |
| 4556 | /* preallocation can change ac_b_ex, thus we store actually |
| 4557 | * allocated blocks for history */ |
| 4558 | ac->ac_f_ex = ac->ac_b_ex; |
| 4559 | |
| 4560 | pa->pa_lstart = ac->ac_b_ex.fe_logical; |
| 4561 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 4562 | pa->pa_len = ac->ac_b_ex.fe_len; |
| 4563 | pa->pa_free = pa->pa_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4564 | spin_lock_init(&pa->pa_lock); |
Aneesh Kumar K.V | d794bf8 | 2009-02-14 10:31:16 -0500 | [diff] [blame] | 4565 | INIT_LIST_HEAD(&pa->pa_inode_list); |
| 4566 | INIT_LIST_HEAD(&pa->pa_group_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4567 | pa->pa_deleted = 0; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4568 | pa->pa_type = MB_INODE_PA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4569 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4570 | mb_debug(sb, "new inode pa %p: %llu/%d for %u\n", pa, pa->pa_pstart, |
| 4571 | pa->pa_len, pa->pa_lstart); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4572 | trace_ext4_mb_new_inode_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4573 | |
| 4574 | ext4_mb_use_inode_pa(ac, pa); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4575 | atomic_add(pa->pa_free, &sbi->s_mb_preallocated); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4576 | |
| 4577 | ei = EXT4_I(ac->ac_inode); |
| 4578 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); |
| 4579 | |
| 4580 | pa->pa_obj_lock = &ei->i_prealloc_lock; |
| 4581 | pa->pa_inode = ac->ac_inode; |
| 4582 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4583 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4584 | |
| 4585 | spin_lock(pa->pa_obj_lock); |
| 4586 | list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list); |
| 4587 | spin_unlock(pa->pa_obj_lock); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4588 | atomic_inc(&ei->i_prealloc_active); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | /* |
| 4592 | * creates new preallocated space for locality group inodes belongs to |
| 4593 | */ |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4594 | static noinline_for_stack void |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4595 | ext4_mb_new_group_pa(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4596 | { |
| 4597 | struct super_block *sb = ac->ac_sb; |
| 4598 | struct ext4_locality_group *lg; |
| 4599 | struct ext4_prealloc_space *pa; |
| 4600 | struct ext4_group_info *grp; |
| 4601 | |
| 4602 | /* preallocate only when found space is larger then requested */ |
| 4603 | BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); |
| 4604 | BUG_ON(ac->ac_status != AC_STATUS_FOUND); |
| 4605 | BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4606 | BUG_ON(ac->ac_pa == NULL); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4607 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4608 | pa = ac->ac_pa; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4609 | |
| 4610 | /* preallocation can change ac_b_ex, thus we store actually |
| 4611 | * allocated blocks for history */ |
| 4612 | ac->ac_f_ex = ac->ac_b_ex; |
| 4613 | |
| 4614 | pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 4615 | pa->pa_lstart = pa->pa_pstart; |
| 4616 | pa->pa_len = ac->ac_b_ex.fe_len; |
| 4617 | pa->pa_free = pa->pa_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4618 | spin_lock_init(&pa->pa_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4619 | INIT_LIST_HEAD(&pa->pa_inode_list); |
Aneesh Kumar K.V | d794bf8 | 2009-02-14 10:31:16 -0500 | [diff] [blame] | 4620 | INIT_LIST_HEAD(&pa->pa_group_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4621 | pa->pa_deleted = 0; |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4622 | pa->pa_type = MB_GROUP_PA; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4623 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4624 | mb_debug(sb, "new group pa %p: %llu/%d for %u\n", pa, pa->pa_pstart, |
| 4625 | pa->pa_len, pa->pa_lstart); |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 4626 | trace_ext4_mb_new_group_pa(ac, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4627 | |
| 4628 | ext4_mb_use_group_pa(ac, pa); |
| 4629 | atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated); |
| 4630 | |
| 4631 | grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); |
| 4632 | lg = ac->ac_lg; |
| 4633 | BUG_ON(lg == NULL); |
| 4634 | |
| 4635 | pa->pa_obj_lock = &lg->lg_prealloc_lock; |
| 4636 | pa->pa_inode = NULL; |
| 4637 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4638 | list_add(&pa->pa_group_list, &grp->bb_prealloc_list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4639 | |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 4640 | /* |
| 4641 | * We will later add the new pa to the right bucket |
| 4642 | * after updating the pa_free in ext4_mb_release_context |
| 4643 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4644 | } |
| 4645 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4646 | static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4647 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4648 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4649 | ext4_mb_new_group_pa(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4650 | else |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4651 | ext4_mb_new_inode_pa(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | /* |
| 4655 | * finds all unused blocks in on-disk bitmap, frees them in |
| 4656 | * in-core bitmap and buddy. |
| 4657 | * @pa must be unlinked from inode and group lists, so that |
| 4658 | * nobody else can find/use it. |
| 4659 | * the caller MUST hold group/inode locks. |
| 4660 | * TODO: optimize the case when there are no in-core structures yet |
| 4661 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4662 | static noinline_for_stack int |
| 4663 | 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] | 4664 | struct ext4_prealloc_space *pa) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4665 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4666 | struct super_block *sb = e4b->bd_sb; |
| 4667 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4668 | unsigned int end; |
| 4669 | unsigned int next; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4670 | ext4_group_t group; |
| 4671 | ext4_grpblk_t bit; |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 4672 | unsigned long long grp_blk_start; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4673 | int free = 0; |
| 4674 | |
| 4675 | BUG_ON(pa->pa_deleted == 0); |
| 4676 | 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] | 4677 | grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4678 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
| 4679 | end = bit + pa->pa_len; |
| 4680 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4681 | while (bit < end) { |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 4682 | bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4683 | if (bit >= end) |
| 4684 | break; |
Aneesh Kumar K.V | ffad0a4 | 2008-02-23 01:38:34 -0500 | [diff] [blame] | 4685 | next = mb_find_next_bit(bitmap_bh->b_data, end, bit); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4686 | mb_debug(sb, "free preallocated %u/%u in group %u\n", |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 4687 | (unsigned) ext4_group_first_block_no(sb, group) + bit, |
| 4688 | (unsigned) next - bit, (unsigned) group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4689 | free += next - bit; |
| 4690 | |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4691 | trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 4692 | trace_ext4_mb_release_inode_pa(pa, (grp_blk_start + |
| 4693 | EXT4_C2B(sbi, bit)), |
Lukas Czerner | a9c667f | 2011-06-06 09:51:52 -0400 | [diff] [blame] | 4694 | next - bit); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4695 | mb_free_blocks(pa->pa_inode, e4b, bit, next - bit); |
| 4696 | bit = next + 1; |
| 4697 | } |
| 4698 | if (free != pa->pa_free) { |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 4699 | ext4_msg(e4b->bd_sb, KERN_CRIT, |
Ritesh Harjani | 36bad42 | 2020-05-10 11:54:44 +0530 | [diff] [blame] | 4700 | "pa %p: logic %lu, phys. %lu, len %d", |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 4701 | pa, (unsigned long) pa->pa_lstart, |
| 4702 | (unsigned long) pa->pa_pstart, |
Ritesh Harjani | 36bad42 | 2020-05-10 11:54:44 +0530 | [diff] [blame] | 4703 | pa->pa_len); |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 4704 | 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] | 4705 | free, pa->pa_free); |
Aneesh Kumar K.V | e56eb65 | 2008-02-15 13:48:21 -0500 | [diff] [blame] | 4706 | /* |
| 4707 | * pa is already deleted so we use the value obtained |
| 4708 | * from the bitmap and continue. |
| 4709 | */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4710 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4711 | atomic_add(free, &sbi->s_mb_discarded); |
| 4712 | |
zhong jiang | 863c37f | 2018-08-04 17:34:07 -0400 | [diff] [blame] | 4713 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4714 | } |
| 4715 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4716 | static noinline_for_stack int |
| 4717 | ext4_mb_release_group_pa(struct ext4_buddy *e4b, |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4718 | struct ext4_prealloc_space *pa) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4719 | { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4720 | struct super_block *sb = e4b->bd_sb; |
| 4721 | ext4_group_t group; |
| 4722 | ext4_grpblk_t bit; |
| 4723 | |
Yongqiang Yang | 60e07cf | 2011-12-18 15:49:54 -0500 | [diff] [blame] | 4724 | trace_ext4_mb_release_group_pa(sb, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4725 | BUG_ON(pa->pa_deleted == 0); |
| 4726 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); |
| 4727 | BUG_ON(group != e4b->bd_group && pa->pa_len != 0); |
| 4728 | mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len); |
| 4729 | atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded); |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4730 | trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4731 | |
| 4732 | return 0; |
| 4733 | } |
| 4734 | |
| 4735 | /* |
| 4736 | * releases all preallocations in given group |
| 4737 | * |
| 4738 | * first, we need to decide discard policy: |
| 4739 | * - when do we discard |
| 4740 | * 1) ENOSPC |
| 4741 | * - how many do we discard |
| 4742 | * 1) how many requested |
| 4743 | */ |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4744 | static noinline_for_stack int |
| 4745 | ext4_mb_discard_group_preallocations(struct super_block *sb, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4746 | ext4_group_t group, int needed) |
| 4747 | { |
| 4748 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
| 4749 | struct buffer_head *bitmap_bh = NULL; |
| 4750 | struct ext4_prealloc_space *pa, *tmp; |
| 4751 | struct list_head list; |
| 4752 | struct ext4_buddy e4b; |
| 4753 | int err; |
| 4754 | int busy = 0; |
Jan Kara | 5b3dc19 | 2020-09-24 17:09:59 +0200 | [diff] [blame] | 4755 | int free, free_total = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4756 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4757 | mb_debug(sb, "discard preallocation for group %u\n", group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4758 | if (list_empty(&grp->bb_prealloc_list)) |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4759 | goto out_dbg; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4760 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4761 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 4762 | if (IS_ERR(bitmap_bh)) { |
| 4763 | err = PTR_ERR(bitmap_bh); |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 4764 | ext4_error_err(sb, -err, |
| 4765 | "Error %d reading block bitmap for %u", |
| 4766 | err, group); |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4767 | goto out_dbg; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | err = ext4_mb_load_buddy(sb, group, &e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4771 | if (err) { |
Konstantin Khlebnikov | 9651e6b | 2017-05-21 22:35:23 -0400 | [diff] [blame] | 4772 | ext4_warning(sb, "Error %d loading buddy information for %u", |
| 4773 | err, group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4774 | put_bh(bitmap_bh); |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4775 | goto out_dbg; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4776 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4777 | |
| 4778 | if (needed == 0) |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 4779 | needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4780 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4781 | INIT_LIST_HEAD(&list); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4782 | repeat: |
Jan Kara | 5b3dc19 | 2020-09-24 17:09:59 +0200 | [diff] [blame] | 4783 | free = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4784 | ext4_lock_group(sb, group); |
| 4785 | list_for_each_entry_safe(pa, tmp, |
| 4786 | &grp->bb_prealloc_list, pa_group_list) { |
| 4787 | spin_lock(&pa->pa_lock); |
| 4788 | if (atomic_read(&pa->pa_count)) { |
| 4789 | spin_unlock(&pa->pa_lock); |
| 4790 | busy = 1; |
| 4791 | continue; |
| 4792 | } |
| 4793 | if (pa->pa_deleted) { |
| 4794 | spin_unlock(&pa->pa_lock); |
| 4795 | continue; |
| 4796 | } |
| 4797 | |
| 4798 | /* seems this one can be freed ... */ |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4799 | ext4_mb_mark_pa_deleted(sb, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4800 | |
Ye Bin | 70022da | 2020-09-16 19:38:59 +0800 | [diff] [blame] | 4801 | if (!free) |
| 4802 | this_cpu_inc(discard_pa_seq); |
| 4803 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4804 | /* we can trust pa_free ... */ |
| 4805 | free += pa->pa_free; |
| 4806 | |
| 4807 | spin_unlock(&pa->pa_lock); |
| 4808 | |
| 4809 | list_del(&pa->pa_group_list); |
| 4810 | list_add(&pa->u.pa_tmp_list, &list); |
| 4811 | } |
| 4812 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4813 | /* now free all selected PAs */ |
| 4814 | list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { |
| 4815 | |
| 4816 | /* remove from object (inode or locality group) */ |
| 4817 | spin_lock(pa->pa_obj_lock); |
| 4818 | list_del_rcu(&pa->pa_inode_list); |
| 4819 | spin_unlock(pa->pa_obj_lock); |
| 4820 | |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 4821 | if (pa->pa_type == MB_GROUP_PA) |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4822 | ext4_mb_release_group_pa(&e4b, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4823 | else |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4824 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4825 | |
| 4826 | list_del(&pa->u.pa_tmp_list); |
| 4827 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4828 | } |
| 4829 | |
Jan Kara | 5b3dc19 | 2020-09-24 17:09:59 +0200 | [diff] [blame] | 4830 | free_total += free; |
| 4831 | |
| 4832 | /* if we still need more blocks and some PAs were used, try again */ |
| 4833 | if (free_total < needed && busy) { |
| 4834 | ext4_unlock_group(sb, group); |
| 4835 | cond_resched(); |
| 4836 | busy = 0; |
| 4837 | goto repeat; |
| 4838 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4839 | ext4_unlock_group(sb, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 4840 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4841 | put_bh(bitmap_bh); |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 4842 | out_dbg: |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4843 | mb_debug(sb, "discarded (%d) blocks preallocated for group %u bb_free (%d)\n", |
Jan Kara | 5b3dc19 | 2020-09-24 17:09:59 +0200 | [diff] [blame] | 4844 | free_total, group, grp->bb_free); |
| 4845 | return free_total; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | /* |
| 4849 | * releases all non-used preallocated blocks for given inode |
| 4850 | * |
| 4851 | * It's important to discard preallocations under i_data_sem |
| 4852 | * We don't want another block to be served from the prealloc |
| 4853 | * space when we are discarding the inode prealloc space. |
| 4854 | * |
| 4855 | * FIXME!! Make sure it is valid at all the call sites |
| 4856 | */ |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4857 | void ext4_discard_preallocations(struct inode *inode, unsigned int needed) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4858 | { |
| 4859 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 4860 | struct super_block *sb = inode->i_sb; |
| 4861 | struct buffer_head *bitmap_bh = NULL; |
| 4862 | struct ext4_prealloc_space *pa, *tmp; |
| 4863 | ext4_group_t group = 0; |
| 4864 | struct list_head list; |
| 4865 | struct ext4_buddy e4b; |
| 4866 | int err; |
| 4867 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4868 | if (!S_ISREG(inode->i_mode)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4869 | /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/ |
| 4870 | return; |
| 4871 | } |
| 4872 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 4873 | if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) |
| 4874 | return; |
| 4875 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 4876 | mb_debug(sb, "discard preallocation for inode %lu\n", |
| 4877 | inode->i_ino); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4878 | trace_ext4_discard_preallocations(inode, |
| 4879 | atomic_read(&ei->i_prealloc_active), needed); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4880 | |
| 4881 | INIT_LIST_HEAD(&list); |
| 4882 | |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4883 | if (needed == 0) |
| 4884 | needed = UINT_MAX; |
| 4885 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4886 | repeat: |
| 4887 | /* first, collect all pa's in the inode */ |
| 4888 | spin_lock(&ei->i_prealloc_lock); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4889 | while (!list_empty(&ei->i_prealloc_list) && needed) { |
| 4890 | pa = list_entry(ei->i_prealloc_list.prev, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4891 | struct ext4_prealloc_space, pa_inode_list); |
| 4892 | BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock); |
| 4893 | spin_lock(&pa->pa_lock); |
| 4894 | if (atomic_read(&pa->pa_count)) { |
| 4895 | /* this shouldn't happen often - nobody should |
| 4896 | * use preallocation while we're discarding it */ |
| 4897 | spin_unlock(&pa->pa_lock); |
| 4898 | spin_unlock(&ei->i_prealloc_lock); |
Theodore Ts'o | 9d8b9ec | 2011-08-01 17:41:35 -0400 | [diff] [blame] | 4899 | ext4_msg(sb, KERN_ERR, |
| 4900 | "uh-oh! used pa while discarding"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4901 | WARN_ON(1); |
| 4902 | schedule_timeout_uninterruptible(HZ); |
| 4903 | goto repeat; |
| 4904 | |
| 4905 | } |
| 4906 | if (pa->pa_deleted == 0) { |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4907 | ext4_mb_mark_pa_deleted(sb, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4908 | spin_unlock(&pa->pa_lock); |
| 4909 | list_del_rcu(&pa->pa_inode_list); |
| 4910 | list_add(&pa->u.pa_tmp_list, &list); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 4911 | needed--; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4912 | continue; |
| 4913 | } |
| 4914 | |
| 4915 | /* someone is deleting pa right now */ |
| 4916 | spin_unlock(&pa->pa_lock); |
| 4917 | spin_unlock(&ei->i_prealloc_lock); |
| 4918 | |
| 4919 | /* we have to wait here because pa_deleted |
| 4920 | * doesn't mean pa is already unlinked from |
| 4921 | * the list. as we might be called from |
| 4922 | * ->clear_inode() the inode will get freed |
| 4923 | * and concurrent thread which is unlinking |
| 4924 | * pa from inode's list may access already |
| 4925 | * freed memory, bad-bad-bad */ |
| 4926 | |
| 4927 | /* XXX: if this happens too often, we can |
| 4928 | * add a flag to force wait only in case |
| 4929 | * of ->clear_inode(), but not in case of |
| 4930 | * regular truncate */ |
| 4931 | schedule_timeout_uninterruptible(HZ); |
| 4932 | goto repeat; |
| 4933 | } |
| 4934 | spin_unlock(&ei->i_prealloc_lock); |
| 4935 | |
| 4936 | 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] | 4937 | BUG_ON(pa->pa_type != MB_INODE_PA); |
Lukas Czerner | bd86298 | 2013-04-03 23:32:34 -0400 | [diff] [blame] | 4938 | group = ext4_get_group_number(sb, pa->pa_pstart); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4939 | |
Konstantin Khlebnikov | 9651e6b | 2017-05-21 22:35:23 -0400 | [diff] [blame] | 4940 | err = ext4_mb_load_buddy_gfp(sb, group, &e4b, |
| 4941 | GFP_NOFS|__GFP_NOFAIL); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4942 | if (err) { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 4943 | ext4_error_err(sb, -err, "Error %d loading buddy information for %u", |
| 4944 | err, group); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4945 | continue; |
| 4946 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4947 | |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4948 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 4949 | if (IS_ERR(bitmap_bh)) { |
| 4950 | err = PTR_ERR(bitmap_bh); |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 4951 | ext4_error_err(sb, -err, "Error %d reading block bitmap for %u", |
| 4952 | err, group); |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 4953 | ext4_mb_unload_buddy(&e4b); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 4954 | continue; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4955 | } |
| 4956 | |
| 4957 | ext4_lock_group(sb, group); |
| 4958 | list_del(&pa->pa_group_list); |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 4959 | ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4960 | ext4_unlock_group(sb, group); |
| 4961 | |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 4962 | ext4_mb_unload_buddy(&e4b); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4963 | put_bh(bitmap_bh); |
| 4964 | |
| 4965 | list_del(&pa->u.pa_tmp_list); |
| 4966 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 4967 | } |
| 4968 | } |
| 4969 | |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 4970 | static int ext4_mb_pa_alloc(struct ext4_allocation_context *ac) |
| 4971 | { |
| 4972 | struct ext4_prealloc_space *pa; |
| 4973 | |
| 4974 | BUG_ON(ext4_pspace_cachep == NULL); |
| 4975 | pa = kmem_cache_zalloc(ext4_pspace_cachep, GFP_NOFS); |
| 4976 | if (!pa) |
| 4977 | return -ENOMEM; |
| 4978 | atomic_set(&pa->pa_count, 1); |
| 4979 | ac->ac_pa = pa; |
| 4980 | return 0; |
| 4981 | } |
| 4982 | |
| 4983 | static void ext4_mb_pa_free(struct ext4_allocation_context *ac) |
| 4984 | { |
| 4985 | struct ext4_prealloc_space *pa = ac->ac_pa; |
| 4986 | |
| 4987 | BUG_ON(!pa); |
| 4988 | ac->ac_pa = NULL; |
| 4989 | WARN_ON(!atomic_dec_and_test(&pa->pa_count)); |
| 4990 | kmem_cache_free(ext4_pspace_cachep, pa); |
| 4991 | } |
| 4992 | |
Theodore Ts'o | 6ba495e | 2009-09-18 13:38:55 -0400 | [diff] [blame] | 4993 | #ifdef CONFIG_EXT4_DEBUG |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4994 | static inline void ext4_mb_show_pa(struct super_block *sb) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4995 | { |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 4996 | ext4_group_t i, ngroups; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4997 | |
Harshad Shirwadkar | 9b5f6c9 | 2020-11-05 19:59:09 -0800 | [diff] [blame] | 4998 | if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) |
Eric Sandeen | e357063 | 2010-07-27 11:56:08 -0400 | [diff] [blame] | 4999 | return; |
| 5000 | |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 5001 | ngroups = ext4_get_groups_count(sb); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5002 | mb_debug(sb, "groups: "); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 5003 | for (i = 0; i < ngroups; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5004 | struct ext4_group_info *grp = ext4_get_group_info(sb, i); |
| 5005 | struct ext4_prealloc_space *pa; |
| 5006 | ext4_grpblk_t start; |
| 5007 | struct list_head *cur; |
| 5008 | ext4_lock_group(sb, i); |
| 5009 | list_for_each(cur, &grp->bb_prealloc_list) { |
| 5010 | pa = list_entry(cur, struct ext4_prealloc_space, |
| 5011 | pa_group_list); |
| 5012 | spin_lock(&pa->pa_lock); |
| 5013 | ext4_get_group_no_and_offset(sb, pa->pa_pstart, |
| 5014 | NULL, &start); |
| 5015 | spin_unlock(&pa->pa_lock); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5016 | mb_debug(sb, "PA:%u:%d:%d\n", i, start, |
| 5017 | pa->pa_len); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5018 | } |
Solofo Ramangalahy | 60bd63d | 2008-04-29 21:59:59 -0400 | [diff] [blame] | 5019 | ext4_unlock_group(sb, i); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5020 | mb_debug(sb, "%u: %d/%d\n", i, grp->bb_free, |
| 5021 | grp->bb_fragments); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5022 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5023 | } |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 5024 | |
| 5025 | static void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
| 5026 | { |
| 5027 | struct super_block *sb = ac->ac_sb; |
| 5028 | |
Harshad Shirwadkar | 9b5f6c9 | 2020-11-05 19:59:09 -0800 | [diff] [blame] | 5029 | if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 5030 | return; |
| 5031 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5032 | mb_debug(sb, "Can't allocate:" |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 5033 | " Allocation context details:"); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5034 | mb_debug(sb, "status %u flags 0x%x", |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 5035 | ac->ac_status, ac->ac_flags); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5036 | mb_debug(sb, "orig %lu/%lu/%lu@%lu, " |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 5037 | "goal %lu/%lu/%lu@%lu, " |
| 5038 | "best %lu/%lu/%lu@%lu cr %d", |
| 5039 | (unsigned long)ac->ac_o_ex.fe_group, |
| 5040 | (unsigned long)ac->ac_o_ex.fe_start, |
| 5041 | (unsigned long)ac->ac_o_ex.fe_len, |
| 5042 | (unsigned long)ac->ac_o_ex.fe_logical, |
| 5043 | (unsigned long)ac->ac_g_ex.fe_group, |
| 5044 | (unsigned long)ac->ac_g_ex.fe_start, |
| 5045 | (unsigned long)ac->ac_g_ex.fe_len, |
| 5046 | (unsigned long)ac->ac_g_ex.fe_logical, |
| 5047 | (unsigned long)ac->ac_b_ex.fe_group, |
| 5048 | (unsigned long)ac->ac_b_ex.fe_start, |
| 5049 | (unsigned long)ac->ac_b_ex.fe_len, |
| 5050 | (unsigned long)ac->ac_b_ex.fe_logical, |
| 5051 | (int)ac->ac_criteria); |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5052 | mb_debug(sb, "%u found", ac->ac_found); |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 5053 | ext4_mb_show_pa(sb); |
| 5054 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5055 | #else |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 5056 | static inline void ext4_mb_show_pa(struct super_block *sb) |
| 5057 | { |
| 5058 | return; |
| 5059 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5060 | static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac) |
| 5061 | { |
Ritesh Harjani | e68cf40 | 2020-05-10 11:54:42 +0530 | [diff] [blame] | 5062 | ext4_mb_show_pa(ac->ac_sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5063 | return; |
| 5064 | } |
| 5065 | #endif |
| 5066 | |
| 5067 | /* |
| 5068 | * We use locality group preallocation for small size file. The size of the |
| 5069 | * file is determined by the current size or the resulting size after |
| 5070 | * allocation which ever is larger |
| 5071 | * |
Theodore Ts'o | b713a5e | 2009-03-31 09:11:14 -0400 | [diff] [blame] | 5072 | * 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] | 5073 | */ |
| 5074 | static void ext4_mb_group_or_file(struct ext4_allocation_context *ac) |
| 5075 | { |
| 5076 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
| 5077 | int bsbits = ac->ac_sb->s_blocksize_bits; |
| 5078 | loff_t size, isize; |
| 5079 | |
| 5080 | if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) |
| 5081 | return; |
| 5082 | |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 5083 | if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) |
| 5084 | return; |
| 5085 | |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5086 | 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] | 5087 | isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1) |
| 5088 | >> bsbits; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5089 | |
Nikolay Borisov | 82dd124 | 2019-02-10 23:04:16 -0500 | [diff] [blame] | 5090 | if ((size == isize) && !ext4_fs_is_busy(sbi) && |
| 5091 | !inode_is_open_for_write(ac->ac_inode)) { |
Theodore Ts'o | 5079748 | 2009-09-18 13:34:02 -0400 | [diff] [blame] | 5092 | ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC; |
| 5093 | return; |
| 5094 | } |
| 5095 | |
Robin Dong | ebbe027 | 2011-10-26 05:14:27 -0400 | [diff] [blame] | 5096 | if (sbi->s_mb_group_prealloc <= 0) { |
| 5097 | ac->ac_flags |= EXT4_MB_STREAM_ALLOC; |
| 5098 | return; |
| 5099 | } |
| 5100 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5101 | /* don't use group allocation for large files */ |
Theodore Ts'o | 7178057 | 2009-09-28 00:06:20 -0400 | [diff] [blame] | 5102 | size = max(size, isize); |
Tao Ma | cc483f1 | 2010-03-01 19:06:35 -0500 | [diff] [blame] | 5103 | if (size > sbi->s_mb_stream_request) { |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 5104 | ac->ac_flags |= EXT4_MB_STREAM_ALLOC; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5105 | return; |
Theodore Ts'o | 4ba74d0 | 2009-08-09 22:01:13 -0400 | [diff] [blame] | 5106 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5107 | |
| 5108 | BUG_ON(ac->ac_lg != NULL); |
| 5109 | /* |
| 5110 | * locality group prealloc space are per cpu. The reason for having |
| 5111 | * per cpu locality group is to reduce the contention between block |
| 5112 | * request from multiple CPUs. |
| 5113 | */ |
Christoph Lameter | a0b6bc6 | 2014-08-17 12:30:28 -0500 | [diff] [blame] | 5114 | ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5115 | |
| 5116 | /* we're going to use group allocation */ |
| 5117 | ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC; |
| 5118 | |
| 5119 | /* serialize all allocations in the group */ |
| 5120 | mutex_lock(&ac->ac_lg->lg_mutex); |
| 5121 | } |
| 5122 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 5123 | static noinline_for_stack int |
| 5124 | ext4_mb_initialize_context(struct ext4_allocation_context *ac, |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5125 | struct ext4_allocation_request *ar) |
| 5126 | { |
| 5127 | struct super_block *sb = ar->inode->i_sb; |
| 5128 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 5129 | struct ext4_super_block *es = sbi->s_es; |
| 5130 | ext4_group_t group; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 5131 | unsigned int len; |
| 5132 | ext4_fsblk_t goal; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5133 | ext4_grpblk_t block; |
| 5134 | |
| 5135 | /* we can't allocate > group size */ |
| 5136 | len = ar->len; |
| 5137 | |
| 5138 | /* just a dirty hack to filter too big requests */ |
Theodore Ts'o | 40ae348 | 2013-02-04 15:08:40 -0500 | [diff] [blame] | 5139 | if (len >= EXT4_CLUSTERS_PER_GROUP(sb)) |
| 5140 | len = EXT4_CLUSTERS_PER_GROUP(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5141 | |
| 5142 | /* start searching from the goal */ |
| 5143 | goal = ar->goal; |
| 5144 | if (goal < le32_to_cpu(es->s_first_data_block) || |
| 5145 | goal >= ext4_blocks_count(es)) |
| 5146 | goal = le32_to_cpu(es->s_first_data_block); |
| 5147 | ext4_get_group_no_and_offset(sb, goal, &group, &block); |
| 5148 | |
| 5149 | /* set up allocation goals */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 5150 | ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5151 | ac->ac_status = AC_STATUS_CONTINUE; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5152 | ac->ac_sb = sb; |
| 5153 | ac->ac_inode = ar->inode; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5154 | ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5155 | ac->ac_o_ex.fe_group = group; |
| 5156 | ac->ac_o_ex.fe_start = block; |
| 5157 | ac->ac_o_ex.fe_len = len; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5158 | ac->ac_g_ex = ac->ac_o_ex; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5159 | ac->ac_flags = ar->flags; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5160 | |
brookxu | 3cb77bd | 2020-07-15 11:00:44 +0800 | [diff] [blame] | 5161 | /* we have to define context: we'll work with a file or |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5162 | * locality group. this is a policy, actually */ |
| 5163 | ext4_mb_group_or_file(ac); |
| 5164 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5165 | 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] | 5166 | "left: %u/%u, right %u/%u to %swritable\n", |
| 5167 | (unsigned) ar->len, (unsigned) ar->logical, |
| 5168 | (unsigned) ar->goal, ac->ac_flags, ac->ac_2order, |
| 5169 | (unsigned) ar->lleft, (unsigned) ar->pleft, |
| 5170 | (unsigned) ar->lright, (unsigned) ar->pright, |
Nikolay Borisov | 82dd124 | 2019-02-10 23:04:16 -0500 | [diff] [blame] | 5171 | inode_is_open_for_write(ar->inode) ? "" : "non-"); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5172 | return 0; |
| 5173 | |
| 5174 | } |
| 5175 | |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5176 | static noinline_for_stack void |
| 5177 | ext4_mb_discard_lg_preallocations(struct super_block *sb, |
| 5178 | struct ext4_locality_group *lg, |
| 5179 | int order, int total_entries) |
| 5180 | { |
| 5181 | ext4_group_t group = 0; |
| 5182 | struct ext4_buddy e4b; |
| 5183 | struct list_head discard_list; |
| 5184 | struct ext4_prealloc_space *pa, *tmp; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5185 | |
Ritesh Harjani | d3df145 | 2020-05-10 11:54:54 +0530 | [diff] [blame] | 5186 | mb_debug(sb, "discard locality group preallocation\n"); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5187 | |
| 5188 | INIT_LIST_HEAD(&discard_list); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5189 | |
| 5190 | spin_lock(&lg->lg_prealloc_lock); |
| 5191 | list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order], |
Madhuparna Bhowmik | 92e9c58 | 2020-02-13 20:55:58 +0530 | [diff] [blame] | 5192 | pa_inode_list, |
| 5193 | lockdep_is_held(&lg->lg_prealloc_lock)) { |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5194 | spin_lock(&pa->pa_lock); |
| 5195 | if (atomic_read(&pa->pa_count)) { |
| 5196 | /* |
| 5197 | * This is the pa that we just used |
| 5198 | * for block allocation. So don't |
| 5199 | * free that |
| 5200 | */ |
| 5201 | spin_unlock(&pa->pa_lock); |
| 5202 | continue; |
| 5203 | } |
| 5204 | if (pa->pa_deleted) { |
| 5205 | spin_unlock(&pa->pa_lock); |
| 5206 | continue; |
| 5207 | } |
| 5208 | /* only lg prealloc space */ |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 5209 | BUG_ON(pa->pa_type != MB_GROUP_PA); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5210 | |
| 5211 | /* seems this one can be freed ... */ |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 5212 | ext4_mb_mark_pa_deleted(sb, pa); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5213 | spin_unlock(&pa->pa_lock); |
| 5214 | |
| 5215 | list_del_rcu(&pa->pa_inode_list); |
| 5216 | list_add(&pa->u.pa_tmp_list, &discard_list); |
| 5217 | |
| 5218 | total_entries--; |
| 5219 | if (total_entries <= 5) { |
| 5220 | /* |
| 5221 | * we want to keep only 5 entries |
| 5222 | * allowing it to grow to 8. This |
| 5223 | * mak sure we don't call discard |
| 5224 | * soon for this list. |
| 5225 | */ |
| 5226 | break; |
| 5227 | } |
| 5228 | } |
| 5229 | spin_unlock(&lg->lg_prealloc_lock); |
| 5230 | |
| 5231 | 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] | 5232 | int err; |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5233 | |
Lukas Czerner | bd86298 | 2013-04-03 23:32:34 -0400 | [diff] [blame] | 5234 | group = ext4_get_group_number(sb, pa->pa_pstart); |
Konstantin Khlebnikov | 9651e6b | 2017-05-21 22:35:23 -0400 | [diff] [blame] | 5235 | err = ext4_mb_load_buddy_gfp(sb, group, &e4b, |
| 5236 | GFP_NOFS|__GFP_NOFAIL); |
| 5237 | if (err) { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 5238 | ext4_error_err(sb, -err, "Error %d loading buddy information for %u", |
| 5239 | err, group); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5240 | continue; |
| 5241 | } |
| 5242 | ext4_lock_group(sb, group); |
| 5243 | list_del(&pa->pa_group_list); |
Eric Sandeen | 3e1e5f5 | 2010-10-27 21:30:07 -0400 | [diff] [blame] | 5244 | ext4_mb_release_group_pa(&e4b, pa); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5245 | ext4_unlock_group(sb, group); |
| 5246 | |
Jing Zhang | e39e07f | 2010-05-14 00:00:00 -0400 | [diff] [blame] | 5247 | ext4_mb_unload_buddy(&e4b); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5248 | list_del(&pa->u.pa_tmp_list); |
| 5249 | call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); |
| 5250 | } |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | /* |
| 5254 | * We have incremented pa_count. So it cannot be freed at this |
| 5255 | * point. Also we hold lg_mutex. So no parallel allocation is |
| 5256 | * possible from this lg. That means pa_free cannot be updated. |
| 5257 | * |
| 5258 | * A parallel ext4_mb_discard_group_preallocations is possible. |
| 5259 | * which can cause the lg_prealloc_list to be updated. |
| 5260 | */ |
| 5261 | |
| 5262 | static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac) |
| 5263 | { |
| 5264 | int order, added = 0, lg_prealloc_count = 1; |
| 5265 | struct super_block *sb = ac->ac_sb; |
| 5266 | struct ext4_locality_group *lg = ac->ac_lg; |
| 5267 | struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa; |
| 5268 | |
| 5269 | order = fls(pa->pa_free) - 1; |
| 5270 | if (order > PREALLOC_TB_SIZE - 1) |
| 5271 | /* The max size of hash table is PREALLOC_TB_SIZE */ |
| 5272 | order = PREALLOC_TB_SIZE - 1; |
| 5273 | /* Add the prealloc space to lg */ |
Niu Yawei | f116700 | 2013-02-01 21:31:27 -0500 | [diff] [blame] | 5274 | spin_lock(&lg->lg_prealloc_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5275 | list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order], |
Madhuparna Bhowmik | 92e9c58 | 2020-02-13 20:55:58 +0530 | [diff] [blame] | 5276 | pa_inode_list, |
| 5277 | lockdep_is_held(&lg->lg_prealloc_lock)) { |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5278 | spin_lock(&tmp_pa->pa_lock); |
| 5279 | if (tmp_pa->pa_deleted) { |
Theodore Ts'o | e7c9e3e | 2009-03-27 19:43:21 -0400 | [diff] [blame] | 5280 | spin_unlock(&tmp_pa->pa_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5281 | continue; |
| 5282 | } |
| 5283 | if (!added && pa->pa_free < tmp_pa->pa_free) { |
| 5284 | /* Add to the tail of the previous entry */ |
| 5285 | list_add_tail_rcu(&pa->pa_inode_list, |
| 5286 | &tmp_pa->pa_inode_list); |
| 5287 | added = 1; |
| 5288 | /* |
| 5289 | * we want to count the total |
| 5290 | * number of entries in the list |
| 5291 | */ |
| 5292 | } |
| 5293 | spin_unlock(&tmp_pa->pa_lock); |
| 5294 | lg_prealloc_count++; |
| 5295 | } |
| 5296 | if (!added) |
| 5297 | list_add_tail_rcu(&pa->pa_inode_list, |
| 5298 | &lg->lg_prealloc_list[order]); |
Niu Yawei | f116700 | 2013-02-01 21:31:27 -0500 | [diff] [blame] | 5299 | spin_unlock(&lg->lg_prealloc_lock); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5300 | |
| 5301 | /* Now trim the list to be not more than 8 elements */ |
| 5302 | if (lg_prealloc_count > 8) { |
| 5303 | ext4_mb_discard_lg_preallocations(sb, lg, |
Niu Yawei | f116700 | 2013-02-01 21:31:27 -0500 | [diff] [blame] | 5304 | order, lg_prealloc_count); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5305 | return; |
| 5306 | } |
| 5307 | return ; |
| 5308 | } |
| 5309 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5310 | /* |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 5311 | * if per-inode prealloc list is too long, trim some PA |
| 5312 | */ |
| 5313 | static void ext4_mb_trim_inode_pa(struct inode *inode) |
| 5314 | { |
| 5315 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 5316 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 5317 | int count, delta; |
| 5318 | |
| 5319 | count = atomic_read(&ei->i_prealloc_active); |
| 5320 | delta = (sbi->s_mb_max_inode_prealloc >> 2) + 1; |
| 5321 | if (count > sbi->s_mb_max_inode_prealloc + delta) { |
| 5322 | count -= sbi->s_mb_max_inode_prealloc; |
| 5323 | ext4_discard_preallocations(inode, count); |
| 5324 | } |
| 5325 | } |
| 5326 | |
| 5327 | /* |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5328 | * release all resource we used in allocation |
| 5329 | */ |
| 5330 | static int ext4_mb_release_context(struct ext4_allocation_context *ac) |
| 5331 | { |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 5332 | struct inode *inode = ac->ac_inode; |
| 5333 | struct ext4_inode_info *ei = EXT4_I(inode); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5334 | struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5335 | struct ext4_prealloc_space *pa = ac->ac_pa; |
| 5336 | if (pa) { |
Aneesh Kumar K.V | cc0fb9a | 2009-03-27 17:16:58 -0400 | [diff] [blame] | 5337 | if (pa->pa_type == MB_GROUP_PA) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5338 | /* see comment in ext4_mb_use_group_pa() */ |
Aneesh Kumar K.V | 6be2ded | 2008-07-23 14:14:05 -0400 | [diff] [blame] | 5339 | spin_lock(&pa->pa_lock); |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5340 | pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len); |
| 5341 | 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] | 5342 | pa->pa_free -= ac->ac_b_ex.fe_len; |
| 5343 | pa->pa_len -= ac->ac_b_ex.fe_len; |
| 5344 | spin_unlock(&pa->pa_lock); |
brookxu | 66d5e02 | 2020-08-17 15:36:06 +0800 | [diff] [blame] | 5345 | |
| 5346 | /* |
| 5347 | * We want to add the pa to the right bucket. |
| 5348 | * Remove it from the list and while adding |
| 5349 | * make sure the list to which we are adding |
| 5350 | * doesn't grow big. |
| 5351 | */ |
| 5352 | if (likely(pa->pa_free)) { |
| 5353 | spin_lock(pa->pa_obj_lock); |
| 5354 | list_del_rcu(&pa->pa_inode_list); |
| 5355 | spin_unlock(pa->pa_obj_lock); |
| 5356 | ext4_mb_add_n_trim(ac); |
| 5357 | } |
Aneesh Kumar K.V | ba44391 | 2009-02-10 11:14:34 -0500 | [diff] [blame] | 5358 | } |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 5359 | |
| 5360 | if (pa->pa_type == MB_INODE_PA) { |
| 5361 | /* |
| 5362 | * treat per-inode prealloc list as a lru list, then try |
| 5363 | * to trim the least recently used PA. |
| 5364 | */ |
| 5365 | spin_lock(pa->pa_obj_lock); |
| 5366 | list_move(&pa->pa_inode_list, &ei->i_prealloc_list); |
| 5367 | spin_unlock(pa->pa_obj_lock); |
| 5368 | } |
| 5369 | |
Aneesh Kumar K.V | ba44391 | 2009-02-10 11:14:34 -0500 | [diff] [blame] | 5370 | ext4_mb_put_pa(ac, ac->ac_sb, pa); |
| 5371 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5372 | if (ac->ac_bitmap_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5373 | put_page(ac->ac_bitmap_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5374 | if (ac->ac_buddy_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5375 | put_page(ac->ac_buddy_page); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5376 | if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) |
| 5377 | mutex_unlock(&ac->ac_lg->lg_mutex); |
| 5378 | ext4_mb_collect_stats(ac); |
brookxu | 27bc446 | 2020-08-17 15:36:15 +0800 | [diff] [blame] | 5379 | ext4_mb_trim_inode_pa(inode); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5380 | return 0; |
| 5381 | } |
| 5382 | |
| 5383 | static int ext4_mb_discard_preallocations(struct super_block *sb, int needed) |
| 5384 | { |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 5385 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5386 | int ret; |
| 5387 | int freed = 0; |
| 5388 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 5389 | trace_ext4_mb_discard_preallocations(sb, needed); |
Theodore Ts'o | 8df9675 | 2009-05-01 08:50:38 -0400 | [diff] [blame] | 5390 | for (i = 0; i < ngroups && needed > 0; i++) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5391 | ret = ext4_mb_discard_group_preallocations(sb, i, needed); |
| 5392 | freed += ret; |
| 5393 | needed -= ret; |
| 5394 | } |
| 5395 | |
| 5396 | return freed; |
| 5397 | } |
| 5398 | |
Ritesh Harjani | cf5e2ca | 2020-05-20 12:10:33 +0530 | [diff] [blame] | 5399 | static bool ext4_mb_discard_preallocations_should_retry(struct super_block *sb, |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 5400 | struct ext4_allocation_context *ac, u64 *seq) |
Ritesh Harjani | cf5e2ca | 2020-05-20 12:10:33 +0530 | [diff] [blame] | 5401 | { |
| 5402 | int freed; |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 5403 | u64 seq_retry = 0; |
| 5404 | bool ret = false; |
Ritesh Harjani | cf5e2ca | 2020-05-20 12:10:33 +0530 | [diff] [blame] | 5405 | |
| 5406 | freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len); |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 5407 | if (freed) { |
| 5408 | ret = true; |
| 5409 | goto out_dbg; |
| 5410 | } |
| 5411 | seq_retry = ext4_get_discard_pa_seq_sum(); |
Ritesh Harjani | 9937783 | 2020-05-20 12:10:36 +0530 | [diff] [blame] | 5412 | if (!(ac->ac_flags & EXT4_MB_STRICT_CHECK) || seq_retry != *seq) { |
| 5413 | ac->ac_flags |= EXT4_MB_STRICT_CHECK; |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 5414 | *seq = seq_retry; |
| 5415 | ret = true; |
| 5416 | } |
| 5417 | |
| 5418 | out_dbg: |
| 5419 | mb_debug(sb, "freed %d, retry ? %s\n", freed, ret ? "yes" : "no"); |
| 5420 | return ret; |
Ritesh Harjani | cf5e2ca | 2020-05-20 12:10:33 +0530 | [diff] [blame] | 5421 | } |
| 5422 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 5423 | static ext4_fsblk_t ext4_mb_new_blocks_simple(handle_t *handle, |
| 5424 | struct ext4_allocation_request *ar, int *errp); |
| 5425 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5426 | /* |
| 5427 | * Main entry point into mballoc to allocate blocks |
| 5428 | * it tries to use preallocation first, then falls back |
| 5429 | * to usual allocation |
| 5430 | */ |
| 5431 | ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5432 | struct ext4_allocation_request *ar, int *errp) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5433 | { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5434 | struct ext4_allocation_context *ac = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5435 | struct ext4_sb_info *sbi; |
| 5436 | struct super_block *sb; |
| 5437 | ext4_fsblk_t block = 0; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 5438 | unsigned int inquota = 0; |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5439 | unsigned int reserv_clstrs = 0; |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 5440 | u64 seq; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5441 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 5442 | might_sleep(); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5443 | sb = ar->inode->i_sb; |
| 5444 | sbi = EXT4_SB(sb); |
| 5445 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 5446 | trace_ext4_request_blocks(ar); |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 5447 | if (sbi->s_mount_state & EXT4_FC_REPLAY) |
| 5448 | return ext4_mb_new_blocks_simple(handle, ar, errp); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 5449 | |
Dmitry Monakhov | 45dc63e | 2011-10-20 20:07:23 -0400 | [diff] [blame] | 5450 | /* Allow to use superuser reservation for quota file */ |
Tahsin Erdogan | 02749a4 | 2017-06-22 11:31:25 -0400 | [diff] [blame] | 5451 | if (ext4_is_quota_file(ar->inode)) |
Dmitry Monakhov | 45dc63e | 2011-10-20 20:07:23 -0400 | [diff] [blame] | 5452 | ar->flags |= EXT4_MB_USE_ROOT_BLOCKS; |
| 5453 | |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 5454 | if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) { |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 5455 | /* Without delayed allocation we need to verify |
| 5456 | * there is enough free blocks to do block allocation |
| 5457 | * and verify allocation doesn't exceed the quota limits. |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 5458 | */ |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 5459 | while (ar->len && |
Theodore Ts'o | e7d5f31 | 2011-09-09 19:14:51 -0400 | [diff] [blame] | 5460 | ext4_claim_free_clusters(sbi, ar->len, ar->flags)) { |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 5461 | |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 5462 | /* let others to free the space */ |
Lukas Czerner | bb8b20e | 2013-03-10 22:28:09 -0400 | [diff] [blame] | 5463 | cond_resched(); |
Aneesh Kumar K.V | 030ba6b | 2008-09-08 23:14:50 -0400 | [diff] [blame] | 5464 | ar->len = ar->len >> 1; |
| 5465 | } |
| 5466 | if (!ar->len) { |
Ritesh Harjani | bbc4ec7 | 2020-05-10 11:54:43 +0530 | [diff] [blame] | 5467 | ext4_mb_show_pa(sb); |
Aneesh Kumar K.V | a30d542a | 2008-10-09 10:56:23 -0400 | [diff] [blame] | 5468 | *errp = -ENOSPC; |
| 5469 | return 0; |
| 5470 | } |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5471 | reserv_clstrs = ar->len; |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 5472 | if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5473 | dquot_alloc_block_nofail(ar->inode, |
| 5474 | EXT4_C2B(sbi, ar->len)); |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 5475 | } else { |
| 5476 | while (ar->len && |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5477 | dquot_alloc_block(ar->inode, |
| 5478 | EXT4_C2B(sbi, ar->len))) { |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 5479 | |
| 5480 | ar->flags |= EXT4_MB_HINT_NOPREALLOC; |
| 5481 | ar->len--; |
| 5482 | } |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 5483 | } |
| 5484 | inquota = ar->len; |
| 5485 | if (ar->len == 0) { |
| 5486 | *errp = -EDQUOT; |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5487 | goto out; |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 5488 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 5489 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 5490 | |
Wei Yongjun | 85556c9 | 2012-09-26 20:43:37 -0400 | [diff] [blame] | 5491 | ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS); |
Theodore Ts'o | 833576b | 2009-07-13 09:45:52 -0400 | [diff] [blame] | 5492 | if (!ac) { |
Shen Feng | 363d425 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5493 | ar->len = 0; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5494 | *errp = -ENOMEM; |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5495 | goto out; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5496 | } |
| 5497 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5498 | *errp = ext4_mb_initialize_context(ac, ar); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5499 | if (*errp) { |
| 5500 | ar->len = 0; |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5501 | goto out; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5502 | } |
| 5503 | |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5504 | ac->ac_op = EXT4_MB_HISTORY_PREALLOC; |
Ritesh Harjani | 8119853 | 2020-06-09 16:23:10 +0530 | [diff] [blame] | 5505 | seq = this_cpu_read(discard_pa_seq); |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5506 | if (!ext4_mb_use_preallocated(ac)) { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5507 | ac->ac_op = EXT4_MB_HISTORY_ALLOC; |
| 5508 | ext4_mb_normalize_request(ac, ar); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 5509 | |
| 5510 | *errp = ext4_mb_pa_alloc(ac); |
| 5511 | if (*errp) |
| 5512 | goto errout; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5513 | repeat: |
| 5514 | /* allocate space in core */ |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5515 | *errp = ext4_mb_regular_allocator(ac); |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 5516 | /* |
| 5517 | * pa allocated above is added to grp->bb_prealloc_list only |
| 5518 | * when we were able to allocate some block i.e. when |
| 5519 | * ac->ac_status == AC_STATUS_FOUND. |
| 5520 | * And error from above mean ac->ac_status != AC_STATUS_FOUND |
| 5521 | * So we have to free this pa here itself. |
| 5522 | */ |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 5523 | if (*errp) { |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 5524 | ext4_mb_pa_free(ac); |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 5525 | ext4_discard_allocated_blocks(ac); |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5526 | goto errout; |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 5527 | } |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 5528 | if (ac->ac_status == AC_STATUS_FOUND && |
| 5529 | ac->ac_o_ex.fe_len >= ac->ac_f_ex.fe_len) |
| 5530 | ext4_mb_pa_free(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5531 | } |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5532 | if (likely(ac->ac_status == AC_STATUS_FOUND)) { |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5533 | *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs); |
Vegard Nossum | 554a5cc | 2016-07-14 23:02:47 -0400 | [diff] [blame] | 5534 | if (*errp) { |
Curt Wohlgemuth | b844167 | 2009-12-08 22:18:25 -0500 | [diff] [blame] | 5535 | ext4_discard_allocated_blocks(ac); |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 5536 | goto errout; |
| 5537 | } else { |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 5538 | block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); |
| 5539 | ar->len = ac->ac_b_ex.fe_len; |
| 5540 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5541 | } else { |
Ritesh Harjani | 07b5b8e | 2020-05-20 12:10:34 +0530 | [diff] [blame] | 5542 | if (ext4_mb_discard_preallocations_should_retry(sb, ac, &seq)) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5543 | goto repeat; |
Ritesh Harjani | 53f86b1 | 2020-05-20 12:10:32 +0530 | [diff] [blame] | 5544 | /* |
| 5545 | * If block allocation fails then the pa allocated above |
| 5546 | * needs to be freed here itself. |
| 5547 | */ |
| 5548 | ext4_mb_pa_free(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5549 | *errp = -ENOSPC; |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5550 | } |
| 5551 | |
Eric Sandeen | 6d138ce | 2012-11-08 11:11:59 -0500 | [diff] [blame] | 5552 | errout: |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5553 | if (*errp) { |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5554 | ac->ac_b_ex.fe_len = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5555 | ar->len = 0; |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5556 | ext4_mb_show_ac(ac); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5557 | } |
Eric Sandeen | 256bdb4 | 2008-02-10 01:13:33 -0500 | [diff] [blame] | 5558 | ext4_mb_release_context(ac); |
Aditya Kali | 6c7a120 | 2010-08-05 16:22:24 -0400 | [diff] [blame] | 5559 | out: |
| 5560 | if (ac) |
| 5561 | kmem_cache_free(ext4_ac_cachep, ac); |
Mingming Cao | 60e58e0 | 2009-01-22 18:13:05 +0100 | [diff] [blame] | 5562 | if (inquota && ar->len < inquota) |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5563 | 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] | 5564 | if (!ar->len) { |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 5565 | if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) |
Aneesh Kumar K.V | 0087d9f | 2009-01-05 21:49:12 -0500 | [diff] [blame] | 5566 | /* release all the reserved blocks if non delalloc */ |
Theodore Ts'o | 5704265 | 2011-09-09 18:56:51 -0400 | [diff] [blame] | 5567 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
Theodore Ts'o | 53accfa | 2011-09-09 18:48:51 -0400 | [diff] [blame] | 5568 | reserv_clstrs); |
Aneesh Kumar K.V | 0087d9f | 2009-01-05 21:49:12 -0500 | [diff] [blame] | 5569 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5570 | |
Theodore Ts'o | 9bffad1 | 2009-06-17 11:48:11 -0400 | [diff] [blame] | 5571 | trace_ext4_allocate_blocks(ar, (unsigned long long)block); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 5572 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5573 | return block; |
| 5574 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5575 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5576 | /* |
| 5577 | * We can merge two free data extents only if the physical blocks |
| 5578 | * are contiguous, AND the extents were freed by the same transaction, |
| 5579 | * AND the blocks are associated with the same group. |
| 5580 | */ |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5581 | static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi, |
| 5582 | struct ext4_free_data *entry, |
| 5583 | struct ext4_free_data *new_entry, |
| 5584 | struct rb_root *entry_rb_root) |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5585 | { |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5586 | if ((entry->efd_tid != new_entry->efd_tid) || |
| 5587 | (entry->efd_group != new_entry->efd_group)) |
| 5588 | return; |
| 5589 | if (entry->efd_start_cluster + entry->efd_count == |
| 5590 | new_entry->efd_start_cluster) { |
| 5591 | new_entry->efd_start_cluster = entry->efd_start_cluster; |
| 5592 | new_entry->efd_count += entry->efd_count; |
| 5593 | } else if (new_entry->efd_start_cluster + new_entry->efd_count == |
| 5594 | entry->efd_start_cluster) { |
| 5595 | new_entry->efd_count += entry->efd_count; |
| 5596 | } else |
| 5597 | return; |
| 5598 | spin_lock(&sbi->s_md_lock); |
| 5599 | list_del(&entry->efd_list); |
| 5600 | spin_unlock(&sbi->s_md_lock); |
| 5601 | rb_erase(&entry->efd_node, entry_rb_root); |
| 5602 | kmem_cache_free(ext4_free_data_cachep, entry); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5603 | } |
| 5604 | |
Eric Sandeen | 4ddfef7 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 5605 | static noinline_for_stack int |
| 5606 | 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] | 5607 | struct ext4_free_data *new_entry) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5608 | { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 5609 | ext4_group_t group = e4b->bd_group; |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5610 | ext4_grpblk_t cluster; |
Theodore Ts'o | d08854f | 2016-06-26 18:24:01 -0400 | [diff] [blame] | 5611 | ext4_grpblk_t clusters = new_entry->efd_count; |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5612 | struct ext4_free_data *entry; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5613 | struct ext4_group_info *db = e4b->bd_info; |
| 5614 | struct super_block *sb = e4b->bd_sb; |
| 5615 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5616 | struct rb_node **n = &db->bb_free_root.rb_node, *node; |
| 5617 | struct rb_node *parent = NULL, *new_node; |
| 5618 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 5619 | BUG_ON(!ext4_handle_valid(handle)); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5620 | BUG_ON(e4b->bd_bitmap_page == NULL); |
| 5621 | BUG_ON(e4b->bd_buddy_page == NULL); |
| 5622 | |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5623 | new_node = &new_entry->efd_node; |
| 5624 | cluster = new_entry->efd_start_cluster; |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5625 | |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5626 | if (!*n) { |
| 5627 | /* first free block exent. We need to |
| 5628 | protect buddy cache from being freed, |
| 5629 | * otherwise we'll refresh it from |
| 5630 | * on-disk bitmap and lose not-yet-available |
| 5631 | * blocks */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5632 | get_page(e4b->bd_buddy_page); |
| 5633 | get_page(e4b->bd_bitmap_page); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5634 | } |
| 5635 | while (*n) { |
| 5636 | parent = *n; |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5637 | entry = rb_entry(parent, struct ext4_free_data, efd_node); |
| 5638 | if (cluster < entry->efd_start_cluster) |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5639 | n = &(*n)->rb_left; |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5640 | else if (cluster >= (entry->efd_start_cluster + entry->efd_count)) |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5641 | n = &(*n)->rb_right; |
| 5642 | else { |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 5643 | ext4_grp_locked_error(sb, group, 0, |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5644 | ext4_group_first_block_no(sb, group) + |
| 5645 | EXT4_C2B(sbi, cluster), |
Theodore Ts'o | e29136f | 2010-06-29 12:54:28 -0400 | [diff] [blame] | 5646 | "Block already on to-be-freed list"); |
Chunguang Xu | cca4155 | 2020-11-07 23:58:18 +0800 | [diff] [blame] | 5647 | kmem_cache_free(ext4_free_data_cachep, new_entry); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5648 | return 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5649 | } |
| 5650 | } |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5651 | |
| 5652 | rb_link_node(new_node, parent, n); |
| 5653 | rb_insert_color(new_node, &db->bb_free_root); |
| 5654 | |
| 5655 | /* Now try to see the extent can be merged to left and right */ |
| 5656 | node = rb_prev(new_node); |
| 5657 | if (node) { |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5658 | entry = rb_entry(node, struct ext4_free_data, efd_node); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5659 | ext4_try_merge_freed_extent(sbi, entry, new_entry, |
| 5660 | &(db->bb_free_root)); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5661 | } |
| 5662 | |
| 5663 | node = rb_next(new_node); |
| 5664 | if (node) { |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5665 | entry = rb_entry(node, struct ext4_free_data, efd_node); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5666 | ext4_try_merge_freed_extent(sbi, entry, new_entry, |
| 5667 | &(db->bb_free_root)); |
Aneesh Kumar K.V | c894058 | 2008-10-16 10:14:27 -0400 | [diff] [blame] | 5668 | } |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5669 | |
Theodore Ts'o | d08854f | 2016-06-26 18:24:01 -0400 | [diff] [blame] | 5670 | spin_lock(&sbi->s_md_lock); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5671 | 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] | 5672 | sbi->s_mb_free_pending += clusters; |
| 5673 | spin_unlock(&sbi->s_md_lock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5674 | return 0; |
| 5675 | } |
| 5676 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 5677 | /* |
| 5678 | * Simple allocator for Ext4 fast commit replay path. It searches for blocks |
| 5679 | * linearly starting at the goal block and also excludes the blocks which |
| 5680 | * are going to be in use after fast commit replay. |
| 5681 | */ |
| 5682 | static ext4_fsblk_t ext4_mb_new_blocks_simple(handle_t *handle, |
| 5683 | struct ext4_allocation_request *ar, int *errp) |
| 5684 | { |
| 5685 | struct buffer_head *bitmap_bh; |
| 5686 | struct super_block *sb = ar->inode->i_sb; |
| 5687 | ext4_group_t group; |
| 5688 | ext4_grpblk_t blkoff; |
Dan Carpenter | e121bd4 | 2020-10-30 14:46:20 +0300 | [diff] [blame] | 5689 | int i = sb->s_blocksize; |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 5690 | ext4_fsblk_t goal, block; |
| 5691 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
| 5692 | |
| 5693 | goal = ar->goal; |
| 5694 | if (goal < le32_to_cpu(es->s_first_data_block) || |
| 5695 | goal >= ext4_blocks_count(es)) |
| 5696 | goal = le32_to_cpu(es->s_first_data_block); |
| 5697 | |
| 5698 | ar->len = 0; |
| 5699 | ext4_get_group_no_and_offset(sb, goal, &group, &blkoff); |
| 5700 | for (; group < ext4_get_groups_count(sb); group++) { |
| 5701 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
| 5702 | if (IS_ERR(bitmap_bh)) { |
| 5703 | *errp = PTR_ERR(bitmap_bh); |
| 5704 | pr_warn("Failed to read block bitmap\n"); |
| 5705 | return 0; |
| 5706 | } |
| 5707 | |
| 5708 | ext4_get_group_no_and_offset(sb, |
| 5709 | max(ext4_group_first_block_no(sb, group), goal), |
| 5710 | NULL, &blkoff); |
| 5711 | i = mb_find_next_zero_bit(bitmap_bh->b_data, sb->s_blocksize, |
| 5712 | blkoff); |
| 5713 | brelse(bitmap_bh); |
| 5714 | if (i >= sb->s_blocksize) |
| 5715 | continue; |
| 5716 | if (ext4_fc_replay_check_excluded(sb, |
| 5717 | ext4_group_first_block_no(sb, group) + i)) |
| 5718 | continue; |
| 5719 | break; |
| 5720 | } |
| 5721 | |
| 5722 | if (group >= ext4_get_groups_count(sb) && i >= sb->s_blocksize) |
| 5723 | return 0; |
| 5724 | |
| 5725 | block = ext4_group_first_block_no(sb, group) + i; |
| 5726 | ext4_mb_mark_bb(sb, block, 1, 1); |
| 5727 | ar->len = 1; |
| 5728 | |
| 5729 | return block; |
| 5730 | } |
| 5731 | |
| 5732 | static void ext4_free_blocks_simple(struct inode *inode, ext4_fsblk_t block, |
| 5733 | unsigned long count) |
| 5734 | { |
| 5735 | struct buffer_head *bitmap_bh; |
| 5736 | struct super_block *sb = inode->i_sb; |
| 5737 | struct ext4_group_desc *gdp; |
| 5738 | struct buffer_head *gdp_bh; |
| 5739 | ext4_group_t group; |
| 5740 | ext4_grpblk_t blkoff; |
| 5741 | int already_freed = 0, err, i; |
| 5742 | |
| 5743 | ext4_get_group_no_and_offset(sb, block, &group, &blkoff); |
| 5744 | bitmap_bh = ext4_read_block_bitmap(sb, group); |
| 5745 | if (IS_ERR(bitmap_bh)) { |
| 5746 | err = PTR_ERR(bitmap_bh); |
| 5747 | pr_warn("Failed to read block bitmap\n"); |
| 5748 | return; |
| 5749 | } |
| 5750 | gdp = ext4_get_group_desc(sb, group, &gdp_bh); |
| 5751 | if (!gdp) |
| 5752 | return; |
| 5753 | |
| 5754 | for (i = 0; i < count; i++) { |
| 5755 | if (!mb_test_bit(blkoff + i, bitmap_bh->b_data)) |
| 5756 | already_freed++; |
| 5757 | } |
| 5758 | mb_clear_bits(bitmap_bh->b_data, blkoff, count); |
| 5759 | err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh); |
| 5760 | if (err) |
| 5761 | return; |
| 5762 | ext4_free_group_clusters_set( |
| 5763 | sb, gdp, ext4_free_group_clusters(sb, gdp) + |
| 5764 | count - already_freed); |
| 5765 | ext4_block_bitmap_csum_set(sb, group, gdp, bitmap_bh); |
| 5766 | ext4_group_desc_csum_set(sb, group, gdp); |
| 5767 | ext4_handle_dirty_metadata(NULL, NULL, gdp_bh); |
| 5768 | sync_dirty_buffer(bitmap_bh); |
| 5769 | sync_dirty_buffer(gdp_bh); |
| 5770 | brelse(bitmap_bh); |
| 5771 | } |
| 5772 | |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 5773 | /** |
| 5774 | * ext4_free_blocks() -- Free given blocks and update quota |
| 5775 | * @handle: handle for this transaction |
| 5776 | * @inode: inode |
Theodore Ts'o | c60990b | 2019-06-19 16:30:03 -0400 | [diff] [blame] | 5777 | * @bh: optional buffer of the block to be freed |
| 5778 | * @block: starting physical block to be freed |
| 5779 | * @count: number of blocks to be freed |
Yongqiang Yang | 5def136 | 2011-06-05 23:26:40 -0400 | [diff] [blame] | 5780 | * @flags: flags used by ext4_free_blocks |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5781 | */ |
Theodore Ts'o | 4433871 | 2009-11-22 07:44:56 -0500 | [diff] [blame] | 5782 | void ext4_free_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5783 | struct buffer_head *bh, ext4_fsblk_t block, |
| 5784 | unsigned long count, int flags) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5785 | { |
Aneesh Kumar K.V | 26346ff | 2008-02-10 01:10:04 -0500 | [diff] [blame] | 5786 | struct buffer_head *bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5787 | struct super_block *sb = inode->i_sb; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5788 | struct ext4_group_desc *gdp; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 5789 | unsigned int overflow; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5790 | ext4_grpblk_t bit; |
| 5791 | struct buffer_head *gd_bh; |
| 5792 | ext4_group_t block_group; |
| 5793 | struct ext4_sb_info *sbi; |
| 5794 | struct ext4_buddy e4b; |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5795 | unsigned int count_clusters; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5796 | int err = 0; |
| 5797 | int ret; |
| 5798 | |
Harshad Shirwadkar | 8016e29 | 2020-10-15 13:37:59 -0700 | [diff] [blame] | 5799 | sbi = EXT4_SB(sb); |
| 5800 | |
| 5801 | if (sbi->s_mount_state & EXT4_FC_REPLAY) { |
| 5802 | ext4_free_blocks_simple(inode, block, count); |
| 5803 | return; |
| 5804 | } |
| 5805 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 5806 | might_sleep(); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5807 | if (bh) { |
| 5808 | if (block) |
| 5809 | BUG_ON(block != bh->b_blocknr); |
| 5810 | else |
| 5811 | block = bh->b_blocknr; |
| 5812 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5813 | |
Theodore Ts'o | 1f2acb6 | 2010-01-22 17:40:42 -0500 | [diff] [blame] | 5814 | if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) && |
Jan Kara | ce9f24c | 2020-07-28 15:04:34 +0200 | [diff] [blame] | 5815 | !ext4_inode_block_valid(inode, block, count)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 5816 | ext4_error(sb, "Freeing blocks not in datazone - " |
Theodore Ts'o | 1f2acb6 | 2010-01-22 17:40:42 -0500 | [diff] [blame] | 5817 | "block = %llu, count = %lu", block, count); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5818 | goto error_return; |
| 5819 | } |
| 5820 | |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 5821 | ext4_debug("freeing block %llu\n", block); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5822 | trace_ext4_free_blocks(inode, block, count, flags); |
| 5823 | |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5824 | if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) { |
| 5825 | BUG_ON(count > 1); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5826 | |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5827 | ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA, |
| 5828 | inode, bh, block); |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5829 | } |
| 5830 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 5831 | /* |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5832 | * If the extent to be freed does not begin on a cluster |
| 5833 | * boundary, we need to deal with partial clusters at the |
| 5834 | * beginning and end of the extent. Normally we will free |
| 5835 | * blocks at the beginning or the end unless we are explicitly |
| 5836 | * requested to avoid doing so. |
| 5837 | */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 5838 | overflow = EXT4_PBLK_COFF(sbi, block); |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5839 | if (overflow) { |
| 5840 | if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) { |
| 5841 | overflow = sbi->s_cluster_ratio - overflow; |
| 5842 | block += overflow; |
| 5843 | if (count > overflow) |
| 5844 | count -= overflow; |
| 5845 | else |
| 5846 | return; |
| 5847 | } else { |
| 5848 | block -= overflow; |
| 5849 | count += overflow; |
| 5850 | } |
| 5851 | } |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 5852 | overflow = EXT4_LBLK_COFF(sbi, count); |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5853 | if (overflow) { |
| 5854 | if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) { |
| 5855 | if (count > overflow) |
| 5856 | count -= overflow; |
| 5857 | else |
| 5858 | return; |
| 5859 | } else |
| 5860 | count += sbi->s_cluster_ratio - overflow; |
| 5861 | } |
| 5862 | |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5863 | if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) { |
| 5864 | int i; |
Daeho Jeong | f96c450 | 2016-02-21 18:31:41 -0500 | [diff] [blame] | 5865 | int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA; |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5866 | |
| 5867 | for (i = 0; i < count; i++) { |
| 5868 | cond_resched(); |
Daeho Jeong | f96c450 | 2016-02-21 18:31:41 -0500 | [diff] [blame] | 5869 | if (is_metadata) |
| 5870 | bh = sb_find_get_block(inode->i_sb, block + i); |
| 5871 | ext4_forget(handle, is_metadata, inode, bh, block + i); |
Daeho Jeong | 9c02ac9 | 2015-10-17 22:28:21 -0400 | [diff] [blame] | 5872 | } |
| 5873 | } |
| 5874 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5875 | do_more: |
| 5876 | overflow = 0; |
| 5877 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
| 5878 | |
Darrick J. Wong | 163a203 | 2013-08-28 17:35:51 -0400 | [diff] [blame] | 5879 | if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT( |
| 5880 | ext4_get_group_info(sb, block_group)))) |
| 5881 | return; |
| 5882 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5883 | /* |
| 5884 | * Check to see if we are freeing blocks across a group |
| 5885 | * boundary. |
| 5886 | */ |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5887 | if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) { |
| 5888 | overflow = EXT4_C2B(sbi, bit) + count - |
| 5889 | EXT4_BLOCKS_PER_GROUP(sb); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5890 | count -= overflow; |
| 5891 | } |
Lukas Czerner | 810da24 | 2013-03-02 17:18:58 -0500 | [diff] [blame] | 5892 | count_clusters = EXT4_NUM_B2C(sbi, count); |
Theodore Ts'o | 574ca17 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5893 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 5894 | if (IS_ERR(bitmap_bh)) { |
| 5895 | err = PTR_ERR(bitmap_bh); |
| 5896 | bitmap_bh = NULL; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5897 | goto error_return; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 5898 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5899 | gdp = ext4_get_group_desc(sb, block_group, &gd_bh); |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 5900 | if (!gdp) { |
| 5901 | err = -EIO; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5902 | goto error_return; |
Aneesh Kumar K.V | ce89f46 | 2008-07-23 14:09:29 -0400 | [diff] [blame] | 5903 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5904 | |
| 5905 | if (in_range(ext4_block_bitmap(sb, gdp), block, count) || |
| 5906 | in_range(ext4_inode_bitmap(sb, gdp), block, count) || |
| 5907 | in_range(block, ext4_inode_table(sb, gdp), |
Jun Piao | 49598e0 | 2018-01-11 13:17:49 -0500 | [diff] [blame] | 5908 | sbi->s_itb_per_group) || |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5909 | in_range(block + count - 1, ext4_inode_table(sb, gdp), |
Jun Piao | 49598e0 | 2018-01-11 13:17:49 -0500 | [diff] [blame] | 5910 | sbi->s_itb_per_group)) { |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5911 | |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 5912 | ext4_error(sb, "Freeing blocks in system zone - " |
Theodore Ts'o | 0610b6e | 2009-06-15 03:45:05 -0400 | [diff] [blame] | 5913 | "Block = %llu, count = %lu", block, count); |
Aneesh Kumar K.V | 519deca0 | 2008-05-15 14:43:20 -0400 | [diff] [blame] | 5914 | /* err = 0. ext4_std_error should be a no op */ |
| 5915 | goto error_return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
| 5919 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
| 5920 | if (err) |
| 5921 | goto error_return; |
| 5922 | |
| 5923 | /* |
| 5924 | * We are about to modify some metadata. Call the journal APIs |
| 5925 | * to unshare ->b_data if a currently-committing transaction is |
| 5926 | * using it |
| 5927 | */ |
| 5928 | BUFFER_TRACE(gd_bh, "get_write_access"); |
| 5929 | err = ext4_journal_get_write_access(handle, gd_bh); |
| 5930 | if (err) |
| 5931 | goto error_return; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5932 | #ifdef AGGRESSIVE_CHECK |
| 5933 | { |
| 5934 | int i; |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5935 | for (i = 0; i < count_clusters; i++) |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5936 | BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data)); |
| 5937 | } |
| 5938 | #endif |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5939 | trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5940 | |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 5941 | /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */ |
| 5942 | err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b, |
| 5943 | GFP_NOFS|__GFP_NOFAIL); |
Aneesh Kumar K.V | 920313a | 2009-01-05 21:36:19 -0500 | [diff] [blame] | 5944 | if (err) |
| 5945 | goto error_return; |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 5946 | |
Daeho Jeong | f96c450 | 2016-02-21 18:31:41 -0500 | [diff] [blame] | 5947 | /* |
| 5948 | * We need to make sure we don't reuse the freed block until after the |
| 5949 | * transaction is committed. We make an exception if the inode is to be |
| 5950 | * written in writeback mode since writeback mode has weak data |
| 5951 | * consistency guarantees. |
| 5952 | */ |
| 5953 | if (ext4_handle_valid(handle) && |
| 5954 | ((flags & EXT4_FREE_BLOCKS_METADATA) || |
| 5955 | !ext4_should_writeback_data(inode))) { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5956 | struct ext4_free_data *new_entry; |
| 5957 | /* |
Michal Hocko | 7444a07 | 2015-07-05 12:33:44 -0400 | [diff] [blame] | 5958 | * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed |
| 5959 | * to fail. |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5960 | */ |
Michal Hocko | 7444a07 | 2015-07-05 12:33:44 -0400 | [diff] [blame] | 5961 | new_entry = kmem_cache_alloc(ext4_free_data_cachep, |
| 5962 | GFP_NOFS|__GFP_NOFAIL); |
Bobi Jam | 18aadd4 | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 5963 | new_entry->efd_start_cluster = bit; |
| 5964 | new_entry->efd_group = block_group; |
| 5965 | new_entry->efd_count = count_clusters; |
| 5966 | new_entry->efd_tid = handle->h_transaction->t_tid; |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 5967 | |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5968 | ext4_lock_group(sb, block_group); |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5969 | mb_clear_bits(bitmap_bh->b_data, bit, count_clusters); |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5970 | ext4_mb_free_metadata(handle, &e4b, new_entry); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5971 | } else { |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 5972 | /* need to update group_info->bb_free and bitmap |
| 5973 | * with group lock held. generate_buddy look at |
| 5974 | * them with group lock_held |
| 5975 | */ |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5976 | if (test_opt(sb, DISCARD)) { |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 5977 | err = ext4_issue_discard(sb, block_group, bit, count, |
| 5978 | NULL); |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5979 | if (err && err != -EOPNOTSUPP) |
| 5980 | ext4_msg(sb, KERN_WARNING, "discard request in" |
| 5981 | " group:%d block:%d count:%lu failed" |
| 5982 | " with %d", block_group, bit, count, |
| 5983 | err); |
Lukas Czerner | 8f9ff18 | 2013-10-30 11:10:52 -0400 | [diff] [blame] | 5984 | } else |
| 5985 | EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info); |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 5986 | |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 5987 | ext4_lock_group(sb, block_group); |
Theodore Ts'o | 8413019 | 2011-09-09 18:50:51 -0400 | [diff] [blame] | 5988 | mb_clear_bits(bitmap_bh->b_data, bit, count_clusters); |
| 5989 | mb_free_blocks(inode, &e4b, bit, count_clusters); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5990 | } |
| 5991 | |
Theodore Ts'o | 021b65b | 2011-09-09 19:08:51 -0400 | [diff] [blame] | 5992 | ret = ext4_free_group_clusters(sb, gdp) + count_clusters; |
| 5993 | ext4_free_group_clusters_set(sb, gdp, ret); |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 5994 | ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh); |
Darrick J. Wong | feb0ab3 | 2012-04-29 18:45:10 -0400 | [diff] [blame] | 5995 | ext4_group_desc_csum_set(sb, block_group, gdp); |
Aneesh Kumar K.V | 955ce5f | 2009-05-02 20:35:09 -0400 | [diff] [blame] | 5996 | ext4_unlock_group(sb, block_group); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 5997 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 5998 | if (sbi->s_log_groups_per_flex) { |
| 5999 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); |
Theodore Ts'o | 90ba983 | 2013-03-11 23:39:59 -0400 | [diff] [blame] | 6000 | atomic64_add(count_clusters, |
Suraj Jitindar Singh | 7c99072 | 2020-02-18 19:08:51 -0800 | [diff] [blame] | 6001 | &sbi_array_rcu_deref(sbi, s_flex_groups, |
| 6002 | flex_group)->free_clusters); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 6003 | } |
| 6004 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 6005 | /* |
| 6006 | * on a bigalloc file system, defer the s_freeclusters_counter |
| 6007 | * update to the caller (ext4_remove_space and friends) so they |
| 6008 | * can determine if a cluster freed here should be rereserved |
| 6009 | */ |
| 6010 | if (!(flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)) { |
| 6011 | if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE)) |
| 6012 | dquot_free_block(inode, EXT4_C2B(sbi, count_clusters)); |
| 6013 | percpu_counter_add(&sbi->s_freeclusters_counter, |
| 6014 | count_clusters); |
| 6015 | } |
Jan Kara | 7d73453 | 2013-08-17 09:36:54 -0400 | [diff] [blame] | 6016 | |
| 6017 | ext4_mb_unload_buddy(&e4b); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 6018 | |
Aneesh Kumar K.V | 7a2fcbf | 2009-01-05 21:36:55 -0500 | [diff] [blame] | 6019 | /* We dirtied the bitmap block */ |
| 6020 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
| 6021 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
| 6022 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 6023 | /* And the group descriptor block */ |
| 6024 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 6025 | ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 6026 | if (!err) |
| 6027 | err = ret; |
| 6028 | |
| 6029 | if (overflow && !err) { |
| 6030 | block += count; |
| 6031 | count = overflow; |
| 6032 | put_bh(bitmap_bh); |
| 6033 | goto do_more; |
| 6034 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 6035 | error_return: |
| 6036 | brelse(bitmap_bh); |
| 6037 | ext4_std_error(sb, err); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 6038 | return; |
| 6039 | } |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6040 | |
| 6041 | /** |
Yongqiang Yang | 0529155 | 2011-07-26 21:43:56 -0400 | [diff] [blame] | 6042 | * ext4_group_add_blocks() -- Add given blocks to an existing group |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6043 | * @handle: handle to this transaction |
| 6044 | * @sb: super block |
Anatol Pomozov | 4907cb7 | 2012-09-01 10:31:09 -0700 | [diff] [blame] | 6045 | * @block: start physical block to add to the block group |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6046 | * @count: number of blocks to free |
| 6047 | * |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 6048 | * This marks the blocks as free in the bitmap and buddy. |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6049 | */ |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 6050 | int ext4_group_add_blocks(handle_t *handle, struct super_block *sb, |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6051 | ext4_fsblk_t block, unsigned long count) |
| 6052 | { |
| 6053 | struct buffer_head *bitmap_bh = NULL; |
| 6054 | struct buffer_head *gd_bh; |
| 6055 | ext4_group_t block_group; |
| 6056 | ext4_grpblk_t bit; |
| 6057 | unsigned int i; |
| 6058 | struct ext4_group_desc *desc; |
| 6059 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 6060 | struct ext4_buddy e4b; |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 6061 | int err = 0, ret, free_clusters_count; |
| 6062 | ext4_grpblk_t clusters_freed; |
| 6063 | ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block); |
| 6064 | ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1); |
| 6065 | unsigned long cluster_count = last_cluster - first_cluster + 1; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6066 | |
| 6067 | ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1); |
| 6068 | |
Yongqiang Yang | 4740b83 | 2011-07-26 21:51:08 -0400 | [diff] [blame] | 6069 | if (count == 0) |
| 6070 | return 0; |
| 6071 | |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6072 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6073 | /* |
| 6074 | * Check to see if we are freeing blocks across a group |
| 6075 | * boundary. |
| 6076 | */ |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 6077 | if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) { |
| 6078 | ext4_warning(sb, "too many blocks added to group %u", |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 6079 | block_group); |
| 6080 | err = -EINVAL; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6081 | goto error_return; |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 6082 | } |
Theodore Ts'o | 2cd05cc | 2011-05-09 10:58:45 -0400 | [diff] [blame] | 6083 | |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6084 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
Darrick J. Wong | 9008a58 | 2015-10-17 21:33:24 -0400 | [diff] [blame] | 6085 | if (IS_ERR(bitmap_bh)) { |
| 6086 | err = PTR_ERR(bitmap_bh); |
| 6087 | bitmap_bh = NULL; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6088 | goto error_return; |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 6089 | } |
| 6090 | |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6091 | desc = ext4_get_group_desc(sb, block_group, &gd_bh); |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 6092 | if (!desc) { |
| 6093 | err = -EIO; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6094 | goto error_return; |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 6095 | } |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6096 | |
| 6097 | if (in_range(ext4_block_bitmap(sb, desc), block, count) || |
| 6098 | in_range(ext4_inode_bitmap(sb, desc), block, count) || |
| 6099 | in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) || |
| 6100 | in_range(block + count - 1, ext4_inode_table(sb, desc), |
| 6101 | sbi->s_itb_per_group)) { |
| 6102 | ext4_error(sb, "Adding blocks in system zones - " |
| 6103 | "Block = %llu, count = %lu", |
| 6104 | block, count); |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 6105 | err = -EINVAL; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6106 | goto error_return; |
| 6107 | } |
| 6108 | |
Theodore Ts'o | 2cd05cc | 2011-05-09 10:58:45 -0400 | [diff] [blame] | 6109 | BUFFER_TRACE(bitmap_bh, "getting write access"); |
| 6110 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6111 | if (err) |
| 6112 | goto error_return; |
| 6113 | |
| 6114 | /* |
| 6115 | * We are about to modify some metadata. Call the journal APIs |
| 6116 | * to unshare ->b_data if a currently-committing transaction is |
| 6117 | * using it |
| 6118 | */ |
| 6119 | BUFFER_TRACE(gd_bh, "get_write_access"); |
| 6120 | err = ext4_journal_get_write_access(handle, gd_bh); |
| 6121 | if (err) |
| 6122 | goto error_return; |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 6123 | |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 6124 | for (i = 0, clusters_freed = 0; i < cluster_count; i++) { |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6125 | BUFFER_TRACE(bitmap_bh, "clear bit"); |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 6126 | if (!mb_test_bit(bit + i, bitmap_bh->b_data)) { |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6127 | ext4_error(sb, "bit already cleared for block %llu", |
| 6128 | (ext4_fsblk_t)(block + i)); |
| 6129 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); |
| 6130 | } else { |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 6131 | clusters_freed++; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6132 | } |
| 6133 | } |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 6134 | |
| 6135 | err = ext4_mb_load_buddy(sb, block_group, &e4b); |
| 6136 | if (err) |
| 6137 | goto error_return; |
| 6138 | |
| 6139 | /* |
| 6140 | * need to update group_info->bb_free and bitmap |
| 6141 | * with group lock held. generate_buddy look at |
| 6142 | * them with group lock_held |
| 6143 | */ |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6144 | ext4_lock_group(sb, block_group); |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 6145 | mb_clear_bits(bitmap_bh->b_data, bit, cluster_count); |
| 6146 | mb_free_blocks(NULL, &e4b, bit, cluster_count); |
| 6147 | free_clusters_count = clusters_freed + |
| 6148 | ext4_free_group_clusters(sb, desc); |
| 6149 | ext4_free_group_clusters_set(sb, desc, free_clusters_count); |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 6150 | ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh); |
Darrick J. Wong | feb0ab3 | 2012-04-29 18:45:10 -0400 | [diff] [blame] | 6151 | ext4_group_desc_csum_set(sb, block_group, desc); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6152 | ext4_unlock_group(sb, block_group); |
Theodore Ts'o | 5704265 | 2011-09-09 18:56:51 -0400 | [diff] [blame] | 6153 | percpu_counter_add(&sbi->s_freeclusters_counter, |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 6154 | clusters_freed); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6155 | |
| 6156 | if (sbi->s_log_groups_per_flex) { |
| 6157 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); |
harshads | d77147f | 2017-10-29 09:38:46 -0400 | [diff] [blame] | 6158 | atomic64_add(clusters_freed, |
Suraj Jitindar Singh | 7c99072 | 2020-02-18 19:08:51 -0800 | [diff] [blame] | 6159 | &sbi_array_rcu_deref(sbi, s_flex_groups, |
| 6160 | flex_group)->free_clusters); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6161 | } |
Amir Goldstein | e73a347 | 2011-05-09 21:40:01 -0400 | [diff] [blame] | 6162 | |
| 6163 | ext4_mb_unload_buddy(&e4b); |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6164 | |
| 6165 | /* We dirtied the bitmap block */ |
| 6166 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
| 6167 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
| 6168 | |
| 6169 | /* And the group descriptor block */ |
| 6170 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
| 6171 | ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh); |
| 6172 | if (!err) |
| 6173 | err = ret; |
| 6174 | |
| 6175 | error_return: |
| 6176 | brelse(bitmap_bh); |
| 6177 | ext4_std_error(sb, err); |
Yongqiang Yang | cc7365d | 2011-07-26 21:46:07 -0400 | [diff] [blame] | 6178 | return err; |
Amir Goldstein | 2846e82 | 2011-05-09 10:46:41 -0400 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | /** |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6182 | * ext4_trim_extent -- function to TRIM one single free extent in the group |
| 6183 | * @sb: super block for the file system |
| 6184 | * @start: starting block of the free extent in the alloc. group |
| 6185 | * @count: number of blocks to TRIM |
| 6186 | * @group: alloc. group we are working with |
| 6187 | * @e4b: ext4 buddy for the group |
| 6188 | * |
| 6189 | * Trim "count" blocks starting at "start" in the "group". To assure that no |
| 6190 | * one will allocate those blocks, mark it as used in buddy bitmap. This must |
| 6191 | * be called with under the group lock. |
| 6192 | */ |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 6193 | 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] | 6194 | ext4_group_t group, struct ext4_buddy *e4b) |
jon ernst | e2cbd58 | 2014-04-12 23:01:28 -0400 | [diff] [blame] | 6195 | __releases(bitlock) |
| 6196 | __acquires(bitlock) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6197 | { |
| 6198 | struct ext4_free_extent ex; |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 6199 | int ret = 0; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6200 | |
Tao Ma | b3d4c2b | 2011-07-11 00:01:52 -0400 | [diff] [blame] | 6201 | trace_ext4_trim_extent(sb, group, start, count); |
| 6202 | |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6203 | assert_spin_locked(ext4_group_lock_ptr(sb, group)); |
| 6204 | |
| 6205 | ex.fe_start = start; |
| 6206 | ex.fe_group = group; |
| 6207 | ex.fe_len = count; |
| 6208 | |
| 6209 | /* |
| 6210 | * Mark blocks used, so no one can reuse them while |
| 6211 | * being trimmed. |
| 6212 | */ |
| 6213 | mb_mark_used(e4b, &ex); |
| 6214 | ext4_unlock_group(sb, group); |
Daeho Jeong | a015434 | 2017-06-22 23:54:33 -0400 | [diff] [blame] | 6215 | ret = ext4_issue_discard(sb, group, start, count, NULL); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6216 | ext4_lock_group(sb, group); |
| 6217 | mb_free_blocks(NULL, e4b, start, ex.fe_len); |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 6218 | return ret; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | /** |
| 6222 | * ext4_trim_all_free -- function to trim all free space in alloc. group |
| 6223 | * @sb: super block for file system |
Tao Ma | 2261228 | 2011-07-11 00:04:34 -0400 | [diff] [blame] | 6224 | * @group: group to be trimmed |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6225 | * @start: first group block to examine |
| 6226 | * @max: last group block to examine |
| 6227 | * @minblocks: minimum extent block count |
| 6228 | * |
| 6229 | * ext4_trim_all_free walks through group's buddy bitmap searching for free |
| 6230 | * extents. When the free block is found, ext4_trim_extent is called to TRIM |
| 6231 | * the extent. |
| 6232 | * |
| 6233 | * |
| 6234 | * ext4_trim_all_free walks through group's block bitmap searching for free |
| 6235 | * extents. When the free extent is found, mark it as used in group buddy |
| 6236 | * bitmap. Then issue a TRIM command on this extent and free the extent in |
| 6237 | * the group buddy bitmap. This is done until whole group is scanned. |
| 6238 | */ |
Lukas Czerner | 0b75a84 | 2011-02-23 12:22:49 -0500 | [diff] [blame] | 6239 | static ext4_grpblk_t |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6240 | ext4_trim_all_free(struct super_block *sb, ext4_group_t group, |
| 6241 | ext4_grpblk_t start, ext4_grpblk_t max, |
| 6242 | ext4_grpblk_t minblocks) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6243 | { |
| 6244 | void *bitmap; |
Tao Ma | 169ddc3 | 2011-07-11 00:00:07 -0400 | [diff] [blame] | 6245 | ext4_grpblk_t next, count = 0, free_count = 0; |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6246 | struct ext4_buddy e4b; |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 6247 | int ret = 0; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6248 | |
Tao Ma | b3d4c2b | 2011-07-11 00:01:52 -0400 | [diff] [blame] | 6249 | trace_ext4_trim_all_free(sb, group, start, max); |
| 6250 | |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6251 | ret = ext4_mb_load_buddy(sb, group, &e4b); |
| 6252 | if (ret) { |
Konstantin Khlebnikov | 9651e6b | 2017-05-21 22:35:23 -0400 | [diff] [blame] | 6253 | ext4_warning(sb, "Error %d loading buddy information for %u", |
| 6254 | ret, group); |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6255 | return ret; |
| 6256 | } |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6257 | bitmap = e4b.bd_bitmap; |
Lukas Czerner | 28739ee | 2011-05-24 18:28:07 -0400 | [diff] [blame] | 6258 | |
| 6259 | ext4_lock_group(sb, group); |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 6260 | if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) && |
| 6261 | minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks)) |
| 6262 | goto out; |
| 6263 | |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6264 | start = (e4b.bd_info->bb_first_free > start) ? |
| 6265 | e4b.bd_info->bb_first_free : start; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6266 | |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6267 | while (start <= max) { |
| 6268 | start = mb_find_next_zero_bit(bitmap, max + 1, start); |
| 6269 | if (start > max) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6270 | break; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6271 | next = mb_find_next_bit(bitmap, max + 1, start); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6272 | |
| 6273 | if ((next - start) >= minblocks) { |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 6274 | ret = ext4_trim_extent(sb, start, |
| 6275 | next - start, group, &e4b); |
| 6276 | if (ret && ret != -EOPNOTSUPP) |
| 6277 | break; |
| 6278 | ret = 0; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6279 | count += next - start; |
| 6280 | } |
Tao Ma | 169ddc3 | 2011-07-11 00:00:07 -0400 | [diff] [blame] | 6281 | free_count += next - start; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6282 | start = next + 1; |
| 6283 | |
| 6284 | if (fatal_signal_pending(current)) { |
| 6285 | count = -ERESTARTSYS; |
| 6286 | break; |
| 6287 | } |
| 6288 | |
| 6289 | if (need_resched()) { |
| 6290 | ext4_unlock_group(sb, group); |
| 6291 | cond_resched(); |
| 6292 | ext4_lock_group(sb, group); |
| 6293 | } |
| 6294 | |
Tao Ma | 169ddc3 | 2011-07-11 00:00:07 -0400 | [diff] [blame] | 6295 | if ((e4b.bd_info->bb_free - free_count) < minblocks) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6296 | break; |
| 6297 | } |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 6298 | |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 6299 | if (!ret) { |
| 6300 | ret = count; |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 6301 | EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info); |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 6302 | } |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 6303 | out: |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6304 | ext4_unlock_group(sb, group); |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6305 | ext4_mb_unload_buddy(&e4b); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6306 | |
| 6307 | ext4_debug("trimmed %d blocks in the group %d\n", |
| 6308 | count, group); |
| 6309 | |
Lukas Czerner | d71c1ae | 2012-11-08 14:04:52 -0500 | [diff] [blame] | 6310 | return ret; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6311 | } |
| 6312 | |
| 6313 | /** |
| 6314 | * ext4_trim_fs() -- trim ioctl handle function |
| 6315 | * @sb: superblock for filesystem |
| 6316 | * @range: fstrim_range structure |
| 6317 | * |
| 6318 | * start: First Byte to trim |
| 6319 | * len: number of Bytes to trim from start |
| 6320 | * minlen: minimum extent length in Bytes |
| 6321 | * ext4_trim_fs goes through all allocation groups containing Bytes from |
| 6322 | * start to start+len. For each such a group ext4_trim_all_free function |
| 6323 | * is invoked to trim all free space. |
| 6324 | */ |
| 6325 | int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) |
| 6326 | { |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6327 | struct ext4_group_info *grp; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6328 | ext4_group_t group, first_group, last_group; |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 6329 | ext4_grpblk_t cnt = 0, first_cluster, last_cluster; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6330 | uint64_t start, end, minlen, trimmed = 0; |
Jan Kara | 0f0a25b | 2011-01-11 15:16:31 -0500 | [diff] [blame] | 6331 | ext4_fsblk_t first_data_blk = |
| 6332 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6333 | 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] | 6334 | int ret = 0; |
| 6335 | |
| 6336 | start = range->start >> sb->s_blocksize_bits; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6337 | end = start + (range->len >> sb->s_blocksize_bits) - 1; |
Lukas Czerner | aaf7d73 | 2012-09-26 22:21:21 -0400 | [diff] [blame] | 6338 | minlen = EXT4_NUM_B2C(EXT4_SB(sb), |
| 6339 | range->minlen >> sb->s_blocksize_bits); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6340 | |
Lukas Czerner | 5de35e8 | 2012-10-22 18:01:19 -0400 | [diff] [blame] | 6341 | if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) || |
| 6342 | start >= max_blks || |
| 6343 | range->len < sb->s_blocksize) |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6344 | return -EINVAL; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6345 | if (end >= max_blks) |
| 6346 | end = max_blks - 1; |
| 6347 | if (end <= first_data_blk) |
Tao Ma | 22f1045 | 2011-07-10 23:52:37 -0400 | [diff] [blame] | 6348 | goto out; |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6349 | if (start < first_data_blk) |
Jan Kara | 0f0a25b | 2011-01-11 15:16:31 -0500 | [diff] [blame] | 6350 | start = first_data_blk; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6351 | |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6352 | /* Determine first and last group to examine based on start and end */ |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6353 | ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start, |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 6354 | &first_group, &first_cluster); |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6355 | ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end, |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 6356 | &last_group, &last_cluster); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6357 | |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6358 | /* end now represents the last cluster to discard in this group */ |
| 6359 | end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6360 | |
| 6361 | for (group = first_group; group <= last_group; group++) { |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6362 | grp = ext4_get_group_info(sb, group); |
| 6363 | /* We only do this if the grp has never been initialized */ |
| 6364 | if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { |
Konstantin Khlebnikov | adb7ef6 | 2016-03-13 17:29:06 -0400 | [diff] [blame] | 6365 | ret = ext4_mb_init_group(sb, group, GFP_NOFS); |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6366 | if (ret) |
| 6367 | break; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6368 | } |
| 6369 | |
Tao Ma | 0ba0851 | 2011-03-23 15:48:11 -0400 | [diff] [blame] | 6370 | /* |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6371 | * For all the groups except the last one, last cluster will |
| 6372 | * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to |
| 6373 | * change it for the last group, note that last_cluster is |
| 6374 | * already computed earlier by ext4_get_group_no_and_offset() |
Tao Ma | 0ba0851 | 2011-03-23 15:48:11 -0400 | [diff] [blame] | 6375 | */ |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6376 | if (group == last_group) |
| 6377 | end = last_cluster; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6378 | |
Lukas Czerner | 7894408 | 2011-05-24 18:16:27 -0400 | [diff] [blame] | 6379 | if (grp->bb_free >= minlen) { |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 6380 | cnt = ext4_trim_all_free(sb, group, first_cluster, |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6381 | end, minlen); |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6382 | if (cnt < 0) { |
| 6383 | ret = cnt; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6384 | break; |
| 6385 | } |
Lukas Czerner | 21e7fd2 | 2012-03-21 21:24:22 -0400 | [diff] [blame] | 6386 | trimmed += cnt; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6387 | } |
Lukas Czerner | 913eed83 | 2012-03-21 21:22:22 -0400 | [diff] [blame] | 6388 | |
| 6389 | /* |
| 6390 | * For every group except the first one, we are sure |
| 6391 | * that the first cluster to discard will be cluster #0. |
| 6392 | */ |
Theodore Ts'o | 7137d7a | 2011-09-09 18:38:51 -0400 | [diff] [blame] | 6393 | first_cluster = 0; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6394 | } |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6395 | |
Tao Ma | 3d56b8d | 2011-07-11 00:03:38 -0400 | [diff] [blame] | 6396 | if (!ret) |
| 6397 | atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen); |
| 6398 | |
Tao Ma | 22f1045 | 2011-07-10 23:52:37 -0400 | [diff] [blame] | 6399 | out: |
Lukas Czerner | aaf7d73 | 2012-09-26 22:21:21 -0400 | [diff] [blame] | 6400 | range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits; |
Lukas Czerner | 7360d17 | 2010-10-27 21:30:12 -0400 | [diff] [blame] | 6401 | return ret; |
| 6402 | } |
Darrick J. Wong | 0c9ec4b | 2017-04-30 00:36:53 -0400 | [diff] [blame] | 6403 | |
| 6404 | /* Iterate all the free extents in the group. */ |
| 6405 | int |
| 6406 | ext4_mballoc_query_range( |
| 6407 | struct super_block *sb, |
| 6408 | ext4_group_t group, |
| 6409 | ext4_grpblk_t start, |
| 6410 | ext4_grpblk_t end, |
| 6411 | ext4_mballoc_query_range_fn formatter, |
| 6412 | void *priv) |
| 6413 | { |
| 6414 | void *bitmap; |
| 6415 | ext4_grpblk_t next; |
| 6416 | struct ext4_buddy e4b; |
| 6417 | int error; |
| 6418 | |
| 6419 | error = ext4_mb_load_buddy(sb, group, &e4b); |
| 6420 | if (error) |
| 6421 | return error; |
| 6422 | bitmap = e4b.bd_bitmap; |
| 6423 | |
| 6424 | ext4_lock_group(sb, group); |
| 6425 | |
| 6426 | start = (e4b.bd_info->bb_first_free > start) ? |
| 6427 | e4b.bd_info->bb_first_free : start; |
| 6428 | if (end >= EXT4_CLUSTERS_PER_GROUP(sb)) |
| 6429 | end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; |
| 6430 | |
| 6431 | while (start <= end) { |
| 6432 | start = mb_find_next_zero_bit(bitmap, end + 1, start); |
| 6433 | if (start > end) |
| 6434 | break; |
| 6435 | next = mb_find_next_bit(bitmap, end + 1, start); |
| 6436 | |
| 6437 | ext4_unlock_group(sb, group); |
| 6438 | error = formatter(sb, group, start, next - start, priv); |
| 6439 | if (error) |
| 6440 | goto out_unload; |
| 6441 | ext4_lock_group(sb, group); |
| 6442 | |
| 6443 | start = next + 1; |
| 6444 | } |
| 6445 | |
| 6446 | ext4_unlock_group(sb, group); |
| 6447 | out_unload: |
| 6448 | ext4_mb_unload_buddy(&e4b); |
| 6449 | |
| 6450 | return error; |
| 6451 | } |