blob: a49d0e5d7baf8d4e99251b4748216a10d008c418 [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;
2380 struct ext4_group_info ***new_groupinfo;
2381
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 }
2393 if (sbi->s_group_info) {
2394 memcpy(new_groupinfo, sbi->s_group_info,
2395 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
Al Virob93b41d2014-11-20 12:19:11 -05002396 kvfree(sbi->s_group_info);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002397 }
2398 sbi->s_group_info = new_groupinfo;
2399 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
2400 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2401 sbi->s_group_info_size);
2402 return 0;
2403}
2404
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002405/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002406int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002407 struct ext4_group_desc *desc)
2408{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002409 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002410 int metalen = 0;
2411 struct ext4_sb_info *sbi = EXT4_SB(sb);
2412 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002413 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002414
2415 /*
2416 * First check if this group is the first of a reserved block.
2417 * If it's true, we have to allocate a new table of pointers
2418 * to ext4_group_info structures
2419 */
2420 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2421 metalen = sizeof(*meta_group_info) <<
2422 EXT4_DESC_PER_BLOCK_BITS(sb);
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002423 meta_group_info = kmalloc(metalen, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002424 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002425 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002426 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002427 goto exit_meta_group_info;
2428 }
2429 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2430 meta_group_info;
2431 }
2432
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002433 meta_group_info =
2434 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2435 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2436
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002437 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002438 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002439 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002440 goto exit_group_info;
2441 }
2442 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2443 &(meta_group_info[i]->bb_state));
2444
2445 /*
2446 * initialize bb_free to be able to skip
2447 * empty groups without initialization
2448 */
Theodore Ts'o5ae57322018-06-14 00:58:00 -04002449 if (ext4_has_group_desc_csum(sb) &&
2450 (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002451 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002452 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002453 } else {
2454 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002455 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002456 }
2457
2458 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002459 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002460 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002461 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002462
2463#ifdef DOUBLE_CHECK
2464 {
2465 struct buffer_head *bh;
2466 meta_group_info[i]->bb_bitmap =
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002467 kmalloc(sb->s_blocksize, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002468 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2469 bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002470 BUG_ON(IS_ERR_OR_NULL(bh));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002471 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2472 sb->s_blocksize);
2473 put_bh(bh);
2474 }
2475#endif
2476
2477 return 0;
2478
2479exit_group_info:
2480 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002481 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002482 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002483 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2484 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002485exit_meta_group_info:
2486 return -ENOMEM;
2487} /* ext4_mb_add_groupinfo */
2488
Alex Tomasc9de5602008-01-29 00:19:52 -05002489static int ext4_mb_init_backend(struct super_block *sb)
2490{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002491 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002492 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002493 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002494 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002495 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002496 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002497
Theodore Ts'o28623c22012-09-05 01:31:50 -04002498 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2499 if (err)
2500 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002501
Alex Tomasc9de5602008-01-29 00:19:52 -05002502 sbi->s_buddy_cache = new_inode(sb);
2503 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002504 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002505 goto err_freesgi;
2506 }
Yu Jian48e60612011-08-01 17:41:39 -04002507 /* To avoid potentially colliding with an valid on-disk inode number,
2508 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2509 * not in the inode hash, so it should never be found by iget(), but
2510 * this will avoid confusion if it ever shows up during debugging. */
2511 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002512 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002513 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002514 desc = ext4_get_group_desc(sb, i, NULL);
2515 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002516 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002517 goto err_freebuddy;
2518 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002519 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2520 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002521 }
2522
2523 return 0;
2524
2525err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002526 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002527 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002528 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002529 i = sbi->s_group_info_size;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002530 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002531 kfree(sbi->s_group_info[i]);
2532 iput(sbi->s_buddy_cache);
2533err_freesgi:
Al Virob93b41d2014-11-20 12:19:11 -05002534 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002535 return -ENOMEM;
2536}
2537
Eric Sandeen2892c152011-02-12 08:12:18 -05002538static void ext4_groupinfo_destroy_slabs(void)
2539{
2540 int i;
2541
2542 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2543 if (ext4_groupinfo_caches[i])
2544 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2545 ext4_groupinfo_caches[i] = NULL;
2546 }
2547}
2548
2549static int ext4_groupinfo_create_slab(size_t size)
2550{
2551 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2552 int slab_size;
2553 int blocksize_bits = order_base_2(size);
2554 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2555 struct kmem_cache *cachep;
2556
2557 if (cache_index >= NR_GRPINFO_CACHES)
2558 return -EINVAL;
2559
2560 if (unlikely(cache_index < 0))
2561 cache_index = 0;
2562
2563 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2564 if (ext4_groupinfo_caches[cache_index]) {
2565 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2566 return 0; /* Already created */
2567 }
2568
2569 slab_size = offsetof(struct ext4_group_info,
2570 bb_counters[blocksize_bits + 2]);
2571
2572 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2573 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2574 NULL);
2575
Tao Ma823ba012011-07-11 18:26:01 -04002576 ext4_groupinfo_caches[cache_index] = cachep;
2577
Eric Sandeen2892c152011-02-12 08:12:18 -05002578 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2579 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002580 printk(KERN_EMERG
2581 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002582 return -ENOMEM;
2583 }
2584
Eric Sandeen2892c152011-02-12 08:12:18 -05002585 return 0;
2586}
2587
Akira Fujita9d990122012-05-28 14:19:25 -04002588int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002589{
2590 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002591 unsigned i, j;
Nicolai Stange935244c2016-05-05 19:46:19 -04002592 unsigned offset, offset_incr;
Alex Tomasc9de5602008-01-29 00:19:52 -05002593 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002594 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002595
Eric Sandeen19278052009-08-25 22:36:25 -04002596 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002597
2598 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2599 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002600 ret = -ENOMEM;
2601 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002602 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002603
Eric Sandeen19278052009-08-25 22:36:25 -04002604 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002605 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2606 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002607 ret = -ENOMEM;
2608 goto out;
2609 }
2610
Eric Sandeen2892c152011-02-12 08:12:18 -05002611 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2612 if (ret < 0)
2613 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002614
2615 /* order 0 is regular bitmap */
2616 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2617 sbi->s_mb_offsets[0] = 0;
2618
2619 i = 1;
2620 offset = 0;
Nicolai Stange935244c2016-05-05 19:46:19 -04002621 offset_incr = 1 << (sb->s_blocksize_bits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05002622 max = sb->s_blocksize << 2;
2623 do {
2624 sbi->s_mb_offsets[i] = offset;
2625 sbi->s_mb_maxs[i] = max;
Nicolai Stange935244c2016-05-05 19:46:19 -04002626 offset += offset_incr;
2627 offset_incr = offset_incr >> 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002628 max = max >> 1;
2629 i++;
2630 } while (i <= sb->s_blocksize_bits + 1);
2631
Alex Tomasc9de5602008-01-29 00:19:52 -05002632 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002633 spin_lock_init(&sbi->s_bal_lock);
Theodore Ts'od08854f2016-06-26 18:24:01 -04002634 sbi->s_mb_free_pending = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002635
2636 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2637 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2638 sbi->s_mb_stats = MB_DEFAULT_STATS;
2639 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2640 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002641 /*
2642 * The default group preallocation is 512, which for 4k block
2643 * sizes translates to 2 megabytes. However for bigalloc file
2644 * systems, this is probably too big (i.e, if the cluster size
2645 * is 1 megabyte, then group preallocation size becomes half a
2646 * gigabyte!). As a default, we will keep a two megabyte
2647 * group pralloc size for cluster sizes up to 64k, and after
2648 * that, we will force a minimum group preallocation size of
2649 * 32 clusters. This translates to 8 megs when the cluster
2650 * size is 256k, and 32 megs when the cluster size is 1 meg,
2651 * which seems reasonable as a default.
2652 */
2653 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2654 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002655 /*
2656 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2657 * to the lowest multiple of s_stripe which is bigger than
2658 * the s_mb_group_prealloc as determined above. We want
2659 * the preallocation size to be an exact multiple of the
2660 * RAID stripe size so that preallocations don't fragment
2661 * the stripes.
2662 */
2663 if (sbi->s_stripe > 1) {
2664 sbi->s_mb_group_prealloc = roundup(
2665 sbi->s_mb_group_prealloc, sbi->s_stripe);
2666 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002667
Eric Sandeen730c2132008-09-13 15:23:29 -04002668 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002669 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002670 ret = -ENOMEM;
Andrey Tsyvarev029b10c2014-05-12 12:34:21 -04002671 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002672 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002673 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002674 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002675 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002676 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002677 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2678 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002679 spin_lock_init(&lg->lg_prealloc_lock);
2680 }
2681
Yu Jian79a77c52011-08-01 17:41:46 -04002682 /* init file for buddy data */
2683 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002684 if (ret != 0)
2685 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002686
Tao Ma7aa0bae2011-10-06 10:22:28 -04002687 return 0;
2688
2689out_free_locality_groups:
2690 free_percpu(sbi->s_locality_groups);
2691 sbi->s_locality_groups = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002692out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002693 kfree(sbi->s_mb_offsets);
2694 sbi->s_mb_offsets = NULL;
2695 kfree(sbi->s_mb_maxs);
2696 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002697 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002698}
2699
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002700/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002701static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2702{
2703 struct ext4_prealloc_space *pa;
2704 struct list_head *cur, *tmp;
2705 int count = 0;
2706
2707 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2708 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2709 list_del(&pa->pa_group_list);
2710 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002711 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002712 }
2713 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002714 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002715
2716}
2717
2718int ext4_mb_release(struct super_block *sb)
2719{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002720 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002721 ext4_group_t i;
2722 int num_meta_group_infos;
2723 struct ext4_group_info *grinfo;
2724 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002725 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002726
Alex Tomasc9de5602008-01-29 00:19:52 -05002727 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002728 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002729 grinfo = ext4_get_group_info(sb, i);
2730#ifdef DOUBLE_CHECK
2731 kfree(grinfo->bb_bitmap);
2732#endif
2733 ext4_lock_group(sb, i);
2734 ext4_mb_cleanup_pa(grinfo);
2735 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002736 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002737 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002738 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002739 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2740 EXT4_DESC_PER_BLOCK_BITS(sb);
2741 for (i = 0; i < num_meta_group_infos; i++)
2742 kfree(sbi->s_group_info[i]);
Al Virob93b41d2014-11-20 12:19:11 -05002743 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002744 }
2745 kfree(sbi->s_mb_offsets);
2746 kfree(sbi->s_mb_maxs);
Markus Elfringbfcba2d2014-11-25 20:01:37 -05002747 iput(sbi->s_buddy_cache);
Alex Tomasc9de5602008-01-29 00:19:52 -05002748 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002749 ext4_msg(sb, KERN_INFO,
2750 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002751 atomic_read(&sbi->s_bal_allocated),
2752 atomic_read(&sbi->s_bal_reqs),
2753 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002754 ext4_msg(sb, KERN_INFO,
2755 "mballoc: %u extents scanned, %u goal hits, "
2756 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002757 atomic_read(&sbi->s_bal_ex_scanned),
2758 atomic_read(&sbi->s_bal_goals),
2759 atomic_read(&sbi->s_bal_2orders),
2760 atomic_read(&sbi->s_bal_breaks),
2761 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002762 ext4_msg(sb, KERN_INFO,
2763 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002764 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002765 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002766 ext4_msg(sb, KERN_INFO,
2767 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002768 atomic_read(&sbi->s_mb_preallocated),
2769 atomic_read(&sbi->s_mb_discarded));
2770 }
2771
Eric Sandeen730c2132008-09-13 15:23:29 -04002772 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002773
2774 return 0;
2775}
2776
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002777static inline int ext4_issue_discard(struct super_block *sb,
Theodore Ts'o84130192011-09-09 18:50:51 -04002778 ext4_group_t block_group, ext4_grpblk_t cluster, int count)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002779{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002780 ext4_fsblk_t discard_block;
2781
Theodore Ts'o84130192011-09-09 18:50:51 -04002782 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2783 ext4_group_first_block_no(sb, block_group));
2784 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002785 trace_ext4_discard_blocks(sb,
2786 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002787 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002788}
2789
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002790/*
2791 * This function is called by the jbd2 layer once the commit has finished,
2792 * so we know we can free the blocks that were released with that commit.
2793 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002794static void ext4_free_data_callback(struct super_block *sb,
2795 struct ext4_journal_cb_entry *jce,
2796 int rc)
Alex Tomasc9de5602008-01-29 00:19:52 -05002797{
Bobi Jam18aadd42012-02-20 17:53:02 -05002798 struct ext4_free_data *entry = (struct ext4_free_data *)jce;
Alex Tomasc9de5602008-01-29 00:19:52 -05002799 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002800 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002801 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002802
Bobi Jam18aadd42012-02-20 17:53:02 -05002803 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2804 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002805
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002806 if (test_opt(sb, DISCARD)) {
2807 err = ext4_issue_discard(sb, entry->efd_group,
2808 entry->efd_start_cluster,
2809 entry->efd_count);
2810 if (err && err != -EOPNOTSUPP)
2811 ext4_msg(sb, KERN_WARNING, "discard request in"
2812 " group:%d block:%d count:%d failed"
2813 " with %d", entry->efd_group,
2814 entry->efd_start_cluster,
2815 entry->efd_count, err);
2816 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002817
Bobi Jam18aadd42012-02-20 17:53:02 -05002818 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2819 /* we expect to find existing buddy because it's pinned */
2820 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002821
Theodore Ts'od08854f2016-06-26 18:24:01 -04002822 spin_lock(&EXT4_SB(sb)->s_md_lock);
2823 EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
2824 spin_unlock(&EXT4_SB(sb)->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002825
Bobi Jam18aadd42012-02-20 17:53:02 -05002826 db = e4b.bd_info;
2827 /* there are blocks to put in buddy to make them really free */
2828 count += entry->efd_count;
2829 count2++;
2830 ext4_lock_group(sb, entry->efd_group);
2831 /* Take it out of per group rb tree */
2832 rb_erase(&entry->efd_node, &(db->bb_free_root));
2833 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002834
Bobi Jam18aadd42012-02-20 17:53:02 -05002835 /*
2836 * Clear the trimmed flag for the group so that the next
2837 * ext4_trim_fs can trim it.
2838 * If the volume is mounted with -o discard, online discard
2839 * is supported and the free blocks will be trimmed online.
2840 */
2841 if (!test_opt(sb, DISCARD))
2842 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2843
2844 if (!db->bb_free_root.rb_node) {
2845 /* No more items in the per group rb tree
2846 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002847 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002848 put_page(e4b.bd_buddy_page);
2849 put_page(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002850 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002851 ext4_unlock_group(sb, entry->efd_group);
2852 kmem_cache_free(ext4_free_data_cachep, entry);
2853 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002854
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002855 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002856}
2857
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002858int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002859{
Theodore Ts'o16828082010-10-27 21:30:09 -04002860 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2861 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002862 if (ext4_pspace_cachep == NULL)
2863 return -ENOMEM;
2864
Theodore Ts'o16828082010-10-27 21:30:09 -04002865 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2866 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002867 if (ext4_ac_cachep == NULL) {
2868 kmem_cache_destroy(ext4_pspace_cachep);
2869 return -ENOMEM;
2870 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002871
Bobi Jam18aadd42012-02-20 17:53:02 -05002872 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2873 SLAB_RECLAIM_ACCOUNT);
2874 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002875 kmem_cache_destroy(ext4_pspace_cachep);
2876 kmem_cache_destroy(ext4_ac_cachep);
2877 return -ENOMEM;
2878 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002879 return 0;
2880}
2881
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002882void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002883{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002884 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002885 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2886 * before destroying the slab cache.
2887 */
2888 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002889 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002890 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002891 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002892 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002893}
2894
2895
2896/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002897 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002898 * Returns 0 if success or error code
2899 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002900static noinline_for_stack int
2901ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002902 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002903{
2904 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002905 struct ext4_group_desc *gdp;
2906 struct buffer_head *gdp_bh;
2907 struct ext4_sb_info *sbi;
2908 struct super_block *sb;
2909 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002910 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002911
2912 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2913 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2914
2915 sb = ac->ac_sb;
2916 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002917
Theodore Ts'o574ca172008-07-11 19:27:31 -04002918 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04002919 if (IS_ERR(bitmap_bh)) {
2920 err = PTR_ERR(bitmap_bh);
2921 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002922 goto out_err;
Darrick J. Wong9008a582015-10-17 21:33:24 -04002923 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002924
liang xie5d601252014-05-12 22:06:43 -04002925 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002926 err = ext4_journal_get_write_access(handle, bitmap_bh);
2927 if (err)
2928 goto out_err;
2929
2930 err = -EIO;
2931 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2932 if (!gdp)
2933 goto out_err;
2934
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002935 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002936 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002937
liang xie5d601252014-05-12 22:06:43 -04002938 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002939 err = ext4_journal_get_write_access(handle, gdp_bh);
2940 if (err)
2941 goto out_err;
2942
Akinobu Mitabda00de2010-03-03 23:53:25 -05002943 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002944
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002945 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002946 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002947 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002948 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002949 /* File system mounted not to panic on error
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002950 * Fix the bitmap and return EFSCORRUPTED
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002951 * We leak some of the blocks here.
2952 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002953 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002954 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2955 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002956 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002957 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002958 if (!err)
Vegard Nossum554a5cc2016-07-14 23:02:47 -04002959 err = -EFSCORRUPTED;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002960 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002961 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002962
2963 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002964#ifdef AGGRESSIVE_CHECK
2965 {
2966 int i;
2967 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2968 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2969 bitmap_bh->b_data));
2970 }
2971 }
2972#endif
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);
Theodore Ts'o5ae57322018-06-14 00:58:00 -04002975 if (ext4_has_group_desc_csum(sb) &&
2976 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002977 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002978 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002979 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002980 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002981 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002982 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
2983 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04002984 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002985 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002986
2987 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04002988 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002989 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002990 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002991 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002992 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2993 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04002994 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
2995 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002996
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002997 if (sbi->s_log_groups_per_flex) {
2998 ext4_group_t flex_group = ext4_flex_group(sbi,
2999 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04003000 atomic64_sub(ac->ac_b_ex.fe_len,
3001 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003002 }
3003
Frank Mayhar03901312009-01-07 00:06:22 -05003004 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003005 if (err)
3006 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003007 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003008
3009out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003010 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003011 return err;
3012}
3013
3014/*
3015 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003016 * Group request are normalized to s_mb_group_prealloc, which goes to
3017 * s_strip if we set the same via mount option.
3018 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003019 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003020 *
3021 * XXX: should we try to preallocate more than the group has now?
3022 */
3023static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3024{
3025 struct super_block *sb = ac->ac_sb;
3026 struct ext4_locality_group *lg = ac->ac_lg;
3027
3028 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04003029 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003030 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003031 current->pid, ac->ac_g_ex.fe_len);
3032}
3033
3034/*
3035 * Normalization means making request better in terms of
3036 * size and alignment
3037 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003038static noinline_for_stack void
3039ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003040 struct ext4_allocation_request *ar)
3041{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003042 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003043 int bsbits, max;
3044 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003045 loff_t size, start_off;
3046 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003047 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003048 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003049 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003050
3051 /* do normalize only data requests, metadata requests
3052 do not need preallocation */
3053 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3054 return;
3055
3056 /* sometime caller may want exact blocks */
3057 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3058 return;
3059
3060 /* caller may indicate that preallocation isn't
3061 * required (it's a tail, for example) */
3062 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3063 return;
3064
3065 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3066 ext4_mb_normalize_group_request(ac);
3067 return ;
3068 }
3069
3070 bsbits = ac->ac_sb->s_blocksize_bits;
3071
3072 /* first, let's learn actual file size
3073 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003074 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003075 size = size << bsbits;
3076 if (size < i_size_read(ac->ac_inode))
3077 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003078 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003079
Valerie Clement19304792008-05-13 19:31:14 -04003080 /* max size of free chunks */
3081 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003082
Valerie Clement19304792008-05-13 19:31:14 -04003083#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3084 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003085
3086 /* first, try to predict filesize */
3087 /* XXX: should this table be tunable? */
3088 start_off = 0;
3089 if (size <= 16 * 1024) {
3090 size = 16 * 1024;
3091 } else if (size <= 32 * 1024) {
3092 size = 32 * 1024;
3093 } else if (size <= 64 * 1024) {
3094 size = 64 * 1024;
3095 } else if (size <= 128 * 1024) {
3096 size = 128 * 1024;
3097 } else if (size <= 256 * 1024) {
3098 size = 256 * 1024;
3099 } else if (size <= 512 * 1024) {
3100 size = 512 * 1024;
3101 } else if (size <= 1024 * 1024) {
3102 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003103 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003104 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003105 (21 - bsbits)) << 21;
3106 size = 2 * 1024 * 1024;
3107 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003108 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3109 (22 - bsbits)) << 22;
3110 size = 4 * 1024 * 1024;
3111 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003112 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003113 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3114 (23 - bsbits)) << 23;
3115 size = 8 * 1024 * 1024;
3116 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003117 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3118 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3119 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003120 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003121 size = size >> bsbits;
3122 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003123
3124 /* don't cover already allocated blocks in selected range */
3125 if (ar->pleft && start <= ar->lleft) {
3126 size -= ar->lleft + 1 - start;
3127 start = ar->lleft + 1;
3128 }
3129 if (ar->pright && start + size - 1 >= ar->lright)
3130 size -= start + size - ar->lright;
3131
Jan Karafc6c2da2017-01-27 14:34:30 -05003132 /*
3133 * Trim allocation request for filesystems with artificially small
3134 * groups.
3135 */
3136 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
3137 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
3138
Alex Tomasc9de5602008-01-29 00:19:52 -05003139 end = start + size;
3140
3141 /* check we don't cross already preallocated blocks */
3142 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003143 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003144 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003145
Alex Tomasc9de5602008-01-29 00:19:52 -05003146 if (pa->pa_deleted)
3147 continue;
3148 spin_lock(&pa->pa_lock);
3149 if (pa->pa_deleted) {
3150 spin_unlock(&pa->pa_lock);
3151 continue;
3152 }
3153
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003154 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3155 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003156
3157 /* PA must not overlap original request */
3158 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3159 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3160
Eric Sandeen38877f42009-08-17 23:55:24 -04003161 /* skip PAs this normalized request doesn't overlap with */
3162 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003163 spin_unlock(&pa->pa_lock);
3164 continue;
3165 }
3166 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3167
Eric Sandeen38877f42009-08-17 23:55:24 -04003168 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003169 if (pa_end <= ac->ac_o_ex.fe_logical) {
3170 BUG_ON(pa_end < start);
3171 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003172 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003173 BUG_ON(pa->pa_lstart > end);
3174 end = pa->pa_lstart;
3175 }
3176 spin_unlock(&pa->pa_lock);
3177 }
3178 rcu_read_unlock();
3179 size = end - start;
3180
3181 /* XXX: extra loop to check we really don't overlap preallocations */
3182 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003183 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003184 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003185
Alex Tomasc9de5602008-01-29 00:19:52 -05003186 spin_lock(&pa->pa_lock);
3187 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003188 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3189 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003190 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3191 }
3192 spin_unlock(&pa->pa_lock);
3193 }
3194 rcu_read_unlock();
3195
3196 if (start + size <= ac->ac_o_ex.fe_logical &&
3197 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003198 ext4_msg(ac->ac_sb, KERN_ERR,
3199 "start %lu, size %lu, fe_logical %lu",
3200 (unsigned long) start, (unsigned long) size,
3201 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003202 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003203 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003204 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003205
3206 /* now prepare goal request */
3207
3208 /* XXX: is it better to align blocks WRT to logical
3209 * placement or satisfy big request as is */
3210 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003211 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003212
3213 /* define goal start in order to merge */
3214 if (ar->pright && (ar->lright == (start + size))) {
3215 /* merge to the right */
3216 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3217 &ac->ac_f_ex.fe_group,
3218 &ac->ac_f_ex.fe_start);
3219 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3220 }
3221 if (ar->pleft && (ar->lleft + 1 == start)) {
3222 /* merge to the left */
3223 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3224 &ac->ac_f_ex.fe_group,
3225 &ac->ac_f_ex.fe_start);
3226 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3227 }
3228
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003229 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003230 (unsigned) orig_size, (unsigned) start);
3231}
3232
3233static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3234{
3235 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3236
3237 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3238 atomic_inc(&sbi->s_bal_reqs);
3239 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003240 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003241 atomic_inc(&sbi->s_bal_success);
3242 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3243 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3244 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3245 atomic_inc(&sbi->s_bal_goals);
3246 if (ac->ac_found > sbi->s_mb_max_to_scan)
3247 atomic_inc(&sbi->s_bal_breaks);
3248 }
3249
Theodore Ts'o296c3552009-09-30 00:32:42 -04003250 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3251 trace_ext4_mballoc_alloc(ac);
3252 else
3253 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003254}
3255
3256/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003257 * Called on failure; free up any blocks from the inode PA for this
3258 * context. We don't need this for MB_GROUP_PA because we only change
3259 * pa_free in ext4_mb_release_context(), but on failure, we've already
3260 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3261 */
3262static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3263{
3264 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003265 struct ext4_buddy e4b;
3266 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003267
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003268 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003269 if (ac->ac_f_ex.fe_len == 0)
3270 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003271 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3272 if (err) {
3273 /*
3274 * This should never happen since we pin the
3275 * pages in the ext4_allocation_context so
3276 * ext4_mb_load_buddy() should never fail.
3277 */
3278 WARN(1, "mb_load_buddy failed (%d)", err);
3279 return;
3280 }
3281 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3282 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3283 ac->ac_f_ex.fe_len);
3284 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003285 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003286 return;
3287 }
3288 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003289 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003290}
3291
3292/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003293 * use blocks preallocated to inode
3294 */
3295static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3296 struct ext4_prealloc_space *pa)
3297{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003298 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003299 ext4_fsblk_t start;
3300 ext4_fsblk_t end;
3301 int len;
3302
3303 /* found preallocated blocks, use them */
3304 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003305 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3306 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3307 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003308 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3309 &ac->ac_b_ex.fe_start);
3310 ac->ac_b_ex.fe_len = len;
3311 ac->ac_status = AC_STATUS_FOUND;
3312 ac->ac_pa = pa;
3313
3314 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003315 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003316 BUG_ON(pa->pa_free < len);
3317 pa->pa_free -= len;
3318
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003319 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003320}
3321
3322/*
3323 * use blocks preallocated to locality group
3324 */
3325static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3326 struct ext4_prealloc_space *pa)
3327{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003328 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003329
Alex Tomasc9de5602008-01-29 00:19:52 -05003330 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3331 &ac->ac_b_ex.fe_group,
3332 &ac->ac_b_ex.fe_start);
3333 ac->ac_b_ex.fe_len = len;
3334 ac->ac_status = AC_STATUS_FOUND;
3335 ac->ac_pa = pa;
3336
3337 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003338 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003339 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003340 * in on-disk bitmap -- see ext4_mb_release_context()
3341 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003342 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003343 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003344}
3345
3346/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003347 * Return the prealloc space that have minimal distance
3348 * from the goal block. @cpa is the prealloc
3349 * space that is having currently known minimal distance
3350 * from the goal block.
3351 */
3352static struct ext4_prealloc_space *
3353ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3354 struct ext4_prealloc_space *pa,
3355 struct ext4_prealloc_space *cpa)
3356{
3357 ext4_fsblk_t cur_distance, new_distance;
3358
3359 if (cpa == NULL) {
3360 atomic_inc(&pa->pa_count);
3361 return pa;
3362 }
Andrew Morton79211c82015-11-09 14:58:13 -08003363 cur_distance = abs(goal_block - cpa->pa_pstart);
3364 new_distance = abs(goal_block - pa->pa_pstart);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003365
Coly Li5a54b2f2011-02-24 14:10:05 -05003366 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003367 return cpa;
3368
3369 /* drop the previous reference */
3370 atomic_dec(&cpa->pa_count);
3371 atomic_inc(&pa->pa_count);
3372 return pa;
3373}
3374
3375/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003376 * search goal blocks in preallocated space
3377 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003378static noinline_for_stack int
3379ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003380{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003381 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003382 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003383 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3384 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003385 struct ext4_prealloc_space *pa, *cpa = NULL;
3386 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003387
3388 /* only data can be preallocated */
3389 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3390 return 0;
3391
3392 /* first, try per-file preallocation */
3393 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003394 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003395
3396 /* all fields in this condition don't change,
3397 * so we can skip locking for them */
3398 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003399 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3400 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003401 continue;
3402
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003403 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003404 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003405 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3406 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003407 continue;
3408
Alex Tomasc9de5602008-01-29 00:19:52 -05003409 /* found preallocated blocks, use them */
3410 spin_lock(&pa->pa_lock);
3411 if (pa->pa_deleted == 0 && pa->pa_free) {
3412 atomic_inc(&pa->pa_count);
3413 ext4_mb_use_inode_pa(ac, pa);
3414 spin_unlock(&pa->pa_lock);
3415 ac->ac_criteria = 10;
3416 rcu_read_unlock();
3417 return 1;
3418 }
3419 spin_unlock(&pa->pa_lock);
3420 }
3421 rcu_read_unlock();
3422
3423 /* can we use group allocation? */
3424 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3425 return 0;
3426
3427 /* inode may have no locality group for some reason */
3428 lg = ac->ac_lg;
3429 if (lg == NULL)
3430 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003431 order = fls(ac->ac_o_ex.fe_len) - 1;
3432 if (order > PREALLOC_TB_SIZE - 1)
3433 /* The max size of hash table is PREALLOC_TB_SIZE */
3434 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003435
Akinobu Mitabda00de2010-03-03 23:53:25 -05003436 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003437 /*
3438 * search for the prealloc space that is having
3439 * minimal distance from the goal block.
3440 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003441 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3442 rcu_read_lock();
3443 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3444 pa_inode_list) {
3445 spin_lock(&pa->pa_lock);
3446 if (pa->pa_deleted == 0 &&
3447 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003448
3449 cpa = ext4_mb_check_group_pa(goal_block,
3450 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003451 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003452 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003453 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003454 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003455 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003456 if (cpa) {
3457 ext4_mb_use_group_pa(ac, cpa);
3458 ac->ac_criteria = 20;
3459 return 1;
3460 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003461 return 0;
3462}
3463
3464/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003465 * the function goes through all block freed in the group
3466 * but not yet committed and marks them used in in-core bitmap.
3467 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003468 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003469 */
3470static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3471 ext4_group_t group)
3472{
3473 struct rb_node *n;
3474 struct ext4_group_info *grp;
3475 struct ext4_free_data *entry;
3476
3477 grp = ext4_get_group_info(sb, group);
3478 n = rb_first(&(grp->bb_free_root));
3479
3480 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003481 entry = rb_entry(n, struct ext4_free_data, efd_node);
3482 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003483 n = rb_next(n);
3484 }
3485 return;
3486}
3487
3488/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003489 * the function goes through all preallocation in this group and marks them
3490 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003491 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003492 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003493static noinline_for_stack
3494void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003495 ext4_group_t group)
3496{
3497 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3498 struct ext4_prealloc_space *pa;
3499 struct list_head *cur;
3500 ext4_group_t groupnr;
3501 ext4_grpblk_t start;
3502 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003503 int len;
3504
3505 /* all form of preallocation discards first load group,
3506 * so the only competing code is preallocation use.
3507 * we don't need any locking here
3508 * notice we do NOT ignore preallocations with pa_deleted
3509 * otherwise we could leave used blocks available for
3510 * allocation in buddy when concurrent ext4_mb_put_pa()
3511 * is dropping preallocation
3512 */
3513 list_for_each(cur, &grp->bb_prealloc_list) {
3514 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3515 spin_lock(&pa->pa_lock);
3516 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3517 &groupnr, &start);
3518 len = pa->pa_len;
3519 spin_unlock(&pa->pa_lock);
3520 if (unlikely(len == 0))
3521 continue;
3522 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003523 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003524 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003525 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003526 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003527}
3528
3529static void ext4_mb_pa_callback(struct rcu_head *head)
3530{
3531 struct ext4_prealloc_space *pa;
3532 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003533
3534 BUG_ON(atomic_read(&pa->pa_count));
3535 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003536 kmem_cache_free(ext4_pspace_cachep, pa);
3537}
3538
3539/*
3540 * drops a reference to preallocated space descriptor
3541 * if this was the last reference and the space is consumed
3542 */
3543static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3544 struct super_block *sb, struct ext4_prealloc_space *pa)
3545{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003546 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003547 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003548
Alex Tomasc9de5602008-01-29 00:19:52 -05003549 /* in this short window concurrent discard can set pa_deleted */
3550 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003551 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3552 spin_unlock(&pa->pa_lock);
3553 return;
3554 }
3555
Alex Tomasc9de5602008-01-29 00:19:52 -05003556 if (pa->pa_deleted == 1) {
3557 spin_unlock(&pa->pa_lock);
3558 return;
3559 }
3560
3561 pa->pa_deleted = 1;
3562 spin_unlock(&pa->pa_lock);
3563
Eric Sandeend33a1972009-03-16 23:25:40 -04003564 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003565 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003566 * If doing group-based preallocation, pa_pstart may be in the
3567 * next group when pa is used up
3568 */
3569 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003570 grp_blk--;
3571
Lukas Czernerbd862982013-04-03 23:32:34 -04003572 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003573
3574 /*
3575 * possible race:
3576 *
3577 * P1 (buddy init) P2 (regular allocation)
3578 * find block B in PA
3579 * copy on-disk bitmap to buddy
3580 * mark B in on-disk bitmap
3581 * drop PA from group
3582 * mark all PAs in buddy
3583 *
3584 * thus, P1 initializes buddy with B available. to prevent this
3585 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3586 * against that pair
3587 */
3588 ext4_lock_group(sb, grp);
3589 list_del(&pa->pa_group_list);
3590 ext4_unlock_group(sb, grp);
3591
3592 spin_lock(pa->pa_obj_lock);
3593 list_del_rcu(&pa->pa_inode_list);
3594 spin_unlock(pa->pa_obj_lock);
3595
3596 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3597}
3598
3599/*
3600 * creates new preallocated space for given inode
3601 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003602static noinline_for_stack int
3603ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003604{
3605 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003606 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003607 struct ext4_prealloc_space *pa;
3608 struct ext4_group_info *grp;
3609 struct ext4_inode_info *ei;
3610
3611 /* preallocate only when found space is larger then requested */
3612 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3613 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3614 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3615
3616 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3617 if (pa == NULL)
3618 return -ENOMEM;
3619
3620 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3621 int winl;
3622 int wins;
3623 int win;
3624 int offs;
3625
3626 /* we can't allocate as much as normalizer wants.
3627 * so, found space must get proper lstart
3628 * to cover original request */
3629 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3630 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3631
3632 /* we're limited by original request in that
3633 * logical block must be covered any way
3634 * winl is window we can move our chunk within */
3635 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3636
3637 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003638 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003639
3640 /* the smallest one defines real window */
3641 win = min(winl, wins);
3642
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003643 offs = ac->ac_o_ex.fe_logical %
3644 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003645 if (offs && offs < win)
3646 win = offs;
3647
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003648 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003649 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003650 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3651 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3652 }
3653
3654 /* preallocation can change ac_b_ex, thus we store actually
3655 * allocated blocks for history */
3656 ac->ac_f_ex = ac->ac_b_ex;
3657
3658 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3659 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3660 pa->pa_len = ac->ac_b_ex.fe_len;
3661 pa->pa_free = pa->pa_len;
3662 atomic_set(&pa->pa_count, 1);
3663 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003664 INIT_LIST_HEAD(&pa->pa_inode_list);
3665 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003666 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003667 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003668
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003669 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003670 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003671 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003672
3673 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003674 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003675
3676 ei = EXT4_I(ac->ac_inode);
3677 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3678
3679 pa->pa_obj_lock = &ei->i_prealloc_lock;
3680 pa->pa_inode = ac->ac_inode;
3681
3682 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3683 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3684 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3685
3686 spin_lock(pa->pa_obj_lock);
3687 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3688 spin_unlock(pa->pa_obj_lock);
3689
3690 return 0;
3691}
3692
3693/*
3694 * creates new preallocated space for locality group inodes belongs to
3695 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003696static noinline_for_stack int
3697ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003698{
3699 struct super_block *sb = ac->ac_sb;
3700 struct ext4_locality_group *lg;
3701 struct ext4_prealloc_space *pa;
3702 struct ext4_group_info *grp;
3703
3704 /* preallocate only when found space is larger then requested */
3705 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3706 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3707 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3708
3709 BUG_ON(ext4_pspace_cachep == NULL);
3710 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3711 if (pa == NULL)
3712 return -ENOMEM;
3713
3714 /* preallocation can change ac_b_ex, thus we store actually
3715 * allocated blocks for history */
3716 ac->ac_f_ex = ac->ac_b_ex;
3717
3718 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3719 pa->pa_lstart = pa->pa_pstart;
3720 pa->pa_len = ac->ac_b_ex.fe_len;
3721 pa->pa_free = pa->pa_len;
3722 atomic_set(&pa->pa_count, 1);
3723 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003724 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003725 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003726 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003727 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003728
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003729 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003730 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3731 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003732
3733 ext4_mb_use_group_pa(ac, pa);
3734 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3735
3736 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3737 lg = ac->ac_lg;
3738 BUG_ON(lg == NULL);
3739
3740 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3741 pa->pa_inode = NULL;
3742
3743 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3744 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3745 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3746
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003747 /*
3748 * We will later add the new pa to the right bucket
3749 * after updating the pa_free in ext4_mb_release_context
3750 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003751 return 0;
3752}
3753
3754static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3755{
3756 int err;
3757
3758 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3759 err = ext4_mb_new_group_pa(ac);
3760 else
3761 err = ext4_mb_new_inode_pa(ac);
3762 return err;
3763}
3764
3765/*
3766 * finds all unused blocks in on-disk bitmap, frees them in
3767 * in-core bitmap and buddy.
3768 * @pa must be unlinked from inode and group lists, so that
3769 * nobody else can find/use it.
3770 * the caller MUST hold group/inode locks.
3771 * TODO: optimize the case when there are no in-core structures yet
3772 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003773static noinline_for_stack int
3774ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003775 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003776{
Alex Tomasc9de5602008-01-29 00:19:52 -05003777 struct super_block *sb = e4b->bd_sb;
3778 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003779 unsigned int end;
3780 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003781 ext4_group_t group;
3782 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003783 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003784 int err = 0;
3785 int free = 0;
3786
3787 BUG_ON(pa->pa_deleted == 0);
3788 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003789 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003790 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3791 end = bit + pa->pa_len;
3792
Alex Tomasc9de5602008-01-29 00:19:52 -05003793 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003794 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003795 if (bit >= end)
3796 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003797 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003798 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003799 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3800 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003801 free += next - bit;
3802
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003803 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003804 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3805 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003806 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003807 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3808 bit = next + 1;
3809 }
3810 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003811 ext4_msg(e4b->bd_sb, KERN_CRIT,
3812 "pa %p: logic %lu, phys. %lu, len %lu",
3813 pa, (unsigned long) pa->pa_lstart,
3814 (unsigned long) pa->pa_pstart,
3815 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003816 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003817 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003818 /*
3819 * pa is already deleted so we use the value obtained
3820 * from the bitmap and continue.
3821 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003822 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003823 atomic_add(free, &sbi->s_mb_discarded);
3824
3825 return err;
3826}
3827
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003828static noinline_for_stack int
3829ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003830 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003831{
Alex Tomasc9de5602008-01-29 00:19:52 -05003832 struct super_block *sb = e4b->bd_sb;
3833 ext4_group_t group;
3834 ext4_grpblk_t bit;
3835
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003836 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003837 BUG_ON(pa->pa_deleted == 0);
3838 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3839 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3840 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3841 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003842 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003843
3844 return 0;
3845}
3846
3847/*
3848 * releases all preallocations in given group
3849 *
3850 * first, we need to decide discard policy:
3851 * - when do we discard
3852 * 1) ENOSPC
3853 * - how many do we discard
3854 * 1) how many requested
3855 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003856static noinline_for_stack int
3857ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003858 ext4_group_t group, int needed)
3859{
3860 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3861 struct buffer_head *bitmap_bh = NULL;
3862 struct ext4_prealloc_space *pa, *tmp;
3863 struct list_head list;
3864 struct ext4_buddy e4b;
3865 int err;
3866 int busy = 0;
3867 int free = 0;
3868
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003869 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003870
3871 if (list_empty(&grp->bb_prealloc_list))
3872 return 0;
3873
Theodore Ts'o574ca172008-07-11 19:27:31 -04003874 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04003875 if (IS_ERR(bitmap_bh)) {
3876 err = PTR_ERR(bitmap_bh);
3877 ext4_error(sb, "Error %d reading block bitmap for %u",
3878 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003879 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003880 }
3881
3882 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003883 if (err) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04003884 ext4_warning(sb, "Error %d loading buddy information for %u",
3885 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003886 put_bh(bitmap_bh);
3887 return 0;
3888 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003889
3890 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003891 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003892
Alex Tomasc9de5602008-01-29 00:19:52 -05003893 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003894repeat:
3895 ext4_lock_group(sb, group);
3896 list_for_each_entry_safe(pa, tmp,
3897 &grp->bb_prealloc_list, pa_group_list) {
3898 spin_lock(&pa->pa_lock);
3899 if (atomic_read(&pa->pa_count)) {
3900 spin_unlock(&pa->pa_lock);
3901 busy = 1;
3902 continue;
3903 }
3904 if (pa->pa_deleted) {
3905 spin_unlock(&pa->pa_lock);
3906 continue;
3907 }
3908
3909 /* seems this one can be freed ... */
3910 pa->pa_deleted = 1;
3911
3912 /* we can trust pa_free ... */
3913 free += pa->pa_free;
3914
3915 spin_unlock(&pa->pa_lock);
3916
3917 list_del(&pa->pa_group_list);
3918 list_add(&pa->u.pa_tmp_list, &list);
3919 }
3920
3921 /* if we still need more blocks and some PAs were used, try again */
3922 if (free < needed && busy) {
3923 busy = 0;
3924 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003925 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003926 goto repeat;
3927 }
3928
3929 /* found anything to free? */
3930 if (list_empty(&list)) {
3931 BUG_ON(free != 0);
3932 goto out;
3933 }
3934
3935 /* now free all selected PAs */
3936 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3937
3938 /* remove from object (inode or locality group) */
3939 spin_lock(pa->pa_obj_lock);
3940 list_del_rcu(&pa->pa_inode_list);
3941 spin_unlock(pa->pa_obj_lock);
3942
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003943 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003944 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003945 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003946 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003947
3948 list_del(&pa->u.pa_tmp_list);
3949 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3950 }
3951
3952out:
3953 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003954 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003955 put_bh(bitmap_bh);
3956 return free;
3957}
3958
3959/*
3960 * releases all non-used preallocated blocks for given inode
3961 *
3962 * It's important to discard preallocations under i_data_sem
3963 * We don't want another block to be served from the prealloc
3964 * space when we are discarding the inode prealloc space.
3965 *
3966 * FIXME!! Make sure it is valid at all the call sites
3967 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003968void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003969{
3970 struct ext4_inode_info *ei = EXT4_I(inode);
3971 struct super_block *sb = inode->i_sb;
3972 struct buffer_head *bitmap_bh = NULL;
3973 struct ext4_prealloc_space *pa, *tmp;
3974 ext4_group_t group = 0;
3975 struct list_head list;
3976 struct ext4_buddy e4b;
3977 int err;
3978
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003979 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003980 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3981 return;
3982 }
3983
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003984 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003985 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003986
3987 INIT_LIST_HEAD(&list);
3988
3989repeat:
3990 /* first, collect all pa's in the inode */
3991 spin_lock(&ei->i_prealloc_lock);
3992 while (!list_empty(&ei->i_prealloc_list)) {
3993 pa = list_entry(ei->i_prealloc_list.next,
3994 struct ext4_prealloc_space, pa_inode_list);
3995 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3996 spin_lock(&pa->pa_lock);
3997 if (atomic_read(&pa->pa_count)) {
3998 /* this shouldn't happen often - nobody should
3999 * use preallocation while we're discarding it */
4000 spin_unlock(&pa->pa_lock);
4001 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004002 ext4_msg(sb, KERN_ERR,
4003 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05004004 WARN_ON(1);
4005 schedule_timeout_uninterruptible(HZ);
4006 goto repeat;
4007
4008 }
4009 if (pa->pa_deleted == 0) {
4010 pa->pa_deleted = 1;
4011 spin_unlock(&pa->pa_lock);
4012 list_del_rcu(&pa->pa_inode_list);
4013 list_add(&pa->u.pa_tmp_list, &list);
4014 continue;
4015 }
4016
4017 /* someone is deleting pa right now */
4018 spin_unlock(&pa->pa_lock);
4019 spin_unlock(&ei->i_prealloc_lock);
4020
4021 /* we have to wait here because pa_deleted
4022 * doesn't mean pa is already unlinked from
4023 * the list. as we might be called from
4024 * ->clear_inode() the inode will get freed
4025 * and concurrent thread which is unlinking
4026 * pa from inode's list may access already
4027 * freed memory, bad-bad-bad */
4028
4029 /* XXX: if this happens too often, we can
4030 * add a flag to force wait only in case
4031 * of ->clear_inode(), but not in case of
4032 * regular truncate */
4033 schedule_timeout_uninterruptible(HZ);
4034 goto repeat;
4035 }
4036 spin_unlock(&ei->i_prealloc_lock);
4037
4038 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004039 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04004040 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05004041
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004042 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4043 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004044 if (err) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004045 ext4_error(sb, "Error %d loading buddy information for %u",
4046 err, group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004047 continue;
4048 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004049
Theodore Ts'o574ca172008-07-11 19:27:31 -04004050 bitmap_bh = ext4_read_block_bitmap(sb, group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004051 if (IS_ERR(bitmap_bh)) {
4052 err = PTR_ERR(bitmap_bh);
4053 ext4_error(sb, "Error %d reading block bitmap for %u",
4054 err, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004055 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004056 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004057 }
4058
4059 ext4_lock_group(sb, group);
4060 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004061 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004062 ext4_unlock_group(sb, group);
4063
Jing Zhange39e07f2010-05-14 00:00:00 -04004064 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004065 put_bh(bitmap_bh);
4066
4067 list_del(&pa->u.pa_tmp_list);
4068 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4069 }
4070}
4071
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004072#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004073static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4074{
4075 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004076 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004077
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05004078 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05004079 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004080 return;
4081
Joe Perches7f6a11e2012-03-19 23:09:43 -04004082 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004083 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04004084 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004085 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004086 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004087 "goal %lu/%lu/%lu@%lu, "
4088 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004089 (unsigned long)ac->ac_o_ex.fe_group,
4090 (unsigned long)ac->ac_o_ex.fe_start,
4091 (unsigned long)ac->ac_o_ex.fe_len,
4092 (unsigned long)ac->ac_o_ex.fe_logical,
4093 (unsigned long)ac->ac_g_ex.fe_group,
4094 (unsigned long)ac->ac_g_ex.fe_start,
4095 (unsigned long)ac->ac_g_ex.fe_len,
4096 (unsigned long)ac->ac_g_ex.fe_logical,
4097 (unsigned long)ac->ac_b_ex.fe_group,
4098 (unsigned long)ac->ac_b_ex.fe_start,
4099 (unsigned long)ac->ac_b_ex.fe_len,
4100 (unsigned long)ac->ac_b_ex.fe_logical,
4101 (int)ac->ac_criteria);
Eric Sandeendc9ddd92014-02-20 13:32:10 -05004102 ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004103 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004104 ngroups = ext4_get_groups_count(sb);
4105 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004106 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4107 struct ext4_prealloc_space *pa;
4108 ext4_grpblk_t start;
4109 struct list_head *cur;
4110 ext4_lock_group(sb, i);
4111 list_for_each(cur, &grp->bb_prealloc_list) {
4112 pa = list_entry(cur, struct ext4_prealloc_space,
4113 pa_group_list);
4114 spin_lock(&pa->pa_lock);
4115 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4116 NULL, &start);
4117 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004118 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4119 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004120 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004121 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004122
4123 if (grp->bb_free == 0)
4124 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004125 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004126 i, grp->bb_free, grp->bb_fragments);
4127 }
4128 printk(KERN_ERR "\n");
4129}
4130#else
4131static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4132{
4133 return;
4134}
4135#endif
4136
4137/*
4138 * We use locality group preallocation for small size file. The size of the
4139 * file is determined by the current size or the resulting size after
4140 * allocation which ever is larger
4141 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004142 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004143 */
4144static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4145{
4146 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4147 int bsbits = ac->ac_sb->s_blocksize_bits;
4148 loff_t size, isize;
4149
4150 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4151 return;
4152
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004153 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4154 return;
4155
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004156 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004157 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4158 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004159
Theodore Ts'o50797482009-09-18 13:34:02 -04004160 if ((size == isize) &&
4161 !ext4_fs_is_busy(sbi) &&
4162 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4163 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4164 return;
4165 }
4166
Robin Dongebbe0272011-10-26 05:14:27 -04004167 if (sbi->s_mb_group_prealloc <= 0) {
4168 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4169 return;
4170 }
4171
Alex Tomasc9de5602008-01-29 00:19:52 -05004172 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004173 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004174 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004175 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004176 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004177 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004178
4179 BUG_ON(ac->ac_lg != NULL);
4180 /*
4181 * locality group prealloc space are per cpu. The reason for having
4182 * per cpu locality group is to reduce the contention between block
4183 * request from multiple CPUs.
4184 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004185 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004186
4187 /* we're going to use group allocation */
4188 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4189
4190 /* serialize all allocations in the group */
4191 mutex_lock(&ac->ac_lg->lg_mutex);
4192}
4193
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004194static noinline_for_stack int
4195ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004196 struct ext4_allocation_request *ar)
4197{
4198 struct super_block *sb = ar->inode->i_sb;
4199 struct ext4_sb_info *sbi = EXT4_SB(sb);
4200 struct ext4_super_block *es = sbi->s_es;
4201 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004202 unsigned int len;
4203 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004204 ext4_grpblk_t block;
4205
4206 /* we can't allocate > group size */
4207 len = ar->len;
4208
4209 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004210 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4211 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004212
4213 /* start searching from the goal */
4214 goal = ar->goal;
4215 if (goal < le32_to_cpu(es->s_first_data_block) ||
4216 goal >= ext4_blocks_count(es))
4217 goal = le32_to_cpu(es->s_first_data_block);
4218 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4219
4220 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004221 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004222 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004223 ac->ac_sb = sb;
4224 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004225 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004226 ac->ac_o_ex.fe_group = group;
4227 ac->ac_o_ex.fe_start = block;
4228 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004229 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004230 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004231
4232 /* we have to define context: we'll we work with a file or
4233 * locality group. this is a policy, actually */
4234 ext4_mb_group_or_file(ac);
4235
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004236 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004237 "left: %u/%u, right %u/%u to %swritable\n",
4238 (unsigned) ar->len, (unsigned) ar->logical,
4239 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4240 (unsigned) ar->lleft, (unsigned) ar->pleft,
4241 (unsigned) ar->lright, (unsigned) ar->pright,
4242 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4243 return 0;
4244
4245}
4246
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004247static noinline_for_stack void
4248ext4_mb_discard_lg_preallocations(struct super_block *sb,
4249 struct ext4_locality_group *lg,
4250 int order, int total_entries)
4251{
4252 ext4_group_t group = 0;
4253 struct ext4_buddy e4b;
4254 struct list_head discard_list;
4255 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004256
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004257 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004258
4259 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004260
4261 spin_lock(&lg->lg_prealloc_lock);
4262 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4263 pa_inode_list) {
4264 spin_lock(&pa->pa_lock);
4265 if (atomic_read(&pa->pa_count)) {
4266 /*
4267 * This is the pa that we just used
4268 * for block allocation. So don't
4269 * free that
4270 */
4271 spin_unlock(&pa->pa_lock);
4272 continue;
4273 }
4274 if (pa->pa_deleted) {
4275 spin_unlock(&pa->pa_lock);
4276 continue;
4277 }
4278 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004279 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004280
4281 /* seems this one can be freed ... */
4282 pa->pa_deleted = 1;
4283 spin_unlock(&pa->pa_lock);
4284
4285 list_del_rcu(&pa->pa_inode_list);
4286 list_add(&pa->u.pa_tmp_list, &discard_list);
4287
4288 total_entries--;
4289 if (total_entries <= 5) {
4290 /*
4291 * we want to keep only 5 entries
4292 * allowing it to grow to 8. This
4293 * mak sure we don't call discard
4294 * soon for this list.
4295 */
4296 break;
4297 }
4298 }
4299 spin_unlock(&lg->lg_prealloc_lock);
4300
4301 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004302 int err;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004303
Lukas Czernerbd862982013-04-03 23:32:34 -04004304 group = ext4_get_group_number(sb, pa->pa_pstart);
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04004305 err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
4306 GFP_NOFS|__GFP_NOFAIL);
4307 if (err) {
4308 ext4_error(sb, "Error %d loading buddy information for %u",
4309 err, group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004310 continue;
4311 }
4312 ext4_lock_group(sb, group);
4313 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004314 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004315 ext4_unlock_group(sb, group);
4316
Jing Zhange39e07f2010-05-14 00:00:00 -04004317 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004318 list_del(&pa->u.pa_tmp_list);
4319 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4320 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004321}
4322
4323/*
4324 * We have incremented pa_count. So it cannot be freed at this
4325 * point. Also we hold lg_mutex. So no parallel allocation is
4326 * possible from this lg. That means pa_free cannot be updated.
4327 *
4328 * A parallel ext4_mb_discard_group_preallocations is possible.
4329 * which can cause the lg_prealloc_list to be updated.
4330 */
4331
4332static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4333{
4334 int order, added = 0, lg_prealloc_count = 1;
4335 struct super_block *sb = ac->ac_sb;
4336 struct ext4_locality_group *lg = ac->ac_lg;
4337 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4338
4339 order = fls(pa->pa_free) - 1;
4340 if (order > PREALLOC_TB_SIZE - 1)
4341 /* The max size of hash table is PREALLOC_TB_SIZE */
4342 order = PREALLOC_TB_SIZE - 1;
4343 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004344 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004345 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4346 pa_inode_list) {
4347 spin_lock(&tmp_pa->pa_lock);
4348 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004349 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004350 continue;
4351 }
4352 if (!added && pa->pa_free < tmp_pa->pa_free) {
4353 /* Add to the tail of the previous entry */
4354 list_add_tail_rcu(&pa->pa_inode_list,
4355 &tmp_pa->pa_inode_list);
4356 added = 1;
4357 /*
4358 * we want to count the total
4359 * number of entries in the list
4360 */
4361 }
4362 spin_unlock(&tmp_pa->pa_lock);
4363 lg_prealloc_count++;
4364 }
4365 if (!added)
4366 list_add_tail_rcu(&pa->pa_inode_list,
4367 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004368 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004369
4370 /* Now trim the list to be not more than 8 elements */
4371 if (lg_prealloc_count > 8) {
4372 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004373 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004374 return;
4375 }
4376 return ;
4377}
4378
Alex Tomasc9de5602008-01-29 00:19:52 -05004379/*
4380 * release all resource we used in allocation
4381 */
4382static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4383{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004384 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004385 struct ext4_prealloc_space *pa = ac->ac_pa;
4386 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004387 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004388 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004389 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004390 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4391 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004392 pa->pa_free -= ac->ac_b_ex.fe_len;
4393 pa->pa_len -= ac->ac_b_ex.fe_len;
4394 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004395 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004396 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004397 if (pa) {
4398 /*
4399 * We want to add the pa to the right bucket.
4400 * Remove it from the list and while adding
4401 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004402 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004403 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004404 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004405 spin_lock(pa->pa_obj_lock);
4406 list_del_rcu(&pa->pa_inode_list);
4407 spin_unlock(pa->pa_obj_lock);
4408 ext4_mb_add_n_trim(ac);
4409 }
4410 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4411 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004412 if (ac->ac_bitmap_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004413 put_page(ac->ac_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004414 if (ac->ac_buddy_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004415 put_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05004416 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4417 mutex_unlock(&ac->ac_lg->lg_mutex);
4418 ext4_mb_collect_stats(ac);
4419 return 0;
4420}
4421
4422static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4423{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004424 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004425 int ret;
4426 int freed = 0;
4427
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004428 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004429 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004430 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4431 freed += ret;
4432 needed -= ret;
4433 }
4434
4435 return freed;
4436}
4437
4438/*
4439 * Main entry point into mballoc to allocate blocks
4440 * it tries to use preallocation first, then falls back
4441 * to usual allocation
4442 */
4443ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004444 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004445{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004446 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004447 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004448 struct ext4_sb_info *sbi;
4449 struct super_block *sb;
4450 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004451 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004452 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004453
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004454 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004455 sb = ar->inode->i_sb;
4456 sbi = EXT4_SB(sb);
4457
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004458 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004459
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004460 /* Allow to use superuser reservation for quota file */
4461 if (IS_NOQUOTA(ar->inode))
4462 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4463
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004464 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004465 /* Without delayed allocation we need to verify
4466 * there is enough free blocks to do block allocation
4467 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004468 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004469 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004470 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004471
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004472 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004473 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004474 ar->len = ar->len >> 1;
4475 }
4476 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004477 *errp = -ENOSPC;
4478 return 0;
4479 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004480 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004481 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004482 dquot_alloc_block_nofail(ar->inode,
4483 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004484 } else {
4485 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004486 dquot_alloc_block(ar->inode,
4487 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004488
4489 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4490 ar->len--;
4491 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004492 }
4493 inquota = ar->len;
4494 if (ar->len == 0) {
4495 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004496 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004497 }
Mingming Caod2a17632008-07-14 17:52:37 -04004498 }
Mingming Caod2a17632008-07-14 17:52:37 -04004499
Wei Yongjun85556c92012-09-26 20:43:37 -04004500 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004501 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004502 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004503 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004504 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004505 }
4506
Eric Sandeen256bdb42008-02-10 01:13:33 -05004507 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004508 if (*errp) {
4509 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004510 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004511 }
4512
Eric Sandeen256bdb42008-02-10 01:13:33 -05004513 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4514 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004515 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4516 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004517repeat:
4518 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004519 *errp = ext4_mb_regular_allocator(ac);
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004520 if (*errp)
4521 goto discard_and_exit;
4522
4523 /* as we've just preallocated more space than
4524 * user requested originally, we store allocated
4525 * space in a special descriptor */
4526 if (ac->ac_status == AC_STATUS_FOUND &&
4527 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4528 *errp = ext4_mb_new_preallocation(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004529 if (*errp) {
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004530 discard_and_exit:
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004531 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004532 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004533 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004534 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004535 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004536 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Vegard Nossum554a5cc2016-07-14 23:02:47 -04004537 if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004538 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004539 goto errout;
4540 } else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004541 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4542 ar->len = ac->ac_b_ex.fe_len;
4543 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004544 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004545 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004546 if (freed)
4547 goto repeat;
4548 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004549 }
4550
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004551errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004552 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004553 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004554 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004555 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004556 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004557 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004558out:
4559 if (ac)
4560 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004561 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004562 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004563 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004564 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004565 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004566 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004567 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004568 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004569
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004570 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004571
Alex Tomasc9de5602008-01-29 00:19:52 -05004572 return block;
4573}
Alex Tomasc9de5602008-01-29 00:19:52 -05004574
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004575/*
4576 * We can merge two free data extents only if the physical blocks
4577 * are contiguous, AND the extents were freed by the same transaction,
4578 * AND the blocks are associated with the same group.
4579 */
4580static int can_merge(struct ext4_free_data *entry1,
4581 struct ext4_free_data *entry2)
4582{
Bobi Jam18aadd42012-02-20 17:53:02 -05004583 if ((entry1->efd_tid == entry2->efd_tid) &&
4584 (entry1->efd_group == entry2->efd_group) &&
4585 ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004586 return 1;
4587 return 0;
4588}
4589
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004590static noinline_for_stack int
4591ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004592 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004593{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004594 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004595 ext4_grpblk_t cluster;
Theodore Ts'od08854f2016-06-26 18:24:01 -04004596 ext4_grpblk_t clusters = new_entry->efd_count;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004597 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004598 struct ext4_group_info *db = e4b->bd_info;
4599 struct super_block *sb = e4b->bd_sb;
4600 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004601 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4602 struct rb_node *parent = NULL, *new_node;
4603
Frank Mayhar03901312009-01-07 00:06:22 -05004604 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004605 BUG_ON(e4b->bd_bitmap_page == NULL);
4606 BUG_ON(e4b->bd_buddy_page == NULL);
4607
Bobi Jam18aadd42012-02-20 17:53:02 -05004608 new_node = &new_entry->efd_node;
4609 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004610
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004611 if (!*n) {
4612 /* first free block exent. We need to
4613 protect buddy cache from being freed,
4614 * otherwise we'll refresh it from
4615 * on-disk bitmap and lose not-yet-available
4616 * blocks */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004617 get_page(e4b->bd_buddy_page);
4618 get_page(e4b->bd_bitmap_page);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004619 }
4620 while (*n) {
4621 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004622 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4623 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004624 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004625 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004626 n = &(*n)->rb_right;
4627 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004628 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004629 ext4_group_first_block_no(sb, group) +
4630 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004631 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004632 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004633 }
4634 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004635
4636 rb_link_node(new_node, parent, n);
4637 rb_insert_color(new_node, &db->bb_free_root);
4638
4639 /* Now try to see the extent can be merged to left and right */
4640 node = rb_prev(new_node);
4641 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004642 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004643 if (can_merge(entry, new_entry) &&
4644 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004645 new_entry->efd_start_cluster = entry->efd_start_cluster;
4646 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004647 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004648 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004649 }
4650 }
4651
4652 node = rb_next(new_node);
4653 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004654 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004655 if (can_merge(new_entry, entry) &&
4656 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004657 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004658 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004659 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004660 }
4661 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004662 /* Add the extent to transaction's private list */
Theodore Ts'od08854f2016-06-26 18:24:01 -04004663 new_entry->efd_jce.jce_func = ext4_free_data_callback;
4664 spin_lock(&sbi->s_md_lock);
4665 _ext4_journal_callback_add(handle, &new_entry->efd_jce);
4666 sbi->s_mb_free_pending += clusters;
4667 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004668 return 0;
4669}
4670
Theodore Ts'o44338712009-11-22 07:44:56 -05004671/**
4672 * ext4_free_blocks() -- Free given blocks and update quota
4673 * @handle: handle for this transaction
4674 * @inode: inode
4675 * @block: start physical block to free
4676 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004677 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004678 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004679void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004680 struct buffer_head *bh, ext4_fsblk_t block,
4681 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004682{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004683 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004684 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004685 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004686 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004687 ext4_grpblk_t bit;
4688 struct buffer_head *gd_bh;
4689 ext4_group_t block_group;
4690 struct ext4_sb_info *sbi;
4691 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004692 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004693 int err = 0;
4694 int ret;
4695
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004696 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004697 if (bh) {
4698 if (block)
4699 BUG_ON(block != bh->b_blocknr);
4700 else
4701 block = bh->b_blocknr;
4702 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004703
Alex Tomasc9de5602008-01-29 00:19:52 -05004704 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004705 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4706 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004707 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004708 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004709 goto error_return;
4710 }
4711
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004712 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004713 trace_ext4_free_blocks(inode, block, count, flags);
4714
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004715 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4716 BUG_ON(count > 1);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004717
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004718 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4719 inode, bh, block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004720 }
4721
Theodore Ts'o60e66792010-05-17 07:00:00 -04004722 /*
Theodore Ts'o84130192011-09-09 18:50:51 -04004723 * If the extent to be freed does not begin on a cluster
4724 * boundary, we need to deal with partial clusters at the
4725 * beginning and end of the extent. Normally we will free
4726 * blocks at the beginning or the end unless we are explicitly
4727 * requested to avoid doing so.
4728 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004729 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004730 if (overflow) {
4731 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4732 overflow = sbi->s_cluster_ratio - overflow;
4733 block += overflow;
4734 if (count > overflow)
4735 count -= overflow;
4736 else
4737 return;
4738 } else {
4739 block -= overflow;
4740 count += overflow;
4741 }
4742 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004743 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004744 if (overflow) {
4745 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4746 if (count > overflow)
4747 count -= overflow;
4748 else
4749 return;
4750 } else
4751 count += sbi->s_cluster_ratio - overflow;
4752 }
4753
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004754 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
4755 int i;
Daeho Jeongf96c4502016-02-21 18:31:41 -05004756 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004757
4758 for (i = 0; i < count; i++) {
4759 cond_resched();
Daeho Jeongf96c4502016-02-21 18:31:41 -05004760 if (is_metadata)
4761 bh = sb_find_get_block(inode->i_sb, block + i);
4762 ext4_forget(handle, is_metadata, inode, bh, block + i);
Daeho Jeong9c02ac92015-10-17 22:28:21 -04004763 }
4764 }
4765
Alex Tomasc9de5602008-01-29 00:19:52 -05004766do_more:
4767 overflow = 0;
4768 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4769
Darrick J. Wong163a2032013-08-28 17:35:51 -04004770 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4771 ext4_get_group_info(sb, block_group))))
4772 return;
4773
Alex Tomasc9de5602008-01-29 00:19:52 -05004774 /*
4775 * Check to see if we are freeing blocks across a group
4776 * boundary.
4777 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004778 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4779 overflow = EXT4_C2B(sbi, bit) + count -
4780 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004781 count -= overflow;
4782 }
Lukas Czerner810da242013-03-02 17:18:58 -05004783 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004784 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004785 if (IS_ERR(bitmap_bh)) {
4786 err = PTR_ERR(bitmap_bh);
4787 bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004788 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004789 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004790 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004791 if (!gdp) {
4792 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004793 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004794 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004795
4796 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4797 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4798 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004799 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004800 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004801 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004802
Eric Sandeen12062dd2010-02-15 14:19:27 -05004803 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004804 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004805 /* err = 0. ext4_std_error should be a no op */
4806 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004807 }
4808
4809 BUFFER_TRACE(bitmap_bh, "getting write access");
4810 err = ext4_journal_get_write_access(handle, bitmap_bh);
4811 if (err)
4812 goto error_return;
4813
4814 /*
4815 * We are about to modify some metadata. Call the journal APIs
4816 * to unshare ->b_data if a currently-committing transaction is
4817 * using it
4818 */
4819 BUFFER_TRACE(gd_bh, "get_write_access");
4820 err = ext4_journal_get_write_access(handle, gd_bh);
4821 if (err)
4822 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004823#ifdef AGGRESSIVE_CHECK
4824 {
4825 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004826 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004827 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4828 }
4829#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004830 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004831
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04004832 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
4833 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
4834 GFP_NOFS|__GFP_NOFAIL);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004835 if (err)
4836 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004837
Daeho Jeongf96c4502016-02-21 18:31:41 -05004838 /*
4839 * We need to make sure we don't reuse the freed block until after the
4840 * transaction is committed. We make an exception if the inode is to be
4841 * written in writeback mode since writeback mode has weak data
4842 * consistency guarantees.
4843 */
4844 if (ext4_handle_valid(handle) &&
4845 ((flags & EXT4_FREE_BLOCKS_METADATA) ||
4846 !ext4_should_writeback_data(inode))) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004847 struct ext4_free_data *new_entry;
4848 /*
Michal Hocko7444a072015-07-05 12:33:44 -04004849 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4850 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004851 */
Michal Hocko7444a072015-07-05 12:33:44 -04004852 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
4853 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05004854 new_entry->efd_start_cluster = bit;
4855 new_entry->efd_group = block_group;
4856 new_entry->efd_count = count_clusters;
4857 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004858
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004859 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004860 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004861 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004862 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004863 /* need to update group_info->bb_free and bitmap
4864 * with group lock held. generate_buddy look at
4865 * them with group lock_held
4866 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004867 if (test_opt(sb, DISCARD)) {
4868 err = ext4_issue_discard(sb, block_group, bit, count);
4869 if (err && err != -EOPNOTSUPP)
4870 ext4_msg(sb, KERN_WARNING, "discard request in"
4871 " group:%d block:%d count:%lu failed"
4872 " with %d", block_group, bit, count,
4873 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04004874 } else
4875 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004876
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004877 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004878 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4879 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004880 }
4881
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004882 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4883 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004884 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004885 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004886 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004887
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004888 if (sbi->s_log_groups_per_flex) {
4889 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004890 atomic64_add(count_clusters,
4891 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004892 }
4893
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04004894 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004895 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
Jan Kara7d734532013-08-17 09:36:54 -04004896 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
4897
4898 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004899
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004900 /* We dirtied the bitmap block */
4901 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4902 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4903
Alex Tomasc9de5602008-01-29 00:19:52 -05004904 /* And the group descriptor block */
4905 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004906 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004907 if (!err)
4908 err = ret;
4909
4910 if (overflow && !err) {
4911 block += count;
4912 count = overflow;
4913 put_bh(bitmap_bh);
4914 goto do_more;
4915 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004916error_return:
4917 brelse(bitmap_bh);
4918 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004919 return;
4920}
Lukas Czerner7360d172010-10-27 21:30:12 -04004921
4922/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004923 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004924 * @handle: handle to this transaction
4925 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004926 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004927 * @count: number of blocks to free
4928 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004929 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004930 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004931int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004932 ext4_fsblk_t block, unsigned long count)
4933{
4934 struct buffer_head *bitmap_bh = NULL;
4935 struct buffer_head *gd_bh;
4936 ext4_group_t block_group;
4937 ext4_grpblk_t bit;
4938 unsigned int i;
4939 struct ext4_group_desc *desc;
4940 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004941 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004942 int err = 0, ret, blk_free_count;
4943 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004944
4945 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4946
Yongqiang Yang4740b832011-07-26 21:51:08 -04004947 if (count == 0)
4948 return 0;
4949
Amir Goldstein2846e822011-05-09 10:46:41 -04004950 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004951 /*
4952 * Check to see if we are freeing blocks across a group
4953 * boundary.
4954 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004955 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04004956 ext4_warning(sb, "too much blocks added to group %u",
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004957 block_group);
4958 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004959 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004960 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004961
Amir Goldstein2846e822011-05-09 10:46:41 -04004962 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Darrick J. Wong9008a582015-10-17 21:33:24 -04004963 if (IS_ERR(bitmap_bh)) {
4964 err = PTR_ERR(bitmap_bh);
4965 bitmap_bh = NULL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004966 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004967 }
4968
Amir Goldstein2846e822011-05-09 10:46:41 -04004969 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004970 if (!desc) {
4971 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004972 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004973 }
Amir Goldstein2846e822011-05-09 10:46:41 -04004974
4975 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4976 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4977 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4978 in_range(block + count - 1, ext4_inode_table(sb, desc),
4979 sbi->s_itb_per_group)) {
4980 ext4_error(sb, "Adding blocks in system zones - "
4981 "Block = %llu, count = %lu",
4982 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004983 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004984 goto error_return;
4985 }
4986
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004987 BUFFER_TRACE(bitmap_bh, "getting write access");
4988 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004989 if (err)
4990 goto error_return;
4991
4992 /*
4993 * We are about to modify some metadata. Call the journal APIs
4994 * to unshare ->b_data if a currently-committing transaction is
4995 * using it
4996 */
4997 BUFFER_TRACE(gd_bh, "get_write_access");
4998 err = ext4_journal_get_write_access(handle, gd_bh);
4999 if (err)
5000 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04005001
Amir Goldstein2846e822011-05-09 10:46:41 -04005002 for (i = 0, blocks_freed = 0; i < count; i++) {
5003 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04005004 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04005005 ext4_error(sb, "bit already cleared for block %llu",
5006 (ext4_fsblk_t)(block + i));
5007 BUFFER_TRACE(bitmap_bh, "bit already cleared");
5008 } else {
5009 blocks_freed++;
5010 }
5011 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005012
5013 err = ext4_mb_load_buddy(sb, block_group, &e4b);
5014 if (err)
5015 goto error_return;
5016
5017 /*
5018 * need to update group_info->bb_free and bitmap
5019 * with group lock held. generate_buddy look at
5020 * them with group lock_held
5021 */
Amir Goldstein2846e822011-05-09 10:46:41 -04005022 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04005023 mb_clear_bits(bitmap_bh->b_data, bit, count);
5024 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04005025 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
5026 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04005027 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005028 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04005029 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04005030 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05005031 EXT4_NUM_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04005032
5033 if (sbi->s_log_groups_per_flex) {
5034 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04005035 atomic64_add(EXT4_NUM_B2C(sbi, blocks_freed),
5036 &sbi->s_flex_groups[flex_group].free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04005037 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04005038
5039 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005040
5041 /* We dirtied the bitmap block */
5042 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5043 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5044
5045 /* And the group descriptor block */
5046 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5047 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5048 if (!err)
5049 err = ret;
5050
5051error_return:
5052 brelse(bitmap_bh);
5053 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005054 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005055}
5056
5057/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005058 * ext4_trim_extent -- function to TRIM one single free extent in the group
5059 * @sb: super block for the file system
5060 * @start: starting block of the free extent in the alloc. group
5061 * @count: number of blocks to TRIM
5062 * @group: alloc. group we are working with
5063 * @e4b: ext4 buddy for the group
5064 *
5065 * Trim "count" blocks starting at "start" in the "group". To assure that no
5066 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5067 * be called with under the group lock.
5068 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005069static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04005070 ext4_group_t group, struct ext4_buddy *e4b)
jon ernste2cbd582014-04-12 23:01:28 -04005071__releases(bitlock)
5072__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005073{
5074 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005075 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005076
Tao Mab3d4c2b2011-07-11 00:01:52 -04005077 trace_ext4_trim_extent(sb, group, start, count);
5078
Lukas Czerner7360d172010-10-27 21:30:12 -04005079 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5080
5081 ex.fe_start = start;
5082 ex.fe_group = group;
5083 ex.fe_len = count;
5084
5085 /*
5086 * Mark blocks used, so no one can reuse them while
5087 * being trimmed.
5088 */
5089 mb_mark_used(e4b, &ex);
5090 ext4_unlock_group(sb, group);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005091 ret = ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04005092 ext4_lock_group(sb, group);
5093 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005094 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005095}
5096
5097/**
5098 * ext4_trim_all_free -- function to trim all free space in alloc. group
5099 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005100 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005101 * @start: first group block to examine
5102 * @max: last group block to examine
5103 * @minblocks: minimum extent block count
5104 *
5105 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5106 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5107 * the extent.
5108 *
5109 *
5110 * ext4_trim_all_free walks through group's block bitmap searching for free
5111 * extents. When the free extent is found, mark it as used in group buddy
5112 * bitmap. Then issue a TRIM command on this extent and free the extent in
5113 * the group buddy bitmap. This is done until whole group is scanned.
5114 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005115static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005116ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5117 ext4_grpblk_t start, ext4_grpblk_t max,
5118 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005119{
5120 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005121 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005122 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005123 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005124
Tao Mab3d4c2b2011-07-11 00:01:52 -04005125 trace_ext4_trim_all_free(sb, group, start, max);
5126
Lukas Czerner78944082011-05-24 18:16:27 -04005127 ret = ext4_mb_load_buddy(sb, group, &e4b);
5128 if (ret) {
Konstantin Khlebnikova90e0452017-05-21 22:35:23 -04005129 ext4_warning(sb, "Error %d loading buddy information for %u",
5130 ret, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005131 return ret;
5132 }
Lukas Czerner78944082011-05-24 18:16:27 -04005133 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005134
5135 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005136 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5137 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5138 goto out;
5139
Lukas Czerner78944082011-05-24 18:16:27 -04005140 start = (e4b.bd_info->bb_first_free > start) ?
5141 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005142
Lukas Czerner913eed832012-03-21 21:22:22 -04005143 while (start <= max) {
5144 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5145 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005146 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005147 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005148
5149 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005150 ret = ext4_trim_extent(sb, start,
5151 next - start, group, &e4b);
5152 if (ret && ret != -EOPNOTSUPP)
5153 break;
5154 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005155 count += next - start;
5156 }
Tao Ma169ddc32011-07-11 00:00:07 -04005157 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005158 start = next + 1;
5159
5160 if (fatal_signal_pending(current)) {
5161 count = -ERESTARTSYS;
5162 break;
5163 }
5164
5165 if (need_resched()) {
5166 ext4_unlock_group(sb, group);
5167 cond_resched();
5168 ext4_lock_group(sb, group);
5169 }
5170
Tao Ma169ddc32011-07-11 00:00:07 -04005171 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005172 break;
5173 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005174
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005175 if (!ret) {
5176 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005177 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005178 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005179out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005180 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005181 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005182
5183 ext4_debug("trimmed %d blocks in the group %d\n",
5184 count, group);
5185
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005186 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005187}
5188
5189/**
5190 * ext4_trim_fs() -- trim ioctl handle function
5191 * @sb: superblock for filesystem
5192 * @range: fstrim_range structure
5193 *
5194 * start: First Byte to trim
5195 * len: number of Bytes to trim from start
5196 * minlen: minimum extent length in Bytes
5197 * ext4_trim_fs goes through all allocation groups containing Bytes from
5198 * start to start+len. For each such a group ext4_trim_all_free function
5199 * is invoked to trim all free space.
5200 */
5201int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
5202{
Lukas Czerner78944082011-05-24 18:16:27 -04005203 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005204 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005205 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005206 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005207 ext4_fsblk_t first_data_blk =
5208 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005209 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005210 int ret = 0;
5211
5212 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005213 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005214 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5215 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005216
Lukas Czerner5de35e82012-10-22 18:01:19 -04005217 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5218 start >= max_blks ||
5219 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005220 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005221 if (end >= max_blks)
5222 end = max_blks - 1;
5223 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005224 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005225 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005226 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005227
Lukas Czerner913eed832012-03-21 21:22:22 -04005228 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005229 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005230 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005231 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005232 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005233
Lukas Czerner913eed832012-03-21 21:22:22 -04005234 /* end now represents the last cluster to discard in this group */
5235 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005236
5237 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005238 grp = ext4_get_group_info(sb, group);
5239 /* We only do this if the grp has never been initialized */
5240 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Konstantin Khlebnikovadb7ef62016-03-13 17:29:06 -04005241 ret = ext4_mb_init_group(sb, group, GFP_NOFS);
Lukas Czerner78944082011-05-24 18:16:27 -04005242 if (ret)
5243 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005244 }
5245
Tao Ma0ba08512011-03-23 15:48:11 -04005246 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005247 * For all the groups except the last one, last cluster will
5248 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5249 * change it for the last group, note that last_cluster is
5250 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005251 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005252 if (group == last_group)
5253 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005254
Lukas Czerner78944082011-05-24 18:16:27 -04005255 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005256 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005257 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005258 if (cnt < 0) {
5259 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005260 break;
5261 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005262 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005263 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005264
5265 /*
5266 * For every group except the first one, we are sure
5267 * that the first cluster to discard will be cluster #0.
5268 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005269 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005270 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005271
Tao Ma3d56b8d2011-07-11 00:03:38 -04005272 if (!ret)
5273 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5274
Tao Ma22f10452011-07-10 23:52:37 -04005275out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005276 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005277 return ret;
5278}