blob: 7bb713a46fe4c3f85ae9d35bd15bea73aa7957f5 [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 *
14 * You should have received a copy of the GNU General Public Licens
15 * 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>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040029#include <trace/events/ext4.h>
30
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050031#ifdef CONFIG_EXT4_DEBUG
32ushort ext4_mballoc_debug __read_mostly;
33
34module_param_named(mballoc_debug, ext4_mballoc_debug, ushort, 0644);
35MODULE_PARM_DESC(mballoc_debug, "Debugging level for ext4's mballoc");
36#endif
37
Alex Tomasc9de5602008-01-29 00:19:52 -050038/*
39 * MUSTDO:
40 * - test ext4_ext_search_left() and ext4_ext_search_right()
41 * - search for metadata in few groups
42 *
43 * TODO v4:
44 * - normalization should take into account whether file is still open
45 * - discard preallocations if no free space left (policy?)
46 * - don't normalize tails
47 * - quota
48 * - reservation for superuser
49 *
50 * TODO v3:
51 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
52 * - track min/max extents in each group for better group selection
53 * - mb_mark_used() may allocate chunk right after splitting buddy
54 * - tree of groups sorted by number of free blocks
55 * - error handling
56 */
57
58/*
59 * The allocation request involve request for multiple number of blocks
60 * near to the goal(block) value specified.
61 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040062 * During initialization phase of the allocator we decide to use the
63 * group preallocation or inode preallocation depending on the size of
64 * the file. The size of the file could be the resulting file size we
65 * would have after allocation, or the current file size, which ever
66 * is larger. If the size is less than sbi->s_mb_stream_request we
67 * select to use the group preallocation. The default value of
68 * s_mb_stream_request is 16 blocks. This can also be tuned via
69 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
70 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050071 *
72 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040073 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050074 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040075 * First stage the allocator looks at the inode prealloc list,
76 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
77 * spaces for this particular inode. The inode prealloc space is
78 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050079 *
80 * pa_lstart -> the logical start block for this prealloc space
81 * pa_pstart -> the physical start block for this prealloc space
Theodore Ts'o53accfa2011-09-09 18:48:51 -040082 * pa_len -> length for this prealloc space (in clusters)
83 * pa_free -> free space available in this prealloc space (in clusters)
Alex Tomasc9de5602008-01-29 00:19:52 -050084 *
85 * The inode preallocation space is used looking at the _logical_ start
86 * block. If only the logical file block falls within the range of prealloc
Tao Macaaf7a22011-07-11 18:42:42 -040087 * space we will consume the particular prealloc space. This makes sure that
88 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050089 *
90 * The important thing to be noted in case of inode prealloc space is that
91 * we don't modify the values associated to inode prealloc space except
92 * pa_free.
93 *
94 * If we are not able to find blocks in the inode prealloc space and if we
95 * have the group allocation flag set then we look at the locality group
Tao Macaaf7a22011-07-11 18:42:42 -040096 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050097 *
98 * ext4_sb_info.s_locality_groups[smp_processor_id()]
99 *
100 * The reason for having a per cpu locality group is to reduce the contention
101 * between CPUs. It is possible to get scheduled at this point.
102 *
103 * The locality group prealloc space is used looking at whether we have
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300104 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -0500105 *
106 * If we can't allocate blocks via inode prealloc or/and locality group
107 * prealloc then we look at the buddy cache. The buddy cache is represented
108 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
109 * mapped to the buddy and bitmap information regarding different
110 * groups. The buddy information is attached to buddy cache inode so that
111 * we can access them through the page cache. The information regarding
112 * each group is loaded via ext4_mb_load_buddy. The information involve
113 * block bitmap and buddy information. The information are stored in the
114 * inode as:
115 *
116 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500117 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500118 *
119 *
120 * one block each for bitmap and buddy information. So for each group we
121 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
122 * blocksize) blocks. So it can have information regarding groups_per_page
123 * which is blocks_per_page/2
124 *
125 * The buddy cache inode is not stored on disk. The inode is thrown
126 * away when the filesystem is unmounted.
127 *
128 * We look for count number of blocks in the buddy cache. If we were able
129 * to locate that many free blocks we return with additional information
130 * regarding rest of the contiguous physical block available
131 *
132 * Before allocating blocks via buddy cache we normalize the request
133 * blocks. This ensure we ask for more blocks that we needed. The extra
134 * blocks that we get after allocation is added to the respective prealloc
135 * list. In case of inode preallocation we follow a list of heuristics
136 * based on file size. This can be found in ext4_mb_normalize_request. If
137 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'o27baebb2011-09-09 19:02:51 -0400138 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
139 * dependent on the cluster size; for non-bigalloc file systems, it is
Alex Tomasc9de5602008-01-29 00:19:52 -0500140 * 512 blocks. This can be tuned via
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400141 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500142 * terms of number of blocks. If we have mounted the file system with -O
143 * stripe=<value> option the group prealloc request is normalized to the
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400144 * the smallest multiple of the stripe value (sbi->s_stripe) which is
145 * greater than the default mb_group_prealloc.
Alex Tomasc9de5602008-01-29 00:19:52 -0500146 *
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400147 * The regular allocator (using the buddy cache) supports a few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500148 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400149 * /sys/fs/ext4/<partition>/mb_min_to_scan
150 * /sys/fs/ext4/<partition>/mb_max_to_scan
151 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500152 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400153 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500154 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
155 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400156 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200157 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400158 * stripe size. This should result in better allocation on RAID setups. If
159 * not, we search in the specific group using bitmap for best extents. The
160 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500161 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400162 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500163 * best extent in the found extents. Searching for the blocks starts with
164 * the group specified as the goal value in allocation context via
165 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400166 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500167 * checked.
168 *
169 * Both the prealloc space are getting populated as above. So for the first
170 * request we will hit the buddy cache which will result in this prealloc
171 * space getting filled. The prealloc space is then later used for the
172 * subsequent request.
173 */
174
175/*
176 * mballoc operates on the following data:
177 * - on-disk bitmap
178 * - in-core buddy (actually includes buddy and bitmap)
179 * - preallocation descriptors (PAs)
180 *
181 * there are two types of preallocations:
182 * - inode
183 * assiged to specific inode and can be used for this inode only.
184 * it describes part of inode's space preallocated to specific
185 * physical blocks. any block from that preallocated can be used
186 * independent. the descriptor just tracks number of blocks left
187 * unused. so, before taking some block from descriptor, one must
188 * make sure corresponded logical block isn't allocated yet. this
189 * also means that freeing any block within descriptor's range
190 * must discard all preallocated blocks.
191 * - locality group
192 * assigned to specific locality group which does not translate to
193 * permanent set of inodes: inode can join and leave group. space
194 * from this type of preallocation can be used for any inode. thus
195 * it's consumed from the beginning to the end.
196 *
197 * relation between them can be expressed as:
198 * in-core buddy = on-disk bitmap + preallocation descriptors
199 *
200 * this mean blocks mballoc considers used are:
201 * - allocated blocks (persistent)
202 * - preallocated blocks (non-persistent)
203 *
204 * consistency in mballoc world means that at any time a block is either
205 * free or used in ALL structures. notice: "any time" should not be read
206 * literally -- time is discrete and delimited by locks.
207 *
208 * to keep it simple, we don't use block numbers, instead we count number of
209 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
210 *
211 * all operations can be expressed as:
212 * - init buddy: buddy = on-disk + PAs
213 * - new PA: buddy += N; PA = N
214 * - use inode PA: on-disk += N; PA -= N
215 * - discard inode PA buddy -= on-disk - PA; PA = 0
216 * - use locality group PA on-disk += N; PA -= N
217 * - discard locality group PA buddy -= PA; PA = 0
218 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
219 * is used in real operation because we can't know actual used
220 * bits from PA, only from on-disk bitmap
221 *
222 * if we follow this strict logic, then all operations above should be atomic.
223 * given some of them can block, we'd have to use something like semaphores
224 * killing performance on high-end SMP hardware. let's try to relax it using
225 * the following knowledge:
226 * 1) if buddy is referenced, it's already initialized
227 * 2) while block is used in buddy and the buddy is referenced,
228 * nobody can re-allocate that block
229 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
230 * bit set and PA claims same block, it's OK. IOW, one can set bit in
231 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
232 * block
233 *
234 * so, now we're building a concurrency table:
235 * - init buddy vs.
236 * - new PA
237 * blocks for PA are allocated in the buddy, buddy must be referenced
238 * until PA is linked to allocation group to avoid concurrent buddy init
239 * - use inode PA
240 * we need to make sure that either on-disk bitmap or PA has uptodate data
241 * given (3) we care that PA-=N operation doesn't interfere with init
242 * - discard inode PA
243 * the simplest way would be to have buddy initialized by the discard
244 * - use locality group PA
245 * again PA-=N must be serialized with init
246 * - discard locality group PA
247 * the simplest way would be to have buddy initialized by the discard
248 * - new PA vs.
249 * - use inode PA
250 * i_data_sem serializes them
251 * - discard inode PA
252 * discard process must wait until PA isn't used by another process
253 * - use locality group PA
254 * some mutex should serialize them
255 * - discard locality group PA
256 * discard process must wait until PA isn't used by another process
257 * - use inode PA
258 * - use inode PA
259 * i_data_sem or another mutex should serializes them
260 * - discard inode PA
261 * discard process must wait until PA isn't used by another process
262 * - use locality group PA
263 * nothing wrong here -- they're different PAs covering different blocks
264 * - discard locality group PA
265 * discard process must wait until PA isn't used by another process
266 *
267 * now we're ready to make few consequences:
268 * - PA is referenced and while it is no discard is possible
269 * - PA is referenced until block isn't marked in on-disk bitmap
270 * - PA changes only after on-disk bitmap
271 * - discard must not compete with init. either init is done before
272 * any discard or they're serialized somehow
273 * - buddy init as sum of on-disk bitmap and PAs is done atomically
274 *
275 * a special case when we've used PA to emptiness. no need to modify buddy
276 * in this case, but we should care about concurrent init
277 *
278 */
279
280 /*
281 * Logic in few words:
282 *
283 * - allocation:
284 * load group
285 * find blocks
286 * mark bits in on-disk bitmap
287 * release group
288 *
289 * - use preallocation:
290 * find proper PA (per-inode or group)
291 * load group
292 * mark bits in on-disk bitmap
293 * release group
294 * release PA
295 *
296 * - free:
297 * load group
298 * mark bits in on-disk bitmap
299 * release group
300 *
301 * - discard preallocations in group:
302 * mark PAs deleted
303 * move them onto local list
304 * load on-disk bitmap
305 * load group
306 * remove PA from object (inode or locality group)
307 * mark free blocks in-core
308 *
309 * - discard inode's preallocations:
310 */
311
312/*
313 * Locking rules
314 *
315 * Locks:
316 * - bitlock on a group (group)
317 * - object (inode/locality) (object)
318 * - per-pa lock (pa)
319 *
320 * Paths:
321 * - new pa
322 * object
323 * group
324 *
325 * - find and use pa:
326 * pa
327 *
328 * - release consumed pa:
329 * pa
330 * group
331 * object
332 *
333 * - generate in-core bitmap:
334 * group
335 * pa
336 *
337 * - discard all for given object (inode, locality group):
338 * object
339 * pa
340 * group
341 *
342 * - discard all for given group:
343 * group
344 * pa
345 * group
346 * object
347 *
348 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500349static struct kmem_cache *ext4_pspace_cachep;
350static struct kmem_cache *ext4_ac_cachep;
Bobi Jam18aadd42012-02-20 17:53:02 -0500351static struct kmem_cache *ext4_free_data_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400352
353/* We create slab caches for groupinfo data structures based on the
354 * superblock block size. There will be one per mounted filesystem for
355 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500356#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400357static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
358
Eric Sandeen2892c152011-02-12 08:12:18 -0500359static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
360 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
361 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
362 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
363};
364
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500365static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
366 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500367static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
368 ext4_group_t group);
Bobi Jam18aadd42012-02-20 17:53:02 -0500369static void ext4_free_data_callback(struct super_block *sb,
370 struct ext4_journal_cb_entry *jce, int rc);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500371
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500372static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
373{
Alex Tomasc9de5602008-01-29 00:19:52 -0500374#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500375 *bit += ((unsigned long) addr & 7UL) << 3;
376 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500377#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500378 *bit += ((unsigned long) addr & 3UL) << 3;
379 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500380#else
381#error "how many bits you are?!"
382#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500383 return addr;
384}
Alex Tomasc9de5602008-01-29 00:19:52 -0500385
386static inline int mb_test_bit(int bit, void *addr)
387{
388 /*
389 * ext4_test_bit on architecture like powerpc
390 * needs unsigned long aligned address
391 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500392 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500393 return ext4_test_bit(bit, addr);
394}
395
396static inline void mb_set_bit(int bit, void *addr)
397{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500398 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500399 ext4_set_bit(bit, addr);
400}
401
Alex Tomasc9de5602008-01-29 00:19:52 -0500402static inline void mb_clear_bit(int bit, void *addr)
403{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500404 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500405 ext4_clear_bit(bit, addr);
406}
407
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500408static inline int mb_find_next_zero_bit(void *addr, int max, int start)
409{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400410 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500411 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400412 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500413 start += fix;
414
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400415 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
416 if (ret > max)
417 return max;
418 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500419}
420
421static inline int mb_find_next_bit(void *addr, int max, int start)
422{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400423 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500424 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400425 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500426 start += fix;
427
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400428 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
429 if (ret > max)
430 return max;
431 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500432}
433
Alex Tomasc9de5602008-01-29 00:19:52 -0500434static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
435{
436 char *bb;
437
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500438 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -0500439 BUG_ON(max == NULL);
440
441 if (order > e4b->bd_blkbits + 1) {
442 *max = 0;
443 return NULL;
444 }
445
446 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500447 if (order == 0) {
448 *max = 1 << (e4b->bd_blkbits + 3);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500449 return e4b->bd_bitmap;
Coly Li84b775a2011-02-24 12:51:59 -0500450 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500451
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500452 bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
Alex Tomasc9de5602008-01-29 00:19:52 -0500453 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
454
455 return bb;
456}
457
458#ifdef DOUBLE_CHECK
459static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
460 int first, int count)
461{
462 int i;
463 struct super_block *sb = e4b->bd_sb;
464
465 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
466 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400467 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500468 for (i = 0; i < count; i++) {
469 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
470 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500471
472 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400473 blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500474 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400475 inode ? inode->i_ino : 0,
476 blocknr,
477 "freeing block already freed "
478 "(bit %u)",
479 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500480 }
481 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
482 }
483}
484
485static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
486{
487 int i;
488
489 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
490 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400491 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500492 for (i = 0; i < count; i++) {
493 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
494 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
495 }
496}
497
498static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
499{
500 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
501 unsigned char *b1, *b2;
502 int i;
503 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
504 b2 = (unsigned char *) bitmap;
505 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
506 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400507 ext4_msg(e4b->bd_sb, KERN_ERR,
508 "corruption in group %u "
509 "at byte %u(%u): %x in copy != %x "
510 "on disk/prealloc",
511 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500512 BUG();
513 }
514 }
515 }
516}
517
518#else
519static inline void mb_free_blocks_double(struct inode *inode,
520 struct ext4_buddy *e4b, int first, int count)
521{
522 return;
523}
524static inline void mb_mark_used_double(struct ext4_buddy *e4b,
525 int first, int count)
526{
527 return;
528}
529static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
530{
531 return;
532}
533#endif
534
535#ifdef AGGRESSIVE_CHECK
536
537#define MB_CHECK_ASSERT(assert) \
538do { \
539 if (!(assert)) { \
540 printk(KERN_EMERG \
541 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
542 function, file, line, # assert); \
543 BUG(); \
544 } \
545} while (0)
546
547static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
548 const char *function, int line)
549{
550 struct super_block *sb = e4b->bd_sb;
551 int order = e4b->bd_blkbits + 1;
552 int max;
553 int max2;
554 int i;
555 int j;
556 int k;
557 int count;
558 struct ext4_group_info *grp;
559 int fragments = 0;
560 int fstart;
561 struct list_head *cur;
562 void *buddy;
563 void *buddy2;
564
Alex Tomasc9de5602008-01-29 00:19:52 -0500565 {
566 static int mb_check_counter;
567 if (mb_check_counter++ % 100 != 0)
568 return 0;
569 }
570
571 while (order > 1) {
572 buddy = mb_find_buddy(e4b, order, &max);
573 MB_CHECK_ASSERT(buddy);
574 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
575 MB_CHECK_ASSERT(buddy2);
576 MB_CHECK_ASSERT(buddy != buddy2);
577 MB_CHECK_ASSERT(max * 2 == max2);
578
579 count = 0;
580 for (i = 0; i < max; i++) {
581
582 if (mb_test_bit(i, buddy)) {
583 /* only single bit in buddy2 may be 1 */
584 if (!mb_test_bit(i << 1, buddy2)) {
585 MB_CHECK_ASSERT(
586 mb_test_bit((i<<1)+1, buddy2));
587 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
588 MB_CHECK_ASSERT(
589 mb_test_bit(i << 1, buddy2));
590 }
591 continue;
592 }
593
Robin Dong0a10da72011-10-26 08:48:54 -0400594 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500595 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
596 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
597
598 for (j = 0; j < (1 << order); j++) {
599 k = (i * (1 << order)) + j;
600 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500601 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500602 }
603 count++;
604 }
605 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
606 order--;
607 }
608
609 fstart = -1;
610 buddy = mb_find_buddy(e4b, 0, &max);
611 for (i = 0; i < max; i++) {
612 if (!mb_test_bit(i, buddy)) {
613 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
614 if (fstart == -1) {
615 fragments++;
616 fstart = i;
617 }
618 continue;
619 }
620 fstart = -1;
621 /* check used bits only */
622 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
623 buddy2 = mb_find_buddy(e4b, j, &max2);
624 k = i >> j;
625 MB_CHECK_ASSERT(k < max2);
626 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
627 }
628 }
629 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
630 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
631
632 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500633 list_for_each(cur, &grp->bb_prealloc_list) {
634 ext4_group_t groupnr;
635 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400636 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
637 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500638 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400639 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500640 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
641 }
642 return 0;
643}
644#undef MB_CHECK_ASSERT
645#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400646 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500647#else
648#define mb_check_buddy(e4b)
649#endif
650
Coly Li7c786052011-02-24 13:24:25 -0500651/*
652 * Divide blocks started from @first with length @len into
653 * smaller chunks with power of 2 blocks.
654 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
655 * then increase bb_counters[] for corresponded chunk size.
656 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500657static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400658 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500659 struct ext4_group_info *grp)
660{
661 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400662 ext4_grpblk_t min;
663 ext4_grpblk_t max;
664 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500665 unsigned short border;
666
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400667 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500668
669 border = 2 << sb->s_blocksize_bits;
670
671 while (len > 0) {
672 /* find how many blocks can be covered since this position */
673 max = ffs(first | border) - 1;
674
675 /* find how many blocks of power 2 we need to mark */
676 min = fls(len) - 1;
677
678 if (max < min)
679 min = max;
680 chunk = 1 << min;
681
682 /* mark multiblock chunks only */
683 grp->bb_counters[min]++;
684 if (min > 0)
685 mb_clear_bit(first >> min,
686 buddy + sbi->s_mb_offsets[min]);
687
688 len -= chunk;
689 first += chunk;
690 }
691}
692
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400693/*
694 * Cache the order of the largest free extent we have available in this block
695 * group.
696 */
697static void
698mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
699{
700 int i;
701 int bits;
702
703 grp->bb_largest_free_order = -1; /* uninit */
704
705 bits = sb->s_blocksize_bits + 1;
706 for (i = bits; i >= 0; i--) {
707 if (grp->bb_counters[i] > 0) {
708 grp->bb_largest_free_order = i;
709 break;
710 }
711 }
712}
713
Eric Sandeen089ceec2009-07-05 22:17:31 -0400714static noinline_for_stack
715void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500716 void *buddy, void *bitmap, ext4_group_t group)
717{
718 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400719 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400720 ext4_grpblk_t i = 0;
721 ext4_grpblk_t first;
722 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500723 unsigned free = 0;
724 unsigned fragments = 0;
725 unsigned long long period = get_cycles();
726
727 /* initialize buddy from bitmap which is aggregation
728 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500729 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500730 grp->bb_first_free = i;
731 while (i < max) {
732 fragments++;
733 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500734 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500735 len = i - first;
736 free += len;
737 if (len > 1)
738 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
739 else
740 grp->bb_counters[0]++;
741 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500742 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500743 }
744 grp->bb_fragments = fragments;
745
746 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400747 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400748 "%u clusters in bitmap, %u in gd",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400749 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500750 /*
751 * If we intent to continue, we consider group descritor
752 * corrupt and update bb_free using bitmap value
753 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500754 grp->bb_free = free;
755 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400756 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500757
758 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
759
760 period = get_cycles() - period;
761 spin_lock(&EXT4_SB(sb)->s_bal_lock);
762 EXT4_SB(sb)->s_mb_buddies_generated++;
763 EXT4_SB(sb)->s_mb_generation_time += period;
764 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
765}
766
767/* The buddy information is attached the buddy cache inode
768 * for convenience. The information regarding each group
769 * is loaded via ext4_mb_load_buddy. The information involve
770 * block bitmap and buddy information. The information are
771 * stored in the inode as
772 *
773 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500774 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500775 *
776 *
777 * one block each for bitmap and buddy information.
778 * So for each group we take up 2 blocks. A page can
779 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
780 * So it can have information regarding groups_per_page which
781 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400782 *
783 * Locking note: This routine takes the block group lock of all groups
784 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500785 */
786
787static int ext4_mb_init_cache(struct page *page, char *incore)
788{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400789 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500790 int blocksize;
791 int blocks_per_page;
792 int groups_per_page;
793 int err = 0;
794 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500795 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500796 int first_block;
797 struct super_block *sb;
798 struct buffer_head *bhs;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -0400799 struct buffer_head **bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500800 struct inode *inode;
801 char *data;
802 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400803 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500804
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400805 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500806
807 inode = page->mapping->host;
808 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400809 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500810 blocksize = 1 << inode->i_blkbits;
811 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
812
813 groups_per_page = blocks_per_page >> 1;
814 if (groups_per_page == 0)
815 groups_per_page = 1;
816
817 /* allocate buffer_heads to read bitmaps */
818 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500819 i = sizeof(struct buffer_head *) * groups_per_page;
820 bh = kzalloc(i, GFP_NOFS);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500821 if (bh == NULL) {
822 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500823 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500824 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500825 } else
826 bh = &bhs;
827
828 first_group = page->index * blocks_per_page / 2;
829
830 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500831 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
832 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500833 break;
834
Theodore Ts'o813e5722012-02-20 17:52:46 -0500835 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400836 /*
837 * If page is uptodate then we came here after online resize
838 * which added some new uninitialized group info structs, so
839 * we must skip all initialized uptodate buddies on the page,
840 * which may be currently in use by an allocating task.
841 */
842 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
843 bh[i] = NULL;
844 continue;
845 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500846 if (!(bh[i] = ext4_read_block_bitmap_nowait(sb, group))) {
847 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500848 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500849 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500850 mb_debug(1, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500851 }
852
853 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500854 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
855 if (bh[i] && ext4_wait_block_bitmap(sb, group, bh[i])) {
856 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500857 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500858 }
859 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500860
861 first_block = page->index * blocks_per_page;
862 for (i = 0; i < blocks_per_page; i++) {
863 int group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500864
865 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400866 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500867 break;
868
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400869 if (!bh[group - first_group])
870 /* skip initialized uptodate buddy */
871 continue;
872
Alex Tomasc9de5602008-01-29 00:19:52 -0500873 /*
874 * data carry information regarding this
875 * particular group in the format specified
876 * above
877 *
878 */
879 data = page_address(page) + (i * blocksize);
880 bitmap = bh[group - first_group]->b_data;
881
882 /*
883 * We place the buddy block and bitmap block
884 * close together
885 */
886 if ((first_block + i) & 1) {
887 /* this is block of buddy */
888 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400889 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500890 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400891 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500892 grinfo = ext4_get_group_info(sb, group);
893 grinfo->bb_fragments = 0;
894 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400895 sizeof(*grinfo->bb_counters) *
896 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500897 /*
898 * incore got set to the group block bitmap below
899 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500900 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400901 /* init the buddy */
902 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500903 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500904 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500905 incore = NULL;
906 } else {
907 /* this is block of bitmap */
908 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400909 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500910 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400911 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500912
913 /* see comments in ext4_mb_put_pa() */
914 ext4_lock_group(sb, group);
915 memcpy(data, bitmap, blocksize);
916
917 /* mark all preallocated blks used in in-core bitmap */
918 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500919 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500920 ext4_unlock_group(sb, group);
921
922 /* set incore so that the buddy information can be
923 * generated using this
924 */
925 incore = data;
926 }
927 }
928 SetPageUptodate(page);
929
930out:
931 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400932 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500933 brelse(bh[i]);
934 if (bh != &bhs)
935 kfree(bh);
936 }
937 return err;
938}
939
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400940/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400941 * Lock the buddy and bitmap pages. This make sure other parallel init_group
942 * on the same buddy page doesn't happen whild holding the buddy page lock.
943 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
944 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400945 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400946static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
947 ext4_group_t group, struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400948{
Amir Goldstein2de88072011-05-09 21:48:13 -0400949 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
950 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400951 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400952 struct page *page;
953
954 e4b->bd_buddy_page = NULL;
955 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400956
957 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
958 /*
959 * the buddy cache inode stores the block bitmap
960 * and buddy information in consecutive blocks.
961 * So for each group we need two blocks.
962 */
963 block = group * 2;
964 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400965 poff = block % blocks_per_page;
966 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
967 if (!page)
968 return -EIO;
969 BUG_ON(page->mapping != inode->i_mapping);
970 e4b->bd_bitmap_page = page;
971 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400972
Amir Goldstein2de88072011-05-09 21:48:13 -0400973 if (blocks_per_page >= 2) {
974 /* buddy and bitmap are on the same page */
975 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400976 }
Amir Goldstein2de88072011-05-09 21:48:13 -0400977
978 block++;
979 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400980 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
981 if (!page)
982 return -EIO;
983 BUG_ON(page->mapping != inode->i_mapping);
984 e4b->bd_buddy_page = page;
985 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400986}
987
Amir Goldstein2de88072011-05-09 21:48:13 -0400988static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400989{
Amir Goldstein2de88072011-05-09 21:48:13 -0400990 if (e4b->bd_bitmap_page) {
991 unlock_page(e4b->bd_bitmap_page);
992 page_cache_release(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400993 }
Amir Goldstein2de88072011-05-09 21:48:13 -0400994 if (e4b->bd_buddy_page) {
995 unlock_page(e4b->bd_buddy_page);
996 page_cache_release(e4b->bd_buddy_page);
997 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400998}
999
1000/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001001 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1002 * block group lock of all groups for this page; do not hold the BG lock when
1003 * calling this routine!
1004 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001005static noinline_for_stack
1006int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1007{
1008
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001009 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001010 struct ext4_buddy e4b;
1011 struct page *page;
1012 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001013
1014 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001015 this_grp = ext4_get_group_info(sb, group);
1016 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001017 * This ensures that we don't reinit the buddy cache
1018 * page which map to the group from which we are already
1019 * allocating. If we are looking at the buddy cache we would
1020 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001021 * would have pinned buddy page to page cache.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001022 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001023 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b);
1024 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001025 /*
1026 * somebody initialized the group
1027 * return without doing anything
1028 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001029 goto err;
1030 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001031
1032 page = e4b.bd_bitmap_page;
1033 ret = ext4_mb_init_cache(page, NULL);
1034 if (ret)
1035 goto err;
1036 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001037 ret = -EIO;
1038 goto err;
1039 }
1040 mark_page_accessed(page);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001041
Amir Goldstein2de88072011-05-09 21:48:13 -04001042 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001043 /*
1044 * If both the bitmap and buddy are in
1045 * the same page we don't need to force
1046 * init the buddy
1047 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001048 ret = 0;
1049 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001050 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001051 /* init buddy cache */
1052 page = e4b.bd_buddy_page;
1053 ret = ext4_mb_init_cache(page, e4b.bd_bitmap);
1054 if (ret)
1055 goto err;
1056 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001057 ret = -EIO;
1058 goto err;
1059 }
1060 mark_page_accessed(page);
1061err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001062 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001063 return ret;
1064}
1065
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001066/*
1067 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1068 * block group lock of all groups for this page; do not hold the BG lock when
1069 * calling this routine!
1070 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001071static noinline_for_stack int
1072ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1073 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001074{
Alex Tomasc9de5602008-01-29 00:19:52 -05001075 int blocks_per_page;
1076 int block;
1077 int pnum;
1078 int poff;
1079 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001080 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001081 struct ext4_group_info *grp;
1082 struct ext4_sb_info *sbi = EXT4_SB(sb);
1083 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001084
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001085 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001086
1087 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001088 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001089
1090 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001091 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001092 e4b->bd_sb = sb;
1093 e4b->bd_group = group;
1094 e4b->bd_buddy_page = NULL;
1095 e4b->bd_bitmap_page = NULL;
1096
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001097 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001098 /*
1099 * we need full data about the group
1100 * to make a good selection
1101 */
1102 ret = ext4_mb_init_group(sb, group);
1103 if (ret)
1104 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001105 }
1106
Alex Tomasc9de5602008-01-29 00:19:52 -05001107 /*
1108 * the buddy cache inode stores the block bitmap
1109 * and buddy information in consecutive blocks.
1110 * So for each group we need two blocks.
1111 */
1112 block = group * 2;
1113 pnum = block / blocks_per_page;
1114 poff = block % blocks_per_page;
1115
1116 /* we could use find_or_create_page(), but it locks page
1117 * what we'd like to avoid in fast path ... */
1118 page = find_get_page(inode->i_mapping, pnum);
1119 if (page == NULL || !PageUptodate(page)) {
1120 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001121 /*
1122 * drop the page reference and try
1123 * to get the page with lock. If we
1124 * are not uptodate that implies
1125 * somebody just created the page but
1126 * is yet to initialize the same. So
1127 * wait for it to initialize.
1128 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001129 page_cache_release(page);
1130 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1131 if (page) {
1132 BUG_ON(page->mapping != inode->i_mapping);
1133 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001134 ret = ext4_mb_init_cache(page, NULL);
1135 if (ret) {
1136 unlock_page(page);
1137 goto err;
1138 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001139 mb_cmp_bitmaps(e4b, page_address(page) +
1140 (poff * sb->s_blocksize));
1141 }
1142 unlock_page(page);
1143 }
1144 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001145 if (page == NULL || !PageUptodate(page)) {
1146 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001147 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001148 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001149 e4b->bd_bitmap_page = page;
1150 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1151 mark_page_accessed(page);
1152
1153 block++;
1154 pnum = block / blocks_per_page;
1155 poff = block % blocks_per_page;
1156
1157 page = find_get_page(inode->i_mapping, pnum);
1158 if (page == NULL || !PageUptodate(page)) {
1159 if (page)
1160 page_cache_release(page);
1161 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1162 if (page) {
1163 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001164 if (!PageUptodate(page)) {
1165 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1166 if (ret) {
1167 unlock_page(page);
1168 goto err;
1169 }
1170 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001171 unlock_page(page);
1172 }
1173 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001174 if (page == NULL || !PageUptodate(page)) {
1175 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001176 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001177 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001178 e4b->bd_buddy_page = page;
1179 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1180 mark_page_accessed(page);
1181
1182 BUG_ON(e4b->bd_bitmap_page == NULL);
1183 BUG_ON(e4b->bd_buddy_page == NULL);
1184
1185 return 0;
1186
1187err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001188 if (page)
1189 page_cache_release(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001190 if (e4b->bd_bitmap_page)
1191 page_cache_release(e4b->bd_bitmap_page);
1192 if (e4b->bd_buddy_page)
1193 page_cache_release(e4b->bd_buddy_page);
1194 e4b->bd_buddy = NULL;
1195 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001196 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001197}
1198
Jing Zhange39e07f2010-05-14 00:00:00 -04001199static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001200{
1201 if (e4b->bd_bitmap_page)
1202 page_cache_release(e4b->bd_bitmap_page);
1203 if (e4b->bd_buddy_page)
1204 page_cache_release(e4b->bd_buddy_page);
1205}
1206
1207
1208static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1209{
1210 int order = 1;
1211 void *bb;
1212
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001213 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -05001214 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1215
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001216 bb = e4b->bd_buddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05001217 while (order <= e4b->bd_blkbits + 1) {
1218 block = block >> 1;
1219 if (!mb_test_bit(block, bb)) {
1220 /* this block is part of buddy of order 'order' */
1221 return order;
1222 }
1223 bb += 1 << (e4b->bd_blkbits - order);
1224 order++;
1225 }
1226 return 0;
1227}
1228
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001229static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001230{
1231 __u32 *addr;
1232
1233 len = cur + len;
1234 while (cur < len) {
1235 if ((cur & 31) == 0 && (len - cur) >= 32) {
1236 /* fast path: clear whole word at once */
1237 addr = bm + (cur >> 3);
1238 *addr = 0;
1239 cur += 32;
1240 continue;
1241 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001242 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001243 cur++;
1244 }
1245}
1246
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001247void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001248{
1249 __u32 *addr;
1250
1251 len = cur + len;
1252 while (cur < len) {
1253 if ((cur & 31) == 0 && (len - cur) >= 32) {
1254 /* fast path: set whole word at once */
1255 addr = bm + (cur >> 3);
1256 *addr = 0xffffffff;
1257 cur += 32;
1258 continue;
1259 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001260 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001261 cur++;
1262 }
1263}
1264
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001265static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001266 int first, int count)
1267{
1268 int block = 0;
1269 int max = 0;
1270 int order;
1271 void *buddy;
1272 void *buddy2;
1273 struct super_block *sb = e4b->bd_sb;
1274
1275 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001276 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001277 mb_check_buddy(e4b);
1278 mb_free_blocks_double(inode, e4b, first, count);
1279
1280 e4b->bd_info->bb_free += count;
1281 if (first < e4b->bd_info->bb_first_free)
1282 e4b->bd_info->bb_first_free = first;
1283
1284 /* let's maintain fragments counter */
1285 if (first != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001286 block = !mb_test_bit(first - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001287 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001288 max = !mb_test_bit(first + count, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001289 if (block && max)
1290 e4b->bd_info->bb_fragments--;
1291 else if (!block && !max)
1292 e4b->bd_info->bb_fragments++;
1293
1294 /* let's maintain buddy itself */
1295 while (count-- > 0) {
1296 block = first++;
1297 order = 0;
1298
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001299 if (!mb_test_bit(block, e4b->bd_bitmap)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001300 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001301
1302 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001303 blocknr += EXT4_C2B(EXT4_SB(sb), block);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001304 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001305 inode ? inode->i_ino : 0,
1306 blocknr,
1307 "freeing already freed block "
1308 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001309 }
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001310 mb_clear_bit(block, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001311 e4b->bd_info->bb_counters[order]++;
1312
1313 /* start of the buddy */
1314 buddy = mb_find_buddy(e4b, order, &max);
1315
1316 do {
1317 block &= ~1UL;
1318 if (mb_test_bit(block, buddy) ||
1319 mb_test_bit(block + 1, buddy))
1320 break;
1321
1322 /* both the buddies are free, try to coalesce them */
1323 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1324
1325 if (!buddy2)
1326 break;
1327
1328 if (order > 0) {
1329 /* for special purposes, we don't set
1330 * free bits in bitmap */
1331 mb_set_bit(block, buddy);
1332 mb_set_bit(block + 1, buddy);
1333 }
1334 e4b->bd_info->bb_counters[order]--;
1335 e4b->bd_info->bb_counters[order]--;
1336
1337 block = block >> 1;
1338 order++;
1339 e4b->bd_info->bb_counters[order]++;
1340
1341 mb_clear_bit(block, buddy2);
1342 buddy = buddy2;
1343 } while (1);
1344 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001345 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001346 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001347}
1348
Robin Dong15c006a2012-08-17 10:02:17 -04001349static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001350 int needed, struct ext4_free_extent *ex)
1351{
1352 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001353 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001354 void *buddy;
1355
Vincent Minetbc8e6742009-05-15 08:33:18 -04001356 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001357 BUG_ON(ex == NULL);
1358
Robin Dong15c006a2012-08-17 10:02:17 -04001359 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001360 BUG_ON(buddy == NULL);
1361 BUG_ON(block >= max);
1362 if (mb_test_bit(block, buddy)) {
1363 ex->fe_len = 0;
1364 ex->fe_start = 0;
1365 ex->fe_group = 0;
1366 return 0;
1367 }
1368
Robin Dong15c006a2012-08-17 10:02:17 -04001369 /* find actual order */
1370 order = mb_find_order_for_block(e4b, block);
1371 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001372
1373 ex->fe_len = 1 << order;
1374 ex->fe_start = block << order;
1375 ex->fe_group = e4b->bd_group;
1376
1377 /* calc difference from given start */
1378 next = next - ex->fe_start;
1379 ex->fe_len -= next;
1380 ex->fe_start += next;
1381
1382 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001383 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001384
1385 if (block + 1 >= max)
1386 break;
1387
1388 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001389 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001390 break;
1391
Robin Dongb051d8d2011-10-26 05:30:30 -04001392 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001393
Alex Tomasc9de5602008-01-29 00:19:52 -05001394 block = next >> order;
1395 ex->fe_len += 1 << order;
1396 }
1397
1398 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1399 return ex->fe_len;
1400}
1401
1402static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1403{
1404 int ord;
1405 int mlen = 0;
1406 int max = 0;
1407 int cur;
1408 int start = ex->fe_start;
1409 int len = ex->fe_len;
1410 unsigned ret = 0;
1411 int len0 = len;
1412 void *buddy;
1413
1414 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1415 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001416 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001417 mb_check_buddy(e4b);
1418 mb_mark_used_double(e4b, start, len);
1419
1420 e4b->bd_info->bb_free -= len;
1421 if (e4b->bd_info->bb_first_free == start)
1422 e4b->bd_info->bb_first_free += len;
1423
1424 /* let's maintain fragments counter */
1425 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001426 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001427 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001428 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001429 if (mlen && max)
1430 e4b->bd_info->bb_fragments++;
1431 else if (!mlen && !max)
1432 e4b->bd_info->bb_fragments--;
1433
1434 /* let's maintain buddy itself */
1435 while (len) {
1436 ord = mb_find_order_for_block(e4b, start);
1437
1438 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1439 /* the whole chunk may be allocated at once! */
1440 mlen = 1 << ord;
1441 buddy = mb_find_buddy(e4b, ord, &max);
1442 BUG_ON((start >> ord) >= max);
1443 mb_set_bit(start >> ord, buddy);
1444 e4b->bd_info->bb_counters[ord]--;
1445 start += mlen;
1446 len -= mlen;
1447 BUG_ON(len < 0);
1448 continue;
1449 }
1450
1451 /* store for history */
1452 if (ret == 0)
1453 ret = len | (ord << 16);
1454
1455 /* we have to split large buddy */
1456 BUG_ON(ord <= 0);
1457 buddy = mb_find_buddy(e4b, ord, &max);
1458 mb_set_bit(start >> ord, buddy);
1459 e4b->bd_info->bb_counters[ord]--;
1460
1461 ord--;
1462 cur = (start >> ord) & ~1U;
1463 buddy = mb_find_buddy(e4b, ord, &max);
1464 mb_clear_bit(cur, buddy);
1465 mb_clear_bit(cur + 1, buddy);
1466 e4b->bd_info->bb_counters[ord]++;
1467 e4b->bd_info->bb_counters[ord]++;
1468 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001469 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001470
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001471 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001472 mb_check_buddy(e4b);
1473
1474 return ret;
1475}
1476
1477/*
1478 * Must be called under group lock!
1479 */
1480static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1481 struct ext4_buddy *e4b)
1482{
1483 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1484 int ret;
1485
1486 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1487 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1488
1489 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1490 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1491 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1492
1493 /* preallocation can change ac_b_ex, thus we store actually
1494 * allocated blocks for history */
1495 ac->ac_f_ex = ac->ac_b_ex;
1496
1497 ac->ac_status = AC_STATUS_FOUND;
1498 ac->ac_tail = ret & 0xffff;
1499 ac->ac_buddy = ret >> 16;
1500
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001501 /*
1502 * take the page reference. We want the page to be pinned
1503 * so that we don't get a ext4_mb_init_cache_call for this
1504 * group until we update the bitmap. That would mean we
1505 * double allocate blocks. The reference is dropped
1506 * in ext4_mb_release_context
1507 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001508 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1509 get_page(ac->ac_bitmap_page);
1510 ac->ac_buddy_page = e4b->bd_buddy_page;
1511 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001512 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001513 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001514 spin_lock(&sbi->s_md_lock);
1515 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1516 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1517 spin_unlock(&sbi->s_md_lock);
1518 }
1519}
1520
1521/*
1522 * regular allocator, for general purposes allocation
1523 */
1524
1525static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1526 struct ext4_buddy *e4b,
1527 int finish_group)
1528{
1529 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1530 struct ext4_free_extent *bex = &ac->ac_b_ex;
1531 struct ext4_free_extent *gex = &ac->ac_g_ex;
1532 struct ext4_free_extent ex;
1533 int max;
1534
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001535 if (ac->ac_status == AC_STATUS_FOUND)
1536 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001537 /*
1538 * We don't want to scan for a whole year
1539 */
1540 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1541 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1542 ac->ac_status = AC_STATUS_BREAK;
1543 return;
1544 }
1545
1546 /*
1547 * Haven't found good chunk so far, let's continue
1548 */
1549 if (bex->fe_len < gex->fe_len)
1550 return;
1551
1552 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1553 && bex->fe_group == e4b->bd_group) {
1554 /* recheck chunk's availability - we don't know
1555 * when it was found (within this lock-unlock
1556 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001557 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001558 if (max >= gex->fe_len) {
1559 ext4_mb_use_best_found(ac, e4b);
1560 return;
1561 }
1562 }
1563}
1564
1565/*
1566 * The routine checks whether found extent is good enough. If it is,
1567 * then the extent gets marked used and flag is set to the context
1568 * to stop scanning. Otherwise, the extent is compared with the
1569 * previous found extent and if new one is better, then it's stored
1570 * in the context. Later, the best found extent will be used, if
1571 * mballoc can't find good enough extent.
1572 *
1573 * FIXME: real allocation policy is to be designed yet!
1574 */
1575static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1576 struct ext4_free_extent *ex,
1577 struct ext4_buddy *e4b)
1578{
1579 struct ext4_free_extent *bex = &ac->ac_b_ex;
1580 struct ext4_free_extent *gex = &ac->ac_g_ex;
1581
1582 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001583 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1584 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001585 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1586
1587 ac->ac_found++;
1588
1589 /*
1590 * The special case - take what you catch first
1591 */
1592 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1593 *bex = *ex;
1594 ext4_mb_use_best_found(ac, e4b);
1595 return;
1596 }
1597
1598 /*
1599 * Let's check whether the chuck is good enough
1600 */
1601 if (ex->fe_len == gex->fe_len) {
1602 *bex = *ex;
1603 ext4_mb_use_best_found(ac, e4b);
1604 return;
1605 }
1606
1607 /*
1608 * If this is first found extent, just store it in the context
1609 */
1610 if (bex->fe_len == 0) {
1611 *bex = *ex;
1612 return;
1613 }
1614
1615 /*
1616 * If new found extent is better, store it in the context
1617 */
1618 if (bex->fe_len < gex->fe_len) {
1619 /* if the request isn't satisfied, any found extent
1620 * larger than previous best one is better */
1621 if (ex->fe_len > bex->fe_len)
1622 *bex = *ex;
1623 } else if (ex->fe_len > gex->fe_len) {
1624 /* if the request is satisfied, then we try to find
1625 * an extent that still satisfy the request, but is
1626 * smaller than previous one */
1627 if (ex->fe_len < bex->fe_len)
1628 *bex = *ex;
1629 }
1630
1631 ext4_mb_check_limits(ac, e4b, 0);
1632}
1633
Eric Sandeen089ceec2009-07-05 22:17:31 -04001634static noinline_for_stack
1635int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001636 struct ext4_buddy *e4b)
1637{
1638 struct ext4_free_extent ex = ac->ac_b_ex;
1639 ext4_group_t group = ex.fe_group;
1640 int max;
1641 int err;
1642
1643 BUG_ON(ex.fe_len <= 0);
1644 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1645 if (err)
1646 return err;
1647
1648 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001649 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001650
1651 if (max > 0) {
1652 ac->ac_b_ex = ex;
1653 ext4_mb_use_best_found(ac, e4b);
1654 }
1655
1656 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001657 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001658
1659 return 0;
1660}
1661
Eric Sandeen089ceec2009-07-05 22:17:31 -04001662static noinline_for_stack
1663int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001664 struct ext4_buddy *e4b)
1665{
1666 ext4_group_t group = ac->ac_g_ex.fe_group;
1667 int max;
1668 int err;
1669 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001670 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001671 struct ext4_free_extent ex;
1672
1673 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1674 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001675 if (grp->bb_free == 0)
1676 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001677
1678 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1679 if (err)
1680 return err;
1681
1682 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001683 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001684 ac->ac_g_ex.fe_len, &ex);
1685
1686 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1687 ext4_fsblk_t start;
1688
Akinobu Mita5661bd62010-03-03 23:53:39 -05001689 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1690 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001691 /* use do_div to get remainder (would be 64-bit modulo) */
1692 if (do_div(start, sbi->s_stripe) == 0) {
1693 ac->ac_found++;
1694 ac->ac_b_ex = ex;
1695 ext4_mb_use_best_found(ac, e4b);
1696 }
1697 } else if (max >= ac->ac_g_ex.fe_len) {
1698 BUG_ON(ex.fe_len <= 0);
1699 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1700 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1701 ac->ac_found++;
1702 ac->ac_b_ex = ex;
1703 ext4_mb_use_best_found(ac, e4b);
1704 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1705 /* Sometimes, caller may want to merge even small
1706 * number of blocks to an existing extent */
1707 BUG_ON(ex.fe_len <= 0);
1708 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1709 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1710 ac->ac_found++;
1711 ac->ac_b_ex = ex;
1712 ext4_mb_use_best_found(ac, e4b);
1713 }
1714 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001715 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001716
1717 return 0;
1718}
1719
1720/*
1721 * The routine scans buddy structures (not bitmap!) from given order
1722 * to max order and tries to find big enough chunk to satisfy the req
1723 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001724static noinline_for_stack
1725void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001726 struct ext4_buddy *e4b)
1727{
1728 struct super_block *sb = ac->ac_sb;
1729 struct ext4_group_info *grp = e4b->bd_info;
1730 void *buddy;
1731 int i;
1732 int k;
1733 int max;
1734
1735 BUG_ON(ac->ac_2order <= 0);
1736 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1737 if (grp->bb_counters[i] == 0)
1738 continue;
1739
1740 buddy = mb_find_buddy(e4b, i, &max);
1741 BUG_ON(buddy == NULL);
1742
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001743 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001744 BUG_ON(k >= max);
1745
1746 ac->ac_found++;
1747
1748 ac->ac_b_ex.fe_len = 1 << i;
1749 ac->ac_b_ex.fe_start = k << i;
1750 ac->ac_b_ex.fe_group = e4b->bd_group;
1751
1752 ext4_mb_use_best_found(ac, e4b);
1753
1754 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1755
1756 if (EXT4_SB(sb)->s_mb_stats)
1757 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1758
1759 break;
1760 }
1761}
1762
1763/*
1764 * The routine scans the group and measures all found extents.
1765 * In order to optimize scanning, caller must pass number of
1766 * free blocks in the group, so the routine can know upper limit.
1767 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001768static noinline_for_stack
1769void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001770 struct ext4_buddy *e4b)
1771{
1772 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001773 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001774 struct ext4_free_extent ex;
1775 int i;
1776 int free;
1777
1778 free = e4b->bd_info->bb_free;
1779 BUG_ON(free <= 0);
1780
1781 i = e4b->bd_info->bb_first_free;
1782
1783 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001784 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001785 EXT4_CLUSTERS_PER_GROUP(sb), i);
1786 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001787 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001788 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001789 * free blocks even though group info says we
1790 * we have free blocks
1791 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001792 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001793 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001794 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001795 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001796 break;
1797 }
1798
Robin Dong15c006a2012-08-17 10:02:17 -04001799 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001800 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001801 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001802 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001803 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001804 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001805 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001806 /*
1807 * The number of free blocks differs. This mostly
1808 * indicate that the bitmap is corrupt. So exit
1809 * without claiming the space.
1810 */
1811 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001812 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001813
1814 ext4_mb_measure_extent(ac, &ex, e4b);
1815
1816 i += ex.fe_len;
1817 free -= ex.fe_len;
1818 }
1819
1820 ext4_mb_check_limits(ac, e4b, 1);
1821}
1822
1823/*
1824 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001825 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001826 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001827static noinline_for_stack
1828void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001829 struct ext4_buddy *e4b)
1830{
1831 struct super_block *sb = ac->ac_sb;
1832 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001833 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001834 struct ext4_free_extent ex;
1835 ext4_fsblk_t first_group_block;
1836 ext4_fsblk_t a;
1837 ext4_grpblk_t i;
1838 int max;
1839
1840 BUG_ON(sbi->s_stripe == 0);
1841
1842 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001843 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1844
Alex Tomasc9de5602008-01-29 00:19:52 -05001845 a = first_group_block + sbi->s_stripe - 1;
1846 do_div(a, sbi->s_stripe);
1847 i = (a * sbi->s_stripe) - first_group_block;
1848
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001849 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001850 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04001851 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001852 if (max >= sbi->s_stripe) {
1853 ac->ac_found++;
1854 ac->ac_b_ex = ex;
1855 ext4_mb_use_best_found(ac, e4b);
1856 break;
1857 }
1858 }
1859 i += sbi->s_stripe;
1860 }
1861}
1862
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001863/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001864static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1865 ext4_group_t group, int cr)
1866{
1867 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001868 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001869 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1870
1871 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001872
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04001873 free = grp->bb_free;
1874 if (free == 0)
1875 return 0;
1876 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
1877 return 0;
1878
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001879 /* We only do this if the grp has never been initialized */
1880 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1881 int ret = ext4_mb_init_group(ac->ac_sb, group);
1882 if (ret)
1883 return 0;
1884 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001885
Alex Tomasc9de5602008-01-29 00:19:52 -05001886 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05001887 if (fragments == 0)
1888 return 0;
1889
1890 switch (cr) {
1891 case 0:
1892 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001893
Theodore Ts'oa4912122009-03-12 12:18:34 -04001894 /* Avoid using the first bg of a flexgroup for data files */
1895 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1896 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1897 ((group % flex_size) == 0))
1898 return 0;
1899
Theodore Ts'o40ae3482013-02-04 15:08:40 -05001900 if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) ||
1901 (free / fragments) >= ac->ac_g_ex.fe_len)
1902 return 1;
1903
1904 if (grp->bb_largest_free_order < ac->ac_2order)
1905 return 0;
1906
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001907 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001908 case 1:
1909 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1910 return 1;
1911 break;
1912 case 2:
1913 if (free >= ac->ac_g_ex.fe_len)
1914 return 1;
1915 break;
1916 case 3:
1917 return 1;
1918 default:
1919 BUG();
1920 }
1921
1922 return 0;
1923}
1924
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001925static noinline_for_stack int
1926ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001927{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001928 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001929 int cr;
1930 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001931 struct ext4_sb_info *sbi;
1932 struct super_block *sb;
1933 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001934
1935 sb = ac->ac_sb;
1936 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001937 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001938 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001939 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001940 ngroups = sbi->s_blockfile_groups;
1941
Alex Tomasc9de5602008-01-29 00:19:52 -05001942 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1943
1944 /* first, try the goal */
1945 err = ext4_mb_find_by_goal(ac, &e4b);
1946 if (err || ac->ac_status == AC_STATUS_FOUND)
1947 goto out;
1948
1949 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1950 goto out;
1951
1952 /*
1953 * ac->ac2_order is set only if the fe_len is a power of 2
1954 * if ac2_order is set we also set criteria to 0 so that we
1955 * try exact allocation using buddy.
1956 */
1957 i = fls(ac->ac_g_ex.fe_len);
1958 ac->ac_2order = 0;
1959 /*
1960 * We search using buddy data only if the order of the request
1961 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04001962 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05001963 */
1964 if (i >= sbi->s_mb_order2_reqs) {
1965 /*
1966 * This should tell if fe_len is exactly power of 2
1967 */
1968 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1969 ac->ac_2order = i - 1;
1970 }
1971
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001972 /* if stream allocation is enabled, use global goal */
1973 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001974 /* TBD: may be hot point */
1975 spin_lock(&sbi->s_md_lock);
1976 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
1977 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
1978 spin_unlock(&sbi->s_md_lock);
1979 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001980
Alex Tomasc9de5602008-01-29 00:19:52 -05001981 /* Let's just scan groups to find more-less suitable blocks */
1982 cr = ac->ac_2order ? 0 : 1;
1983 /*
1984 * cr == 0 try to get exact allocation,
1985 * cr == 3 try to get anything
1986 */
1987repeat:
1988 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
1989 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04001990 /*
1991 * searching for the right group start
1992 * from the goal value specified
1993 */
1994 group = ac->ac_g_ex.fe_group;
1995
Theodore Ts'o8df96752009-05-01 08:50:38 -04001996 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04001997 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05001998 group = 0;
1999
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002000 /* This now checks without needing the buddy page */
2001 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002002 continue;
2003
Alex Tomasc9de5602008-01-29 00:19:52 -05002004 err = ext4_mb_load_buddy(sb, group, &e4b);
2005 if (err)
2006 goto out;
2007
2008 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002009
2010 /*
2011 * We need to check again after locking the
2012 * block group
2013 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002014 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002015 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002016 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002017 continue;
2018 }
2019
2020 ac->ac_groups_scanned++;
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002021 if (cr == 0 && ac->ac_2order < sb->s_blocksize_bits+2)
Alex Tomasc9de5602008-01-29 00:19:52 -05002022 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002023 else if (cr == 1 && sbi->s_stripe &&
2024 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002025 ext4_mb_scan_aligned(ac, &e4b);
2026 else
2027 ext4_mb_complex_scan_group(ac, &e4b);
2028
2029 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002030 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002031
2032 if (ac->ac_status != AC_STATUS_CONTINUE)
2033 break;
2034 }
2035 }
2036
2037 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2038 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2039 /*
2040 * We've been searching too long. Let's try to allocate
2041 * the best chunk we've found so far
2042 */
2043
2044 ext4_mb_try_best_found(ac, &e4b);
2045 if (ac->ac_status != AC_STATUS_FOUND) {
2046 /*
2047 * Someone more lucky has already allocated it.
2048 * The only thing we can do is just take first
2049 * found block(s)
2050 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2051 */
2052 ac->ac_b_ex.fe_group = 0;
2053 ac->ac_b_ex.fe_start = 0;
2054 ac->ac_b_ex.fe_len = 0;
2055 ac->ac_status = AC_STATUS_CONTINUE;
2056 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2057 cr = 3;
2058 atomic_inc(&sbi->s_mb_lost_chunks);
2059 goto repeat;
2060 }
2061 }
2062out:
2063 return err;
2064}
2065
Alex Tomasc9de5602008-01-29 00:19:52 -05002066static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2067{
2068 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002069 ext4_group_t group;
2070
Theodore Ts'o8df96752009-05-01 08:50:38 -04002071 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002072 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002073 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002074 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002075}
2076
2077static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2078{
2079 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002080 ext4_group_t group;
2081
2082 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002083 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002084 return NULL;
2085 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002086 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002087}
2088
2089static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2090{
2091 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002092 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002093 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002094 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002095 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002096 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -05002097 struct sg {
2098 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002099 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002100 } sg;
2101
2102 group--;
2103 if (group == 0)
2104 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2105 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2106 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2107 "group", "free", "frags", "first",
2108 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2109 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2110
2111 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2112 sizeof(struct ext4_group_info);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002113 grinfo = ext4_get_group_info(sb, group);
2114 /* Load the group info in memory only if not already loaded. */
2115 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2116 err = ext4_mb_load_buddy(sb, group, &e4b);
2117 if (err) {
2118 seq_printf(seq, "#%-5u: I/O error\n", group);
2119 return 0;
2120 }
2121 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002122 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002123
Alex Tomasc9de5602008-01-29 00:19:52 -05002124 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002125
2126 if (buddy_loaded)
2127 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002128
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002129 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002130 sg.info.bb_fragments, sg.info.bb_first_free);
2131 for (i = 0; i <= 13; i++)
2132 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2133 sg.info.bb_counters[i] : 0);
2134 seq_printf(seq, " ]\n");
2135
2136 return 0;
2137}
2138
2139static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2140{
2141}
2142
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002143static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002144 .start = ext4_mb_seq_groups_start,
2145 .next = ext4_mb_seq_groups_next,
2146 .stop = ext4_mb_seq_groups_stop,
2147 .show = ext4_mb_seq_groups_show,
2148};
2149
2150static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2151{
2152 struct super_block *sb = PDE(inode)->data;
2153 int rc;
2154
2155 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2156 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002157 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002158 m->private = sb;
2159 }
2160 return rc;
2161
2162}
2163
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002164static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002165 .owner = THIS_MODULE,
2166 .open = ext4_mb_seq_groups_open,
2167 .read = seq_read,
2168 .llseek = seq_lseek,
2169 .release = seq_release,
2170};
2171
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002172static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2173{
2174 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2175 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2176
2177 BUG_ON(!cachep);
2178 return cachep;
2179}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002180
Theodore Ts'o28623c22012-09-05 01:31:50 -04002181/*
2182 * Allocate the top-level s_group_info array for the specified number
2183 * of groups
2184 */
2185int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2186{
2187 struct ext4_sb_info *sbi = EXT4_SB(sb);
2188 unsigned size;
2189 struct ext4_group_info ***new_groupinfo;
2190
2191 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2192 EXT4_DESC_PER_BLOCK_BITS(sb);
2193 if (size <= sbi->s_group_info_size)
2194 return 0;
2195
2196 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
2197 new_groupinfo = ext4_kvzalloc(size, GFP_KERNEL);
2198 if (!new_groupinfo) {
2199 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2200 return -ENOMEM;
2201 }
2202 if (sbi->s_group_info) {
2203 memcpy(new_groupinfo, sbi->s_group_info,
2204 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
2205 ext4_kvfree(sbi->s_group_info);
2206 }
2207 sbi->s_group_info = new_groupinfo;
2208 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
2209 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2210 sbi->s_group_info_size);
2211 return 0;
2212}
2213
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002214/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002215int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002216 struct ext4_group_desc *desc)
2217{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002218 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002219 int metalen = 0;
2220 struct ext4_sb_info *sbi = EXT4_SB(sb);
2221 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002222 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002223
2224 /*
2225 * First check if this group is the first of a reserved block.
2226 * If it's true, we have to allocate a new table of pointers
2227 * to ext4_group_info structures
2228 */
2229 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2230 metalen = sizeof(*meta_group_info) <<
2231 EXT4_DESC_PER_BLOCK_BITS(sb);
2232 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2233 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002234 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002235 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002236 goto exit_meta_group_info;
2237 }
2238 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2239 meta_group_info;
2240 }
2241
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002242 meta_group_info =
2243 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2244 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2245
Wei Yongjun85556c92012-09-26 20:43:37 -04002246 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002247 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002248 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002249 goto exit_group_info;
2250 }
2251 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2252 &(meta_group_info[i]->bb_state));
2253
2254 /*
2255 * initialize bb_free to be able to skip
2256 * empty groups without initialization
2257 */
2258 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2259 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002260 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002261 } else {
2262 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002263 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002264 }
2265
2266 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002267 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002268 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002269 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002270
2271#ifdef DOUBLE_CHECK
2272 {
2273 struct buffer_head *bh;
2274 meta_group_info[i]->bb_bitmap =
2275 kmalloc(sb->s_blocksize, GFP_KERNEL);
2276 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2277 bh = ext4_read_block_bitmap(sb, group);
2278 BUG_ON(bh == NULL);
2279 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2280 sb->s_blocksize);
2281 put_bh(bh);
2282 }
2283#endif
2284
2285 return 0;
2286
2287exit_group_info:
2288 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002289 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002290 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002291 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2292 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002293exit_meta_group_info:
2294 return -ENOMEM;
2295} /* ext4_mb_add_groupinfo */
2296
Alex Tomasc9de5602008-01-29 00:19:52 -05002297static int ext4_mb_init_backend(struct super_block *sb)
2298{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002299 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002300 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002301 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002302 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002303 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002304 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002305
Theodore Ts'o28623c22012-09-05 01:31:50 -04002306 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2307 if (err)
2308 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002309
Alex Tomasc9de5602008-01-29 00:19:52 -05002310 sbi->s_buddy_cache = new_inode(sb);
2311 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002312 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002313 goto err_freesgi;
2314 }
Yu Jian48e60612011-08-01 17:41:39 -04002315 /* To avoid potentially colliding with an valid on-disk inode number,
2316 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2317 * not in the inode hash, so it should never be found by iget(), but
2318 * this will avoid confusion if it ever shows up during debugging. */
2319 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002320 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002321 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002322 desc = ext4_get_group_desc(sb, i, NULL);
2323 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002324 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002325 goto err_freebuddy;
2326 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002327 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2328 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002329 }
2330
2331 return 0;
2332
2333err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002334 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002335 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002336 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002337 i = sbi->s_group_info_size;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002338 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002339 kfree(sbi->s_group_info[i]);
2340 iput(sbi->s_buddy_cache);
2341err_freesgi:
Theodore Ts'of18a5f22011-08-01 08:45:38 -04002342 ext4_kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002343 return -ENOMEM;
2344}
2345
Eric Sandeen2892c152011-02-12 08:12:18 -05002346static void ext4_groupinfo_destroy_slabs(void)
2347{
2348 int i;
2349
2350 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2351 if (ext4_groupinfo_caches[i])
2352 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2353 ext4_groupinfo_caches[i] = NULL;
2354 }
2355}
2356
2357static int ext4_groupinfo_create_slab(size_t size)
2358{
2359 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2360 int slab_size;
2361 int blocksize_bits = order_base_2(size);
2362 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2363 struct kmem_cache *cachep;
2364
2365 if (cache_index >= NR_GRPINFO_CACHES)
2366 return -EINVAL;
2367
2368 if (unlikely(cache_index < 0))
2369 cache_index = 0;
2370
2371 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2372 if (ext4_groupinfo_caches[cache_index]) {
2373 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2374 return 0; /* Already created */
2375 }
2376
2377 slab_size = offsetof(struct ext4_group_info,
2378 bb_counters[blocksize_bits + 2]);
2379
2380 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2381 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2382 NULL);
2383
Tao Ma823ba012011-07-11 18:26:01 -04002384 ext4_groupinfo_caches[cache_index] = cachep;
2385
Eric Sandeen2892c152011-02-12 08:12:18 -05002386 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2387 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002388 printk(KERN_EMERG
2389 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002390 return -ENOMEM;
2391 }
2392
Eric Sandeen2892c152011-02-12 08:12:18 -05002393 return 0;
2394}
2395
Akira Fujita9d990122012-05-28 14:19:25 -04002396int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002397{
2398 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002399 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002400 unsigned offset;
2401 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002402 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002403
Eric Sandeen19278052009-08-25 22:36:25 -04002404 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002405
2406 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2407 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002408 ret = -ENOMEM;
2409 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002410 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002411
Eric Sandeen19278052009-08-25 22:36:25 -04002412 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002413 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2414 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002415 ret = -ENOMEM;
2416 goto out;
2417 }
2418
Eric Sandeen2892c152011-02-12 08:12:18 -05002419 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2420 if (ret < 0)
2421 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002422
2423 /* order 0 is regular bitmap */
2424 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2425 sbi->s_mb_offsets[0] = 0;
2426
2427 i = 1;
2428 offset = 0;
2429 max = sb->s_blocksize << 2;
2430 do {
2431 sbi->s_mb_offsets[i] = offset;
2432 sbi->s_mb_maxs[i] = max;
2433 offset += 1 << (sb->s_blocksize_bits - i);
2434 max = max >> 1;
2435 i++;
2436 } while (i <= sb->s_blocksize_bits + 1);
2437
Alex Tomasc9de5602008-01-29 00:19:52 -05002438 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002439 spin_lock_init(&sbi->s_bal_lock);
2440
2441 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2442 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2443 sbi->s_mb_stats = MB_DEFAULT_STATS;
2444 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2445 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002446 /*
2447 * The default group preallocation is 512, which for 4k block
2448 * sizes translates to 2 megabytes. However for bigalloc file
2449 * systems, this is probably too big (i.e, if the cluster size
2450 * is 1 megabyte, then group preallocation size becomes half a
2451 * gigabyte!). As a default, we will keep a two megabyte
2452 * group pralloc size for cluster sizes up to 64k, and after
2453 * that, we will force a minimum group preallocation size of
2454 * 32 clusters. This translates to 8 megs when the cluster
2455 * size is 256k, and 32 megs when the cluster size is 1 meg,
2456 * which seems reasonable as a default.
2457 */
2458 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2459 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002460 /*
2461 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2462 * to the lowest multiple of s_stripe which is bigger than
2463 * the s_mb_group_prealloc as determined above. We want
2464 * the preallocation size to be an exact multiple of the
2465 * RAID stripe size so that preallocations don't fragment
2466 * the stripes.
2467 */
2468 if (sbi->s_stripe > 1) {
2469 sbi->s_mb_group_prealloc = roundup(
2470 sbi->s_mb_group_prealloc, sbi->s_stripe);
2471 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002472
Eric Sandeen730c2132008-09-13 15:23:29 -04002473 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002474 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002475 ret = -ENOMEM;
Tao Ma7aa0bae2011-10-06 10:22:28 -04002476 goto out_free_groupinfo_slab;
Alex Tomasc9de5602008-01-29 00:19:52 -05002477 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002478 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002479 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002480 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002481 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002482 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2483 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002484 spin_lock_init(&lg->lg_prealloc_lock);
2485 }
2486
Yu Jian79a77c52011-08-01 17:41:46 -04002487 /* init file for buddy data */
2488 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002489 if (ret != 0)
2490 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002491
Theodore Ts'o296c3552009-09-30 00:32:42 -04002492 if (sbi->s_proc)
2493 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2494 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002495
Tao Ma7aa0bae2011-10-06 10:22:28 -04002496 return 0;
2497
2498out_free_locality_groups:
2499 free_percpu(sbi->s_locality_groups);
2500 sbi->s_locality_groups = NULL;
2501out_free_groupinfo_slab:
2502 ext4_groupinfo_destroy_slabs();
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002503out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002504 kfree(sbi->s_mb_offsets);
2505 sbi->s_mb_offsets = NULL;
2506 kfree(sbi->s_mb_maxs);
2507 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002508 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002509}
2510
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002511/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002512static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2513{
2514 struct ext4_prealloc_space *pa;
2515 struct list_head *cur, *tmp;
2516 int count = 0;
2517
2518 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2519 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2520 list_del(&pa->pa_group_list);
2521 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002522 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002523 }
2524 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002525 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002526
2527}
2528
2529int ext4_mb_release(struct super_block *sb)
2530{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002531 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002532 ext4_group_t i;
2533 int num_meta_group_infos;
2534 struct ext4_group_info *grinfo;
2535 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002536 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002537
Salman Qazi95599962012-05-31 23:52:14 -04002538 if (sbi->s_proc)
2539 remove_proc_entry("mb_groups", sbi->s_proc);
2540
Alex Tomasc9de5602008-01-29 00:19:52 -05002541 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002542 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002543 grinfo = ext4_get_group_info(sb, i);
2544#ifdef DOUBLE_CHECK
2545 kfree(grinfo->bb_bitmap);
2546#endif
2547 ext4_lock_group(sb, i);
2548 ext4_mb_cleanup_pa(grinfo);
2549 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002550 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002551 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002552 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002553 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2554 EXT4_DESC_PER_BLOCK_BITS(sb);
2555 for (i = 0; i < num_meta_group_infos; i++)
2556 kfree(sbi->s_group_info[i]);
Theodore Ts'of18a5f22011-08-01 08:45:38 -04002557 ext4_kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002558 }
2559 kfree(sbi->s_mb_offsets);
2560 kfree(sbi->s_mb_maxs);
2561 if (sbi->s_buddy_cache)
2562 iput(sbi->s_buddy_cache);
2563 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002564 ext4_msg(sb, KERN_INFO,
2565 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002566 atomic_read(&sbi->s_bal_allocated),
2567 atomic_read(&sbi->s_bal_reqs),
2568 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002569 ext4_msg(sb, KERN_INFO,
2570 "mballoc: %u extents scanned, %u goal hits, "
2571 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002572 atomic_read(&sbi->s_bal_ex_scanned),
2573 atomic_read(&sbi->s_bal_goals),
2574 atomic_read(&sbi->s_bal_2orders),
2575 atomic_read(&sbi->s_bal_breaks),
2576 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002577 ext4_msg(sb, KERN_INFO,
2578 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002579 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002580 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002581 ext4_msg(sb, KERN_INFO,
2582 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002583 atomic_read(&sbi->s_mb_preallocated),
2584 atomic_read(&sbi->s_mb_discarded));
2585 }
2586
Eric Sandeen730c2132008-09-13 15:23:29 -04002587 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002588
2589 return 0;
2590}
2591
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002592static inline int ext4_issue_discard(struct super_block *sb,
Theodore Ts'o84130192011-09-09 18:50:51 -04002593 ext4_group_t block_group, ext4_grpblk_t cluster, int count)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002594{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002595 ext4_fsblk_t discard_block;
2596
Theodore Ts'o84130192011-09-09 18:50:51 -04002597 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2598 ext4_group_first_block_no(sb, block_group));
2599 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002600 trace_ext4_discard_blocks(sb,
2601 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002602 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002603}
2604
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002605/*
2606 * This function is called by the jbd2 layer once the commit has finished,
2607 * so we know we can free the blocks that were released with that commit.
2608 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002609static void ext4_free_data_callback(struct super_block *sb,
2610 struct ext4_journal_cb_entry *jce,
2611 int rc)
Alex Tomasc9de5602008-01-29 00:19:52 -05002612{
Bobi Jam18aadd42012-02-20 17:53:02 -05002613 struct ext4_free_data *entry = (struct ext4_free_data *)jce;
Alex Tomasc9de5602008-01-29 00:19:52 -05002614 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002615 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002616 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002617
Bobi Jam18aadd42012-02-20 17:53:02 -05002618 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2619 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002620
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002621 if (test_opt(sb, DISCARD)) {
2622 err = ext4_issue_discard(sb, entry->efd_group,
2623 entry->efd_start_cluster,
2624 entry->efd_count);
2625 if (err && err != -EOPNOTSUPP)
2626 ext4_msg(sb, KERN_WARNING, "discard request in"
2627 " group:%d block:%d count:%d failed"
2628 " with %d", entry->efd_group,
2629 entry->efd_start_cluster,
2630 entry->efd_count, err);
2631 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002632
Bobi Jam18aadd42012-02-20 17:53:02 -05002633 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2634 /* we expect to find existing buddy because it's pinned */
2635 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002636
Alex Tomasc9de5602008-01-29 00:19:52 -05002637
Bobi Jam18aadd42012-02-20 17:53:02 -05002638 db = e4b.bd_info;
2639 /* there are blocks to put in buddy to make them really free */
2640 count += entry->efd_count;
2641 count2++;
2642 ext4_lock_group(sb, entry->efd_group);
2643 /* Take it out of per group rb tree */
2644 rb_erase(&entry->efd_node, &(db->bb_free_root));
2645 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002646
Bobi Jam18aadd42012-02-20 17:53:02 -05002647 /*
2648 * Clear the trimmed flag for the group so that the next
2649 * ext4_trim_fs can trim it.
2650 * If the volume is mounted with -o discard, online discard
2651 * is supported and the free blocks will be trimmed online.
2652 */
2653 if (!test_opt(sb, DISCARD))
2654 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2655
2656 if (!db->bb_free_root.rb_node) {
2657 /* No more items in the per group rb tree
2658 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002659 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002660 page_cache_release(e4b.bd_buddy_page);
2661 page_cache_release(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002662 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002663 ext4_unlock_group(sb, entry->efd_group);
2664 kmem_cache_free(ext4_free_data_cachep, entry);
2665 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002666
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002667 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002668}
2669
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002670int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002671{
Theodore Ts'o16828082010-10-27 21:30:09 -04002672 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2673 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002674 if (ext4_pspace_cachep == NULL)
2675 return -ENOMEM;
2676
Theodore Ts'o16828082010-10-27 21:30:09 -04002677 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2678 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002679 if (ext4_ac_cachep == NULL) {
2680 kmem_cache_destroy(ext4_pspace_cachep);
2681 return -ENOMEM;
2682 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002683
Bobi Jam18aadd42012-02-20 17:53:02 -05002684 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2685 SLAB_RECLAIM_ACCOUNT);
2686 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002687 kmem_cache_destroy(ext4_pspace_cachep);
2688 kmem_cache_destroy(ext4_ac_cachep);
2689 return -ENOMEM;
2690 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002691 return 0;
2692}
2693
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002694void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002695{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002696 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002697 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2698 * before destroying the slab cache.
2699 */
2700 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002701 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002702 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002703 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002704 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002705}
2706
2707
2708/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002709 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002710 * Returns 0 if success or error code
2711 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002712static noinline_for_stack int
2713ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002714 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002715{
2716 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002717 struct ext4_group_desc *gdp;
2718 struct buffer_head *gdp_bh;
2719 struct ext4_sb_info *sbi;
2720 struct super_block *sb;
2721 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002722 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002723
2724 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2725 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2726
2727 sb = ac->ac_sb;
2728 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002729
2730 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002731 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002732 if (!bitmap_bh)
2733 goto out_err;
2734
2735 err = ext4_journal_get_write_access(handle, bitmap_bh);
2736 if (err)
2737 goto out_err;
2738
2739 err = -EIO;
2740 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2741 if (!gdp)
2742 goto out_err;
2743
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002744 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002745 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002746
Alex Tomasc9de5602008-01-29 00:19:52 -05002747 err = ext4_journal_get_write_access(handle, gdp_bh);
2748 if (err)
2749 goto out_err;
2750
Akinobu Mitabda00de2010-03-03 23:53:25 -05002751 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002752
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002753 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002754 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002755 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002756 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002757 /* File system mounted not to panic on error
2758 * Fix the bitmap and repeat the block allocation
2759 * We leak some of the blocks here.
2760 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002761 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002762 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2763 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002764 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002765 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002766 if (!err)
2767 err = -EAGAIN;
2768 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002769 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002770
2771 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002772#ifdef AGGRESSIVE_CHECK
2773 {
2774 int i;
2775 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2776 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2777 bitmap_bh->b_data));
2778 }
2779 }
2780#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002781 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2782 ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002783 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2784 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002785 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002786 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002787 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002788 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002789 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
2790 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04002791 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002792 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002793
2794 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04002795 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002796 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002797 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002798 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002799 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2800 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04002801 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
2802 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002803
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002804 if (sbi->s_log_groups_per_flex) {
2805 ext4_group_t flex_group = ext4_flex_group(sbi,
2806 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002807 atomic_sub(ac->ac_b_ex.fe_len,
Theodore Ts'o24aaa8e2011-09-09 18:58:51 -04002808 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002809 }
2810
Frank Mayhar03901312009-01-07 00:06:22 -05002811 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002812 if (err)
2813 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002814 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002815
2816out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002817 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002818 return err;
2819}
2820
2821/*
2822 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002823 * Group request are normalized to s_mb_group_prealloc, which goes to
2824 * s_strip if we set the same via mount option.
2825 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002826 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002827 *
2828 * XXX: should we try to preallocate more than the group has now?
2829 */
2830static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2831{
2832 struct super_block *sb = ac->ac_sb;
2833 struct ext4_locality_group *lg = ac->ac_lg;
2834
2835 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002836 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002837 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002838 current->pid, ac->ac_g_ex.fe_len);
2839}
2840
2841/*
2842 * Normalization means making request better in terms of
2843 * size and alignment
2844 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002845static noinline_for_stack void
2846ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002847 struct ext4_allocation_request *ar)
2848{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002849 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002850 int bsbits, max;
2851 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05002852 loff_t size, start_off;
2853 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002854 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002855 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002856 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002857
2858 /* do normalize only data requests, metadata requests
2859 do not need preallocation */
2860 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2861 return;
2862
2863 /* sometime caller may want exact blocks */
2864 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2865 return;
2866
2867 /* caller may indicate that preallocation isn't
2868 * required (it's a tail, for example) */
2869 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2870 return;
2871
2872 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2873 ext4_mb_normalize_group_request(ac);
2874 return ;
2875 }
2876
2877 bsbits = ac->ac_sb->s_blocksize_bits;
2878
2879 /* first, let's learn actual file size
2880 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002881 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002882 size = size << bsbits;
2883 if (size < i_size_read(ac->ac_inode))
2884 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002885 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002886
Valerie Clement19304792008-05-13 19:31:14 -04002887 /* max size of free chunks */
2888 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002889
Valerie Clement19304792008-05-13 19:31:14 -04002890#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2891 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002892
2893 /* first, try to predict filesize */
2894 /* XXX: should this table be tunable? */
2895 start_off = 0;
2896 if (size <= 16 * 1024) {
2897 size = 16 * 1024;
2898 } else if (size <= 32 * 1024) {
2899 size = 32 * 1024;
2900 } else if (size <= 64 * 1024) {
2901 size = 64 * 1024;
2902 } else if (size <= 128 * 1024) {
2903 size = 128 * 1024;
2904 } else if (size <= 256 * 1024) {
2905 size = 256 * 1024;
2906 } else if (size <= 512 * 1024) {
2907 size = 512 * 1024;
2908 } else if (size <= 1024 * 1024) {
2909 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002910 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002911 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002912 (21 - bsbits)) << 21;
2913 size = 2 * 1024 * 1024;
2914 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002915 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2916 (22 - bsbits)) << 22;
2917 size = 4 * 1024 * 1024;
2918 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002919 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002920 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2921 (23 - bsbits)) << 23;
2922 size = 8 * 1024 * 1024;
2923 } else {
2924 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2925 size = ac->ac_o_ex.fe_len << bsbits;
2926 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002927 size = size >> bsbits;
2928 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002929
2930 /* don't cover already allocated blocks in selected range */
2931 if (ar->pleft && start <= ar->lleft) {
2932 size -= ar->lleft + 1 - start;
2933 start = ar->lleft + 1;
2934 }
2935 if (ar->pright && start + size - 1 >= ar->lright)
2936 size -= start + size - ar->lright;
2937
2938 end = start + size;
2939
2940 /* check we don't cross already preallocated blocks */
2941 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002942 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002943 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002944
Alex Tomasc9de5602008-01-29 00:19:52 -05002945 if (pa->pa_deleted)
2946 continue;
2947 spin_lock(&pa->pa_lock);
2948 if (pa->pa_deleted) {
2949 spin_unlock(&pa->pa_lock);
2950 continue;
2951 }
2952
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002953 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
2954 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002955
2956 /* PA must not overlap original request */
2957 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2958 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2959
Eric Sandeen38877f42009-08-17 23:55:24 -04002960 /* skip PAs this normalized request doesn't overlap with */
2961 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002962 spin_unlock(&pa->pa_lock);
2963 continue;
2964 }
2965 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2966
Eric Sandeen38877f42009-08-17 23:55:24 -04002967 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05002968 if (pa_end <= ac->ac_o_ex.fe_logical) {
2969 BUG_ON(pa_end < start);
2970 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04002971 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002972 BUG_ON(pa->pa_lstart > end);
2973 end = pa->pa_lstart;
2974 }
2975 spin_unlock(&pa->pa_lock);
2976 }
2977 rcu_read_unlock();
2978 size = end - start;
2979
2980 /* XXX: extra loop to check we really don't overlap preallocations */
2981 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002982 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002983 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002984
Alex Tomasc9de5602008-01-29 00:19:52 -05002985 spin_lock(&pa->pa_lock);
2986 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002987 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
2988 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002989 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
2990 }
2991 spin_unlock(&pa->pa_lock);
2992 }
2993 rcu_read_unlock();
2994
2995 if (start + size <= ac->ac_o_ex.fe_logical &&
2996 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002997 ext4_msg(ac->ac_sb, KERN_ERR,
2998 "start %lu, size %lu, fe_logical %lu",
2999 (unsigned long) start, (unsigned long) size,
3000 (unsigned long) ac->ac_o_ex.fe_logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05003001 }
3002 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3003 start > ac->ac_o_ex.fe_logical);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003004 BUG_ON(size <= 0 || size > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003005
3006 /* now prepare goal request */
3007
3008 /* XXX: is it better to align blocks WRT to logical
3009 * placement or satisfy big request as is */
3010 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003011 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003012
3013 /* define goal start in order to merge */
3014 if (ar->pright && (ar->lright == (start + size))) {
3015 /* merge to the right */
3016 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3017 &ac->ac_f_ex.fe_group,
3018 &ac->ac_f_ex.fe_start);
3019 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3020 }
3021 if (ar->pleft && (ar->lleft + 1 == start)) {
3022 /* merge to the left */
3023 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3024 &ac->ac_f_ex.fe_group,
3025 &ac->ac_f_ex.fe_start);
3026 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3027 }
3028
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003029 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003030 (unsigned) orig_size, (unsigned) start);
3031}
3032
3033static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3034{
3035 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3036
3037 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3038 atomic_inc(&sbi->s_bal_reqs);
3039 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003040 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003041 atomic_inc(&sbi->s_bal_success);
3042 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3043 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3044 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3045 atomic_inc(&sbi->s_bal_goals);
3046 if (ac->ac_found > sbi->s_mb_max_to_scan)
3047 atomic_inc(&sbi->s_bal_breaks);
3048 }
3049
Theodore Ts'o296c3552009-09-30 00:32:42 -04003050 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3051 trace_ext4_mballoc_alloc(ac);
3052 else
3053 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003054}
3055
3056/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003057 * Called on failure; free up any blocks from the inode PA for this
3058 * context. We don't need this for MB_GROUP_PA because we only change
3059 * pa_free in ext4_mb_release_context(), but on failure, we've already
3060 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3061 */
3062static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3063{
3064 struct ext4_prealloc_space *pa = ac->ac_pa;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003065
Zheng Liu400db9d2012-05-28 17:53:53 -04003066 if (pa && pa->pa_type == MB_INODE_PA)
3067 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003068}
3069
3070/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003071 * use blocks preallocated to inode
3072 */
3073static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3074 struct ext4_prealloc_space *pa)
3075{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003076 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003077 ext4_fsblk_t start;
3078 ext4_fsblk_t end;
3079 int len;
3080
3081 /* found preallocated blocks, use them */
3082 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003083 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3084 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3085 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003086 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3087 &ac->ac_b_ex.fe_start);
3088 ac->ac_b_ex.fe_len = len;
3089 ac->ac_status = AC_STATUS_FOUND;
3090 ac->ac_pa = pa;
3091
3092 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003093 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003094 BUG_ON(pa->pa_free < len);
3095 pa->pa_free -= len;
3096
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003097 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003098}
3099
3100/*
3101 * use blocks preallocated to locality group
3102 */
3103static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3104 struct ext4_prealloc_space *pa)
3105{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003106 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003107
Alex Tomasc9de5602008-01-29 00:19:52 -05003108 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3109 &ac->ac_b_ex.fe_group,
3110 &ac->ac_b_ex.fe_start);
3111 ac->ac_b_ex.fe_len = len;
3112 ac->ac_status = AC_STATUS_FOUND;
3113 ac->ac_pa = pa;
3114
3115 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003116 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003117 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003118 * in on-disk bitmap -- see ext4_mb_release_context()
3119 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003120 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003121 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003122}
3123
3124/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003125 * Return the prealloc space that have minimal distance
3126 * from the goal block. @cpa is the prealloc
3127 * space that is having currently known minimal distance
3128 * from the goal block.
3129 */
3130static struct ext4_prealloc_space *
3131ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3132 struct ext4_prealloc_space *pa,
3133 struct ext4_prealloc_space *cpa)
3134{
3135 ext4_fsblk_t cur_distance, new_distance;
3136
3137 if (cpa == NULL) {
3138 atomic_inc(&pa->pa_count);
3139 return pa;
3140 }
3141 cur_distance = abs(goal_block - cpa->pa_pstart);
3142 new_distance = abs(goal_block - pa->pa_pstart);
3143
Coly Li5a54b2f2011-02-24 14:10:05 -05003144 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003145 return cpa;
3146
3147 /* drop the previous reference */
3148 atomic_dec(&cpa->pa_count);
3149 atomic_inc(&pa->pa_count);
3150 return pa;
3151}
3152
3153/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003154 * search goal blocks in preallocated space
3155 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003156static noinline_for_stack int
3157ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003158{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003159 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003160 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003161 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3162 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003163 struct ext4_prealloc_space *pa, *cpa = NULL;
3164 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003165
3166 /* only data can be preallocated */
3167 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3168 return 0;
3169
3170 /* first, try per-file preallocation */
3171 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003172 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003173
3174 /* all fields in this condition don't change,
3175 * so we can skip locking for them */
3176 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003177 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3178 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003179 continue;
3180
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003181 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003182 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003183 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3184 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003185 continue;
3186
Alex Tomasc9de5602008-01-29 00:19:52 -05003187 /* found preallocated blocks, use them */
3188 spin_lock(&pa->pa_lock);
3189 if (pa->pa_deleted == 0 && pa->pa_free) {
3190 atomic_inc(&pa->pa_count);
3191 ext4_mb_use_inode_pa(ac, pa);
3192 spin_unlock(&pa->pa_lock);
3193 ac->ac_criteria = 10;
3194 rcu_read_unlock();
3195 return 1;
3196 }
3197 spin_unlock(&pa->pa_lock);
3198 }
3199 rcu_read_unlock();
3200
3201 /* can we use group allocation? */
3202 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3203 return 0;
3204
3205 /* inode may have no locality group for some reason */
3206 lg = ac->ac_lg;
3207 if (lg == NULL)
3208 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003209 order = fls(ac->ac_o_ex.fe_len) - 1;
3210 if (order > PREALLOC_TB_SIZE - 1)
3211 /* The max size of hash table is PREALLOC_TB_SIZE */
3212 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003213
Akinobu Mitabda00de2010-03-03 23:53:25 -05003214 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003215 /*
3216 * search for the prealloc space that is having
3217 * minimal distance from the goal block.
3218 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003219 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3220 rcu_read_lock();
3221 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3222 pa_inode_list) {
3223 spin_lock(&pa->pa_lock);
3224 if (pa->pa_deleted == 0 &&
3225 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003226
3227 cpa = ext4_mb_check_group_pa(goal_block,
3228 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003229 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003230 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003231 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003232 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003233 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003234 if (cpa) {
3235 ext4_mb_use_group_pa(ac, cpa);
3236 ac->ac_criteria = 20;
3237 return 1;
3238 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003239 return 0;
3240}
3241
3242/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003243 * the function goes through all block freed in the group
3244 * but not yet committed and marks them used in in-core bitmap.
3245 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003246 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003247 */
3248static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3249 ext4_group_t group)
3250{
3251 struct rb_node *n;
3252 struct ext4_group_info *grp;
3253 struct ext4_free_data *entry;
3254
3255 grp = ext4_get_group_info(sb, group);
3256 n = rb_first(&(grp->bb_free_root));
3257
3258 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003259 entry = rb_entry(n, struct ext4_free_data, efd_node);
3260 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003261 n = rb_next(n);
3262 }
3263 return;
3264}
3265
3266/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003267 * the function goes through all preallocation in this group and marks them
3268 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003269 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003270 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003271static noinline_for_stack
3272void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003273 ext4_group_t group)
3274{
3275 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3276 struct ext4_prealloc_space *pa;
3277 struct list_head *cur;
3278 ext4_group_t groupnr;
3279 ext4_grpblk_t start;
3280 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003281 int len;
3282
3283 /* all form of preallocation discards first load group,
3284 * so the only competing code is preallocation use.
3285 * we don't need any locking here
3286 * notice we do NOT ignore preallocations with pa_deleted
3287 * otherwise we could leave used blocks available for
3288 * allocation in buddy when concurrent ext4_mb_put_pa()
3289 * is dropping preallocation
3290 */
3291 list_for_each(cur, &grp->bb_prealloc_list) {
3292 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3293 spin_lock(&pa->pa_lock);
3294 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3295 &groupnr, &start);
3296 len = pa->pa_len;
3297 spin_unlock(&pa->pa_lock);
3298 if (unlikely(len == 0))
3299 continue;
3300 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003301 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003302 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003303 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003304 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003305}
3306
3307static void ext4_mb_pa_callback(struct rcu_head *head)
3308{
3309 struct ext4_prealloc_space *pa;
3310 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3311 kmem_cache_free(ext4_pspace_cachep, pa);
3312}
3313
3314/*
3315 * drops a reference to preallocated space descriptor
3316 * if this was the last reference and the space is consumed
3317 */
3318static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3319 struct super_block *sb, struct ext4_prealloc_space *pa)
3320{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003321 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003322 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003323
3324 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3325 return;
3326
3327 /* in this short window concurrent discard can set pa_deleted */
3328 spin_lock(&pa->pa_lock);
3329 if (pa->pa_deleted == 1) {
3330 spin_unlock(&pa->pa_lock);
3331 return;
3332 }
3333
3334 pa->pa_deleted = 1;
3335 spin_unlock(&pa->pa_lock);
3336
Eric Sandeend33a1972009-03-16 23:25:40 -04003337 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003338 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003339 * If doing group-based preallocation, pa_pstart may be in the
3340 * next group when pa is used up
3341 */
3342 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003343 grp_blk--;
3344
3345 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003346
3347 /*
3348 * possible race:
3349 *
3350 * P1 (buddy init) P2 (regular allocation)
3351 * find block B in PA
3352 * copy on-disk bitmap to buddy
3353 * mark B in on-disk bitmap
3354 * drop PA from group
3355 * mark all PAs in buddy
3356 *
3357 * thus, P1 initializes buddy with B available. to prevent this
3358 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3359 * against that pair
3360 */
3361 ext4_lock_group(sb, grp);
3362 list_del(&pa->pa_group_list);
3363 ext4_unlock_group(sb, grp);
3364
3365 spin_lock(pa->pa_obj_lock);
3366 list_del_rcu(&pa->pa_inode_list);
3367 spin_unlock(pa->pa_obj_lock);
3368
3369 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3370}
3371
3372/*
3373 * creates new preallocated space for given inode
3374 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003375static noinline_for_stack int
3376ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003377{
3378 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003379 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003380 struct ext4_prealloc_space *pa;
3381 struct ext4_group_info *grp;
3382 struct ext4_inode_info *ei;
3383
3384 /* preallocate only when found space is larger then requested */
3385 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3386 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3387 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3388
3389 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3390 if (pa == NULL)
3391 return -ENOMEM;
3392
3393 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3394 int winl;
3395 int wins;
3396 int win;
3397 int offs;
3398
3399 /* we can't allocate as much as normalizer wants.
3400 * so, found space must get proper lstart
3401 * to cover original request */
3402 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3403 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3404
3405 /* we're limited by original request in that
3406 * logical block must be covered any way
3407 * winl is window we can move our chunk within */
3408 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3409
3410 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003411 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003412
3413 /* the smallest one defines real window */
3414 win = min(winl, wins);
3415
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003416 offs = ac->ac_o_ex.fe_logical %
3417 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003418 if (offs && offs < win)
3419 win = offs;
3420
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003421 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003422 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003423 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3424 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3425 }
3426
3427 /* preallocation can change ac_b_ex, thus we store actually
3428 * allocated blocks for history */
3429 ac->ac_f_ex = ac->ac_b_ex;
3430
3431 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3432 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3433 pa->pa_len = ac->ac_b_ex.fe_len;
3434 pa->pa_free = pa->pa_len;
3435 atomic_set(&pa->pa_count, 1);
3436 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003437 INIT_LIST_HEAD(&pa->pa_inode_list);
3438 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003439 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003440 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003441
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003442 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003443 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003444 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003445
3446 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003447 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003448
3449 ei = EXT4_I(ac->ac_inode);
3450 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3451
3452 pa->pa_obj_lock = &ei->i_prealloc_lock;
3453 pa->pa_inode = ac->ac_inode;
3454
3455 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3456 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3457 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3458
3459 spin_lock(pa->pa_obj_lock);
3460 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3461 spin_unlock(pa->pa_obj_lock);
3462
3463 return 0;
3464}
3465
3466/*
3467 * creates new preallocated space for locality group inodes belongs to
3468 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003469static noinline_for_stack int
3470ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003471{
3472 struct super_block *sb = ac->ac_sb;
3473 struct ext4_locality_group *lg;
3474 struct ext4_prealloc_space *pa;
3475 struct ext4_group_info *grp;
3476
3477 /* preallocate only when found space is larger then requested */
3478 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3479 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3480 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3481
3482 BUG_ON(ext4_pspace_cachep == NULL);
3483 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3484 if (pa == NULL)
3485 return -ENOMEM;
3486
3487 /* preallocation can change ac_b_ex, thus we store actually
3488 * allocated blocks for history */
3489 ac->ac_f_ex = ac->ac_b_ex;
3490
3491 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3492 pa->pa_lstart = pa->pa_pstart;
3493 pa->pa_len = ac->ac_b_ex.fe_len;
3494 pa->pa_free = pa->pa_len;
3495 atomic_set(&pa->pa_count, 1);
3496 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003497 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003498 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003499 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003500 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003501
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003502 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003503 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3504 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003505
3506 ext4_mb_use_group_pa(ac, pa);
3507 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3508
3509 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3510 lg = ac->ac_lg;
3511 BUG_ON(lg == NULL);
3512
3513 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3514 pa->pa_inode = NULL;
3515
3516 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3517 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3518 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3519
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003520 /*
3521 * We will later add the new pa to the right bucket
3522 * after updating the pa_free in ext4_mb_release_context
3523 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003524 return 0;
3525}
3526
3527static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3528{
3529 int err;
3530
3531 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3532 err = ext4_mb_new_group_pa(ac);
3533 else
3534 err = ext4_mb_new_inode_pa(ac);
3535 return err;
3536}
3537
3538/*
3539 * finds all unused blocks in on-disk bitmap, frees them in
3540 * in-core bitmap and buddy.
3541 * @pa must be unlinked from inode and group lists, so that
3542 * nobody else can find/use it.
3543 * the caller MUST hold group/inode locks.
3544 * TODO: optimize the case when there are no in-core structures yet
3545 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003546static noinline_for_stack int
3547ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003548 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003549{
Alex Tomasc9de5602008-01-29 00:19:52 -05003550 struct super_block *sb = e4b->bd_sb;
3551 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003552 unsigned int end;
3553 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003554 ext4_group_t group;
3555 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003556 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003557 int err = 0;
3558 int free = 0;
3559
3560 BUG_ON(pa->pa_deleted == 0);
3561 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003562 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003563 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3564 end = bit + pa->pa_len;
3565
Alex Tomasc9de5602008-01-29 00:19:52 -05003566 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003567 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003568 if (bit >= end)
3569 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003570 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003571 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003572 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3573 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003574 free += next - bit;
3575
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003576 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003577 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3578 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003579 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003580 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3581 bit = next + 1;
3582 }
3583 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003584 ext4_msg(e4b->bd_sb, KERN_CRIT,
3585 "pa %p: logic %lu, phys. %lu, len %lu",
3586 pa, (unsigned long) pa->pa_lstart,
3587 (unsigned long) pa->pa_pstart,
3588 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003589 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003590 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003591 /*
3592 * pa is already deleted so we use the value obtained
3593 * from the bitmap and continue.
3594 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003595 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003596 atomic_add(free, &sbi->s_mb_discarded);
3597
3598 return err;
3599}
3600
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003601static noinline_for_stack int
3602ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003603 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003604{
Alex Tomasc9de5602008-01-29 00:19:52 -05003605 struct super_block *sb = e4b->bd_sb;
3606 ext4_group_t group;
3607 ext4_grpblk_t bit;
3608
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003609 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003610 BUG_ON(pa->pa_deleted == 0);
3611 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3612 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3613 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3614 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003615 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003616
3617 return 0;
3618}
3619
3620/*
3621 * releases all preallocations in given group
3622 *
3623 * first, we need to decide discard policy:
3624 * - when do we discard
3625 * 1) ENOSPC
3626 * - how many do we discard
3627 * 1) how many requested
3628 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003629static noinline_for_stack int
3630ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003631 ext4_group_t group, int needed)
3632{
3633 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3634 struct buffer_head *bitmap_bh = NULL;
3635 struct ext4_prealloc_space *pa, *tmp;
3636 struct list_head list;
3637 struct ext4_buddy e4b;
3638 int err;
3639 int busy = 0;
3640 int free = 0;
3641
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003642 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003643
3644 if (list_empty(&grp->bb_prealloc_list))
3645 return 0;
3646
Theodore Ts'o574ca172008-07-11 19:27:31 -04003647 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003648 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003649 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003650 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003651 }
3652
3653 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003654 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003655 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003656 put_bh(bitmap_bh);
3657 return 0;
3658 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003659
3660 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003661 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003662
Alex Tomasc9de5602008-01-29 00:19:52 -05003663 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003664repeat:
3665 ext4_lock_group(sb, group);
3666 list_for_each_entry_safe(pa, tmp,
3667 &grp->bb_prealloc_list, pa_group_list) {
3668 spin_lock(&pa->pa_lock);
3669 if (atomic_read(&pa->pa_count)) {
3670 spin_unlock(&pa->pa_lock);
3671 busy = 1;
3672 continue;
3673 }
3674 if (pa->pa_deleted) {
3675 spin_unlock(&pa->pa_lock);
3676 continue;
3677 }
3678
3679 /* seems this one can be freed ... */
3680 pa->pa_deleted = 1;
3681
3682 /* we can trust pa_free ... */
3683 free += pa->pa_free;
3684
3685 spin_unlock(&pa->pa_lock);
3686
3687 list_del(&pa->pa_group_list);
3688 list_add(&pa->u.pa_tmp_list, &list);
3689 }
3690
3691 /* if we still need more blocks and some PAs were used, try again */
3692 if (free < needed && busy) {
3693 busy = 0;
3694 ext4_unlock_group(sb, group);
3695 /*
3696 * Yield the CPU here so that we don't get soft lockup
3697 * in non preempt case.
3698 */
3699 yield();
3700 goto repeat;
3701 }
3702
3703 /* found anything to free? */
3704 if (list_empty(&list)) {
3705 BUG_ON(free != 0);
3706 goto out;
3707 }
3708
3709 /* now free all selected PAs */
3710 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3711
3712 /* remove from object (inode or locality group) */
3713 spin_lock(pa->pa_obj_lock);
3714 list_del_rcu(&pa->pa_inode_list);
3715 spin_unlock(pa->pa_obj_lock);
3716
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003717 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003718 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003719 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003720 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003721
3722 list_del(&pa->u.pa_tmp_list);
3723 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3724 }
3725
3726out:
3727 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003728 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003729 put_bh(bitmap_bh);
3730 return free;
3731}
3732
3733/*
3734 * releases all non-used preallocated blocks for given inode
3735 *
3736 * It's important to discard preallocations under i_data_sem
3737 * We don't want another block to be served from the prealloc
3738 * space when we are discarding the inode prealloc space.
3739 *
3740 * FIXME!! Make sure it is valid at all the call sites
3741 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003742void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003743{
3744 struct ext4_inode_info *ei = EXT4_I(inode);
3745 struct super_block *sb = inode->i_sb;
3746 struct buffer_head *bitmap_bh = NULL;
3747 struct ext4_prealloc_space *pa, *tmp;
3748 ext4_group_t group = 0;
3749 struct list_head list;
3750 struct ext4_buddy e4b;
3751 int err;
3752
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003753 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003754 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3755 return;
3756 }
3757
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003758 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003759 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003760
3761 INIT_LIST_HEAD(&list);
3762
3763repeat:
3764 /* first, collect all pa's in the inode */
3765 spin_lock(&ei->i_prealloc_lock);
3766 while (!list_empty(&ei->i_prealloc_list)) {
3767 pa = list_entry(ei->i_prealloc_list.next,
3768 struct ext4_prealloc_space, pa_inode_list);
3769 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3770 spin_lock(&pa->pa_lock);
3771 if (atomic_read(&pa->pa_count)) {
3772 /* this shouldn't happen often - nobody should
3773 * use preallocation while we're discarding it */
3774 spin_unlock(&pa->pa_lock);
3775 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003776 ext4_msg(sb, KERN_ERR,
3777 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05003778 WARN_ON(1);
3779 schedule_timeout_uninterruptible(HZ);
3780 goto repeat;
3781
3782 }
3783 if (pa->pa_deleted == 0) {
3784 pa->pa_deleted = 1;
3785 spin_unlock(&pa->pa_lock);
3786 list_del_rcu(&pa->pa_inode_list);
3787 list_add(&pa->u.pa_tmp_list, &list);
3788 continue;
3789 }
3790
3791 /* someone is deleting pa right now */
3792 spin_unlock(&pa->pa_lock);
3793 spin_unlock(&ei->i_prealloc_lock);
3794
3795 /* we have to wait here because pa_deleted
3796 * doesn't mean pa is already unlinked from
3797 * the list. as we might be called from
3798 * ->clear_inode() the inode will get freed
3799 * and concurrent thread which is unlinking
3800 * pa from inode's list may access already
3801 * freed memory, bad-bad-bad */
3802
3803 /* XXX: if this happens too often, we can
3804 * add a flag to force wait only in case
3805 * of ->clear_inode(), but not in case of
3806 * regular truncate */
3807 schedule_timeout_uninterruptible(HZ);
3808 goto repeat;
3809 }
3810 spin_unlock(&ei->i_prealloc_lock);
3811
3812 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003813 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003814 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3815
3816 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003817 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003818 ext4_error(sb, "Error loading buddy information for %u",
3819 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003820 continue;
3821 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003822
Theodore Ts'o574ca172008-07-11 19:27:31 -04003823 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003824 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003825 ext4_error(sb, "Error reading block bitmap for %u",
3826 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003827 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003828 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003829 }
3830
3831 ext4_lock_group(sb, group);
3832 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003833 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003834 ext4_unlock_group(sb, group);
3835
Jing Zhange39e07f2010-05-14 00:00:00 -04003836 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003837 put_bh(bitmap_bh);
3838
3839 list_del(&pa->u.pa_tmp_list);
3840 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3841 }
3842}
3843
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003844#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003845static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3846{
3847 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003848 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003849
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05003850 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05003851 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04003852 return;
3853
Joe Perches7f6a11e2012-03-19 23:09:43 -04003854 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003855 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04003856 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05003857 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04003858 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003859 "goal %lu/%lu/%lu@%lu, "
3860 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05003861 (unsigned long)ac->ac_o_ex.fe_group,
3862 (unsigned long)ac->ac_o_ex.fe_start,
3863 (unsigned long)ac->ac_o_ex.fe_len,
3864 (unsigned long)ac->ac_o_ex.fe_logical,
3865 (unsigned long)ac->ac_g_ex.fe_group,
3866 (unsigned long)ac->ac_g_ex.fe_start,
3867 (unsigned long)ac->ac_g_ex.fe_len,
3868 (unsigned long)ac->ac_g_ex.fe_logical,
3869 (unsigned long)ac->ac_b_ex.fe_group,
3870 (unsigned long)ac->ac_b_ex.fe_start,
3871 (unsigned long)ac->ac_b_ex.fe_len,
3872 (unsigned long)ac->ac_b_ex.fe_logical,
3873 (int)ac->ac_criteria);
Joe Perches7f6a11e2012-03-19 23:09:43 -04003874 ext4_msg(ac->ac_sb, KERN_ERR, "%lu scanned, %d found",
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003875 ac->ac_ex_scanned, ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04003876 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003877 ngroups = ext4_get_groups_count(sb);
3878 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003879 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3880 struct ext4_prealloc_space *pa;
3881 ext4_grpblk_t start;
3882 struct list_head *cur;
3883 ext4_lock_group(sb, i);
3884 list_for_each(cur, &grp->bb_prealloc_list) {
3885 pa = list_entry(cur, struct ext4_prealloc_space,
3886 pa_group_list);
3887 spin_lock(&pa->pa_lock);
3888 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3889 NULL, &start);
3890 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003891 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3892 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003893 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003894 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003895
3896 if (grp->bb_free == 0)
3897 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003898 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003899 i, grp->bb_free, grp->bb_fragments);
3900 }
3901 printk(KERN_ERR "\n");
3902}
3903#else
3904static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3905{
3906 return;
3907}
3908#endif
3909
3910/*
3911 * We use locality group preallocation for small size file. The size of the
3912 * file is determined by the current size or the resulting size after
3913 * allocation which ever is larger
3914 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003915 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003916 */
3917static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3918{
3919 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3920 int bsbits = ac->ac_sb->s_blocksize_bits;
3921 loff_t size, isize;
3922
3923 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3924 return;
3925
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003926 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3927 return;
3928
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003929 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04003930 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3931 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003932
Theodore Ts'o50797482009-09-18 13:34:02 -04003933 if ((size == isize) &&
3934 !ext4_fs_is_busy(sbi) &&
3935 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3936 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3937 return;
3938 }
3939
Robin Dongebbe0272011-10-26 05:14:27 -04003940 if (sbi->s_mb_group_prealloc <= 0) {
3941 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
3942 return;
3943 }
3944
Alex Tomasc9de5602008-01-29 00:19:52 -05003945 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003946 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003947 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003948 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003949 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003950 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003951
3952 BUG_ON(ac->ac_lg != NULL);
3953 /*
3954 * locality group prealloc space are per cpu. The reason for having
3955 * per cpu locality group is to reduce the contention between block
3956 * request from multiple CPUs.
3957 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003958 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003959
3960 /* we're going to use group allocation */
3961 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3962
3963 /* serialize all allocations in the group */
3964 mutex_lock(&ac->ac_lg->lg_mutex);
3965}
3966
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003967static noinline_for_stack int
3968ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003969 struct ext4_allocation_request *ar)
3970{
3971 struct super_block *sb = ar->inode->i_sb;
3972 struct ext4_sb_info *sbi = EXT4_SB(sb);
3973 struct ext4_super_block *es = sbi->s_es;
3974 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003975 unsigned int len;
3976 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05003977 ext4_grpblk_t block;
3978
3979 /* we can't allocate > group size */
3980 len = ar->len;
3981
3982 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05003983 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
3984 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003985
3986 /* start searching from the goal */
3987 goal = ar->goal;
3988 if (goal < le32_to_cpu(es->s_first_data_block) ||
3989 goal >= ext4_blocks_count(es))
3990 goal = le32_to_cpu(es->s_first_data_block);
3991 ext4_get_group_no_and_offset(sb, goal, &group, &block);
3992
3993 /* set up allocation goals */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003994 ac->ac_b_ex.fe_logical = ar->logical & ~(sbi->s_cluster_ratio - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05003995 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05003996 ac->ac_sb = sb;
3997 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003998 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05003999 ac->ac_o_ex.fe_group = group;
4000 ac->ac_o_ex.fe_start = block;
4001 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004002 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004003 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004004
4005 /* we have to define context: we'll we work with a file or
4006 * locality group. this is a policy, actually */
4007 ext4_mb_group_or_file(ac);
4008
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004009 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004010 "left: %u/%u, right %u/%u to %swritable\n",
4011 (unsigned) ar->len, (unsigned) ar->logical,
4012 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4013 (unsigned) ar->lleft, (unsigned) ar->pleft,
4014 (unsigned) ar->lright, (unsigned) ar->pright,
4015 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4016 return 0;
4017
4018}
4019
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004020static noinline_for_stack void
4021ext4_mb_discard_lg_preallocations(struct super_block *sb,
4022 struct ext4_locality_group *lg,
4023 int order, int total_entries)
4024{
4025 ext4_group_t group = 0;
4026 struct ext4_buddy e4b;
4027 struct list_head discard_list;
4028 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004029
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004030 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004031
4032 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004033
4034 spin_lock(&lg->lg_prealloc_lock);
4035 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4036 pa_inode_list) {
4037 spin_lock(&pa->pa_lock);
4038 if (atomic_read(&pa->pa_count)) {
4039 /*
4040 * This is the pa that we just used
4041 * for block allocation. So don't
4042 * free that
4043 */
4044 spin_unlock(&pa->pa_lock);
4045 continue;
4046 }
4047 if (pa->pa_deleted) {
4048 spin_unlock(&pa->pa_lock);
4049 continue;
4050 }
4051 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004052 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004053
4054 /* seems this one can be freed ... */
4055 pa->pa_deleted = 1;
4056 spin_unlock(&pa->pa_lock);
4057
4058 list_del_rcu(&pa->pa_inode_list);
4059 list_add(&pa->u.pa_tmp_list, &discard_list);
4060
4061 total_entries--;
4062 if (total_entries <= 5) {
4063 /*
4064 * we want to keep only 5 entries
4065 * allowing it to grow to 8. This
4066 * mak sure we don't call discard
4067 * soon for this list.
4068 */
4069 break;
4070 }
4071 }
4072 spin_unlock(&lg->lg_prealloc_lock);
4073
4074 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4075
4076 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4077 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004078 ext4_error(sb, "Error loading buddy information for %u",
4079 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004080 continue;
4081 }
4082 ext4_lock_group(sb, group);
4083 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004084 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004085 ext4_unlock_group(sb, group);
4086
Jing Zhange39e07f2010-05-14 00:00:00 -04004087 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004088 list_del(&pa->u.pa_tmp_list);
4089 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4090 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004091}
4092
4093/*
4094 * We have incremented pa_count. So it cannot be freed at this
4095 * point. Also we hold lg_mutex. So no parallel allocation is
4096 * possible from this lg. That means pa_free cannot be updated.
4097 *
4098 * A parallel ext4_mb_discard_group_preallocations is possible.
4099 * which can cause the lg_prealloc_list to be updated.
4100 */
4101
4102static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4103{
4104 int order, added = 0, lg_prealloc_count = 1;
4105 struct super_block *sb = ac->ac_sb;
4106 struct ext4_locality_group *lg = ac->ac_lg;
4107 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4108
4109 order = fls(pa->pa_free) - 1;
4110 if (order > PREALLOC_TB_SIZE - 1)
4111 /* The max size of hash table is PREALLOC_TB_SIZE */
4112 order = PREALLOC_TB_SIZE - 1;
4113 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004114 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004115 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4116 pa_inode_list) {
4117 spin_lock(&tmp_pa->pa_lock);
4118 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004119 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004120 continue;
4121 }
4122 if (!added && pa->pa_free < tmp_pa->pa_free) {
4123 /* Add to the tail of the previous entry */
4124 list_add_tail_rcu(&pa->pa_inode_list,
4125 &tmp_pa->pa_inode_list);
4126 added = 1;
4127 /*
4128 * we want to count the total
4129 * number of entries in the list
4130 */
4131 }
4132 spin_unlock(&tmp_pa->pa_lock);
4133 lg_prealloc_count++;
4134 }
4135 if (!added)
4136 list_add_tail_rcu(&pa->pa_inode_list,
4137 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004138 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004139
4140 /* Now trim the list to be not more than 8 elements */
4141 if (lg_prealloc_count > 8) {
4142 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004143 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004144 return;
4145 }
4146 return ;
4147}
4148
Alex Tomasc9de5602008-01-29 00:19:52 -05004149/*
4150 * release all resource we used in allocation
4151 */
4152static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4153{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004154 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004155 struct ext4_prealloc_space *pa = ac->ac_pa;
4156 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004157 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004158 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004159 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004160 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4161 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004162 pa->pa_free -= ac->ac_b_ex.fe_len;
4163 pa->pa_len -= ac->ac_b_ex.fe_len;
4164 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004165 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004166 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004167 if (pa) {
4168 /*
4169 * We want to add the pa to the right bucket.
4170 * Remove it from the list and while adding
4171 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004172 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004173 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004174 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004175 spin_lock(pa->pa_obj_lock);
4176 list_del_rcu(&pa->pa_inode_list);
4177 spin_unlock(pa->pa_obj_lock);
4178 ext4_mb_add_n_trim(ac);
4179 }
4180 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4181 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004182 if (ac->ac_bitmap_page)
4183 page_cache_release(ac->ac_bitmap_page);
4184 if (ac->ac_buddy_page)
4185 page_cache_release(ac->ac_buddy_page);
4186 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4187 mutex_unlock(&ac->ac_lg->lg_mutex);
4188 ext4_mb_collect_stats(ac);
4189 return 0;
4190}
4191
4192static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4193{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004194 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004195 int ret;
4196 int freed = 0;
4197
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004198 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004199 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004200 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4201 freed += ret;
4202 needed -= ret;
4203 }
4204
4205 return freed;
4206}
4207
4208/*
4209 * Main entry point into mballoc to allocate blocks
4210 * it tries to use preallocation first, then falls back
4211 * to usual allocation
4212 */
4213ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004214 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004215{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004216 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004217 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004218 struct ext4_sb_info *sbi;
4219 struct super_block *sb;
4220 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004221 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004222 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004223
4224 sb = ar->inode->i_sb;
4225 sbi = EXT4_SB(sb);
4226
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004227 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004228
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004229 /* Allow to use superuser reservation for quota file */
4230 if (IS_NOQUOTA(ar->inode))
4231 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4232
Mingming Cao60e58e02009-01-22 18:13:05 +01004233 /*
4234 * For delayed allocation, we could skip the ENOSPC and
4235 * EDQUOT check, as blocks and quotas have been already
4236 * reserved when data being copied into pagecache.
4237 */
Theodore Ts'of2321092011-01-10 12:12:36 -05004238 if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
Mingming Cao60e58e02009-01-22 18:13:05 +01004239 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4240 else {
4241 /* Without delayed allocation we need to verify
4242 * there is enough free blocks to do block allocation
4243 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004244 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004245 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004246 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004247
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004248 /* let others to free the space */
4249 yield();
4250 ar->len = ar->len >> 1;
4251 }
4252 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004253 *errp = -ENOSPC;
4254 return 0;
4255 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004256 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004257 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004258 dquot_alloc_block_nofail(ar->inode,
4259 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004260 } else {
4261 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004262 dquot_alloc_block(ar->inode,
4263 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004264
4265 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4266 ar->len--;
4267 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004268 }
4269 inquota = ar->len;
4270 if (ar->len == 0) {
4271 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004272 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004273 }
Mingming Caod2a17632008-07-14 17:52:37 -04004274 }
Mingming Caod2a17632008-07-14 17:52:37 -04004275
Wei Yongjun85556c92012-09-26 20:43:37 -04004276 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004277 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004278 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004279 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004280 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004281 }
4282
Eric Sandeen256bdb42008-02-10 01:13:33 -05004283 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004284 if (*errp) {
4285 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004286 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004287 }
4288
Eric Sandeen256bdb42008-02-10 01:13:33 -05004289 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4290 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004291 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4292 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004293repeat:
4294 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004295 *errp = ext4_mb_regular_allocator(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004296 if (*errp) {
4297 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004298 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004299 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004300
4301 /* as we've just preallocated more space than
4302 * user requested orinally, we store allocated
4303 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004304 if (ac->ac_status == AC_STATUS_FOUND &&
4305 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4306 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004307 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004308 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004309 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004310 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004311 /*
4312 * drop the reference that we took
4313 * in ext4_mb_use_best_found
4314 */
4315 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004316 ac->ac_b_ex.fe_group = 0;
4317 ac->ac_b_ex.fe_start = 0;
4318 ac->ac_b_ex.fe_len = 0;
4319 ac->ac_status = AC_STATUS_CONTINUE;
4320 goto repeat;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004321 } else if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004322 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004323 goto errout;
4324 } else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004325 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4326 ar->len = ac->ac_b_ex.fe_len;
4327 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004328 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004329 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004330 if (freed)
4331 goto repeat;
4332 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004333 }
4334
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004335errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004336 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004337 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004338 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004339 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004340 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004341 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004342out:
4343 if (ac)
4344 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004345 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004346 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004347 if (!ar->len) {
Theodore Ts'of2321092011-01-10 12:12:36 -05004348 if (!ext4_test_inode_state(ar->inode,
4349 EXT4_STATE_DELALLOC_RESERVED))
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004350 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004351 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004352 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004353 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004354
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004355 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004356
Alex Tomasc9de5602008-01-29 00:19:52 -05004357 return block;
4358}
Alex Tomasc9de5602008-01-29 00:19:52 -05004359
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004360/*
4361 * We can merge two free data extents only if the physical blocks
4362 * are contiguous, AND the extents were freed by the same transaction,
4363 * AND the blocks are associated with the same group.
4364 */
4365static int can_merge(struct ext4_free_data *entry1,
4366 struct ext4_free_data *entry2)
4367{
Bobi Jam18aadd42012-02-20 17:53:02 -05004368 if ((entry1->efd_tid == entry2->efd_tid) &&
4369 (entry1->efd_group == entry2->efd_group) &&
4370 ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004371 return 1;
4372 return 0;
4373}
4374
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004375static noinline_for_stack int
4376ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004377 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004378{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004379 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004380 ext4_grpblk_t cluster;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004381 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004382 struct ext4_group_info *db = e4b->bd_info;
4383 struct super_block *sb = e4b->bd_sb;
4384 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004385 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4386 struct rb_node *parent = NULL, *new_node;
4387
Frank Mayhar03901312009-01-07 00:06:22 -05004388 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004389 BUG_ON(e4b->bd_bitmap_page == NULL);
4390 BUG_ON(e4b->bd_buddy_page == NULL);
4391
Bobi Jam18aadd42012-02-20 17:53:02 -05004392 new_node = &new_entry->efd_node;
4393 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004394
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004395 if (!*n) {
4396 /* first free block exent. We need to
4397 protect buddy cache from being freed,
4398 * otherwise we'll refresh it from
4399 * on-disk bitmap and lose not-yet-available
4400 * blocks */
4401 page_cache_get(e4b->bd_buddy_page);
4402 page_cache_get(e4b->bd_bitmap_page);
4403 }
4404 while (*n) {
4405 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004406 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4407 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004408 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004409 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004410 n = &(*n)->rb_right;
4411 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004412 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004413 ext4_group_first_block_no(sb, group) +
4414 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004415 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004416 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004417 }
4418 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004419
4420 rb_link_node(new_node, parent, n);
4421 rb_insert_color(new_node, &db->bb_free_root);
4422
4423 /* Now try to see the extent can be merged to left and right */
4424 node = rb_prev(new_node);
4425 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004426 entry = rb_entry(node, struct ext4_free_data, efd_node);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004427 if (can_merge(entry, new_entry)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004428 new_entry->efd_start_cluster = entry->efd_start_cluster;
4429 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004430 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004431 ext4_journal_callback_del(handle, &entry->efd_jce);
4432 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004433 }
4434 }
4435
4436 node = rb_next(new_node);
4437 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004438 entry = rb_entry(node, struct ext4_free_data, efd_node);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004439 if (can_merge(new_entry, entry)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004440 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004441 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004442 ext4_journal_callback_del(handle, &entry->efd_jce);
4443 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004444 }
4445 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004446 /* Add the extent to transaction's private list */
Bobi Jam18aadd42012-02-20 17:53:02 -05004447 ext4_journal_callback_add(handle, ext4_free_data_callback,
4448 &new_entry->efd_jce);
Alex Tomasc9de5602008-01-29 00:19:52 -05004449 return 0;
4450}
4451
Theodore Ts'o44338712009-11-22 07:44:56 -05004452/**
4453 * ext4_free_blocks() -- Free given blocks and update quota
4454 * @handle: handle for this transaction
4455 * @inode: inode
4456 * @block: start physical block to free
4457 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004458 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004459 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004460void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004461 struct buffer_head *bh, ext4_fsblk_t block,
4462 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004463{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004464 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004465 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004466 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004467 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004468 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004469 ext4_grpblk_t bit;
4470 struct buffer_head *gd_bh;
4471 ext4_group_t block_group;
4472 struct ext4_sb_info *sbi;
4473 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004474 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004475 int err = 0;
4476 int ret;
4477
Theodore Ts'oe6362602009-11-23 07:17:05 -05004478 if (bh) {
4479 if (block)
4480 BUG_ON(block != bh->b_blocknr);
4481 else
4482 block = bh->b_blocknr;
4483 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004484
Alex Tomasc9de5602008-01-29 00:19:52 -05004485 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004486 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4487 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004488 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004489 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004490 goto error_return;
4491 }
4492
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004493 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004494 trace_ext4_free_blocks(inode, block, count, flags);
4495
4496 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4497 struct buffer_head *tbh = bh;
4498 int i;
4499
4500 BUG_ON(bh && (count > 1));
4501
4502 for (i = 0; i < count; i++) {
4503 if (!bh)
4504 tbh = sb_find_get_block(inode->i_sb,
4505 block + i);
Namhyung Kim87783692010-10-27 21:30:11 -04004506 if (unlikely(!tbh))
4507 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004508 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004509 inode, tbh, block + i);
4510 }
4511 }
4512
Theodore Ts'o60e66792010-05-17 07:00:00 -04004513 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004514 * We need to make sure we don't reuse the freed block until
4515 * after the transaction is committed, which we can do by
4516 * treating the block as metadata, below. We make an
4517 * exception if the inode is to be written in writeback mode
4518 * since writeback mode has weak data consistency guarantees.
4519 */
4520 if (!ext4_should_writeback_data(inode))
4521 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004522
Theodore Ts'o84130192011-09-09 18:50:51 -04004523 /*
4524 * If the extent to be freed does not begin on a cluster
4525 * boundary, we need to deal with partial clusters at the
4526 * beginning and end of the extent. Normally we will free
4527 * blocks at the beginning or the end unless we are explicitly
4528 * requested to avoid doing so.
4529 */
4530 overflow = block & (sbi->s_cluster_ratio - 1);
4531 if (overflow) {
4532 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4533 overflow = sbi->s_cluster_ratio - overflow;
4534 block += overflow;
4535 if (count > overflow)
4536 count -= overflow;
4537 else
4538 return;
4539 } else {
4540 block -= overflow;
4541 count += overflow;
4542 }
4543 }
4544 overflow = count & (sbi->s_cluster_ratio - 1);
4545 if (overflow) {
4546 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4547 if (count > overflow)
4548 count -= overflow;
4549 else
4550 return;
4551 } else
4552 count += sbi->s_cluster_ratio - overflow;
4553 }
4554
Alex Tomasc9de5602008-01-29 00:19:52 -05004555do_more:
4556 overflow = 0;
4557 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4558
4559 /*
4560 * Check to see if we are freeing blocks across a group
4561 * boundary.
4562 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004563 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4564 overflow = EXT4_C2B(sbi, bit) + count -
4565 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004566 count -= overflow;
4567 }
Lukas Czerner810da242013-03-02 17:18:58 -05004568 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004569 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004570 if (!bitmap_bh) {
4571 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004572 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004573 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004574 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004575 if (!gdp) {
4576 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004577 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004578 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004579
4580 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4581 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4582 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004583 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004584 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004585 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004586
Eric Sandeen12062dd2010-02-15 14:19:27 -05004587 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004588 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004589 /* err = 0. ext4_std_error should be a no op */
4590 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004591 }
4592
4593 BUFFER_TRACE(bitmap_bh, "getting write access");
4594 err = ext4_journal_get_write_access(handle, bitmap_bh);
4595 if (err)
4596 goto error_return;
4597
4598 /*
4599 * We are about to modify some metadata. Call the journal APIs
4600 * to unshare ->b_data if a currently-committing transaction is
4601 * using it
4602 */
4603 BUFFER_TRACE(gd_bh, "get_write_access");
4604 err = ext4_journal_get_write_access(handle, gd_bh);
4605 if (err)
4606 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004607#ifdef AGGRESSIVE_CHECK
4608 {
4609 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004610 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004611 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4612 }
4613#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004614 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004615
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004616 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4617 if (err)
4618 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004619
4620 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004621 struct ext4_free_data *new_entry;
4622 /*
4623 * blocks being freed are metadata. these blocks shouldn't
4624 * be used until this transaction is committed
4625 */
Bobi Jam18aadd42012-02-20 17:53:02 -05004626 new_entry = kmem_cache_alloc(ext4_free_data_cachep, GFP_NOFS);
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004627 if (!new_entry) {
Salman Qazi02b78312012-05-31 23:51:27 -04004628 ext4_mb_unload_buddy(&e4b);
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004629 err = -ENOMEM;
4630 goto error_return;
4631 }
Bobi Jam18aadd42012-02-20 17:53:02 -05004632 new_entry->efd_start_cluster = bit;
4633 new_entry->efd_group = block_group;
4634 new_entry->efd_count = count_clusters;
4635 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004636
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004637 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004638 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004639 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004640 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004641 /* need to update group_info->bb_free and bitmap
4642 * with group lock held. generate_buddy look at
4643 * them with group lock_held
4644 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004645 if (test_opt(sb, DISCARD)) {
4646 err = ext4_issue_discard(sb, block_group, bit, count);
4647 if (err && err != -EOPNOTSUPP)
4648 ext4_msg(sb, KERN_WARNING, "discard request in"
4649 " group:%d block:%d count:%lu failed"
4650 " with %d", block_group, bit, count,
4651 err);
4652 }
4653
4654
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004655 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004656 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4657 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004658 }
4659
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004660 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4661 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004662 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004663 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004664 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04004665 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004666
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004667 if (sbi->s_log_groups_per_flex) {
4668 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o24aaa8e2011-09-09 18:58:51 -04004669 atomic_add(count_clusters,
4670 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004671 }
4672
Jing Zhange39e07f2010-05-14 00:00:00 -04004673 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004674
Theodore Ts'o44338712009-11-22 07:44:56 -05004675 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004676
Aditya Kali7b415bf2011-09-09 19:04:51 -04004677 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
4678 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
4679
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004680 /* We dirtied the bitmap block */
4681 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4682 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4683
Alex Tomasc9de5602008-01-29 00:19:52 -05004684 /* And the group descriptor block */
4685 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004686 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004687 if (!err)
4688 err = ret;
4689
4690 if (overflow && !err) {
4691 block += count;
4692 count = overflow;
4693 put_bh(bitmap_bh);
4694 goto do_more;
4695 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004696error_return:
4697 brelse(bitmap_bh);
4698 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004699 return;
4700}
Lukas Czerner7360d172010-10-27 21:30:12 -04004701
4702/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004703 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004704 * @handle: handle to this transaction
4705 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004706 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004707 * @count: number of blocks to free
4708 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004709 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004710 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004711int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004712 ext4_fsblk_t block, unsigned long count)
4713{
4714 struct buffer_head *bitmap_bh = NULL;
4715 struct buffer_head *gd_bh;
4716 ext4_group_t block_group;
4717 ext4_grpblk_t bit;
4718 unsigned int i;
4719 struct ext4_group_desc *desc;
4720 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004721 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004722 int err = 0, ret, blk_free_count;
4723 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004724
4725 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4726
Yongqiang Yang4740b832011-07-26 21:51:08 -04004727 if (count == 0)
4728 return 0;
4729
Amir Goldstein2846e822011-05-09 10:46:41 -04004730 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004731 /*
4732 * Check to see if we are freeing blocks across a group
4733 * boundary.
4734 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004735 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4736 ext4_warning(sb, "too much blocks added to group %u\n",
4737 block_group);
4738 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004739 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004740 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004741
Amir Goldstein2846e822011-05-09 10:46:41 -04004742 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004743 if (!bitmap_bh) {
4744 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004745 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004746 }
4747
Amir Goldstein2846e822011-05-09 10:46:41 -04004748 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004749 if (!desc) {
4750 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004751 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004752 }
Amir Goldstein2846e822011-05-09 10:46:41 -04004753
4754 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4755 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4756 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4757 in_range(block + count - 1, ext4_inode_table(sb, desc),
4758 sbi->s_itb_per_group)) {
4759 ext4_error(sb, "Adding blocks in system zones - "
4760 "Block = %llu, count = %lu",
4761 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004762 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004763 goto error_return;
4764 }
4765
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004766 BUFFER_TRACE(bitmap_bh, "getting write access");
4767 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004768 if (err)
4769 goto error_return;
4770
4771 /*
4772 * We are about to modify some metadata. Call the journal APIs
4773 * to unshare ->b_data if a currently-committing transaction is
4774 * using it
4775 */
4776 BUFFER_TRACE(gd_bh, "get_write_access");
4777 err = ext4_journal_get_write_access(handle, gd_bh);
4778 if (err)
4779 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04004780
Amir Goldstein2846e822011-05-09 10:46:41 -04004781 for (i = 0, blocks_freed = 0; i < count; i++) {
4782 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04004783 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04004784 ext4_error(sb, "bit already cleared for block %llu",
4785 (ext4_fsblk_t)(block + i));
4786 BUFFER_TRACE(bitmap_bh, "bit already cleared");
4787 } else {
4788 blocks_freed++;
4789 }
4790 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004791
4792 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4793 if (err)
4794 goto error_return;
4795
4796 /*
4797 * need to update group_info->bb_free and bitmap
4798 * with group lock held. generate_buddy look at
4799 * them with group lock_held
4800 */
Amir Goldstein2846e822011-05-09 10:46:41 -04004801 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004802 mb_clear_bits(bitmap_bh->b_data, bit, count);
4803 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004804 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
4805 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04004806 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004807 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04004808 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04004809 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05004810 EXT4_NUM_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04004811
4812 if (sbi->s_log_groups_per_flex) {
4813 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Lukas Czerner810da242013-03-02 17:18:58 -05004814 atomic_add(EXT4_NUM_B2C(sbi, blocks_freed),
Theodore Ts'o24aaa8e2011-09-09 18:58:51 -04004815 &sbi->s_flex_groups[flex_group].free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04004816 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004817
4818 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04004819
4820 /* We dirtied the bitmap block */
4821 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4822 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4823
4824 /* And the group descriptor block */
4825 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4826 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4827 if (!err)
4828 err = ret;
4829
4830error_return:
4831 brelse(bitmap_bh);
4832 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004833 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04004834}
4835
4836/**
Lukas Czerner7360d172010-10-27 21:30:12 -04004837 * ext4_trim_extent -- function to TRIM one single free extent in the group
4838 * @sb: super block for the file system
4839 * @start: starting block of the free extent in the alloc. group
4840 * @count: number of blocks to TRIM
4841 * @group: alloc. group we are working with
4842 * @e4b: ext4 buddy for the group
4843 *
4844 * Trim "count" blocks starting at "start" in the "group". To assure that no
4845 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4846 * be called with under the group lock.
4847 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004848static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04004849 ext4_group_t group, struct ext4_buddy *e4b)
Lukas Czerner7360d172010-10-27 21:30:12 -04004850{
4851 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004852 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04004853
Tao Mab3d4c2b2011-07-11 00:01:52 -04004854 trace_ext4_trim_extent(sb, group, start, count);
4855
Lukas Czerner7360d172010-10-27 21:30:12 -04004856 assert_spin_locked(ext4_group_lock_ptr(sb, group));
4857
4858 ex.fe_start = start;
4859 ex.fe_group = group;
4860 ex.fe_len = count;
4861
4862 /*
4863 * Mark blocks used, so no one can reuse them while
4864 * being trimmed.
4865 */
4866 mb_mark_used(e4b, &ex);
4867 ext4_unlock_group(sb, group);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004868 ret = ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04004869 ext4_lock_group(sb, group);
4870 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004871 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04004872}
4873
4874/**
4875 * ext4_trim_all_free -- function to trim all free space in alloc. group
4876 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04004877 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04004878 * @start: first group block to examine
4879 * @max: last group block to examine
4880 * @minblocks: minimum extent block count
4881 *
4882 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4883 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4884 * the extent.
4885 *
4886 *
4887 * ext4_trim_all_free walks through group's block bitmap searching for free
4888 * extents. When the free extent is found, mark it as used in group buddy
4889 * bitmap. Then issue a TRIM command on this extent and free the extent in
4890 * the group buddy bitmap. This is done until whole group is scanned.
4891 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05004892static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04004893ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
4894 ext4_grpblk_t start, ext4_grpblk_t max,
4895 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04004896{
4897 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04004898 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04004899 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004900 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04004901
Tao Mab3d4c2b2011-07-11 00:01:52 -04004902 trace_ext4_trim_all_free(sb, group, start, max);
4903
Lukas Czerner78944082011-05-24 18:16:27 -04004904 ret = ext4_mb_load_buddy(sb, group, &e4b);
4905 if (ret) {
4906 ext4_error(sb, "Error in loading buddy "
4907 "information for %u", group);
4908 return ret;
4909 }
Lukas Czerner78944082011-05-24 18:16:27 -04004910 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04004911
4912 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04004913 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
4914 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
4915 goto out;
4916
Lukas Czerner78944082011-05-24 18:16:27 -04004917 start = (e4b.bd_info->bb_first_free > start) ?
4918 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04004919
Lukas Czerner913eed832012-03-21 21:22:22 -04004920 while (start <= max) {
4921 start = mb_find_next_zero_bit(bitmap, max + 1, start);
4922 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04004923 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04004924 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04004925
4926 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004927 ret = ext4_trim_extent(sb, start,
4928 next - start, group, &e4b);
4929 if (ret && ret != -EOPNOTSUPP)
4930 break;
4931 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04004932 count += next - start;
4933 }
Tao Ma169ddc32011-07-11 00:00:07 -04004934 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04004935 start = next + 1;
4936
4937 if (fatal_signal_pending(current)) {
4938 count = -ERESTARTSYS;
4939 break;
4940 }
4941
4942 if (need_resched()) {
4943 ext4_unlock_group(sb, group);
4944 cond_resched();
4945 ext4_lock_group(sb, group);
4946 }
4947
Tao Ma169ddc32011-07-11 00:00:07 -04004948 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04004949 break;
4950 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04004951
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004952 if (!ret) {
4953 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04004954 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004955 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04004956out:
Lukas Czerner7360d172010-10-27 21:30:12 -04004957 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04004958 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04004959
4960 ext4_debug("trimmed %d blocks in the group %d\n",
4961 count, group);
4962
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004963 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04004964}
4965
4966/**
4967 * ext4_trim_fs() -- trim ioctl handle function
4968 * @sb: superblock for filesystem
4969 * @range: fstrim_range structure
4970 *
4971 * start: First Byte to trim
4972 * len: number of Bytes to trim from start
4973 * minlen: minimum extent length in Bytes
4974 * ext4_trim_fs goes through all allocation groups containing Bytes from
4975 * start to start+len. For each such a group ext4_trim_all_free function
4976 * is invoked to trim all free space.
4977 */
4978int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
4979{
Lukas Czerner78944082011-05-24 18:16:27 -04004980 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04004981 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04004982 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04004983 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004984 ext4_fsblk_t first_data_blk =
4985 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04004986 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04004987 int ret = 0;
4988
4989 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04004990 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04004991 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
4992 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04004993
Lukas Czerner5de35e82012-10-22 18:01:19 -04004994 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
4995 start >= max_blks ||
4996 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04004997 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04004998 if (end >= max_blks)
4999 end = max_blks - 1;
5000 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005001 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005002 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005003 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005004
Lukas Czerner913eed832012-03-21 21:22:22 -04005005 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005006 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005007 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005008 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005009 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005010
Lukas Czerner913eed832012-03-21 21:22:22 -04005011 /* end now represents the last cluster to discard in this group */
5012 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005013
5014 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005015 grp = ext4_get_group_info(sb, group);
5016 /* We only do this if the grp has never been initialized */
5017 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
5018 ret = ext4_mb_init_group(sb, group);
5019 if (ret)
5020 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005021 }
5022
Tao Ma0ba08512011-03-23 15:48:11 -04005023 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005024 * For all the groups except the last one, last cluster will
5025 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5026 * change it for the last group, note that last_cluster is
5027 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005028 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005029 if (group == last_group)
5030 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005031
Lukas Czerner78944082011-05-24 18:16:27 -04005032 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005033 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005034 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005035 if (cnt < 0) {
5036 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005037 break;
5038 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005039 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005040 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005041
5042 /*
5043 * For every group except the first one, we are sure
5044 * that the first cluster to discard will be cluster #0.
5045 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005046 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005047 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005048
Tao Ma3d56b8d2011-07-11 00:03:38 -04005049 if (!ret)
5050 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5051
Tao Ma22f10452011-07-10 23:52:37 -04005052out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005053 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005054 return ret;
5055}