blob: c0a331e2feb02454a10f111b4d7f7f6c1cc29f09 [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
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400127 * the smallest multiple of the stripe value (sbi->s_stripe) which is
128 * 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
Alex Tomasc9de5602008-01-29 00:19:52 -0500622 {
623 static int mb_check_counter;
624 if (mb_check_counter++ % 100 != 0)
625 return 0;
626 }
627
628 while (order > 1) {
629 buddy = mb_find_buddy(e4b, order, &max);
630 MB_CHECK_ASSERT(buddy);
631 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
632 MB_CHECK_ASSERT(buddy2);
633 MB_CHECK_ASSERT(buddy != buddy2);
634 MB_CHECK_ASSERT(max * 2 == max2);
635
636 count = 0;
637 for (i = 0; i < max; i++) {
638
639 if (mb_test_bit(i, buddy)) {
640 /* only single bit in buddy2 may be 1 */
641 if (!mb_test_bit(i << 1, buddy2)) {
642 MB_CHECK_ASSERT(
643 mb_test_bit((i<<1)+1, buddy2));
644 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
645 MB_CHECK_ASSERT(
646 mb_test_bit(i << 1, buddy2));
647 }
648 continue;
649 }
650
Robin Dong0a10da72011-10-26 08:48:54 -0400651 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500652 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
653 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
654
655 for (j = 0; j < (1 << order); j++) {
656 k = (i * (1 << order)) + j;
657 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500658 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500659 }
660 count++;
661 }
662 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
663 order--;
664 }
665
666 fstart = -1;
667 buddy = mb_find_buddy(e4b, 0, &max);
668 for (i = 0; i < max; i++) {
669 if (!mb_test_bit(i, buddy)) {
670 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
671 if (fstart == -1) {
672 fragments++;
673 fstart = i;
674 }
675 continue;
676 }
677 fstart = -1;
678 /* check used bits only */
679 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
680 buddy2 = mb_find_buddy(e4b, j, &max2);
681 k = i >> j;
682 MB_CHECK_ASSERT(k < max2);
683 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
684 }
685 }
686 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
687 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
688
689 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500690 list_for_each(cur, &grp->bb_prealloc_list) {
691 ext4_group_t groupnr;
692 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400693 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
694 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500695 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400696 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500697 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
698 }
699 return 0;
700}
701#undef MB_CHECK_ASSERT
702#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400703 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500704#else
705#define mb_check_buddy(e4b)
706#endif
707
Coly Li7c786052011-02-24 13:24:25 -0500708/*
709 * Divide blocks started from @first with length @len into
710 * smaller chunks with power of 2 blocks.
711 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
712 * then increase bb_counters[] for corresponded chunk size.
713 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500714static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400715 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500716 struct ext4_group_info *grp)
717{
718 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400719 ext4_grpblk_t min;
720 ext4_grpblk_t max;
721 ext4_grpblk_t chunk;
Chandan Rajendra69e43e82016-11-14 21:04:37 -0500722 unsigned int border;
Alex Tomasc9de5602008-01-29 00:19:52 -0500723
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400724 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500725
726 border = 2 << sb->s_blocksize_bits;
727
728 while (len > 0) {
729 /* find how many blocks can be covered since this position */
730 max = ffs(first | border) - 1;
731
732 /* find how many blocks of power 2 we need to mark */
733 min = fls(len) - 1;
734
735 if (max < min)
736 min = max;
737 chunk = 1 << min;
738
739 /* mark multiblock chunks only */
740 grp->bb_counters[min]++;
741 if (min > 0)
742 mb_clear_bit(first >> min,
743 buddy + sbi->s_mb_offsets[min]);
744
745 len -= chunk;
746 first += chunk;
747 }
748}
749
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400750/*
751 * Cache the order of the largest free extent we have available in this block
752 * group.
753 */
754static void
755mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
756{
757 int i;
758 int bits;
759
760 grp->bb_largest_free_order = -1; /* uninit */
761
762 bits = sb->s_blocksize_bits + 1;
763 for (i = bits; i >= 0; i--) {
764 if (grp->bb_counters[i] > 0) {
765 grp->bb_largest_free_order = i;
766 break;
767 }
768 }
769}
770
Eric Sandeen089ceec2009-07-05 22:17:31 -0400771static noinline_for_stack
772void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500773 void *buddy, void *bitmap, ext4_group_t group)
774{
775 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400776 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400777 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400778 ext4_grpblk_t i = 0;
779 ext4_grpblk_t first;
780 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500781 unsigned free = 0;
782 unsigned fragments = 0;
783 unsigned long long period = get_cycles();
784
785 /* initialize buddy from bitmap which is aggregation
786 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500787 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500788 grp->bb_first_free = i;
789 while (i < max) {
790 fragments++;
791 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500792 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500793 len = i - first;
794 free += len;
795 if (len > 1)
796 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
797 else
798 grp->bb_counters[0]++;
799 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500800 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500801 }
802 grp->bb_fragments = fragments;
803
804 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400805 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o94d4c062014-07-05 19:15:50 -0400806 "block bitmap and bg descriptor "
807 "inconsistent: %u vs %u free clusters",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400808 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500809 /*
Darrick J. Wong163a2032013-08-28 17:35:51 -0400810 * If we intend to continue, we consider group descriptor
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500811 * corrupt and update bb_free using bitmap value
812 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500813 grp->bb_free = free;
Wang Shilongdb79e6d2018-05-12 11:39:40 -0400814 ext4_mark_group_bitmap_corrupted(sb, group,
815 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Alex Tomasc9de5602008-01-29 00:19:52 -0500816 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400817 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500818
819 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
820
821 period = get_cycles() - period;
Jun Piao49598e02018-01-11 13:17:49 -0500822 spin_lock(&sbi->s_bal_lock);
823 sbi->s_mb_buddies_generated++;
824 sbi->s_mb_generation_time += period;
825 spin_unlock(&sbi->s_bal_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -0500826}
827
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400828static void mb_regenerate_buddy(struct ext4_buddy *e4b)
829{
830 int count;
831 int order = 1;
832 void *buddy;
833
834 while ((buddy = mb_find_buddy(e4b, order++, &count))) {
835 ext4_set_bits(buddy, 0, count);
836 }
837 e4b->bd_info->bb_fragments = 0;
838 memset(e4b->bd_info->bb_counters, 0,
839 sizeof(*e4b->bd_info->bb_counters) *
840 (e4b->bd_sb->s_blocksize_bits + 2));
841
842 ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy,
843 e4b->bd_bitmap, e4b->bd_group);
844}
845
Alex Tomasc9de5602008-01-29 00:19:52 -0500846/* The buddy information is attached the buddy cache inode
847 * for convenience. The information regarding each group
848 * is loaded via ext4_mb_load_buddy. The information involve
849 * block bitmap and buddy information. The information are
850 * stored in the inode as
851 *
852 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500853 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500854 *
855 *
856 * one block each for bitmap and buddy information.
857 * So for each group we take up 2 blocks. A page can
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300858 * contain blocks_per_page (PAGE_SIZE / blocksize) blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -0500859 * So it can have information regarding groups_per_page which
860 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400861 *
862 * Locking note: This routine takes the block group lock of all groups
863 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500864 */
865
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400866static int ext4_mb_init_cache(struct page *page, char *incore, gfp_t gfp)
Alex Tomasc9de5602008-01-29 00:19:52 -0500867{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400868 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500869 int blocksize;
870 int blocks_per_page;
871 int groups_per_page;
872 int err = 0;
873 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500874 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500875 int first_block;
876 struct super_block *sb;
877 struct buffer_head *bhs;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -0400878 struct buffer_head **bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500879 struct inode *inode;
880 char *data;
881 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400882 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500883
Alex Tomasc9de5602008-01-29 00:19:52 -0500884 inode = page->mapping->host;
885 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400886 ngroups = ext4_get_groups_count(sb);
Fabian Frederick93407472017-02-27 14:28:32 -0800887 blocksize = i_blocksize(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300888 blocks_per_page = PAGE_SIZE / blocksize;
Alex Tomasc9de5602008-01-29 00:19:52 -0500889
Ritesh Harjanid3df1452020-05-10 11:54:54 +0530890 mb_debug(sb, "init page %lu\n", page->index);
891
Alex Tomasc9de5602008-01-29 00:19:52 -0500892 groups_per_page = blocks_per_page >> 1;
893 if (groups_per_page == 0)
894 groups_per_page = 1;
895
896 /* allocate buffer_heads to read bitmaps */
897 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500898 i = sizeof(struct buffer_head *) * groups_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400899 bh = kzalloc(i, gfp);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500900 if (bh == NULL) {
901 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500902 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500903 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500904 } else
905 bh = &bhs;
906
907 first_group = page->index * blocks_per_page / 2;
908
909 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500910 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
911 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500912 break;
913
Theodore Ts'o813e5722012-02-20 17:52:46 -0500914 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400915 /*
916 * If page is uptodate then we came here after online resize
917 * which added some new uninitialized group info structs, so
918 * we must skip all initialized uptodate buddies on the page,
919 * which may be currently in use by an allocating task.
920 */
921 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
922 bh[i] = NULL;
923 continue;
924 }
Darrick J. Wong9008a582015-10-17 21:33:24 -0400925 bh[i] = ext4_read_block_bitmap_nowait(sb, group);
926 if (IS_ERR(bh[i])) {
927 err = PTR_ERR(bh[i]);
928 bh[i] = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500929 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500930 }
Ritesh Harjanid3df1452020-05-10 11:54:54 +0530931 mb_debug(sb, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500932 }
933
934 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500935 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
Darrick J. Wong9008a582015-10-17 21:33:24 -0400936 int err2;
937
938 if (!bh[i])
939 continue;
940 err2 = ext4_wait_block_bitmap(sb, group, bh[i]);
941 if (!err)
942 err = err2;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500943 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500944
945 first_block = page->index * blocks_per_page;
946 for (i = 0; i < blocks_per_page; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500947 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400948 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500949 break;
950
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400951 if (!bh[group - first_group])
952 /* skip initialized uptodate buddy */
953 continue;
954
Lukas Czernerbbdc3222015-06-08 11:38:37 -0400955 if (!buffer_verified(bh[group - first_group]))
956 /* Skip faulty bitmaps */
957 continue;
958 err = 0;
959
Alex Tomasc9de5602008-01-29 00:19:52 -0500960 /*
961 * data carry information regarding this
962 * particular group in the format specified
963 * above
964 *
965 */
966 data = page_address(page) + (i * blocksize);
967 bitmap = bh[group - first_group]->b_data;
968
969 /*
970 * We place the buddy block and bitmap block
971 * close together
972 */
973 if ((first_block + i) & 1) {
974 /* this is block of buddy */
975 BUG_ON(incore == NULL);
Ritesh Harjanid3df1452020-05-10 11:54:54 +0530976 mb_debug(sb, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500977 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400978 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500979 grinfo = ext4_get_group_info(sb, group);
980 grinfo->bb_fragments = 0;
981 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400982 sizeof(*grinfo->bb_counters) *
983 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500984 /*
985 * incore got set to the group block bitmap below
986 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500987 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400988 /* init the buddy */
989 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500990 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500991 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500992 incore = NULL;
993 } else {
994 /* this is block of bitmap */
995 BUG_ON(incore != NULL);
Ritesh Harjanid3df1452020-05-10 11:54:54 +0530996 mb_debug(sb, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500997 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400998 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500999
1000 /* see comments in ext4_mb_put_pa() */
1001 ext4_lock_group(sb, group);
1002 memcpy(data, bitmap, blocksize);
1003
1004 /* mark all preallocated blks used in in-core bitmap */
1005 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05001006 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001007 ext4_unlock_group(sb, group);
1008
1009 /* set incore so that the buddy information can be
1010 * generated using this
1011 */
1012 incore = data;
1013 }
1014 }
1015 SetPageUptodate(page);
1016
1017out:
1018 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -04001019 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05001020 brelse(bh[i]);
1021 if (bh != &bhs)
1022 kfree(bh);
1023 }
1024 return err;
1025}
1026
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001027/*
Amir Goldstein2de88072011-05-09 21:48:13 -04001028 * Lock the buddy and bitmap pages. This make sure other parallel init_group
1029 * on the same buddy page doesn't happen whild holding the buddy page lock.
1030 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
1031 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001032 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001033static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001034 ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001035{
Amir Goldstein2de88072011-05-09 21:48:13 -04001036 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
1037 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001038 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -04001039 struct page *page;
1040
1041 e4b->bd_buddy_page = NULL;
1042 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001043
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001044 blocks_per_page = PAGE_SIZE / sb->s_blocksize;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001045 /*
1046 * the buddy cache inode stores the block bitmap
1047 * and buddy information in consecutive blocks.
1048 * So for each group we need two blocks.
1049 */
1050 block = group * 2;
1051 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -04001052 poff = block % blocks_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001053 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001054 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001055 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001056 BUG_ON(page->mapping != inode->i_mapping);
1057 e4b->bd_bitmap_page = page;
1058 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001059
Amir Goldstein2de88072011-05-09 21:48:13 -04001060 if (blocks_per_page >= 2) {
1061 /* buddy and bitmap are on the same page */
1062 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001063 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001064
1065 block++;
1066 pnum = block / blocks_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001067 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001068 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001069 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001070 BUG_ON(page->mapping != inode->i_mapping);
1071 e4b->bd_buddy_page = page;
1072 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001073}
1074
Amir Goldstein2de88072011-05-09 21:48:13 -04001075static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001076{
Amir Goldstein2de88072011-05-09 21:48:13 -04001077 if (e4b->bd_bitmap_page) {
1078 unlock_page(e4b->bd_bitmap_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001079 put_page(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001080 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001081 if (e4b->bd_buddy_page) {
1082 unlock_page(e4b->bd_buddy_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001083 put_page(e4b->bd_buddy_page);
Amir Goldstein2de88072011-05-09 21:48:13 -04001084 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001085}
1086
1087/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001088 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1089 * block group lock of all groups for this page; do not hold the BG lock when
1090 * calling this routine!
1091 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001092static noinline_for_stack
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001093int ext4_mb_init_group(struct super_block *sb, ext4_group_t group, gfp_t gfp)
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001094{
1095
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001096 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001097 struct ext4_buddy e4b;
1098 struct page *page;
1099 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001100
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001101 might_sleep();
Ritesh Harjanid3df1452020-05-10 11:54:54 +05301102 mb_debug(sb, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001103 this_grp = ext4_get_group_info(sb, group);
1104 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001105 * This ensures that we don't reinit the buddy cache
1106 * page which map to the group from which we are already
1107 * allocating. If we are looking at the buddy cache we would
1108 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001109 * would have pinned buddy page to page cache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001110 * The call to ext4_mb_get_buddy_page_lock will mark the
1111 * page accessed.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001112 */
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001113 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001114 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001115 /*
1116 * somebody initialized the group
1117 * return without doing anything
1118 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001119 goto err;
1120 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001121
1122 page = e4b.bd_bitmap_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001123 ret = ext4_mb_init_cache(page, NULL, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001124 if (ret)
1125 goto err;
1126 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001127 ret = -EIO;
1128 goto err;
1129 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001130
Amir Goldstein2de88072011-05-09 21:48:13 -04001131 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001132 /*
1133 * If both the bitmap and buddy are in
1134 * the same page we don't need to force
1135 * init the buddy
1136 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001137 ret = 0;
1138 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001139 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001140 /* init buddy cache */
1141 page = e4b.bd_buddy_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001142 ret = ext4_mb_init_cache(page, e4b.bd_bitmap, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001143 if (ret)
1144 goto err;
1145 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001146 ret = -EIO;
1147 goto err;
1148 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001149err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001150 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001151 return ret;
1152}
1153
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001154/*
1155 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1156 * block group lock of all groups for this page; do not hold the BG lock when
1157 * calling this routine!
1158 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001159static noinline_for_stack int
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001160ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group,
1161 struct ext4_buddy *e4b, gfp_t gfp)
Alex Tomasc9de5602008-01-29 00:19:52 -05001162{
Alex Tomasc9de5602008-01-29 00:19:52 -05001163 int blocks_per_page;
1164 int block;
1165 int pnum;
1166 int poff;
1167 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001168 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001169 struct ext4_group_info *grp;
1170 struct ext4_sb_info *sbi = EXT4_SB(sb);
1171 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001172
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001173 might_sleep();
Ritesh Harjanid3df1452020-05-10 11:54:54 +05301174 mb_debug(sb, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001175
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001176 blocks_per_page = PAGE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001177 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001178
1179 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001180 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001181 e4b->bd_sb = sb;
1182 e4b->bd_group = group;
1183 e4b->bd_buddy_page = NULL;
1184 e4b->bd_bitmap_page = NULL;
1185
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001186 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001187 /*
1188 * we need full data about the group
1189 * to make a good selection
1190 */
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001191 ret = ext4_mb_init_group(sb, group, gfp);
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001192 if (ret)
1193 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001194 }
1195
Alex Tomasc9de5602008-01-29 00:19:52 -05001196 /*
1197 * the buddy cache inode stores the block bitmap
1198 * and buddy information in consecutive blocks.
1199 * So for each group we need two blocks.
1200 */
1201 block = group * 2;
1202 pnum = block / blocks_per_page;
1203 poff = block % blocks_per_page;
1204
1205 /* we could use find_or_create_page(), but it locks page
1206 * what we'd like to avoid in fast path ... */
Mel Gorman2457aec2014-06-04 16:10:31 -07001207 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001208 if (page == NULL || !PageUptodate(page)) {
1209 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001210 /*
1211 * drop the page reference and try
1212 * to get the page with lock. If we
1213 * are not uptodate that implies
1214 * somebody just created the page but
1215 * is yet to initialize the same. So
1216 * wait for it to initialize.
1217 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001218 put_page(page);
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001219 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001220 if (page) {
1221 BUG_ON(page->mapping != inode->i_mapping);
1222 if (!PageUptodate(page)) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001223 ret = ext4_mb_init_cache(page, NULL, gfp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001224 if (ret) {
1225 unlock_page(page);
1226 goto err;
1227 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001228 mb_cmp_bitmaps(e4b, page_address(page) +
1229 (poff * sb->s_blocksize));
1230 }
1231 unlock_page(page);
1232 }
1233 }
Younger Liuc57ab392014-04-10 23:03:43 -04001234 if (page == NULL) {
1235 ret = -ENOMEM;
1236 goto err;
1237 }
1238 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001239 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001240 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001241 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001242
1243 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001244 e4b->bd_bitmap_page = page;
1245 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001246
1247 block++;
1248 pnum = block / blocks_per_page;
1249 poff = block % blocks_per_page;
1250
Mel Gorman2457aec2014-06-04 16:10:31 -07001251 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001252 if (page == NULL || !PageUptodate(page)) {
1253 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001254 put_page(page);
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001255 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001256 if (page) {
1257 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001258 if (!PageUptodate(page)) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001259 ret = ext4_mb_init_cache(page, e4b->bd_bitmap,
1260 gfp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001261 if (ret) {
1262 unlock_page(page);
1263 goto err;
1264 }
1265 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001266 unlock_page(page);
1267 }
1268 }
Younger Liuc57ab392014-04-10 23:03:43 -04001269 if (page == NULL) {
1270 ret = -ENOMEM;
1271 goto err;
1272 }
1273 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001274 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001275 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001276 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001277
1278 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001279 e4b->bd_buddy_page = page;
1280 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001281
1282 BUG_ON(e4b->bd_bitmap_page == NULL);
1283 BUG_ON(e4b->bd_buddy_page == NULL);
1284
1285 return 0;
1286
1287err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001288 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001289 put_page(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001290 if (e4b->bd_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001291 put_page(e4b->bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001292 if (e4b->bd_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001293 put_page(e4b->bd_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001294 e4b->bd_buddy = NULL;
1295 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001296 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001297}
1298
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001299static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1300 struct ext4_buddy *e4b)
1301{
1302 return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS);
1303}
1304
Jing Zhange39e07f2010-05-14 00:00:00 -04001305static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001306{
1307 if (e4b->bd_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001308 put_page(e4b->bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001309 if (e4b->bd_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001310 put_page(e4b->bd_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001311}
1312
1313
1314static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1315{
1316 int order = 1;
Nicolai Stangeb5cb3162016-05-05 17:38:03 -04001317 int bb_incr = 1 << (e4b->bd_blkbits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05001318 void *bb;
1319
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001320 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -05001321 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1322
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001323 bb = e4b->bd_buddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05001324 while (order <= e4b->bd_blkbits + 1) {
1325 block = block >> 1;
1326 if (!mb_test_bit(block, bb)) {
1327 /* this block is part of buddy of order 'order' */
1328 return order;
1329 }
Nicolai Stangeb5cb3162016-05-05 17:38:03 -04001330 bb += bb_incr;
1331 bb_incr >>= 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001332 order++;
1333 }
1334 return 0;
1335}
1336
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001337static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001338{
1339 __u32 *addr;
1340
1341 len = cur + len;
1342 while (cur < len) {
1343 if ((cur & 31) == 0 && (len - cur) >= 32) {
1344 /* fast path: clear whole word at once */
1345 addr = bm + (cur >> 3);
1346 *addr = 0;
1347 cur += 32;
1348 continue;
1349 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001350 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001351 cur++;
1352 }
1353}
1354
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001355/* clear bits in given range
1356 * will return first found zero bit if any, -1 otherwise
1357 */
1358static int mb_test_and_clear_bits(void *bm, int cur, int len)
1359{
1360 __u32 *addr;
1361 int zero_bit = -1;
1362
1363 len = cur + len;
1364 while (cur < len) {
1365 if ((cur & 31) == 0 && (len - cur) >= 32) {
1366 /* fast path: clear whole word at once */
1367 addr = bm + (cur >> 3);
1368 if (*addr != (__u32)(-1) && zero_bit == -1)
1369 zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0);
1370 *addr = 0;
1371 cur += 32;
1372 continue;
1373 }
1374 if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1)
1375 zero_bit = cur;
1376 cur++;
1377 }
1378
1379 return zero_bit;
1380}
1381
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001382void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001383{
1384 __u32 *addr;
1385
1386 len = cur + len;
1387 while (cur < len) {
1388 if ((cur & 31) == 0 && (len - cur) >= 32) {
1389 /* fast path: set whole word at once */
1390 addr = bm + (cur >> 3);
1391 *addr = 0xffffffff;
1392 cur += 32;
1393 continue;
1394 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001395 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001396 cur++;
1397 }
1398}
1399
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001400/*
1401 * _________________________________________________________________ */
1402
1403static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side)
Alex Tomasc9de5602008-01-29 00:19:52 -05001404{
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001405 if (mb_test_bit(*bit + side, bitmap)) {
1406 mb_clear_bit(*bit, bitmap);
1407 (*bit) -= side;
1408 return 1;
1409 }
1410 else {
1411 (*bit) += side;
1412 mb_set_bit(*bit, bitmap);
1413 return -1;
1414 }
1415}
1416
1417static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last)
1418{
1419 int max;
1420 int order = 1;
1421 void *buddy = mb_find_buddy(e4b, order, &max);
1422
1423 while (buddy) {
1424 void *buddy2;
1425
1426 /* Bits in range [first; last] are known to be set since
1427 * corresponding blocks were allocated. Bits in range
1428 * (first; last) will stay set because they form buddies on
1429 * upper layer. We just deal with borders if they don't
1430 * align with upper layer and then go up.
1431 * Releasing entire group is all about clearing
1432 * single bit of highest order buddy.
1433 */
1434
1435 /* Example:
1436 * ---------------------------------
1437 * | 1 | 1 | 1 | 1 |
1438 * ---------------------------------
1439 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1440 * ---------------------------------
1441 * 0 1 2 3 4 5 6 7
1442 * \_____________________/
1443 *
1444 * Neither [1] nor [6] is aligned to above layer.
1445 * Left neighbour [0] is free, so mark it busy,
1446 * decrease bb_counters and extend range to
1447 * [0; 6]
1448 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1449 * mark [6] free, increase bb_counters and shrink range to
1450 * [0; 5].
1451 * Then shift range to [0; 2], go up and do the same.
1452 */
1453
1454
1455 if (first & 1)
1456 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1);
1457 if (!(last & 1))
1458 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1);
1459 if (first > last)
1460 break;
1461 order++;
1462
1463 if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) {
1464 mb_clear_bits(buddy, first, last - first + 1);
1465 e4b->bd_info->bb_counters[order - 1] += last - first + 1;
1466 break;
1467 }
1468 first >>= 1;
1469 last >>= 1;
1470 buddy = buddy2;
1471 }
1472}
1473
1474static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1475 int first, int count)
1476{
1477 int left_is_free = 0;
1478 int right_is_free = 0;
1479 int block;
1480 int last = first + count - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001481 struct super_block *sb = e4b->bd_sb;
1482
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04001483 if (WARN_ON(count == 0))
1484 return;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001485 BUG_ON(last >= (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001486 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Darrick J. Wong163a2032013-08-28 17:35:51 -04001487 /* Don't bother if the block group is corrupt. */
1488 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
1489 return;
1490
Alex Tomasc9de5602008-01-29 00:19:52 -05001491 mb_check_buddy(e4b);
1492 mb_free_blocks_double(inode, e4b, first, count);
1493
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05301494 this_cpu_inc(discard_pa_seq);
Alex Tomasc9de5602008-01-29 00:19:52 -05001495 e4b->bd_info->bb_free += count;
1496 if (first < e4b->bd_info->bb_first_free)
1497 e4b->bd_info->bb_first_free = first;
1498
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001499 /* access memory sequentially: check left neighbour,
1500 * clear range and then check right neighbour
1501 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001502 if (first != 0)
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001503 left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap);
1504 block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count);
1505 if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0])
1506 right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap);
1507
1508 if (unlikely(block != -1)) {
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001509 struct ext4_sb_info *sbi = EXT4_SB(sb);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001510 ext4_fsblk_t blocknr;
1511
1512 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Jun Piao49598e02018-01-11 13:17:49 -05001513 blocknr += EXT4_C2B(sbi, block);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001514 ext4_grp_locked_error(sb, e4b->bd_group,
1515 inode ? inode->i_ino : 0,
1516 blocknr,
1517 "freeing already freed block "
Darrick J. Wong163a2032013-08-28 17:35:51 -04001518 "(bit %u); block bitmap corrupt.",
1519 block);
Wang Shilongdb79e6d2018-05-12 11:39:40 -04001520 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
1521 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001522 mb_regenerate_buddy(e4b);
1523 goto done;
1524 }
1525
1526 /* let's maintain fragments counter */
1527 if (left_is_free && right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001528 e4b->bd_info->bb_fragments--;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001529 else if (!left_is_free && !right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001530 e4b->bd_info->bb_fragments++;
1531
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001532 /* buddy[0] == bd_bitmap is a special case, so handle
1533 * it right away and let mb_buddy_mark_free stay free of
1534 * zero order checks.
1535 * Check if neighbours are to be coaleasced,
1536 * adjust bitmap bb_counters and borders appropriately.
1537 */
1538 if (first & 1) {
1539 first += !left_is_free;
1540 e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001541 }
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001542 if (!(last & 1)) {
1543 last -= !right_is_free;
1544 e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1;
1545 }
1546
1547 if (first <= last)
1548 mb_buddy_mark_free(e4b, first >> 1, last >> 1);
1549
1550done:
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001551 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001552 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001553}
1554
Robin Dong15c006a2012-08-17 10:02:17 -04001555static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001556 int needed, struct ext4_free_extent *ex)
1557{
1558 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001559 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001560 void *buddy;
1561
Vincent Minetbc8e6742009-05-15 08:33:18 -04001562 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001563 BUG_ON(ex == NULL);
1564
Robin Dong15c006a2012-08-17 10:02:17 -04001565 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001566 BUG_ON(buddy == NULL);
1567 BUG_ON(block >= max);
1568 if (mb_test_bit(block, buddy)) {
1569 ex->fe_len = 0;
1570 ex->fe_start = 0;
1571 ex->fe_group = 0;
1572 return 0;
1573 }
1574
Robin Dong15c006a2012-08-17 10:02:17 -04001575 /* find actual order */
1576 order = mb_find_order_for_block(e4b, block);
1577 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001578
1579 ex->fe_len = 1 << order;
1580 ex->fe_start = block << order;
1581 ex->fe_group = e4b->bd_group;
1582
1583 /* calc difference from given start */
1584 next = next - ex->fe_start;
1585 ex->fe_len -= next;
1586 ex->fe_start += next;
1587
1588 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001589 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001590
1591 if (block + 1 >= max)
1592 break;
1593
1594 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001595 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001596 break;
1597
Robin Dongb051d8d2011-10-26 05:30:30 -04001598 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001599
Alex Tomasc9de5602008-01-29 00:19:52 -05001600 block = next >> order;
1601 ex->fe_len += 1 << order;
1602 }
1603
Jan Kara31562b92019-04-06 18:33:06 -04001604 if (ex->fe_start + ex->fe_len > EXT4_CLUSTERS_PER_GROUP(e4b->bd_sb)) {
Theodore Ts'o43c73222017-01-22 19:35:52 -05001605 /* Should never happen! (but apparently sometimes does?!?) */
1606 WARN_ON(1);
1607 ext4_error(e4b->bd_sb, "corruption or bug in mb_find_extent "
1608 "block=%d, order=%d needed=%d ex=%u/%d/%d@%u",
1609 block, order, needed, ex->fe_group, ex->fe_start,
1610 ex->fe_len, ex->fe_logical);
1611 ex->fe_len = 0;
1612 ex->fe_start = 0;
1613 ex->fe_group = 0;
1614 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001615 return ex->fe_len;
1616}
1617
1618static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1619{
1620 int ord;
1621 int mlen = 0;
1622 int max = 0;
1623 int cur;
1624 int start = ex->fe_start;
1625 int len = ex->fe_len;
1626 unsigned ret = 0;
1627 int len0 = len;
1628 void *buddy;
1629
1630 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1631 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001632 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001633 mb_check_buddy(e4b);
1634 mb_mark_used_double(e4b, start, len);
1635
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05301636 this_cpu_inc(discard_pa_seq);
Alex Tomasc9de5602008-01-29 00:19:52 -05001637 e4b->bd_info->bb_free -= len;
1638 if (e4b->bd_info->bb_first_free == start)
1639 e4b->bd_info->bb_first_free += len;
1640
1641 /* let's maintain fragments counter */
1642 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001643 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001644 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001645 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001646 if (mlen && max)
1647 e4b->bd_info->bb_fragments++;
1648 else if (!mlen && !max)
1649 e4b->bd_info->bb_fragments--;
1650
1651 /* let's maintain buddy itself */
1652 while (len) {
1653 ord = mb_find_order_for_block(e4b, start);
1654
1655 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1656 /* the whole chunk may be allocated at once! */
1657 mlen = 1 << ord;
1658 buddy = mb_find_buddy(e4b, ord, &max);
1659 BUG_ON((start >> ord) >= max);
1660 mb_set_bit(start >> ord, buddy);
1661 e4b->bd_info->bb_counters[ord]--;
1662 start += mlen;
1663 len -= mlen;
1664 BUG_ON(len < 0);
1665 continue;
1666 }
1667
1668 /* store for history */
1669 if (ret == 0)
1670 ret = len | (ord << 16);
1671
1672 /* we have to split large buddy */
1673 BUG_ON(ord <= 0);
1674 buddy = mb_find_buddy(e4b, ord, &max);
1675 mb_set_bit(start >> ord, buddy);
1676 e4b->bd_info->bb_counters[ord]--;
1677
1678 ord--;
1679 cur = (start >> ord) & ~1U;
1680 buddy = mb_find_buddy(e4b, ord, &max);
1681 mb_clear_bit(cur, buddy);
1682 mb_clear_bit(cur + 1, buddy);
1683 e4b->bd_info->bb_counters[ord]++;
1684 e4b->bd_info->bb_counters[ord]++;
1685 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001686 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001687
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001688 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001689 mb_check_buddy(e4b);
1690
1691 return ret;
1692}
1693
1694/*
1695 * Must be called under group lock!
1696 */
1697static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1698 struct ext4_buddy *e4b)
1699{
1700 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1701 int ret;
1702
1703 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1704 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1705
1706 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1707 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1708 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1709
1710 /* preallocation can change ac_b_ex, thus we store actually
1711 * allocated blocks for history */
1712 ac->ac_f_ex = ac->ac_b_ex;
1713
1714 ac->ac_status = AC_STATUS_FOUND;
1715 ac->ac_tail = ret & 0xffff;
1716 ac->ac_buddy = ret >> 16;
1717
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001718 /*
1719 * take the page reference. We want the page to be pinned
1720 * so that we don't get a ext4_mb_init_cache_call for this
1721 * group until we update the bitmap. That would mean we
1722 * double allocate blocks. The reference is dropped
1723 * in ext4_mb_release_context
1724 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001725 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1726 get_page(ac->ac_bitmap_page);
1727 ac->ac_buddy_page = e4b->bd_buddy_page;
1728 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001729 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001730 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001731 spin_lock(&sbi->s_md_lock);
1732 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1733 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1734 spin_unlock(&sbi->s_md_lock);
1735 }
Ritesh Harjani53f86b12020-05-20 12:10:32 +05301736 /*
1737 * As we've just preallocated more space than
1738 * user requested originally, we store allocated
1739 * space in a special descriptor.
1740 */
1741 if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
1742 ext4_mb_new_preallocation(ac);
1743
Alex Tomasc9de5602008-01-29 00:19:52 -05001744}
1745
1746/*
1747 * regular allocator, for general purposes allocation
1748 */
1749
1750static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1751 struct ext4_buddy *e4b,
1752 int finish_group)
1753{
1754 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1755 struct ext4_free_extent *bex = &ac->ac_b_ex;
1756 struct ext4_free_extent *gex = &ac->ac_g_ex;
1757 struct ext4_free_extent ex;
1758 int max;
1759
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001760 if (ac->ac_status == AC_STATUS_FOUND)
1761 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001762 /*
1763 * We don't want to scan for a whole year
1764 */
1765 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1766 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1767 ac->ac_status = AC_STATUS_BREAK;
1768 return;
1769 }
1770
1771 /*
1772 * Haven't found good chunk so far, let's continue
1773 */
1774 if (bex->fe_len < gex->fe_len)
1775 return;
1776
1777 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1778 && bex->fe_group == e4b->bd_group) {
1779 /* recheck chunk's availability - we don't know
1780 * when it was found (within this lock-unlock
1781 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001782 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001783 if (max >= gex->fe_len) {
1784 ext4_mb_use_best_found(ac, e4b);
1785 return;
1786 }
1787 }
1788}
1789
1790/*
1791 * The routine checks whether found extent is good enough. If it is,
1792 * then the extent gets marked used and flag is set to the context
1793 * to stop scanning. Otherwise, the extent is compared with the
1794 * previous found extent and if new one is better, then it's stored
1795 * in the context. Later, the best found extent will be used, if
1796 * mballoc can't find good enough extent.
1797 *
1798 * FIXME: real allocation policy is to be designed yet!
1799 */
1800static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1801 struct ext4_free_extent *ex,
1802 struct ext4_buddy *e4b)
1803{
1804 struct ext4_free_extent *bex = &ac->ac_b_ex;
1805 struct ext4_free_extent *gex = &ac->ac_g_ex;
1806
1807 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001808 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1809 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001810 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1811
1812 ac->ac_found++;
1813
1814 /*
1815 * The special case - take what you catch first
1816 */
1817 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1818 *bex = *ex;
1819 ext4_mb_use_best_found(ac, e4b);
1820 return;
1821 }
1822
1823 /*
1824 * Let's check whether the chuck is good enough
1825 */
1826 if (ex->fe_len == gex->fe_len) {
1827 *bex = *ex;
1828 ext4_mb_use_best_found(ac, e4b);
1829 return;
1830 }
1831
1832 /*
1833 * If this is first found extent, just store it in the context
1834 */
1835 if (bex->fe_len == 0) {
1836 *bex = *ex;
1837 return;
1838 }
1839
1840 /*
1841 * If new found extent is better, store it in the context
1842 */
1843 if (bex->fe_len < gex->fe_len) {
1844 /* if the request isn't satisfied, any found extent
1845 * larger than previous best one is better */
1846 if (ex->fe_len > bex->fe_len)
1847 *bex = *ex;
1848 } else if (ex->fe_len > gex->fe_len) {
1849 /* if the request is satisfied, then we try to find
1850 * an extent that still satisfy the request, but is
1851 * smaller than previous one */
1852 if (ex->fe_len < bex->fe_len)
1853 *bex = *ex;
1854 }
1855
1856 ext4_mb_check_limits(ac, e4b, 0);
1857}
1858
Eric Sandeen089ceec2009-07-05 22:17:31 -04001859static noinline_for_stack
1860int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001861 struct ext4_buddy *e4b)
1862{
1863 struct ext4_free_extent ex = ac->ac_b_ex;
1864 ext4_group_t group = ex.fe_group;
1865 int max;
1866 int err;
1867
1868 BUG_ON(ex.fe_len <= 0);
1869 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1870 if (err)
1871 return err;
1872
1873 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001874 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001875
1876 if (max > 0) {
1877 ac->ac_b_ex = ex;
1878 ext4_mb_use_best_found(ac, e4b);
1879 }
1880
1881 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001882 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001883
1884 return 0;
1885}
1886
Eric Sandeen089ceec2009-07-05 22:17:31 -04001887static noinline_for_stack
1888int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001889 struct ext4_buddy *e4b)
1890{
1891 ext4_group_t group = ac->ac_g_ex.fe_group;
1892 int max;
1893 int err;
1894 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001895 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001896 struct ext4_free_extent ex;
1897
1898 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1899 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001900 if (grp->bb_free == 0)
1901 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001902
1903 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1904 if (err)
1905 return err;
1906
Darrick J. Wong163a2032013-08-28 17:35:51 -04001907 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) {
1908 ext4_mb_unload_buddy(e4b);
1909 return 0;
1910 }
1911
Alex Tomasc9de5602008-01-29 00:19:52 -05001912 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001913 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001914 ac->ac_g_ex.fe_len, &ex);
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001915 ex.fe_logical = 0xDEADFA11; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001916
1917 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1918 ext4_fsblk_t start;
1919
Akinobu Mita5661bd62010-03-03 23:53:39 -05001920 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1921 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001922 /* use do_div to get remainder (would be 64-bit modulo) */
1923 if (do_div(start, sbi->s_stripe) == 0) {
1924 ac->ac_found++;
1925 ac->ac_b_ex = ex;
1926 ext4_mb_use_best_found(ac, e4b);
1927 }
1928 } else if (max >= ac->ac_g_ex.fe_len) {
1929 BUG_ON(ex.fe_len <= 0);
1930 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1931 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1932 ac->ac_found++;
1933 ac->ac_b_ex = ex;
1934 ext4_mb_use_best_found(ac, e4b);
1935 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1936 /* Sometimes, caller may want to merge even small
1937 * number of blocks to an existing extent */
1938 BUG_ON(ex.fe_len <= 0);
1939 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1940 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1941 ac->ac_found++;
1942 ac->ac_b_ex = ex;
1943 ext4_mb_use_best_found(ac, e4b);
1944 }
1945 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001946 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001947
1948 return 0;
1949}
1950
1951/*
1952 * The routine scans buddy structures (not bitmap!) from given order
1953 * to max order and tries to find big enough chunk to satisfy the req
1954 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001955static noinline_for_stack
1956void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001957 struct ext4_buddy *e4b)
1958{
1959 struct super_block *sb = ac->ac_sb;
1960 struct ext4_group_info *grp = e4b->bd_info;
1961 void *buddy;
1962 int i;
1963 int k;
1964 int max;
1965
1966 BUG_ON(ac->ac_2order <= 0);
1967 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1968 if (grp->bb_counters[i] == 0)
1969 continue;
1970
1971 buddy = mb_find_buddy(e4b, i, &max);
1972 BUG_ON(buddy == NULL);
1973
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001974 k = mb_find_next_zero_bit(buddy, max, 0);
Dmitry Monakhoveb576082020-03-10 15:01:56 +00001975 if (k >= max) {
1976 ext4_grp_locked_error(ac->ac_sb, e4b->bd_group, 0, 0,
1977 "%d free clusters of order %d. But found 0",
1978 grp->bb_counters[i], i);
1979 ext4_mark_group_bitmap_corrupted(ac->ac_sb,
1980 e4b->bd_group,
1981 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
1982 break;
1983 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001984 ac->ac_found++;
1985
1986 ac->ac_b_ex.fe_len = 1 << i;
1987 ac->ac_b_ex.fe_start = k << i;
1988 ac->ac_b_ex.fe_group = e4b->bd_group;
1989
1990 ext4_mb_use_best_found(ac, e4b);
1991
Ritesh Harjani53f86b12020-05-20 12:10:32 +05301992 BUG_ON(ac->ac_f_ex.fe_len != ac->ac_g_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05001993
1994 if (EXT4_SB(sb)->s_mb_stats)
1995 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1996
1997 break;
1998 }
1999}
2000
2001/*
2002 * The routine scans the group and measures all found extents.
2003 * In order to optimize scanning, caller must pass number of
2004 * free blocks in the group, so the routine can know upper limit.
2005 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04002006static noinline_for_stack
2007void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002008 struct ext4_buddy *e4b)
2009{
2010 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05002011 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05002012 struct ext4_free_extent ex;
2013 int i;
2014 int free;
2015
2016 free = e4b->bd_info->bb_free;
Theodore Ts'o907ea522020-04-13 23:33:05 -04002017 if (WARN_ON(free <= 0))
2018 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05002019
2020 i = e4b->bd_info->bb_first_free;
2021
2022 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05002023 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04002024 EXT4_CLUSTERS_PER_GROUP(sb), i);
2025 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002026 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05002027 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002028 * free blocks even though group info says we
2029 * we have free blocks
2030 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04002031 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002032 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05002033 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002034 free);
Wang Shilong736dedb2018-05-12 12:37:58 -04002035 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
2036 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002037 break;
2038 }
2039
Robin Dong15c006a2012-08-17 10:02:17 -04002040 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Theodore Ts'o907ea522020-04-13 23:33:05 -04002041 if (WARN_ON(ex.fe_len <= 0))
2042 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002043 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04002044 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002045 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05002046 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002047 free, ex.fe_len);
Wang Shilong736dedb2018-05-12 12:37:58 -04002048 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
2049 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05002050 /*
2051 * The number of free blocks differs. This mostly
2052 * indicate that the bitmap is corrupt. So exit
2053 * without claiming the space.
2054 */
2055 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05002056 }
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002057 ex.fe_logical = 0xDEADC0DE; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002058 ext4_mb_measure_extent(ac, &ex, e4b);
2059
2060 i += ex.fe_len;
2061 free -= ex.fe_len;
2062 }
2063
2064 ext4_mb_check_limits(ac, e4b, 1);
2065}
2066
2067/*
2068 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002069 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05002070 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04002071static noinline_for_stack
2072void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002073 struct ext4_buddy *e4b)
2074{
2075 struct super_block *sb = ac->ac_sb;
2076 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05002077 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05002078 struct ext4_free_extent ex;
2079 ext4_fsblk_t first_group_block;
2080 ext4_fsblk_t a;
2081 ext4_grpblk_t i;
2082 int max;
2083
2084 BUG_ON(sbi->s_stripe == 0);
2085
2086 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05002087 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
2088
Alex Tomasc9de5602008-01-29 00:19:52 -05002089 a = first_group_block + sbi->s_stripe - 1;
2090 do_div(a, sbi->s_stripe);
2091 i = (a * sbi->s_stripe) - first_group_block;
2092
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04002093 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002094 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04002095 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002096 if (max >= sbi->s_stripe) {
2097 ac->ac_found++;
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002098 ex.fe_logical = 0xDEADF00D; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002099 ac->ac_b_ex = ex;
2100 ext4_mb_use_best_found(ac, e4b);
2101 break;
2102 }
2103 }
2104 i += sbi->s_stripe;
2105 }
2106}
2107
Lukas Czerner42ac1842015-06-08 11:40:40 -04002108/*
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302109 * This is also called BEFORE we load the buddy bitmap.
Lukas Czerner42ac1842015-06-08 11:40:40 -04002110 * Returns either 1 or 0 indicating that the group is either suitable
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302111 * for the allocation or not.
Lukas Czerner42ac1842015-06-08 11:40:40 -04002112 */
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302113static bool ext4_mb_good_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002114 ext4_group_t group, int cr)
2115{
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302116 ext4_grpblk_t free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002117 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002118 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2119
2120 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002121
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002122 free = grp->bb_free;
2123 if (free == 0)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302124 return false;
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002125 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302126 return false;
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002127
Darrick J. Wong163a2032013-08-28 17:35:51 -04002128 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302129 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05002130
Alex Tomasc9de5602008-01-29 00:19:52 -05002131 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05002132 if (fragments == 0)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302133 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05002134
2135 switch (cr) {
2136 case 0:
2137 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05002138
Theodore Ts'oa4912122009-03-12 12:18:34 -04002139 /* Avoid using the first bg of a flexgroup for data files */
2140 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
2141 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
2142 ((group % flex_size) == 0))
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302143 return false;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002144
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002145 if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) ||
2146 (free / fragments) >= ac->ac_g_ex.fe_len)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302147 return true;
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002148
2149 if (grp->bb_largest_free_order < ac->ac_2order)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302150 return false;
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002151
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302152 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05002153 case 1:
2154 if ((free / fragments) >= ac->ac_g_ex.fe_len)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302155 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05002156 break;
2157 case 2:
2158 if (free >= ac->ac_g_ex.fe_len)
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302159 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05002160 break;
2161 case 3:
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302162 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05002163 default:
2164 BUG();
2165 }
2166
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302167 return false;
2168}
2169
2170/*
2171 * This could return negative error code if something goes wrong
2172 * during ext4_mb_init_group(). This should not be called with
2173 * ext4_lock_group() held.
2174 */
2175static int ext4_mb_good_group_nolock(struct ext4_allocation_context *ac,
2176 ext4_group_t group, int cr)
2177{
2178 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Ritesh Harjani99377832020-05-20 12:10:36 +05302179 struct super_block *sb = ac->ac_sb;
2180 bool should_lock = ac->ac_flags & EXT4_MB_STRICT_CHECK;
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302181 ext4_grpblk_t free;
2182 int ret = 0;
2183
Ritesh Harjani99377832020-05-20 12:10:36 +05302184 if (should_lock)
2185 ext4_lock_group(sb, group);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302186 free = grp->bb_free;
2187 if (free == 0)
2188 goto out;
2189 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
2190 goto out;
2191 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
2192 goto out;
Ritesh Harjani99377832020-05-20 12:10:36 +05302193 if (should_lock)
2194 ext4_unlock_group(sb, group);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302195
2196 /* We only do this if the grp has never been initialized */
2197 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
2198 ret = ext4_mb_init_group(ac->ac_sb, group, GFP_NOFS);
2199 if (ret)
2200 return ret;
2201 }
2202
Ritesh Harjani99377832020-05-20 12:10:36 +05302203 if (should_lock)
2204 ext4_lock_group(sb, group);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302205 ret = ext4_mb_good_group(ac, group, cr);
2206out:
Ritesh Harjani99377832020-05-20 12:10:36 +05302207 if (should_lock)
2208 ext4_unlock_group(sb, group);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302209 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002210}
2211
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002212static noinline_for_stack int
2213ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002214{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002215 ext4_group_t ngroups, group, i;
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05302216 int cr = -1;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002217 int err = 0, first_err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002218 struct ext4_sb_info *sbi;
2219 struct super_block *sb;
2220 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002221
2222 sb = ac->ac_sb;
2223 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002224 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002225 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002226 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002227 ngroups = sbi->s_blockfile_groups;
2228
Alex Tomasc9de5602008-01-29 00:19:52 -05002229 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2230
2231 /* first, try the goal */
2232 err = ext4_mb_find_by_goal(ac, &e4b);
2233 if (err || ac->ac_status == AC_STATUS_FOUND)
2234 goto out;
2235
2236 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2237 goto out;
2238
2239 /*
2240 * ac->ac2_order is set only if the fe_len is a power of 2
2241 * if ac2_order is set we also set criteria to 0 so that we
2242 * try exact allocation using buddy.
2243 */
2244 i = fls(ac->ac_g_ex.fe_len);
2245 ac->ac_2order = 0;
2246 /*
2247 * We search using buddy data only if the order of the request
2248 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002249 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Jan Karad9b22cf2017-02-10 00:50:56 -05002250 * We also support searching for power-of-two requests only for
2251 * requests upto maximum buddy size we have constructed.
Alex Tomasc9de5602008-01-29 00:19:52 -05002252 */
Jan Karad9b22cf2017-02-10 00:50:56 -05002253 if (i >= sbi->s_mb_order2_reqs && i <= sb->s_blocksize_bits + 2) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002254 /*
2255 * This should tell if fe_len is exactly power of 2
2256 */
2257 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
Jeremy Cline1a5d5e52018-08-02 00:03:40 -04002258 ac->ac_2order = array_index_nospec(i - 1,
2259 sb->s_blocksize_bits + 2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002260 }
2261
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002262 /* if stream allocation is enabled, use global goal */
2263 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002264 /* TBD: may be hot point */
2265 spin_lock(&sbi->s_md_lock);
2266 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2267 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2268 spin_unlock(&sbi->s_md_lock);
2269 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002270
Alex Tomasc9de5602008-01-29 00:19:52 -05002271 /* Let's just scan groups to find more-less suitable blocks */
2272 cr = ac->ac_2order ? 0 : 1;
2273 /*
2274 * cr == 0 try to get exact allocation,
2275 * cr == 3 try to get anything
2276 */
2277repeat:
2278 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2279 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002280 /*
2281 * searching for the right group start
2282 * from the goal value specified
2283 */
2284 group = ac->ac_g_ex.fe_group;
2285
Theodore Ts'o8df96752009-05-01 08:50:38 -04002286 for (i = 0; i < ngroups; group++, i++) {
Lukas Czerner42ac1842015-06-08 11:40:40 -04002287 int ret = 0;
Theodore Ts'o2ed57242013-06-12 11:43:02 -04002288 cond_resched();
Lachlan McIlroye6155732013-05-05 23:10:00 -04002289 /*
2290 * Artificially restricted ngroups for non-extent
2291 * files makes group > ngroups possible on first loop.
2292 */
2293 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002294 group = 0;
2295
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002296 /* This now checks without needing the buddy page */
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302297 ret = ext4_mb_good_group_nolock(ac, group, cr);
Lukas Czerner42ac1842015-06-08 11:40:40 -04002298 if (ret <= 0) {
2299 if (!first_err)
2300 first_err = ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002301 continue;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002302 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002303
Alex Tomasc9de5602008-01-29 00:19:52 -05002304 err = ext4_mb_load_buddy(sb, group, &e4b);
2305 if (err)
2306 goto out;
2307
2308 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002309
2310 /*
2311 * We need to check again after locking the
2312 * block group
2313 */
Lukas Czerner42ac1842015-06-08 11:40:40 -04002314 ret = ext4_mb_good_group(ac, group, cr);
Ritesh Harjani8ef123fe2020-05-20 12:10:35 +05302315 if (ret == 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002316 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002317 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002318 continue;
2319 }
2320
2321 ac->ac_groups_scanned++;
Jan Karad9b22cf2017-02-10 00:50:56 -05002322 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002323 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002324 else if (cr == 1 && sbi->s_stripe &&
2325 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002326 ext4_mb_scan_aligned(ac, &e4b);
2327 else
2328 ext4_mb_complex_scan_group(ac, &e4b);
2329
2330 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002331 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002332
2333 if (ac->ac_status != AC_STATUS_CONTINUE)
2334 break;
2335 }
2336 }
2337
2338 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2339 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2340 /*
2341 * We've been searching too long. Let's try to allocate
2342 * the best chunk we've found so far
2343 */
2344
2345 ext4_mb_try_best_found(ac, &e4b);
2346 if (ac->ac_status != AC_STATUS_FOUND) {
2347 /*
2348 * Someone more lucky has already allocated it.
2349 * The only thing we can do is just take first
2350 * found block(s)
2351 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2352 */
2353 ac->ac_b_ex.fe_group = 0;
2354 ac->ac_b_ex.fe_start = 0;
2355 ac->ac_b_ex.fe_len = 0;
2356 ac->ac_status = AC_STATUS_CONTINUE;
2357 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2358 cr = 3;
2359 atomic_inc(&sbi->s_mb_lost_chunks);
2360 goto repeat;
2361 }
2362 }
2363out:
Lukas Czerner42ac1842015-06-08 11:40:40 -04002364 if (!err && ac->ac_status != AC_STATUS_FOUND && first_err)
2365 err = first_err;
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05302366
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302367 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 +05302368 ac->ac_b_ex.fe_len, ac->ac_o_ex.fe_len, ac->ac_status,
2369 ac->ac_flags, cr, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05002370 return err;
2371}
2372
Alex Tomasc9de5602008-01-29 00:19:52 -05002373static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2374{
Christoph Hellwig247dbed2018-04-11 11:37:23 +02002375 struct super_block *sb = PDE_DATA(file_inode(seq->file));
Alex Tomasc9de5602008-01-29 00:19:52 -05002376 ext4_group_t group;
2377
Theodore Ts'o8df96752009-05-01 08:50:38 -04002378 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002379 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002380 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002381 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002382}
2383
2384static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2385{
Christoph Hellwig247dbed2018-04-11 11:37:23 +02002386 struct super_block *sb = PDE_DATA(file_inode(seq->file));
Alex Tomasc9de5602008-01-29 00:19:52 -05002387 ext4_group_t group;
2388
2389 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002390 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002391 return NULL;
2392 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002393 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002394}
2395
2396static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2397{
Christoph Hellwig247dbed2018-04-11 11:37:23 +02002398 struct super_block *sb = PDE_DATA(file_inode(seq->file));
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002399 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002400 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002401 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002402 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002403 struct ext4_group_info *grinfo;
Arnd Bergmann2df2c342017-08-05 21:57:46 -04002404 unsigned char blocksize_bits = min_t(unsigned char,
2405 sb->s_blocksize_bits,
2406 EXT4_MAX_BLOCK_LOG_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -05002407 struct sg {
2408 struct ext4_group_info info;
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002409 ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2];
Alex Tomasc9de5602008-01-29 00:19:52 -05002410 } sg;
2411
2412 group--;
2413 if (group == 0)
Rasmus Villemoes97b4af22015-06-15 00:32:58 -04002414 seq_puts(seq, "#group: free frags first ["
2415 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
Huaitong Han802cf1f2016-02-12 00:17:16 -05002416 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002417
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002418 i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2419 sizeof(struct ext4_group_info);
2420
Aditya Kali1c8457c2012-06-30 19:10:57 -04002421 grinfo = ext4_get_group_info(sb, group);
2422 /* Load the group info in memory only if not already loaded. */
2423 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2424 err = ext4_mb_load_buddy(sb, group, &e4b);
2425 if (err) {
2426 seq_printf(seq, "#%-5u: I/O error\n", group);
2427 return 0;
2428 }
2429 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002430 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002431
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002432 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002433
2434 if (buddy_loaded)
2435 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002436
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002437 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002438 sg.info.bb_fragments, sg.info.bb_first_free);
2439 for (i = 0; i <= 13; i++)
Arnd Bergmann2df2c342017-08-05 21:57:46 -04002440 seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ?
Alex Tomasc9de5602008-01-29 00:19:52 -05002441 sg.info.bb_counters[i] : 0);
2442 seq_printf(seq, " ]\n");
2443
2444 return 0;
2445}
2446
2447static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2448{
2449}
2450
Christoph Hellwig247dbed2018-04-11 11:37:23 +02002451const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002452 .start = ext4_mb_seq_groups_start,
2453 .next = ext4_mb_seq_groups_next,
2454 .stop = ext4_mb_seq_groups_stop,
2455 .show = ext4_mb_seq_groups_show,
2456};
2457
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002458static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2459{
2460 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2461 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2462
2463 BUG_ON(!cachep);
2464 return cachep;
2465}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002466
Theodore Ts'o28623c22012-09-05 01:31:50 -04002467/*
2468 * Allocate the top-level s_group_info array for the specified number
2469 * of groups
2470 */
2471int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2472{
2473 struct ext4_sb_info *sbi = EXT4_SB(sb);
2474 unsigned size;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002475 struct ext4_group_info ***old_groupinfo, ***new_groupinfo;
Theodore Ts'o28623c22012-09-05 01:31:50 -04002476
2477 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2478 EXT4_DESC_PER_BLOCK_BITS(sb);
2479 if (size <= sbi->s_group_info_size)
2480 return 0;
2481
2482 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
Michal Hockoa7c3e902017-05-08 15:57:09 -07002483 new_groupinfo = kvzalloc(size, GFP_KERNEL);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002484 if (!new_groupinfo) {
2485 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2486 return -ENOMEM;
2487 }
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002488 rcu_read_lock();
2489 old_groupinfo = rcu_dereference(sbi->s_group_info);
2490 if (old_groupinfo)
2491 memcpy(new_groupinfo, old_groupinfo,
Theodore Ts'o28623c22012-09-05 01:31:50 -04002492 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002493 rcu_read_unlock();
2494 rcu_assign_pointer(sbi->s_group_info, new_groupinfo);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002495 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002496 if (old_groupinfo)
2497 ext4_kvfree_array_rcu(old_groupinfo);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002498 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2499 sbi->s_group_info_size);
2500 return 0;
2501}
2502
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002503/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002504int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002505 struct ext4_group_desc *desc)
2506{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002507 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002508 int metalen = 0;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002509 int idx = group >> EXT4_DESC_PER_BLOCK_BITS(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002510 struct ext4_sb_info *sbi = EXT4_SB(sb);
2511 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002512 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002513
2514 /*
2515 * First check if this group is the first of a reserved block.
2516 * If it's true, we have to allocate a new table of pointers
2517 * to ext4_group_info structures
2518 */
2519 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2520 metalen = sizeof(*meta_group_info) <<
2521 EXT4_DESC_PER_BLOCK_BITS(sb);
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002522 meta_group_info = kmalloc(metalen, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002523 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002524 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002525 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002526 goto exit_meta_group_info;
2527 }
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002528 rcu_read_lock();
2529 rcu_dereference(sbi->s_group_info)[idx] = meta_group_info;
2530 rcu_read_unlock();
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002531 }
2532
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002533 meta_group_info = sbi_array_rcu_deref(sbi, s_group_info, idx);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002534 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2535
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002536 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002537 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002538 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002539 goto exit_group_info;
2540 }
2541 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2542 &(meta_group_info[i]->bb_state));
2543
2544 /*
2545 * initialize bb_free to be able to skip
2546 * empty groups without initialization
2547 */
Theodore Ts'o88446182018-06-14 00:58:00 -04002548 if (ext4_has_group_desc_csum(sb) &&
2549 (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002550 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002551 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002552 } else {
2553 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002554 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002555 }
2556
2557 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002558 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002559 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002560 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002561
Ritesh Harjania3450212020-05-10 11:54:48 +05302562 mb_group_bb_bitmap_alloc(sb, meta_group_info[i], group);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002563 return 0;
2564
2565exit_group_info:
2566 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002567 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002568 struct ext4_group_info ***group_info;
2569
2570 rcu_read_lock();
2571 group_info = rcu_dereference(sbi->s_group_info);
2572 kfree(group_info[idx]);
2573 group_info[idx] = NULL;
2574 rcu_read_unlock();
Tao Macaaf7a22011-07-11 18:42:42 -04002575 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002576exit_meta_group_info:
2577 return -ENOMEM;
2578} /* ext4_mb_add_groupinfo */
2579
Alex Tomasc9de5602008-01-29 00:19:52 -05002580static int ext4_mb_init_backend(struct super_block *sb)
2581{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002582 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002583 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002584 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002585 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002586 struct ext4_group_desc *desc;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002587 struct ext4_group_info ***group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002588 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002589
Theodore Ts'o28623c22012-09-05 01:31:50 -04002590 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2591 if (err)
2592 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002593
Alex Tomasc9de5602008-01-29 00:19:52 -05002594 sbi->s_buddy_cache = new_inode(sb);
2595 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002596 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002597 goto err_freesgi;
2598 }
Yu Jian48e60612011-08-01 17:41:39 -04002599 /* To avoid potentially colliding with an valid on-disk inode number,
2600 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2601 * not in the inode hash, so it should never be found by iget(), but
2602 * this will avoid confusion if it ever shows up during debugging. */
2603 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002604 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002605 for (i = 0; i < ngroups; i++) {
Khazhismel Kumykov4b99faa2019-04-25 12:58:01 -04002606 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05002607 desc = ext4_get_group_desc(sb, i, NULL);
2608 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002609 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002610 goto err_freebuddy;
2611 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002612 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2613 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002614 }
2615
2616 return 0;
2617
2618err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002619 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002620 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002621 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002622 i = sbi->s_group_info_size;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002623 rcu_read_lock();
2624 group_info = rcu_dereference(sbi->s_group_info);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002625 while (i-- > 0)
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002626 kfree(group_info[i]);
2627 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002628 iput(sbi->s_buddy_cache);
2629err_freesgi:
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002630 rcu_read_lock();
2631 kvfree(rcu_dereference(sbi->s_group_info));
2632 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002633 return -ENOMEM;
2634}
2635
Eric Sandeen2892c152011-02-12 08:12:18 -05002636static void ext4_groupinfo_destroy_slabs(void)
2637{
2638 int i;
2639
2640 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
Sean Fu21c580d2018-05-20 22:44:13 -04002641 kmem_cache_destroy(ext4_groupinfo_caches[i]);
Eric Sandeen2892c152011-02-12 08:12:18 -05002642 ext4_groupinfo_caches[i] = NULL;
2643 }
2644}
2645
2646static int ext4_groupinfo_create_slab(size_t size)
2647{
2648 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2649 int slab_size;
2650 int blocksize_bits = order_base_2(size);
2651 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2652 struct kmem_cache *cachep;
2653
2654 if (cache_index >= NR_GRPINFO_CACHES)
2655 return -EINVAL;
2656
2657 if (unlikely(cache_index < 0))
2658 cache_index = 0;
2659
2660 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2661 if (ext4_groupinfo_caches[cache_index]) {
2662 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2663 return 0; /* Already created */
2664 }
2665
2666 slab_size = offsetof(struct ext4_group_info,
2667 bb_counters[blocksize_bits + 2]);
2668
2669 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2670 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2671 NULL);
2672
Tao Ma823ba012011-07-11 18:26:01 -04002673 ext4_groupinfo_caches[cache_index] = cachep;
2674
Eric Sandeen2892c152011-02-12 08:12:18 -05002675 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2676 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002677 printk(KERN_EMERG
2678 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002679 return -ENOMEM;
2680 }
2681
Eric Sandeen2892c152011-02-12 08:12:18 -05002682 return 0;
2683}
2684
Akira Fujita9d990122012-05-28 14:19:25 -04002685int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002686{
2687 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002688 unsigned i, j;
Nicolai Stange935244c2016-05-05 19:46:19 -04002689 unsigned offset, offset_incr;
Alex Tomasc9de5602008-01-29 00:19:52 -05002690 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002691 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002692
Eric Sandeen19278052009-08-25 22:36:25 -04002693 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002694
2695 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2696 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002697 ret = -ENOMEM;
2698 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002699 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002700
Eric Sandeen19278052009-08-25 22:36:25 -04002701 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002702 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2703 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002704 ret = -ENOMEM;
2705 goto out;
2706 }
2707
Eric Sandeen2892c152011-02-12 08:12:18 -05002708 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2709 if (ret < 0)
2710 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002711
2712 /* order 0 is regular bitmap */
2713 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2714 sbi->s_mb_offsets[0] = 0;
2715
2716 i = 1;
2717 offset = 0;
Nicolai Stange935244c2016-05-05 19:46:19 -04002718 offset_incr = 1 << (sb->s_blocksize_bits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05002719 max = sb->s_blocksize << 2;
2720 do {
2721 sbi->s_mb_offsets[i] = offset;
2722 sbi->s_mb_maxs[i] = max;
Nicolai Stange935244c2016-05-05 19:46:19 -04002723 offset += offset_incr;
2724 offset_incr = offset_incr >> 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002725 max = max >> 1;
2726 i++;
2727 } while (i <= sb->s_blocksize_bits + 1);
2728
Alex Tomasc9de5602008-01-29 00:19:52 -05002729 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002730 spin_lock_init(&sbi->s_bal_lock);
Theodore Ts'od08854f2016-06-26 18:24:01 -04002731 sbi->s_mb_free_pending = 0;
Daeho Jeonga0154342017-06-22 23:54:33 -04002732 INIT_LIST_HEAD(&sbi->s_freed_data_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002733
2734 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2735 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2736 sbi->s_mb_stats = MB_DEFAULT_STATS;
2737 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2738 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002739 /*
2740 * The default group preallocation is 512, which for 4k block
2741 * sizes translates to 2 megabytes. However for bigalloc file
2742 * systems, this is probably too big (i.e, if the cluster size
2743 * is 1 megabyte, then group preallocation size becomes half a
2744 * gigabyte!). As a default, we will keep a two megabyte
2745 * group pralloc size for cluster sizes up to 64k, and after
2746 * that, we will force a minimum group preallocation size of
2747 * 32 clusters. This translates to 8 megs when the cluster
2748 * size is 256k, and 32 megs when the cluster size is 1 meg,
2749 * which seems reasonable as a default.
2750 */
2751 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2752 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002753 /*
2754 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2755 * to the lowest multiple of s_stripe which is bigger than
2756 * the s_mb_group_prealloc as determined above. We want
2757 * the preallocation size to be an exact multiple of the
2758 * RAID stripe size so that preallocations don't fragment
2759 * the stripes.
2760 */
2761 if (sbi->s_stripe > 1) {
2762 sbi->s_mb_group_prealloc = roundup(
2763 sbi->s_mb_group_prealloc, sbi->s_stripe);
2764 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002765
Eric Sandeen730c2132008-09-13 15:23:29 -04002766 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002767 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002768 ret = -ENOMEM;
Andrey Tsyvarev029b10c2014-05-12 12:34:21 -04002769 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002770 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002771 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002772 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002773 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002774 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002775 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2776 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002777 spin_lock_init(&lg->lg_prealloc_lock);
2778 }
2779
Yu Jian79a77c52011-08-01 17:41:46 -04002780 /* init file for buddy data */
2781 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002782 if (ret != 0)
2783 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002784
Tao Ma7aa0bae2011-10-06 10:22:28 -04002785 return 0;
2786
2787out_free_locality_groups:
2788 free_percpu(sbi->s_locality_groups);
2789 sbi->s_locality_groups = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002790out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002791 kfree(sbi->s_mb_offsets);
2792 sbi->s_mb_offsets = NULL;
2793 kfree(sbi->s_mb_maxs);
2794 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002795 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002796}
2797
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002798/* need to called with the ext4 group lock held */
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302799static int ext4_mb_cleanup_pa(struct ext4_group_info *grp)
Alex Tomasc9de5602008-01-29 00:19:52 -05002800{
2801 struct ext4_prealloc_space *pa;
2802 struct list_head *cur, *tmp;
2803 int count = 0;
2804
2805 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2806 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2807 list_del(&pa->pa_group_list);
2808 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002809 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002810 }
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302811 return count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002812}
2813
2814int ext4_mb_release(struct super_block *sb)
2815{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002816 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002817 ext4_group_t i;
2818 int num_meta_group_infos;
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002819 struct ext4_group_info *grinfo, ***group_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002820 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002821 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302822 int count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002823
Alex Tomasc9de5602008-01-29 00:19:52 -05002824 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002825 for (i = 0; i < ngroups; i++) {
Khazhismel Kumykov4b99faa2019-04-25 12:58:01 -04002826 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05002827 grinfo = ext4_get_group_info(sb, i);
Ritesh Harjania3450212020-05-10 11:54:48 +05302828 mb_group_bb_bitmap_free(grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002829 ext4_lock_group(sb, i);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302830 count = ext4_mb_cleanup_pa(grinfo);
2831 if (count)
2832 mb_debug(sb, "mballoc: %d PAs left\n",
2833 count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002834 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002835 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002836 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002837 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002838 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2839 EXT4_DESC_PER_BLOCK_BITS(sb);
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002840 rcu_read_lock();
2841 group_info = rcu_dereference(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002842 for (i = 0; i < num_meta_group_infos; i++)
Suraj Jitindar Singhdf3da4e2020-02-18 19:08:50 -08002843 kfree(group_info[i]);
2844 kvfree(group_info);
2845 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002846 }
2847 kfree(sbi->s_mb_offsets);
2848 kfree(sbi->s_mb_maxs);
Markus Elfringbfcba2d2014-11-25 20:01:37 -05002849 iput(sbi->s_buddy_cache);
Alex Tomasc9de5602008-01-29 00:19:52 -05002850 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002851 ext4_msg(sb, KERN_INFO,
2852 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002853 atomic_read(&sbi->s_bal_allocated),
2854 atomic_read(&sbi->s_bal_reqs),
2855 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002856 ext4_msg(sb, KERN_INFO,
2857 "mballoc: %u extents scanned, %u goal hits, "
2858 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002859 atomic_read(&sbi->s_bal_ex_scanned),
2860 atomic_read(&sbi->s_bal_goals),
2861 atomic_read(&sbi->s_bal_2orders),
2862 atomic_read(&sbi->s_bal_breaks),
2863 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002864 ext4_msg(sb, KERN_INFO,
2865 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002866 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002867 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002868 ext4_msg(sb, KERN_INFO,
2869 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002870 atomic_read(&sbi->s_mb_preallocated),
2871 atomic_read(&sbi->s_mb_discarded));
2872 }
2873
Eric Sandeen730c2132008-09-13 15:23:29 -04002874 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002875
2876 return 0;
2877}
2878
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002879static inline int ext4_issue_discard(struct super_block *sb,
Daeho Jeonga0154342017-06-22 23:54:33 -04002880 ext4_group_t block_group, ext4_grpblk_t cluster, int count,
2881 struct bio **biop)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002882{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002883 ext4_fsblk_t discard_block;
2884
Theodore Ts'o84130192011-09-09 18:50:51 -04002885 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2886 ext4_group_first_block_no(sb, block_group));
2887 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002888 trace_ext4_discard_blocks(sb,
2889 (unsigned long long) discard_block, count);
Daeho Jeonga0154342017-06-22 23:54:33 -04002890 if (biop) {
2891 return __blkdev_issue_discard(sb->s_bdev,
2892 (sector_t)discard_block << (sb->s_blocksize_bits - 9),
2893 (sector_t)count << (sb->s_blocksize_bits - 9),
2894 GFP_NOFS, 0, biop);
2895 } else
2896 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002897}
2898
Daeho Jeonga0154342017-06-22 23:54:33 -04002899static void ext4_free_data_in_buddy(struct super_block *sb,
2900 struct ext4_free_data *entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05002901{
Alex Tomasc9de5602008-01-29 00:19:52 -05002902 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002903 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002904 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002905
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302906 mb_debug(sb, "gonna free %u blocks in group %u (0x%p):",
Bobi Jam18aadd42012-02-20 17:53:02 -05002907 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002908
Bobi Jam18aadd42012-02-20 17:53:02 -05002909 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2910 /* we expect to find existing buddy because it's pinned */
2911 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002912
Theodore Ts'od08854f2016-06-26 18:24:01 -04002913 spin_lock(&EXT4_SB(sb)->s_md_lock);
2914 EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
2915 spin_unlock(&EXT4_SB(sb)->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002916
Bobi Jam18aadd42012-02-20 17:53:02 -05002917 db = e4b.bd_info;
2918 /* there are blocks to put in buddy to make them really free */
2919 count += entry->efd_count;
2920 count2++;
2921 ext4_lock_group(sb, entry->efd_group);
2922 /* Take it out of per group rb tree */
2923 rb_erase(&entry->efd_node, &(db->bb_free_root));
2924 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002925
Bobi Jam18aadd42012-02-20 17:53:02 -05002926 /*
2927 * Clear the trimmed flag for the group so that the next
2928 * ext4_trim_fs can trim it.
2929 * If the volume is mounted with -o discard, online discard
2930 * is supported and the free blocks will be trimmed online.
2931 */
2932 if (!test_opt(sb, DISCARD))
2933 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2934
2935 if (!db->bb_free_root.rb_node) {
2936 /* No more items in the per group rb tree
2937 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002938 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002939 put_page(e4b.bd_buddy_page);
2940 put_page(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002941 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002942 ext4_unlock_group(sb, entry->efd_group);
2943 kmem_cache_free(ext4_free_data_cachep, entry);
2944 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002945
Ritesh Harjanid3df1452020-05-10 11:54:54 +05302946 mb_debug(sb, "freed %d blocks in %d structures\n", count,
2947 count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002948}
2949
Daeho Jeonga0154342017-06-22 23:54:33 -04002950/*
2951 * This function is called by the jbd2 layer once the commit has finished,
2952 * so we know we can free the blocks that were released with that commit.
2953 */
2954void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid)
2955{
2956 struct ext4_sb_info *sbi = EXT4_SB(sb);
2957 struct ext4_free_data *entry, *tmp;
2958 struct bio *discard_bio = NULL;
2959 struct list_head freed_data_list;
2960 struct list_head *cut_pos = NULL;
2961 int err;
2962
2963 INIT_LIST_HEAD(&freed_data_list);
2964
2965 spin_lock(&sbi->s_md_lock);
2966 list_for_each_entry(entry, &sbi->s_freed_data_list, efd_list) {
2967 if (entry->efd_tid != commit_tid)
2968 break;
2969 cut_pos = &entry->efd_list;
2970 }
2971 if (cut_pos)
2972 list_cut_position(&freed_data_list, &sbi->s_freed_data_list,
2973 cut_pos);
2974 spin_unlock(&sbi->s_md_lock);
2975
2976 if (test_opt(sb, DISCARD)) {
2977 list_for_each_entry(entry, &freed_data_list, efd_list) {
2978 err = ext4_issue_discard(sb, entry->efd_group,
2979 entry->efd_start_cluster,
2980 entry->efd_count,
2981 &discard_bio);
2982 if (err && err != -EOPNOTSUPP) {
2983 ext4_msg(sb, KERN_WARNING, "discard request in"
2984 " group:%d block:%d count:%d failed"
2985 " with %d", entry->efd_group,
2986 entry->efd_start_cluster,
2987 entry->efd_count, err);
2988 } else if (err == -EOPNOTSUPP)
2989 break;
2990 }
2991
Daeho Jeonge4510572017-08-05 13:11:57 -04002992 if (discard_bio) {
Daeho Jeonga0154342017-06-22 23:54:33 -04002993 submit_bio_wait(discard_bio);
Daeho Jeonge4510572017-08-05 13:11:57 -04002994 bio_put(discard_bio);
2995 }
Daeho Jeonga0154342017-06-22 23:54:33 -04002996 }
2997
2998 list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list)
2999 ext4_free_data_in_buddy(sb, entry);
3000}
3001
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04003002int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05003003{
Theodore Ts'o16828082010-10-27 21:30:09 -04003004 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
3005 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05003006 if (ext4_pspace_cachep == NULL)
Ritesh Harjanif2835292020-05-10 11:54:46 +05303007 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05003008
Theodore Ts'o16828082010-10-27 21:30:09 -04003009 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
3010 SLAB_RECLAIM_ACCOUNT);
Ritesh Harjanif2835292020-05-10 11:54:46 +05303011 if (ext4_ac_cachep == NULL)
3012 goto out_pa_free;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04003013
Bobi Jam18aadd42012-02-20 17:53:02 -05003014 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
3015 SLAB_RECLAIM_ACCOUNT);
Ritesh Harjanif2835292020-05-10 11:54:46 +05303016 if (ext4_free_data_cachep == NULL)
3017 goto out_ac_free;
3018
Alex Tomasc9de5602008-01-29 00:19:52 -05003019 return 0;
Ritesh Harjanif2835292020-05-10 11:54:46 +05303020
3021out_ac_free:
3022 kmem_cache_destroy(ext4_ac_cachep);
3023out_pa_free:
3024 kmem_cache_destroy(ext4_pspace_cachep);
3025out:
3026 return -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -05003027}
3028
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04003029void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05003030{
Theodore Ts'o60e66792010-05-17 07:00:00 -04003031 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04003032 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
3033 * before destroying the slab cache.
3034 */
3035 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05003036 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003037 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05003038 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05003039 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05003040}
3041
3042
3043/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02003044 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05003045 * Returns 0 if success or error code
3046 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003047static noinline_for_stack int
3048ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003049 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05003050{
3051 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05003052 struct ext4_group_desc *gdp;
3053 struct buffer_head *gdp_bh;
3054 struct ext4_sb_info *sbi;
3055 struct super_block *sb;
3056 ext4_fsblk_t block;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003057 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003058
3059 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3060 BUG_ON(ac->ac_b_ex.fe_len <= 0);
3061
3062 sb = ac->ac_sb;
3063 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003064
Theodore Ts'o574ca172008-07-11 19:27:31 -04003065 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04003066 if (IS_ERR(bitmap_bh)) {
3067 err = PTR_ERR(bitmap_bh);
3068 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05003069 goto out_err;
Darrick J. Wong9008a582015-10-17 21:33:24 -04003070 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003071
liang xie5d601252014-05-12 22:06:43 -04003072 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05003073 err = ext4_journal_get_write_access(handle, bitmap_bh);
3074 if (err)
3075 goto out_err;
3076
3077 err = -EIO;
3078 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
3079 if (!gdp)
3080 goto out_err;
3081
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003082 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003083 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003084
liang xie5d601252014-05-12 22:06:43 -04003085 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05003086 err = ext4_journal_get_write_access(handle, gdp_bh);
3087 if (err)
3088 goto out_err;
3089
Akinobu Mitabda00de2010-03-03 23:53:25 -05003090 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05003091
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003092 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003093 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003094 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04003095 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003096 /* File system mounted not to panic on error
Vegard Nossum554a5cc2016-07-14 23:02:47 -04003097 * Fix the bitmap and return EFSCORRUPTED
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003098 * We leak some of the blocks here.
3099 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003100 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003101 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3102 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003103 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05003104 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003105 if (!err)
Vegard Nossum554a5cc2016-07-14 23:02:47 -04003106 err = -EFSCORRUPTED;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04003107 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003108 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003109
3110 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003111#ifdef AGGRESSIVE_CHECK
3112 {
3113 int i;
3114 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3115 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3116 bitmap_bh->b_data));
3117 }
3118 }
3119#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003120 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3121 ac->ac_b_ex.fe_len);
Theodore Ts'o88446182018-06-14 00:58:00 -04003122 if (ext4_has_group_desc_csum(sb) &&
3123 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003124 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003125 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04003126 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003127 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05003128 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003129 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
3130 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04003131 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003132 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003133
3134 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04003135 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003136 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003137 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003138 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003139 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3140 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04003141 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
3142 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05003143
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003144 if (sbi->s_log_groups_per_flex) {
3145 ext4_group_t flex_group = ext4_flex_group(sbi,
3146 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04003147 atomic64_sub(ac->ac_b_ex.fe_len,
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08003148 &sbi_array_rcu_deref(sbi, s_flex_groups,
3149 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003150 }
3151
Frank Mayhar03901312009-01-07 00:06:22 -05003152 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003153 if (err)
3154 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003155 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003156
3157out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003158 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003159 return err;
3160}
3161
3162/*
3163 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003164 * Group request are normalized to s_mb_group_prealloc, which goes to
3165 * s_strip if we set the same via mount option.
3166 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003167 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003168 *
3169 * XXX: should we try to preallocate more than the group has now?
3170 */
3171static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3172{
3173 struct super_block *sb = ac->ac_sb;
3174 struct ext4_locality_group *lg = ac->ac_lg;
3175
3176 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003177 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303178 mb_debug(sb, "goal %u blocks for locality group\n", ac->ac_g_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003179}
3180
3181/*
3182 * Normalization means making request better in terms of
3183 * size and alignment
3184 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003185static noinline_for_stack void
3186ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003187 struct ext4_allocation_request *ar)
3188{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003189 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003190 int bsbits, max;
3191 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003192 loff_t size, start_off;
3193 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003194 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003195 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003196 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003197
3198 /* do normalize only data requests, metadata requests
3199 do not need preallocation */
3200 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3201 return;
3202
3203 /* sometime caller may want exact blocks */
3204 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3205 return;
3206
3207 /* caller may indicate that preallocation isn't
3208 * required (it's a tail, for example) */
3209 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3210 return;
3211
3212 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3213 ext4_mb_normalize_group_request(ac);
3214 return ;
3215 }
3216
3217 bsbits = ac->ac_sb->s_blocksize_bits;
3218
3219 /* first, let's learn actual file size
3220 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003221 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003222 size = size << bsbits;
3223 if (size < i_size_read(ac->ac_inode))
3224 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003225 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003226
Valerie Clement19304792008-05-13 19:31:14 -04003227 /* max size of free chunks */
3228 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003229
Valerie Clement19304792008-05-13 19:31:14 -04003230#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3231 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003232
3233 /* first, try to predict filesize */
3234 /* XXX: should this table be tunable? */
3235 start_off = 0;
3236 if (size <= 16 * 1024) {
3237 size = 16 * 1024;
3238 } else if (size <= 32 * 1024) {
3239 size = 32 * 1024;
3240 } else if (size <= 64 * 1024) {
3241 size = 64 * 1024;
3242 } else if (size <= 128 * 1024) {
3243 size = 128 * 1024;
3244 } else if (size <= 256 * 1024) {
3245 size = 256 * 1024;
3246 } else if (size <= 512 * 1024) {
3247 size = 512 * 1024;
3248 } else if (size <= 1024 * 1024) {
3249 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003250 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003251 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003252 (21 - bsbits)) << 21;
3253 size = 2 * 1024 * 1024;
3254 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003255 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3256 (22 - bsbits)) << 22;
3257 size = 4 * 1024 * 1024;
3258 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003259 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003260 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3261 (23 - bsbits)) << 23;
3262 size = 8 * 1024 * 1024;
3263 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003264 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3265 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3266 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003267 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003268 size = size >> bsbits;
3269 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003270
3271 /* don't cover already allocated blocks in selected range */
3272 if (ar->pleft && start <= ar->lleft) {
3273 size -= ar->lleft + 1 - start;
3274 start = ar->lleft + 1;
3275 }
3276 if (ar->pright && start + size - 1 >= ar->lright)
3277 size -= start + size - ar->lright;
3278
Jan Karacd648b82017-01-27 14:34:30 -05003279 /*
3280 * Trim allocation request for filesystems with artificially small
3281 * groups.
3282 */
3283 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
3284 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
3285
Alex Tomasc9de5602008-01-29 00:19:52 -05003286 end = start + size;
3287
3288 /* check we don't cross already preallocated blocks */
3289 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003290 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003291 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003292
Alex Tomasc9de5602008-01-29 00:19:52 -05003293 if (pa->pa_deleted)
3294 continue;
3295 spin_lock(&pa->pa_lock);
3296 if (pa->pa_deleted) {
3297 spin_unlock(&pa->pa_lock);
3298 continue;
3299 }
3300
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003301 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3302 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003303
3304 /* PA must not overlap original request */
3305 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3306 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3307
Eric Sandeen38877f42009-08-17 23:55:24 -04003308 /* skip PAs this normalized request doesn't overlap with */
3309 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003310 spin_unlock(&pa->pa_lock);
3311 continue;
3312 }
3313 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3314
Eric Sandeen38877f42009-08-17 23:55:24 -04003315 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003316 if (pa_end <= ac->ac_o_ex.fe_logical) {
3317 BUG_ON(pa_end < start);
3318 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003319 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003320 BUG_ON(pa->pa_lstart > end);
3321 end = pa->pa_lstart;
3322 }
3323 spin_unlock(&pa->pa_lock);
3324 }
3325 rcu_read_unlock();
3326 size = end - start;
3327
3328 /* XXX: extra loop to check we really don't overlap preallocations */
3329 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003330 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003331 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003332
Alex Tomasc9de5602008-01-29 00:19:52 -05003333 spin_lock(&pa->pa_lock);
3334 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003335 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3336 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003337 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3338 }
3339 spin_unlock(&pa->pa_lock);
3340 }
3341 rcu_read_unlock();
3342
3343 if (start + size <= ac->ac_o_ex.fe_logical &&
3344 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003345 ext4_msg(ac->ac_sb, KERN_ERR,
3346 "start %lu, size %lu, fe_logical %lu",
3347 (unsigned long) start, (unsigned long) size,
3348 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003349 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003350 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003351 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003352
3353 /* now prepare goal request */
3354
3355 /* XXX: is it better to align blocks WRT to logical
3356 * placement or satisfy big request as is */
3357 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003358 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003359
3360 /* define goal start in order to merge */
3361 if (ar->pright && (ar->lright == (start + size))) {
3362 /* merge to the right */
3363 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3364 &ac->ac_f_ex.fe_group,
3365 &ac->ac_f_ex.fe_start);
3366 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3367 }
3368 if (ar->pleft && (ar->lleft + 1 == start)) {
3369 /* merge to the left */
3370 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3371 &ac->ac_f_ex.fe_group,
3372 &ac->ac_f_ex.fe_start);
3373 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3374 }
3375
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303376 mb_debug(ac->ac_sb, "goal: %lld(was %lld) blocks at %u\n", size,
3377 orig_size, start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003378}
3379
3380static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3381{
3382 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3383
3384 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3385 atomic_inc(&sbi->s_bal_reqs);
3386 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003387 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003388 atomic_inc(&sbi->s_bal_success);
3389 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3390 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3391 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3392 atomic_inc(&sbi->s_bal_goals);
3393 if (ac->ac_found > sbi->s_mb_max_to_scan)
3394 atomic_inc(&sbi->s_bal_breaks);
3395 }
3396
Theodore Ts'o296c3552009-09-30 00:32:42 -04003397 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3398 trace_ext4_mballoc_alloc(ac);
3399 else
3400 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003401}
3402
3403/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003404 * Called on failure; free up any blocks from the inode PA for this
3405 * context. We don't need this for MB_GROUP_PA because we only change
3406 * pa_free in ext4_mb_release_context(), but on failure, we've already
3407 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3408 */
3409static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3410{
3411 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003412 struct ext4_buddy e4b;
3413 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003414
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003415 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003416 if (ac->ac_f_ex.fe_len == 0)
3417 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003418 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3419 if (err) {
3420 /*
3421 * This should never happen since we pin the
3422 * pages in the ext4_allocation_context so
3423 * ext4_mb_load_buddy() should never fail.
3424 */
3425 WARN(1, "mb_load_buddy failed (%d)", err);
3426 return;
3427 }
3428 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3429 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3430 ac->ac_f_ex.fe_len);
3431 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003432 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003433 return;
3434 }
3435 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003436 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003437}
3438
3439/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003440 * use blocks preallocated to inode
3441 */
3442static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3443 struct ext4_prealloc_space *pa)
3444{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003445 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003446 ext4_fsblk_t start;
3447 ext4_fsblk_t end;
3448 int len;
3449
3450 /* found preallocated blocks, use them */
3451 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003452 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3453 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3454 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003455 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3456 &ac->ac_b_ex.fe_start);
3457 ac->ac_b_ex.fe_len = len;
3458 ac->ac_status = AC_STATUS_FOUND;
3459 ac->ac_pa = pa;
3460
3461 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003462 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003463 BUG_ON(pa->pa_free < len);
3464 pa->pa_free -= len;
3465
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303466 mb_debug(ac->ac_sb, "use %llu/%d from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003467}
3468
3469/*
3470 * use blocks preallocated to locality group
3471 */
3472static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3473 struct ext4_prealloc_space *pa)
3474{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003475 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003476
Alex Tomasc9de5602008-01-29 00:19:52 -05003477 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3478 &ac->ac_b_ex.fe_group,
3479 &ac->ac_b_ex.fe_start);
3480 ac->ac_b_ex.fe_len = len;
3481 ac->ac_status = AC_STATUS_FOUND;
3482 ac->ac_pa = pa;
3483
3484 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003485 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003486 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003487 * in on-disk bitmap -- see ext4_mb_release_context()
3488 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003489 */
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303490 mb_debug(ac->ac_sb, "use %u/%u from group pa %p\n",
3491 pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003492}
3493
3494/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003495 * Return the prealloc space that have minimal distance
3496 * from the goal block. @cpa is the prealloc
3497 * space that is having currently known minimal distance
3498 * from the goal block.
3499 */
3500static struct ext4_prealloc_space *
3501ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3502 struct ext4_prealloc_space *pa,
3503 struct ext4_prealloc_space *cpa)
3504{
3505 ext4_fsblk_t cur_distance, new_distance;
3506
3507 if (cpa == NULL) {
3508 atomic_inc(&pa->pa_count);
3509 return pa;
3510 }
Andrew Morton79211c82015-11-09 14:58:13 -08003511 cur_distance = abs(goal_block - cpa->pa_pstart);
3512 new_distance = abs(goal_block - pa->pa_pstart);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003513
Coly Li5a54b2f2011-02-24 14:10:05 -05003514 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003515 return cpa;
3516
3517 /* drop the previous reference */
3518 atomic_dec(&cpa->pa_count);
3519 atomic_inc(&pa->pa_count);
3520 return pa;
3521}
3522
3523/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003524 * search goal blocks in preallocated space
3525 */
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303526static noinline_for_stack bool
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003527ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003528{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003529 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003530 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003531 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3532 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003533 struct ext4_prealloc_space *pa, *cpa = NULL;
3534 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003535
3536 /* only data can be preallocated */
3537 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303538 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05003539
3540 /* first, try per-file preallocation */
3541 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003542 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003543
3544 /* all fields in this condition don't change,
3545 * so we can skip locking for them */
3546 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003547 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3548 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003549 continue;
3550
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003551 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003552 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003553 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3554 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003555 continue;
3556
Alex Tomasc9de5602008-01-29 00:19:52 -05003557 /* found preallocated blocks, use them */
3558 spin_lock(&pa->pa_lock);
3559 if (pa->pa_deleted == 0 && pa->pa_free) {
3560 atomic_inc(&pa->pa_count);
3561 ext4_mb_use_inode_pa(ac, pa);
3562 spin_unlock(&pa->pa_lock);
3563 ac->ac_criteria = 10;
3564 rcu_read_unlock();
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303565 return true;
Alex Tomasc9de5602008-01-29 00:19:52 -05003566 }
3567 spin_unlock(&pa->pa_lock);
3568 }
3569 rcu_read_unlock();
3570
3571 /* can we use group allocation? */
3572 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303573 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05003574
3575 /* inode may have no locality group for some reason */
3576 lg = ac->ac_lg;
3577 if (lg == NULL)
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303578 return false;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003579 order = fls(ac->ac_o_ex.fe_len) - 1;
3580 if (order > PREALLOC_TB_SIZE - 1)
3581 /* The max size of hash table is PREALLOC_TB_SIZE */
3582 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003583
Akinobu Mitabda00de2010-03-03 23:53:25 -05003584 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003585 /*
3586 * search for the prealloc space that is having
3587 * minimal distance from the goal block.
3588 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003589 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3590 rcu_read_lock();
3591 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3592 pa_inode_list) {
3593 spin_lock(&pa->pa_lock);
3594 if (pa->pa_deleted == 0 &&
3595 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003596
3597 cpa = ext4_mb_check_group_pa(goal_block,
3598 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003599 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003600 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003601 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003602 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003603 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003604 if (cpa) {
3605 ext4_mb_use_group_pa(ac, cpa);
3606 ac->ac_criteria = 20;
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303607 return true;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003608 }
Ritesh Harjani4fca8f02020-05-10 11:54:47 +05303609 return false;
Alex Tomasc9de5602008-01-29 00:19:52 -05003610}
3611
3612/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003613 * the function goes through all block freed in the group
3614 * but not yet committed and marks them used in in-core bitmap.
3615 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003616 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003617 */
3618static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3619 ext4_group_t group)
3620{
3621 struct rb_node *n;
3622 struct ext4_group_info *grp;
3623 struct ext4_free_data *entry;
3624
3625 grp = ext4_get_group_info(sb, group);
3626 n = rb_first(&(grp->bb_free_root));
3627
3628 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003629 entry = rb_entry(n, struct ext4_free_data, efd_node);
3630 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003631 n = rb_next(n);
3632 }
3633 return;
3634}
3635
3636/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003637 * the function goes through all preallocation in this group and marks them
3638 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003639 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003640 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003641static noinline_for_stack
3642void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003643 ext4_group_t group)
3644{
3645 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3646 struct ext4_prealloc_space *pa;
3647 struct list_head *cur;
3648 ext4_group_t groupnr;
3649 ext4_grpblk_t start;
3650 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003651 int len;
3652
3653 /* all form of preallocation discards first load group,
3654 * so the only competing code is preallocation use.
3655 * we don't need any locking here
3656 * notice we do NOT ignore preallocations with pa_deleted
3657 * otherwise we could leave used blocks available for
3658 * allocation in buddy when concurrent ext4_mb_put_pa()
3659 * is dropping preallocation
3660 */
3661 list_for_each(cur, &grp->bb_prealloc_list) {
3662 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3663 spin_lock(&pa->pa_lock);
3664 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3665 &groupnr, &start);
3666 len = pa->pa_len;
3667 spin_unlock(&pa->pa_lock);
3668 if (unlikely(len == 0))
3669 continue;
3670 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003671 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003672 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003673 }
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303674 mb_debug(sb, "preallocated %d for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003675}
3676
3677static void ext4_mb_pa_callback(struct rcu_head *head)
3678{
3679 struct ext4_prealloc_space *pa;
3680 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003681
3682 BUG_ON(atomic_read(&pa->pa_count));
3683 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003684 kmem_cache_free(ext4_pspace_cachep, pa);
3685}
3686
3687/*
3688 * drops a reference to preallocated space descriptor
3689 * if this was the last reference and the space is consumed
3690 */
3691static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3692 struct super_block *sb, struct ext4_prealloc_space *pa)
3693{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003694 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003695 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003696
Alex Tomasc9de5602008-01-29 00:19:52 -05003697 /* in this short window concurrent discard can set pa_deleted */
3698 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003699 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3700 spin_unlock(&pa->pa_lock);
3701 return;
3702 }
3703
Alex Tomasc9de5602008-01-29 00:19:52 -05003704 if (pa->pa_deleted == 1) {
3705 spin_unlock(&pa->pa_lock);
3706 return;
3707 }
3708
3709 pa->pa_deleted = 1;
3710 spin_unlock(&pa->pa_lock);
3711
Eric Sandeend33a1972009-03-16 23:25:40 -04003712 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003713 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003714 * If doing group-based preallocation, pa_pstart may be in the
3715 * next group when pa is used up
3716 */
3717 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003718 grp_blk--;
3719
Lukas Czernerbd862982013-04-03 23:32:34 -04003720 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003721
3722 /*
3723 * possible race:
3724 *
3725 * P1 (buddy init) P2 (regular allocation)
3726 * find block B in PA
3727 * copy on-disk bitmap to buddy
3728 * mark B in on-disk bitmap
3729 * drop PA from group
3730 * mark all PAs in buddy
3731 *
3732 * thus, P1 initializes buddy with B available. to prevent this
3733 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3734 * against that pair
3735 */
3736 ext4_lock_group(sb, grp);
3737 list_del(&pa->pa_group_list);
3738 ext4_unlock_group(sb, grp);
3739
3740 spin_lock(pa->pa_obj_lock);
3741 list_del_rcu(&pa->pa_inode_list);
3742 spin_unlock(pa->pa_obj_lock);
3743
3744 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3745}
3746
3747/*
3748 * creates new preallocated space for given inode
3749 */
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303750static noinline_for_stack void
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003751ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003752{
3753 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003754 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003755 struct ext4_prealloc_space *pa;
3756 struct ext4_group_info *grp;
3757 struct ext4_inode_info *ei;
3758
3759 /* preallocate only when found space is larger then requested */
3760 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3761 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3762 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303763 BUG_ON(ac->ac_pa == NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003764
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303765 pa = ac->ac_pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003766
3767 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3768 int winl;
3769 int wins;
3770 int win;
3771 int offs;
3772
3773 /* we can't allocate as much as normalizer wants.
3774 * so, found space must get proper lstart
3775 * to cover original request */
3776 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3777 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3778
3779 /* we're limited by original request in that
3780 * logical block must be covered any way
3781 * winl is window we can move our chunk within */
3782 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3783
3784 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003785 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003786
3787 /* the smallest one defines real window */
3788 win = min(winl, wins);
3789
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003790 offs = ac->ac_o_ex.fe_logical %
3791 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003792 if (offs && offs < win)
3793 win = offs;
3794
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003795 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003796 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003797 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3798 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3799 }
3800
3801 /* preallocation can change ac_b_ex, thus we store actually
3802 * allocated blocks for history */
3803 ac->ac_f_ex = ac->ac_b_ex;
3804
3805 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3806 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3807 pa->pa_len = ac->ac_b_ex.fe_len;
3808 pa->pa_free = pa->pa_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003809 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003810 INIT_LIST_HEAD(&pa->pa_inode_list);
3811 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003812 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003813 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003814
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303815 mb_debug(sb, "new inode pa %p: %llu/%d for %u\n", pa, pa->pa_pstart,
3816 pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003817 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003818
3819 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003820 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003821
3822 ei = EXT4_I(ac->ac_inode);
3823 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3824
3825 pa->pa_obj_lock = &ei->i_prealloc_lock;
3826 pa->pa_inode = ac->ac_inode;
3827
Alex Tomasc9de5602008-01-29 00:19:52 -05003828 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003829
3830 spin_lock(pa->pa_obj_lock);
3831 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3832 spin_unlock(pa->pa_obj_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003833}
3834
3835/*
3836 * creates new preallocated space for locality group inodes belongs to
3837 */
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303838static noinline_for_stack void
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003839ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003840{
3841 struct super_block *sb = ac->ac_sb;
3842 struct ext4_locality_group *lg;
3843 struct ext4_prealloc_space *pa;
3844 struct ext4_group_info *grp;
3845
3846 /* preallocate only when found space is larger then requested */
3847 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3848 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3849 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303850 BUG_ON(ac->ac_pa == NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003851
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303852 pa = ac->ac_pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003853
3854 /* preallocation can change ac_b_ex, thus we store actually
3855 * allocated blocks for history */
3856 ac->ac_f_ex = ac->ac_b_ex;
3857
3858 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3859 pa->pa_lstart = pa->pa_pstart;
3860 pa->pa_len = ac->ac_b_ex.fe_len;
3861 pa->pa_free = pa->pa_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003862 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003863 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003864 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003865 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003866 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003867
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303868 mb_debug(sb, "new group pa %p: %llu/%d for %u\n", pa, pa->pa_pstart,
3869 pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003870 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003871
3872 ext4_mb_use_group_pa(ac, pa);
3873 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3874
3875 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3876 lg = ac->ac_lg;
3877 BUG_ON(lg == NULL);
3878
3879 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3880 pa->pa_inode = NULL;
3881
Alex Tomasc9de5602008-01-29 00:19:52 -05003882 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003883
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003884 /*
3885 * We will later add the new pa to the right bucket
3886 * after updating the pa_free in ext4_mb_release_context
3887 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003888}
3889
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303890static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003891{
Alex Tomasc9de5602008-01-29 00:19:52 -05003892 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303893 ext4_mb_new_group_pa(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003894 else
Ritesh Harjani53f86b12020-05-20 12:10:32 +05303895 ext4_mb_new_inode_pa(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003896}
3897
3898/*
3899 * finds all unused blocks in on-disk bitmap, frees them in
3900 * in-core bitmap and buddy.
3901 * @pa must be unlinked from inode and group lists, so that
3902 * nobody else can find/use it.
3903 * the caller MUST hold group/inode locks.
3904 * TODO: optimize the case when there are no in-core structures yet
3905 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003906static noinline_for_stack int
3907ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003908 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003909{
Alex Tomasc9de5602008-01-29 00:19:52 -05003910 struct super_block *sb = e4b->bd_sb;
3911 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003912 unsigned int end;
3913 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003914 ext4_group_t group;
3915 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003916 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003917 int free = 0;
3918
3919 BUG_ON(pa->pa_deleted == 0);
3920 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003921 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003922 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3923 end = bit + pa->pa_len;
3924
Alex Tomasc9de5602008-01-29 00:19:52 -05003925 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003926 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003927 if (bit >= end)
3928 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003929 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05303930 mb_debug(sb, "free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003931 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3932 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003933 free += next - bit;
3934
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003935 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003936 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3937 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003938 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003939 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3940 bit = next + 1;
3941 }
3942 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003943 ext4_msg(e4b->bd_sb, KERN_CRIT,
Ritesh Harjani36bad422020-05-10 11:54:44 +05303944 "pa %p: logic %lu, phys. %lu, len %d",
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003945 pa, (unsigned long) pa->pa_lstart,
3946 (unsigned long) pa->pa_pstart,
Ritesh Harjani36bad422020-05-10 11:54:44 +05303947 pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003948 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003949 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003950 /*
3951 * pa is already deleted so we use the value obtained
3952 * from the bitmap and continue.
3953 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003954 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003955 atomic_add(free, &sbi->s_mb_discarded);
3956
zhong jiang863c37f2018-08-04 17:34:07 -04003957 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003958}
3959
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003960static noinline_for_stack int
3961ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003962 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003963{
Alex Tomasc9de5602008-01-29 00:19:52 -05003964 struct super_block *sb = e4b->bd_sb;
3965 ext4_group_t group;
3966 ext4_grpblk_t bit;
3967
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003968 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003969 BUG_ON(pa->pa_deleted == 0);
3970 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3971 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3972 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3973 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003974 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003975
3976 return 0;
3977}
3978
3979/*
3980 * releases all preallocations in given group
3981 *
3982 * first, we need to decide discard policy:
3983 * - when do we discard
3984 * 1) ENOSPC
3985 * - how many do we discard
3986 * 1) how many requested
3987 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003988static noinline_for_stack int
3989ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003990 ext4_group_t group, int needed)
3991{
3992 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3993 struct buffer_head *bitmap_bh = NULL;
3994 struct ext4_prealloc_space *pa, *tmp;
3995 struct list_head list;
3996 struct ext4_buddy e4b;
3997 int err;
3998 int busy = 0;
3999 int free = 0;
4000
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304001 mb_debug(sb, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004002 if (list_empty(&grp->bb_prealloc_list))
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304003 goto out_dbg;
Alex Tomasc9de5602008-01-29 00:19:52 -05004004
Theodore Ts'o574ca172008-07-11 19:27:31 -04004005 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004006 if (IS_ERR(bitmap_bh)) {
4007 err = PTR_ERR(bitmap_bh);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004008 ext4_error_err(sb, -err,
4009 "Error %d reading block bitmap for %u",
4010 err, group);
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304011 goto out_dbg;
Alex Tomasc9de5602008-01-29 00:19:52 -05004012 }
4013
4014 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004015 if (err) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004016 ext4_warning(sb, "Error %d loading buddy information for %u",
4017 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004018 put_bh(bitmap_bh);
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304019 goto out_dbg;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004020 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004021
4022 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04004023 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05004024
Alex Tomasc9de5602008-01-29 00:19:52 -05004025 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05004026repeat:
4027 ext4_lock_group(sb, group);
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304028 this_cpu_inc(discard_pa_seq);
Alex Tomasc9de5602008-01-29 00:19:52 -05004029 list_for_each_entry_safe(pa, tmp,
4030 &grp->bb_prealloc_list, pa_group_list) {
4031 spin_lock(&pa->pa_lock);
4032 if (atomic_read(&pa->pa_count)) {
4033 spin_unlock(&pa->pa_lock);
4034 busy = 1;
4035 continue;
4036 }
4037 if (pa->pa_deleted) {
4038 spin_unlock(&pa->pa_lock);
4039 continue;
4040 }
4041
4042 /* seems this one can be freed ... */
4043 pa->pa_deleted = 1;
4044
4045 /* we can trust pa_free ... */
4046 free += pa->pa_free;
4047
4048 spin_unlock(&pa->pa_lock);
4049
4050 list_del(&pa->pa_group_list);
4051 list_add(&pa->u.pa_tmp_list, &list);
4052 }
4053
4054 /* if we still need more blocks and some PAs were used, try again */
4055 if (free < needed && busy) {
4056 busy = 0;
4057 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004058 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05004059 goto repeat;
4060 }
4061
4062 /* found anything to free? */
4063 if (list_empty(&list)) {
4064 BUG_ON(free != 0);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304065 mb_debug(sb, "Someone else may have freed PA for this group %u\n",
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304066 group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004067 goto out;
4068 }
4069
4070 /* now free all selected PAs */
4071 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4072
4073 /* remove from object (inode or locality group) */
4074 spin_lock(pa->pa_obj_lock);
4075 list_del_rcu(&pa->pa_inode_list);
4076 spin_unlock(pa->pa_obj_lock);
4077
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004078 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004079 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004080 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004081 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004082
4083 list_del(&pa->u.pa_tmp_list);
4084 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4085 }
4086
4087out:
4088 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004089 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004090 put_bh(bitmap_bh);
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304091out_dbg:
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304092 mb_debug(sb, "discarded (%d) blocks preallocated for group %u bb_free (%d)\n",
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304093 free, group, grp->bb_free);
Alex Tomasc9de5602008-01-29 00:19:52 -05004094 return free;
4095}
4096
4097/*
4098 * releases all non-used preallocated blocks for given inode
4099 *
4100 * It's important to discard preallocations under i_data_sem
4101 * We don't want another block to be served from the prealloc
4102 * space when we are discarding the inode prealloc space.
4103 *
4104 * FIXME!! Make sure it is valid at all the call sites
4105 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004106void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05004107{
4108 struct ext4_inode_info *ei = EXT4_I(inode);
4109 struct super_block *sb = inode->i_sb;
4110 struct buffer_head *bitmap_bh = NULL;
4111 struct ext4_prealloc_space *pa, *tmp;
4112 ext4_group_t group = 0;
4113 struct list_head list;
4114 struct ext4_buddy e4b;
4115 int err;
4116
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004117 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004118 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4119 return;
4120 }
4121
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304122 mb_debug(sb, "discard preallocation for inode %lu\n",
4123 inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004124 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004125
4126 INIT_LIST_HEAD(&list);
4127
4128repeat:
4129 /* first, collect all pa's in the inode */
4130 spin_lock(&ei->i_prealloc_lock);
4131 while (!list_empty(&ei->i_prealloc_list)) {
4132 pa = list_entry(ei->i_prealloc_list.next,
4133 struct ext4_prealloc_space, pa_inode_list);
4134 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4135 spin_lock(&pa->pa_lock);
4136 if (atomic_read(&pa->pa_count)) {
4137 /* this shouldn't happen often - nobody should
4138 * use preallocation while we're discarding it */
4139 spin_unlock(&pa->pa_lock);
4140 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004141 ext4_msg(sb, KERN_ERR,
4142 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05004143 WARN_ON(1);
4144 schedule_timeout_uninterruptible(HZ);
4145 goto repeat;
4146
4147 }
4148 if (pa->pa_deleted == 0) {
4149 pa->pa_deleted = 1;
4150 spin_unlock(&pa->pa_lock);
4151 list_del_rcu(&pa->pa_inode_list);
4152 list_add(&pa->u.pa_tmp_list, &list);
4153 continue;
4154 }
4155
4156 /* someone is deleting pa right now */
4157 spin_unlock(&pa->pa_lock);
4158 spin_unlock(&ei->i_prealloc_lock);
4159
4160 /* we have to wait here because pa_deleted
4161 * doesn't mean pa is already unlinked from
4162 * the list. as we might be called from
4163 * ->clear_inode() the inode will get freed
4164 * and concurrent thread which is unlinking
4165 * pa from inode's list may access already
4166 * freed memory, bad-bad-bad */
4167
4168 /* XXX: if this happens too often, we can
4169 * add a flag to force wait only in case
4170 * of ->clear_inode(), but not in case of
4171 * regular truncate */
4172 schedule_timeout_uninterruptible(HZ);
4173 goto repeat;
4174 }
4175 spin_unlock(&ei->i_prealloc_lock);
4176
4177 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004178 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04004179 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05004180
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004181 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4182 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004183 if (err) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004184 ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
4185 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004186 continue;
4187 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004188
Theodore Ts'o574ca172008-07-11 19:27:31 -04004189 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004190 if (IS_ERR(bitmap_bh)) {
4191 err = PTR_ERR(bitmap_bh);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004192 ext4_error_err(sb, -err, "Error %d reading block bitmap for %u",
4193 err, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004194 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004195 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004196 }
4197
4198 ext4_lock_group(sb, group);
4199 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004200 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004201 ext4_unlock_group(sb, group);
4202
Jing Zhange39e07f2010-05-14 00:00:00 -04004203 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004204 put_bh(bitmap_bh);
4205
4206 list_del(&pa->u.pa_tmp_list);
4207 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4208 }
4209}
4210
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304211static int ext4_mb_pa_alloc(struct ext4_allocation_context *ac)
4212{
4213 struct ext4_prealloc_space *pa;
4214
4215 BUG_ON(ext4_pspace_cachep == NULL);
4216 pa = kmem_cache_zalloc(ext4_pspace_cachep, GFP_NOFS);
4217 if (!pa)
4218 return -ENOMEM;
4219 atomic_set(&pa->pa_count, 1);
4220 ac->ac_pa = pa;
4221 return 0;
4222}
4223
4224static void ext4_mb_pa_free(struct ext4_allocation_context *ac)
4225{
4226 struct ext4_prealloc_space *pa = ac->ac_pa;
4227
4228 BUG_ON(!pa);
4229 ac->ac_pa = NULL;
4230 WARN_ON(!atomic_dec_and_test(&pa->pa_count));
4231 kmem_cache_free(ext4_pspace_cachep, pa);
4232}
4233
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004234#ifdef CONFIG_EXT4_DEBUG
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304235static inline void ext4_mb_show_pa(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05004236{
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304237 ext4_group_t i, ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -05004238
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304239 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
Eric Sandeene3570632010-07-27 11:56:08 -04004240 return;
4241
Theodore Ts'o8df96752009-05-01 08:50:38 -04004242 ngroups = ext4_get_groups_count(sb);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304243 mb_debug(sb, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004244 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004245 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4246 struct ext4_prealloc_space *pa;
4247 ext4_grpblk_t start;
4248 struct list_head *cur;
4249 ext4_lock_group(sb, i);
4250 list_for_each(cur, &grp->bb_prealloc_list) {
4251 pa = list_entry(cur, struct ext4_prealloc_space,
4252 pa_group_list);
4253 spin_lock(&pa->pa_lock);
4254 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4255 NULL, &start);
4256 spin_unlock(&pa->pa_lock);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304257 mb_debug(sb, "PA:%u:%d:%d\n", i, start,
4258 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004259 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004260 ext4_unlock_group(sb, i);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304261 mb_debug(sb, "%u: %d/%d\n", i, grp->bb_free,
4262 grp->bb_fragments);
Alex Tomasc9de5602008-01-29 00:19:52 -05004263 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004264}
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304265
4266static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4267{
4268 struct super_block *sb = ac->ac_sb;
4269
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304270 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304271 return;
4272
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304273 mb_debug(sb, "Can't allocate:"
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304274 " Allocation context details:");
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304275 mb_debug(sb, "status %u flags 0x%x",
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304276 ac->ac_status, ac->ac_flags);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304277 mb_debug(sb, "orig %lu/%lu/%lu@%lu, "
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304278 "goal %lu/%lu/%lu@%lu, "
4279 "best %lu/%lu/%lu@%lu cr %d",
4280 (unsigned long)ac->ac_o_ex.fe_group,
4281 (unsigned long)ac->ac_o_ex.fe_start,
4282 (unsigned long)ac->ac_o_ex.fe_len,
4283 (unsigned long)ac->ac_o_ex.fe_logical,
4284 (unsigned long)ac->ac_g_ex.fe_group,
4285 (unsigned long)ac->ac_g_ex.fe_start,
4286 (unsigned long)ac->ac_g_ex.fe_len,
4287 (unsigned long)ac->ac_g_ex.fe_logical,
4288 (unsigned long)ac->ac_b_ex.fe_group,
4289 (unsigned long)ac->ac_b_ex.fe_start,
4290 (unsigned long)ac->ac_b_ex.fe_len,
4291 (unsigned long)ac->ac_b_ex.fe_logical,
4292 (int)ac->ac_criteria);
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304293 mb_debug(sb, "%u found", ac->ac_found);
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304294 ext4_mb_show_pa(sb);
4295}
Alex Tomasc9de5602008-01-29 00:19:52 -05004296#else
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304297static inline void ext4_mb_show_pa(struct super_block *sb)
4298{
4299 return;
4300}
Alex Tomasc9de5602008-01-29 00:19:52 -05004301static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4302{
Ritesh Harjanie68cf402020-05-10 11:54:42 +05304303 ext4_mb_show_pa(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004304 return;
4305}
4306#endif
4307
4308/*
4309 * We use locality group preallocation for small size file. The size of the
4310 * file is determined by the current size or the resulting size after
4311 * allocation which ever is larger
4312 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004313 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004314 */
4315static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4316{
4317 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4318 int bsbits = ac->ac_sb->s_blocksize_bits;
4319 loff_t size, isize;
4320
4321 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4322 return;
4323
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004324 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4325 return;
4326
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004327 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004328 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4329 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004330
Nikolay Borisov82dd1242019-02-10 23:04:16 -05004331 if ((size == isize) && !ext4_fs_is_busy(sbi) &&
4332 !inode_is_open_for_write(ac->ac_inode)) {
Theodore Ts'o50797482009-09-18 13:34:02 -04004333 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4334 return;
4335 }
4336
Robin Dongebbe0272011-10-26 05:14:27 -04004337 if (sbi->s_mb_group_prealloc <= 0) {
4338 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4339 return;
4340 }
4341
Alex Tomasc9de5602008-01-29 00:19:52 -05004342 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004343 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004344 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004345 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004346 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004347 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004348
4349 BUG_ON(ac->ac_lg != NULL);
4350 /*
4351 * locality group prealloc space are per cpu. The reason for having
4352 * per cpu locality group is to reduce the contention between block
4353 * request from multiple CPUs.
4354 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004355 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004356
4357 /* we're going to use group allocation */
4358 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4359
4360 /* serialize all allocations in the group */
4361 mutex_lock(&ac->ac_lg->lg_mutex);
4362}
4363
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004364static noinline_for_stack int
4365ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004366 struct ext4_allocation_request *ar)
4367{
4368 struct super_block *sb = ar->inode->i_sb;
4369 struct ext4_sb_info *sbi = EXT4_SB(sb);
4370 struct ext4_super_block *es = sbi->s_es;
4371 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004372 unsigned int len;
4373 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004374 ext4_grpblk_t block;
4375
4376 /* we can't allocate > group size */
4377 len = ar->len;
4378
4379 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004380 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4381 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004382
4383 /* start searching from the goal */
4384 goal = ar->goal;
4385 if (goal < le32_to_cpu(es->s_first_data_block) ||
4386 goal >= ext4_blocks_count(es))
4387 goal = le32_to_cpu(es->s_first_data_block);
4388 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4389
4390 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004391 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004392 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004393 ac->ac_sb = sb;
4394 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004395 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004396 ac->ac_o_ex.fe_group = group;
4397 ac->ac_o_ex.fe_start = block;
4398 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004399 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004400 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004401
4402 /* we have to define context: we'll we work with a file or
4403 * locality group. this is a policy, actually */
4404 ext4_mb_group_or_file(ac);
4405
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304406 mb_debug(sb, "init ac: %u blocks @ %u, goal %u, flags 0x%x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004407 "left: %u/%u, right %u/%u to %swritable\n",
4408 (unsigned) ar->len, (unsigned) ar->logical,
4409 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4410 (unsigned) ar->lleft, (unsigned) ar->pleft,
4411 (unsigned) ar->lright, (unsigned) ar->pright,
Nikolay Borisov82dd1242019-02-10 23:04:16 -05004412 inode_is_open_for_write(ar->inode) ? "" : "non-");
Alex Tomasc9de5602008-01-29 00:19:52 -05004413 return 0;
4414
4415}
4416
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004417static noinline_for_stack void
4418ext4_mb_discard_lg_preallocations(struct super_block *sb,
4419 struct ext4_locality_group *lg,
4420 int order, int total_entries)
4421{
4422 ext4_group_t group = 0;
4423 struct ext4_buddy e4b;
4424 struct list_head discard_list;
4425 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004426
Ritesh Harjanid3df1452020-05-10 11:54:54 +05304427 mb_debug(sb, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004428
4429 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004430
4431 spin_lock(&lg->lg_prealloc_lock);
4432 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
Madhuparna Bhowmik92e9c582020-02-13 20:55:58 +05304433 pa_inode_list,
4434 lockdep_is_held(&lg->lg_prealloc_lock)) {
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004435 spin_lock(&pa->pa_lock);
4436 if (atomic_read(&pa->pa_count)) {
4437 /*
4438 * This is the pa that we just used
4439 * for block allocation. So don't
4440 * free that
4441 */
4442 spin_unlock(&pa->pa_lock);
4443 continue;
4444 }
4445 if (pa->pa_deleted) {
4446 spin_unlock(&pa->pa_lock);
4447 continue;
4448 }
4449 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004450 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004451
4452 /* seems this one can be freed ... */
4453 pa->pa_deleted = 1;
4454 spin_unlock(&pa->pa_lock);
4455
4456 list_del_rcu(&pa->pa_inode_list);
4457 list_add(&pa->u.pa_tmp_list, &discard_list);
4458
4459 total_entries--;
4460 if (total_entries <= 5) {
4461 /*
4462 * we want to keep only 5 entries
4463 * allowing it to grow to 8. This
4464 * mak sure we don't call discard
4465 * soon for this list.
4466 */
4467 break;
4468 }
4469 }
4470 spin_unlock(&lg->lg_prealloc_lock);
4471
4472 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004473 int err;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004474
Lukas Czernerbd862982013-04-03 23:32:34 -04004475 group = ext4_get_group_number(sb, pa->pa_pstart);
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004476 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4477 GFP_NOFS|__GFP_NOFAIL);
4478 if (err) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004479 ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
4480 err, group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004481 continue;
4482 }
4483 ext4_lock_group(sb, group);
4484 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004485 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004486 ext4_unlock_group(sb, group);
4487
Jing Zhange39e07f2010-05-14 00:00:00 -04004488 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004489 list_del(&pa->u.pa_tmp_list);
4490 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4491 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004492}
4493
4494/*
4495 * We have incremented pa_count. So it cannot be freed at this
4496 * point. Also we hold lg_mutex. So no parallel allocation is
4497 * possible from this lg. That means pa_free cannot be updated.
4498 *
4499 * A parallel ext4_mb_discard_group_preallocations is possible.
4500 * which can cause the lg_prealloc_list to be updated.
4501 */
4502
4503static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4504{
4505 int order, added = 0, lg_prealloc_count = 1;
4506 struct super_block *sb = ac->ac_sb;
4507 struct ext4_locality_group *lg = ac->ac_lg;
4508 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4509
4510 order = fls(pa->pa_free) - 1;
4511 if (order > PREALLOC_TB_SIZE - 1)
4512 /* The max size of hash table is PREALLOC_TB_SIZE */
4513 order = PREALLOC_TB_SIZE - 1;
4514 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004515 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004516 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
Madhuparna Bhowmik92e9c582020-02-13 20:55:58 +05304517 pa_inode_list,
4518 lockdep_is_held(&lg->lg_prealloc_lock)) {
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004519 spin_lock(&tmp_pa->pa_lock);
4520 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004521 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004522 continue;
4523 }
4524 if (!added && pa->pa_free < tmp_pa->pa_free) {
4525 /* Add to the tail of the previous entry */
4526 list_add_tail_rcu(&pa->pa_inode_list,
4527 &tmp_pa->pa_inode_list);
4528 added = 1;
4529 /*
4530 * we want to count the total
4531 * number of entries in the list
4532 */
4533 }
4534 spin_unlock(&tmp_pa->pa_lock);
4535 lg_prealloc_count++;
4536 }
4537 if (!added)
4538 list_add_tail_rcu(&pa->pa_inode_list,
4539 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004540 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004541
4542 /* Now trim the list to be not more than 8 elements */
4543 if (lg_prealloc_count > 8) {
4544 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004545 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004546 return;
4547 }
4548 return ;
4549}
4550
Alex Tomasc9de5602008-01-29 00:19:52 -05004551/*
4552 * release all resource we used in allocation
4553 */
4554static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4555{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004556 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004557 struct ext4_prealloc_space *pa = ac->ac_pa;
4558 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004559 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004560 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004561 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004562 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4563 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004564 pa->pa_free -= ac->ac_b_ex.fe_len;
4565 pa->pa_len -= ac->ac_b_ex.fe_len;
4566 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004567 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004568 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004569 if (pa) {
4570 /*
4571 * We want to add the pa to the right bucket.
4572 * Remove it from the list and while adding
4573 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004574 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004575 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004576 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004577 spin_lock(pa->pa_obj_lock);
4578 list_del_rcu(&pa->pa_inode_list);
4579 spin_unlock(pa->pa_obj_lock);
4580 ext4_mb_add_n_trim(ac);
4581 }
4582 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4583 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004584 if (ac->ac_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004585 put_page(ac->ac_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004586 if (ac->ac_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004587 put_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004588 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4589 mutex_unlock(&ac->ac_lg->lg_mutex);
4590 ext4_mb_collect_stats(ac);
4591 return 0;
4592}
4593
4594static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4595{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004596 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004597 int ret;
4598 int freed = 0;
4599
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004600 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004601 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004602 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4603 freed += ret;
4604 needed -= ret;
4605 }
4606
4607 return freed;
4608}
4609
Ritesh Harjanicf5e2ca2020-05-20 12:10:33 +05304610static bool ext4_mb_discard_preallocations_should_retry(struct super_block *sb,
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304611 struct ext4_allocation_context *ac, u64 *seq)
Ritesh Harjanicf5e2ca2020-05-20 12:10:33 +05304612{
4613 int freed;
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304614 u64 seq_retry = 0;
4615 bool ret = false;
Ritesh Harjanicf5e2ca2020-05-20 12:10:33 +05304616
4617 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304618 if (freed) {
4619 ret = true;
4620 goto out_dbg;
4621 }
4622 seq_retry = ext4_get_discard_pa_seq_sum();
Ritesh Harjani99377832020-05-20 12:10:36 +05304623 if (!(ac->ac_flags & EXT4_MB_STRICT_CHECK) || seq_retry != *seq) {
4624 ac->ac_flags |= EXT4_MB_STRICT_CHECK;
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304625 *seq = seq_retry;
4626 ret = true;
4627 }
4628
4629out_dbg:
4630 mb_debug(sb, "freed %d, retry ? %s\n", freed, ret ? "yes" : "no");
4631 return ret;
Ritesh Harjanicf5e2ca2020-05-20 12:10:33 +05304632}
4633
Alex Tomasc9de5602008-01-29 00:19:52 -05004634/*
4635 * Main entry point into mballoc to allocate blocks
4636 * it tries to use preallocation first, then falls back
4637 * to usual allocation
4638 */
4639ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004640 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004641{
Eric Sandeen256bdb42008-02-10 01:13:33 -05004642 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004643 struct ext4_sb_info *sbi;
4644 struct super_block *sb;
4645 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004646 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004647 unsigned int reserv_clstrs = 0;
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304648 u64 seq;
Alex Tomasc9de5602008-01-29 00:19:52 -05004649
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004650 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004651 sb = ar->inode->i_sb;
4652 sbi = EXT4_SB(sb);
4653
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004654 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004655
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004656 /* Allow to use superuser reservation for quota file */
Tahsin Erdogan02749a42017-06-22 11:31:25 -04004657 if (ext4_is_quota_file(ar->inode))
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004658 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4659
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004660 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004661 /* Without delayed allocation we need to verify
4662 * there is enough free blocks to do block allocation
4663 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004664 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004665 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004666 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004667
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004668 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004669 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004670 ar->len = ar->len >> 1;
4671 }
4672 if (!ar->len) {
Ritesh Harjanibbc4ec72020-05-10 11:54:43 +05304673 ext4_mb_show_pa(sb);
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004674 *errp = -ENOSPC;
4675 return 0;
4676 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004677 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004678 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004679 dquot_alloc_block_nofail(ar->inode,
4680 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004681 } else {
4682 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004683 dquot_alloc_block(ar->inode,
4684 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004685
4686 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4687 ar->len--;
4688 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004689 }
4690 inquota = ar->len;
4691 if (ar->len == 0) {
4692 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004693 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004694 }
Mingming Caod2a17632008-07-14 17:52:37 -04004695 }
Mingming Caod2a17632008-07-14 17:52:37 -04004696
Wei Yongjun85556c92012-09-26 20:43:37 -04004697 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004698 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004699 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004700 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004701 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004702 }
4703
Eric Sandeen256bdb42008-02-10 01:13:33 -05004704 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004705 if (*errp) {
4706 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004707 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004708 }
4709
Eric Sandeen256bdb42008-02-10 01:13:33 -05004710 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
Ritesh Harjani81198532020-06-09 16:23:10 +05304711 seq = this_cpu_read(discard_pa_seq);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004712 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004713 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4714 ext4_mb_normalize_request(ac, ar);
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304715
4716 *errp = ext4_mb_pa_alloc(ac);
4717 if (*errp)
4718 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004719repeat:
4720 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004721 *errp = ext4_mb_regular_allocator(ac);
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304722 /*
4723 * pa allocated above is added to grp->bb_prealloc_list only
4724 * when we were able to allocate some block i.e. when
4725 * ac->ac_status == AC_STATUS_FOUND.
4726 * And error from above mean ac->ac_status != AC_STATUS_FOUND
4727 * So we have to free this pa here itself.
4728 */
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004729 if (*errp) {
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304730 ext4_mb_pa_free(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004731 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004732 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004733 }
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304734 if (ac->ac_status == AC_STATUS_FOUND &&
4735 ac->ac_o_ex.fe_len >= ac->ac_f_ex.fe_len)
4736 ext4_mb_pa_free(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004737 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004738 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004739 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Vegard Nossum554a5cc2016-07-14 23:02:47 -04004740 if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004741 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004742 goto errout;
4743 } else {
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004744 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4745 ar->len = ac->ac_b_ex.fe_len;
4746 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004747 } else {
Ritesh Harjani07b5b8e2020-05-20 12:10:34 +05304748 if (ext4_mb_discard_preallocations_should_retry(sb, ac, &seq))
Alex Tomasc9de5602008-01-29 00:19:52 -05004749 goto repeat;
Ritesh Harjani53f86b12020-05-20 12:10:32 +05304750 /*
4751 * If block allocation fails then the pa allocated above
4752 * needs to be freed here itself.
4753 */
4754 ext4_mb_pa_free(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004755 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004756 }
4757
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004758errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004759 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004760 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004761 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004762 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004763 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004764 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004765out:
4766 if (ac)
4767 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004768 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004769 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004770 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004771 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004772 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004773 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004774 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004775 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004776
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004777 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004778
Alex Tomasc9de5602008-01-29 00:19:52 -05004779 return block;
4780}
Alex Tomasc9de5602008-01-29 00:19:52 -05004781
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004782/*
4783 * We can merge two free data extents only if the physical blocks
4784 * are contiguous, AND the extents were freed by the same transaction,
4785 * AND the blocks are associated with the same group.
4786 */
Daeho Jeonga0154342017-06-22 23:54:33 -04004787static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi,
4788 struct ext4_free_data *entry,
4789 struct ext4_free_data *new_entry,
4790 struct rb_root *entry_rb_root)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004791{
Daeho Jeonga0154342017-06-22 23:54:33 -04004792 if ((entry->efd_tid != new_entry->efd_tid) ||
4793 (entry->efd_group != new_entry->efd_group))
4794 return;
4795 if (entry->efd_start_cluster + entry->efd_count ==
4796 new_entry->efd_start_cluster) {
4797 new_entry->efd_start_cluster = entry->efd_start_cluster;
4798 new_entry->efd_count += entry->efd_count;
4799 } else if (new_entry->efd_start_cluster + new_entry->efd_count ==
4800 entry->efd_start_cluster) {
4801 new_entry->efd_count += entry->efd_count;
4802 } else
4803 return;
4804 spin_lock(&sbi->s_md_lock);
4805 list_del(&entry->efd_list);
4806 spin_unlock(&sbi->s_md_lock);
4807 rb_erase(&entry->efd_node, entry_rb_root);
4808 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004809}
4810
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004811static noinline_for_stack int
4812ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004813 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004814{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004815 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004816 ext4_grpblk_t cluster;
Theodore Ts'od08854f2016-06-26 18:24:01 -04004817 ext4_grpblk_t clusters = new_entry->efd_count;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004818 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004819 struct ext4_group_info *db = e4b->bd_info;
4820 struct super_block *sb = e4b->bd_sb;
4821 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004822 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4823 struct rb_node *parent = NULL, *new_node;
4824
Frank Mayhar03901312009-01-07 00:06:22 -05004825 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004826 BUG_ON(e4b->bd_bitmap_page == NULL);
4827 BUG_ON(e4b->bd_buddy_page == NULL);
4828
Bobi Jam18aadd42012-02-20 17:53:02 -05004829 new_node = &new_entry->efd_node;
4830 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004831
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004832 if (!*n) {
4833 /* first free block exent. We need to
4834 protect buddy cache from being freed,
4835 * otherwise we'll refresh it from
4836 * on-disk bitmap and lose not-yet-available
4837 * blocks */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004838 get_page(e4b->bd_buddy_page);
4839 get_page(e4b->bd_bitmap_page);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004840 }
4841 while (*n) {
4842 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004843 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4844 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004845 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004846 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004847 n = &(*n)->rb_right;
4848 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004849 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004850 ext4_group_first_block_no(sb, group) +
4851 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004852 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004853 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004854 }
4855 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004856
4857 rb_link_node(new_node, parent, n);
4858 rb_insert_color(new_node, &db->bb_free_root);
4859
4860 /* Now try to see the extent can be merged to left and right */
4861 node = rb_prev(new_node);
4862 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004863 entry = rb_entry(node, struct ext4_free_data, efd_node);
Daeho Jeonga0154342017-06-22 23:54:33 -04004864 ext4_try_merge_freed_extent(sbi, entry, new_entry,
4865 &(db->bb_free_root));
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004866 }
4867
4868 node = rb_next(new_node);
4869 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004870 entry = rb_entry(node, struct ext4_free_data, efd_node);
Daeho Jeonga0154342017-06-22 23:54:33 -04004871 ext4_try_merge_freed_extent(sbi, entry, new_entry,
4872 &(db->bb_free_root));
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004873 }
Daeho Jeonga0154342017-06-22 23:54:33 -04004874
Theodore Ts'od08854f2016-06-26 18:24:01 -04004875 spin_lock(&sbi->s_md_lock);
Daeho Jeonga0154342017-06-22 23:54:33 -04004876 list_add_tail(&new_entry->efd_list, &sbi->s_freed_data_list);
Theodore Ts'od08854f2016-06-26 18:24:01 -04004877 sbi->s_mb_free_pending += clusters;
4878 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004879 return 0;
4880}
4881
Theodore Ts'o44338712009-11-22 07:44:56 -05004882/**
4883 * ext4_free_blocks() -- Free given blocks and update quota
4884 * @handle: handle for this transaction
4885 * @inode: inode
Theodore Ts'oc60990b2019-06-19 16:30:03 -04004886 * @bh: optional buffer of the block to be freed
4887 * @block: starting physical block to be freed
4888 * @count: number of blocks to be freed
Yongqiang Yang5def1362011-06-05 23:26:40 -04004889 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004890 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004891void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004892 struct buffer_head *bh, ext4_fsblk_t block,
4893 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004894{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004895 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004896 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004897 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004898 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004899 ext4_grpblk_t bit;
4900 struct buffer_head *gd_bh;
4901 ext4_group_t block_group;
4902 struct ext4_sb_info *sbi;
4903 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004904 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004905 int err = 0;
4906 int ret;
4907
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004908 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004909 if (bh) {
4910 if (block)
4911 BUG_ON(block != bh->b_blocknr);
4912 else
4913 block = bh->b_blocknr;
4914 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004915
Alex Tomasc9de5602008-01-29 00:19:52 -05004916 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004917 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4918 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004919 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004920 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004921 goto error_return;
4922 }
4923
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004924 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004925 trace_ext4_free_blocks(inode, block, count, flags);
4926
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004927 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4928 BUG_ON(count > 1);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004929
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004930 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4931 inode, bh, block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004932 }
4933
Theodore Ts'o60e66792010-05-17 07:00:00 -04004934 /*
Theodore Ts'o84130192011-09-09 18:50:51 -04004935 * If the extent to be freed does not begin on a cluster
4936 * boundary, we need to deal with partial clusters at the
4937 * beginning and end of the extent. Normally we will free
4938 * blocks at the beginning or the end unless we are explicitly
4939 * requested to avoid doing so.
4940 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004941 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004942 if (overflow) {
4943 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4944 overflow = sbi->s_cluster_ratio - overflow;
4945 block += overflow;
4946 if (count > overflow)
4947 count -= overflow;
4948 else
4949 return;
4950 } else {
4951 block -= overflow;
4952 count += overflow;
4953 }
4954 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004955 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004956 if (overflow) {
4957 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4958 if (count > overflow)
4959 count -= overflow;
4960 else
4961 return;
4962 } else
4963 count += sbi->s_cluster_ratio - overflow;
4964 }
4965
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004966 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4967 int i;
Daeho Jeongf96c4502016-02-21 18:31:41 -05004968 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004969
4970 for (i = 0; i < count; i++) {
4971 cond_resched();
Daeho Jeongf96c4502016-02-21 18:31:41 -05004972 if (is_metadata)
4973 bh = sb_find_get_block(inode->i_sb, block + i);
4974 ext4_forget(handle, is_metadata, inode, bh, block + i);
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004975 }
4976 }
4977
Alex Tomasc9de5602008-01-29 00:19:52 -05004978do_more:
4979 overflow = 0;
4980 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4981
Darrick J. Wong163a2032013-08-28 17:35:51 -04004982 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4983 ext4_get_group_info(sb, block_group))))
4984 return;
4985
Alex Tomasc9de5602008-01-29 00:19:52 -05004986 /*
4987 * Check to see if we are freeing blocks across a group
4988 * boundary.
4989 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004990 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4991 overflow = EXT4_C2B(sbi, bit) + count -
4992 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004993 count -= overflow;
4994 }
Lukas Czerner810da242013-03-02 17:18:58 -05004995 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004996 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004997 if (IS_ERR(bitmap_bh)) {
4998 err = PTR_ERR(bitmap_bh);
4999 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05005000 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04005001 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005002 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04005003 if (!gdp) {
5004 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05005005 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04005006 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005007
5008 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
5009 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
5010 in_range(block, ext4_inode_table(sb, gdp),
Jun Piao49598e02018-01-11 13:17:49 -05005011 sbi->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05005012 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Jun Piao49598e02018-01-11 13:17:49 -05005013 sbi->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05005014
Eric Sandeen12062dd2010-02-15 14:19:27 -05005015 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04005016 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04005017 /* err = 0. ext4_std_error should be a no op */
5018 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05005019 }
5020
5021 BUFFER_TRACE(bitmap_bh, "getting write access");
5022 err = ext4_journal_get_write_access(handle, bitmap_bh);
5023 if (err)
5024 goto error_return;
5025
5026 /*
5027 * We are about to modify some metadata. Call the journal APIs
5028 * to unshare ->b_data if a currently-committing transaction is
5029 * using it
5030 */
5031 BUFFER_TRACE(gd_bh, "get_write_access");
5032 err = ext4_journal_get_write_access(handle, gd_bh);
5033 if (err)
5034 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05005035#ifdef AGGRESSIVE_CHECK
5036 {
5037 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04005038 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05005039 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
5040 }
5041#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04005042 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05005043
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005044 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
5045 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
5046 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05005047 if (err)
5048 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05005049
Daeho Jeongf96c4502016-02-21 18:31:41 -05005050 /*
5051 * We need to make sure we don't reuse the freed block until after the
5052 * transaction is committed. We make an exception if the inode is to be
5053 * written in writeback mode since writeback mode has weak data
5054 * consistency guarantees.
5055 */
5056 if (ext4_handle_valid(handle) &&
5057 ((flags & EXT4_FREE_BLOCKS_METADATA) ||
5058 !ext4_should_writeback_data(inode))) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005059 struct ext4_free_data *new_entry;
5060 /*
Michal Hocko7444a072015-07-05 12:33:44 -04005061 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
5062 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005063 */
Michal Hocko7444a072015-07-05 12:33:44 -04005064 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
5065 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05005066 new_entry->efd_start_cluster = bit;
5067 new_entry->efd_group = block_group;
5068 new_entry->efd_count = count_clusters;
5069 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04005070
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005071 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04005072 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005073 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05005074 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005075 /* need to update group_info->bb_free and bitmap
5076 * with group lock held. generate_buddy look at
5077 * them with group lock_held
5078 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005079 if (test_opt(sb, DISCARD)) {
Daeho Jeonga0154342017-06-22 23:54:33 -04005080 err = ext4_issue_discard(sb, block_group, bit, count,
5081 NULL);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005082 if (err && err != -EOPNOTSUPP)
5083 ext4_msg(sb, KERN_WARNING, "discard request in"
5084 " group:%d block:%d count:%lu failed"
5085 " with %d", block_group, bit, count,
5086 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04005087 } else
5088 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005089
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04005090 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04005091 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
5092 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05005093 }
5094
Theodore Ts'o021b65b2011-09-09 19:08:51 -04005095 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
5096 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04005097 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005098 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04005099 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05005100
Jose R. Santos772cb7c2008-07-11 19:27:31 -04005101 if (sbi->s_log_groups_per_flex) {
5102 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04005103 atomic64_add(count_clusters,
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08005104 &sbi_array_rcu_deref(sbi, s_flex_groups,
5105 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04005106 }
5107
Eric Whitney9fe67142018-10-01 14:25:08 -04005108 /*
5109 * on a bigalloc file system, defer the s_freeclusters_counter
5110 * update to the caller (ext4_remove_space and friends) so they
5111 * can determine if a cluster freed here should be rereserved
5112 */
5113 if (!(flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)) {
5114 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
5115 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
5116 percpu_counter_add(&sbi->s_freeclusters_counter,
5117 count_clusters);
5118 }
Jan Kara7d734532013-08-17 09:36:54 -04005119
5120 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04005121
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05005122 /* We dirtied the bitmap block */
5123 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5124 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5125
Alex Tomasc9de5602008-01-29 00:19:52 -05005126 /* And the group descriptor block */
5127 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05005128 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05005129 if (!err)
5130 err = ret;
5131
5132 if (overflow && !err) {
5133 block += count;
5134 count = overflow;
5135 put_bh(bitmap_bh);
5136 goto do_more;
5137 }
Alex Tomasc9de5602008-01-29 00:19:52 -05005138error_return:
5139 brelse(bitmap_bh);
5140 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05005141 return;
5142}
Lukas Czerner7360d172010-10-27 21:30:12 -04005143
5144/**
Yongqiang Yang05291552011-07-26 21:43:56 -04005145 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04005146 * @handle: handle to this transaction
5147 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005148 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04005149 * @count: number of blocks to free
5150 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04005151 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04005152 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005153int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04005154 ext4_fsblk_t block, unsigned long count)
5155{
5156 struct buffer_head *bitmap_bh = NULL;
5157 struct buffer_head *gd_bh;
5158 ext4_group_t block_group;
5159 ext4_grpblk_t bit;
5160 unsigned int i;
5161 struct ext4_group_desc *desc;
5162 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04005163 struct ext4_buddy e4b;
harshadsd77147f2017-10-29 09:38:46 -04005164 int err = 0, ret, free_clusters_count;
5165 ext4_grpblk_t clusters_freed;
5166 ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block);
5167 ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1);
5168 unsigned long cluster_count = last_cluster - first_cluster + 1;
Amir Goldstein2846e822011-05-09 10:46:41 -04005169
5170 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
5171
Yongqiang Yang4740b832011-07-26 21:51:08 -04005172 if (count == 0)
5173 return 0;
5174
Amir Goldstein2846e822011-05-09 10:46:41 -04005175 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04005176 /*
5177 * Check to see if we are freeing blocks across a group
5178 * boundary.
5179 */
harshadsd77147f2017-10-29 09:38:46 -04005180 if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) {
5181 ext4_warning(sb, "too many blocks added to group %u",
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005182 block_group);
5183 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005184 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005185 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005186
Amir Goldstein2846e822011-05-09 10:46:41 -04005187 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04005188 if (IS_ERR(bitmap_bh)) {
5189 err = PTR_ERR(bitmap_bh);
5190 bitmap_bh = NULL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005191 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005192 }
5193
Amir Goldstein2846e822011-05-09 10:46:41 -04005194 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005195 if (!desc) {
5196 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04005197 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005198 }
Amir Goldstein2846e822011-05-09 10:46:41 -04005199
5200 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
5201 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
5202 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
5203 in_range(block + count - 1, ext4_inode_table(sb, desc),
5204 sbi->s_itb_per_group)) {
5205 ext4_error(sb, "Adding blocks in system zones - "
5206 "Block = %llu, count = %lu",
5207 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005208 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005209 goto error_return;
5210 }
5211
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005212 BUFFER_TRACE(bitmap_bh, "getting write access");
5213 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04005214 if (err)
5215 goto error_return;
5216
5217 /*
5218 * We are about to modify some metadata. Call the journal APIs
5219 * to unshare ->b_data if a currently-committing transaction is
5220 * using it
5221 */
5222 BUFFER_TRACE(gd_bh, "get_write_access");
5223 err = ext4_journal_get_write_access(handle, gd_bh);
5224 if (err)
5225 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04005226
harshadsd77147f2017-10-29 09:38:46 -04005227 for (i = 0, clusters_freed = 0; i < cluster_count; i++) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005228 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04005229 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005230 ext4_error(sb, "bit already cleared for block %llu",
5231 (ext4_fsblk_t)(block + i));
5232 BUFFER_TRACE(bitmap_bh, "bit already cleared");
5233 } else {
harshadsd77147f2017-10-29 09:38:46 -04005234 clusters_freed++;
Amir Goldstein2846e822011-05-09 10:46:41 -04005235 }
5236 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005237
5238 err = ext4_mb_load_buddy(sb, block_group, &e4b);
5239 if (err)
5240 goto error_return;
5241
5242 /*
5243 * need to update group_info->bb_free and bitmap
5244 * with group lock held. generate_buddy look at
5245 * them with group lock_held
5246 */
Amir Goldstein2846e822011-05-09 10:46:41 -04005247 ext4_lock_group(sb, block_group);
harshadsd77147f2017-10-29 09:38:46 -04005248 mb_clear_bits(bitmap_bh->b_data, bit, cluster_count);
5249 mb_free_blocks(NULL, &e4b, bit, cluster_count);
5250 free_clusters_count = clusters_freed +
5251 ext4_free_group_clusters(sb, desc);
5252 ext4_free_group_clusters_set(sb, desc, free_clusters_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04005253 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005254 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04005255 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04005256 percpu_counter_add(&sbi->s_freeclusters_counter,
harshadsd77147f2017-10-29 09:38:46 -04005257 clusters_freed);
Amir Goldstein2846e822011-05-09 10:46:41 -04005258
5259 if (sbi->s_log_groups_per_flex) {
5260 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
harshadsd77147f2017-10-29 09:38:46 -04005261 atomic64_add(clusters_freed,
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08005262 &sbi_array_rcu_deref(sbi, s_flex_groups,
5263 flex_group)->free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04005264 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005265
5266 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005267
5268 /* We dirtied the bitmap block */
5269 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5270 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5271
5272 /* And the group descriptor block */
5273 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5274 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5275 if (!err)
5276 err = ret;
5277
5278error_return:
5279 brelse(bitmap_bh);
5280 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005281 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005282}
5283
5284/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005285 * ext4_trim_extent -- function to TRIM one single free extent in the group
5286 * @sb: super block for the file system
5287 * @start: starting block of the free extent in the alloc. group
5288 * @count: number of blocks to TRIM
5289 * @group: alloc. group we are working with
5290 * @e4b: ext4 buddy for the group
5291 *
5292 * Trim "count" blocks starting at "start" in the "group". To assure that no
5293 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5294 * be called with under the group lock.
5295 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005296static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04005297 ext4_group_t group, struct ext4_buddy *e4b)
jon ernste2cbd582014-04-12 23:01:28 -04005298__releases(bitlock)
5299__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005300{
5301 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005302 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005303
Tao Mab3d4c2b2011-07-11 00:01:52 -04005304 trace_ext4_trim_extent(sb, group, start, count);
5305
Lukas Czerner7360d172010-10-27 21:30:12 -04005306 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5307
5308 ex.fe_start = start;
5309 ex.fe_group = group;
5310 ex.fe_len = count;
5311
5312 /*
5313 * Mark blocks used, so no one can reuse them while
5314 * being trimmed.
5315 */
5316 mb_mark_used(e4b, &ex);
5317 ext4_unlock_group(sb, group);
Daeho Jeonga0154342017-06-22 23:54:33 -04005318 ret = ext4_issue_discard(sb, group, start, count, NULL);
Lukas Czerner7360d172010-10-27 21:30:12 -04005319 ext4_lock_group(sb, group);
5320 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005321 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005322}
5323
5324/**
5325 * ext4_trim_all_free -- function to trim all free space in alloc. group
5326 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005327 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005328 * @start: first group block to examine
5329 * @max: last group block to examine
5330 * @minblocks: minimum extent block count
5331 *
5332 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5333 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5334 * the extent.
5335 *
5336 *
5337 * ext4_trim_all_free walks through group's block bitmap searching for free
5338 * extents. When the free extent is found, mark it as used in group buddy
5339 * bitmap. Then issue a TRIM command on this extent and free the extent in
5340 * the group buddy bitmap. This is done until whole group is scanned.
5341 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005342static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005343ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5344 ext4_grpblk_t start, ext4_grpblk_t max,
5345 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005346{
5347 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005348 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005349 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005350 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005351
Tao Mab3d4c2b2011-07-11 00:01:52 -04005352 trace_ext4_trim_all_free(sb, group, start, max);
5353
Lukas Czerner78944082011-05-24 18:16:27 -04005354 ret = ext4_mb_load_buddy(sb, group, &e4b);
5355 if (ret) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04005356 ext4_warning(sb, "Error %d loading buddy information for %u",
5357 ret, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005358 return ret;
5359 }
Lukas Czerner78944082011-05-24 18:16:27 -04005360 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005361
5362 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005363 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5364 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5365 goto out;
5366
Lukas Czerner78944082011-05-24 18:16:27 -04005367 start = (e4b.bd_info->bb_first_free > start) ?
5368 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005369
Lukas Czerner913eed832012-03-21 21:22:22 -04005370 while (start <= max) {
5371 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5372 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005373 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005374 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005375
5376 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005377 ret = ext4_trim_extent(sb, start,
5378 next - start, group, &e4b);
5379 if (ret && ret != -EOPNOTSUPP)
5380 break;
5381 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005382 count += next - start;
5383 }
Tao Ma169ddc32011-07-11 00:00:07 -04005384 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005385 start = next + 1;
5386
5387 if (fatal_signal_pending(current)) {
5388 count = -ERESTARTSYS;
5389 break;
5390 }
5391
5392 if (need_resched()) {
5393 ext4_unlock_group(sb, group);
5394 cond_resched();
5395 ext4_lock_group(sb, group);
5396 }
5397
Tao Ma169ddc32011-07-11 00:00:07 -04005398 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005399 break;
5400 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005401
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005402 if (!ret) {
5403 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005404 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005405 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005406out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005407 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005408 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005409
5410 ext4_debug("trimmed %d blocks in the group %d\n",
5411 count, group);
5412
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005413 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005414}
5415
5416/**
5417 * ext4_trim_fs() -- trim ioctl handle function
5418 * @sb: superblock for filesystem
5419 * @range: fstrim_range structure
5420 *
5421 * start: First Byte to trim
5422 * len: number of Bytes to trim from start
5423 * minlen: minimum extent length in Bytes
5424 * ext4_trim_fs goes through all allocation groups containing Bytes from
5425 * start to start+len. For each such a group ext4_trim_all_free function
5426 * is invoked to trim all free space.
5427 */
5428int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
5429{
Lukas Czerner78944082011-05-24 18:16:27 -04005430 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005431 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005432 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005433 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005434 ext4_fsblk_t first_data_blk =
5435 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005436 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005437 int ret = 0;
5438
5439 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005440 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005441 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5442 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005443
Lukas Czerner5de35e82012-10-22 18:01:19 -04005444 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5445 start >= max_blks ||
5446 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005447 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005448 if (end >= max_blks)
5449 end = max_blks - 1;
5450 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005451 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005452 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005453 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005454
Lukas Czerner913eed832012-03-21 21:22:22 -04005455 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005456 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005457 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005458 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005459 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005460
Lukas Czerner913eed832012-03-21 21:22:22 -04005461 /* end now represents the last cluster to discard in this group */
5462 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005463
5464 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005465 grp = ext4_get_group_info(sb, group);
5466 /* We only do this if the grp has never been initialized */
5467 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005468 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Lukas Czerner78944082011-05-24 18:16:27 -04005469 if (ret)
5470 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005471 }
5472
Tao Ma0ba08512011-03-23 15:48:11 -04005473 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005474 * For all the groups except the last one, last cluster will
5475 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5476 * change it for the last group, note that last_cluster is
5477 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005478 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005479 if (group == last_group)
5480 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005481
Lukas Czerner78944082011-05-24 18:16:27 -04005482 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005483 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005484 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005485 if (cnt < 0) {
5486 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005487 break;
5488 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005489 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005490 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005491
5492 /*
5493 * For every group except the first one, we are sure
5494 * that the first cluster to discard will be cluster #0.
5495 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005496 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005497 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005498
Tao Ma3d56b8d2011-07-11 00:03:38 -04005499 if (!ret)
5500 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5501
Tao Ma22f10452011-07-10 23:52:37 -04005502out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005503 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005504 return ret;
5505}
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -04005506
5507/* Iterate all the free extents in the group. */
5508int
5509ext4_mballoc_query_range(
5510 struct super_block *sb,
5511 ext4_group_t group,
5512 ext4_grpblk_t start,
5513 ext4_grpblk_t end,
5514 ext4_mballoc_query_range_fn formatter,
5515 void *priv)
5516{
5517 void *bitmap;
5518 ext4_grpblk_t next;
5519 struct ext4_buddy e4b;
5520 int error;
5521
5522 error = ext4_mb_load_buddy(sb, group, &e4b);
5523 if (error)
5524 return error;
5525 bitmap = e4b.bd_bitmap;
5526
5527 ext4_lock_group(sb, group);
5528
5529 start = (e4b.bd_info->bb_first_free > start) ?
5530 e4b.bd_info->bb_first_free : start;
5531 if (end >= EXT4_CLUSTERS_PER_GROUP(sb))
5532 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
5533
5534 while (start <= end) {
5535 start = mb_find_next_zero_bit(bitmap, end + 1, start);
5536 if (start > end)
5537 break;
5538 next = mb_find_next_bit(bitmap, end + 1, start);
5539
5540 ext4_unlock_group(sb, group);
5541 error = formatter(sb, group, start, next - start, priv);
5542 if (error)
5543 goto out_unload;
5544 ext4_lock_group(sb, group);
5545
5546 start = next + 1;
5547 }
5548
5549 ext4_unlock_group(sb, group);
5550out_unload:
5551 ext4_mb_unload_buddy(&e4b);
5552
5553 return error;
5554}