blob: b97a2d2f0fdf1461f8257ef1dfabf54abd3bfa47 [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
Mingming Cao8f6e39a2008-04-29 22:01:31 -040024#include "mballoc.h"
Theodore Ts'o6ba495e2009-09-18 13:38:55 -040025#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040027#include <trace/events/ext4.h>
28
Alex Tomasc9de5602008-01-29 00:19:52 -050029/*
30 * MUSTDO:
31 * - test ext4_ext_search_left() and ext4_ext_search_right()
32 * - search for metadata in few groups
33 *
34 * TODO v4:
35 * - normalization should take into account whether file is still open
36 * - discard preallocations if no free space left (policy?)
37 * - don't normalize tails
38 * - quota
39 * - reservation for superuser
40 *
41 * TODO v3:
42 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
43 * - track min/max extents in each group for better group selection
44 * - mb_mark_used() may allocate chunk right after splitting buddy
45 * - tree of groups sorted by number of free blocks
46 * - error handling
47 */
48
49/*
50 * The allocation request involve request for multiple number of blocks
51 * near to the goal(block) value specified.
52 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040053 * During initialization phase of the allocator we decide to use the
54 * group preallocation or inode preallocation depending on the size of
55 * the file. The size of the file could be the resulting file size we
56 * would have after allocation, or the current file size, which ever
57 * is larger. If the size is less than sbi->s_mb_stream_request we
58 * select to use the group preallocation. The default value of
59 * s_mb_stream_request is 16 blocks. This can also be tuned via
60 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
61 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050062 *
63 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040064 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050065 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040066 * First stage the allocator looks at the inode prealloc list,
67 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
68 * spaces for this particular inode. The inode prealloc space is
69 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050070 *
71 * pa_lstart -> the logical start block for this prealloc space
72 * pa_pstart -> the physical start block for this prealloc space
Daniel Mack1537a362010-01-29 15:57:49 +080073 * pa_len -> length for this prealloc space
Alex Tomasc9de5602008-01-29 00:19:52 -050074 * pa_free -> free space available in this prealloc space
75 *
76 * The inode preallocation space is used looking at the _logical_ start
77 * block. If only the logical file block falls within the range of prealloc
Tao Macaaf7a22011-07-11 18:42:42 -040078 * space we will consume the particular prealloc space. This makes sure that
79 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050080 *
81 * The important thing to be noted in case of inode prealloc space is that
82 * we don't modify the values associated to inode prealloc space except
83 * pa_free.
84 *
85 * If we are not able to find blocks in the inode prealloc space and if we
86 * have the group allocation flag set then we look at the locality group
Tao Macaaf7a22011-07-11 18:42:42 -040087 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050088 *
89 * ext4_sb_info.s_locality_groups[smp_processor_id()]
90 *
91 * The reason for having a per cpu locality group is to reduce the contention
92 * between CPUs. It is possible to get scheduled at this point.
93 *
94 * The locality group prealloc space is used looking at whether we have
Lucas De Marchi25985ed2011-03-30 22:57:33 -030095 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -050096 *
97 * If we can't allocate blocks via inode prealloc or/and locality group
98 * prealloc then we look at the buddy cache. The buddy cache is represented
99 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
100 * mapped to the buddy and bitmap information regarding different
101 * groups. The buddy information is attached to buddy cache inode so that
102 * we can access them through the page cache. The information regarding
103 * each group is loaded via ext4_mb_load_buddy. The information involve
104 * block bitmap and buddy information. The information are stored in the
105 * inode as:
106 *
107 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500108 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500109 *
110 *
111 * one block each for bitmap and buddy information. So for each group we
112 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
113 * blocksize) blocks. So it can have information regarding groups_per_page
114 * which is blocks_per_page/2
115 *
116 * The buddy cache inode is not stored on disk. The inode is thrown
117 * away when the filesystem is unmounted.
118 *
119 * We look for count number of blocks in the buddy cache. If we were able
120 * to locate that many free blocks we return with additional information
121 * regarding rest of the contiguous physical block available
122 *
123 * Before allocating blocks via buddy cache we normalize the request
124 * blocks. This ensure we ask for more blocks that we needed. The extra
125 * blocks that we get after allocation is added to the respective prealloc
126 * list. In case of inode preallocation we follow a list of heuristics
127 * based on file size. This can be found in ext4_mb_normalize_request. If
128 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400129 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
Alex Tomasc9de5602008-01-29 00:19:52 -0500130 * 512 blocks. This can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400131 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500132 * terms of number of blocks. If we have mounted the file system with -O
133 * stripe=<value> option the group prealloc request is normalized to the
134 * stripe value (sbi->s_stripe)
135 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400136 * The regular allocator(using the buddy cache) supports few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500137 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400138 * /sys/fs/ext4/<partition>/mb_min_to_scan
139 * /sys/fs/ext4/<partition>/mb_max_to_scan
140 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500141 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400142 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500143 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
144 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400145 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200146 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400147 * stripe size. This should result in better allocation on RAID setups. If
148 * not, we search in the specific group using bitmap for best extents. The
149 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500150 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400151 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500152 * best extent in the found extents. Searching for the blocks starts with
153 * the group specified as the goal value in allocation context via
154 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400155 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500156 * checked.
157 *
158 * Both the prealloc space are getting populated as above. So for the first
159 * request we will hit the buddy cache which will result in this prealloc
160 * space getting filled. The prealloc space is then later used for the
161 * subsequent request.
162 */
163
164/*
165 * mballoc operates on the following data:
166 * - on-disk bitmap
167 * - in-core buddy (actually includes buddy and bitmap)
168 * - preallocation descriptors (PAs)
169 *
170 * there are two types of preallocations:
171 * - inode
172 * assiged to specific inode and can be used for this inode only.
173 * it describes part of inode's space preallocated to specific
174 * physical blocks. any block from that preallocated can be used
175 * independent. the descriptor just tracks number of blocks left
176 * unused. so, before taking some block from descriptor, one must
177 * make sure corresponded logical block isn't allocated yet. this
178 * also means that freeing any block within descriptor's range
179 * must discard all preallocated blocks.
180 * - locality group
181 * assigned to specific locality group which does not translate to
182 * permanent set of inodes: inode can join and leave group. space
183 * from this type of preallocation can be used for any inode. thus
184 * it's consumed from the beginning to the end.
185 *
186 * relation between them can be expressed as:
187 * in-core buddy = on-disk bitmap + preallocation descriptors
188 *
189 * this mean blocks mballoc considers used are:
190 * - allocated blocks (persistent)
191 * - preallocated blocks (non-persistent)
192 *
193 * consistency in mballoc world means that at any time a block is either
194 * free or used in ALL structures. notice: "any time" should not be read
195 * literally -- time is discrete and delimited by locks.
196 *
197 * to keep it simple, we don't use block numbers, instead we count number of
198 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
199 *
200 * all operations can be expressed as:
201 * - init buddy: buddy = on-disk + PAs
202 * - new PA: buddy += N; PA = N
203 * - use inode PA: on-disk += N; PA -= N
204 * - discard inode PA buddy -= on-disk - PA; PA = 0
205 * - use locality group PA on-disk += N; PA -= N
206 * - discard locality group PA buddy -= PA; PA = 0
207 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
208 * is used in real operation because we can't know actual used
209 * bits from PA, only from on-disk bitmap
210 *
211 * if we follow this strict logic, then all operations above should be atomic.
212 * given some of them can block, we'd have to use something like semaphores
213 * killing performance on high-end SMP hardware. let's try to relax it using
214 * the following knowledge:
215 * 1) if buddy is referenced, it's already initialized
216 * 2) while block is used in buddy and the buddy is referenced,
217 * nobody can re-allocate that block
218 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
219 * bit set and PA claims same block, it's OK. IOW, one can set bit in
220 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
221 * block
222 *
223 * so, now we're building a concurrency table:
224 * - init buddy vs.
225 * - new PA
226 * blocks for PA are allocated in the buddy, buddy must be referenced
227 * until PA is linked to allocation group to avoid concurrent buddy init
228 * - use inode PA
229 * we need to make sure that either on-disk bitmap or PA has uptodate data
230 * given (3) we care that PA-=N operation doesn't interfere with init
231 * - discard inode PA
232 * the simplest way would be to have buddy initialized by the discard
233 * - use locality group PA
234 * again PA-=N must be serialized with init
235 * - discard locality group PA
236 * the simplest way would be to have buddy initialized by the discard
237 * - new PA vs.
238 * - use inode PA
239 * i_data_sem serializes them
240 * - discard inode PA
241 * discard process must wait until PA isn't used by another process
242 * - use locality group PA
243 * some mutex should serialize them
244 * - discard locality group PA
245 * discard process must wait until PA isn't used by another process
246 * - use inode PA
247 * - use inode PA
248 * i_data_sem or another mutex should serializes them
249 * - discard inode PA
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * nothing wrong here -- they're different PAs covering different blocks
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
255 *
256 * now we're ready to make few consequences:
257 * - PA is referenced and while it is no discard is possible
258 * - PA is referenced until block isn't marked in on-disk bitmap
259 * - PA changes only after on-disk bitmap
260 * - discard must not compete with init. either init is done before
261 * any discard or they're serialized somehow
262 * - buddy init as sum of on-disk bitmap and PAs is done atomically
263 *
264 * a special case when we've used PA to emptiness. no need to modify buddy
265 * in this case, but we should care about concurrent init
266 *
267 */
268
269 /*
270 * Logic in few words:
271 *
272 * - allocation:
273 * load group
274 * find blocks
275 * mark bits in on-disk bitmap
276 * release group
277 *
278 * - use preallocation:
279 * find proper PA (per-inode or group)
280 * load group
281 * mark bits in on-disk bitmap
282 * release group
283 * release PA
284 *
285 * - free:
286 * load group
287 * mark bits in on-disk bitmap
288 * release group
289 *
290 * - discard preallocations in group:
291 * mark PAs deleted
292 * move them onto local list
293 * load on-disk bitmap
294 * load group
295 * remove PA from object (inode or locality group)
296 * mark free blocks in-core
297 *
298 * - discard inode's preallocations:
299 */
300
301/*
302 * Locking rules
303 *
304 * Locks:
305 * - bitlock on a group (group)
306 * - object (inode/locality) (object)
307 * - per-pa lock (pa)
308 *
309 * Paths:
310 * - new pa
311 * object
312 * group
313 *
314 * - find and use pa:
315 * pa
316 *
317 * - release consumed pa:
318 * pa
319 * group
320 * object
321 *
322 * - generate in-core bitmap:
323 * group
324 * pa
325 *
326 * - discard all for given object (inode, locality group):
327 * object
328 * pa
329 * group
330 *
331 * - discard all for given group:
332 * group
333 * pa
334 * group
335 * object
336 *
337 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500338static struct kmem_cache *ext4_pspace_cachep;
339static struct kmem_cache *ext4_ac_cachep;
340static struct kmem_cache *ext4_free_ext_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400341
342/* We create slab caches for groupinfo data structures based on the
343 * superblock block size. There will be one per mounted filesystem for
344 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500345#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400346static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
347
Eric Sandeen2892c152011-02-12 08:12:18 -0500348static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
349 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
350 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
351 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
352};
353
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500354static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
355 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500356static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
357 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500358static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
359
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500360static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
361{
Alex Tomasc9de5602008-01-29 00:19:52 -0500362#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500363 *bit += ((unsigned long) addr & 7UL) << 3;
364 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500365#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500366 *bit += ((unsigned long) addr & 3UL) << 3;
367 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500368#else
369#error "how many bits you are?!"
370#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500371 return addr;
372}
Alex Tomasc9de5602008-01-29 00:19:52 -0500373
374static inline int mb_test_bit(int bit, void *addr)
375{
376 /*
377 * ext4_test_bit on architecture like powerpc
378 * needs unsigned long aligned address
379 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500380 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500381 return ext4_test_bit(bit, addr);
382}
383
384static inline void mb_set_bit(int bit, void *addr)
385{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500386 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500387 ext4_set_bit(bit, addr);
388}
389
Alex Tomasc9de5602008-01-29 00:19:52 -0500390static inline void mb_clear_bit(int bit, void *addr)
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 ext4_clear_bit(bit, addr);
394}
395
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500396static inline int mb_find_next_zero_bit(void *addr, int max, int start)
397{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400398 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500399 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400400 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500401 start += fix;
402
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400403 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
404 if (ret > max)
405 return max;
406 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500407}
408
409static inline int mb_find_next_bit(void *addr, int max, int start)
410{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400411 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500412 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400413 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500414 start += fix;
415
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400416 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
417 if (ret > max)
418 return max;
419 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500420}
421
Alex Tomasc9de5602008-01-29 00:19:52 -0500422static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
423{
424 char *bb;
425
Alex Tomasc9de5602008-01-29 00:19:52 -0500426 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
427 BUG_ON(max == NULL);
428
429 if (order > e4b->bd_blkbits + 1) {
430 *max = 0;
431 return NULL;
432 }
433
434 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500435 if (order == 0) {
436 *max = 1 << (e4b->bd_blkbits + 3);
Alex Tomasc9de5602008-01-29 00:19:52 -0500437 return EXT4_MB_BITMAP(e4b);
Coly Li84b775a2011-02-24 12:51:59 -0500438 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500439
440 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
441 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
442
443 return bb;
444}
445
446#ifdef DOUBLE_CHECK
447static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
448 int first, int count)
449{
450 int i;
451 struct super_block *sb = e4b->bd_sb;
452
453 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
454 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400455 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500456 for (i = 0; i < count; i++) {
457 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
458 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500459
460 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500461 blocknr += first + i;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500462 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400463 inode ? inode->i_ino : 0,
464 blocknr,
465 "freeing block already freed "
466 "(bit %u)",
467 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500468 }
469 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
470 }
471}
472
473static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
474{
475 int i;
476
477 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
478 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400479 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500480 for (i = 0; i < count; i++) {
481 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
482 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
483 }
484}
485
486static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
487{
488 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
489 unsigned char *b1, *b2;
490 int i;
491 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
492 b2 = (unsigned char *) bitmap;
493 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
494 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500495 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400496 "at byte %u(%u): %x in copy != %x "
497 "on disk/prealloc\n",
498 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500499 BUG();
500 }
501 }
502 }
503}
504
505#else
506static inline void mb_free_blocks_double(struct inode *inode,
507 struct ext4_buddy *e4b, int first, int count)
508{
509 return;
510}
511static inline void mb_mark_used_double(struct ext4_buddy *e4b,
512 int first, int count)
513{
514 return;
515}
516static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
517{
518 return;
519}
520#endif
521
522#ifdef AGGRESSIVE_CHECK
523
524#define MB_CHECK_ASSERT(assert) \
525do { \
526 if (!(assert)) { \
527 printk(KERN_EMERG \
528 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
529 function, file, line, # assert); \
530 BUG(); \
531 } \
532} while (0)
533
534static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
535 const char *function, int line)
536{
537 struct super_block *sb = e4b->bd_sb;
538 int order = e4b->bd_blkbits + 1;
539 int max;
540 int max2;
541 int i;
542 int j;
543 int k;
544 int count;
545 struct ext4_group_info *grp;
546 int fragments = 0;
547 int fstart;
548 struct list_head *cur;
549 void *buddy;
550 void *buddy2;
551
Alex Tomasc9de5602008-01-29 00:19:52 -0500552 {
553 static int mb_check_counter;
554 if (mb_check_counter++ % 100 != 0)
555 return 0;
556 }
557
558 while (order > 1) {
559 buddy = mb_find_buddy(e4b, order, &max);
560 MB_CHECK_ASSERT(buddy);
561 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
562 MB_CHECK_ASSERT(buddy2);
563 MB_CHECK_ASSERT(buddy != buddy2);
564 MB_CHECK_ASSERT(max * 2 == max2);
565
566 count = 0;
567 for (i = 0; i < max; i++) {
568
569 if (mb_test_bit(i, buddy)) {
570 /* only single bit in buddy2 may be 1 */
571 if (!mb_test_bit(i << 1, buddy2)) {
572 MB_CHECK_ASSERT(
573 mb_test_bit((i<<1)+1, buddy2));
574 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
575 MB_CHECK_ASSERT(
576 mb_test_bit(i << 1, buddy2));
577 }
578 continue;
579 }
580
581 /* both bits in buddy2 must be 0 */
582 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
583 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
584
585 for (j = 0; j < (1 << order); j++) {
586 k = (i * (1 << order)) + j;
587 MB_CHECK_ASSERT(
588 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
589 }
590 count++;
591 }
592 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
593 order--;
594 }
595
596 fstart = -1;
597 buddy = mb_find_buddy(e4b, 0, &max);
598 for (i = 0; i < max; i++) {
599 if (!mb_test_bit(i, buddy)) {
600 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
601 if (fstart == -1) {
602 fragments++;
603 fstart = i;
604 }
605 continue;
606 }
607 fstart = -1;
608 /* check used bits only */
609 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
610 buddy2 = mb_find_buddy(e4b, j, &max2);
611 k = i >> j;
612 MB_CHECK_ASSERT(k < max2);
613 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
614 }
615 }
616 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
617 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
618
619 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500620 list_for_each(cur, &grp->bb_prealloc_list) {
621 ext4_group_t groupnr;
622 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400623 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
624 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500625 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400626 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500627 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
628 }
629 return 0;
630}
631#undef MB_CHECK_ASSERT
632#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400633 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500634#else
635#define mb_check_buddy(e4b)
636#endif
637
Coly Li7c786052011-02-24 13:24:25 -0500638/*
639 * Divide blocks started from @first with length @len into
640 * smaller chunks with power of 2 blocks.
641 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
642 * then increase bb_counters[] for corresponded chunk size.
643 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500644static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400645 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500646 struct ext4_group_info *grp)
647{
648 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400649 ext4_grpblk_t min;
650 ext4_grpblk_t max;
651 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500652 unsigned short border;
653
Valerie Clementb73fce62008-02-15 13:48:51 -0500654 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500655
656 border = 2 << sb->s_blocksize_bits;
657
658 while (len > 0) {
659 /* find how many blocks can be covered since this position */
660 max = ffs(first | border) - 1;
661
662 /* find how many blocks of power 2 we need to mark */
663 min = fls(len) - 1;
664
665 if (max < min)
666 min = max;
667 chunk = 1 << min;
668
669 /* mark multiblock chunks only */
670 grp->bb_counters[min]++;
671 if (min > 0)
672 mb_clear_bit(first >> min,
673 buddy + sbi->s_mb_offsets[min]);
674
675 len -= chunk;
676 first += chunk;
677 }
678}
679
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400680/*
681 * Cache the order of the largest free extent we have available in this block
682 * group.
683 */
684static void
685mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
686{
687 int i;
688 int bits;
689
690 grp->bb_largest_free_order = -1; /* uninit */
691
692 bits = sb->s_blocksize_bits + 1;
693 for (i = bits; i >= 0; i--) {
694 if (grp->bb_counters[i] > 0) {
695 grp->bb_largest_free_order = i;
696 break;
697 }
698 }
699}
700
Eric Sandeen089ceec2009-07-05 22:17:31 -0400701static noinline_for_stack
702void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500703 void *buddy, void *bitmap, ext4_group_t group)
704{
705 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Eric Sandeena36b4492009-08-25 22:36:45 -0400706 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
707 ext4_grpblk_t i = 0;
708 ext4_grpblk_t first;
709 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500710 unsigned free = 0;
711 unsigned fragments = 0;
712 unsigned long long period = get_cycles();
713
714 /* initialize buddy from bitmap which is aggregation
715 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500716 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500717 grp->bb_first_free = i;
718 while (i < max) {
719 fragments++;
720 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500721 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500722 len = i - first;
723 free += len;
724 if (len > 1)
725 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
726 else
727 grp->bb_counters[0]++;
728 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500729 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500730 }
731 grp->bb_fragments = fragments;
732
733 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400734 ext4_grp_locked_error(sb, group, 0, 0,
735 "%u blocks in bitmap, %u in gd",
736 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500737 /*
738 * If we intent to continue, we consider group descritor
739 * corrupt and update bb_free using bitmap value
740 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500741 grp->bb_free = free;
742 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400743 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500744
745 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
746
747 period = get_cycles() - period;
748 spin_lock(&EXT4_SB(sb)->s_bal_lock);
749 EXT4_SB(sb)->s_mb_buddies_generated++;
750 EXT4_SB(sb)->s_mb_generation_time += period;
751 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
752}
753
754/* The buddy information is attached the buddy cache inode
755 * for convenience. The information regarding each group
756 * is loaded via ext4_mb_load_buddy. The information involve
757 * block bitmap and buddy information. The information are
758 * stored in the inode as
759 *
760 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500761 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500762 *
763 *
764 * one block each for bitmap and buddy information.
765 * So for each group we take up 2 blocks. A page can
766 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
767 * So it can have information regarding groups_per_page which
768 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400769 *
770 * Locking note: This routine takes the block group lock of all groups
771 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500772 */
773
774static int ext4_mb_init_cache(struct page *page, char *incore)
775{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400776 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500777 int blocksize;
778 int blocks_per_page;
779 int groups_per_page;
780 int err = 0;
781 int i;
782 ext4_group_t first_group;
783 int first_block;
784 struct super_block *sb;
785 struct buffer_head *bhs;
786 struct buffer_head **bh;
787 struct inode *inode;
788 char *data;
789 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400790 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500791
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400792 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500793
794 inode = page->mapping->host;
795 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400796 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500797 blocksize = 1 << inode->i_blkbits;
798 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
799
800 groups_per_page = blocks_per_page >> 1;
801 if (groups_per_page == 0)
802 groups_per_page = 1;
803
804 /* allocate buffer_heads to read bitmaps */
805 if (groups_per_page > 1) {
806 err = -ENOMEM;
807 i = sizeof(struct buffer_head *) * groups_per_page;
808 bh = kzalloc(i, GFP_NOFS);
809 if (bh == NULL)
810 goto out;
811 } else
812 bh = &bhs;
813
814 first_group = page->index * blocks_per_page / 2;
815
816 /* read all groups the page covers into the cache */
817 for (i = 0; i < groups_per_page; i++) {
818 struct ext4_group_desc *desc;
819
Theodore Ts'o8df96752009-05-01 08:50:38 -0400820 if (first_group + i >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500821 break;
822
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400823 grinfo = ext4_get_group_info(sb, first_group + i);
824 /*
825 * If page is uptodate then we came here after online resize
826 * which added some new uninitialized group info structs, so
827 * we must skip all initialized uptodate buddies on the page,
828 * which may be currently in use by an allocating task.
829 */
830 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
831 bh[i] = NULL;
832 continue;
833 }
834
Alex Tomasc9de5602008-01-29 00:19:52 -0500835 err = -EIO;
836 desc = ext4_get_group_desc(sb, first_group + i, NULL);
837 if (desc == NULL)
838 goto out;
839
840 err = -ENOMEM;
841 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
842 if (bh[i] == NULL)
843 goto out;
844
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500845 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500846 continue;
847
Frederic Bohec806e682008-10-10 08:09:18 -0400848 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500849 if (bitmap_uptodate(bh[i])) {
850 unlock_buffer(bh[i]);
851 continue;
852 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400853 ext4_lock_group(sb, first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500854 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
855 ext4_init_block_bitmap(sb, bh[i],
856 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500857 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500858 set_buffer_uptodate(bh[i]);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400859 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500860 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500861 continue;
862 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400863 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500864 if (buffer_uptodate(bh[i])) {
865 /*
866 * if not uninit if bh is uptodate,
867 * bitmap is also uptodate
868 */
869 set_bitmap_uptodate(bh[i]);
870 unlock_buffer(bh[i]);
871 continue;
872 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500873 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500874 /*
875 * submit the buffer_head for read. We can
876 * safely mark the bitmap as uptodate now.
877 * We do it here so the bitmap uptodate bit
878 * get set with buffer lock held.
879 */
880 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500881 bh[i]->b_end_io = end_buffer_read_sync;
882 submit_bh(READ, bh[i]);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400883 mb_debug(1, "read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500884 }
885
886 /* wait for I/O completion */
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400887 for (i = 0; i < groups_per_page; i++)
888 if (bh[i])
889 wait_on_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500890
891 err = -EIO;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400892 for (i = 0; i < groups_per_page; i++)
893 if (bh[i] && !buffer_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500894 goto out;
895
Mingming Cao31b481d2008-07-11 19:27:31 -0400896 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500897 first_block = page->index * blocks_per_page;
898 for (i = 0; i < blocks_per_page; i++) {
899 int group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500900
901 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400902 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500903 break;
904
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400905 if (!bh[group - first_group])
906 /* skip initialized uptodate buddy */
907 continue;
908
Alex Tomasc9de5602008-01-29 00:19:52 -0500909 /*
910 * data carry information regarding this
911 * particular group in the format specified
912 * above
913 *
914 */
915 data = page_address(page) + (i * blocksize);
916 bitmap = bh[group - first_group]->b_data;
917
918 /*
919 * We place the buddy block and bitmap block
920 * close together
921 */
922 if ((first_block + i) & 1) {
923 /* this is block of buddy */
924 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400925 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500926 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400927 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500928 grinfo = ext4_get_group_info(sb, group);
929 grinfo->bb_fragments = 0;
930 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400931 sizeof(*grinfo->bb_counters) *
932 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500933 /*
934 * incore got set to the group block bitmap below
935 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500936 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400937 /* init the buddy */
938 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500939 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500940 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500941 incore = NULL;
942 } else {
943 /* this is block of bitmap */
944 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400945 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500946 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400947 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500948
949 /* see comments in ext4_mb_put_pa() */
950 ext4_lock_group(sb, group);
951 memcpy(data, bitmap, blocksize);
952
953 /* mark all preallocated blks used in in-core bitmap */
954 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500955 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500956 ext4_unlock_group(sb, group);
957
958 /* set incore so that the buddy information can be
959 * generated using this
960 */
961 incore = data;
962 }
963 }
964 SetPageUptodate(page);
965
966out:
967 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400968 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500969 brelse(bh[i]);
970 if (bh != &bhs)
971 kfree(bh);
972 }
973 return err;
974}
975
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400976/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400977 * Lock the buddy and bitmap pages. This make sure other parallel init_group
978 * on the same buddy page doesn't happen whild holding the buddy page lock.
979 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
980 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400981 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400982static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
983 ext4_group_t group, struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400984{
Amir Goldstein2de88072011-05-09 21:48:13 -0400985 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
986 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400987 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400988 struct page *page;
989
990 e4b->bd_buddy_page = NULL;
991 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400992
993 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
994 /*
995 * the buddy cache inode stores the block bitmap
996 * and buddy information in consecutive blocks.
997 * So for each group we need two blocks.
998 */
999 block = group * 2;
1000 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -04001001 poff = block % blocks_per_page;
1002 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1003 if (!page)
1004 return -EIO;
1005 BUG_ON(page->mapping != inode->i_mapping);
1006 e4b->bd_bitmap_page = page;
1007 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001008
Amir Goldstein2de88072011-05-09 21:48:13 -04001009 if (blocks_per_page >= 2) {
1010 /* buddy and bitmap are on the same page */
1011 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001012 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001013
1014 block++;
1015 pnum = block / blocks_per_page;
1016 poff = block % blocks_per_page;
1017 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1018 if (!page)
1019 return -EIO;
1020 BUG_ON(page->mapping != inode->i_mapping);
1021 e4b->bd_buddy_page = page;
1022 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001023}
1024
Amir Goldstein2de88072011-05-09 21:48:13 -04001025static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001026{
Amir Goldstein2de88072011-05-09 21:48:13 -04001027 if (e4b->bd_bitmap_page) {
1028 unlock_page(e4b->bd_bitmap_page);
1029 page_cache_release(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001030 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001031 if (e4b->bd_buddy_page) {
1032 unlock_page(e4b->bd_buddy_page);
1033 page_cache_release(e4b->bd_buddy_page);
1034 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001035}
1036
1037/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001038 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1039 * block group lock of all groups for this page; do not hold the BG lock when
1040 * calling this routine!
1041 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001042static noinline_for_stack
1043int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1044{
1045
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001046 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001047 struct ext4_buddy e4b;
1048 struct page *page;
1049 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001050
1051 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001052 this_grp = ext4_get_group_info(sb, group);
1053 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001054 * This ensures that we don't reinit the buddy cache
1055 * page which map to the group from which we are already
1056 * allocating. If we are looking at the buddy cache we would
1057 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001058 * would have pinned buddy page to page cache.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001059 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001060 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b);
1061 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001062 /*
1063 * somebody initialized the group
1064 * return without doing anything
1065 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001066 goto err;
1067 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001068
1069 page = e4b.bd_bitmap_page;
1070 ret = ext4_mb_init_cache(page, NULL);
1071 if (ret)
1072 goto err;
1073 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001074 ret = -EIO;
1075 goto err;
1076 }
1077 mark_page_accessed(page);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001078
Amir Goldstein2de88072011-05-09 21:48:13 -04001079 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001080 /*
1081 * If both the bitmap and buddy are in
1082 * the same page we don't need to force
1083 * init the buddy
1084 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001085 ret = 0;
1086 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001087 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001088 /* init buddy cache */
1089 page = e4b.bd_buddy_page;
1090 ret = ext4_mb_init_cache(page, e4b.bd_bitmap);
1091 if (ret)
1092 goto err;
1093 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001094 ret = -EIO;
1095 goto err;
1096 }
1097 mark_page_accessed(page);
1098err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001099 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001100 return ret;
1101}
1102
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001103/*
1104 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1105 * block group lock of all groups for this page; do not hold the BG lock when
1106 * calling this routine!
1107 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001108static noinline_for_stack int
1109ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1110 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001111{
Alex Tomasc9de5602008-01-29 00:19:52 -05001112 int blocks_per_page;
1113 int block;
1114 int pnum;
1115 int poff;
1116 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001117 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001118 struct ext4_group_info *grp;
1119 struct ext4_sb_info *sbi = EXT4_SB(sb);
1120 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001121
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001122 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001123
1124 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001125 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001126
1127 e4b->bd_blkbits = sb->s_blocksize_bits;
1128 e4b->bd_info = ext4_get_group_info(sb, group);
1129 e4b->bd_sb = sb;
1130 e4b->bd_group = group;
1131 e4b->bd_buddy_page = NULL;
1132 e4b->bd_bitmap_page = NULL;
1133
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001134 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001135 /*
1136 * we need full data about the group
1137 * to make a good selection
1138 */
1139 ret = ext4_mb_init_group(sb, group);
1140 if (ret)
1141 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001142 }
1143
Alex Tomasc9de5602008-01-29 00:19:52 -05001144 /*
1145 * the buddy cache inode stores the block bitmap
1146 * and buddy information in consecutive blocks.
1147 * So for each group we need two blocks.
1148 */
1149 block = group * 2;
1150 pnum = block / blocks_per_page;
1151 poff = block % blocks_per_page;
1152
1153 /* we could use find_or_create_page(), but it locks page
1154 * what we'd like to avoid in fast path ... */
1155 page = find_get_page(inode->i_mapping, pnum);
1156 if (page == NULL || !PageUptodate(page)) {
1157 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001158 /*
1159 * drop the page reference and try
1160 * to get the page with lock. If we
1161 * are not uptodate that implies
1162 * somebody just created the page but
1163 * is yet to initialize the same. So
1164 * wait for it to initialize.
1165 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001166 page_cache_release(page);
1167 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1168 if (page) {
1169 BUG_ON(page->mapping != inode->i_mapping);
1170 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001171 ret = ext4_mb_init_cache(page, NULL);
1172 if (ret) {
1173 unlock_page(page);
1174 goto err;
1175 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001176 mb_cmp_bitmaps(e4b, page_address(page) +
1177 (poff * sb->s_blocksize));
1178 }
1179 unlock_page(page);
1180 }
1181 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001182 if (page == NULL || !PageUptodate(page)) {
1183 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001184 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001185 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001186 e4b->bd_bitmap_page = page;
1187 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1188 mark_page_accessed(page);
1189
1190 block++;
1191 pnum = block / blocks_per_page;
1192 poff = block % blocks_per_page;
1193
1194 page = find_get_page(inode->i_mapping, pnum);
1195 if (page == NULL || !PageUptodate(page)) {
1196 if (page)
1197 page_cache_release(page);
1198 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1199 if (page) {
1200 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001201 if (!PageUptodate(page)) {
1202 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1203 if (ret) {
1204 unlock_page(page);
1205 goto err;
1206 }
1207 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001208 unlock_page(page);
1209 }
1210 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001211 if (page == NULL || !PageUptodate(page)) {
1212 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001213 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001214 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001215 e4b->bd_buddy_page = page;
1216 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1217 mark_page_accessed(page);
1218
1219 BUG_ON(e4b->bd_bitmap_page == NULL);
1220 BUG_ON(e4b->bd_buddy_page == NULL);
1221
1222 return 0;
1223
1224err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001225 if (page)
1226 page_cache_release(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001227 if (e4b->bd_bitmap_page)
1228 page_cache_release(e4b->bd_bitmap_page);
1229 if (e4b->bd_buddy_page)
1230 page_cache_release(e4b->bd_buddy_page);
1231 e4b->bd_buddy = NULL;
1232 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001233 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001234}
1235
Jing Zhange39e07f2010-05-14 00:00:00 -04001236static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001237{
1238 if (e4b->bd_bitmap_page)
1239 page_cache_release(e4b->bd_bitmap_page);
1240 if (e4b->bd_buddy_page)
1241 page_cache_release(e4b->bd_buddy_page);
1242}
1243
1244
1245static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1246{
1247 int order = 1;
1248 void *bb;
1249
1250 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1251 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1252
1253 bb = EXT4_MB_BUDDY(e4b);
1254 while (order <= e4b->bd_blkbits + 1) {
1255 block = block >> 1;
1256 if (!mb_test_bit(block, bb)) {
1257 /* this block is part of buddy of order 'order' */
1258 return order;
1259 }
1260 bb += 1 << (e4b->bd_blkbits - order);
1261 order++;
1262 }
1263 return 0;
1264}
1265
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001266static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001267{
1268 __u32 *addr;
1269
1270 len = cur + len;
1271 while (cur < len) {
1272 if ((cur & 31) == 0 && (len - cur) >= 32) {
1273 /* fast path: clear whole word at once */
1274 addr = bm + (cur >> 3);
1275 *addr = 0;
1276 cur += 32;
1277 continue;
1278 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001279 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001280 cur++;
1281 }
1282}
1283
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001284static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001285{
1286 __u32 *addr;
1287
1288 len = cur + len;
1289 while (cur < len) {
1290 if ((cur & 31) == 0 && (len - cur) >= 32) {
1291 /* fast path: set whole word at once */
1292 addr = bm + (cur >> 3);
1293 *addr = 0xffffffff;
1294 cur += 32;
1295 continue;
1296 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001297 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001298 cur++;
1299 }
1300}
1301
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001302static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001303 int first, int count)
1304{
1305 int block = 0;
1306 int max = 0;
1307 int order;
1308 void *buddy;
1309 void *buddy2;
1310 struct super_block *sb = e4b->bd_sb;
1311
1312 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001313 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001314 mb_check_buddy(e4b);
1315 mb_free_blocks_double(inode, e4b, first, count);
1316
1317 e4b->bd_info->bb_free += count;
1318 if (first < e4b->bd_info->bb_first_free)
1319 e4b->bd_info->bb_first_free = first;
1320
1321 /* let's maintain fragments counter */
1322 if (first != 0)
1323 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1324 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1325 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1326 if (block && max)
1327 e4b->bd_info->bb_fragments--;
1328 else if (!block && !max)
1329 e4b->bd_info->bb_fragments++;
1330
1331 /* let's maintain buddy itself */
1332 while (count-- > 0) {
1333 block = first++;
1334 order = 0;
1335
1336 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1337 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001338
1339 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001340 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001341 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001342 inode ? inode->i_ino : 0,
1343 blocknr,
1344 "freeing already freed block "
1345 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001346 }
1347 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1348 e4b->bd_info->bb_counters[order]++;
1349
1350 /* start of the buddy */
1351 buddy = mb_find_buddy(e4b, order, &max);
1352
1353 do {
1354 block &= ~1UL;
1355 if (mb_test_bit(block, buddy) ||
1356 mb_test_bit(block + 1, buddy))
1357 break;
1358
1359 /* both the buddies are free, try to coalesce them */
1360 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1361
1362 if (!buddy2)
1363 break;
1364
1365 if (order > 0) {
1366 /* for special purposes, we don't set
1367 * free bits in bitmap */
1368 mb_set_bit(block, buddy);
1369 mb_set_bit(block + 1, buddy);
1370 }
1371 e4b->bd_info->bb_counters[order]--;
1372 e4b->bd_info->bb_counters[order]--;
1373
1374 block = block >> 1;
1375 order++;
1376 e4b->bd_info->bb_counters[order]++;
1377
1378 mb_clear_bit(block, buddy2);
1379 buddy = buddy2;
1380 } while (1);
1381 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001382 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001383 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001384}
1385
1386static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1387 int needed, struct ext4_free_extent *ex)
1388{
1389 int next = block;
1390 int max;
1391 int ord;
1392 void *buddy;
1393
Vincent Minetbc8e6742009-05-15 08:33:18 -04001394 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001395 BUG_ON(ex == NULL);
1396
1397 buddy = mb_find_buddy(e4b, order, &max);
1398 BUG_ON(buddy == NULL);
1399 BUG_ON(block >= max);
1400 if (mb_test_bit(block, buddy)) {
1401 ex->fe_len = 0;
1402 ex->fe_start = 0;
1403 ex->fe_group = 0;
1404 return 0;
1405 }
1406
1407 /* FIXME dorp order completely ? */
1408 if (likely(order == 0)) {
1409 /* find actual order */
1410 order = mb_find_order_for_block(e4b, block);
1411 block = block >> order;
1412 }
1413
1414 ex->fe_len = 1 << order;
1415 ex->fe_start = block << order;
1416 ex->fe_group = e4b->bd_group;
1417
1418 /* calc difference from given start */
1419 next = next - ex->fe_start;
1420 ex->fe_len -= next;
1421 ex->fe_start += next;
1422
1423 while (needed > ex->fe_len &&
1424 (buddy = mb_find_buddy(e4b, order, &max))) {
1425
1426 if (block + 1 >= max)
1427 break;
1428
1429 next = (block + 1) * (1 << order);
1430 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1431 break;
1432
1433 ord = mb_find_order_for_block(e4b, next);
1434
1435 order = ord;
1436 block = next >> order;
1437 ex->fe_len += 1 << order;
1438 }
1439
1440 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1441 return ex->fe_len;
1442}
1443
1444static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1445{
1446 int ord;
1447 int mlen = 0;
1448 int max = 0;
1449 int cur;
1450 int start = ex->fe_start;
1451 int len = ex->fe_len;
1452 unsigned ret = 0;
1453 int len0 = len;
1454 void *buddy;
1455
1456 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1457 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001458 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001459 mb_check_buddy(e4b);
1460 mb_mark_used_double(e4b, start, len);
1461
1462 e4b->bd_info->bb_free -= len;
1463 if (e4b->bd_info->bb_first_free == start)
1464 e4b->bd_info->bb_first_free += len;
1465
1466 /* let's maintain fragments counter */
1467 if (start != 0)
1468 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1469 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1470 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1471 if (mlen && max)
1472 e4b->bd_info->bb_fragments++;
1473 else if (!mlen && !max)
1474 e4b->bd_info->bb_fragments--;
1475
1476 /* let's maintain buddy itself */
1477 while (len) {
1478 ord = mb_find_order_for_block(e4b, start);
1479
1480 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1481 /* the whole chunk may be allocated at once! */
1482 mlen = 1 << ord;
1483 buddy = mb_find_buddy(e4b, ord, &max);
1484 BUG_ON((start >> ord) >= max);
1485 mb_set_bit(start >> ord, buddy);
1486 e4b->bd_info->bb_counters[ord]--;
1487 start += mlen;
1488 len -= mlen;
1489 BUG_ON(len < 0);
1490 continue;
1491 }
1492
1493 /* store for history */
1494 if (ret == 0)
1495 ret = len | (ord << 16);
1496
1497 /* we have to split large buddy */
1498 BUG_ON(ord <= 0);
1499 buddy = mb_find_buddy(e4b, ord, &max);
1500 mb_set_bit(start >> ord, buddy);
1501 e4b->bd_info->bb_counters[ord]--;
1502
1503 ord--;
1504 cur = (start >> ord) & ~1U;
1505 buddy = mb_find_buddy(e4b, ord, &max);
1506 mb_clear_bit(cur, buddy);
1507 mb_clear_bit(cur + 1, buddy);
1508 e4b->bd_info->bb_counters[ord]++;
1509 e4b->bd_info->bb_counters[ord]++;
1510 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001511 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001512
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001513 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001514 mb_check_buddy(e4b);
1515
1516 return ret;
1517}
1518
1519/*
1520 * Must be called under group lock!
1521 */
1522static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1523 struct ext4_buddy *e4b)
1524{
1525 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1526 int ret;
1527
1528 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1529 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1530
1531 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1532 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1533 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1534
1535 /* preallocation can change ac_b_ex, thus we store actually
1536 * allocated blocks for history */
1537 ac->ac_f_ex = ac->ac_b_ex;
1538
1539 ac->ac_status = AC_STATUS_FOUND;
1540 ac->ac_tail = ret & 0xffff;
1541 ac->ac_buddy = ret >> 16;
1542
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001543 /*
1544 * take the page reference. We want the page to be pinned
1545 * so that we don't get a ext4_mb_init_cache_call for this
1546 * group until we update the bitmap. That would mean we
1547 * double allocate blocks. The reference is dropped
1548 * in ext4_mb_release_context
1549 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001550 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1551 get_page(ac->ac_bitmap_page);
1552 ac->ac_buddy_page = e4b->bd_buddy_page;
1553 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001554 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001555 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001556 spin_lock(&sbi->s_md_lock);
1557 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1558 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1559 spin_unlock(&sbi->s_md_lock);
1560 }
1561}
1562
1563/*
1564 * regular allocator, for general purposes allocation
1565 */
1566
1567static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1568 struct ext4_buddy *e4b,
1569 int finish_group)
1570{
1571 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1572 struct ext4_free_extent *bex = &ac->ac_b_ex;
1573 struct ext4_free_extent *gex = &ac->ac_g_ex;
1574 struct ext4_free_extent ex;
1575 int max;
1576
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001577 if (ac->ac_status == AC_STATUS_FOUND)
1578 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001579 /*
1580 * We don't want to scan for a whole year
1581 */
1582 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1583 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1584 ac->ac_status = AC_STATUS_BREAK;
1585 return;
1586 }
1587
1588 /*
1589 * Haven't found good chunk so far, let's continue
1590 */
1591 if (bex->fe_len < gex->fe_len)
1592 return;
1593
1594 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1595 && bex->fe_group == e4b->bd_group) {
1596 /* recheck chunk's availability - we don't know
1597 * when it was found (within this lock-unlock
1598 * period or not) */
1599 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1600 if (max >= gex->fe_len) {
1601 ext4_mb_use_best_found(ac, e4b);
1602 return;
1603 }
1604 }
1605}
1606
1607/*
1608 * The routine checks whether found extent is good enough. If it is,
1609 * then the extent gets marked used and flag is set to the context
1610 * to stop scanning. Otherwise, the extent is compared with the
1611 * previous found extent and if new one is better, then it's stored
1612 * in the context. Later, the best found extent will be used, if
1613 * mballoc can't find good enough extent.
1614 *
1615 * FIXME: real allocation policy is to be designed yet!
1616 */
1617static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1618 struct ext4_free_extent *ex,
1619 struct ext4_buddy *e4b)
1620{
1621 struct ext4_free_extent *bex = &ac->ac_b_ex;
1622 struct ext4_free_extent *gex = &ac->ac_g_ex;
1623
1624 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001625 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001626 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1627 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1628
1629 ac->ac_found++;
1630
1631 /*
1632 * The special case - take what you catch first
1633 */
1634 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1635 *bex = *ex;
1636 ext4_mb_use_best_found(ac, e4b);
1637 return;
1638 }
1639
1640 /*
1641 * Let's check whether the chuck is good enough
1642 */
1643 if (ex->fe_len == gex->fe_len) {
1644 *bex = *ex;
1645 ext4_mb_use_best_found(ac, e4b);
1646 return;
1647 }
1648
1649 /*
1650 * If this is first found extent, just store it in the context
1651 */
1652 if (bex->fe_len == 0) {
1653 *bex = *ex;
1654 return;
1655 }
1656
1657 /*
1658 * If new found extent is better, store it in the context
1659 */
1660 if (bex->fe_len < gex->fe_len) {
1661 /* if the request isn't satisfied, any found extent
1662 * larger than previous best one is better */
1663 if (ex->fe_len > bex->fe_len)
1664 *bex = *ex;
1665 } else if (ex->fe_len > gex->fe_len) {
1666 /* if the request is satisfied, then we try to find
1667 * an extent that still satisfy the request, but is
1668 * smaller than previous one */
1669 if (ex->fe_len < bex->fe_len)
1670 *bex = *ex;
1671 }
1672
1673 ext4_mb_check_limits(ac, e4b, 0);
1674}
1675
Eric Sandeen089ceec2009-07-05 22:17:31 -04001676static noinline_for_stack
1677int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001678 struct ext4_buddy *e4b)
1679{
1680 struct ext4_free_extent ex = ac->ac_b_ex;
1681 ext4_group_t group = ex.fe_group;
1682 int max;
1683 int err;
1684
1685 BUG_ON(ex.fe_len <= 0);
1686 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1687 if (err)
1688 return err;
1689
1690 ext4_lock_group(ac->ac_sb, group);
1691 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1692
1693 if (max > 0) {
1694 ac->ac_b_ex = ex;
1695 ext4_mb_use_best_found(ac, e4b);
1696 }
1697
1698 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001699 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001700
1701 return 0;
1702}
1703
Eric Sandeen089ceec2009-07-05 22:17:31 -04001704static noinline_for_stack
1705int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001706 struct ext4_buddy *e4b)
1707{
1708 ext4_group_t group = ac->ac_g_ex.fe_group;
1709 int max;
1710 int err;
1711 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001712 struct ext4_free_extent ex;
1713
1714 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1715 return 0;
1716
1717 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1718 if (err)
1719 return err;
1720
1721 ext4_lock_group(ac->ac_sb, group);
1722 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1723 ac->ac_g_ex.fe_len, &ex);
1724
1725 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1726 ext4_fsblk_t start;
1727
Akinobu Mita5661bd62010-03-03 23:53:39 -05001728 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1729 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001730 /* use do_div to get remainder (would be 64-bit modulo) */
1731 if (do_div(start, sbi->s_stripe) == 0) {
1732 ac->ac_found++;
1733 ac->ac_b_ex = ex;
1734 ext4_mb_use_best_found(ac, e4b);
1735 }
1736 } else if (max >= ac->ac_g_ex.fe_len) {
1737 BUG_ON(ex.fe_len <= 0);
1738 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1739 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1740 ac->ac_found++;
1741 ac->ac_b_ex = ex;
1742 ext4_mb_use_best_found(ac, e4b);
1743 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1744 /* Sometimes, caller may want to merge even small
1745 * number of blocks to an existing extent */
1746 BUG_ON(ex.fe_len <= 0);
1747 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1748 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1749 ac->ac_found++;
1750 ac->ac_b_ex = ex;
1751 ext4_mb_use_best_found(ac, e4b);
1752 }
1753 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001754 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001755
1756 return 0;
1757}
1758
1759/*
1760 * The routine scans buddy structures (not bitmap!) from given order
1761 * to max order and tries to find big enough chunk to satisfy the req
1762 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001763static noinline_for_stack
1764void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001765 struct ext4_buddy *e4b)
1766{
1767 struct super_block *sb = ac->ac_sb;
1768 struct ext4_group_info *grp = e4b->bd_info;
1769 void *buddy;
1770 int i;
1771 int k;
1772 int max;
1773
1774 BUG_ON(ac->ac_2order <= 0);
1775 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1776 if (grp->bb_counters[i] == 0)
1777 continue;
1778
1779 buddy = mb_find_buddy(e4b, i, &max);
1780 BUG_ON(buddy == NULL);
1781
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001782 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001783 BUG_ON(k >= max);
1784
1785 ac->ac_found++;
1786
1787 ac->ac_b_ex.fe_len = 1 << i;
1788 ac->ac_b_ex.fe_start = k << i;
1789 ac->ac_b_ex.fe_group = e4b->bd_group;
1790
1791 ext4_mb_use_best_found(ac, e4b);
1792
1793 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1794
1795 if (EXT4_SB(sb)->s_mb_stats)
1796 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1797
1798 break;
1799 }
1800}
1801
1802/*
1803 * The routine scans the group and measures all found extents.
1804 * In order to optimize scanning, caller must pass number of
1805 * free blocks in the group, so the routine can know upper limit.
1806 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001807static noinline_for_stack
1808void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001809 struct ext4_buddy *e4b)
1810{
1811 struct super_block *sb = ac->ac_sb;
1812 void *bitmap = EXT4_MB_BITMAP(e4b);
1813 struct ext4_free_extent ex;
1814 int i;
1815 int free;
1816
1817 free = e4b->bd_info->bb_free;
1818 BUG_ON(free <= 0);
1819
1820 i = e4b->bd_info->bb_first_free;
1821
1822 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001823 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001824 EXT4_BLOCKS_PER_GROUP(sb), i);
1825 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001826 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001827 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001828 * free blocks even though group info says we
1829 * we have free blocks
1830 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001831 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1832 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001833 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001834 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001835 break;
1836 }
1837
1838 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1839 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001840 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001841 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1842 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001843 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001844 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001845 /*
1846 * The number of free blocks differs. This mostly
1847 * indicate that the bitmap is corrupt. So exit
1848 * without claiming the space.
1849 */
1850 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001851 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001852
1853 ext4_mb_measure_extent(ac, &ex, e4b);
1854
1855 i += ex.fe_len;
1856 free -= ex.fe_len;
1857 }
1858
1859 ext4_mb_check_limits(ac, e4b, 1);
1860}
1861
1862/*
1863 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001864 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001865 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001866static noinline_for_stack
1867void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001868 struct ext4_buddy *e4b)
1869{
1870 struct super_block *sb = ac->ac_sb;
1871 struct ext4_sb_info *sbi = EXT4_SB(sb);
1872 void *bitmap = EXT4_MB_BITMAP(e4b);
1873 struct ext4_free_extent ex;
1874 ext4_fsblk_t first_group_block;
1875 ext4_fsblk_t a;
1876 ext4_grpblk_t i;
1877 int max;
1878
1879 BUG_ON(sbi->s_stripe == 0);
1880
1881 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001882 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1883
Alex Tomasc9de5602008-01-29 00:19:52 -05001884 a = first_group_block + sbi->s_stripe - 1;
1885 do_div(a, sbi->s_stripe);
1886 i = (a * sbi->s_stripe) - first_group_block;
1887
1888 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1889 if (!mb_test_bit(i, bitmap)) {
1890 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1891 if (max >= sbi->s_stripe) {
1892 ac->ac_found++;
1893 ac->ac_b_ex = ex;
1894 ext4_mb_use_best_found(ac, e4b);
1895 break;
1896 }
1897 }
1898 i += sbi->s_stripe;
1899 }
1900}
1901
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001902/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001903static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1904 ext4_group_t group, int cr)
1905{
1906 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001907 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001908 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1909
1910 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001911
1912 /* We only do this if the grp has never been initialized */
1913 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1914 int ret = ext4_mb_init_group(ac->ac_sb, group);
1915 if (ret)
1916 return 0;
1917 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001918
1919 free = grp->bb_free;
1920 fragments = grp->bb_fragments;
1921 if (free == 0)
1922 return 0;
1923 if (fragments == 0)
1924 return 0;
1925
1926 switch (cr) {
1927 case 0:
1928 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001929
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001930 if (grp->bb_largest_free_order < ac->ac_2order)
1931 return 0;
1932
Theodore Ts'oa4912122009-03-12 12:18:34 -04001933 /* Avoid using the first bg of a flexgroup for data files */
1934 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1935 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1936 ((group % flex_size) == 0))
1937 return 0;
1938
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001939 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001940 case 1:
1941 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1942 return 1;
1943 break;
1944 case 2:
1945 if (free >= ac->ac_g_ex.fe_len)
1946 return 1;
1947 break;
1948 case 3:
1949 return 1;
1950 default:
1951 BUG();
1952 }
1953
1954 return 0;
1955}
1956
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001957static noinline_for_stack int
1958ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001959{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001960 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001961 int cr;
1962 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001963 struct ext4_sb_info *sbi;
1964 struct super_block *sb;
1965 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001966
1967 sb = ac->ac_sb;
1968 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001969 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001970 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001971 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001972 ngroups = sbi->s_blockfile_groups;
1973
Alex Tomasc9de5602008-01-29 00:19:52 -05001974 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1975
1976 /* first, try the goal */
1977 err = ext4_mb_find_by_goal(ac, &e4b);
1978 if (err || ac->ac_status == AC_STATUS_FOUND)
1979 goto out;
1980
1981 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1982 goto out;
1983
1984 /*
1985 * ac->ac2_order is set only if the fe_len is a power of 2
1986 * if ac2_order is set we also set criteria to 0 so that we
1987 * try exact allocation using buddy.
1988 */
1989 i = fls(ac->ac_g_ex.fe_len);
1990 ac->ac_2order = 0;
1991 /*
1992 * We search using buddy data only if the order of the request
1993 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04001994 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05001995 */
1996 if (i >= sbi->s_mb_order2_reqs) {
1997 /*
1998 * This should tell if fe_len is exactly power of 2
1999 */
2000 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2001 ac->ac_2order = i - 1;
2002 }
2003
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002004 /* if stream allocation is enabled, use global goal */
2005 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002006 /* TBD: may be hot point */
2007 spin_lock(&sbi->s_md_lock);
2008 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2009 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2010 spin_unlock(&sbi->s_md_lock);
2011 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002012
Alex Tomasc9de5602008-01-29 00:19:52 -05002013 /* Let's just scan groups to find more-less suitable blocks */
2014 cr = ac->ac_2order ? 0 : 1;
2015 /*
2016 * cr == 0 try to get exact allocation,
2017 * cr == 3 try to get anything
2018 */
2019repeat:
2020 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2021 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002022 /*
2023 * searching for the right group start
2024 * from the goal value specified
2025 */
2026 group = ac->ac_g_ex.fe_group;
2027
Theodore Ts'o8df96752009-05-01 08:50:38 -04002028 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002029 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002030 group = 0;
2031
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002032 /* This now checks without needing the buddy page */
2033 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002034 continue;
2035
Alex Tomasc9de5602008-01-29 00:19:52 -05002036 err = ext4_mb_load_buddy(sb, group, &e4b);
2037 if (err)
2038 goto out;
2039
2040 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002041
2042 /*
2043 * We need to check again after locking the
2044 * block group
2045 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002046 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002047 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002048 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002049 continue;
2050 }
2051
2052 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002053 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002054 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002055 else if (cr == 1 && sbi->s_stripe &&
2056 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002057 ext4_mb_scan_aligned(ac, &e4b);
2058 else
2059 ext4_mb_complex_scan_group(ac, &e4b);
2060
2061 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002062 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002063
2064 if (ac->ac_status != AC_STATUS_CONTINUE)
2065 break;
2066 }
2067 }
2068
2069 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2070 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2071 /*
2072 * We've been searching too long. Let's try to allocate
2073 * the best chunk we've found so far
2074 */
2075
2076 ext4_mb_try_best_found(ac, &e4b);
2077 if (ac->ac_status != AC_STATUS_FOUND) {
2078 /*
2079 * Someone more lucky has already allocated it.
2080 * The only thing we can do is just take first
2081 * found block(s)
2082 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2083 */
2084 ac->ac_b_ex.fe_group = 0;
2085 ac->ac_b_ex.fe_start = 0;
2086 ac->ac_b_ex.fe_len = 0;
2087 ac->ac_status = AC_STATUS_CONTINUE;
2088 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2089 cr = 3;
2090 atomic_inc(&sbi->s_mb_lost_chunks);
2091 goto repeat;
2092 }
2093 }
2094out:
2095 return err;
2096}
2097
Alex Tomasc9de5602008-01-29 00:19:52 -05002098static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2099{
2100 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002101 ext4_group_t group;
2102
Theodore Ts'o8df96752009-05-01 08:50:38 -04002103 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002104 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002105 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002106 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002107}
2108
2109static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2110{
2111 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002112 ext4_group_t group;
2113
2114 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002115 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002116 return NULL;
2117 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002118 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002119}
2120
2121static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2122{
2123 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002124 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002125 int i;
2126 int err;
2127 struct ext4_buddy e4b;
2128 struct sg {
2129 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002130 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002131 } sg;
2132
2133 group--;
2134 if (group == 0)
2135 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2136 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2137 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2138 "group", "free", "frags", "first",
2139 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2140 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2141
2142 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2143 sizeof(struct ext4_group_info);
2144 err = ext4_mb_load_buddy(sb, group, &e4b);
2145 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002146 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002147 return 0;
2148 }
2149 ext4_lock_group(sb, group);
2150 memcpy(&sg, ext4_get_group_info(sb, group), i);
2151 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002152 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002153
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002154 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002155 sg.info.bb_fragments, sg.info.bb_first_free);
2156 for (i = 0; i <= 13; i++)
2157 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2158 sg.info.bb_counters[i] : 0);
2159 seq_printf(seq, " ]\n");
2160
2161 return 0;
2162}
2163
2164static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2165{
2166}
2167
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002168static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002169 .start = ext4_mb_seq_groups_start,
2170 .next = ext4_mb_seq_groups_next,
2171 .stop = ext4_mb_seq_groups_stop,
2172 .show = ext4_mb_seq_groups_show,
2173};
2174
2175static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2176{
2177 struct super_block *sb = PDE(inode)->data;
2178 int rc;
2179
2180 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2181 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002182 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002183 m->private = sb;
2184 }
2185 return rc;
2186
2187}
2188
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002189static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002190 .owner = THIS_MODULE,
2191 .open = ext4_mb_seq_groups_open,
2192 .read = seq_read,
2193 .llseek = seq_lseek,
2194 .release = seq_release,
2195};
2196
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002197static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2198{
2199 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2200 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2201
2202 BUG_ON(!cachep);
2203 return cachep;
2204}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002205
2206/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002207int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002208 struct ext4_group_desc *desc)
2209{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002210 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002211 int metalen = 0;
2212 struct ext4_sb_info *sbi = EXT4_SB(sb);
2213 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002214 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002215
2216 /*
2217 * First check if this group is the first of a reserved block.
2218 * If it's true, we have to allocate a new table of pointers
2219 * to ext4_group_info structures
2220 */
2221 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2222 metalen = sizeof(*meta_group_info) <<
2223 EXT4_DESC_PER_BLOCK_BITS(sb);
2224 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2225 if (meta_group_info == NULL) {
2226 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2227 "buddy group\n");
2228 goto exit_meta_group_info;
2229 }
2230 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2231 meta_group_info;
2232 }
2233
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002234 meta_group_info =
2235 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2236 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2237
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002238 meta_group_info[i] = kmem_cache_alloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002239 if (meta_group_info[i] == NULL) {
2240 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2241 goto exit_group_info;
2242 }
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002243 memset(meta_group_info[i], 0, kmem_cache_size(cachep));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002244 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2245 &(meta_group_info[i]->bb_state));
2246
2247 /*
2248 * initialize bb_free to be able to skip
2249 * empty groups without initialization
2250 */
2251 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2252 meta_group_info[i]->bb_free =
2253 ext4_free_blocks_after_init(sb, group, desc);
2254 } else {
2255 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002256 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002257 }
2258
2259 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002260 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002261 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002262 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002263
2264#ifdef DOUBLE_CHECK
2265 {
2266 struct buffer_head *bh;
2267 meta_group_info[i]->bb_bitmap =
2268 kmalloc(sb->s_blocksize, GFP_KERNEL);
2269 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2270 bh = ext4_read_block_bitmap(sb, group);
2271 BUG_ON(bh == NULL);
2272 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2273 sb->s_blocksize);
2274 put_bh(bh);
2275 }
2276#endif
2277
2278 return 0;
2279
2280exit_group_info:
2281 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002282 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002283 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002284 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2285 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002286exit_meta_group_info:
2287 return -ENOMEM;
2288} /* ext4_mb_add_groupinfo */
2289
Alex Tomasc9de5602008-01-29 00:19:52 -05002290static int ext4_mb_init_backend(struct super_block *sb)
2291{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002292 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002293 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002294 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002295 struct ext4_super_block *es = sbi->s_es;
2296 int num_meta_group_infos;
2297 int num_meta_group_infos_max;
2298 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002299 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002300 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002301
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002302 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002303 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002304 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2305
2306 /*
2307 * This is the total number of blocks used by GDT including
2308 * the number of reserved blocks for GDT.
2309 * The s_group_info array is allocated with this value
2310 * to allow a clean online resize without a complex
2311 * manipulation of pointer.
2312 * The drawback is the unused memory when no resize
2313 * occurs but it's very low in terms of pages
2314 * (see comments below)
2315 * Need to handle this properly when META_BG resizing is allowed
2316 */
2317 num_meta_group_infos_max = num_meta_group_infos +
2318 le16_to_cpu(es->s_reserved_gdt_blocks);
2319
2320 /*
2321 * array_size is the size of s_group_info array. We round it
2322 * to the next power of two because this approximation is done
2323 * internally by kmalloc so we can have some more memory
2324 * for free here (e.g. may be used for META_BG resize).
2325 */
2326 array_size = 1;
2327 while (array_size < sizeof(*sbi->s_group_info) *
2328 num_meta_group_infos_max)
2329 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002330 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2331 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2332 * So a two level scheme suffices for now. */
Eric Sandeen4596fe02011-03-21 21:25:13 -04002333 sbi->s_group_info = kzalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002334 if (sbi->s_group_info == NULL) {
2335 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2336 return -ENOMEM;
2337 }
2338 sbi->s_buddy_cache = new_inode(sb);
2339 if (sbi->s_buddy_cache == NULL) {
2340 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2341 goto err_freesgi;
2342 }
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002343 sbi->s_buddy_cache->i_ino = get_next_ino();
Alex Tomasc9de5602008-01-29 00:19:52 -05002344 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002345 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002346 desc = ext4_get_group_desc(sb, i, NULL);
2347 if (desc == NULL) {
2348 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002349 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002350 goto err_freebuddy;
2351 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002352 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2353 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002354 }
2355
2356 return 0;
2357
2358err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002359 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002360 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002361 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002362 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002363 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002364 kfree(sbi->s_group_info[i]);
2365 iput(sbi->s_buddy_cache);
2366err_freesgi:
2367 kfree(sbi->s_group_info);
2368 return -ENOMEM;
2369}
2370
Eric Sandeen2892c152011-02-12 08:12:18 -05002371static void ext4_groupinfo_destroy_slabs(void)
2372{
2373 int i;
2374
2375 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2376 if (ext4_groupinfo_caches[i])
2377 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2378 ext4_groupinfo_caches[i] = NULL;
2379 }
2380}
2381
2382static int ext4_groupinfo_create_slab(size_t size)
2383{
2384 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2385 int slab_size;
2386 int blocksize_bits = order_base_2(size);
2387 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2388 struct kmem_cache *cachep;
2389
2390 if (cache_index >= NR_GRPINFO_CACHES)
2391 return -EINVAL;
2392
2393 if (unlikely(cache_index < 0))
2394 cache_index = 0;
2395
2396 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2397 if (ext4_groupinfo_caches[cache_index]) {
2398 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2399 return 0; /* Already created */
2400 }
2401
2402 slab_size = offsetof(struct ext4_group_info,
2403 bb_counters[blocksize_bits + 2]);
2404
2405 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2406 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2407 NULL);
2408
Tao Ma823ba012011-07-11 18:26:01 -04002409 ext4_groupinfo_caches[cache_index] = cachep;
2410
Eric Sandeen2892c152011-02-12 08:12:18 -05002411 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2412 if (!cachep) {
2413 printk(KERN_EMERG "EXT4: no memory for groupinfo slab cache\n");
2414 return -ENOMEM;
2415 }
2416
Eric Sandeen2892c152011-02-12 08:12:18 -05002417 return 0;
2418}
2419
Alex Tomasc9de5602008-01-29 00:19:52 -05002420int ext4_mb_init(struct super_block *sb, int needs_recovery)
2421{
2422 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002423 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002424 unsigned offset;
2425 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002426 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002427
Eric Sandeen19278052009-08-25 22:36:25 -04002428 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002429
2430 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2431 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002432 ret = -ENOMEM;
2433 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002434 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002435
Eric Sandeen19278052009-08-25 22:36:25 -04002436 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002437 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2438 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002439 ret = -ENOMEM;
2440 goto out;
2441 }
2442
Eric Sandeen2892c152011-02-12 08:12:18 -05002443 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2444 if (ret < 0)
2445 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002446
2447 /* order 0 is regular bitmap */
2448 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2449 sbi->s_mb_offsets[0] = 0;
2450
2451 i = 1;
2452 offset = 0;
2453 max = sb->s_blocksize << 2;
2454 do {
2455 sbi->s_mb_offsets[i] = offset;
2456 sbi->s_mb_maxs[i] = max;
2457 offset += 1 << (sb->s_blocksize_bits - i);
2458 max = max >> 1;
2459 i++;
2460 } while (i <= sb->s_blocksize_bits + 1);
2461
2462 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002463 ret = ext4_mb_init_backend(sb);
2464 if (ret != 0) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002465 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002466 }
2467
2468 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002469 spin_lock_init(&sbi->s_bal_lock);
2470
2471 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2472 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2473 sbi->s_mb_stats = MB_DEFAULT_STATS;
2474 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2475 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002476 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2477
Eric Sandeen730c2132008-09-13 15:23:29 -04002478 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002479 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002480 ret = -ENOMEM;
2481 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002482 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002483 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002484 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002485 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002486 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002487 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2488 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002489 spin_lock_init(&lg->lg_prealloc_lock);
2490 }
2491
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
Frank Mayhar03901312009-01-07 00:06:22 -05002496 if (sbi->s_journal)
2497 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002498out:
2499 if (ret) {
2500 kfree(sbi->s_mb_offsets);
2501 kfree(sbi->s_mb_maxs);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002502 }
2503 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002504}
2505
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002506/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002507static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2508{
2509 struct ext4_prealloc_space *pa;
2510 struct list_head *cur, *tmp;
2511 int count = 0;
2512
2513 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2514 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2515 list_del(&pa->pa_group_list);
2516 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002517 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002518 }
2519 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002520 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002521
2522}
2523
2524int ext4_mb_release(struct super_block *sb)
2525{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002526 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002527 ext4_group_t i;
2528 int num_meta_group_infos;
2529 struct ext4_group_info *grinfo;
2530 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002531 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002532
Alex Tomasc9de5602008-01-29 00:19:52 -05002533 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002534 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002535 grinfo = ext4_get_group_info(sb, i);
2536#ifdef DOUBLE_CHECK
2537 kfree(grinfo->bb_bitmap);
2538#endif
2539 ext4_lock_group(sb, i);
2540 ext4_mb_cleanup_pa(grinfo);
2541 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002542 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002543 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002544 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002545 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2546 EXT4_DESC_PER_BLOCK_BITS(sb);
2547 for (i = 0; i < num_meta_group_infos; i++)
2548 kfree(sbi->s_group_info[i]);
2549 kfree(sbi->s_group_info);
2550 }
2551 kfree(sbi->s_mb_offsets);
2552 kfree(sbi->s_mb_maxs);
2553 if (sbi->s_buddy_cache)
2554 iput(sbi->s_buddy_cache);
2555 if (sbi->s_mb_stats) {
2556 printk(KERN_INFO
2557 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2558 atomic_read(&sbi->s_bal_allocated),
2559 atomic_read(&sbi->s_bal_reqs),
2560 atomic_read(&sbi->s_bal_success));
2561 printk(KERN_INFO
2562 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2563 "%u 2^N hits, %u breaks, %u lost\n",
2564 atomic_read(&sbi->s_bal_ex_scanned),
2565 atomic_read(&sbi->s_bal_goals),
2566 atomic_read(&sbi->s_bal_2orders),
2567 atomic_read(&sbi->s_bal_breaks),
2568 atomic_read(&sbi->s_mb_lost_chunks));
2569 printk(KERN_INFO
2570 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2571 sbi->s_mb_buddies_generated++,
2572 sbi->s_mb_generation_time);
2573 printk(KERN_INFO
2574 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2575 atomic_read(&sbi->s_mb_preallocated),
2576 atomic_read(&sbi->s_mb_discarded));
2577 }
2578
Eric Sandeen730c2132008-09-13 15:23:29 -04002579 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002580 if (sbi->s_proc)
2581 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002582
2583 return 0;
2584}
2585
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002586static inline int ext4_issue_discard(struct super_block *sb,
Jiaying Zhang5c521832010-07-27 11:56:05 -04002587 ext4_group_t block_group, ext4_grpblk_t block, int count)
2588{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002589 ext4_fsblk_t discard_block;
2590
2591 discard_block = block + ext4_group_first_block_no(sb, block_group);
2592 trace_ext4_discard_blocks(sb,
2593 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002594 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002595}
2596
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002597/*
2598 * This function is called by the jbd2 layer once the commit has finished,
2599 * so we know we can free the blocks that were released with that commit.
2600 */
2601static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002602{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002603 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002604 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002605 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002606 int err, count = 0, count2 = 0;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002607 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002608 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002609
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002610 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2611 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002612
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002613 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002614 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002615
Theodore Ts'od9f34502011-04-30 13:47:24 -04002616 if (test_opt(sb, DISCARD))
2617 ext4_issue_discard(sb, entry->group,
2618 entry->start_blk, entry->count);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002619
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002620 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002621 /* we expect to find existing buddy because it's pinned */
2622 BUG_ON(err != 0);
2623
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002624 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002625 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002626 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002627 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002628 ext4_lock_group(sb, entry->group);
2629 /* Take it out of per group rb tree */
2630 rb_erase(&entry->node, &(db->bb_free_root));
2631 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2632
Tao Ma3d56b8d2011-07-11 00:03:38 -04002633 /*
2634 * Clear the trimmed flag for the group so that the next
2635 * ext4_trim_fs can trim it.
2636 * If the volume is mounted with -o discard, online discard
2637 * is supported and the free blocks will be trimmed online.
2638 */
2639 if (!test_opt(sb, DISCARD))
2640 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2641
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002642 if (!db->bb_free_root.rb_node) {
2643 /* No more items in the per group rb tree
2644 * balance refcounts from ext4_mb_free_metadata()
2645 */
2646 page_cache_release(e4b.bd_buddy_page);
2647 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002648 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002649 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002650 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002651 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002652 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002653
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002654 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002655}
2656
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002657#ifdef CONFIG_EXT4_DEBUG
2658u8 mb_enable_debug __read_mostly;
2659
2660static struct dentry *debugfs_dir;
2661static struct dentry *debugfs_debug;
2662
2663static void __init ext4_create_debugfs_entry(void)
2664{
2665 debugfs_dir = debugfs_create_dir("ext4", NULL);
2666 if (debugfs_dir)
2667 debugfs_debug = debugfs_create_u8("mballoc-debug",
2668 S_IRUGO | S_IWUSR,
2669 debugfs_dir,
2670 &mb_enable_debug);
2671}
2672
2673static void ext4_remove_debugfs_entry(void)
2674{
2675 debugfs_remove(debugfs_debug);
2676 debugfs_remove(debugfs_dir);
2677}
2678
2679#else
2680
2681static void __init ext4_create_debugfs_entry(void)
2682{
2683}
2684
2685static void ext4_remove_debugfs_entry(void)
2686{
2687}
2688
2689#endif
2690
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002691int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002692{
Theodore Ts'o16828082010-10-27 21:30:09 -04002693 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2694 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002695 if (ext4_pspace_cachep == NULL)
2696 return -ENOMEM;
2697
Theodore Ts'o16828082010-10-27 21:30:09 -04002698 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2699 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002700 if (ext4_ac_cachep == NULL) {
2701 kmem_cache_destroy(ext4_pspace_cachep);
2702 return -ENOMEM;
2703 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002704
Theodore Ts'o16828082010-10-27 21:30:09 -04002705 ext4_free_ext_cachep = KMEM_CACHE(ext4_free_data,
2706 SLAB_RECLAIM_ACCOUNT);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002707 if (ext4_free_ext_cachep == NULL) {
2708 kmem_cache_destroy(ext4_pspace_cachep);
2709 kmem_cache_destroy(ext4_ac_cachep);
2710 return -ENOMEM;
2711 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002712 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002713 return 0;
2714}
2715
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002716void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002717{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002718 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002719 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2720 * before destroying the slab cache.
2721 */
2722 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002723 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002724 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002725 kmem_cache_destroy(ext4_free_ext_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002726 ext4_groupinfo_destroy_slabs();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002727 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002728}
2729
2730
2731/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002732 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002733 * Returns 0 if success or error code
2734 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002735static noinline_for_stack int
2736ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002737 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002738{
2739 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002740 struct ext4_group_desc *gdp;
2741 struct buffer_head *gdp_bh;
2742 struct ext4_sb_info *sbi;
2743 struct super_block *sb;
2744 ext4_fsblk_t block;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002745 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002746
2747 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2748 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2749
2750 sb = ac->ac_sb;
2751 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002752
2753 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002754 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002755 if (!bitmap_bh)
2756 goto out_err;
2757
2758 err = ext4_journal_get_write_access(handle, bitmap_bh);
2759 if (err)
2760 goto out_err;
2761
2762 err = -EIO;
2763 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2764 if (!gdp)
2765 goto out_err;
2766
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002767 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002768 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002769
Alex Tomasc9de5602008-01-29 00:19:52 -05002770 err = ext4_journal_get_write_access(handle, gdp_bh);
2771 if (err)
2772 goto out_err;
2773
Akinobu Mitabda00de2010-03-03 23:53:25 -05002774 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002775
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002776 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002777 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002778 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002779 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002780 /* File system mounted not to panic on error
2781 * Fix the bitmap and repeat the block allocation
2782 * We leak some of the blocks here.
2783 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002784 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2785 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2786 ac->ac_b_ex.fe_len);
2787 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002788 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002789 if (!err)
2790 err = -EAGAIN;
2791 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002792 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002793
2794 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002795#ifdef AGGRESSIVE_CHECK
2796 {
2797 int i;
2798 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2799 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2800 bitmap_bh->b_data));
2801 }
2802 }
2803#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002804 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002805 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2806 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002807 ext4_free_blks_set(sb, gdp,
2808 ext4_free_blocks_after_init(sb,
2809 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002810 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002811 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2812 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002813 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002814
2815 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002816 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002817 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002818 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002819 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002820 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2821 /* release all the reserved blocks if non delalloc */
2822 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002823
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002824 if (sbi->s_log_groups_per_flex) {
2825 ext4_group_t flex_group = ext4_flex_group(sbi,
2826 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002827 atomic_sub(ac->ac_b_ex.fe_len,
2828 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002829 }
2830
Frank Mayhar03901312009-01-07 00:06:22 -05002831 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002832 if (err)
2833 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002834 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002835
2836out_err:
Theodore Ts'oa0375152010-06-11 23:14:04 -04002837 ext4_mark_super_dirty(sb);
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002838 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002839 return err;
2840}
2841
2842/*
2843 * here we normalize request for locality group
2844 * Group request are normalized to s_strip size if we set the same via mount
2845 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002846 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002847 *
2848 * XXX: should we try to preallocate more than the group has now?
2849 */
2850static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2851{
2852 struct super_block *sb = ac->ac_sb;
2853 struct ext4_locality_group *lg = ac->ac_lg;
2854
2855 BUG_ON(lg == NULL);
2856 if (EXT4_SB(sb)->s_stripe)
2857 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2858 else
2859 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002860 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002861 current->pid, ac->ac_g_ex.fe_len);
2862}
2863
2864/*
2865 * Normalization means making request better in terms of
2866 * size and alignment
2867 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002868static noinline_for_stack void
2869ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002870 struct ext4_allocation_request *ar)
2871{
2872 int bsbits, max;
2873 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002874 loff_t size, orig_size, start_off;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002875 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002876 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002877 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002878
2879 /* do normalize only data requests, metadata requests
2880 do not need preallocation */
2881 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2882 return;
2883
2884 /* sometime caller may want exact blocks */
2885 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2886 return;
2887
2888 /* caller may indicate that preallocation isn't
2889 * required (it's a tail, for example) */
2890 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2891 return;
2892
2893 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2894 ext4_mb_normalize_group_request(ac);
2895 return ;
2896 }
2897
2898 bsbits = ac->ac_sb->s_blocksize_bits;
2899
2900 /* first, let's learn actual file size
2901 * given current request is allocated */
2902 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2903 size = size << bsbits;
2904 if (size < i_size_read(ac->ac_inode))
2905 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002906 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002907
Valerie Clement19304792008-05-13 19:31:14 -04002908 /* max size of free chunks */
2909 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002910
Valerie Clement19304792008-05-13 19:31:14 -04002911#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2912 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002913
2914 /* first, try to predict filesize */
2915 /* XXX: should this table be tunable? */
2916 start_off = 0;
2917 if (size <= 16 * 1024) {
2918 size = 16 * 1024;
2919 } else if (size <= 32 * 1024) {
2920 size = 32 * 1024;
2921 } else if (size <= 64 * 1024) {
2922 size = 64 * 1024;
2923 } else if (size <= 128 * 1024) {
2924 size = 128 * 1024;
2925 } else if (size <= 256 * 1024) {
2926 size = 256 * 1024;
2927 } else if (size <= 512 * 1024) {
2928 size = 512 * 1024;
2929 } else if (size <= 1024 * 1024) {
2930 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002931 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002932 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002933 (21 - bsbits)) << 21;
2934 size = 2 * 1024 * 1024;
2935 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002936 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2937 (22 - bsbits)) << 22;
2938 size = 4 * 1024 * 1024;
2939 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002940 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002941 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2942 (23 - bsbits)) << 23;
2943 size = 8 * 1024 * 1024;
2944 } else {
2945 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2946 size = ac->ac_o_ex.fe_len << bsbits;
2947 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002948 size = size >> bsbits;
2949 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002950
2951 /* don't cover already allocated blocks in selected range */
2952 if (ar->pleft && start <= ar->lleft) {
2953 size -= ar->lleft + 1 - start;
2954 start = ar->lleft + 1;
2955 }
2956 if (ar->pright && start + size - 1 >= ar->lright)
2957 size -= start + size - ar->lright;
2958
2959 end = start + size;
2960
2961 /* check we don't cross already preallocated blocks */
2962 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002963 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002964 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002965
Alex Tomasc9de5602008-01-29 00:19:52 -05002966 if (pa->pa_deleted)
2967 continue;
2968 spin_lock(&pa->pa_lock);
2969 if (pa->pa_deleted) {
2970 spin_unlock(&pa->pa_lock);
2971 continue;
2972 }
2973
2974 pa_end = pa->pa_lstart + pa->pa_len;
2975
2976 /* PA must not overlap original request */
2977 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2978 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2979
Eric Sandeen38877f42009-08-17 23:55:24 -04002980 /* skip PAs this normalized request doesn't overlap with */
2981 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002982 spin_unlock(&pa->pa_lock);
2983 continue;
2984 }
2985 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2986
Eric Sandeen38877f42009-08-17 23:55:24 -04002987 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05002988 if (pa_end <= ac->ac_o_ex.fe_logical) {
2989 BUG_ON(pa_end < start);
2990 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04002991 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002992 BUG_ON(pa->pa_lstart > end);
2993 end = pa->pa_lstart;
2994 }
2995 spin_unlock(&pa->pa_lock);
2996 }
2997 rcu_read_unlock();
2998 size = end - start;
2999
3000 /* XXX: extra loop to check we really don't overlap preallocations */
3001 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003002 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003003 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003004 spin_lock(&pa->pa_lock);
3005 if (pa->pa_deleted == 0) {
3006 pa_end = pa->pa_lstart + pa->pa_len;
3007 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3008 }
3009 spin_unlock(&pa->pa_lock);
3010 }
3011 rcu_read_unlock();
3012
3013 if (start + size <= ac->ac_o_ex.fe_logical &&
3014 start > ac->ac_o_ex.fe_logical) {
3015 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3016 (unsigned long) start, (unsigned long) size,
3017 (unsigned long) ac->ac_o_ex.fe_logical);
3018 }
3019 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3020 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04003021 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003022
3023 /* now prepare goal request */
3024
3025 /* XXX: is it better to align blocks WRT to logical
3026 * placement or satisfy big request as is */
3027 ac->ac_g_ex.fe_logical = start;
3028 ac->ac_g_ex.fe_len = size;
3029
3030 /* define goal start in order to merge */
3031 if (ar->pright && (ar->lright == (start + size))) {
3032 /* merge to the right */
3033 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3034 &ac->ac_f_ex.fe_group,
3035 &ac->ac_f_ex.fe_start);
3036 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3037 }
3038 if (ar->pleft && (ar->lleft + 1 == start)) {
3039 /* merge to the left */
3040 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3041 &ac->ac_f_ex.fe_group,
3042 &ac->ac_f_ex.fe_start);
3043 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3044 }
3045
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003046 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003047 (unsigned) orig_size, (unsigned) start);
3048}
3049
3050static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3051{
3052 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3053
3054 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3055 atomic_inc(&sbi->s_bal_reqs);
3056 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003057 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003058 atomic_inc(&sbi->s_bal_success);
3059 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3060 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3061 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3062 atomic_inc(&sbi->s_bal_goals);
3063 if (ac->ac_found > sbi->s_mb_max_to_scan)
3064 atomic_inc(&sbi->s_bal_breaks);
3065 }
3066
Theodore Ts'o296c3552009-09-30 00:32:42 -04003067 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3068 trace_ext4_mballoc_alloc(ac);
3069 else
3070 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003071}
3072
3073/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003074 * Called on failure; free up any blocks from the inode PA for this
3075 * context. We don't need this for MB_GROUP_PA because we only change
3076 * pa_free in ext4_mb_release_context(), but on failure, we've already
3077 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3078 */
3079static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3080{
3081 struct ext4_prealloc_space *pa = ac->ac_pa;
3082 int len;
3083
3084 if (pa && pa->pa_type == MB_INODE_PA) {
3085 len = ac->ac_b_ex.fe_len;
3086 pa->pa_free += len;
3087 }
3088
3089}
3090
3091/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003092 * use blocks preallocated to inode
3093 */
3094static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3095 struct ext4_prealloc_space *pa)
3096{
3097 ext4_fsblk_t start;
3098 ext4_fsblk_t end;
3099 int len;
3100
3101 /* found preallocated blocks, use them */
3102 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3103 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3104 len = end - start;
3105 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3106 &ac->ac_b_ex.fe_start);
3107 ac->ac_b_ex.fe_len = len;
3108 ac->ac_status = AC_STATUS_FOUND;
3109 ac->ac_pa = pa;
3110
3111 BUG_ON(start < pa->pa_pstart);
3112 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3113 BUG_ON(pa->pa_free < len);
3114 pa->pa_free -= len;
3115
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003116 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003117}
3118
3119/*
3120 * use blocks preallocated to locality group
3121 */
3122static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3123 struct ext4_prealloc_space *pa)
3124{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003125 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003126
Alex Tomasc9de5602008-01-29 00:19:52 -05003127 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3128 &ac->ac_b_ex.fe_group,
3129 &ac->ac_b_ex.fe_start);
3130 ac->ac_b_ex.fe_len = len;
3131 ac->ac_status = AC_STATUS_FOUND;
3132 ac->ac_pa = pa;
3133
3134 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003135 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003136 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003137 * in on-disk bitmap -- see ext4_mb_release_context()
3138 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003139 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003140 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003141}
3142
3143/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003144 * Return the prealloc space that have minimal distance
3145 * from the goal block. @cpa is the prealloc
3146 * space that is having currently known minimal distance
3147 * from the goal block.
3148 */
3149static struct ext4_prealloc_space *
3150ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3151 struct ext4_prealloc_space *pa,
3152 struct ext4_prealloc_space *cpa)
3153{
3154 ext4_fsblk_t cur_distance, new_distance;
3155
3156 if (cpa == NULL) {
3157 atomic_inc(&pa->pa_count);
3158 return pa;
3159 }
3160 cur_distance = abs(goal_block - cpa->pa_pstart);
3161 new_distance = abs(goal_block - pa->pa_pstart);
3162
Coly Li5a54b2f2011-02-24 14:10:05 -05003163 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003164 return cpa;
3165
3166 /* drop the previous reference */
3167 atomic_dec(&cpa->pa_count);
3168 atomic_inc(&pa->pa_count);
3169 return pa;
3170}
3171
3172/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003173 * search goal blocks in preallocated space
3174 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003175static noinline_for_stack int
3176ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003177{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003178 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003179 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3180 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003181 struct ext4_prealloc_space *pa, *cpa = NULL;
3182 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003183
3184 /* only data can be preallocated */
3185 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3186 return 0;
3187
3188 /* first, try per-file preallocation */
3189 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003190 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003191
3192 /* all fields in this condition don't change,
3193 * so we can skip locking for them */
3194 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3195 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3196 continue;
3197
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003198 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003199 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003200 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3201 continue;
3202
Alex Tomasc9de5602008-01-29 00:19:52 -05003203 /* found preallocated blocks, use them */
3204 spin_lock(&pa->pa_lock);
3205 if (pa->pa_deleted == 0 && pa->pa_free) {
3206 atomic_inc(&pa->pa_count);
3207 ext4_mb_use_inode_pa(ac, pa);
3208 spin_unlock(&pa->pa_lock);
3209 ac->ac_criteria = 10;
3210 rcu_read_unlock();
3211 return 1;
3212 }
3213 spin_unlock(&pa->pa_lock);
3214 }
3215 rcu_read_unlock();
3216
3217 /* can we use group allocation? */
3218 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3219 return 0;
3220
3221 /* inode may have no locality group for some reason */
3222 lg = ac->ac_lg;
3223 if (lg == NULL)
3224 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003225 order = fls(ac->ac_o_ex.fe_len) - 1;
3226 if (order > PREALLOC_TB_SIZE - 1)
3227 /* The max size of hash table is PREALLOC_TB_SIZE */
3228 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003229
Akinobu Mitabda00de2010-03-03 23:53:25 -05003230 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003231 /*
3232 * search for the prealloc space that is having
3233 * minimal distance from the goal block.
3234 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003235 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3236 rcu_read_lock();
3237 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3238 pa_inode_list) {
3239 spin_lock(&pa->pa_lock);
3240 if (pa->pa_deleted == 0 &&
3241 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003242
3243 cpa = ext4_mb_check_group_pa(goal_block,
3244 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003245 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003246 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003247 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003248 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003249 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003250 if (cpa) {
3251 ext4_mb_use_group_pa(ac, cpa);
3252 ac->ac_criteria = 20;
3253 return 1;
3254 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003255 return 0;
3256}
3257
3258/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003259 * the function goes through all block freed in the group
3260 * but not yet committed and marks them used in in-core bitmap.
3261 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003262 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003263 */
3264static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3265 ext4_group_t group)
3266{
3267 struct rb_node *n;
3268 struct ext4_group_info *grp;
3269 struct ext4_free_data *entry;
3270
3271 grp = ext4_get_group_info(sb, group);
3272 n = rb_first(&(grp->bb_free_root));
3273
3274 while (n) {
3275 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003276 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003277 n = rb_next(n);
3278 }
3279 return;
3280}
3281
3282/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003283 * the function goes through all preallocation in this group and marks them
3284 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003285 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003286 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003287static noinline_for_stack
3288void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003289 ext4_group_t group)
3290{
3291 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3292 struct ext4_prealloc_space *pa;
3293 struct list_head *cur;
3294 ext4_group_t groupnr;
3295 ext4_grpblk_t start;
3296 int preallocated = 0;
3297 int count = 0;
3298 int len;
3299
3300 /* all form of preallocation discards first load group,
3301 * so the only competing code is preallocation use.
3302 * we don't need any locking here
3303 * notice we do NOT ignore preallocations with pa_deleted
3304 * otherwise we could leave used blocks available for
3305 * allocation in buddy when concurrent ext4_mb_put_pa()
3306 * is dropping preallocation
3307 */
3308 list_for_each(cur, &grp->bb_prealloc_list) {
3309 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3310 spin_lock(&pa->pa_lock);
3311 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3312 &groupnr, &start);
3313 len = pa->pa_len;
3314 spin_unlock(&pa->pa_lock);
3315 if (unlikely(len == 0))
3316 continue;
3317 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003318 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003319 preallocated += len;
3320 count++;
3321 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003322 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003323}
3324
3325static void ext4_mb_pa_callback(struct rcu_head *head)
3326{
3327 struct ext4_prealloc_space *pa;
3328 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3329 kmem_cache_free(ext4_pspace_cachep, pa);
3330}
3331
3332/*
3333 * drops a reference to preallocated space descriptor
3334 * if this was the last reference and the space is consumed
3335 */
3336static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3337 struct super_block *sb, struct ext4_prealloc_space *pa)
3338{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003339 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003340 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003341
3342 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3343 return;
3344
3345 /* in this short window concurrent discard can set pa_deleted */
3346 spin_lock(&pa->pa_lock);
3347 if (pa->pa_deleted == 1) {
3348 spin_unlock(&pa->pa_lock);
3349 return;
3350 }
3351
3352 pa->pa_deleted = 1;
3353 spin_unlock(&pa->pa_lock);
3354
Eric Sandeend33a1972009-03-16 23:25:40 -04003355 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003356 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003357 * If doing group-based preallocation, pa_pstart may be in the
3358 * next group when pa is used up
3359 */
3360 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003361 grp_blk--;
3362
3363 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003364
3365 /*
3366 * possible race:
3367 *
3368 * P1 (buddy init) P2 (regular allocation)
3369 * find block B in PA
3370 * copy on-disk bitmap to buddy
3371 * mark B in on-disk bitmap
3372 * drop PA from group
3373 * mark all PAs in buddy
3374 *
3375 * thus, P1 initializes buddy with B available. to prevent this
3376 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3377 * against that pair
3378 */
3379 ext4_lock_group(sb, grp);
3380 list_del(&pa->pa_group_list);
3381 ext4_unlock_group(sb, grp);
3382
3383 spin_lock(pa->pa_obj_lock);
3384 list_del_rcu(&pa->pa_inode_list);
3385 spin_unlock(pa->pa_obj_lock);
3386
3387 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3388}
3389
3390/*
3391 * creates new preallocated space for given inode
3392 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003393static noinline_for_stack int
3394ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003395{
3396 struct super_block *sb = ac->ac_sb;
3397 struct ext4_prealloc_space *pa;
3398 struct ext4_group_info *grp;
3399 struct ext4_inode_info *ei;
3400
3401 /* preallocate only when found space is larger then requested */
3402 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3403 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3404 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3405
3406 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3407 if (pa == NULL)
3408 return -ENOMEM;
3409
3410 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3411 int winl;
3412 int wins;
3413 int win;
3414 int offs;
3415
3416 /* we can't allocate as much as normalizer wants.
3417 * so, found space must get proper lstart
3418 * to cover original request */
3419 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3420 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3421
3422 /* we're limited by original request in that
3423 * logical block must be covered any way
3424 * winl is window we can move our chunk within */
3425 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3426
3427 /* also, we should cover whole original request */
3428 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3429
3430 /* the smallest one defines real window */
3431 win = min(winl, wins);
3432
3433 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3434 if (offs && offs < win)
3435 win = offs;
3436
3437 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3438 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3439 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3440 }
3441
3442 /* preallocation can change ac_b_ex, thus we store actually
3443 * allocated blocks for history */
3444 ac->ac_f_ex = ac->ac_b_ex;
3445
3446 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3447 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3448 pa->pa_len = ac->ac_b_ex.fe_len;
3449 pa->pa_free = pa->pa_len;
3450 atomic_set(&pa->pa_count, 1);
3451 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003452 INIT_LIST_HEAD(&pa->pa_inode_list);
3453 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003454 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003455 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003456
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003457 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003458 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003459 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003460
3461 ext4_mb_use_inode_pa(ac, pa);
3462 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3463
3464 ei = EXT4_I(ac->ac_inode);
3465 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3466
3467 pa->pa_obj_lock = &ei->i_prealloc_lock;
3468 pa->pa_inode = ac->ac_inode;
3469
3470 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3471 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3472 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3473
3474 spin_lock(pa->pa_obj_lock);
3475 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3476 spin_unlock(pa->pa_obj_lock);
3477
3478 return 0;
3479}
3480
3481/*
3482 * creates new preallocated space for locality group inodes belongs to
3483 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003484static noinline_for_stack int
3485ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003486{
3487 struct super_block *sb = ac->ac_sb;
3488 struct ext4_locality_group *lg;
3489 struct ext4_prealloc_space *pa;
3490 struct ext4_group_info *grp;
3491
3492 /* preallocate only when found space is larger then requested */
3493 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3494 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3495 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3496
3497 BUG_ON(ext4_pspace_cachep == NULL);
3498 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3499 if (pa == NULL)
3500 return -ENOMEM;
3501
3502 /* preallocation can change ac_b_ex, thus we store actually
3503 * allocated blocks for history */
3504 ac->ac_f_ex = ac->ac_b_ex;
3505
3506 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3507 pa->pa_lstart = pa->pa_pstart;
3508 pa->pa_len = ac->ac_b_ex.fe_len;
3509 pa->pa_free = pa->pa_len;
3510 atomic_set(&pa->pa_count, 1);
3511 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003512 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003513 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003514 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003515 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003516
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003517 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003518 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3519 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003520
3521 ext4_mb_use_group_pa(ac, pa);
3522 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3523
3524 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3525 lg = ac->ac_lg;
3526 BUG_ON(lg == NULL);
3527
3528 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3529 pa->pa_inode = NULL;
3530
3531 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3532 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3533 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3534
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003535 /*
3536 * We will later add the new pa to the right bucket
3537 * after updating the pa_free in ext4_mb_release_context
3538 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003539 return 0;
3540}
3541
3542static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3543{
3544 int err;
3545
3546 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3547 err = ext4_mb_new_group_pa(ac);
3548 else
3549 err = ext4_mb_new_inode_pa(ac);
3550 return err;
3551}
3552
3553/*
3554 * finds all unused blocks in on-disk bitmap, frees them in
3555 * in-core bitmap and buddy.
3556 * @pa must be unlinked from inode and group lists, so that
3557 * nobody else can find/use it.
3558 * the caller MUST hold group/inode locks.
3559 * TODO: optimize the case when there are no in-core structures yet
3560 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003561static noinline_for_stack int
3562ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003563 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003564{
Alex Tomasc9de5602008-01-29 00:19:52 -05003565 struct super_block *sb = e4b->bd_sb;
3566 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003567 unsigned int end;
3568 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003569 ext4_group_t group;
3570 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003571 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003572 int err = 0;
3573 int free = 0;
3574
3575 BUG_ON(pa->pa_deleted == 0);
3576 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003577 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003578 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3579 end = bit + pa->pa_len;
3580
Alex Tomasc9de5602008-01-29 00:19:52 -05003581 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003582 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003583 if (bit >= end)
3584 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003585 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003586 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003587 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3588 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003589 free += next - bit;
3590
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003591 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Lukas Czernera9c667f2011-06-06 09:51:52 -04003592 trace_ext4_mb_release_inode_pa(pa, grp_blk_start + bit,
3593 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003594 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3595 bit = next + 1;
3596 }
3597 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003598 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003599 pa, (unsigned long) pa->pa_lstart,
3600 (unsigned long) pa->pa_pstart,
3601 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003602 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003603 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003604 /*
3605 * pa is already deleted so we use the value obtained
3606 * from the bitmap and continue.
3607 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003608 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003609 atomic_add(free, &sbi->s_mb_discarded);
3610
3611 return err;
3612}
3613
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003614static noinline_for_stack int
3615ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003616 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003617{
Alex Tomasc9de5602008-01-29 00:19:52 -05003618 struct super_block *sb = e4b->bd_sb;
3619 ext4_group_t group;
3620 ext4_grpblk_t bit;
3621
Lukas Czernera9c667f2011-06-06 09:51:52 -04003622 trace_ext4_mb_release_group_pa(pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003623 BUG_ON(pa->pa_deleted == 0);
3624 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3625 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3626 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3627 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003628 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003629
3630 return 0;
3631}
3632
3633/*
3634 * releases all preallocations in given group
3635 *
3636 * first, we need to decide discard policy:
3637 * - when do we discard
3638 * 1) ENOSPC
3639 * - how many do we discard
3640 * 1) how many requested
3641 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003642static noinline_for_stack int
3643ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003644 ext4_group_t group, int needed)
3645{
3646 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3647 struct buffer_head *bitmap_bh = NULL;
3648 struct ext4_prealloc_space *pa, *tmp;
3649 struct list_head list;
3650 struct ext4_buddy e4b;
3651 int err;
3652 int busy = 0;
3653 int free = 0;
3654
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003655 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003656
3657 if (list_empty(&grp->bb_prealloc_list))
3658 return 0;
3659
Theodore Ts'o574ca172008-07-11 19:27:31 -04003660 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003661 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003662 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003663 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003664 }
3665
3666 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003667 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003668 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003669 put_bh(bitmap_bh);
3670 return 0;
3671 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003672
3673 if (needed == 0)
3674 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3675
Alex Tomasc9de5602008-01-29 00:19:52 -05003676 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003677repeat:
3678 ext4_lock_group(sb, group);
3679 list_for_each_entry_safe(pa, tmp,
3680 &grp->bb_prealloc_list, pa_group_list) {
3681 spin_lock(&pa->pa_lock);
3682 if (atomic_read(&pa->pa_count)) {
3683 spin_unlock(&pa->pa_lock);
3684 busy = 1;
3685 continue;
3686 }
3687 if (pa->pa_deleted) {
3688 spin_unlock(&pa->pa_lock);
3689 continue;
3690 }
3691
3692 /* seems this one can be freed ... */
3693 pa->pa_deleted = 1;
3694
3695 /* we can trust pa_free ... */
3696 free += pa->pa_free;
3697
3698 spin_unlock(&pa->pa_lock);
3699
3700 list_del(&pa->pa_group_list);
3701 list_add(&pa->u.pa_tmp_list, &list);
3702 }
3703
3704 /* if we still need more blocks and some PAs were used, try again */
3705 if (free < needed && busy) {
3706 busy = 0;
3707 ext4_unlock_group(sb, group);
3708 /*
3709 * Yield the CPU here so that we don't get soft lockup
3710 * in non preempt case.
3711 */
3712 yield();
3713 goto repeat;
3714 }
3715
3716 /* found anything to free? */
3717 if (list_empty(&list)) {
3718 BUG_ON(free != 0);
3719 goto out;
3720 }
3721
3722 /* now free all selected PAs */
3723 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3724
3725 /* remove from object (inode or locality group) */
3726 spin_lock(pa->pa_obj_lock);
3727 list_del_rcu(&pa->pa_inode_list);
3728 spin_unlock(pa->pa_obj_lock);
3729
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003730 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003731 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003732 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003733 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003734
3735 list_del(&pa->u.pa_tmp_list);
3736 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3737 }
3738
3739out:
3740 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003741 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003742 put_bh(bitmap_bh);
3743 return free;
3744}
3745
3746/*
3747 * releases all non-used preallocated blocks for given inode
3748 *
3749 * It's important to discard preallocations under i_data_sem
3750 * We don't want another block to be served from the prealloc
3751 * space when we are discarding the inode prealloc space.
3752 *
3753 * FIXME!! Make sure it is valid at all the call sites
3754 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003755void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003756{
3757 struct ext4_inode_info *ei = EXT4_I(inode);
3758 struct super_block *sb = inode->i_sb;
3759 struct buffer_head *bitmap_bh = NULL;
3760 struct ext4_prealloc_space *pa, *tmp;
3761 ext4_group_t group = 0;
3762 struct list_head list;
3763 struct ext4_buddy e4b;
3764 int err;
3765
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003766 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003767 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3768 return;
3769 }
3770
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003771 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003772 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003773
3774 INIT_LIST_HEAD(&list);
3775
3776repeat:
3777 /* first, collect all pa's in the inode */
3778 spin_lock(&ei->i_prealloc_lock);
3779 while (!list_empty(&ei->i_prealloc_list)) {
3780 pa = list_entry(ei->i_prealloc_list.next,
3781 struct ext4_prealloc_space, pa_inode_list);
3782 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3783 spin_lock(&pa->pa_lock);
3784 if (atomic_read(&pa->pa_count)) {
3785 /* this shouldn't happen often - nobody should
3786 * use preallocation while we're discarding it */
3787 spin_unlock(&pa->pa_lock);
3788 spin_unlock(&ei->i_prealloc_lock);
3789 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3790 WARN_ON(1);
3791 schedule_timeout_uninterruptible(HZ);
3792 goto repeat;
3793
3794 }
3795 if (pa->pa_deleted == 0) {
3796 pa->pa_deleted = 1;
3797 spin_unlock(&pa->pa_lock);
3798 list_del_rcu(&pa->pa_inode_list);
3799 list_add(&pa->u.pa_tmp_list, &list);
3800 continue;
3801 }
3802
3803 /* someone is deleting pa right now */
3804 spin_unlock(&pa->pa_lock);
3805 spin_unlock(&ei->i_prealloc_lock);
3806
3807 /* we have to wait here because pa_deleted
3808 * doesn't mean pa is already unlinked from
3809 * the list. as we might be called from
3810 * ->clear_inode() the inode will get freed
3811 * and concurrent thread which is unlinking
3812 * pa from inode's list may access already
3813 * freed memory, bad-bad-bad */
3814
3815 /* XXX: if this happens too often, we can
3816 * add a flag to force wait only in case
3817 * of ->clear_inode(), but not in case of
3818 * regular truncate */
3819 schedule_timeout_uninterruptible(HZ);
3820 goto repeat;
3821 }
3822 spin_unlock(&ei->i_prealloc_lock);
3823
3824 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003825 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003826 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3827
3828 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003829 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003830 ext4_error(sb, "Error loading buddy information for %u",
3831 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003832 continue;
3833 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003834
Theodore Ts'o574ca172008-07-11 19:27:31 -04003835 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003836 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003837 ext4_error(sb, "Error reading block bitmap for %u",
3838 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003839 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003840 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003841 }
3842
3843 ext4_lock_group(sb, group);
3844 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003845 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003846 ext4_unlock_group(sb, group);
3847
Jing Zhange39e07f2010-05-14 00:00:00 -04003848 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003849 put_bh(bitmap_bh);
3850
3851 list_del(&pa->u.pa_tmp_list);
3852 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3853 }
3854}
3855
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003856#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003857static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3858{
3859 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003860 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003861
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05003862 if (!mb_enable_debug ||
3863 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04003864 return;
3865
Alex Tomasc9de5602008-01-29 00:19:52 -05003866 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3867 " Allocation context details:\n");
3868 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3869 ac->ac_status, ac->ac_flags);
3870 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3871 "best %lu/%lu/%lu@%lu cr %d\n",
3872 (unsigned long)ac->ac_o_ex.fe_group,
3873 (unsigned long)ac->ac_o_ex.fe_start,
3874 (unsigned long)ac->ac_o_ex.fe_len,
3875 (unsigned long)ac->ac_o_ex.fe_logical,
3876 (unsigned long)ac->ac_g_ex.fe_group,
3877 (unsigned long)ac->ac_g_ex.fe_start,
3878 (unsigned long)ac->ac_g_ex.fe_len,
3879 (unsigned long)ac->ac_g_ex.fe_logical,
3880 (unsigned long)ac->ac_b_ex.fe_group,
3881 (unsigned long)ac->ac_b_ex.fe_start,
3882 (unsigned long)ac->ac_b_ex.fe_len,
3883 (unsigned long)ac->ac_b_ex.fe_logical,
3884 (int)ac->ac_criteria);
3885 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3886 ac->ac_found);
3887 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003888 ngroups = ext4_get_groups_count(sb);
3889 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003890 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3891 struct ext4_prealloc_space *pa;
3892 ext4_grpblk_t start;
3893 struct list_head *cur;
3894 ext4_lock_group(sb, i);
3895 list_for_each(cur, &grp->bb_prealloc_list) {
3896 pa = list_entry(cur, struct ext4_prealloc_space,
3897 pa_group_list);
3898 spin_lock(&pa->pa_lock);
3899 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3900 NULL, &start);
3901 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003902 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3903 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003904 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003905 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003906
3907 if (grp->bb_free == 0)
3908 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003909 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003910 i, grp->bb_free, grp->bb_fragments);
3911 }
3912 printk(KERN_ERR "\n");
3913}
3914#else
3915static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3916{
3917 return;
3918}
3919#endif
3920
3921/*
3922 * We use locality group preallocation for small size file. The size of the
3923 * file is determined by the current size or the resulting size after
3924 * allocation which ever is larger
3925 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003926 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003927 */
3928static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3929{
3930 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3931 int bsbits = ac->ac_sb->s_blocksize_bits;
3932 loff_t size, isize;
3933
3934 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3935 return;
3936
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003937 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3938 return;
3939
Alex Tomasc9de5602008-01-29 00:19:52 -05003940 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04003941 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3942 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003943
Theodore Ts'o50797482009-09-18 13:34:02 -04003944 if ((size == isize) &&
3945 !ext4_fs_is_busy(sbi) &&
3946 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3947 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3948 return;
3949 }
3950
Alex Tomasc9de5602008-01-29 00:19:52 -05003951 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003952 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003953 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003954 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003955 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003956 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003957
3958 BUG_ON(ac->ac_lg != NULL);
3959 /*
3960 * locality group prealloc space are per cpu. The reason for having
3961 * per cpu locality group is to reduce the contention between block
3962 * request from multiple CPUs.
3963 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003964 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003965
3966 /* we're going to use group allocation */
3967 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3968
3969 /* serialize all allocations in the group */
3970 mutex_lock(&ac->ac_lg->lg_mutex);
3971}
3972
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003973static noinline_for_stack int
3974ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003975 struct ext4_allocation_request *ar)
3976{
3977 struct super_block *sb = ar->inode->i_sb;
3978 struct ext4_sb_info *sbi = EXT4_SB(sb);
3979 struct ext4_super_block *es = sbi->s_es;
3980 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003981 unsigned int len;
3982 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05003983 ext4_grpblk_t block;
3984
3985 /* we can't allocate > group size */
3986 len = ar->len;
3987
3988 /* just a dirty hack to filter too big requests */
3989 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
3990 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
3991
3992 /* start searching from the goal */
3993 goal = ar->goal;
3994 if (goal < le32_to_cpu(es->s_first_data_block) ||
3995 goal >= ext4_blocks_count(es))
3996 goal = le32_to_cpu(es->s_first_data_block);
3997 ext4_get_group_no_and_offset(sb, goal, &group, &block);
3998
3999 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04004000 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05004001 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004002 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004003 ac->ac_sb = sb;
4004 ac->ac_inode = ar->inode;
4005 ac->ac_o_ex.fe_logical = ar->logical;
4006 ac->ac_o_ex.fe_group = group;
4007 ac->ac_o_ex.fe_start = block;
4008 ac->ac_o_ex.fe_len = len;
4009 ac->ac_g_ex.fe_logical = ar->logical;
4010 ac->ac_g_ex.fe_group = group;
4011 ac->ac_g_ex.fe_start = block;
4012 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004013 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004014
4015 /* we have to define context: we'll we work with a file or
4016 * locality group. this is a policy, actually */
4017 ext4_mb_group_or_file(ac);
4018
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004019 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004020 "left: %u/%u, right %u/%u to %swritable\n",
4021 (unsigned) ar->len, (unsigned) ar->logical,
4022 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4023 (unsigned) ar->lleft, (unsigned) ar->pleft,
4024 (unsigned) ar->lright, (unsigned) ar->pright,
4025 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4026 return 0;
4027
4028}
4029
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004030static noinline_for_stack void
4031ext4_mb_discard_lg_preallocations(struct super_block *sb,
4032 struct ext4_locality_group *lg,
4033 int order, int total_entries)
4034{
4035 ext4_group_t group = 0;
4036 struct ext4_buddy e4b;
4037 struct list_head discard_list;
4038 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004039
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004040 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004041
4042 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004043
4044 spin_lock(&lg->lg_prealloc_lock);
4045 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4046 pa_inode_list) {
4047 spin_lock(&pa->pa_lock);
4048 if (atomic_read(&pa->pa_count)) {
4049 /*
4050 * This is the pa that we just used
4051 * for block allocation. So don't
4052 * free that
4053 */
4054 spin_unlock(&pa->pa_lock);
4055 continue;
4056 }
4057 if (pa->pa_deleted) {
4058 spin_unlock(&pa->pa_lock);
4059 continue;
4060 }
4061 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004062 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004063
4064 /* seems this one can be freed ... */
4065 pa->pa_deleted = 1;
4066 spin_unlock(&pa->pa_lock);
4067
4068 list_del_rcu(&pa->pa_inode_list);
4069 list_add(&pa->u.pa_tmp_list, &discard_list);
4070
4071 total_entries--;
4072 if (total_entries <= 5) {
4073 /*
4074 * we want to keep only 5 entries
4075 * allowing it to grow to 8. This
4076 * mak sure we don't call discard
4077 * soon for this list.
4078 */
4079 break;
4080 }
4081 }
4082 spin_unlock(&lg->lg_prealloc_lock);
4083
4084 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4085
4086 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4087 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004088 ext4_error(sb, "Error loading buddy information for %u",
4089 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004090 continue;
4091 }
4092 ext4_lock_group(sb, group);
4093 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004094 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004095 ext4_unlock_group(sb, group);
4096
Jing Zhange39e07f2010-05-14 00:00:00 -04004097 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004098 list_del(&pa->u.pa_tmp_list);
4099 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4100 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004101}
4102
4103/*
4104 * We have incremented pa_count. So it cannot be freed at this
4105 * point. Also we hold lg_mutex. So no parallel allocation is
4106 * possible from this lg. That means pa_free cannot be updated.
4107 *
4108 * A parallel ext4_mb_discard_group_preallocations is possible.
4109 * which can cause the lg_prealloc_list to be updated.
4110 */
4111
4112static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4113{
4114 int order, added = 0, lg_prealloc_count = 1;
4115 struct super_block *sb = ac->ac_sb;
4116 struct ext4_locality_group *lg = ac->ac_lg;
4117 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4118
4119 order = fls(pa->pa_free) - 1;
4120 if (order > PREALLOC_TB_SIZE - 1)
4121 /* The max size of hash table is PREALLOC_TB_SIZE */
4122 order = PREALLOC_TB_SIZE - 1;
4123 /* Add the prealloc space to lg */
4124 rcu_read_lock();
4125 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4126 pa_inode_list) {
4127 spin_lock(&tmp_pa->pa_lock);
4128 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004129 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004130 continue;
4131 }
4132 if (!added && pa->pa_free < tmp_pa->pa_free) {
4133 /* Add to the tail of the previous entry */
4134 list_add_tail_rcu(&pa->pa_inode_list,
4135 &tmp_pa->pa_inode_list);
4136 added = 1;
4137 /*
4138 * we want to count the total
4139 * number of entries in the list
4140 */
4141 }
4142 spin_unlock(&tmp_pa->pa_lock);
4143 lg_prealloc_count++;
4144 }
4145 if (!added)
4146 list_add_tail_rcu(&pa->pa_inode_list,
4147 &lg->lg_prealloc_list[order]);
4148 rcu_read_unlock();
4149
4150 /* Now trim the list to be not more than 8 elements */
4151 if (lg_prealloc_count > 8) {
4152 ext4_mb_discard_lg_preallocations(sb, lg,
4153 order, lg_prealloc_count);
4154 return;
4155 }
4156 return ;
4157}
4158
Alex Tomasc9de5602008-01-29 00:19:52 -05004159/*
4160 * release all resource we used in allocation
4161 */
4162static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4163{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004164 struct ext4_prealloc_space *pa = ac->ac_pa;
4165 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004166 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004167 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004168 spin_lock(&pa->pa_lock);
4169 pa->pa_pstart += ac->ac_b_ex.fe_len;
4170 pa->pa_lstart += ac->ac_b_ex.fe_len;
4171 pa->pa_free -= ac->ac_b_ex.fe_len;
4172 pa->pa_len -= ac->ac_b_ex.fe_len;
4173 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004174 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004175 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004176 if (pa) {
4177 /*
4178 * We want to add the pa to the right bucket.
4179 * Remove it from the list and while adding
4180 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004181 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004182 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004183 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004184 spin_lock(pa->pa_obj_lock);
4185 list_del_rcu(&pa->pa_inode_list);
4186 spin_unlock(pa->pa_obj_lock);
4187 ext4_mb_add_n_trim(ac);
4188 }
4189 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4190 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004191 if (ac->ac_bitmap_page)
4192 page_cache_release(ac->ac_bitmap_page);
4193 if (ac->ac_buddy_page)
4194 page_cache_release(ac->ac_buddy_page);
4195 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4196 mutex_unlock(&ac->ac_lg->lg_mutex);
4197 ext4_mb_collect_stats(ac);
4198 return 0;
4199}
4200
4201static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4202{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004203 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004204 int ret;
4205 int freed = 0;
4206
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004207 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004208 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004209 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4210 freed += ret;
4211 needed -= ret;
4212 }
4213
4214 return freed;
4215}
4216
4217/*
4218 * Main entry point into mballoc to allocate blocks
4219 * it tries to use preallocation first, then falls back
4220 * to usual allocation
4221 */
4222ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004223 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004224{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004225 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004226 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004227 struct ext4_sb_info *sbi;
4228 struct super_block *sb;
4229 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004230 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004231 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004232
4233 sb = ar->inode->i_sb;
4234 sbi = EXT4_SB(sb);
4235
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004236 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004237
Mingming Cao60e58e02009-01-22 18:13:05 +01004238 /*
4239 * For delayed allocation, we could skip the ENOSPC and
4240 * EDQUOT check, as blocks and quotas have been already
4241 * reserved when data being copied into pagecache.
4242 */
Theodore Ts'of2321092011-01-10 12:12:36 -05004243 if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
Mingming Cao60e58e02009-01-22 18:13:05 +01004244 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4245 else {
4246 /* Without delayed allocation we need to verify
4247 * there is enough free blocks to do block allocation
4248 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004249 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004250 while (ar->len &&
4251 ext4_claim_free_blocks(sbi, ar->len, ar->flags)) {
4252
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004253 /* let others to free the space */
4254 yield();
4255 ar->len = ar->len >> 1;
4256 }
4257 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004258 *errp = -ENOSPC;
4259 return 0;
4260 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004261 reserv_blks = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004262 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
4263 dquot_alloc_block_nofail(ar->inode, ar->len);
4264 } else {
4265 while (ar->len &&
4266 dquot_alloc_block(ar->inode, ar->len)) {
4267
4268 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4269 ar->len--;
4270 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004271 }
4272 inquota = ar->len;
4273 if (ar->len == 0) {
4274 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004275 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004276 }
Mingming Caod2a17632008-07-14 17:52:37 -04004277 }
Mingming Caod2a17632008-07-14 17:52:37 -04004278
Eric Sandeen256bdb42008-02-10 01:13:33 -05004279 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004280 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004281 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004282 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004283 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004284 }
4285
Eric Sandeen256bdb42008-02-10 01:13:33 -05004286 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004287 if (*errp) {
4288 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004289 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004290 }
4291
Eric Sandeen256bdb42008-02-10 01:13:33 -05004292 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4293 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004294 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4295 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004296repeat:
4297 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004298 *errp = ext4_mb_regular_allocator(ac);
4299 if (*errp)
4300 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004301
4302 /* as we've just preallocated more space than
4303 * user requested orinally, we store allocated
4304 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004305 if (ac->ac_status == AC_STATUS_FOUND &&
4306 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4307 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004308 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004309 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004310 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004311 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004312 /*
4313 * drop the reference that we took
4314 * in ext4_mb_use_best_found
4315 */
4316 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004317 ac->ac_b_ex.fe_group = 0;
4318 ac->ac_b_ex.fe_start = 0;
4319 ac->ac_b_ex.fe_len = 0;
4320 ac->ac_status = AC_STATUS_CONTINUE;
4321 goto repeat;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004322 } else if (*errp)
4323 errout:
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004324 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004325 else {
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004326 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4327 ar->len = ac->ac_b_ex.fe_len;
4328 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004329 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004330 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004331 if (freed)
4332 goto repeat;
4333 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004334 }
4335
4336 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)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004346 dquot_free_block(ar->inode, 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 */
4351 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4352 reserv_blks);
4353 }
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{
4368 if ((entry1->t_tid == entry2->t_tid) &&
4369 (entry1->group == entry2->group) &&
4370 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4371 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;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004380 ext4_grpblk_t block;
4381 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
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004392 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004393 block = new_entry->start_blk;
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;
4406 entry = rb_entry(parent, struct ext4_free_data, node);
4407 if (block < entry->start_blk)
4408 n = &(*n)->rb_left;
4409 else if (block >= (entry->start_blk + entry->count))
4410 n = &(*n)->rb_right;
4411 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004412 ext4_grp_locked_error(sb, group, 0,
4413 ext4_group_first_block_no(sb, group) + block,
4414 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004415 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004416 }
4417 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004418
4419 rb_link_node(new_node, parent, n);
4420 rb_insert_color(new_node, &db->bb_free_root);
4421
4422 /* Now try to see the extent can be merged to left and right */
4423 node = rb_prev(new_node);
4424 if (node) {
4425 entry = rb_entry(node, struct ext4_free_data, node);
4426 if (can_merge(entry, new_entry)) {
4427 new_entry->start_blk = entry->start_blk;
4428 new_entry->count += entry->count;
4429 rb_erase(node, &(db->bb_free_root));
4430 spin_lock(&sbi->s_md_lock);
4431 list_del(&entry->list);
4432 spin_unlock(&sbi->s_md_lock);
4433 kmem_cache_free(ext4_free_ext_cachep, entry);
4434 }
4435 }
4436
4437 node = rb_next(new_node);
4438 if (node) {
4439 entry = rb_entry(node, struct ext4_free_data, node);
4440 if (can_merge(new_entry, entry)) {
4441 new_entry->count += entry->count;
4442 rb_erase(node, &(db->bb_free_root));
4443 spin_lock(&sbi->s_md_lock);
4444 list_del(&entry->list);
4445 spin_unlock(&sbi->s_md_lock);
4446 kmem_cache_free(ext4_free_ext_cachep, entry);
4447 }
4448 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004449 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004450 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004451 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004452 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004453 return 0;
4454}
4455
Theodore Ts'o44338712009-11-22 07:44:56 -05004456/**
4457 * ext4_free_blocks() -- Free given blocks and update quota
4458 * @handle: handle for this transaction
4459 * @inode: inode
4460 * @block: start physical block to free
4461 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004462 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004463 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004464void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004465 struct buffer_head *bh, ext4_fsblk_t block,
4466 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004467{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004468 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004469 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004470 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004471 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004472 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004473 ext4_grpblk_t bit;
4474 struct buffer_head *gd_bh;
4475 ext4_group_t block_group;
4476 struct ext4_sb_info *sbi;
4477 struct ext4_buddy e4b;
4478 int err = 0;
4479 int ret;
4480
Theodore Ts'oe6362602009-11-23 07:17:05 -05004481 if (bh) {
4482 if (block)
4483 BUG_ON(block != bh->b_blocknr);
4484 else
4485 block = bh->b_blocknr;
4486 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004487
Alex Tomasc9de5602008-01-29 00:19:52 -05004488 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004489 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4490 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004491 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004492 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004493 goto error_return;
4494 }
4495
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004496 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004497 trace_ext4_free_blocks(inode, block, count, flags);
4498
4499 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4500 struct buffer_head *tbh = bh;
4501 int i;
4502
4503 BUG_ON(bh && (count > 1));
4504
4505 for (i = 0; i < count; i++) {
4506 if (!bh)
4507 tbh = sb_find_get_block(inode->i_sb,
4508 block + i);
Namhyung Kim87783692010-10-27 21:30:11 -04004509 if (unlikely(!tbh))
4510 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004511 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004512 inode, tbh, block + i);
4513 }
4514 }
4515
Theodore Ts'o60e66792010-05-17 07:00:00 -04004516 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004517 * We need to make sure we don't reuse the freed block until
4518 * after the transaction is committed, which we can do by
4519 * treating the block as metadata, below. We make an
4520 * exception if the inode is to be written in writeback mode
4521 * since writeback mode has weak data consistency guarantees.
4522 */
4523 if (!ext4_should_writeback_data(inode))
4524 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004525
Alex Tomasc9de5602008-01-29 00:19:52 -05004526do_more:
4527 overflow = 0;
4528 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4529
4530 /*
4531 * Check to see if we are freeing blocks across a group
4532 * boundary.
4533 */
4534 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4535 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4536 count -= overflow;
4537 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004538 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004539 if (!bitmap_bh) {
4540 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004541 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004542 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004543 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004544 if (!gdp) {
4545 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004546 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004547 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004548
4549 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4550 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4551 in_range(block, ext4_inode_table(sb, gdp),
4552 EXT4_SB(sb)->s_itb_per_group) ||
4553 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4554 EXT4_SB(sb)->s_itb_per_group)) {
4555
Eric Sandeen12062dd2010-02-15 14:19:27 -05004556 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004557 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004558 /* err = 0. ext4_std_error should be a no op */
4559 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004560 }
4561
4562 BUFFER_TRACE(bitmap_bh, "getting write access");
4563 err = ext4_journal_get_write_access(handle, bitmap_bh);
4564 if (err)
4565 goto error_return;
4566
4567 /*
4568 * We are about to modify some metadata. Call the journal APIs
4569 * to unshare ->b_data if a currently-committing transaction is
4570 * using it
4571 */
4572 BUFFER_TRACE(gd_bh, "get_write_access");
4573 err = ext4_journal_get_write_access(handle, gd_bh);
4574 if (err)
4575 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004576#ifdef AGGRESSIVE_CHECK
4577 {
4578 int i;
4579 for (i = 0; i < count; i++)
4580 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4581 }
4582#endif
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004583 trace_ext4_mballoc_free(sb, inode, block_group, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004584
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004585 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4586 if (err)
4587 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004588
4589 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004590 struct ext4_free_data *new_entry;
4591 /*
4592 * blocks being freed are metadata. these blocks shouldn't
4593 * be used until this transaction is committed
4594 */
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004595 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4596 if (!new_entry) {
4597 err = -ENOMEM;
4598 goto error_return;
4599 }
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004600 new_entry->start_blk = bit;
4601 new_entry->group = block_group;
4602 new_entry->count = count;
4603 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004604
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004605 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004606 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004607 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004608 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004609 /* need to update group_info->bb_free and bitmap
4610 * with group lock held. generate_buddy look at
4611 * them with group lock_held
4612 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004613 ext4_lock_group(sb, block_group);
4614 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004615 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004616 }
4617
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004618 ret = ext4_free_blks_count(sb, gdp) + count;
4619 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004620 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004621 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004622 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4623
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004624 if (sbi->s_log_groups_per_flex) {
4625 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004626 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004627 }
4628
Jing Zhange39e07f2010-05-14 00:00:00 -04004629 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004630
Theodore Ts'o44338712009-11-22 07:44:56 -05004631 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004632
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004633 /* We dirtied the bitmap block */
4634 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4635 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4636
Alex Tomasc9de5602008-01-29 00:19:52 -05004637 /* And the group descriptor block */
4638 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004639 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004640 if (!err)
4641 err = ret;
4642
4643 if (overflow && !err) {
4644 block += count;
4645 count = overflow;
4646 put_bh(bitmap_bh);
4647 goto do_more;
4648 }
Theodore Ts'oa0375152010-06-11 23:14:04 -04004649 ext4_mark_super_dirty(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004650error_return:
Maxim Patlasov7132de72011-07-10 19:37:48 -04004651 if (freed && !(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004652 dquot_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004653 brelse(bitmap_bh);
4654 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004655 return;
4656}
Lukas Czerner7360d172010-10-27 21:30:12 -04004657
4658/**
Amir Goldstein2846e822011-05-09 10:46:41 -04004659 * ext4_add_groupblocks() -- Add given blocks to an existing group
4660 * @handle: handle to this transaction
4661 * @sb: super block
4662 * @block: start physcial block to add to the block group
4663 * @count: number of blocks to free
4664 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004665 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004666 */
4667void ext4_add_groupblocks(handle_t *handle, struct super_block *sb,
4668 ext4_fsblk_t block, unsigned long count)
4669{
4670 struct buffer_head *bitmap_bh = NULL;
4671 struct buffer_head *gd_bh;
4672 ext4_group_t block_group;
4673 ext4_grpblk_t bit;
4674 unsigned int i;
4675 struct ext4_group_desc *desc;
4676 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004677 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004678 int err = 0, ret, blk_free_count;
4679 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004680
4681 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4682
4683 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004684 /*
4685 * Check to see if we are freeing blocks across a group
4686 * boundary.
4687 */
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004688 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb))
Amir Goldstein2846e822011-05-09 10:46:41 -04004689 goto error_return;
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004690
Amir Goldstein2846e822011-05-09 10:46:41 -04004691 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
4692 if (!bitmap_bh)
4693 goto error_return;
4694 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
4695 if (!desc)
4696 goto error_return;
4697
4698 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4699 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4700 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4701 in_range(block + count - 1, ext4_inode_table(sb, desc),
4702 sbi->s_itb_per_group)) {
4703 ext4_error(sb, "Adding blocks in system zones - "
4704 "Block = %llu, count = %lu",
4705 block, count);
4706 goto error_return;
4707 }
4708
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004709 BUFFER_TRACE(bitmap_bh, "getting write access");
4710 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004711 if (err)
4712 goto error_return;
4713
4714 /*
4715 * We are about to modify some metadata. Call the journal APIs
4716 * to unshare ->b_data if a currently-committing transaction is
4717 * using it
4718 */
4719 BUFFER_TRACE(gd_bh, "get_write_access");
4720 err = ext4_journal_get_write_access(handle, gd_bh);
4721 if (err)
4722 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04004723
Amir Goldstein2846e822011-05-09 10:46:41 -04004724 for (i = 0, blocks_freed = 0; i < count; i++) {
4725 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04004726 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04004727 ext4_error(sb, "bit already cleared for block %llu",
4728 (ext4_fsblk_t)(block + i));
4729 BUFFER_TRACE(bitmap_bh, "bit already cleared");
4730 } else {
4731 blocks_freed++;
4732 }
4733 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004734
4735 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4736 if (err)
4737 goto error_return;
4738
4739 /*
4740 * need to update group_info->bb_free and bitmap
4741 * with group lock held. generate_buddy look at
4742 * them with group lock_held
4743 */
Amir Goldstein2846e822011-05-09 10:46:41 -04004744 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004745 mb_clear_bits(bitmap_bh->b_data, bit, count);
4746 mb_free_blocks(NULL, &e4b, bit, count);
Amir Goldstein2846e822011-05-09 10:46:41 -04004747 blk_free_count = blocks_freed + ext4_free_blks_count(sb, desc);
4748 ext4_free_blks_set(sb, desc, blk_free_count);
4749 desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
4750 ext4_unlock_group(sb, block_group);
4751 percpu_counter_add(&sbi->s_freeblocks_counter, blocks_freed);
4752
4753 if (sbi->s_log_groups_per_flex) {
4754 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
4755 atomic_add(blocks_freed,
4756 &sbi->s_flex_groups[flex_group].free_blocks);
4757 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004758
4759 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04004760
4761 /* We dirtied the bitmap block */
4762 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4763 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4764
4765 /* And the group descriptor block */
4766 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4767 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4768 if (!err)
4769 err = ret;
4770
4771error_return:
4772 brelse(bitmap_bh);
4773 ext4_std_error(sb, err);
4774 return;
4775}
4776
4777/**
Lukas Czerner7360d172010-10-27 21:30:12 -04004778 * ext4_trim_extent -- function to TRIM one single free extent in the group
4779 * @sb: super block for the file system
4780 * @start: starting block of the free extent in the alloc. group
4781 * @count: number of blocks to TRIM
4782 * @group: alloc. group we are working with
4783 * @e4b: ext4 buddy for the group
4784 *
4785 * Trim "count" blocks starting at "start" in the "group". To assure that no
4786 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4787 * be called with under the group lock.
4788 */
Theodore Ts'od9f34502011-04-30 13:47:24 -04004789static void ext4_trim_extent(struct super_block *sb, int start, int count,
4790 ext4_group_t group, struct ext4_buddy *e4b)
Lukas Czerner7360d172010-10-27 21:30:12 -04004791{
4792 struct ext4_free_extent ex;
Lukas Czerner7360d172010-10-27 21:30:12 -04004793
Tao Mab3d4c2b2011-07-11 00:01:52 -04004794 trace_ext4_trim_extent(sb, group, start, count);
4795
Lukas Czerner7360d172010-10-27 21:30:12 -04004796 assert_spin_locked(ext4_group_lock_ptr(sb, group));
4797
4798 ex.fe_start = start;
4799 ex.fe_group = group;
4800 ex.fe_len = count;
4801
4802 /*
4803 * Mark blocks used, so no one can reuse them while
4804 * being trimmed.
4805 */
4806 mb_mark_used(e4b, &ex);
4807 ext4_unlock_group(sb, group);
Theodore Ts'od9f34502011-04-30 13:47:24 -04004808 ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04004809 ext4_lock_group(sb, group);
4810 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czerner7360d172010-10-27 21:30:12 -04004811}
4812
4813/**
4814 * ext4_trim_all_free -- function to trim all free space in alloc. group
4815 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04004816 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04004817 * @start: first group block to examine
4818 * @max: last group block to examine
4819 * @minblocks: minimum extent block count
4820 *
4821 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4822 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4823 * the extent.
4824 *
4825 *
4826 * ext4_trim_all_free walks through group's block bitmap searching for free
4827 * extents. When the free extent is found, mark it as used in group buddy
4828 * bitmap. Then issue a TRIM command on this extent and free the extent in
4829 * the group buddy bitmap. This is done until whole group is scanned.
4830 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05004831static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04004832ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
4833 ext4_grpblk_t start, ext4_grpblk_t max,
4834 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04004835{
4836 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04004837 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04004838 struct ext4_buddy e4b;
4839 int ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04004840
Tao Mab3d4c2b2011-07-11 00:01:52 -04004841 trace_ext4_trim_all_free(sb, group, start, max);
4842
Lukas Czerner78944082011-05-24 18:16:27 -04004843 ret = ext4_mb_load_buddy(sb, group, &e4b);
4844 if (ret) {
4845 ext4_error(sb, "Error in loading buddy "
4846 "information for %u", group);
4847 return ret;
4848 }
Lukas Czerner78944082011-05-24 18:16:27 -04004849 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04004850
4851 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04004852 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
4853 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
4854 goto out;
4855
Lukas Czerner78944082011-05-24 18:16:27 -04004856 start = (e4b.bd_info->bb_first_free > start) ?
4857 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04004858
4859 while (start < max) {
4860 start = mb_find_next_zero_bit(bitmap, max, start);
4861 if (start >= max)
4862 break;
4863 next = mb_find_next_bit(bitmap, max, start);
4864
4865 if ((next - start) >= minblocks) {
Theodore Ts'od9f34502011-04-30 13:47:24 -04004866 ext4_trim_extent(sb, start,
Lukas Czerner78944082011-05-24 18:16:27 -04004867 next - start, group, &e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04004868 count += next - start;
4869 }
Tao Ma169ddc32011-07-11 00:00:07 -04004870 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04004871 start = next + 1;
4872
4873 if (fatal_signal_pending(current)) {
4874 count = -ERESTARTSYS;
4875 break;
4876 }
4877
4878 if (need_resched()) {
4879 ext4_unlock_group(sb, group);
4880 cond_resched();
4881 ext4_lock_group(sb, group);
4882 }
4883
Tao Ma169ddc32011-07-11 00:00:07 -04004884 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04004885 break;
4886 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04004887
4888 if (!ret)
4889 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
4890out:
Lukas Czerner7360d172010-10-27 21:30:12 -04004891 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04004892 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04004893
4894 ext4_debug("trimmed %d blocks in the group %d\n",
4895 count, group);
4896
Lukas Czerner7360d172010-10-27 21:30:12 -04004897 return count;
4898}
4899
4900/**
4901 * ext4_trim_fs() -- trim ioctl handle function
4902 * @sb: superblock for filesystem
4903 * @range: fstrim_range structure
4904 *
4905 * start: First Byte to trim
4906 * len: number of Bytes to trim from start
4907 * minlen: minimum extent length in Bytes
4908 * ext4_trim_fs goes through all allocation groups containing Bytes from
4909 * start to start+len. For each such a group ext4_trim_all_free function
4910 * is invoked to trim all free space.
4911 */
4912int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
4913{
Lukas Czerner78944082011-05-24 18:16:27 -04004914 struct ext4_group_info *grp;
Lukas Czerner7360d172010-10-27 21:30:12 -04004915 ext4_group_t first_group, last_group;
4916 ext4_group_t group, ngroups = ext4_get_groups_count(sb);
4917 ext4_grpblk_t cnt = 0, first_block, last_block;
Lukas Czerner78944082011-05-24 18:16:27 -04004918 uint64_t start, len, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004919 ext4_fsblk_t first_data_blk =
4920 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner7360d172010-10-27 21:30:12 -04004921 int ret = 0;
4922
4923 start = range->start >> sb->s_blocksize_bits;
4924 len = range->len >> sb->s_blocksize_bits;
4925 minlen = range->minlen >> sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04004926
4927 if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
4928 return -EINVAL;
Tao Ma22f10452011-07-10 23:52:37 -04004929 if (start + len <= first_data_blk)
4930 goto out;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004931 if (start < first_data_blk) {
4932 len -= first_data_blk - start;
4933 start = first_data_blk;
4934 }
Lukas Czerner7360d172010-10-27 21:30:12 -04004935
4936 /* Determine first and last group to examine based on start and len */
4937 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
4938 &first_group, &first_block);
4939 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) (start + len),
4940 &last_group, &last_block);
4941 last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
4942 last_block = EXT4_BLOCKS_PER_GROUP(sb);
4943
4944 if (first_group > last_group)
4945 return -EINVAL;
4946
4947 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04004948 grp = ext4_get_group_info(sb, group);
4949 /* We only do this if the grp has never been initialized */
4950 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
4951 ret = ext4_mb_init_group(sb, group);
4952 if (ret)
4953 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04004954 }
4955
Tao Ma0ba08512011-03-23 15:48:11 -04004956 /*
4957 * For all the groups except the last one, last block will
4958 * always be EXT4_BLOCKS_PER_GROUP(sb), so we only need to
4959 * change it for the last group in which case start +
4960 * len < EXT4_BLOCKS_PER_GROUP(sb).
4961 */
4962 if (first_block + len < EXT4_BLOCKS_PER_GROUP(sb))
Jan Karaca6e9092011-01-10 12:30:39 -05004963 last_block = first_block + len;
Tao Ma0ba08512011-03-23 15:48:11 -04004964 len -= last_block - first_block;
Lukas Czerner7360d172010-10-27 21:30:12 -04004965
Lukas Czerner78944082011-05-24 18:16:27 -04004966 if (grp->bb_free >= minlen) {
4967 cnt = ext4_trim_all_free(sb, group, first_block,
Lukas Czerner7360d172010-10-27 21:30:12 -04004968 last_block, minlen);
4969 if (cnt < 0) {
4970 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04004971 break;
4972 }
4973 }
Lukas Czerner7360d172010-10-27 21:30:12 -04004974 trimmed += cnt;
4975 first_block = 0;
4976 }
4977 range->len = trimmed * sb->s_blocksize;
4978
Tao Ma3d56b8d2011-07-11 00:03:38 -04004979 if (!ret)
4980 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
4981
Tao Ma22f10452011-07-10 23:52:37 -04004982out:
Lukas Czerner7360d172010-10-27 21:30:12 -04004983 return ret;
4984}