blob: 89fbff1a9b2ca062d649c976e075eb3f36abb6c2 [file] [log] [blame]
Alex Tomasc9de5602008-01-29 00:19:52 -05001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
Adam Buchbinderb8a074632016-03-09 23:49:05 -050014 * You should have received a copy of the GNU General Public License
Alex Tomasc9de5602008-01-29 00:19:52 -050015 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
17 */
18
19
20/*
21 * mballoc.c contains the multiblocks allocation routines
22 */
23
Bobi Jam18aadd42012-02-20 17:53:02 -050024#include "ext4_jbd2.h"
Mingming Cao8f6e39a2008-04-29 22:01:31 -040025#include "mballoc.h"
Theodore Ts'o28623c22012-09-05 01:31:50 -040026#include <linux/log2.h>
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050027#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Jeremy Cline51ada112018-08-02 00:03:40 -040029#include <linux/nospec.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040030#include <linux/backing-dev.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040031#include <trace/events/ext4.h>
32
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050033#ifdef CONFIG_EXT4_DEBUG
34ushort ext4_mballoc_debug __read_mostly;
35
36module_param_named(mballoc_debug, ext4_mballoc_debug, ushort, 0644);
37MODULE_PARM_DESC(mballoc_debug, "Debugging level for ext4's mballoc");
38#endif
39
Alex Tomasc9de5602008-01-29 00:19:52 -050040/*
41 * MUSTDO:
42 * - test ext4_ext_search_left() and ext4_ext_search_right()
43 * - search for metadata in few groups
44 *
45 * TODO v4:
46 * - normalization should take into account whether file is still open
47 * - discard preallocations if no free space left (policy?)
48 * - don't normalize tails
49 * - quota
50 * - reservation for superuser
51 *
52 * TODO v3:
53 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
54 * - track min/max extents in each group for better group selection
55 * - mb_mark_used() may allocate chunk right after splitting buddy
56 * - tree of groups sorted by number of free blocks
57 * - error handling
58 */
59
60/*
61 * The allocation request involve request for multiple number of blocks
62 * near to the goal(block) value specified.
63 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040064 * During initialization phase of the allocator we decide to use the
65 * group preallocation or inode preallocation depending on the size of
66 * the file. The size of the file could be the resulting file size we
67 * would have after allocation, or the current file size, which ever
68 * is larger. If the size is less than sbi->s_mb_stream_request we
69 * select to use the group preallocation. The default value of
70 * s_mb_stream_request is 16 blocks. This can also be tuned via
71 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
72 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050073 *
74 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040075 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050076 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040077 * First stage the allocator looks at the inode prealloc list,
78 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
79 * spaces for this particular inode. The inode prealloc space is
80 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050081 *
82 * pa_lstart -> the logical start block for this prealloc space
83 * pa_pstart -> the physical start block for this prealloc space
Theodore Ts'o53accfa2011-09-09 18:48:51 -040084 * pa_len -> length for this prealloc space (in clusters)
85 * pa_free -> free space available in this prealloc space (in clusters)
Alex Tomasc9de5602008-01-29 00:19:52 -050086 *
87 * The inode preallocation space is used looking at the _logical_ start
88 * block. If only the logical file block falls within the range of prealloc
Tao Macaaf7a22011-07-11 18:42:42 -040089 * space we will consume the particular prealloc space. This makes sure that
90 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050091 *
92 * The important thing to be noted in case of inode prealloc space is that
93 * we don't modify the values associated to inode prealloc space except
94 * pa_free.
95 *
96 * If we are not able to find blocks in the inode prealloc space and if we
97 * have the group allocation flag set then we look at the locality group
Tao Macaaf7a22011-07-11 18:42:42 -040098 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050099 *
100 * ext4_sb_info.s_locality_groups[smp_processor_id()]
101 *
102 * The reason for having a per cpu locality group is to reduce the contention
103 * between CPUs. It is possible to get scheduled at this point.
104 *
105 * The locality group prealloc space is used looking at whether we have
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300106 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -0500107 *
108 * If we can't allocate blocks via inode prealloc or/and locality group
109 * prealloc then we look at the buddy cache. The buddy cache is represented
110 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
111 * mapped to the buddy and bitmap information regarding different
112 * groups. The buddy information is attached to buddy cache inode so that
113 * we can access them through the page cache. The information regarding
114 * each group is loaded via ext4_mb_load_buddy. The information involve
115 * block bitmap and buddy information. The information are stored in the
116 * inode as:
117 *
118 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500119 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500120 *
121 *
122 * one block each for bitmap and buddy information. So for each group we
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300123 * take up 2 blocks. A page can contain blocks_per_page (PAGE_SIZE /
Alex Tomasc9de5602008-01-29 00:19:52 -0500124 * blocksize) blocks. So it can have information regarding groups_per_page
125 * which is blocks_per_page/2
126 *
127 * The buddy cache inode is not stored on disk. The inode is thrown
128 * away when the filesystem is unmounted.
129 *
130 * We look for count number of blocks in the buddy cache. If we were able
131 * to locate that many free blocks we return with additional information
132 * regarding rest of the contiguous physical block available
133 *
134 * Before allocating blocks via buddy cache we normalize the request
135 * blocks. This ensure we ask for more blocks that we needed. The extra
136 * blocks that we get after allocation is added to the respective prealloc
137 * list. In case of inode preallocation we follow a list of heuristics
138 * based on file size. This can be found in ext4_mb_normalize_request. If
139 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'o27baebb2011-09-09 19:02:51 -0400140 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
141 * dependent on the cluster size; for non-bigalloc file systems, it is
Alex Tomasc9de5602008-01-29 00:19:52 -0500142 * 512 blocks. This can be tuned via
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400143 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500144 * terms of number of blocks. If we have mounted the file system with -O
145 * stripe=<value> option the group prealloc request is normalized to the
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400146 * the smallest multiple of the stripe value (sbi->s_stripe) which is
147 * greater than the default mb_group_prealloc.
Alex Tomasc9de5602008-01-29 00:19:52 -0500148 *
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400149 * The regular allocator (using the buddy cache) supports a few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500150 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400151 * /sys/fs/ext4/<partition>/mb_min_to_scan
152 * /sys/fs/ext4/<partition>/mb_max_to_scan
153 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500154 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400155 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500156 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
157 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400158 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200159 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400160 * stripe size. This should result in better allocation on RAID setups. If
161 * not, we search in the specific group using bitmap for best extents. The
162 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500163 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400164 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500165 * best extent in the found extents. Searching for the blocks starts with
166 * the group specified as the goal value in allocation context via
167 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400168 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500169 * checked.
170 *
171 * Both the prealloc space are getting populated as above. So for the first
172 * request we will hit the buddy cache which will result in this prealloc
173 * space getting filled. The prealloc space is then later used for the
174 * subsequent request.
175 */
176
177/*
178 * mballoc operates on the following data:
179 * - on-disk bitmap
180 * - in-core buddy (actually includes buddy and bitmap)
181 * - preallocation descriptors (PAs)
182 *
183 * there are two types of preallocations:
184 * - inode
185 * assiged to specific inode and can be used for this inode only.
186 * it describes part of inode's space preallocated to specific
187 * physical blocks. any block from that preallocated can be used
188 * independent. the descriptor just tracks number of blocks left
189 * unused. so, before taking some block from descriptor, one must
190 * make sure corresponded logical block isn't allocated yet. this
191 * also means that freeing any block within descriptor's range
192 * must discard all preallocated blocks.
193 * - locality group
194 * assigned to specific locality group which does not translate to
195 * permanent set of inodes: inode can join and leave group. space
196 * from this type of preallocation can be used for any inode. thus
197 * it's consumed from the beginning to the end.
198 *
199 * relation between them can be expressed as:
200 * in-core buddy = on-disk bitmap + preallocation descriptors
201 *
202 * this mean blocks mballoc considers used are:
203 * - allocated blocks (persistent)
204 * - preallocated blocks (non-persistent)
205 *
206 * consistency in mballoc world means that at any time a block is either
207 * free or used in ALL structures. notice: "any time" should not be read
208 * literally -- time is discrete and delimited by locks.
209 *
210 * to keep it simple, we don't use block numbers, instead we count number of
211 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
212 *
213 * all operations can be expressed as:
214 * - init buddy: buddy = on-disk + PAs
215 * - new PA: buddy += N; PA = N
216 * - use inode PA: on-disk += N; PA -= N
217 * - discard inode PA buddy -= on-disk - PA; PA = 0
218 * - use locality group PA on-disk += N; PA -= N
219 * - discard locality group PA buddy -= PA; PA = 0
220 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
221 * is used in real operation because we can't know actual used
222 * bits from PA, only from on-disk bitmap
223 *
224 * if we follow this strict logic, then all operations above should be atomic.
225 * given some of them can block, we'd have to use something like semaphores
226 * killing performance on high-end SMP hardware. let's try to relax it using
227 * the following knowledge:
228 * 1) if buddy is referenced, it's already initialized
229 * 2) while block is used in buddy and the buddy is referenced,
230 * nobody can re-allocate that block
231 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
232 * bit set and PA claims same block, it's OK. IOW, one can set bit in
233 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
234 * block
235 *
236 * so, now we're building a concurrency table:
237 * - init buddy vs.
238 * - new PA
239 * blocks for PA are allocated in the buddy, buddy must be referenced
240 * until PA is linked to allocation group to avoid concurrent buddy init
241 * - use inode PA
242 * we need to make sure that either on-disk bitmap or PA has uptodate data
243 * given (3) we care that PA-=N operation doesn't interfere with init
244 * - discard inode PA
245 * the simplest way would be to have buddy initialized by the discard
246 * - use locality group PA
247 * again PA-=N must be serialized with init
248 * - discard locality group PA
249 * the simplest way would be to have buddy initialized by the discard
250 * - new PA vs.
251 * - use inode PA
252 * i_data_sem serializes them
253 * - discard inode PA
254 * discard process must wait until PA isn't used by another process
255 * - use locality group PA
256 * some mutex should serialize them
257 * - discard locality group PA
258 * discard process must wait until PA isn't used by another process
259 * - use inode PA
260 * - use inode PA
261 * i_data_sem or another mutex should serializes them
262 * - discard inode PA
263 * discard process must wait until PA isn't used by another process
264 * - use locality group PA
265 * nothing wrong here -- they're different PAs covering different blocks
266 * - discard locality group PA
267 * discard process must wait until PA isn't used by another process
268 *
269 * now we're ready to make few consequences:
270 * - PA is referenced and while it is no discard is possible
271 * - PA is referenced until block isn't marked in on-disk bitmap
272 * - PA changes only after on-disk bitmap
273 * - discard must not compete with init. either init is done before
274 * any discard or they're serialized somehow
275 * - buddy init as sum of on-disk bitmap and PAs is done atomically
276 *
277 * a special case when we've used PA to emptiness. no need to modify buddy
278 * in this case, but we should care about concurrent init
279 *
280 */
281
282 /*
283 * Logic in few words:
284 *
285 * - allocation:
286 * load group
287 * find blocks
288 * mark bits in on-disk bitmap
289 * release group
290 *
291 * - use preallocation:
292 * find proper PA (per-inode or group)
293 * load group
294 * mark bits in on-disk bitmap
295 * release group
296 * release PA
297 *
298 * - free:
299 * load group
300 * mark bits in on-disk bitmap
301 * release group
302 *
303 * - discard preallocations in group:
304 * mark PAs deleted
305 * move them onto local list
306 * load on-disk bitmap
307 * load group
308 * remove PA from object (inode or locality group)
309 * mark free blocks in-core
310 *
311 * - discard inode's preallocations:
312 */
313
314/*
315 * Locking rules
316 *
317 * Locks:
318 * - bitlock on a group (group)
319 * - object (inode/locality) (object)
320 * - per-pa lock (pa)
321 *
322 * Paths:
323 * - new pa
324 * object
325 * group
326 *
327 * - find and use pa:
328 * pa
329 *
330 * - release consumed pa:
331 * pa
332 * group
333 * object
334 *
335 * - generate in-core bitmap:
336 * group
337 * pa
338 *
339 * - discard all for given object (inode, locality group):
340 * object
341 * pa
342 * group
343 *
344 * - discard all for given group:
345 * group
346 * pa
347 * group
348 * object
349 *
350 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500351static struct kmem_cache *ext4_pspace_cachep;
352static struct kmem_cache *ext4_ac_cachep;
Bobi Jam18aadd42012-02-20 17:53:02 -0500353static struct kmem_cache *ext4_free_data_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400354
355/* We create slab caches for groupinfo data structures based on the
356 * superblock block size. There will be one per mounted filesystem for
357 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500358#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400359static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
360
Eric Sandeen2892c152011-02-12 08:12:18 -0500361static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
362 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
363 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
364 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
365};
366
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500367static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
368 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500369static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
370 ext4_group_t group);
Bobi Jam18aadd42012-02-20 17:53:02 -0500371static void ext4_free_data_callback(struct super_block *sb,
372 struct ext4_journal_cb_entry *jce, int rc);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500373
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500374static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
375{
Alex Tomasc9de5602008-01-29 00:19:52 -0500376#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500377 *bit += ((unsigned long) addr & 7UL) << 3;
378 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500379#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500380 *bit += ((unsigned long) addr & 3UL) << 3;
381 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500382#else
383#error "how many bits you are?!"
384#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500385 return addr;
386}
Alex Tomasc9de5602008-01-29 00:19:52 -0500387
388static inline int mb_test_bit(int bit, void *addr)
389{
390 /*
391 * ext4_test_bit on architecture like powerpc
392 * needs unsigned long aligned address
393 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500394 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500395 return ext4_test_bit(bit, addr);
396}
397
398static inline void mb_set_bit(int bit, void *addr)
399{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500400 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500401 ext4_set_bit(bit, addr);
402}
403
Alex Tomasc9de5602008-01-29 00:19:52 -0500404static inline void mb_clear_bit(int bit, void *addr)
405{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500406 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500407 ext4_clear_bit(bit, addr);
408}
409
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400410static inline int mb_test_and_clear_bit(int bit, void *addr)
411{
412 addr = mb_correct_addr_and_bit(&bit, addr);
413 return ext4_test_and_clear_bit(bit, addr);
414}
415
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500416static inline int mb_find_next_zero_bit(void *addr, int max, int start)
417{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400418 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500419 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400420 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500421 start += fix;
422
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400423 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
424 if (ret > max)
425 return max;
426 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500427}
428
429static inline int mb_find_next_bit(void *addr, int max, int start)
430{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400431 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500432 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400433 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500434 start += fix;
435
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400436 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
437 if (ret > max)
438 return max;
439 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500440}
441
Alex Tomasc9de5602008-01-29 00:19:52 -0500442static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
443{
444 char *bb;
445
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500446 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -0500447 BUG_ON(max == NULL);
448
449 if (order > e4b->bd_blkbits + 1) {
450 *max = 0;
451 return NULL;
452 }
453
454 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500455 if (order == 0) {
456 *max = 1 << (e4b->bd_blkbits + 3);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500457 return e4b->bd_bitmap;
Coly Li84b775a2011-02-24 12:51:59 -0500458 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500459
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500460 bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
Alex Tomasc9de5602008-01-29 00:19:52 -0500461 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
462
463 return bb;
464}
465
466#ifdef DOUBLE_CHECK
467static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
468 int first, int count)
469{
470 int i;
471 struct super_block *sb = e4b->bd_sb;
472
473 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
474 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400475 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500476 for (i = 0; i < count; i++) {
477 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
478 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500479
480 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400481 blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500482 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400483 inode ? inode->i_ino : 0,
484 blocknr,
485 "freeing block already freed "
486 "(bit %u)",
487 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500488 }
489 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
490 }
491}
492
493static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
494{
495 int i;
496
497 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
498 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400499 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500500 for (i = 0; i < count; i++) {
501 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
502 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
503 }
504}
505
506static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
507{
508 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
509 unsigned char *b1, *b2;
510 int i;
511 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
512 b2 = (unsigned char *) bitmap;
513 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
514 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400515 ext4_msg(e4b->bd_sb, KERN_ERR,
516 "corruption in group %u "
517 "at byte %u(%u): %x in copy != %x "
518 "on disk/prealloc",
519 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500520 BUG();
521 }
522 }
523 }
524}
525
526#else
527static inline void mb_free_blocks_double(struct inode *inode,
528 struct ext4_buddy *e4b, int first, int count)
529{
530 return;
531}
532static inline void mb_mark_used_double(struct ext4_buddy *e4b,
533 int first, int count)
534{
535 return;
536}
537static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
538{
539 return;
540}
541#endif
542
543#ifdef AGGRESSIVE_CHECK
544
545#define MB_CHECK_ASSERT(assert) \
546do { \
547 if (!(assert)) { \
548 printk(KERN_EMERG \
549 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
550 function, file, line, # assert); \
551 BUG(); \
552 } \
553} while (0)
554
555static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
556 const char *function, int line)
557{
558 struct super_block *sb = e4b->bd_sb;
559 int order = e4b->bd_blkbits + 1;
560 int max;
561 int max2;
562 int i;
563 int j;
564 int k;
565 int count;
566 struct ext4_group_info *grp;
567 int fragments = 0;
568 int fstart;
569 struct list_head *cur;
570 void *buddy;
571 void *buddy2;
572
Alex Tomasc9de5602008-01-29 00:19:52 -0500573 {
574 static int mb_check_counter;
575 if (mb_check_counter++ % 100 != 0)
576 return 0;
577 }
578
579 while (order > 1) {
580 buddy = mb_find_buddy(e4b, order, &max);
581 MB_CHECK_ASSERT(buddy);
582 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
583 MB_CHECK_ASSERT(buddy2);
584 MB_CHECK_ASSERT(buddy != buddy2);
585 MB_CHECK_ASSERT(max * 2 == max2);
586
587 count = 0;
588 for (i = 0; i < max; i++) {
589
590 if (mb_test_bit(i, buddy)) {
591 /* only single bit in buddy2 may be 1 */
592 if (!mb_test_bit(i << 1, buddy2)) {
593 MB_CHECK_ASSERT(
594 mb_test_bit((i<<1)+1, buddy2));
595 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
596 MB_CHECK_ASSERT(
597 mb_test_bit(i << 1, buddy2));
598 }
599 continue;
600 }
601
Robin Dong0a10da72011-10-26 08:48:54 -0400602 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500603 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
604 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
605
606 for (j = 0; j < (1 << order); j++) {
607 k = (i * (1 << order)) + j;
608 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500609 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500610 }
611 count++;
612 }
613 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
614 order--;
615 }
616
617 fstart = -1;
618 buddy = mb_find_buddy(e4b, 0, &max);
619 for (i = 0; i < max; i++) {
620 if (!mb_test_bit(i, buddy)) {
621 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
622 if (fstart == -1) {
623 fragments++;
624 fstart = i;
625 }
626 continue;
627 }
628 fstart = -1;
629 /* check used bits only */
630 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
631 buddy2 = mb_find_buddy(e4b, j, &max2);
632 k = i >> j;
633 MB_CHECK_ASSERT(k < max2);
634 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
635 }
636 }
637 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
638 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
639
640 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500641 list_for_each(cur, &grp->bb_prealloc_list) {
642 ext4_group_t groupnr;
643 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400644 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
645 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500646 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400647 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500648 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
649 }
650 return 0;
651}
652#undef MB_CHECK_ASSERT
653#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400654 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500655#else
656#define mb_check_buddy(e4b)
657#endif
658
Coly Li7c786052011-02-24 13:24:25 -0500659/*
660 * Divide blocks started from @first with length @len into
661 * smaller chunks with power of 2 blocks.
662 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
663 * then increase bb_counters[] for corresponded chunk size.
664 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500665static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400666 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500667 struct ext4_group_info *grp)
668{
669 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400670 ext4_grpblk_t min;
671 ext4_grpblk_t max;
672 ext4_grpblk_t chunk;
Chandan Rajendrac3881ab2016-11-14 21:04:37 -0500673 unsigned int border;
Alex Tomasc9de5602008-01-29 00:19:52 -0500674
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400675 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500676
677 border = 2 << sb->s_blocksize_bits;
678
679 while (len > 0) {
680 /* find how many blocks can be covered since this position */
681 max = ffs(first | border) - 1;
682
683 /* find how many blocks of power 2 we need to mark */
684 min = fls(len) - 1;
685
686 if (max < min)
687 min = max;
688 chunk = 1 << min;
689
690 /* mark multiblock chunks only */
691 grp->bb_counters[min]++;
692 if (min > 0)
693 mb_clear_bit(first >> min,
694 buddy + sbi->s_mb_offsets[min]);
695
696 len -= chunk;
697 first += chunk;
698 }
699}
700
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400701/*
702 * Cache the order of the largest free extent we have available in this block
703 * group.
704 */
705static void
706mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
707{
708 int i;
709 int bits;
710
711 grp->bb_largest_free_order = -1; /* uninit */
712
713 bits = sb->s_blocksize_bits + 1;
714 for (i = bits; i >= 0; i--) {
715 if (grp->bb_counters[i] > 0) {
716 grp->bb_largest_free_order = i;
717 break;
718 }
719 }
720}
721
Eric Sandeen089ceec2009-07-05 22:17:31 -0400722static noinline_for_stack
723void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500724 void *buddy, void *bitmap, ext4_group_t group)
725{
726 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400727 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400728 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400729 ext4_grpblk_t i = 0;
730 ext4_grpblk_t first;
731 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500732 unsigned free = 0;
733 unsigned fragments = 0;
734 unsigned long long period = get_cycles();
735
736 /* initialize buddy from bitmap which is aggregation
737 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500738 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500739 grp->bb_first_free = i;
740 while (i < max) {
741 fragments++;
742 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500743 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500744 len = i - first;
745 free += len;
746 if (len > 1)
747 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
748 else
749 grp->bb_counters[0]++;
750 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500751 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500752 }
753 grp->bb_fragments = fragments;
754
755 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400756 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o94d4c062014-07-05 19:15:50 -0400757 "block bitmap and bg descriptor "
758 "inconsistent: %u vs %u free clusters",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400759 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500760 /*
Darrick J. Wong163a2032013-08-28 17:35:51 -0400761 * If we intend to continue, we consider group descriptor
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500762 * corrupt and update bb_free using bitmap value
763 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500764 grp->bb_free = free;
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400765 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
766 percpu_counter_sub(&sbi->s_freeclusters_counter,
767 grp->bb_free);
Darrick J. Wong163a2032013-08-28 17:35:51 -0400768 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
Alex Tomasc9de5602008-01-29 00:19:52 -0500769 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400770 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500771
772 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
773
774 period = get_cycles() - period;
775 spin_lock(&EXT4_SB(sb)->s_bal_lock);
776 EXT4_SB(sb)->s_mb_buddies_generated++;
777 EXT4_SB(sb)->s_mb_generation_time += period;
778 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
779}
780
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400781static void mb_regenerate_buddy(struct ext4_buddy *e4b)
782{
783 int count;
784 int order = 1;
785 void *buddy;
786
787 while ((buddy = mb_find_buddy(e4b, order++, &count))) {
788 ext4_set_bits(buddy, 0, count);
789 }
790 e4b->bd_info->bb_fragments = 0;
791 memset(e4b->bd_info->bb_counters, 0,
792 sizeof(*e4b->bd_info->bb_counters) *
793 (e4b->bd_sb->s_blocksize_bits + 2));
794
795 ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy,
796 e4b->bd_bitmap, e4b->bd_group);
797}
798
Alex Tomasc9de5602008-01-29 00:19:52 -0500799/* The buddy information is attached the buddy cache inode
800 * for convenience. The information regarding each group
801 * is loaded via ext4_mb_load_buddy. The information involve
802 * block bitmap and buddy information. The information are
803 * stored in the inode as
804 *
805 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500806 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500807 *
808 *
809 * one block each for bitmap and buddy information.
810 * So for each group we take up 2 blocks. A page can
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300811 * contain blocks_per_page (PAGE_SIZE / blocksize) blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -0500812 * So it can have information regarding groups_per_page which
813 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400814 *
815 * Locking note: This routine takes the block group lock of all groups
816 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500817 */
818
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400819static int ext4_mb_init_cache(struct page *page, char *incore, gfp_t gfp)
Alex Tomasc9de5602008-01-29 00:19:52 -0500820{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400821 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500822 int blocksize;
823 int blocks_per_page;
824 int groups_per_page;
825 int err = 0;
826 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500827 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500828 int first_block;
829 struct super_block *sb;
830 struct buffer_head *bhs;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -0400831 struct buffer_head **bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500832 struct inode *inode;
833 char *data;
834 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400835 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500836
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400837 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500838
839 inode = page->mapping->host;
840 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400841 ngroups = ext4_get_groups_count(sb);
Fabian Frederick61604a22017-02-27 14:28:32 -0800842 blocksize = i_blocksize(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300843 blocks_per_page = PAGE_SIZE / blocksize;
Alex Tomasc9de5602008-01-29 00:19:52 -0500844
845 groups_per_page = blocks_per_page >> 1;
846 if (groups_per_page == 0)
847 groups_per_page = 1;
848
849 /* allocate buffer_heads to read bitmaps */
850 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500851 i = sizeof(struct buffer_head *) * groups_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400852 bh = kzalloc(i, gfp);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500853 if (bh == NULL) {
854 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500855 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500856 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500857 } else
858 bh = &bhs;
859
860 first_group = page->index * blocks_per_page / 2;
861
862 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500863 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
864 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500865 break;
866
Theodore Ts'o813e5722012-02-20 17:52:46 -0500867 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400868 /*
869 * If page is uptodate then we came here after online resize
870 * which added some new uninitialized group info structs, so
871 * we must skip all initialized uptodate buddies on the page,
872 * which may be currently in use by an allocating task.
873 */
874 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
875 bh[i] = NULL;
876 continue;
877 }
Darrick J. Wong9008a582015-10-17 21:33:24 -0400878 bh[i] = ext4_read_block_bitmap_nowait(sb, group);
879 if (IS_ERR(bh[i])) {
880 err = PTR_ERR(bh[i]);
881 bh[i] = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500882 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500883 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500884 mb_debug(1, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500885 }
886
887 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500888 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
Darrick J. Wong9008a582015-10-17 21:33:24 -0400889 int err2;
890
891 if (!bh[i])
892 continue;
893 err2 = ext4_wait_block_bitmap(sb, group, bh[i]);
894 if (!err)
895 err = err2;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500896 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500897
898 first_block = page->index * blocks_per_page;
899 for (i = 0; i < blocks_per_page; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500900 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400901 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500902 break;
903
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400904 if (!bh[group - first_group])
905 /* skip initialized uptodate buddy */
906 continue;
907
Lukas Czernerbbdc3222015-06-08 11:38:37 -0400908 if (!buffer_verified(bh[group - first_group]))
909 /* Skip faulty bitmaps */
910 continue;
911 err = 0;
912
Alex Tomasc9de5602008-01-29 00:19:52 -0500913 /*
914 * data carry information regarding this
915 * particular group in the format specified
916 * above
917 *
918 */
919 data = page_address(page) + (i * blocksize);
920 bitmap = bh[group - first_group]->b_data;
921
922 /*
923 * We place the buddy block and bitmap block
924 * close together
925 */
926 if ((first_block + i) & 1) {
927 /* this is block of buddy */
928 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400929 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500930 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400931 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500932 grinfo = ext4_get_group_info(sb, group);
933 grinfo->bb_fragments = 0;
934 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400935 sizeof(*grinfo->bb_counters) *
936 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500937 /*
938 * incore got set to the group block bitmap below
939 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500940 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400941 /* init the buddy */
942 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500943 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500944 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500945 incore = NULL;
946 } else {
947 /* this is block of bitmap */
948 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400949 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500950 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400951 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500952
953 /* see comments in ext4_mb_put_pa() */
954 ext4_lock_group(sb, group);
955 memcpy(data, bitmap, blocksize);
956
957 /* mark all preallocated blks used in in-core bitmap */
958 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500959 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500960 ext4_unlock_group(sb, group);
961
962 /* set incore so that the buddy information can be
963 * generated using this
964 */
965 incore = data;
966 }
967 }
968 SetPageUptodate(page);
969
970out:
971 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400972 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500973 brelse(bh[i]);
974 if (bh != &bhs)
975 kfree(bh);
976 }
977 return err;
978}
979
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400980/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400981 * Lock the buddy and bitmap pages. This make sure other parallel init_group
982 * on the same buddy page doesn't happen whild holding the buddy page lock.
983 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
984 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400985 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400986static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -0400987 ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400988{
Amir Goldstein2de88072011-05-09 21:48:13 -0400989 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
990 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400991 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400992 struct page *page;
993
994 e4b->bd_buddy_page = NULL;
995 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400996
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300997 blocks_per_page = PAGE_SIZE / sb->s_blocksize;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400998 /*
999 * the buddy cache inode stores the block bitmap
1000 * and buddy information in consecutive blocks.
1001 * So for each group we need two blocks.
1002 */
1003 block = group * 2;
1004 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -04001005 poff = block % blocks_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001006 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001007 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001008 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001009 BUG_ON(page->mapping != inode->i_mapping);
1010 e4b->bd_bitmap_page = page;
1011 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001012
Amir Goldstein2de88072011-05-09 21:48:13 -04001013 if (blocks_per_page >= 2) {
1014 /* buddy and bitmap are on the same page */
1015 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001016 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001017
1018 block++;
1019 pnum = block / blocks_per_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001020 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001021 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001022 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001023 BUG_ON(page->mapping != inode->i_mapping);
1024 e4b->bd_buddy_page = page;
1025 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001026}
1027
Amir Goldstein2de88072011-05-09 21:48:13 -04001028static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001029{
Amir Goldstein2de88072011-05-09 21:48:13 -04001030 if (e4b->bd_bitmap_page) {
1031 unlock_page(e4b->bd_bitmap_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001032 put_page(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001033 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001034 if (e4b->bd_buddy_page) {
1035 unlock_page(e4b->bd_buddy_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001036 put_page(e4b->bd_buddy_page);
Amir Goldstein2de88072011-05-09 21:48:13 -04001037 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001038}
1039
1040/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001041 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1042 * block group lock of all groups for this page; do not hold the BG lock when
1043 * calling this routine!
1044 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001045static noinline_for_stack
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001046int ext4_mb_init_group(struct super_block *sb, ext4_group_t group, gfp_t gfp)
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001047{
1048
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001049 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001050 struct ext4_buddy e4b;
1051 struct page *page;
1052 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001053
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001054 might_sleep();
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001055 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001056 this_grp = ext4_get_group_info(sb, group);
1057 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001058 * This ensures that we don't reinit the buddy cache
1059 * page which map to the group from which we are already
1060 * allocating. If we are looking at the buddy cache we would
1061 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001062 * would have pinned buddy page to page cache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001063 * The call to ext4_mb_get_buddy_page_lock will mark the
1064 * page accessed.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001065 */
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001066 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001067 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001068 /*
1069 * somebody initialized the group
1070 * return without doing anything
1071 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001072 goto err;
1073 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001074
1075 page = e4b.bd_bitmap_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001076 ret = ext4_mb_init_cache(page, NULL, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001077 if (ret)
1078 goto err;
1079 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001080 ret = -EIO;
1081 goto err;
1082 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001083
Amir Goldstein2de88072011-05-09 21:48:13 -04001084 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001085 /*
1086 * If both the bitmap and buddy are in
1087 * the same page we don't need to force
1088 * init the buddy
1089 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001090 ret = 0;
1091 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001092 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001093 /* init buddy cache */
1094 page = e4b.bd_buddy_page;
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001095 ret = ext4_mb_init_cache(page, e4b.bd_bitmap, gfp);
Amir Goldstein2de88072011-05-09 21:48:13 -04001096 if (ret)
1097 goto err;
1098 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001099 ret = -EIO;
1100 goto err;
1101 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001102err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001103 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001104 return ret;
1105}
1106
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001107/*
1108 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1109 * block group lock of all groups for this page; do not hold the BG lock when
1110 * calling this routine!
1111 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001112static noinline_for_stack int
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001113ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group,
1114 struct ext4_buddy *e4b, gfp_t gfp)
Alex Tomasc9de5602008-01-29 00:19:52 -05001115{
Alex Tomasc9de5602008-01-29 00:19:52 -05001116 int blocks_per_page;
1117 int block;
1118 int pnum;
1119 int poff;
1120 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001121 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001122 struct ext4_group_info *grp;
1123 struct ext4_sb_info *sbi = EXT4_SB(sb);
1124 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001125
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001126 might_sleep();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001127 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001128
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001129 blocks_per_page = PAGE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001130 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001131
1132 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001133 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001134 e4b->bd_sb = sb;
1135 e4b->bd_group = group;
1136 e4b->bd_buddy_page = NULL;
1137 e4b->bd_bitmap_page = NULL;
1138
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001139 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001140 /*
1141 * we need full data about the group
1142 * to make a good selection
1143 */
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001144 ret = ext4_mb_init_group(sb, group, gfp);
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001145 if (ret)
1146 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001147 }
1148
Alex Tomasc9de5602008-01-29 00:19:52 -05001149 /*
1150 * the buddy cache inode stores the block bitmap
1151 * and buddy information in consecutive blocks.
1152 * So for each group we need two blocks.
1153 */
1154 block = group * 2;
1155 pnum = block / blocks_per_page;
1156 poff = block % blocks_per_page;
1157
1158 /* we could use find_or_create_page(), but it locks page
1159 * what we'd like to avoid in fast path ... */
Mel Gorman2457aec2014-06-04 16:10:31 -07001160 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001161 if (page == NULL || !PageUptodate(page)) {
1162 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001163 /*
1164 * drop the page reference and try
1165 * to get the page with lock. If we
1166 * are not uptodate that implies
1167 * somebody just created the page but
1168 * is yet to initialize the same. So
1169 * wait for it to initialize.
1170 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001171 put_page(page);
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001172 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001173 if (page) {
1174 BUG_ON(page->mapping != inode->i_mapping);
1175 if (!PageUptodate(page)) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001176 ret = ext4_mb_init_cache(page, NULL, gfp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001177 if (ret) {
1178 unlock_page(page);
1179 goto err;
1180 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001181 mb_cmp_bitmaps(e4b, page_address(page) +
1182 (poff * sb->s_blocksize));
1183 }
1184 unlock_page(page);
1185 }
1186 }
Younger Liuc57ab392014-04-10 23:03:43 -04001187 if (page == NULL) {
1188 ret = -ENOMEM;
1189 goto err;
1190 }
1191 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001192 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001193 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001194 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001195
1196 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001197 e4b->bd_bitmap_page = page;
1198 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001199
1200 block++;
1201 pnum = block / blocks_per_page;
1202 poff = block % blocks_per_page;
1203
Mel Gorman2457aec2014-06-04 16:10:31 -07001204 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001205 if (page == NULL || !PageUptodate(page)) {
1206 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001207 put_page(page);
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001208 page = find_or_create_page(inode->i_mapping, pnum, gfp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001209 if (page) {
1210 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001211 if (!PageUptodate(page)) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001212 ret = ext4_mb_init_cache(page, e4b->bd_bitmap,
1213 gfp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001214 if (ret) {
1215 unlock_page(page);
1216 goto err;
1217 }
1218 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001219 unlock_page(page);
1220 }
1221 }
Younger Liuc57ab392014-04-10 23:03:43 -04001222 if (page == NULL) {
1223 ret = -ENOMEM;
1224 goto err;
1225 }
1226 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001227 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001228 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001229 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001230
1231 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001232 e4b->bd_buddy_page = page;
1233 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001234
1235 BUG_ON(e4b->bd_bitmap_page == NULL);
1236 BUG_ON(e4b->bd_buddy_page == NULL);
1237
1238 return 0;
1239
1240err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001241 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001242 put_page(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001243 if (e4b->bd_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001244 put_page(e4b->bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001245 if (e4b->bd_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001246 put_page(e4b->bd_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001247 e4b->bd_buddy = NULL;
1248 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001249 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001250}
1251
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04001252static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1253 struct ext4_buddy *e4b)
1254{
1255 return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS);
1256}
1257
Jing Zhange39e07f2010-05-14 00:00:00 -04001258static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001259{
1260 if (e4b->bd_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001261 put_page(e4b->bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001262 if (e4b->bd_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001263 put_page(e4b->bd_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001264}
1265
1266
1267static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1268{
1269 int order = 1;
Nicolai Stangeb5cb3162016-05-05 17:38:03 -04001270 int bb_incr = 1 << (e4b->bd_blkbits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05001271 void *bb;
1272
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001273 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -05001274 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1275
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001276 bb = e4b->bd_buddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05001277 while (order <= e4b->bd_blkbits + 1) {
1278 block = block >> 1;
1279 if (!mb_test_bit(block, bb)) {
1280 /* this block is part of buddy of order 'order' */
1281 return order;
1282 }
Nicolai Stangeb5cb3162016-05-05 17:38:03 -04001283 bb += bb_incr;
1284 bb_incr >>= 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001285 order++;
1286 }
1287 return 0;
1288}
1289
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001290static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001291{
1292 __u32 *addr;
1293
1294 len = cur + len;
1295 while (cur < len) {
1296 if ((cur & 31) == 0 && (len - cur) >= 32) {
1297 /* fast path: clear whole word at once */
1298 addr = bm + (cur >> 3);
1299 *addr = 0;
1300 cur += 32;
1301 continue;
1302 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001303 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001304 cur++;
1305 }
1306}
1307
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001308/* clear bits in given range
1309 * will return first found zero bit if any, -1 otherwise
1310 */
1311static int mb_test_and_clear_bits(void *bm, int cur, int len)
1312{
1313 __u32 *addr;
1314 int zero_bit = -1;
1315
1316 len = cur + len;
1317 while (cur < len) {
1318 if ((cur & 31) == 0 && (len - cur) >= 32) {
1319 /* fast path: clear whole word at once */
1320 addr = bm + (cur >> 3);
1321 if (*addr != (__u32)(-1) && zero_bit == -1)
1322 zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0);
1323 *addr = 0;
1324 cur += 32;
1325 continue;
1326 }
1327 if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1)
1328 zero_bit = cur;
1329 cur++;
1330 }
1331
1332 return zero_bit;
1333}
1334
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001335void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001336{
1337 __u32 *addr;
1338
1339 len = cur + len;
1340 while (cur < len) {
1341 if ((cur & 31) == 0 && (len - cur) >= 32) {
1342 /* fast path: set whole word at once */
1343 addr = bm + (cur >> 3);
1344 *addr = 0xffffffff;
1345 cur += 32;
1346 continue;
1347 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001348 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001349 cur++;
1350 }
1351}
1352
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001353/*
1354 * _________________________________________________________________ */
1355
1356static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side)
Alex Tomasc9de5602008-01-29 00:19:52 -05001357{
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001358 if (mb_test_bit(*bit + side, bitmap)) {
1359 mb_clear_bit(*bit, bitmap);
1360 (*bit) -= side;
1361 return 1;
1362 }
1363 else {
1364 (*bit) += side;
1365 mb_set_bit(*bit, bitmap);
1366 return -1;
1367 }
1368}
1369
1370static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last)
1371{
1372 int max;
1373 int order = 1;
1374 void *buddy = mb_find_buddy(e4b, order, &max);
1375
1376 while (buddy) {
1377 void *buddy2;
1378
1379 /* Bits in range [first; last] are known to be set since
1380 * corresponding blocks were allocated. Bits in range
1381 * (first; last) will stay set because they form buddies on
1382 * upper layer. We just deal with borders if they don't
1383 * align with upper layer and then go up.
1384 * Releasing entire group is all about clearing
1385 * single bit of highest order buddy.
1386 */
1387
1388 /* Example:
1389 * ---------------------------------
1390 * | 1 | 1 | 1 | 1 |
1391 * ---------------------------------
1392 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1393 * ---------------------------------
1394 * 0 1 2 3 4 5 6 7
1395 * \_____________________/
1396 *
1397 * Neither [1] nor [6] is aligned to above layer.
1398 * Left neighbour [0] is free, so mark it busy,
1399 * decrease bb_counters and extend range to
1400 * [0; 6]
1401 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1402 * mark [6] free, increase bb_counters and shrink range to
1403 * [0; 5].
1404 * Then shift range to [0; 2], go up and do the same.
1405 */
1406
1407
1408 if (first & 1)
1409 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1);
1410 if (!(last & 1))
1411 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1);
1412 if (first > last)
1413 break;
1414 order++;
1415
1416 if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) {
1417 mb_clear_bits(buddy, first, last - first + 1);
1418 e4b->bd_info->bb_counters[order - 1] += last - first + 1;
1419 break;
1420 }
1421 first >>= 1;
1422 last >>= 1;
1423 buddy = buddy2;
1424 }
1425}
1426
1427static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1428 int first, int count)
1429{
1430 int left_is_free = 0;
1431 int right_is_free = 0;
1432 int block;
1433 int last = first + count - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001434 struct super_block *sb = e4b->bd_sb;
1435
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04001436 if (WARN_ON(count == 0))
1437 return;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001438 BUG_ON(last >= (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001439 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Darrick J. Wong163a2032013-08-28 17:35:51 -04001440 /* Don't bother if the block group is corrupt. */
1441 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
1442 return;
1443
Alex Tomasc9de5602008-01-29 00:19:52 -05001444 mb_check_buddy(e4b);
1445 mb_free_blocks_double(inode, e4b, first, count);
1446
1447 e4b->bd_info->bb_free += count;
1448 if (first < e4b->bd_info->bb_first_free)
1449 e4b->bd_info->bb_first_free = first;
1450
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001451 /* access memory sequentially: check left neighbour,
1452 * clear range and then check right neighbour
1453 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001454 if (first != 0)
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001455 left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap);
1456 block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count);
1457 if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0])
1458 right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap);
1459
1460 if (unlikely(block != -1)) {
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001461 struct ext4_sb_info *sbi = EXT4_SB(sb);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001462 ext4_fsblk_t blocknr;
1463
1464 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
1465 blocknr += EXT4_C2B(EXT4_SB(sb), block);
1466 ext4_grp_locked_error(sb, e4b->bd_group,
1467 inode ? inode->i_ino : 0,
1468 blocknr,
1469 "freeing already freed block "
Darrick J. Wong163a2032013-08-28 17:35:51 -04001470 "(bit %u); block bitmap corrupt.",
1471 block);
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001472 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))
1473 percpu_counter_sub(&sbi->s_freeclusters_counter,
1474 e4b->bd_info->bb_free);
Darrick J. Wong163a2032013-08-28 17:35:51 -04001475 /* Mark the block group as corrupt. */
1476 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1477 &e4b->bd_info->bb_state);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001478 mb_regenerate_buddy(e4b);
1479 goto done;
1480 }
1481
1482 /* let's maintain fragments counter */
1483 if (left_is_free && right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001484 e4b->bd_info->bb_fragments--;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001485 else if (!left_is_free && !right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001486 e4b->bd_info->bb_fragments++;
1487
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001488 /* buddy[0] == bd_bitmap is a special case, so handle
1489 * it right away and let mb_buddy_mark_free stay free of
1490 * zero order checks.
1491 * Check if neighbours are to be coaleasced,
1492 * adjust bitmap bb_counters and borders appropriately.
1493 */
1494 if (first & 1) {
1495 first += !left_is_free;
1496 e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001497 }
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001498 if (!(last & 1)) {
1499 last -= !right_is_free;
1500 e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1;
1501 }
1502
1503 if (first <= last)
1504 mb_buddy_mark_free(e4b, first >> 1, last >> 1);
1505
1506done:
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001507 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001508 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001509}
1510
Robin Dong15c006a2012-08-17 10:02:17 -04001511static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001512 int needed, struct ext4_free_extent *ex)
1513{
1514 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001515 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001516 void *buddy;
1517
Vincent Minetbc8e6742009-05-15 08:33:18 -04001518 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001519 BUG_ON(ex == NULL);
1520
Robin Dong15c006a2012-08-17 10:02:17 -04001521 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001522 BUG_ON(buddy == NULL);
1523 BUG_ON(block >= max);
1524 if (mb_test_bit(block, buddy)) {
1525 ex->fe_len = 0;
1526 ex->fe_start = 0;
1527 ex->fe_group = 0;
1528 return 0;
1529 }
1530
Robin Dong15c006a2012-08-17 10:02:17 -04001531 /* find actual order */
1532 order = mb_find_order_for_block(e4b, block);
1533 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001534
1535 ex->fe_len = 1 << order;
1536 ex->fe_start = block << order;
1537 ex->fe_group = e4b->bd_group;
1538
1539 /* calc difference from given start */
1540 next = next - ex->fe_start;
1541 ex->fe_len -= next;
1542 ex->fe_start += next;
1543
1544 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001545 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001546
1547 if (block + 1 >= max)
1548 break;
1549
1550 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001551 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001552 break;
1553
Robin Dongb051d8d2011-10-26 05:30:30 -04001554 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001555
Alex Tomasc9de5602008-01-29 00:19:52 -05001556 block = next >> order;
1557 ex->fe_len += 1 << order;
1558 }
1559
1560 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1561 return ex->fe_len;
1562}
1563
1564static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1565{
1566 int ord;
1567 int mlen = 0;
1568 int max = 0;
1569 int cur;
1570 int start = ex->fe_start;
1571 int len = ex->fe_len;
1572 unsigned ret = 0;
1573 int len0 = len;
1574 void *buddy;
1575
1576 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1577 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001578 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001579 mb_check_buddy(e4b);
1580 mb_mark_used_double(e4b, start, len);
1581
1582 e4b->bd_info->bb_free -= len;
1583 if (e4b->bd_info->bb_first_free == start)
1584 e4b->bd_info->bb_first_free += len;
1585
1586 /* let's maintain fragments counter */
1587 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001588 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001589 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001590 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001591 if (mlen && max)
1592 e4b->bd_info->bb_fragments++;
1593 else if (!mlen && !max)
1594 e4b->bd_info->bb_fragments--;
1595
1596 /* let's maintain buddy itself */
1597 while (len) {
1598 ord = mb_find_order_for_block(e4b, start);
1599
1600 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1601 /* the whole chunk may be allocated at once! */
1602 mlen = 1 << ord;
1603 buddy = mb_find_buddy(e4b, ord, &max);
1604 BUG_ON((start >> ord) >= max);
1605 mb_set_bit(start >> ord, buddy);
1606 e4b->bd_info->bb_counters[ord]--;
1607 start += mlen;
1608 len -= mlen;
1609 BUG_ON(len < 0);
1610 continue;
1611 }
1612
1613 /* store for history */
1614 if (ret == 0)
1615 ret = len | (ord << 16);
1616
1617 /* we have to split large buddy */
1618 BUG_ON(ord <= 0);
1619 buddy = mb_find_buddy(e4b, ord, &max);
1620 mb_set_bit(start >> ord, buddy);
1621 e4b->bd_info->bb_counters[ord]--;
1622
1623 ord--;
1624 cur = (start >> ord) & ~1U;
1625 buddy = mb_find_buddy(e4b, ord, &max);
1626 mb_clear_bit(cur, buddy);
1627 mb_clear_bit(cur + 1, buddy);
1628 e4b->bd_info->bb_counters[ord]++;
1629 e4b->bd_info->bb_counters[ord]++;
1630 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001631 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001632
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001633 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001634 mb_check_buddy(e4b);
1635
1636 return ret;
1637}
1638
1639/*
1640 * Must be called under group lock!
1641 */
1642static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1643 struct ext4_buddy *e4b)
1644{
1645 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1646 int ret;
1647
1648 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1649 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1650
1651 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1652 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1653 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1654
1655 /* preallocation can change ac_b_ex, thus we store actually
1656 * allocated blocks for history */
1657 ac->ac_f_ex = ac->ac_b_ex;
1658
1659 ac->ac_status = AC_STATUS_FOUND;
1660 ac->ac_tail = ret & 0xffff;
1661 ac->ac_buddy = ret >> 16;
1662
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001663 /*
1664 * take the page reference. We want the page to be pinned
1665 * so that we don't get a ext4_mb_init_cache_call for this
1666 * group until we update the bitmap. That would mean we
1667 * double allocate blocks. The reference is dropped
1668 * in ext4_mb_release_context
1669 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001670 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1671 get_page(ac->ac_bitmap_page);
1672 ac->ac_buddy_page = e4b->bd_buddy_page;
1673 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001674 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001675 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001676 spin_lock(&sbi->s_md_lock);
1677 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1678 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1679 spin_unlock(&sbi->s_md_lock);
1680 }
1681}
1682
1683/*
1684 * regular allocator, for general purposes allocation
1685 */
1686
1687static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1688 struct ext4_buddy *e4b,
1689 int finish_group)
1690{
1691 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1692 struct ext4_free_extent *bex = &ac->ac_b_ex;
1693 struct ext4_free_extent *gex = &ac->ac_g_ex;
1694 struct ext4_free_extent ex;
1695 int max;
1696
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001697 if (ac->ac_status == AC_STATUS_FOUND)
1698 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001699 /*
1700 * We don't want to scan for a whole year
1701 */
1702 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1703 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1704 ac->ac_status = AC_STATUS_BREAK;
1705 return;
1706 }
1707
1708 /*
1709 * Haven't found good chunk so far, let's continue
1710 */
1711 if (bex->fe_len < gex->fe_len)
1712 return;
1713
1714 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1715 && bex->fe_group == e4b->bd_group) {
1716 /* recheck chunk's availability - we don't know
1717 * when it was found (within this lock-unlock
1718 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001719 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001720 if (max >= gex->fe_len) {
1721 ext4_mb_use_best_found(ac, e4b);
1722 return;
1723 }
1724 }
1725}
1726
1727/*
1728 * The routine checks whether found extent is good enough. If it is,
1729 * then the extent gets marked used and flag is set to the context
1730 * to stop scanning. Otherwise, the extent is compared with the
1731 * previous found extent and if new one is better, then it's stored
1732 * in the context. Later, the best found extent will be used, if
1733 * mballoc can't find good enough extent.
1734 *
1735 * FIXME: real allocation policy is to be designed yet!
1736 */
1737static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1738 struct ext4_free_extent *ex,
1739 struct ext4_buddy *e4b)
1740{
1741 struct ext4_free_extent *bex = &ac->ac_b_ex;
1742 struct ext4_free_extent *gex = &ac->ac_g_ex;
1743
1744 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001745 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1746 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001747 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1748
1749 ac->ac_found++;
1750
1751 /*
1752 * The special case - take what you catch first
1753 */
1754 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1755 *bex = *ex;
1756 ext4_mb_use_best_found(ac, e4b);
1757 return;
1758 }
1759
1760 /*
1761 * Let's check whether the chuck is good enough
1762 */
1763 if (ex->fe_len == gex->fe_len) {
1764 *bex = *ex;
1765 ext4_mb_use_best_found(ac, e4b);
1766 return;
1767 }
1768
1769 /*
1770 * If this is first found extent, just store it in the context
1771 */
1772 if (bex->fe_len == 0) {
1773 *bex = *ex;
1774 return;
1775 }
1776
1777 /*
1778 * If new found extent is better, store it in the context
1779 */
1780 if (bex->fe_len < gex->fe_len) {
1781 /* if the request isn't satisfied, any found extent
1782 * larger than previous best one is better */
1783 if (ex->fe_len > bex->fe_len)
1784 *bex = *ex;
1785 } else if (ex->fe_len > gex->fe_len) {
1786 /* if the request is satisfied, then we try to find
1787 * an extent that still satisfy the request, but is
1788 * smaller than previous one */
1789 if (ex->fe_len < bex->fe_len)
1790 *bex = *ex;
1791 }
1792
1793 ext4_mb_check_limits(ac, e4b, 0);
1794}
1795
Eric Sandeen089ceec2009-07-05 22:17:31 -04001796static noinline_for_stack
1797int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001798 struct ext4_buddy *e4b)
1799{
1800 struct ext4_free_extent ex = ac->ac_b_ex;
1801 ext4_group_t group = ex.fe_group;
1802 int max;
1803 int err;
1804
1805 BUG_ON(ex.fe_len <= 0);
1806 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1807 if (err)
1808 return err;
1809
1810 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001811 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001812
1813 if (max > 0) {
1814 ac->ac_b_ex = ex;
1815 ext4_mb_use_best_found(ac, e4b);
1816 }
1817
1818 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001819 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001820
1821 return 0;
1822}
1823
Eric Sandeen089ceec2009-07-05 22:17:31 -04001824static noinline_for_stack
1825int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001826 struct ext4_buddy *e4b)
1827{
1828 ext4_group_t group = ac->ac_g_ex.fe_group;
1829 int max;
1830 int err;
1831 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001832 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001833 struct ext4_free_extent ex;
1834
1835 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1836 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001837 if (grp->bb_free == 0)
1838 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001839
1840 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1841 if (err)
1842 return err;
1843
Darrick J. Wong163a2032013-08-28 17:35:51 -04001844 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) {
1845 ext4_mb_unload_buddy(e4b);
1846 return 0;
1847 }
1848
Alex Tomasc9de5602008-01-29 00:19:52 -05001849 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001850 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001851 ac->ac_g_ex.fe_len, &ex);
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001852 ex.fe_logical = 0xDEADFA11; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001853
1854 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1855 ext4_fsblk_t start;
1856
Akinobu Mita5661bd62010-03-03 23:53:39 -05001857 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1858 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001859 /* use do_div to get remainder (would be 64-bit modulo) */
1860 if (do_div(start, sbi->s_stripe) == 0) {
1861 ac->ac_found++;
1862 ac->ac_b_ex = ex;
1863 ext4_mb_use_best_found(ac, e4b);
1864 }
1865 } else if (max >= ac->ac_g_ex.fe_len) {
1866 BUG_ON(ex.fe_len <= 0);
1867 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1868 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1869 ac->ac_found++;
1870 ac->ac_b_ex = ex;
1871 ext4_mb_use_best_found(ac, e4b);
1872 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1873 /* Sometimes, caller may want to merge even small
1874 * number of blocks to an existing extent */
1875 BUG_ON(ex.fe_len <= 0);
1876 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1877 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1878 ac->ac_found++;
1879 ac->ac_b_ex = ex;
1880 ext4_mb_use_best_found(ac, e4b);
1881 }
1882 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001883 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001884
1885 return 0;
1886}
1887
1888/*
1889 * The routine scans buddy structures (not bitmap!) from given order
1890 * to max order and tries to find big enough chunk to satisfy the req
1891 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001892static noinline_for_stack
1893void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001894 struct ext4_buddy *e4b)
1895{
1896 struct super_block *sb = ac->ac_sb;
1897 struct ext4_group_info *grp = e4b->bd_info;
1898 void *buddy;
1899 int i;
1900 int k;
1901 int max;
1902
1903 BUG_ON(ac->ac_2order <= 0);
1904 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1905 if (grp->bb_counters[i] == 0)
1906 continue;
1907
1908 buddy = mb_find_buddy(e4b, i, &max);
1909 BUG_ON(buddy == NULL);
1910
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001911 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001912 BUG_ON(k >= max);
1913
1914 ac->ac_found++;
1915
1916 ac->ac_b_ex.fe_len = 1 << i;
1917 ac->ac_b_ex.fe_start = k << i;
1918 ac->ac_b_ex.fe_group = e4b->bd_group;
1919
1920 ext4_mb_use_best_found(ac, e4b);
1921
1922 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1923
1924 if (EXT4_SB(sb)->s_mb_stats)
1925 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1926
1927 break;
1928 }
1929}
1930
1931/*
1932 * The routine scans the group and measures all found extents.
1933 * In order to optimize scanning, caller must pass number of
1934 * free blocks in the group, so the routine can know upper limit.
1935 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001936static noinline_for_stack
1937void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001938 struct ext4_buddy *e4b)
1939{
1940 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001941 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001942 struct ext4_free_extent ex;
1943 int i;
1944 int free;
1945
1946 free = e4b->bd_info->bb_free;
Theodore Ts'oae8bbfe2020-04-13 23:33:05 -04001947 if (WARN_ON(free <= 0))
1948 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001949
1950 i = e4b->bd_info->bb_first_free;
1951
1952 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001953 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001954 EXT4_CLUSTERS_PER_GROUP(sb), i);
1955 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001956 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001957 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001958 * free blocks even though group info says we
1959 * we have free blocks
1960 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001961 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001962 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001963 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001964 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001965 break;
1966 }
1967
Robin Dong15c006a2012-08-17 10:02:17 -04001968 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Theodore Ts'oae8bbfe2020-04-13 23:33:05 -04001969 if (WARN_ON(ex.fe_len <= 0))
1970 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001971 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001972 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001973 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001974 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001975 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001976 /*
1977 * The number of free blocks differs. This mostly
1978 * indicate that the bitmap is corrupt. So exit
1979 * without claiming the space.
1980 */
1981 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001982 }
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001983 ex.fe_logical = 0xDEADC0DE; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001984 ext4_mb_measure_extent(ac, &ex, e4b);
1985
1986 i += ex.fe_len;
1987 free -= ex.fe_len;
1988 }
1989
1990 ext4_mb_check_limits(ac, e4b, 1);
1991}
1992
1993/*
1994 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001995 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001996 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001997static noinline_for_stack
1998void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001999 struct ext4_buddy *e4b)
2000{
2001 struct super_block *sb = ac->ac_sb;
2002 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05002003 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05002004 struct ext4_free_extent ex;
2005 ext4_fsblk_t first_group_block;
2006 ext4_fsblk_t a;
2007 ext4_grpblk_t i;
2008 int max;
2009
2010 BUG_ON(sbi->s_stripe == 0);
2011
2012 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05002013 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
2014
Alex Tomasc9de5602008-01-29 00:19:52 -05002015 a = first_group_block + sbi->s_stripe - 1;
2016 do_div(a, sbi->s_stripe);
2017 i = (a * sbi->s_stripe) - first_group_block;
2018
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04002019 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002020 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04002021 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002022 if (max >= sbi->s_stripe) {
2023 ac->ac_found++;
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002024 ex.fe_logical = 0xDEADF00D; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002025 ac->ac_b_ex = ex;
2026 ext4_mb_use_best_found(ac, e4b);
2027 break;
2028 }
2029 }
2030 i += sbi->s_stripe;
2031 }
2032}
2033
Lukas Czerner42ac1842015-06-08 11:40:40 -04002034/*
2035 * This is now called BEFORE we load the buddy bitmap.
2036 * Returns either 1 or 0 indicating that the group is either suitable
2037 * for the allocation or not. In addition it can also return negative
2038 * error code when something goes wrong.
2039 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002040static int ext4_mb_good_group(struct ext4_allocation_context *ac,
2041 ext4_group_t group, int cr)
2042{
2043 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002044 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002045 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2046
2047 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002048
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002049 free = grp->bb_free;
2050 if (free == 0)
2051 return 0;
2052 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
2053 return 0;
2054
Darrick J. Wong163a2032013-08-28 17:35:51 -04002055 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
2056 return 0;
2057
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002058 /* We only do this if the grp has never been initialized */
2059 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04002060 int ret = ext4_mb_init_group(ac->ac_sb, group, GFP_NOFS);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002061 if (ret)
Lukas Czerner42ac1842015-06-08 11:40:40 -04002062 return ret;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002063 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002064
Alex Tomasc9de5602008-01-29 00:19:52 -05002065 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05002066 if (fragments == 0)
2067 return 0;
2068
2069 switch (cr) {
2070 case 0:
2071 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05002072
Theodore Ts'oa4912122009-03-12 12:18:34 -04002073 /* Avoid using the first bg of a flexgroup for data files */
2074 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
2075 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
2076 ((group % flex_size) == 0))
2077 return 0;
2078
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002079 if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) ||
2080 (free / fragments) >= ac->ac_g_ex.fe_len)
2081 return 1;
2082
2083 if (grp->bb_largest_free_order < ac->ac_2order)
2084 return 0;
2085
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002086 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002087 case 1:
2088 if ((free / fragments) >= ac->ac_g_ex.fe_len)
2089 return 1;
2090 break;
2091 case 2:
2092 if (free >= ac->ac_g_ex.fe_len)
2093 return 1;
2094 break;
2095 case 3:
2096 return 1;
2097 default:
2098 BUG();
2099 }
2100
2101 return 0;
2102}
2103
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002104static noinline_for_stack int
2105ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002106{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002107 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002108 int cr;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002109 int err = 0, first_err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002110 struct ext4_sb_info *sbi;
2111 struct super_block *sb;
2112 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002113
2114 sb = ac->ac_sb;
2115 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002116 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002117 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002118 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002119 ngroups = sbi->s_blockfile_groups;
2120
Alex Tomasc9de5602008-01-29 00:19:52 -05002121 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2122
2123 /* first, try the goal */
2124 err = ext4_mb_find_by_goal(ac, &e4b);
2125 if (err || ac->ac_status == AC_STATUS_FOUND)
2126 goto out;
2127
2128 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2129 goto out;
2130
2131 /*
2132 * ac->ac2_order is set only if the fe_len is a power of 2
2133 * if ac2_order is set we also set criteria to 0 so that we
2134 * try exact allocation using buddy.
2135 */
2136 i = fls(ac->ac_g_ex.fe_len);
2137 ac->ac_2order = 0;
2138 /*
2139 * We search using buddy data only if the order of the request
2140 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002141 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Jan Kara0e9deca2017-10-07 22:36:49 +00002142 * We also support searching for power-of-two requests only for
2143 * requests upto maximum buddy size we have constructed.
Alex Tomasc9de5602008-01-29 00:19:52 -05002144 */
Jan Kara0e9deca2017-10-07 22:36:49 +00002145 if (i >= sbi->s_mb_order2_reqs && i <= sb->s_blocksize_bits + 2) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002146 /*
2147 * This should tell if fe_len is exactly power of 2
2148 */
2149 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
Jeremy Cline51ada112018-08-02 00:03:40 -04002150 ac->ac_2order = array_index_nospec(i - 1,
2151 sb->s_blocksize_bits + 2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002152 }
2153
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002154 /* if stream allocation is enabled, use global goal */
2155 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002156 /* TBD: may be hot point */
2157 spin_lock(&sbi->s_md_lock);
2158 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2159 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2160 spin_unlock(&sbi->s_md_lock);
2161 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002162
Alex Tomasc9de5602008-01-29 00:19:52 -05002163 /* Let's just scan groups to find more-less suitable blocks */
2164 cr = ac->ac_2order ? 0 : 1;
2165 /*
2166 * cr == 0 try to get exact allocation,
2167 * cr == 3 try to get anything
2168 */
2169repeat:
2170 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2171 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002172 /*
2173 * searching for the right group start
2174 * from the goal value specified
2175 */
2176 group = ac->ac_g_ex.fe_group;
2177
Theodore Ts'o8df96752009-05-01 08:50:38 -04002178 for (i = 0; i < ngroups; group++, i++) {
Lukas Czerner42ac1842015-06-08 11:40:40 -04002179 int ret = 0;
Theodore Ts'o2ed57242013-06-12 11:43:02 -04002180 cond_resched();
Lachlan McIlroye6155732013-05-05 23:10:00 -04002181 /*
2182 * Artificially restricted ngroups for non-extent
2183 * files makes group > ngroups possible on first loop.
2184 */
2185 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002186 group = 0;
2187
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002188 /* This now checks without needing the buddy page */
Lukas Czerner42ac1842015-06-08 11:40:40 -04002189 ret = ext4_mb_good_group(ac, group, cr);
2190 if (ret <= 0) {
2191 if (!first_err)
2192 first_err = ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002193 continue;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002194 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002195
Alex Tomasc9de5602008-01-29 00:19:52 -05002196 err = ext4_mb_load_buddy(sb, group, &e4b);
2197 if (err)
2198 goto out;
2199
2200 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002201
2202 /*
2203 * We need to check again after locking the
2204 * block group
2205 */
Lukas Czerner42ac1842015-06-08 11:40:40 -04002206 ret = ext4_mb_good_group(ac, group, cr);
2207 if (ret <= 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002208 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002209 ext4_mb_unload_buddy(&e4b);
Lukas Czerner42ac1842015-06-08 11:40:40 -04002210 if (!first_err)
2211 first_err = ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002212 continue;
2213 }
2214
2215 ac->ac_groups_scanned++;
Jan Kara0e9deca2017-10-07 22:36:49 +00002216 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002217 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002218 else if (cr == 1 && sbi->s_stripe &&
2219 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002220 ext4_mb_scan_aligned(ac, &e4b);
2221 else
2222 ext4_mb_complex_scan_group(ac, &e4b);
2223
2224 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002225 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002226
2227 if (ac->ac_status != AC_STATUS_CONTINUE)
2228 break;
2229 }
2230 }
2231
2232 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2233 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2234 /*
2235 * We've been searching too long. Let's try to allocate
2236 * the best chunk we've found so far
2237 */
2238
2239 ext4_mb_try_best_found(ac, &e4b);
2240 if (ac->ac_status != AC_STATUS_FOUND) {
2241 /*
2242 * Someone more lucky has already allocated it.
2243 * The only thing we can do is just take first
2244 * found block(s)
2245 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2246 */
2247 ac->ac_b_ex.fe_group = 0;
2248 ac->ac_b_ex.fe_start = 0;
2249 ac->ac_b_ex.fe_len = 0;
2250 ac->ac_status = AC_STATUS_CONTINUE;
2251 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2252 cr = 3;
2253 atomic_inc(&sbi->s_mb_lost_chunks);
2254 goto repeat;
2255 }
2256 }
2257out:
Lukas Czerner42ac1842015-06-08 11:40:40 -04002258 if (!err && ac->ac_status != AC_STATUS_FOUND && first_err)
2259 err = first_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002260 return err;
2261}
2262
Alex Tomasc9de5602008-01-29 00:19:52 -05002263static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2264{
2265 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002266 ext4_group_t group;
2267
Theodore Ts'o8df96752009-05-01 08:50:38 -04002268 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002269 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002270 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002271 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002272}
2273
2274static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2275{
2276 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002277 ext4_group_t group;
2278
2279 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002280 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002281 return NULL;
2282 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002283 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002284}
2285
2286static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2287{
2288 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002289 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002290 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002291 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002292 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002293 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -05002294 struct sg {
2295 struct ext4_group_info info;
Chandan Rajendrab493c712016-11-14 21:26:26 -05002296 ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2];
Alex Tomasc9de5602008-01-29 00:19:52 -05002297 } sg;
2298
2299 group--;
2300 if (group == 0)
Rasmus Villemoes97b4af22015-06-15 00:32:58 -04002301 seq_puts(seq, "#group: free frags first ["
2302 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
Huaitong Han802cf1f2016-02-12 00:17:16 -05002303 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002304
2305 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2306 sizeof(struct ext4_group_info);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002307 grinfo = ext4_get_group_info(sb, group);
2308 /* Load the group info in memory only if not already loaded. */
2309 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2310 err = ext4_mb_load_buddy(sb, group, &e4b);
2311 if (err) {
2312 seq_printf(seq, "#%-5u: I/O error\n", group);
2313 return 0;
2314 }
2315 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002316 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002317
Alex Tomasc9de5602008-01-29 00:19:52 -05002318 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002319
2320 if (buddy_loaded)
2321 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002322
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002323 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002324 sg.info.bb_fragments, sg.info.bb_first_free);
2325 for (i = 0; i <= 13; i++)
2326 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2327 sg.info.bb_counters[i] : 0);
2328 seq_printf(seq, " ]\n");
2329
2330 return 0;
2331}
2332
2333static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2334{
2335}
2336
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002337static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002338 .start = ext4_mb_seq_groups_start,
2339 .next = ext4_mb_seq_groups_next,
2340 .stop = ext4_mb_seq_groups_stop,
2341 .show = ext4_mb_seq_groups_show,
2342};
2343
2344static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2345{
Al Virod9dda782013-03-31 18:16:14 -04002346 struct super_block *sb = PDE_DATA(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05002347 int rc;
2348
2349 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2350 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002351 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002352 m->private = sb;
2353 }
2354 return rc;
2355
2356}
2357
Theodore Ts'oebd173b2015-09-23 12:46:17 -04002358const struct file_operations ext4_seq_mb_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002359 .open = ext4_mb_seq_groups_open,
2360 .read = seq_read,
2361 .llseek = seq_lseek,
2362 .release = seq_release,
2363};
2364
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002365static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2366{
2367 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2368 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2369
2370 BUG_ON(!cachep);
2371 return cachep;
2372}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002373
Theodore Ts'o28623c22012-09-05 01:31:50 -04002374/*
2375 * Allocate the top-level s_group_info array for the specified number
2376 * of groups
2377 */
2378int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2379{
2380 struct ext4_sb_info *sbi = EXT4_SB(sb);
2381 unsigned size;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002382 struct ext4_group_info ***old_groupinfo, ***new_groupinfo;
Theodore Ts'o28623c22012-09-05 01:31:50 -04002383
2384 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2385 EXT4_DESC_PER_BLOCK_BITS(sb);
2386 if (size <= sbi->s_group_info_size)
2387 return 0;
2388
2389 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
2390 new_groupinfo = ext4_kvzalloc(size, GFP_KERNEL);
2391 if (!new_groupinfo) {
2392 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2393 return -ENOMEM;
2394 }
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002395 rcu_read_lock();
2396 old_groupinfo = rcu_dereference(sbi->s_group_info);
2397 if (old_groupinfo)
2398 memcpy(new_groupinfo, old_groupinfo,
Theodore Ts'o28623c22012-09-05 01:31:50 -04002399 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002400 rcu_read_unlock();
2401 rcu_assign_pointer(sbi->s_group_info, new_groupinfo);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002402 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002403 if (old_groupinfo)
2404 ext4_kvfree_array_rcu(old_groupinfo);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002405 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2406 sbi->s_group_info_size);
2407 return 0;
2408}
2409
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002410/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002411int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002412 struct ext4_group_desc *desc)
2413{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002414 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002415 int metalen = 0;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002416 int idx = group >> EXT4_DESC_PER_BLOCK_BITS(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002417 struct ext4_sb_info *sbi = EXT4_SB(sb);
2418 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002419 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002420
2421 /*
2422 * First check if this group is the first of a reserved block.
2423 * If it's true, we have to allocate a new table of pointers
2424 * to ext4_group_info structures
2425 */
2426 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2427 metalen = sizeof(*meta_group_info) <<
2428 EXT4_DESC_PER_BLOCK_BITS(sb);
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002429 meta_group_info = kmalloc(metalen, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002430 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002431 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002432 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002433 goto exit_meta_group_info;
2434 }
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002435 rcu_read_lock();
2436 rcu_dereference(sbi->s_group_info)[idx] = meta_group_info;
2437 rcu_read_unlock();
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002438 }
2439
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002440 meta_group_info = sbi_array_rcu_deref(sbi, s_group_info, idx);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002441 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2442
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002443 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002444 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002445 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002446 goto exit_group_info;
2447 }
2448 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2449 &(meta_group_info[i]->bb_state));
2450
2451 /*
2452 * initialize bb_free to be able to skip
2453 * empty groups without initialization
2454 */
Theodore Ts'o5ae57322018-06-14 00:58:00 -04002455 if (ext4_has_group_desc_csum(sb) &&
2456 (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002457 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd2011-09-09 19:12:51 -04002458 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002459 } else {
2460 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002461 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002462 }
2463
2464 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002465 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002466 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002467 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002468
2469#ifdef DOUBLE_CHECK
2470 {
2471 struct buffer_head *bh;
2472 meta_group_info[i]->bb_bitmap =
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002473 kmalloc(sb->s_blocksize, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002474 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2475 bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002476 BUG_ON(IS_ERR_OR_NULL(bh));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002477 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2478 sb->s_blocksize);
2479 put_bh(bh);
2480 }
2481#endif
2482
2483 return 0;
2484
2485exit_group_info:
2486 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002487 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002488 struct ext4_group_info ***group_info;
2489
2490 rcu_read_lock();
2491 group_info = rcu_dereference(sbi->s_group_info);
2492 kfree(group_info[idx]);
2493 group_info[idx] = NULL;
2494 rcu_read_unlock();
Tao Macaaf7a22011-07-11 18:42:42 -04002495 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002496exit_meta_group_info:
2497 return -ENOMEM;
2498} /* ext4_mb_add_groupinfo */
2499
Alex Tomasc9de5602008-01-29 00:19:52 -05002500static int ext4_mb_init_backend(struct super_block *sb)
2501{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002502 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002503 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002504 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002505 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002506 struct ext4_group_desc *desc;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002507 struct ext4_group_info ***group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002508 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002509
Theodore Ts'o28623c22012-09-05 01:31:50 -04002510 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2511 if (err)
2512 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002513
Alex Tomasc9de5602008-01-29 00:19:52 -05002514 sbi->s_buddy_cache = new_inode(sb);
2515 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002516 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002517 goto err_freesgi;
2518 }
Yu Jian48e60612011-08-01 17:41:39 -04002519 /* To avoid potentially colliding with an valid on-disk inode number,
2520 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2521 * not in the inode hash, so it should never be found by iget(), but
2522 * this will avoid confusion if it ever shows up during debugging. */
2523 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002524 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002525 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002526 desc = ext4_get_group_desc(sb, i, NULL);
2527 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002528 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002529 goto err_freebuddy;
2530 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002531 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2532 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002533 }
2534
2535 return 0;
2536
2537err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002538 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002539 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002540 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002541 i = sbi->s_group_info_size;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002542 rcu_read_lock();
2543 group_info = rcu_dereference(sbi->s_group_info);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002544 while (i-- > 0)
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002545 kfree(group_info[i]);
2546 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002547 iput(sbi->s_buddy_cache);
2548err_freesgi:
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002549 rcu_read_lock();
2550 kvfree(rcu_dereference(sbi->s_group_info));
2551 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002552 return -ENOMEM;
2553}
2554
Eric Sandeen2892c152011-02-12 08:12:18 -05002555static void ext4_groupinfo_destroy_slabs(void)
2556{
2557 int i;
2558
2559 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2560 if (ext4_groupinfo_caches[i])
2561 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2562 ext4_groupinfo_caches[i] = NULL;
2563 }
2564}
2565
2566static int ext4_groupinfo_create_slab(size_t size)
2567{
2568 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2569 int slab_size;
2570 int blocksize_bits = order_base_2(size);
2571 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2572 struct kmem_cache *cachep;
2573
2574 if (cache_index >= NR_GRPINFO_CACHES)
2575 return -EINVAL;
2576
2577 if (unlikely(cache_index < 0))
2578 cache_index = 0;
2579
2580 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2581 if (ext4_groupinfo_caches[cache_index]) {
2582 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2583 return 0; /* Already created */
2584 }
2585
2586 slab_size = offsetof(struct ext4_group_info,
2587 bb_counters[blocksize_bits + 2]);
2588
2589 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2590 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2591 NULL);
2592
Tao Ma823ba012011-07-11 18:26:01 -04002593 ext4_groupinfo_caches[cache_index] = cachep;
2594
Eric Sandeen2892c152011-02-12 08:12:18 -05002595 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2596 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002597 printk(KERN_EMERG
2598 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002599 return -ENOMEM;
2600 }
2601
Eric Sandeen2892c152011-02-12 08:12:18 -05002602 return 0;
2603}
2604
Akira Fujita9d990122012-05-28 14:19:25 -04002605int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002606{
2607 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002608 unsigned i, j;
Nicolai Stange935244c2016-05-05 19:46:19 -04002609 unsigned offset, offset_incr;
Alex Tomasc9de5602008-01-29 00:19:52 -05002610 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002611 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002612
Eric Sandeen19278052009-08-25 22:36:25 -04002613 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002614
2615 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2616 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002617 ret = -ENOMEM;
2618 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002619 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002620
Eric Sandeen19278052009-08-25 22:36:25 -04002621 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002622 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2623 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002624 ret = -ENOMEM;
2625 goto out;
2626 }
2627
Eric Sandeen2892c152011-02-12 08:12:18 -05002628 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2629 if (ret < 0)
2630 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002631
2632 /* order 0 is regular bitmap */
2633 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2634 sbi->s_mb_offsets[0] = 0;
2635
2636 i = 1;
2637 offset = 0;
Nicolai Stange935244c2016-05-05 19:46:19 -04002638 offset_incr = 1 << (sb->s_blocksize_bits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05002639 max = sb->s_blocksize << 2;
2640 do {
2641 sbi->s_mb_offsets[i] = offset;
2642 sbi->s_mb_maxs[i] = max;
Nicolai Stange935244c2016-05-05 19:46:19 -04002643 offset += offset_incr;
2644 offset_incr = offset_incr >> 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002645 max = max >> 1;
2646 i++;
2647 } while (i <= sb->s_blocksize_bits + 1);
2648
Alex Tomasc9de5602008-01-29 00:19:52 -05002649 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002650 spin_lock_init(&sbi->s_bal_lock);
Theodore Ts'od08854f2016-06-26 18:24:01 -04002651 sbi->s_mb_free_pending = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002652
2653 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2654 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2655 sbi->s_mb_stats = MB_DEFAULT_STATS;
2656 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2657 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002658 /*
2659 * The default group preallocation is 512, which for 4k block
2660 * sizes translates to 2 megabytes. However for bigalloc file
2661 * systems, this is probably too big (i.e, if the cluster size
2662 * is 1 megabyte, then group preallocation size becomes half a
2663 * gigabyte!). As a default, we will keep a two megabyte
2664 * group pralloc size for cluster sizes up to 64k, and after
2665 * that, we will force a minimum group preallocation size of
2666 * 32 clusters. This translates to 8 megs when the cluster
2667 * size is 256k, and 32 megs when the cluster size is 1 meg,
2668 * which seems reasonable as a default.
2669 */
2670 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2671 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002672 /*
2673 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2674 * to the lowest multiple of s_stripe which is bigger than
2675 * the s_mb_group_prealloc as determined above. We want
2676 * the preallocation size to be an exact multiple of the
2677 * RAID stripe size so that preallocations don't fragment
2678 * the stripes.
2679 */
2680 if (sbi->s_stripe > 1) {
2681 sbi->s_mb_group_prealloc = roundup(
2682 sbi->s_mb_group_prealloc, sbi->s_stripe);
2683 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002684
Eric Sandeen730c2132008-09-13 15:23:29 -04002685 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002686 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002687 ret = -ENOMEM;
Andrey Tsyvarev029b10c2014-05-12 12:34:21 -04002688 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002689 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002690 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002691 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002692 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002693 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002694 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2695 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002696 spin_lock_init(&lg->lg_prealloc_lock);
2697 }
2698
Yu Jian79a77c52011-08-01 17:41:46 -04002699 /* init file for buddy data */
2700 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002701 if (ret != 0)
2702 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002703
Tao Ma7aa0bae2011-10-06 10:22:28 -04002704 return 0;
2705
2706out_free_locality_groups:
2707 free_percpu(sbi->s_locality_groups);
2708 sbi->s_locality_groups = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002709out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002710 kfree(sbi->s_mb_offsets);
2711 sbi->s_mb_offsets = NULL;
2712 kfree(sbi->s_mb_maxs);
2713 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002714 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002715}
2716
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002717/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002718static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2719{
2720 struct ext4_prealloc_space *pa;
2721 struct list_head *cur, *tmp;
2722 int count = 0;
2723
2724 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2725 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2726 list_del(&pa->pa_group_list);
2727 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002728 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002729 }
2730 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002731 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002732
2733}
2734
2735int ext4_mb_release(struct super_block *sb)
2736{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002737 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002738 ext4_group_t i;
2739 int num_meta_group_infos;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002740 struct ext4_group_info *grinfo, ***group_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002741 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002742 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002743
Alex Tomasc9de5602008-01-29 00:19:52 -05002744 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002745 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002746 grinfo = ext4_get_group_info(sb, i);
2747#ifdef DOUBLE_CHECK
2748 kfree(grinfo->bb_bitmap);
2749#endif
2750 ext4_lock_group(sb, i);
2751 ext4_mb_cleanup_pa(grinfo);
2752 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002753 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002754 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002755 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002756 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2757 EXT4_DESC_PER_BLOCK_BITS(sb);
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002758 rcu_read_lock();
2759 group_info = rcu_dereference(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002760 for (i = 0; i < num_meta_group_infos; i++)
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002761 kfree(group_info[i]);
2762 kvfree(group_info);
2763 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002764 }
2765 kfree(sbi->s_mb_offsets);
2766 kfree(sbi->s_mb_maxs);
Markus Elfringbfcba2d2014-11-25 20:01:37 -05002767 iput(sbi->s_buddy_cache);
Alex Tomasc9de5602008-01-29 00:19:52 -05002768 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002769 ext4_msg(sb, KERN_INFO,
2770 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002771 atomic_read(&sbi->s_bal_allocated),
2772 atomic_read(&sbi->s_bal_reqs),
2773 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002774 ext4_msg(sb, KERN_INFO,
2775 "mballoc: %u extents scanned, %u goal hits, "
2776 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002777 atomic_read(&sbi->s_bal_ex_scanned),
2778 atomic_read(&sbi->s_bal_goals),
2779 atomic_read(&sbi->s_bal_2orders),
2780 atomic_read(&sbi->s_bal_breaks),
2781 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002782 ext4_msg(sb, KERN_INFO,
2783 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002784 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002785 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002786 ext4_msg(sb, KERN_INFO,
2787 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002788 atomic_read(&sbi->s_mb_preallocated),
2789 atomic_read(&sbi->s_mb_discarded));
2790 }
2791
Eric Sandeen730c2132008-09-13 15:23:29 -04002792 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002793
2794 return 0;
2795}
2796
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002797static inline int ext4_issue_discard(struct super_block *sb,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07002798 ext4_group_t block_group, ext4_grpblk_t cluster, int count,
2799 unsigned long flags)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002800{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002801 ext4_fsblk_t discard_block;
2802
Theodore Ts'o84130192011-09-09 18:50:51 -04002803 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2804 ext4_group_first_block_no(sb, block_group));
2805 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002806 trace_ext4_discard_blocks(sb,
2807 (unsigned long long) discard_block, count);
JP Abgrallcc14b9e2014-07-23 16:55:07 -07002808 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, flags);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002809}
2810
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002811/*
2812 * This function is called by the jbd2 layer once the commit has finished,
2813 * so we know we can free the blocks that were released with that commit.
2814 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002815static void ext4_free_data_callback(struct super_block *sb,
2816 struct ext4_journal_cb_entry *jce,
2817 int rc)
Alex Tomasc9de5602008-01-29 00:19:52 -05002818{
Bobi Jam18aadd42012-02-20 17:53:02 -05002819 struct ext4_free_data *entry = (struct ext4_free_data *)jce;
Alex Tomasc9de5602008-01-29 00:19:52 -05002820 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002821 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002822 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002823
Bobi Jam18aadd42012-02-20 17:53:02 -05002824 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2825 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002826
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002827 if (test_opt(sb, DISCARD)) {
2828 err = ext4_issue_discard(sb, entry->efd_group,
2829 entry->efd_start_cluster,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07002830 entry->efd_count, 0);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002831 if (err && err != -EOPNOTSUPP)
2832 ext4_msg(sb, KERN_WARNING, "discard request in"
2833 " group:%d block:%d count:%d failed"
2834 " with %d", entry->efd_group,
2835 entry->efd_start_cluster,
2836 entry->efd_count, err);
2837 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002838
Bobi Jam18aadd42012-02-20 17:53:02 -05002839 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2840 /* we expect to find existing buddy because it's pinned */
2841 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002842
Theodore Ts'od08854f2016-06-26 18:24:01 -04002843 spin_lock(&EXT4_SB(sb)->s_md_lock);
2844 EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
2845 spin_unlock(&EXT4_SB(sb)->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002846
Bobi Jam18aadd42012-02-20 17:53:02 -05002847 db = e4b.bd_info;
2848 /* there are blocks to put in buddy to make them really free */
2849 count += entry->efd_count;
2850 count2++;
2851 ext4_lock_group(sb, entry->efd_group);
2852 /* Take it out of per group rb tree */
2853 rb_erase(&entry->efd_node, &(db->bb_free_root));
2854 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002855
Bobi Jam18aadd42012-02-20 17:53:02 -05002856 /*
2857 * Clear the trimmed flag for the group so that the next
2858 * ext4_trim_fs can trim it.
2859 * If the volume is mounted with -o discard, online discard
2860 * is supported and the free blocks will be trimmed online.
2861 */
2862 if (!test_opt(sb, DISCARD))
2863 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2864
2865 if (!db->bb_free_root.rb_node) {
2866 /* No more items in the per group rb tree
2867 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002868 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002869 put_page(e4b.bd_buddy_page);
2870 put_page(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002871 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002872 ext4_unlock_group(sb, entry->efd_group);
2873 kmem_cache_free(ext4_free_data_cachep, entry);
2874 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002875
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002876 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002877}
2878
Theodore Ts'o5dabfc782010-10-27 21:30:14 -04002879int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002880{
Theodore Ts'o16828082010-10-27 21:30:09 -04002881 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2882 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002883 if (ext4_pspace_cachep == NULL)
2884 return -ENOMEM;
2885
Theodore Ts'o16828082010-10-27 21:30:09 -04002886 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2887 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002888 if (ext4_ac_cachep == NULL) {
2889 kmem_cache_destroy(ext4_pspace_cachep);
2890 return -ENOMEM;
2891 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002892
Bobi Jam18aadd42012-02-20 17:53:02 -05002893 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2894 SLAB_RECLAIM_ACCOUNT);
2895 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002896 kmem_cache_destroy(ext4_pspace_cachep);
2897 kmem_cache_destroy(ext4_ac_cachep);
2898 return -ENOMEM;
2899 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002900 return 0;
2901}
2902
Theodore Ts'o5dabfc782010-10-27 21:30:14 -04002903void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002904{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002905 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002906 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2907 * before destroying the slab cache.
2908 */
2909 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002910 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002911 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002912 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002913 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002914}
2915
2916
2917/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002918 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002919 * Returns 0 if success or error code
2920 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002921static noinline_for_stack int
2922ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002923 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002924{
2925 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002926 struct ext4_group_desc *gdp;
2927 struct buffer_head *gdp_bh;
2928 struct ext4_sb_info *sbi;
2929 struct super_block *sb;
2930 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002931 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002932
2933 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2934 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2935
2936 sb = ac->ac_sb;
2937 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002938
Theodore Ts'o574ca172008-07-11 19:27:31 -04002939 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002940 if (IS_ERR(bitmap_bh)) {
2941 err = PTR_ERR(bitmap_bh);
2942 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002943 goto out_err;
Darrick J. Wong9008a582015-10-17 21:33:24 -04002944 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002945
liang xie5d601252014-05-12 22:06:43 -04002946 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002947 err = ext4_journal_get_write_access(handle, bitmap_bh);
2948 if (err)
2949 goto out_err;
2950
2951 err = -EIO;
2952 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2953 if (!gdp)
2954 goto out_err;
2955
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002956 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002957 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002958
liang xie5d601252014-05-12 22:06:43 -04002959 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002960 err = ext4_journal_get_write_access(handle, gdp_bh);
2961 if (err)
2962 goto out_err;
2963
Akinobu Mitabda00de2010-03-03 23:53:25 -05002964 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002965
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002966 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Jan Kara58ef3832021-03-17 17:44:14 +01002967 if (!ext4_inode_block_valid(ac->ac_inode, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002968 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002969 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002970 /* File system mounted not to panic on error
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002971 * Fix the bitmap and return EFSCORRUPTED
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002972 * We leak some of the blocks here.
2973 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002974 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002975 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2976 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002977 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002978 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002979 if (!err)
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002980 err = -EFSCORRUPTED;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002981 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002982 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002983
2984 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002985#ifdef AGGRESSIVE_CHECK
2986 {
2987 int i;
2988 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2989 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2990 bitmap_bh->b_data));
2991 }
2992 }
2993#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002994 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2995 ac->ac_b_ex.fe_len);
Theodore Ts'o5ae57322018-06-14 00:58:00 -04002996 if (ext4_has_group_desc_csum(sb) &&
2997 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002998 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002999 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd2011-09-09 19:12:51 -04003000 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003001 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05003002 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003003 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
3004 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04003005 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003006 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003007
3008 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04003009 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003010 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003011 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003012 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003013 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3014 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04003015 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
3016 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05003017
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003018 if (sbi->s_log_groups_per_flex) {
3019 ext4_group_t flex_group = ext4_flex_group(sbi,
3020 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04003021 atomic64_sub(ac->ac_b_ex.fe_len,
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08003022 &sbi_array_rcu_deref(sbi, s_flex_groups,
3023 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003024 }
3025
Frank Mayhar03901312009-01-07 00:06:22 -05003026 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003027 if (err)
3028 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003029 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003030
3031out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003032 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003033 return err;
3034}
3035
3036/*
3037 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003038 * Group request are normalized to s_mb_group_prealloc, which goes to
3039 * s_strip if we set the same via mount option.
3040 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003041 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003042 *
3043 * XXX: should we try to preallocate more than the group has now?
3044 */
3045static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3046{
3047 struct super_block *sb = ac->ac_sb;
3048 struct ext4_locality_group *lg = ac->ac_lg;
3049
3050 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003051 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003052 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003053 current->pid, ac->ac_g_ex.fe_len);
3054}
3055
3056/*
3057 * Normalization means making request better in terms of
3058 * size and alignment
3059 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003060static noinline_for_stack void
3061ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003062 struct ext4_allocation_request *ar)
3063{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003064 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003065 int bsbits, max;
3066 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003067 loff_t size, start_off;
3068 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003069 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003070 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003071 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003072
3073 /* do normalize only data requests, metadata requests
3074 do not need preallocation */
3075 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3076 return;
3077
3078 /* sometime caller may want exact blocks */
3079 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3080 return;
3081
3082 /* caller may indicate that preallocation isn't
3083 * required (it's a tail, for example) */
3084 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3085 return;
3086
3087 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3088 ext4_mb_normalize_group_request(ac);
3089 return ;
3090 }
3091
3092 bsbits = ac->ac_sb->s_blocksize_bits;
3093
3094 /* first, let's learn actual file size
3095 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003096 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003097 size = size << bsbits;
3098 if (size < i_size_read(ac->ac_inode))
3099 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003100 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003101
Valerie Clement19304792008-05-13 19:31:14 -04003102 /* max size of free chunks */
3103 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003104
Valerie Clement19304792008-05-13 19:31:14 -04003105#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3106 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003107
3108 /* first, try to predict filesize */
3109 /* XXX: should this table be tunable? */
3110 start_off = 0;
3111 if (size <= 16 * 1024) {
3112 size = 16 * 1024;
3113 } else if (size <= 32 * 1024) {
3114 size = 32 * 1024;
3115 } else if (size <= 64 * 1024) {
3116 size = 64 * 1024;
3117 } else if (size <= 128 * 1024) {
3118 size = 128 * 1024;
3119 } else if (size <= 256 * 1024) {
3120 size = 256 * 1024;
3121 } else if (size <= 512 * 1024) {
3122 size = 512 * 1024;
3123 } else if (size <= 1024 * 1024) {
3124 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003125 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003126 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003127 (21 - bsbits)) << 21;
3128 size = 2 * 1024 * 1024;
3129 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003130 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3131 (22 - bsbits)) << 22;
3132 size = 4 * 1024 * 1024;
3133 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003134 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003135 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3136 (23 - bsbits)) << 23;
3137 size = 8 * 1024 * 1024;
3138 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003139 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3140 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3141 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003142 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003143 size = size >> bsbits;
3144 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003145
Baokun Li6880fb22022-05-28 19:00:15 +08003146 /*
3147 * For tiny groups (smaller than 8MB) the chosen allocation
3148 * alignment may be larger than group size. Make sure the
3149 * alignment does not move allocation to a different group which
3150 * makes mballoc fail assertions later.
3151 */
3152 start = max(start, rounddown(ac->ac_o_ex.fe_logical,
3153 (ext4_lblk_t)EXT4_BLOCKS_PER_GROUP(ac->ac_sb)));
3154
Alex Tomasc9de5602008-01-29 00:19:52 -05003155 /* don't cover already allocated blocks in selected range */
3156 if (ar->pleft && start <= ar->lleft) {
3157 size -= ar->lleft + 1 - start;
3158 start = ar->lleft + 1;
3159 }
3160 if (ar->pright && start + size - 1 >= ar->lright)
3161 size -= start + size - ar->lright;
3162
Jan Karafc6c2da2017-01-27 14:34:30 -05003163 /*
3164 * Trim allocation request for filesystems with artificially small
3165 * groups.
3166 */
3167 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
3168 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
3169
Alex Tomasc9de5602008-01-29 00:19:52 -05003170 end = start + size;
3171
3172 /* check we don't cross already preallocated blocks */
3173 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003174 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003175 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003176
Alex Tomasc9de5602008-01-29 00:19:52 -05003177 if (pa->pa_deleted)
3178 continue;
3179 spin_lock(&pa->pa_lock);
3180 if (pa->pa_deleted) {
3181 spin_unlock(&pa->pa_lock);
3182 continue;
3183 }
3184
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003185 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3186 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003187
3188 /* PA must not overlap original request */
3189 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3190 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3191
Eric Sandeen38877f42009-08-17 23:55:24 -04003192 /* skip PAs this normalized request doesn't overlap with */
3193 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003194 spin_unlock(&pa->pa_lock);
3195 continue;
3196 }
3197 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3198
Eric Sandeen38877f42009-08-17 23:55:24 -04003199 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003200 if (pa_end <= ac->ac_o_ex.fe_logical) {
3201 BUG_ON(pa_end < start);
3202 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003203 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003204 BUG_ON(pa->pa_lstart > end);
3205 end = pa->pa_lstart;
3206 }
3207 spin_unlock(&pa->pa_lock);
3208 }
3209 rcu_read_unlock();
3210 size = end - start;
3211
3212 /* XXX: extra loop to check we really don't overlap preallocations */
3213 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003214 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003215 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003216
Alex Tomasc9de5602008-01-29 00:19:52 -05003217 spin_lock(&pa->pa_lock);
3218 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003219 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3220 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003221 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3222 }
3223 spin_unlock(&pa->pa_lock);
3224 }
3225 rcu_read_unlock();
3226
3227 if (start + size <= ac->ac_o_ex.fe_logical &&
3228 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003229 ext4_msg(ac->ac_sb, KERN_ERR,
3230 "start %lu, size %lu, fe_logical %lu",
3231 (unsigned long) start, (unsigned long) size,
3232 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003233 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003234 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003235 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003236
3237 /* now prepare goal request */
3238
3239 /* XXX: is it better to align blocks WRT to logical
3240 * placement or satisfy big request as is */
3241 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003242 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003243
3244 /* define goal start in order to merge */
3245 if (ar->pright && (ar->lright == (start + size))) {
3246 /* merge to the right */
3247 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3248 &ac->ac_f_ex.fe_group,
3249 &ac->ac_f_ex.fe_start);
3250 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3251 }
3252 if (ar->pleft && (ar->lleft + 1 == start)) {
3253 /* merge to the left */
3254 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
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
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003260 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003261 (unsigned) orig_size, (unsigned) start);
3262}
3263
3264static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3265{
3266 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3267
3268 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3269 atomic_inc(&sbi->s_bal_reqs);
3270 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003271 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003272 atomic_inc(&sbi->s_bal_success);
3273 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3274 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3275 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3276 atomic_inc(&sbi->s_bal_goals);
3277 if (ac->ac_found > sbi->s_mb_max_to_scan)
3278 atomic_inc(&sbi->s_bal_breaks);
3279 }
3280
Theodore Ts'o296c3552009-09-30 00:32:42 -04003281 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3282 trace_ext4_mballoc_alloc(ac);
3283 else
3284 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003285}
3286
3287/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003288 * Called on failure; free up any blocks from the inode PA for this
3289 * context. We don't need this for MB_GROUP_PA because we only change
3290 * pa_free in ext4_mb_release_context(), but on failure, we've already
3291 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3292 */
3293static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3294{
3295 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003296 struct ext4_buddy e4b;
3297 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003298
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003299 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003300 if (ac->ac_f_ex.fe_len == 0)
3301 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003302 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3303 if (err) {
3304 /*
3305 * This should never happen since we pin the
3306 * pages in the ext4_allocation_context so
3307 * ext4_mb_load_buddy() should never fail.
3308 */
3309 WARN(1, "mb_load_buddy failed (%d)", err);
3310 return;
3311 }
3312 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3313 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3314 ac->ac_f_ex.fe_len);
3315 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003316 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003317 return;
3318 }
3319 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003320 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003321}
3322
3323/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003324 * use blocks preallocated to inode
3325 */
3326static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3327 struct ext4_prealloc_space *pa)
3328{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003329 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003330 ext4_fsblk_t start;
3331 ext4_fsblk_t end;
3332 int len;
3333
3334 /* found preallocated blocks, use them */
3335 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003336 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3337 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3338 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003339 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3340 &ac->ac_b_ex.fe_start);
3341 ac->ac_b_ex.fe_len = len;
3342 ac->ac_status = AC_STATUS_FOUND;
3343 ac->ac_pa = pa;
3344
3345 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003346 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003347 BUG_ON(pa->pa_free < len);
3348 pa->pa_free -= len;
3349
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003350 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003351}
3352
3353/*
3354 * use blocks preallocated to locality group
3355 */
3356static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3357 struct ext4_prealloc_space *pa)
3358{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003359 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003360
Alex Tomasc9de5602008-01-29 00:19:52 -05003361 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3362 &ac->ac_b_ex.fe_group,
3363 &ac->ac_b_ex.fe_start);
3364 ac->ac_b_ex.fe_len = len;
3365 ac->ac_status = AC_STATUS_FOUND;
3366 ac->ac_pa = pa;
3367
3368 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003369 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003370 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003371 * in on-disk bitmap -- see ext4_mb_release_context()
3372 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003373 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003374 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003375}
3376
3377/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003378 * Return the prealloc space that have minimal distance
3379 * from the goal block. @cpa is the prealloc
3380 * space that is having currently known minimal distance
3381 * from the goal block.
3382 */
3383static struct ext4_prealloc_space *
3384ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3385 struct ext4_prealloc_space *pa,
3386 struct ext4_prealloc_space *cpa)
3387{
3388 ext4_fsblk_t cur_distance, new_distance;
3389
3390 if (cpa == NULL) {
3391 atomic_inc(&pa->pa_count);
3392 return pa;
3393 }
Andrew Morton79211c82015-11-09 14:58:13 -08003394 cur_distance = abs(goal_block - cpa->pa_pstart);
3395 new_distance = abs(goal_block - pa->pa_pstart);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003396
Coly Li5a54b2f2011-02-24 14:10:05 -05003397 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003398 return cpa;
3399
3400 /* drop the previous reference */
3401 atomic_dec(&cpa->pa_count);
3402 atomic_inc(&pa->pa_count);
3403 return pa;
3404}
3405
3406/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003407 * search goal blocks in preallocated space
3408 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003409static noinline_for_stack int
3410ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003411{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003412 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003413 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003414 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3415 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003416 struct ext4_prealloc_space *pa, *cpa = NULL;
3417 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003418
3419 /* only data can be preallocated */
3420 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3421 return 0;
3422
3423 /* first, try per-file preallocation */
3424 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003425 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003426
3427 /* all fields in this condition don't change,
3428 * so we can skip locking for them */
3429 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003430 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3431 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003432 continue;
3433
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003434 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003435 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003436 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3437 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003438 continue;
3439
Alex Tomasc9de5602008-01-29 00:19:52 -05003440 /* found preallocated blocks, use them */
3441 spin_lock(&pa->pa_lock);
3442 if (pa->pa_deleted == 0 && pa->pa_free) {
3443 atomic_inc(&pa->pa_count);
3444 ext4_mb_use_inode_pa(ac, pa);
3445 spin_unlock(&pa->pa_lock);
3446 ac->ac_criteria = 10;
3447 rcu_read_unlock();
3448 return 1;
3449 }
3450 spin_unlock(&pa->pa_lock);
3451 }
3452 rcu_read_unlock();
3453
3454 /* can we use group allocation? */
3455 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3456 return 0;
3457
3458 /* inode may have no locality group for some reason */
3459 lg = ac->ac_lg;
3460 if (lg == NULL)
3461 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003462 order = fls(ac->ac_o_ex.fe_len) - 1;
3463 if (order > PREALLOC_TB_SIZE - 1)
3464 /* The max size of hash table is PREALLOC_TB_SIZE */
3465 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003466
Akinobu Mitabda00de2010-03-03 23:53:25 -05003467 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003468 /*
3469 * search for the prealloc space that is having
3470 * minimal distance from the goal block.
3471 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003472 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3473 rcu_read_lock();
3474 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3475 pa_inode_list) {
3476 spin_lock(&pa->pa_lock);
3477 if (pa->pa_deleted == 0 &&
3478 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003479
3480 cpa = ext4_mb_check_group_pa(goal_block,
3481 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003482 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003483 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003484 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003485 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003486 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003487 if (cpa) {
3488 ext4_mb_use_group_pa(ac, cpa);
3489 ac->ac_criteria = 20;
3490 return 1;
3491 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003492 return 0;
3493}
3494
3495/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003496 * the function goes through all block freed in the group
3497 * but not yet committed and marks them used in in-core bitmap.
3498 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003499 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003500 */
3501static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3502 ext4_group_t group)
3503{
3504 struct rb_node *n;
3505 struct ext4_group_info *grp;
3506 struct ext4_free_data *entry;
3507
3508 grp = ext4_get_group_info(sb, group);
3509 n = rb_first(&(grp->bb_free_root));
3510
3511 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003512 entry = rb_entry(n, struct ext4_free_data, efd_node);
3513 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003514 n = rb_next(n);
3515 }
3516 return;
3517}
3518
3519/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003520 * the function goes through all preallocation in this group and marks them
3521 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003522 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003523 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003524static noinline_for_stack
3525void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003526 ext4_group_t group)
3527{
3528 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3529 struct ext4_prealloc_space *pa;
3530 struct list_head *cur;
3531 ext4_group_t groupnr;
3532 ext4_grpblk_t start;
3533 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003534 int len;
3535
3536 /* all form of preallocation discards first load group,
3537 * so the only competing code is preallocation use.
3538 * we don't need any locking here
3539 * notice we do NOT ignore preallocations with pa_deleted
3540 * otherwise we could leave used blocks available for
3541 * allocation in buddy when concurrent ext4_mb_put_pa()
3542 * is dropping preallocation
3543 */
3544 list_for_each(cur, &grp->bb_prealloc_list) {
3545 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3546 spin_lock(&pa->pa_lock);
3547 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3548 &groupnr, &start);
3549 len = pa->pa_len;
3550 spin_unlock(&pa->pa_lock);
3551 if (unlikely(len == 0))
3552 continue;
3553 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003554 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003555 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003556 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003557 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003558}
3559
3560static void ext4_mb_pa_callback(struct rcu_head *head)
3561{
3562 struct ext4_prealloc_space *pa;
3563 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003564
3565 BUG_ON(atomic_read(&pa->pa_count));
3566 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003567 kmem_cache_free(ext4_pspace_cachep, pa);
3568}
3569
3570/*
3571 * drops a reference to preallocated space descriptor
3572 * if this was the last reference and the space is consumed
3573 */
3574static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3575 struct super_block *sb, struct ext4_prealloc_space *pa)
3576{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003577 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003578 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003579
Alex Tomasc9de5602008-01-29 00:19:52 -05003580 /* in this short window concurrent discard can set pa_deleted */
3581 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003582 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3583 spin_unlock(&pa->pa_lock);
3584 return;
3585 }
3586
Alex Tomasc9de5602008-01-29 00:19:52 -05003587 if (pa->pa_deleted == 1) {
3588 spin_unlock(&pa->pa_lock);
3589 return;
3590 }
3591
3592 pa->pa_deleted = 1;
3593 spin_unlock(&pa->pa_lock);
3594
Eric Sandeend33a1972009-03-16 23:25:40 -04003595 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003596 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003597 * If doing group-based preallocation, pa_pstart may be in the
3598 * next group when pa is used up
3599 */
3600 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003601 grp_blk--;
3602
Lukas Czernerbd862982013-04-03 23:32:34 -04003603 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003604
3605 /*
3606 * possible race:
3607 *
3608 * P1 (buddy init) P2 (regular allocation)
3609 * find block B in PA
3610 * copy on-disk bitmap to buddy
3611 * mark B in on-disk bitmap
3612 * drop PA from group
3613 * mark all PAs in buddy
3614 *
3615 * thus, P1 initializes buddy with B available. to prevent this
3616 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3617 * against that pair
3618 */
3619 ext4_lock_group(sb, grp);
3620 list_del(&pa->pa_group_list);
3621 ext4_unlock_group(sb, grp);
3622
3623 spin_lock(pa->pa_obj_lock);
3624 list_del_rcu(&pa->pa_inode_list);
3625 spin_unlock(pa->pa_obj_lock);
3626
3627 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3628}
3629
3630/*
3631 * creates new preallocated space for given inode
3632 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003633static noinline_for_stack int
3634ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003635{
3636 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003637 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003638 struct ext4_prealloc_space *pa;
3639 struct ext4_group_info *grp;
3640 struct ext4_inode_info *ei;
3641
3642 /* preallocate only when found space is larger then requested */
3643 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3644 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3645 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3646
3647 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3648 if (pa == NULL)
3649 return -ENOMEM;
3650
3651 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3652 int winl;
3653 int wins;
3654 int win;
3655 int offs;
3656
3657 /* we can't allocate as much as normalizer wants.
3658 * so, found space must get proper lstart
3659 * to cover original request */
3660 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3661 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3662
3663 /* we're limited by original request in that
3664 * logical block must be covered any way
3665 * winl is window we can move our chunk within */
3666 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3667
3668 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003669 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003670
3671 /* the smallest one defines real window */
3672 win = min(winl, wins);
3673
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003674 offs = ac->ac_o_ex.fe_logical %
3675 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003676 if (offs && offs < win)
3677 win = offs;
3678
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003679 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003680 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003681 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3682 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3683 }
3684
3685 /* preallocation can change ac_b_ex, thus we store actually
3686 * allocated blocks for history */
3687 ac->ac_f_ex = ac->ac_b_ex;
3688
3689 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3690 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3691 pa->pa_len = ac->ac_b_ex.fe_len;
3692 pa->pa_free = pa->pa_len;
3693 atomic_set(&pa->pa_count, 1);
3694 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003695 INIT_LIST_HEAD(&pa->pa_inode_list);
3696 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003697 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003698 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003699
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003700 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003701 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003702 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003703
3704 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003705 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003706
3707 ei = EXT4_I(ac->ac_inode);
3708 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3709
3710 pa->pa_obj_lock = &ei->i_prealloc_lock;
3711 pa->pa_inode = ac->ac_inode;
3712
3713 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3714 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3715 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3716
3717 spin_lock(pa->pa_obj_lock);
3718 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3719 spin_unlock(pa->pa_obj_lock);
3720
3721 return 0;
3722}
3723
3724/*
3725 * creates new preallocated space for locality group inodes belongs to
3726 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003727static noinline_for_stack int
3728ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003729{
3730 struct super_block *sb = ac->ac_sb;
3731 struct ext4_locality_group *lg;
3732 struct ext4_prealloc_space *pa;
3733 struct ext4_group_info *grp;
3734
3735 /* preallocate only when found space is larger then requested */
3736 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3737 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3738 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3739
3740 BUG_ON(ext4_pspace_cachep == NULL);
3741 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3742 if (pa == NULL)
3743 return -ENOMEM;
3744
3745 /* preallocation can change ac_b_ex, thus we store actually
3746 * allocated blocks for history */
3747 ac->ac_f_ex = ac->ac_b_ex;
3748
3749 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3750 pa->pa_lstart = pa->pa_pstart;
3751 pa->pa_len = ac->ac_b_ex.fe_len;
3752 pa->pa_free = pa->pa_len;
3753 atomic_set(&pa->pa_count, 1);
3754 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003755 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003756 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003757 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003758 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003759
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003760 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003761 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3762 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003763
3764 ext4_mb_use_group_pa(ac, pa);
3765 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3766
3767 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3768 lg = ac->ac_lg;
3769 BUG_ON(lg == NULL);
3770
3771 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3772 pa->pa_inode = NULL;
3773
3774 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3775 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3776 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3777
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003778 /*
3779 * We will later add the new pa to the right bucket
3780 * after updating the pa_free in ext4_mb_release_context
3781 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003782 return 0;
3783}
3784
3785static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3786{
3787 int err;
3788
3789 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3790 err = ext4_mb_new_group_pa(ac);
3791 else
3792 err = ext4_mb_new_inode_pa(ac);
3793 return err;
3794}
3795
3796/*
3797 * finds all unused blocks in on-disk bitmap, frees them in
3798 * in-core bitmap and buddy.
3799 * @pa must be unlinked from inode and group lists, so that
3800 * nobody else can find/use it.
3801 * the caller MUST hold group/inode locks.
3802 * TODO: optimize the case when there are no in-core structures yet
3803 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003804static noinline_for_stack int
3805ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003806 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003807{
Alex Tomasc9de5602008-01-29 00:19:52 -05003808 struct super_block *sb = e4b->bd_sb;
3809 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003810 unsigned int end;
3811 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003812 ext4_group_t group;
3813 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003814 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003815 int err = 0;
3816 int free = 0;
3817
3818 BUG_ON(pa->pa_deleted == 0);
3819 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003820 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003821 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3822 end = bit + pa->pa_len;
3823
Alex Tomasc9de5602008-01-29 00:19:52 -05003824 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003825 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003826 if (bit >= end)
3827 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003828 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003829 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003830 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3831 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003832 free += next - bit;
3833
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003834 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003835 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3836 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003837 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003838 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3839 bit = next + 1;
3840 }
3841 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003842 ext4_msg(e4b->bd_sb, KERN_CRIT,
3843 "pa %p: logic %lu, phys. %lu, len %lu",
3844 pa, (unsigned long) pa->pa_lstart,
3845 (unsigned long) pa->pa_pstart,
3846 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003847 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003848 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003849 /*
3850 * pa is already deleted so we use the value obtained
3851 * from the bitmap and continue.
3852 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003853 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003854 atomic_add(free, &sbi->s_mb_discarded);
3855
3856 return err;
3857}
3858
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003859static noinline_for_stack int
3860ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003861 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003862{
Alex Tomasc9de5602008-01-29 00:19:52 -05003863 struct super_block *sb = e4b->bd_sb;
3864 ext4_group_t group;
3865 ext4_grpblk_t bit;
3866
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003867 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003868 BUG_ON(pa->pa_deleted == 0);
3869 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3870 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3871 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3872 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003873 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003874
3875 return 0;
3876}
3877
3878/*
3879 * releases all preallocations in given group
3880 *
3881 * first, we need to decide discard policy:
3882 * - when do we discard
3883 * 1) ENOSPC
3884 * - how many do we discard
3885 * 1) how many requested
3886 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003887static noinline_for_stack int
3888ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003889 ext4_group_t group, int needed)
3890{
3891 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3892 struct buffer_head *bitmap_bh = NULL;
3893 struct ext4_prealloc_space *pa, *tmp;
3894 struct list_head list;
3895 struct ext4_buddy e4b;
3896 int err;
3897 int busy = 0;
3898 int free = 0;
3899
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003900 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003901
3902 if (list_empty(&grp->bb_prealloc_list))
3903 return 0;
3904
Theodore Ts'o574ca172008-07-11 19:27:31 -04003905 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04003906 if (IS_ERR(bitmap_bh)) {
3907 err = PTR_ERR(bitmap_bh);
3908 ext4_error(sb, "Error %d reading block bitmap for %u",
3909 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003910 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003911 }
3912
3913 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003914 if (err) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04003915 ext4_warning(sb, "Error %d loading buddy information for %u",
3916 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003917 put_bh(bitmap_bh);
3918 return 0;
3919 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003920
3921 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003922 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003923
Alex Tomasc9de5602008-01-29 00:19:52 -05003924 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003925repeat:
3926 ext4_lock_group(sb, group);
3927 list_for_each_entry_safe(pa, tmp,
3928 &grp->bb_prealloc_list, pa_group_list) {
3929 spin_lock(&pa->pa_lock);
3930 if (atomic_read(&pa->pa_count)) {
3931 spin_unlock(&pa->pa_lock);
3932 busy = 1;
3933 continue;
3934 }
3935 if (pa->pa_deleted) {
3936 spin_unlock(&pa->pa_lock);
3937 continue;
3938 }
3939
3940 /* seems this one can be freed ... */
3941 pa->pa_deleted = 1;
3942
3943 /* we can trust pa_free ... */
3944 free += pa->pa_free;
3945
3946 spin_unlock(&pa->pa_lock);
3947
3948 list_del(&pa->pa_group_list);
3949 list_add(&pa->u.pa_tmp_list, &list);
3950 }
3951
3952 /* if we still need more blocks and some PAs were used, try again */
3953 if (free < needed && busy) {
3954 busy = 0;
3955 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003956 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003957 goto repeat;
3958 }
3959
3960 /* found anything to free? */
3961 if (list_empty(&list)) {
3962 BUG_ON(free != 0);
3963 goto out;
3964 }
3965
3966 /* now free all selected PAs */
3967 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3968
3969 /* remove from object (inode or locality group) */
3970 spin_lock(pa->pa_obj_lock);
3971 list_del_rcu(&pa->pa_inode_list);
3972 spin_unlock(pa->pa_obj_lock);
3973
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003974 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003975 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003976 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003977 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003978
3979 list_del(&pa->u.pa_tmp_list);
3980 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3981 }
3982
3983out:
3984 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003985 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003986 put_bh(bitmap_bh);
3987 return free;
3988}
3989
3990/*
3991 * releases all non-used preallocated blocks for given inode
3992 *
3993 * It's important to discard preallocations under i_data_sem
3994 * We don't want another block to be served from the prealloc
3995 * space when we are discarding the inode prealloc space.
3996 *
3997 * FIXME!! Make sure it is valid at all the call sites
3998 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003999void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05004000{
4001 struct ext4_inode_info *ei = EXT4_I(inode);
4002 struct super_block *sb = inode->i_sb;
4003 struct buffer_head *bitmap_bh = NULL;
4004 struct ext4_prealloc_space *pa, *tmp;
4005 ext4_group_t group = 0;
4006 struct list_head list;
4007 struct ext4_buddy e4b;
4008 int err;
4009
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004010 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004011 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4012 return;
4013 }
4014
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004015 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004016 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004017
4018 INIT_LIST_HEAD(&list);
4019
4020repeat:
4021 /* first, collect all pa's in the inode */
4022 spin_lock(&ei->i_prealloc_lock);
4023 while (!list_empty(&ei->i_prealloc_list)) {
4024 pa = list_entry(ei->i_prealloc_list.next,
4025 struct ext4_prealloc_space, pa_inode_list);
4026 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4027 spin_lock(&pa->pa_lock);
4028 if (atomic_read(&pa->pa_count)) {
4029 /* this shouldn't happen often - nobody should
4030 * use preallocation while we're discarding it */
4031 spin_unlock(&pa->pa_lock);
4032 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004033 ext4_msg(sb, KERN_ERR,
4034 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05004035 WARN_ON(1);
4036 schedule_timeout_uninterruptible(HZ);
4037 goto repeat;
4038
4039 }
4040 if (pa->pa_deleted == 0) {
4041 pa->pa_deleted = 1;
4042 spin_unlock(&pa->pa_lock);
4043 list_del_rcu(&pa->pa_inode_list);
4044 list_add(&pa->u.pa_tmp_list, &list);
4045 continue;
4046 }
4047
4048 /* someone is deleting pa right now */
4049 spin_unlock(&pa->pa_lock);
4050 spin_unlock(&ei->i_prealloc_lock);
4051
4052 /* we have to wait here because pa_deleted
4053 * doesn't mean pa is already unlinked from
4054 * the list. as we might be called from
4055 * ->clear_inode() the inode will get freed
4056 * and concurrent thread which is unlinking
4057 * pa from inode's list may access already
4058 * freed memory, bad-bad-bad */
4059
4060 /* XXX: if this happens too often, we can
4061 * add a flag to force wait only in case
4062 * of ->clear_inode(), but not in case of
4063 * regular truncate */
4064 schedule_timeout_uninterruptible(HZ);
4065 goto repeat;
4066 }
4067 spin_unlock(&ei->i_prealloc_lock);
4068
4069 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004070 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04004071 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05004072
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004073 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4074 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004075 if (err) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004076 ext4_error(sb, "Error %d loading buddy information for %u",
4077 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004078 continue;
4079 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004080
Theodore Ts'o574ca172008-07-11 19:27:31 -04004081 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004082 if (IS_ERR(bitmap_bh)) {
4083 err = PTR_ERR(bitmap_bh);
4084 ext4_error(sb, "Error %d reading block bitmap for %u",
4085 err, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004086 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004087 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004088 }
4089
4090 ext4_lock_group(sb, group);
4091 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004092 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004093 ext4_unlock_group(sb, group);
4094
Jing Zhange39e07f2010-05-14 00:00:00 -04004095 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004096 put_bh(bitmap_bh);
4097
4098 list_del(&pa->u.pa_tmp_list);
4099 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4100 }
4101}
4102
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004103#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004104static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4105{
4106 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004107 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004108
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05004109 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05004110 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004111 return;
4112
Joe Perches7f6a11e2012-03-19 23:09:43 -04004113 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004114 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04004115 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004116 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004117 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004118 "goal %lu/%lu/%lu@%lu, "
4119 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004120 (unsigned long)ac->ac_o_ex.fe_group,
4121 (unsigned long)ac->ac_o_ex.fe_start,
4122 (unsigned long)ac->ac_o_ex.fe_len,
4123 (unsigned long)ac->ac_o_ex.fe_logical,
4124 (unsigned long)ac->ac_g_ex.fe_group,
4125 (unsigned long)ac->ac_g_ex.fe_start,
4126 (unsigned long)ac->ac_g_ex.fe_len,
4127 (unsigned long)ac->ac_g_ex.fe_logical,
4128 (unsigned long)ac->ac_b_ex.fe_group,
4129 (unsigned long)ac->ac_b_ex.fe_start,
4130 (unsigned long)ac->ac_b_ex.fe_len,
4131 (unsigned long)ac->ac_b_ex.fe_logical,
4132 (int)ac->ac_criteria);
Eric Sandeendc9ddd92014-02-20 13:32:10 -05004133 ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004134 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004135 ngroups = ext4_get_groups_count(sb);
4136 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004137 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4138 struct ext4_prealloc_space *pa;
4139 ext4_grpblk_t start;
4140 struct list_head *cur;
4141 ext4_lock_group(sb, i);
4142 list_for_each(cur, &grp->bb_prealloc_list) {
4143 pa = list_entry(cur, struct ext4_prealloc_space,
4144 pa_group_list);
4145 spin_lock(&pa->pa_lock);
4146 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4147 NULL, &start);
4148 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004149 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4150 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004151 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004152 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004153
4154 if (grp->bb_free == 0)
4155 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004156 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004157 i, grp->bb_free, grp->bb_fragments);
4158 }
4159 printk(KERN_ERR "\n");
4160}
4161#else
4162static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4163{
4164 return;
4165}
4166#endif
4167
4168/*
4169 * We use locality group preallocation for small size file. The size of the
4170 * file is determined by the current size or the resulting size after
4171 * allocation which ever is larger
4172 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004173 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004174 */
4175static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4176{
4177 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4178 int bsbits = ac->ac_sb->s_blocksize_bits;
4179 loff_t size, isize;
4180
4181 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4182 return;
4183
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004184 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4185 return;
4186
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004187 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004188 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4189 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004190
Theodore Ts'o50797482009-09-18 13:34:02 -04004191 if ((size == isize) &&
4192 !ext4_fs_is_busy(sbi) &&
4193 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4194 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4195 return;
4196 }
4197
Robin Dongebbe0272011-10-26 05:14:27 -04004198 if (sbi->s_mb_group_prealloc <= 0) {
4199 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4200 return;
4201 }
4202
Alex Tomasc9de5602008-01-29 00:19:52 -05004203 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004204 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004205 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004206 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004207 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004208 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004209
4210 BUG_ON(ac->ac_lg != NULL);
4211 /*
4212 * locality group prealloc space are per cpu. The reason for having
4213 * per cpu locality group is to reduce the contention between block
4214 * request from multiple CPUs.
4215 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004216 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004217
4218 /* we're going to use group allocation */
4219 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4220
4221 /* serialize all allocations in the group */
4222 mutex_lock(&ac->ac_lg->lg_mutex);
4223}
4224
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004225static noinline_for_stack int
4226ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004227 struct ext4_allocation_request *ar)
4228{
4229 struct super_block *sb = ar->inode->i_sb;
4230 struct ext4_sb_info *sbi = EXT4_SB(sb);
4231 struct ext4_super_block *es = sbi->s_es;
4232 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004233 unsigned int len;
4234 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004235 ext4_grpblk_t block;
4236
4237 /* we can't allocate > group size */
4238 len = ar->len;
4239
4240 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004241 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4242 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004243
4244 /* start searching from the goal */
4245 goal = ar->goal;
4246 if (goal < le32_to_cpu(es->s_first_data_block) ||
4247 goal >= ext4_blocks_count(es))
4248 goal = le32_to_cpu(es->s_first_data_block);
4249 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4250
4251 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004252 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004253 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004254 ac->ac_sb = sb;
4255 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004256 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004257 ac->ac_o_ex.fe_group = group;
4258 ac->ac_o_ex.fe_start = block;
4259 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004260 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004261 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004262
4263 /* we have to define context: we'll we work with a file or
4264 * locality group. this is a policy, actually */
4265 ext4_mb_group_or_file(ac);
4266
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004267 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004268 "left: %u/%u, right %u/%u to %swritable\n",
4269 (unsigned) ar->len, (unsigned) ar->logical,
4270 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4271 (unsigned) ar->lleft, (unsigned) ar->pleft,
4272 (unsigned) ar->lright, (unsigned) ar->pright,
4273 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4274 return 0;
4275
4276}
4277
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004278static noinline_for_stack void
4279ext4_mb_discard_lg_preallocations(struct super_block *sb,
4280 struct ext4_locality_group *lg,
4281 int order, int total_entries)
4282{
4283 ext4_group_t group = 0;
4284 struct ext4_buddy e4b;
4285 struct list_head discard_list;
4286 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004287
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004288 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004289
4290 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004291
4292 spin_lock(&lg->lg_prealloc_lock);
4293 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4294 pa_inode_list) {
4295 spin_lock(&pa->pa_lock);
4296 if (atomic_read(&pa->pa_count)) {
4297 /*
4298 * This is the pa that we just used
4299 * for block allocation. So don't
4300 * free that
4301 */
4302 spin_unlock(&pa->pa_lock);
4303 continue;
4304 }
4305 if (pa->pa_deleted) {
4306 spin_unlock(&pa->pa_lock);
4307 continue;
4308 }
4309 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004310 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004311
4312 /* seems this one can be freed ... */
4313 pa->pa_deleted = 1;
4314 spin_unlock(&pa->pa_lock);
4315
4316 list_del_rcu(&pa->pa_inode_list);
4317 list_add(&pa->u.pa_tmp_list, &discard_list);
4318
4319 total_entries--;
4320 if (total_entries <= 5) {
4321 /*
4322 * we want to keep only 5 entries
4323 * allowing it to grow to 8. This
4324 * mak sure we don't call discard
4325 * soon for this list.
4326 */
4327 break;
4328 }
4329 }
4330 spin_unlock(&lg->lg_prealloc_lock);
4331
4332 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004333 int err;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004334
Lukas Czernerbd862982013-04-03 23:32:34 -04004335 group = ext4_get_group_number(sb, pa->pa_pstart);
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004336 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4337 GFP_NOFS|__GFP_NOFAIL);
4338 if (err) {
4339 ext4_error(sb, "Error %d loading buddy information for %u",
4340 err, group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004341 continue;
4342 }
4343 ext4_lock_group(sb, group);
4344 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004345 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004346 ext4_unlock_group(sb, group);
4347
Jing Zhange39e07f2010-05-14 00:00:00 -04004348 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004349 list_del(&pa->u.pa_tmp_list);
4350 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4351 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004352}
4353
4354/*
4355 * We have incremented pa_count. So it cannot be freed at this
4356 * point. Also we hold lg_mutex. So no parallel allocation is
4357 * possible from this lg. That means pa_free cannot be updated.
4358 *
4359 * A parallel ext4_mb_discard_group_preallocations is possible.
4360 * which can cause the lg_prealloc_list to be updated.
4361 */
4362
4363static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4364{
4365 int order, added = 0, lg_prealloc_count = 1;
4366 struct super_block *sb = ac->ac_sb;
4367 struct ext4_locality_group *lg = ac->ac_lg;
4368 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4369
4370 order = fls(pa->pa_free) - 1;
4371 if (order > PREALLOC_TB_SIZE - 1)
4372 /* The max size of hash table is PREALLOC_TB_SIZE */
4373 order = PREALLOC_TB_SIZE - 1;
4374 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004375 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004376 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4377 pa_inode_list) {
4378 spin_lock(&tmp_pa->pa_lock);
4379 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004380 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004381 continue;
4382 }
4383 if (!added && pa->pa_free < tmp_pa->pa_free) {
4384 /* Add to the tail of the previous entry */
4385 list_add_tail_rcu(&pa->pa_inode_list,
4386 &tmp_pa->pa_inode_list);
4387 added = 1;
4388 /*
4389 * we want to count the total
4390 * number of entries in the list
4391 */
4392 }
4393 spin_unlock(&tmp_pa->pa_lock);
4394 lg_prealloc_count++;
4395 }
4396 if (!added)
4397 list_add_tail_rcu(&pa->pa_inode_list,
4398 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004399 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004400
4401 /* Now trim the list to be not more than 8 elements */
4402 if (lg_prealloc_count > 8) {
4403 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004404 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004405 return;
4406 }
4407 return ;
4408}
4409
Alex Tomasc9de5602008-01-29 00:19:52 -05004410/*
4411 * release all resource we used in allocation
4412 */
4413static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4414{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004415 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004416 struct ext4_prealloc_space *pa = ac->ac_pa;
4417 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004418 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004419 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004420 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004421 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4422 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004423 pa->pa_free -= ac->ac_b_ex.fe_len;
4424 pa->pa_len -= ac->ac_b_ex.fe_len;
4425 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004426 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004427 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004428 if (pa) {
4429 /*
4430 * We want to add the pa to the right bucket.
4431 * Remove it from the list and while adding
4432 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004433 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004434 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004435 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004436 spin_lock(pa->pa_obj_lock);
4437 list_del_rcu(&pa->pa_inode_list);
4438 spin_unlock(pa->pa_obj_lock);
4439 ext4_mb_add_n_trim(ac);
4440 }
4441 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4442 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004443 if (ac->ac_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004444 put_page(ac->ac_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004445 if (ac->ac_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004446 put_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004447 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4448 mutex_unlock(&ac->ac_lg->lg_mutex);
4449 ext4_mb_collect_stats(ac);
4450 return 0;
4451}
4452
4453static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4454{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004455 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004456 int ret;
4457 int freed = 0;
4458
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004459 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004460 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004461 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4462 freed += ret;
4463 needed -= ret;
4464 }
4465
4466 return freed;
4467}
4468
4469/*
4470 * Main entry point into mballoc to allocate blocks
4471 * it tries to use preallocation first, then falls back
4472 * to usual allocation
4473 */
4474ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004475 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004476{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004477 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004478 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004479 struct ext4_sb_info *sbi;
4480 struct super_block *sb;
4481 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004482 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004483 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004484
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004485 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004486 sb = ar->inode->i_sb;
4487 sbi = EXT4_SB(sb);
4488
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004489 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004490
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004491 /* Allow to use superuser reservation for quota file */
4492 if (IS_NOQUOTA(ar->inode))
4493 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4494
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004495 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004496 /* Without delayed allocation we need to verify
4497 * there is enough free blocks to do block allocation
4498 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004499 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004500 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004501 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004502
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004503 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004504 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004505 ar->len = ar->len >> 1;
4506 }
4507 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004508 *errp = -ENOSPC;
4509 return 0;
4510 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004511 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004512 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004513 dquot_alloc_block_nofail(ar->inode,
4514 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004515 } else {
4516 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004517 dquot_alloc_block(ar->inode,
4518 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004519
4520 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4521 ar->len--;
4522 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004523 }
4524 inquota = ar->len;
4525 if (ar->len == 0) {
4526 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004527 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004528 }
Mingming Caod2a17632008-07-14 17:52:37 -04004529 }
Mingming Caod2a17632008-07-14 17:52:37 -04004530
Wei Yongjun85556c92012-09-26 20:43:37 -04004531 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004532 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004533 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004534 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004535 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004536 }
4537
Eric Sandeen256bdb42008-02-10 01:13:33 -05004538 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004539 if (*errp) {
4540 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004541 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004542 }
4543
Eric Sandeen256bdb42008-02-10 01:13:33 -05004544 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4545 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004546 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4547 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004548repeat:
4549 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004550 *errp = ext4_mb_regular_allocator(ac);
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004551 if (*errp)
4552 goto discard_and_exit;
4553
4554 /* as we've just preallocated more space than
4555 * user requested originally, we store allocated
4556 * space in a special descriptor */
4557 if (ac->ac_status == AC_STATUS_FOUND &&
4558 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4559 *errp = ext4_mb_new_preallocation(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004560 if (*errp) {
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004561 discard_and_exit:
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004562 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004563 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004564 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004565 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004566 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004567 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Vegard Nossum554a5cc2016-07-14 23:02:47 -04004568 if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004569 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004570 goto errout;
4571 } else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004572 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4573 ar->len = ac->ac_b_ex.fe_len;
4574 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004575 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004576 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004577 if (freed)
4578 goto repeat;
4579 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004580 }
4581
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004582errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004583 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004584 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004585 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004586 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004587 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004588 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004589out:
4590 if (ac)
4591 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004592 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004593 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004594 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004595 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004596 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004597 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004598 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004599 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004600
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004601 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004602
Alex Tomasc9de5602008-01-29 00:19:52 -05004603 return block;
4604}
Alex Tomasc9de5602008-01-29 00:19:52 -05004605
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004606/*
4607 * We can merge two free data extents only if the physical blocks
4608 * are contiguous, AND the extents were freed by the same transaction,
4609 * AND the blocks are associated with the same group.
4610 */
4611static int can_merge(struct ext4_free_data *entry1,
4612 struct ext4_free_data *entry2)
4613{
Bobi Jam18aadd42012-02-20 17:53:02 -05004614 if ((entry1->efd_tid == entry2->efd_tid) &&
4615 (entry1->efd_group == entry2->efd_group) &&
4616 ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004617 return 1;
4618 return 0;
4619}
4620
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004621static noinline_for_stack int
4622ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004623 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004624{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004625 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004626 ext4_grpblk_t cluster;
Theodore Ts'od08854f2016-06-26 18:24:01 -04004627 ext4_grpblk_t clusters = new_entry->efd_count;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004628 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004629 struct ext4_group_info *db = e4b->bd_info;
4630 struct super_block *sb = e4b->bd_sb;
4631 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004632 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4633 struct rb_node *parent = NULL, *new_node;
4634
Frank Mayhar03901312009-01-07 00:06:22 -05004635 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004636 BUG_ON(e4b->bd_bitmap_page == NULL);
4637 BUG_ON(e4b->bd_buddy_page == NULL);
4638
Bobi Jam18aadd42012-02-20 17:53:02 -05004639 new_node = &new_entry->efd_node;
4640 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004641
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004642 if (!*n) {
4643 /* first free block exent. We need to
4644 protect buddy cache from being freed,
4645 * otherwise we'll refresh it from
4646 * on-disk bitmap and lose not-yet-available
4647 * blocks */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004648 get_page(e4b->bd_buddy_page);
4649 get_page(e4b->bd_bitmap_page);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004650 }
4651 while (*n) {
4652 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004653 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4654 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004655 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004656 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004657 n = &(*n)->rb_right;
4658 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004659 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004660 ext4_group_first_block_no(sb, group) +
4661 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004662 "Block already on to-be-freed list");
Chunguang Xu4b0d8872020-11-07 23:58:18 +08004663 kmem_cache_free(ext4_free_data_cachep, new_entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004664 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004665 }
4666 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004667
4668 rb_link_node(new_node, parent, n);
4669 rb_insert_color(new_node, &db->bb_free_root);
4670
4671 /* Now try to see the extent can be merged to left and right */
4672 node = rb_prev(new_node);
4673 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004674 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004675 if (can_merge(entry, new_entry) &&
4676 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004677 new_entry->efd_start_cluster = entry->efd_start_cluster;
4678 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004679 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004680 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004681 }
4682 }
4683
4684 node = rb_next(new_node);
4685 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004686 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004687 if (can_merge(new_entry, entry) &&
4688 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004689 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004690 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004691 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004692 }
4693 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004694 /* Add the extent to transaction's private list */
Theodore Ts'od08854f2016-06-26 18:24:01 -04004695 new_entry->efd_jce.jce_func = ext4_free_data_callback;
4696 spin_lock(&sbi->s_md_lock);
4697 _ext4_journal_callback_add(handle, &new_entry->efd_jce);
4698 sbi->s_mb_free_pending += clusters;
4699 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004700 return 0;
4701}
4702
Theodore Ts'o44338712009-11-22 07:44:56 -05004703/**
4704 * ext4_free_blocks() -- Free given blocks and update quota
4705 * @handle: handle for this transaction
4706 * @inode: inode
4707 * @block: start physical block to free
4708 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004709 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004710 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004711void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004712 struct buffer_head *bh, ext4_fsblk_t block,
4713 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004714{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004715 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004716 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004717 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004718 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004719 ext4_grpblk_t bit;
4720 struct buffer_head *gd_bh;
4721 ext4_group_t block_group;
4722 struct ext4_sb_info *sbi;
4723 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004724 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004725 int err = 0;
4726 int ret;
4727
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004728 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004729 if (bh) {
4730 if (block)
4731 BUG_ON(block != bh->b_blocknr);
4732 else
4733 block = bh->b_blocknr;
4734 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004735
Alex Tomasc9de5602008-01-29 00:19:52 -05004736 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004737 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
Jan Kara58ef3832021-03-17 17:44:14 +01004738 !ext4_inode_block_valid(inode, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004739 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004740 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004741 goto error_return;
4742 }
4743
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004744 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004745 trace_ext4_free_blocks(inode, block, count, flags);
4746
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004747 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4748 BUG_ON(count > 1);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004749
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004750 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4751 inode, bh, block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004752 }
4753
Theodore Ts'o60e66792010-05-17 07:00:00 -04004754 /*
Theodore Ts'o84130192011-09-09 18:50:51 -04004755 * If the extent to be freed does not begin on a cluster
4756 * boundary, we need to deal with partial clusters at the
4757 * beginning and end of the extent. Normally we will free
4758 * blocks at the beginning or the end unless we are explicitly
4759 * requested to avoid doing so.
4760 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004761 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004762 if (overflow) {
4763 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4764 overflow = sbi->s_cluster_ratio - overflow;
4765 block += overflow;
4766 if (count > overflow)
4767 count -= overflow;
4768 else
4769 return;
4770 } else {
4771 block -= overflow;
4772 count += overflow;
4773 }
4774 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004775 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004776 if (overflow) {
4777 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4778 if (count > overflow)
4779 count -= overflow;
4780 else
4781 return;
4782 } else
4783 count += sbi->s_cluster_ratio - overflow;
4784 }
4785
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004786 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4787 int i;
Daeho Jeongf96c4502016-02-21 18:31:41 -05004788 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004789
4790 for (i = 0; i < count; i++) {
4791 cond_resched();
Daeho Jeongf96c4502016-02-21 18:31:41 -05004792 if (is_metadata)
4793 bh = sb_find_get_block(inode->i_sb, block + i);
4794 ext4_forget(handle, is_metadata, inode, bh, block + i);
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004795 }
4796 }
4797
Alex Tomasc9de5602008-01-29 00:19:52 -05004798do_more:
4799 overflow = 0;
4800 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4801
Darrick J. Wong163a2032013-08-28 17:35:51 -04004802 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4803 ext4_get_group_info(sb, block_group))))
4804 return;
4805
Alex Tomasc9de5602008-01-29 00:19:52 -05004806 /*
4807 * Check to see if we are freeing blocks across a group
4808 * boundary.
4809 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004810 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4811 overflow = EXT4_C2B(sbi, bit) + count -
4812 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004813 count -= overflow;
4814 }
Lukas Czerner810da242013-03-02 17:18:58 -05004815 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004816 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004817 if (IS_ERR(bitmap_bh)) {
4818 err = PTR_ERR(bitmap_bh);
4819 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004820 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004821 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004822 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004823 if (!gdp) {
4824 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004825 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004826 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004827
4828 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4829 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4830 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004831 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004832 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004833 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004834
Eric Sandeen12062dd2010-02-15 14:19:27 -05004835 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004836 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004837 /* err = 0. ext4_std_error should be a no op */
4838 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004839 }
4840
4841 BUFFER_TRACE(bitmap_bh, "getting write access");
4842 err = ext4_journal_get_write_access(handle, bitmap_bh);
4843 if (err)
4844 goto error_return;
4845
4846 /*
4847 * We are about to modify some metadata. Call the journal APIs
4848 * to unshare ->b_data if a currently-committing transaction is
4849 * using it
4850 */
4851 BUFFER_TRACE(gd_bh, "get_write_access");
4852 err = ext4_journal_get_write_access(handle, gd_bh);
4853 if (err)
4854 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004855#ifdef AGGRESSIVE_CHECK
4856 {
4857 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004858 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004859 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4860 }
4861#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004862 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004863
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04004864 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
4865 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
4866 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004867 if (err)
4868 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004869
Daeho Jeongf96c4502016-02-21 18:31:41 -05004870 /*
4871 * We need to make sure we don't reuse the freed block until after the
4872 * transaction is committed. We make an exception if the inode is to be
4873 * written in writeback mode since writeback mode has weak data
4874 * consistency guarantees.
4875 */
4876 if (ext4_handle_valid(handle) &&
4877 ((flags & EXT4_FREE_BLOCKS_METADATA) ||
4878 !ext4_should_writeback_data(inode))) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004879 struct ext4_free_data *new_entry;
4880 /*
Michal Hocko7444a072015-07-05 12:33:44 -04004881 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4882 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004883 */
Michal Hocko7444a072015-07-05 12:33:44 -04004884 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
4885 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05004886 new_entry->efd_start_cluster = bit;
4887 new_entry->efd_group = block_group;
4888 new_entry->efd_count = count_clusters;
4889 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004890
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004891 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004892 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004893 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004894 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004895 /* need to update group_info->bb_free and bitmap
4896 * with group lock held. generate_buddy look at
4897 * them with group lock_held
4898 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004899 if (test_opt(sb, DISCARD)) {
JP Abgrallcc14b9e2014-07-23 16:55:07 -07004900 err = ext4_issue_discard(sb, block_group, bit, count,
4901 0);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004902 if (err && err != -EOPNOTSUPP)
4903 ext4_msg(sb, KERN_WARNING, "discard request in"
4904 " group:%d block:%d count:%lu failed"
4905 " with %d", block_group, bit, count,
4906 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04004907 } else
4908 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004909
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004910 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004911 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4912 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004913 }
4914
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004915 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4916 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004917 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004918 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004919 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004920
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004921 if (sbi->s_log_groups_per_flex) {
4922 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004923 atomic64_add(count_clusters,
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08004924 &sbi_array_rcu_deref(sbi, s_flex_groups,
4925 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004926 }
4927
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04004928 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004929 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
Jan Kara7d734532013-08-17 09:36:54 -04004930 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
4931
4932 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004933
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004934 /* We dirtied the bitmap block */
4935 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4936 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4937
Alex Tomasc9de5602008-01-29 00:19:52 -05004938 /* And the group descriptor block */
4939 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004940 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004941 if (!err)
4942 err = ret;
4943
4944 if (overflow && !err) {
4945 block += count;
4946 count = overflow;
4947 put_bh(bitmap_bh);
4948 goto do_more;
4949 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004950error_return:
4951 brelse(bitmap_bh);
4952 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004953 return;
4954}
Lukas Czerner7360d172010-10-27 21:30:12 -04004955
4956/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004957 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004958 * @handle: handle to this transaction
4959 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004960 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004961 * @count: number of blocks to free
4962 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004963 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004964 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004965int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004966 ext4_fsblk_t block, unsigned long count)
4967{
4968 struct buffer_head *bitmap_bh = NULL;
4969 struct buffer_head *gd_bh;
4970 ext4_group_t block_group;
4971 ext4_grpblk_t bit;
4972 unsigned int i;
4973 struct ext4_group_desc *desc;
4974 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004975 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004976 int err = 0, ret, blk_free_count;
4977 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004978
4979 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4980
Yongqiang Yang4740b832011-07-26 21:51:08 -04004981 if (count == 0)
4982 return 0;
4983
Amir Goldstein2846e822011-05-09 10:46:41 -04004984 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004985 /*
4986 * Check to see if we are freeing blocks across a group
4987 * boundary.
4988 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004989 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04004990 ext4_warning(sb, "too much blocks added to group %u",
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004991 block_group);
4992 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004993 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004994 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004995
Amir Goldstein2846e822011-05-09 10:46:41 -04004996 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004997 if (IS_ERR(bitmap_bh)) {
4998 err = PTR_ERR(bitmap_bh);
4999 bitmap_bh = NULL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005000 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005001 }
5002
Amir Goldstein2846e822011-05-09 10:46:41 -04005003 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005004 if (!desc) {
5005 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04005006 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005007 }
Amir Goldstein2846e822011-05-09 10:46:41 -04005008
5009 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
5010 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
5011 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
5012 in_range(block + count - 1, ext4_inode_table(sb, desc),
5013 sbi->s_itb_per_group)) {
5014 ext4_error(sb, "Adding blocks in system zones - "
5015 "Block = %llu, count = %lu",
5016 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005017 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005018 goto error_return;
5019 }
5020
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005021 BUFFER_TRACE(bitmap_bh, "getting write access");
5022 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04005023 if (err)
5024 goto error_return;
5025
5026 /*
5027 * We are about to modify some metadata. Call the journal APIs
5028 * to unshare ->b_data if a currently-committing transaction is
5029 * using it
5030 */
5031 BUFFER_TRACE(gd_bh, "get_write_access");
5032 err = ext4_journal_get_write_access(handle, gd_bh);
5033 if (err)
5034 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04005035
Amir Goldstein2846e822011-05-09 10:46:41 -04005036 for (i = 0, blocks_freed = 0; i < count; i++) {
5037 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04005038 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005039 ext4_error(sb, "bit already cleared for block %llu",
5040 (ext4_fsblk_t)(block + i));
5041 BUFFER_TRACE(bitmap_bh, "bit already cleared");
5042 } else {
5043 blocks_freed++;
5044 }
5045 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005046
5047 err = ext4_mb_load_buddy(sb, block_group, &e4b);
5048 if (err)
5049 goto error_return;
5050
5051 /*
5052 * need to update group_info->bb_free and bitmap
5053 * with group lock held. generate_buddy look at
5054 * them with group lock_held
5055 */
Amir Goldstein2846e822011-05-09 10:46:41 -04005056 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04005057 mb_clear_bits(bitmap_bh->b_data, bit, count);
5058 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04005059 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
5060 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04005061 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005062 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04005063 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04005064 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05005065 EXT4_NUM_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04005066
5067 if (sbi->s_log_groups_per_flex) {
5068 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04005069 atomic64_add(EXT4_NUM_B2C(sbi, blocks_freed),
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08005070 &sbi_array_rcu_deref(sbi, s_flex_groups,
5071 flex_group)->free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04005072 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005073
5074 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005075
5076 /* We dirtied the bitmap block */
5077 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5078 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5079
5080 /* And the group descriptor block */
5081 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5082 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5083 if (!err)
5084 err = ret;
5085
5086error_return:
5087 brelse(bitmap_bh);
5088 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005089 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005090}
5091
5092/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005093 * ext4_trim_extent -- function to TRIM one single free extent in the group
5094 * @sb: super block for the file system
5095 * @start: starting block of the free extent in the alloc. group
5096 * @count: number of blocks to TRIM
5097 * @group: alloc. group we are working with
5098 * @e4b: ext4 buddy for the group
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005099 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005100 *
5101 * Trim "count" blocks starting at "start" in the "group". To assure that no
5102 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5103 * be called with under the group lock.
5104 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005105static int ext4_trim_extent(struct super_block *sb, int start, int count,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005106 ext4_group_t group, struct ext4_buddy *e4b,
5107 unsigned long blkdev_flags)
jon ernste2cbd582014-04-12 23:01:28 -04005108__releases(bitlock)
5109__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005110{
5111 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005112 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005113
Tao Mab3d4c2b2011-07-11 00:01:52 -04005114 trace_ext4_trim_extent(sb, group, start, count);
5115
Lukas Czerner7360d172010-10-27 21:30:12 -04005116 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5117
5118 ex.fe_start = start;
5119 ex.fe_group = group;
5120 ex.fe_len = count;
5121
5122 /*
5123 * Mark blocks used, so no one can reuse them while
5124 * being trimmed.
5125 */
5126 mb_mark_used(e4b, &ex);
5127 ext4_unlock_group(sb, group);
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005128 ret = ext4_issue_discard(sb, group, start, count, blkdev_flags);
Lukas Czerner7360d172010-10-27 21:30:12 -04005129 ext4_lock_group(sb, group);
5130 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005131 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005132}
5133
5134/**
5135 * ext4_trim_all_free -- function to trim all free space in alloc. group
5136 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005137 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005138 * @start: first group block to examine
5139 * @max: last group block to examine
5140 * @minblocks: minimum extent block count
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005141 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005142 *
5143 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5144 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5145 * the extent.
5146 *
5147 *
5148 * ext4_trim_all_free walks through group's block bitmap searching for free
5149 * extents. When the free extent is found, mark it as used in group buddy
5150 * bitmap. Then issue a TRIM command on this extent and free the extent in
5151 * the group buddy bitmap. This is done until whole group is scanned.
5152 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005153static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005154ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5155 ext4_grpblk_t start, ext4_grpblk_t max,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005156 ext4_grpblk_t minblocks, unsigned long blkdev_flags)
Lukas Czerner7360d172010-10-27 21:30:12 -04005157{
5158 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005159 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005160 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005161 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005162
Tao Mab3d4c2b2011-07-11 00:01:52 -04005163 trace_ext4_trim_all_free(sb, group, start, max);
5164
Lukas Czerner78944082011-05-24 18:16:27 -04005165 ret = ext4_mb_load_buddy(sb, group, &e4b);
5166 if (ret) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04005167 ext4_warning(sb, "Error %d loading buddy information for %u",
5168 ret, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005169 return ret;
5170 }
Lukas Czerner78944082011-05-24 18:16:27 -04005171 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005172
5173 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005174 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5175 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5176 goto out;
5177
Lukas Czerner78944082011-05-24 18:16:27 -04005178 start = (e4b.bd_info->bb_first_free > start) ?
5179 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005180
Lukas Czerner913eed82012-03-21 21:22:22 -04005181 while (start <= max) {
5182 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5183 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005184 break;
Lukas Czerner913eed82012-03-21 21:22:22 -04005185 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005186
5187 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005188 ret = ext4_trim_extent(sb, start,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005189 next - start, group, &e4b,
5190 blkdev_flags);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005191 if (ret && ret != -EOPNOTSUPP)
5192 break;
5193 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005194 count += next - start;
5195 }
Tao Ma169ddc32011-07-11 00:00:07 -04005196 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005197 start = next + 1;
5198
5199 if (fatal_signal_pending(current)) {
5200 count = -ERESTARTSYS;
5201 break;
5202 }
5203
5204 if (need_resched()) {
5205 ext4_unlock_group(sb, group);
5206 cond_resched();
5207 ext4_lock_group(sb, group);
5208 }
5209
Tao Ma169ddc32011-07-11 00:00:07 -04005210 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005211 break;
5212 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005213
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005214 if (!ret) {
5215 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005216 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005217 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005218out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005219 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005220 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005221
5222 ext4_debug("trimmed %d blocks in the group %d\n",
5223 count, group);
5224
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005225 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005226}
5227
5228/**
5229 * ext4_trim_fs() -- trim ioctl handle function
5230 * @sb: superblock for filesystem
5231 * @range: fstrim_range structure
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005232 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005233 *
5234 * start: First Byte to trim
5235 * len: number of Bytes to trim from start
5236 * minlen: minimum extent length in Bytes
5237 * ext4_trim_fs goes through all allocation groups containing Bytes from
5238 * start to start+len. For each such a group ext4_trim_all_free function
5239 * is invoked to trim all free space.
5240 */
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005241int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range,
5242 unsigned long blkdev_flags)
Lukas Czerner7360d172010-10-27 21:30:12 -04005243{
Jan Kara067f735c2021-11-12 16:22:02 +01005244 struct request_queue *q = bdev_get_queue(sb->s_bdev);
Lukas Czerner78944082011-05-24 18:16:27 -04005245 struct ext4_group_info *grp;
Lukas Czerner913eed82012-03-21 21:22:22 -04005246 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005247 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed82012-03-21 21:22:22 -04005248 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005249 ext4_fsblk_t first_data_blk =
5250 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed82012-03-21 21:22:22 -04005251 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005252 int ret = 0;
5253
5254 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed82012-03-21 21:22:22 -04005255 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005256 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5257 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005258
Lukas Czerner5de35e82012-10-22 18:01:19 -04005259 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5260 start >= max_blks ||
5261 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005262 return -EINVAL;
Jan Kara067f735c2021-11-12 16:22:02 +01005263 /* No point to try to trim less than discard granularity */
5264 if (range->minlen < q->limits.discard_granularity) {
5265 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5266 q->limits.discard_granularity >> sb->s_blocksize_bits);
5267 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb))
5268 goto out;
5269 }
Lukas Czerner913eed82012-03-21 21:22:22 -04005270 if (end >= max_blks)
5271 end = max_blks - 1;
5272 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005273 goto out;
Lukas Czerner913eed82012-03-21 21:22:22 -04005274 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005275 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005276
Lukas Czerner913eed82012-03-21 21:22:22 -04005277 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005278 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005279 &first_group, &first_cluster);
Lukas Czerner913eed82012-03-21 21:22:22 -04005280 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005281 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005282
Lukas Czerner913eed82012-03-21 21:22:22 -04005283 /* end now represents the last cluster to discard in this group */
5284 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005285
5286 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005287 grp = ext4_get_group_info(sb, group);
5288 /* We only do this if the grp has never been initialized */
5289 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005290 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Lukas Czerner78944082011-05-24 18:16:27 -04005291 if (ret)
5292 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005293 }
5294
Tao Ma0ba08512011-03-23 15:48:11 -04005295 /*
Lukas Czerner913eed82012-03-21 21:22:22 -04005296 * For all the groups except the last one, last cluster will
5297 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5298 * change it for the last group, note that last_cluster is
5299 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005300 */
Lukas Czerner913eed82012-03-21 21:22:22 -04005301 if (group == last_group)
5302 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005303
Lukas Czerner78944082011-05-24 18:16:27 -04005304 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005305 cnt = ext4_trim_all_free(sb, group, first_cluster,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005306 end, minlen, blkdev_flags);
Lukas Czerner7360d172010-10-27 21:30:12 -04005307 if (cnt < 0) {
5308 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005309 break;
5310 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005311 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005312 }
Lukas Czerner913eed82012-03-21 21:22:22 -04005313
5314 /*
5315 * For every group except the first one, we are sure
5316 * that the first cluster to discard will be cluster #0.
5317 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005318 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005319 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005320
Tao Ma3d56b8d2011-07-11 00:03:38 -04005321 if (!ret)
5322 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5323
Tao Ma22f10452011-07-10 23:52:37 -04005324out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005325 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005326 return ret;
5327}