blob: 402c769c51ea16d4ccb0ef6114c5f7f58a593c2b [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>
Tejun Heo66114ca2015-05-22 17:13:32 -040017#include <linux/backing-dev.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040018#include <trace/events/ext4.h>
19
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050020#ifdef CONFIG_EXT4_DEBUG
21ushort ext4_mballoc_debug __read_mostly;
22
23module_param_named(mballoc_debug, ext4_mballoc_debug, ushort, 0644);
24MODULE_PARM_DESC(mballoc_debug, "Debugging level for ext4's mballoc");
25#endif
26
Alex Tomasc9de5602008-01-29 00:19:52 -050027/*
28 * MUSTDO:
29 * - test ext4_ext_search_left() and ext4_ext_search_right()
30 * - search for metadata in few groups
31 *
32 * TODO v4:
33 * - normalization should take into account whether file is still open
34 * - discard preallocations if no free space left (policy?)
35 * - don't normalize tails
36 * - quota
37 * - reservation for superuser
38 *
39 * TODO v3:
40 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
41 * - track min/max extents in each group for better group selection
42 * - mb_mark_used() may allocate chunk right after splitting buddy
43 * - tree of groups sorted by number of free blocks
44 * - error handling
45 */
46
47/*
48 * The allocation request involve request for multiple number of blocks
49 * near to the goal(block) value specified.
50 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040051 * During initialization phase of the allocator we decide to use the
52 * group preallocation or inode preallocation depending on the size of
53 * the file. The size of the file could be the resulting file size we
54 * would have after allocation, or the current file size, which ever
55 * is larger. If the size is less than sbi->s_mb_stream_request we
56 * select to use the group preallocation. The default value of
57 * s_mb_stream_request is 16 blocks. This can also be tuned via
58 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
59 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050060 *
61 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040062 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050063 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040064 * First stage the allocator looks at the inode prealloc list,
65 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
66 * spaces for this particular inode. The inode prealloc space is
67 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050068 *
69 * pa_lstart -> the logical start block for this prealloc space
70 * pa_pstart -> the physical start block for this prealloc space
Theodore Ts'o53accfa2011-09-09 18:48:51 -040071 * pa_len -> length for this prealloc space (in clusters)
72 * pa_free -> free space available in this prealloc space (in clusters)
Alex Tomasc9de5602008-01-29 00:19:52 -050073 *
74 * The inode preallocation space is used looking at the _logical_ start
75 * block. If only the logical file block falls within the range of prealloc
Tao Macaaf7a22011-07-11 18:42:42 -040076 * space we will consume the particular prealloc space. This makes sure that
77 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050078 *
79 * The important thing to be noted in case of inode prealloc space is that
80 * we don't modify the values associated to inode prealloc space except
81 * pa_free.
82 *
83 * If we are not able to find blocks in the inode prealloc space and if we
84 * have the group allocation flag set then we look at the locality group
Tao Macaaf7a22011-07-11 18:42:42 -040085 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050086 *
87 * ext4_sb_info.s_locality_groups[smp_processor_id()]
88 *
89 * The reason for having a per cpu locality group is to reduce the contention
90 * between CPUs. It is possible to get scheduled at this point.
91 *
92 * The locality group prealloc space is used looking at whether we have
Lucas De Marchi25985ed2011-03-30 22:57:33 -030093 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -050094 *
95 * If we can't allocate blocks via inode prealloc or/and locality group
96 * prealloc then we look at the buddy cache. The buddy cache is represented
97 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
98 * mapped to the buddy and bitmap information regarding different
99 * groups. The buddy information is attached to buddy cache inode so that
100 * we can access them through the page cache. The information regarding
101 * each group is loaded via ext4_mb_load_buddy. The information involve
102 * block bitmap and buddy information. The information are stored in the
103 * inode as:
104 *
105 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500106 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500107 *
108 *
109 * one block each for bitmap and buddy information. So for each group we
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300110 * take up 2 blocks. A page can contain blocks_per_page (PAGE_SIZE /
Alex Tomasc9de5602008-01-29 00:19:52 -0500111 * blocksize) blocks. So it can have information regarding groups_per_page
112 * which is blocks_per_page/2
113 *
114 * The buddy cache inode is not stored on disk. The inode is thrown
115 * away when the filesystem is unmounted.
116 *
117 * We look for count number of blocks in the buddy cache. If we were able
118 * to locate that many free blocks we return with additional information
119 * regarding rest of the contiguous physical block available
120 *
121 * Before allocating blocks via buddy cache we normalize the request
122 * blocks. This ensure we ask for more blocks that we needed. The extra
123 * blocks that we get after allocation is added to the respective prealloc
124 * list. In case of inode preallocation we follow a list of heuristics
125 * based on file size. This can be found in ext4_mb_normalize_request. If
126 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'o27baebb2011-09-09 19:02:51 -0400127 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
128 * dependent on the cluster size; for non-bigalloc file systems, it is
Alex Tomasc9de5602008-01-29 00:19:52 -0500129 * 512 blocks. This can be tuned via
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400130 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500131 * terms of number of blocks. If we have mounted the file system with -O
132 * stripe=<value> option the group prealloc request is normalized to the
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400133 * the smallest multiple of the stripe value (sbi->s_stripe) which is
134 * greater than the default mb_group_prealloc.
Alex Tomasc9de5602008-01-29 00:19:52 -0500135 *
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400136 * The regular allocator (using the buddy cache) supports a few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500137 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400138 * /sys/fs/ext4/<partition>/mb_min_to_scan
139 * /sys/fs/ext4/<partition>/mb_max_to_scan
140 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500141 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400142 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500143 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
144 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400145 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200146 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400147 * stripe size. This should result in better allocation on RAID setups. If
148 * not, we search in the specific group using bitmap for best extents. The
149 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500150 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400151 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500152 * best extent in the found extents. Searching for the blocks starts with
153 * the group specified as the goal value in allocation context via
154 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400155 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500156 * checked.
157 *
158 * Both the prealloc space are getting populated as above. So for the first
159 * request we will hit the buddy cache which will result in this prealloc
160 * space getting filled. The prealloc space is then later used for the
161 * subsequent request.
162 */
163
164/*
165 * mballoc operates on the following data:
166 * - on-disk bitmap
167 * - in-core buddy (actually includes buddy and bitmap)
168 * - preallocation descriptors (PAs)
169 *
170 * there are two types of preallocations:
171 * - inode
172 * assiged to specific inode and can be used for this inode only.
173 * it describes part of inode's space preallocated to specific
174 * physical blocks. any block from that preallocated can be used
175 * independent. the descriptor just tracks number of blocks left
176 * unused. so, before taking some block from descriptor, one must
177 * make sure corresponded logical block isn't allocated yet. this
178 * also means that freeing any block within descriptor's range
179 * must discard all preallocated blocks.
180 * - locality group
181 * assigned to specific locality group which does not translate to
182 * permanent set of inodes: inode can join and leave group. space
183 * from this type of preallocation can be used for any inode. thus
184 * it's consumed from the beginning to the end.
185 *
186 * relation between them can be expressed as:
187 * in-core buddy = on-disk bitmap + preallocation descriptors
188 *
189 * this mean blocks mballoc considers used are:
190 * - allocated blocks (persistent)
191 * - preallocated blocks (non-persistent)
192 *
193 * consistency in mballoc world means that at any time a block is either
194 * free or used in ALL structures. notice: "any time" should not be read
195 * literally -- time is discrete and delimited by locks.
196 *
197 * to keep it simple, we don't use block numbers, instead we count number of
198 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
199 *
200 * all operations can be expressed as:
201 * - init buddy: buddy = on-disk + PAs
202 * - new PA: buddy += N; PA = N
203 * - use inode PA: on-disk += N; PA -= N
204 * - discard inode PA buddy -= on-disk - PA; PA = 0
205 * - use locality group PA on-disk += N; PA -= N
206 * - discard locality group PA buddy -= PA; PA = 0
207 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
208 * is used in real operation because we can't know actual used
209 * bits from PA, only from on-disk bitmap
210 *
211 * if we follow this strict logic, then all operations above should be atomic.
212 * given some of them can block, we'd have to use something like semaphores
213 * killing performance on high-end SMP hardware. let's try to relax it using
214 * the following knowledge:
215 * 1) if buddy is referenced, it's already initialized
216 * 2) while block is used in buddy and the buddy is referenced,
217 * nobody can re-allocate that block
218 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
219 * bit set and PA claims same block, it's OK. IOW, one can set bit in
220 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
221 * block
222 *
223 * so, now we're building a concurrency table:
224 * - init buddy vs.
225 * - new PA
226 * blocks for PA are allocated in the buddy, buddy must be referenced
227 * until PA is linked to allocation group to avoid concurrent buddy init
228 * - use inode PA
229 * we need to make sure that either on-disk bitmap or PA has uptodate data
230 * given (3) we care that PA-=N operation doesn't interfere with init
231 * - discard inode PA
232 * the simplest way would be to have buddy initialized by the discard
233 * - use locality group PA
234 * again PA-=N must be serialized with init
235 * - discard locality group PA
236 * the simplest way would be to have buddy initialized by the discard
237 * - new PA vs.
238 * - use inode PA
239 * i_data_sem serializes them
240 * - discard inode PA
241 * discard process must wait until PA isn't used by another process
242 * - use locality group PA
243 * some mutex should serialize them
244 * - discard locality group PA
245 * discard process must wait until PA isn't used by another process
246 * - use inode PA
247 * - use inode PA
248 * i_data_sem or another mutex should serializes them
249 * - discard inode PA
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * nothing wrong here -- they're different PAs covering different blocks
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
255 *
256 * now we're ready to make few consequences:
257 * - PA is referenced and while it is no discard is possible
258 * - PA is referenced until block isn't marked in on-disk bitmap
259 * - PA changes only after on-disk bitmap
260 * - discard must not compete with init. either init is done before
261 * any discard or they're serialized somehow
262 * - buddy init as sum of on-disk bitmap and PAs is done atomically
263 *
264 * a special case when we've used PA to emptiness. no need to modify buddy
265 * in this case, but we should care about concurrent init
266 *
267 */
268
269 /*
270 * Logic in few words:
271 *
272 * - allocation:
273 * load group
274 * find blocks
275 * mark bits in on-disk bitmap
276 * release group
277 *
278 * - use preallocation:
279 * find proper PA (per-inode or group)
280 * load group
281 * mark bits in on-disk bitmap
282 * release group
283 * release PA
284 *
285 * - free:
286 * load group
287 * mark bits in on-disk bitmap
288 * release group
289 *
290 * - discard preallocations in group:
291 * mark PAs deleted
292 * move them onto local list
293 * load on-disk bitmap
294 * load group
295 * remove PA from object (inode or locality group)
296 * mark free blocks in-core
297 *
298 * - discard inode's preallocations:
299 */
300
301/*
302 * Locking rules
303 *
304 * Locks:
305 * - bitlock on a group (group)
306 * - object (inode/locality) (object)
307 * - per-pa lock (pa)
308 *
309 * Paths:
310 * - new pa
311 * object
312 * group
313 *
314 * - find and use pa:
315 * pa
316 *
317 * - release consumed pa:
318 * pa
319 * group
320 * object
321 *
322 * - generate in-core bitmap:
323 * group
324 * pa
325 *
326 * - discard all for given object (inode, locality group):
327 * object
328 * pa
329 * group
330 *
331 * - discard all for given group:
332 * group
333 * pa
334 * group
335 * object
336 *
337 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500338static struct kmem_cache *ext4_pspace_cachep;
339static struct kmem_cache *ext4_ac_cachep;
Bobi Jam18aadd42012-02-20 17:53:02 -0500340static struct kmem_cache *ext4_free_data_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400341
342/* We create slab caches for groupinfo data structures based on the
343 * superblock block size. There will be one per mounted filesystem for
344 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500345#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400346static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
347
Eric Biggersd6006182017-04-29 23:47:50 -0400348static const char * const ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
Eric Sandeen2892c152011-02-12 08:12:18 -0500349 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
350 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
351 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
352};
353
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500354static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
355 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500356static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
357 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500358
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500359static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
360{
Alex Tomasc9de5602008-01-29 00:19:52 -0500361#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500362 *bit += ((unsigned long) addr & 7UL) << 3;
363 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500364#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500365 *bit += ((unsigned long) addr & 3UL) << 3;
366 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500367#else
368#error "how many bits you are?!"
369#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500370 return addr;
371}
Alex Tomasc9de5602008-01-29 00:19:52 -0500372
373static inline int mb_test_bit(int bit, void *addr)
374{
375 /*
376 * ext4_test_bit on architecture like powerpc
377 * needs unsigned long aligned address
378 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500379 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500380 return ext4_test_bit(bit, addr);
381}
382
383static inline void mb_set_bit(int bit, void *addr)
384{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500385 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500386 ext4_set_bit(bit, addr);
387}
388
Alex Tomasc9de5602008-01-29 00:19:52 -0500389static inline void mb_clear_bit(int bit, void *addr)
390{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500391 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500392 ext4_clear_bit(bit, addr);
393}
394
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400395static inline int mb_test_and_clear_bit(int bit, void *addr)
396{
397 addr = mb_correct_addr_and_bit(&bit, addr);
398 return ext4_test_and_clear_bit(bit, addr);
399}
400
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500401static inline int mb_find_next_zero_bit(void *addr, int max, int start)
402{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400403 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500404 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400405 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500406 start += fix;
407
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400408 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
409 if (ret > max)
410 return max;
411 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500412}
413
414static inline int mb_find_next_bit(void *addr, int max, int start)
415{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400416 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500417 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400418 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500419 start += fix;
420
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400421 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
422 if (ret > max)
423 return max;
424 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500425}
426
Alex Tomasc9de5602008-01-29 00:19:52 -0500427static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
428{
429 char *bb;
430
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500431 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -0500432 BUG_ON(max == NULL);
433
434 if (order > e4b->bd_blkbits + 1) {
435 *max = 0;
436 return NULL;
437 }
438
439 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500440 if (order == 0) {
441 *max = 1 << (e4b->bd_blkbits + 3);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500442 return e4b->bd_bitmap;
Coly Li84b775a2011-02-24 12:51:59 -0500443 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500444
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500445 bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
Alex Tomasc9de5602008-01-29 00:19:52 -0500446 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
447
448 return bb;
449}
450
451#ifdef DOUBLE_CHECK
452static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
453 int first, int count)
454{
455 int i;
456 struct super_block *sb = e4b->bd_sb;
457
458 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
459 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400460 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500461 for (i = 0; i < count; i++) {
462 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
463 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500464
465 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400466 blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500467 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400468 inode ? inode->i_ino : 0,
469 blocknr,
470 "freeing block already freed "
471 "(bit %u)",
472 first + i);
Wang Shilong736dedb2018-05-12 12:37:58 -0400473 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
474 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Alex Tomasc9de5602008-01-29 00:19:52 -0500475 }
476 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
477 }
478}
479
480static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
481{
482 int i;
483
484 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
485 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400486 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500487 for (i = 0; i < count; i++) {
488 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
489 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
490 }
491}
492
493static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
494{
495 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
496 unsigned char *b1, *b2;
497 int i;
498 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
499 b2 = (unsigned char *) bitmap;
500 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
501 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400502 ext4_msg(e4b->bd_sb, KERN_ERR,
503 "corruption in group %u "
504 "at byte %u(%u): %x in copy != %x "
505 "on disk/prealloc",
506 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500507 BUG();
508 }
509 }
510 }
511}
512
513#else
514static inline void mb_free_blocks_double(struct inode *inode,
515 struct ext4_buddy *e4b, int first, int count)
516{
517 return;
518}
519static inline void mb_mark_used_double(struct ext4_buddy *e4b,
520 int first, int count)
521{
522 return;
523}
524static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
525{
526 return;
527}
528#endif
529
530#ifdef AGGRESSIVE_CHECK
531
532#define MB_CHECK_ASSERT(assert) \
533do { \
534 if (!(assert)) { \
535 printk(KERN_EMERG \
536 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
537 function, file, line, # assert); \
538 BUG(); \
539 } \
540} while (0)
541
542static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
543 const char *function, int line)
544{
545 struct super_block *sb = e4b->bd_sb;
546 int order = e4b->bd_blkbits + 1;
547 int max;
548 int max2;
549 int i;
550 int j;
551 int k;
552 int count;
553 struct ext4_group_info *grp;
554 int fragments = 0;
555 int fstart;
556 struct list_head *cur;
557 void *buddy;
558 void *buddy2;
559
Alex Tomasc9de5602008-01-29 00:19:52 -0500560 {
561 static int mb_check_counter;
562 if (mb_check_counter++ % 100 != 0)
563 return 0;
564 }
565
566 while (order > 1) {
567 buddy = mb_find_buddy(e4b, order, &max);
568 MB_CHECK_ASSERT(buddy);
569 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
570 MB_CHECK_ASSERT(buddy2);
571 MB_CHECK_ASSERT(buddy != buddy2);
572 MB_CHECK_ASSERT(max * 2 == max2);
573
574 count = 0;
575 for (i = 0; i < max; i++) {
576
577 if (mb_test_bit(i, buddy)) {
578 /* only single bit in buddy2 may be 1 */
579 if (!mb_test_bit(i << 1, buddy2)) {
580 MB_CHECK_ASSERT(
581 mb_test_bit((i<<1)+1, buddy2));
582 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
583 MB_CHECK_ASSERT(
584 mb_test_bit(i << 1, buddy2));
585 }
586 continue;
587 }
588
Robin Dong0a10da72011-10-26 08:48:54 -0400589 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500590 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
591 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
592
593 for (j = 0; j < (1 << order); j++) {
594 k = (i * (1 << order)) + j;
595 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500596 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500597 }
598 count++;
599 }
600 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
601 order--;
602 }
603
604 fstart = -1;
605 buddy = mb_find_buddy(e4b, 0, &max);
606 for (i = 0; i < max; i++) {
607 if (!mb_test_bit(i, buddy)) {
608 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
609 if (fstart == -1) {
610 fragments++;
611 fstart = i;
612 }
613 continue;
614 }
615 fstart = -1;
616 /* check used bits only */
617 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
618 buddy2 = mb_find_buddy(e4b, j, &max2);
619 k = i >> j;
620 MB_CHECK_ASSERT(k < max2);
621 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
622 }
623 }
624 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
625 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
626
627 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500628 list_for_each(cur, &grp->bb_prealloc_list) {
629 ext4_group_t groupnr;
630 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400631 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
632 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500633 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400634 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500635 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
636 }
637 return 0;
638}
639#undef MB_CHECK_ASSERT
640#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400641 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500642#else
643#define mb_check_buddy(e4b)
644#endif
645
Coly Li7c786052011-02-24 13:24:25 -0500646/*
647 * Divide blocks started from @first with length @len into
648 * smaller chunks with power of 2 blocks.
649 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
650 * then increase bb_counters[] for corresponded chunk size.
651 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500652static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400653 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500654 struct ext4_group_info *grp)
655{
656 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400657 ext4_grpblk_t min;
658 ext4_grpblk_t max;
659 ext4_grpblk_t chunk;
Chandan Rajendra69e43e82016-11-14 21:04:37 -0500660 unsigned int border;
Alex Tomasc9de5602008-01-29 00:19:52 -0500661
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400662 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500663
664 border = 2 << sb->s_blocksize_bits;
665
666 while (len > 0) {
667 /* find how many blocks can be covered since this position */
668 max = ffs(first | border) - 1;
669
670 /* find how many blocks of power 2 we need to mark */
671 min = fls(len) - 1;
672
673 if (max < min)
674 min = max;
675 chunk = 1 << min;
676
677 /* mark multiblock chunks only */
678 grp->bb_counters[min]++;
679 if (min > 0)
680 mb_clear_bit(first >> min,
681 buddy + sbi->s_mb_offsets[min]);
682
683 len -= chunk;
684 first += chunk;
685 }
686}
687
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400688/*
689 * Cache the order of the largest free extent we have available in this block
690 * group.
691 */
692static void
693mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
694{
695 int i;
696 int bits;
697
698 grp->bb_largest_free_order = -1; /* uninit */
699
700 bits = sb->s_blocksize_bits + 1;
701 for (i = bits; i >= 0; i--) {
702 if (grp->bb_counters[i] > 0) {
703 grp->bb_largest_free_order = i;
704 break;
705 }
706 }
707}
708
Eric Sandeen089ceec2009-07-05 22:17:31 -0400709static noinline_for_stack
710void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500711 void *buddy, void *bitmap, ext4_group_t group)
712{
713 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400714 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400715 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400716 ext4_grpblk_t i = 0;
717 ext4_grpblk_t first;
718 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500719 unsigned free = 0;
720 unsigned fragments = 0;
721 unsigned long long period = get_cycles();
722
723 /* initialize buddy from bitmap which is aggregation
724 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500725 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500726 grp->bb_first_free = i;
727 while (i < max) {
728 fragments++;
729 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500730 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500731 len = i - first;
732 free += len;
733 if (len > 1)
734 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
735 else
736 grp->bb_counters[0]++;
737 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500738 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500739 }
740 grp->bb_fragments = fragments;
741
742 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400743 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o94d4c062014-07-05 19:15:50 -0400744 "block bitmap and bg descriptor "
745 "inconsistent: %u vs %u free clusters",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400746 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500747 /*
Darrick J. Wong163a2032013-08-28 17:35:51 -0400748 * If we intend to continue, we consider group descriptor
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500749 * corrupt and update bb_free using bitmap value
750 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500751 grp->bb_free = free;
Wang Shilongdb79e6d2018-05-12 11:39:40 -0400752 ext4_mark_group_bitmap_corrupted(sb, group,
753 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Alex Tomasc9de5602008-01-29 00:19:52 -0500754 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400755 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500756
757 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
758
759 period = get_cycles() - period;
Jun Piao49598e02018-01-11 13:17:49 -0500760 spin_lock(&sbi->s_bal_lock);
761 sbi->s_mb_buddies_generated++;
762 sbi->s_mb_generation_time += period;
763 spin_unlock(&sbi->s_bal_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -0500764}
765
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400766static void mb_regenerate_buddy(struct ext4_buddy *e4b)
767{
768 int count;
769 int order = 1;
770 void *buddy;
771
772 while ((buddy = mb_find_buddy(e4b, order++, &count))) {
773 ext4_set_bits(buddy, 0, count);
774 }
775 e4b->bd_info->bb_fragments = 0;
776 memset(e4b->bd_info->bb_counters, 0,
777 sizeof(*e4b->bd_info->bb_counters) *
778 (e4b->bd_sb->s_blocksize_bits + 2));
779
780 ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy,
781 e4b->bd_bitmap, e4b->bd_group);
782}
783
Alex Tomasc9de5602008-01-29 00:19:52 -0500784/* The buddy information is attached the buddy cache inode
785 * for convenience. The information regarding each group
786 * is loaded via ext4_mb_load_buddy. The information involve
787 * block bitmap and buddy information. The information are
788 * stored in the inode as
789 *
790 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500791 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500792 *
793 *
794 * one block each for bitmap and buddy information.
795 * So for each group we take up 2 blocks. A page can
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300796 * contain blocks_per_page (PAGE_SIZE / blocksize) blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -0500797 * So it can have information regarding groups_per_page which
798 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400799 *
800 * Locking note: This routine takes the block group lock of all groups
801 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500802 */
803
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400804static int ext4_mb_init_cache(struct page *page, char *incore, gfp_t gfp)
Alex Tomasc9de5602008-01-29 00:19:52 -0500805{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400806 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500807 int blocksize;
808 int blocks_per_page;
809 int groups_per_page;
810 int err = 0;
811 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500812 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500813 int first_block;
814 struct super_block *sb;
815 struct buffer_head *bhs;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -0400816 struct buffer_head **bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500817 struct inode *inode;
818 char *data;
819 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400820 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500821
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400822 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500823
824 inode = page->mapping->host;
825 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400826 ngroups = ext4_get_groups_count(sb);
Fabian Frederick93407472017-02-27 14:28:32 -0800827 blocksize = i_blocksize(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300828 blocks_per_page = PAGE_SIZE / blocksize;
Alex Tomasc9de5602008-01-29 00:19:52 -0500829
830 groups_per_page = blocks_per_page >> 1;
831 if (groups_per_page == 0)
832 groups_per_page = 1;
833
834 /* allocate buffer_heads to read bitmaps */
835 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500836 i = sizeof(struct buffer_head *) * groups_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400837 bh = kzalloc(i, gfp);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500838 if (bh == NULL) {
839 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500840 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500841 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500842 } else
843 bh = &bhs;
844
845 first_group = page->index * blocks_per_page / 2;
846
847 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500848 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
849 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500850 break;
851
Theodore Ts'o813e5722012-02-20 17:52:46 -0500852 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400853 /*
854 * If page is uptodate then we came here after online resize
855 * which added some new uninitialized group info structs, so
856 * we must skip all initialized uptodate buddies on the page,
857 * which may be currently in use by an allocating task.
858 */
859 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
860 bh[i] = NULL;
861 continue;
862 }
Darrick J. Wong9008a582015-10-17 21:33:24 -0400863 bh[i] = ext4_read_block_bitmap_nowait(sb, group);
864 if (IS_ERR(bh[i])) {
865 err = PTR_ERR(bh[i]);
866 bh[i] = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500867 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500868 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500869 mb_debug(1, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500870 }
871
872 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500873 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
Darrick J. Wong9008a582015-10-17 21:33:24 -0400874 int err2;
875
876 if (!bh[i])
877 continue;
878 err2 = ext4_wait_block_bitmap(sb, group, bh[i]);
879 if (!err)
880 err = err2;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500881 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500882
883 first_block = page->index * blocks_per_page;
884 for (i = 0; i < blocks_per_page; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500885 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400886 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500887 break;
888
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400889 if (!bh[group - first_group])
890 /* skip initialized uptodate buddy */
891 continue;
892
Lukas Czernerbbdc3222015-06-08 11:38:37 -0400893 if (!buffer_verified(bh[group - first_group]))
894 /* Skip faulty bitmaps */
895 continue;
896 err = 0;
897
Alex Tomasc9de5602008-01-29 00:19:52 -0500898 /*
899 * data carry information regarding this
900 * particular group in the format specified
901 * above
902 *
903 */
904 data = page_address(page) + (i * blocksize);
905 bitmap = bh[group - first_group]->b_data;
906
907 /*
908 * We place the buddy block and bitmap block
909 * close together
910 */
911 if ((first_block + i) & 1) {
912 /* this is block of buddy */
913 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400914 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500915 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400916 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500917 grinfo = ext4_get_group_info(sb, group);
918 grinfo->bb_fragments = 0;
919 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400920 sizeof(*grinfo->bb_counters) *
921 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500922 /*
923 * incore got set to the group block bitmap below
924 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500925 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400926 /* init the buddy */
927 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500928 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500929 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500930 incore = NULL;
931 } else {
932 /* this is block of bitmap */
933 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400934 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500935 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400936 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500937
938 /* see comments in ext4_mb_put_pa() */
939 ext4_lock_group(sb, group);
940 memcpy(data, bitmap, blocksize);
941
942 /* mark all preallocated blks used in in-core bitmap */
943 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500944 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500945 ext4_unlock_group(sb, group);
946
947 /* set incore so that the buddy information can be
948 * generated using this
949 */
950 incore = data;
951 }
952 }
953 SetPageUptodate(page);
954
955out:
956 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400957 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500958 brelse(bh[i]);
959 if (bh != &bhs)
960 kfree(bh);
961 }
962 return err;
963}
964
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400965/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400966 * Lock the buddy and bitmap pages. This make sure other parallel init_group
967 * on the same buddy page doesn't happen whild holding the buddy page lock.
968 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
969 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400970 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400971static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400972 ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400973{
Amir Goldstein2de88072011-05-09 21:48:13 -0400974 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
975 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400976 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400977 struct page *page;
978
979 e4b->bd_buddy_page = NULL;
980 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400981
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300982 blocks_per_page = PAGE_SIZE / sb->s_blocksize;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400983 /*
984 * the buddy cache inode stores the block bitmap
985 * and buddy information in consecutive blocks.
986 * So for each group we need two blocks.
987 */
988 block = group * 2;
989 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400990 poff = block % blocks_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400991 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -0400992 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -0400993 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -0400994 BUG_ON(page->mapping != inode->i_mapping);
995 e4b->bd_bitmap_page = page;
996 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400997
Amir Goldstein2de88072011-05-09 21:48:13 -0400998 if (blocks_per_page >= 2) {
999 /* buddy and bitmap are on the same page */
1000 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001001 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001002
1003 block++;
1004 pnum = block / blocks_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001005 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001006 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001007 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001008 BUG_ON(page->mapping != inode->i_mapping);
1009 e4b->bd_buddy_page = page;
1010 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001011}
1012
Amir Goldstein2de88072011-05-09 21:48:13 -04001013static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001014{
Amir Goldstein2de88072011-05-09 21:48:13 -04001015 if (e4b->bd_bitmap_page) {
1016 unlock_page(e4b->bd_bitmap_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001017 put_page(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001018 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001019 if (e4b->bd_buddy_page) {
1020 unlock_page(e4b->bd_buddy_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001021 put_page(e4b->bd_buddy_page);
Amir Goldstein2de88072011-05-09 21:48:13 -04001022 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001023}
1024
1025/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001026 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1027 * block group lock of all groups for this page; do not hold the BG lock when
1028 * calling this routine!
1029 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001030static noinline_for_stack
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001031int ext4_mb_init_group(struct super_block *sb, ext4_group_t group, gfp_t gfp)
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001032{
1033
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001034 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001035 struct ext4_buddy e4b;
1036 struct page *page;
1037 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001038
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001039 might_sleep();
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001040 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001041 this_grp = ext4_get_group_info(sb, group);
1042 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001043 * This ensures that we don't reinit the buddy cache
1044 * page which map to the group from which we are already
1045 * allocating. If we are looking at the buddy cache we would
1046 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001047 * would have pinned buddy page to page cache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001048 * The call to ext4_mb_get_buddy_page_lock will mark the
1049 * page accessed.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001050 */
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001051 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001052 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001053 /*
1054 * somebody initialized the group
1055 * return without doing anything
1056 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001057 goto err;
1058 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001059
1060 page = e4b.bd_bitmap_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001061 ret = ext4_mb_init_cache(page, NULL, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001062 if (ret)
1063 goto err;
1064 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001065 ret = -EIO;
1066 goto err;
1067 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001068
Amir Goldstein2de88072011-05-09 21:48:13 -04001069 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001070 /*
1071 * If both the bitmap and buddy are in
1072 * the same page we don't need to force
1073 * init the buddy
1074 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001075 ret = 0;
1076 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001077 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001078 /* init buddy cache */
1079 page = e4b.bd_buddy_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001080 ret = ext4_mb_init_cache(page, e4b.bd_bitmap, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001081 if (ret)
1082 goto err;
1083 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001084 ret = -EIO;
1085 goto err;
1086 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001087err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001088 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001089 return ret;
1090}
1091
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001092/*
1093 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1094 * block group lock of all groups for this page; do not hold the BG lock when
1095 * calling this routine!
1096 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001097static noinline_for_stack int
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001098ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group,
1099 struct ext4_buddy *e4b, gfp_t gfp)
Alex Tomasc9de5602008-01-29 00:19:52 -05001100{
Alex Tomasc9de5602008-01-29 00:19:52 -05001101 int blocks_per_page;
1102 int block;
1103 int pnum;
1104 int poff;
1105 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001106 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001107 struct ext4_group_info *grp;
1108 struct ext4_sb_info *sbi = EXT4_SB(sb);
1109 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001110
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001111 might_sleep();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001112 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001113
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001114 blocks_per_page = PAGE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001115 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001116
1117 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001118 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001119 e4b->bd_sb = sb;
1120 e4b->bd_group = group;
1121 e4b->bd_buddy_page = NULL;
1122 e4b->bd_bitmap_page = NULL;
1123
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001124 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001125 /*
1126 * we need full data about the group
1127 * to make a good selection
1128 */
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001129 ret = ext4_mb_init_group(sb, group, gfp);
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001130 if (ret)
1131 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001132 }
1133
Alex Tomasc9de5602008-01-29 00:19:52 -05001134 /*
1135 * the buddy cache inode stores the block bitmap
1136 * and buddy information in consecutive blocks.
1137 * So for each group we need two blocks.
1138 */
1139 block = group * 2;
1140 pnum = block / blocks_per_page;
1141 poff = block % blocks_per_page;
1142
1143 /* we could use find_or_create_page(), but it locks page
1144 * what we'd like to avoid in fast path ... */
Mel Gorman2457aec2014-06-04 16:10:31 -07001145 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001146 if (page == NULL || !PageUptodate(page)) {
1147 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001148 /*
1149 * drop the page reference and try
1150 * to get the page with lock. If we
1151 * are not uptodate that implies
1152 * somebody just created the page but
1153 * is yet to initialize the same. So
1154 * wait for it to initialize.
1155 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001156 put_page(page);
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001157 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001158 if (page) {
1159 BUG_ON(page->mapping != inode->i_mapping);
1160 if (!PageUptodate(page)) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001161 ret = ext4_mb_init_cache(page, NULL, gfp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001162 if (ret) {
1163 unlock_page(page);
1164 goto err;
1165 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001166 mb_cmp_bitmaps(e4b, page_address(page) +
1167 (poff * sb->s_blocksize));
1168 }
1169 unlock_page(page);
1170 }
1171 }
Younger Liuc57ab392014-04-10 23:03:43 -04001172 if (page == NULL) {
1173 ret = -ENOMEM;
1174 goto err;
1175 }
1176 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001177 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001178 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001179 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001180
1181 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001182 e4b->bd_bitmap_page = page;
1183 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001184
1185 block++;
1186 pnum = block / blocks_per_page;
1187 poff = block % blocks_per_page;
1188
Mel Gorman2457aec2014-06-04 16:10:31 -07001189 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001190 if (page == NULL || !PageUptodate(page)) {
1191 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001192 put_page(page);
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001193 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001194 if (page) {
1195 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001196 if (!PageUptodate(page)) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001197 ret = ext4_mb_init_cache(page, e4b->bd_bitmap,
1198 gfp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001199 if (ret) {
1200 unlock_page(page);
1201 goto err;
1202 }
1203 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001204 unlock_page(page);
1205 }
1206 }
Younger Liuc57ab392014-04-10 23:03:43 -04001207 if (page == NULL) {
1208 ret = -ENOMEM;
1209 goto err;
1210 }
1211 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001212 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001213 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001214 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001215
1216 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001217 e4b->bd_buddy_page = page;
1218 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001219
1220 BUG_ON(e4b->bd_bitmap_page == NULL);
1221 BUG_ON(e4b->bd_buddy_page == NULL);
1222
1223 return 0;
1224
1225err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001226 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001227 put_page(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001228 if (e4b->bd_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001229 put_page(e4b->bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001230 if (e4b->bd_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001231 put_page(e4b->bd_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001232 e4b->bd_buddy = NULL;
1233 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001234 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001235}
1236
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001237static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1238 struct ext4_buddy *e4b)
1239{
1240 return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS);
1241}
1242
Jing Zhange39e07f2010-05-14 00:00:00 -04001243static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001244{
1245 if (e4b->bd_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001246 put_page(e4b->bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001247 if (e4b->bd_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001248 put_page(e4b->bd_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001249}
1250
1251
1252static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1253{
1254 int order = 1;
Nicolai Stangeb5cb3162016-05-05 17:38:03 -04001255 int bb_incr = 1 << (e4b->bd_blkbits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05001256 void *bb;
1257
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001258 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -05001259 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1260
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001261 bb = e4b->bd_buddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05001262 while (order <= e4b->bd_blkbits + 1) {
1263 block = block >> 1;
1264 if (!mb_test_bit(block, bb)) {
1265 /* this block is part of buddy of order 'order' */
1266 return order;
1267 }
Nicolai Stangeb5cb3162016-05-05 17:38:03 -04001268 bb += bb_incr;
1269 bb_incr >>= 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001270 order++;
1271 }
1272 return 0;
1273}
1274
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001275static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001276{
1277 __u32 *addr;
1278
1279 len = cur + len;
1280 while (cur < len) {
1281 if ((cur & 31) == 0 && (len - cur) >= 32) {
1282 /* fast path: clear whole word at once */
1283 addr = bm + (cur >> 3);
1284 *addr = 0;
1285 cur += 32;
1286 continue;
1287 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001288 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001289 cur++;
1290 }
1291}
1292
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001293/* clear bits in given range
1294 * will return first found zero bit if any, -1 otherwise
1295 */
1296static int mb_test_and_clear_bits(void *bm, int cur, int len)
1297{
1298 __u32 *addr;
1299 int zero_bit = -1;
1300
1301 len = cur + len;
1302 while (cur < len) {
1303 if ((cur & 31) == 0 && (len - cur) >= 32) {
1304 /* fast path: clear whole word at once */
1305 addr = bm + (cur >> 3);
1306 if (*addr != (__u32)(-1) && zero_bit == -1)
1307 zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0);
1308 *addr = 0;
1309 cur += 32;
1310 continue;
1311 }
1312 if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1)
1313 zero_bit = cur;
1314 cur++;
1315 }
1316
1317 return zero_bit;
1318}
1319
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001320void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001321{
1322 __u32 *addr;
1323
1324 len = cur + len;
1325 while (cur < len) {
1326 if ((cur & 31) == 0 && (len - cur) >= 32) {
1327 /* fast path: set whole word at once */
1328 addr = bm + (cur >> 3);
1329 *addr = 0xffffffff;
1330 cur += 32;
1331 continue;
1332 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001333 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001334 cur++;
1335 }
1336}
1337
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001338/*
1339 * _________________________________________________________________ */
1340
1341static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side)
Alex Tomasc9de5602008-01-29 00:19:52 -05001342{
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001343 if (mb_test_bit(*bit + side, bitmap)) {
1344 mb_clear_bit(*bit, bitmap);
1345 (*bit) -= side;
1346 return 1;
1347 }
1348 else {
1349 (*bit) += side;
1350 mb_set_bit(*bit, bitmap);
1351 return -1;
1352 }
1353}
1354
1355static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last)
1356{
1357 int max;
1358 int order = 1;
1359 void *buddy = mb_find_buddy(e4b, order, &max);
1360
1361 while (buddy) {
1362 void *buddy2;
1363
1364 /* Bits in range [first; last] are known to be set since
1365 * corresponding blocks were allocated. Bits in range
1366 * (first; last) will stay set because they form buddies on
1367 * upper layer. We just deal with borders if they don't
1368 * align with upper layer and then go up.
1369 * Releasing entire group is all about clearing
1370 * single bit of highest order buddy.
1371 */
1372
1373 /* Example:
1374 * ---------------------------------
1375 * | 1 | 1 | 1 | 1 |
1376 * ---------------------------------
1377 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1378 * ---------------------------------
1379 * 0 1 2 3 4 5 6 7
1380 * \_____________________/
1381 *
1382 * Neither [1] nor [6] is aligned to above layer.
1383 * Left neighbour [0] is free, so mark it busy,
1384 * decrease bb_counters and extend range to
1385 * [0; 6]
1386 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1387 * mark [6] free, increase bb_counters and shrink range to
1388 * [0; 5].
1389 * Then shift range to [0; 2], go up and do the same.
1390 */
1391
1392
1393 if (first & 1)
1394 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1);
1395 if (!(last & 1))
1396 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1);
1397 if (first > last)
1398 break;
1399 order++;
1400
1401 if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) {
1402 mb_clear_bits(buddy, first, last - first + 1);
1403 e4b->bd_info->bb_counters[order - 1] += last - first + 1;
1404 break;
1405 }
1406 first >>= 1;
1407 last >>= 1;
1408 buddy = buddy2;
1409 }
1410}
1411
1412static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1413 int first, int count)
1414{
1415 int left_is_free = 0;
1416 int right_is_free = 0;
1417 int block;
1418 int last = first + count - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001419 struct super_block *sb = e4b->bd_sb;
1420
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04001421 if (WARN_ON(count == 0))
1422 return;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001423 BUG_ON(last >= (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001424 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Darrick J. Wong163a2032013-08-28 17:35:51 -04001425 /* Don't bother if the block group is corrupt. */
1426 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
1427 return;
1428
Alex Tomasc9de5602008-01-29 00:19:52 -05001429 mb_check_buddy(e4b);
1430 mb_free_blocks_double(inode, e4b, first, count);
1431
1432 e4b->bd_info->bb_free += count;
1433 if (first < e4b->bd_info->bb_first_free)
1434 e4b->bd_info->bb_first_free = first;
1435
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001436 /* access memory sequentially: check left neighbour,
1437 * clear range and then check right neighbour
1438 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001439 if (first != 0)
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001440 left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap);
1441 block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count);
1442 if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0])
1443 right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap);
1444
1445 if (unlikely(block != -1)) {
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001446 struct ext4_sb_info *sbi = EXT4_SB(sb);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001447 ext4_fsblk_t blocknr;
1448
1449 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Jun Piao49598e02018-01-11 13:17:49 -05001450 blocknr += EXT4_C2B(sbi, block);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001451 ext4_grp_locked_error(sb, e4b->bd_group,
1452 inode ? inode->i_ino : 0,
1453 blocknr,
1454 "freeing already freed block "
Darrick J. Wong163a2032013-08-28 17:35:51 -04001455 "(bit %u); block bitmap corrupt.",
1456 block);
Wang Shilongdb79e6d2018-05-12 11:39:40 -04001457 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
1458 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001459 mb_regenerate_buddy(e4b);
1460 goto done;
1461 }
1462
1463 /* let's maintain fragments counter */
1464 if (left_is_free && right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001465 e4b->bd_info->bb_fragments--;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001466 else if (!left_is_free && !right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001467 e4b->bd_info->bb_fragments++;
1468
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001469 /* buddy[0] == bd_bitmap is a special case, so handle
1470 * it right away and let mb_buddy_mark_free stay free of
1471 * zero order checks.
1472 * Check if neighbours are to be coaleasced,
1473 * adjust bitmap bb_counters and borders appropriately.
1474 */
1475 if (first & 1) {
1476 first += !left_is_free;
1477 e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001478 }
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001479 if (!(last & 1)) {
1480 last -= !right_is_free;
1481 e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1;
1482 }
1483
1484 if (first <= last)
1485 mb_buddy_mark_free(e4b, first >> 1, last >> 1);
1486
1487done:
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001488 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001489 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001490}
1491
Robin Dong15c006a2012-08-17 10:02:17 -04001492static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001493 int needed, struct ext4_free_extent *ex)
1494{
1495 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001496 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001497 void *buddy;
1498
Vincent Minetbc8e6742009-05-15 08:33:18 -04001499 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001500 BUG_ON(ex == NULL);
1501
Robin Dong15c006a2012-08-17 10:02:17 -04001502 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001503 BUG_ON(buddy == NULL);
1504 BUG_ON(block >= max);
1505 if (mb_test_bit(block, buddy)) {
1506 ex->fe_len = 0;
1507 ex->fe_start = 0;
1508 ex->fe_group = 0;
1509 return 0;
1510 }
1511
Robin Dong15c006a2012-08-17 10:02:17 -04001512 /* find actual order */
1513 order = mb_find_order_for_block(e4b, block);
1514 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001515
1516 ex->fe_len = 1 << order;
1517 ex->fe_start = block << order;
1518 ex->fe_group = e4b->bd_group;
1519
1520 /* calc difference from given start */
1521 next = next - ex->fe_start;
1522 ex->fe_len -= next;
1523 ex->fe_start += next;
1524
1525 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001526 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001527
1528 if (block + 1 >= max)
1529 break;
1530
1531 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001532 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001533 break;
1534
Robin Dongb051d8d2011-10-26 05:30:30 -04001535 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001536
Alex Tomasc9de5602008-01-29 00:19:52 -05001537 block = next >> order;
1538 ex->fe_len += 1 << order;
1539 }
1540
Theodore Ts'o43c73222017-01-22 19:35:52 -05001541 if (ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3))) {
1542 /* Should never happen! (but apparently sometimes does?!?) */
1543 WARN_ON(1);
1544 ext4_error(e4b->bd_sb, "corruption or bug in mb_find_extent "
1545 "block=%d, order=%d needed=%d ex=%u/%d/%d@%u",
1546 block, order, needed, ex->fe_group, ex->fe_start,
1547 ex->fe_len, ex->fe_logical);
1548 ex->fe_len = 0;
1549 ex->fe_start = 0;
1550 ex->fe_group = 0;
1551 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001552 return ex->fe_len;
1553}
1554
1555static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1556{
1557 int ord;
1558 int mlen = 0;
1559 int max = 0;
1560 int cur;
1561 int start = ex->fe_start;
1562 int len = ex->fe_len;
1563 unsigned ret = 0;
1564 int len0 = len;
1565 void *buddy;
1566
1567 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1568 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001569 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001570 mb_check_buddy(e4b);
1571 mb_mark_used_double(e4b, start, len);
1572
1573 e4b->bd_info->bb_free -= len;
1574 if (e4b->bd_info->bb_first_free == start)
1575 e4b->bd_info->bb_first_free += len;
1576
1577 /* let's maintain fragments counter */
1578 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001579 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001580 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001581 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001582 if (mlen && max)
1583 e4b->bd_info->bb_fragments++;
1584 else if (!mlen && !max)
1585 e4b->bd_info->bb_fragments--;
1586
1587 /* let's maintain buddy itself */
1588 while (len) {
1589 ord = mb_find_order_for_block(e4b, start);
1590
1591 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1592 /* the whole chunk may be allocated at once! */
1593 mlen = 1 << ord;
1594 buddy = mb_find_buddy(e4b, ord, &max);
1595 BUG_ON((start >> ord) >= max);
1596 mb_set_bit(start >> ord, buddy);
1597 e4b->bd_info->bb_counters[ord]--;
1598 start += mlen;
1599 len -= mlen;
1600 BUG_ON(len < 0);
1601 continue;
1602 }
1603
1604 /* store for history */
1605 if (ret == 0)
1606 ret = len | (ord << 16);
1607
1608 /* we have to split large buddy */
1609 BUG_ON(ord <= 0);
1610 buddy = mb_find_buddy(e4b, ord, &max);
1611 mb_set_bit(start >> ord, buddy);
1612 e4b->bd_info->bb_counters[ord]--;
1613
1614 ord--;
1615 cur = (start >> ord) & ~1U;
1616 buddy = mb_find_buddy(e4b, ord, &max);
1617 mb_clear_bit(cur, buddy);
1618 mb_clear_bit(cur + 1, buddy);
1619 e4b->bd_info->bb_counters[ord]++;
1620 e4b->bd_info->bb_counters[ord]++;
1621 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001622 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001623
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001624 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001625 mb_check_buddy(e4b);
1626
1627 return ret;
1628}
1629
1630/*
1631 * Must be called under group lock!
1632 */
1633static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1634 struct ext4_buddy *e4b)
1635{
1636 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1637 int ret;
1638
1639 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1640 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1641
1642 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1643 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1644 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1645
1646 /* preallocation can change ac_b_ex, thus we store actually
1647 * allocated blocks for history */
1648 ac->ac_f_ex = ac->ac_b_ex;
1649
1650 ac->ac_status = AC_STATUS_FOUND;
1651 ac->ac_tail = ret & 0xffff;
1652 ac->ac_buddy = ret >> 16;
1653
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001654 /*
1655 * take the page reference. We want the page to be pinned
1656 * so that we don't get a ext4_mb_init_cache_call for this
1657 * group until we update the bitmap. That would mean we
1658 * double allocate blocks. The reference is dropped
1659 * in ext4_mb_release_context
1660 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001661 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1662 get_page(ac->ac_bitmap_page);
1663 ac->ac_buddy_page = e4b->bd_buddy_page;
1664 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001665 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001666 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001667 spin_lock(&sbi->s_md_lock);
1668 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1669 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1670 spin_unlock(&sbi->s_md_lock);
1671 }
1672}
1673
1674/*
1675 * regular allocator, for general purposes allocation
1676 */
1677
1678static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1679 struct ext4_buddy *e4b,
1680 int finish_group)
1681{
1682 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1683 struct ext4_free_extent *bex = &ac->ac_b_ex;
1684 struct ext4_free_extent *gex = &ac->ac_g_ex;
1685 struct ext4_free_extent ex;
1686 int max;
1687
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001688 if (ac->ac_status == AC_STATUS_FOUND)
1689 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001690 /*
1691 * We don't want to scan for a whole year
1692 */
1693 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1694 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1695 ac->ac_status = AC_STATUS_BREAK;
1696 return;
1697 }
1698
1699 /*
1700 * Haven't found good chunk so far, let's continue
1701 */
1702 if (bex->fe_len < gex->fe_len)
1703 return;
1704
1705 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1706 && bex->fe_group == e4b->bd_group) {
1707 /* recheck chunk's availability - we don't know
1708 * when it was found (within this lock-unlock
1709 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001710 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001711 if (max >= gex->fe_len) {
1712 ext4_mb_use_best_found(ac, e4b);
1713 return;
1714 }
1715 }
1716}
1717
1718/*
1719 * The routine checks whether found extent is good enough. If it is,
1720 * then the extent gets marked used and flag is set to the context
1721 * to stop scanning. Otherwise, the extent is compared with the
1722 * previous found extent and if new one is better, then it's stored
1723 * in the context. Later, the best found extent will be used, if
1724 * mballoc can't find good enough extent.
1725 *
1726 * FIXME: real allocation policy is to be designed yet!
1727 */
1728static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1729 struct ext4_free_extent *ex,
1730 struct ext4_buddy *e4b)
1731{
1732 struct ext4_free_extent *bex = &ac->ac_b_ex;
1733 struct ext4_free_extent *gex = &ac->ac_g_ex;
1734
1735 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001736 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1737 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001738 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1739
1740 ac->ac_found++;
1741
1742 /*
1743 * The special case - take what you catch first
1744 */
1745 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1746 *bex = *ex;
1747 ext4_mb_use_best_found(ac, e4b);
1748 return;
1749 }
1750
1751 /*
1752 * Let's check whether the chuck is good enough
1753 */
1754 if (ex->fe_len == gex->fe_len) {
1755 *bex = *ex;
1756 ext4_mb_use_best_found(ac, e4b);
1757 return;
1758 }
1759
1760 /*
1761 * If this is first found extent, just store it in the context
1762 */
1763 if (bex->fe_len == 0) {
1764 *bex = *ex;
1765 return;
1766 }
1767
1768 /*
1769 * If new found extent is better, store it in the context
1770 */
1771 if (bex->fe_len < gex->fe_len) {
1772 /* if the request isn't satisfied, any found extent
1773 * larger than previous best one is better */
1774 if (ex->fe_len > bex->fe_len)
1775 *bex = *ex;
1776 } else if (ex->fe_len > gex->fe_len) {
1777 /* if the request is satisfied, then we try to find
1778 * an extent that still satisfy the request, but is
1779 * smaller than previous one */
1780 if (ex->fe_len < bex->fe_len)
1781 *bex = *ex;
1782 }
1783
1784 ext4_mb_check_limits(ac, e4b, 0);
1785}
1786
Eric Sandeen089ceec2009-07-05 22:17:31 -04001787static noinline_for_stack
1788int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001789 struct ext4_buddy *e4b)
1790{
1791 struct ext4_free_extent ex = ac->ac_b_ex;
1792 ext4_group_t group = ex.fe_group;
1793 int max;
1794 int err;
1795
1796 BUG_ON(ex.fe_len <= 0);
1797 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1798 if (err)
1799 return err;
1800
1801 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001802 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001803
1804 if (max > 0) {
1805 ac->ac_b_ex = ex;
1806 ext4_mb_use_best_found(ac, e4b);
1807 }
1808
1809 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001810 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001811
1812 return 0;
1813}
1814
Eric Sandeen089ceec2009-07-05 22:17:31 -04001815static noinline_for_stack
1816int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001817 struct ext4_buddy *e4b)
1818{
1819 ext4_group_t group = ac->ac_g_ex.fe_group;
1820 int max;
1821 int err;
1822 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001823 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001824 struct ext4_free_extent ex;
1825
1826 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1827 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001828 if (grp->bb_free == 0)
1829 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001830
1831 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1832 if (err)
1833 return err;
1834
Darrick J. Wong163a2032013-08-28 17:35:51 -04001835 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) {
1836 ext4_mb_unload_buddy(e4b);
1837 return 0;
1838 }
1839
Alex Tomasc9de5602008-01-29 00:19:52 -05001840 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001841 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001842 ac->ac_g_ex.fe_len, &ex);
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001843 ex.fe_logical = 0xDEADFA11; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001844
1845 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1846 ext4_fsblk_t start;
1847
Akinobu Mita5661bd62010-03-03 23:53:39 -05001848 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1849 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001850 /* use do_div to get remainder (would be 64-bit modulo) */
1851 if (do_div(start, sbi->s_stripe) == 0) {
1852 ac->ac_found++;
1853 ac->ac_b_ex = ex;
1854 ext4_mb_use_best_found(ac, e4b);
1855 }
1856 } else if (max >= ac->ac_g_ex.fe_len) {
1857 BUG_ON(ex.fe_len <= 0);
1858 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1859 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1860 ac->ac_found++;
1861 ac->ac_b_ex = ex;
1862 ext4_mb_use_best_found(ac, e4b);
1863 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1864 /* Sometimes, caller may want to merge even small
1865 * number of blocks to an existing extent */
1866 BUG_ON(ex.fe_len <= 0);
1867 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1868 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1869 ac->ac_found++;
1870 ac->ac_b_ex = ex;
1871 ext4_mb_use_best_found(ac, e4b);
1872 }
1873 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001874 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001875
1876 return 0;
1877}
1878
1879/*
1880 * The routine scans buddy structures (not bitmap!) from given order
1881 * to max order and tries to find big enough chunk to satisfy the req
1882 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001883static noinline_for_stack
1884void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001885 struct ext4_buddy *e4b)
1886{
1887 struct super_block *sb = ac->ac_sb;
1888 struct ext4_group_info *grp = e4b->bd_info;
1889 void *buddy;
1890 int i;
1891 int k;
1892 int max;
1893
1894 BUG_ON(ac->ac_2order <= 0);
1895 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1896 if (grp->bb_counters[i] == 0)
1897 continue;
1898
1899 buddy = mb_find_buddy(e4b, i, &max);
1900 BUG_ON(buddy == NULL);
1901
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001902 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001903 BUG_ON(k >= max);
1904
1905 ac->ac_found++;
1906
1907 ac->ac_b_ex.fe_len = 1 << i;
1908 ac->ac_b_ex.fe_start = k << i;
1909 ac->ac_b_ex.fe_group = e4b->bd_group;
1910
1911 ext4_mb_use_best_found(ac, e4b);
1912
1913 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1914
1915 if (EXT4_SB(sb)->s_mb_stats)
1916 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1917
1918 break;
1919 }
1920}
1921
1922/*
1923 * The routine scans the group and measures all found extents.
1924 * In order to optimize scanning, caller must pass number of
1925 * free blocks in the group, so the routine can know upper limit.
1926 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001927static noinline_for_stack
1928void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001929 struct ext4_buddy *e4b)
1930{
1931 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001932 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001933 struct ext4_free_extent ex;
1934 int i;
1935 int free;
1936
1937 free = e4b->bd_info->bb_free;
1938 BUG_ON(free <= 0);
1939
1940 i = e4b->bd_info->bb_first_free;
1941
1942 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001943 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001944 EXT4_CLUSTERS_PER_GROUP(sb), i);
1945 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001946 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001947 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001948 * free blocks even though group info says we
1949 * we have free blocks
1950 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001951 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001952 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001953 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001954 free);
Wang Shilong736dedb2018-05-12 12:37:58 -04001955 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
1956 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Alex Tomasc9de5602008-01-29 00:19:52 -05001957 break;
1958 }
1959
Robin Dong15c006a2012-08-17 10:02:17 -04001960 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001961 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001962 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001963 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001964 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001965 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001966 free, ex.fe_len);
Wang Shilong736dedb2018-05-12 12:37:58 -04001967 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
1968 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001969 /*
1970 * The number of free blocks differs. This mostly
1971 * indicate that the bitmap is corrupt. So exit
1972 * without claiming the space.
1973 */
1974 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001975 }
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001976 ex.fe_logical = 0xDEADC0DE; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001977 ext4_mb_measure_extent(ac, &ex, e4b);
1978
1979 i += ex.fe_len;
1980 free -= ex.fe_len;
1981 }
1982
1983 ext4_mb_check_limits(ac, e4b, 1);
1984}
1985
1986/*
1987 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001988 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001989 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001990static noinline_for_stack
1991void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001992 struct ext4_buddy *e4b)
1993{
1994 struct super_block *sb = ac->ac_sb;
1995 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001996 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001997 struct ext4_free_extent ex;
1998 ext4_fsblk_t first_group_block;
1999 ext4_fsblk_t a;
2000 ext4_grpblk_t i;
2001 int max;
2002
2003 BUG_ON(sbi->s_stripe == 0);
2004
2005 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05002006 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
2007
Alex Tomasc9de5602008-01-29 00:19:52 -05002008 a = first_group_block + sbi->s_stripe - 1;
2009 do_div(a, sbi->s_stripe);
2010 i = (a * sbi->s_stripe) - first_group_block;
2011
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04002012 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002013 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04002014 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002015 if (max >= sbi->s_stripe) {
2016 ac->ac_found++;
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002017 ex.fe_logical = 0xDEADF00D; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002018 ac->ac_b_ex = ex;
2019 ext4_mb_use_best_found(ac, e4b);
2020 break;
2021 }
2022 }
2023 i += sbi->s_stripe;
2024 }
2025}
2026
Lukas Czerner42ac1842015-06-08 11:40:40 -04002027/*
2028 * This is now called BEFORE we load the buddy bitmap.
2029 * Returns either 1 or 0 indicating that the group is either suitable
2030 * for the allocation or not. In addition it can also return negative
2031 * error code when something goes wrong.
2032 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002033static int ext4_mb_good_group(struct ext4_allocation_context *ac,
2034 ext4_group_t group, int cr)
2035{
2036 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002037 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002038 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2039
2040 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002041
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002042 free = grp->bb_free;
2043 if (free == 0)
2044 return 0;
2045 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
2046 return 0;
2047
Darrick J. Wong163a2032013-08-28 17:35:51 -04002048 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
2049 return 0;
2050
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002051 /* We only do this if the grp has never been initialized */
2052 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04002053 int ret = ext4_mb_init_group(ac->ac_sb, group, GFP_NOFS);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002054 if (ret)
Lukas Czerner42ac1842015-06-08 11:40:40 -04002055 return ret;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002056 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002057
Alex Tomasc9de5602008-01-29 00:19:52 -05002058 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05002059 if (fragments == 0)
2060 return 0;
2061
2062 switch (cr) {
2063 case 0:
2064 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05002065
Theodore Ts'oa4912122009-03-12 12:18:34 -04002066 /* Avoid using the first bg of a flexgroup for data files */
2067 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
2068 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
2069 ((group % flex_size) == 0))
2070 return 0;
2071
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002072 if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) ||
2073 (free / fragments) >= ac->ac_g_ex.fe_len)
2074 return 1;
2075
2076 if (grp->bb_largest_free_order < ac->ac_2order)
2077 return 0;
2078
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002079 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002080 case 1:
2081 if ((free / fragments) >= ac->ac_g_ex.fe_len)
2082 return 1;
2083 break;
2084 case 2:
2085 if (free >= ac->ac_g_ex.fe_len)
2086 return 1;
2087 break;
2088 case 3:
2089 return 1;
2090 default:
2091 BUG();
2092 }
2093
2094 return 0;
2095}
2096
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002097static noinline_for_stack int
2098ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002099{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002100 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002101 int cr;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002102 int err = 0, first_err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002103 struct ext4_sb_info *sbi;
2104 struct super_block *sb;
2105 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002106
2107 sb = ac->ac_sb;
2108 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002109 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002110 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002111 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002112 ngroups = sbi->s_blockfile_groups;
2113
Alex Tomasc9de5602008-01-29 00:19:52 -05002114 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2115
2116 /* first, try the goal */
2117 err = ext4_mb_find_by_goal(ac, &e4b);
2118 if (err || ac->ac_status == AC_STATUS_FOUND)
2119 goto out;
2120
2121 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2122 goto out;
2123
2124 /*
2125 * ac->ac2_order is set only if the fe_len is a power of 2
2126 * if ac2_order is set we also set criteria to 0 so that we
2127 * try exact allocation using buddy.
2128 */
2129 i = fls(ac->ac_g_ex.fe_len);
2130 ac->ac_2order = 0;
2131 /*
2132 * We search using buddy data only if the order of the request
2133 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002134 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Jan Karad9b22cf2017-02-10 00:50:56 -05002135 * We also support searching for power-of-two requests only for
2136 * requests upto maximum buddy size we have constructed.
Alex Tomasc9de5602008-01-29 00:19:52 -05002137 */
Jan Karad9b22cf2017-02-10 00:50:56 -05002138 if (i >= sbi->s_mb_order2_reqs && i <= sb->s_blocksize_bits + 2) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002139 /*
2140 * This should tell if fe_len is exactly power of 2
2141 */
2142 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2143 ac->ac_2order = i - 1;
2144 }
2145
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002146 /* if stream allocation is enabled, use global goal */
2147 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002148 /* TBD: may be hot point */
2149 spin_lock(&sbi->s_md_lock);
2150 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2151 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2152 spin_unlock(&sbi->s_md_lock);
2153 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002154
Alex Tomasc9de5602008-01-29 00:19:52 -05002155 /* Let's just scan groups to find more-less suitable blocks */
2156 cr = ac->ac_2order ? 0 : 1;
2157 /*
2158 * cr == 0 try to get exact allocation,
2159 * cr == 3 try to get anything
2160 */
2161repeat:
2162 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2163 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002164 /*
2165 * searching for the right group start
2166 * from the goal value specified
2167 */
2168 group = ac->ac_g_ex.fe_group;
2169
Theodore Ts'o8df96752009-05-01 08:50:38 -04002170 for (i = 0; i < ngroups; group++, i++) {
Lukas Czerner42ac1842015-06-08 11:40:40 -04002171 int ret = 0;
Theodore Ts'o2ed57242013-06-12 11:43:02 -04002172 cond_resched();
Lachlan McIlroye6155732013-05-05 23:10:00 -04002173 /*
2174 * Artificially restricted ngroups for non-extent
2175 * files makes group > ngroups possible on first loop.
2176 */
2177 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002178 group = 0;
2179
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002180 /* This now checks without needing the buddy page */
Lukas Czerner42ac1842015-06-08 11:40:40 -04002181 ret = ext4_mb_good_group(ac, group, cr);
2182 if (ret <= 0) {
2183 if (!first_err)
2184 first_err = ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002185 continue;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002186 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002187
Alex Tomasc9de5602008-01-29 00:19:52 -05002188 err = ext4_mb_load_buddy(sb, group, &e4b);
2189 if (err)
2190 goto out;
2191
2192 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002193
2194 /*
2195 * We need to check again after locking the
2196 * block group
2197 */
Lukas Czerner42ac1842015-06-08 11:40:40 -04002198 ret = ext4_mb_good_group(ac, group, cr);
2199 if (ret <= 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002200 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002201 ext4_mb_unload_buddy(&e4b);
Lukas Czerner42ac1842015-06-08 11:40:40 -04002202 if (!first_err)
2203 first_err = ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002204 continue;
2205 }
2206
2207 ac->ac_groups_scanned++;
Jan Karad9b22cf2017-02-10 00:50:56 -05002208 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002209 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002210 else if (cr == 1 && sbi->s_stripe &&
2211 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002212 ext4_mb_scan_aligned(ac, &e4b);
2213 else
2214 ext4_mb_complex_scan_group(ac, &e4b);
2215
2216 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002217 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002218
2219 if (ac->ac_status != AC_STATUS_CONTINUE)
2220 break;
2221 }
2222 }
2223
2224 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2225 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2226 /*
2227 * We've been searching too long. Let's try to allocate
2228 * the best chunk we've found so far
2229 */
2230
2231 ext4_mb_try_best_found(ac, &e4b);
2232 if (ac->ac_status != AC_STATUS_FOUND) {
2233 /*
2234 * Someone more lucky has already allocated it.
2235 * The only thing we can do is just take first
2236 * found block(s)
2237 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2238 */
2239 ac->ac_b_ex.fe_group = 0;
2240 ac->ac_b_ex.fe_start = 0;
2241 ac->ac_b_ex.fe_len = 0;
2242 ac->ac_status = AC_STATUS_CONTINUE;
2243 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2244 cr = 3;
2245 atomic_inc(&sbi->s_mb_lost_chunks);
2246 goto repeat;
2247 }
2248 }
2249out:
Lukas Czerner42ac1842015-06-08 11:40:40 -04002250 if (!err && ac->ac_status != AC_STATUS_FOUND && first_err)
2251 err = first_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002252 return err;
2253}
2254
Alex Tomasc9de5602008-01-29 00:19:52 -05002255static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2256{
2257 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002258 ext4_group_t group;
2259
Theodore Ts'o8df96752009-05-01 08:50:38 -04002260 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002261 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002262 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002263 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002264}
2265
2266static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2267{
2268 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002269 ext4_group_t group;
2270
2271 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002272 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002273 return NULL;
2274 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002275 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002276}
2277
2278static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2279{
2280 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002281 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002282 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002283 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002284 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002285 struct ext4_group_info *grinfo;
Arnd Bergmann2df2c342017-08-05 21:57:46 -04002286 unsigned char blocksize_bits = min_t(unsigned char,
2287 sb->s_blocksize_bits,
2288 EXT4_MAX_BLOCK_LOG_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -05002289 struct sg {
2290 struct ext4_group_info info;
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002291 ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2];
Alex Tomasc9de5602008-01-29 00:19:52 -05002292 } sg;
2293
2294 group--;
2295 if (group == 0)
Rasmus Villemoes97b4af22015-06-15 00:32:58 -04002296 seq_puts(seq, "#group: free frags first ["
2297 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
Huaitong Han802cf1f2016-02-12 00:17:16 -05002298 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002299
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002300 i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2301 sizeof(struct ext4_group_info);
2302
Aditya Kali1c8457c2012-06-30 19:10:57 -04002303 grinfo = ext4_get_group_info(sb, group);
2304 /* Load the group info in memory only if not already loaded. */
2305 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2306 err = ext4_mb_load_buddy(sb, group, &e4b);
2307 if (err) {
2308 seq_printf(seq, "#%-5u: I/O error\n", group);
2309 return 0;
2310 }
2311 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002312 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002313
Theodore Ts'ob80b32b2017-08-14 08:29:18 -04002314 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002315
2316 if (buddy_loaded)
2317 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002318
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002319 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002320 sg.info.bb_fragments, sg.info.bb_first_free);
2321 for (i = 0; i <= 13; i++)
Arnd Bergmann2df2c342017-08-05 21:57:46 -04002322 seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ?
Alex Tomasc9de5602008-01-29 00:19:52 -05002323 sg.info.bb_counters[i] : 0);
2324 seq_printf(seq, " ]\n");
2325
2326 return 0;
2327}
2328
2329static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2330{
2331}
2332
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002333static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002334 .start = ext4_mb_seq_groups_start,
2335 .next = ext4_mb_seq_groups_next,
2336 .stop = ext4_mb_seq_groups_stop,
2337 .show = ext4_mb_seq_groups_show,
2338};
2339
2340static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2341{
Al Virod9dda782013-03-31 18:16:14 -04002342 struct super_block *sb = PDE_DATA(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05002343 int rc;
2344
2345 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2346 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002347 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002348 m->private = sb;
2349 }
2350 return rc;
2351
2352}
2353
Theodore Ts'oebd173b2015-09-23 12:46:17 -04002354const struct file_operations ext4_seq_mb_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002355 .open = ext4_mb_seq_groups_open,
2356 .read = seq_read,
2357 .llseek = seq_lseek,
2358 .release = seq_release,
2359};
2360
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002361static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2362{
2363 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2364 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2365
2366 BUG_ON(!cachep);
2367 return cachep;
2368}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002369
Theodore Ts'o28623c22012-09-05 01:31:50 -04002370/*
2371 * Allocate the top-level s_group_info array for the specified number
2372 * of groups
2373 */
2374int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2375{
2376 struct ext4_sb_info *sbi = EXT4_SB(sb);
2377 unsigned size;
2378 struct ext4_group_info ***new_groupinfo;
2379
2380 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2381 EXT4_DESC_PER_BLOCK_BITS(sb);
2382 if (size <= sbi->s_group_info_size)
2383 return 0;
2384
2385 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
Michal Hockoa7c3e902017-05-08 15:57:09 -07002386 new_groupinfo = kvzalloc(size, GFP_KERNEL);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002387 if (!new_groupinfo) {
2388 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2389 return -ENOMEM;
2390 }
2391 if (sbi->s_group_info) {
2392 memcpy(new_groupinfo, sbi->s_group_info,
2393 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
Al Virob93b41d2014-11-20 12:19:11 -05002394 kvfree(sbi->s_group_info);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002395 }
2396 sbi->s_group_info = new_groupinfo;
2397 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
2398 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2399 sbi->s_group_info_size);
2400 return 0;
2401}
2402
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002403/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002404int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002405 struct ext4_group_desc *desc)
2406{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002407 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002408 int metalen = 0;
2409 struct ext4_sb_info *sbi = EXT4_SB(sb);
2410 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002411 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002412
2413 /*
2414 * First check if this group is the first of a reserved block.
2415 * If it's true, we have to allocate a new table of pointers
2416 * to ext4_group_info structures
2417 */
2418 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2419 metalen = sizeof(*meta_group_info) <<
2420 EXT4_DESC_PER_BLOCK_BITS(sb);
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002421 meta_group_info = kmalloc(metalen, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002422 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002423 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002424 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002425 goto exit_meta_group_info;
2426 }
2427 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2428 meta_group_info;
2429 }
2430
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002431 meta_group_info =
2432 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2433 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2434
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002435 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002436 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002437 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002438 goto exit_group_info;
2439 }
2440 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2441 &(meta_group_info[i]->bb_state));
2442
2443 /*
2444 * initialize bb_free to be able to skip
2445 * empty groups without initialization
2446 */
Theodore Ts'o88446182018-06-14 00:58:00 -04002447 if (ext4_has_group_desc_csum(sb) &&
2448 (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002449 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002450 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002451 } else {
2452 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002453 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002454 }
2455
2456 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002457 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002458 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002459 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002460
2461#ifdef DOUBLE_CHECK
2462 {
2463 struct buffer_head *bh;
2464 meta_group_info[i]->bb_bitmap =
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002465 kmalloc(sb->s_blocksize, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002466 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2467 bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002468 BUG_ON(IS_ERR_OR_NULL(bh));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002469 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2470 sb->s_blocksize);
2471 put_bh(bh);
2472 }
2473#endif
2474
2475 return 0;
2476
2477exit_group_info:
2478 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002479 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002480 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002481 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2482 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002483exit_meta_group_info:
2484 return -ENOMEM;
2485} /* ext4_mb_add_groupinfo */
2486
Alex Tomasc9de5602008-01-29 00:19:52 -05002487static int ext4_mb_init_backend(struct super_block *sb)
2488{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002489 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002490 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002491 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002492 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002493 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002494 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002495
Theodore Ts'o28623c22012-09-05 01:31:50 -04002496 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2497 if (err)
2498 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002499
Alex Tomasc9de5602008-01-29 00:19:52 -05002500 sbi->s_buddy_cache = new_inode(sb);
2501 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002502 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002503 goto err_freesgi;
2504 }
Yu Jian48e60612011-08-01 17:41:39 -04002505 /* To avoid potentially colliding with an valid on-disk inode number,
2506 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2507 * not in the inode hash, so it should never be found by iget(), but
2508 * this will avoid confusion if it ever shows up during debugging. */
2509 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002510 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002511 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002512 desc = ext4_get_group_desc(sb, i, NULL);
2513 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002514 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002515 goto err_freebuddy;
2516 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002517 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2518 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002519 }
2520
2521 return 0;
2522
2523err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002524 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002525 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002526 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002527 i = sbi->s_group_info_size;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002528 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002529 kfree(sbi->s_group_info[i]);
2530 iput(sbi->s_buddy_cache);
2531err_freesgi:
Al Virob93b41d2014-11-20 12:19:11 -05002532 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002533 return -ENOMEM;
2534}
2535
Eric Sandeen2892c152011-02-12 08:12:18 -05002536static void ext4_groupinfo_destroy_slabs(void)
2537{
2538 int i;
2539
2540 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
Sean Fu21c580d2018-05-20 22:44:13 -04002541 kmem_cache_destroy(ext4_groupinfo_caches[i]);
Eric Sandeen2892c152011-02-12 08:12:18 -05002542 ext4_groupinfo_caches[i] = NULL;
2543 }
2544}
2545
2546static int ext4_groupinfo_create_slab(size_t size)
2547{
2548 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2549 int slab_size;
2550 int blocksize_bits = order_base_2(size);
2551 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2552 struct kmem_cache *cachep;
2553
2554 if (cache_index >= NR_GRPINFO_CACHES)
2555 return -EINVAL;
2556
2557 if (unlikely(cache_index < 0))
2558 cache_index = 0;
2559
2560 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2561 if (ext4_groupinfo_caches[cache_index]) {
2562 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2563 return 0; /* Already created */
2564 }
2565
2566 slab_size = offsetof(struct ext4_group_info,
2567 bb_counters[blocksize_bits + 2]);
2568
2569 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2570 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2571 NULL);
2572
Tao Ma823ba012011-07-11 18:26:01 -04002573 ext4_groupinfo_caches[cache_index] = cachep;
2574
Eric Sandeen2892c152011-02-12 08:12:18 -05002575 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2576 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002577 printk(KERN_EMERG
2578 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002579 return -ENOMEM;
2580 }
2581
Eric Sandeen2892c152011-02-12 08:12:18 -05002582 return 0;
2583}
2584
Akira Fujita9d990122012-05-28 14:19:25 -04002585int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002586{
2587 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002588 unsigned i, j;
Nicolai Stange935244c2016-05-05 19:46:19 -04002589 unsigned offset, offset_incr;
Alex Tomasc9de5602008-01-29 00:19:52 -05002590 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002591 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002592
Eric Sandeen19278052009-08-25 22:36:25 -04002593 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002594
2595 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2596 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002597 ret = -ENOMEM;
2598 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002599 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002600
Eric Sandeen19278052009-08-25 22:36:25 -04002601 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002602 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2603 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002604 ret = -ENOMEM;
2605 goto out;
2606 }
2607
Eric Sandeen2892c152011-02-12 08:12:18 -05002608 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2609 if (ret < 0)
2610 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002611
2612 /* order 0 is regular bitmap */
2613 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2614 sbi->s_mb_offsets[0] = 0;
2615
2616 i = 1;
2617 offset = 0;
Nicolai Stange935244c2016-05-05 19:46:19 -04002618 offset_incr = 1 << (sb->s_blocksize_bits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05002619 max = sb->s_blocksize << 2;
2620 do {
2621 sbi->s_mb_offsets[i] = offset;
2622 sbi->s_mb_maxs[i] = max;
Nicolai Stange935244c2016-05-05 19:46:19 -04002623 offset += offset_incr;
2624 offset_incr = offset_incr >> 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002625 max = max >> 1;
2626 i++;
2627 } while (i <= sb->s_blocksize_bits + 1);
2628
Alex Tomasc9de5602008-01-29 00:19:52 -05002629 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002630 spin_lock_init(&sbi->s_bal_lock);
Theodore Ts'od08854f2016-06-26 18:24:01 -04002631 sbi->s_mb_free_pending = 0;
Daeho Jeonga0154342017-06-22 23:54:33 -04002632 INIT_LIST_HEAD(&sbi->s_freed_data_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002633
2634 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2635 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2636 sbi->s_mb_stats = MB_DEFAULT_STATS;
2637 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2638 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002639 /*
2640 * The default group preallocation is 512, which for 4k block
2641 * sizes translates to 2 megabytes. However for bigalloc file
2642 * systems, this is probably too big (i.e, if the cluster size
2643 * is 1 megabyte, then group preallocation size becomes half a
2644 * gigabyte!). As a default, we will keep a two megabyte
2645 * group pralloc size for cluster sizes up to 64k, and after
2646 * that, we will force a minimum group preallocation size of
2647 * 32 clusters. This translates to 8 megs when the cluster
2648 * size is 256k, and 32 megs when the cluster size is 1 meg,
2649 * which seems reasonable as a default.
2650 */
2651 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2652 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002653 /*
2654 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2655 * to the lowest multiple of s_stripe which is bigger than
2656 * the s_mb_group_prealloc as determined above. We want
2657 * the preallocation size to be an exact multiple of the
2658 * RAID stripe size so that preallocations don't fragment
2659 * the stripes.
2660 */
2661 if (sbi->s_stripe > 1) {
2662 sbi->s_mb_group_prealloc = roundup(
2663 sbi->s_mb_group_prealloc, sbi->s_stripe);
2664 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002665
Eric Sandeen730c2132008-09-13 15:23:29 -04002666 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002667 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002668 ret = -ENOMEM;
Andrey Tsyvarev029b10c2014-05-12 12:34:21 -04002669 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002670 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002671 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002672 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002673 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002674 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002675 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2676 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002677 spin_lock_init(&lg->lg_prealloc_lock);
2678 }
2679
Yu Jian79a77c52011-08-01 17:41:46 -04002680 /* init file for buddy data */
2681 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002682 if (ret != 0)
2683 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002684
Tao Ma7aa0bae2011-10-06 10:22:28 -04002685 return 0;
2686
2687out_free_locality_groups:
2688 free_percpu(sbi->s_locality_groups);
2689 sbi->s_locality_groups = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002690out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002691 kfree(sbi->s_mb_offsets);
2692 sbi->s_mb_offsets = NULL;
2693 kfree(sbi->s_mb_maxs);
2694 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002695 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002696}
2697
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002698/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002699static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2700{
2701 struct ext4_prealloc_space *pa;
2702 struct list_head *cur, *tmp;
2703 int count = 0;
2704
2705 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2706 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2707 list_del(&pa->pa_group_list);
2708 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002709 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002710 }
2711 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002712 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002713
2714}
2715
2716int ext4_mb_release(struct super_block *sb)
2717{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002718 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002719 ext4_group_t i;
2720 int num_meta_group_infos;
2721 struct ext4_group_info *grinfo;
2722 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002723 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002724
Alex Tomasc9de5602008-01-29 00:19:52 -05002725 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002726 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002727 grinfo = ext4_get_group_info(sb, i);
2728#ifdef DOUBLE_CHECK
2729 kfree(grinfo->bb_bitmap);
2730#endif
2731 ext4_lock_group(sb, i);
2732 ext4_mb_cleanup_pa(grinfo);
2733 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002734 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002735 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002736 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002737 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2738 EXT4_DESC_PER_BLOCK_BITS(sb);
2739 for (i = 0; i < num_meta_group_infos; i++)
2740 kfree(sbi->s_group_info[i]);
Al Virob93b41d2014-11-20 12:19:11 -05002741 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002742 }
2743 kfree(sbi->s_mb_offsets);
2744 kfree(sbi->s_mb_maxs);
Markus Elfringbfcba2d2014-11-25 20:01:37 -05002745 iput(sbi->s_buddy_cache);
Alex Tomasc9de5602008-01-29 00:19:52 -05002746 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002747 ext4_msg(sb, KERN_INFO,
2748 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002749 atomic_read(&sbi->s_bal_allocated),
2750 atomic_read(&sbi->s_bal_reqs),
2751 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002752 ext4_msg(sb, KERN_INFO,
2753 "mballoc: %u extents scanned, %u goal hits, "
2754 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002755 atomic_read(&sbi->s_bal_ex_scanned),
2756 atomic_read(&sbi->s_bal_goals),
2757 atomic_read(&sbi->s_bal_2orders),
2758 atomic_read(&sbi->s_bal_breaks),
2759 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002760 ext4_msg(sb, KERN_INFO,
2761 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002762 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002763 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002764 ext4_msg(sb, KERN_INFO,
2765 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002766 atomic_read(&sbi->s_mb_preallocated),
2767 atomic_read(&sbi->s_mb_discarded));
2768 }
2769
Eric Sandeen730c2132008-09-13 15:23:29 -04002770 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002771
2772 return 0;
2773}
2774
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002775static inline int ext4_issue_discard(struct super_block *sb,
Daeho Jeonga0154342017-06-22 23:54:33 -04002776 ext4_group_t block_group, ext4_grpblk_t cluster, int count,
2777 struct bio **biop)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002778{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002779 ext4_fsblk_t discard_block;
2780
Theodore Ts'o84130192011-09-09 18:50:51 -04002781 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2782 ext4_group_first_block_no(sb, block_group));
2783 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002784 trace_ext4_discard_blocks(sb,
2785 (unsigned long long) discard_block, count);
Daeho Jeonga0154342017-06-22 23:54:33 -04002786 if (biop) {
2787 return __blkdev_issue_discard(sb->s_bdev,
2788 (sector_t)discard_block << (sb->s_blocksize_bits - 9),
2789 (sector_t)count << (sb->s_blocksize_bits - 9),
2790 GFP_NOFS, 0, biop);
2791 } else
2792 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002793}
2794
Daeho Jeonga0154342017-06-22 23:54:33 -04002795static void ext4_free_data_in_buddy(struct super_block *sb,
2796 struct ext4_free_data *entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05002797{
Alex Tomasc9de5602008-01-29 00:19:52 -05002798 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002799 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002800 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002801
Bobi Jam18aadd42012-02-20 17:53:02 -05002802 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2803 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002804
Bobi Jam18aadd42012-02-20 17:53:02 -05002805 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2806 /* we expect to find existing buddy because it's pinned */
2807 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002808
Theodore Ts'od08854f2016-06-26 18:24:01 -04002809 spin_lock(&EXT4_SB(sb)->s_md_lock);
2810 EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
2811 spin_unlock(&EXT4_SB(sb)->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002812
Bobi Jam18aadd42012-02-20 17:53:02 -05002813 db = e4b.bd_info;
2814 /* there are blocks to put in buddy to make them really free */
2815 count += entry->efd_count;
2816 count2++;
2817 ext4_lock_group(sb, entry->efd_group);
2818 /* Take it out of per group rb tree */
2819 rb_erase(&entry->efd_node, &(db->bb_free_root));
2820 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002821
Bobi Jam18aadd42012-02-20 17:53:02 -05002822 /*
2823 * Clear the trimmed flag for the group so that the next
2824 * ext4_trim_fs can trim it.
2825 * If the volume is mounted with -o discard, online discard
2826 * is supported and the free blocks will be trimmed online.
2827 */
2828 if (!test_opt(sb, DISCARD))
2829 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2830
2831 if (!db->bb_free_root.rb_node) {
2832 /* No more items in the per group rb tree
2833 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002834 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002835 put_page(e4b.bd_buddy_page);
2836 put_page(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002837 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002838 ext4_unlock_group(sb, entry->efd_group);
2839 kmem_cache_free(ext4_free_data_cachep, entry);
2840 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002841
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002842 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002843}
2844
Daeho Jeonga0154342017-06-22 23:54:33 -04002845/*
2846 * This function is called by the jbd2 layer once the commit has finished,
2847 * so we know we can free the blocks that were released with that commit.
2848 */
2849void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid)
2850{
2851 struct ext4_sb_info *sbi = EXT4_SB(sb);
2852 struct ext4_free_data *entry, *tmp;
2853 struct bio *discard_bio = NULL;
2854 struct list_head freed_data_list;
2855 struct list_head *cut_pos = NULL;
2856 int err;
2857
2858 INIT_LIST_HEAD(&freed_data_list);
2859
2860 spin_lock(&sbi->s_md_lock);
2861 list_for_each_entry(entry, &sbi->s_freed_data_list, efd_list) {
2862 if (entry->efd_tid != commit_tid)
2863 break;
2864 cut_pos = &entry->efd_list;
2865 }
2866 if (cut_pos)
2867 list_cut_position(&freed_data_list, &sbi->s_freed_data_list,
2868 cut_pos);
2869 spin_unlock(&sbi->s_md_lock);
2870
2871 if (test_opt(sb, DISCARD)) {
2872 list_for_each_entry(entry, &freed_data_list, efd_list) {
2873 err = ext4_issue_discard(sb, entry->efd_group,
2874 entry->efd_start_cluster,
2875 entry->efd_count,
2876 &discard_bio);
2877 if (err && err != -EOPNOTSUPP) {
2878 ext4_msg(sb, KERN_WARNING, "discard request in"
2879 " group:%d block:%d count:%d failed"
2880 " with %d", entry->efd_group,
2881 entry->efd_start_cluster,
2882 entry->efd_count, err);
2883 } else if (err == -EOPNOTSUPP)
2884 break;
2885 }
2886
Daeho Jeonge4510572017-08-05 13:11:57 -04002887 if (discard_bio) {
Daeho Jeonga0154342017-06-22 23:54:33 -04002888 submit_bio_wait(discard_bio);
Daeho Jeonge4510572017-08-05 13:11:57 -04002889 bio_put(discard_bio);
2890 }
Daeho Jeonga0154342017-06-22 23:54:33 -04002891 }
2892
2893 list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list)
2894 ext4_free_data_in_buddy(sb, entry);
2895}
2896
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002897int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002898{
Theodore Ts'o16828082010-10-27 21:30:09 -04002899 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2900 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002901 if (ext4_pspace_cachep == NULL)
2902 return -ENOMEM;
2903
Theodore Ts'o16828082010-10-27 21:30:09 -04002904 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2905 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002906 if (ext4_ac_cachep == NULL) {
2907 kmem_cache_destroy(ext4_pspace_cachep);
2908 return -ENOMEM;
2909 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002910
Bobi Jam18aadd42012-02-20 17:53:02 -05002911 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2912 SLAB_RECLAIM_ACCOUNT);
2913 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002914 kmem_cache_destroy(ext4_pspace_cachep);
2915 kmem_cache_destroy(ext4_ac_cachep);
2916 return -ENOMEM;
2917 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002918 return 0;
2919}
2920
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002921void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002922{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002923 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002924 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2925 * before destroying the slab cache.
2926 */
2927 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002928 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002929 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002930 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002931 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002932}
2933
2934
2935/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002936 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002937 * Returns 0 if success or error code
2938 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002939static noinline_for_stack int
2940ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002941 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002942{
2943 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002944 struct ext4_group_desc *gdp;
2945 struct buffer_head *gdp_bh;
2946 struct ext4_sb_info *sbi;
2947 struct super_block *sb;
2948 ext4_fsblk_t block;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002949 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002950
2951 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2952 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2953
2954 sb = ac->ac_sb;
2955 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002956
Theodore Ts'o574ca172008-07-11 19:27:31 -04002957 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002958 if (IS_ERR(bitmap_bh)) {
2959 err = PTR_ERR(bitmap_bh);
2960 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002961 goto out_err;
Darrick J. Wong9008a582015-10-17 21:33:24 -04002962 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002963
liang xie5d601252014-05-12 22:06:43 -04002964 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002965 err = ext4_journal_get_write_access(handle, bitmap_bh);
2966 if (err)
2967 goto out_err;
2968
2969 err = -EIO;
2970 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2971 if (!gdp)
2972 goto out_err;
2973
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002974 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002975 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002976
liang xie5d601252014-05-12 22:06:43 -04002977 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002978 err = ext4_journal_get_write_access(handle, gdp_bh);
2979 if (err)
2980 goto out_err;
2981
Akinobu Mitabda00de2010-03-03 23:53:25 -05002982 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002983
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002984 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002985 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002986 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002987 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002988 /* File system mounted not to panic on error
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002989 * Fix the bitmap and return EFSCORRUPTED
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002990 * We leak some of the blocks here.
2991 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002992 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002993 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2994 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002995 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002996 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002997 if (!err)
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002998 err = -EFSCORRUPTED;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002999 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003000 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003001
3002 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003003#ifdef AGGRESSIVE_CHECK
3004 {
3005 int i;
3006 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3007 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3008 bitmap_bh->b_data));
3009 }
3010 }
3011#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003012 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3013 ac->ac_b_ex.fe_len);
Theodore Ts'o88446182018-06-14 00:58:00 -04003014 if (ext4_has_group_desc_csum(sb) &&
3015 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003016 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003017 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04003018 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003019 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05003020 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003021 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
3022 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04003023 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003024 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003025
3026 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04003027 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003028 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003029 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003030 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003031 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3032 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04003033 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
3034 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05003035
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003036 if (sbi->s_log_groups_per_flex) {
3037 ext4_group_t flex_group = ext4_flex_group(sbi,
3038 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04003039 atomic64_sub(ac->ac_b_ex.fe_len,
3040 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003041 }
3042
Frank Mayhar03901312009-01-07 00:06:22 -05003043 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003044 if (err)
3045 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003046 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003047
3048out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003049 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003050 return err;
3051}
3052
3053/*
3054 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003055 * Group request are normalized to s_mb_group_prealloc, which goes to
3056 * s_strip if we set the same via mount option.
3057 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003058 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003059 *
3060 * XXX: should we try to preallocate more than the group has now?
3061 */
3062static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3063{
3064 struct super_block *sb = ac->ac_sb;
3065 struct ext4_locality_group *lg = ac->ac_lg;
3066
3067 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003068 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003069 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003070 current->pid, ac->ac_g_ex.fe_len);
3071}
3072
3073/*
3074 * Normalization means making request better in terms of
3075 * size and alignment
3076 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003077static noinline_for_stack void
3078ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003079 struct ext4_allocation_request *ar)
3080{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003081 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003082 int bsbits, max;
3083 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003084 loff_t size, start_off;
3085 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003086 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003087 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003088 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003089
3090 /* do normalize only data requests, metadata requests
3091 do not need preallocation */
3092 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3093 return;
3094
3095 /* sometime caller may want exact blocks */
3096 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3097 return;
3098
3099 /* caller may indicate that preallocation isn't
3100 * required (it's a tail, for example) */
3101 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3102 return;
3103
3104 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3105 ext4_mb_normalize_group_request(ac);
3106 return ;
3107 }
3108
3109 bsbits = ac->ac_sb->s_blocksize_bits;
3110
3111 /* first, let's learn actual file size
3112 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003113 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003114 size = size << bsbits;
3115 if (size < i_size_read(ac->ac_inode))
3116 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003117 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003118
Valerie Clement19304792008-05-13 19:31:14 -04003119 /* max size of free chunks */
3120 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003121
Valerie Clement19304792008-05-13 19:31:14 -04003122#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3123 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003124
3125 /* first, try to predict filesize */
3126 /* XXX: should this table be tunable? */
3127 start_off = 0;
3128 if (size <= 16 * 1024) {
3129 size = 16 * 1024;
3130 } else if (size <= 32 * 1024) {
3131 size = 32 * 1024;
3132 } else if (size <= 64 * 1024) {
3133 size = 64 * 1024;
3134 } else if (size <= 128 * 1024) {
3135 size = 128 * 1024;
3136 } else if (size <= 256 * 1024) {
3137 size = 256 * 1024;
3138 } else if (size <= 512 * 1024) {
3139 size = 512 * 1024;
3140 } else if (size <= 1024 * 1024) {
3141 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003142 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003143 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003144 (21 - bsbits)) << 21;
3145 size = 2 * 1024 * 1024;
3146 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003147 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3148 (22 - bsbits)) << 22;
3149 size = 4 * 1024 * 1024;
3150 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003151 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003152 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3153 (23 - bsbits)) << 23;
3154 size = 8 * 1024 * 1024;
3155 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003156 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3157 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3158 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003159 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003160 size = size >> bsbits;
3161 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003162
3163 /* don't cover already allocated blocks in selected range */
3164 if (ar->pleft && start <= ar->lleft) {
3165 size -= ar->lleft + 1 - start;
3166 start = ar->lleft + 1;
3167 }
3168 if (ar->pright && start + size - 1 >= ar->lright)
3169 size -= start + size - ar->lright;
3170
Jan Karacd648b82017-01-27 14:34:30 -05003171 /*
3172 * Trim allocation request for filesystems with artificially small
3173 * groups.
3174 */
3175 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
3176 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
3177
Alex Tomasc9de5602008-01-29 00:19:52 -05003178 end = start + size;
3179
3180 /* check we don't cross already preallocated blocks */
3181 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003182 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003183 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003184
Alex Tomasc9de5602008-01-29 00:19:52 -05003185 if (pa->pa_deleted)
3186 continue;
3187 spin_lock(&pa->pa_lock);
3188 if (pa->pa_deleted) {
3189 spin_unlock(&pa->pa_lock);
3190 continue;
3191 }
3192
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003193 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3194 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003195
3196 /* PA must not overlap original request */
3197 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3198 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3199
Eric Sandeen38877f42009-08-17 23:55:24 -04003200 /* skip PAs this normalized request doesn't overlap with */
3201 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003202 spin_unlock(&pa->pa_lock);
3203 continue;
3204 }
3205 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3206
Eric Sandeen38877f42009-08-17 23:55:24 -04003207 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003208 if (pa_end <= ac->ac_o_ex.fe_logical) {
3209 BUG_ON(pa_end < start);
3210 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003211 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003212 BUG_ON(pa->pa_lstart > end);
3213 end = pa->pa_lstart;
3214 }
3215 spin_unlock(&pa->pa_lock);
3216 }
3217 rcu_read_unlock();
3218 size = end - start;
3219
3220 /* XXX: extra loop to check we really don't overlap preallocations */
3221 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003222 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003223 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003224
Alex Tomasc9de5602008-01-29 00:19:52 -05003225 spin_lock(&pa->pa_lock);
3226 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003227 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3228 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003229 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3230 }
3231 spin_unlock(&pa->pa_lock);
3232 }
3233 rcu_read_unlock();
3234
3235 if (start + size <= ac->ac_o_ex.fe_logical &&
3236 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003237 ext4_msg(ac->ac_sb, KERN_ERR,
3238 "start %lu, size %lu, fe_logical %lu",
3239 (unsigned long) start, (unsigned long) size,
3240 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003241 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003242 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003243 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003244
3245 /* now prepare goal request */
3246
3247 /* XXX: is it better to align blocks WRT to logical
3248 * placement or satisfy big request as is */
3249 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003250 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003251
3252 /* define goal start in order to merge */
3253 if (ar->pright && (ar->lright == (start + size))) {
3254 /* merge to the right */
3255 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3256 &ac->ac_f_ex.fe_group,
3257 &ac->ac_f_ex.fe_start);
3258 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3259 }
3260 if (ar->pleft && (ar->lleft + 1 == start)) {
3261 /* merge to the left */
3262 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3263 &ac->ac_f_ex.fe_group,
3264 &ac->ac_f_ex.fe_start);
3265 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3266 }
3267
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003268 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003269 (unsigned) orig_size, (unsigned) start);
3270}
3271
3272static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3273{
3274 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3275
3276 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3277 atomic_inc(&sbi->s_bal_reqs);
3278 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003279 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003280 atomic_inc(&sbi->s_bal_success);
3281 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3282 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3283 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3284 atomic_inc(&sbi->s_bal_goals);
3285 if (ac->ac_found > sbi->s_mb_max_to_scan)
3286 atomic_inc(&sbi->s_bal_breaks);
3287 }
3288
Theodore Ts'o296c3552009-09-30 00:32:42 -04003289 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3290 trace_ext4_mballoc_alloc(ac);
3291 else
3292 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003293}
3294
3295/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003296 * Called on failure; free up any blocks from the inode PA for this
3297 * context. We don't need this for MB_GROUP_PA because we only change
3298 * pa_free in ext4_mb_release_context(), but on failure, we've already
3299 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3300 */
3301static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3302{
3303 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003304 struct ext4_buddy e4b;
3305 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003306
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003307 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003308 if (ac->ac_f_ex.fe_len == 0)
3309 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003310 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3311 if (err) {
3312 /*
3313 * This should never happen since we pin the
3314 * pages in the ext4_allocation_context so
3315 * ext4_mb_load_buddy() should never fail.
3316 */
3317 WARN(1, "mb_load_buddy failed (%d)", err);
3318 return;
3319 }
3320 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3321 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3322 ac->ac_f_ex.fe_len);
3323 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003324 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003325 return;
3326 }
3327 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003328 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003329}
3330
3331/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003332 * use blocks preallocated to inode
3333 */
3334static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3335 struct ext4_prealloc_space *pa)
3336{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003337 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003338 ext4_fsblk_t start;
3339 ext4_fsblk_t end;
3340 int len;
3341
3342 /* found preallocated blocks, use them */
3343 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003344 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3345 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3346 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003347 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3348 &ac->ac_b_ex.fe_start);
3349 ac->ac_b_ex.fe_len = len;
3350 ac->ac_status = AC_STATUS_FOUND;
3351 ac->ac_pa = pa;
3352
3353 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003354 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003355 BUG_ON(pa->pa_free < len);
3356 pa->pa_free -= len;
3357
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003358 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003359}
3360
3361/*
3362 * use blocks preallocated to locality group
3363 */
3364static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3365 struct ext4_prealloc_space *pa)
3366{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003367 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003368
Alex Tomasc9de5602008-01-29 00:19:52 -05003369 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3370 &ac->ac_b_ex.fe_group,
3371 &ac->ac_b_ex.fe_start);
3372 ac->ac_b_ex.fe_len = len;
3373 ac->ac_status = AC_STATUS_FOUND;
3374 ac->ac_pa = pa;
3375
3376 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003377 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003378 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003379 * in on-disk bitmap -- see ext4_mb_release_context()
3380 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003381 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003382 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003383}
3384
3385/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003386 * Return the prealloc space that have minimal distance
3387 * from the goal block. @cpa is the prealloc
3388 * space that is having currently known minimal distance
3389 * from the goal block.
3390 */
3391static struct ext4_prealloc_space *
3392ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3393 struct ext4_prealloc_space *pa,
3394 struct ext4_prealloc_space *cpa)
3395{
3396 ext4_fsblk_t cur_distance, new_distance;
3397
3398 if (cpa == NULL) {
3399 atomic_inc(&pa->pa_count);
3400 return pa;
3401 }
Andrew Morton79211c82015-11-09 14:58:13 -08003402 cur_distance = abs(goal_block - cpa->pa_pstart);
3403 new_distance = abs(goal_block - pa->pa_pstart);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003404
Coly Li5a54b2f2011-02-24 14:10:05 -05003405 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003406 return cpa;
3407
3408 /* drop the previous reference */
3409 atomic_dec(&cpa->pa_count);
3410 atomic_inc(&pa->pa_count);
3411 return pa;
3412}
3413
3414/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003415 * search goal blocks in preallocated space
3416 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003417static noinline_for_stack int
3418ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003419{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003420 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003421 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003422 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3423 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003424 struct ext4_prealloc_space *pa, *cpa = NULL;
3425 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003426
3427 /* only data can be preallocated */
3428 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3429 return 0;
3430
3431 /* first, try per-file preallocation */
3432 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003433 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003434
3435 /* all fields in this condition don't change,
3436 * so we can skip locking for them */
3437 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003438 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3439 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003440 continue;
3441
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003442 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003443 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003444 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3445 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003446 continue;
3447
Alex Tomasc9de5602008-01-29 00:19:52 -05003448 /* found preallocated blocks, use them */
3449 spin_lock(&pa->pa_lock);
3450 if (pa->pa_deleted == 0 && pa->pa_free) {
3451 atomic_inc(&pa->pa_count);
3452 ext4_mb_use_inode_pa(ac, pa);
3453 spin_unlock(&pa->pa_lock);
3454 ac->ac_criteria = 10;
3455 rcu_read_unlock();
3456 return 1;
3457 }
3458 spin_unlock(&pa->pa_lock);
3459 }
3460 rcu_read_unlock();
3461
3462 /* can we use group allocation? */
3463 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3464 return 0;
3465
3466 /* inode may have no locality group for some reason */
3467 lg = ac->ac_lg;
3468 if (lg == NULL)
3469 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003470 order = fls(ac->ac_o_ex.fe_len) - 1;
3471 if (order > PREALLOC_TB_SIZE - 1)
3472 /* The max size of hash table is PREALLOC_TB_SIZE */
3473 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003474
Akinobu Mitabda00de2010-03-03 23:53:25 -05003475 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003476 /*
3477 * search for the prealloc space that is having
3478 * minimal distance from the goal block.
3479 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003480 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3481 rcu_read_lock();
3482 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3483 pa_inode_list) {
3484 spin_lock(&pa->pa_lock);
3485 if (pa->pa_deleted == 0 &&
3486 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003487
3488 cpa = ext4_mb_check_group_pa(goal_block,
3489 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003490 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003491 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003492 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003493 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003494 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003495 if (cpa) {
3496 ext4_mb_use_group_pa(ac, cpa);
3497 ac->ac_criteria = 20;
3498 return 1;
3499 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003500 return 0;
3501}
3502
3503/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003504 * the function goes through all block freed in the group
3505 * but not yet committed and marks them used in in-core bitmap.
3506 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003507 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003508 */
3509static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3510 ext4_group_t group)
3511{
3512 struct rb_node *n;
3513 struct ext4_group_info *grp;
3514 struct ext4_free_data *entry;
3515
3516 grp = ext4_get_group_info(sb, group);
3517 n = rb_first(&(grp->bb_free_root));
3518
3519 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003520 entry = rb_entry(n, struct ext4_free_data, efd_node);
3521 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003522 n = rb_next(n);
3523 }
3524 return;
3525}
3526
3527/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003528 * the function goes through all preallocation in this group and marks them
3529 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003530 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003531 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003532static noinline_for_stack
3533void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003534 ext4_group_t group)
3535{
3536 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3537 struct ext4_prealloc_space *pa;
3538 struct list_head *cur;
3539 ext4_group_t groupnr;
3540 ext4_grpblk_t start;
3541 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003542 int len;
3543
3544 /* all form of preallocation discards first load group,
3545 * so the only competing code is preallocation use.
3546 * we don't need any locking here
3547 * notice we do NOT ignore preallocations with pa_deleted
3548 * otherwise we could leave used blocks available for
3549 * allocation in buddy when concurrent ext4_mb_put_pa()
3550 * is dropping preallocation
3551 */
3552 list_for_each(cur, &grp->bb_prealloc_list) {
3553 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3554 spin_lock(&pa->pa_lock);
3555 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3556 &groupnr, &start);
3557 len = pa->pa_len;
3558 spin_unlock(&pa->pa_lock);
3559 if (unlikely(len == 0))
3560 continue;
3561 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003562 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003563 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003564 }
Colin Ian Kingff950152017-07-06 15:28:45 -04003565 mb_debug(1, "preallocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003566}
3567
3568static void ext4_mb_pa_callback(struct rcu_head *head)
3569{
3570 struct ext4_prealloc_space *pa;
3571 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003572
3573 BUG_ON(atomic_read(&pa->pa_count));
3574 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003575 kmem_cache_free(ext4_pspace_cachep, pa);
3576}
3577
3578/*
3579 * drops a reference to preallocated space descriptor
3580 * if this was the last reference and the space is consumed
3581 */
3582static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3583 struct super_block *sb, struct ext4_prealloc_space *pa)
3584{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003585 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003586 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003587
Alex Tomasc9de5602008-01-29 00:19:52 -05003588 /* in this short window concurrent discard can set pa_deleted */
3589 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003590 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3591 spin_unlock(&pa->pa_lock);
3592 return;
3593 }
3594
Alex Tomasc9de5602008-01-29 00:19:52 -05003595 if (pa->pa_deleted == 1) {
3596 spin_unlock(&pa->pa_lock);
3597 return;
3598 }
3599
3600 pa->pa_deleted = 1;
3601 spin_unlock(&pa->pa_lock);
3602
Eric Sandeend33a1972009-03-16 23:25:40 -04003603 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003604 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003605 * If doing group-based preallocation, pa_pstart may be in the
3606 * next group when pa is used up
3607 */
3608 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003609 grp_blk--;
3610
Lukas Czernerbd862982013-04-03 23:32:34 -04003611 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003612
3613 /*
3614 * possible race:
3615 *
3616 * P1 (buddy init) P2 (regular allocation)
3617 * find block B in PA
3618 * copy on-disk bitmap to buddy
3619 * mark B in on-disk bitmap
3620 * drop PA from group
3621 * mark all PAs in buddy
3622 *
3623 * thus, P1 initializes buddy with B available. to prevent this
3624 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3625 * against that pair
3626 */
3627 ext4_lock_group(sb, grp);
3628 list_del(&pa->pa_group_list);
3629 ext4_unlock_group(sb, grp);
3630
3631 spin_lock(pa->pa_obj_lock);
3632 list_del_rcu(&pa->pa_inode_list);
3633 spin_unlock(pa->pa_obj_lock);
3634
3635 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3636}
3637
3638/*
3639 * creates new preallocated space for given inode
3640 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003641static noinline_for_stack int
3642ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003643{
3644 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003645 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003646 struct ext4_prealloc_space *pa;
3647 struct ext4_group_info *grp;
3648 struct ext4_inode_info *ei;
3649
3650 /* preallocate only when found space is larger then requested */
3651 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3652 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3653 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3654
3655 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3656 if (pa == NULL)
3657 return -ENOMEM;
3658
3659 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3660 int winl;
3661 int wins;
3662 int win;
3663 int offs;
3664
3665 /* we can't allocate as much as normalizer wants.
3666 * so, found space must get proper lstart
3667 * to cover original request */
3668 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3669 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3670
3671 /* we're limited by original request in that
3672 * logical block must be covered any way
3673 * winl is window we can move our chunk within */
3674 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3675
3676 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003677 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003678
3679 /* the smallest one defines real window */
3680 win = min(winl, wins);
3681
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003682 offs = ac->ac_o_ex.fe_logical %
3683 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003684 if (offs && offs < win)
3685 win = offs;
3686
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003687 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003688 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003689 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3690 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3691 }
3692
3693 /* preallocation can change ac_b_ex, thus we store actually
3694 * allocated blocks for history */
3695 ac->ac_f_ex = ac->ac_b_ex;
3696
3697 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3698 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3699 pa->pa_len = ac->ac_b_ex.fe_len;
3700 pa->pa_free = pa->pa_len;
3701 atomic_set(&pa->pa_count, 1);
3702 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003703 INIT_LIST_HEAD(&pa->pa_inode_list);
3704 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003705 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003706 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003707
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003708 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003709 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003710 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003711
3712 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003713 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003714
3715 ei = EXT4_I(ac->ac_inode);
3716 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3717
3718 pa->pa_obj_lock = &ei->i_prealloc_lock;
3719 pa->pa_inode = ac->ac_inode;
3720
3721 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3722 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3723 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3724
3725 spin_lock(pa->pa_obj_lock);
3726 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3727 spin_unlock(pa->pa_obj_lock);
3728
3729 return 0;
3730}
3731
3732/*
3733 * creates new preallocated space for locality group inodes belongs to
3734 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003735static noinline_for_stack int
3736ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003737{
3738 struct super_block *sb = ac->ac_sb;
3739 struct ext4_locality_group *lg;
3740 struct ext4_prealloc_space *pa;
3741 struct ext4_group_info *grp;
3742
3743 /* preallocate only when found space is larger then requested */
3744 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3745 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3746 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3747
3748 BUG_ON(ext4_pspace_cachep == NULL);
3749 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3750 if (pa == NULL)
3751 return -ENOMEM;
3752
3753 /* preallocation can change ac_b_ex, thus we store actually
3754 * allocated blocks for history */
3755 ac->ac_f_ex = ac->ac_b_ex;
3756
3757 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3758 pa->pa_lstart = pa->pa_pstart;
3759 pa->pa_len = ac->ac_b_ex.fe_len;
3760 pa->pa_free = pa->pa_len;
3761 atomic_set(&pa->pa_count, 1);
3762 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003763 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003764 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003765 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003766 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003767
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003768 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003769 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3770 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003771
3772 ext4_mb_use_group_pa(ac, pa);
3773 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3774
3775 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3776 lg = ac->ac_lg;
3777 BUG_ON(lg == NULL);
3778
3779 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3780 pa->pa_inode = NULL;
3781
3782 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3783 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3784 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3785
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003786 /*
3787 * We will later add the new pa to the right bucket
3788 * after updating the pa_free in ext4_mb_release_context
3789 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003790 return 0;
3791}
3792
3793static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3794{
3795 int err;
3796
3797 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3798 err = ext4_mb_new_group_pa(ac);
3799 else
3800 err = ext4_mb_new_inode_pa(ac);
3801 return err;
3802}
3803
3804/*
3805 * finds all unused blocks in on-disk bitmap, frees them in
3806 * in-core bitmap and buddy.
3807 * @pa must be unlinked from inode and group lists, so that
3808 * nobody else can find/use it.
3809 * the caller MUST hold group/inode locks.
3810 * TODO: optimize the case when there are no in-core structures yet
3811 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003812static noinline_for_stack int
3813ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003814 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003815{
Alex Tomasc9de5602008-01-29 00:19:52 -05003816 struct super_block *sb = e4b->bd_sb;
3817 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003818 unsigned int end;
3819 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003820 ext4_group_t group;
3821 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003822 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003823 int err = 0;
3824 int free = 0;
3825
3826 BUG_ON(pa->pa_deleted == 0);
3827 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003828 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003829 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3830 end = bit + pa->pa_len;
3831
Alex Tomasc9de5602008-01-29 00:19:52 -05003832 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003833 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003834 if (bit >= end)
3835 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003836 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003837 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003838 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3839 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003840 free += next - bit;
3841
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003842 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003843 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3844 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003845 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003846 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3847 bit = next + 1;
3848 }
3849 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003850 ext4_msg(e4b->bd_sb, KERN_CRIT,
3851 "pa %p: logic %lu, phys. %lu, len %lu",
3852 pa, (unsigned long) pa->pa_lstart,
3853 (unsigned long) pa->pa_pstart,
3854 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003855 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003856 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003857 /*
3858 * pa is already deleted so we use the value obtained
3859 * from the bitmap and continue.
3860 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003861 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003862 atomic_add(free, &sbi->s_mb_discarded);
3863
3864 return err;
3865}
3866
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003867static noinline_for_stack int
3868ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003869 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003870{
Alex Tomasc9de5602008-01-29 00:19:52 -05003871 struct super_block *sb = e4b->bd_sb;
3872 ext4_group_t group;
3873 ext4_grpblk_t bit;
3874
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003875 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003876 BUG_ON(pa->pa_deleted == 0);
3877 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3878 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3879 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3880 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003881 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003882
3883 return 0;
3884}
3885
3886/*
3887 * releases all preallocations in given group
3888 *
3889 * first, we need to decide discard policy:
3890 * - when do we discard
3891 * 1) ENOSPC
3892 * - how many do we discard
3893 * 1) how many requested
3894 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003895static noinline_for_stack int
3896ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003897 ext4_group_t group, int needed)
3898{
3899 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3900 struct buffer_head *bitmap_bh = NULL;
3901 struct ext4_prealloc_space *pa, *tmp;
3902 struct list_head list;
3903 struct ext4_buddy e4b;
3904 int err;
3905 int busy = 0;
3906 int free = 0;
3907
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003908 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003909
3910 if (list_empty(&grp->bb_prealloc_list))
3911 return 0;
3912
Theodore Ts'o574ca172008-07-11 19:27:31 -04003913 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04003914 if (IS_ERR(bitmap_bh)) {
3915 err = PTR_ERR(bitmap_bh);
3916 ext4_error(sb, "Error %d reading block bitmap for %u",
3917 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003918 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003919 }
3920
3921 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003922 if (err) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04003923 ext4_warning(sb, "Error %d loading buddy information for %u",
3924 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003925 put_bh(bitmap_bh);
3926 return 0;
3927 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003928
3929 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003930 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003931
Alex Tomasc9de5602008-01-29 00:19:52 -05003932 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003933repeat:
3934 ext4_lock_group(sb, group);
3935 list_for_each_entry_safe(pa, tmp,
3936 &grp->bb_prealloc_list, pa_group_list) {
3937 spin_lock(&pa->pa_lock);
3938 if (atomic_read(&pa->pa_count)) {
3939 spin_unlock(&pa->pa_lock);
3940 busy = 1;
3941 continue;
3942 }
3943 if (pa->pa_deleted) {
3944 spin_unlock(&pa->pa_lock);
3945 continue;
3946 }
3947
3948 /* seems this one can be freed ... */
3949 pa->pa_deleted = 1;
3950
3951 /* we can trust pa_free ... */
3952 free += pa->pa_free;
3953
3954 spin_unlock(&pa->pa_lock);
3955
3956 list_del(&pa->pa_group_list);
3957 list_add(&pa->u.pa_tmp_list, &list);
3958 }
3959
3960 /* if we still need more blocks and some PAs were used, try again */
3961 if (free < needed && busy) {
3962 busy = 0;
3963 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003964 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003965 goto repeat;
3966 }
3967
3968 /* found anything to free? */
3969 if (list_empty(&list)) {
3970 BUG_ON(free != 0);
3971 goto out;
3972 }
3973
3974 /* now free all selected PAs */
3975 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3976
3977 /* remove from object (inode or locality group) */
3978 spin_lock(pa->pa_obj_lock);
3979 list_del_rcu(&pa->pa_inode_list);
3980 spin_unlock(pa->pa_obj_lock);
3981
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003982 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003983 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003984 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003985 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003986
3987 list_del(&pa->u.pa_tmp_list);
3988 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3989 }
3990
3991out:
3992 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003993 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003994 put_bh(bitmap_bh);
3995 return free;
3996}
3997
3998/*
3999 * releases all non-used preallocated blocks for given inode
4000 *
4001 * It's important to discard preallocations under i_data_sem
4002 * We don't want another block to be served from the prealloc
4003 * space when we are discarding the inode prealloc space.
4004 *
4005 * FIXME!! Make sure it is valid at all the call sites
4006 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004007void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05004008{
4009 struct ext4_inode_info *ei = EXT4_I(inode);
4010 struct super_block *sb = inode->i_sb;
4011 struct buffer_head *bitmap_bh = NULL;
4012 struct ext4_prealloc_space *pa, *tmp;
4013 ext4_group_t group = 0;
4014 struct list_head list;
4015 struct ext4_buddy e4b;
4016 int err;
4017
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004018 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004019 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4020 return;
4021 }
4022
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004023 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004024 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004025
4026 INIT_LIST_HEAD(&list);
4027
4028repeat:
4029 /* first, collect all pa's in the inode */
4030 spin_lock(&ei->i_prealloc_lock);
4031 while (!list_empty(&ei->i_prealloc_list)) {
4032 pa = list_entry(ei->i_prealloc_list.next,
4033 struct ext4_prealloc_space, pa_inode_list);
4034 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4035 spin_lock(&pa->pa_lock);
4036 if (atomic_read(&pa->pa_count)) {
4037 /* this shouldn't happen often - nobody should
4038 * use preallocation while we're discarding it */
4039 spin_unlock(&pa->pa_lock);
4040 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004041 ext4_msg(sb, KERN_ERR,
4042 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05004043 WARN_ON(1);
4044 schedule_timeout_uninterruptible(HZ);
4045 goto repeat;
4046
4047 }
4048 if (pa->pa_deleted == 0) {
4049 pa->pa_deleted = 1;
4050 spin_unlock(&pa->pa_lock);
4051 list_del_rcu(&pa->pa_inode_list);
4052 list_add(&pa->u.pa_tmp_list, &list);
4053 continue;
4054 }
4055
4056 /* someone is deleting pa right now */
4057 spin_unlock(&pa->pa_lock);
4058 spin_unlock(&ei->i_prealloc_lock);
4059
4060 /* we have to wait here because pa_deleted
4061 * doesn't mean pa is already unlinked from
4062 * the list. as we might be called from
4063 * ->clear_inode() the inode will get freed
4064 * and concurrent thread which is unlinking
4065 * pa from inode's list may access already
4066 * freed memory, bad-bad-bad */
4067
4068 /* XXX: if this happens too often, we can
4069 * add a flag to force wait only in case
4070 * of ->clear_inode(), but not in case of
4071 * regular truncate */
4072 schedule_timeout_uninterruptible(HZ);
4073 goto repeat;
4074 }
4075 spin_unlock(&ei->i_prealloc_lock);
4076
4077 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004078 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04004079 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05004080
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004081 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4082 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004083 if (err) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004084 ext4_error(sb, "Error %d loading buddy information for %u",
4085 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004086 continue;
4087 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004088
Theodore Ts'o574ca172008-07-11 19:27:31 -04004089 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004090 if (IS_ERR(bitmap_bh)) {
4091 err = PTR_ERR(bitmap_bh);
4092 ext4_error(sb, "Error %d reading block bitmap for %u",
4093 err, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004094 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004095 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004096 }
4097
4098 ext4_lock_group(sb, group);
4099 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004100 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004101 ext4_unlock_group(sb, group);
4102
Jing Zhange39e07f2010-05-14 00:00:00 -04004103 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004104 put_bh(bitmap_bh);
4105
4106 list_del(&pa->u.pa_tmp_list);
4107 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4108 }
4109}
4110
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004111#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004112static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4113{
4114 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004115 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004116
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05004117 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05004118 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004119 return;
4120
Joe Perches7f6a11e2012-03-19 23:09:43 -04004121 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004122 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04004123 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004124 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004125 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004126 "goal %lu/%lu/%lu@%lu, "
4127 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004128 (unsigned long)ac->ac_o_ex.fe_group,
4129 (unsigned long)ac->ac_o_ex.fe_start,
4130 (unsigned long)ac->ac_o_ex.fe_len,
4131 (unsigned long)ac->ac_o_ex.fe_logical,
4132 (unsigned long)ac->ac_g_ex.fe_group,
4133 (unsigned long)ac->ac_g_ex.fe_start,
4134 (unsigned long)ac->ac_g_ex.fe_len,
4135 (unsigned long)ac->ac_g_ex.fe_logical,
4136 (unsigned long)ac->ac_b_ex.fe_group,
4137 (unsigned long)ac->ac_b_ex.fe_start,
4138 (unsigned long)ac->ac_b_ex.fe_len,
4139 (unsigned long)ac->ac_b_ex.fe_logical,
4140 (int)ac->ac_criteria);
Eric Sandeendc9ddd92014-02-20 13:32:10 -05004141 ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004142 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004143 ngroups = ext4_get_groups_count(sb);
4144 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004145 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4146 struct ext4_prealloc_space *pa;
4147 ext4_grpblk_t start;
4148 struct list_head *cur;
4149 ext4_lock_group(sb, i);
4150 list_for_each(cur, &grp->bb_prealloc_list) {
4151 pa = list_entry(cur, struct ext4_prealloc_space,
4152 pa_group_list);
4153 spin_lock(&pa->pa_lock);
4154 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4155 NULL, &start);
4156 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004157 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4158 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004159 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004160 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004161
4162 if (grp->bb_free == 0)
4163 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004164 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004165 i, grp->bb_free, grp->bb_fragments);
4166 }
4167 printk(KERN_ERR "\n");
4168}
4169#else
4170static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4171{
4172 return;
4173}
4174#endif
4175
4176/*
4177 * We use locality group preallocation for small size file. The size of the
4178 * file is determined by the current size or the resulting size after
4179 * allocation which ever is larger
4180 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004181 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004182 */
4183static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4184{
4185 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4186 int bsbits = ac->ac_sb->s_blocksize_bits;
4187 loff_t size, isize;
4188
4189 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4190 return;
4191
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004192 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4193 return;
4194
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004195 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004196 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4197 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004198
Theodore Ts'o50797482009-09-18 13:34:02 -04004199 if ((size == isize) &&
4200 !ext4_fs_is_busy(sbi) &&
4201 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4202 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4203 return;
4204 }
4205
Robin Dongebbe0272011-10-26 05:14:27 -04004206 if (sbi->s_mb_group_prealloc <= 0) {
4207 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4208 return;
4209 }
4210
Alex Tomasc9de5602008-01-29 00:19:52 -05004211 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004212 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004213 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004214 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004215 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004216 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004217
4218 BUG_ON(ac->ac_lg != NULL);
4219 /*
4220 * locality group prealloc space are per cpu. The reason for having
4221 * per cpu locality group is to reduce the contention between block
4222 * request from multiple CPUs.
4223 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004224 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004225
4226 /* we're going to use group allocation */
4227 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4228
4229 /* serialize all allocations in the group */
4230 mutex_lock(&ac->ac_lg->lg_mutex);
4231}
4232
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004233static noinline_for_stack int
4234ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004235 struct ext4_allocation_request *ar)
4236{
4237 struct super_block *sb = ar->inode->i_sb;
4238 struct ext4_sb_info *sbi = EXT4_SB(sb);
4239 struct ext4_super_block *es = sbi->s_es;
4240 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004241 unsigned int len;
4242 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004243 ext4_grpblk_t block;
4244
4245 /* we can't allocate > group size */
4246 len = ar->len;
4247
4248 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004249 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4250 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004251
4252 /* start searching from the goal */
4253 goal = ar->goal;
4254 if (goal < le32_to_cpu(es->s_first_data_block) ||
4255 goal >= ext4_blocks_count(es))
4256 goal = le32_to_cpu(es->s_first_data_block);
4257 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4258
4259 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004260 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004261 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004262 ac->ac_sb = sb;
4263 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004264 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004265 ac->ac_o_ex.fe_group = group;
4266 ac->ac_o_ex.fe_start = block;
4267 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004268 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004269 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004270
4271 /* we have to define context: we'll we work with a file or
4272 * locality group. this is a policy, actually */
4273 ext4_mb_group_or_file(ac);
4274
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004275 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004276 "left: %u/%u, right %u/%u to %swritable\n",
4277 (unsigned) ar->len, (unsigned) ar->logical,
4278 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4279 (unsigned) ar->lleft, (unsigned) ar->pleft,
4280 (unsigned) ar->lright, (unsigned) ar->pright,
4281 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4282 return 0;
4283
4284}
4285
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004286static noinline_for_stack void
4287ext4_mb_discard_lg_preallocations(struct super_block *sb,
4288 struct ext4_locality_group *lg,
4289 int order, int total_entries)
4290{
4291 ext4_group_t group = 0;
4292 struct ext4_buddy e4b;
4293 struct list_head discard_list;
4294 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004295
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004296 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004297
4298 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004299
4300 spin_lock(&lg->lg_prealloc_lock);
4301 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4302 pa_inode_list) {
4303 spin_lock(&pa->pa_lock);
4304 if (atomic_read(&pa->pa_count)) {
4305 /*
4306 * This is the pa that we just used
4307 * for block allocation. So don't
4308 * free that
4309 */
4310 spin_unlock(&pa->pa_lock);
4311 continue;
4312 }
4313 if (pa->pa_deleted) {
4314 spin_unlock(&pa->pa_lock);
4315 continue;
4316 }
4317 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004318 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004319
4320 /* seems this one can be freed ... */
4321 pa->pa_deleted = 1;
4322 spin_unlock(&pa->pa_lock);
4323
4324 list_del_rcu(&pa->pa_inode_list);
4325 list_add(&pa->u.pa_tmp_list, &discard_list);
4326
4327 total_entries--;
4328 if (total_entries <= 5) {
4329 /*
4330 * we want to keep only 5 entries
4331 * allowing it to grow to 8. This
4332 * mak sure we don't call discard
4333 * soon for this list.
4334 */
4335 break;
4336 }
4337 }
4338 spin_unlock(&lg->lg_prealloc_lock);
4339
4340 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004341 int err;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004342
Lukas Czernerbd862982013-04-03 23:32:34 -04004343 group = ext4_get_group_number(sb, pa->pa_pstart);
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004344 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4345 GFP_NOFS|__GFP_NOFAIL);
4346 if (err) {
4347 ext4_error(sb, "Error %d loading buddy information for %u",
4348 err, group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004349 continue;
4350 }
4351 ext4_lock_group(sb, group);
4352 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004353 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004354 ext4_unlock_group(sb, group);
4355
Jing Zhange39e07f2010-05-14 00:00:00 -04004356 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004357 list_del(&pa->u.pa_tmp_list);
4358 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4359 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004360}
4361
4362/*
4363 * We have incremented pa_count. So it cannot be freed at this
4364 * point. Also we hold lg_mutex. So no parallel allocation is
4365 * possible from this lg. That means pa_free cannot be updated.
4366 *
4367 * A parallel ext4_mb_discard_group_preallocations is possible.
4368 * which can cause the lg_prealloc_list to be updated.
4369 */
4370
4371static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4372{
4373 int order, added = 0, lg_prealloc_count = 1;
4374 struct super_block *sb = ac->ac_sb;
4375 struct ext4_locality_group *lg = ac->ac_lg;
4376 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4377
4378 order = fls(pa->pa_free) - 1;
4379 if (order > PREALLOC_TB_SIZE - 1)
4380 /* The max size of hash table is PREALLOC_TB_SIZE */
4381 order = PREALLOC_TB_SIZE - 1;
4382 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004383 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004384 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4385 pa_inode_list) {
4386 spin_lock(&tmp_pa->pa_lock);
4387 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004388 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004389 continue;
4390 }
4391 if (!added && pa->pa_free < tmp_pa->pa_free) {
4392 /* Add to the tail of the previous entry */
4393 list_add_tail_rcu(&pa->pa_inode_list,
4394 &tmp_pa->pa_inode_list);
4395 added = 1;
4396 /*
4397 * we want to count the total
4398 * number of entries in the list
4399 */
4400 }
4401 spin_unlock(&tmp_pa->pa_lock);
4402 lg_prealloc_count++;
4403 }
4404 if (!added)
4405 list_add_tail_rcu(&pa->pa_inode_list,
4406 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004407 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004408
4409 /* Now trim the list to be not more than 8 elements */
4410 if (lg_prealloc_count > 8) {
4411 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004412 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004413 return;
4414 }
4415 return ;
4416}
4417
Alex Tomasc9de5602008-01-29 00:19:52 -05004418/*
4419 * release all resource we used in allocation
4420 */
4421static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4422{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004423 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004424 struct ext4_prealloc_space *pa = ac->ac_pa;
4425 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004426 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004427 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004428 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004429 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4430 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004431 pa->pa_free -= ac->ac_b_ex.fe_len;
4432 pa->pa_len -= ac->ac_b_ex.fe_len;
4433 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004434 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004435 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004436 if (pa) {
4437 /*
4438 * We want to add the pa to the right bucket.
4439 * Remove it from the list and while adding
4440 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004441 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004442 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004443 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004444 spin_lock(pa->pa_obj_lock);
4445 list_del_rcu(&pa->pa_inode_list);
4446 spin_unlock(pa->pa_obj_lock);
4447 ext4_mb_add_n_trim(ac);
4448 }
4449 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4450 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004451 if (ac->ac_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004452 put_page(ac->ac_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004453 if (ac->ac_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004454 put_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004455 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4456 mutex_unlock(&ac->ac_lg->lg_mutex);
4457 ext4_mb_collect_stats(ac);
4458 return 0;
4459}
4460
4461static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4462{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004463 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004464 int ret;
4465 int freed = 0;
4466
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004467 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004468 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004469 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4470 freed += ret;
4471 needed -= ret;
4472 }
4473
4474 return freed;
4475}
4476
4477/*
4478 * Main entry point into mballoc to allocate blocks
4479 * it tries to use preallocation first, then falls back
4480 * to usual allocation
4481 */
4482ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004483 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004484{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004485 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004486 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004487 struct ext4_sb_info *sbi;
4488 struct super_block *sb;
4489 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004490 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004491 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004492
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004493 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004494 sb = ar->inode->i_sb;
4495 sbi = EXT4_SB(sb);
4496
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004497 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004498
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004499 /* Allow to use superuser reservation for quota file */
Tahsin Erdogan02749a42017-06-22 11:31:25 -04004500 if (ext4_is_quota_file(ar->inode))
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004501 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4502
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004503 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004504 /* Without delayed allocation we need to verify
4505 * there is enough free blocks to do block allocation
4506 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004507 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004508 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004509 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004510
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004511 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004512 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004513 ar->len = ar->len >> 1;
4514 }
4515 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004516 *errp = -ENOSPC;
4517 return 0;
4518 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004519 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004520 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004521 dquot_alloc_block_nofail(ar->inode,
4522 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004523 } else {
4524 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004525 dquot_alloc_block(ar->inode,
4526 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004527
4528 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4529 ar->len--;
4530 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004531 }
4532 inquota = ar->len;
4533 if (ar->len == 0) {
4534 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004535 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004536 }
Mingming Caod2a17632008-07-14 17:52:37 -04004537 }
Mingming Caod2a17632008-07-14 17:52:37 -04004538
Wei Yongjun85556c92012-09-26 20:43:37 -04004539 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004540 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004541 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004542 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004543 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004544 }
4545
Eric Sandeen256bdb42008-02-10 01:13:33 -05004546 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004547 if (*errp) {
4548 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004549 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004550 }
4551
Eric Sandeen256bdb42008-02-10 01:13:33 -05004552 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4553 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004554 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4555 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004556repeat:
4557 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004558 *errp = ext4_mb_regular_allocator(ac);
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004559 if (*errp)
4560 goto discard_and_exit;
4561
4562 /* as we've just preallocated more space than
4563 * user requested originally, we store allocated
4564 * space in a special descriptor */
4565 if (ac->ac_status == AC_STATUS_FOUND &&
4566 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4567 *errp = ext4_mb_new_preallocation(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004568 if (*errp) {
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004569 discard_and_exit:
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004570 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004571 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004572 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004573 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004574 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004575 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Vegard Nossum554a5cc2016-07-14 23:02:47 -04004576 if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004577 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004578 goto errout;
4579 } else {
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004580 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4581 ar->len = ac->ac_b_ex.fe_len;
4582 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004583 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004584 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004585 if (freed)
4586 goto repeat;
4587 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004588 }
4589
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004590errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004591 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004592 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004593 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004594 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004595 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004596 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004597out:
4598 if (ac)
4599 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004600 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004601 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004602 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004603 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004604 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004605 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004606 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004607 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004608
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004609 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004610
Alex Tomasc9de5602008-01-29 00:19:52 -05004611 return block;
4612}
Alex Tomasc9de5602008-01-29 00:19:52 -05004613
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004614/*
4615 * We can merge two free data extents only if the physical blocks
4616 * are contiguous, AND the extents were freed by the same transaction,
4617 * AND the blocks are associated with the same group.
4618 */
Daeho Jeonga0154342017-06-22 23:54:33 -04004619static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi,
4620 struct ext4_free_data *entry,
4621 struct ext4_free_data *new_entry,
4622 struct rb_root *entry_rb_root)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004623{
Daeho Jeonga0154342017-06-22 23:54:33 -04004624 if ((entry->efd_tid != new_entry->efd_tid) ||
4625 (entry->efd_group != new_entry->efd_group))
4626 return;
4627 if (entry->efd_start_cluster + entry->efd_count ==
4628 new_entry->efd_start_cluster) {
4629 new_entry->efd_start_cluster = entry->efd_start_cluster;
4630 new_entry->efd_count += entry->efd_count;
4631 } else if (new_entry->efd_start_cluster + new_entry->efd_count ==
4632 entry->efd_start_cluster) {
4633 new_entry->efd_count += entry->efd_count;
4634 } else
4635 return;
4636 spin_lock(&sbi->s_md_lock);
4637 list_del(&entry->efd_list);
4638 spin_unlock(&sbi->s_md_lock);
4639 rb_erase(&entry->efd_node, entry_rb_root);
4640 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004641}
4642
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004643static noinline_for_stack int
4644ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004645 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004646{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004647 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004648 ext4_grpblk_t cluster;
Theodore Ts'od08854f2016-06-26 18:24:01 -04004649 ext4_grpblk_t clusters = new_entry->efd_count;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004650 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004651 struct ext4_group_info *db = e4b->bd_info;
4652 struct super_block *sb = e4b->bd_sb;
4653 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004654 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4655 struct rb_node *parent = NULL, *new_node;
4656
Frank Mayhar03901312009-01-07 00:06:22 -05004657 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004658 BUG_ON(e4b->bd_bitmap_page == NULL);
4659 BUG_ON(e4b->bd_buddy_page == NULL);
4660
Bobi Jam18aadd42012-02-20 17:53:02 -05004661 new_node = &new_entry->efd_node;
4662 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004663
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004664 if (!*n) {
4665 /* first free block exent. We need to
4666 protect buddy cache from being freed,
4667 * otherwise we'll refresh it from
4668 * on-disk bitmap and lose not-yet-available
4669 * blocks */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004670 get_page(e4b->bd_buddy_page);
4671 get_page(e4b->bd_bitmap_page);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004672 }
4673 while (*n) {
4674 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004675 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4676 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004677 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004678 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004679 n = &(*n)->rb_right;
4680 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004681 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004682 ext4_group_first_block_no(sb, group) +
4683 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004684 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004685 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004686 }
4687 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004688
4689 rb_link_node(new_node, parent, n);
4690 rb_insert_color(new_node, &db->bb_free_root);
4691
4692 /* Now try to see the extent can be merged to left and right */
4693 node = rb_prev(new_node);
4694 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004695 entry = rb_entry(node, struct ext4_free_data, efd_node);
Daeho Jeonga0154342017-06-22 23:54:33 -04004696 ext4_try_merge_freed_extent(sbi, entry, new_entry,
4697 &(db->bb_free_root));
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004698 }
4699
4700 node = rb_next(new_node);
4701 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004702 entry = rb_entry(node, struct ext4_free_data, efd_node);
Daeho Jeonga0154342017-06-22 23:54:33 -04004703 ext4_try_merge_freed_extent(sbi, entry, new_entry,
4704 &(db->bb_free_root));
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004705 }
Daeho Jeonga0154342017-06-22 23:54:33 -04004706
Theodore Ts'od08854f2016-06-26 18:24:01 -04004707 spin_lock(&sbi->s_md_lock);
Daeho Jeonga0154342017-06-22 23:54:33 -04004708 list_add_tail(&new_entry->efd_list, &sbi->s_freed_data_list);
Theodore Ts'od08854f2016-06-26 18:24:01 -04004709 sbi->s_mb_free_pending += clusters;
4710 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004711 return 0;
4712}
4713
Theodore Ts'o44338712009-11-22 07:44:56 -05004714/**
4715 * ext4_free_blocks() -- Free given blocks and update quota
4716 * @handle: handle for this transaction
4717 * @inode: inode
4718 * @block: start physical block to free
4719 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004720 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004721 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004722void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004723 struct buffer_head *bh, ext4_fsblk_t block,
4724 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004725{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004726 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004727 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004728 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004729 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004730 ext4_grpblk_t bit;
4731 struct buffer_head *gd_bh;
4732 ext4_group_t block_group;
4733 struct ext4_sb_info *sbi;
4734 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004735 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004736 int err = 0;
4737 int ret;
4738
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004739 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004740 if (bh) {
4741 if (block)
4742 BUG_ON(block != bh->b_blocknr);
4743 else
4744 block = bh->b_blocknr;
4745 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004746
Alex Tomasc9de5602008-01-29 00:19:52 -05004747 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004748 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4749 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004750 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004751 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004752 goto error_return;
4753 }
4754
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004755 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004756 trace_ext4_free_blocks(inode, block, count, flags);
4757
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004758 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4759 BUG_ON(count > 1);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004760
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004761 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4762 inode, bh, block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004763 }
4764
Theodore Ts'o60e66792010-05-17 07:00:00 -04004765 /*
Theodore Ts'o84130192011-09-09 18:50:51 -04004766 * If the extent to be freed does not begin on a cluster
4767 * boundary, we need to deal with partial clusters at the
4768 * beginning and end of the extent. Normally we will free
4769 * blocks at the beginning or the end unless we are explicitly
4770 * requested to avoid doing so.
4771 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004772 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004773 if (overflow) {
4774 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4775 overflow = sbi->s_cluster_ratio - overflow;
4776 block += overflow;
4777 if (count > overflow)
4778 count -= overflow;
4779 else
4780 return;
4781 } else {
4782 block -= overflow;
4783 count += overflow;
4784 }
4785 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004786 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004787 if (overflow) {
4788 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4789 if (count > overflow)
4790 count -= overflow;
4791 else
4792 return;
4793 } else
4794 count += sbi->s_cluster_ratio - overflow;
4795 }
4796
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004797 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4798 int i;
Daeho Jeongf96c4502016-02-21 18:31:41 -05004799 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004800
4801 for (i = 0; i < count; i++) {
4802 cond_resched();
Daeho Jeongf96c4502016-02-21 18:31:41 -05004803 if (is_metadata)
4804 bh = sb_find_get_block(inode->i_sb, block + i);
4805 ext4_forget(handle, is_metadata, inode, bh, block + i);
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004806 }
4807 }
4808
Alex Tomasc9de5602008-01-29 00:19:52 -05004809do_more:
4810 overflow = 0;
4811 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4812
Darrick J. Wong163a2032013-08-28 17:35:51 -04004813 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4814 ext4_get_group_info(sb, block_group))))
4815 return;
4816
Alex Tomasc9de5602008-01-29 00:19:52 -05004817 /*
4818 * Check to see if we are freeing blocks across a group
4819 * boundary.
4820 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004821 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4822 overflow = EXT4_C2B(sbi, bit) + count -
4823 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004824 count -= overflow;
4825 }
Lukas Czerner810da242013-03-02 17:18:58 -05004826 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004827 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004828 if (IS_ERR(bitmap_bh)) {
4829 err = PTR_ERR(bitmap_bh);
4830 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004831 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004832 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004833 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004834 if (!gdp) {
4835 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004836 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004837 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004838
4839 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4840 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4841 in_range(block, ext4_inode_table(sb, gdp),
Jun Piao49598e02018-01-11 13:17:49 -05004842 sbi->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004843 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Jun Piao49598e02018-01-11 13:17:49 -05004844 sbi->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004845
Eric Sandeen12062dd2010-02-15 14:19:27 -05004846 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004847 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004848 /* err = 0. ext4_std_error should be a no op */
4849 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004850 }
4851
4852 BUFFER_TRACE(bitmap_bh, "getting write access");
4853 err = ext4_journal_get_write_access(handle, bitmap_bh);
4854 if (err)
4855 goto error_return;
4856
4857 /*
4858 * We are about to modify some metadata. Call the journal APIs
4859 * to unshare ->b_data if a currently-committing transaction is
4860 * using it
4861 */
4862 BUFFER_TRACE(gd_bh, "get_write_access");
4863 err = ext4_journal_get_write_access(handle, gd_bh);
4864 if (err)
4865 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004866#ifdef AGGRESSIVE_CHECK
4867 {
4868 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004869 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004870 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4871 }
4872#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004873 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004874
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04004875 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
4876 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
4877 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004878 if (err)
4879 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004880
Daeho Jeongf96c4502016-02-21 18:31:41 -05004881 /*
4882 * We need to make sure we don't reuse the freed block until after the
4883 * transaction is committed. We make an exception if the inode is to be
4884 * written in writeback mode since writeback mode has weak data
4885 * consistency guarantees.
4886 */
4887 if (ext4_handle_valid(handle) &&
4888 ((flags & EXT4_FREE_BLOCKS_METADATA) ||
4889 !ext4_should_writeback_data(inode))) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004890 struct ext4_free_data *new_entry;
4891 /*
Michal Hocko7444a072015-07-05 12:33:44 -04004892 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4893 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004894 */
Michal Hocko7444a072015-07-05 12:33:44 -04004895 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
4896 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05004897 new_entry->efd_start_cluster = bit;
4898 new_entry->efd_group = block_group;
4899 new_entry->efd_count = count_clusters;
4900 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004901
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004902 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004903 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004904 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004905 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004906 /* need to update group_info->bb_free and bitmap
4907 * with group lock held. generate_buddy look at
4908 * them with group lock_held
4909 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004910 if (test_opt(sb, DISCARD)) {
Daeho Jeonga0154342017-06-22 23:54:33 -04004911 err = ext4_issue_discard(sb, block_group, bit, count,
4912 NULL);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004913 if (err && err != -EOPNOTSUPP)
4914 ext4_msg(sb, KERN_WARNING, "discard request in"
4915 " group:%d block:%d count:%lu failed"
4916 " with %d", block_group, bit, count,
4917 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04004918 } else
4919 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004920
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004921 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004922 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4923 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004924 }
4925
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004926 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4927 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004928 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004929 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004930 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004931
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004932 if (sbi->s_log_groups_per_flex) {
4933 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004934 atomic64_add(count_clusters,
4935 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004936 }
4937
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04004938 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004939 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
Jan Kara7d734532013-08-17 09:36:54 -04004940 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
4941
4942 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004943
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004944 /* We dirtied the bitmap block */
4945 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4946 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4947
Alex Tomasc9de5602008-01-29 00:19:52 -05004948 /* And the group descriptor block */
4949 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004950 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004951 if (!err)
4952 err = ret;
4953
4954 if (overflow && !err) {
4955 block += count;
4956 count = overflow;
4957 put_bh(bitmap_bh);
4958 goto do_more;
4959 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004960error_return:
4961 brelse(bitmap_bh);
4962 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004963 return;
4964}
Lukas Czerner7360d172010-10-27 21:30:12 -04004965
4966/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004967 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004968 * @handle: handle to this transaction
4969 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004970 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004971 * @count: number of blocks to free
4972 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004973 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004974 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004975int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004976 ext4_fsblk_t block, unsigned long count)
4977{
4978 struct buffer_head *bitmap_bh = NULL;
4979 struct buffer_head *gd_bh;
4980 ext4_group_t block_group;
4981 ext4_grpblk_t bit;
4982 unsigned int i;
4983 struct ext4_group_desc *desc;
4984 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004985 struct ext4_buddy e4b;
harshadsd77147f2017-10-29 09:38:46 -04004986 int err = 0, ret, free_clusters_count;
4987 ext4_grpblk_t clusters_freed;
4988 ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block);
4989 ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1);
4990 unsigned long cluster_count = last_cluster - first_cluster + 1;
Amir Goldstein2846e822011-05-09 10:46:41 -04004991
4992 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4993
Yongqiang Yang4740b832011-07-26 21:51:08 -04004994 if (count == 0)
4995 return 0;
4996
Amir Goldstein2846e822011-05-09 10:46:41 -04004997 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004998 /*
4999 * Check to see if we are freeing blocks across a group
5000 * boundary.
5001 */
harshadsd77147f2017-10-29 09:38:46 -04005002 if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) {
5003 ext4_warning(sb, "too many blocks added to group %u",
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005004 block_group);
5005 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005006 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005007 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005008
Amir Goldstein2846e822011-05-09 10:46:41 -04005009 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04005010 if (IS_ERR(bitmap_bh)) {
5011 err = PTR_ERR(bitmap_bh);
5012 bitmap_bh = NULL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005013 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005014 }
5015
Amir Goldstein2846e822011-05-09 10:46:41 -04005016 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005017 if (!desc) {
5018 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04005019 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005020 }
Amir Goldstein2846e822011-05-09 10:46:41 -04005021
5022 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
5023 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
5024 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
5025 in_range(block + count - 1, ext4_inode_table(sb, desc),
5026 sbi->s_itb_per_group)) {
5027 ext4_error(sb, "Adding blocks in system zones - "
5028 "Block = %llu, count = %lu",
5029 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005030 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005031 goto error_return;
5032 }
5033
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005034 BUFFER_TRACE(bitmap_bh, "getting write access");
5035 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04005036 if (err)
5037 goto error_return;
5038
5039 /*
5040 * We are about to modify some metadata. Call the journal APIs
5041 * to unshare ->b_data if a currently-committing transaction is
5042 * using it
5043 */
5044 BUFFER_TRACE(gd_bh, "get_write_access");
5045 err = ext4_journal_get_write_access(handle, gd_bh);
5046 if (err)
5047 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04005048
harshadsd77147f2017-10-29 09:38:46 -04005049 for (i = 0, clusters_freed = 0; i < cluster_count; i++) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005050 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04005051 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005052 ext4_error(sb, "bit already cleared for block %llu",
5053 (ext4_fsblk_t)(block + i));
5054 BUFFER_TRACE(bitmap_bh, "bit already cleared");
5055 } else {
harshadsd77147f2017-10-29 09:38:46 -04005056 clusters_freed++;
Amir Goldstein2846e822011-05-09 10:46:41 -04005057 }
5058 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005059
5060 err = ext4_mb_load_buddy(sb, block_group, &e4b);
5061 if (err)
5062 goto error_return;
5063
5064 /*
5065 * need to update group_info->bb_free and bitmap
5066 * with group lock held. generate_buddy look at
5067 * them with group lock_held
5068 */
Amir Goldstein2846e822011-05-09 10:46:41 -04005069 ext4_lock_group(sb, block_group);
harshadsd77147f2017-10-29 09:38:46 -04005070 mb_clear_bits(bitmap_bh->b_data, bit, cluster_count);
5071 mb_free_blocks(NULL, &e4b, bit, cluster_count);
5072 free_clusters_count = clusters_freed +
5073 ext4_free_group_clusters(sb, desc);
5074 ext4_free_group_clusters_set(sb, desc, free_clusters_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04005075 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005076 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04005077 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04005078 percpu_counter_add(&sbi->s_freeclusters_counter,
harshadsd77147f2017-10-29 09:38:46 -04005079 clusters_freed);
Amir Goldstein2846e822011-05-09 10:46:41 -04005080
5081 if (sbi->s_log_groups_per_flex) {
5082 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
harshadsd77147f2017-10-29 09:38:46 -04005083 atomic64_add(clusters_freed,
Theodore Ts'o90ba9832013-03-11 23:39:59 -04005084 &sbi->s_flex_groups[flex_group].free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04005085 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005086
5087 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005088
5089 /* We dirtied the bitmap block */
5090 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5091 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5092
5093 /* And the group descriptor block */
5094 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5095 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5096 if (!err)
5097 err = ret;
5098
5099error_return:
5100 brelse(bitmap_bh);
5101 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005102 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005103}
5104
5105/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005106 * ext4_trim_extent -- function to TRIM one single free extent in the group
5107 * @sb: super block for the file system
5108 * @start: starting block of the free extent in the alloc. group
5109 * @count: number of blocks to TRIM
5110 * @group: alloc. group we are working with
5111 * @e4b: ext4 buddy for the group
5112 *
5113 * Trim "count" blocks starting at "start" in the "group". To assure that no
5114 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5115 * be called with under the group lock.
5116 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005117static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04005118 ext4_group_t group, struct ext4_buddy *e4b)
jon ernste2cbd582014-04-12 23:01:28 -04005119__releases(bitlock)
5120__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005121{
5122 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005123 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005124
Tao Mab3d4c2b2011-07-11 00:01:52 -04005125 trace_ext4_trim_extent(sb, group, start, count);
5126
Lukas Czerner7360d172010-10-27 21:30:12 -04005127 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5128
5129 ex.fe_start = start;
5130 ex.fe_group = group;
5131 ex.fe_len = count;
5132
5133 /*
5134 * Mark blocks used, so no one can reuse them while
5135 * being trimmed.
5136 */
5137 mb_mark_used(e4b, &ex);
5138 ext4_unlock_group(sb, group);
Daeho Jeonga0154342017-06-22 23:54:33 -04005139 ret = ext4_issue_discard(sb, group, start, count, NULL);
Lukas Czerner7360d172010-10-27 21:30:12 -04005140 ext4_lock_group(sb, group);
5141 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005142 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005143}
5144
5145/**
5146 * ext4_trim_all_free -- function to trim all free space in alloc. group
5147 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005148 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005149 * @start: first group block to examine
5150 * @max: last group block to examine
5151 * @minblocks: minimum extent block count
5152 *
5153 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5154 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5155 * the extent.
5156 *
5157 *
5158 * ext4_trim_all_free walks through group's block bitmap searching for free
5159 * extents. When the free extent is found, mark it as used in group buddy
5160 * bitmap. Then issue a TRIM command on this extent and free the extent in
5161 * the group buddy bitmap. This is done until whole group is scanned.
5162 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005163static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005164ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5165 ext4_grpblk_t start, ext4_grpblk_t max,
5166 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005167{
5168 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005169 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005170 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005171 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005172
Tao Mab3d4c2b2011-07-11 00:01:52 -04005173 trace_ext4_trim_all_free(sb, group, start, max);
5174
Lukas Czerner78944082011-05-24 18:16:27 -04005175 ret = ext4_mb_load_buddy(sb, group, &e4b);
5176 if (ret) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04005177 ext4_warning(sb, "Error %d loading buddy information for %u",
5178 ret, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005179 return ret;
5180 }
Lukas Czerner78944082011-05-24 18:16:27 -04005181 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005182
5183 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005184 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5185 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5186 goto out;
5187
Lukas Czerner78944082011-05-24 18:16:27 -04005188 start = (e4b.bd_info->bb_first_free > start) ?
5189 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005190
Lukas Czerner913eed832012-03-21 21:22:22 -04005191 while (start <= max) {
5192 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5193 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005194 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005195 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005196
5197 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005198 ret = ext4_trim_extent(sb, start,
5199 next - start, group, &e4b);
5200 if (ret && ret != -EOPNOTSUPP)
5201 break;
5202 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005203 count += next - start;
5204 }
Tao Ma169ddc32011-07-11 00:00:07 -04005205 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005206 start = next + 1;
5207
5208 if (fatal_signal_pending(current)) {
5209 count = -ERESTARTSYS;
5210 break;
5211 }
5212
5213 if (need_resched()) {
5214 ext4_unlock_group(sb, group);
5215 cond_resched();
5216 ext4_lock_group(sb, group);
5217 }
5218
Tao Ma169ddc32011-07-11 00:00:07 -04005219 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005220 break;
5221 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005222
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005223 if (!ret) {
5224 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005225 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005226 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005227out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005228 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005229 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005230
5231 ext4_debug("trimmed %d blocks in the group %d\n",
5232 count, group);
5233
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005234 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005235}
5236
5237/**
5238 * ext4_trim_fs() -- trim ioctl handle function
5239 * @sb: superblock for filesystem
5240 * @range: fstrim_range structure
5241 *
5242 * start: First Byte to trim
5243 * len: number of Bytes to trim from start
5244 * minlen: minimum extent length in Bytes
5245 * ext4_trim_fs goes through all allocation groups containing Bytes from
5246 * start to start+len. For each such a group ext4_trim_all_free function
5247 * is invoked to trim all free space.
5248 */
5249int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
5250{
Lukas Czerner78944082011-05-24 18:16:27 -04005251 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005252 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005253 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005254 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005255 ext4_fsblk_t first_data_blk =
5256 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005257 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005258 int ret = 0;
5259
5260 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005261 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005262 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5263 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005264
Lukas Czerner5de35e82012-10-22 18:01:19 -04005265 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5266 start >= max_blks ||
5267 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005268 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005269 if (end >= max_blks)
5270 end = max_blks - 1;
5271 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005272 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005273 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005274 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005275
Lukas Czerner913eed832012-03-21 21:22:22 -04005276 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005277 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005278 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005279 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005280 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005281
Lukas Czerner913eed832012-03-21 21:22:22 -04005282 /* end now represents the last cluster to discard in this group */
5283 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005284
5285 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005286 grp = ext4_get_group_info(sb, group);
5287 /* We only do this if the grp has never been initialized */
5288 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005289 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Lukas Czerner78944082011-05-24 18:16:27 -04005290 if (ret)
5291 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005292 }
5293
Tao Ma0ba08512011-03-23 15:48:11 -04005294 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005295 * For all the groups except the last one, last cluster will
5296 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5297 * change it for the last group, note that last_cluster is
5298 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005299 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005300 if (group == last_group)
5301 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005302
Lukas Czerner78944082011-05-24 18:16:27 -04005303 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005304 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005305 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005306 if (cnt < 0) {
5307 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005308 break;
5309 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005310 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005311 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005312
5313 /*
5314 * For every group except the first one, we are sure
5315 * that the first cluster to discard will be cluster #0.
5316 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005317 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005318 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005319
Tao Ma3d56b8d2011-07-11 00:03:38 -04005320 if (!ret)
5321 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5322
Tao Ma22f10452011-07-10 23:52:37 -04005323out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005324 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005325 return ret;
5326}
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -04005327
5328/* Iterate all the free extents in the group. */
5329int
5330ext4_mballoc_query_range(
5331 struct super_block *sb,
5332 ext4_group_t group,
5333 ext4_grpblk_t start,
5334 ext4_grpblk_t end,
5335 ext4_mballoc_query_range_fn formatter,
5336 void *priv)
5337{
5338 void *bitmap;
5339 ext4_grpblk_t next;
5340 struct ext4_buddy e4b;
5341 int error;
5342
5343 error = ext4_mb_load_buddy(sb, group, &e4b);
5344 if (error)
5345 return error;
5346 bitmap = e4b.bd_bitmap;
5347
5348 ext4_lock_group(sb, group);
5349
5350 start = (e4b.bd_info->bb_first_free > start) ?
5351 e4b.bd_info->bb_first_free : start;
5352 if (end >= EXT4_CLUSTERS_PER_GROUP(sb))
5353 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
5354
5355 while (start <= end) {
5356 start = mb_find_next_zero_bit(bitmap, end + 1, start);
5357 if (start > end)
5358 break;
5359 next = mb_find_next_bit(bitmap, end + 1, start);
5360
5361 ext4_unlock_group(sb, group);
5362 error = formatter(sb, group, start, next - start, priv);
5363 if (error)
5364 goto out_unload;
5365 ext4_lock_group(sb, group);
5366
5367 start = next + 1;
5368 }
5369
5370 ext4_unlock_group(sb, group);
5371out_unload:
5372 ext4_mb_unload_buddy(&e4b);
5373
5374 return error;
5375}