blob: 471686ea3e18b5f5ee22fedd82b9c4c71eb75b52 [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,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07002796 ext4_group_t block_group, ext4_grpblk_t cluster, int count,
2797 unsigned long flags)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002798{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002799 ext4_fsblk_t discard_block;
2800
Theodore Ts'o84130192011-09-09 18:50:51 -04002801 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2802 ext4_group_first_block_no(sb, block_group));
2803 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002804 trace_ext4_discard_blocks(sb,
2805 (unsigned long long) discard_block, count);
JP Abgrallcc14b9e2014-07-23 16:55:07 -07002806 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, flags);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002807}
2808
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002809/*
2810 * This function is called by the jbd2 layer once the commit has finished,
2811 * so we know we can free the blocks that were released with that commit.
2812 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002813static void ext4_free_data_callback(struct super_block *sb,
2814 struct ext4_journal_cb_entry *jce,
2815 int rc)
Alex Tomasc9de5602008-01-29 00:19:52 -05002816{
Bobi Jam18aadd42012-02-20 17:53:02 -05002817 struct ext4_free_data *entry = (struct ext4_free_data *)jce;
Alex Tomasc9de5602008-01-29 00:19:52 -05002818 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002819 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002820 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002821
Bobi Jam18aadd42012-02-20 17:53:02 -05002822 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2823 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002824
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002825 if (test_opt(sb, DISCARD)) {
2826 err = ext4_issue_discard(sb, entry->efd_group,
2827 entry->efd_start_cluster,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07002828 entry->efd_count, 0);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002829 if (err && err != -EOPNOTSUPP)
2830 ext4_msg(sb, KERN_WARNING, "discard request in"
2831 " group:%d block:%d count:%d failed"
2832 " with %d", entry->efd_group,
2833 entry->efd_start_cluster,
2834 entry->efd_count, err);
2835 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002836
Bobi Jam18aadd42012-02-20 17:53:02 -05002837 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2838 /* we expect to find existing buddy because it's pinned */
2839 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002840
Theodore Ts'od08854f2016-06-26 18:24:01 -04002841 spin_lock(&EXT4_SB(sb)->s_md_lock);
2842 EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
2843 spin_unlock(&EXT4_SB(sb)->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002844
Bobi Jam18aadd42012-02-20 17:53:02 -05002845 db = e4b.bd_info;
2846 /* there are blocks to put in buddy to make them really free */
2847 count += entry->efd_count;
2848 count2++;
2849 ext4_lock_group(sb, entry->efd_group);
2850 /* Take it out of per group rb tree */
2851 rb_erase(&entry->efd_node, &(db->bb_free_root));
2852 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002853
Bobi Jam18aadd42012-02-20 17:53:02 -05002854 /*
2855 * Clear the trimmed flag for the group so that the next
2856 * ext4_trim_fs can trim it.
2857 * If the volume is mounted with -o discard, online discard
2858 * is supported and the free blocks will be trimmed online.
2859 */
2860 if (!test_opt(sb, DISCARD))
2861 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2862
2863 if (!db->bb_free_root.rb_node) {
2864 /* No more items in the per group rb tree
2865 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002866 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002867 put_page(e4b.bd_buddy_page);
2868 put_page(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002869 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002870 ext4_unlock_group(sb, entry->efd_group);
2871 kmem_cache_free(ext4_free_data_cachep, entry);
2872 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002873
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002874 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002875}
2876
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002877int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002878{
Theodore Ts'o16828082010-10-27 21:30:09 -04002879 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2880 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002881 if (ext4_pspace_cachep == NULL)
2882 return -ENOMEM;
2883
Theodore Ts'o16828082010-10-27 21:30:09 -04002884 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2885 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002886 if (ext4_ac_cachep == NULL) {
2887 kmem_cache_destroy(ext4_pspace_cachep);
2888 return -ENOMEM;
2889 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002890
Bobi Jam18aadd42012-02-20 17:53:02 -05002891 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2892 SLAB_RECLAIM_ACCOUNT);
2893 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002894 kmem_cache_destroy(ext4_pspace_cachep);
2895 kmem_cache_destroy(ext4_ac_cachep);
2896 return -ENOMEM;
2897 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002898 return 0;
2899}
2900
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002901void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002902{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002903 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002904 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2905 * before destroying the slab cache.
2906 */
2907 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002908 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002909 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002910 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002911 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002912}
2913
2914
2915/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002916 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002917 * Returns 0 if success or error code
2918 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002919static noinline_for_stack int
2920ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002921 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002922{
2923 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002924 struct ext4_group_desc *gdp;
2925 struct buffer_head *gdp_bh;
2926 struct ext4_sb_info *sbi;
2927 struct super_block *sb;
2928 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002929 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002930
2931 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2932 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2933
2934 sb = ac->ac_sb;
2935 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002936
Theodore Ts'o574ca172008-07-11 19:27:31 -04002937 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002938 if (IS_ERR(bitmap_bh)) {
2939 err = PTR_ERR(bitmap_bh);
2940 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002941 goto out_err;
Darrick J. Wong9008a582015-10-17 21:33:24 -04002942 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002943
liang xie5d601252014-05-12 22:06:43 -04002944 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002945 err = ext4_journal_get_write_access(handle, bitmap_bh);
2946 if (err)
2947 goto out_err;
2948
2949 err = -EIO;
2950 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2951 if (!gdp)
2952 goto out_err;
2953
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002954 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002955 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002956
liang xie5d601252014-05-12 22:06:43 -04002957 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002958 err = ext4_journal_get_write_access(handle, gdp_bh);
2959 if (err)
2960 goto out_err;
2961
Akinobu Mitabda00de2010-03-03 23:53:25 -05002962 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002963
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002964 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002965 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002966 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002967 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002968 /* File system mounted not to panic on error
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002969 * Fix the bitmap and return EFSCORRUPTED
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002970 * We leak some of the blocks here.
2971 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002972 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002973 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2974 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002975 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002976 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002977 if (!err)
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002978 err = -EFSCORRUPTED;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002979 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002980 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002981
2982 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002983#ifdef AGGRESSIVE_CHECK
2984 {
2985 int i;
2986 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2987 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2988 bitmap_bh->b_data));
2989 }
2990 }
2991#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002992 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2993 ac->ac_b_ex.fe_len);
Theodore Ts'o5ae57322018-06-14 00:58:00 -04002994 if (ext4_has_group_desc_csum(sb) &&
2995 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002996 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002997 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002998 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002999 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05003000 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04003001 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
3002 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04003003 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003004 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003005
3006 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04003007 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003008 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003009 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003010 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003011 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3012 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04003013 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
3014 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05003015
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003016 if (sbi->s_log_groups_per_flex) {
3017 ext4_group_t flex_group = ext4_flex_group(sbi,
3018 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04003019 atomic64_sub(ac->ac_b_ex.fe_len,
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08003020 &sbi_array_rcu_deref(sbi, s_flex_groups,
3021 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003022 }
3023
Frank Mayhar03901312009-01-07 00:06:22 -05003024 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003025 if (err)
3026 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003027 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003028
3029out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003030 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003031 return err;
3032}
3033
3034/*
3035 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003036 * Group request are normalized to s_mb_group_prealloc, which goes to
3037 * s_strip if we set the same via mount option.
3038 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003039 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003040 *
3041 * XXX: should we try to preallocate more than the group has now?
3042 */
3043static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3044{
3045 struct super_block *sb = ac->ac_sb;
3046 struct ext4_locality_group *lg = ac->ac_lg;
3047
3048 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003049 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003050 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003051 current->pid, ac->ac_g_ex.fe_len);
3052}
3053
3054/*
3055 * Normalization means making request better in terms of
3056 * size and alignment
3057 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003058static noinline_for_stack void
3059ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003060 struct ext4_allocation_request *ar)
3061{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003062 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003063 int bsbits, max;
3064 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003065 loff_t size, start_off;
3066 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003067 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003068 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003069 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003070
3071 /* do normalize only data requests, metadata requests
3072 do not need preallocation */
3073 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3074 return;
3075
3076 /* sometime caller may want exact blocks */
3077 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3078 return;
3079
3080 /* caller may indicate that preallocation isn't
3081 * required (it's a tail, for example) */
3082 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3083 return;
3084
3085 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3086 ext4_mb_normalize_group_request(ac);
3087 return ;
3088 }
3089
3090 bsbits = ac->ac_sb->s_blocksize_bits;
3091
3092 /* first, let's learn actual file size
3093 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003094 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003095 size = size << bsbits;
3096 if (size < i_size_read(ac->ac_inode))
3097 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003098 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003099
Valerie Clement19304792008-05-13 19:31:14 -04003100 /* max size of free chunks */
3101 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003102
Valerie Clement19304792008-05-13 19:31:14 -04003103#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3104 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003105
3106 /* first, try to predict filesize */
3107 /* XXX: should this table be tunable? */
3108 start_off = 0;
3109 if (size <= 16 * 1024) {
3110 size = 16 * 1024;
3111 } else if (size <= 32 * 1024) {
3112 size = 32 * 1024;
3113 } else if (size <= 64 * 1024) {
3114 size = 64 * 1024;
3115 } else if (size <= 128 * 1024) {
3116 size = 128 * 1024;
3117 } else if (size <= 256 * 1024) {
3118 size = 256 * 1024;
3119 } else if (size <= 512 * 1024) {
3120 size = 512 * 1024;
3121 } else if (size <= 1024 * 1024) {
3122 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003123 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003124 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003125 (21 - bsbits)) << 21;
3126 size = 2 * 1024 * 1024;
3127 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003128 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3129 (22 - bsbits)) << 22;
3130 size = 4 * 1024 * 1024;
3131 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003132 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003133 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3134 (23 - bsbits)) << 23;
3135 size = 8 * 1024 * 1024;
3136 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003137 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3138 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3139 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003140 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003141 size = size >> bsbits;
3142 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003143
3144 /* don't cover already allocated blocks in selected range */
3145 if (ar->pleft && start <= ar->lleft) {
3146 size -= ar->lleft + 1 - start;
3147 start = ar->lleft + 1;
3148 }
3149 if (ar->pright && start + size - 1 >= ar->lright)
3150 size -= start + size - ar->lright;
3151
Jan Karafc6c2da2017-01-27 14:34:30 -05003152 /*
3153 * Trim allocation request for filesystems with artificially small
3154 * groups.
3155 */
3156 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
3157 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
3158
Alex Tomasc9de5602008-01-29 00:19:52 -05003159 end = start + size;
3160
3161 /* check we don't cross already preallocated blocks */
3162 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003163 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003164 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003165
Alex Tomasc9de5602008-01-29 00:19:52 -05003166 if (pa->pa_deleted)
3167 continue;
3168 spin_lock(&pa->pa_lock);
3169 if (pa->pa_deleted) {
3170 spin_unlock(&pa->pa_lock);
3171 continue;
3172 }
3173
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003174 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3175 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003176
3177 /* PA must not overlap original request */
3178 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3179 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3180
Eric Sandeen38877f42009-08-17 23:55:24 -04003181 /* skip PAs this normalized request doesn't overlap with */
3182 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003183 spin_unlock(&pa->pa_lock);
3184 continue;
3185 }
3186 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3187
Eric Sandeen38877f42009-08-17 23:55:24 -04003188 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003189 if (pa_end <= ac->ac_o_ex.fe_logical) {
3190 BUG_ON(pa_end < start);
3191 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003192 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003193 BUG_ON(pa->pa_lstart > end);
3194 end = pa->pa_lstart;
3195 }
3196 spin_unlock(&pa->pa_lock);
3197 }
3198 rcu_read_unlock();
3199 size = end - start;
3200
3201 /* XXX: extra loop to check we really don't overlap preallocations */
3202 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003203 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003204 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003205
Alex Tomasc9de5602008-01-29 00:19:52 -05003206 spin_lock(&pa->pa_lock);
3207 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003208 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3209 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003210 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3211 }
3212 spin_unlock(&pa->pa_lock);
3213 }
3214 rcu_read_unlock();
3215
3216 if (start + size <= ac->ac_o_ex.fe_logical &&
3217 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003218 ext4_msg(ac->ac_sb, KERN_ERR,
3219 "start %lu, size %lu, fe_logical %lu",
3220 (unsigned long) start, (unsigned long) size,
3221 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003222 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003223 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003224 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003225
3226 /* now prepare goal request */
3227
3228 /* XXX: is it better to align blocks WRT to logical
3229 * placement or satisfy big request as is */
3230 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003231 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003232
3233 /* define goal start in order to merge */
3234 if (ar->pright && (ar->lright == (start + size))) {
3235 /* merge to the right */
3236 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3237 &ac->ac_f_ex.fe_group,
3238 &ac->ac_f_ex.fe_start);
3239 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3240 }
3241 if (ar->pleft && (ar->lleft + 1 == start)) {
3242 /* merge to the left */
3243 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3244 &ac->ac_f_ex.fe_group,
3245 &ac->ac_f_ex.fe_start);
3246 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3247 }
3248
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003249 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003250 (unsigned) orig_size, (unsigned) start);
3251}
3252
3253static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3254{
3255 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3256
3257 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3258 atomic_inc(&sbi->s_bal_reqs);
3259 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003260 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003261 atomic_inc(&sbi->s_bal_success);
3262 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3263 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3264 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3265 atomic_inc(&sbi->s_bal_goals);
3266 if (ac->ac_found > sbi->s_mb_max_to_scan)
3267 atomic_inc(&sbi->s_bal_breaks);
3268 }
3269
Theodore Ts'o296c3552009-09-30 00:32:42 -04003270 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3271 trace_ext4_mballoc_alloc(ac);
3272 else
3273 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003274}
3275
3276/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003277 * Called on failure; free up any blocks from the inode PA for this
3278 * context. We don't need this for MB_GROUP_PA because we only change
3279 * pa_free in ext4_mb_release_context(), but on failure, we've already
3280 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3281 */
3282static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3283{
3284 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003285 struct ext4_buddy e4b;
3286 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003287
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003288 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003289 if (ac->ac_f_ex.fe_len == 0)
3290 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003291 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3292 if (err) {
3293 /*
3294 * This should never happen since we pin the
3295 * pages in the ext4_allocation_context so
3296 * ext4_mb_load_buddy() should never fail.
3297 */
3298 WARN(1, "mb_load_buddy failed (%d)", err);
3299 return;
3300 }
3301 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3302 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3303 ac->ac_f_ex.fe_len);
3304 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003305 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003306 return;
3307 }
3308 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003309 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003310}
3311
3312/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003313 * use blocks preallocated to inode
3314 */
3315static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3316 struct ext4_prealloc_space *pa)
3317{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003318 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003319 ext4_fsblk_t start;
3320 ext4_fsblk_t end;
3321 int len;
3322
3323 /* found preallocated blocks, use them */
3324 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003325 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3326 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3327 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003328 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3329 &ac->ac_b_ex.fe_start);
3330 ac->ac_b_ex.fe_len = len;
3331 ac->ac_status = AC_STATUS_FOUND;
3332 ac->ac_pa = pa;
3333
3334 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003335 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003336 BUG_ON(pa->pa_free < len);
3337 pa->pa_free -= len;
3338
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003339 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003340}
3341
3342/*
3343 * use blocks preallocated to locality group
3344 */
3345static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3346 struct ext4_prealloc_space *pa)
3347{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003348 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003349
Alex Tomasc9de5602008-01-29 00:19:52 -05003350 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3351 &ac->ac_b_ex.fe_group,
3352 &ac->ac_b_ex.fe_start);
3353 ac->ac_b_ex.fe_len = len;
3354 ac->ac_status = AC_STATUS_FOUND;
3355 ac->ac_pa = pa;
3356
3357 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003358 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003359 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003360 * in on-disk bitmap -- see ext4_mb_release_context()
3361 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003362 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003363 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003364}
3365
3366/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003367 * Return the prealloc space that have minimal distance
3368 * from the goal block. @cpa is the prealloc
3369 * space that is having currently known minimal distance
3370 * from the goal block.
3371 */
3372static struct ext4_prealloc_space *
3373ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3374 struct ext4_prealloc_space *pa,
3375 struct ext4_prealloc_space *cpa)
3376{
3377 ext4_fsblk_t cur_distance, new_distance;
3378
3379 if (cpa == NULL) {
3380 atomic_inc(&pa->pa_count);
3381 return pa;
3382 }
Andrew Morton79211c82015-11-09 14:58:13 -08003383 cur_distance = abs(goal_block - cpa->pa_pstart);
3384 new_distance = abs(goal_block - pa->pa_pstart);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003385
Coly Li5a54b2f2011-02-24 14:10:05 -05003386 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003387 return cpa;
3388
3389 /* drop the previous reference */
3390 atomic_dec(&cpa->pa_count);
3391 atomic_inc(&pa->pa_count);
3392 return pa;
3393}
3394
3395/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003396 * search goal blocks in preallocated space
3397 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003398static noinline_for_stack int
3399ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003400{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003401 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003402 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003403 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3404 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003405 struct ext4_prealloc_space *pa, *cpa = NULL;
3406 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003407
3408 /* only data can be preallocated */
3409 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3410 return 0;
3411
3412 /* first, try per-file preallocation */
3413 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003414 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003415
3416 /* all fields in this condition don't change,
3417 * so we can skip locking for them */
3418 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003419 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3420 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003421 continue;
3422
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003423 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003424 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003425 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3426 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003427 continue;
3428
Alex Tomasc9de5602008-01-29 00:19:52 -05003429 /* found preallocated blocks, use them */
3430 spin_lock(&pa->pa_lock);
3431 if (pa->pa_deleted == 0 && pa->pa_free) {
3432 atomic_inc(&pa->pa_count);
3433 ext4_mb_use_inode_pa(ac, pa);
3434 spin_unlock(&pa->pa_lock);
3435 ac->ac_criteria = 10;
3436 rcu_read_unlock();
3437 return 1;
3438 }
3439 spin_unlock(&pa->pa_lock);
3440 }
3441 rcu_read_unlock();
3442
3443 /* can we use group allocation? */
3444 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3445 return 0;
3446
3447 /* inode may have no locality group for some reason */
3448 lg = ac->ac_lg;
3449 if (lg == NULL)
3450 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003451 order = fls(ac->ac_o_ex.fe_len) - 1;
3452 if (order > PREALLOC_TB_SIZE - 1)
3453 /* The max size of hash table is PREALLOC_TB_SIZE */
3454 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003455
Akinobu Mitabda00de2010-03-03 23:53:25 -05003456 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003457 /*
3458 * search for the prealloc space that is having
3459 * minimal distance from the goal block.
3460 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003461 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3462 rcu_read_lock();
3463 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3464 pa_inode_list) {
3465 spin_lock(&pa->pa_lock);
3466 if (pa->pa_deleted == 0 &&
3467 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003468
3469 cpa = ext4_mb_check_group_pa(goal_block,
3470 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003471 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003472 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003473 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003474 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003475 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003476 if (cpa) {
3477 ext4_mb_use_group_pa(ac, cpa);
3478 ac->ac_criteria = 20;
3479 return 1;
3480 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003481 return 0;
3482}
3483
3484/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003485 * the function goes through all block freed in the group
3486 * but not yet committed and marks them used in in-core bitmap.
3487 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003488 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003489 */
3490static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3491 ext4_group_t group)
3492{
3493 struct rb_node *n;
3494 struct ext4_group_info *grp;
3495 struct ext4_free_data *entry;
3496
3497 grp = ext4_get_group_info(sb, group);
3498 n = rb_first(&(grp->bb_free_root));
3499
3500 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003501 entry = rb_entry(n, struct ext4_free_data, efd_node);
3502 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003503 n = rb_next(n);
3504 }
3505 return;
3506}
3507
3508/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003509 * the function goes through all preallocation in this group and marks them
3510 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003511 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003512 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003513static noinline_for_stack
3514void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003515 ext4_group_t group)
3516{
3517 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3518 struct ext4_prealloc_space *pa;
3519 struct list_head *cur;
3520 ext4_group_t groupnr;
3521 ext4_grpblk_t start;
3522 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003523 int len;
3524
3525 /* all form of preallocation discards first load group,
3526 * so the only competing code is preallocation use.
3527 * we don't need any locking here
3528 * notice we do NOT ignore preallocations with pa_deleted
3529 * otherwise we could leave used blocks available for
3530 * allocation in buddy when concurrent ext4_mb_put_pa()
3531 * is dropping preallocation
3532 */
3533 list_for_each(cur, &grp->bb_prealloc_list) {
3534 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3535 spin_lock(&pa->pa_lock);
3536 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3537 &groupnr, &start);
3538 len = pa->pa_len;
3539 spin_unlock(&pa->pa_lock);
3540 if (unlikely(len == 0))
3541 continue;
3542 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003543 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003544 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003545 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003546 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003547}
3548
3549static void ext4_mb_pa_callback(struct rcu_head *head)
3550{
3551 struct ext4_prealloc_space *pa;
3552 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003553
3554 BUG_ON(atomic_read(&pa->pa_count));
3555 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003556 kmem_cache_free(ext4_pspace_cachep, pa);
3557}
3558
3559/*
3560 * drops a reference to preallocated space descriptor
3561 * if this was the last reference and the space is consumed
3562 */
3563static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3564 struct super_block *sb, struct ext4_prealloc_space *pa)
3565{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003566 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003567 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003568
Alex Tomasc9de5602008-01-29 00:19:52 -05003569 /* in this short window concurrent discard can set pa_deleted */
3570 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003571 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3572 spin_unlock(&pa->pa_lock);
3573 return;
3574 }
3575
Alex Tomasc9de5602008-01-29 00:19:52 -05003576 if (pa->pa_deleted == 1) {
3577 spin_unlock(&pa->pa_lock);
3578 return;
3579 }
3580
3581 pa->pa_deleted = 1;
3582 spin_unlock(&pa->pa_lock);
3583
Eric Sandeend33a1972009-03-16 23:25:40 -04003584 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003585 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003586 * If doing group-based preallocation, pa_pstart may be in the
3587 * next group when pa is used up
3588 */
3589 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003590 grp_blk--;
3591
Lukas Czernerbd862982013-04-03 23:32:34 -04003592 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003593
3594 /*
3595 * possible race:
3596 *
3597 * P1 (buddy init) P2 (regular allocation)
3598 * find block B in PA
3599 * copy on-disk bitmap to buddy
3600 * mark B in on-disk bitmap
3601 * drop PA from group
3602 * mark all PAs in buddy
3603 *
3604 * thus, P1 initializes buddy with B available. to prevent this
3605 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3606 * against that pair
3607 */
3608 ext4_lock_group(sb, grp);
3609 list_del(&pa->pa_group_list);
3610 ext4_unlock_group(sb, grp);
3611
3612 spin_lock(pa->pa_obj_lock);
3613 list_del_rcu(&pa->pa_inode_list);
3614 spin_unlock(pa->pa_obj_lock);
3615
3616 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3617}
3618
3619/*
3620 * creates new preallocated space for given inode
3621 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003622static noinline_for_stack int
3623ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003624{
3625 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003626 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003627 struct ext4_prealloc_space *pa;
3628 struct ext4_group_info *grp;
3629 struct ext4_inode_info *ei;
3630
3631 /* preallocate only when found space is larger then requested */
3632 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3633 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3634 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3635
3636 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3637 if (pa == NULL)
3638 return -ENOMEM;
3639
3640 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3641 int winl;
3642 int wins;
3643 int win;
3644 int offs;
3645
3646 /* we can't allocate as much as normalizer wants.
3647 * so, found space must get proper lstart
3648 * to cover original request */
3649 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3650 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3651
3652 /* we're limited by original request in that
3653 * logical block must be covered any way
3654 * winl is window we can move our chunk within */
3655 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3656
3657 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003658 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003659
3660 /* the smallest one defines real window */
3661 win = min(winl, wins);
3662
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003663 offs = ac->ac_o_ex.fe_logical %
3664 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003665 if (offs && offs < win)
3666 win = offs;
3667
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003668 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003669 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003670 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3671 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3672 }
3673
3674 /* preallocation can change ac_b_ex, thus we store actually
3675 * allocated blocks for history */
3676 ac->ac_f_ex = ac->ac_b_ex;
3677
3678 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3679 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3680 pa->pa_len = ac->ac_b_ex.fe_len;
3681 pa->pa_free = pa->pa_len;
3682 atomic_set(&pa->pa_count, 1);
3683 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003684 INIT_LIST_HEAD(&pa->pa_inode_list);
3685 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003686 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003687 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003688
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003689 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003690 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003691 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003692
3693 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003694 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003695
3696 ei = EXT4_I(ac->ac_inode);
3697 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3698
3699 pa->pa_obj_lock = &ei->i_prealloc_lock;
3700 pa->pa_inode = ac->ac_inode;
3701
3702 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3703 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3704 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3705
3706 spin_lock(pa->pa_obj_lock);
3707 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3708 spin_unlock(pa->pa_obj_lock);
3709
3710 return 0;
3711}
3712
3713/*
3714 * creates new preallocated space for locality group inodes belongs to
3715 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003716static noinline_for_stack int
3717ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003718{
3719 struct super_block *sb = ac->ac_sb;
3720 struct ext4_locality_group *lg;
3721 struct ext4_prealloc_space *pa;
3722 struct ext4_group_info *grp;
3723
3724 /* preallocate only when found space is larger then requested */
3725 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3726 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3727 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3728
3729 BUG_ON(ext4_pspace_cachep == NULL);
3730 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3731 if (pa == NULL)
3732 return -ENOMEM;
3733
3734 /* preallocation can change ac_b_ex, thus we store actually
3735 * allocated blocks for history */
3736 ac->ac_f_ex = ac->ac_b_ex;
3737
3738 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3739 pa->pa_lstart = pa->pa_pstart;
3740 pa->pa_len = ac->ac_b_ex.fe_len;
3741 pa->pa_free = pa->pa_len;
3742 atomic_set(&pa->pa_count, 1);
3743 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003744 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003745 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003746 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003747 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003748
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003749 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003750 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3751 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003752
3753 ext4_mb_use_group_pa(ac, pa);
3754 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3755
3756 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3757 lg = ac->ac_lg;
3758 BUG_ON(lg == NULL);
3759
3760 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3761 pa->pa_inode = NULL;
3762
3763 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3764 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3765 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3766
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003767 /*
3768 * We will later add the new pa to the right bucket
3769 * after updating the pa_free in ext4_mb_release_context
3770 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003771 return 0;
3772}
3773
3774static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3775{
3776 int err;
3777
3778 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3779 err = ext4_mb_new_group_pa(ac);
3780 else
3781 err = ext4_mb_new_inode_pa(ac);
3782 return err;
3783}
3784
3785/*
3786 * finds all unused blocks in on-disk bitmap, frees them in
3787 * in-core bitmap and buddy.
3788 * @pa must be unlinked from inode and group lists, so that
3789 * nobody else can find/use it.
3790 * the caller MUST hold group/inode locks.
3791 * TODO: optimize the case when there are no in-core structures yet
3792 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003793static noinline_for_stack int
3794ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003795 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003796{
Alex Tomasc9de5602008-01-29 00:19:52 -05003797 struct super_block *sb = e4b->bd_sb;
3798 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003799 unsigned int end;
3800 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003801 ext4_group_t group;
3802 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003803 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003804 int err = 0;
3805 int free = 0;
3806
3807 BUG_ON(pa->pa_deleted == 0);
3808 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003809 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003810 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3811 end = bit + pa->pa_len;
3812
Alex Tomasc9de5602008-01-29 00:19:52 -05003813 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003814 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003815 if (bit >= end)
3816 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003817 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003818 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003819 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3820 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003821 free += next - bit;
3822
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003823 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003824 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3825 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003826 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003827 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3828 bit = next + 1;
3829 }
3830 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003831 ext4_msg(e4b->bd_sb, KERN_CRIT,
3832 "pa %p: logic %lu, phys. %lu, len %lu",
3833 pa, (unsigned long) pa->pa_lstart,
3834 (unsigned long) pa->pa_pstart,
3835 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003836 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003837 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003838 /*
3839 * pa is already deleted so we use the value obtained
3840 * from the bitmap and continue.
3841 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003842 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003843 atomic_add(free, &sbi->s_mb_discarded);
3844
3845 return err;
3846}
3847
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003848static noinline_for_stack int
3849ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003850 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003851{
Alex Tomasc9de5602008-01-29 00:19:52 -05003852 struct super_block *sb = e4b->bd_sb;
3853 ext4_group_t group;
3854 ext4_grpblk_t bit;
3855
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003856 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003857 BUG_ON(pa->pa_deleted == 0);
3858 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3859 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3860 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3861 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003862 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003863
3864 return 0;
3865}
3866
3867/*
3868 * releases all preallocations in given group
3869 *
3870 * first, we need to decide discard policy:
3871 * - when do we discard
3872 * 1) ENOSPC
3873 * - how many do we discard
3874 * 1) how many requested
3875 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003876static noinline_for_stack int
3877ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003878 ext4_group_t group, int needed)
3879{
3880 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3881 struct buffer_head *bitmap_bh = NULL;
3882 struct ext4_prealloc_space *pa, *tmp;
3883 struct list_head list;
3884 struct ext4_buddy e4b;
3885 int err;
3886 int busy = 0;
3887 int free = 0;
3888
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003889 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003890
3891 if (list_empty(&grp->bb_prealloc_list))
3892 return 0;
3893
Theodore Ts'o574ca172008-07-11 19:27:31 -04003894 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04003895 if (IS_ERR(bitmap_bh)) {
3896 err = PTR_ERR(bitmap_bh);
3897 ext4_error(sb, "Error %d reading block bitmap for %u",
3898 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003899 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003900 }
3901
3902 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003903 if (err) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04003904 ext4_warning(sb, "Error %d loading buddy information for %u",
3905 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003906 put_bh(bitmap_bh);
3907 return 0;
3908 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003909
3910 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003911 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003912
Alex Tomasc9de5602008-01-29 00:19:52 -05003913 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003914repeat:
3915 ext4_lock_group(sb, group);
3916 list_for_each_entry_safe(pa, tmp,
3917 &grp->bb_prealloc_list, pa_group_list) {
3918 spin_lock(&pa->pa_lock);
3919 if (atomic_read(&pa->pa_count)) {
3920 spin_unlock(&pa->pa_lock);
3921 busy = 1;
3922 continue;
3923 }
3924 if (pa->pa_deleted) {
3925 spin_unlock(&pa->pa_lock);
3926 continue;
3927 }
3928
3929 /* seems this one can be freed ... */
3930 pa->pa_deleted = 1;
3931
3932 /* we can trust pa_free ... */
3933 free += pa->pa_free;
3934
3935 spin_unlock(&pa->pa_lock);
3936
3937 list_del(&pa->pa_group_list);
3938 list_add(&pa->u.pa_tmp_list, &list);
3939 }
3940
3941 /* if we still need more blocks and some PAs were used, try again */
3942 if (free < needed && busy) {
3943 busy = 0;
3944 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003945 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003946 goto repeat;
3947 }
3948
3949 /* found anything to free? */
3950 if (list_empty(&list)) {
3951 BUG_ON(free != 0);
3952 goto out;
3953 }
3954
3955 /* now free all selected PAs */
3956 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3957
3958 /* remove from object (inode or locality group) */
3959 spin_lock(pa->pa_obj_lock);
3960 list_del_rcu(&pa->pa_inode_list);
3961 spin_unlock(pa->pa_obj_lock);
3962
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003963 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003964 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003965 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003966 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003967
3968 list_del(&pa->u.pa_tmp_list);
3969 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3970 }
3971
3972out:
3973 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003974 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003975 put_bh(bitmap_bh);
3976 return free;
3977}
3978
3979/*
3980 * releases all non-used preallocated blocks for given inode
3981 *
3982 * It's important to discard preallocations under i_data_sem
3983 * We don't want another block to be served from the prealloc
3984 * space when we are discarding the inode prealloc space.
3985 *
3986 * FIXME!! Make sure it is valid at all the call sites
3987 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003988void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003989{
3990 struct ext4_inode_info *ei = EXT4_I(inode);
3991 struct super_block *sb = inode->i_sb;
3992 struct buffer_head *bitmap_bh = NULL;
3993 struct ext4_prealloc_space *pa, *tmp;
3994 ext4_group_t group = 0;
3995 struct list_head list;
3996 struct ext4_buddy e4b;
3997 int err;
3998
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003999 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004000 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4001 return;
4002 }
4003
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004004 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004005 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004006
4007 INIT_LIST_HEAD(&list);
4008
4009repeat:
4010 /* first, collect all pa's in the inode */
4011 spin_lock(&ei->i_prealloc_lock);
4012 while (!list_empty(&ei->i_prealloc_list)) {
4013 pa = list_entry(ei->i_prealloc_list.next,
4014 struct ext4_prealloc_space, pa_inode_list);
4015 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4016 spin_lock(&pa->pa_lock);
4017 if (atomic_read(&pa->pa_count)) {
4018 /* this shouldn't happen often - nobody should
4019 * use preallocation while we're discarding it */
4020 spin_unlock(&pa->pa_lock);
4021 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004022 ext4_msg(sb, KERN_ERR,
4023 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05004024 WARN_ON(1);
4025 schedule_timeout_uninterruptible(HZ);
4026 goto repeat;
4027
4028 }
4029 if (pa->pa_deleted == 0) {
4030 pa->pa_deleted = 1;
4031 spin_unlock(&pa->pa_lock);
4032 list_del_rcu(&pa->pa_inode_list);
4033 list_add(&pa->u.pa_tmp_list, &list);
4034 continue;
4035 }
4036
4037 /* someone is deleting pa right now */
4038 spin_unlock(&pa->pa_lock);
4039 spin_unlock(&ei->i_prealloc_lock);
4040
4041 /* we have to wait here because pa_deleted
4042 * doesn't mean pa is already unlinked from
4043 * the list. as we might be called from
4044 * ->clear_inode() the inode will get freed
4045 * and concurrent thread which is unlinking
4046 * pa from inode's list may access already
4047 * freed memory, bad-bad-bad */
4048
4049 /* XXX: if this happens too often, we can
4050 * add a flag to force wait only in case
4051 * of ->clear_inode(), but not in case of
4052 * regular truncate */
4053 schedule_timeout_uninterruptible(HZ);
4054 goto repeat;
4055 }
4056 spin_unlock(&ei->i_prealloc_lock);
4057
4058 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004059 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04004060 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05004061
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004062 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4063 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004064 if (err) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004065 ext4_error(sb, "Error %d loading buddy information for %u",
4066 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004067 continue;
4068 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004069
Theodore Ts'o574ca172008-07-11 19:27:31 -04004070 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004071 if (IS_ERR(bitmap_bh)) {
4072 err = PTR_ERR(bitmap_bh);
4073 ext4_error(sb, "Error %d reading block bitmap for %u",
4074 err, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004075 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004076 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004077 }
4078
4079 ext4_lock_group(sb, group);
4080 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004081 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004082 ext4_unlock_group(sb, group);
4083
Jing Zhange39e07f2010-05-14 00:00:00 -04004084 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004085 put_bh(bitmap_bh);
4086
4087 list_del(&pa->u.pa_tmp_list);
4088 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4089 }
4090}
4091
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004092#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004093static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4094{
4095 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004096 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004097
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05004098 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05004099 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004100 return;
4101
Joe Perches7f6a11e2012-03-19 23:09:43 -04004102 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004103 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04004104 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004105 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004106 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004107 "goal %lu/%lu/%lu@%lu, "
4108 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004109 (unsigned long)ac->ac_o_ex.fe_group,
4110 (unsigned long)ac->ac_o_ex.fe_start,
4111 (unsigned long)ac->ac_o_ex.fe_len,
4112 (unsigned long)ac->ac_o_ex.fe_logical,
4113 (unsigned long)ac->ac_g_ex.fe_group,
4114 (unsigned long)ac->ac_g_ex.fe_start,
4115 (unsigned long)ac->ac_g_ex.fe_len,
4116 (unsigned long)ac->ac_g_ex.fe_logical,
4117 (unsigned long)ac->ac_b_ex.fe_group,
4118 (unsigned long)ac->ac_b_ex.fe_start,
4119 (unsigned long)ac->ac_b_ex.fe_len,
4120 (unsigned long)ac->ac_b_ex.fe_logical,
4121 (int)ac->ac_criteria);
Eric Sandeendc9ddd92014-02-20 13:32:10 -05004122 ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004123 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004124 ngroups = ext4_get_groups_count(sb);
4125 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004126 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4127 struct ext4_prealloc_space *pa;
4128 ext4_grpblk_t start;
4129 struct list_head *cur;
4130 ext4_lock_group(sb, i);
4131 list_for_each(cur, &grp->bb_prealloc_list) {
4132 pa = list_entry(cur, struct ext4_prealloc_space,
4133 pa_group_list);
4134 spin_lock(&pa->pa_lock);
4135 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4136 NULL, &start);
4137 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004138 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4139 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004140 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004141 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004142
4143 if (grp->bb_free == 0)
4144 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004145 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004146 i, grp->bb_free, grp->bb_fragments);
4147 }
4148 printk(KERN_ERR "\n");
4149}
4150#else
4151static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4152{
4153 return;
4154}
4155#endif
4156
4157/*
4158 * We use locality group preallocation for small size file. The size of the
4159 * file is determined by the current size or the resulting size after
4160 * allocation which ever is larger
4161 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004162 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004163 */
4164static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4165{
4166 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4167 int bsbits = ac->ac_sb->s_blocksize_bits;
4168 loff_t size, isize;
4169
4170 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4171 return;
4172
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004173 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4174 return;
4175
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004176 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004177 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4178 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004179
Theodore Ts'o50797482009-09-18 13:34:02 -04004180 if ((size == isize) &&
4181 !ext4_fs_is_busy(sbi) &&
4182 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4183 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4184 return;
4185 }
4186
Robin Dongebbe0272011-10-26 05:14:27 -04004187 if (sbi->s_mb_group_prealloc <= 0) {
4188 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4189 return;
4190 }
4191
Alex Tomasc9de5602008-01-29 00:19:52 -05004192 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004193 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004194 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004195 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004196 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004197 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004198
4199 BUG_ON(ac->ac_lg != NULL);
4200 /*
4201 * locality group prealloc space are per cpu. The reason for having
4202 * per cpu locality group is to reduce the contention between block
4203 * request from multiple CPUs.
4204 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004205 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004206
4207 /* we're going to use group allocation */
4208 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4209
4210 /* serialize all allocations in the group */
4211 mutex_lock(&ac->ac_lg->lg_mutex);
4212}
4213
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004214static noinline_for_stack int
4215ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004216 struct ext4_allocation_request *ar)
4217{
4218 struct super_block *sb = ar->inode->i_sb;
4219 struct ext4_sb_info *sbi = EXT4_SB(sb);
4220 struct ext4_super_block *es = sbi->s_es;
4221 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004222 unsigned int len;
4223 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004224 ext4_grpblk_t block;
4225
4226 /* we can't allocate > group size */
4227 len = ar->len;
4228
4229 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004230 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4231 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004232
4233 /* start searching from the goal */
4234 goal = ar->goal;
4235 if (goal < le32_to_cpu(es->s_first_data_block) ||
4236 goal >= ext4_blocks_count(es))
4237 goal = le32_to_cpu(es->s_first_data_block);
4238 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4239
4240 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004241 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004242 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004243 ac->ac_sb = sb;
4244 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004245 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004246 ac->ac_o_ex.fe_group = group;
4247 ac->ac_o_ex.fe_start = block;
4248 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004249 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004250 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004251
4252 /* we have to define context: we'll we work with a file or
4253 * locality group. this is a policy, actually */
4254 ext4_mb_group_or_file(ac);
4255
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004256 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004257 "left: %u/%u, right %u/%u to %swritable\n",
4258 (unsigned) ar->len, (unsigned) ar->logical,
4259 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4260 (unsigned) ar->lleft, (unsigned) ar->pleft,
4261 (unsigned) ar->lright, (unsigned) ar->pright,
4262 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4263 return 0;
4264
4265}
4266
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004267static noinline_for_stack void
4268ext4_mb_discard_lg_preallocations(struct super_block *sb,
4269 struct ext4_locality_group *lg,
4270 int order, int total_entries)
4271{
4272 ext4_group_t group = 0;
4273 struct ext4_buddy e4b;
4274 struct list_head discard_list;
4275 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004276
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004277 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004278
4279 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004280
4281 spin_lock(&lg->lg_prealloc_lock);
4282 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4283 pa_inode_list) {
4284 spin_lock(&pa->pa_lock);
4285 if (atomic_read(&pa->pa_count)) {
4286 /*
4287 * This is the pa that we just used
4288 * for block allocation. So don't
4289 * free that
4290 */
4291 spin_unlock(&pa->pa_lock);
4292 continue;
4293 }
4294 if (pa->pa_deleted) {
4295 spin_unlock(&pa->pa_lock);
4296 continue;
4297 }
4298 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004299 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004300
4301 /* seems this one can be freed ... */
4302 pa->pa_deleted = 1;
4303 spin_unlock(&pa->pa_lock);
4304
4305 list_del_rcu(&pa->pa_inode_list);
4306 list_add(&pa->u.pa_tmp_list, &discard_list);
4307
4308 total_entries--;
4309 if (total_entries <= 5) {
4310 /*
4311 * we want to keep only 5 entries
4312 * allowing it to grow to 8. This
4313 * mak sure we don't call discard
4314 * soon for this list.
4315 */
4316 break;
4317 }
4318 }
4319 spin_unlock(&lg->lg_prealloc_lock);
4320
4321 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004322 int err;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004323
Lukas Czernerbd862982013-04-03 23:32:34 -04004324 group = ext4_get_group_number(sb, pa->pa_pstart);
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004325 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4326 GFP_NOFS|__GFP_NOFAIL);
4327 if (err) {
4328 ext4_error(sb, "Error %d loading buddy information for %u",
4329 err, group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004330 continue;
4331 }
4332 ext4_lock_group(sb, group);
4333 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004334 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004335 ext4_unlock_group(sb, group);
4336
Jing Zhange39e07f2010-05-14 00:00:00 -04004337 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004338 list_del(&pa->u.pa_tmp_list);
4339 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4340 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004341}
4342
4343/*
4344 * We have incremented pa_count. So it cannot be freed at this
4345 * point. Also we hold lg_mutex. So no parallel allocation is
4346 * possible from this lg. That means pa_free cannot be updated.
4347 *
4348 * A parallel ext4_mb_discard_group_preallocations is possible.
4349 * which can cause the lg_prealloc_list to be updated.
4350 */
4351
4352static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4353{
4354 int order, added = 0, lg_prealloc_count = 1;
4355 struct super_block *sb = ac->ac_sb;
4356 struct ext4_locality_group *lg = ac->ac_lg;
4357 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4358
4359 order = fls(pa->pa_free) - 1;
4360 if (order > PREALLOC_TB_SIZE - 1)
4361 /* The max size of hash table is PREALLOC_TB_SIZE */
4362 order = PREALLOC_TB_SIZE - 1;
4363 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004364 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004365 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4366 pa_inode_list) {
4367 spin_lock(&tmp_pa->pa_lock);
4368 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004369 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004370 continue;
4371 }
4372 if (!added && pa->pa_free < tmp_pa->pa_free) {
4373 /* Add to the tail of the previous entry */
4374 list_add_tail_rcu(&pa->pa_inode_list,
4375 &tmp_pa->pa_inode_list);
4376 added = 1;
4377 /*
4378 * we want to count the total
4379 * number of entries in the list
4380 */
4381 }
4382 spin_unlock(&tmp_pa->pa_lock);
4383 lg_prealloc_count++;
4384 }
4385 if (!added)
4386 list_add_tail_rcu(&pa->pa_inode_list,
4387 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004388 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004389
4390 /* Now trim the list to be not more than 8 elements */
4391 if (lg_prealloc_count > 8) {
4392 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004393 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004394 return;
4395 }
4396 return ;
4397}
4398
Alex Tomasc9de5602008-01-29 00:19:52 -05004399/*
4400 * release all resource we used in allocation
4401 */
4402static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4403{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004404 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004405 struct ext4_prealloc_space *pa = ac->ac_pa;
4406 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004407 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004408 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004409 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004410 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4411 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004412 pa->pa_free -= ac->ac_b_ex.fe_len;
4413 pa->pa_len -= ac->ac_b_ex.fe_len;
4414 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004415 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004416 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004417 if (pa) {
4418 /*
4419 * We want to add the pa to the right bucket.
4420 * Remove it from the list and while adding
4421 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004422 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004423 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004424 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004425 spin_lock(pa->pa_obj_lock);
4426 list_del_rcu(&pa->pa_inode_list);
4427 spin_unlock(pa->pa_obj_lock);
4428 ext4_mb_add_n_trim(ac);
4429 }
4430 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4431 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004432 if (ac->ac_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004433 put_page(ac->ac_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004434 if (ac->ac_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004435 put_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004436 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4437 mutex_unlock(&ac->ac_lg->lg_mutex);
4438 ext4_mb_collect_stats(ac);
4439 return 0;
4440}
4441
4442static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4443{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004444 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004445 int ret;
4446 int freed = 0;
4447
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004448 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004449 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004450 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4451 freed += ret;
4452 needed -= ret;
4453 }
4454
4455 return freed;
4456}
4457
4458/*
4459 * Main entry point into mballoc to allocate blocks
4460 * it tries to use preallocation first, then falls back
4461 * to usual allocation
4462 */
4463ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004464 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004465{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004466 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004467 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004468 struct ext4_sb_info *sbi;
4469 struct super_block *sb;
4470 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004471 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004472 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004473
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004474 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004475 sb = ar->inode->i_sb;
4476 sbi = EXT4_SB(sb);
4477
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004478 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004479
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004480 /* Allow to use superuser reservation for quota file */
4481 if (IS_NOQUOTA(ar->inode))
4482 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4483
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004484 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004485 /* Without delayed allocation we need to verify
4486 * there is enough free blocks to do block allocation
4487 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004488 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004489 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004490 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004491
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004492 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004493 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004494 ar->len = ar->len >> 1;
4495 }
4496 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004497 *errp = -ENOSPC;
4498 return 0;
4499 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004500 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004501 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004502 dquot_alloc_block_nofail(ar->inode,
4503 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004504 } else {
4505 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004506 dquot_alloc_block(ar->inode,
4507 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004508
4509 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4510 ar->len--;
4511 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004512 }
4513 inquota = ar->len;
4514 if (ar->len == 0) {
4515 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004516 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004517 }
Mingming Caod2a17632008-07-14 17:52:37 -04004518 }
Mingming Caod2a17632008-07-14 17:52:37 -04004519
Wei Yongjun85556c92012-09-26 20:43:37 -04004520 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004521 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004522 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004523 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004524 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004525 }
4526
Eric Sandeen256bdb42008-02-10 01:13:33 -05004527 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004528 if (*errp) {
4529 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004530 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004531 }
4532
Eric Sandeen256bdb42008-02-10 01:13:33 -05004533 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4534 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004535 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4536 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004537repeat:
4538 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004539 *errp = ext4_mb_regular_allocator(ac);
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004540 if (*errp)
4541 goto discard_and_exit;
4542
4543 /* as we've just preallocated more space than
4544 * user requested originally, we store allocated
4545 * space in a special descriptor */
4546 if (ac->ac_status == AC_STATUS_FOUND &&
4547 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4548 *errp = ext4_mb_new_preallocation(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004549 if (*errp) {
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004550 discard_and_exit:
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004551 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004552 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004553 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004554 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004555 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004556 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Vegard Nossum554a5cc2016-07-14 23:02:47 -04004557 if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004558 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004559 goto errout;
4560 } else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004561 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4562 ar->len = ac->ac_b_ex.fe_len;
4563 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004564 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004565 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004566 if (freed)
4567 goto repeat;
4568 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004569 }
4570
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004571errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004572 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004573 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004574 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004575 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004576 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004577 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004578out:
4579 if (ac)
4580 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004581 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004582 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004583 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004584 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004585 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004586 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004587 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004588 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004589
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004590 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004591
Alex Tomasc9de5602008-01-29 00:19:52 -05004592 return block;
4593}
Alex Tomasc9de5602008-01-29 00:19:52 -05004594
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004595/*
4596 * We can merge two free data extents only if the physical blocks
4597 * are contiguous, AND the extents were freed by the same transaction,
4598 * AND the blocks are associated with the same group.
4599 */
4600static int can_merge(struct ext4_free_data *entry1,
4601 struct ext4_free_data *entry2)
4602{
Bobi Jam18aadd42012-02-20 17:53:02 -05004603 if ((entry1->efd_tid == entry2->efd_tid) &&
4604 (entry1->efd_group == entry2->efd_group) &&
4605 ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004606 return 1;
4607 return 0;
4608}
4609
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004610static noinline_for_stack int
4611ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004612 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004613{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004614 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004615 ext4_grpblk_t cluster;
Theodore Ts'od08854f2016-06-26 18:24:01 -04004616 ext4_grpblk_t clusters = new_entry->efd_count;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004617 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004618 struct ext4_group_info *db = e4b->bd_info;
4619 struct super_block *sb = e4b->bd_sb;
4620 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004621 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4622 struct rb_node *parent = NULL, *new_node;
4623
Frank Mayhar03901312009-01-07 00:06:22 -05004624 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004625 BUG_ON(e4b->bd_bitmap_page == NULL);
4626 BUG_ON(e4b->bd_buddy_page == NULL);
4627
Bobi Jam18aadd42012-02-20 17:53:02 -05004628 new_node = &new_entry->efd_node;
4629 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004630
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004631 if (!*n) {
4632 /* first free block exent. We need to
4633 protect buddy cache from being freed,
4634 * otherwise we'll refresh it from
4635 * on-disk bitmap and lose not-yet-available
4636 * blocks */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004637 get_page(e4b->bd_buddy_page);
4638 get_page(e4b->bd_bitmap_page);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004639 }
4640 while (*n) {
4641 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004642 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4643 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004644 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004645 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004646 n = &(*n)->rb_right;
4647 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004648 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004649 ext4_group_first_block_no(sb, group) +
4650 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004651 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004652 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004653 }
4654 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004655
4656 rb_link_node(new_node, parent, n);
4657 rb_insert_color(new_node, &db->bb_free_root);
4658
4659 /* Now try to see the extent can be merged to left and right */
4660 node = rb_prev(new_node);
4661 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004662 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004663 if (can_merge(entry, new_entry) &&
4664 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004665 new_entry->efd_start_cluster = entry->efd_start_cluster;
4666 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004667 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004668 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004669 }
4670 }
4671
4672 node = rb_next(new_node);
4673 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004674 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004675 if (can_merge(new_entry, entry) &&
4676 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004677 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004678 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004679 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004680 }
4681 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004682 /* Add the extent to transaction's private list */
Theodore Ts'od08854f2016-06-26 18:24:01 -04004683 new_entry->efd_jce.jce_func = ext4_free_data_callback;
4684 spin_lock(&sbi->s_md_lock);
4685 _ext4_journal_callback_add(handle, &new_entry->efd_jce);
4686 sbi->s_mb_free_pending += clusters;
4687 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004688 return 0;
4689}
4690
Theodore Ts'o44338712009-11-22 07:44:56 -05004691/**
4692 * ext4_free_blocks() -- Free given blocks and update quota
4693 * @handle: handle for this transaction
4694 * @inode: inode
4695 * @block: start physical block to free
4696 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004697 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004698 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004699void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004700 struct buffer_head *bh, ext4_fsblk_t block,
4701 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004702{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004703 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004704 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004705 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004706 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004707 ext4_grpblk_t bit;
4708 struct buffer_head *gd_bh;
4709 ext4_group_t block_group;
4710 struct ext4_sb_info *sbi;
4711 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004712 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004713 int err = 0;
4714 int ret;
4715
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004716 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004717 if (bh) {
4718 if (block)
4719 BUG_ON(block != bh->b_blocknr);
4720 else
4721 block = bh->b_blocknr;
4722 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004723
Alex Tomasc9de5602008-01-29 00:19:52 -05004724 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004725 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4726 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004727 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004728 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004729 goto error_return;
4730 }
4731
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004732 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004733 trace_ext4_free_blocks(inode, block, count, flags);
4734
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004735 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4736 BUG_ON(count > 1);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004737
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004738 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4739 inode, bh, block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004740 }
4741
Theodore Ts'o60e66792010-05-17 07:00:00 -04004742 /*
Theodore Ts'o84130192011-09-09 18:50:51 -04004743 * If the extent to be freed does not begin on a cluster
4744 * boundary, we need to deal with partial clusters at the
4745 * beginning and end of the extent. Normally we will free
4746 * blocks at the beginning or the end unless we are explicitly
4747 * requested to avoid doing so.
4748 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004749 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004750 if (overflow) {
4751 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4752 overflow = sbi->s_cluster_ratio - overflow;
4753 block += overflow;
4754 if (count > overflow)
4755 count -= overflow;
4756 else
4757 return;
4758 } else {
4759 block -= overflow;
4760 count += overflow;
4761 }
4762 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004763 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004764 if (overflow) {
4765 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4766 if (count > overflow)
4767 count -= overflow;
4768 else
4769 return;
4770 } else
4771 count += sbi->s_cluster_ratio - overflow;
4772 }
4773
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004774 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4775 int i;
Daeho Jeongf96c4502016-02-21 18:31:41 -05004776 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004777
4778 for (i = 0; i < count; i++) {
4779 cond_resched();
Daeho Jeongf96c4502016-02-21 18:31:41 -05004780 if (is_metadata)
4781 bh = sb_find_get_block(inode->i_sb, block + i);
4782 ext4_forget(handle, is_metadata, inode, bh, block + i);
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004783 }
4784 }
4785
Alex Tomasc9de5602008-01-29 00:19:52 -05004786do_more:
4787 overflow = 0;
4788 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4789
Darrick J. Wong163a2032013-08-28 17:35:51 -04004790 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4791 ext4_get_group_info(sb, block_group))))
4792 return;
4793
Alex Tomasc9de5602008-01-29 00:19:52 -05004794 /*
4795 * Check to see if we are freeing blocks across a group
4796 * boundary.
4797 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004798 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4799 overflow = EXT4_C2B(sbi, bit) + count -
4800 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004801 count -= overflow;
4802 }
Lukas Czerner810da242013-03-02 17:18:58 -05004803 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004804 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004805 if (IS_ERR(bitmap_bh)) {
4806 err = PTR_ERR(bitmap_bh);
4807 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004808 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004809 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004810 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004811 if (!gdp) {
4812 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004813 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004814 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004815
4816 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4817 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4818 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004819 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004820 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004821 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004822
Eric Sandeen12062dd2010-02-15 14:19:27 -05004823 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004824 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004825 /* err = 0. ext4_std_error should be a no op */
4826 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004827 }
4828
4829 BUFFER_TRACE(bitmap_bh, "getting write access");
4830 err = ext4_journal_get_write_access(handle, bitmap_bh);
4831 if (err)
4832 goto error_return;
4833
4834 /*
4835 * We are about to modify some metadata. Call the journal APIs
4836 * to unshare ->b_data if a currently-committing transaction is
4837 * using it
4838 */
4839 BUFFER_TRACE(gd_bh, "get_write_access");
4840 err = ext4_journal_get_write_access(handle, gd_bh);
4841 if (err)
4842 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004843#ifdef AGGRESSIVE_CHECK
4844 {
4845 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004846 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004847 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4848 }
4849#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004850 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004851
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04004852 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
4853 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
4854 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004855 if (err)
4856 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004857
Daeho Jeongf96c4502016-02-21 18:31:41 -05004858 /*
4859 * We need to make sure we don't reuse the freed block until after the
4860 * transaction is committed. We make an exception if the inode is to be
4861 * written in writeback mode since writeback mode has weak data
4862 * consistency guarantees.
4863 */
4864 if (ext4_handle_valid(handle) &&
4865 ((flags & EXT4_FREE_BLOCKS_METADATA) ||
4866 !ext4_should_writeback_data(inode))) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004867 struct ext4_free_data *new_entry;
4868 /*
Michal Hocko7444a072015-07-05 12:33:44 -04004869 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4870 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004871 */
Michal Hocko7444a072015-07-05 12:33:44 -04004872 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
4873 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05004874 new_entry->efd_start_cluster = bit;
4875 new_entry->efd_group = block_group;
4876 new_entry->efd_count = count_clusters;
4877 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004878
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004879 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004880 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004881 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004882 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004883 /* need to update group_info->bb_free and bitmap
4884 * with group lock held. generate_buddy look at
4885 * them with group lock_held
4886 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004887 if (test_opt(sb, DISCARD)) {
JP Abgrallcc14b9e2014-07-23 16:55:07 -07004888 err = ext4_issue_discard(sb, block_group, bit, count,
4889 0);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004890 if (err && err != -EOPNOTSUPP)
4891 ext4_msg(sb, KERN_WARNING, "discard request in"
4892 " group:%d block:%d count:%lu failed"
4893 " with %d", block_group, bit, count,
4894 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04004895 } else
4896 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004897
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004898 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004899 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4900 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004901 }
4902
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004903 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4904 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004905 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004906 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004907 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004908
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004909 if (sbi->s_log_groups_per_flex) {
4910 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004911 atomic64_add(count_clusters,
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08004912 &sbi_array_rcu_deref(sbi, s_flex_groups,
4913 flex_group)->free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004914 }
4915
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04004916 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004917 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
Jan Kara7d734532013-08-17 09:36:54 -04004918 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
4919
4920 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004921
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004922 /* We dirtied the bitmap block */
4923 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4924 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4925
Alex Tomasc9de5602008-01-29 00:19:52 -05004926 /* And the group descriptor block */
4927 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004928 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004929 if (!err)
4930 err = ret;
4931
4932 if (overflow && !err) {
4933 block += count;
4934 count = overflow;
4935 put_bh(bitmap_bh);
4936 goto do_more;
4937 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004938error_return:
4939 brelse(bitmap_bh);
4940 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004941 return;
4942}
Lukas Czerner7360d172010-10-27 21:30:12 -04004943
4944/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004945 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004946 * @handle: handle to this transaction
4947 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004948 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004949 * @count: number of blocks to free
4950 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004951 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004952 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004953int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004954 ext4_fsblk_t block, unsigned long count)
4955{
4956 struct buffer_head *bitmap_bh = NULL;
4957 struct buffer_head *gd_bh;
4958 ext4_group_t block_group;
4959 ext4_grpblk_t bit;
4960 unsigned int i;
4961 struct ext4_group_desc *desc;
4962 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004963 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004964 int err = 0, ret, blk_free_count;
4965 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004966
4967 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4968
Yongqiang Yang4740b832011-07-26 21:51:08 -04004969 if (count == 0)
4970 return 0;
4971
Amir Goldstein2846e822011-05-09 10:46:41 -04004972 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004973 /*
4974 * Check to see if we are freeing blocks across a group
4975 * boundary.
4976 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004977 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04004978 ext4_warning(sb, "too much blocks added to group %u",
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004979 block_group);
4980 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004981 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004982 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004983
Amir Goldstein2846e822011-05-09 10:46:41 -04004984 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004985 if (IS_ERR(bitmap_bh)) {
4986 err = PTR_ERR(bitmap_bh);
4987 bitmap_bh = NULL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004988 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004989 }
4990
Amir Goldstein2846e822011-05-09 10:46:41 -04004991 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004992 if (!desc) {
4993 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004994 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004995 }
Amir Goldstein2846e822011-05-09 10:46:41 -04004996
4997 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4998 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4999 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
5000 in_range(block + count - 1, ext4_inode_table(sb, desc),
5001 sbi->s_itb_per_group)) {
5002 ext4_error(sb, "Adding blocks in system zones - "
5003 "Block = %llu, count = %lu",
5004 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005005 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04005006 goto error_return;
5007 }
5008
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04005009 BUFFER_TRACE(bitmap_bh, "getting write access");
5010 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04005011 if (err)
5012 goto error_return;
5013
5014 /*
5015 * We are about to modify some metadata. Call the journal APIs
5016 * to unshare ->b_data if a currently-committing transaction is
5017 * using it
5018 */
5019 BUFFER_TRACE(gd_bh, "get_write_access");
5020 err = ext4_journal_get_write_access(handle, gd_bh);
5021 if (err)
5022 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04005023
Amir Goldstein2846e822011-05-09 10:46:41 -04005024 for (i = 0, blocks_freed = 0; i < count; i++) {
5025 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04005026 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005027 ext4_error(sb, "bit already cleared for block %llu",
5028 (ext4_fsblk_t)(block + i));
5029 BUFFER_TRACE(bitmap_bh, "bit already cleared");
5030 } else {
5031 blocks_freed++;
5032 }
5033 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005034
5035 err = ext4_mb_load_buddy(sb, block_group, &e4b);
5036 if (err)
5037 goto error_return;
5038
5039 /*
5040 * need to update group_info->bb_free and bitmap
5041 * with group lock held. generate_buddy look at
5042 * them with group lock_held
5043 */
Amir Goldstein2846e822011-05-09 10:46:41 -04005044 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04005045 mb_clear_bits(bitmap_bh->b_data, bit, count);
5046 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04005047 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
5048 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04005049 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005050 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04005051 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04005052 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05005053 EXT4_NUM_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04005054
5055 if (sbi->s_log_groups_per_flex) {
5056 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04005057 atomic64_add(EXT4_NUM_B2C(sbi, blocks_freed),
Suraj Jitindar Singh277bc962020-02-28 16:51:19 -08005058 &sbi_array_rcu_deref(sbi, s_flex_groups,
5059 flex_group)->free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04005060 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005061
5062 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005063
5064 /* We dirtied the bitmap block */
5065 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5066 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5067
5068 /* And the group descriptor block */
5069 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5070 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5071 if (!err)
5072 err = ret;
5073
5074error_return:
5075 brelse(bitmap_bh);
5076 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005077 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005078}
5079
5080/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005081 * ext4_trim_extent -- function to TRIM one single free extent in the group
5082 * @sb: super block for the file system
5083 * @start: starting block of the free extent in the alloc. group
5084 * @count: number of blocks to TRIM
5085 * @group: alloc. group we are working with
5086 * @e4b: ext4 buddy for the group
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005087 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005088 *
5089 * Trim "count" blocks starting at "start" in the "group". To assure that no
5090 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5091 * be called with under the group lock.
5092 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005093static int ext4_trim_extent(struct super_block *sb, int start, int count,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005094 ext4_group_t group, struct ext4_buddy *e4b,
5095 unsigned long blkdev_flags)
jon ernste2cbd582014-04-12 23:01:28 -04005096__releases(bitlock)
5097__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005098{
5099 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005100 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005101
Tao Mab3d4c2b2011-07-11 00:01:52 -04005102 trace_ext4_trim_extent(sb, group, start, count);
5103
Lukas Czerner7360d172010-10-27 21:30:12 -04005104 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5105
5106 ex.fe_start = start;
5107 ex.fe_group = group;
5108 ex.fe_len = count;
5109
5110 /*
5111 * Mark blocks used, so no one can reuse them while
5112 * being trimmed.
5113 */
5114 mb_mark_used(e4b, &ex);
5115 ext4_unlock_group(sb, group);
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005116 ret = ext4_issue_discard(sb, group, start, count, blkdev_flags);
Lukas Czerner7360d172010-10-27 21:30:12 -04005117 ext4_lock_group(sb, group);
5118 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005119 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005120}
5121
5122/**
5123 * ext4_trim_all_free -- function to trim all free space in alloc. group
5124 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005125 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005126 * @start: first group block to examine
5127 * @max: last group block to examine
5128 * @minblocks: minimum extent block count
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005129 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005130 *
5131 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5132 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5133 * the extent.
5134 *
5135 *
5136 * ext4_trim_all_free walks through group's block bitmap searching for free
5137 * extents. When the free extent is found, mark it as used in group buddy
5138 * bitmap. Then issue a TRIM command on this extent and free the extent in
5139 * the group buddy bitmap. This is done until whole group is scanned.
5140 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005141static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005142ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5143 ext4_grpblk_t start, ext4_grpblk_t max,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005144 ext4_grpblk_t minblocks, unsigned long blkdev_flags)
Lukas Czerner7360d172010-10-27 21:30:12 -04005145{
5146 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005147 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005148 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005149 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005150
Tao Mab3d4c2b2011-07-11 00:01:52 -04005151 trace_ext4_trim_all_free(sb, group, start, max);
5152
Lukas Czerner78944082011-05-24 18:16:27 -04005153 ret = ext4_mb_load_buddy(sb, group, &e4b);
5154 if (ret) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04005155 ext4_warning(sb, "Error %d loading buddy information for %u",
5156 ret, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005157 return ret;
5158 }
Lukas Czerner78944082011-05-24 18:16:27 -04005159 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005160
5161 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005162 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5163 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5164 goto out;
5165
Lukas Czerner78944082011-05-24 18:16:27 -04005166 start = (e4b.bd_info->bb_first_free > start) ?
5167 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005168
Lukas Czerner913eed832012-03-21 21:22:22 -04005169 while (start <= max) {
5170 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5171 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005172 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005173 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005174
5175 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005176 ret = ext4_trim_extent(sb, start,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005177 next - start, group, &e4b,
5178 blkdev_flags);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005179 if (ret && ret != -EOPNOTSUPP)
5180 break;
5181 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005182 count += next - start;
5183 }
Tao Ma169ddc32011-07-11 00:00:07 -04005184 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005185 start = next + 1;
5186
5187 if (fatal_signal_pending(current)) {
5188 count = -ERESTARTSYS;
5189 break;
5190 }
5191
5192 if (need_resched()) {
5193 ext4_unlock_group(sb, group);
5194 cond_resched();
5195 ext4_lock_group(sb, group);
5196 }
5197
Tao Ma169ddc32011-07-11 00:00:07 -04005198 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005199 break;
5200 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005201
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005202 if (!ret) {
5203 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005204 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005205 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005206out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005207 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005208 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005209
5210 ext4_debug("trimmed %d blocks in the group %d\n",
5211 count, group);
5212
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005213 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005214}
5215
5216/**
5217 * ext4_trim_fs() -- trim ioctl handle function
5218 * @sb: superblock for filesystem
5219 * @range: fstrim_range structure
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005220 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005221 *
5222 * start: First Byte to trim
5223 * len: number of Bytes to trim from start
5224 * minlen: minimum extent length in Bytes
5225 * ext4_trim_fs goes through all allocation groups containing Bytes from
5226 * start to start+len. For each such a group ext4_trim_all_free function
5227 * is invoked to trim all free space.
5228 */
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005229int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range,
5230 unsigned long blkdev_flags)
Lukas Czerner7360d172010-10-27 21:30:12 -04005231{
Lukas Czerner78944082011-05-24 18:16:27 -04005232 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005233 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005234 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005235 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005236 ext4_fsblk_t first_data_blk =
5237 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005238 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005239 int ret = 0;
5240
5241 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005242 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005243 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5244 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005245
Lukas Czerner5de35e82012-10-22 18:01:19 -04005246 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5247 start >= max_blks ||
5248 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005249 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005250 if (end >= max_blks)
5251 end = max_blks - 1;
5252 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005253 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005254 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005255 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005256
Lukas Czerner913eed832012-03-21 21:22:22 -04005257 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005258 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005259 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005260 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005261 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005262
Lukas Czerner913eed832012-03-21 21:22:22 -04005263 /* end now represents the last cluster to discard in this group */
5264 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005265
5266 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005267 grp = ext4_get_group_info(sb, group);
5268 /* We only do this if the grp has never been initialized */
5269 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005270 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Lukas Czerner78944082011-05-24 18:16:27 -04005271 if (ret)
5272 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005273 }
5274
Tao Ma0ba08512011-03-23 15:48:11 -04005275 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005276 * For all the groups except the last one, last cluster will
5277 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5278 * change it for the last group, note that last_cluster is
5279 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005280 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005281 if (group == last_group)
5282 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005283
Lukas Czerner78944082011-05-24 18:16:27 -04005284 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005285 cnt = ext4_trim_all_free(sb, group, first_cluster,
JP Abgrallcc14b9e2014-07-23 16:55:07 -07005286 end, minlen, blkdev_flags);
Lukas Czerner7360d172010-10-27 21:30:12 -04005287 if (cnt < 0) {
5288 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005289 break;
5290 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005291 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005292 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005293
5294 /*
5295 * For every group except the first one, we are sure
5296 * that the first cluster to discard will be cluster #0.
5297 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005298 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005299 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005300
Tao Ma3d56b8d2011-07-11 00:03:38 -04005301 if (!ret)
5302 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5303
Tao Ma22f10452011-07-10 23:52:37 -04005304out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005305 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005306 return ret;
5307}