blob: 769a62708b1cf64168e884159f9cb554dff816bc [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);
Alex Tomasc9de5602008-01-29 00:19:52 -0500473 }
474 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
475 }
476}
477
478static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
479{
480 int i;
481
482 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
483 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400484 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500485 for (i = 0; i < count; i++) {
486 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
487 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
488 }
489}
490
491static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
492{
493 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
494 unsigned char *b1, *b2;
495 int i;
496 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
497 b2 = (unsigned char *) bitmap;
498 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
499 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400500 ext4_msg(e4b->bd_sb, KERN_ERR,
501 "corruption in group %u "
502 "at byte %u(%u): %x in copy != %x "
503 "on disk/prealloc",
504 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500505 BUG();
506 }
507 }
508 }
509}
510
511#else
512static inline void mb_free_blocks_double(struct inode *inode,
513 struct ext4_buddy *e4b, int first, int count)
514{
515 return;
516}
517static inline void mb_mark_used_double(struct ext4_buddy *e4b,
518 int first, int count)
519{
520 return;
521}
522static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
523{
524 return;
525}
526#endif
527
528#ifdef AGGRESSIVE_CHECK
529
530#define MB_CHECK_ASSERT(assert) \
531do { \
532 if (!(assert)) { \
533 printk(KERN_EMERG \
534 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
535 function, file, line, # assert); \
536 BUG(); \
537 } \
538} while (0)
539
540static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
541 const char *function, int line)
542{
543 struct super_block *sb = e4b->bd_sb;
544 int order = e4b->bd_blkbits + 1;
545 int max;
546 int max2;
547 int i;
548 int j;
549 int k;
550 int count;
551 struct ext4_group_info *grp;
552 int fragments = 0;
553 int fstart;
554 struct list_head *cur;
555 void *buddy;
556 void *buddy2;
557
Alex Tomasc9de5602008-01-29 00:19:52 -0500558 {
559 static int mb_check_counter;
560 if (mb_check_counter++ % 100 != 0)
561 return 0;
562 }
563
564 while (order > 1) {
565 buddy = mb_find_buddy(e4b, order, &max);
566 MB_CHECK_ASSERT(buddy);
567 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
568 MB_CHECK_ASSERT(buddy2);
569 MB_CHECK_ASSERT(buddy != buddy2);
570 MB_CHECK_ASSERT(max * 2 == max2);
571
572 count = 0;
573 for (i = 0; i < max; i++) {
574
575 if (mb_test_bit(i, buddy)) {
576 /* only single bit in buddy2 may be 1 */
577 if (!mb_test_bit(i << 1, buddy2)) {
578 MB_CHECK_ASSERT(
579 mb_test_bit((i<<1)+1, buddy2));
580 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
581 MB_CHECK_ASSERT(
582 mb_test_bit(i << 1, buddy2));
583 }
584 continue;
585 }
586
Robin Dong0a10da72011-10-26 08:48:54 -0400587 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500588 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
589 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
590
591 for (j = 0; j < (1 << order); j++) {
592 k = (i * (1 << order)) + j;
593 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500594 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500595 }
596 count++;
597 }
598 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
599 order--;
600 }
601
602 fstart = -1;
603 buddy = mb_find_buddy(e4b, 0, &max);
604 for (i = 0; i < max; i++) {
605 if (!mb_test_bit(i, buddy)) {
606 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
607 if (fstart == -1) {
608 fragments++;
609 fstart = i;
610 }
611 continue;
612 }
613 fstart = -1;
614 /* check used bits only */
615 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
616 buddy2 = mb_find_buddy(e4b, j, &max2);
617 k = i >> j;
618 MB_CHECK_ASSERT(k < max2);
619 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
620 }
621 }
622 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
623 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
624
625 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500626 list_for_each(cur, &grp->bb_prealloc_list) {
627 ext4_group_t groupnr;
628 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400629 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
630 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500631 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400632 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500633 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
634 }
635 return 0;
636}
637#undef MB_CHECK_ASSERT
638#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400639 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500640#else
641#define mb_check_buddy(e4b)
642#endif
643
Coly Li7c786052011-02-24 13:24:25 -0500644/*
645 * Divide blocks started from @first with length @len into
646 * smaller chunks with power of 2 blocks.
647 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
648 * then increase bb_counters[] for corresponded chunk size.
649 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500650static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400651 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500652 struct ext4_group_info *grp)
653{
654 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400655 ext4_grpblk_t min;
656 ext4_grpblk_t max;
657 ext4_grpblk_t chunk;
Chandan Rajendra69e43e82016-11-14 21:04:37 -0500658 unsigned int border;
Alex Tomasc9de5602008-01-29 00:19:52 -0500659
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400660 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500661
662 border = 2 << sb->s_blocksize_bits;
663
664 while (len > 0) {
665 /* find how many blocks can be covered since this position */
666 max = ffs(first | border) - 1;
667
668 /* find how many blocks of power 2 we need to mark */
669 min = fls(len) - 1;
670
671 if (max < min)
672 min = max;
673 chunk = 1 << min;
674
675 /* mark multiblock chunks only */
676 grp->bb_counters[min]++;
677 if (min > 0)
678 mb_clear_bit(first >> min,
679 buddy + sbi->s_mb_offsets[min]);
680
681 len -= chunk;
682 first += chunk;
683 }
684}
685
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400686/*
687 * Cache the order of the largest free extent we have available in this block
688 * group.
689 */
690static void
691mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
692{
693 int i;
694 int bits;
695
696 grp->bb_largest_free_order = -1; /* uninit */
697
698 bits = sb->s_blocksize_bits + 1;
699 for (i = bits; i >= 0; i--) {
700 if (grp->bb_counters[i] > 0) {
701 grp->bb_largest_free_order = i;
702 break;
703 }
704 }
705}
706
Eric Sandeen089ceec2009-07-05 22:17:31 -0400707static noinline_for_stack
708void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500709 void *buddy, void *bitmap, ext4_group_t group)
710{
711 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400712 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400713 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400714 ext4_grpblk_t i = 0;
715 ext4_grpblk_t first;
716 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500717 unsigned free = 0;
718 unsigned fragments = 0;
719 unsigned long long period = get_cycles();
720
721 /* initialize buddy from bitmap which is aggregation
722 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500723 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500724 grp->bb_first_free = i;
725 while (i < max) {
726 fragments++;
727 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500728 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500729 len = i - first;
730 free += len;
731 if (len > 1)
732 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
733 else
734 grp->bb_counters[0]++;
735 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500736 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500737 }
738 grp->bb_fragments = fragments;
739
740 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400741 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o94d4c062014-07-05 19:15:50 -0400742 "block bitmap and bg descriptor "
743 "inconsistent: %u vs %u free clusters",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400744 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500745 /*
Darrick J. Wong163a2032013-08-28 17:35:51 -0400746 * If we intend to continue, we consider group descriptor
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500747 * corrupt and update bb_free using bitmap value
748 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500749 grp->bb_free = free;
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400750 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
751 percpu_counter_sub(&sbi->s_freeclusters_counter,
752 grp->bb_free);
Darrick J. Wong163a2032013-08-28 17:35:51 -0400753 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
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);
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001457 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))
1458 percpu_counter_sub(&sbi->s_freeclusters_counter,
1459 e4b->bd_info->bb_free);
Darrick J. Wong163a2032013-08-28 17:35:51 -04001460 /* Mark the block group as corrupt. */
1461 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1462 &e4b->bd_info->bb_state);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001463 mb_regenerate_buddy(e4b);
1464 goto done;
1465 }
1466
1467 /* let's maintain fragments counter */
1468 if (left_is_free && right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001469 e4b->bd_info->bb_fragments--;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001470 else if (!left_is_free && !right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001471 e4b->bd_info->bb_fragments++;
1472
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001473 /* buddy[0] == bd_bitmap is a special case, so handle
1474 * it right away and let mb_buddy_mark_free stay free of
1475 * zero order checks.
1476 * Check if neighbours are to be coaleasced,
1477 * adjust bitmap bb_counters and borders appropriately.
1478 */
1479 if (first & 1) {
1480 first += !left_is_free;
1481 e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001482 }
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001483 if (!(last & 1)) {
1484 last -= !right_is_free;
1485 e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1;
1486 }
1487
1488 if (first <= last)
1489 mb_buddy_mark_free(e4b, first >> 1, last >> 1);
1490
1491done:
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001492 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001493 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001494}
1495
Robin Dong15c006a2012-08-17 10:02:17 -04001496static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001497 int needed, struct ext4_free_extent *ex)
1498{
1499 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001500 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001501 void *buddy;
1502
Vincent Minetbc8e6742009-05-15 08:33:18 -04001503 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001504 BUG_ON(ex == NULL);
1505
Robin Dong15c006a2012-08-17 10:02:17 -04001506 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001507 BUG_ON(buddy == NULL);
1508 BUG_ON(block >= max);
1509 if (mb_test_bit(block, buddy)) {
1510 ex->fe_len = 0;
1511 ex->fe_start = 0;
1512 ex->fe_group = 0;
1513 return 0;
1514 }
1515
Robin Dong15c006a2012-08-17 10:02:17 -04001516 /* find actual order */
1517 order = mb_find_order_for_block(e4b, block);
1518 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001519
1520 ex->fe_len = 1 << order;
1521 ex->fe_start = block << order;
1522 ex->fe_group = e4b->bd_group;
1523
1524 /* calc difference from given start */
1525 next = next - ex->fe_start;
1526 ex->fe_len -= next;
1527 ex->fe_start += next;
1528
1529 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001530 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001531
1532 if (block + 1 >= max)
1533 break;
1534
1535 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001536 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001537 break;
1538
Robin Dongb051d8d2011-10-26 05:30:30 -04001539 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001540
Alex Tomasc9de5602008-01-29 00:19:52 -05001541 block = next >> order;
1542 ex->fe_len += 1 << order;
1543 }
1544
Theodore Ts'o43c73222017-01-22 19:35:52 -05001545 if (ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3))) {
1546 /* Should never happen! (but apparently sometimes does?!?) */
1547 WARN_ON(1);
1548 ext4_error(e4b->bd_sb, "corruption or bug in mb_find_extent "
1549 "block=%d, order=%d needed=%d ex=%u/%d/%d@%u",
1550 block, order, needed, ex->fe_group, ex->fe_start,
1551 ex->fe_len, ex->fe_logical);
1552 ex->fe_len = 0;
1553 ex->fe_start = 0;
1554 ex->fe_group = 0;
1555 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001556 return ex->fe_len;
1557}
1558
1559static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1560{
1561 int ord;
1562 int mlen = 0;
1563 int max = 0;
1564 int cur;
1565 int start = ex->fe_start;
1566 int len = ex->fe_len;
1567 unsigned ret = 0;
1568 int len0 = len;
1569 void *buddy;
1570
1571 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1572 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001573 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001574 mb_check_buddy(e4b);
1575 mb_mark_used_double(e4b, start, len);
1576
1577 e4b->bd_info->bb_free -= len;
1578 if (e4b->bd_info->bb_first_free == start)
1579 e4b->bd_info->bb_first_free += len;
1580
1581 /* let's maintain fragments counter */
1582 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001583 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001584 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001585 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001586 if (mlen && max)
1587 e4b->bd_info->bb_fragments++;
1588 else if (!mlen && !max)
1589 e4b->bd_info->bb_fragments--;
1590
1591 /* let's maintain buddy itself */
1592 while (len) {
1593 ord = mb_find_order_for_block(e4b, start);
1594
1595 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1596 /* the whole chunk may be allocated at once! */
1597 mlen = 1 << ord;
1598 buddy = mb_find_buddy(e4b, ord, &max);
1599 BUG_ON((start >> ord) >= max);
1600 mb_set_bit(start >> ord, buddy);
1601 e4b->bd_info->bb_counters[ord]--;
1602 start += mlen;
1603 len -= mlen;
1604 BUG_ON(len < 0);
1605 continue;
1606 }
1607
1608 /* store for history */
1609 if (ret == 0)
1610 ret = len | (ord << 16);
1611
1612 /* we have to split large buddy */
1613 BUG_ON(ord <= 0);
1614 buddy = mb_find_buddy(e4b, ord, &max);
1615 mb_set_bit(start >> ord, buddy);
1616 e4b->bd_info->bb_counters[ord]--;
1617
1618 ord--;
1619 cur = (start >> ord) & ~1U;
1620 buddy = mb_find_buddy(e4b, ord, &max);
1621 mb_clear_bit(cur, buddy);
1622 mb_clear_bit(cur + 1, buddy);
1623 e4b->bd_info->bb_counters[ord]++;
1624 e4b->bd_info->bb_counters[ord]++;
1625 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001626 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001627
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001628 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001629 mb_check_buddy(e4b);
1630
1631 return ret;
1632}
1633
1634/*
1635 * Must be called under group lock!
1636 */
1637static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1638 struct ext4_buddy *e4b)
1639{
1640 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1641 int ret;
1642
1643 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1644 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1645
1646 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1647 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1648 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1649
1650 /* preallocation can change ac_b_ex, thus we store actually
1651 * allocated blocks for history */
1652 ac->ac_f_ex = ac->ac_b_ex;
1653
1654 ac->ac_status = AC_STATUS_FOUND;
1655 ac->ac_tail = ret & 0xffff;
1656 ac->ac_buddy = ret >> 16;
1657
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001658 /*
1659 * take the page reference. We want the page to be pinned
1660 * so that we don't get a ext4_mb_init_cache_call for this
1661 * group until we update the bitmap. That would mean we
1662 * double allocate blocks. The reference is dropped
1663 * in ext4_mb_release_context
1664 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001665 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1666 get_page(ac->ac_bitmap_page);
1667 ac->ac_buddy_page = e4b->bd_buddy_page;
1668 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001669 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001670 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001671 spin_lock(&sbi->s_md_lock);
1672 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1673 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1674 spin_unlock(&sbi->s_md_lock);
1675 }
1676}
1677
1678/*
1679 * regular allocator, for general purposes allocation
1680 */
1681
1682static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1683 struct ext4_buddy *e4b,
1684 int finish_group)
1685{
1686 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1687 struct ext4_free_extent *bex = &ac->ac_b_ex;
1688 struct ext4_free_extent *gex = &ac->ac_g_ex;
1689 struct ext4_free_extent ex;
1690 int max;
1691
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001692 if (ac->ac_status == AC_STATUS_FOUND)
1693 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001694 /*
1695 * We don't want to scan for a whole year
1696 */
1697 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1698 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1699 ac->ac_status = AC_STATUS_BREAK;
1700 return;
1701 }
1702
1703 /*
1704 * Haven't found good chunk so far, let's continue
1705 */
1706 if (bex->fe_len < gex->fe_len)
1707 return;
1708
1709 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1710 && bex->fe_group == e4b->bd_group) {
1711 /* recheck chunk's availability - we don't know
1712 * when it was found (within this lock-unlock
1713 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001714 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001715 if (max >= gex->fe_len) {
1716 ext4_mb_use_best_found(ac, e4b);
1717 return;
1718 }
1719 }
1720}
1721
1722/*
1723 * The routine checks whether found extent is good enough. If it is,
1724 * then the extent gets marked used and flag is set to the context
1725 * to stop scanning. Otherwise, the extent is compared with the
1726 * previous found extent and if new one is better, then it's stored
1727 * in the context. Later, the best found extent will be used, if
1728 * mballoc can't find good enough extent.
1729 *
1730 * FIXME: real allocation policy is to be designed yet!
1731 */
1732static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1733 struct ext4_free_extent *ex,
1734 struct ext4_buddy *e4b)
1735{
1736 struct ext4_free_extent *bex = &ac->ac_b_ex;
1737 struct ext4_free_extent *gex = &ac->ac_g_ex;
1738
1739 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001740 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1741 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001742 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1743
1744 ac->ac_found++;
1745
1746 /*
1747 * The special case - take what you catch first
1748 */
1749 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1750 *bex = *ex;
1751 ext4_mb_use_best_found(ac, e4b);
1752 return;
1753 }
1754
1755 /*
1756 * Let's check whether the chuck is good enough
1757 */
1758 if (ex->fe_len == gex->fe_len) {
1759 *bex = *ex;
1760 ext4_mb_use_best_found(ac, e4b);
1761 return;
1762 }
1763
1764 /*
1765 * If this is first found extent, just store it in the context
1766 */
1767 if (bex->fe_len == 0) {
1768 *bex = *ex;
1769 return;
1770 }
1771
1772 /*
1773 * If new found extent is better, store it in the context
1774 */
1775 if (bex->fe_len < gex->fe_len) {
1776 /* if the request isn't satisfied, any found extent
1777 * larger than previous best one is better */
1778 if (ex->fe_len > bex->fe_len)
1779 *bex = *ex;
1780 } else if (ex->fe_len > gex->fe_len) {
1781 /* if the request is satisfied, then we try to find
1782 * an extent that still satisfy the request, but is
1783 * smaller than previous one */
1784 if (ex->fe_len < bex->fe_len)
1785 *bex = *ex;
1786 }
1787
1788 ext4_mb_check_limits(ac, e4b, 0);
1789}
1790
Eric Sandeen089ceec2009-07-05 22:17:31 -04001791static noinline_for_stack
1792int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001793 struct ext4_buddy *e4b)
1794{
1795 struct ext4_free_extent ex = ac->ac_b_ex;
1796 ext4_group_t group = ex.fe_group;
1797 int max;
1798 int err;
1799
1800 BUG_ON(ex.fe_len <= 0);
1801 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1802 if (err)
1803 return err;
1804
1805 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001806 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001807
1808 if (max > 0) {
1809 ac->ac_b_ex = ex;
1810 ext4_mb_use_best_found(ac, e4b);
1811 }
1812
1813 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001814 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001815
1816 return 0;
1817}
1818
Eric Sandeen089ceec2009-07-05 22:17:31 -04001819static noinline_for_stack
1820int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001821 struct ext4_buddy *e4b)
1822{
1823 ext4_group_t group = ac->ac_g_ex.fe_group;
1824 int max;
1825 int err;
1826 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001827 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001828 struct ext4_free_extent ex;
1829
1830 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1831 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001832 if (grp->bb_free == 0)
1833 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001834
1835 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1836 if (err)
1837 return err;
1838
Darrick J. Wong163a2032013-08-28 17:35:51 -04001839 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) {
1840 ext4_mb_unload_buddy(e4b);
1841 return 0;
1842 }
1843
Alex Tomasc9de5602008-01-29 00:19:52 -05001844 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001845 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001846 ac->ac_g_ex.fe_len, &ex);
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001847 ex.fe_logical = 0xDEADFA11; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001848
1849 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1850 ext4_fsblk_t start;
1851
Akinobu Mita5661bd62010-03-03 23:53:39 -05001852 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1853 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001854 /* use do_div to get remainder (would be 64-bit modulo) */
1855 if (do_div(start, sbi->s_stripe) == 0) {
1856 ac->ac_found++;
1857 ac->ac_b_ex = ex;
1858 ext4_mb_use_best_found(ac, e4b);
1859 }
1860 } else if (max >= ac->ac_g_ex.fe_len) {
1861 BUG_ON(ex.fe_len <= 0);
1862 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1863 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1864 ac->ac_found++;
1865 ac->ac_b_ex = ex;
1866 ext4_mb_use_best_found(ac, e4b);
1867 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1868 /* Sometimes, caller may want to merge even small
1869 * number of blocks to an existing extent */
1870 BUG_ON(ex.fe_len <= 0);
1871 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1872 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1873 ac->ac_found++;
1874 ac->ac_b_ex = ex;
1875 ext4_mb_use_best_found(ac, e4b);
1876 }
1877 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001878 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001879
1880 return 0;
1881}
1882
1883/*
1884 * The routine scans buddy structures (not bitmap!) from given order
1885 * to max order and tries to find big enough chunk to satisfy the req
1886 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001887static noinline_for_stack
1888void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001889 struct ext4_buddy *e4b)
1890{
1891 struct super_block *sb = ac->ac_sb;
1892 struct ext4_group_info *grp = e4b->bd_info;
1893 void *buddy;
1894 int i;
1895 int k;
1896 int max;
1897
1898 BUG_ON(ac->ac_2order <= 0);
1899 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1900 if (grp->bb_counters[i] == 0)
1901 continue;
1902
1903 buddy = mb_find_buddy(e4b, i, &max);
1904 BUG_ON(buddy == NULL);
1905
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001906 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001907 BUG_ON(k >= max);
1908
1909 ac->ac_found++;
1910
1911 ac->ac_b_ex.fe_len = 1 << i;
1912 ac->ac_b_ex.fe_start = k << i;
1913 ac->ac_b_ex.fe_group = e4b->bd_group;
1914
1915 ext4_mb_use_best_found(ac, e4b);
1916
1917 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1918
1919 if (EXT4_SB(sb)->s_mb_stats)
1920 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1921
1922 break;
1923 }
1924}
1925
1926/*
1927 * The routine scans the group and measures all found extents.
1928 * In order to optimize scanning, caller must pass number of
1929 * free blocks in the group, so the routine can know upper limit.
1930 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001931static noinline_for_stack
1932void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001933 struct ext4_buddy *e4b)
1934{
1935 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001936 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001937 struct ext4_free_extent ex;
1938 int i;
1939 int free;
1940
1941 free = e4b->bd_info->bb_free;
1942 BUG_ON(free <= 0);
1943
1944 i = e4b->bd_info->bb_first_free;
1945
1946 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001947 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001948 EXT4_CLUSTERS_PER_GROUP(sb), i);
1949 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001950 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001951 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001952 * free blocks even though group info says we
1953 * we have free blocks
1954 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001955 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001956 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001957 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001958 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001959 break;
1960 }
1961
Robin Dong15c006a2012-08-17 10:02:17 -04001962 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001963 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001964 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001965 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001966 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001967 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001968 free, ex.fe_len);
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 */
2447 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2448 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002449 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002450 } else {
2451 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002452 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002453 }
2454
2455 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002456 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002457 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002458 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002459
2460#ifdef DOUBLE_CHECK
2461 {
2462 struct buffer_head *bh;
2463 meta_group_info[i]->bb_bitmap =
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002464 kmalloc(sb->s_blocksize, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002465 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2466 bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002467 BUG_ON(IS_ERR_OR_NULL(bh));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002468 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2469 sb->s_blocksize);
2470 put_bh(bh);
2471 }
2472#endif
2473
2474 return 0;
2475
2476exit_group_info:
2477 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002478 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002479 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002480 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2481 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002482exit_meta_group_info:
2483 return -ENOMEM;
2484} /* ext4_mb_add_groupinfo */
2485
Alex Tomasc9de5602008-01-29 00:19:52 -05002486static int ext4_mb_init_backend(struct super_block *sb)
2487{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002488 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002489 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002490 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002491 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002492 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002493 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002494
Theodore Ts'o28623c22012-09-05 01:31:50 -04002495 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2496 if (err)
2497 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002498
Alex Tomasc9de5602008-01-29 00:19:52 -05002499 sbi->s_buddy_cache = new_inode(sb);
2500 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002501 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002502 goto err_freesgi;
2503 }
Yu Jian48e60612011-08-01 17:41:39 -04002504 /* To avoid potentially colliding with an valid on-disk inode number,
2505 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2506 * not in the inode hash, so it should never be found by iget(), but
2507 * this will avoid confusion if it ever shows up during debugging. */
2508 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002509 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002510 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002511 desc = ext4_get_group_desc(sb, i, NULL);
2512 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002513 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002514 goto err_freebuddy;
2515 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002516 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2517 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002518 }
2519
2520 return 0;
2521
2522err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002523 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002524 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002525 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002526 i = sbi->s_group_info_size;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002527 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002528 kfree(sbi->s_group_info[i]);
2529 iput(sbi->s_buddy_cache);
2530err_freesgi:
Al Virob93b41d2014-11-20 12:19:11 -05002531 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002532 return -ENOMEM;
2533}
2534
Eric Sandeen2892c152011-02-12 08:12:18 -05002535static void ext4_groupinfo_destroy_slabs(void)
2536{
2537 int i;
2538
2539 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2540 if (ext4_groupinfo_caches[i])
2541 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2542 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);
Alex Tomasc9de5602008-01-29 00:19:52 -05003014 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3015 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003016 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04003017 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003018 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05003019 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003020 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
3021 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04003022 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003023 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003024
3025 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04003026 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003027 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003028 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003029 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003030 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3031 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04003032 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
3033 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05003034
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003035 if (sbi->s_log_groups_per_flex) {
3036 ext4_group_t flex_group = ext4_flex_group(sbi,
3037 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04003038 atomic64_sub(ac->ac_b_ex.fe_len,
3039 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003040 }
3041
Frank Mayhar03901312009-01-07 00:06:22 -05003042 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003043 if (err)
3044 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003045 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003046
3047out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003048 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003049 return err;
3050}
3051
3052/*
3053 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003054 * Group request are normalized to s_mb_group_prealloc, which goes to
3055 * s_strip if we set the same via mount option.
3056 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003057 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003058 *
3059 * XXX: should we try to preallocate more than the group has now?
3060 */
3061static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3062{
3063 struct super_block *sb = ac->ac_sb;
3064 struct ext4_locality_group *lg = ac->ac_lg;
3065
3066 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003067 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003068 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003069 current->pid, ac->ac_g_ex.fe_len);
3070}
3071
3072/*
3073 * Normalization means making request better in terms of
3074 * size and alignment
3075 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003076static noinline_for_stack void
3077ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003078 struct ext4_allocation_request *ar)
3079{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003080 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003081 int bsbits, max;
3082 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003083 loff_t size, start_off;
3084 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003085 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003086 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003087 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003088
3089 /* do normalize only data requests, metadata requests
3090 do not need preallocation */
3091 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3092 return;
3093
3094 /* sometime caller may want exact blocks */
3095 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3096 return;
3097
3098 /* caller may indicate that preallocation isn't
3099 * required (it's a tail, for example) */
3100 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3101 return;
3102
3103 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3104 ext4_mb_normalize_group_request(ac);
3105 return ;
3106 }
3107
3108 bsbits = ac->ac_sb->s_blocksize_bits;
3109
3110 /* first, let's learn actual file size
3111 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003112 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003113 size = size << bsbits;
3114 if (size < i_size_read(ac->ac_inode))
3115 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003116 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003117
Valerie Clement19304792008-05-13 19:31:14 -04003118 /* max size of free chunks */
3119 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003120
Valerie Clement19304792008-05-13 19:31:14 -04003121#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3122 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003123
3124 /* first, try to predict filesize */
3125 /* XXX: should this table be tunable? */
3126 start_off = 0;
3127 if (size <= 16 * 1024) {
3128 size = 16 * 1024;
3129 } else if (size <= 32 * 1024) {
3130 size = 32 * 1024;
3131 } else if (size <= 64 * 1024) {
3132 size = 64 * 1024;
3133 } else if (size <= 128 * 1024) {
3134 size = 128 * 1024;
3135 } else if (size <= 256 * 1024) {
3136 size = 256 * 1024;
3137 } else if (size <= 512 * 1024) {
3138 size = 512 * 1024;
3139 } else if (size <= 1024 * 1024) {
3140 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003141 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003142 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003143 (21 - bsbits)) << 21;
3144 size = 2 * 1024 * 1024;
3145 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003146 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3147 (22 - bsbits)) << 22;
3148 size = 4 * 1024 * 1024;
3149 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003150 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003151 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3152 (23 - bsbits)) << 23;
3153 size = 8 * 1024 * 1024;
3154 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003155 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3156 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3157 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003158 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003159 size = size >> bsbits;
3160 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003161
3162 /* don't cover already allocated blocks in selected range */
3163 if (ar->pleft && start <= ar->lleft) {
3164 size -= ar->lleft + 1 - start;
3165 start = ar->lleft + 1;
3166 }
3167 if (ar->pright && start + size - 1 >= ar->lright)
3168 size -= start + size - ar->lright;
3169
Jan Karacd648b82017-01-27 14:34:30 -05003170 /*
3171 * Trim allocation request for filesystems with artificially small
3172 * groups.
3173 */
3174 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
3175 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
3176
Alex Tomasc9de5602008-01-29 00:19:52 -05003177 end = start + size;
3178
3179 /* check we don't cross already preallocated blocks */
3180 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003181 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003182 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003183
Alex Tomasc9de5602008-01-29 00:19:52 -05003184 if (pa->pa_deleted)
3185 continue;
3186 spin_lock(&pa->pa_lock);
3187 if (pa->pa_deleted) {
3188 spin_unlock(&pa->pa_lock);
3189 continue;
3190 }
3191
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003192 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3193 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003194
3195 /* PA must not overlap original request */
3196 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3197 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3198
Eric Sandeen38877f42009-08-17 23:55:24 -04003199 /* skip PAs this normalized request doesn't overlap with */
3200 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003201 spin_unlock(&pa->pa_lock);
3202 continue;
3203 }
3204 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3205
Eric Sandeen38877f42009-08-17 23:55:24 -04003206 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003207 if (pa_end <= ac->ac_o_ex.fe_logical) {
3208 BUG_ON(pa_end < start);
3209 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003210 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003211 BUG_ON(pa->pa_lstart > end);
3212 end = pa->pa_lstart;
3213 }
3214 spin_unlock(&pa->pa_lock);
3215 }
3216 rcu_read_unlock();
3217 size = end - start;
3218
3219 /* XXX: extra loop to check we really don't overlap preallocations */
3220 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003221 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003222 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003223
Alex Tomasc9de5602008-01-29 00:19:52 -05003224 spin_lock(&pa->pa_lock);
3225 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003226 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3227 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003228 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3229 }
3230 spin_unlock(&pa->pa_lock);
3231 }
3232 rcu_read_unlock();
3233
3234 if (start + size <= ac->ac_o_ex.fe_logical &&
3235 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003236 ext4_msg(ac->ac_sb, KERN_ERR,
3237 "start %lu, size %lu, fe_logical %lu",
3238 (unsigned long) start, (unsigned long) size,
3239 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003240 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003241 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003242 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003243
3244 /* now prepare goal request */
3245
3246 /* XXX: is it better to align blocks WRT to logical
3247 * placement or satisfy big request as is */
3248 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003249 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003250
3251 /* define goal start in order to merge */
3252 if (ar->pright && (ar->lright == (start + size))) {
3253 /* merge to the right */
3254 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3255 &ac->ac_f_ex.fe_group,
3256 &ac->ac_f_ex.fe_start);
3257 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3258 }
3259 if (ar->pleft && (ar->lleft + 1 == start)) {
3260 /* merge to the left */
3261 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3262 &ac->ac_f_ex.fe_group,
3263 &ac->ac_f_ex.fe_start);
3264 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3265 }
3266
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003267 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003268 (unsigned) orig_size, (unsigned) start);
3269}
3270
3271static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3272{
3273 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3274
3275 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3276 atomic_inc(&sbi->s_bal_reqs);
3277 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003278 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003279 atomic_inc(&sbi->s_bal_success);
3280 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3281 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3282 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3283 atomic_inc(&sbi->s_bal_goals);
3284 if (ac->ac_found > sbi->s_mb_max_to_scan)
3285 atomic_inc(&sbi->s_bal_breaks);
3286 }
3287
Theodore Ts'o296c3552009-09-30 00:32:42 -04003288 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3289 trace_ext4_mballoc_alloc(ac);
3290 else
3291 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003292}
3293
3294/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003295 * Called on failure; free up any blocks from the inode PA for this
3296 * context. We don't need this for MB_GROUP_PA because we only change
3297 * pa_free in ext4_mb_release_context(), but on failure, we've already
3298 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3299 */
3300static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3301{
3302 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003303 struct ext4_buddy e4b;
3304 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003305
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003306 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003307 if (ac->ac_f_ex.fe_len == 0)
3308 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003309 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3310 if (err) {
3311 /*
3312 * This should never happen since we pin the
3313 * pages in the ext4_allocation_context so
3314 * ext4_mb_load_buddy() should never fail.
3315 */
3316 WARN(1, "mb_load_buddy failed (%d)", err);
3317 return;
3318 }
3319 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3320 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3321 ac->ac_f_ex.fe_len);
3322 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003323 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003324 return;
3325 }
3326 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003327 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003328}
3329
3330/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003331 * use blocks preallocated to inode
3332 */
3333static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3334 struct ext4_prealloc_space *pa)
3335{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003336 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003337 ext4_fsblk_t start;
3338 ext4_fsblk_t end;
3339 int len;
3340
3341 /* found preallocated blocks, use them */
3342 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003343 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3344 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3345 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003346 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3347 &ac->ac_b_ex.fe_start);
3348 ac->ac_b_ex.fe_len = len;
3349 ac->ac_status = AC_STATUS_FOUND;
3350 ac->ac_pa = pa;
3351
3352 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003353 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003354 BUG_ON(pa->pa_free < len);
3355 pa->pa_free -= len;
3356
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003357 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003358}
3359
3360/*
3361 * use blocks preallocated to locality group
3362 */
3363static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3364 struct ext4_prealloc_space *pa)
3365{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003366 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003367
Alex Tomasc9de5602008-01-29 00:19:52 -05003368 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3369 &ac->ac_b_ex.fe_group,
3370 &ac->ac_b_ex.fe_start);
3371 ac->ac_b_ex.fe_len = len;
3372 ac->ac_status = AC_STATUS_FOUND;
3373 ac->ac_pa = pa;
3374
3375 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003376 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003377 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003378 * in on-disk bitmap -- see ext4_mb_release_context()
3379 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003380 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003381 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003382}
3383
3384/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003385 * Return the prealloc space that have minimal distance
3386 * from the goal block. @cpa is the prealloc
3387 * space that is having currently known minimal distance
3388 * from the goal block.
3389 */
3390static struct ext4_prealloc_space *
3391ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3392 struct ext4_prealloc_space *pa,
3393 struct ext4_prealloc_space *cpa)
3394{
3395 ext4_fsblk_t cur_distance, new_distance;
3396
3397 if (cpa == NULL) {
3398 atomic_inc(&pa->pa_count);
3399 return pa;
3400 }
Andrew Morton79211c82015-11-09 14:58:13 -08003401 cur_distance = abs(goal_block - cpa->pa_pstart);
3402 new_distance = abs(goal_block - pa->pa_pstart);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003403
Coly Li5a54b2f2011-02-24 14:10:05 -05003404 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003405 return cpa;
3406
3407 /* drop the previous reference */
3408 atomic_dec(&cpa->pa_count);
3409 atomic_inc(&pa->pa_count);
3410 return pa;
3411}
3412
3413/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003414 * search goal blocks in preallocated space
3415 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003416static noinline_for_stack int
3417ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003418{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003419 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003420 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003421 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3422 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003423 struct ext4_prealloc_space *pa, *cpa = NULL;
3424 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003425
3426 /* only data can be preallocated */
3427 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3428 return 0;
3429
3430 /* first, try per-file preallocation */
3431 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003432 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003433
3434 /* all fields in this condition don't change,
3435 * so we can skip locking for them */
3436 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003437 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3438 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003439 continue;
3440
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003441 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003442 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003443 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3444 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003445 continue;
3446
Alex Tomasc9de5602008-01-29 00:19:52 -05003447 /* found preallocated blocks, use them */
3448 spin_lock(&pa->pa_lock);
3449 if (pa->pa_deleted == 0 && pa->pa_free) {
3450 atomic_inc(&pa->pa_count);
3451 ext4_mb_use_inode_pa(ac, pa);
3452 spin_unlock(&pa->pa_lock);
3453 ac->ac_criteria = 10;
3454 rcu_read_unlock();
3455 return 1;
3456 }
3457 spin_unlock(&pa->pa_lock);
3458 }
3459 rcu_read_unlock();
3460
3461 /* can we use group allocation? */
3462 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3463 return 0;
3464
3465 /* inode may have no locality group for some reason */
3466 lg = ac->ac_lg;
3467 if (lg == NULL)
3468 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003469 order = fls(ac->ac_o_ex.fe_len) - 1;
3470 if (order > PREALLOC_TB_SIZE - 1)
3471 /* The max size of hash table is PREALLOC_TB_SIZE */
3472 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003473
Akinobu Mitabda00de2010-03-03 23:53:25 -05003474 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003475 /*
3476 * search for the prealloc space that is having
3477 * minimal distance from the goal block.
3478 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003479 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3480 rcu_read_lock();
3481 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3482 pa_inode_list) {
3483 spin_lock(&pa->pa_lock);
3484 if (pa->pa_deleted == 0 &&
3485 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003486
3487 cpa = ext4_mb_check_group_pa(goal_block,
3488 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003489 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003490 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003491 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003492 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003493 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003494 if (cpa) {
3495 ext4_mb_use_group_pa(ac, cpa);
3496 ac->ac_criteria = 20;
3497 return 1;
3498 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003499 return 0;
3500}
3501
3502/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003503 * the function goes through all block freed in the group
3504 * but not yet committed and marks them used in in-core bitmap.
3505 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003506 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003507 */
3508static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3509 ext4_group_t group)
3510{
3511 struct rb_node *n;
3512 struct ext4_group_info *grp;
3513 struct ext4_free_data *entry;
3514
3515 grp = ext4_get_group_info(sb, group);
3516 n = rb_first(&(grp->bb_free_root));
3517
3518 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003519 entry = rb_entry(n, struct ext4_free_data, efd_node);
3520 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003521 n = rb_next(n);
3522 }
3523 return;
3524}
3525
3526/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003527 * the function goes through all preallocation in this group and marks them
3528 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003529 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003530 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003531static noinline_for_stack
3532void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003533 ext4_group_t group)
3534{
3535 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3536 struct ext4_prealloc_space *pa;
3537 struct list_head *cur;
3538 ext4_group_t groupnr;
3539 ext4_grpblk_t start;
3540 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003541 int len;
3542
3543 /* all form of preallocation discards first load group,
3544 * so the only competing code is preallocation use.
3545 * we don't need any locking here
3546 * notice we do NOT ignore preallocations with pa_deleted
3547 * otherwise we could leave used blocks available for
3548 * allocation in buddy when concurrent ext4_mb_put_pa()
3549 * is dropping preallocation
3550 */
3551 list_for_each(cur, &grp->bb_prealloc_list) {
3552 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3553 spin_lock(&pa->pa_lock);
3554 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3555 &groupnr, &start);
3556 len = pa->pa_len;
3557 spin_unlock(&pa->pa_lock);
3558 if (unlikely(len == 0))
3559 continue;
3560 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003561 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003562 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003563 }
Colin Ian Kingff950152017-07-06 15:28:45 -04003564 mb_debug(1, "preallocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003565}
3566
3567static void ext4_mb_pa_callback(struct rcu_head *head)
3568{
3569 struct ext4_prealloc_space *pa;
3570 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003571
3572 BUG_ON(atomic_read(&pa->pa_count));
3573 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003574 kmem_cache_free(ext4_pspace_cachep, pa);
3575}
3576
3577/*
3578 * drops a reference to preallocated space descriptor
3579 * if this was the last reference and the space is consumed
3580 */
3581static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3582 struct super_block *sb, struct ext4_prealloc_space *pa)
3583{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003584 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003585 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003586
Alex Tomasc9de5602008-01-29 00:19:52 -05003587 /* in this short window concurrent discard can set pa_deleted */
3588 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003589 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3590 spin_unlock(&pa->pa_lock);
3591 return;
3592 }
3593
Alex Tomasc9de5602008-01-29 00:19:52 -05003594 if (pa->pa_deleted == 1) {
3595 spin_unlock(&pa->pa_lock);
3596 return;
3597 }
3598
3599 pa->pa_deleted = 1;
3600 spin_unlock(&pa->pa_lock);
3601
Eric Sandeend33a1972009-03-16 23:25:40 -04003602 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003603 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003604 * If doing group-based preallocation, pa_pstart may be in the
3605 * next group when pa is used up
3606 */
3607 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003608 grp_blk--;
3609
Lukas Czernerbd862982013-04-03 23:32:34 -04003610 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003611
3612 /*
3613 * possible race:
3614 *
3615 * P1 (buddy init) P2 (regular allocation)
3616 * find block B in PA
3617 * copy on-disk bitmap to buddy
3618 * mark B in on-disk bitmap
3619 * drop PA from group
3620 * mark all PAs in buddy
3621 *
3622 * thus, P1 initializes buddy with B available. to prevent this
3623 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3624 * against that pair
3625 */
3626 ext4_lock_group(sb, grp);
3627 list_del(&pa->pa_group_list);
3628 ext4_unlock_group(sb, grp);
3629
3630 spin_lock(pa->pa_obj_lock);
3631 list_del_rcu(&pa->pa_inode_list);
3632 spin_unlock(pa->pa_obj_lock);
3633
3634 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3635}
3636
3637/*
3638 * creates new preallocated space for given inode
3639 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003640static noinline_for_stack int
3641ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003642{
3643 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003644 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003645 struct ext4_prealloc_space *pa;
3646 struct ext4_group_info *grp;
3647 struct ext4_inode_info *ei;
3648
3649 /* preallocate only when found space is larger then requested */
3650 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3651 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3652 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3653
3654 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3655 if (pa == NULL)
3656 return -ENOMEM;
3657
3658 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3659 int winl;
3660 int wins;
3661 int win;
3662 int offs;
3663
3664 /* we can't allocate as much as normalizer wants.
3665 * so, found space must get proper lstart
3666 * to cover original request */
3667 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3668 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3669
3670 /* we're limited by original request in that
3671 * logical block must be covered any way
3672 * winl is window we can move our chunk within */
3673 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3674
3675 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003676 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003677
3678 /* the smallest one defines real window */
3679 win = min(winl, wins);
3680
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003681 offs = ac->ac_o_ex.fe_logical %
3682 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003683 if (offs && offs < win)
3684 win = offs;
3685
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003686 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003687 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003688 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3689 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3690 }
3691
3692 /* preallocation can change ac_b_ex, thus we store actually
3693 * allocated blocks for history */
3694 ac->ac_f_ex = ac->ac_b_ex;
3695
3696 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3697 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3698 pa->pa_len = ac->ac_b_ex.fe_len;
3699 pa->pa_free = pa->pa_len;
3700 atomic_set(&pa->pa_count, 1);
3701 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003702 INIT_LIST_HEAD(&pa->pa_inode_list);
3703 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003704 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003705 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003706
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003707 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003708 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003709 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003710
3711 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003712 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003713
3714 ei = EXT4_I(ac->ac_inode);
3715 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3716
3717 pa->pa_obj_lock = &ei->i_prealloc_lock;
3718 pa->pa_inode = ac->ac_inode;
3719
3720 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3721 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3722 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3723
3724 spin_lock(pa->pa_obj_lock);
3725 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3726 spin_unlock(pa->pa_obj_lock);
3727
3728 return 0;
3729}
3730
3731/*
3732 * creates new preallocated space for locality group inodes belongs to
3733 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003734static noinline_for_stack int
3735ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003736{
3737 struct super_block *sb = ac->ac_sb;
3738 struct ext4_locality_group *lg;
3739 struct ext4_prealloc_space *pa;
3740 struct ext4_group_info *grp;
3741
3742 /* preallocate only when found space is larger then requested */
3743 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3744 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3745 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3746
3747 BUG_ON(ext4_pspace_cachep == NULL);
3748 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3749 if (pa == NULL)
3750 return -ENOMEM;
3751
3752 /* preallocation can change ac_b_ex, thus we store actually
3753 * allocated blocks for history */
3754 ac->ac_f_ex = ac->ac_b_ex;
3755
3756 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3757 pa->pa_lstart = pa->pa_pstart;
3758 pa->pa_len = ac->ac_b_ex.fe_len;
3759 pa->pa_free = pa->pa_len;
3760 atomic_set(&pa->pa_count, 1);
3761 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003762 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003763 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003764 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003765 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003766
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003767 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003768 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3769 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003770
3771 ext4_mb_use_group_pa(ac, pa);
3772 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3773
3774 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3775 lg = ac->ac_lg;
3776 BUG_ON(lg == NULL);
3777
3778 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3779 pa->pa_inode = NULL;
3780
3781 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3782 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3783 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3784
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003785 /*
3786 * We will later add the new pa to the right bucket
3787 * after updating the pa_free in ext4_mb_release_context
3788 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003789 return 0;
3790}
3791
3792static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3793{
3794 int err;
3795
3796 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3797 err = ext4_mb_new_group_pa(ac);
3798 else
3799 err = ext4_mb_new_inode_pa(ac);
3800 return err;
3801}
3802
3803/*
3804 * finds all unused blocks in on-disk bitmap, frees them in
3805 * in-core bitmap and buddy.
3806 * @pa must be unlinked from inode and group lists, so that
3807 * nobody else can find/use it.
3808 * the caller MUST hold group/inode locks.
3809 * TODO: optimize the case when there are no in-core structures yet
3810 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003811static noinline_for_stack int
3812ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003813 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003814{
Alex Tomasc9de5602008-01-29 00:19:52 -05003815 struct super_block *sb = e4b->bd_sb;
3816 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003817 unsigned int end;
3818 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003819 ext4_group_t group;
3820 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003821 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003822 int err = 0;
3823 int free = 0;
3824
3825 BUG_ON(pa->pa_deleted == 0);
3826 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003827 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003828 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3829 end = bit + pa->pa_len;
3830
Alex Tomasc9de5602008-01-29 00:19:52 -05003831 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003832 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003833 if (bit >= end)
3834 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003835 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003836 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003837 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3838 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003839 free += next - bit;
3840
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003841 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003842 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3843 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003844 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003845 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3846 bit = next + 1;
3847 }
3848 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003849 ext4_msg(e4b->bd_sb, KERN_CRIT,
3850 "pa %p: logic %lu, phys. %lu, len %lu",
3851 pa, (unsigned long) pa->pa_lstart,
3852 (unsigned long) pa->pa_pstart,
3853 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003854 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003855 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003856 /*
3857 * pa is already deleted so we use the value obtained
3858 * from the bitmap and continue.
3859 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003860 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003861 atomic_add(free, &sbi->s_mb_discarded);
3862
3863 return err;
3864}
3865
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003866static noinline_for_stack int
3867ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003868 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003869{
Alex Tomasc9de5602008-01-29 00:19:52 -05003870 struct super_block *sb = e4b->bd_sb;
3871 ext4_group_t group;
3872 ext4_grpblk_t bit;
3873
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003874 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003875 BUG_ON(pa->pa_deleted == 0);
3876 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3877 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3878 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3879 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003880 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003881
3882 return 0;
3883}
3884
3885/*
3886 * releases all preallocations in given group
3887 *
3888 * first, we need to decide discard policy:
3889 * - when do we discard
3890 * 1) ENOSPC
3891 * - how many do we discard
3892 * 1) how many requested
3893 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003894static noinline_for_stack int
3895ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003896 ext4_group_t group, int needed)
3897{
3898 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3899 struct buffer_head *bitmap_bh = NULL;
3900 struct ext4_prealloc_space *pa, *tmp;
3901 struct list_head list;
3902 struct ext4_buddy e4b;
3903 int err;
3904 int busy = 0;
3905 int free = 0;
3906
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003907 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003908
3909 if (list_empty(&grp->bb_prealloc_list))
3910 return 0;
3911
Theodore Ts'o574ca172008-07-11 19:27:31 -04003912 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04003913 if (IS_ERR(bitmap_bh)) {
3914 err = PTR_ERR(bitmap_bh);
3915 ext4_error(sb, "Error %d reading block bitmap for %u",
3916 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003917 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003918 }
3919
3920 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003921 if (err) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04003922 ext4_warning(sb, "Error %d loading buddy information for %u",
3923 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003924 put_bh(bitmap_bh);
3925 return 0;
3926 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003927
3928 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003929 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003930
Alex Tomasc9de5602008-01-29 00:19:52 -05003931 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003932repeat:
3933 ext4_lock_group(sb, group);
3934 list_for_each_entry_safe(pa, tmp,
3935 &grp->bb_prealloc_list, pa_group_list) {
3936 spin_lock(&pa->pa_lock);
3937 if (atomic_read(&pa->pa_count)) {
3938 spin_unlock(&pa->pa_lock);
3939 busy = 1;
3940 continue;
3941 }
3942 if (pa->pa_deleted) {
3943 spin_unlock(&pa->pa_lock);
3944 continue;
3945 }
3946
3947 /* seems this one can be freed ... */
3948 pa->pa_deleted = 1;
3949
3950 /* we can trust pa_free ... */
3951 free += pa->pa_free;
3952
3953 spin_unlock(&pa->pa_lock);
3954
3955 list_del(&pa->pa_group_list);
3956 list_add(&pa->u.pa_tmp_list, &list);
3957 }
3958
3959 /* if we still need more blocks and some PAs were used, try again */
3960 if (free < needed && busy) {
3961 busy = 0;
3962 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003963 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003964 goto repeat;
3965 }
3966
3967 /* found anything to free? */
3968 if (list_empty(&list)) {
3969 BUG_ON(free != 0);
3970 goto out;
3971 }
3972
3973 /* now free all selected PAs */
3974 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3975
3976 /* remove from object (inode or locality group) */
3977 spin_lock(pa->pa_obj_lock);
3978 list_del_rcu(&pa->pa_inode_list);
3979 spin_unlock(pa->pa_obj_lock);
3980
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003981 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003982 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003983 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003984 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003985
3986 list_del(&pa->u.pa_tmp_list);
3987 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3988 }
3989
3990out:
3991 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003992 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003993 put_bh(bitmap_bh);
3994 return free;
3995}
3996
3997/*
3998 * releases all non-used preallocated blocks for given inode
3999 *
4000 * It's important to discard preallocations under i_data_sem
4001 * We don't want another block to be served from the prealloc
4002 * space when we are discarding the inode prealloc space.
4003 *
4004 * FIXME!! Make sure it is valid at all the call sites
4005 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004006void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05004007{
4008 struct ext4_inode_info *ei = EXT4_I(inode);
4009 struct super_block *sb = inode->i_sb;
4010 struct buffer_head *bitmap_bh = NULL;
4011 struct ext4_prealloc_space *pa, *tmp;
4012 ext4_group_t group = 0;
4013 struct list_head list;
4014 struct ext4_buddy e4b;
4015 int err;
4016
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004017 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004018 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4019 return;
4020 }
4021
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004022 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004023 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004024
4025 INIT_LIST_HEAD(&list);
4026
4027repeat:
4028 /* first, collect all pa's in the inode */
4029 spin_lock(&ei->i_prealloc_lock);
4030 while (!list_empty(&ei->i_prealloc_list)) {
4031 pa = list_entry(ei->i_prealloc_list.next,
4032 struct ext4_prealloc_space, pa_inode_list);
4033 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4034 spin_lock(&pa->pa_lock);
4035 if (atomic_read(&pa->pa_count)) {
4036 /* this shouldn't happen often - nobody should
4037 * use preallocation while we're discarding it */
4038 spin_unlock(&pa->pa_lock);
4039 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004040 ext4_msg(sb, KERN_ERR,
4041 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05004042 WARN_ON(1);
4043 schedule_timeout_uninterruptible(HZ);
4044 goto repeat;
4045
4046 }
4047 if (pa->pa_deleted == 0) {
4048 pa->pa_deleted = 1;
4049 spin_unlock(&pa->pa_lock);
4050 list_del_rcu(&pa->pa_inode_list);
4051 list_add(&pa->u.pa_tmp_list, &list);
4052 continue;
4053 }
4054
4055 /* someone is deleting pa right now */
4056 spin_unlock(&pa->pa_lock);
4057 spin_unlock(&ei->i_prealloc_lock);
4058
4059 /* we have to wait here because pa_deleted
4060 * doesn't mean pa is already unlinked from
4061 * the list. as we might be called from
4062 * ->clear_inode() the inode will get freed
4063 * and concurrent thread which is unlinking
4064 * pa from inode's list may access already
4065 * freed memory, bad-bad-bad */
4066
4067 /* XXX: if this happens too often, we can
4068 * add a flag to force wait only in case
4069 * of ->clear_inode(), but not in case of
4070 * regular truncate */
4071 schedule_timeout_uninterruptible(HZ);
4072 goto repeat;
4073 }
4074 spin_unlock(&ei->i_prealloc_lock);
4075
4076 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004077 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04004078 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05004079
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004080 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4081 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004082 if (err) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004083 ext4_error(sb, "Error %d loading buddy information for %u",
4084 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004085 continue;
4086 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004087
Theodore Ts'o574ca172008-07-11 19:27:31 -04004088 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004089 if (IS_ERR(bitmap_bh)) {
4090 err = PTR_ERR(bitmap_bh);
4091 ext4_error(sb, "Error %d reading block bitmap for %u",
4092 err, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004093 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004094 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004095 }
4096
4097 ext4_lock_group(sb, group);
4098 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004099 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004100 ext4_unlock_group(sb, group);
4101
Jing Zhange39e07f2010-05-14 00:00:00 -04004102 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004103 put_bh(bitmap_bh);
4104
4105 list_del(&pa->u.pa_tmp_list);
4106 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4107 }
4108}
4109
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004110#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004111static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4112{
4113 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004114 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004115
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05004116 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05004117 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004118 return;
4119
Joe Perches7f6a11e2012-03-19 23:09:43 -04004120 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004121 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04004122 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004123 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004124 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004125 "goal %lu/%lu/%lu@%lu, "
4126 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004127 (unsigned long)ac->ac_o_ex.fe_group,
4128 (unsigned long)ac->ac_o_ex.fe_start,
4129 (unsigned long)ac->ac_o_ex.fe_len,
4130 (unsigned long)ac->ac_o_ex.fe_logical,
4131 (unsigned long)ac->ac_g_ex.fe_group,
4132 (unsigned long)ac->ac_g_ex.fe_start,
4133 (unsigned long)ac->ac_g_ex.fe_len,
4134 (unsigned long)ac->ac_g_ex.fe_logical,
4135 (unsigned long)ac->ac_b_ex.fe_group,
4136 (unsigned long)ac->ac_b_ex.fe_start,
4137 (unsigned long)ac->ac_b_ex.fe_len,
4138 (unsigned long)ac->ac_b_ex.fe_logical,
4139 (int)ac->ac_criteria);
Eric Sandeendc9ddd92014-02-20 13:32:10 -05004140 ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004141 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004142 ngroups = ext4_get_groups_count(sb);
4143 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004144 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4145 struct ext4_prealloc_space *pa;
4146 ext4_grpblk_t start;
4147 struct list_head *cur;
4148 ext4_lock_group(sb, i);
4149 list_for_each(cur, &grp->bb_prealloc_list) {
4150 pa = list_entry(cur, struct ext4_prealloc_space,
4151 pa_group_list);
4152 spin_lock(&pa->pa_lock);
4153 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4154 NULL, &start);
4155 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004156 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4157 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004158 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004159 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004160
4161 if (grp->bb_free == 0)
4162 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004163 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004164 i, grp->bb_free, grp->bb_fragments);
4165 }
4166 printk(KERN_ERR "\n");
4167}
4168#else
4169static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4170{
4171 return;
4172}
4173#endif
4174
4175/*
4176 * We use locality group preallocation for small size file. The size of the
4177 * file is determined by the current size or the resulting size after
4178 * allocation which ever is larger
4179 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004180 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004181 */
4182static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4183{
4184 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4185 int bsbits = ac->ac_sb->s_blocksize_bits;
4186 loff_t size, isize;
4187
4188 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4189 return;
4190
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004191 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4192 return;
4193
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004194 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004195 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4196 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004197
Theodore Ts'o50797482009-09-18 13:34:02 -04004198 if ((size == isize) &&
4199 !ext4_fs_is_busy(sbi) &&
4200 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4201 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4202 return;
4203 }
4204
Robin Dongebbe0272011-10-26 05:14:27 -04004205 if (sbi->s_mb_group_prealloc <= 0) {
4206 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4207 return;
4208 }
4209
Alex Tomasc9de5602008-01-29 00:19:52 -05004210 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004211 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004212 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004213 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004214 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004215 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004216
4217 BUG_ON(ac->ac_lg != NULL);
4218 /*
4219 * locality group prealloc space are per cpu. The reason for having
4220 * per cpu locality group is to reduce the contention between block
4221 * request from multiple CPUs.
4222 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004223 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004224
4225 /* we're going to use group allocation */
4226 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4227
4228 /* serialize all allocations in the group */
4229 mutex_lock(&ac->ac_lg->lg_mutex);
4230}
4231
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004232static noinline_for_stack int
4233ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004234 struct ext4_allocation_request *ar)
4235{
4236 struct super_block *sb = ar->inode->i_sb;
4237 struct ext4_sb_info *sbi = EXT4_SB(sb);
4238 struct ext4_super_block *es = sbi->s_es;
4239 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004240 unsigned int len;
4241 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004242 ext4_grpblk_t block;
4243
4244 /* we can't allocate > group size */
4245 len = ar->len;
4246
4247 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004248 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4249 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004250
4251 /* start searching from the goal */
4252 goal = ar->goal;
4253 if (goal < le32_to_cpu(es->s_first_data_block) ||
4254 goal >= ext4_blocks_count(es))
4255 goal = le32_to_cpu(es->s_first_data_block);
4256 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4257
4258 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004259 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004260 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004261 ac->ac_sb = sb;
4262 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004263 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004264 ac->ac_o_ex.fe_group = group;
4265 ac->ac_o_ex.fe_start = block;
4266 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004267 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004268 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004269
4270 /* we have to define context: we'll we work with a file or
4271 * locality group. this is a policy, actually */
4272 ext4_mb_group_or_file(ac);
4273
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004274 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004275 "left: %u/%u, right %u/%u to %swritable\n",
4276 (unsigned) ar->len, (unsigned) ar->logical,
4277 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4278 (unsigned) ar->lleft, (unsigned) ar->pleft,
4279 (unsigned) ar->lright, (unsigned) ar->pright,
4280 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4281 return 0;
4282
4283}
4284
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004285static noinline_for_stack void
4286ext4_mb_discard_lg_preallocations(struct super_block *sb,
4287 struct ext4_locality_group *lg,
4288 int order, int total_entries)
4289{
4290 ext4_group_t group = 0;
4291 struct ext4_buddy e4b;
4292 struct list_head discard_list;
4293 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004294
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004295 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004296
4297 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004298
4299 spin_lock(&lg->lg_prealloc_lock);
4300 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4301 pa_inode_list) {
4302 spin_lock(&pa->pa_lock);
4303 if (atomic_read(&pa->pa_count)) {
4304 /*
4305 * This is the pa that we just used
4306 * for block allocation. So don't
4307 * free that
4308 */
4309 spin_unlock(&pa->pa_lock);
4310 continue;
4311 }
4312 if (pa->pa_deleted) {
4313 spin_unlock(&pa->pa_lock);
4314 continue;
4315 }
4316 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004317 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004318
4319 /* seems this one can be freed ... */
4320 pa->pa_deleted = 1;
4321 spin_unlock(&pa->pa_lock);
4322
4323 list_del_rcu(&pa->pa_inode_list);
4324 list_add(&pa->u.pa_tmp_list, &discard_list);
4325
4326 total_entries--;
4327 if (total_entries <= 5) {
4328 /*
4329 * we want to keep only 5 entries
4330 * allowing it to grow to 8. This
4331 * mak sure we don't call discard
4332 * soon for this list.
4333 */
4334 break;
4335 }
4336 }
4337 spin_unlock(&lg->lg_prealloc_lock);
4338
4339 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004340 int err;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004341
Lukas Czernerbd862982013-04-03 23:32:34 -04004342 group = ext4_get_group_number(sb, pa->pa_pstart);
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04004343 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4344 GFP_NOFS|__GFP_NOFAIL);
4345 if (err) {
4346 ext4_error(sb, "Error %d loading buddy information for %u",
4347 err, group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004348 continue;
4349 }
4350 ext4_lock_group(sb, group);
4351 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004352 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004353 ext4_unlock_group(sb, group);
4354
Jing Zhange39e07f2010-05-14 00:00:00 -04004355 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004356 list_del(&pa->u.pa_tmp_list);
4357 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4358 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004359}
4360
4361/*
4362 * We have incremented pa_count. So it cannot be freed at this
4363 * point. Also we hold lg_mutex. So no parallel allocation is
4364 * possible from this lg. That means pa_free cannot be updated.
4365 *
4366 * A parallel ext4_mb_discard_group_preallocations is possible.
4367 * which can cause the lg_prealloc_list to be updated.
4368 */
4369
4370static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4371{
4372 int order, added = 0, lg_prealloc_count = 1;
4373 struct super_block *sb = ac->ac_sb;
4374 struct ext4_locality_group *lg = ac->ac_lg;
4375 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4376
4377 order = fls(pa->pa_free) - 1;
4378 if (order > PREALLOC_TB_SIZE - 1)
4379 /* The max size of hash table is PREALLOC_TB_SIZE */
4380 order = PREALLOC_TB_SIZE - 1;
4381 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004382 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004383 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4384 pa_inode_list) {
4385 spin_lock(&tmp_pa->pa_lock);
4386 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004387 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004388 continue;
4389 }
4390 if (!added && pa->pa_free < tmp_pa->pa_free) {
4391 /* Add to the tail of the previous entry */
4392 list_add_tail_rcu(&pa->pa_inode_list,
4393 &tmp_pa->pa_inode_list);
4394 added = 1;
4395 /*
4396 * we want to count the total
4397 * number of entries in the list
4398 */
4399 }
4400 spin_unlock(&tmp_pa->pa_lock);
4401 lg_prealloc_count++;
4402 }
4403 if (!added)
4404 list_add_tail_rcu(&pa->pa_inode_list,
4405 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004406 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004407
4408 /* Now trim the list to be not more than 8 elements */
4409 if (lg_prealloc_count > 8) {
4410 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004411 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004412 return;
4413 }
4414 return ;
4415}
4416
Alex Tomasc9de5602008-01-29 00:19:52 -05004417/*
4418 * release all resource we used in allocation
4419 */
4420static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4421{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004422 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004423 struct ext4_prealloc_space *pa = ac->ac_pa;
4424 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004425 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004426 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004427 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004428 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4429 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004430 pa->pa_free -= ac->ac_b_ex.fe_len;
4431 pa->pa_len -= ac->ac_b_ex.fe_len;
4432 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004433 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004434 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004435 if (pa) {
4436 /*
4437 * We want to add the pa to the right bucket.
4438 * Remove it from the list and while adding
4439 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004440 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004441 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004442 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004443 spin_lock(pa->pa_obj_lock);
4444 list_del_rcu(&pa->pa_inode_list);
4445 spin_unlock(pa->pa_obj_lock);
4446 ext4_mb_add_n_trim(ac);
4447 }
4448 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4449 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004450 if (ac->ac_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004451 put_page(ac->ac_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004452 if (ac->ac_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004453 put_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004454 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4455 mutex_unlock(&ac->ac_lg->lg_mutex);
4456 ext4_mb_collect_stats(ac);
4457 return 0;
4458}
4459
4460static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4461{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004462 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004463 int ret;
4464 int freed = 0;
4465
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004466 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004467 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004468 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4469 freed += ret;
4470 needed -= ret;
4471 }
4472
4473 return freed;
4474}
4475
4476/*
4477 * Main entry point into mballoc to allocate blocks
4478 * it tries to use preallocation first, then falls back
4479 * to usual allocation
4480 */
4481ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004482 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004483{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004484 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004485 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004486 struct ext4_sb_info *sbi;
4487 struct super_block *sb;
4488 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004489 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004490 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004491
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004492 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004493 sb = ar->inode->i_sb;
4494 sbi = EXT4_SB(sb);
4495
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004496 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004497
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004498 /* Allow to use superuser reservation for quota file */
Tahsin Erdogan02749a42017-06-22 11:31:25 -04004499 if (ext4_is_quota_file(ar->inode))
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004500 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4501
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004502 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004503 /* Without delayed allocation we need to verify
4504 * there is enough free blocks to do block allocation
4505 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004506 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004507 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004508 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004509
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004510 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004511 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004512 ar->len = ar->len >> 1;
4513 }
4514 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004515 *errp = -ENOSPC;
4516 return 0;
4517 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004518 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004519 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004520 dquot_alloc_block_nofail(ar->inode,
4521 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004522 } else {
4523 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004524 dquot_alloc_block(ar->inode,
4525 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004526
4527 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4528 ar->len--;
4529 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004530 }
4531 inquota = ar->len;
4532 if (ar->len == 0) {
4533 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004534 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004535 }
Mingming Caod2a17632008-07-14 17:52:37 -04004536 }
Mingming Caod2a17632008-07-14 17:52:37 -04004537
Wei Yongjun85556c92012-09-26 20:43:37 -04004538 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004539 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004540 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004541 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004542 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004543 }
4544
Eric Sandeen256bdb42008-02-10 01:13:33 -05004545 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004546 if (*errp) {
4547 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004548 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004549 }
4550
Eric Sandeen256bdb42008-02-10 01:13:33 -05004551 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4552 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004553 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4554 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004555repeat:
4556 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004557 *errp = ext4_mb_regular_allocator(ac);
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004558 if (*errp)
4559 goto discard_and_exit;
4560
4561 /* as we've just preallocated more space than
4562 * user requested originally, we store allocated
4563 * space in a special descriptor */
4564 if (ac->ac_status == AC_STATUS_FOUND &&
4565 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4566 *errp = ext4_mb_new_preallocation(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004567 if (*errp) {
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004568 discard_and_exit:
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004569 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004570 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004571 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004572 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004573 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004574 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Vegard Nossum554a5cc2016-07-14 23:02:47 -04004575 if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004576 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004577 goto errout;
4578 } else {
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004579 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4580 ar->len = ac->ac_b_ex.fe_len;
4581 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004582 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004583 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004584 if (freed)
4585 goto repeat;
4586 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004587 }
4588
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004589errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004590 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004591 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004592 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004593 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004594 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004595 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004596out:
4597 if (ac)
4598 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004599 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004600 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004601 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004602 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004603 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004604 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004605 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004606 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004607
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004608 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004609
Alex Tomasc9de5602008-01-29 00:19:52 -05004610 return block;
4611}
Alex Tomasc9de5602008-01-29 00:19:52 -05004612
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004613/*
4614 * We can merge two free data extents only if the physical blocks
4615 * are contiguous, AND the extents were freed by the same transaction,
4616 * AND the blocks are associated with the same group.
4617 */
Daeho Jeonga0154342017-06-22 23:54:33 -04004618static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi,
4619 struct ext4_free_data *entry,
4620 struct ext4_free_data *new_entry,
4621 struct rb_root *entry_rb_root)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004622{
Daeho Jeonga0154342017-06-22 23:54:33 -04004623 if ((entry->efd_tid != new_entry->efd_tid) ||
4624 (entry->efd_group != new_entry->efd_group))
4625 return;
4626 if (entry->efd_start_cluster + entry->efd_count ==
4627 new_entry->efd_start_cluster) {
4628 new_entry->efd_start_cluster = entry->efd_start_cluster;
4629 new_entry->efd_count += entry->efd_count;
4630 } else if (new_entry->efd_start_cluster + new_entry->efd_count ==
4631 entry->efd_start_cluster) {
4632 new_entry->efd_count += entry->efd_count;
4633 } else
4634 return;
4635 spin_lock(&sbi->s_md_lock);
4636 list_del(&entry->efd_list);
4637 spin_unlock(&sbi->s_md_lock);
4638 rb_erase(&entry->efd_node, entry_rb_root);
4639 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004640}
4641
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004642static noinline_for_stack int
4643ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004644 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004645{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004646 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004647 ext4_grpblk_t cluster;
Theodore Ts'od08854f2016-06-26 18:24:01 -04004648 ext4_grpblk_t clusters = new_entry->efd_count;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004649 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004650 struct ext4_group_info *db = e4b->bd_info;
4651 struct super_block *sb = e4b->bd_sb;
4652 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004653 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4654 struct rb_node *parent = NULL, *new_node;
4655
Frank Mayhar03901312009-01-07 00:06:22 -05004656 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004657 BUG_ON(e4b->bd_bitmap_page == NULL);
4658 BUG_ON(e4b->bd_buddy_page == NULL);
4659
Bobi Jam18aadd42012-02-20 17:53:02 -05004660 new_node = &new_entry->efd_node;
4661 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004662
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004663 if (!*n) {
4664 /* first free block exent. We need to
4665 protect buddy cache from being freed,
4666 * otherwise we'll refresh it from
4667 * on-disk bitmap and lose not-yet-available
4668 * blocks */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004669 get_page(e4b->bd_buddy_page);
4670 get_page(e4b->bd_bitmap_page);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004671 }
4672 while (*n) {
4673 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004674 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4675 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004676 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004677 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004678 n = &(*n)->rb_right;
4679 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004680 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004681 ext4_group_first_block_no(sb, group) +
4682 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004683 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004684 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004685 }
4686 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004687
4688 rb_link_node(new_node, parent, n);
4689 rb_insert_color(new_node, &db->bb_free_root);
4690
4691 /* Now try to see the extent can be merged to left and right */
4692 node = rb_prev(new_node);
4693 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004694 entry = rb_entry(node, struct ext4_free_data, efd_node);
Daeho Jeonga0154342017-06-22 23:54:33 -04004695 ext4_try_merge_freed_extent(sbi, entry, new_entry,
4696 &(db->bb_free_root));
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004697 }
4698
4699 node = rb_next(new_node);
4700 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004701 entry = rb_entry(node, struct ext4_free_data, efd_node);
Daeho Jeonga0154342017-06-22 23:54:33 -04004702 ext4_try_merge_freed_extent(sbi, entry, new_entry,
4703 &(db->bb_free_root));
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004704 }
Daeho Jeonga0154342017-06-22 23:54:33 -04004705
Theodore Ts'od08854f2016-06-26 18:24:01 -04004706 spin_lock(&sbi->s_md_lock);
Daeho Jeonga0154342017-06-22 23:54:33 -04004707 list_add_tail(&new_entry->efd_list, &sbi->s_freed_data_list);
Theodore Ts'od08854f2016-06-26 18:24:01 -04004708 sbi->s_mb_free_pending += clusters;
4709 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004710 return 0;
4711}
4712
Theodore Ts'o44338712009-11-22 07:44:56 -05004713/**
4714 * ext4_free_blocks() -- Free given blocks and update quota
4715 * @handle: handle for this transaction
4716 * @inode: inode
4717 * @block: start physical block to free
4718 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004719 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004720 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004721void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004722 struct buffer_head *bh, ext4_fsblk_t block,
4723 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004724{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004725 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004726 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004727 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004728 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004729 ext4_grpblk_t bit;
4730 struct buffer_head *gd_bh;
4731 ext4_group_t block_group;
4732 struct ext4_sb_info *sbi;
4733 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004734 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004735 int err = 0;
4736 int ret;
4737
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004738 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004739 if (bh) {
4740 if (block)
4741 BUG_ON(block != bh->b_blocknr);
4742 else
4743 block = bh->b_blocknr;
4744 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004745
Alex Tomasc9de5602008-01-29 00:19:52 -05004746 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004747 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4748 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004749 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004750 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004751 goto error_return;
4752 }
4753
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004754 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004755 trace_ext4_free_blocks(inode, block, count, flags);
4756
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004757 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4758 BUG_ON(count > 1);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004759
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004760 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4761 inode, bh, block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004762 }
4763
Theodore Ts'o60e66792010-05-17 07:00:00 -04004764 /*
Theodore Ts'o84130192011-09-09 18:50:51 -04004765 * If the extent to be freed does not begin on a cluster
4766 * boundary, we need to deal with partial clusters at the
4767 * beginning and end of the extent. Normally we will free
4768 * blocks at the beginning or the end unless we are explicitly
4769 * requested to avoid doing so.
4770 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004771 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004772 if (overflow) {
4773 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4774 overflow = sbi->s_cluster_ratio - overflow;
4775 block += overflow;
4776 if (count > overflow)
4777 count -= overflow;
4778 else
4779 return;
4780 } else {
4781 block -= overflow;
4782 count += overflow;
4783 }
4784 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004785 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004786 if (overflow) {
4787 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4788 if (count > overflow)
4789 count -= overflow;
4790 else
4791 return;
4792 } else
4793 count += sbi->s_cluster_ratio - overflow;
4794 }
4795
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004796 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4797 int i;
Daeho Jeongf96c4502016-02-21 18:31:41 -05004798 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004799
4800 for (i = 0; i < count; i++) {
4801 cond_resched();
Daeho Jeongf96c4502016-02-21 18:31:41 -05004802 if (is_metadata)
4803 bh = sb_find_get_block(inode->i_sb, block + i);
4804 ext4_forget(handle, is_metadata, inode, bh, block + i);
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004805 }
4806 }
4807
Alex Tomasc9de5602008-01-29 00:19:52 -05004808do_more:
4809 overflow = 0;
4810 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4811
Darrick J. Wong163a2032013-08-28 17:35:51 -04004812 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4813 ext4_get_group_info(sb, block_group))))
4814 return;
4815
Alex Tomasc9de5602008-01-29 00:19:52 -05004816 /*
4817 * Check to see if we are freeing blocks across a group
4818 * boundary.
4819 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004820 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4821 overflow = EXT4_C2B(sbi, bit) + count -
4822 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004823 count -= overflow;
4824 }
Lukas Czerner810da242013-03-02 17:18:58 -05004825 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004826 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004827 if (IS_ERR(bitmap_bh)) {
4828 err = PTR_ERR(bitmap_bh);
4829 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004830 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004831 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004832 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004833 if (!gdp) {
4834 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004835 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004836 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004837
4838 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4839 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4840 in_range(block, ext4_inode_table(sb, gdp),
Jun Piao49598e02018-01-11 13:17:49 -05004841 sbi->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004842 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Jun Piao49598e02018-01-11 13:17:49 -05004843 sbi->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004844
Eric Sandeen12062dd2010-02-15 14:19:27 -05004845 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004846 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004847 /* err = 0. ext4_std_error should be a no op */
4848 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004849 }
4850
4851 BUFFER_TRACE(bitmap_bh, "getting write access");
4852 err = ext4_journal_get_write_access(handle, bitmap_bh);
4853 if (err)
4854 goto error_return;
4855
4856 /*
4857 * We are about to modify some metadata. Call the journal APIs
4858 * to unshare ->b_data if a currently-committing transaction is
4859 * using it
4860 */
4861 BUFFER_TRACE(gd_bh, "get_write_access");
4862 err = ext4_journal_get_write_access(handle, gd_bh);
4863 if (err)
4864 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004865#ifdef AGGRESSIVE_CHECK
4866 {
4867 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004868 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004869 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4870 }
4871#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004872 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004873
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04004874 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
4875 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
4876 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004877 if (err)
4878 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004879
Daeho Jeongf96c4502016-02-21 18:31:41 -05004880 /*
4881 * We need to make sure we don't reuse the freed block until after the
4882 * transaction is committed. We make an exception if the inode is to be
4883 * written in writeback mode since writeback mode has weak data
4884 * consistency guarantees.
4885 */
4886 if (ext4_handle_valid(handle) &&
4887 ((flags & EXT4_FREE_BLOCKS_METADATA) ||
4888 !ext4_should_writeback_data(inode))) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004889 struct ext4_free_data *new_entry;
4890 /*
Michal Hocko7444a072015-07-05 12:33:44 -04004891 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4892 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004893 */
Michal Hocko7444a072015-07-05 12:33:44 -04004894 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
4895 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05004896 new_entry->efd_start_cluster = bit;
4897 new_entry->efd_group = block_group;
4898 new_entry->efd_count = count_clusters;
4899 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004900
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004901 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004902 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004903 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004904 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004905 /* need to update group_info->bb_free and bitmap
4906 * with group lock held. generate_buddy look at
4907 * them with group lock_held
4908 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004909 if (test_opt(sb, DISCARD)) {
Daeho Jeonga0154342017-06-22 23:54:33 -04004910 err = ext4_issue_discard(sb, block_group, bit, count,
4911 NULL);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004912 if (err && err != -EOPNOTSUPP)
4913 ext4_msg(sb, KERN_WARNING, "discard request in"
4914 " group:%d block:%d count:%lu failed"
4915 " with %d", block_group, bit, count,
4916 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04004917 } else
4918 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004919
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004920 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004921 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4922 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004923 }
4924
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004925 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4926 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004927 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004928 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004929 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004930
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004931 if (sbi->s_log_groups_per_flex) {
4932 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004933 atomic64_add(count_clusters,
4934 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004935 }
4936
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04004937 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004938 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
Jan Kara7d734532013-08-17 09:36:54 -04004939 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
4940
4941 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004942
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004943 /* We dirtied the bitmap block */
4944 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4945 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4946
Alex Tomasc9de5602008-01-29 00:19:52 -05004947 /* And the group descriptor block */
4948 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004949 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004950 if (!err)
4951 err = ret;
4952
4953 if (overflow && !err) {
4954 block += count;
4955 count = overflow;
4956 put_bh(bitmap_bh);
4957 goto do_more;
4958 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004959error_return:
4960 brelse(bitmap_bh);
4961 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004962 return;
4963}
Lukas Czerner7360d172010-10-27 21:30:12 -04004964
4965/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004966 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004967 * @handle: handle to this transaction
4968 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004969 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004970 * @count: number of blocks to free
4971 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004972 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004973 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004974int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004975 ext4_fsblk_t block, unsigned long count)
4976{
4977 struct buffer_head *bitmap_bh = NULL;
4978 struct buffer_head *gd_bh;
4979 ext4_group_t block_group;
4980 ext4_grpblk_t bit;
4981 unsigned int i;
4982 struct ext4_group_desc *desc;
4983 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004984 struct ext4_buddy e4b;
harshadsd77147f2017-10-29 09:38:46 -04004985 int err = 0, ret, free_clusters_count;
4986 ext4_grpblk_t clusters_freed;
4987 ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block);
4988 ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1);
4989 unsigned long cluster_count = last_cluster - first_cluster + 1;
Amir Goldstein2846e822011-05-09 10:46:41 -04004990
4991 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4992
Yongqiang Yang4740b832011-07-26 21:51:08 -04004993 if (count == 0)
4994 return 0;
4995
Amir Goldstein2846e822011-05-09 10:46:41 -04004996 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004997 /*
4998 * Check to see if we are freeing blocks across a group
4999 * boundary.
5000 */
harshadsd77147f2017-10-29 09:38:46 -04005001 if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) {
5002 ext4_warning(sb, "too many blocks added to group %u",
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005003 block_group);
5004 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005005 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005006 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005007
Amir Goldstein2846e822011-05-09 10:46:41 -04005008 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04005009 if (IS_ERR(bitmap_bh)) {
5010 err = PTR_ERR(bitmap_bh);
5011 bitmap_bh = NULL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005012 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005013 }
5014
Amir Goldstein2846e822011-05-09 10:46:41 -04005015 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005016 if (!desc) {
5017 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04005018 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005019 }
Amir Goldstein2846e822011-05-09 10:46:41 -04005020
5021 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
5022 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
5023 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
5024 in_range(block + count - 1, ext4_inode_table(sb, desc),
5025 sbi->s_itb_per_group)) {
5026 ext4_error(sb, "Adding blocks in system zones - "
5027 "Block = %llu, count = %lu",
5028 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005029 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005030 goto error_return;
5031 }
5032
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005033 BUFFER_TRACE(bitmap_bh, "getting write access");
5034 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04005035 if (err)
5036 goto error_return;
5037
5038 /*
5039 * We are about to modify some metadata. Call the journal APIs
5040 * to unshare ->b_data if a currently-committing transaction is
5041 * using it
5042 */
5043 BUFFER_TRACE(gd_bh, "get_write_access");
5044 err = ext4_journal_get_write_access(handle, gd_bh);
5045 if (err)
5046 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04005047
harshadsd77147f2017-10-29 09:38:46 -04005048 for (i = 0, clusters_freed = 0; i < cluster_count; i++) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005049 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04005050 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005051 ext4_error(sb, "bit already cleared for block %llu",
5052 (ext4_fsblk_t)(block + i));
5053 BUFFER_TRACE(bitmap_bh, "bit already cleared");
5054 } else {
harshadsd77147f2017-10-29 09:38:46 -04005055 clusters_freed++;
Amir Goldstein2846e822011-05-09 10:46:41 -04005056 }
5057 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005058
5059 err = ext4_mb_load_buddy(sb, block_group, &e4b);
5060 if (err)
5061 goto error_return;
5062
5063 /*
5064 * need to update group_info->bb_free and bitmap
5065 * with group lock held. generate_buddy look at
5066 * them with group lock_held
5067 */
Amir Goldstein2846e822011-05-09 10:46:41 -04005068 ext4_lock_group(sb, block_group);
harshadsd77147f2017-10-29 09:38:46 -04005069 mb_clear_bits(bitmap_bh->b_data, bit, cluster_count);
5070 mb_free_blocks(NULL, &e4b, bit, cluster_count);
5071 free_clusters_count = clusters_freed +
5072 ext4_free_group_clusters(sb, desc);
5073 ext4_free_group_clusters_set(sb, desc, free_clusters_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04005074 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005075 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04005076 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04005077 percpu_counter_add(&sbi->s_freeclusters_counter,
harshadsd77147f2017-10-29 09:38:46 -04005078 clusters_freed);
Amir Goldstein2846e822011-05-09 10:46:41 -04005079
5080 if (sbi->s_log_groups_per_flex) {
5081 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
harshadsd77147f2017-10-29 09:38:46 -04005082 atomic64_add(clusters_freed,
Theodore Ts'o90ba9832013-03-11 23:39:59 -04005083 &sbi->s_flex_groups[flex_group].free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04005084 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005085
5086 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005087
5088 /* We dirtied the bitmap block */
5089 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5090 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5091
5092 /* And the group descriptor block */
5093 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5094 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5095 if (!err)
5096 err = ret;
5097
5098error_return:
5099 brelse(bitmap_bh);
5100 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005101 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005102}
5103
5104/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005105 * ext4_trim_extent -- function to TRIM one single free extent in the group
5106 * @sb: super block for the file system
5107 * @start: starting block of the free extent in the alloc. group
5108 * @count: number of blocks to TRIM
5109 * @group: alloc. group we are working with
5110 * @e4b: ext4 buddy for the group
5111 *
5112 * Trim "count" blocks starting at "start" in the "group". To assure that no
5113 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5114 * be called with under the group lock.
5115 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005116static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04005117 ext4_group_t group, struct ext4_buddy *e4b)
jon ernste2cbd582014-04-12 23:01:28 -04005118__releases(bitlock)
5119__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005120{
5121 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005122 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005123
Tao Mab3d4c2b2011-07-11 00:01:52 -04005124 trace_ext4_trim_extent(sb, group, start, count);
5125
Lukas Czerner7360d172010-10-27 21:30:12 -04005126 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5127
5128 ex.fe_start = start;
5129 ex.fe_group = group;
5130 ex.fe_len = count;
5131
5132 /*
5133 * Mark blocks used, so no one can reuse them while
5134 * being trimmed.
5135 */
5136 mb_mark_used(e4b, &ex);
5137 ext4_unlock_group(sb, group);
Daeho Jeonga0154342017-06-22 23:54:33 -04005138 ret = ext4_issue_discard(sb, group, start, count, NULL);
Lukas Czerner7360d172010-10-27 21:30:12 -04005139 ext4_lock_group(sb, group);
5140 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005141 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005142}
5143
5144/**
5145 * ext4_trim_all_free -- function to trim all free space in alloc. group
5146 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005147 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005148 * @start: first group block to examine
5149 * @max: last group block to examine
5150 * @minblocks: minimum extent block count
5151 *
5152 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5153 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5154 * the extent.
5155 *
5156 *
5157 * ext4_trim_all_free walks through group's block bitmap searching for free
5158 * extents. When the free extent is found, mark it as used in group buddy
5159 * bitmap. Then issue a TRIM command on this extent and free the extent in
5160 * the group buddy bitmap. This is done until whole group is scanned.
5161 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005162static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005163ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5164 ext4_grpblk_t start, ext4_grpblk_t max,
5165 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005166{
5167 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005168 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005169 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005170 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005171
Tao Mab3d4c2b2011-07-11 00:01:52 -04005172 trace_ext4_trim_all_free(sb, group, start, max);
5173
Lukas Czerner78944082011-05-24 18:16:27 -04005174 ret = ext4_mb_load_buddy(sb, group, &e4b);
5175 if (ret) {
Konstantin Khlebnikov9651e6b2017-05-21 22:35:23 -04005176 ext4_warning(sb, "Error %d loading buddy information for %u",
5177 ret, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005178 return ret;
5179 }
Lukas Czerner78944082011-05-24 18:16:27 -04005180 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005181
5182 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005183 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5184 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5185 goto out;
5186
Lukas Czerner78944082011-05-24 18:16:27 -04005187 start = (e4b.bd_info->bb_first_free > start) ?
5188 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005189
Lukas Czerner913eed832012-03-21 21:22:22 -04005190 while (start <= max) {
5191 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5192 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005193 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005194 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005195
5196 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005197 ret = ext4_trim_extent(sb, start,
5198 next - start, group, &e4b);
5199 if (ret && ret != -EOPNOTSUPP)
5200 break;
5201 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005202 count += next - start;
5203 }
Tao Ma169ddc32011-07-11 00:00:07 -04005204 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005205 start = next + 1;
5206
5207 if (fatal_signal_pending(current)) {
5208 count = -ERESTARTSYS;
5209 break;
5210 }
5211
5212 if (need_resched()) {
5213 ext4_unlock_group(sb, group);
5214 cond_resched();
5215 ext4_lock_group(sb, group);
5216 }
5217
Tao Ma169ddc32011-07-11 00:00:07 -04005218 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005219 break;
5220 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005221
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005222 if (!ret) {
5223 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005224 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005225 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005226out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005227 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005228 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005229
5230 ext4_debug("trimmed %d blocks in the group %d\n",
5231 count, group);
5232
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005233 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005234}
5235
5236/**
5237 * ext4_trim_fs() -- trim ioctl handle function
5238 * @sb: superblock for filesystem
5239 * @range: fstrim_range structure
5240 *
5241 * start: First Byte to trim
5242 * len: number of Bytes to trim from start
5243 * minlen: minimum extent length in Bytes
5244 * ext4_trim_fs goes through all allocation groups containing Bytes from
5245 * start to start+len. For each such a group ext4_trim_all_free function
5246 * is invoked to trim all free space.
5247 */
5248int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
5249{
Lukas Czerner78944082011-05-24 18:16:27 -04005250 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005251 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005252 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005253 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005254 ext4_fsblk_t first_data_blk =
5255 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005256 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005257 int ret = 0;
5258
5259 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005260 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005261 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5262 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005263
Lukas Czerner5de35e82012-10-22 18:01:19 -04005264 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5265 start >= max_blks ||
5266 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005267 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005268 if (end >= max_blks)
5269 end = max_blks - 1;
5270 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005271 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005272 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005273 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005274
Lukas Czerner913eed832012-03-21 21:22:22 -04005275 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005276 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005277 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005278 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005279 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005280
Lukas Czerner913eed832012-03-21 21:22:22 -04005281 /* end now represents the last cluster to discard in this group */
5282 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005283
5284 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005285 grp = ext4_get_group_info(sb, group);
5286 /* We only do this if the grp has never been initialized */
5287 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005288 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Lukas Czerner78944082011-05-24 18:16:27 -04005289 if (ret)
5290 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005291 }
5292
Tao Ma0ba08512011-03-23 15:48:11 -04005293 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005294 * For all the groups except the last one, last cluster will
5295 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5296 * change it for the last group, note that last_cluster is
5297 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005298 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005299 if (group == last_group)
5300 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005301
Lukas Czerner78944082011-05-24 18:16:27 -04005302 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005303 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005304 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005305 if (cnt < 0) {
5306 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005307 break;
5308 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005309 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005310 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005311
5312 /*
5313 * For every group except the first one, we are sure
5314 * that the first cluster to discard will be cluster #0.
5315 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005316 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005317 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005318
Tao Ma3d56b8d2011-07-11 00:03:38 -04005319 if (!ret)
5320 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5321
Tao Ma22f10452011-07-10 23:52:37 -04005322out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005323 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005324 return ret;
5325}
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -04005326
5327/* Iterate all the free extents in the group. */
5328int
5329ext4_mballoc_query_range(
5330 struct super_block *sb,
5331 ext4_group_t group,
5332 ext4_grpblk_t start,
5333 ext4_grpblk_t end,
5334 ext4_mballoc_query_range_fn formatter,
5335 void *priv)
5336{
5337 void *bitmap;
5338 ext4_grpblk_t next;
5339 struct ext4_buddy e4b;
5340 int error;
5341
5342 error = ext4_mb_load_buddy(sb, group, &e4b);
5343 if (error)
5344 return error;
5345 bitmap = e4b.bd_bitmap;
5346
5347 ext4_lock_group(sb, group);
5348
5349 start = (e4b.bd_info->bb_first_free > start) ?
5350 e4b.bd_info->bb_first_free : start;
5351 if (end >= EXT4_CLUSTERS_PER_GROUP(sb))
5352 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
5353
5354 while (start <= end) {
5355 start = mb_find_next_zero_bit(bitmap, end + 1, start);
5356 if (start > end)
5357 break;
5358 next = mb_find_next_bit(bitmap, end + 1, start);
5359
5360 ext4_unlock_group(sb, group);
5361 error = formatter(sb, group, start, next - start, priv);
5362 if (error)
5363 goto out_unload;
5364 ext4_lock_group(sb, group);
5365
5366 start = next + 1;
5367 }
5368
5369 ext4_unlock_group(sb, group);
5370out_unload:
5371 ext4_mb_unload_buddy(&e4b);
5372
5373 return error;
5374}