blob: c18668e3135e8be168e68a5d5dbeedf882c109ac [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;
1947 BUG_ON(free <= 0);
1948
1949 i = e4b->bd_info->bb_first_free;
1950
1951 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001952 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001953 EXT4_CLUSTERS_PER_GROUP(sb), i);
1954 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001955 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001956 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001957 * free blocks even though group info says we
1958 * we have free blocks
1959 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001960 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001961 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001962 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001963 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001964 break;
1965 }
1966
Robin Dong15c006a2012-08-17 10:02:17 -04001967 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001968 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001969 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001970 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001971 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001972 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001973 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001974 /*
1975 * The number of free blocks differs. This mostly
1976 * indicate that the bitmap is corrupt. So exit
1977 * without claiming the space.
1978 */
1979 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001980 }
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001981 ex.fe_logical = 0xDEADC0DE; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001982 ext4_mb_measure_extent(ac, &ex, e4b);
1983
1984 i += ex.fe_len;
1985 free -= ex.fe_len;
1986 }
1987
1988 ext4_mb_check_limits(ac, e4b, 1);
1989}
1990
1991/*
1992 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001993 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001994 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001995static noinline_for_stack
1996void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001997 struct ext4_buddy *e4b)
1998{
1999 struct super_block *sb = ac->ac_sb;
2000 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05002001 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05002002 struct ext4_free_extent ex;
2003 ext4_fsblk_t first_group_block;
2004 ext4_fsblk_t a;
2005 ext4_grpblk_t i;
2006 int max;
2007
2008 BUG_ON(sbi->s_stripe == 0);
2009
2010 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05002011 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
2012
Alex Tomasc9de5602008-01-29 00:19:52 -05002013 a = first_group_block + sbi->s_stripe - 1;
2014 do_div(a, sbi->s_stripe);
2015 i = (a * sbi->s_stripe) - first_group_block;
2016
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04002017 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002018 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04002019 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002020 if (max >= sbi->s_stripe) {
2021 ac->ac_found++;
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002022 ex.fe_logical = 0xDEADF00D; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002023 ac->ac_b_ex = ex;
2024 ext4_mb_use_best_found(ac, e4b);
2025 break;
2026 }
2027 }
2028 i += sbi->s_stripe;
2029 }
2030}
2031
Lukas Czerner42ac1842015-06-08 11:40:40 -04002032/*
2033 * This is now called BEFORE we load the buddy bitmap.
2034 * Returns either 1 or 0 indicating that the group is either suitable
2035 * for the allocation or not. In addition it can also return negative
2036 * error code when something goes wrong.
2037 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002038static int ext4_mb_good_group(struct ext4_allocation_context *ac,
2039 ext4_group_t group, int cr)
2040{
2041 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002042 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002043 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2044
2045 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002046
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002047 free = grp->bb_free;
2048 if (free == 0)
2049 return 0;
2050 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
2051 return 0;
2052
Darrick J. Wong163a2032013-08-28 17:35:51 -04002053 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
2054 return 0;
2055
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002056 /* We only do this if the grp has never been initialized */
2057 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04002058 int ret = ext4_mb_init_group(ac->ac_sb, group, GFP_NOFS);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002059 if (ret)
Lukas Czerner42ac1842015-06-08 11:40:40 -04002060 return ret;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002061 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002062
Alex Tomasc9de5602008-01-29 00:19:52 -05002063 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05002064 if (fragments == 0)
2065 return 0;
2066
2067 switch (cr) {
2068 case 0:
2069 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05002070
Theodore Ts'oa4912122009-03-12 12:18:34 -04002071 /* Avoid using the first bg of a flexgroup for data files */
2072 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
2073 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
2074 ((group % flex_size) == 0))
2075 return 0;
2076
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002077 if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) ||
2078 (free / fragments) >= ac->ac_g_ex.fe_len)
2079 return 1;
2080
2081 if (grp->bb_largest_free_order < ac->ac_2order)
2082 return 0;
2083
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002084 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002085 case 1:
2086 if ((free / fragments) >= ac->ac_g_ex.fe_len)
2087 return 1;
2088 break;
2089 case 2:
2090 if (free >= ac->ac_g_ex.fe_len)
2091 return 1;
2092 break;
2093 case 3:
2094 return 1;
2095 default:
2096 BUG();
2097 }
2098
2099 return 0;
2100}
2101
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002102static noinline_for_stack int
2103ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002104{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002105 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002106 int cr;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002107 int err = 0, first_err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002108 struct ext4_sb_info *sbi;
2109 struct super_block *sb;
2110 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002111
2112 sb = ac->ac_sb;
2113 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002114 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002115 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002116 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002117 ngroups = sbi->s_blockfile_groups;
2118
Alex Tomasc9de5602008-01-29 00:19:52 -05002119 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2120
2121 /* first, try the goal */
2122 err = ext4_mb_find_by_goal(ac, &e4b);
2123 if (err || ac->ac_status == AC_STATUS_FOUND)
2124 goto out;
2125
2126 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2127 goto out;
2128
2129 /*
2130 * ac->ac2_order is set only if the fe_len is a power of 2
2131 * if ac2_order is set we also set criteria to 0 so that we
2132 * try exact allocation using buddy.
2133 */
2134 i = fls(ac->ac_g_ex.fe_len);
2135 ac->ac_2order = 0;
2136 /*
2137 * We search using buddy data only if the order of the request
2138 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002139 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Jan Kara0e9deca2017-10-07 22:36:49 +00002140 * We also support searching for power-of-two requests only for
2141 * requests upto maximum buddy size we have constructed.
Alex Tomasc9de5602008-01-29 00:19:52 -05002142 */
Jan Kara0e9deca2017-10-07 22:36:49 +00002143 if (i >= sbi->s_mb_order2_reqs && i <= sb->s_blocksize_bits + 2) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002144 /*
2145 * This should tell if fe_len is exactly power of 2
2146 */
2147 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
Jeremy Cline51ada112018-08-02 00:03:40 -04002148 ac->ac_2order = array_index_nospec(i - 1,
2149 sb->s_blocksize_bits + 2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002150 }
2151
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002152 /* if stream allocation is enabled, use global goal */
2153 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002154 /* TBD: may be hot point */
2155 spin_lock(&sbi->s_md_lock);
2156 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2157 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2158 spin_unlock(&sbi->s_md_lock);
2159 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002160
Alex Tomasc9de5602008-01-29 00:19:52 -05002161 /* Let's just scan groups to find more-less suitable blocks */
2162 cr = ac->ac_2order ? 0 : 1;
2163 /*
2164 * cr == 0 try to get exact allocation,
2165 * cr == 3 try to get anything
2166 */
2167repeat:
2168 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2169 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002170 /*
2171 * searching for the right group start
2172 * from the goal value specified
2173 */
2174 group = ac->ac_g_ex.fe_group;
2175
Theodore Ts'o8df96752009-05-01 08:50:38 -04002176 for (i = 0; i < ngroups; group++, i++) {
Lukas Czerner42ac1842015-06-08 11:40:40 -04002177 int ret = 0;
Theodore Ts'o2ed57242013-06-12 11:43:02 -04002178 cond_resched();
Lachlan McIlroye6155732013-05-05 23:10:00 -04002179 /*
2180 * Artificially restricted ngroups for non-extent
2181 * files makes group > ngroups possible on first loop.
2182 */
2183 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002184 group = 0;
2185
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002186 /* This now checks without needing the buddy page */
Lukas Czerner42ac1842015-06-08 11:40:40 -04002187 ret = ext4_mb_good_group(ac, group, cr);
2188 if (ret <= 0) {
2189 if (!first_err)
2190 first_err = ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002191 continue;
Lukas Czerner42ac1842015-06-08 11:40:40 -04002192 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002193
Alex Tomasc9de5602008-01-29 00:19:52 -05002194 err = ext4_mb_load_buddy(sb, group, &e4b);
2195 if (err)
2196 goto out;
2197
2198 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002199
2200 /*
2201 * We need to check again after locking the
2202 * block group
2203 */
Lukas Czerner42ac1842015-06-08 11:40:40 -04002204 ret = ext4_mb_good_group(ac, group, cr);
2205 if (ret <= 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002206 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002207 ext4_mb_unload_buddy(&e4b);
Lukas Czerner42ac1842015-06-08 11:40:40 -04002208 if (!first_err)
2209 first_err = ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002210 continue;
2211 }
2212
2213 ac->ac_groups_scanned++;
Jan Kara0e9deca2017-10-07 22:36:49 +00002214 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002215 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002216 else if (cr == 1 && sbi->s_stripe &&
2217 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002218 ext4_mb_scan_aligned(ac, &e4b);
2219 else
2220 ext4_mb_complex_scan_group(ac, &e4b);
2221
2222 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002223 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002224
2225 if (ac->ac_status != AC_STATUS_CONTINUE)
2226 break;
2227 }
2228 }
2229
2230 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2231 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2232 /*
2233 * We've been searching too long. Let's try to allocate
2234 * the best chunk we've found so far
2235 */
2236
2237 ext4_mb_try_best_found(ac, &e4b);
2238 if (ac->ac_status != AC_STATUS_FOUND) {
2239 /*
2240 * Someone more lucky has already allocated it.
2241 * The only thing we can do is just take first
2242 * found block(s)
2243 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2244 */
2245 ac->ac_b_ex.fe_group = 0;
2246 ac->ac_b_ex.fe_start = 0;
2247 ac->ac_b_ex.fe_len = 0;
2248 ac->ac_status = AC_STATUS_CONTINUE;
2249 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2250 cr = 3;
2251 atomic_inc(&sbi->s_mb_lost_chunks);
2252 goto repeat;
2253 }
2254 }
2255out:
Lukas Czerner42ac1842015-06-08 11:40:40 -04002256 if (!err && ac->ac_status != AC_STATUS_FOUND && first_err)
2257 err = first_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002258 return err;
2259}
2260
Alex Tomasc9de5602008-01-29 00:19:52 -05002261static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2262{
2263 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002264 ext4_group_t group;
2265
Theodore Ts'o8df96752009-05-01 08:50:38 -04002266 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002267 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002268 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002269 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002270}
2271
2272static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2273{
2274 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002275 ext4_group_t group;
2276
2277 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002278 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002279 return NULL;
2280 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002281 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002282}
2283
2284static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2285{
2286 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002287 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002288 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002289 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002290 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002291 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -05002292 struct sg {
2293 struct ext4_group_info info;
Chandan Rajendrab493c712016-11-14 21:26:26 -05002294 ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2];
Alex Tomasc9de5602008-01-29 00:19:52 -05002295 } sg;
2296
2297 group--;
2298 if (group == 0)
Rasmus Villemoes97b4af22015-06-15 00:32:58 -04002299 seq_puts(seq, "#group: free frags first ["
2300 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
Huaitong Han802cf1f2016-02-12 00:17:16 -05002301 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002302
2303 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2304 sizeof(struct ext4_group_info);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002305 grinfo = ext4_get_group_info(sb, group);
2306 /* Load the group info in memory only if not already loaded. */
2307 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2308 err = ext4_mb_load_buddy(sb, group, &e4b);
2309 if (err) {
2310 seq_printf(seq, "#%-5u: I/O error\n", group);
2311 return 0;
2312 }
2313 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002314 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002315
Alex Tomasc9de5602008-01-29 00:19:52 -05002316 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002317
2318 if (buddy_loaded)
2319 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002320
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002321 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002322 sg.info.bb_fragments, sg.info.bb_first_free);
2323 for (i = 0; i <= 13; i++)
2324 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2325 sg.info.bb_counters[i] : 0);
2326 seq_printf(seq, " ]\n");
2327
2328 return 0;
2329}
2330
2331static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2332{
2333}
2334
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002335static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002336 .start = ext4_mb_seq_groups_start,
2337 .next = ext4_mb_seq_groups_next,
2338 .stop = ext4_mb_seq_groups_stop,
2339 .show = ext4_mb_seq_groups_show,
2340};
2341
2342static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2343{
Al Virod9dda782013-03-31 18:16:14 -04002344 struct super_block *sb = PDE_DATA(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05002345 int rc;
2346
2347 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2348 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002349 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002350 m->private = sb;
2351 }
2352 return rc;
2353
2354}
2355
Theodore Ts'oebd173b2015-09-23 12:46:17 -04002356const struct file_operations ext4_seq_mb_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002357 .open = ext4_mb_seq_groups_open,
2358 .read = seq_read,
2359 .llseek = seq_lseek,
2360 .release = seq_release,
2361};
2362
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002363static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2364{
2365 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2366 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2367
2368 BUG_ON(!cachep);
2369 return cachep;
2370}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002371
Theodore Ts'o28623c22012-09-05 01:31:50 -04002372/*
2373 * Allocate the top-level s_group_info array for the specified number
2374 * of groups
2375 */
2376int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2377{
2378 struct ext4_sb_info *sbi = EXT4_SB(sb);
2379 unsigned size;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002380 struct ext4_group_info ***old_groupinfo, ***new_groupinfo;
Theodore Ts'o28623c22012-09-05 01:31:50 -04002381
2382 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2383 EXT4_DESC_PER_BLOCK_BITS(sb);
2384 if (size <= sbi->s_group_info_size)
2385 return 0;
2386
2387 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
2388 new_groupinfo = ext4_kvzalloc(size, GFP_KERNEL);
2389 if (!new_groupinfo) {
2390 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2391 return -ENOMEM;
2392 }
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002393 rcu_read_lock();
2394 old_groupinfo = rcu_dereference(sbi->s_group_info);
2395 if (old_groupinfo)
2396 memcpy(new_groupinfo, old_groupinfo,
Theodore Ts'o28623c22012-09-05 01:31:50 -04002397 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002398 rcu_read_unlock();
2399 rcu_assign_pointer(sbi->s_group_info, new_groupinfo);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002400 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002401 if (old_groupinfo)
2402 ext4_kvfree_array_rcu(old_groupinfo);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002403 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2404 sbi->s_group_info_size);
2405 return 0;
2406}
2407
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002408/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002409int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002410 struct ext4_group_desc *desc)
2411{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002412 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002413 int metalen = 0;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002414 int idx = group >> EXT4_DESC_PER_BLOCK_BITS(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002415 struct ext4_sb_info *sbi = EXT4_SB(sb);
2416 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002417 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002418
2419 /*
2420 * First check if this group is the first of a reserved block.
2421 * If it's true, we have to allocate a new table of pointers
2422 * to ext4_group_info structures
2423 */
2424 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2425 metalen = sizeof(*meta_group_info) <<
2426 EXT4_DESC_PER_BLOCK_BITS(sb);
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002427 meta_group_info = kmalloc(metalen, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002428 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002429 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002430 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002431 goto exit_meta_group_info;
2432 }
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002433 rcu_read_lock();
2434 rcu_dereference(sbi->s_group_info)[idx] = meta_group_info;
2435 rcu_read_unlock();
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002436 }
2437
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002438 meta_group_info = sbi_array_rcu_deref(sbi, s_group_info, idx);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002439 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2440
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002441 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002442 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002443 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002444 goto exit_group_info;
2445 }
2446 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2447 &(meta_group_info[i]->bb_state));
2448
2449 /*
2450 * initialize bb_free to be able to skip
2451 * empty groups without initialization
2452 */
Theodore Ts'o5ae57322018-06-14 00:58:00 -04002453 if (ext4_has_group_desc_csum(sb) &&
2454 (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002455 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002456 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002457 } else {
2458 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002459 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002460 }
2461
2462 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002463 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002464 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002465 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002466
2467#ifdef DOUBLE_CHECK
2468 {
2469 struct buffer_head *bh;
2470 meta_group_info[i]->bb_bitmap =
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002471 kmalloc(sb->s_blocksize, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002472 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2473 bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002474 BUG_ON(IS_ERR_OR_NULL(bh));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002475 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2476 sb->s_blocksize);
2477 put_bh(bh);
2478 }
2479#endif
2480
2481 return 0;
2482
2483exit_group_info:
2484 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002485 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002486 struct ext4_group_info ***group_info;
2487
2488 rcu_read_lock();
2489 group_info = rcu_dereference(sbi->s_group_info);
2490 kfree(group_info[idx]);
2491 group_info[idx] = NULL;
2492 rcu_read_unlock();
Tao Macaaf7a22011-07-11 18:42:42 -04002493 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002494exit_meta_group_info:
2495 return -ENOMEM;
2496} /* ext4_mb_add_groupinfo */
2497
Alex Tomasc9de5602008-01-29 00:19:52 -05002498static int ext4_mb_init_backend(struct super_block *sb)
2499{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002500 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002501 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002502 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002503 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002504 struct ext4_group_desc *desc;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002505 struct ext4_group_info ***group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002506 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002507
Theodore Ts'o28623c22012-09-05 01:31:50 -04002508 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2509 if (err)
2510 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002511
Alex Tomasc9de5602008-01-29 00:19:52 -05002512 sbi->s_buddy_cache = new_inode(sb);
2513 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002514 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002515 goto err_freesgi;
2516 }
Yu Jian48e60612011-08-01 17:41:39 -04002517 /* To avoid potentially colliding with an valid on-disk inode number,
2518 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2519 * not in the inode hash, so it should never be found by iget(), but
2520 * this will avoid confusion if it ever shows up during debugging. */
2521 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002522 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002523 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002524 desc = ext4_get_group_desc(sb, i, NULL);
2525 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002526 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002527 goto err_freebuddy;
2528 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002529 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2530 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002531 }
2532
2533 return 0;
2534
2535err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002536 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002537 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002538 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002539 i = sbi->s_group_info_size;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002540 rcu_read_lock();
2541 group_info = rcu_dereference(sbi->s_group_info);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002542 while (i-- > 0)
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002543 kfree(group_info[i]);
2544 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002545 iput(sbi->s_buddy_cache);
2546err_freesgi:
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002547 rcu_read_lock();
2548 kvfree(rcu_dereference(sbi->s_group_info));
2549 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002550 return -ENOMEM;
2551}
2552
Eric Sandeen2892c152011-02-12 08:12:18 -05002553static void ext4_groupinfo_destroy_slabs(void)
2554{
2555 int i;
2556
2557 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2558 if (ext4_groupinfo_caches[i])
2559 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2560 ext4_groupinfo_caches[i] = NULL;
2561 }
2562}
2563
2564static int ext4_groupinfo_create_slab(size_t size)
2565{
2566 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2567 int slab_size;
2568 int blocksize_bits = order_base_2(size);
2569 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2570 struct kmem_cache *cachep;
2571
2572 if (cache_index >= NR_GRPINFO_CACHES)
2573 return -EINVAL;
2574
2575 if (unlikely(cache_index < 0))
2576 cache_index = 0;
2577
2578 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2579 if (ext4_groupinfo_caches[cache_index]) {
2580 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2581 return 0; /* Already created */
2582 }
2583
2584 slab_size = offsetof(struct ext4_group_info,
2585 bb_counters[blocksize_bits + 2]);
2586
2587 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2588 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2589 NULL);
2590
Tao Ma823ba012011-07-11 18:26:01 -04002591 ext4_groupinfo_caches[cache_index] = cachep;
2592
Eric Sandeen2892c152011-02-12 08:12:18 -05002593 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2594 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002595 printk(KERN_EMERG
2596 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002597 return -ENOMEM;
2598 }
2599
Eric Sandeen2892c152011-02-12 08:12:18 -05002600 return 0;
2601}
2602
Akira Fujita9d990122012-05-28 14:19:25 -04002603int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002604{
2605 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002606 unsigned i, j;
Nicolai Stange935244c2016-05-05 19:46:19 -04002607 unsigned offset, offset_incr;
Alex Tomasc9de5602008-01-29 00:19:52 -05002608 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002609 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002610
Eric Sandeen19278052009-08-25 22:36:25 -04002611 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002612
2613 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2614 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002615 ret = -ENOMEM;
2616 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002617 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002618
Eric Sandeen19278052009-08-25 22:36:25 -04002619 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002620 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2621 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002622 ret = -ENOMEM;
2623 goto out;
2624 }
2625
Eric Sandeen2892c152011-02-12 08:12:18 -05002626 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2627 if (ret < 0)
2628 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002629
2630 /* order 0 is regular bitmap */
2631 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2632 sbi->s_mb_offsets[0] = 0;
2633
2634 i = 1;
2635 offset = 0;
Nicolai Stange935244c2016-05-05 19:46:19 -04002636 offset_incr = 1 << (sb->s_blocksize_bits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05002637 max = sb->s_blocksize << 2;
2638 do {
2639 sbi->s_mb_offsets[i] = offset;
2640 sbi->s_mb_maxs[i] = max;
Nicolai Stange935244c2016-05-05 19:46:19 -04002641 offset += offset_incr;
2642 offset_incr = offset_incr >> 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002643 max = max >> 1;
2644 i++;
2645 } while (i <= sb->s_blocksize_bits + 1);
2646
Alex Tomasc9de5602008-01-29 00:19:52 -05002647 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002648 spin_lock_init(&sbi->s_bal_lock);
Theodore Ts'od08854f2016-06-26 18:24:01 -04002649 sbi->s_mb_free_pending = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002650
2651 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2652 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2653 sbi->s_mb_stats = MB_DEFAULT_STATS;
2654 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2655 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002656 /*
2657 * The default group preallocation is 512, which for 4k block
2658 * sizes translates to 2 megabytes. However for bigalloc file
2659 * systems, this is probably too big (i.e, if the cluster size
2660 * is 1 megabyte, then group preallocation size becomes half a
2661 * gigabyte!). As a default, we will keep a two megabyte
2662 * group pralloc size for cluster sizes up to 64k, and after
2663 * that, we will force a minimum group preallocation size of
2664 * 32 clusters. This translates to 8 megs when the cluster
2665 * size is 256k, and 32 megs when the cluster size is 1 meg,
2666 * which seems reasonable as a default.
2667 */
2668 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2669 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002670 /*
2671 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2672 * to the lowest multiple of s_stripe which is bigger than
2673 * the s_mb_group_prealloc as determined above. We want
2674 * the preallocation size to be an exact multiple of the
2675 * RAID stripe size so that preallocations don't fragment
2676 * the stripes.
2677 */
2678 if (sbi->s_stripe > 1) {
2679 sbi->s_mb_group_prealloc = roundup(
2680 sbi->s_mb_group_prealloc, sbi->s_stripe);
2681 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002682
Eric Sandeen730c2132008-09-13 15:23:29 -04002683 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002684 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002685 ret = -ENOMEM;
Andrey Tsyvarev029b10c2014-05-12 12:34:21 -04002686 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002687 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002688 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002689 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002690 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002691 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002692 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2693 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002694 spin_lock_init(&lg->lg_prealloc_lock);
2695 }
2696
Yu Jian79a77c52011-08-01 17:41:46 -04002697 /* init file for buddy data */
2698 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002699 if (ret != 0)
2700 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002701
Tao Ma7aa0bae2011-10-06 10:22:28 -04002702 return 0;
2703
2704out_free_locality_groups:
2705 free_percpu(sbi->s_locality_groups);
2706 sbi->s_locality_groups = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002707out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002708 kfree(sbi->s_mb_offsets);
2709 sbi->s_mb_offsets = NULL;
2710 kfree(sbi->s_mb_maxs);
2711 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002712 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002713}
2714
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002715/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002716static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2717{
2718 struct ext4_prealloc_space *pa;
2719 struct list_head *cur, *tmp;
2720 int count = 0;
2721
2722 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2723 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2724 list_del(&pa->pa_group_list);
2725 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002726 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002727 }
2728 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002729 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002730
2731}
2732
2733int ext4_mb_release(struct super_block *sb)
2734{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002735 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002736 ext4_group_t i;
2737 int num_meta_group_infos;
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002738 struct ext4_group_info *grinfo, ***group_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002739 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002740 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002741
Alex Tomasc9de5602008-01-29 00:19:52 -05002742 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002743 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002744 grinfo = ext4_get_group_info(sb, i);
2745#ifdef DOUBLE_CHECK
2746 kfree(grinfo->bb_bitmap);
2747#endif
2748 ext4_lock_group(sb, i);
2749 ext4_mb_cleanup_pa(grinfo);
2750 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002751 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002752 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002753 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002754 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2755 EXT4_DESC_PER_BLOCK_BITS(sb);
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002756 rcu_read_lock();
2757 group_info = rcu_dereference(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002758 for (i = 0; i < num_meta_group_infos; i++)
Suraj Jitindar Singh96f59962020-02-18 19:08:50 -08002759 kfree(group_info[i]);
2760 kvfree(group_info);
2761 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05002762 }
2763 kfree(sbi->s_mb_offsets);
2764 kfree(sbi->s_mb_maxs);
Markus Elfringbfcba2d2014-11-25 20:01:37 -05002765 iput(sbi->s_buddy_cache);
Alex Tomasc9de5602008-01-29 00:19:52 -05002766 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002767 ext4_msg(sb, KERN_INFO,
2768 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002769 atomic_read(&sbi->s_bal_allocated),
2770 atomic_read(&sbi->s_bal_reqs),
2771 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002772 ext4_msg(sb, KERN_INFO,
2773 "mballoc: %u extents scanned, %u goal hits, "
2774 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002775 atomic_read(&sbi->s_bal_ex_scanned),
2776 atomic_read(&sbi->s_bal_goals),
2777 atomic_read(&sbi->s_bal_2orders),
2778 atomic_read(&sbi->s_bal_breaks),
2779 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002780 ext4_msg(sb, KERN_INFO,
2781 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002782 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002783 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002784 ext4_msg(sb, KERN_INFO,
2785 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002786 atomic_read(&sbi->s_mb_preallocated),
2787 atomic_read(&sbi->s_mb_discarded));
2788 }
2789
Eric Sandeen730c2132008-09-13 15:23:29 -04002790 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002791
2792 return 0;
2793}
2794
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002795static inline int ext4_issue_discard(struct super_block *sb,
Theodore Ts'o84130192011-09-09 18:50:51 -04002796 ext4_group_t block_group, ext4_grpblk_t cluster, int count)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002797{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002798 ext4_fsblk_t discard_block;
2799
Theodore Ts'o84130192011-09-09 18:50:51 -04002800 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2801 ext4_group_first_block_no(sb, block_group));
2802 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002803 trace_ext4_discard_blocks(sb,
2804 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002805 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002806}
2807
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002808/*
2809 * This function is called by the jbd2 layer once the commit has finished,
2810 * so we know we can free the blocks that were released with that commit.
2811 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002812static void ext4_free_data_callback(struct super_block *sb,
2813 struct ext4_journal_cb_entry *jce,
2814 int rc)
Alex Tomasc9de5602008-01-29 00:19:52 -05002815{
Bobi Jam18aadd42012-02-20 17:53:02 -05002816 struct ext4_free_data *entry = (struct ext4_free_data *)jce;
Alex Tomasc9de5602008-01-29 00:19:52 -05002817 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002818 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002819 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002820
Bobi Jam18aadd42012-02-20 17:53:02 -05002821 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2822 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002823
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002824 if (test_opt(sb, DISCARD)) {
2825 err = ext4_issue_discard(sb, entry->efd_group,
2826 entry->efd_start_cluster,
2827 entry->efd_count);
2828 if (err && err != -EOPNOTSUPP)
2829 ext4_msg(sb, KERN_WARNING, "discard request in"
2830 " group:%d block:%d count:%d failed"
2831 " with %d", entry->efd_group,
2832 entry->efd_start_cluster,
2833 entry->efd_count, err);
2834 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002835
Bobi Jam18aadd42012-02-20 17:53:02 -05002836 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2837 /* we expect to find existing buddy because it's pinned */
2838 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002839
Theodore Ts'od08854f2016-06-26 18:24:01 -04002840 spin_lock(&EXT4_SB(sb)->s_md_lock);
2841 EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
2842 spin_unlock(&EXT4_SB(sb)->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002843
Bobi Jam18aadd42012-02-20 17:53:02 -05002844 db = e4b.bd_info;
2845 /* there are blocks to put in buddy to make them really free */
2846 count += entry->efd_count;
2847 count2++;
2848 ext4_lock_group(sb, entry->efd_group);
2849 /* Take it out of per group rb tree */
2850 rb_erase(&entry->efd_node, &(db->bb_free_root));
2851 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002852
Bobi Jam18aadd42012-02-20 17:53:02 -05002853 /*
2854 * Clear the trimmed flag for the group so that the next
2855 * ext4_trim_fs can trim it.
2856 * If the volume is mounted with -o discard, online discard
2857 * is supported and the free blocks will be trimmed online.
2858 */
2859 if (!test_opt(sb, DISCARD))
2860 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2861
2862 if (!db->bb_free_root.rb_node) {
2863 /* No more items in the per group rb tree
2864 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002865 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002866 put_page(e4b.bd_buddy_page);
2867 put_page(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002868 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002869 ext4_unlock_group(sb, entry->efd_group);
2870 kmem_cache_free(ext4_free_data_cachep, entry);
2871 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002872
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002873 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002874}
2875
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002876int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002877{
Theodore Ts'o16828082010-10-27 21:30:09 -04002878 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2879 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002880 if (ext4_pspace_cachep == NULL)
2881 return -ENOMEM;
2882
Theodore Ts'o16828082010-10-27 21:30:09 -04002883 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2884 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002885 if (ext4_ac_cachep == NULL) {
2886 kmem_cache_destroy(ext4_pspace_cachep);
2887 return -ENOMEM;
2888 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002889
Bobi Jam18aadd42012-02-20 17:53:02 -05002890 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2891 SLAB_RECLAIM_ACCOUNT);
2892 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002893 kmem_cache_destroy(ext4_pspace_cachep);
2894 kmem_cache_destroy(ext4_ac_cachep);
2895 return -ENOMEM;
2896 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002897 return 0;
2898}
2899
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002900void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002901{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002902 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002903 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2904 * before destroying the slab cache.
2905 */
2906 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002907 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002908 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002909 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002910 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002911}
2912
2913
2914/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002915 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002916 * Returns 0 if success or error code
2917 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002918static noinline_for_stack int
2919ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002920 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002921{
2922 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002923 struct ext4_group_desc *gdp;
2924 struct buffer_head *gdp_bh;
2925 struct ext4_sb_info *sbi;
2926 struct super_block *sb;
2927 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002928 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002929
2930 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2931 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2932
2933 sb = ac->ac_sb;
2934 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002935
Theodore Ts'o574ca172008-07-11 19:27:31 -04002936 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002937 if (IS_ERR(bitmap_bh)) {
2938 err = PTR_ERR(bitmap_bh);
2939 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002940 goto out_err;
Darrick J. Wong9008a582015-10-17 21:33:24 -04002941 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002942
liang xie5d601252014-05-12 22:06:43 -04002943 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002944 err = ext4_journal_get_write_access(handle, bitmap_bh);
2945 if (err)
2946 goto out_err;
2947
2948 err = -EIO;
2949 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2950 if (!gdp)
2951 goto out_err;
2952
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002953 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002954 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002955
liang xie5d601252014-05-12 22:06:43 -04002956 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002957 err = ext4_journal_get_write_access(handle, gdp_bh);
2958 if (err)
2959 goto out_err;
2960
Akinobu Mitabda00de2010-03-03 23:53:25 -05002961 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002962
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002963 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002964 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002965 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002966 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002967 /* File system mounted not to panic on error
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002968 * Fix the bitmap and return EFSCORRUPTED
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002969 * We leak some of the blocks here.
2970 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002971 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002972 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2973 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002974 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002975 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002976 if (!err)
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002977 err = -EFSCORRUPTED;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002978 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002979 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002980
2981 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002982#ifdef AGGRESSIVE_CHECK
2983 {
2984 int i;
2985 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2986 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2987 bitmap_bh->b_data));
2988 }
2989 }
2990#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002991 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2992 ac->ac_b_ex.fe_len);
Theodore Ts'o5ae57322018-06-14 00:58:00 -04002993 if (ext4_has_group_desc_csum(sb) &&
2994 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002995 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002996 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002997 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002998 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002999 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003000 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
3001 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04003002 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003003 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003004
3005 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04003006 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003007 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003008 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003009 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003010 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3011 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04003012 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
3013 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05003014
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003015 if (sbi->s_log_groups_per_flex) {
3016 ext4_group_t flex_group = ext4_flex_group(sbi,
3017 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04003018 atomic64_sub(ac->ac_b_ex.fe_len,
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08003019 &sbi_array_rcu_deref(sbi, s_flex_groups,
3020 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003021 }
3022
Frank Mayhar03901312009-01-07 00:06:22 -05003023 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003024 if (err)
3025 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003026 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003027
3028out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003029 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003030 return err;
3031}
3032
3033/*
3034 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003035 * Group request are normalized to s_mb_group_prealloc, which goes to
3036 * s_strip if we set the same via mount option.
3037 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003038 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003039 *
3040 * XXX: should we try to preallocate more than the group has now?
3041 */
3042static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3043{
3044 struct super_block *sb = ac->ac_sb;
3045 struct ext4_locality_group *lg = ac->ac_lg;
3046
3047 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003048 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003049 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003050 current->pid, ac->ac_g_ex.fe_len);
3051}
3052
3053/*
3054 * Normalization means making request better in terms of
3055 * size and alignment
3056 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003057static noinline_for_stack void
3058ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003059 struct ext4_allocation_request *ar)
3060{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003061 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003062 int bsbits, max;
3063 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003064 loff_t size, start_off;
3065 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003066 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003067 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003068 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003069
3070 /* do normalize only data requests, metadata requests
3071 do not need preallocation */
3072 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3073 return;
3074
3075 /* sometime caller may want exact blocks */
3076 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3077 return;
3078
3079 /* caller may indicate that preallocation isn't
3080 * required (it's a tail, for example) */
3081 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3082 return;
3083
3084 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3085 ext4_mb_normalize_group_request(ac);
3086 return ;
3087 }
3088
3089 bsbits = ac->ac_sb->s_blocksize_bits;
3090
3091 /* first, let's learn actual file size
3092 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003093 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003094 size = size << bsbits;
3095 if (size < i_size_read(ac->ac_inode))
3096 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003097 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003098
Valerie Clement19304792008-05-13 19:31:14 -04003099 /* max size of free chunks */
3100 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003101
Valerie Clement19304792008-05-13 19:31:14 -04003102#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3103 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003104
3105 /* first, try to predict filesize */
3106 /* XXX: should this table be tunable? */
3107 start_off = 0;
3108 if (size <= 16 * 1024) {
3109 size = 16 * 1024;
3110 } else if (size <= 32 * 1024) {
3111 size = 32 * 1024;
3112 } else if (size <= 64 * 1024) {
3113 size = 64 * 1024;
3114 } else if (size <= 128 * 1024) {
3115 size = 128 * 1024;
3116 } else if (size <= 256 * 1024) {
3117 size = 256 * 1024;
3118 } else if (size <= 512 * 1024) {
3119 size = 512 * 1024;
3120 } else if (size <= 1024 * 1024) {
3121 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003122 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003123 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003124 (21 - bsbits)) << 21;
3125 size = 2 * 1024 * 1024;
3126 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003127 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3128 (22 - bsbits)) << 22;
3129 size = 4 * 1024 * 1024;
3130 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003131 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003132 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3133 (23 - bsbits)) << 23;
3134 size = 8 * 1024 * 1024;
3135 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003136 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3137 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3138 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003139 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003140 size = size >> bsbits;
3141 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003142
3143 /* don't cover already allocated blocks in selected range */
3144 if (ar->pleft && start <= ar->lleft) {
3145 size -= ar->lleft + 1 - start;
3146 start = ar->lleft + 1;
3147 }
3148 if (ar->pright && start + size - 1 >= ar->lright)
3149 size -= start + size - ar->lright;
3150
Jan Karafc6c2da2017-01-27 14:34:30 -05003151 /*
3152 * Trim allocation request for filesystems with artificially small
3153 * groups.
3154 */
3155 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
3156 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
3157
Alex Tomasc9de5602008-01-29 00:19:52 -05003158 end = start + size;
3159
3160 /* check we don't cross already preallocated blocks */
3161 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003162 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003163 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003164
Alex Tomasc9de5602008-01-29 00:19:52 -05003165 if (pa->pa_deleted)
3166 continue;
3167 spin_lock(&pa->pa_lock);
3168 if (pa->pa_deleted) {
3169 spin_unlock(&pa->pa_lock);
3170 continue;
3171 }
3172
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003173 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3174 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003175
3176 /* PA must not overlap original request */
3177 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3178 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3179
Eric Sandeen38877f42009-08-17 23:55:24 -04003180 /* skip PAs this normalized request doesn't overlap with */
3181 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003182 spin_unlock(&pa->pa_lock);
3183 continue;
3184 }
3185 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3186
Eric Sandeen38877f42009-08-17 23:55:24 -04003187 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003188 if (pa_end <= ac->ac_o_ex.fe_logical) {
3189 BUG_ON(pa_end < start);
3190 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003191 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003192 BUG_ON(pa->pa_lstart > end);
3193 end = pa->pa_lstart;
3194 }
3195 spin_unlock(&pa->pa_lock);
3196 }
3197 rcu_read_unlock();
3198 size = end - start;
3199
3200 /* XXX: extra loop to check we really don't overlap preallocations */
3201 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003202 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003203 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003204
Alex Tomasc9de5602008-01-29 00:19:52 -05003205 spin_lock(&pa->pa_lock);
3206 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003207 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3208 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003209 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3210 }
3211 spin_unlock(&pa->pa_lock);
3212 }
3213 rcu_read_unlock();
3214
3215 if (start + size <= ac->ac_o_ex.fe_logical &&
3216 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003217 ext4_msg(ac->ac_sb, KERN_ERR,
3218 "start %lu, size %lu, fe_logical %lu",
3219 (unsigned long) start, (unsigned long) size,
3220 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003221 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003222 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003223 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003224
3225 /* now prepare goal request */
3226
3227 /* XXX: is it better to align blocks WRT to logical
3228 * placement or satisfy big request as is */
3229 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003230 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003231
3232 /* define goal start in order to merge */
3233 if (ar->pright && (ar->lright == (start + size))) {
3234 /* merge to the right */
3235 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3236 &ac->ac_f_ex.fe_group,
3237 &ac->ac_f_ex.fe_start);
3238 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3239 }
3240 if (ar->pleft && (ar->lleft + 1 == start)) {
3241 /* merge to the left */
3242 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3243 &ac->ac_f_ex.fe_group,
3244 &ac->ac_f_ex.fe_start);
3245 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3246 }
3247
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003248 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003249 (unsigned) orig_size, (unsigned) start);
3250}
3251
3252static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3253{
3254 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3255
3256 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3257 atomic_inc(&sbi->s_bal_reqs);
3258 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003259 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003260 atomic_inc(&sbi->s_bal_success);
3261 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3262 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3263 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3264 atomic_inc(&sbi->s_bal_goals);
3265 if (ac->ac_found > sbi->s_mb_max_to_scan)
3266 atomic_inc(&sbi->s_bal_breaks);
3267 }
3268
Theodore Ts'o296c3552009-09-30 00:32:42 -04003269 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3270 trace_ext4_mballoc_alloc(ac);
3271 else
3272 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003273}
3274
3275/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003276 * Called on failure; free up any blocks from the inode PA for this
3277 * context. We don't need this for MB_GROUP_PA because we only change
3278 * pa_free in ext4_mb_release_context(), but on failure, we've already
3279 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3280 */
3281static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3282{
3283 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003284 struct ext4_buddy e4b;
3285 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003286
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003287 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003288 if (ac->ac_f_ex.fe_len == 0)
3289 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003290 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3291 if (err) {
3292 /*
3293 * This should never happen since we pin the
3294 * pages in the ext4_allocation_context so
3295 * ext4_mb_load_buddy() should never fail.
3296 */
3297 WARN(1, "mb_load_buddy failed (%d)", err);
3298 return;
3299 }
3300 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3301 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3302 ac->ac_f_ex.fe_len);
3303 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003304 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003305 return;
3306 }
3307 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003308 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003309}
3310
3311/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003312 * use blocks preallocated to inode
3313 */
3314static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3315 struct ext4_prealloc_space *pa)
3316{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003317 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003318 ext4_fsblk_t start;
3319 ext4_fsblk_t end;
3320 int len;
3321
3322 /* found preallocated blocks, use them */
3323 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003324 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3325 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3326 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003327 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3328 &ac->ac_b_ex.fe_start);
3329 ac->ac_b_ex.fe_len = len;
3330 ac->ac_status = AC_STATUS_FOUND;
3331 ac->ac_pa = pa;
3332
3333 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003334 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003335 BUG_ON(pa->pa_free < len);
3336 pa->pa_free -= len;
3337
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003338 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003339}
3340
3341/*
3342 * use blocks preallocated to locality group
3343 */
3344static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3345 struct ext4_prealloc_space *pa)
3346{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003347 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003348
Alex Tomasc9de5602008-01-29 00:19:52 -05003349 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3350 &ac->ac_b_ex.fe_group,
3351 &ac->ac_b_ex.fe_start);
3352 ac->ac_b_ex.fe_len = len;
3353 ac->ac_status = AC_STATUS_FOUND;
3354 ac->ac_pa = pa;
3355
3356 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003357 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003358 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003359 * in on-disk bitmap -- see ext4_mb_release_context()
3360 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003361 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003362 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003363}
3364
3365/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003366 * Return the prealloc space that have minimal distance
3367 * from the goal block. @cpa is the prealloc
3368 * space that is having currently known minimal distance
3369 * from the goal block.
3370 */
3371static struct ext4_prealloc_space *
3372ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3373 struct ext4_prealloc_space *pa,
3374 struct ext4_prealloc_space *cpa)
3375{
3376 ext4_fsblk_t cur_distance, new_distance;
3377
3378 if (cpa == NULL) {
3379 atomic_inc(&pa->pa_count);
3380 return pa;
3381 }
Andrew Morton79211c82015-11-09 14:58:13 -08003382 cur_distance = abs(goal_block - cpa->pa_pstart);
3383 new_distance = abs(goal_block - pa->pa_pstart);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003384
Coly Li5a54b2f2011-02-24 14:10:05 -05003385 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003386 return cpa;
3387
3388 /* drop the previous reference */
3389 atomic_dec(&cpa->pa_count);
3390 atomic_inc(&pa->pa_count);
3391 return pa;
3392}
3393
3394/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003395 * search goal blocks in preallocated space
3396 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003397static noinline_for_stack int
3398ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003399{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003400 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003401 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003402 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3403 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003404 struct ext4_prealloc_space *pa, *cpa = NULL;
3405 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003406
3407 /* only data can be preallocated */
3408 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3409 return 0;
3410
3411 /* first, try per-file preallocation */
3412 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003413 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003414
3415 /* all fields in this condition don't change,
3416 * so we can skip locking for them */
3417 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003418 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3419 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003420 continue;
3421
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003422 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003423 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003424 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3425 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003426 continue;
3427
Alex Tomasc9de5602008-01-29 00:19:52 -05003428 /* found preallocated blocks, use them */
3429 spin_lock(&pa->pa_lock);
3430 if (pa->pa_deleted == 0 && pa->pa_free) {
3431 atomic_inc(&pa->pa_count);
3432 ext4_mb_use_inode_pa(ac, pa);
3433 spin_unlock(&pa->pa_lock);
3434 ac->ac_criteria = 10;
3435 rcu_read_unlock();
3436 return 1;
3437 }
3438 spin_unlock(&pa->pa_lock);
3439 }
3440 rcu_read_unlock();
3441
3442 /* can we use group allocation? */
3443 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3444 return 0;
3445
3446 /* inode may have no locality group for some reason */
3447 lg = ac->ac_lg;
3448 if (lg == NULL)
3449 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003450 order = fls(ac->ac_o_ex.fe_len) - 1;
3451 if (order > PREALLOC_TB_SIZE - 1)
3452 /* The max size of hash table is PREALLOC_TB_SIZE */
3453 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003454
Akinobu Mitabda00de2010-03-03 23:53:25 -05003455 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003456 /*
3457 * search for the prealloc space that is having
3458 * minimal distance from the goal block.
3459 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003460 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3461 rcu_read_lock();
3462 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3463 pa_inode_list) {
3464 spin_lock(&pa->pa_lock);
3465 if (pa->pa_deleted == 0 &&
3466 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003467
3468 cpa = ext4_mb_check_group_pa(goal_block,
3469 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003470 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003471 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003472 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003473 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003474 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003475 if (cpa) {
3476 ext4_mb_use_group_pa(ac, cpa);
3477 ac->ac_criteria = 20;
3478 return 1;
3479 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003480 return 0;
3481}
3482
3483/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003484 * the function goes through all block freed in the group
3485 * but not yet committed and marks them used in in-core bitmap.
3486 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003487 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003488 */
3489static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3490 ext4_group_t group)
3491{
3492 struct rb_node *n;
3493 struct ext4_group_info *grp;
3494 struct ext4_free_data *entry;
3495
3496 grp = ext4_get_group_info(sb, group);
3497 n = rb_first(&(grp->bb_free_root));
3498
3499 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003500 entry = rb_entry(n, struct ext4_free_data, efd_node);
3501 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003502 n = rb_next(n);
3503 }
3504 return;
3505}
3506
3507/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003508 * the function goes through all preallocation in this group and marks them
3509 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003510 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003511 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003512static noinline_for_stack
3513void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003514 ext4_group_t group)
3515{
3516 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3517 struct ext4_prealloc_space *pa;
3518 struct list_head *cur;
3519 ext4_group_t groupnr;
3520 ext4_grpblk_t start;
3521 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003522 int len;
3523
3524 /* all form of preallocation discards first load group,
3525 * so the only competing code is preallocation use.
3526 * we don't need any locking here
3527 * notice we do NOT ignore preallocations with pa_deleted
3528 * otherwise we could leave used blocks available for
3529 * allocation in buddy when concurrent ext4_mb_put_pa()
3530 * is dropping preallocation
3531 */
3532 list_for_each(cur, &grp->bb_prealloc_list) {
3533 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3534 spin_lock(&pa->pa_lock);
3535 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3536 &groupnr, &start);
3537 len = pa->pa_len;
3538 spin_unlock(&pa->pa_lock);
3539 if (unlikely(len == 0))
3540 continue;
3541 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003542 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003543 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003544 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003545 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003546}
3547
3548static void ext4_mb_pa_callback(struct rcu_head *head)
3549{
3550 struct ext4_prealloc_space *pa;
3551 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003552
3553 BUG_ON(atomic_read(&pa->pa_count));
3554 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003555 kmem_cache_free(ext4_pspace_cachep, pa);
3556}
3557
3558/*
3559 * drops a reference to preallocated space descriptor
3560 * if this was the last reference and the space is consumed
3561 */
3562static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3563 struct super_block *sb, struct ext4_prealloc_space *pa)
3564{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003565 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003566 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003567
Alex Tomasc9de5602008-01-29 00:19:52 -05003568 /* in this short window concurrent discard can set pa_deleted */
3569 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003570 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3571 spin_unlock(&pa->pa_lock);
3572 return;
3573 }
3574
Alex Tomasc9de5602008-01-29 00:19:52 -05003575 if (pa->pa_deleted == 1) {
3576 spin_unlock(&pa->pa_lock);
3577 return;
3578 }
3579
3580 pa->pa_deleted = 1;
3581 spin_unlock(&pa->pa_lock);
3582
Eric Sandeend33a1972009-03-16 23:25:40 -04003583 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003584 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003585 * If doing group-based preallocation, pa_pstart may be in the
3586 * next group when pa is used up
3587 */
3588 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003589 grp_blk--;
3590
Lukas Czernerbd862982013-04-03 23:32:34 -04003591 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003592
3593 /*
3594 * possible race:
3595 *
3596 * P1 (buddy init) P2 (regular allocation)
3597 * find block B in PA
3598 * copy on-disk bitmap to buddy
3599 * mark B in on-disk bitmap
3600 * drop PA from group
3601 * mark all PAs in buddy
3602 *
3603 * thus, P1 initializes buddy with B available. to prevent this
3604 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3605 * against that pair
3606 */
3607 ext4_lock_group(sb, grp);
3608 list_del(&pa->pa_group_list);
3609 ext4_unlock_group(sb, grp);
3610
3611 spin_lock(pa->pa_obj_lock);
3612 list_del_rcu(&pa->pa_inode_list);
3613 spin_unlock(pa->pa_obj_lock);
3614
3615 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3616}
3617
3618/*
3619 * creates new preallocated space for given inode
3620 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003621static noinline_for_stack int
3622ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003623{
3624 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003625 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003626 struct ext4_prealloc_space *pa;
3627 struct ext4_group_info *grp;
3628 struct ext4_inode_info *ei;
3629
3630 /* preallocate only when found space is larger then requested */
3631 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3632 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3633 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3634
3635 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3636 if (pa == NULL)
3637 return -ENOMEM;
3638
3639 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3640 int winl;
3641 int wins;
3642 int win;
3643 int offs;
3644
3645 /* we can't allocate as much as normalizer wants.
3646 * so, found space must get proper lstart
3647 * to cover original request */
3648 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3649 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3650
3651 /* we're limited by original request in that
3652 * logical block must be covered any way
3653 * winl is window we can move our chunk within */
3654 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3655
3656 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003657 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003658
3659 /* the smallest one defines real window */
3660 win = min(winl, wins);
3661
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003662 offs = ac->ac_o_ex.fe_logical %
3663 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003664 if (offs && offs < win)
3665 win = offs;
3666
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003667 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003668 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003669 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3670 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3671 }
3672
3673 /* preallocation can change ac_b_ex, thus we store actually
3674 * allocated blocks for history */
3675 ac->ac_f_ex = ac->ac_b_ex;
3676
3677 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3678 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3679 pa->pa_len = ac->ac_b_ex.fe_len;
3680 pa->pa_free = pa->pa_len;
3681 atomic_set(&pa->pa_count, 1);
3682 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003683 INIT_LIST_HEAD(&pa->pa_inode_list);
3684 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003685 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003686 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003687
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003688 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003689 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003690 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003691
3692 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003693 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003694
3695 ei = EXT4_I(ac->ac_inode);
3696 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3697
3698 pa->pa_obj_lock = &ei->i_prealloc_lock;
3699 pa->pa_inode = ac->ac_inode;
3700
3701 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3702 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3703 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3704
3705 spin_lock(pa->pa_obj_lock);
3706 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3707 spin_unlock(pa->pa_obj_lock);
3708
3709 return 0;
3710}
3711
3712/*
3713 * creates new preallocated space for locality group inodes belongs to
3714 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003715static noinline_for_stack int
3716ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003717{
3718 struct super_block *sb = ac->ac_sb;
3719 struct ext4_locality_group *lg;
3720 struct ext4_prealloc_space *pa;
3721 struct ext4_group_info *grp;
3722
3723 /* preallocate only when found space is larger then requested */
3724 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3725 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3726 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3727
3728 BUG_ON(ext4_pspace_cachep == NULL);
3729 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3730 if (pa == NULL)
3731 return -ENOMEM;
3732
3733 /* preallocation can change ac_b_ex, thus we store actually
3734 * allocated blocks for history */
3735 ac->ac_f_ex = ac->ac_b_ex;
3736
3737 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3738 pa->pa_lstart = pa->pa_pstart;
3739 pa->pa_len = ac->ac_b_ex.fe_len;
3740 pa->pa_free = pa->pa_len;
3741 atomic_set(&pa->pa_count, 1);
3742 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003743 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003744 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003745 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003746 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003747
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003748 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003749 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3750 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003751
3752 ext4_mb_use_group_pa(ac, pa);
3753 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3754
3755 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3756 lg = ac->ac_lg;
3757 BUG_ON(lg == NULL);
3758
3759 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3760 pa->pa_inode = NULL;
3761
3762 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3763 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3764 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3765
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003766 /*
3767 * We will later add the new pa to the right bucket
3768 * after updating the pa_free in ext4_mb_release_context
3769 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003770 return 0;
3771}
3772
3773static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3774{
3775 int err;
3776
3777 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3778 err = ext4_mb_new_group_pa(ac);
3779 else
3780 err = ext4_mb_new_inode_pa(ac);
3781 return err;
3782}
3783
3784/*
3785 * finds all unused blocks in on-disk bitmap, frees them in
3786 * in-core bitmap and buddy.
3787 * @pa must be unlinked from inode and group lists, so that
3788 * nobody else can find/use it.
3789 * the caller MUST hold group/inode locks.
3790 * TODO: optimize the case when there are no in-core structures yet
3791 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003792static noinline_for_stack int
3793ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003794 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003795{
Alex Tomasc9de5602008-01-29 00:19:52 -05003796 struct super_block *sb = e4b->bd_sb;
3797 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003798 unsigned int end;
3799 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003800 ext4_group_t group;
3801 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003802 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003803 int err = 0;
3804 int free = 0;
3805
3806 BUG_ON(pa->pa_deleted == 0);
3807 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003808 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003809 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3810 end = bit + pa->pa_len;
3811
Alex Tomasc9de5602008-01-29 00:19:52 -05003812 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003813 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003814 if (bit >= end)
3815 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003816 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003817 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003818 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3819 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003820 free += next - bit;
3821
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003822 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003823 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3824 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003825 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003826 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3827 bit = next + 1;
3828 }
3829 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003830 ext4_msg(e4b->bd_sb, KERN_CRIT,
3831 "pa %p: logic %lu, phys. %lu, len %lu",
3832 pa, (unsigned long) pa->pa_lstart,
3833 (unsigned long) pa->pa_pstart,
3834 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003835 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003836 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003837 /*
3838 * pa is already deleted so we use the value obtained
3839 * from the bitmap and continue.
3840 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003841 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003842 atomic_add(free, &sbi->s_mb_discarded);
3843
3844 return err;
3845}
3846
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003847static noinline_for_stack int
3848ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003849 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003850{
Alex Tomasc9de5602008-01-29 00:19:52 -05003851 struct super_block *sb = e4b->bd_sb;
3852 ext4_group_t group;
3853 ext4_grpblk_t bit;
3854
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003855 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003856 BUG_ON(pa->pa_deleted == 0);
3857 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3858 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3859 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3860 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003861 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003862
3863 return 0;
3864}
3865
3866/*
3867 * releases all preallocations in given group
3868 *
3869 * first, we need to decide discard policy:
3870 * - when do we discard
3871 * 1) ENOSPC
3872 * - how many do we discard
3873 * 1) how many requested
3874 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003875static noinline_for_stack int
3876ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003877 ext4_group_t group, int needed)
3878{
3879 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3880 struct buffer_head *bitmap_bh = NULL;
3881 struct ext4_prealloc_space *pa, *tmp;
3882 struct list_head list;
3883 struct ext4_buddy e4b;
3884 int err;
3885 int busy = 0;
3886 int free = 0;
3887
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003888 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003889
3890 if (list_empty(&grp->bb_prealloc_list))
3891 return 0;
3892
Theodore Ts'o574ca172008-07-11 19:27:31 -04003893 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04003894 if (IS_ERR(bitmap_bh)) {
3895 err = PTR_ERR(bitmap_bh);
3896 ext4_error(sb, "Error %d reading block bitmap for %u",
3897 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003898 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003899 }
3900
3901 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003902 if (err) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04003903 ext4_warning(sb, "Error %d loading buddy information for %u",
3904 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003905 put_bh(bitmap_bh);
3906 return 0;
3907 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003908
3909 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003910 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003911
Alex Tomasc9de5602008-01-29 00:19:52 -05003912 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003913repeat:
3914 ext4_lock_group(sb, group);
3915 list_for_each_entry_safe(pa, tmp,
3916 &grp->bb_prealloc_list, pa_group_list) {
3917 spin_lock(&pa->pa_lock);
3918 if (atomic_read(&pa->pa_count)) {
3919 spin_unlock(&pa->pa_lock);
3920 busy = 1;
3921 continue;
3922 }
3923 if (pa->pa_deleted) {
3924 spin_unlock(&pa->pa_lock);
3925 continue;
3926 }
3927
3928 /* seems this one can be freed ... */
3929 pa->pa_deleted = 1;
3930
3931 /* we can trust pa_free ... */
3932 free += pa->pa_free;
3933
3934 spin_unlock(&pa->pa_lock);
3935
3936 list_del(&pa->pa_group_list);
3937 list_add(&pa->u.pa_tmp_list, &list);
3938 }
3939
3940 /* if we still need more blocks and some PAs were used, try again */
3941 if (free < needed && busy) {
3942 busy = 0;
3943 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003944 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003945 goto repeat;
3946 }
3947
3948 /* found anything to free? */
3949 if (list_empty(&list)) {
3950 BUG_ON(free != 0);
3951 goto out;
3952 }
3953
3954 /* now free all selected PAs */
3955 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3956
3957 /* remove from object (inode or locality group) */
3958 spin_lock(pa->pa_obj_lock);
3959 list_del_rcu(&pa->pa_inode_list);
3960 spin_unlock(pa->pa_obj_lock);
3961
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003962 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003963 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003964 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003965 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003966
3967 list_del(&pa->u.pa_tmp_list);
3968 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3969 }
3970
3971out:
3972 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003973 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003974 put_bh(bitmap_bh);
3975 return free;
3976}
3977
3978/*
3979 * releases all non-used preallocated blocks for given inode
3980 *
3981 * It's important to discard preallocations under i_data_sem
3982 * We don't want another block to be served from the prealloc
3983 * space when we are discarding the inode prealloc space.
3984 *
3985 * FIXME!! Make sure it is valid at all the call sites
3986 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003987void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003988{
3989 struct ext4_inode_info *ei = EXT4_I(inode);
3990 struct super_block *sb = inode->i_sb;
3991 struct buffer_head *bitmap_bh = NULL;
3992 struct ext4_prealloc_space *pa, *tmp;
3993 ext4_group_t group = 0;
3994 struct list_head list;
3995 struct ext4_buddy e4b;
3996 int err;
3997
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003998 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003999 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4000 return;
4001 }
4002
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004003 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004004 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004005
4006 INIT_LIST_HEAD(&list);
4007
4008repeat:
4009 /* first, collect all pa's in the inode */
4010 spin_lock(&ei->i_prealloc_lock);
4011 while (!list_empty(&ei->i_prealloc_list)) {
4012 pa = list_entry(ei->i_prealloc_list.next,
4013 struct ext4_prealloc_space, pa_inode_list);
4014 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4015 spin_lock(&pa->pa_lock);
4016 if (atomic_read(&pa->pa_count)) {
4017 /* this shouldn't happen often - nobody should
4018 * use preallocation while we're discarding it */
4019 spin_unlock(&pa->pa_lock);
4020 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004021 ext4_msg(sb, KERN_ERR,
4022 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05004023 WARN_ON(1);
4024 schedule_timeout_uninterruptible(HZ);
4025 goto repeat;
4026
4027 }
4028 if (pa->pa_deleted == 0) {
4029 pa->pa_deleted = 1;
4030 spin_unlock(&pa->pa_lock);
4031 list_del_rcu(&pa->pa_inode_list);
4032 list_add(&pa->u.pa_tmp_list, &list);
4033 continue;
4034 }
4035
4036 /* someone is deleting pa right now */
4037 spin_unlock(&pa->pa_lock);
4038 spin_unlock(&ei->i_prealloc_lock);
4039
4040 /* we have to wait here because pa_deleted
4041 * doesn't mean pa is already unlinked from
4042 * the list. as we might be called from
4043 * ->clear_inode() the inode will get freed
4044 * and concurrent thread which is unlinking
4045 * pa from inode's list may access already
4046 * freed memory, bad-bad-bad */
4047
4048 /* XXX: if this happens too often, we can
4049 * add a flag to force wait only in case
4050 * of ->clear_inode(), but not in case of
4051 * regular truncate */
4052 schedule_timeout_uninterruptible(HZ);
4053 goto repeat;
4054 }
4055 spin_unlock(&ei->i_prealloc_lock);
4056
4057 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004058 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04004059 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05004060
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004061 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4062 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004063 if (err) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004064 ext4_error(sb, "Error %d loading buddy information for %u",
4065 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004066 continue;
4067 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004068
Theodore Ts'o574ca172008-07-11 19:27:31 -04004069 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004070 if (IS_ERR(bitmap_bh)) {
4071 err = PTR_ERR(bitmap_bh);
4072 ext4_error(sb, "Error %d reading block bitmap for %u",
4073 err, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004074 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004075 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004076 }
4077
4078 ext4_lock_group(sb, group);
4079 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004080 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004081 ext4_unlock_group(sb, group);
4082
Jing Zhange39e07f2010-05-14 00:00:00 -04004083 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004084 put_bh(bitmap_bh);
4085
4086 list_del(&pa->u.pa_tmp_list);
4087 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4088 }
4089}
4090
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004091#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004092static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4093{
4094 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004095 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004096
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05004097 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05004098 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004099 return;
4100
Joe Perches7f6a11e2012-03-19 23:09:43 -04004101 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004102 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04004103 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004104 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004105 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004106 "goal %lu/%lu/%lu@%lu, "
4107 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004108 (unsigned long)ac->ac_o_ex.fe_group,
4109 (unsigned long)ac->ac_o_ex.fe_start,
4110 (unsigned long)ac->ac_o_ex.fe_len,
4111 (unsigned long)ac->ac_o_ex.fe_logical,
4112 (unsigned long)ac->ac_g_ex.fe_group,
4113 (unsigned long)ac->ac_g_ex.fe_start,
4114 (unsigned long)ac->ac_g_ex.fe_len,
4115 (unsigned long)ac->ac_g_ex.fe_logical,
4116 (unsigned long)ac->ac_b_ex.fe_group,
4117 (unsigned long)ac->ac_b_ex.fe_start,
4118 (unsigned long)ac->ac_b_ex.fe_len,
4119 (unsigned long)ac->ac_b_ex.fe_logical,
4120 (int)ac->ac_criteria);
Eric Sandeendc9ddd92014-02-20 13:32:10 -05004121 ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004122 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004123 ngroups = ext4_get_groups_count(sb);
4124 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004125 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4126 struct ext4_prealloc_space *pa;
4127 ext4_grpblk_t start;
4128 struct list_head *cur;
4129 ext4_lock_group(sb, i);
4130 list_for_each(cur, &grp->bb_prealloc_list) {
4131 pa = list_entry(cur, struct ext4_prealloc_space,
4132 pa_group_list);
4133 spin_lock(&pa->pa_lock);
4134 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4135 NULL, &start);
4136 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004137 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4138 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004139 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004140 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004141
4142 if (grp->bb_free == 0)
4143 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004144 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004145 i, grp->bb_free, grp->bb_fragments);
4146 }
4147 printk(KERN_ERR "\n");
4148}
4149#else
4150static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4151{
4152 return;
4153}
4154#endif
4155
4156/*
4157 * We use locality group preallocation for small size file. The size of the
4158 * file is determined by the current size or the resulting size after
4159 * allocation which ever is larger
4160 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004161 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004162 */
4163static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4164{
4165 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4166 int bsbits = ac->ac_sb->s_blocksize_bits;
4167 loff_t size, isize;
4168
4169 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4170 return;
4171
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004172 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4173 return;
4174
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004175 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004176 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4177 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004178
Theodore Ts'o50797482009-09-18 13:34:02 -04004179 if ((size == isize) &&
4180 !ext4_fs_is_busy(sbi) &&
4181 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4182 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4183 return;
4184 }
4185
Robin Dongebbe0272011-10-26 05:14:27 -04004186 if (sbi->s_mb_group_prealloc <= 0) {
4187 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4188 return;
4189 }
4190
Alex Tomasc9de5602008-01-29 00:19:52 -05004191 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004192 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004193 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004194 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004195 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004196 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004197
4198 BUG_ON(ac->ac_lg != NULL);
4199 /*
4200 * locality group prealloc space are per cpu. The reason for having
4201 * per cpu locality group is to reduce the contention between block
4202 * request from multiple CPUs.
4203 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004204 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004205
4206 /* we're going to use group allocation */
4207 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4208
4209 /* serialize all allocations in the group */
4210 mutex_lock(&ac->ac_lg->lg_mutex);
4211}
4212
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004213static noinline_for_stack int
4214ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004215 struct ext4_allocation_request *ar)
4216{
4217 struct super_block *sb = ar->inode->i_sb;
4218 struct ext4_sb_info *sbi = EXT4_SB(sb);
4219 struct ext4_super_block *es = sbi->s_es;
4220 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004221 unsigned int len;
4222 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004223 ext4_grpblk_t block;
4224
4225 /* we can't allocate > group size */
4226 len = ar->len;
4227
4228 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004229 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4230 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004231
4232 /* start searching from the goal */
4233 goal = ar->goal;
4234 if (goal < le32_to_cpu(es->s_first_data_block) ||
4235 goal >= ext4_blocks_count(es))
4236 goal = le32_to_cpu(es->s_first_data_block);
4237 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4238
4239 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004240 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004241 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004242 ac->ac_sb = sb;
4243 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004244 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004245 ac->ac_o_ex.fe_group = group;
4246 ac->ac_o_ex.fe_start = block;
4247 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004248 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004249 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004250
4251 /* we have to define context: we'll we work with a file or
4252 * locality group. this is a policy, actually */
4253 ext4_mb_group_or_file(ac);
4254
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004255 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004256 "left: %u/%u, right %u/%u to %swritable\n",
4257 (unsigned) ar->len, (unsigned) ar->logical,
4258 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4259 (unsigned) ar->lleft, (unsigned) ar->pleft,
4260 (unsigned) ar->lright, (unsigned) ar->pright,
4261 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4262 return 0;
4263
4264}
4265
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004266static noinline_for_stack void
4267ext4_mb_discard_lg_preallocations(struct super_block *sb,
4268 struct ext4_locality_group *lg,
4269 int order, int total_entries)
4270{
4271 ext4_group_t group = 0;
4272 struct ext4_buddy e4b;
4273 struct list_head discard_list;
4274 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004275
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004276 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004277
4278 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004279
4280 spin_lock(&lg->lg_prealloc_lock);
4281 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4282 pa_inode_list) {
4283 spin_lock(&pa->pa_lock);
4284 if (atomic_read(&pa->pa_count)) {
4285 /*
4286 * This is the pa that we just used
4287 * for block allocation. So don't
4288 * free that
4289 */
4290 spin_unlock(&pa->pa_lock);
4291 continue;
4292 }
4293 if (pa->pa_deleted) {
4294 spin_unlock(&pa->pa_lock);
4295 continue;
4296 }
4297 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004298 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004299
4300 /* seems this one can be freed ... */
4301 pa->pa_deleted = 1;
4302 spin_unlock(&pa->pa_lock);
4303
4304 list_del_rcu(&pa->pa_inode_list);
4305 list_add(&pa->u.pa_tmp_list, &discard_list);
4306
4307 total_entries--;
4308 if (total_entries <= 5) {
4309 /*
4310 * we want to keep only 5 entries
4311 * allowing it to grow to 8. This
4312 * mak sure we don't call discard
4313 * soon for this list.
4314 */
4315 break;
4316 }
4317 }
4318 spin_unlock(&lg->lg_prealloc_lock);
4319
4320 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004321 int err;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004322
Lukas Czernerbd862982013-04-03 23:32:34 -04004323 group = ext4_get_group_number(sb, pa->pa_pstart);
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004324 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4325 GFP_NOFS|__GFP_NOFAIL);
4326 if (err) {
4327 ext4_error(sb, "Error %d loading buddy information for %u",
4328 err, group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004329 continue;
4330 }
4331 ext4_lock_group(sb, group);
4332 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004333 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004334 ext4_unlock_group(sb, group);
4335
Jing Zhange39e07f2010-05-14 00:00:00 -04004336 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004337 list_del(&pa->u.pa_tmp_list);
4338 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4339 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004340}
4341
4342/*
4343 * We have incremented pa_count. So it cannot be freed at this
4344 * point. Also we hold lg_mutex. So no parallel allocation is
4345 * possible from this lg. That means pa_free cannot be updated.
4346 *
4347 * A parallel ext4_mb_discard_group_preallocations is possible.
4348 * which can cause the lg_prealloc_list to be updated.
4349 */
4350
4351static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4352{
4353 int order, added = 0, lg_prealloc_count = 1;
4354 struct super_block *sb = ac->ac_sb;
4355 struct ext4_locality_group *lg = ac->ac_lg;
4356 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4357
4358 order = fls(pa->pa_free) - 1;
4359 if (order > PREALLOC_TB_SIZE - 1)
4360 /* The max size of hash table is PREALLOC_TB_SIZE */
4361 order = PREALLOC_TB_SIZE - 1;
4362 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004363 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004364 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4365 pa_inode_list) {
4366 spin_lock(&tmp_pa->pa_lock);
4367 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004368 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004369 continue;
4370 }
4371 if (!added && pa->pa_free < tmp_pa->pa_free) {
4372 /* Add to the tail of the previous entry */
4373 list_add_tail_rcu(&pa->pa_inode_list,
4374 &tmp_pa->pa_inode_list);
4375 added = 1;
4376 /*
4377 * we want to count the total
4378 * number of entries in the list
4379 */
4380 }
4381 spin_unlock(&tmp_pa->pa_lock);
4382 lg_prealloc_count++;
4383 }
4384 if (!added)
4385 list_add_tail_rcu(&pa->pa_inode_list,
4386 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004387 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004388
4389 /* Now trim the list to be not more than 8 elements */
4390 if (lg_prealloc_count > 8) {
4391 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004392 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004393 return;
4394 }
4395 return ;
4396}
4397
Alex Tomasc9de5602008-01-29 00:19:52 -05004398/*
4399 * release all resource we used in allocation
4400 */
4401static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4402{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004403 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004404 struct ext4_prealloc_space *pa = ac->ac_pa;
4405 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004406 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004407 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004408 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004409 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4410 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004411 pa->pa_free -= ac->ac_b_ex.fe_len;
4412 pa->pa_len -= ac->ac_b_ex.fe_len;
4413 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004414 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004415 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004416 if (pa) {
4417 /*
4418 * We want to add the pa to the right bucket.
4419 * Remove it from the list and while adding
4420 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004421 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004422 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004423 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004424 spin_lock(pa->pa_obj_lock);
4425 list_del_rcu(&pa->pa_inode_list);
4426 spin_unlock(pa->pa_obj_lock);
4427 ext4_mb_add_n_trim(ac);
4428 }
4429 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4430 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004431 if (ac->ac_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004432 put_page(ac->ac_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004433 if (ac->ac_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004434 put_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004435 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4436 mutex_unlock(&ac->ac_lg->lg_mutex);
4437 ext4_mb_collect_stats(ac);
4438 return 0;
4439}
4440
4441static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4442{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004443 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004444 int ret;
4445 int freed = 0;
4446
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004447 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004448 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004449 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4450 freed += ret;
4451 needed -= ret;
4452 }
4453
4454 return freed;
4455}
4456
4457/*
4458 * Main entry point into mballoc to allocate blocks
4459 * it tries to use preallocation first, then falls back
4460 * to usual allocation
4461 */
4462ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004463 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004464{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004465 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004466 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004467 struct ext4_sb_info *sbi;
4468 struct super_block *sb;
4469 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004470 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004471 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004472
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004473 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004474 sb = ar->inode->i_sb;
4475 sbi = EXT4_SB(sb);
4476
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004477 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004478
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004479 /* Allow to use superuser reservation for quota file */
4480 if (IS_NOQUOTA(ar->inode))
4481 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4482
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004483 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004484 /* Without delayed allocation we need to verify
4485 * there is enough free blocks to do block allocation
4486 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004487 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004488 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004489 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004490
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004491 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004492 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004493 ar->len = ar->len >> 1;
4494 }
4495 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004496 *errp = -ENOSPC;
4497 return 0;
4498 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004499 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004500 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004501 dquot_alloc_block_nofail(ar->inode,
4502 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004503 } else {
4504 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004505 dquot_alloc_block(ar->inode,
4506 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004507
4508 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4509 ar->len--;
4510 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004511 }
4512 inquota = ar->len;
4513 if (ar->len == 0) {
4514 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004515 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004516 }
Mingming Caod2a17632008-07-14 17:52:37 -04004517 }
Mingming Caod2a17632008-07-14 17:52:37 -04004518
Wei Yongjun85556c92012-09-26 20:43:37 -04004519 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004520 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004521 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004522 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004523 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004524 }
4525
Eric Sandeen256bdb42008-02-10 01:13:33 -05004526 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004527 if (*errp) {
4528 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004529 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004530 }
4531
Eric Sandeen256bdb42008-02-10 01:13:33 -05004532 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4533 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004534 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4535 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004536repeat:
4537 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004538 *errp = ext4_mb_regular_allocator(ac);
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004539 if (*errp)
4540 goto discard_and_exit;
4541
4542 /* as we've just preallocated more space than
4543 * user requested originally, we store allocated
4544 * space in a special descriptor */
4545 if (ac->ac_status == AC_STATUS_FOUND &&
4546 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4547 *errp = ext4_mb_new_preallocation(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004548 if (*errp) {
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004549 discard_and_exit:
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004550 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004551 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004552 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004553 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004554 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004555 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Vegard Nossum554a5cc2016-07-14 23:02:47 -04004556 if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004557 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004558 goto errout;
4559 } else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004560 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4561 ar->len = ac->ac_b_ex.fe_len;
4562 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004563 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004564 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004565 if (freed)
4566 goto repeat;
4567 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004568 }
4569
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004570errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004571 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004572 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004573 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004574 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004575 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004576 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004577out:
4578 if (ac)
4579 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004580 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004581 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004582 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004583 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004584 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004585 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004586 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004587 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004588
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004589 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004590
Alex Tomasc9de5602008-01-29 00:19:52 -05004591 return block;
4592}
Alex Tomasc9de5602008-01-29 00:19:52 -05004593
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004594/*
4595 * We can merge two free data extents only if the physical blocks
4596 * are contiguous, AND the extents were freed by the same transaction,
4597 * AND the blocks are associated with the same group.
4598 */
4599static int can_merge(struct ext4_free_data *entry1,
4600 struct ext4_free_data *entry2)
4601{
Bobi Jam18aadd42012-02-20 17:53:02 -05004602 if ((entry1->efd_tid == entry2->efd_tid) &&
4603 (entry1->efd_group == entry2->efd_group) &&
4604 ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004605 return 1;
4606 return 0;
4607}
4608
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004609static noinline_for_stack int
4610ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004611 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004612{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004613 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004614 ext4_grpblk_t cluster;
Theodore Ts'od08854f2016-06-26 18:24:01 -04004615 ext4_grpblk_t clusters = new_entry->efd_count;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004616 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004617 struct ext4_group_info *db = e4b->bd_info;
4618 struct super_block *sb = e4b->bd_sb;
4619 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004620 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4621 struct rb_node *parent = NULL, *new_node;
4622
Frank Mayhar03901312009-01-07 00:06:22 -05004623 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004624 BUG_ON(e4b->bd_bitmap_page == NULL);
4625 BUG_ON(e4b->bd_buddy_page == NULL);
4626
Bobi Jam18aadd42012-02-20 17:53:02 -05004627 new_node = &new_entry->efd_node;
4628 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004629
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004630 if (!*n) {
4631 /* first free block exent. We need to
4632 protect buddy cache from being freed,
4633 * otherwise we'll refresh it from
4634 * on-disk bitmap and lose not-yet-available
4635 * blocks */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004636 get_page(e4b->bd_buddy_page);
4637 get_page(e4b->bd_bitmap_page);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004638 }
4639 while (*n) {
4640 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004641 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4642 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004643 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004644 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004645 n = &(*n)->rb_right;
4646 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004647 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004648 ext4_group_first_block_no(sb, group) +
4649 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004650 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004651 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004652 }
4653 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004654
4655 rb_link_node(new_node, parent, n);
4656 rb_insert_color(new_node, &db->bb_free_root);
4657
4658 /* Now try to see the extent can be merged to left and right */
4659 node = rb_prev(new_node);
4660 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004661 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004662 if (can_merge(entry, new_entry) &&
4663 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004664 new_entry->efd_start_cluster = entry->efd_start_cluster;
4665 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004666 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004667 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004668 }
4669 }
4670
4671 node = rb_next(new_node);
4672 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004673 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004674 if (can_merge(new_entry, entry) &&
4675 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004676 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004677 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004678 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004679 }
4680 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004681 /* Add the extent to transaction's private list */
Theodore Ts'od08854f2016-06-26 18:24:01 -04004682 new_entry->efd_jce.jce_func = ext4_free_data_callback;
4683 spin_lock(&sbi->s_md_lock);
4684 _ext4_journal_callback_add(handle, &new_entry->efd_jce);
4685 sbi->s_mb_free_pending += clusters;
4686 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004687 return 0;
4688}
4689
Theodore Ts'o44338712009-11-22 07:44:56 -05004690/**
4691 * ext4_free_blocks() -- Free given blocks and update quota
4692 * @handle: handle for this transaction
4693 * @inode: inode
4694 * @block: start physical block to free
4695 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004696 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004697 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004698void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004699 struct buffer_head *bh, ext4_fsblk_t block,
4700 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004701{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004702 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004703 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004704 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004705 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004706 ext4_grpblk_t bit;
4707 struct buffer_head *gd_bh;
4708 ext4_group_t block_group;
4709 struct ext4_sb_info *sbi;
4710 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004711 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004712 int err = 0;
4713 int ret;
4714
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004715 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004716 if (bh) {
4717 if (block)
4718 BUG_ON(block != bh->b_blocknr);
4719 else
4720 block = bh->b_blocknr;
4721 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004722
Alex Tomasc9de5602008-01-29 00:19:52 -05004723 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004724 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4725 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004726 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004727 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004728 goto error_return;
4729 }
4730
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004731 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004732 trace_ext4_free_blocks(inode, block, count, flags);
4733
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004734 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4735 BUG_ON(count > 1);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004736
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004737 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4738 inode, bh, block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004739 }
4740
Theodore Ts'o60e66792010-05-17 07:00:00 -04004741 /*
Theodore Ts'o84130192011-09-09 18:50:51 -04004742 * If the extent to be freed does not begin on a cluster
4743 * boundary, we need to deal with partial clusters at the
4744 * beginning and end of the extent. Normally we will free
4745 * blocks at the beginning or the end unless we are explicitly
4746 * requested to avoid doing so.
4747 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004748 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004749 if (overflow) {
4750 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4751 overflow = sbi->s_cluster_ratio - overflow;
4752 block += overflow;
4753 if (count > overflow)
4754 count -= overflow;
4755 else
4756 return;
4757 } else {
4758 block -= overflow;
4759 count += overflow;
4760 }
4761 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004762 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004763 if (overflow) {
4764 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4765 if (count > overflow)
4766 count -= overflow;
4767 else
4768 return;
4769 } else
4770 count += sbi->s_cluster_ratio - overflow;
4771 }
4772
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004773 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4774 int i;
Daeho Jeongf96c4502016-02-21 18:31:41 -05004775 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004776
4777 for (i = 0; i < count; i++) {
4778 cond_resched();
Daeho Jeongf96c4502016-02-21 18:31:41 -05004779 if (is_metadata)
4780 bh = sb_find_get_block(inode->i_sb, block + i);
4781 ext4_forget(handle, is_metadata, inode, bh, block + i);
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004782 }
4783 }
4784
Alex Tomasc9de5602008-01-29 00:19:52 -05004785do_more:
4786 overflow = 0;
4787 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4788
Darrick J. Wong163a2032013-08-28 17:35:51 -04004789 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4790 ext4_get_group_info(sb, block_group))))
4791 return;
4792
Alex Tomasc9de5602008-01-29 00:19:52 -05004793 /*
4794 * Check to see if we are freeing blocks across a group
4795 * boundary.
4796 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004797 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4798 overflow = EXT4_C2B(sbi, bit) + count -
4799 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004800 count -= overflow;
4801 }
Lukas Czerner810da242013-03-02 17:18:58 -05004802 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004803 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004804 if (IS_ERR(bitmap_bh)) {
4805 err = PTR_ERR(bitmap_bh);
4806 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004807 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004808 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004809 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004810 if (!gdp) {
4811 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004812 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004813 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004814
4815 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4816 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4817 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004818 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004819 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004820 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004821
Eric Sandeen12062dd2010-02-15 14:19:27 -05004822 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004823 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004824 /* err = 0. ext4_std_error should be a no op */
4825 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004826 }
4827
4828 BUFFER_TRACE(bitmap_bh, "getting write access");
4829 err = ext4_journal_get_write_access(handle, bitmap_bh);
4830 if (err)
4831 goto error_return;
4832
4833 /*
4834 * We are about to modify some metadata. Call the journal APIs
4835 * to unshare ->b_data if a currently-committing transaction is
4836 * using it
4837 */
4838 BUFFER_TRACE(gd_bh, "get_write_access");
4839 err = ext4_journal_get_write_access(handle, gd_bh);
4840 if (err)
4841 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004842#ifdef AGGRESSIVE_CHECK
4843 {
4844 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004845 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004846 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4847 }
4848#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004849 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004850
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04004851 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
4852 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
4853 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004854 if (err)
4855 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004856
Daeho Jeongf96c4502016-02-21 18:31:41 -05004857 /*
4858 * We need to make sure we don't reuse the freed block until after the
4859 * transaction is committed. We make an exception if the inode is to be
4860 * written in writeback mode since writeback mode has weak data
4861 * consistency guarantees.
4862 */
4863 if (ext4_handle_valid(handle) &&
4864 ((flags & EXT4_FREE_BLOCKS_METADATA) ||
4865 !ext4_should_writeback_data(inode))) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004866 struct ext4_free_data *new_entry;
4867 /*
Michal Hocko7444a072015-07-05 12:33:44 -04004868 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4869 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004870 */
Michal Hocko7444a072015-07-05 12:33:44 -04004871 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
4872 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05004873 new_entry->efd_start_cluster = bit;
4874 new_entry->efd_group = block_group;
4875 new_entry->efd_count = count_clusters;
4876 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004877
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004878 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004879 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004880 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004881 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004882 /* need to update group_info->bb_free and bitmap
4883 * with group lock held. generate_buddy look at
4884 * them with group lock_held
4885 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004886 if (test_opt(sb, DISCARD)) {
4887 err = ext4_issue_discard(sb, block_group, bit, count);
4888 if (err && err != -EOPNOTSUPP)
4889 ext4_msg(sb, KERN_WARNING, "discard request in"
4890 " group:%d block:%d count:%lu failed"
4891 " with %d", block_group, bit, count,
4892 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04004893 } else
4894 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004895
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004896 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004897 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4898 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004899 }
4900
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004901 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4902 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004903 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004904 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004905 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004906
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004907 if (sbi->s_log_groups_per_flex) {
4908 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004909 atomic64_add(count_clusters,
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08004910 &sbi_array_rcu_deref(sbi, s_flex_groups,
4911 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004912 }
4913
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04004914 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004915 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
Jan Kara7d734532013-08-17 09:36:54 -04004916 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
4917
4918 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004919
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004920 /* We dirtied the bitmap block */
4921 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4922 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4923
Alex Tomasc9de5602008-01-29 00:19:52 -05004924 /* And the group descriptor block */
4925 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004926 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004927 if (!err)
4928 err = ret;
4929
4930 if (overflow && !err) {
4931 block += count;
4932 count = overflow;
4933 put_bh(bitmap_bh);
4934 goto do_more;
4935 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004936error_return:
4937 brelse(bitmap_bh);
4938 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004939 return;
4940}
Lukas Czerner7360d172010-10-27 21:30:12 -04004941
4942/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004943 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004944 * @handle: handle to this transaction
4945 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004946 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004947 * @count: number of blocks to free
4948 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004949 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004950 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004951int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004952 ext4_fsblk_t block, unsigned long count)
4953{
4954 struct buffer_head *bitmap_bh = NULL;
4955 struct buffer_head *gd_bh;
4956 ext4_group_t block_group;
4957 ext4_grpblk_t bit;
4958 unsigned int i;
4959 struct ext4_group_desc *desc;
4960 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004961 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004962 int err = 0, ret, blk_free_count;
4963 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004964
4965 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4966
Yongqiang Yang4740b832011-07-26 21:51:08 -04004967 if (count == 0)
4968 return 0;
4969
Amir Goldstein2846e822011-05-09 10:46:41 -04004970 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004971 /*
4972 * Check to see if we are freeing blocks across a group
4973 * boundary.
4974 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004975 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04004976 ext4_warning(sb, "too much blocks added to group %u",
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004977 block_group);
4978 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004979 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004980 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004981
Amir Goldstein2846e822011-05-09 10:46:41 -04004982 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004983 if (IS_ERR(bitmap_bh)) {
4984 err = PTR_ERR(bitmap_bh);
4985 bitmap_bh = NULL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004986 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004987 }
4988
Amir Goldstein2846e822011-05-09 10:46:41 -04004989 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004990 if (!desc) {
4991 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004992 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004993 }
Amir Goldstein2846e822011-05-09 10:46:41 -04004994
4995 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4996 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4997 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4998 in_range(block + count - 1, ext4_inode_table(sb, desc),
4999 sbi->s_itb_per_group)) {
5000 ext4_error(sb, "Adding blocks in system zones - "
5001 "Block = %llu, count = %lu",
5002 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005003 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005004 goto error_return;
5005 }
5006
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005007 BUFFER_TRACE(bitmap_bh, "getting write access");
5008 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04005009 if (err)
5010 goto error_return;
5011
5012 /*
5013 * We are about to modify some metadata. Call the journal APIs
5014 * to unshare ->b_data if a currently-committing transaction is
5015 * using it
5016 */
5017 BUFFER_TRACE(gd_bh, "get_write_access");
5018 err = ext4_journal_get_write_access(handle, gd_bh);
5019 if (err)
5020 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04005021
Amir Goldstein2846e822011-05-09 10:46:41 -04005022 for (i = 0, blocks_freed = 0; i < count; i++) {
5023 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04005024 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005025 ext4_error(sb, "bit already cleared for block %llu",
5026 (ext4_fsblk_t)(block + i));
5027 BUFFER_TRACE(bitmap_bh, "bit already cleared");
5028 } else {
5029 blocks_freed++;
5030 }
5031 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005032
5033 err = ext4_mb_load_buddy(sb, block_group, &e4b);
5034 if (err)
5035 goto error_return;
5036
5037 /*
5038 * need to update group_info->bb_free and bitmap
5039 * with group lock held. generate_buddy look at
5040 * them with group lock_held
5041 */
Amir Goldstein2846e822011-05-09 10:46:41 -04005042 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04005043 mb_clear_bits(bitmap_bh->b_data, bit, count);
5044 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04005045 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
5046 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04005047 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005048 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04005049 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04005050 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05005051 EXT4_NUM_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04005052
5053 if (sbi->s_log_groups_per_flex) {
5054 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04005055 atomic64_add(EXT4_NUM_B2C(sbi, blocks_freed),
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08005056 &sbi_array_rcu_deref(sbi, s_flex_groups,
5057 flex_group)->free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04005058 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005059
5060 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005061
5062 /* We dirtied the bitmap block */
5063 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5064 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5065
5066 /* And the group descriptor block */
5067 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5068 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5069 if (!err)
5070 err = ret;
5071
5072error_return:
5073 brelse(bitmap_bh);
5074 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005075 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005076}
5077
5078/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005079 * ext4_trim_extent -- function to TRIM one single free extent in the group
5080 * @sb: super block for the file system
5081 * @start: starting block of the free extent in the alloc. group
5082 * @count: number of blocks to TRIM
5083 * @group: alloc. group we are working with
5084 * @e4b: ext4 buddy for the group
5085 *
5086 * Trim "count" blocks starting at "start" in the "group". To assure that no
5087 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5088 * be called with under the group lock.
5089 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005090static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04005091 ext4_group_t group, struct ext4_buddy *e4b)
jon ernste2cbd582014-04-12 23:01:28 -04005092__releases(bitlock)
5093__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005094{
5095 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005096 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005097
Tao Mab3d4c2b2011-07-11 00:01:52 -04005098 trace_ext4_trim_extent(sb, group, start, count);
5099
Lukas Czerner7360d172010-10-27 21:30:12 -04005100 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5101
5102 ex.fe_start = start;
5103 ex.fe_group = group;
5104 ex.fe_len = count;
5105
5106 /*
5107 * Mark blocks used, so no one can reuse them while
5108 * being trimmed.
5109 */
5110 mb_mark_used(e4b, &ex);
5111 ext4_unlock_group(sb, group);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005112 ret = ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04005113 ext4_lock_group(sb, group);
5114 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005115 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005116}
5117
5118/**
5119 * ext4_trim_all_free -- function to trim all free space in alloc. group
5120 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005121 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005122 * @start: first group block to examine
5123 * @max: last group block to examine
5124 * @minblocks: minimum extent block count
5125 *
5126 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5127 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5128 * the extent.
5129 *
5130 *
5131 * ext4_trim_all_free walks through group's block bitmap searching for free
5132 * extents. When the free extent is found, mark it as used in group buddy
5133 * bitmap. Then issue a TRIM command on this extent and free the extent in
5134 * the group buddy bitmap. This is done until whole group is scanned.
5135 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005136static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005137ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5138 ext4_grpblk_t start, ext4_grpblk_t max,
5139 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005140{
5141 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005142 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005143 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005144 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005145
Tao Mab3d4c2b2011-07-11 00:01:52 -04005146 trace_ext4_trim_all_free(sb, group, start, max);
5147
Lukas Czerner78944082011-05-24 18:16:27 -04005148 ret = ext4_mb_load_buddy(sb, group, &e4b);
5149 if (ret) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04005150 ext4_warning(sb, "Error %d loading buddy information for %u",
5151 ret, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005152 return ret;
5153 }
Lukas Czerner78944082011-05-24 18:16:27 -04005154 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005155
5156 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005157 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5158 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5159 goto out;
5160
Lukas Czerner78944082011-05-24 18:16:27 -04005161 start = (e4b.bd_info->bb_first_free > start) ?
5162 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005163
Lukas Czerner913eed832012-03-21 21:22:22 -04005164 while (start <= max) {
5165 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5166 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005167 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005168 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005169
5170 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005171 ret = ext4_trim_extent(sb, start,
5172 next - start, group, &e4b);
5173 if (ret && ret != -EOPNOTSUPP)
5174 break;
5175 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005176 count += next - start;
5177 }
Tao Ma169ddc32011-07-11 00:00:07 -04005178 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005179 start = next + 1;
5180
5181 if (fatal_signal_pending(current)) {
5182 count = -ERESTARTSYS;
5183 break;
5184 }
5185
5186 if (need_resched()) {
5187 ext4_unlock_group(sb, group);
5188 cond_resched();
5189 ext4_lock_group(sb, group);
5190 }
5191
Tao Ma169ddc32011-07-11 00:00:07 -04005192 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005193 break;
5194 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005195
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005196 if (!ret) {
5197 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005198 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005199 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005200out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005201 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005202 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005203
5204 ext4_debug("trimmed %d blocks in the group %d\n",
5205 count, group);
5206
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005207 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005208}
5209
5210/**
5211 * ext4_trim_fs() -- trim ioctl handle function
5212 * @sb: superblock for filesystem
5213 * @range: fstrim_range structure
5214 *
5215 * start: First Byte to trim
5216 * len: number of Bytes to trim from start
5217 * minlen: minimum extent length in Bytes
5218 * ext4_trim_fs goes through all allocation groups containing Bytes from
5219 * start to start+len. For each such a group ext4_trim_all_free function
5220 * is invoked to trim all free space.
5221 */
5222int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
5223{
Lukas Czerner78944082011-05-24 18:16:27 -04005224 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005225 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005226 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005227 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005228 ext4_fsblk_t first_data_blk =
5229 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005230 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005231 int ret = 0;
5232
5233 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005234 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005235 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5236 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005237
Lukas Czerner5de35e82012-10-22 18:01:19 -04005238 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5239 start >= max_blks ||
5240 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005241 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005242 if (end >= max_blks)
5243 end = max_blks - 1;
5244 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005245 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005246 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005247 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005248
Lukas Czerner913eed832012-03-21 21:22:22 -04005249 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005250 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005251 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005252 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005253 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005254
Lukas Czerner913eed832012-03-21 21:22:22 -04005255 /* end now represents the last cluster to discard in this group */
5256 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005257
5258 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005259 grp = ext4_get_group_info(sb, group);
5260 /* We only do this if the grp has never been initialized */
5261 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005262 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Lukas Czerner78944082011-05-24 18:16:27 -04005263 if (ret)
5264 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005265 }
5266
Tao Ma0ba08512011-03-23 15:48:11 -04005267 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005268 * For all the groups except the last one, last cluster will
5269 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5270 * change it for the last group, note that last_cluster is
5271 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005272 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005273 if (group == last_group)
5274 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005275
Lukas Czerner78944082011-05-24 18:16:27 -04005276 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005277 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005278 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005279 if (cnt < 0) {
5280 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005281 break;
5282 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005283 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005284 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005285
5286 /*
5287 * For every group except the first one, we are sure
5288 * that the first cluster to discard will be cluster #0.
5289 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005290 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005291 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005292
Tao Ma3d56b8d2011-07-11 00:03:38 -04005293 if (!ret)
5294 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5295
Tao Ma22f10452011-07-10 23:52:37 -04005296out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005297 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005298 return ret;
5299}