blob: 99bf091fee10eeb0f6dbc250389e664ba8d07633 [file] [log] [blame]
Theodore Ts'of5166762017-12-17 22:00:59 -05001// SPDX-License-Identifier: GPL-2.0
Alex Tomasc9de5602008-01-29 00:19:52 -05002/*
3 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
4 * Written by Alex Tomas <alex@clusterfs.com>
Alex Tomasc9de5602008-01-29 00:19:52 -05005 */
6
7
8/*
9 * mballoc.c contains the multiblocks allocation routines
10 */
11
Bobi Jam18aadd42012-02-20 17:53:02 -050012#include "ext4_jbd2.h"
Mingming Cao8f6e39a2008-04-29 22:01:31 -040013#include "mballoc.h"
Theodore Ts'o28623c22012-09-05 01:31:50 -040014#include <linux/log2.h>
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050015#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Jeremy Cline1a5d5e52018-08-02 00:03:40 -040017#include <linux/nospec.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040018#include <linux/backing-dev.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040019#include <trace/events/ext4.h>
20
Alex Tomasc9de5602008-01-29 00:19:52 -050021/*
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'ob713a5e2009-03-31 09:11:14 -040045 * 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 Tomasc9de5602008-01-29 00:19:52 -050054 *
55 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040056 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050057 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040058 * 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 Tomasc9de5602008-01-29 00:19:52 -050062 *
63 * pa_lstart -> the logical start block for this prealloc space
64 * pa_pstart -> the physical start block for this prealloc space
Theodore Ts'o53accfa2011-09-09 18:48:51 -040065 * pa_len -> length for this prealloc space (in clusters)
66 * pa_free -> free space available in this prealloc space (in clusters)
Alex Tomasc9de5602008-01-29 00:19:52 -050067 *
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 Macaaf7a22011-07-11 18:42:42 -040070 * space we will consume the particular prealloc space. This makes sure that
71 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050072 *
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 Macaaf7a22011-07-11 18:42:42 -040079 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050080 *
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 Marchi25985ed2011-03-30 22:57:33 -030087 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -050088 *
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.Vc3a326a2008-11-25 15:11:52 -0500100 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500101 *
102 *
103 * one block each for bitmap and buddy information. So for each group we
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300104 * take up 2 blocks. A page can contain blocks_per_page (PAGE_SIZE /
Alex Tomasc9de5602008-01-29 00:19:52 -0500105 * 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'o27baebb2011-09-09 19:02:51 -0400121 * 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 Tomasc9de5602008-01-29 00:19:52 -0500123 * 512 blocks. This can be tuned via
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400124 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500125 * 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 Dunlapb483bb72020-08-04 19:48:50 -0700127 * smallest multiple of the stripe value (sbi->s_stripe) which is
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400128 * greater than the default mb_group_prealloc.
Alex Tomasc9de5602008-01-29 00:19:52 -0500129 *
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400130 * The regular allocator (using the buddy cache) supports a few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500131 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400132 * /sys/fs/ext4/<partition>/mb_min_to_scan
133 * /sys/fs/ext4/<partition>/mb_max_to_scan
134 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500135 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400136 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500137 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
138 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400139 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200140 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400141 * stripe size. This should result in better allocation on RAID setups. If
142 * not, we search in the specific group using bitmap for best extents. The
143 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500144 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400145 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500146 * best extent in the found extents. Searching for the blocks starts with
147 * the group specified as the goal value in allocation context via
148 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400149 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500150 * checked.
151 *
152 * Both the prealloc space are getting populated as above. So for the first
153 * request we will hit the buddy cache which will result in this prealloc
154 * space getting filled. The prealloc space is then later used for the
155 * subsequent request.
156 */
157
158/*
159 * mballoc operates on the following data:
160 * - on-disk bitmap
161 * - in-core buddy (actually includes buddy and bitmap)
162 * - preallocation descriptors (PAs)
163 *
164 * there are two types of preallocations:
165 * - inode
166 * assiged to specific inode and can be used for this inode only.
167 * it describes part of inode's space preallocated to specific
168 * physical blocks. any block from that preallocated can be used
169 * independent. the descriptor just tracks number of blocks left
170 * unused. so, before taking some block from descriptor, one must
171 * make sure corresponded logical block isn't allocated yet. this
172 * also means that freeing any block within descriptor's range
173 * must discard all preallocated blocks.
174 * - locality group
175 * assigned to specific locality group which does not translate to
176 * permanent set of inodes: inode can join and leave group. space
177 * from this type of preallocation can be used for any inode. thus
178 * it's consumed from the beginning to the end.
179 *
180 * relation between them can be expressed as:
181 * in-core buddy = on-disk bitmap + preallocation descriptors
182 *
183 * this mean blocks mballoc considers used are:
184 * - allocated blocks (persistent)
185 * - preallocated blocks (non-persistent)
186 *
187 * consistency in mballoc world means that at any time a block is either
188 * free or used in ALL structures. notice: "any time" should not be read
189 * literally -- time is discrete and delimited by locks.
190 *
191 * to keep it simple, we don't use block numbers, instead we count number of
192 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
193 *
194 * all operations can be expressed as:
195 * - init buddy: buddy = on-disk + PAs
196 * - new PA: buddy += N; PA = N
197 * - use inode PA: on-disk += N; PA -= N
198 * - discard inode PA buddy -= on-disk - PA; PA = 0
199 * - use locality group PA on-disk += N; PA -= N
200 * - discard locality group PA buddy -= PA; PA = 0
201 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
202 * is used in real operation because we can't know actual used
203 * bits from PA, only from on-disk bitmap
204 *
205 * if we follow this strict logic, then all operations above should be atomic.
206 * given some of them can block, we'd have to use something like semaphores
207 * killing performance on high-end SMP hardware. let's try to relax it using
208 * the following knowledge:
209 * 1) if buddy is referenced, it's already initialized
210 * 2) while block is used in buddy and the buddy is referenced,
211 * nobody can re-allocate that block
212 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
213 * bit set and PA claims same block, it's OK. IOW, one can set bit in
214 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
215 * block
216 *
217 * so, now we're building a concurrency table:
218 * - init buddy vs.
219 * - new PA
220 * blocks for PA are allocated in the buddy, buddy must be referenced
221 * until PA is linked to allocation group to avoid concurrent buddy init
222 * - use inode PA
223 * we need to make sure that either on-disk bitmap or PA has uptodate data
224 * given (3) we care that PA-=N operation doesn't interfere with init
225 * - discard inode PA
226 * the simplest way would be to have buddy initialized by the discard
227 * - use locality group PA
228 * again PA-=N must be serialized with init
229 * - discard locality group PA
230 * the simplest way would be to have buddy initialized by the discard
231 * - new PA vs.
232 * - use inode PA
233 * i_data_sem serializes them
234 * - discard inode PA
235 * discard process must wait until PA isn't used by another process
236 * - use locality group PA
237 * some mutex should serialize them
238 * - discard locality group PA
239 * discard process must wait until PA isn't used by another process
240 * - use inode PA
241 * - use inode PA
242 * i_data_sem or another mutex should serializes them
243 * - discard inode PA
244 * discard process must wait until PA isn't used by another process
245 * - use locality group PA
246 * nothing wrong here -- they're different PAs covering different blocks
247 * - discard locality group PA
248 * discard process must wait until PA isn't used by another process
249 *
250 * now we're ready to make few consequences:
251 * - PA is referenced and while it is no discard is possible
252 * - PA is referenced until block isn't marked in on-disk bitmap
253 * - PA changes only after on-disk bitmap
254 * - discard must not compete with init. either init is done before
255 * any discard or they're serialized somehow
256 * - buddy init as sum of on-disk bitmap and PAs is done atomically
257 *
258 * a special case when we've used PA to emptiness. no need to modify buddy
259 * in this case, but we should care about concurrent init
260 *
261 */
262
263 /*
264 * Logic in few words:
265 *
266 * - allocation:
267 * load group
268 * find blocks
269 * mark bits in on-disk bitmap
270 * release group
271 *
272 * - use preallocation:
273 * find proper PA (per-inode or group)
274 * load group
275 * mark bits in on-disk bitmap
276 * release group
277 * release PA
278 *
279 * - free:
280 * load group
281 * mark bits in on-disk bitmap
282 * release group
283 *
284 * - discard preallocations in group:
285 * mark PAs deleted
286 * move them onto local list
287 * load on-disk bitmap
288 * load group
289 * remove PA from object (inode or locality group)
290 * mark free blocks in-core
291 *
292 * - discard inode's preallocations:
293 */
294
295/*
296 * Locking rules
297 *
298 * Locks:
299 * - bitlock on a group (group)
300 * - object (inode/locality) (object)
301 * - per-pa lock (pa)
302 *
303 * Paths:
304 * - new pa
305 * object
306 * group
307 *
308 * - find and use pa:
309 * pa
310 *
311 * - release consumed pa:
312 * pa
313 * group
314 * object
315 *
316 * - generate in-core bitmap:
317 * group
318 * pa
319 *
320 * - discard all for given object (inode, locality group):
321 * object
322 * pa
323 * group
324 *
325 * - discard all for given group:
326 * group
327 * pa
328 * group
329 * object
330 *
331 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500332static struct kmem_cache *ext4_pspace_cachep;
333static struct kmem_cache *ext4_ac_cachep;
Bobi Jam18aadd42012-02-20 17:53:02 -0500334static struct kmem_cache *ext4_free_data_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400335
336/* We create slab caches for groupinfo data structures based on the
337 * superblock block size. There will be one per mounted filesystem for
338 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500339#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400340static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
341
Eric Biggersd6006182017-04-29 23:47:50 -0400342static const char * const ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
Eric Sandeen2892c152011-02-12 08:12:18 -0500343 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
344 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
345 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
346};
347
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500348static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
349 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500350static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
351 ext4_group_t group);
Ritesh Harjani53f86b12020-05-20 12:10:32 +0530352static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500353
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +0530354/*
355 * The algorithm using this percpu seq counter goes below:
356 * 1. We sample the percpu discard_pa_seq counter before trying for block
357 * allocation in ext4_mb_new_blocks().
358 * 2. We increment this percpu discard_pa_seq counter when we either allocate
359 * or free these blocks i.e. while marking those blocks as used/free in
360 * mb_mark_used()/mb_free_blocks().
361 * 3. We also increment this percpu seq counter when we successfully identify
362 * that the bb_prealloc_list is not empty and hence proceed for discarding
363 * of those PAs inside ext4_mb_discard_group_preallocations().
364 *
365 * Now to make sure that the regular fast path of block allocation is not
366 * affected, as a small optimization we only sample the percpu seq counter
367 * on that cpu. Only when the block allocation fails and when freed blocks
368 * found were 0, that is when we sample percpu seq counter for all cpus using
369 * below function ext4_get_discard_pa_seq_sum(). This happens after making
370 * sure that all the PAs on grp->bb_prealloc_list got freed or if it's empty.
371 */
372static DEFINE_PER_CPU(u64, discard_pa_seq);
373static inline u64 ext4_get_discard_pa_seq_sum(void)
374{
375 int __cpu;
376 u64 __seq = 0;
377
378 for_each_possible_cpu(__cpu)
379 __seq += per_cpu(discard_pa_seq, __cpu);
380 return __seq;
381}
382
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500383static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
384{
Alex Tomasc9de5602008-01-29 00:19:52 -0500385#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500386 *bit += ((unsigned long) addr & 7UL) << 3;
387 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500388#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500389 *bit += ((unsigned long) addr & 3UL) << 3;
390 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500391#else
392#error "how many bits you are?!"
393#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500394 return addr;
395}
Alex Tomasc9de5602008-01-29 00:19:52 -0500396
397static inline int mb_test_bit(int bit, void *addr)
398{
399 /*
400 * ext4_test_bit on architecture like powerpc
401 * needs unsigned long aligned address
402 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500403 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500404 return ext4_test_bit(bit, addr);
405}
406
407static inline void mb_set_bit(int bit, void *addr)
408{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500409 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500410 ext4_set_bit(bit, addr);
411}
412
Alex Tomasc9de5602008-01-29 00:19:52 -0500413static inline void mb_clear_bit(int bit, void *addr)
414{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500415 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500416 ext4_clear_bit(bit, addr);
417}
418
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400419static inline int mb_test_and_clear_bit(int bit, void *addr)
420{
421 addr = mb_correct_addr_and_bit(&bit, addr);
422 return ext4_test_and_clear_bit(bit, addr);
423}
424
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500425static inline int mb_find_next_zero_bit(void *addr, int max, int start)
426{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400427 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500428 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400429 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500430 start += fix;
431
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400432 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
433 if (ret > max)
434 return max;
435 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500436}
437
438static inline int mb_find_next_bit(void *addr, int max, int start)
439{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400440 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500441 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400442 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500443 start += fix;
444
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400445 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
446 if (ret > max)
447 return max;
448 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500449}
450
Alex Tomasc9de5602008-01-29 00:19:52 -0500451static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
452{
453 char *bb;
454
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500455 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -0500456 BUG_ON(max == NULL);
457
458 if (order > e4b->bd_blkbits + 1) {
459 *max = 0;
460 return NULL;
461 }
462
463 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500464 if (order == 0) {
465 *max = 1 << (e4b->bd_blkbits + 3);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500466 return e4b->bd_bitmap;
Coly Li84b775a2011-02-24 12:51:59 -0500467 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500468
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500469 bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
Alex Tomasc9de5602008-01-29 00:19:52 -0500470 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
471
472 return bb;
473}
474
475#ifdef DOUBLE_CHECK
476static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
477 int first, int count)
478{
479 int i;
480 struct super_block *sb = e4b->bd_sb;
481
482 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
483 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400484 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500485 for (i = 0; i < count; i++) {
486 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
487 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500488
489 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400490 blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500491 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400492 inode ? inode->i_ino : 0,
493 blocknr,
494 "freeing block already freed "
495 "(bit %u)",
496 first + i);
Wang Shilong736dedb2018-05-12 12:37:58 -0400497 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
498 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Alex Tomasc9de5602008-01-29 00:19:52 -0500499 }
500 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
501 }
502}
503
504static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
505{
506 int i;
507
508 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
509 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400510 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500511 for (i = 0; i < count; i++) {
512 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
513 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
514 }
515}
516
517static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
518{
Ritesh Harjanieb2b8eb2020-05-10 11:54:49 +0530519 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
520 return;
Alex Tomasc9de5602008-01-29 00:19:52 -0500521 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
522 unsigned char *b1, *b2;
523 int i;
524 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
525 b2 = (unsigned char *) bitmap;
526 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
527 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400528 ext4_msg(e4b->bd_sb, KERN_ERR,
529 "corruption in group %u "
530 "at byte %u(%u): %x in copy != %x "
531 "on disk/prealloc",
532 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500533 BUG();
534 }
535 }
536 }
537}
538
Ritesh Harjania3450212020-05-10 11:54:48 +0530539static void mb_group_bb_bitmap_alloc(struct super_block *sb,
540 struct ext4_group_info *grp, ext4_group_t group)
541{
542 struct buffer_head *bh;
543
544 grp->bb_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
Ritesh Harjanieb2b8eb2020-05-10 11:54:49 +0530545 if (!grp->bb_bitmap)
546 return;
Ritesh Harjania3450212020-05-10 11:54:48 +0530547
548 bh = ext4_read_block_bitmap(sb, group);
Ritesh Harjanieb2b8eb2020-05-10 11:54:49 +0530549 if (IS_ERR_OR_NULL(bh)) {
550 kfree(grp->bb_bitmap);
551 grp->bb_bitmap = NULL;
552 return;
553 }
Ritesh Harjania3450212020-05-10 11:54:48 +0530554
555 memcpy(grp->bb_bitmap, bh->b_data, sb->s_blocksize);
556 put_bh(bh);
557}
558
559static void mb_group_bb_bitmap_free(struct ext4_group_info *grp)
560{
561 kfree(grp->bb_bitmap);
562}
563
Alex Tomasc9de5602008-01-29 00:19:52 -0500564#else
565static inline void mb_free_blocks_double(struct inode *inode,
566 struct ext4_buddy *e4b, int first, int count)
567{
568 return;
569}
570static inline void mb_mark_used_double(struct ext4_buddy *e4b,
571 int first, int count)
572{
573 return;
574}
575static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
576{
577 return;
578}
Ritesh Harjania3450212020-05-10 11:54:48 +0530579
580static inline void mb_group_bb_bitmap_alloc(struct super_block *sb,
581 struct ext4_group_info *grp, ext4_group_t group)
582{
583 return;
584}
585
586static inline void mb_group_bb_bitmap_free(struct ext4_group_info *grp)
587{
588 return;
589}
Alex Tomasc9de5602008-01-29 00:19:52 -0500590#endif
591
592#ifdef AGGRESSIVE_CHECK
593
594#define MB_CHECK_ASSERT(assert) \
595do { \
596 if (!(assert)) { \
597 printk(KERN_EMERG \
598 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
599 function, file, line, # assert); \
600 BUG(); \
601 } \
602} while (0)
603
604static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
605 const char *function, int line)
606{
607 struct super_block *sb = e4b->bd_sb;
608 int order = e4b->bd_blkbits + 1;
609 int max;
610 int max2;
611 int i;
612 int j;
613 int k;
614 int count;
615 struct ext4_group_info *grp;
616 int fragments = 0;
617 int fstart;
618 struct list_head *cur;
619 void *buddy;
620 void *buddy2;
621
Chunguang Xuaddd7522020-09-28 19:36:35 +0800622 if (e4b->bd_info->bb_check_counter++ % 10)
623 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500624
625 while (order > 1) {
626 buddy = mb_find_buddy(e4b, order, &max);
627 MB_CHECK_ASSERT(buddy);
628 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
629 MB_CHECK_ASSERT(buddy2);
630 MB_CHECK_ASSERT(buddy != buddy2);
631 MB_CHECK_ASSERT(max * 2 == max2);
632
633 count = 0;
634 for (i = 0; i < max; i++) {
635
636 if (mb_test_bit(i, buddy)) {
637 /* only single bit in buddy2 may be 1 */
638 if (!mb_test_bit(i << 1, buddy2)) {
639 MB_CHECK_ASSERT(
640 mb_test_bit((i<<1)+1, buddy2));
641 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
642 MB_CHECK_ASSERT(
643 mb_test_bit(i << 1, buddy2));
644 }
645 continue;
646 }
647
Robin Dong0a10da72011-10-26 08:48:54 -0400648 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500649 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
650 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
651
652 for (j = 0; j < (1 << order); j++) {
653 k = (i * (1 << order)) + j;
654 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500655 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500656 }
657 count++;
658 }
659 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
660 order--;
661 }
662
663 fstart = -1;
664 buddy = mb_find_buddy(e4b, 0, &max);
665 for (i = 0; i < max; i++) {
666 if (!mb_test_bit(i, buddy)) {
667 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
668 if (fstart == -1) {
669 fragments++;
670 fstart = i;
671 }
672 continue;
673 }
674 fstart = -1;
675 /* check used bits only */
676 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
677 buddy2 = mb_find_buddy(e4b, j, &max2);
678 k = i >> j;
679 MB_CHECK_ASSERT(k < max2);
680 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
681 }
682 }
683 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
684 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
685
686 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500687 list_for_each(cur, &grp->bb_prealloc_list) {
688 ext4_group_t groupnr;
689 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400690 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
691 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500692 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400693 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500694 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
695 }
696 return 0;
697}
698#undef MB_CHECK_ASSERT
699#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400700 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500701#else
702#define mb_check_buddy(e4b)
703#endif
704
Coly Li7c786052011-02-24 13:24:25 -0500705/*
706 * Divide blocks started from @first with length @len into
707 * smaller chunks with power of 2 blocks.
708 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
709 * then increase bb_counters[] for corresponded chunk size.
710 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500711static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400712 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500713 struct ext4_group_info *grp)
714{
715 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400716 ext4_grpblk_t min;
717 ext4_grpblk_t max;
718 ext4_grpblk_t chunk;
Chandan Rajendra69e43e82016-11-14 21:04:37 -0500719 unsigned int border;
Alex Tomasc9de5602008-01-29 00:19:52 -0500720
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400721 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500722
723 border = 2 << sb->s_blocksize_bits;
724
725 while (len > 0) {
726 /* find how many blocks can be covered since this position */
727 max = ffs(first | border) - 1;
728
729 /* find how many blocks of power 2 we need to mark */
730 min = fls(len) - 1;
731
732 if (max < min)
733 min = max;
734 chunk = 1 << min;
735
736 /* mark multiblock chunks only */
737 grp->bb_counters[min]++;
738 if (min > 0)
739 mb_clear_bit(first >> min,
740 buddy + sbi->s_mb_offsets[min]);
741
742 len -= chunk;
743 first += chunk;
744 }
745}
746
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400747/*
748 * Cache the order of the largest free extent we have available in this block
749 * group.
750 */
751static void
752mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
753{
754 int i;
755 int bits;
756
757 grp->bb_largest_free_order = -1; /* uninit */
758
759 bits = sb->s_blocksize_bits + 1;
760 for (i = bits; i >= 0; i--) {
761 if (grp->bb_counters[i] > 0) {
762 grp->bb_largest_free_order = i;
763 break;
764 }
765 }
766}
767
Eric Sandeen089ceec2009-07-05 22:17:31 -0400768static noinline_for_stack
769void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500770 void *buddy, void *bitmap, ext4_group_t group)
771{
772 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400773 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400774 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400775 ext4_grpblk_t i = 0;
776 ext4_grpblk_t first;
777 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500778 unsigned free = 0;
779 unsigned fragments = 0;
780 unsigned long long period = get_cycles();
781
782 /* initialize buddy from bitmap which is aggregation
783 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500784 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500785 grp->bb_first_free = i;
786 while (i < max) {
787 fragments++;
788 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500789 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500790 len = i - first;
791 free += len;
792 if (len > 1)
793 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
794 else
795 grp->bb_counters[0]++;
796 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500797 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500798 }
799 grp->bb_fragments = fragments;
800
801 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400802 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o94d4c062014-07-05 19:15:50 -0400803 "block bitmap and bg descriptor "
804 "inconsistent: %u vs %u free clusters",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400805 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500806 /*
Darrick J. Wong163a2032013-08-28 17:35:51 -0400807 * If we intend to continue, we consider group descriptor
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500808 * corrupt and update bb_free using bitmap value
809 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500810 grp->bb_free = free;
Wang Shilongdb79e6d2018-05-12 11:39:40 -0400811 ext4_mark_group_bitmap_corrupted(sb, group,
812 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Alex Tomasc9de5602008-01-29 00:19:52 -0500813 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400814 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500815
816 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
817
818 period = get_cycles() - period;
Jun Piao49598e02018-01-11 13:17:49 -0500819 spin_lock(&sbi->s_bal_lock);
820 sbi->s_mb_buddies_generated++;
821 sbi->s_mb_generation_time += period;
822 spin_unlock(&sbi->s_bal_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -0500823}
824
825/* The buddy information is attached the buddy cache inode
826 * for convenience. The information regarding each group
827 * is loaded via ext4_mb_load_buddy. The information involve
828 * block bitmap and buddy information. The information are
829 * stored in the inode as
830 *
831 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500832 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500833 *
834 *
835 * one block each for bitmap and buddy information.
836 * So for each group we take up 2 blocks. A page can
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300837 * contain blocks_per_page (PAGE_SIZE / blocksize) blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -0500838 * So it can have information regarding groups_per_page which
839 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400840 *
841 * Locking note: This routine takes the block group lock of all groups
842 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500843 */
844
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400845static int ext4_mb_init_cache(struct page *page, char *incore, gfp_t gfp)
Alex Tomasc9de5602008-01-29 00:19:52 -0500846{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400847 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500848 int blocksize;
849 int blocks_per_page;
850 int groups_per_page;
851 int err = 0;
852 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500853 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500854 int first_block;
855 struct super_block *sb;
856 struct buffer_head *bhs;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -0400857 struct buffer_head **bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500858 struct inode *inode;
859 char *data;
860 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400861 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500862
Alex Tomasc9de5602008-01-29 00:19:52 -0500863 inode = page->mapping->host;
864 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400865 ngroups = ext4_get_groups_count(sb);
Fabian Frederick93407472017-02-27 14:28:32 -0800866 blocksize = i_blocksize(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300867 blocks_per_page = PAGE_SIZE / blocksize;
Alex Tomasc9de5602008-01-29 00:19:52 -0500868
Ritesh Harjanid3df1452020-05-10 11:54:54 +0530869 mb_debug(sb, "init page %lu\n", page->index);
870
Alex Tomasc9de5602008-01-29 00:19:52 -0500871 groups_per_page = blocks_per_page >> 1;
872 if (groups_per_page == 0)
873 groups_per_page = 1;
874
875 /* allocate buffer_heads to read bitmaps */
876 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500877 i = sizeof(struct buffer_head *) * groups_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400878 bh = kzalloc(i, gfp);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500879 if (bh == NULL) {
880 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500881 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500882 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500883 } else
884 bh = &bhs;
885
886 first_group = page->index * blocks_per_page / 2;
887
888 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500889 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
890 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500891 break;
892
Theodore Ts'o813e5722012-02-20 17:52:46 -0500893 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400894 /*
895 * If page is uptodate then we came here after online resize
896 * which added some new uninitialized group info structs, so
897 * we must skip all initialized uptodate buddies on the page,
898 * which may be currently in use by an allocating task.
899 */
900 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
901 bh[i] = NULL;
902 continue;
903 }
Alex Zhuravlevcfd73232020-04-21 10:54:07 +0300904 bh[i] = ext4_read_block_bitmap_nowait(sb, group, false);
Darrick J. Wong9008a582015-10-17 21:33:24 -0400905 if (IS_ERR(bh[i])) {
906 err = PTR_ERR(bh[i]);
907 bh[i] = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500908 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500909 }
Ritesh Harjanid3df1452020-05-10 11:54:54 +0530910 mb_debug(sb, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500911 }
912
913 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500914 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
Darrick J. Wong9008a582015-10-17 21:33:24 -0400915 int err2;
916
917 if (!bh[i])
918 continue;
919 err2 = ext4_wait_block_bitmap(sb, group, bh[i]);
920 if (!err)
921 err = err2;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500922 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500923
924 first_block = page->index * blocks_per_page;
925 for (i = 0; i < blocks_per_page; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500926 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400927 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500928 break;
929
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400930 if (!bh[group - first_group])
931 /* skip initialized uptodate buddy */
932 continue;
933
Lukas Czernerbbdc3222015-06-08 11:38:37 -0400934 if (!buffer_verified(bh[group - first_group]))
935 /* Skip faulty bitmaps */
936 continue;
937 err = 0;
938
Alex Tomasc9de5602008-01-29 00:19:52 -0500939 /*
940 * data carry information regarding this
941 * particular group in the format specified
942 * above
943 *
944 */
945 data = page_address(page) + (i * blocksize);
946 bitmap = bh[group - first_group]->b_data;
947
948 /*
949 * We place the buddy block and bitmap block
950 * close together
951 */
952 if ((first_block + i) & 1) {
953 /* this is block of buddy */
954 BUG_ON(incore == NULL);
Ritesh Harjanid3df1452020-05-10 11:54:54 +0530955 mb_debug(sb, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500956 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400957 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500958 grinfo = ext4_get_group_info(sb, group);
959 grinfo->bb_fragments = 0;
960 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400961 sizeof(*grinfo->bb_counters) *
962 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500963 /*
964 * incore got set to the group block bitmap below
965 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500966 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400967 /* init the buddy */
968 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500969 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500970 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500971 incore = NULL;
972 } else {
973 /* this is block of bitmap */
974 BUG_ON(incore != NULL);
Ritesh Harjanid3df1452020-05-10 11:54:54 +0530975 mb_debug(sb, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500976 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400977 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500978
979 /* see comments in ext4_mb_put_pa() */
980 ext4_lock_group(sb, group);
981 memcpy(data, bitmap, blocksize);
982
983 /* mark all preallocated blks used in in-core bitmap */
984 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500985 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500986 ext4_unlock_group(sb, group);
987
988 /* set incore so that the buddy information can be
989 * generated using this
990 */
991 incore = data;
992 }
993 }
994 SetPageUptodate(page);
995
996out:
997 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400998 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500999 brelse(bh[i]);
1000 if (bh != &bhs)
1001 kfree(bh);
1002 }
1003 return err;
1004}
1005
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001006/*
Amir Goldstein2de88072011-05-09 21:48:13 -04001007 * Lock the buddy and bitmap pages. This make sure other parallel init_group
1008 * on the same buddy page doesn't happen whild holding the buddy page lock.
1009 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
1010 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001011 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001012static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001013 ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001014{
Amir Goldstein2de88072011-05-09 21:48:13 -04001015 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
1016 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001017 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -04001018 struct page *page;
1019
1020 e4b->bd_buddy_page = NULL;
1021 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001022
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001023 blocks_per_page = PAGE_SIZE / sb->s_blocksize;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001024 /*
1025 * the buddy cache inode stores the block bitmap
1026 * and buddy information in consecutive blocks.
1027 * So for each group we need two blocks.
1028 */
1029 block = group * 2;
1030 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -04001031 poff = block % blocks_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001032 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001033 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001034 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001035 BUG_ON(page->mapping != inode->i_mapping);
1036 e4b->bd_bitmap_page = page;
1037 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001038
Amir Goldstein2de88072011-05-09 21:48:13 -04001039 if (blocks_per_page >= 2) {
1040 /* buddy and bitmap are on the same page */
1041 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001042 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001043
1044 block++;
1045 pnum = block / blocks_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001046 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001047 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001048 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001049 BUG_ON(page->mapping != inode->i_mapping);
1050 e4b->bd_buddy_page = page;
1051 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001052}
1053
Amir Goldstein2de88072011-05-09 21:48:13 -04001054static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001055{
Amir Goldstein2de88072011-05-09 21:48:13 -04001056 if (e4b->bd_bitmap_page) {
1057 unlock_page(e4b->bd_bitmap_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001058 put_page(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001059 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001060 if (e4b->bd_buddy_page) {
1061 unlock_page(e4b->bd_buddy_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001062 put_page(e4b->bd_buddy_page);
Amir Goldstein2de88072011-05-09 21:48:13 -04001063 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001064}
1065
1066/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001067 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1068 * block group lock of all groups for this page; do not hold the BG lock when
1069 * calling this routine!
1070 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001071static noinline_for_stack
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001072int ext4_mb_init_group(struct super_block *sb, ext4_group_t group, gfp_t gfp)
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001073{
1074
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001075 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001076 struct ext4_buddy e4b;
1077 struct page *page;
1078 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001079
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001080 might_sleep();
Ritesh Harjanid3df1452020-05-10 11:54:54 +05301081 mb_debug(sb, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001082 this_grp = ext4_get_group_info(sb, group);
1083 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001084 * This ensures that we don't reinit the buddy cache
1085 * page which map to the group from which we are already
1086 * allocating. If we are looking at the buddy cache we would
1087 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001088 * would have pinned buddy page to page cache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001089 * The call to ext4_mb_get_buddy_page_lock will mark the
1090 * page accessed.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001091 */
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001092 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001093 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001094 /*
1095 * somebody initialized the group
1096 * return without doing anything
1097 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001098 goto err;
1099 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001100
1101 page = e4b.bd_bitmap_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001102 ret = ext4_mb_init_cache(page, NULL, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001103 if (ret)
1104 goto err;
1105 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001106 ret = -EIO;
1107 goto err;
1108 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001109
Amir Goldstein2de88072011-05-09 21:48:13 -04001110 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001111 /*
1112 * If both the bitmap and buddy are in
1113 * the same page we don't need to force
1114 * init the buddy
1115 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001116 ret = 0;
1117 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001118 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001119 /* init buddy cache */
1120 page = e4b.bd_buddy_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001121 ret = ext4_mb_init_cache(page, e4b.bd_bitmap, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001122 if (ret)
1123 goto err;
1124 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001125 ret = -EIO;
1126 goto err;
1127 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001128err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001129 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001130 return ret;
1131}
1132
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001133/*
1134 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1135 * block group lock of all groups for this page; do not hold the BG lock when
1136 * calling this routine!
1137 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001138static noinline_for_stack int
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001139ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group,
1140 struct ext4_buddy *e4b, gfp_t gfp)
Alex Tomasc9de5602008-01-29 00:19:52 -05001141{
Alex Tomasc9de5602008-01-29 00:19:52 -05001142 int blocks_per_page;
1143 int block;
1144 int pnum;
1145 int poff;
1146 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001147 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001148 struct ext4_group_info *grp;
1149 struct ext4_sb_info *sbi = EXT4_SB(sb);
1150 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001151
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001152 might_sleep();
Ritesh Harjanid3df1452020-05-10 11:54:54 +05301153 mb_debug(sb, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001154
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001155 blocks_per_page = PAGE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001156 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001157
1158 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001159 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001160 e4b->bd_sb = sb;
1161 e4b->bd_group = group;
1162 e4b->bd_buddy_page = NULL;
1163 e4b->bd_bitmap_page = NULL;
1164
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001165 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001166 /*
1167 * we need full data about the group
1168 * to make a good selection
1169 */
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001170 ret = ext4_mb_init_group(sb, group, gfp);
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001171 if (ret)
1172 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001173 }
1174
Alex Tomasc9de5602008-01-29 00:19:52 -05001175 /*
1176 * the buddy cache inode stores the block bitmap
1177 * and buddy information in consecutive blocks.
1178 * So for each group we need two blocks.
1179 */
1180 block = group * 2;
1181 pnum = block / blocks_per_page;
1182 poff = block % blocks_per_page;
1183
1184 /* we could use find_or_create_page(), but it locks page
1185 * what we'd like to avoid in fast path ... */
Mel Gorman2457aec2014-06-04 16:10:31 -07001186 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001187 if (page == NULL || !PageUptodate(page)) {
1188 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001189 /*
1190 * drop the page reference and try
1191 * to get the page with lock. If we
1192 * are not uptodate that implies
1193 * somebody just created the page but
1194 * is yet to initialize the same. So
1195 * wait for it to initialize.
1196 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001197 put_page(page);
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001198 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001199 if (page) {
1200 BUG_ON(page->mapping != inode->i_mapping);
1201 if (!PageUptodate(page)) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001202 ret = ext4_mb_init_cache(page, NULL, gfp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001203 if (ret) {
1204 unlock_page(page);
1205 goto err;
1206 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001207 mb_cmp_bitmaps(e4b, page_address(page) +
1208 (poff * sb->s_blocksize));
1209 }
1210 unlock_page(page);
1211 }
1212 }
Younger Liuc57ab392014-04-10 23:03:43 -04001213 if (page == NULL) {
1214 ret = -ENOMEM;
1215 goto err;
1216 }
1217 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001218 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001219 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001220 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001221
1222 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001223 e4b->bd_bitmap_page = page;
1224 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001225
1226 block++;
1227 pnum = block / blocks_per_page;
1228 poff = block % blocks_per_page;
1229
Mel Gorman2457aec2014-06-04 16:10:31 -07001230 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001231 if (page == NULL || !PageUptodate(page)) {
1232 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001233 put_page(page);
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001234 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001235 if (page) {
1236 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001237 if (!PageUptodate(page)) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001238 ret = ext4_mb_init_cache(page, e4b->bd_bitmap,
1239 gfp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001240 if (ret) {
1241 unlock_page(page);
1242 goto err;
1243 }
1244 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001245 unlock_page(page);
1246 }
1247 }
Younger Liuc57ab392014-04-10 23:03:43 -04001248 if (page == NULL) {
1249 ret = -ENOMEM;
1250 goto err;
1251 }
1252 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001253 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001254 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001255 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001256
1257 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001258 e4b->bd_buddy_page = page;
1259 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001260
Alex Tomasc9de5602008-01-29 00:19:52 -05001261 return 0;
1262
1263err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001264 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001265 put_page(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001266 if (e4b->bd_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001267 put_page(e4b->bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001268 if (e4b->bd_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001269 put_page(e4b->bd_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001270 e4b->bd_buddy = NULL;
1271 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001272 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001273}
1274
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001275static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1276 struct ext4_buddy *e4b)
1277{
1278 return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS);
1279}
1280
Jing Zhange39e07f2010-05-14 00:00:00 -04001281static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001282{
1283 if (e4b->bd_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001284 put_page(e4b->bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001285 if (e4b->bd_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001286 put_page(e4b->bd_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001287}
1288
1289
1290static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1291{
Chunguang Xuce3cca32020-11-07 23:58:13 +08001292 int order = 1, max;
Alex Tomasc9de5602008-01-29 00:19:52 -05001293 void *bb;
1294
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001295 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -05001296 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1297
Alex Tomasc9de5602008-01-29 00:19:52 -05001298 while (order <= e4b->bd_blkbits + 1) {
Chunguang Xuce3cca32020-11-07 23:58:13 +08001299 bb = mb_find_buddy(e4b, order, &max);
1300 if (!mb_test_bit(block >> order, bb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001301 /* this block is part of buddy of order 'order' */
1302 return order;
1303 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001304 order++;
1305 }
1306 return 0;
1307}
1308
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001309static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001310{
1311 __u32 *addr;
1312
1313 len = cur + len;
1314 while (cur < len) {
1315 if ((cur & 31) == 0 && (len - cur) >= 32) {
1316 /* fast path: clear whole word at once */
1317 addr = bm + (cur >> 3);
1318 *addr = 0;
1319 cur += 32;
1320 continue;
1321 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001322 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001323 cur++;
1324 }
1325}
1326
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001327/* clear bits in given range
1328 * will return first found zero bit if any, -1 otherwise
1329 */
1330static int mb_test_and_clear_bits(void *bm, int cur, int len)
1331{
1332 __u32 *addr;
1333 int zero_bit = -1;
1334
1335 len = cur + len;
1336 while (cur < len) {
1337 if ((cur & 31) == 0 && (len - cur) >= 32) {
1338 /* fast path: clear whole word at once */
1339 addr = bm + (cur >> 3);
1340 if (*addr != (__u32)(-1) && zero_bit == -1)
1341 zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0);
1342 *addr = 0;
1343 cur += 32;
1344 continue;
1345 }
1346 if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1)
1347 zero_bit = cur;
1348 cur++;
1349 }
1350
1351 return zero_bit;
1352}
1353
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001354void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001355{
1356 __u32 *addr;
1357
1358 len = cur + len;
1359 while (cur < len) {
1360 if ((cur & 31) == 0 && (len - cur) >= 32) {
1361 /* fast path: set whole word at once */
1362 addr = bm + (cur >> 3);
1363 *addr = 0xffffffff;
1364 cur += 32;
1365 continue;
1366 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001367 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001368 cur++;
1369 }
1370}
1371
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001372static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side)
Alex Tomasc9de5602008-01-29 00:19:52 -05001373{
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001374 if (mb_test_bit(*bit + side, bitmap)) {
1375 mb_clear_bit(*bit, bitmap);
1376 (*bit) -= side;
1377 return 1;
1378 }
1379 else {
1380 (*bit) += side;
1381 mb_set_bit(*bit, bitmap);
1382 return -1;
1383 }
1384}
1385
1386static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last)
1387{
1388 int max;
1389 int order = 1;
1390 void *buddy = mb_find_buddy(e4b, order, &max);
1391
1392 while (buddy) {
1393 void *buddy2;
1394
1395 /* Bits in range [first; last] are known to be set since
1396 * corresponding blocks were allocated. Bits in range
1397 * (first; last) will stay set because they form buddies on
1398 * upper layer. We just deal with borders if they don't
1399 * align with upper layer and then go up.
1400 * Releasing entire group is all about clearing
1401 * single bit of highest order buddy.
1402 */
1403
1404 /* Example:
1405 * ---------------------------------
1406 * | 1 | 1 | 1 | 1 |
1407 * ---------------------------------
1408 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1409 * ---------------------------------
1410 * 0 1 2 3 4 5 6 7
1411 * \_____________________/
1412 *
1413 * Neither [1] nor [6] is aligned to above layer.
1414 * Left neighbour [0] is free, so mark it busy,
1415 * decrease bb_counters and extend range to
1416 * [0; 6]
1417 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1418 * mark [6] free, increase bb_counters and shrink range to
1419 * [0; 5].
1420 * Then shift range to [0; 2], go up and do the same.
1421 */
1422
1423
1424 if (first & 1)
1425 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1);
1426 if (!(last & 1))
1427 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1);
1428 if (first > last)
1429 break;
1430 order++;
1431
1432 if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) {
1433 mb_clear_bits(buddy, first, last - first + 1);
1434 e4b->bd_info->bb_counters[order - 1] += last - first + 1;
1435 break;
1436 }
1437 first >>= 1;
1438 last >>= 1;
1439 buddy = buddy2;
1440 }
1441}
1442
1443static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1444 int first, int count)
1445{
1446 int left_is_free = 0;
1447 int right_is_free = 0;
1448 int block;
1449 int last = first + count - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001450 struct super_block *sb = e4b->bd_sb;
1451
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04001452 if (WARN_ON(count == 0))
1453 return;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001454 BUG_ON(last >= (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001455 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Darrick J. Wong163a2032013-08-28 17:35:51 -04001456 /* Don't bother if the block group is corrupt. */
1457 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
1458 return;
1459
Alex Tomasc9de5602008-01-29 00:19:52 -05001460 mb_check_buddy(e4b);
1461 mb_free_blocks_double(inode, e4b, first, count);
1462
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05301463 this_cpu_inc(discard_pa_seq);
Alex Tomasc9de5602008-01-29 00:19:52 -05001464 e4b->bd_info->bb_free += count;
1465 if (first < e4b->bd_info->bb_first_free)
1466 e4b->bd_info->bb_first_free = first;
1467
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001468 /* access memory sequentially: check left neighbour,
1469 * clear range and then check right neighbour
1470 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001471 if (first != 0)
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001472 left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap);
1473 block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count);
1474 if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0])
1475 right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap);
1476
1477 if (unlikely(block != -1)) {
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001478 struct ext4_sb_info *sbi = EXT4_SB(sb);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001479 ext4_fsblk_t blocknr;
1480
1481 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Jun Piao49598e02018-01-11 13:17:49 -05001482 blocknr += EXT4_C2B(sbi, block);
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07001483 if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
1484 ext4_grp_locked_error(sb, e4b->bd_group,
1485 inode ? inode->i_ino : 0,
1486 blocknr,
1487 "freeing already freed block (bit %u); block bitmap corrupt.",
1488 block);
1489 ext4_mark_group_bitmap_corrupted(
1490 sb, e4b->bd_group,
Wang Shilongdb79e6d2018-05-12 11:39:40 -04001491 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07001492 }
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001493 goto done;
1494 }
1495
1496 /* let's maintain fragments counter */
1497 if (left_is_free && right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001498 e4b->bd_info->bb_fragments--;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001499 else if (!left_is_free && !right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001500 e4b->bd_info->bb_fragments++;
1501
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001502 /* buddy[0] == bd_bitmap is a special case, so handle
1503 * it right away and let mb_buddy_mark_free stay free of
1504 * zero order checks.
1505 * Check if neighbours are to be coaleasced,
1506 * adjust bitmap bb_counters and borders appropriately.
1507 */
1508 if (first & 1) {
1509 first += !left_is_free;
1510 e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001511 }
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001512 if (!(last & 1)) {
1513 last -= !right_is_free;
1514 e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1;
1515 }
1516
1517 if (first <= last)
1518 mb_buddy_mark_free(e4b, first >> 1, last >> 1);
1519
1520done:
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001521 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001522 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001523}
1524
Robin Dong15c006a2012-08-17 10:02:17 -04001525static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001526 int needed, struct ext4_free_extent *ex)
1527{
1528 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001529 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001530 void *buddy;
1531
Vincent Minetbc8e6742009-05-15 08:33:18 -04001532 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001533 BUG_ON(ex == NULL);
1534
Robin Dong15c006a2012-08-17 10:02:17 -04001535 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001536 BUG_ON(buddy == NULL);
1537 BUG_ON(block >= max);
1538 if (mb_test_bit(block, buddy)) {
1539 ex->fe_len = 0;
1540 ex->fe_start = 0;
1541 ex->fe_group = 0;
1542 return 0;
1543 }
1544
Robin Dong15c006a2012-08-17 10:02:17 -04001545 /* find actual order */
1546 order = mb_find_order_for_block(e4b, block);
1547 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001548
1549 ex->fe_len = 1 << order;
1550 ex->fe_start = block << order;
1551 ex->fe_group = e4b->bd_group;
1552
1553 /* calc difference from given start */
1554 next = next - ex->fe_start;
1555 ex->fe_len -= next;
1556 ex->fe_start += next;
1557
1558 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001559 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001560
1561 if (block + 1 >= max)
1562 break;
1563
1564 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001565 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001566 break;
1567
Robin Dongb051d8d2011-10-26 05:30:30 -04001568 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001569
Alex Tomasc9de5602008-01-29 00:19:52 -05001570 block = next >> order;
1571 ex->fe_len += 1 << order;
1572 }
1573
Jan Kara31562b92019-04-06 18:33:06 -04001574 if (ex->fe_start + ex->fe_len > EXT4_CLUSTERS_PER_GROUP(e4b->bd_sb)) {
Theodore Ts'o43c73222017-01-22 19:35:52 -05001575 /* Should never happen! (but apparently sometimes does?!?) */
1576 WARN_ON(1);
1577 ext4_error(e4b->bd_sb, "corruption or bug in mb_find_extent "
1578 "block=%d, order=%d needed=%d ex=%u/%d/%d@%u",
1579 block, order, needed, ex->fe_group, ex->fe_start,
1580 ex->fe_len, ex->fe_logical);
1581 ex->fe_len = 0;
1582 ex->fe_start = 0;
1583 ex->fe_group = 0;
1584 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001585 return ex->fe_len;
1586}
1587
1588static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1589{
1590 int ord;
1591 int mlen = 0;
1592 int max = 0;
1593 int cur;
1594 int start = ex->fe_start;
1595 int len = ex->fe_len;
1596 unsigned ret = 0;
1597 int len0 = len;
1598 void *buddy;
1599
1600 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1601 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001602 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001603 mb_check_buddy(e4b);
1604 mb_mark_used_double(e4b, start, len);
1605
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05301606 this_cpu_inc(discard_pa_seq);
Alex Tomasc9de5602008-01-29 00:19:52 -05001607 e4b->bd_info->bb_free -= len;
1608 if (e4b->bd_info->bb_first_free == start)
1609 e4b->bd_info->bb_first_free += len;
1610
1611 /* let's maintain fragments counter */
1612 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001613 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001614 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001615 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001616 if (mlen && max)
1617 e4b->bd_info->bb_fragments++;
1618 else if (!mlen && !max)
1619 e4b->bd_info->bb_fragments--;
1620
1621 /* let's maintain buddy itself */
1622 while (len) {
1623 ord = mb_find_order_for_block(e4b, start);
1624
1625 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1626 /* the whole chunk may be allocated at once! */
1627 mlen = 1 << ord;
1628 buddy = mb_find_buddy(e4b, ord, &max);
1629 BUG_ON((start >> ord) >= max);
1630 mb_set_bit(start >> ord, buddy);
1631 e4b->bd_info->bb_counters[ord]--;
1632 start += mlen;
1633 len -= mlen;
1634 BUG_ON(len < 0);
1635 continue;
1636 }
1637
1638 /* store for history */
1639 if (ret == 0)
1640 ret = len | (ord << 16);
1641
1642 /* we have to split large buddy */
1643 BUG_ON(ord <= 0);
1644 buddy = mb_find_buddy(e4b, ord, &max);
1645 mb_set_bit(start >> ord, buddy);
1646 e4b->bd_info->bb_counters[ord]--;
1647
1648 ord--;
1649 cur = (start >> ord) & ~1U;
1650 buddy = mb_find_buddy(e4b, ord, &max);
1651 mb_clear_bit(cur, buddy);
1652 mb_clear_bit(cur + 1, buddy);
1653 e4b->bd_info->bb_counters[ord]++;
1654 e4b->bd_info->bb_counters[ord]++;
1655 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001656 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001657
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001658 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001659 mb_check_buddy(e4b);
1660
1661 return ret;
1662}
1663
1664/*
1665 * Must be called under group lock!
1666 */
1667static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1668 struct ext4_buddy *e4b)
1669{
1670 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1671 int ret;
1672
1673 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1674 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1675
1676 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1677 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1678 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1679
1680 /* preallocation can change ac_b_ex, thus we store actually
1681 * allocated blocks for history */
1682 ac->ac_f_ex = ac->ac_b_ex;
1683
1684 ac->ac_status = AC_STATUS_FOUND;
1685 ac->ac_tail = ret & 0xffff;
1686 ac->ac_buddy = ret >> 16;
1687
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001688 /*
1689 * take the page reference. We want the page to be pinned
1690 * so that we don't get a ext4_mb_init_cache_call for this
1691 * group until we update the bitmap. That would mean we
1692 * double allocate blocks. The reference is dropped
1693 * in ext4_mb_release_context
1694 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001695 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1696 get_page(ac->ac_bitmap_page);
1697 ac->ac_buddy_page = e4b->bd_buddy_page;
1698 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001699 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001700 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001701 spin_lock(&sbi->s_md_lock);
1702 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1703 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1704 spin_unlock(&sbi->s_md_lock);
1705 }
Ritesh Harjani53f86b12020-05-20 12:10:32 +05301706 /*
1707 * As we've just preallocated more space than
1708 * user requested originally, we store allocated
1709 * space in a special descriptor.
1710 */
1711 if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
1712 ext4_mb_new_preallocation(ac);
1713
Alex Tomasc9de5602008-01-29 00:19:52 -05001714}
1715
Alex Tomasc9de5602008-01-29 00:19:52 -05001716static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1717 struct ext4_buddy *e4b,
1718 int finish_group)
1719{
1720 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1721 struct ext4_free_extent *bex = &ac->ac_b_ex;
1722 struct ext4_free_extent *gex = &ac->ac_g_ex;
1723 struct ext4_free_extent ex;
1724 int max;
1725
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001726 if (ac->ac_status == AC_STATUS_FOUND)
1727 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001728 /*
1729 * We don't want to scan for a whole year
1730 */
1731 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1732 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1733 ac->ac_status = AC_STATUS_BREAK;
1734 return;
1735 }
1736
1737 /*
1738 * Haven't found good chunk so far, let's continue
1739 */
1740 if (bex->fe_len < gex->fe_len)
1741 return;
1742
1743 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1744 && bex->fe_group == e4b->bd_group) {
1745 /* recheck chunk's availability - we don't know
1746 * when it was found (within this lock-unlock
1747 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001748 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001749 if (max >= gex->fe_len) {
1750 ext4_mb_use_best_found(ac, e4b);
1751 return;
1752 }
1753 }
1754}
1755
1756/*
1757 * The routine checks whether found extent is good enough. If it is,
1758 * then the extent gets marked used and flag is set to the context
1759 * to stop scanning. Otherwise, the extent is compared with the
1760 * previous found extent and if new one is better, then it's stored
1761 * in the context. Later, the best found extent will be used, if
1762 * mballoc can't find good enough extent.
1763 *
1764 * FIXME: real allocation policy is to be designed yet!
1765 */
1766static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1767 struct ext4_free_extent *ex,
1768 struct ext4_buddy *e4b)
1769{
1770 struct ext4_free_extent *bex = &ac->ac_b_ex;
1771 struct ext4_free_extent *gex = &ac->ac_g_ex;
1772
1773 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001774 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1775 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001776 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1777
1778 ac->ac_found++;
1779
1780 /*
1781 * The special case - take what you catch first
1782 */
1783 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1784 *bex = *ex;
1785 ext4_mb_use_best_found(ac, e4b);
1786 return;
1787 }
1788
1789 /*
1790 * Let's check whether the chuck is good enough
1791 */
1792 if (ex->fe_len == gex->fe_len) {
1793 *bex = *ex;
1794 ext4_mb_use_best_found(ac, e4b);
1795 return;
1796 }
1797
1798 /*
1799 * If this is first found extent, just store it in the context
1800 */
1801 if (bex->fe_len == 0) {
1802 *bex = *ex;
1803 return;
1804 }
1805
1806 /*
1807 * If new found extent is better, store it in the context
1808 */
1809 if (bex->fe_len < gex->fe_len) {
1810 /* if the request isn't satisfied, any found extent
1811 * larger than previous best one is better */
1812 if (ex->fe_len > bex->fe_len)
1813 *bex = *ex;
1814 } else if (ex->fe_len > gex->fe_len) {
1815 /* if the request is satisfied, then we try to find
1816 * an extent that still satisfy the request, but is
1817 * smaller than previous one */
1818 if (ex->fe_len < bex->fe_len)
1819 *bex = *ex;
1820 }
1821
1822 ext4_mb_check_limits(ac, e4b, 0);
1823}
1824
Eric Sandeen089ceec2009-07-05 22:17:31 -04001825static noinline_for_stack
1826int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001827 struct ext4_buddy *e4b)
1828{
1829 struct ext4_free_extent ex = ac->ac_b_ex;
1830 ext4_group_t group = ex.fe_group;
1831 int max;
1832 int err;
1833
1834 BUG_ON(ex.fe_len <= 0);
1835 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1836 if (err)
1837 return err;
1838
1839 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001840 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001841
1842 if (max > 0) {
1843 ac->ac_b_ex = ex;
1844 ext4_mb_use_best_found(ac, e4b);
1845 }
1846
1847 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001848 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001849
1850 return 0;
1851}
1852
Eric Sandeen089ceec2009-07-05 22:17:31 -04001853static noinline_for_stack
1854int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001855 struct ext4_buddy *e4b)
1856{
1857 ext4_group_t group = ac->ac_g_ex.fe_group;
1858 int max;
1859 int err;
1860 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001861 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001862 struct ext4_free_extent ex;
1863
1864 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1865 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001866 if (grp->bb_free == 0)
1867 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001868
1869 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1870 if (err)
1871 return err;
1872
Darrick J. Wong163a2032013-08-28 17:35:51 -04001873 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) {
1874 ext4_mb_unload_buddy(e4b);
1875 return 0;
1876 }
1877
Alex Tomasc9de5602008-01-29 00:19:52 -05001878 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001879 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001880 ac->ac_g_ex.fe_len, &ex);
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001881 ex.fe_logical = 0xDEADFA11; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001882
1883 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1884 ext4_fsblk_t start;
1885
Akinobu Mita5661bd62010-03-03 23:53:39 -05001886 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1887 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001888 /* use do_div to get remainder (would be 64-bit modulo) */
1889 if (do_div(start, sbi->s_stripe) == 0) {
1890 ac->ac_found++;
1891 ac->ac_b_ex = ex;
1892 ext4_mb_use_best_found(ac, e4b);
1893 }
1894 } else if (max >= ac->ac_g_ex.fe_len) {
1895 BUG_ON(ex.fe_len <= 0);
1896 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1897 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1898 ac->ac_found++;
1899 ac->ac_b_ex = ex;
1900 ext4_mb_use_best_found(ac, e4b);
1901 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1902 /* Sometimes, caller may want to merge even small
1903 * number of blocks to an existing extent */
1904 BUG_ON(ex.fe_len <= 0);
1905 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1906 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1907 ac->ac_found++;
1908 ac->ac_b_ex = ex;
1909 ext4_mb_use_best_found(ac, e4b);
1910 }
1911 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001912 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001913
1914 return 0;
1915}
1916
1917/*
1918 * The routine scans buddy structures (not bitmap!) from given order
1919 * to max order and tries to find big enough chunk to satisfy the req
1920 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001921static noinline_for_stack
1922void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001923 struct ext4_buddy *e4b)
1924{
1925 struct super_block *sb = ac->ac_sb;
1926 struct ext4_group_info *grp = e4b->bd_info;
1927 void *buddy;
1928 int i;
1929 int k;
1930 int max;
1931
1932 BUG_ON(ac->ac_2order <= 0);
1933 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1934 if (grp->bb_counters[i] == 0)
1935 continue;
1936
1937 buddy = mb_find_buddy(e4b, i, &max);
1938 BUG_ON(buddy == NULL);
1939
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001940 k = mb_find_next_zero_bit(buddy, max, 0);
Dmitry Monakhoveb576082020-03-10 15:01:56 +00001941 if (k >= max) {
1942 ext4_grp_locked_error(ac->ac_sb, e4b->bd_group, 0, 0,
1943 "%d free clusters of order %d. But found 0",
1944 grp->bb_counters[i], i);
1945 ext4_mark_group_bitmap_corrupted(ac->ac_sb,
1946 e4b->bd_group,
1947 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
1948 break;
1949 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001950 ac->ac_found++;
1951
1952 ac->ac_b_ex.fe_len = 1 << i;
1953 ac->ac_b_ex.fe_start = k << i;
1954 ac->ac_b_ex.fe_group = e4b->bd_group;
1955
1956 ext4_mb_use_best_found(ac, e4b);
1957
Ritesh Harjani53f86b12020-05-20 12:10:32 +05301958 BUG_ON(ac->ac_f_ex.fe_len != ac->ac_g_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05001959
1960 if (EXT4_SB(sb)->s_mb_stats)
1961 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1962
1963 break;
1964 }
1965}
1966
1967/*
1968 * The routine scans the group and measures all found extents.
1969 * In order to optimize scanning, caller must pass number of
1970 * free blocks in the group, so the routine can know upper limit.
1971 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001972static noinline_for_stack
1973void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001974 struct ext4_buddy *e4b)
1975{
1976 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001977 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001978 struct ext4_free_extent ex;
1979 int i;
1980 int free;
1981
1982 free = e4b->bd_info->bb_free;
Theodore Ts'o907ea522020-04-13 23:33:05 -04001983 if (WARN_ON(free <= 0))
1984 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001985
1986 i = e4b->bd_info->bb_first_free;
1987
1988 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001989 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001990 EXT4_CLUSTERS_PER_GROUP(sb), i);
1991 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001992 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001993 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001994 * free blocks even though group info says we
Randy Dunlapb483bb72020-08-04 19:48:50 -07001995 * have free blocks
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001996 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001997 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001998 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001999 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002000 free);
Wang Shilong736dedb2018-05-12 12:37:58 -04002001 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
2002 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002003 break;
2004 }
2005
Robin Dong15c006a2012-08-17 10:02:17 -04002006 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Theodore Ts'o907ea522020-04-13 23:33:05 -04002007 if (WARN_ON(ex.fe_len <= 0))
2008 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002009 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04002010 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002011 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05002012 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002013 free, ex.fe_len);
Wang Shilong736dedb2018-05-12 12:37:58 -04002014 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
2015 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05002016 /*
2017 * The number of free blocks differs. This mostly
2018 * indicate that the bitmap is corrupt. So exit
2019 * without claiming the space.
2020 */
2021 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002022 }
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002023 ex.fe_logical = 0xDEADC0DE; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002024 ext4_mb_measure_extent(ac, &ex, e4b);
2025
2026 i += ex.fe_len;
2027 free -= ex.fe_len;
2028 }
2029
2030 ext4_mb_check_limits(ac, e4b, 1);
2031}
2032
2033/*
2034 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002035 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05002036 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04002037static noinline_for_stack
2038void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002039 struct ext4_buddy *e4b)
2040{
2041 struct super_block *sb = ac->ac_sb;
2042 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05002043 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05002044 struct ext4_free_extent ex;
2045 ext4_fsblk_t first_group_block;
2046 ext4_fsblk_t a;
2047 ext4_grpblk_t i;
2048 int max;
2049
2050 BUG_ON(sbi->s_stripe == 0);
2051
2052 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05002053 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
2054
Alex Tomasc9de5602008-01-29 00:19:52 -05002055 a = first_group_block + sbi->s_stripe - 1;
2056 do_div(a, sbi->s_stripe);
2057 i = (a * sbi->s_stripe) - first_group_block;
2058
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04002059 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002060 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04002061 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002062 if (max >= sbi->s_stripe) {
2063 ac->ac_found++;
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002064 ex.fe_logical = 0xDEADF00D; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002065 ac->ac_b_ex = ex;
2066 ext4_mb_use_best_found(ac, e4b);
2067 break;
2068 }
2069 }
2070 i += sbi->s_stripe;
2071 }
2072}
2073
Lukas Czerner42ac1842015-06-08 11:40:40 -04002074/*
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302075 * This is also called BEFORE we load the buddy bitmap.
Lukas Czerner42ac1842015-06-08 11:40:40 -04002076 * Returns either 1 or 0 indicating that the group is either suitable
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302077 * for the allocation or not.
Lukas Czerner42ac1842015-06-08 11:40:40 -04002078 */
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302079static bool ext4_mb_good_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002080 ext4_group_t group, int cr)
2081{
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302082 ext4_grpblk_t free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002083 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002084 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2085
2086 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002087
brookxudddcd2f2020-08-07 22:01:39 +08002088 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302089 return false;
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002090
brookxudddcd2f2020-08-07 22:01:39 +08002091 free = grp->bb_free;
2092 if (free == 0)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302093 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05002094
Alex Tomasc9de5602008-01-29 00:19:52 -05002095 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05002096 if (fragments == 0)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302097 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05002098
2099 switch (cr) {
2100 case 0:
2101 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05002102
Theodore Ts'oa4912122009-03-12 12:18:34 -04002103 /* Avoid using the first bg of a flexgroup for data files */
2104 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
2105 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
2106 ((group % flex_size) == 0))
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302107 return false;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002108
brookxudddcd2f2020-08-07 22:01:39 +08002109 if (free < ac->ac_g_ex.fe_len)
2110 return false;
2111
2112 if (ac->ac_2order > ac->ac_sb->s_blocksize_bits+1)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302113 return true;
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002114
2115 if (grp->bb_largest_free_order < ac->ac_2order)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302116 return false;
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002117
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302118 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05002119 case 1:
2120 if ((free / fragments) >= ac->ac_g_ex.fe_len)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302121 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05002122 break;
2123 case 2:
2124 if (free >= ac->ac_g_ex.fe_len)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302125 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05002126 break;
2127 case 3:
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302128 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05002129 default:
2130 BUG();
2131 }
2132
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302133 return false;
2134}
2135
2136/*
2137 * This could return negative error code if something goes wrong
2138 * during ext4_mb_init_group(). This should not be called with
2139 * ext4_lock_group() held.
2140 */
2141static int ext4_mb_good_group_nolock(struct ext4_allocation_context *ac,
2142 ext4_group_t group, int cr)
2143{
2144 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Ritesh Harjani99377832020-05-20 12:10:36 +05302145 struct super_block *sb = ac->ac_sb;
Alex Zhuravlevc1d2c7d2020-06-19 22:08:56 -04002146 struct ext4_sb_info *sbi = EXT4_SB(sb);
Ritesh Harjani99377832020-05-20 12:10:36 +05302147 bool should_lock = ac->ac_flags & EXT4_MB_STRICT_CHECK;
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302148 ext4_grpblk_t free;
2149 int ret = 0;
2150
Ritesh Harjani99377832020-05-20 12:10:36 +05302151 if (should_lock)
2152 ext4_lock_group(sb, group);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302153 free = grp->bb_free;
2154 if (free == 0)
2155 goto out;
2156 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
2157 goto out;
2158 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
2159 goto out;
Ritesh Harjani99377832020-05-20 12:10:36 +05302160 if (should_lock)
2161 ext4_unlock_group(sb, group);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302162
2163 /* We only do this if the grp has never been initialized */
2164 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Alex Zhuravlevc1d2c7d2020-06-19 22:08:56 -04002165 struct ext4_group_desc *gdp =
2166 ext4_get_group_desc(sb, group, NULL);
2167 int ret;
2168
2169 /* cr=0/1 is a very optimistic search to find large
2170 * good chunks almost for free. If buddy data is not
2171 * ready, then this optimization makes no sense. But
2172 * we never skip the first block group in a flex_bg,
2173 * since this gets used for metadata block allocation,
2174 * and we want to make sure we locate metadata blocks
2175 * in the first block group in the flex_bg if possible.
2176 */
2177 if (cr < 2 &&
2178 (!sbi->s_log_groups_per_flex ||
2179 ((group & ((1 << sbi->s_log_groups_per_flex) - 1)) != 0)) &&
2180 !(ext4_has_group_desc_csum(sb) &&
2181 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))))
2182 return 0;
2183 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302184 if (ret)
2185 return ret;
2186 }
2187
Ritesh Harjani99377832020-05-20 12:10:36 +05302188 if (should_lock)
2189 ext4_lock_group(sb, group);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302190 ret = ext4_mb_good_group(ac, group, cr);
2191out:
Ritesh Harjani99377832020-05-20 12:10:36 +05302192 if (should_lock)
2193 ext4_unlock_group(sb, group);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302194 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002195}
2196
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002197/*
2198 * Start prefetching @nr block bitmaps starting at @group.
2199 * Return the next group which needs to be prefetched.
2200 */
Theodore Ts'o3d392b22020-07-17 00:14:40 -04002201ext4_group_t ext4_mb_prefetch(struct super_block *sb, ext4_group_t group,
2202 unsigned int nr, int *cnt)
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002203{
2204 ext4_group_t ngroups = ext4_get_groups_count(sb);
2205 struct buffer_head *bh;
2206 struct blk_plug plug;
2207
2208 blk_start_plug(&plug);
2209 while (nr-- > 0) {
2210 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group,
2211 NULL);
2212 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
2213
2214 /*
2215 * Prefetch block groups with free blocks; but don't
2216 * bother if it is marked uninitialized on disk, since
2217 * it won't require I/O to read. Also only try to
2218 * prefetch once, so we avoid getblk() call, which can
2219 * be expensive.
2220 */
2221 if (!EXT4_MB_GRP_TEST_AND_SET_READ(grp) &&
2222 EXT4_MB_GRP_NEED_INIT(grp) &&
2223 ext4_free_group_clusters(sb, gdp) > 0 &&
2224 !(ext4_has_group_desc_csum(sb) &&
2225 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) {
2226 bh = ext4_read_block_bitmap_nowait(sb, group, true);
2227 if (bh && !IS_ERR(bh)) {
2228 if (!buffer_uptodate(bh) && cnt)
2229 (*cnt)++;
2230 brelse(bh);
2231 }
2232 }
2233 if (++group >= ngroups)
2234 group = 0;
2235 }
2236 blk_finish_plug(&plug);
2237 return group;
2238}
2239
2240/*
2241 * Prefetching reads the block bitmap into the buffer cache; but we
2242 * need to make sure that the buddy bitmap in the page cache has been
2243 * initialized. Note that ext4_mb_init_group() will block if the I/O
2244 * is not yet completed, or indeed if it was not initiated by
2245 * ext4_mb_prefetch did not start the I/O.
2246 *
2247 * TODO: We should actually kick off the buddy bitmap setup in a work
2248 * queue when the buffer I/O is completed, so that we don't block
2249 * waiting for the block allocation bitmap read to finish when
2250 * ext4_mb_prefetch_fini is called from ext4_mb_regular_allocator().
2251 */
Theodore Ts'o3d392b22020-07-17 00:14:40 -04002252void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
2253 unsigned int nr)
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002254{
2255 while (nr-- > 0) {
2256 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group,
2257 NULL);
2258 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
2259
2260 if (!group)
2261 group = ext4_get_groups_count(sb);
2262 group--;
2263 grp = ext4_get_group_info(sb, group);
2264
2265 if (EXT4_MB_GRP_NEED_INIT(grp) &&
2266 ext4_free_group_clusters(sb, gdp) > 0 &&
2267 !(ext4_has_group_desc_csum(sb) &&
2268 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) {
2269 if (ext4_mb_init_group(sb, group, GFP_NOFS))
2270 break;
2271 }
2272 }
2273}
2274
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002275static noinline_for_stack int
2276ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002277{
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002278 ext4_group_t prefetch_grp = 0, ngroups, group, i;
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05302279 int cr = -1;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002280 int err = 0, first_err = 0;
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002281 unsigned int nr = 0, prefetch_ios = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002282 struct ext4_sb_info *sbi;
2283 struct super_block *sb;
2284 struct ext4_buddy e4b;
brookxu66d5e022020-08-17 15:36:06 +08002285 int lost;
Alex Tomasc9de5602008-01-29 00:19:52 -05002286
2287 sb = ac->ac_sb;
2288 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002289 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002290 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002291 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002292 ngroups = sbi->s_blockfile_groups;
2293
Alex Tomasc9de5602008-01-29 00:19:52 -05002294 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2295
2296 /* first, try the goal */
2297 err = ext4_mb_find_by_goal(ac, &e4b);
2298 if (err || ac->ac_status == AC_STATUS_FOUND)
2299 goto out;
2300
2301 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2302 goto out;
2303
2304 /*
brookxue9a3cd42020-08-07 22:01:23 +08002305 * ac->ac_2order is set only if the fe_len is a power of 2
2306 * if ac->ac_2order is set we also set criteria to 0 so that we
Alex Tomasc9de5602008-01-29 00:19:52 -05002307 * try exact allocation using buddy.
2308 */
2309 i = fls(ac->ac_g_ex.fe_len);
2310 ac->ac_2order = 0;
2311 /*
2312 * We search using buddy data only if the order of the request
2313 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002314 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Jan Karad9b22cf2017-02-10 00:50:56 -05002315 * We also support searching for power-of-two requests only for
2316 * requests upto maximum buddy size we have constructed.
Alex Tomasc9de5602008-01-29 00:19:52 -05002317 */
Jan Karad9b22cf2017-02-10 00:50:56 -05002318 if (i >= sbi->s_mb_order2_reqs && i <= sb->s_blocksize_bits + 2) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002319 /*
2320 * This should tell if fe_len is exactly power of 2
2321 */
2322 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
Jeremy Cline1a5d5e52018-08-02 00:03:40 -04002323 ac->ac_2order = array_index_nospec(i - 1,
2324 sb->s_blocksize_bits + 2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002325 }
2326
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002327 /* if stream allocation is enabled, use global goal */
2328 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002329 /* TBD: may be hot point */
2330 spin_lock(&sbi->s_md_lock);
2331 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2332 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2333 spin_unlock(&sbi->s_md_lock);
2334 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002335
Alex Tomasc9de5602008-01-29 00:19:52 -05002336 /* Let's just scan groups to find more-less suitable blocks */
2337 cr = ac->ac_2order ? 0 : 1;
2338 /*
2339 * cr == 0 try to get exact allocation,
2340 * cr == 3 try to get anything
2341 */
2342repeat:
2343 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2344 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002345 /*
2346 * searching for the right group start
2347 * from the goal value specified
2348 */
2349 group = ac->ac_g_ex.fe_group;
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002350 prefetch_grp = group;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002351
Theodore Ts'o8df96752009-05-01 08:50:38 -04002352 for (i = 0; i < ngroups; group++, i++) {
Lukas Czerner42ac1842015-06-08 11:40:40 -04002353 int ret = 0;
Theodore Ts'o2ed57242013-06-12 11:43:02 -04002354 cond_resched();
Lachlan McIlroye6155732013-05-05 23:10:00 -04002355 /*
2356 * Artificially restricted ngroups for non-extent
2357 * files makes group > ngroups possible on first loop.
2358 */
2359 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002360 group = 0;
2361
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002362 /*
2363 * Batch reads of the block allocation bitmaps
2364 * to get multiple READs in flight; limit
2365 * prefetching at cr=0/1, otherwise mballoc can
2366 * spend a lot of time loading imperfect groups
2367 */
2368 if ((prefetch_grp == group) &&
2369 (cr > 1 ||
2370 prefetch_ios < sbi->s_mb_prefetch_limit)) {
2371 unsigned int curr_ios = prefetch_ios;
2372
2373 nr = sbi->s_mb_prefetch;
2374 if (ext4_has_feature_flex_bg(sb)) {
Chunguang Xu82ef1372020-12-04 11:05:43 +08002375 nr = 1 << sbi->s_log_groups_per_flex;
2376 nr -= group & (nr - 1);
2377 nr = min(nr, sbi->s_mb_prefetch);
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002378 }
2379 prefetch_grp = ext4_mb_prefetch(sb, group,
2380 nr, &prefetch_ios);
2381 if (prefetch_ios == curr_ios)
2382 nr = 0;
2383 }
2384
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002385 /* This now checks without needing the buddy page */
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302386 ret = ext4_mb_good_group_nolock(ac, group, cr);
Lukas Czerner42ac1842015-06-08 11:40:40 -04002387 if (ret <= 0) {
2388 if (!first_err)
2389 first_err = ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002390 continue;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002391 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002392
Alex Tomasc9de5602008-01-29 00:19:52 -05002393 err = ext4_mb_load_buddy(sb, group, &e4b);
2394 if (err)
2395 goto out;
2396
2397 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002398
2399 /*
2400 * We need to check again after locking the
2401 * block group
2402 */
Lukas Czerner42ac1842015-06-08 11:40:40 -04002403 ret = ext4_mb_good_group(ac, group, cr);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302404 if (ret == 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002405 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002406 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002407 continue;
2408 }
2409
2410 ac->ac_groups_scanned++;
Jan Karad9b22cf2017-02-10 00:50:56 -05002411 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002412 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002413 else if (cr == 1 && sbi->s_stripe &&
2414 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002415 ext4_mb_scan_aligned(ac, &e4b);
2416 else
2417 ext4_mb_complex_scan_group(ac, &e4b);
2418
2419 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002420 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002421
2422 if (ac->ac_status != AC_STATUS_CONTINUE)
2423 break;
2424 }
2425 }
2426
2427 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2428 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2429 /*
2430 * We've been searching too long. Let's try to allocate
2431 * the best chunk we've found so far
2432 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002433 ext4_mb_try_best_found(ac, &e4b);
2434 if (ac->ac_status != AC_STATUS_FOUND) {
2435 /*
2436 * Someone more lucky has already allocated it.
2437 * The only thing we can do is just take first
2438 * found block(s)
Alex Tomasc9de5602008-01-29 00:19:52 -05002439 */
brookxu66d5e022020-08-17 15:36:06 +08002440 lost = atomic_inc_return(&sbi->s_mb_lost_chunks);
2441 mb_debug(sb, "lost chunk, group: %u, start: %d, len: %d, lost: %d\n",
brookxuc55ee7d2020-08-15 08:10:44 +08002442 ac->ac_b_ex.fe_group, ac->ac_b_ex.fe_start,
2443 ac->ac_b_ex.fe_len, lost);
2444
Alex Tomasc9de5602008-01-29 00:19:52 -05002445 ac->ac_b_ex.fe_group = 0;
2446 ac->ac_b_ex.fe_start = 0;
2447 ac->ac_b_ex.fe_len = 0;
2448 ac->ac_status = AC_STATUS_CONTINUE;
2449 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2450 cr = 3;
Alex Tomasc9de5602008-01-29 00:19:52 -05002451 goto repeat;
2452 }
2453 }
2454out:
Lukas Czerner42ac1842015-06-08 11:40:40 -04002455 if (!err && ac->ac_status != AC_STATUS_FOUND && first_err)
2456 err = first_err;
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05302457
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302458 mb_debug(sb, "Best len %d, origin len %d, ac_status %u, ac_flags 0x%x, cr %d ret %d\n",
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05302459 ac->ac_b_ex.fe_len, ac->ac_o_ex.fe_len, ac->ac_status,
2460 ac->ac_flags, cr, err);
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002461
2462 if (nr)
2463 ext4_mb_prefetch_fini(sb, prefetch_grp, nr);
2464
Alex Tomasc9de5602008-01-29 00:19:52 -05002465 return err;
2466}
2467
Alex Tomasc9de5602008-01-29 00:19:52 -05002468static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2469{
Christoph Hellwig247dbed2018-04-11 11:37:23 +02002470 struct super_block *sb = PDE_DATA(file_inode(seq->file));
Alex Tomasc9de5602008-01-29 00:19:52 -05002471 ext4_group_t group;
2472
Theodore Ts'o8df96752009-05-01 08:50:38 -04002473 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002474 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002475 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002476 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002477}
2478
2479static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2480{
Christoph Hellwig247dbed2018-04-11 11:37:23 +02002481 struct super_block *sb = PDE_DATA(file_inode(seq->file));
Alex Tomasc9de5602008-01-29 00:19:52 -05002482 ext4_group_t group;
2483
2484 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002485 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002486 return NULL;
2487 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002488 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002489}
2490
2491static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2492{
Christoph Hellwig247dbed2018-04-11 11:37:23 +02002493 struct super_block *sb = PDE_DATA(file_inode(seq->file));
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002494 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002495 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002496 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002497 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002498 struct ext4_group_info *grinfo;
Arnd Bergmann2df2c342017-08-05 21:57:46 -04002499 unsigned char blocksize_bits = min_t(unsigned char,
2500 sb->s_blocksize_bits,
2501 EXT4_MAX_BLOCK_LOG_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -05002502 struct sg {
2503 struct ext4_group_info info;
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002504 ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2];
Alex Tomasc9de5602008-01-29 00:19:52 -05002505 } sg;
2506
2507 group--;
2508 if (group == 0)
Rasmus Villemoes97b4af22015-06-15 00:32:58 -04002509 seq_puts(seq, "#group: free frags first ["
2510 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
Huaitong Han802cf1f2016-02-12 00:17:16 -05002511 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002512
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002513 i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2514 sizeof(struct ext4_group_info);
2515
Aditya Kali1c8457c2012-06-30 19:10:57 -04002516 grinfo = ext4_get_group_info(sb, group);
2517 /* Load the group info in memory only if not already loaded. */
2518 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2519 err = ext4_mb_load_buddy(sb, group, &e4b);
2520 if (err) {
2521 seq_printf(seq, "#%-5u: I/O error\n", group);
2522 return 0;
2523 }
2524 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002525 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002526
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002527 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002528
2529 if (buddy_loaded)
2530 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002531
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002532 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002533 sg.info.bb_fragments, sg.info.bb_first_free);
2534 for (i = 0; i <= 13; i++)
Arnd Bergmann2df2c342017-08-05 21:57:46 -04002535 seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ?
Alex Tomasc9de5602008-01-29 00:19:52 -05002536 sg.info.bb_counters[i] : 0);
Xu Wange0d438c2020-08-10 02:21:58 +00002537 seq_puts(seq, " ]\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002538
2539 return 0;
2540}
2541
2542static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2543{
2544}
2545
Christoph Hellwig247dbed2018-04-11 11:37:23 +02002546const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002547 .start = ext4_mb_seq_groups_start,
2548 .next = ext4_mb_seq_groups_next,
2549 .stop = ext4_mb_seq_groups_stop,
2550 .show = ext4_mb_seq_groups_show,
2551};
2552
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002553static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2554{
2555 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2556 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2557
2558 BUG_ON(!cachep);
2559 return cachep;
2560}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002561
Theodore Ts'o28623c22012-09-05 01:31:50 -04002562/*
2563 * Allocate the top-level s_group_info array for the specified number
2564 * of groups
2565 */
2566int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2567{
2568 struct ext4_sb_info *sbi = EXT4_SB(sb);
2569 unsigned size;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002570 struct ext4_group_info ***old_groupinfo, ***new_groupinfo;
Theodore Ts'o28623c22012-09-05 01:31:50 -04002571
2572 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2573 EXT4_DESC_PER_BLOCK_BITS(sb);
2574 if (size <= sbi->s_group_info_size)
2575 return 0;
2576
2577 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
Michal Hockoa7c3e902017-05-08 15:57:09 -07002578 new_groupinfo = kvzalloc(size, GFP_KERNEL);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002579 if (!new_groupinfo) {
2580 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2581 return -ENOMEM;
2582 }
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002583 rcu_read_lock();
2584 old_groupinfo = rcu_dereference(sbi->s_group_info);
2585 if (old_groupinfo)
2586 memcpy(new_groupinfo, old_groupinfo,
Theodore Ts'o28623c22012-09-05 01:31:50 -04002587 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002588 rcu_read_unlock();
2589 rcu_assign_pointer(sbi->s_group_info, new_groupinfo);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002590 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002591 if (old_groupinfo)
2592 ext4_kvfree_array_rcu(old_groupinfo);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002593 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2594 sbi->s_group_info_size);
2595 return 0;
2596}
2597
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002598/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002599int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002600 struct ext4_group_desc *desc)
2601{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002602 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002603 int metalen = 0;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002604 int idx = group >> EXT4_DESC_PER_BLOCK_BITS(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002605 struct ext4_sb_info *sbi = EXT4_SB(sb);
2606 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002607 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002608
2609 /*
2610 * First check if this group is the first of a reserved block.
2611 * If it's true, we have to allocate a new table of pointers
2612 * to ext4_group_info structures
2613 */
2614 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2615 metalen = sizeof(*meta_group_info) <<
2616 EXT4_DESC_PER_BLOCK_BITS(sb);
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002617 meta_group_info = kmalloc(metalen, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002618 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002619 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002620 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002621 goto exit_meta_group_info;
2622 }
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002623 rcu_read_lock();
2624 rcu_dereference(sbi->s_group_info)[idx] = meta_group_info;
2625 rcu_read_unlock();
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002626 }
2627
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002628 meta_group_info = sbi_array_rcu_deref(sbi, s_group_info, idx);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002629 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2630
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002631 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002632 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002633 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002634 goto exit_group_info;
2635 }
2636 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2637 &(meta_group_info[i]->bb_state));
2638
2639 /*
2640 * initialize bb_free to be able to skip
2641 * empty groups without initialization
2642 */
Theodore Ts'o88446182018-06-14 00:58:00 -04002643 if (ext4_has_group_desc_csum(sb) &&
2644 (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002645 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002646 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002647 } else {
2648 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002649 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002650 }
2651
2652 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002653 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002654 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002655 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002656
Ritesh Harjania3450212020-05-10 11:54:48 +05302657 mb_group_bb_bitmap_alloc(sb, meta_group_info[i], group);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002658 return 0;
2659
2660exit_group_info:
2661 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002662 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002663 struct ext4_group_info ***group_info;
2664
2665 rcu_read_lock();
2666 group_info = rcu_dereference(sbi->s_group_info);
2667 kfree(group_info[idx]);
2668 group_info[idx] = NULL;
2669 rcu_read_unlock();
Tao Macaaf7a22011-07-11 18:42:42 -04002670 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002671exit_meta_group_info:
2672 return -ENOMEM;
2673} /* ext4_mb_add_groupinfo */
2674
Alex Tomasc9de5602008-01-29 00:19:52 -05002675static int ext4_mb_init_backend(struct super_block *sb)
2676{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002677 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002678 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002679 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002680 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002681 struct ext4_group_desc *desc;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002682 struct ext4_group_info ***group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002683 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002684
Theodore Ts'o28623c22012-09-05 01:31:50 -04002685 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2686 if (err)
2687 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002688
Alex Tomasc9de5602008-01-29 00:19:52 -05002689 sbi->s_buddy_cache = new_inode(sb);
2690 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002691 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002692 goto err_freesgi;
2693 }
Yu Jian48e60612011-08-01 17:41:39 -04002694 /* To avoid potentially colliding with an valid on-disk inode number,
2695 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2696 * not in the inode hash, so it should never be found by iget(), but
2697 * this will avoid confusion if it ever shows up during debugging. */
2698 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002699 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002700 for (i = 0; i < ngroups; i++) {
Khazhismel Kumykov4b99faa2019-04-25 12:58:01 -04002701 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05002702 desc = ext4_get_group_desc(sb, i, NULL);
2703 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002704 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002705 goto err_freebuddy;
2706 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002707 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2708 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002709 }
2710
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002711 if (ext4_has_feature_flex_bg(sb)) {
2712 /* a single flex group is supposed to be read by a single IO */
Chunguang Xu82ef1372020-12-04 11:05:43 +08002713 sbi->s_mb_prefetch = min(1 << sbi->s_es->s_log_groups_per_flex,
2714 BLK_MAX_SEGMENT_SIZE >> (sb->s_blocksize_bits - 9));
Alex Zhuravlevcfd73232020-04-21 10:54:07 +03002715 sbi->s_mb_prefetch *= 8; /* 8 prefetch IOs in flight at most */
2716 } else {
2717 sbi->s_mb_prefetch = 32;
2718 }
2719 if (sbi->s_mb_prefetch > ext4_get_groups_count(sb))
2720 sbi->s_mb_prefetch = ext4_get_groups_count(sb);
2721 /* now many real IOs to prefetch within a single allocation at cr=0
2722 * given cr=0 is an CPU-related optimization we shouldn't try to
2723 * load too many groups, at some point we should start to use what
2724 * we've got in memory.
2725 * with an average random access time 5ms, it'd take a second to get
2726 * 200 groups (* N with flex_bg), so let's make this limit 4
2727 */
2728 sbi->s_mb_prefetch_limit = sbi->s_mb_prefetch * 4;
2729 if (sbi->s_mb_prefetch_limit > ext4_get_groups_count(sb))
2730 sbi->s_mb_prefetch_limit = ext4_get_groups_count(sb);
2731
Alex Tomasc9de5602008-01-29 00:19:52 -05002732 return 0;
2733
2734err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002735 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002736 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002737 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002738 i = sbi->s_group_info_size;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002739 rcu_read_lock();
2740 group_info = rcu_dereference(sbi->s_group_info);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002741 while (i-- > 0)
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002742 kfree(group_info[i]);
2743 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002744 iput(sbi->s_buddy_cache);
2745err_freesgi:
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002746 rcu_read_lock();
2747 kvfree(rcu_dereference(sbi->s_group_info));
2748 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002749 return -ENOMEM;
2750}
2751
Eric Sandeen2892c152011-02-12 08:12:18 -05002752static void ext4_groupinfo_destroy_slabs(void)
2753{
2754 int i;
2755
2756 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
Sean Fu21c580d2018-05-20 22:44:13 -04002757 kmem_cache_destroy(ext4_groupinfo_caches[i]);
Eric Sandeen2892c152011-02-12 08:12:18 -05002758 ext4_groupinfo_caches[i] = NULL;
2759 }
2760}
2761
2762static int ext4_groupinfo_create_slab(size_t size)
2763{
2764 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2765 int slab_size;
2766 int blocksize_bits = order_base_2(size);
2767 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2768 struct kmem_cache *cachep;
2769
2770 if (cache_index >= NR_GRPINFO_CACHES)
2771 return -EINVAL;
2772
2773 if (unlikely(cache_index < 0))
2774 cache_index = 0;
2775
2776 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2777 if (ext4_groupinfo_caches[cache_index]) {
2778 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2779 return 0; /* Already created */
2780 }
2781
2782 slab_size = offsetof(struct ext4_group_info,
2783 bb_counters[blocksize_bits + 2]);
2784
2785 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2786 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2787 NULL);
2788
Tao Ma823ba012011-07-11 18:26:01 -04002789 ext4_groupinfo_caches[cache_index] = cachep;
2790
Eric Sandeen2892c152011-02-12 08:12:18 -05002791 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2792 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002793 printk(KERN_EMERG
2794 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002795 return -ENOMEM;
2796 }
2797
Eric Sandeen2892c152011-02-12 08:12:18 -05002798 return 0;
2799}
2800
Akira Fujita9d990122012-05-28 14:19:25 -04002801int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002802{
2803 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002804 unsigned i, j;
Nicolai Stange935244c2016-05-05 19:46:19 -04002805 unsigned offset, offset_incr;
Alex Tomasc9de5602008-01-29 00:19:52 -05002806 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002807 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002808
Eric Sandeen19278052009-08-25 22:36:25 -04002809 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002810
2811 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2812 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002813 ret = -ENOMEM;
2814 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002815 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002816
Eric Sandeen19278052009-08-25 22:36:25 -04002817 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002818 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2819 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002820 ret = -ENOMEM;
2821 goto out;
2822 }
2823
Eric Sandeen2892c152011-02-12 08:12:18 -05002824 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2825 if (ret < 0)
2826 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002827
2828 /* order 0 is regular bitmap */
2829 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2830 sbi->s_mb_offsets[0] = 0;
2831
2832 i = 1;
2833 offset = 0;
Nicolai Stange935244c2016-05-05 19:46:19 -04002834 offset_incr = 1 << (sb->s_blocksize_bits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05002835 max = sb->s_blocksize << 2;
2836 do {
2837 sbi->s_mb_offsets[i] = offset;
2838 sbi->s_mb_maxs[i] = max;
Nicolai Stange935244c2016-05-05 19:46:19 -04002839 offset += offset_incr;
2840 offset_incr = offset_incr >> 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002841 max = max >> 1;
2842 i++;
2843 } while (i <= sb->s_blocksize_bits + 1);
2844
Alex Tomasc9de5602008-01-29 00:19:52 -05002845 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002846 spin_lock_init(&sbi->s_bal_lock);
Theodore Ts'od08854f2016-06-26 18:24:01 -04002847 sbi->s_mb_free_pending = 0;
Daeho Jeonga0154342017-06-22 23:54:33 -04002848 INIT_LIST_HEAD(&sbi->s_freed_data_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002849
2850 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2851 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2852 sbi->s_mb_stats = MB_DEFAULT_STATS;
2853 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2854 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
brookxu27bc4462020-08-17 15:36:15 +08002855 sbi->s_mb_max_inode_prealloc = MB_DEFAULT_MAX_INODE_PREALLOC;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002856 /*
2857 * The default group preallocation is 512, which for 4k block
2858 * sizes translates to 2 megabytes. However for bigalloc file
2859 * systems, this is probably too big (i.e, if the cluster size
2860 * is 1 megabyte, then group preallocation size becomes half a
2861 * gigabyte!). As a default, we will keep a two megabyte
2862 * group pralloc size for cluster sizes up to 64k, and after
2863 * that, we will force a minimum group preallocation size of
2864 * 32 clusters. This translates to 8 megs when the cluster
2865 * size is 256k, and 32 megs when the cluster size is 1 meg,
2866 * which seems reasonable as a default.
2867 */
2868 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2869 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002870 /*
2871 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2872 * to the lowest multiple of s_stripe which is bigger than
2873 * the s_mb_group_prealloc as determined above. We want
2874 * the preallocation size to be an exact multiple of the
2875 * RAID stripe size so that preallocations don't fragment
2876 * the stripes.
2877 */
2878 if (sbi->s_stripe > 1) {
2879 sbi->s_mb_group_prealloc = roundup(
2880 sbi->s_mb_group_prealloc, sbi->s_stripe);
2881 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002882
Eric Sandeen730c2132008-09-13 15:23:29 -04002883 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002884 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002885 ret = -ENOMEM;
Andrey Tsyvarev029b10c2014-05-12 12:34:21 -04002886 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002887 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002888 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002889 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002890 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002891 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002892 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2893 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002894 spin_lock_init(&lg->lg_prealloc_lock);
2895 }
2896
Yu Jian79a77c52011-08-01 17:41:46 -04002897 /* init file for buddy data */
2898 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002899 if (ret != 0)
2900 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002901
Tao Ma7aa0bae2011-10-06 10:22:28 -04002902 return 0;
2903
2904out_free_locality_groups:
2905 free_percpu(sbi->s_locality_groups);
2906 sbi->s_locality_groups = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002907out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002908 kfree(sbi->s_mb_offsets);
2909 sbi->s_mb_offsets = NULL;
2910 kfree(sbi->s_mb_maxs);
2911 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002912 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002913}
2914
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002915/* need to called with the ext4 group lock held */
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302916static int ext4_mb_cleanup_pa(struct ext4_group_info *grp)
Alex Tomasc9de5602008-01-29 00:19:52 -05002917{
2918 struct ext4_prealloc_space *pa;
2919 struct list_head *cur, *tmp;
2920 int count = 0;
2921
2922 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2923 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2924 list_del(&pa->pa_group_list);
2925 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002926 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002927 }
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302928 return count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002929}
2930
2931int ext4_mb_release(struct super_block *sb)
2932{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002933 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002934 ext4_group_t i;
2935 int num_meta_group_infos;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002936 struct ext4_group_info *grinfo, ***group_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002937 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002938 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302939 int count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002940
Alex Tomasc9de5602008-01-29 00:19:52 -05002941 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002942 for (i = 0; i < ngroups; i++) {
Khazhismel Kumykov4b99faa2019-04-25 12:58:01 -04002943 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05002944 grinfo = ext4_get_group_info(sb, i);
Ritesh Harjania3450212020-05-10 11:54:48 +05302945 mb_group_bb_bitmap_free(grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002946 ext4_lock_group(sb, i);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302947 count = ext4_mb_cleanup_pa(grinfo);
2948 if (count)
2949 mb_debug(sb, "mballoc: %d PAs left\n",
2950 count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002951 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002952 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002953 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002954 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002955 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2956 EXT4_DESC_PER_BLOCK_BITS(sb);
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002957 rcu_read_lock();
2958 group_info = rcu_dereference(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002959 for (i = 0; i < num_meta_group_infos; i++)
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002960 kfree(group_info[i]);
2961 kvfree(group_info);
2962 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002963 }
2964 kfree(sbi->s_mb_offsets);
2965 kfree(sbi->s_mb_maxs);
Markus Elfringbfcba2d2014-11-25 20:01:37 -05002966 iput(sbi->s_buddy_cache);
Alex Tomasc9de5602008-01-29 00:19:52 -05002967 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002968 ext4_msg(sb, KERN_INFO,
2969 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002970 atomic_read(&sbi->s_bal_allocated),
2971 atomic_read(&sbi->s_bal_reqs),
2972 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002973 ext4_msg(sb, KERN_INFO,
2974 "mballoc: %u extents scanned, %u goal hits, "
2975 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002976 atomic_read(&sbi->s_bal_ex_scanned),
2977 atomic_read(&sbi->s_bal_goals),
2978 atomic_read(&sbi->s_bal_2orders),
2979 atomic_read(&sbi->s_bal_breaks),
2980 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002981 ext4_msg(sb, KERN_INFO,
2982 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002983 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002984 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002985 ext4_msg(sb, KERN_INFO,
2986 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002987 atomic_read(&sbi->s_mb_preallocated),
2988 atomic_read(&sbi->s_mb_discarded));
2989 }
2990
Eric Sandeen730c2132008-09-13 15:23:29 -04002991 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002992
2993 return 0;
2994}
2995
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002996static inline int ext4_issue_discard(struct super_block *sb,
Daeho Jeonga0154342017-06-22 23:54:33 -04002997 ext4_group_t block_group, ext4_grpblk_t cluster, int count,
2998 struct bio **biop)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002999{
Jiaying Zhang5c521832010-07-27 11:56:05 -04003000 ext4_fsblk_t discard_block;
3001
Theodore Ts'o84130192011-09-09 18:50:51 -04003002 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
3003 ext4_group_first_block_no(sb, block_group));
3004 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04003005 trace_ext4_discard_blocks(sb,
3006 (unsigned long long) discard_block, count);
Daeho Jeonga0154342017-06-22 23:54:33 -04003007 if (biop) {
3008 return __blkdev_issue_discard(sb->s_bdev,
3009 (sector_t)discard_block << (sb->s_blocksize_bits - 9),
3010 (sector_t)count << (sb->s_blocksize_bits - 9),
3011 GFP_NOFS, 0, biop);
3012 } else
3013 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04003014}
3015
Daeho Jeonga0154342017-06-22 23:54:33 -04003016static void ext4_free_data_in_buddy(struct super_block *sb,
3017 struct ext4_free_data *entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05003018{
Alex Tomasc9de5602008-01-29 00:19:52 -05003019 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04003020 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04003021 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003022
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303023 mb_debug(sb, "gonna free %u blocks in group %u (0x%p):",
Bobi Jam18aadd42012-02-20 17:53:02 -05003024 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05003025
Bobi Jam18aadd42012-02-20 17:53:02 -05003026 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
3027 /* we expect to find existing buddy because it's pinned */
3028 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04003029
Theodore Ts'od08854f2016-06-26 18:24:01 -04003030 spin_lock(&EXT4_SB(sb)->s_md_lock);
3031 EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
3032 spin_unlock(&EXT4_SB(sb)->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003033
Bobi Jam18aadd42012-02-20 17:53:02 -05003034 db = e4b.bd_info;
3035 /* there are blocks to put in buddy to make them really free */
3036 count += entry->efd_count;
3037 count2++;
3038 ext4_lock_group(sb, entry->efd_group);
3039 /* Take it out of per group rb tree */
3040 rb_erase(&entry->efd_node, &(db->bb_free_root));
3041 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04003042
Bobi Jam18aadd42012-02-20 17:53:02 -05003043 /*
3044 * Clear the trimmed flag for the group so that the next
3045 * ext4_trim_fs can trim it.
3046 * If the volume is mounted with -o discard, online discard
3047 * is supported and the free blocks will be trimmed online.
3048 */
3049 if (!test_opt(sb, DISCARD))
3050 EXT4_MB_GRP_CLEAR_TRIMMED(db);
3051
3052 if (!db->bb_free_root.rb_node) {
3053 /* No more items in the per group rb tree
3054 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04003055 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003056 put_page(e4b.bd_buddy_page);
3057 put_page(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04003058 }
Bobi Jam18aadd42012-02-20 17:53:02 -05003059 ext4_unlock_group(sb, entry->efd_group);
3060 kmem_cache_free(ext4_free_data_cachep, entry);
3061 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003062
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303063 mb_debug(sb, "freed %d blocks in %d structures\n", count,
3064 count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003065}
3066
Daeho Jeonga0154342017-06-22 23:54:33 -04003067/*
3068 * This function is called by the jbd2 layer once the commit has finished,
3069 * so we know we can free the blocks that were released with that commit.
3070 */
3071void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid)
3072{
3073 struct ext4_sb_info *sbi = EXT4_SB(sb);
3074 struct ext4_free_data *entry, *tmp;
3075 struct bio *discard_bio = NULL;
3076 struct list_head freed_data_list;
3077 struct list_head *cut_pos = NULL;
3078 int err;
3079
3080 INIT_LIST_HEAD(&freed_data_list);
3081
3082 spin_lock(&sbi->s_md_lock);
3083 list_for_each_entry(entry, &sbi->s_freed_data_list, efd_list) {
3084 if (entry->efd_tid != commit_tid)
3085 break;
3086 cut_pos = &entry->efd_list;
3087 }
3088 if (cut_pos)
3089 list_cut_position(&freed_data_list, &sbi->s_freed_data_list,
3090 cut_pos);
3091 spin_unlock(&sbi->s_md_lock);
3092
3093 if (test_opt(sb, DISCARD)) {
3094 list_for_each_entry(entry, &freed_data_list, efd_list) {
3095 err = ext4_issue_discard(sb, entry->efd_group,
3096 entry->efd_start_cluster,
3097 entry->efd_count,
3098 &discard_bio);
3099 if (err && err != -EOPNOTSUPP) {
3100 ext4_msg(sb, KERN_WARNING, "discard request in"
3101 " group:%d block:%d count:%d failed"
3102 " with %d", entry->efd_group,
3103 entry->efd_start_cluster,
3104 entry->efd_count, err);
3105 } else if (err == -EOPNOTSUPP)
3106 break;
3107 }
3108
Daeho Jeonge4510572017-08-05 13:11:57 -04003109 if (discard_bio) {
Daeho Jeonga0154342017-06-22 23:54:33 -04003110 submit_bio_wait(discard_bio);
Daeho Jeonge4510572017-08-05 13:11:57 -04003111 bio_put(discard_bio);
3112 }
Daeho Jeonga0154342017-06-22 23:54:33 -04003113 }
3114
3115 list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list)
3116 ext4_free_data_in_buddy(sb, entry);
3117}
3118
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04003119int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05003120{
Theodore Ts'o16828082010-10-27 21:30:09 -04003121 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
3122 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05003123 if (ext4_pspace_cachep == NULL)
Ritesh Harjanif2835292020-05-10 11:54:46 +05303124 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05003125
Theodore Ts'o16828082010-10-27 21:30:09 -04003126 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
3127 SLAB_RECLAIM_ACCOUNT);
Ritesh Harjanif2835292020-05-10 11:54:46 +05303128 if (ext4_ac_cachep == NULL)
3129 goto out_pa_free;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04003130
Bobi Jam18aadd42012-02-20 17:53:02 -05003131 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
3132 SLAB_RECLAIM_ACCOUNT);
Ritesh Harjanif2835292020-05-10 11:54:46 +05303133 if (ext4_free_data_cachep == NULL)
3134 goto out_ac_free;
3135
Alex Tomasc9de5602008-01-29 00:19:52 -05003136 return 0;
Ritesh Harjanif2835292020-05-10 11:54:46 +05303137
3138out_ac_free:
3139 kmem_cache_destroy(ext4_ac_cachep);
3140out_pa_free:
3141 kmem_cache_destroy(ext4_pspace_cachep);
3142out:
3143 return -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -05003144}
3145
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04003146void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05003147{
Theodore Ts'o60e66792010-05-17 07:00:00 -04003148 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04003149 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
3150 * before destroying the slab cache.
3151 */
3152 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05003153 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003154 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05003155 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05003156 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05003157}
3158
3159
3160/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02003161 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05003162 * Returns 0 if success or error code
3163 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003164static noinline_for_stack int
3165ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003166 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05003167{
3168 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05003169 struct ext4_group_desc *gdp;
3170 struct buffer_head *gdp_bh;
3171 struct ext4_sb_info *sbi;
3172 struct super_block *sb;
3173 ext4_fsblk_t block;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003174 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003175
3176 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3177 BUG_ON(ac->ac_b_ex.fe_len <= 0);
3178
3179 sb = ac->ac_sb;
3180 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003181
Theodore Ts'o574ca172008-07-11 19:27:31 -04003182 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04003183 if (IS_ERR(bitmap_bh)) {
3184 err = PTR_ERR(bitmap_bh);
3185 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05003186 goto out_err;
Darrick J. Wong9008a582015-10-17 21:33:24 -04003187 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003188
liang xie5d601252014-05-12 22:06:43 -04003189 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05003190 err = ext4_journal_get_write_access(handle, bitmap_bh);
3191 if (err)
3192 goto out_err;
3193
3194 err = -EIO;
3195 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
3196 if (!gdp)
3197 goto out_err;
3198
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003199 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003200 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003201
liang xie5d601252014-05-12 22:06:43 -04003202 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05003203 err = ext4_journal_get_write_access(handle, gdp_bh);
3204 if (err)
3205 goto out_err;
3206
Akinobu Mitabda00de2010-03-03 23:53:25 -05003207 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05003208
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003209 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Jan Karace9f24c2020-07-28 15:04:34 +02003210 if (!ext4_inode_block_valid(ac->ac_inode, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003211 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04003212 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003213 /* File system mounted not to panic on error
Vegard Nossum554a5cc2016-07-14 23:02:47 -04003214 * Fix the bitmap and return EFSCORRUPTED
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003215 * We leak some of the blocks here.
3216 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003217 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003218 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3219 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003220 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05003221 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003222 if (!err)
Vegard Nossum554a5cc2016-07-14 23:02:47 -04003223 err = -EFSCORRUPTED;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003224 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003225 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003226
3227 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003228#ifdef AGGRESSIVE_CHECK
3229 {
3230 int i;
3231 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3232 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3233 bitmap_bh->b_data));
3234 }
3235 }
3236#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003237 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3238 ac->ac_b_ex.fe_len);
Theodore Ts'o88446182018-06-14 00:58:00 -04003239 if (ext4_has_group_desc_csum(sb) &&
3240 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003241 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003242 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04003243 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003244 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05003245 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003246 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
3247 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04003248 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003249 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003250
3251 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04003252 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003253 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003254 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003255 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003256 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3257 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04003258 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
3259 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05003260
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003261 if (sbi->s_log_groups_per_flex) {
3262 ext4_group_t flex_group = ext4_flex_group(sbi,
3263 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04003264 atomic64_sub(ac->ac_b_ex.fe_len,
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08003265 &sbi_array_rcu_deref(sbi, s_flex_groups,
3266 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003267 }
3268
Frank Mayhar03901312009-01-07 00:06:22 -05003269 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003270 if (err)
3271 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003272 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003273
3274out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003275 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003276 return err;
3277}
3278
3279/*
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07003280 * Idempotent helper for Ext4 fast commit replay path to set the state of
3281 * blocks in bitmaps and update counters.
3282 */
3283void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
3284 int len, int state)
3285{
3286 struct buffer_head *bitmap_bh = NULL;
3287 struct ext4_group_desc *gdp;
3288 struct buffer_head *gdp_bh;
3289 struct ext4_sb_info *sbi = EXT4_SB(sb);
3290 ext4_group_t group;
3291 ext4_grpblk_t blkoff;
3292 int i, clen, err;
3293 int already;
3294
3295 clen = EXT4_B2C(sbi, len);
3296
3297 ext4_get_group_no_and_offset(sb, block, &group, &blkoff);
3298 bitmap_bh = ext4_read_block_bitmap(sb, group);
3299 if (IS_ERR(bitmap_bh)) {
3300 err = PTR_ERR(bitmap_bh);
3301 bitmap_bh = NULL;
3302 goto out_err;
3303 }
3304
3305 err = -EIO;
3306 gdp = ext4_get_group_desc(sb, group, &gdp_bh);
3307 if (!gdp)
3308 goto out_err;
3309
3310 ext4_lock_group(sb, group);
3311 already = 0;
3312 for (i = 0; i < clen; i++)
3313 if (!mb_test_bit(blkoff + i, bitmap_bh->b_data) == !state)
3314 already++;
3315
3316 if (state)
3317 ext4_set_bits(bitmap_bh->b_data, blkoff, clen);
3318 else
3319 mb_test_and_clear_bits(bitmap_bh->b_data, blkoff, clen);
3320 if (ext4_has_group_desc_csum(sb) &&
3321 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
3322 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
3323 ext4_free_group_clusters_set(sb, gdp,
3324 ext4_free_clusters_after_init(sb,
3325 group, gdp));
3326 }
3327 if (state)
3328 clen = ext4_free_group_clusters(sb, gdp) - clen + already;
3329 else
3330 clen = ext4_free_group_clusters(sb, gdp) + clen - already;
3331
3332 ext4_free_group_clusters_set(sb, gdp, clen);
3333 ext4_block_bitmap_csum_set(sb, group, gdp, bitmap_bh);
3334 ext4_group_desc_csum_set(sb, group, gdp);
3335
3336 ext4_unlock_group(sb, group);
3337
3338 if (sbi->s_log_groups_per_flex) {
3339 ext4_group_t flex_group = ext4_flex_group(sbi, group);
3340
3341 atomic64_sub(len,
3342 &sbi_array_rcu_deref(sbi, s_flex_groups,
3343 flex_group)->free_clusters);
3344 }
3345
3346 err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh);
3347 if (err)
3348 goto out_err;
3349 sync_dirty_buffer(bitmap_bh);
3350 err = ext4_handle_dirty_metadata(NULL, NULL, gdp_bh);
3351 sync_dirty_buffer(gdp_bh);
3352
3353out_err:
3354 brelse(bitmap_bh);
3355}
3356
3357/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003358 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003359 * Group request are normalized to s_mb_group_prealloc, which goes to
3360 * s_strip if we set the same via mount option.
3361 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003362 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003363 *
3364 * XXX: should we try to preallocate more than the group has now?
3365 */
3366static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3367{
3368 struct super_block *sb = ac->ac_sb;
3369 struct ext4_locality_group *lg = ac->ac_lg;
3370
3371 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003372 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303373 mb_debug(sb, "goal %u blocks for locality group\n", ac->ac_g_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003374}
3375
3376/*
3377 * Normalization means making request better in terms of
3378 * size and alignment
3379 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003380static noinline_for_stack void
3381ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003382 struct ext4_allocation_request *ar)
3383{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003384 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003385 int bsbits, max;
3386 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003387 loff_t size, start_off;
3388 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003389 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003390 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003391 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003392
3393 /* do normalize only data requests, metadata requests
3394 do not need preallocation */
3395 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3396 return;
3397
3398 /* sometime caller may want exact blocks */
3399 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3400 return;
3401
3402 /* caller may indicate that preallocation isn't
3403 * required (it's a tail, for example) */
3404 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3405 return;
3406
3407 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3408 ext4_mb_normalize_group_request(ac);
3409 return ;
3410 }
3411
3412 bsbits = ac->ac_sb->s_blocksize_bits;
3413
3414 /* first, let's learn actual file size
3415 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003416 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003417 size = size << bsbits;
3418 if (size < i_size_read(ac->ac_inode))
3419 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003420 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003421
Valerie Clement19304792008-05-13 19:31:14 -04003422 /* max size of free chunks */
3423 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003424
Valerie Clement19304792008-05-13 19:31:14 -04003425#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3426 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003427
3428 /* first, try to predict filesize */
3429 /* XXX: should this table be tunable? */
3430 start_off = 0;
3431 if (size <= 16 * 1024) {
3432 size = 16 * 1024;
3433 } else if (size <= 32 * 1024) {
3434 size = 32 * 1024;
3435 } else if (size <= 64 * 1024) {
3436 size = 64 * 1024;
3437 } else if (size <= 128 * 1024) {
3438 size = 128 * 1024;
3439 } else if (size <= 256 * 1024) {
3440 size = 256 * 1024;
3441 } else if (size <= 512 * 1024) {
3442 size = 512 * 1024;
3443 } else if (size <= 1024 * 1024) {
3444 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003445 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003446 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003447 (21 - bsbits)) << 21;
3448 size = 2 * 1024 * 1024;
3449 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003450 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3451 (22 - bsbits)) << 22;
3452 size = 4 * 1024 * 1024;
3453 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003454 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003455 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3456 (23 - bsbits)) << 23;
3457 size = 8 * 1024 * 1024;
3458 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003459 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3460 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3461 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003462 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003463 size = size >> bsbits;
3464 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003465
3466 /* don't cover already allocated blocks in selected range */
3467 if (ar->pleft && start <= ar->lleft) {
3468 size -= ar->lleft + 1 - start;
3469 start = ar->lleft + 1;
3470 }
3471 if (ar->pright && start + size - 1 >= ar->lright)
3472 size -= start + size - ar->lright;
3473
Jan Karacd648b82017-01-27 14:34:30 -05003474 /*
3475 * Trim allocation request for filesystems with artificially small
3476 * groups.
3477 */
3478 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
3479 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
3480
Alex Tomasc9de5602008-01-29 00:19:52 -05003481 end = start + size;
3482
3483 /* check we don't cross already preallocated blocks */
3484 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003485 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003486 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003487
Alex Tomasc9de5602008-01-29 00:19:52 -05003488 if (pa->pa_deleted)
3489 continue;
3490 spin_lock(&pa->pa_lock);
3491 if (pa->pa_deleted) {
3492 spin_unlock(&pa->pa_lock);
3493 continue;
3494 }
3495
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003496 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3497 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003498
3499 /* PA must not overlap original request */
3500 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3501 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3502
Eric Sandeen38877f42009-08-17 23:55:24 -04003503 /* skip PAs this normalized request doesn't overlap with */
3504 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003505 spin_unlock(&pa->pa_lock);
3506 continue;
3507 }
3508 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3509
Eric Sandeen38877f42009-08-17 23:55:24 -04003510 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003511 if (pa_end <= ac->ac_o_ex.fe_logical) {
3512 BUG_ON(pa_end < start);
3513 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003514 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003515 BUG_ON(pa->pa_lstart > end);
3516 end = pa->pa_lstart;
3517 }
3518 spin_unlock(&pa->pa_lock);
3519 }
3520 rcu_read_unlock();
3521 size = end - start;
3522
3523 /* XXX: extra loop to check we really don't overlap preallocations */
3524 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003525 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003526 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003527
Alex Tomasc9de5602008-01-29 00:19:52 -05003528 spin_lock(&pa->pa_lock);
3529 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003530 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3531 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003532 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3533 }
3534 spin_unlock(&pa->pa_lock);
3535 }
3536 rcu_read_unlock();
3537
3538 if (start + size <= ac->ac_o_ex.fe_logical &&
3539 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003540 ext4_msg(ac->ac_sb, KERN_ERR,
3541 "start %lu, size %lu, fe_logical %lu",
3542 (unsigned long) start, (unsigned long) size,
3543 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003544 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003545 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003546 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003547
3548 /* now prepare goal request */
3549
3550 /* XXX: is it better to align blocks WRT to logical
3551 * placement or satisfy big request as is */
3552 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003553 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003554
3555 /* define goal start in order to merge */
3556 if (ar->pright && (ar->lright == (start + size))) {
3557 /* merge to the right */
3558 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3559 &ac->ac_f_ex.fe_group,
3560 &ac->ac_f_ex.fe_start);
3561 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3562 }
3563 if (ar->pleft && (ar->lleft + 1 == start)) {
3564 /* merge to the left */
3565 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3566 &ac->ac_f_ex.fe_group,
3567 &ac->ac_f_ex.fe_start);
3568 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3569 }
3570
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303571 mb_debug(ac->ac_sb, "goal: %lld(was %lld) blocks at %u\n", size,
3572 orig_size, start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003573}
3574
3575static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3576{
3577 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3578
3579 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3580 atomic_inc(&sbi->s_bal_reqs);
3581 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003582 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003583 atomic_inc(&sbi->s_bal_success);
3584 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3585 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3586 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3587 atomic_inc(&sbi->s_bal_goals);
3588 if (ac->ac_found > sbi->s_mb_max_to_scan)
3589 atomic_inc(&sbi->s_bal_breaks);
3590 }
3591
Theodore Ts'o296c3552009-09-30 00:32:42 -04003592 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3593 trace_ext4_mballoc_alloc(ac);
3594 else
3595 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003596}
3597
3598/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003599 * Called on failure; free up any blocks from the inode PA for this
3600 * context. We don't need this for MB_GROUP_PA because we only change
3601 * pa_free in ext4_mb_release_context(), but on failure, we've already
3602 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3603 */
3604static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3605{
3606 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003607 struct ext4_buddy e4b;
3608 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003609
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003610 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003611 if (ac->ac_f_ex.fe_len == 0)
3612 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003613 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3614 if (err) {
3615 /*
3616 * This should never happen since we pin the
3617 * pages in the ext4_allocation_context so
3618 * ext4_mb_load_buddy() should never fail.
3619 */
3620 WARN(1, "mb_load_buddy failed (%d)", err);
3621 return;
3622 }
3623 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3624 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3625 ac->ac_f_ex.fe_len);
3626 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003627 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003628 return;
3629 }
3630 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003631 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003632}
3633
3634/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003635 * use blocks preallocated to inode
3636 */
3637static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3638 struct ext4_prealloc_space *pa)
3639{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003640 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003641 ext4_fsblk_t start;
3642 ext4_fsblk_t end;
3643 int len;
3644
3645 /* found preallocated blocks, use them */
3646 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003647 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3648 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3649 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003650 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3651 &ac->ac_b_ex.fe_start);
3652 ac->ac_b_ex.fe_len = len;
3653 ac->ac_status = AC_STATUS_FOUND;
3654 ac->ac_pa = pa;
3655
3656 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003657 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003658 BUG_ON(pa->pa_free < len);
3659 pa->pa_free -= len;
3660
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303661 mb_debug(ac->ac_sb, "use %llu/%d from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003662}
3663
3664/*
3665 * use blocks preallocated to locality group
3666 */
3667static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3668 struct ext4_prealloc_space *pa)
3669{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003670 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003671
Alex Tomasc9de5602008-01-29 00:19:52 -05003672 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3673 &ac->ac_b_ex.fe_group,
3674 &ac->ac_b_ex.fe_start);
3675 ac->ac_b_ex.fe_len = len;
3676 ac->ac_status = AC_STATUS_FOUND;
3677 ac->ac_pa = pa;
3678
3679 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003680 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003681 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003682 * in on-disk bitmap -- see ext4_mb_release_context()
3683 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003684 */
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303685 mb_debug(ac->ac_sb, "use %u/%u from group pa %p\n",
3686 pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003687}
3688
3689/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003690 * Return the prealloc space that have minimal distance
3691 * from the goal block. @cpa is the prealloc
3692 * space that is having currently known minimal distance
3693 * from the goal block.
3694 */
3695static struct ext4_prealloc_space *
3696ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3697 struct ext4_prealloc_space *pa,
3698 struct ext4_prealloc_space *cpa)
3699{
3700 ext4_fsblk_t cur_distance, new_distance;
3701
3702 if (cpa == NULL) {
3703 atomic_inc(&pa->pa_count);
3704 return pa;
3705 }
Andrew Morton79211c82015-11-09 14:58:13 -08003706 cur_distance = abs(goal_block - cpa->pa_pstart);
3707 new_distance = abs(goal_block - pa->pa_pstart);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003708
Coly Li5a54b2f2011-02-24 14:10:05 -05003709 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003710 return cpa;
3711
3712 /* drop the previous reference */
3713 atomic_dec(&cpa->pa_count);
3714 atomic_inc(&pa->pa_count);
3715 return pa;
3716}
3717
3718/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003719 * search goal blocks in preallocated space
3720 */
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303721static noinline_for_stack bool
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003722ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003723{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003724 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003725 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003726 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3727 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003728 struct ext4_prealloc_space *pa, *cpa = NULL;
3729 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003730
3731 /* only data can be preallocated */
3732 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303733 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05003734
3735 /* first, try per-file preallocation */
3736 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003737 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003738
3739 /* all fields in this condition don't change,
3740 * so we can skip locking for them */
3741 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003742 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3743 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003744 continue;
3745
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003746 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003747 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003748 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3749 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003750 continue;
3751
Alex Tomasc9de5602008-01-29 00:19:52 -05003752 /* found preallocated blocks, use them */
3753 spin_lock(&pa->pa_lock);
3754 if (pa->pa_deleted == 0 && pa->pa_free) {
3755 atomic_inc(&pa->pa_count);
3756 ext4_mb_use_inode_pa(ac, pa);
3757 spin_unlock(&pa->pa_lock);
3758 ac->ac_criteria = 10;
3759 rcu_read_unlock();
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303760 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05003761 }
3762 spin_unlock(&pa->pa_lock);
3763 }
3764 rcu_read_unlock();
3765
3766 /* can we use group allocation? */
3767 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303768 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05003769
3770 /* inode may have no locality group for some reason */
3771 lg = ac->ac_lg;
3772 if (lg == NULL)
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303773 return false;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003774 order = fls(ac->ac_o_ex.fe_len) - 1;
3775 if (order > PREALLOC_TB_SIZE - 1)
3776 /* The max size of hash table is PREALLOC_TB_SIZE */
3777 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003778
Akinobu Mitabda00de2010-03-03 23:53:25 -05003779 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003780 /*
3781 * search for the prealloc space that is having
3782 * minimal distance from the goal block.
3783 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003784 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3785 rcu_read_lock();
3786 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3787 pa_inode_list) {
3788 spin_lock(&pa->pa_lock);
3789 if (pa->pa_deleted == 0 &&
3790 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003791
3792 cpa = ext4_mb_check_group_pa(goal_block,
3793 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003794 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003795 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003796 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003797 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003798 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003799 if (cpa) {
3800 ext4_mb_use_group_pa(ac, cpa);
3801 ac->ac_criteria = 20;
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303802 return true;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003803 }
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303804 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05003805}
3806
3807/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003808 * the function goes through all block freed in the group
3809 * but not yet committed and marks them used in in-core bitmap.
3810 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003811 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003812 */
3813static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3814 ext4_group_t group)
3815{
3816 struct rb_node *n;
3817 struct ext4_group_info *grp;
3818 struct ext4_free_data *entry;
3819
3820 grp = ext4_get_group_info(sb, group);
3821 n = rb_first(&(grp->bb_free_root));
3822
3823 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003824 entry = rb_entry(n, struct ext4_free_data, efd_node);
3825 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003826 n = rb_next(n);
3827 }
3828 return;
3829}
3830
3831/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003832 * the function goes through all preallocation in this group and marks them
3833 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003834 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003835 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003836static noinline_for_stack
3837void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003838 ext4_group_t group)
3839{
3840 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3841 struct ext4_prealloc_space *pa;
3842 struct list_head *cur;
3843 ext4_group_t groupnr;
3844 ext4_grpblk_t start;
3845 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003846 int len;
3847
3848 /* all form of preallocation discards first load group,
3849 * so the only competing code is preallocation use.
3850 * we don't need any locking here
3851 * notice we do NOT ignore preallocations with pa_deleted
3852 * otherwise we could leave used blocks available for
3853 * allocation in buddy when concurrent ext4_mb_put_pa()
3854 * is dropping preallocation
3855 */
3856 list_for_each(cur, &grp->bb_prealloc_list) {
3857 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3858 spin_lock(&pa->pa_lock);
3859 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3860 &groupnr, &start);
3861 len = pa->pa_len;
3862 spin_unlock(&pa->pa_lock);
3863 if (unlikely(len == 0))
3864 continue;
3865 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003866 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003867 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003868 }
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303869 mb_debug(sb, "preallocated %d for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003870}
3871
brookxu27bc4462020-08-17 15:36:15 +08003872static void ext4_mb_mark_pa_deleted(struct super_block *sb,
3873 struct ext4_prealloc_space *pa)
3874{
3875 struct ext4_inode_info *ei;
3876
3877 if (pa->pa_deleted) {
3878 ext4_warning(sb, "deleted pa, type:%d, pblk:%llu, lblk:%u, len:%d\n",
3879 pa->pa_type, pa->pa_pstart, pa->pa_lstart,
3880 pa->pa_len);
3881 return;
3882 }
3883
3884 pa->pa_deleted = 1;
3885
3886 if (pa->pa_type == MB_INODE_PA) {
3887 ei = EXT4_I(pa->pa_inode);
3888 atomic_dec(&ei->i_prealloc_active);
3889 }
3890}
3891
Alex Tomasc9de5602008-01-29 00:19:52 -05003892static void ext4_mb_pa_callback(struct rcu_head *head)
3893{
3894 struct ext4_prealloc_space *pa;
3895 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003896
3897 BUG_ON(atomic_read(&pa->pa_count));
3898 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003899 kmem_cache_free(ext4_pspace_cachep, pa);
3900}
3901
3902/*
3903 * drops a reference to preallocated space descriptor
3904 * if this was the last reference and the space is consumed
3905 */
3906static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3907 struct super_block *sb, struct ext4_prealloc_space *pa)
3908{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003909 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003910 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003911
Alex Tomasc9de5602008-01-29 00:19:52 -05003912 /* in this short window concurrent discard can set pa_deleted */
3913 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003914 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3915 spin_unlock(&pa->pa_lock);
3916 return;
3917 }
3918
Alex Tomasc9de5602008-01-29 00:19:52 -05003919 if (pa->pa_deleted == 1) {
3920 spin_unlock(&pa->pa_lock);
3921 return;
3922 }
3923
brookxu27bc4462020-08-17 15:36:15 +08003924 ext4_mb_mark_pa_deleted(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003925 spin_unlock(&pa->pa_lock);
3926
Eric Sandeend33a1972009-03-16 23:25:40 -04003927 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003928 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003929 * If doing group-based preallocation, pa_pstart may be in the
3930 * next group when pa is used up
3931 */
3932 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003933 grp_blk--;
3934
Lukas Czernerbd862982013-04-03 23:32:34 -04003935 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003936
3937 /*
3938 * possible race:
3939 *
3940 * P1 (buddy init) P2 (regular allocation)
3941 * find block B in PA
3942 * copy on-disk bitmap to buddy
3943 * mark B in on-disk bitmap
3944 * drop PA from group
3945 * mark all PAs in buddy
3946 *
3947 * thus, P1 initializes buddy with B available. to prevent this
3948 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3949 * against that pair
3950 */
3951 ext4_lock_group(sb, grp);
3952 list_del(&pa->pa_group_list);
3953 ext4_unlock_group(sb, grp);
3954
3955 spin_lock(pa->pa_obj_lock);
3956 list_del_rcu(&pa->pa_inode_list);
3957 spin_unlock(pa->pa_obj_lock);
3958
3959 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3960}
3961
3962/*
3963 * creates new preallocated space for given inode
3964 */
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303965static noinline_for_stack void
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003966ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003967{
3968 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003969 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003970 struct ext4_prealloc_space *pa;
3971 struct ext4_group_info *grp;
3972 struct ext4_inode_info *ei;
3973
3974 /* preallocate only when found space is larger then requested */
3975 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3976 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3977 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303978 BUG_ON(ac->ac_pa == NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003979
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303980 pa = ac->ac_pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003981
3982 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3983 int winl;
3984 int wins;
3985 int win;
3986 int offs;
3987
3988 /* we can't allocate as much as normalizer wants.
3989 * so, found space must get proper lstart
3990 * to cover original request */
3991 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3992 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3993
3994 /* we're limited by original request in that
3995 * logical block must be covered any way
3996 * winl is window we can move our chunk within */
3997 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3998
3999 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004000 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004001
4002 /* the smallest one defines real window */
4003 win = min(winl, wins);
4004
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004005 offs = ac->ac_o_ex.fe_logical %
4006 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004007 if (offs && offs < win)
4008 win = offs;
4009
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004010 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05004011 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05004012 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
4013 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
4014 }
4015
4016 /* preallocation can change ac_b_ex, thus we store actually
4017 * allocated blocks for history */
4018 ac->ac_f_ex = ac->ac_b_ex;
4019
4020 pa->pa_lstart = ac->ac_b_ex.fe_logical;
4021 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4022 pa->pa_len = ac->ac_b_ex.fe_len;
4023 pa->pa_free = pa->pa_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004024 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05004025 INIT_LIST_HEAD(&pa->pa_inode_list);
4026 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05004027 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004028 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004029
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304030 mb_debug(sb, "new inode pa %p: %llu/%d for %u\n", pa, pa->pa_pstart,
4031 pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004032 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004033
4034 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004035 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05004036
4037 ei = EXT4_I(ac->ac_inode);
4038 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
4039
4040 pa->pa_obj_lock = &ei->i_prealloc_lock;
4041 pa->pa_inode = ac->ac_inode;
4042
Alex Tomasc9de5602008-01-29 00:19:52 -05004043 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05004044
4045 spin_lock(pa->pa_obj_lock);
4046 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
4047 spin_unlock(pa->pa_obj_lock);
brookxu27bc4462020-08-17 15:36:15 +08004048 atomic_inc(&ei->i_prealloc_active);
Alex Tomasc9de5602008-01-29 00:19:52 -05004049}
4050
4051/*
4052 * creates new preallocated space for locality group inodes belongs to
4053 */
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304054static noinline_for_stack void
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004055ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05004056{
4057 struct super_block *sb = ac->ac_sb;
4058 struct ext4_locality_group *lg;
4059 struct ext4_prealloc_space *pa;
4060 struct ext4_group_info *grp;
4061
4062 /* preallocate only when found space is larger then requested */
4063 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
4064 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
4065 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304066 BUG_ON(ac->ac_pa == NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05004067
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304068 pa = ac->ac_pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05004069
4070 /* preallocation can change ac_b_ex, thus we store actually
4071 * allocated blocks for history */
4072 ac->ac_f_ex = ac->ac_b_ex;
4073
4074 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4075 pa->pa_lstart = pa->pa_pstart;
4076 pa->pa_len = ac->ac_b_ex.fe_len;
4077 pa->pa_free = pa->pa_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004078 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004079 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05004080 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05004081 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004082 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004083
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304084 mb_debug(sb, "new group pa %p: %llu/%d for %u\n", pa, pa->pa_pstart,
4085 pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004086 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004087
4088 ext4_mb_use_group_pa(ac, pa);
4089 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
4090
4091 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
4092 lg = ac->ac_lg;
4093 BUG_ON(lg == NULL);
4094
4095 pa->pa_obj_lock = &lg->lg_prealloc_lock;
4096 pa->pa_inode = NULL;
4097
Alex Tomasc9de5602008-01-29 00:19:52 -05004098 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05004099
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004100 /*
4101 * We will later add the new pa to the right bucket
4102 * after updating the pa_free in ext4_mb_release_context
4103 */
Alex Tomasc9de5602008-01-29 00:19:52 -05004104}
4105
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304106static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05004107{
Alex Tomasc9de5602008-01-29 00:19:52 -05004108 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304109 ext4_mb_new_group_pa(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004110 else
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304111 ext4_mb_new_inode_pa(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004112}
4113
4114/*
4115 * finds all unused blocks in on-disk bitmap, frees them in
4116 * in-core bitmap and buddy.
4117 * @pa must be unlinked from inode and group lists, so that
4118 * nobody else can find/use it.
4119 * the caller MUST hold group/inode locks.
4120 * TODO: optimize the case when there are no in-core structures yet
4121 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004122static noinline_for_stack int
4123ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004124 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05004125{
Alex Tomasc9de5602008-01-29 00:19:52 -05004126 struct super_block *sb = e4b->bd_sb;
4127 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004128 unsigned int end;
4129 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05004130 ext4_group_t group;
4131 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05004132 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05004133 int free = 0;
4134
4135 BUG_ON(pa->pa_deleted == 0);
4136 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004137 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05004138 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
4139 end = bit + pa->pa_len;
4140
Alex Tomasc9de5602008-01-29 00:19:52 -05004141 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05004142 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05004143 if (bit >= end)
4144 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05004145 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304146 mb_debug(sb, "free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04004147 (unsigned) ext4_group_first_block_no(sb, group) + bit,
4148 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004149 free += next - bit;
4150
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004151 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004152 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
4153 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04004154 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05004155 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
4156 bit = next + 1;
4157 }
4158 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004159 ext4_msg(e4b->bd_sb, KERN_CRIT,
Ritesh Harjani36bad422020-05-10 11:54:44 +05304160 "pa %p: logic %lu, phys. %lu, len %d",
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004161 pa, (unsigned long) pa->pa_lstart,
4162 (unsigned long) pa->pa_pstart,
Ritesh Harjani36bad422020-05-10 11:54:44 +05304163 pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004164 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05004165 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05004166 /*
4167 * pa is already deleted so we use the value obtained
4168 * from the bitmap and continue.
4169 */
Alex Tomasc9de5602008-01-29 00:19:52 -05004170 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004171 atomic_add(free, &sbi->s_mb_discarded);
4172
zhong jiang863c37f2018-08-04 17:34:07 -04004173 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004174}
4175
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004176static noinline_for_stack int
4177ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004178 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05004179{
Alex Tomasc9de5602008-01-29 00:19:52 -05004180 struct super_block *sb = e4b->bd_sb;
4181 ext4_group_t group;
4182 ext4_grpblk_t bit;
4183
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05004184 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004185 BUG_ON(pa->pa_deleted == 0);
4186 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
4187 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
4188 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
4189 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004190 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004191
4192 return 0;
4193}
4194
4195/*
4196 * releases all preallocations in given group
4197 *
4198 * first, we need to decide discard policy:
4199 * - when do we discard
4200 * 1) ENOSPC
4201 * - how many do we discard
4202 * 1) how many requested
4203 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004204static noinline_for_stack int
4205ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05004206 ext4_group_t group, int needed)
4207{
4208 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
4209 struct buffer_head *bitmap_bh = NULL;
4210 struct ext4_prealloc_space *pa, *tmp;
4211 struct list_head list;
4212 struct ext4_buddy e4b;
4213 int err;
4214 int busy = 0;
Jan Kara5b3dc192020-09-24 17:09:59 +02004215 int free, free_total = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004216
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304217 mb_debug(sb, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004218 if (list_empty(&grp->bb_prealloc_list))
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304219 goto out_dbg;
Alex Tomasc9de5602008-01-29 00:19:52 -05004220
Theodore Ts'o574ca172008-07-11 19:27:31 -04004221 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004222 if (IS_ERR(bitmap_bh)) {
4223 err = PTR_ERR(bitmap_bh);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004224 ext4_error_err(sb, -err,
4225 "Error %d reading block bitmap for %u",
4226 err, group);
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304227 goto out_dbg;
Alex Tomasc9de5602008-01-29 00:19:52 -05004228 }
4229
4230 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004231 if (err) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004232 ext4_warning(sb, "Error %d loading buddy information for %u",
4233 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004234 put_bh(bitmap_bh);
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304235 goto out_dbg;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004236 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004237
4238 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04004239 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05004240
Alex Tomasc9de5602008-01-29 00:19:52 -05004241 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05004242repeat:
Jan Kara5b3dc192020-09-24 17:09:59 +02004243 free = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004244 ext4_lock_group(sb, group);
4245 list_for_each_entry_safe(pa, tmp,
4246 &grp->bb_prealloc_list, pa_group_list) {
4247 spin_lock(&pa->pa_lock);
4248 if (atomic_read(&pa->pa_count)) {
4249 spin_unlock(&pa->pa_lock);
4250 busy = 1;
4251 continue;
4252 }
4253 if (pa->pa_deleted) {
4254 spin_unlock(&pa->pa_lock);
4255 continue;
4256 }
4257
4258 /* seems this one can be freed ... */
brookxu27bc4462020-08-17 15:36:15 +08004259 ext4_mb_mark_pa_deleted(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004260
Ye Bin70022da2020-09-16 19:38:59 +08004261 if (!free)
4262 this_cpu_inc(discard_pa_seq);
4263
Alex Tomasc9de5602008-01-29 00:19:52 -05004264 /* we can trust pa_free ... */
4265 free += pa->pa_free;
4266
4267 spin_unlock(&pa->pa_lock);
4268
4269 list_del(&pa->pa_group_list);
4270 list_add(&pa->u.pa_tmp_list, &list);
4271 }
4272
Alex Tomasc9de5602008-01-29 00:19:52 -05004273 /* now free all selected PAs */
4274 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4275
4276 /* remove from object (inode or locality group) */
4277 spin_lock(pa->pa_obj_lock);
4278 list_del_rcu(&pa->pa_inode_list);
4279 spin_unlock(pa->pa_obj_lock);
4280
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004281 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004282 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004283 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004284 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004285
4286 list_del(&pa->u.pa_tmp_list);
4287 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4288 }
4289
Jan Kara5b3dc192020-09-24 17:09:59 +02004290 free_total += free;
4291
4292 /* if we still need more blocks and some PAs were used, try again */
4293 if (free_total < needed && busy) {
4294 ext4_unlock_group(sb, group);
4295 cond_resched();
4296 busy = 0;
4297 goto repeat;
4298 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004299 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004300 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004301 put_bh(bitmap_bh);
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304302out_dbg:
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304303 mb_debug(sb, "discarded (%d) blocks preallocated for group %u bb_free (%d)\n",
Jan Kara5b3dc192020-09-24 17:09:59 +02004304 free_total, group, grp->bb_free);
4305 return free_total;
Alex Tomasc9de5602008-01-29 00:19:52 -05004306}
4307
4308/*
4309 * releases all non-used preallocated blocks for given inode
4310 *
4311 * It's important to discard preallocations under i_data_sem
4312 * We don't want another block to be served from the prealloc
4313 * space when we are discarding the inode prealloc space.
4314 *
4315 * FIXME!! Make sure it is valid at all the call sites
4316 */
brookxu27bc4462020-08-17 15:36:15 +08004317void ext4_discard_preallocations(struct inode *inode, unsigned int needed)
Alex Tomasc9de5602008-01-29 00:19:52 -05004318{
4319 struct ext4_inode_info *ei = EXT4_I(inode);
4320 struct super_block *sb = inode->i_sb;
4321 struct buffer_head *bitmap_bh = NULL;
4322 struct ext4_prealloc_space *pa, *tmp;
4323 ext4_group_t group = 0;
4324 struct list_head list;
4325 struct ext4_buddy e4b;
4326 int err;
4327
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004328 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004329 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4330 return;
4331 }
4332
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004333 if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
4334 return;
4335
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304336 mb_debug(sb, "discard preallocation for inode %lu\n",
4337 inode->i_ino);
brookxu27bc4462020-08-17 15:36:15 +08004338 trace_ext4_discard_preallocations(inode,
4339 atomic_read(&ei->i_prealloc_active), needed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004340
4341 INIT_LIST_HEAD(&list);
4342
brookxu27bc4462020-08-17 15:36:15 +08004343 if (needed == 0)
4344 needed = UINT_MAX;
4345
Alex Tomasc9de5602008-01-29 00:19:52 -05004346repeat:
4347 /* first, collect all pa's in the inode */
4348 spin_lock(&ei->i_prealloc_lock);
brookxu27bc4462020-08-17 15:36:15 +08004349 while (!list_empty(&ei->i_prealloc_list) && needed) {
4350 pa = list_entry(ei->i_prealloc_list.prev,
Alex Tomasc9de5602008-01-29 00:19:52 -05004351 struct ext4_prealloc_space, pa_inode_list);
4352 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4353 spin_lock(&pa->pa_lock);
4354 if (atomic_read(&pa->pa_count)) {
4355 /* this shouldn't happen often - nobody should
4356 * use preallocation while we're discarding it */
4357 spin_unlock(&pa->pa_lock);
4358 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004359 ext4_msg(sb, KERN_ERR,
4360 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05004361 WARN_ON(1);
4362 schedule_timeout_uninterruptible(HZ);
4363 goto repeat;
4364
4365 }
4366 if (pa->pa_deleted == 0) {
brookxu27bc4462020-08-17 15:36:15 +08004367 ext4_mb_mark_pa_deleted(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004368 spin_unlock(&pa->pa_lock);
4369 list_del_rcu(&pa->pa_inode_list);
4370 list_add(&pa->u.pa_tmp_list, &list);
brookxu27bc4462020-08-17 15:36:15 +08004371 needed--;
Alex Tomasc9de5602008-01-29 00:19:52 -05004372 continue;
4373 }
4374
4375 /* someone is deleting pa right now */
4376 spin_unlock(&pa->pa_lock);
4377 spin_unlock(&ei->i_prealloc_lock);
4378
4379 /* we have to wait here because pa_deleted
4380 * doesn't mean pa is already unlinked from
4381 * the list. as we might be called from
4382 * ->clear_inode() the inode will get freed
4383 * and concurrent thread which is unlinking
4384 * pa from inode's list may access already
4385 * freed memory, bad-bad-bad */
4386
4387 /* XXX: if this happens too often, we can
4388 * add a flag to force wait only in case
4389 * of ->clear_inode(), but not in case of
4390 * regular truncate */
4391 schedule_timeout_uninterruptible(HZ);
4392 goto repeat;
4393 }
4394 spin_unlock(&ei->i_prealloc_lock);
4395
4396 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004397 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04004398 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05004399
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004400 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4401 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004402 if (err) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004403 ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
4404 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004405 continue;
4406 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004407
Theodore Ts'o574ca172008-07-11 19:27:31 -04004408 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004409 if (IS_ERR(bitmap_bh)) {
4410 err = PTR_ERR(bitmap_bh);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004411 ext4_error_err(sb, -err, "Error %d reading block bitmap for %u",
4412 err, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004413 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004414 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004415 }
4416
4417 ext4_lock_group(sb, group);
4418 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004419 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004420 ext4_unlock_group(sb, group);
4421
Jing Zhange39e07f2010-05-14 00:00:00 -04004422 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004423 put_bh(bitmap_bh);
4424
4425 list_del(&pa->u.pa_tmp_list);
4426 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4427 }
4428}
4429
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304430static int ext4_mb_pa_alloc(struct ext4_allocation_context *ac)
4431{
4432 struct ext4_prealloc_space *pa;
4433
4434 BUG_ON(ext4_pspace_cachep == NULL);
4435 pa = kmem_cache_zalloc(ext4_pspace_cachep, GFP_NOFS);
4436 if (!pa)
4437 return -ENOMEM;
4438 atomic_set(&pa->pa_count, 1);
4439 ac->ac_pa = pa;
4440 return 0;
4441}
4442
4443static void ext4_mb_pa_free(struct ext4_allocation_context *ac)
4444{
4445 struct ext4_prealloc_space *pa = ac->ac_pa;
4446
4447 BUG_ON(!pa);
4448 ac->ac_pa = NULL;
4449 WARN_ON(!atomic_dec_and_test(&pa->pa_count));
4450 kmem_cache_free(ext4_pspace_cachep, pa);
4451}
4452
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004453#ifdef CONFIG_EXT4_DEBUG
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304454static inline void ext4_mb_show_pa(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05004455{
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304456 ext4_group_t i, ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -05004457
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -08004458 if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004459 return;
4460
Theodore Ts'o8df96752009-05-01 08:50:38 -04004461 ngroups = ext4_get_groups_count(sb);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304462 mb_debug(sb, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004463 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004464 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4465 struct ext4_prealloc_space *pa;
4466 ext4_grpblk_t start;
4467 struct list_head *cur;
4468 ext4_lock_group(sb, i);
4469 list_for_each(cur, &grp->bb_prealloc_list) {
4470 pa = list_entry(cur, struct ext4_prealloc_space,
4471 pa_group_list);
4472 spin_lock(&pa->pa_lock);
4473 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4474 NULL, &start);
4475 spin_unlock(&pa->pa_lock);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304476 mb_debug(sb, "PA:%u:%d:%d\n", i, start,
4477 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004478 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004479 ext4_unlock_group(sb, i);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304480 mb_debug(sb, "%u: %d/%d\n", i, grp->bb_free,
4481 grp->bb_fragments);
Alex Tomasc9de5602008-01-29 00:19:52 -05004482 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004483}
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304484
4485static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4486{
4487 struct super_block *sb = ac->ac_sb;
4488
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -08004489 if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304490 return;
4491
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304492 mb_debug(sb, "Can't allocate:"
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304493 " Allocation context details:");
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304494 mb_debug(sb, "status %u flags 0x%x",
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304495 ac->ac_status, ac->ac_flags);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304496 mb_debug(sb, "orig %lu/%lu/%lu@%lu, "
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304497 "goal %lu/%lu/%lu@%lu, "
4498 "best %lu/%lu/%lu@%lu cr %d",
4499 (unsigned long)ac->ac_o_ex.fe_group,
4500 (unsigned long)ac->ac_o_ex.fe_start,
4501 (unsigned long)ac->ac_o_ex.fe_len,
4502 (unsigned long)ac->ac_o_ex.fe_logical,
4503 (unsigned long)ac->ac_g_ex.fe_group,
4504 (unsigned long)ac->ac_g_ex.fe_start,
4505 (unsigned long)ac->ac_g_ex.fe_len,
4506 (unsigned long)ac->ac_g_ex.fe_logical,
4507 (unsigned long)ac->ac_b_ex.fe_group,
4508 (unsigned long)ac->ac_b_ex.fe_start,
4509 (unsigned long)ac->ac_b_ex.fe_len,
4510 (unsigned long)ac->ac_b_ex.fe_logical,
4511 (int)ac->ac_criteria);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304512 mb_debug(sb, "%u found", ac->ac_found);
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304513 ext4_mb_show_pa(sb);
4514}
Alex Tomasc9de5602008-01-29 00:19:52 -05004515#else
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304516static inline void ext4_mb_show_pa(struct super_block *sb)
4517{
4518 return;
4519}
Alex Tomasc9de5602008-01-29 00:19:52 -05004520static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4521{
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304522 ext4_mb_show_pa(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004523 return;
4524}
4525#endif
4526
4527/*
4528 * We use locality group preallocation for small size file. The size of the
4529 * file is determined by the current size or the resulting size after
4530 * allocation which ever is larger
4531 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004532 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004533 */
4534static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4535{
4536 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4537 int bsbits = ac->ac_sb->s_blocksize_bits;
4538 loff_t size, isize;
4539
4540 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4541 return;
4542
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004543 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4544 return;
4545
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004546 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004547 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4548 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004549
Nikolay Borisov82dd1242019-02-10 23:04:16 -05004550 if ((size == isize) && !ext4_fs_is_busy(sbi) &&
4551 !inode_is_open_for_write(ac->ac_inode)) {
Theodore Ts'o50797482009-09-18 13:34:02 -04004552 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4553 return;
4554 }
4555
Robin Dongebbe0272011-10-26 05:14:27 -04004556 if (sbi->s_mb_group_prealloc <= 0) {
4557 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4558 return;
4559 }
4560
Alex Tomasc9de5602008-01-29 00:19:52 -05004561 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004562 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004563 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004564 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004565 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004566 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004567
4568 BUG_ON(ac->ac_lg != NULL);
4569 /*
4570 * locality group prealloc space are per cpu. The reason for having
4571 * per cpu locality group is to reduce the contention between block
4572 * request from multiple CPUs.
4573 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004574 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004575
4576 /* we're going to use group allocation */
4577 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4578
4579 /* serialize all allocations in the group */
4580 mutex_lock(&ac->ac_lg->lg_mutex);
4581}
4582
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004583static noinline_for_stack int
4584ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004585 struct ext4_allocation_request *ar)
4586{
4587 struct super_block *sb = ar->inode->i_sb;
4588 struct ext4_sb_info *sbi = EXT4_SB(sb);
4589 struct ext4_super_block *es = sbi->s_es;
4590 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004591 unsigned int len;
4592 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004593 ext4_grpblk_t block;
4594
4595 /* we can't allocate > group size */
4596 len = ar->len;
4597
4598 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004599 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4600 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004601
4602 /* start searching from the goal */
4603 goal = ar->goal;
4604 if (goal < le32_to_cpu(es->s_first_data_block) ||
4605 goal >= ext4_blocks_count(es))
4606 goal = le32_to_cpu(es->s_first_data_block);
4607 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4608
4609 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004610 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004611 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004612 ac->ac_sb = sb;
4613 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004614 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004615 ac->ac_o_ex.fe_group = group;
4616 ac->ac_o_ex.fe_start = block;
4617 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004618 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004619 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004620
brookxu3cb77bd2020-07-15 11:00:44 +08004621 /* we have to define context: we'll work with a file or
Alex Tomasc9de5602008-01-29 00:19:52 -05004622 * locality group. this is a policy, actually */
4623 ext4_mb_group_or_file(ac);
4624
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304625 mb_debug(sb, "init ac: %u blocks @ %u, goal %u, flags 0x%x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004626 "left: %u/%u, right %u/%u to %swritable\n",
4627 (unsigned) ar->len, (unsigned) ar->logical,
4628 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4629 (unsigned) ar->lleft, (unsigned) ar->pleft,
4630 (unsigned) ar->lright, (unsigned) ar->pright,
Nikolay Borisov82dd1242019-02-10 23:04:16 -05004631 inode_is_open_for_write(ar->inode) ? "" : "non-");
Alex Tomasc9de5602008-01-29 00:19:52 -05004632 return 0;
4633
4634}
4635
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004636static noinline_for_stack void
4637ext4_mb_discard_lg_preallocations(struct super_block *sb,
4638 struct ext4_locality_group *lg,
4639 int order, int total_entries)
4640{
4641 ext4_group_t group = 0;
4642 struct ext4_buddy e4b;
4643 struct list_head discard_list;
4644 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004645
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304646 mb_debug(sb, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004647
4648 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004649
4650 spin_lock(&lg->lg_prealloc_lock);
4651 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
Madhuparna Bhowmik92e9c582020-02-13 20:55:58 +05304652 pa_inode_list,
4653 lockdep_is_held(&lg->lg_prealloc_lock)) {
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004654 spin_lock(&pa->pa_lock);
4655 if (atomic_read(&pa->pa_count)) {
4656 /*
4657 * This is the pa that we just used
4658 * for block allocation. So don't
4659 * free that
4660 */
4661 spin_unlock(&pa->pa_lock);
4662 continue;
4663 }
4664 if (pa->pa_deleted) {
4665 spin_unlock(&pa->pa_lock);
4666 continue;
4667 }
4668 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004669 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004670
4671 /* seems this one can be freed ... */
brookxu27bc4462020-08-17 15:36:15 +08004672 ext4_mb_mark_pa_deleted(sb, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004673 spin_unlock(&pa->pa_lock);
4674
4675 list_del_rcu(&pa->pa_inode_list);
4676 list_add(&pa->u.pa_tmp_list, &discard_list);
4677
4678 total_entries--;
4679 if (total_entries <= 5) {
4680 /*
4681 * we want to keep only 5 entries
4682 * allowing it to grow to 8. This
4683 * mak sure we don't call discard
4684 * soon for this list.
4685 */
4686 break;
4687 }
4688 }
4689 spin_unlock(&lg->lg_prealloc_lock);
4690
4691 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004692 int err;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004693
Lukas Czernerbd862982013-04-03 23:32:34 -04004694 group = ext4_get_group_number(sb, pa->pa_pstart);
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004695 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4696 GFP_NOFS|__GFP_NOFAIL);
4697 if (err) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004698 ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
4699 err, group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004700 continue;
4701 }
4702 ext4_lock_group(sb, group);
4703 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004704 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004705 ext4_unlock_group(sb, group);
4706
Jing Zhange39e07f2010-05-14 00:00:00 -04004707 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004708 list_del(&pa->u.pa_tmp_list);
4709 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4710 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004711}
4712
4713/*
4714 * We have incremented pa_count. So it cannot be freed at this
4715 * point. Also we hold lg_mutex. So no parallel allocation is
4716 * possible from this lg. That means pa_free cannot be updated.
4717 *
4718 * A parallel ext4_mb_discard_group_preallocations is possible.
4719 * which can cause the lg_prealloc_list to be updated.
4720 */
4721
4722static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4723{
4724 int order, added = 0, lg_prealloc_count = 1;
4725 struct super_block *sb = ac->ac_sb;
4726 struct ext4_locality_group *lg = ac->ac_lg;
4727 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4728
4729 order = fls(pa->pa_free) - 1;
4730 if (order > PREALLOC_TB_SIZE - 1)
4731 /* The max size of hash table is PREALLOC_TB_SIZE */
4732 order = PREALLOC_TB_SIZE - 1;
4733 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004734 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004735 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
Madhuparna Bhowmik92e9c582020-02-13 20:55:58 +05304736 pa_inode_list,
4737 lockdep_is_held(&lg->lg_prealloc_lock)) {
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004738 spin_lock(&tmp_pa->pa_lock);
4739 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004740 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004741 continue;
4742 }
4743 if (!added && pa->pa_free < tmp_pa->pa_free) {
4744 /* Add to the tail of the previous entry */
4745 list_add_tail_rcu(&pa->pa_inode_list,
4746 &tmp_pa->pa_inode_list);
4747 added = 1;
4748 /*
4749 * we want to count the total
4750 * number of entries in the list
4751 */
4752 }
4753 spin_unlock(&tmp_pa->pa_lock);
4754 lg_prealloc_count++;
4755 }
4756 if (!added)
4757 list_add_tail_rcu(&pa->pa_inode_list,
4758 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004759 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004760
4761 /* Now trim the list to be not more than 8 elements */
4762 if (lg_prealloc_count > 8) {
4763 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004764 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004765 return;
4766 }
4767 return ;
4768}
4769
Alex Tomasc9de5602008-01-29 00:19:52 -05004770/*
brookxu27bc4462020-08-17 15:36:15 +08004771 * if per-inode prealloc list is too long, trim some PA
4772 */
4773static void ext4_mb_trim_inode_pa(struct inode *inode)
4774{
4775 struct ext4_inode_info *ei = EXT4_I(inode);
4776 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4777 int count, delta;
4778
4779 count = atomic_read(&ei->i_prealloc_active);
4780 delta = (sbi->s_mb_max_inode_prealloc >> 2) + 1;
4781 if (count > sbi->s_mb_max_inode_prealloc + delta) {
4782 count -= sbi->s_mb_max_inode_prealloc;
4783 ext4_discard_preallocations(inode, count);
4784 }
4785}
4786
4787/*
Alex Tomasc9de5602008-01-29 00:19:52 -05004788 * release all resource we used in allocation
4789 */
4790static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4791{
brookxu27bc4462020-08-17 15:36:15 +08004792 struct inode *inode = ac->ac_inode;
4793 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004794 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004795 struct ext4_prealloc_space *pa = ac->ac_pa;
4796 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004797 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004798 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004799 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004800 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4801 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004802 pa->pa_free -= ac->ac_b_ex.fe_len;
4803 pa->pa_len -= ac->ac_b_ex.fe_len;
4804 spin_unlock(&pa->pa_lock);
brookxu66d5e022020-08-17 15:36:06 +08004805
4806 /*
4807 * We want to add the pa to the right bucket.
4808 * Remove it from the list and while adding
4809 * make sure the list to which we are adding
4810 * doesn't grow big.
4811 */
4812 if (likely(pa->pa_free)) {
4813 spin_lock(pa->pa_obj_lock);
4814 list_del_rcu(&pa->pa_inode_list);
4815 spin_unlock(pa->pa_obj_lock);
4816 ext4_mb_add_n_trim(ac);
4817 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004818 }
brookxu27bc4462020-08-17 15:36:15 +08004819
4820 if (pa->pa_type == MB_INODE_PA) {
4821 /*
4822 * treat per-inode prealloc list as a lru list, then try
4823 * to trim the least recently used PA.
4824 */
4825 spin_lock(pa->pa_obj_lock);
4826 list_move(&pa->pa_inode_list, &ei->i_prealloc_list);
4827 spin_unlock(pa->pa_obj_lock);
4828 }
4829
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004830 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4831 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004832 if (ac->ac_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004833 put_page(ac->ac_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004834 if (ac->ac_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004835 put_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004836 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4837 mutex_unlock(&ac->ac_lg->lg_mutex);
4838 ext4_mb_collect_stats(ac);
brookxu27bc4462020-08-17 15:36:15 +08004839 ext4_mb_trim_inode_pa(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004840 return 0;
4841}
4842
4843static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4844{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004845 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004846 int ret;
4847 int freed = 0;
4848
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004849 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004850 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004851 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4852 freed += ret;
4853 needed -= ret;
4854 }
4855
4856 return freed;
4857}
4858
Ritesh Harjanicf5e2ca2020-05-20 12:10:33 +05304859static bool ext4_mb_discard_preallocations_should_retry(struct super_block *sb,
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304860 struct ext4_allocation_context *ac, u64 *seq)
Ritesh Harjanicf5e2ca2020-05-20 12:10:33 +05304861{
4862 int freed;
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304863 u64 seq_retry = 0;
4864 bool ret = false;
Ritesh Harjanicf5e2ca2020-05-20 12:10:33 +05304865
4866 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304867 if (freed) {
4868 ret = true;
4869 goto out_dbg;
4870 }
4871 seq_retry = ext4_get_discard_pa_seq_sum();
Ritesh Harjani99377832020-05-20 12:10:36 +05304872 if (!(ac->ac_flags & EXT4_MB_STRICT_CHECK) || seq_retry != *seq) {
4873 ac->ac_flags |= EXT4_MB_STRICT_CHECK;
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304874 *seq = seq_retry;
4875 ret = true;
4876 }
4877
4878out_dbg:
4879 mb_debug(sb, "freed %d, retry ? %s\n", freed, ret ? "yes" : "no");
4880 return ret;
Ritesh Harjanicf5e2ca2020-05-20 12:10:33 +05304881}
4882
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004883static ext4_fsblk_t ext4_mb_new_blocks_simple(handle_t *handle,
4884 struct ext4_allocation_request *ar, int *errp);
4885
Alex Tomasc9de5602008-01-29 00:19:52 -05004886/*
4887 * Main entry point into mballoc to allocate blocks
4888 * it tries to use preallocation first, then falls back
4889 * to usual allocation
4890 */
4891ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004892 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004893{
Eric Sandeen256bdb42008-02-10 01:13:33 -05004894 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004895 struct ext4_sb_info *sbi;
4896 struct super_block *sb;
4897 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004898 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004899 unsigned int reserv_clstrs = 0;
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304900 u64 seq;
Alex Tomasc9de5602008-01-29 00:19:52 -05004901
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004902 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004903 sb = ar->inode->i_sb;
4904 sbi = EXT4_SB(sb);
4905
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004906 trace_ext4_request_blocks(ar);
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004907 if (sbi->s_mount_state & EXT4_FC_REPLAY)
4908 return ext4_mb_new_blocks_simple(handle, ar, errp);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004909
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004910 /* Allow to use superuser reservation for quota file */
Tahsin Erdogan02749a42017-06-22 11:31:25 -04004911 if (ext4_is_quota_file(ar->inode))
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004912 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4913
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004914 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004915 /* Without delayed allocation we need to verify
4916 * there is enough free blocks to do block allocation
4917 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004918 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004919 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004920 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004921
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004922 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004923 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004924 ar->len = ar->len >> 1;
4925 }
4926 if (!ar->len) {
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304927 ext4_mb_show_pa(sb);
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004928 *errp = -ENOSPC;
4929 return 0;
4930 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004931 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004932 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004933 dquot_alloc_block_nofail(ar->inode,
4934 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004935 } else {
4936 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004937 dquot_alloc_block(ar->inode,
4938 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004939
4940 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4941 ar->len--;
4942 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004943 }
4944 inquota = ar->len;
4945 if (ar->len == 0) {
4946 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004947 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004948 }
Mingming Caod2a17632008-07-14 17:52:37 -04004949 }
Mingming Caod2a17632008-07-14 17:52:37 -04004950
Wei Yongjun85556c92012-09-26 20:43:37 -04004951 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004952 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004953 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004954 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004955 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004956 }
4957
Eric Sandeen256bdb42008-02-10 01:13:33 -05004958 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004959 if (*errp) {
4960 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004961 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004962 }
4963
Eric Sandeen256bdb42008-02-10 01:13:33 -05004964 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
Ritesh Harjani81198532020-06-09 16:23:10 +05304965 seq = this_cpu_read(discard_pa_seq);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004966 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004967 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4968 ext4_mb_normalize_request(ac, ar);
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304969
4970 *errp = ext4_mb_pa_alloc(ac);
4971 if (*errp)
4972 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004973repeat:
4974 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004975 *errp = ext4_mb_regular_allocator(ac);
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304976 /*
4977 * pa allocated above is added to grp->bb_prealloc_list only
4978 * when we were able to allocate some block i.e. when
4979 * ac->ac_status == AC_STATUS_FOUND.
4980 * And error from above mean ac->ac_status != AC_STATUS_FOUND
4981 * So we have to free this pa here itself.
4982 */
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004983 if (*errp) {
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304984 ext4_mb_pa_free(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004985 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004986 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004987 }
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304988 if (ac->ac_status == AC_STATUS_FOUND &&
4989 ac->ac_o_ex.fe_len >= ac->ac_f_ex.fe_len)
4990 ext4_mb_pa_free(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004991 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004992 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004993 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Vegard Nossum554a5cc2016-07-14 23:02:47 -04004994 if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004995 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004996 goto errout;
4997 } else {
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004998 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4999 ar->len = ac->ac_b_ex.fe_len;
5000 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005001 } else {
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05305002 if (ext4_mb_discard_preallocations_should_retry(sb, ac, &seq))
Alex Tomasc9de5602008-01-29 00:19:52 -05005003 goto repeat;
Ritesh Harjani53f86b12020-05-20 12:10:32 +05305004 /*
5005 * If block allocation fails then the pa allocated above
5006 * needs to be freed here itself.
5007 */
5008 ext4_mb_pa_free(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05005009 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04005010 }
5011
Eric Sandeen6d138ce2012-11-08 11:11:59 -05005012errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04005013 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05005014 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05005015 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05005016 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05005017 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05005018 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04005019out:
5020 if (ac)
5021 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01005022 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04005023 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05005024 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04005025 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05005026 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04005027 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04005028 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05005029 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005030
Theodore Ts'o9bffad12009-06-17 11:48:11 -04005031 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05005032
Alex Tomasc9de5602008-01-29 00:19:52 -05005033 return block;
5034}
Alex Tomasc9de5602008-01-29 00:19:52 -05005035
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005036/*
5037 * We can merge two free data extents only if the physical blocks
5038 * are contiguous, AND the extents were freed by the same transaction,
5039 * AND the blocks are associated with the same group.
5040 */
Daeho Jeonga0154342017-06-22 23:54:33 -04005041static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi,
5042 struct ext4_free_data *entry,
5043 struct ext4_free_data *new_entry,
5044 struct rb_root *entry_rb_root)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005045{
Daeho Jeonga0154342017-06-22 23:54:33 -04005046 if ((entry->efd_tid != new_entry->efd_tid) ||
5047 (entry->efd_group != new_entry->efd_group))
5048 return;
5049 if (entry->efd_start_cluster + entry->efd_count ==
5050 new_entry->efd_start_cluster) {
5051 new_entry->efd_start_cluster = entry->efd_start_cluster;
5052 new_entry->efd_count += entry->efd_count;
5053 } else if (new_entry->efd_start_cluster + new_entry->efd_count ==
5054 entry->efd_start_cluster) {
5055 new_entry->efd_count += entry->efd_count;
5056 } else
5057 return;
5058 spin_lock(&sbi->s_md_lock);
5059 list_del(&entry->efd_list);
5060 spin_unlock(&sbi->s_md_lock);
5061 rb_erase(&entry->efd_node, entry_rb_root);
5062 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005063}
5064
Eric Sandeen4ddfef72008-04-29 08:11:12 -04005065static noinline_for_stack int
5066ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005067 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05005068{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04005069 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04005070 ext4_grpblk_t cluster;
Theodore Ts'od08854f2016-06-26 18:24:01 -04005071 ext4_grpblk_t clusters = new_entry->efd_count;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005072 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05005073 struct ext4_group_info *db = e4b->bd_info;
5074 struct super_block *sb = e4b->bd_sb;
5075 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005076 struct rb_node **n = &db->bb_free_root.rb_node, *node;
5077 struct rb_node *parent = NULL, *new_node;
5078
Frank Mayhar03901312009-01-07 00:06:22 -05005079 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05005080 BUG_ON(e4b->bd_bitmap_page == NULL);
5081 BUG_ON(e4b->bd_buddy_page == NULL);
5082
Bobi Jam18aadd42012-02-20 17:53:02 -05005083 new_node = &new_entry->efd_node;
5084 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005085
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005086 if (!*n) {
5087 /* first free block exent. We need to
5088 protect buddy cache from being freed,
5089 * otherwise we'll refresh it from
5090 * on-disk bitmap and lose not-yet-available
5091 * blocks */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005092 get_page(e4b->bd_buddy_page);
5093 get_page(e4b->bd_bitmap_page);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005094 }
5095 while (*n) {
5096 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05005097 entry = rb_entry(parent, struct ext4_free_data, efd_node);
5098 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005099 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05005100 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005101 n = &(*n)->rb_right;
5102 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04005103 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04005104 ext4_group_first_block_no(sb, group) +
5105 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04005106 "Block already on to-be-freed list");
Chunguang Xucca41552020-11-07 23:58:18 +08005107 kmem_cache_free(ext4_free_data_cachep, new_entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005108 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05005109 }
5110 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005111
5112 rb_link_node(new_node, parent, n);
5113 rb_insert_color(new_node, &db->bb_free_root);
5114
5115 /* Now try to see the extent can be merged to left and right */
5116 node = rb_prev(new_node);
5117 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05005118 entry = rb_entry(node, struct ext4_free_data, efd_node);
Daeho Jeonga0154342017-06-22 23:54:33 -04005119 ext4_try_merge_freed_extent(sbi, entry, new_entry,
5120 &(db->bb_free_root));
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005121 }
5122
5123 node = rb_next(new_node);
5124 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05005125 entry = rb_entry(node, struct ext4_free_data, efd_node);
Daeho Jeonga0154342017-06-22 23:54:33 -04005126 ext4_try_merge_freed_extent(sbi, entry, new_entry,
5127 &(db->bb_free_root));
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04005128 }
Daeho Jeonga0154342017-06-22 23:54:33 -04005129
Theodore Ts'od08854f2016-06-26 18:24:01 -04005130 spin_lock(&sbi->s_md_lock);
Daeho Jeonga0154342017-06-22 23:54:33 -04005131 list_add_tail(&new_entry->efd_list, &sbi->s_freed_data_list);
Theodore Ts'od08854f2016-06-26 18:24:01 -04005132 sbi->s_mb_free_pending += clusters;
5133 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05005134 return 0;
5135}
5136
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07005137/*
5138 * Simple allocator for Ext4 fast commit replay path. It searches for blocks
5139 * linearly starting at the goal block and also excludes the blocks which
5140 * are going to be in use after fast commit replay.
5141 */
5142static ext4_fsblk_t ext4_mb_new_blocks_simple(handle_t *handle,
5143 struct ext4_allocation_request *ar, int *errp)
5144{
5145 struct buffer_head *bitmap_bh;
5146 struct super_block *sb = ar->inode->i_sb;
5147 ext4_group_t group;
5148 ext4_grpblk_t blkoff;
Dan Carpentere121bd42020-10-30 14:46:20 +03005149 int i = sb->s_blocksize;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07005150 ext4_fsblk_t goal, block;
5151 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5152
5153 goal = ar->goal;
5154 if (goal < le32_to_cpu(es->s_first_data_block) ||
5155 goal >= ext4_blocks_count(es))
5156 goal = le32_to_cpu(es->s_first_data_block);
5157
5158 ar->len = 0;
5159 ext4_get_group_no_and_offset(sb, goal, &group, &blkoff);
5160 for (; group < ext4_get_groups_count(sb); group++) {
5161 bitmap_bh = ext4_read_block_bitmap(sb, group);
5162 if (IS_ERR(bitmap_bh)) {
5163 *errp = PTR_ERR(bitmap_bh);
5164 pr_warn("Failed to read block bitmap\n");
5165 return 0;
5166 }
5167
5168 ext4_get_group_no_and_offset(sb,
5169 max(ext4_group_first_block_no(sb, group), goal),
5170 NULL, &blkoff);
5171 i = mb_find_next_zero_bit(bitmap_bh->b_data, sb->s_blocksize,
5172 blkoff);
5173 brelse(bitmap_bh);
5174 if (i >= sb->s_blocksize)
5175 continue;
5176 if (ext4_fc_replay_check_excluded(sb,
5177 ext4_group_first_block_no(sb, group) + i))
5178 continue;
5179 break;
5180 }
5181
5182 if (group >= ext4_get_groups_count(sb) && i >= sb->s_blocksize)
5183 return 0;
5184
5185 block = ext4_group_first_block_no(sb, group) + i;
5186 ext4_mb_mark_bb(sb, block, 1, 1);
5187 ar->len = 1;
5188
5189 return block;
5190}
5191
5192static void ext4_free_blocks_simple(struct inode *inode, ext4_fsblk_t block,
5193 unsigned long count)
5194{
5195 struct buffer_head *bitmap_bh;
5196 struct super_block *sb = inode->i_sb;
5197 struct ext4_group_desc *gdp;
5198 struct buffer_head *gdp_bh;
5199 ext4_group_t group;
5200 ext4_grpblk_t blkoff;
5201 int already_freed = 0, err, i;
5202
5203 ext4_get_group_no_and_offset(sb, block, &group, &blkoff);
5204 bitmap_bh = ext4_read_block_bitmap(sb, group);
5205 if (IS_ERR(bitmap_bh)) {
5206 err = PTR_ERR(bitmap_bh);
5207 pr_warn("Failed to read block bitmap\n");
5208 return;
5209 }
5210 gdp = ext4_get_group_desc(sb, group, &gdp_bh);
5211 if (!gdp)
5212 return;
5213
5214 for (i = 0; i < count; i++) {
5215 if (!mb_test_bit(blkoff + i, bitmap_bh->b_data))
5216 already_freed++;
5217 }
5218 mb_clear_bits(bitmap_bh->b_data, blkoff, count);
5219 err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh);
5220 if (err)
5221 return;
5222 ext4_free_group_clusters_set(
5223 sb, gdp, ext4_free_group_clusters(sb, gdp) +
5224 count - already_freed);
5225 ext4_block_bitmap_csum_set(sb, group, gdp, bitmap_bh);
5226 ext4_group_desc_csum_set(sb, group, gdp);
5227 ext4_handle_dirty_metadata(NULL, NULL, gdp_bh);
5228 sync_dirty_buffer(bitmap_bh);
5229 sync_dirty_buffer(gdp_bh);
5230 brelse(bitmap_bh);
5231}
5232
Theodore Ts'o44338712009-11-22 07:44:56 -05005233/**
5234 * ext4_free_blocks() -- Free given blocks and update quota
5235 * @handle: handle for this transaction
5236 * @inode: inode
Theodore Ts'oc60990b2019-06-19 16:30:03 -04005237 * @bh: optional buffer of the block to be freed
5238 * @block: starting physical block to be freed
5239 * @count: number of blocks to be freed
Yongqiang Yang5def1362011-06-05 23:26:40 -04005240 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05005241 */
Theodore Ts'o44338712009-11-22 07:44:56 -05005242void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05005243 struct buffer_head *bh, ext4_fsblk_t block,
5244 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05005245{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05005246 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05005247 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05005248 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05005249 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05005250 ext4_grpblk_t bit;
5251 struct buffer_head *gd_bh;
5252 ext4_group_t block_group;
5253 struct ext4_sb_info *sbi;
5254 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04005255 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05005256 int err = 0;
5257 int ret;
5258
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07005259 sbi = EXT4_SB(sb);
5260
5261 if (sbi->s_mount_state & EXT4_FC_REPLAY) {
5262 ext4_free_blocks_simple(inode, block, count);
5263 return;
5264 }
5265
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04005266 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05005267 if (bh) {
5268 if (block)
5269 BUG_ON(block != bh->b_blocknr);
5270 else
5271 block = bh->b_blocknr;
5272 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005273
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05005274 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
Jan Karace9f24c2020-07-28 15:04:34 +02005275 !ext4_inode_block_valid(inode, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05005276 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05005277 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05005278 goto error_return;
5279 }
5280
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04005281 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05005282 trace_ext4_free_blocks(inode, block, count, flags);
5283
Daeho Jeong9c02ac92015-10-17 22:28:21 -04005284 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
5285 BUG_ON(count > 1);
Theodore Ts'oe6362602009-11-23 07:17:05 -05005286
Daeho Jeong9c02ac92015-10-17 22:28:21 -04005287 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
5288 inode, bh, block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05005289 }
5290
Theodore Ts'o60e66792010-05-17 07:00:00 -04005291 /*
Theodore Ts'o84130192011-09-09 18:50:51 -04005292 * If the extent to be freed does not begin on a cluster
5293 * boundary, we need to deal with partial clusters at the
5294 * beginning and end of the extent. Normally we will free
5295 * blocks at the beginning or the end unless we are explicitly
5296 * requested to avoid doing so.
5297 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05005298 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04005299 if (overflow) {
5300 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
5301 overflow = sbi->s_cluster_ratio - overflow;
5302 block += overflow;
5303 if (count > overflow)
5304 count -= overflow;
5305 else
5306 return;
5307 } else {
5308 block -= overflow;
5309 count += overflow;
5310 }
5311 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05005312 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04005313 if (overflow) {
5314 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
5315 if (count > overflow)
5316 count -= overflow;
5317 else
5318 return;
5319 } else
5320 count += sbi->s_cluster_ratio - overflow;
5321 }
5322
Daeho Jeong9c02ac92015-10-17 22:28:21 -04005323 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
5324 int i;
Daeho Jeongf96c4502016-02-21 18:31:41 -05005325 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
Daeho Jeong9c02ac92015-10-17 22:28:21 -04005326
5327 for (i = 0; i < count; i++) {
5328 cond_resched();
Daeho Jeongf96c4502016-02-21 18:31:41 -05005329 if (is_metadata)
5330 bh = sb_find_get_block(inode->i_sb, block + i);
5331 ext4_forget(handle, is_metadata, inode, bh, block + i);
Daeho Jeong9c02ac92015-10-17 22:28:21 -04005332 }
5333 }
5334
Alex Tomasc9de5602008-01-29 00:19:52 -05005335do_more:
5336 overflow = 0;
5337 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
5338
Darrick J. Wong163a2032013-08-28 17:35:51 -04005339 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
5340 ext4_get_group_info(sb, block_group))))
5341 return;
5342
Alex Tomasc9de5602008-01-29 00:19:52 -05005343 /*
5344 * Check to see if we are freeing blocks across a group
5345 * boundary.
5346 */
Theodore Ts'o84130192011-09-09 18:50:51 -04005347 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
5348 overflow = EXT4_C2B(sbi, bit) + count -
5349 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05005350 count -= overflow;
5351 }
Lukas Czerner810da242013-03-02 17:18:58 -05005352 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04005353 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04005354 if (IS_ERR(bitmap_bh)) {
5355 err = PTR_ERR(bitmap_bh);
5356 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05005357 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04005358 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005359 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04005360 if (!gdp) {
5361 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05005362 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04005363 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005364
5365 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
5366 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
5367 in_range(block, ext4_inode_table(sb, gdp),
Jun Piao49598e02018-01-11 13:17:49 -05005368 sbi->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05005369 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Jun Piao49598e02018-01-11 13:17:49 -05005370 sbi->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05005371
Eric Sandeen12062dd2010-02-15 14:19:27 -05005372 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04005373 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04005374 /* err = 0. ext4_std_error should be a no op */
5375 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05005376 }
5377
5378 BUFFER_TRACE(bitmap_bh, "getting write access");
5379 err = ext4_journal_get_write_access(handle, bitmap_bh);
5380 if (err)
5381 goto error_return;
5382
5383 /*
5384 * We are about to modify some metadata. Call the journal APIs
5385 * to unshare ->b_data if a currently-committing transaction is
5386 * using it
5387 */
5388 BUFFER_TRACE(gd_bh, "get_write_access");
5389 err = ext4_journal_get_write_access(handle, gd_bh);
5390 if (err)
5391 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05005392#ifdef AGGRESSIVE_CHECK
5393 {
5394 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04005395 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05005396 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
5397 }
5398#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04005399 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05005400
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005401 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
5402 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
5403 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05005404 if (err)
5405 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05005406
Daeho Jeongf96c4502016-02-21 18:31:41 -05005407 /*
5408 * We need to make sure we don't reuse the freed block until after the
5409 * transaction is committed. We make an exception if the inode is to be
5410 * written in writeback mode since writeback mode has weak data
5411 * consistency guarantees.
5412 */
5413 if (ext4_handle_valid(handle) &&
5414 ((flags & EXT4_FREE_BLOCKS_METADATA) ||
5415 !ext4_should_writeback_data(inode))) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005416 struct ext4_free_data *new_entry;
5417 /*
Michal Hocko7444a072015-07-05 12:33:44 -04005418 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
5419 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005420 */
Michal Hocko7444a072015-07-05 12:33:44 -04005421 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
5422 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05005423 new_entry->efd_start_cluster = bit;
5424 new_entry->efd_group = block_group;
5425 new_entry->efd_count = count_clusters;
5426 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04005427
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005428 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04005429 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005430 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05005431 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005432 /* need to update group_info->bb_free and bitmap
5433 * with group lock held. generate_buddy look at
5434 * them with group lock_held
5435 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005436 if (test_opt(sb, DISCARD)) {
Daeho Jeonga0154342017-06-22 23:54:33 -04005437 err = ext4_issue_discard(sb, block_group, bit, count,
5438 NULL);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005439 if (err && err != -EOPNOTSUPP)
5440 ext4_msg(sb, KERN_WARNING, "discard request in"
5441 " group:%d block:%d count:%lu failed"
5442 " with %d", block_group, bit, count,
5443 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04005444 } else
5445 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005446
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04005447 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04005448 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
5449 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05005450 }
5451
Theodore Ts'o021b65b2011-09-09 19:08:51 -04005452 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
5453 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04005454 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005455 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04005456 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05005457
Jose R. Santos772cb7c2008-07-11 19:27:31 -04005458 if (sbi->s_log_groups_per_flex) {
5459 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04005460 atomic64_add(count_clusters,
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08005461 &sbi_array_rcu_deref(sbi, s_flex_groups,
5462 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04005463 }
5464
Eric Whitney9fe67142018-10-01 14:25:08 -04005465 /*
5466 * on a bigalloc file system, defer the s_freeclusters_counter
5467 * update to the caller (ext4_remove_space and friends) so they
5468 * can determine if a cluster freed here should be rereserved
5469 */
5470 if (!(flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)) {
5471 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
5472 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
5473 percpu_counter_add(&sbi->s_freeclusters_counter,
5474 count_clusters);
5475 }
Jan Kara7d734532013-08-17 09:36:54 -04005476
5477 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04005478
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005479 /* We dirtied the bitmap block */
5480 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5481 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5482
Alex Tomasc9de5602008-01-29 00:19:52 -05005483 /* And the group descriptor block */
5484 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05005485 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05005486 if (!err)
5487 err = ret;
5488
5489 if (overflow && !err) {
5490 block += count;
5491 count = overflow;
5492 put_bh(bitmap_bh);
5493 goto do_more;
5494 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005495error_return:
5496 brelse(bitmap_bh);
5497 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05005498 return;
5499}
Lukas Czerner7360d172010-10-27 21:30:12 -04005500
5501/**
Yongqiang Yang05291552011-07-26 21:43:56 -04005502 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04005503 * @handle: handle to this transaction
5504 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005505 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04005506 * @count: number of blocks to free
5507 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04005508 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04005509 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005510int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04005511 ext4_fsblk_t block, unsigned long count)
5512{
5513 struct buffer_head *bitmap_bh = NULL;
5514 struct buffer_head *gd_bh;
5515 ext4_group_t block_group;
5516 ext4_grpblk_t bit;
5517 unsigned int i;
5518 struct ext4_group_desc *desc;
5519 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04005520 struct ext4_buddy e4b;
harshadsd77147f2017-10-29 09:38:46 -04005521 int err = 0, ret, free_clusters_count;
5522 ext4_grpblk_t clusters_freed;
5523 ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block);
5524 ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1);
5525 unsigned long cluster_count = last_cluster - first_cluster + 1;
Amir Goldstein2846e822011-05-09 10:46:41 -04005526
5527 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
5528
Yongqiang Yang4740b832011-07-26 21:51:08 -04005529 if (count == 0)
5530 return 0;
5531
Amir Goldstein2846e822011-05-09 10:46:41 -04005532 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04005533 /*
5534 * Check to see if we are freeing blocks across a group
5535 * boundary.
5536 */
harshadsd77147f2017-10-29 09:38:46 -04005537 if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) {
5538 ext4_warning(sb, "too many blocks added to group %u",
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005539 block_group);
5540 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005541 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005542 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005543
Amir Goldstein2846e822011-05-09 10:46:41 -04005544 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04005545 if (IS_ERR(bitmap_bh)) {
5546 err = PTR_ERR(bitmap_bh);
5547 bitmap_bh = NULL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005548 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005549 }
5550
Amir Goldstein2846e822011-05-09 10:46:41 -04005551 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005552 if (!desc) {
5553 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04005554 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005555 }
Amir Goldstein2846e822011-05-09 10:46:41 -04005556
5557 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
5558 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
5559 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
5560 in_range(block + count - 1, ext4_inode_table(sb, desc),
5561 sbi->s_itb_per_group)) {
5562 ext4_error(sb, "Adding blocks in system zones - "
5563 "Block = %llu, count = %lu",
5564 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005565 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005566 goto error_return;
5567 }
5568
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005569 BUFFER_TRACE(bitmap_bh, "getting write access");
5570 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04005571 if (err)
5572 goto error_return;
5573
5574 /*
5575 * We are about to modify some metadata. Call the journal APIs
5576 * to unshare ->b_data if a currently-committing transaction is
5577 * using it
5578 */
5579 BUFFER_TRACE(gd_bh, "get_write_access");
5580 err = ext4_journal_get_write_access(handle, gd_bh);
5581 if (err)
5582 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04005583
harshadsd77147f2017-10-29 09:38:46 -04005584 for (i = 0, clusters_freed = 0; i < cluster_count; i++) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005585 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04005586 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005587 ext4_error(sb, "bit already cleared for block %llu",
5588 (ext4_fsblk_t)(block + i));
5589 BUFFER_TRACE(bitmap_bh, "bit already cleared");
5590 } else {
harshadsd77147f2017-10-29 09:38:46 -04005591 clusters_freed++;
Amir Goldstein2846e822011-05-09 10:46:41 -04005592 }
5593 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005594
5595 err = ext4_mb_load_buddy(sb, block_group, &e4b);
5596 if (err)
5597 goto error_return;
5598
5599 /*
5600 * need to update group_info->bb_free and bitmap
5601 * with group lock held. generate_buddy look at
5602 * them with group lock_held
5603 */
Amir Goldstein2846e822011-05-09 10:46:41 -04005604 ext4_lock_group(sb, block_group);
harshadsd77147f2017-10-29 09:38:46 -04005605 mb_clear_bits(bitmap_bh->b_data, bit, cluster_count);
5606 mb_free_blocks(NULL, &e4b, bit, cluster_count);
5607 free_clusters_count = clusters_freed +
5608 ext4_free_group_clusters(sb, desc);
5609 ext4_free_group_clusters_set(sb, desc, free_clusters_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04005610 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005611 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04005612 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04005613 percpu_counter_add(&sbi->s_freeclusters_counter,
harshadsd77147f2017-10-29 09:38:46 -04005614 clusters_freed);
Amir Goldstein2846e822011-05-09 10:46:41 -04005615
5616 if (sbi->s_log_groups_per_flex) {
5617 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
harshadsd77147f2017-10-29 09:38:46 -04005618 atomic64_add(clusters_freed,
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08005619 &sbi_array_rcu_deref(sbi, s_flex_groups,
5620 flex_group)->free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04005621 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005622
5623 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005624
5625 /* We dirtied the bitmap block */
5626 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5627 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5628
5629 /* And the group descriptor block */
5630 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5631 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5632 if (!err)
5633 err = ret;
5634
5635error_return:
5636 brelse(bitmap_bh);
5637 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005638 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005639}
5640
5641/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005642 * ext4_trim_extent -- function to TRIM one single free extent in the group
5643 * @sb: super block for the file system
5644 * @start: starting block of the free extent in the alloc. group
5645 * @count: number of blocks to TRIM
5646 * @group: alloc. group we are working with
5647 * @e4b: ext4 buddy for the group
5648 *
5649 * Trim "count" blocks starting at "start" in the "group". To assure that no
5650 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5651 * be called with under the group lock.
5652 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005653static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04005654 ext4_group_t group, struct ext4_buddy *e4b)
jon ernste2cbd582014-04-12 23:01:28 -04005655__releases(bitlock)
5656__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005657{
5658 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005659 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005660
Tao Mab3d4c2b2011-07-11 00:01:52 -04005661 trace_ext4_trim_extent(sb, group, start, count);
5662
Lukas Czerner7360d172010-10-27 21:30:12 -04005663 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5664
5665 ex.fe_start = start;
5666 ex.fe_group = group;
5667 ex.fe_len = count;
5668
5669 /*
5670 * Mark blocks used, so no one can reuse them while
5671 * being trimmed.
5672 */
5673 mb_mark_used(e4b, &ex);
5674 ext4_unlock_group(sb, group);
Daeho Jeonga0154342017-06-22 23:54:33 -04005675 ret = ext4_issue_discard(sb, group, start, count, NULL);
Lukas Czerner7360d172010-10-27 21:30:12 -04005676 ext4_lock_group(sb, group);
5677 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005678 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005679}
5680
5681/**
5682 * ext4_trim_all_free -- function to trim all free space in alloc. group
5683 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005684 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005685 * @start: first group block to examine
5686 * @max: last group block to examine
5687 * @minblocks: minimum extent block count
5688 *
5689 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5690 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5691 * the extent.
5692 *
5693 *
5694 * ext4_trim_all_free walks through group's block bitmap searching for free
5695 * extents. When the free extent is found, mark it as used in group buddy
5696 * bitmap. Then issue a TRIM command on this extent and free the extent in
5697 * the group buddy bitmap. This is done until whole group is scanned.
5698 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005699static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005700ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5701 ext4_grpblk_t start, ext4_grpblk_t max,
5702 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005703{
5704 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005705 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005706 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005707 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005708
Tao Mab3d4c2b2011-07-11 00:01:52 -04005709 trace_ext4_trim_all_free(sb, group, start, max);
5710
Lukas Czerner78944082011-05-24 18:16:27 -04005711 ret = ext4_mb_load_buddy(sb, group, &e4b);
5712 if (ret) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04005713 ext4_warning(sb, "Error %d loading buddy information for %u",
5714 ret, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005715 return ret;
5716 }
Lukas Czerner78944082011-05-24 18:16:27 -04005717 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005718
5719 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005720 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5721 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5722 goto out;
5723
Lukas Czerner78944082011-05-24 18:16:27 -04005724 start = (e4b.bd_info->bb_first_free > start) ?
5725 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005726
Lukas Czerner913eed832012-03-21 21:22:22 -04005727 while (start <= max) {
5728 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5729 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005730 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005731 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005732
5733 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005734 ret = ext4_trim_extent(sb, start,
5735 next - start, group, &e4b);
5736 if (ret && ret != -EOPNOTSUPP)
5737 break;
5738 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005739 count += next - start;
5740 }
Tao Ma169ddc32011-07-11 00:00:07 -04005741 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005742 start = next + 1;
5743
5744 if (fatal_signal_pending(current)) {
5745 count = -ERESTARTSYS;
5746 break;
5747 }
5748
5749 if (need_resched()) {
5750 ext4_unlock_group(sb, group);
5751 cond_resched();
5752 ext4_lock_group(sb, group);
5753 }
5754
Tao Ma169ddc32011-07-11 00:00:07 -04005755 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005756 break;
5757 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005758
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005759 if (!ret) {
5760 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005761 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005762 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005763out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005764 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005765 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005766
5767 ext4_debug("trimmed %d blocks in the group %d\n",
5768 count, group);
5769
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005770 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005771}
5772
5773/**
5774 * ext4_trim_fs() -- trim ioctl handle function
5775 * @sb: superblock for filesystem
5776 * @range: fstrim_range structure
5777 *
5778 * start: First Byte to trim
5779 * len: number of Bytes to trim from start
5780 * minlen: minimum extent length in Bytes
5781 * ext4_trim_fs goes through all allocation groups containing Bytes from
5782 * start to start+len. For each such a group ext4_trim_all_free function
5783 * is invoked to trim all free space.
5784 */
5785int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
5786{
Lukas Czerner78944082011-05-24 18:16:27 -04005787 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005788 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005789 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005790 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005791 ext4_fsblk_t first_data_blk =
5792 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005793 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005794 int ret = 0;
5795
5796 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005797 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005798 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5799 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005800
Lukas Czerner5de35e82012-10-22 18:01:19 -04005801 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5802 start >= max_blks ||
5803 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005804 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005805 if (end >= max_blks)
5806 end = max_blks - 1;
5807 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005808 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005809 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005810 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005811
Lukas Czerner913eed832012-03-21 21:22:22 -04005812 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005813 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005814 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005815 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005816 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005817
Lukas Czerner913eed832012-03-21 21:22:22 -04005818 /* end now represents the last cluster to discard in this group */
5819 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005820
5821 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005822 grp = ext4_get_group_info(sb, group);
5823 /* We only do this if the grp has never been initialized */
5824 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005825 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Lukas Czerner78944082011-05-24 18:16:27 -04005826 if (ret)
5827 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005828 }
5829
Tao Ma0ba08512011-03-23 15:48:11 -04005830 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005831 * For all the groups except the last one, last cluster will
5832 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5833 * change it for the last group, note that last_cluster is
5834 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005835 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005836 if (group == last_group)
5837 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005838
Lukas Czerner78944082011-05-24 18:16:27 -04005839 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005840 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005841 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005842 if (cnt < 0) {
5843 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005844 break;
5845 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005846 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005847 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005848
5849 /*
5850 * For every group except the first one, we are sure
5851 * that the first cluster to discard will be cluster #0.
5852 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005853 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005854 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005855
Tao Ma3d56b8d2011-07-11 00:03:38 -04005856 if (!ret)
5857 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5858
Tao Ma22f10452011-07-10 23:52:37 -04005859out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005860 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005861 return ret;
5862}
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -04005863
5864/* Iterate all the free extents in the group. */
5865int
5866ext4_mballoc_query_range(
5867 struct super_block *sb,
5868 ext4_group_t group,
5869 ext4_grpblk_t start,
5870 ext4_grpblk_t end,
5871 ext4_mballoc_query_range_fn formatter,
5872 void *priv)
5873{
5874 void *bitmap;
5875 ext4_grpblk_t next;
5876 struct ext4_buddy e4b;
5877 int error;
5878
5879 error = ext4_mb_load_buddy(sb, group, &e4b);
5880 if (error)
5881 return error;
5882 bitmap = e4b.bd_bitmap;
5883
5884 ext4_lock_group(sb, group);
5885
5886 start = (e4b.bd_info->bb_first_free > start) ?
5887 e4b.bd_info->bb_first_free : start;
5888 if (end >= EXT4_CLUSTERS_PER_GROUP(sb))
5889 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
5890
5891 while (start <= end) {
5892 start = mb_find_next_zero_bit(bitmap, end + 1, start);
5893 if (start > end)
5894 break;
5895 next = mb_find_next_bit(bitmap, end + 1, start);
5896
5897 ext4_unlock_group(sb, group);
5898 error = formatter(sb, group, start, next - start, priv);
5899 if (error)
5900 goto out_unload;
5901 ext4_lock_group(sb, group);
5902
5903 start = next + 1;
5904 }
5905
5906 ext4_unlock_group(sb, group);
5907out_unload:
5908 ext4_mb_unload_buddy(&e4b);
5909
5910 return error;
5911}