blob: 1816eafb0a0537b5f43129b678f0f6e042e253f0 [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
78 * space we will consume the particular prealloc space. This make sure that
79 * that the we have contiguous physical blocks representing the file blocks
80 *
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
87 * prealloc space. These are per CPU prealloc list repreasented as
88 *
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
155 * can used for allocation. ext4_mb_good_group explains how the groups are
156 * 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;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001133 e4b->alloc_semp = &grp->alloc_sem;
1134
1135 /* Take the read lock on the group alloc
1136 * sem. This would make sure a parallel
1137 * ext4_mb_init_group happening on other
1138 * groups mapped by the page is blocked
1139 * till we are done with allocation
1140 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001141repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001142 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001143
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001144 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1145 /* we need to check for group need init flag
1146 * with alloc_semp held so that we can be sure
1147 * that new blocks didn't get added to the group
1148 * when we are loading the buddy cache
1149 */
1150 up_read(e4b->alloc_semp);
1151 /*
1152 * we need full data about the group
1153 * to make a good selection
1154 */
1155 ret = ext4_mb_init_group(sb, group);
1156 if (ret)
1157 return ret;
1158 goto repeat_load_buddy;
1159 }
1160
Alex Tomasc9de5602008-01-29 00:19:52 -05001161 /*
1162 * the buddy cache inode stores the block bitmap
1163 * and buddy information in consecutive blocks.
1164 * So for each group we need two blocks.
1165 */
1166 block = group * 2;
1167 pnum = block / blocks_per_page;
1168 poff = block % blocks_per_page;
1169
1170 /* we could use find_or_create_page(), but it locks page
1171 * what we'd like to avoid in fast path ... */
1172 page = find_get_page(inode->i_mapping, pnum);
1173 if (page == NULL || !PageUptodate(page)) {
1174 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001175 /*
1176 * drop the page reference and try
1177 * to get the page with lock. If we
1178 * are not uptodate that implies
1179 * somebody just created the page but
1180 * is yet to initialize the same. So
1181 * wait for it to initialize.
1182 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001183 page_cache_release(page);
1184 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1185 if (page) {
1186 BUG_ON(page->mapping != inode->i_mapping);
1187 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001188 ret = ext4_mb_init_cache(page, NULL);
1189 if (ret) {
1190 unlock_page(page);
1191 goto err;
1192 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001193 mb_cmp_bitmaps(e4b, page_address(page) +
1194 (poff * sb->s_blocksize));
1195 }
1196 unlock_page(page);
1197 }
1198 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001199 if (page == NULL || !PageUptodate(page)) {
1200 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001201 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001202 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001203 e4b->bd_bitmap_page = page;
1204 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1205 mark_page_accessed(page);
1206
1207 block++;
1208 pnum = block / blocks_per_page;
1209 poff = block % blocks_per_page;
1210
1211 page = find_get_page(inode->i_mapping, pnum);
1212 if (page == NULL || !PageUptodate(page)) {
1213 if (page)
1214 page_cache_release(page);
1215 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1216 if (page) {
1217 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001218 if (!PageUptodate(page)) {
1219 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1220 if (ret) {
1221 unlock_page(page);
1222 goto err;
1223 }
1224 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001225 unlock_page(page);
1226 }
1227 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001228 if (page == NULL || !PageUptodate(page)) {
1229 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001230 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001231 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001232 e4b->bd_buddy_page = page;
1233 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1234 mark_page_accessed(page);
1235
1236 BUG_ON(e4b->bd_bitmap_page == NULL);
1237 BUG_ON(e4b->bd_buddy_page == NULL);
1238
1239 return 0;
1240
1241err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001242 if (page)
1243 page_cache_release(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001244 if (e4b->bd_bitmap_page)
1245 page_cache_release(e4b->bd_bitmap_page);
1246 if (e4b->bd_buddy_page)
1247 page_cache_release(e4b->bd_buddy_page);
1248 e4b->bd_buddy = NULL;
1249 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001250
1251 /* Done with the buddy cache */
1252 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001253 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001254}
1255
Jing Zhange39e07f2010-05-14 00:00:00 -04001256static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001257{
1258 if (e4b->bd_bitmap_page)
1259 page_cache_release(e4b->bd_bitmap_page);
1260 if (e4b->bd_buddy_page)
1261 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001262 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001263 if (e4b->alloc_semp)
1264 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001265}
1266
1267
1268static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1269{
1270 int order = 1;
1271 void *bb;
1272
1273 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1274 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1275
1276 bb = EXT4_MB_BUDDY(e4b);
1277 while (order <= e4b->bd_blkbits + 1) {
1278 block = block >> 1;
1279 if (!mb_test_bit(block, bb)) {
1280 /* this block is part of buddy of order 'order' */
1281 return order;
1282 }
1283 bb += 1 << (e4b->bd_blkbits - order);
1284 order++;
1285 }
1286 return 0;
1287}
1288
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001289static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001290{
1291 __u32 *addr;
1292
1293 len = cur + len;
1294 while (cur < len) {
1295 if ((cur & 31) == 0 && (len - cur) >= 32) {
1296 /* fast path: clear whole word at once */
1297 addr = bm + (cur >> 3);
1298 *addr = 0;
1299 cur += 32;
1300 continue;
1301 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001302 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001303 cur++;
1304 }
1305}
1306
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001307static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001308{
1309 __u32 *addr;
1310
1311 len = cur + len;
1312 while (cur < len) {
1313 if ((cur & 31) == 0 && (len - cur) >= 32) {
1314 /* fast path: set whole word at once */
1315 addr = bm + (cur >> 3);
1316 *addr = 0xffffffff;
1317 cur += 32;
1318 continue;
1319 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001320 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001321 cur++;
1322 }
1323}
1324
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001325static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001326 int first, int count)
1327{
1328 int block = 0;
1329 int max = 0;
1330 int order;
1331 void *buddy;
1332 void *buddy2;
1333 struct super_block *sb = e4b->bd_sb;
1334
1335 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001336 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001337 mb_check_buddy(e4b);
1338 mb_free_blocks_double(inode, e4b, first, count);
1339
1340 e4b->bd_info->bb_free += count;
1341 if (first < e4b->bd_info->bb_first_free)
1342 e4b->bd_info->bb_first_free = first;
1343
1344 /* let's maintain fragments counter */
1345 if (first != 0)
1346 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1347 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1348 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1349 if (block && max)
1350 e4b->bd_info->bb_fragments--;
1351 else if (!block && !max)
1352 e4b->bd_info->bb_fragments++;
1353
1354 /* let's maintain buddy itself */
1355 while (count-- > 0) {
1356 block = first++;
1357 order = 0;
1358
1359 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1360 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001361
1362 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001363 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001364 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001365 inode ? inode->i_ino : 0,
1366 blocknr,
1367 "freeing already freed block "
1368 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001369 }
1370 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1371 e4b->bd_info->bb_counters[order]++;
1372
1373 /* start of the buddy */
1374 buddy = mb_find_buddy(e4b, order, &max);
1375
1376 do {
1377 block &= ~1UL;
1378 if (mb_test_bit(block, buddy) ||
1379 mb_test_bit(block + 1, buddy))
1380 break;
1381
1382 /* both the buddies are free, try to coalesce them */
1383 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1384
1385 if (!buddy2)
1386 break;
1387
1388 if (order > 0) {
1389 /* for special purposes, we don't set
1390 * free bits in bitmap */
1391 mb_set_bit(block, buddy);
1392 mb_set_bit(block + 1, buddy);
1393 }
1394 e4b->bd_info->bb_counters[order]--;
1395 e4b->bd_info->bb_counters[order]--;
1396
1397 block = block >> 1;
1398 order++;
1399 e4b->bd_info->bb_counters[order]++;
1400
1401 mb_clear_bit(block, buddy2);
1402 buddy = buddy2;
1403 } while (1);
1404 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001405 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001406 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001407}
1408
1409static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1410 int needed, struct ext4_free_extent *ex)
1411{
1412 int next = block;
1413 int max;
1414 int ord;
1415 void *buddy;
1416
Vincent Minetbc8e6742009-05-15 08:33:18 -04001417 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001418 BUG_ON(ex == NULL);
1419
1420 buddy = mb_find_buddy(e4b, order, &max);
1421 BUG_ON(buddy == NULL);
1422 BUG_ON(block >= max);
1423 if (mb_test_bit(block, buddy)) {
1424 ex->fe_len = 0;
1425 ex->fe_start = 0;
1426 ex->fe_group = 0;
1427 return 0;
1428 }
1429
1430 /* FIXME dorp order completely ? */
1431 if (likely(order == 0)) {
1432 /* find actual order */
1433 order = mb_find_order_for_block(e4b, block);
1434 block = block >> order;
1435 }
1436
1437 ex->fe_len = 1 << order;
1438 ex->fe_start = block << order;
1439 ex->fe_group = e4b->bd_group;
1440
1441 /* calc difference from given start */
1442 next = next - ex->fe_start;
1443 ex->fe_len -= next;
1444 ex->fe_start += next;
1445
1446 while (needed > ex->fe_len &&
1447 (buddy = mb_find_buddy(e4b, order, &max))) {
1448
1449 if (block + 1 >= max)
1450 break;
1451
1452 next = (block + 1) * (1 << order);
1453 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1454 break;
1455
1456 ord = mb_find_order_for_block(e4b, next);
1457
1458 order = ord;
1459 block = next >> order;
1460 ex->fe_len += 1 << order;
1461 }
1462
1463 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1464 return ex->fe_len;
1465}
1466
1467static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1468{
1469 int ord;
1470 int mlen = 0;
1471 int max = 0;
1472 int cur;
1473 int start = ex->fe_start;
1474 int len = ex->fe_len;
1475 unsigned ret = 0;
1476 int len0 = len;
1477 void *buddy;
1478
1479 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1480 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001481 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001482 mb_check_buddy(e4b);
1483 mb_mark_used_double(e4b, start, len);
1484
1485 e4b->bd_info->bb_free -= len;
1486 if (e4b->bd_info->bb_first_free == start)
1487 e4b->bd_info->bb_first_free += len;
1488
1489 /* let's maintain fragments counter */
1490 if (start != 0)
1491 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1492 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1493 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1494 if (mlen && max)
1495 e4b->bd_info->bb_fragments++;
1496 else if (!mlen && !max)
1497 e4b->bd_info->bb_fragments--;
1498
1499 /* let's maintain buddy itself */
1500 while (len) {
1501 ord = mb_find_order_for_block(e4b, start);
1502
1503 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1504 /* the whole chunk may be allocated at once! */
1505 mlen = 1 << ord;
1506 buddy = mb_find_buddy(e4b, ord, &max);
1507 BUG_ON((start >> ord) >= max);
1508 mb_set_bit(start >> ord, buddy);
1509 e4b->bd_info->bb_counters[ord]--;
1510 start += mlen;
1511 len -= mlen;
1512 BUG_ON(len < 0);
1513 continue;
1514 }
1515
1516 /* store for history */
1517 if (ret == 0)
1518 ret = len | (ord << 16);
1519
1520 /* we have to split large buddy */
1521 BUG_ON(ord <= 0);
1522 buddy = mb_find_buddy(e4b, ord, &max);
1523 mb_set_bit(start >> ord, buddy);
1524 e4b->bd_info->bb_counters[ord]--;
1525
1526 ord--;
1527 cur = (start >> ord) & ~1U;
1528 buddy = mb_find_buddy(e4b, ord, &max);
1529 mb_clear_bit(cur, buddy);
1530 mb_clear_bit(cur + 1, buddy);
1531 e4b->bd_info->bb_counters[ord]++;
1532 e4b->bd_info->bb_counters[ord]++;
1533 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001534 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001535
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001536 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001537 mb_check_buddy(e4b);
1538
1539 return ret;
1540}
1541
1542/*
1543 * Must be called under group lock!
1544 */
1545static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1546 struct ext4_buddy *e4b)
1547{
1548 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1549 int ret;
1550
1551 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1552 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1553
1554 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1555 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1556 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1557
1558 /* preallocation can change ac_b_ex, thus we store actually
1559 * allocated blocks for history */
1560 ac->ac_f_ex = ac->ac_b_ex;
1561
1562 ac->ac_status = AC_STATUS_FOUND;
1563 ac->ac_tail = ret & 0xffff;
1564 ac->ac_buddy = ret >> 16;
1565
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001566 /*
1567 * take the page reference. We want the page to be pinned
1568 * so that we don't get a ext4_mb_init_cache_call for this
1569 * group until we update the bitmap. That would mean we
1570 * double allocate blocks. The reference is dropped
1571 * in ext4_mb_release_context
1572 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001573 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1574 get_page(ac->ac_bitmap_page);
1575 ac->ac_buddy_page = e4b->bd_buddy_page;
1576 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001577 /* on allocation we use ac to track the held semaphore */
1578 ac->alloc_semp = e4b->alloc_semp;
1579 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001580 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001581 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001582 spin_lock(&sbi->s_md_lock);
1583 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1584 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1585 spin_unlock(&sbi->s_md_lock);
1586 }
1587}
1588
1589/*
1590 * regular allocator, for general purposes allocation
1591 */
1592
1593static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1594 struct ext4_buddy *e4b,
1595 int finish_group)
1596{
1597 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1598 struct ext4_free_extent *bex = &ac->ac_b_ex;
1599 struct ext4_free_extent *gex = &ac->ac_g_ex;
1600 struct ext4_free_extent ex;
1601 int max;
1602
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001603 if (ac->ac_status == AC_STATUS_FOUND)
1604 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001605 /*
1606 * We don't want to scan for a whole year
1607 */
1608 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1609 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1610 ac->ac_status = AC_STATUS_BREAK;
1611 return;
1612 }
1613
1614 /*
1615 * Haven't found good chunk so far, let's continue
1616 */
1617 if (bex->fe_len < gex->fe_len)
1618 return;
1619
1620 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1621 && bex->fe_group == e4b->bd_group) {
1622 /* recheck chunk's availability - we don't know
1623 * when it was found (within this lock-unlock
1624 * period or not) */
1625 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1626 if (max >= gex->fe_len) {
1627 ext4_mb_use_best_found(ac, e4b);
1628 return;
1629 }
1630 }
1631}
1632
1633/*
1634 * The routine checks whether found extent is good enough. If it is,
1635 * then the extent gets marked used and flag is set to the context
1636 * to stop scanning. Otherwise, the extent is compared with the
1637 * previous found extent and if new one is better, then it's stored
1638 * in the context. Later, the best found extent will be used, if
1639 * mballoc can't find good enough extent.
1640 *
1641 * FIXME: real allocation policy is to be designed yet!
1642 */
1643static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1644 struct ext4_free_extent *ex,
1645 struct ext4_buddy *e4b)
1646{
1647 struct ext4_free_extent *bex = &ac->ac_b_ex;
1648 struct ext4_free_extent *gex = &ac->ac_g_ex;
1649
1650 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001651 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001652 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1653 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1654
1655 ac->ac_found++;
1656
1657 /*
1658 * The special case - take what you catch first
1659 */
1660 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1661 *bex = *ex;
1662 ext4_mb_use_best_found(ac, e4b);
1663 return;
1664 }
1665
1666 /*
1667 * Let's check whether the chuck is good enough
1668 */
1669 if (ex->fe_len == gex->fe_len) {
1670 *bex = *ex;
1671 ext4_mb_use_best_found(ac, e4b);
1672 return;
1673 }
1674
1675 /*
1676 * If this is first found extent, just store it in the context
1677 */
1678 if (bex->fe_len == 0) {
1679 *bex = *ex;
1680 return;
1681 }
1682
1683 /*
1684 * If new found extent is better, store it in the context
1685 */
1686 if (bex->fe_len < gex->fe_len) {
1687 /* if the request isn't satisfied, any found extent
1688 * larger than previous best one is better */
1689 if (ex->fe_len > bex->fe_len)
1690 *bex = *ex;
1691 } else if (ex->fe_len > gex->fe_len) {
1692 /* if the request is satisfied, then we try to find
1693 * an extent that still satisfy the request, but is
1694 * smaller than previous one */
1695 if (ex->fe_len < bex->fe_len)
1696 *bex = *ex;
1697 }
1698
1699 ext4_mb_check_limits(ac, e4b, 0);
1700}
1701
Eric Sandeen089ceec2009-07-05 22:17:31 -04001702static noinline_for_stack
1703int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001704 struct ext4_buddy *e4b)
1705{
1706 struct ext4_free_extent ex = ac->ac_b_ex;
1707 ext4_group_t group = ex.fe_group;
1708 int max;
1709 int err;
1710
1711 BUG_ON(ex.fe_len <= 0);
1712 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1713 if (err)
1714 return err;
1715
1716 ext4_lock_group(ac->ac_sb, group);
1717 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1718
1719 if (max > 0) {
1720 ac->ac_b_ex = ex;
1721 ext4_mb_use_best_found(ac, e4b);
1722 }
1723
1724 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001725 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001726
1727 return 0;
1728}
1729
Eric Sandeen089ceec2009-07-05 22:17:31 -04001730static noinline_for_stack
1731int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001732 struct ext4_buddy *e4b)
1733{
1734 ext4_group_t group = ac->ac_g_ex.fe_group;
1735 int max;
1736 int err;
1737 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001738 struct ext4_free_extent ex;
1739
1740 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1741 return 0;
1742
1743 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1744 if (err)
1745 return err;
1746
1747 ext4_lock_group(ac->ac_sb, group);
1748 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1749 ac->ac_g_ex.fe_len, &ex);
1750
1751 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1752 ext4_fsblk_t start;
1753
Akinobu Mita5661bd62010-03-03 23:53:39 -05001754 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1755 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001756 /* use do_div to get remainder (would be 64-bit modulo) */
1757 if (do_div(start, sbi->s_stripe) == 0) {
1758 ac->ac_found++;
1759 ac->ac_b_ex = ex;
1760 ext4_mb_use_best_found(ac, e4b);
1761 }
1762 } else if (max >= ac->ac_g_ex.fe_len) {
1763 BUG_ON(ex.fe_len <= 0);
1764 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1765 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1766 ac->ac_found++;
1767 ac->ac_b_ex = ex;
1768 ext4_mb_use_best_found(ac, e4b);
1769 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1770 /* Sometimes, caller may want to merge even small
1771 * number of blocks to an existing extent */
1772 BUG_ON(ex.fe_len <= 0);
1773 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1774 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1775 ac->ac_found++;
1776 ac->ac_b_ex = ex;
1777 ext4_mb_use_best_found(ac, e4b);
1778 }
1779 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001780 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001781
1782 return 0;
1783}
1784
1785/*
1786 * The routine scans buddy structures (not bitmap!) from given order
1787 * to max order and tries to find big enough chunk to satisfy the req
1788 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001789static noinline_for_stack
1790void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001791 struct ext4_buddy *e4b)
1792{
1793 struct super_block *sb = ac->ac_sb;
1794 struct ext4_group_info *grp = e4b->bd_info;
1795 void *buddy;
1796 int i;
1797 int k;
1798 int max;
1799
1800 BUG_ON(ac->ac_2order <= 0);
1801 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1802 if (grp->bb_counters[i] == 0)
1803 continue;
1804
1805 buddy = mb_find_buddy(e4b, i, &max);
1806 BUG_ON(buddy == NULL);
1807
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001808 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001809 BUG_ON(k >= max);
1810
1811 ac->ac_found++;
1812
1813 ac->ac_b_ex.fe_len = 1 << i;
1814 ac->ac_b_ex.fe_start = k << i;
1815 ac->ac_b_ex.fe_group = e4b->bd_group;
1816
1817 ext4_mb_use_best_found(ac, e4b);
1818
1819 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1820
1821 if (EXT4_SB(sb)->s_mb_stats)
1822 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1823
1824 break;
1825 }
1826}
1827
1828/*
1829 * The routine scans the group and measures all found extents.
1830 * In order to optimize scanning, caller must pass number of
1831 * free blocks in the group, so the routine can know upper limit.
1832 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001833static noinline_for_stack
1834void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001835 struct ext4_buddy *e4b)
1836{
1837 struct super_block *sb = ac->ac_sb;
1838 void *bitmap = EXT4_MB_BITMAP(e4b);
1839 struct ext4_free_extent ex;
1840 int i;
1841 int free;
1842
1843 free = e4b->bd_info->bb_free;
1844 BUG_ON(free <= 0);
1845
1846 i = e4b->bd_info->bb_first_free;
1847
1848 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001849 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001850 EXT4_BLOCKS_PER_GROUP(sb), i);
1851 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001852 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001853 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001854 * free blocks even though group info says we
1855 * we have free blocks
1856 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001857 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1858 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001859 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001860 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001861 break;
1862 }
1863
1864 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1865 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001866 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001867 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1868 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001869 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001870 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001871 /*
1872 * The number of free blocks differs. This mostly
1873 * indicate that the bitmap is corrupt. So exit
1874 * without claiming the space.
1875 */
1876 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001877 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001878
1879 ext4_mb_measure_extent(ac, &ex, e4b);
1880
1881 i += ex.fe_len;
1882 free -= ex.fe_len;
1883 }
1884
1885 ext4_mb_check_limits(ac, e4b, 1);
1886}
1887
1888/*
1889 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001890 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001891 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001892static noinline_for_stack
1893void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001894 struct ext4_buddy *e4b)
1895{
1896 struct super_block *sb = ac->ac_sb;
1897 struct ext4_sb_info *sbi = EXT4_SB(sb);
1898 void *bitmap = EXT4_MB_BITMAP(e4b);
1899 struct ext4_free_extent ex;
1900 ext4_fsblk_t first_group_block;
1901 ext4_fsblk_t a;
1902 ext4_grpblk_t i;
1903 int max;
1904
1905 BUG_ON(sbi->s_stripe == 0);
1906
1907 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001908 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1909
Alex Tomasc9de5602008-01-29 00:19:52 -05001910 a = first_group_block + sbi->s_stripe - 1;
1911 do_div(a, sbi->s_stripe);
1912 i = (a * sbi->s_stripe) - first_group_block;
1913
1914 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1915 if (!mb_test_bit(i, bitmap)) {
1916 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1917 if (max >= sbi->s_stripe) {
1918 ac->ac_found++;
1919 ac->ac_b_ex = ex;
1920 ext4_mb_use_best_found(ac, e4b);
1921 break;
1922 }
1923 }
1924 i += sbi->s_stripe;
1925 }
1926}
1927
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001928/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001929static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1930 ext4_group_t group, int cr)
1931{
1932 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001933 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001934 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1935
1936 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001937
1938 /* We only do this if the grp has never been initialized */
1939 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1940 int ret = ext4_mb_init_group(ac->ac_sb, group);
1941 if (ret)
1942 return 0;
1943 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001944
1945 free = grp->bb_free;
1946 fragments = grp->bb_fragments;
1947 if (free == 0)
1948 return 0;
1949 if (fragments == 0)
1950 return 0;
1951
1952 switch (cr) {
1953 case 0:
1954 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001955
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001956 if (grp->bb_largest_free_order < ac->ac_2order)
1957 return 0;
1958
Theodore Ts'oa4912122009-03-12 12:18:34 -04001959 /* Avoid using the first bg of a flexgroup for data files */
1960 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1961 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1962 ((group % flex_size) == 0))
1963 return 0;
1964
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001965 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001966 case 1:
1967 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1968 return 1;
1969 break;
1970 case 2:
1971 if (free >= ac->ac_g_ex.fe_len)
1972 return 1;
1973 break;
1974 case 3:
1975 return 1;
1976 default:
1977 BUG();
1978 }
1979
1980 return 0;
1981}
1982
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001983static noinline_for_stack int
1984ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001985{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001986 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001987 int cr;
1988 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001989 struct ext4_sb_info *sbi;
1990 struct super_block *sb;
1991 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001992
1993 sb = ac->ac_sb;
1994 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001995 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001996 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001997 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001998 ngroups = sbi->s_blockfile_groups;
1999
Alex Tomasc9de5602008-01-29 00:19:52 -05002000 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2001
2002 /* first, try the goal */
2003 err = ext4_mb_find_by_goal(ac, &e4b);
2004 if (err || ac->ac_status == AC_STATUS_FOUND)
2005 goto out;
2006
2007 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2008 goto out;
2009
2010 /*
2011 * ac->ac2_order is set only if the fe_len is a power of 2
2012 * if ac2_order is set we also set criteria to 0 so that we
2013 * try exact allocation using buddy.
2014 */
2015 i = fls(ac->ac_g_ex.fe_len);
2016 ac->ac_2order = 0;
2017 /*
2018 * We search using buddy data only if the order of the request
2019 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002020 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05002021 */
2022 if (i >= sbi->s_mb_order2_reqs) {
2023 /*
2024 * This should tell if fe_len is exactly power of 2
2025 */
2026 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2027 ac->ac_2order = i - 1;
2028 }
2029
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002030 /* if stream allocation is enabled, use global goal */
2031 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002032 /* TBD: may be hot point */
2033 spin_lock(&sbi->s_md_lock);
2034 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2035 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2036 spin_unlock(&sbi->s_md_lock);
2037 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002038
Alex Tomasc9de5602008-01-29 00:19:52 -05002039 /* Let's just scan groups to find more-less suitable blocks */
2040 cr = ac->ac_2order ? 0 : 1;
2041 /*
2042 * cr == 0 try to get exact allocation,
2043 * cr == 3 try to get anything
2044 */
2045repeat:
2046 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2047 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002048 /*
2049 * searching for the right group start
2050 * from the goal value specified
2051 */
2052 group = ac->ac_g_ex.fe_group;
2053
Theodore Ts'o8df96752009-05-01 08:50:38 -04002054 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002055 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002056 group = 0;
2057
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002058 /* This now checks without needing the buddy page */
2059 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002060 continue;
2061
Alex Tomasc9de5602008-01-29 00:19:52 -05002062 err = ext4_mb_load_buddy(sb, group, &e4b);
2063 if (err)
2064 goto out;
2065
2066 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002067
2068 /*
2069 * We need to check again after locking the
2070 * block group
2071 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002072 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002073 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002074 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002075 continue;
2076 }
2077
2078 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002079 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002080 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002081 else if (cr == 1 && sbi->s_stripe &&
2082 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002083 ext4_mb_scan_aligned(ac, &e4b);
2084 else
2085 ext4_mb_complex_scan_group(ac, &e4b);
2086
2087 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002088 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002089
2090 if (ac->ac_status != AC_STATUS_CONTINUE)
2091 break;
2092 }
2093 }
2094
2095 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2096 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2097 /*
2098 * We've been searching too long. Let's try to allocate
2099 * the best chunk we've found so far
2100 */
2101
2102 ext4_mb_try_best_found(ac, &e4b);
2103 if (ac->ac_status != AC_STATUS_FOUND) {
2104 /*
2105 * Someone more lucky has already allocated it.
2106 * The only thing we can do is just take first
2107 * found block(s)
2108 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2109 */
2110 ac->ac_b_ex.fe_group = 0;
2111 ac->ac_b_ex.fe_start = 0;
2112 ac->ac_b_ex.fe_len = 0;
2113 ac->ac_status = AC_STATUS_CONTINUE;
2114 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2115 cr = 3;
2116 atomic_inc(&sbi->s_mb_lost_chunks);
2117 goto repeat;
2118 }
2119 }
2120out:
2121 return err;
2122}
2123
Alex Tomasc9de5602008-01-29 00:19:52 -05002124static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2125{
2126 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002127 ext4_group_t group;
2128
Theodore Ts'o8df96752009-05-01 08:50:38 -04002129 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002130 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002131 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002132 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002133}
2134
2135static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2136{
2137 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002138 ext4_group_t group;
2139
2140 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002141 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002142 return NULL;
2143 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002144 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002145}
2146
2147static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2148{
2149 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002150 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002151 int i;
2152 int err;
2153 struct ext4_buddy e4b;
2154 struct sg {
2155 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002156 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002157 } sg;
2158
2159 group--;
2160 if (group == 0)
2161 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2162 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2163 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2164 "group", "free", "frags", "first",
2165 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2166 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2167
2168 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2169 sizeof(struct ext4_group_info);
2170 err = ext4_mb_load_buddy(sb, group, &e4b);
2171 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002172 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002173 return 0;
2174 }
2175 ext4_lock_group(sb, group);
2176 memcpy(&sg, ext4_get_group_info(sb, group), i);
2177 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002178 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002179
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002180 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002181 sg.info.bb_fragments, sg.info.bb_first_free);
2182 for (i = 0; i <= 13; i++)
2183 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2184 sg.info.bb_counters[i] : 0);
2185 seq_printf(seq, " ]\n");
2186
2187 return 0;
2188}
2189
2190static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2191{
2192}
2193
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002194static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002195 .start = ext4_mb_seq_groups_start,
2196 .next = ext4_mb_seq_groups_next,
2197 .stop = ext4_mb_seq_groups_stop,
2198 .show = ext4_mb_seq_groups_show,
2199};
2200
2201static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2202{
2203 struct super_block *sb = PDE(inode)->data;
2204 int rc;
2205
2206 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2207 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002208 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002209 m->private = sb;
2210 }
2211 return rc;
2212
2213}
2214
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002215static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002216 .owner = THIS_MODULE,
2217 .open = ext4_mb_seq_groups_open,
2218 .read = seq_read,
2219 .llseek = seq_lseek,
2220 .release = seq_release,
2221};
2222
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002223static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2224{
2225 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2226 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2227
2228 BUG_ON(!cachep);
2229 return cachep;
2230}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002231
2232/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002233int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002234 struct ext4_group_desc *desc)
2235{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002236 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002237 int metalen = 0;
2238 struct ext4_sb_info *sbi = EXT4_SB(sb);
2239 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002240 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002241
2242 /*
2243 * First check if this group is the first of a reserved block.
2244 * If it's true, we have to allocate a new table of pointers
2245 * to ext4_group_info structures
2246 */
2247 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2248 metalen = sizeof(*meta_group_info) <<
2249 EXT4_DESC_PER_BLOCK_BITS(sb);
2250 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2251 if (meta_group_info == NULL) {
2252 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2253 "buddy group\n");
2254 goto exit_meta_group_info;
2255 }
2256 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2257 meta_group_info;
2258 }
2259
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002260 meta_group_info =
2261 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2262 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2263
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002264 meta_group_info[i] = kmem_cache_alloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002265 if (meta_group_info[i] == NULL) {
2266 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2267 goto exit_group_info;
2268 }
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002269 memset(meta_group_info[i], 0, kmem_cache_size(cachep));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002270 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2271 &(meta_group_info[i]->bb_state));
2272
2273 /*
2274 * initialize bb_free to be able to skip
2275 * empty groups without initialization
2276 */
2277 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2278 meta_group_info[i]->bb_free =
2279 ext4_free_blocks_after_init(sb, group, desc);
2280 } else {
2281 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002282 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002283 }
2284
2285 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002286 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002287 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002288 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002289
2290#ifdef DOUBLE_CHECK
2291 {
2292 struct buffer_head *bh;
2293 meta_group_info[i]->bb_bitmap =
2294 kmalloc(sb->s_blocksize, GFP_KERNEL);
2295 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2296 bh = ext4_read_block_bitmap(sb, group);
2297 BUG_ON(bh == NULL);
2298 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2299 sb->s_blocksize);
2300 put_bh(bh);
2301 }
2302#endif
2303
2304 return 0;
2305
2306exit_group_info:
2307 /* If a meta_group_info table has been allocated, release it now */
2308 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2309 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2310exit_meta_group_info:
2311 return -ENOMEM;
2312} /* ext4_mb_add_groupinfo */
2313
Alex Tomasc9de5602008-01-29 00:19:52 -05002314static int ext4_mb_init_backend(struct super_block *sb)
2315{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002316 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002317 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002318 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002319 struct ext4_super_block *es = sbi->s_es;
2320 int num_meta_group_infos;
2321 int num_meta_group_infos_max;
2322 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002323 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002324 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002325
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002326 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002327 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002328 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2329
2330 /*
2331 * This is the total number of blocks used by GDT including
2332 * the number of reserved blocks for GDT.
2333 * The s_group_info array is allocated with this value
2334 * to allow a clean online resize without a complex
2335 * manipulation of pointer.
2336 * The drawback is the unused memory when no resize
2337 * occurs but it's very low in terms of pages
2338 * (see comments below)
2339 * Need to handle this properly when META_BG resizing is allowed
2340 */
2341 num_meta_group_infos_max = num_meta_group_infos +
2342 le16_to_cpu(es->s_reserved_gdt_blocks);
2343
2344 /*
2345 * array_size is the size of s_group_info array. We round it
2346 * to the next power of two because this approximation is done
2347 * internally by kmalloc so we can have some more memory
2348 * for free here (e.g. may be used for META_BG resize).
2349 */
2350 array_size = 1;
2351 while (array_size < sizeof(*sbi->s_group_info) *
2352 num_meta_group_infos_max)
2353 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002354 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2355 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2356 * So a two level scheme suffices for now. */
Eric Sandeen4596fe02011-03-21 21:25:13 -04002357 sbi->s_group_info = kzalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002358 if (sbi->s_group_info == NULL) {
2359 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2360 return -ENOMEM;
2361 }
2362 sbi->s_buddy_cache = new_inode(sb);
2363 if (sbi->s_buddy_cache == NULL) {
2364 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2365 goto err_freesgi;
2366 }
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002367 sbi->s_buddy_cache->i_ino = get_next_ino();
Alex Tomasc9de5602008-01-29 00:19:52 -05002368 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002369 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002370 desc = ext4_get_group_desc(sb, i, NULL);
2371 if (desc == NULL) {
2372 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002373 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002374 goto err_freebuddy;
2375 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002376 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2377 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002378 }
2379
2380 return 0;
2381
2382err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002383 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002384 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002385 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002386 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002387 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002388 kfree(sbi->s_group_info[i]);
2389 iput(sbi->s_buddy_cache);
2390err_freesgi:
2391 kfree(sbi->s_group_info);
2392 return -ENOMEM;
2393}
2394
Eric Sandeen2892c152011-02-12 08:12:18 -05002395static void ext4_groupinfo_destroy_slabs(void)
2396{
2397 int i;
2398
2399 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2400 if (ext4_groupinfo_caches[i])
2401 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2402 ext4_groupinfo_caches[i] = NULL;
2403 }
2404}
2405
2406static int ext4_groupinfo_create_slab(size_t size)
2407{
2408 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2409 int slab_size;
2410 int blocksize_bits = order_base_2(size);
2411 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2412 struct kmem_cache *cachep;
2413
2414 if (cache_index >= NR_GRPINFO_CACHES)
2415 return -EINVAL;
2416
2417 if (unlikely(cache_index < 0))
2418 cache_index = 0;
2419
2420 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2421 if (ext4_groupinfo_caches[cache_index]) {
2422 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2423 return 0; /* Already created */
2424 }
2425
2426 slab_size = offsetof(struct ext4_group_info,
2427 bb_counters[blocksize_bits + 2]);
2428
2429 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2430 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2431 NULL);
2432
2433 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2434 if (!cachep) {
2435 printk(KERN_EMERG "EXT4: no memory for groupinfo slab cache\n");
2436 return -ENOMEM;
2437 }
2438
2439 ext4_groupinfo_caches[cache_index] = cachep;
2440
2441 return 0;
2442}
2443
Alex Tomasc9de5602008-01-29 00:19:52 -05002444int ext4_mb_init(struct super_block *sb, int needs_recovery)
2445{
2446 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002447 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002448 unsigned offset;
2449 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002450 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002451
Eric Sandeen19278052009-08-25 22:36:25 -04002452 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002453
2454 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2455 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002456 ret = -ENOMEM;
2457 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002458 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002459
Eric Sandeen19278052009-08-25 22:36:25 -04002460 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002461 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2462 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002463 ret = -ENOMEM;
2464 goto out;
2465 }
2466
Eric Sandeen2892c152011-02-12 08:12:18 -05002467 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2468 if (ret < 0)
2469 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002470
2471 /* order 0 is regular bitmap */
2472 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2473 sbi->s_mb_offsets[0] = 0;
2474
2475 i = 1;
2476 offset = 0;
2477 max = sb->s_blocksize << 2;
2478 do {
2479 sbi->s_mb_offsets[i] = offset;
2480 sbi->s_mb_maxs[i] = max;
2481 offset += 1 << (sb->s_blocksize_bits - i);
2482 max = max >> 1;
2483 i++;
2484 } while (i <= sb->s_blocksize_bits + 1);
2485
2486 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002487 ret = ext4_mb_init_backend(sb);
2488 if (ret != 0) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002489 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002490 }
2491
2492 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002493 spin_lock_init(&sbi->s_bal_lock);
2494
2495 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2496 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2497 sbi->s_mb_stats = MB_DEFAULT_STATS;
2498 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2499 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002500 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2501
Eric Sandeen730c2132008-09-13 15:23:29 -04002502 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002503 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002504 ret = -ENOMEM;
2505 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002506 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002507 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002508 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002509 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002510 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002511 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2512 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002513 spin_lock_init(&lg->lg_prealloc_lock);
2514 }
2515
Theodore Ts'o296c3552009-09-30 00:32:42 -04002516 if (sbi->s_proc)
2517 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2518 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002519
Frank Mayhar03901312009-01-07 00:06:22 -05002520 if (sbi->s_journal)
2521 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002522out:
2523 if (ret) {
2524 kfree(sbi->s_mb_offsets);
2525 kfree(sbi->s_mb_maxs);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002526 }
2527 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002528}
2529
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002530/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002531static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2532{
2533 struct ext4_prealloc_space *pa;
2534 struct list_head *cur, *tmp;
2535 int count = 0;
2536
2537 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2538 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2539 list_del(&pa->pa_group_list);
2540 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002541 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002542 }
2543 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002544 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002545
2546}
2547
2548int ext4_mb_release(struct super_block *sb)
2549{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002550 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002551 ext4_group_t i;
2552 int num_meta_group_infos;
2553 struct ext4_group_info *grinfo;
2554 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002555 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002556
Alex Tomasc9de5602008-01-29 00:19:52 -05002557 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002558 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002559 grinfo = ext4_get_group_info(sb, i);
2560#ifdef DOUBLE_CHECK
2561 kfree(grinfo->bb_bitmap);
2562#endif
2563 ext4_lock_group(sb, i);
2564 ext4_mb_cleanup_pa(grinfo);
2565 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002566 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002567 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002568 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002569 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2570 EXT4_DESC_PER_BLOCK_BITS(sb);
2571 for (i = 0; i < num_meta_group_infos; i++)
2572 kfree(sbi->s_group_info[i]);
2573 kfree(sbi->s_group_info);
2574 }
2575 kfree(sbi->s_mb_offsets);
2576 kfree(sbi->s_mb_maxs);
2577 if (sbi->s_buddy_cache)
2578 iput(sbi->s_buddy_cache);
2579 if (sbi->s_mb_stats) {
2580 printk(KERN_INFO
2581 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2582 atomic_read(&sbi->s_bal_allocated),
2583 atomic_read(&sbi->s_bal_reqs),
2584 atomic_read(&sbi->s_bal_success));
2585 printk(KERN_INFO
2586 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2587 "%u 2^N hits, %u breaks, %u lost\n",
2588 atomic_read(&sbi->s_bal_ex_scanned),
2589 atomic_read(&sbi->s_bal_goals),
2590 atomic_read(&sbi->s_bal_2orders),
2591 atomic_read(&sbi->s_bal_breaks),
2592 atomic_read(&sbi->s_mb_lost_chunks));
2593 printk(KERN_INFO
2594 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2595 sbi->s_mb_buddies_generated++,
2596 sbi->s_mb_generation_time);
2597 printk(KERN_INFO
2598 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2599 atomic_read(&sbi->s_mb_preallocated),
2600 atomic_read(&sbi->s_mb_discarded));
2601 }
2602
Eric Sandeen730c2132008-09-13 15:23:29 -04002603 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002604 if (sbi->s_proc)
2605 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002606
2607 return 0;
2608}
2609
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002610static inline int ext4_issue_discard(struct super_block *sb,
Jiaying Zhang5c521832010-07-27 11:56:05 -04002611 ext4_group_t block_group, ext4_grpblk_t block, int count)
2612{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002613 ext4_fsblk_t discard_block;
2614
2615 discard_block = block + ext4_group_first_block_no(sb, block_group);
2616 trace_ext4_discard_blocks(sb,
2617 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002618 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002619}
2620
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002621/*
2622 * This function is called by the jbd2 layer once the commit has finished,
2623 * so we know we can free the blocks that were released with that commit.
2624 */
2625static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002626{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002627 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002628 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002629 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002630 int err, count = 0, count2 = 0;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002631 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002632 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002633
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002634 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2635 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002636
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002637 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002638 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002639
Theodore Ts'od9f34502011-04-30 13:47:24 -04002640 if (test_opt(sb, DISCARD))
2641 ext4_issue_discard(sb, entry->group,
2642 entry->start_blk, entry->count);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002643
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002644 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002645 /* we expect to find existing buddy because it's pinned */
2646 BUG_ON(err != 0);
2647
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002648 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002649 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002650 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002651 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002652 ext4_lock_group(sb, entry->group);
2653 /* Take it out of per group rb tree */
2654 rb_erase(&entry->node, &(db->bb_free_root));
2655 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2656
2657 if (!db->bb_free_root.rb_node) {
2658 /* No more items in the per group rb tree
2659 * balance refcounts from ext4_mb_free_metadata()
2660 */
2661 page_cache_release(e4b.bd_buddy_page);
2662 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002663 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002664 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002665 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002666 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002667 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002668
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002669 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002670}
2671
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002672#ifdef CONFIG_EXT4_DEBUG
2673u8 mb_enable_debug __read_mostly;
2674
2675static struct dentry *debugfs_dir;
2676static struct dentry *debugfs_debug;
2677
2678static void __init ext4_create_debugfs_entry(void)
2679{
2680 debugfs_dir = debugfs_create_dir("ext4", NULL);
2681 if (debugfs_dir)
2682 debugfs_debug = debugfs_create_u8("mballoc-debug",
2683 S_IRUGO | S_IWUSR,
2684 debugfs_dir,
2685 &mb_enable_debug);
2686}
2687
2688static void ext4_remove_debugfs_entry(void)
2689{
2690 debugfs_remove(debugfs_debug);
2691 debugfs_remove(debugfs_dir);
2692}
2693
2694#else
2695
2696static void __init ext4_create_debugfs_entry(void)
2697{
2698}
2699
2700static void ext4_remove_debugfs_entry(void)
2701{
2702}
2703
2704#endif
2705
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002706int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002707{
Theodore Ts'o16828082010-10-27 21:30:09 -04002708 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2709 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002710 if (ext4_pspace_cachep == NULL)
2711 return -ENOMEM;
2712
Theodore Ts'o16828082010-10-27 21:30:09 -04002713 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2714 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002715 if (ext4_ac_cachep == NULL) {
2716 kmem_cache_destroy(ext4_pspace_cachep);
2717 return -ENOMEM;
2718 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002719
Theodore Ts'o16828082010-10-27 21:30:09 -04002720 ext4_free_ext_cachep = KMEM_CACHE(ext4_free_data,
2721 SLAB_RECLAIM_ACCOUNT);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002722 if (ext4_free_ext_cachep == NULL) {
2723 kmem_cache_destroy(ext4_pspace_cachep);
2724 kmem_cache_destroy(ext4_ac_cachep);
2725 return -ENOMEM;
2726 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002727 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002728 return 0;
2729}
2730
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002731void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002732{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002733 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002734 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2735 * before destroying the slab cache.
2736 */
2737 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002738 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002739 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002740 kmem_cache_destroy(ext4_free_ext_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002741 ext4_groupinfo_destroy_slabs();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002742 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002743}
2744
2745
2746/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002747 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002748 * Returns 0 if success or error code
2749 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002750static noinline_for_stack int
2751ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002752 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002753{
2754 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002755 struct ext4_group_desc *gdp;
2756 struct buffer_head *gdp_bh;
2757 struct ext4_sb_info *sbi;
2758 struct super_block *sb;
2759 ext4_fsblk_t block;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002760 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002761
2762 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2763 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2764
2765 sb = ac->ac_sb;
2766 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002767
2768 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002769 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002770 if (!bitmap_bh)
2771 goto out_err;
2772
2773 err = ext4_journal_get_write_access(handle, bitmap_bh);
2774 if (err)
2775 goto out_err;
2776
2777 err = -EIO;
2778 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2779 if (!gdp)
2780 goto out_err;
2781
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002782 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002783 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002784
Alex Tomasc9de5602008-01-29 00:19:52 -05002785 err = ext4_journal_get_write_access(handle, gdp_bh);
2786 if (err)
2787 goto out_err;
2788
Akinobu Mitabda00de2010-03-03 23:53:25 -05002789 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002790
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002791 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002792 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002793 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002794 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002795 /* File system mounted not to panic on error
2796 * Fix the bitmap and repeat the block allocation
2797 * We leak some of the blocks here.
2798 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002799 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2800 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2801 ac->ac_b_ex.fe_len);
2802 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002803 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002804 if (!err)
2805 err = -EAGAIN;
2806 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002807 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002808
2809 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002810#ifdef AGGRESSIVE_CHECK
2811 {
2812 int i;
2813 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2814 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2815 bitmap_bh->b_data));
2816 }
2817 }
2818#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002819 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 -05002820 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2821 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002822 ext4_free_blks_set(sb, gdp,
2823 ext4_free_blocks_after_init(sb,
2824 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002825 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002826 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2827 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002828 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002829
2830 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002831 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002832 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002833 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002834 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002835 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2836 /* release all the reserved blocks if non delalloc */
2837 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002838
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002839 if (sbi->s_log_groups_per_flex) {
2840 ext4_group_t flex_group = ext4_flex_group(sbi,
2841 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002842 atomic_sub(ac->ac_b_ex.fe_len,
2843 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002844 }
2845
Frank Mayhar03901312009-01-07 00:06:22 -05002846 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002847 if (err)
2848 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002849 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002850
2851out_err:
Theodore Ts'oa0375152010-06-11 23:14:04 -04002852 ext4_mark_super_dirty(sb);
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002853 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002854 return err;
2855}
2856
2857/*
2858 * here we normalize request for locality group
2859 * Group request are normalized to s_strip size if we set the same via mount
2860 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002861 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002862 *
2863 * XXX: should we try to preallocate more than the group has now?
2864 */
2865static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2866{
2867 struct super_block *sb = ac->ac_sb;
2868 struct ext4_locality_group *lg = ac->ac_lg;
2869
2870 BUG_ON(lg == NULL);
2871 if (EXT4_SB(sb)->s_stripe)
2872 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2873 else
2874 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002875 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002876 current->pid, ac->ac_g_ex.fe_len);
2877}
2878
2879/*
2880 * Normalization means making request better in terms of
2881 * size and alignment
2882 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002883static noinline_for_stack void
2884ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002885 struct ext4_allocation_request *ar)
2886{
2887 int bsbits, max;
2888 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002889 loff_t size, orig_size, start_off;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002890 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002891 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002892 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002893
2894 /* do normalize only data requests, metadata requests
2895 do not need preallocation */
2896 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2897 return;
2898
2899 /* sometime caller may want exact blocks */
2900 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2901 return;
2902
2903 /* caller may indicate that preallocation isn't
2904 * required (it's a tail, for example) */
2905 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2906 return;
2907
2908 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2909 ext4_mb_normalize_group_request(ac);
2910 return ;
2911 }
2912
2913 bsbits = ac->ac_sb->s_blocksize_bits;
2914
2915 /* first, let's learn actual file size
2916 * given current request is allocated */
2917 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2918 size = size << bsbits;
2919 if (size < i_size_read(ac->ac_inode))
2920 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002921 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002922
Valerie Clement19304792008-05-13 19:31:14 -04002923 /* max size of free chunks */
2924 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002925
Valerie Clement19304792008-05-13 19:31:14 -04002926#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2927 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002928
2929 /* first, try to predict filesize */
2930 /* XXX: should this table be tunable? */
2931 start_off = 0;
2932 if (size <= 16 * 1024) {
2933 size = 16 * 1024;
2934 } else if (size <= 32 * 1024) {
2935 size = 32 * 1024;
2936 } else if (size <= 64 * 1024) {
2937 size = 64 * 1024;
2938 } else if (size <= 128 * 1024) {
2939 size = 128 * 1024;
2940 } else if (size <= 256 * 1024) {
2941 size = 256 * 1024;
2942 } else if (size <= 512 * 1024) {
2943 size = 512 * 1024;
2944 } else if (size <= 1024 * 1024) {
2945 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002946 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002947 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002948 (21 - bsbits)) << 21;
2949 size = 2 * 1024 * 1024;
2950 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002951 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2952 (22 - bsbits)) << 22;
2953 size = 4 * 1024 * 1024;
2954 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002955 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002956 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2957 (23 - bsbits)) << 23;
2958 size = 8 * 1024 * 1024;
2959 } else {
2960 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2961 size = ac->ac_o_ex.fe_len << bsbits;
2962 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002963 size = size >> bsbits;
2964 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002965
2966 /* don't cover already allocated blocks in selected range */
2967 if (ar->pleft && start <= ar->lleft) {
2968 size -= ar->lleft + 1 - start;
2969 start = ar->lleft + 1;
2970 }
2971 if (ar->pright && start + size - 1 >= ar->lright)
2972 size -= start + size - ar->lright;
2973
2974 end = start + size;
2975
2976 /* check we don't cross already preallocated blocks */
2977 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002978 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002979 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002980
Alex Tomasc9de5602008-01-29 00:19:52 -05002981 if (pa->pa_deleted)
2982 continue;
2983 spin_lock(&pa->pa_lock);
2984 if (pa->pa_deleted) {
2985 spin_unlock(&pa->pa_lock);
2986 continue;
2987 }
2988
2989 pa_end = pa->pa_lstart + pa->pa_len;
2990
2991 /* PA must not overlap original request */
2992 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2993 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2994
Eric Sandeen38877f42009-08-17 23:55:24 -04002995 /* skip PAs this normalized request doesn't overlap with */
2996 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002997 spin_unlock(&pa->pa_lock);
2998 continue;
2999 }
3000 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3001
Eric Sandeen38877f42009-08-17 23:55:24 -04003002 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003003 if (pa_end <= ac->ac_o_ex.fe_logical) {
3004 BUG_ON(pa_end < start);
3005 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003006 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003007 BUG_ON(pa->pa_lstart > end);
3008 end = pa->pa_lstart;
3009 }
3010 spin_unlock(&pa->pa_lock);
3011 }
3012 rcu_read_unlock();
3013 size = end - start;
3014
3015 /* XXX: extra loop to check we really don't overlap preallocations */
3016 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003017 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003018 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003019 spin_lock(&pa->pa_lock);
3020 if (pa->pa_deleted == 0) {
3021 pa_end = pa->pa_lstart + pa->pa_len;
3022 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3023 }
3024 spin_unlock(&pa->pa_lock);
3025 }
3026 rcu_read_unlock();
3027
3028 if (start + size <= ac->ac_o_ex.fe_logical &&
3029 start > ac->ac_o_ex.fe_logical) {
3030 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3031 (unsigned long) start, (unsigned long) size,
3032 (unsigned long) ac->ac_o_ex.fe_logical);
3033 }
3034 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3035 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04003036 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003037
3038 /* now prepare goal request */
3039
3040 /* XXX: is it better to align blocks WRT to logical
3041 * placement or satisfy big request as is */
3042 ac->ac_g_ex.fe_logical = start;
3043 ac->ac_g_ex.fe_len = size;
3044
3045 /* define goal start in order to merge */
3046 if (ar->pright && (ar->lright == (start + size))) {
3047 /* merge to the right */
3048 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3049 &ac->ac_f_ex.fe_group,
3050 &ac->ac_f_ex.fe_start);
3051 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3052 }
3053 if (ar->pleft && (ar->lleft + 1 == start)) {
3054 /* merge to the left */
3055 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3056 &ac->ac_f_ex.fe_group,
3057 &ac->ac_f_ex.fe_start);
3058 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3059 }
3060
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003061 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003062 (unsigned) orig_size, (unsigned) start);
3063}
3064
3065static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3066{
3067 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3068
3069 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3070 atomic_inc(&sbi->s_bal_reqs);
3071 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003072 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003073 atomic_inc(&sbi->s_bal_success);
3074 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3075 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3076 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3077 atomic_inc(&sbi->s_bal_goals);
3078 if (ac->ac_found > sbi->s_mb_max_to_scan)
3079 atomic_inc(&sbi->s_bal_breaks);
3080 }
3081
Theodore Ts'o296c3552009-09-30 00:32:42 -04003082 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3083 trace_ext4_mballoc_alloc(ac);
3084 else
3085 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003086}
3087
3088/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003089 * Called on failure; free up any blocks from the inode PA for this
3090 * context. We don't need this for MB_GROUP_PA because we only change
3091 * pa_free in ext4_mb_release_context(), but on failure, we've already
3092 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3093 */
3094static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3095{
3096 struct ext4_prealloc_space *pa = ac->ac_pa;
3097 int len;
3098
3099 if (pa && pa->pa_type == MB_INODE_PA) {
3100 len = ac->ac_b_ex.fe_len;
3101 pa->pa_free += len;
3102 }
3103
3104}
3105
3106/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003107 * use blocks preallocated to inode
3108 */
3109static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3110 struct ext4_prealloc_space *pa)
3111{
3112 ext4_fsblk_t start;
3113 ext4_fsblk_t end;
3114 int len;
3115
3116 /* found preallocated blocks, use them */
3117 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3118 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3119 len = end - start;
3120 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3121 &ac->ac_b_ex.fe_start);
3122 ac->ac_b_ex.fe_len = len;
3123 ac->ac_status = AC_STATUS_FOUND;
3124 ac->ac_pa = pa;
3125
3126 BUG_ON(start < pa->pa_pstart);
3127 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3128 BUG_ON(pa->pa_free < len);
3129 pa->pa_free -= len;
3130
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003131 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003132}
3133
3134/*
3135 * use blocks preallocated to locality group
3136 */
3137static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3138 struct ext4_prealloc_space *pa)
3139{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003140 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003141
Alex Tomasc9de5602008-01-29 00:19:52 -05003142 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3143 &ac->ac_b_ex.fe_group,
3144 &ac->ac_b_ex.fe_start);
3145 ac->ac_b_ex.fe_len = len;
3146 ac->ac_status = AC_STATUS_FOUND;
3147 ac->ac_pa = pa;
3148
3149 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003150 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003151 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003152 * in on-disk bitmap -- see ext4_mb_release_context()
3153 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003154 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003155 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003156}
3157
3158/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003159 * Return the prealloc space that have minimal distance
3160 * from the goal block. @cpa is the prealloc
3161 * space that is having currently known minimal distance
3162 * from the goal block.
3163 */
3164static struct ext4_prealloc_space *
3165ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3166 struct ext4_prealloc_space *pa,
3167 struct ext4_prealloc_space *cpa)
3168{
3169 ext4_fsblk_t cur_distance, new_distance;
3170
3171 if (cpa == NULL) {
3172 atomic_inc(&pa->pa_count);
3173 return pa;
3174 }
3175 cur_distance = abs(goal_block - cpa->pa_pstart);
3176 new_distance = abs(goal_block - pa->pa_pstart);
3177
Coly Li5a54b2f2011-02-24 14:10:05 -05003178 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003179 return cpa;
3180
3181 /* drop the previous reference */
3182 atomic_dec(&cpa->pa_count);
3183 atomic_inc(&pa->pa_count);
3184 return pa;
3185}
3186
3187/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003188 * search goal blocks in preallocated space
3189 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003190static noinline_for_stack int
3191ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003192{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003193 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003194 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3195 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003196 struct ext4_prealloc_space *pa, *cpa = NULL;
3197 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003198
3199 /* only data can be preallocated */
3200 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3201 return 0;
3202
3203 /* first, try per-file preallocation */
3204 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003205 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003206
3207 /* all fields in this condition don't change,
3208 * so we can skip locking for them */
3209 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3210 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3211 continue;
3212
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003213 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003214 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003215 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3216 continue;
3217
Alex Tomasc9de5602008-01-29 00:19:52 -05003218 /* found preallocated blocks, use them */
3219 spin_lock(&pa->pa_lock);
3220 if (pa->pa_deleted == 0 && pa->pa_free) {
3221 atomic_inc(&pa->pa_count);
3222 ext4_mb_use_inode_pa(ac, pa);
3223 spin_unlock(&pa->pa_lock);
3224 ac->ac_criteria = 10;
3225 rcu_read_unlock();
3226 return 1;
3227 }
3228 spin_unlock(&pa->pa_lock);
3229 }
3230 rcu_read_unlock();
3231
3232 /* can we use group allocation? */
3233 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3234 return 0;
3235
3236 /* inode may have no locality group for some reason */
3237 lg = ac->ac_lg;
3238 if (lg == NULL)
3239 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003240 order = fls(ac->ac_o_ex.fe_len) - 1;
3241 if (order > PREALLOC_TB_SIZE - 1)
3242 /* The max size of hash table is PREALLOC_TB_SIZE */
3243 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003244
Akinobu Mitabda00de2010-03-03 23:53:25 -05003245 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003246 /*
3247 * search for the prealloc space that is having
3248 * minimal distance from the goal block.
3249 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003250 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3251 rcu_read_lock();
3252 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3253 pa_inode_list) {
3254 spin_lock(&pa->pa_lock);
3255 if (pa->pa_deleted == 0 &&
3256 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003257
3258 cpa = ext4_mb_check_group_pa(goal_block,
3259 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003260 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003261 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003262 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003263 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003264 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003265 if (cpa) {
3266 ext4_mb_use_group_pa(ac, cpa);
3267 ac->ac_criteria = 20;
3268 return 1;
3269 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003270 return 0;
3271}
3272
3273/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003274 * the function goes through all block freed in the group
3275 * but not yet committed and marks them used in in-core bitmap.
3276 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003277 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003278 */
3279static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3280 ext4_group_t group)
3281{
3282 struct rb_node *n;
3283 struct ext4_group_info *grp;
3284 struct ext4_free_data *entry;
3285
3286 grp = ext4_get_group_info(sb, group);
3287 n = rb_first(&(grp->bb_free_root));
3288
3289 while (n) {
3290 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003291 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003292 n = rb_next(n);
3293 }
3294 return;
3295}
3296
3297/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003298 * the function goes through all preallocation in this group and marks them
3299 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003300 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003301 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003302static noinline_for_stack
3303void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003304 ext4_group_t group)
3305{
3306 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3307 struct ext4_prealloc_space *pa;
3308 struct list_head *cur;
3309 ext4_group_t groupnr;
3310 ext4_grpblk_t start;
3311 int preallocated = 0;
3312 int count = 0;
3313 int len;
3314
3315 /* all form of preallocation discards first load group,
3316 * so the only competing code is preallocation use.
3317 * we don't need any locking here
3318 * notice we do NOT ignore preallocations with pa_deleted
3319 * otherwise we could leave used blocks available for
3320 * allocation in buddy when concurrent ext4_mb_put_pa()
3321 * is dropping preallocation
3322 */
3323 list_for_each(cur, &grp->bb_prealloc_list) {
3324 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3325 spin_lock(&pa->pa_lock);
3326 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3327 &groupnr, &start);
3328 len = pa->pa_len;
3329 spin_unlock(&pa->pa_lock);
3330 if (unlikely(len == 0))
3331 continue;
3332 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003333 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003334 preallocated += len;
3335 count++;
3336 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003337 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003338}
3339
3340static void ext4_mb_pa_callback(struct rcu_head *head)
3341{
3342 struct ext4_prealloc_space *pa;
3343 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3344 kmem_cache_free(ext4_pspace_cachep, pa);
3345}
3346
3347/*
3348 * drops a reference to preallocated space descriptor
3349 * if this was the last reference and the space is consumed
3350 */
3351static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3352 struct super_block *sb, struct ext4_prealloc_space *pa)
3353{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003354 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003355 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003356
3357 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3358 return;
3359
3360 /* in this short window concurrent discard can set pa_deleted */
3361 spin_lock(&pa->pa_lock);
3362 if (pa->pa_deleted == 1) {
3363 spin_unlock(&pa->pa_lock);
3364 return;
3365 }
3366
3367 pa->pa_deleted = 1;
3368 spin_unlock(&pa->pa_lock);
3369
Eric Sandeend33a1972009-03-16 23:25:40 -04003370 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003371 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003372 * If doing group-based preallocation, pa_pstart may be in the
3373 * next group when pa is used up
3374 */
3375 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003376 grp_blk--;
3377
3378 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003379
3380 /*
3381 * possible race:
3382 *
3383 * P1 (buddy init) P2 (regular allocation)
3384 * find block B in PA
3385 * copy on-disk bitmap to buddy
3386 * mark B in on-disk bitmap
3387 * drop PA from group
3388 * mark all PAs in buddy
3389 *
3390 * thus, P1 initializes buddy with B available. to prevent this
3391 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3392 * against that pair
3393 */
3394 ext4_lock_group(sb, grp);
3395 list_del(&pa->pa_group_list);
3396 ext4_unlock_group(sb, grp);
3397
3398 spin_lock(pa->pa_obj_lock);
3399 list_del_rcu(&pa->pa_inode_list);
3400 spin_unlock(pa->pa_obj_lock);
3401
3402 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3403}
3404
3405/*
3406 * creates new preallocated space for given inode
3407 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003408static noinline_for_stack int
3409ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003410{
3411 struct super_block *sb = ac->ac_sb;
3412 struct ext4_prealloc_space *pa;
3413 struct ext4_group_info *grp;
3414 struct ext4_inode_info *ei;
3415
3416 /* preallocate only when found space is larger then requested */
3417 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3418 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3419 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3420
3421 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3422 if (pa == NULL)
3423 return -ENOMEM;
3424
3425 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3426 int winl;
3427 int wins;
3428 int win;
3429 int offs;
3430
3431 /* we can't allocate as much as normalizer wants.
3432 * so, found space must get proper lstart
3433 * to cover original request */
3434 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3435 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3436
3437 /* we're limited by original request in that
3438 * logical block must be covered any way
3439 * winl is window we can move our chunk within */
3440 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3441
3442 /* also, we should cover whole original request */
3443 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3444
3445 /* the smallest one defines real window */
3446 win = min(winl, wins);
3447
3448 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3449 if (offs && offs < win)
3450 win = offs;
3451
3452 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3453 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3454 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3455 }
3456
3457 /* preallocation can change ac_b_ex, thus we store actually
3458 * allocated blocks for history */
3459 ac->ac_f_ex = ac->ac_b_ex;
3460
3461 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3462 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3463 pa->pa_len = ac->ac_b_ex.fe_len;
3464 pa->pa_free = pa->pa_len;
3465 atomic_set(&pa->pa_count, 1);
3466 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003467 INIT_LIST_HEAD(&pa->pa_inode_list);
3468 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003469 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003470 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003471
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003472 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003473 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003474 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003475
3476 ext4_mb_use_inode_pa(ac, pa);
3477 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3478
3479 ei = EXT4_I(ac->ac_inode);
3480 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3481
3482 pa->pa_obj_lock = &ei->i_prealloc_lock;
3483 pa->pa_inode = ac->ac_inode;
3484
3485 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3486 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3487 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3488
3489 spin_lock(pa->pa_obj_lock);
3490 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3491 spin_unlock(pa->pa_obj_lock);
3492
3493 return 0;
3494}
3495
3496/*
3497 * creates new preallocated space for locality group inodes belongs to
3498 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003499static noinline_for_stack int
3500ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003501{
3502 struct super_block *sb = ac->ac_sb;
3503 struct ext4_locality_group *lg;
3504 struct ext4_prealloc_space *pa;
3505 struct ext4_group_info *grp;
3506
3507 /* preallocate only when found space is larger then requested */
3508 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3509 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3510 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3511
3512 BUG_ON(ext4_pspace_cachep == NULL);
3513 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3514 if (pa == NULL)
3515 return -ENOMEM;
3516
3517 /* preallocation can change ac_b_ex, thus we store actually
3518 * allocated blocks for history */
3519 ac->ac_f_ex = ac->ac_b_ex;
3520
3521 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3522 pa->pa_lstart = pa->pa_pstart;
3523 pa->pa_len = ac->ac_b_ex.fe_len;
3524 pa->pa_free = pa->pa_len;
3525 atomic_set(&pa->pa_count, 1);
3526 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003527 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003528 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003529 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003530 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003531
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003532 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003533 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3534 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003535
3536 ext4_mb_use_group_pa(ac, pa);
3537 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3538
3539 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3540 lg = ac->ac_lg;
3541 BUG_ON(lg == NULL);
3542
3543 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3544 pa->pa_inode = NULL;
3545
3546 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3547 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3548 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3549
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003550 /*
3551 * We will later add the new pa to the right bucket
3552 * after updating the pa_free in ext4_mb_release_context
3553 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003554 return 0;
3555}
3556
3557static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3558{
3559 int err;
3560
3561 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3562 err = ext4_mb_new_group_pa(ac);
3563 else
3564 err = ext4_mb_new_inode_pa(ac);
3565 return err;
3566}
3567
3568/*
3569 * finds all unused blocks in on-disk bitmap, frees them in
3570 * in-core bitmap and buddy.
3571 * @pa must be unlinked from inode and group lists, so that
3572 * nobody else can find/use it.
3573 * the caller MUST hold group/inode locks.
3574 * TODO: optimize the case when there are no in-core structures yet
3575 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003576static noinline_for_stack int
3577ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003578 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003579{
Alex Tomasc9de5602008-01-29 00:19:52 -05003580 struct super_block *sb = e4b->bd_sb;
3581 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003582 unsigned int end;
3583 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003584 ext4_group_t group;
3585 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003586 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003587 int err = 0;
3588 int free = 0;
3589
3590 BUG_ON(pa->pa_deleted == 0);
3591 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003592 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003593 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3594 end = bit + pa->pa_len;
3595
Alex Tomasc9de5602008-01-29 00:19:52 -05003596 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003597 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003598 if (bit >= end)
3599 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003600 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003601 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003602 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3603 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003604 free += next - bit;
3605
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003606 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
3607 trace_ext4_mb_release_inode_pa(sb, pa->pa_inode, pa,
3608 grp_blk_start + bit, next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003609 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3610 bit = next + 1;
3611 }
3612 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003613 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003614 pa, (unsigned long) pa->pa_lstart,
3615 (unsigned long) pa->pa_pstart,
3616 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003617 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003618 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003619 /*
3620 * pa is already deleted so we use the value obtained
3621 * from the bitmap and continue.
3622 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003623 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003624 atomic_add(free, &sbi->s_mb_discarded);
3625
3626 return err;
3627}
3628
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003629static noinline_for_stack int
3630ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003631 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003632{
Alex Tomasc9de5602008-01-29 00:19:52 -05003633 struct super_block *sb = e4b->bd_sb;
3634 ext4_group_t group;
3635 ext4_grpblk_t bit;
3636
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003637 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003638 BUG_ON(pa->pa_deleted == 0);
3639 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3640 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3641 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3642 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003643 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003644
3645 return 0;
3646}
3647
3648/*
3649 * releases all preallocations in given group
3650 *
3651 * first, we need to decide discard policy:
3652 * - when do we discard
3653 * 1) ENOSPC
3654 * - how many do we discard
3655 * 1) how many requested
3656 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003657static noinline_for_stack int
3658ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003659 ext4_group_t group, int needed)
3660{
3661 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3662 struct buffer_head *bitmap_bh = NULL;
3663 struct ext4_prealloc_space *pa, *tmp;
3664 struct list_head list;
3665 struct ext4_buddy e4b;
3666 int err;
3667 int busy = 0;
3668 int free = 0;
3669
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003670 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003671
3672 if (list_empty(&grp->bb_prealloc_list))
3673 return 0;
3674
Theodore Ts'o574ca172008-07-11 19:27:31 -04003675 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003676 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003677 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003678 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003679 }
3680
3681 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003682 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003683 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003684 put_bh(bitmap_bh);
3685 return 0;
3686 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003687
3688 if (needed == 0)
3689 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3690
Alex Tomasc9de5602008-01-29 00:19:52 -05003691 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003692repeat:
3693 ext4_lock_group(sb, group);
3694 list_for_each_entry_safe(pa, tmp,
3695 &grp->bb_prealloc_list, pa_group_list) {
3696 spin_lock(&pa->pa_lock);
3697 if (atomic_read(&pa->pa_count)) {
3698 spin_unlock(&pa->pa_lock);
3699 busy = 1;
3700 continue;
3701 }
3702 if (pa->pa_deleted) {
3703 spin_unlock(&pa->pa_lock);
3704 continue;
3705 }
3706
3707 /* seems this one can be freed ... */
3708 pa->pa_deleted = 1;
3709
3710 /* we can trust pa_free ... */
3711 free += pa->pa_free;
3712
3713 spin_unlock(&pa->pa_lock);
3714
3715 list_del(&pa->pa_group_list);
3716 list_add(&pa->u.pa_tmp_list, &list);
3717 }
3718
3719 /* if we still need more blocks and some PAs were used, try again */
3720 if (free < needed && busy) {
3721 busy = 0;
3722 ext4_unlock_group(sb, group);
3723 /*
3724 * Yield the CPU here so that we don't get soft lockup
3725 * in non preempt case.
3726 */
3727 yield();
3728 goto repeat;
3729 }
3730
3731 /* found anything to free? */
3732 if (list_empty(&list)) {
3733 BUG_ON(free != 0);
3734 goto out;
3735 }
3736
3737 /* now free all selected PAs */
3738 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3739
3740 /* remove from object (inode or locality group) */
3741 spin_lock(pa->pa_obj_lock);
3742 list_del_rcu(&pa->pa_inode_list);
3743 spin_unlock(pa->pa_obj_lock);
3744
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003745 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003746 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003747 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003748 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003749
3750 list_del(&pa->u.pa_tmp_list);
3751 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3752 }
3753
3754out:
3755 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003756 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003757 put_bh(bitmap_bh);
3758 return free;
3759}
3760
3761/*
3762 * releases all non-used preallocated blocks for given inode
3763 *
3764 * It's important to discard preallocations under i_data_sem
3765 * We don't want another block to be served from the prealloc
3766 * space when we are discarding the inode prealloc space.
3767 *
3768 * FIXME!! Make sure it is valid at all the call sites
3769 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003770void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003771{
3772 struct ext4_inode_info *ei = EXT4_I(inode);
3773 struct super_block *sb = inode->i_sb;
3774 struct buffer_head *bitmap_bh = NULL;
3775 struct ext4_prealloc_space *pa, *tmp;
3776 ext4_group_t group = 0;
3777 struct list_head list;
3778 struct ext4_buddy e4b;
3779 int err;
3780
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003781 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003782 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3783 return;
3784 }
3785
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003786 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003787 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003788
3789 INIT_LIST_HEAD(&list);
3790
3791repeat:
3792 /* first, collect all pa's in the inode */
3793 spin_lock(&ei->i_prealloc_lock);
3794 while (!list_empty(&ei->i_prealloc_list)) {
3795 pa = list_entry(ei->i_prealloc_list.next,
3796 struct ext4_prealloc_space, pa_inode_list);
3797 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3798 spin_lock(&pa->pa_lock);
3799 if (atomic_read(&pa->pa_count)) {
3800 /* this shouldn't happen often - nobody should
3801 * use preallocation while we're discarding it */
3802 spin_unlock(&pa->pa_lock);
3803 spin_unlock(&ei->i_prealloc_lock);
3804 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3805 WARN_ON(1);
3806 schedule_timeout_uninterruptible(HZ);
3807 goto repeat;
3808
3809 }
3810 if (pa->pa_deleted == 0) {
3811 pa->pa_deleted = 1;
3812 spin_unlock(&pa->pa_lock);
3813 list_del_rcu(&pa->pa_inode_list);
3814 list_add(&pa->u.pa_tmp_list, &list);
3815 continue;
3816 }
3817
3818 /* someone is deleting pa right now */
3819 spin_unlock(&pa->pa_lock);
3820 spin_unlock(&ei->i_prealloc_lock);
3821
3822 /* we have to wait here because pa_deleted
3823 * doesn't mean pa is already unlinked from
3824 * the list. as we might be called from
3825 * ->clear_inode() the inode will get freed
3826 * and concurrent thread which is unlinking
3827 * pa from inode's list may access already
3828 * freed memory, bad-bad-bad */
3829
3830 /* XXX: if this happens too often, we can
3831 * add a flag to force wait only in case
3832 * of ->clear_inode(), but not in case of
3833 * regular truncate */
3834 schedule_timeout_uninterruptible(HZ);
3835 goto repeat;
3836 }
3837 spin_unlock(&ei->i_prealloc_lock);
3838
3839 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003840 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003841 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3842
3843 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003844 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003845 ext4_error(sb, "Error loading buddy information for %u",
3846 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003847 continue;
3848 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003849
Theodore Ts'o574ca172008-07-11 19:27:31 -04003850 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003851 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003852 ext4_error(sb, "Error reading block bitmap for %u",
3853 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003854 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003855 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003856 }
3857
3858 ext4_lock_group(sb, group);
3859 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003860 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003861 ext4_unlock_group(sb, group);
3862
Jing Zhange39e07f2010-05-14 00:00:00 -04003863 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003864 put_bh(bitmap_bh);
3865
3866 list_del(&pa->u.pa_tmp_list);
3867 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3868 }
3869}
3870
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003871#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003872static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3873{
3874 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003875 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003876
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05003877 if (!mb_enable_debug ||
3878 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04003879 return;
3880
Alex Tomasc9de5602008-01-29 00:19:52 -05003881 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3882 " Allocation context details:\n");
3883 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3884 ac->ac_status, ac->ac_flags);
3885 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3886 "best %lu/%lu/%lu@%lu cr %d\n",
3887 (unsigned long)ac->ac_o_ex.fe_group,
3888 (unsigned long)ac->ac_o_ex.fe_start,
3889 (unsigned long)ac->ac_o_ex.fe_len,
3890 (unsigned long)ac->ac_o_ex.fe_logical,
3891 (unsigned long)ac->ac_g_ex.fe_group,
3892 (unsigned long)ac->ac_g_ex.fe_start,
3893 (unsigned long)ac->ac_g_ex.fe_len,
3894 (unsigned long)ac->ac_g_ex.fe_logical,
3895 (unsigned long)ac->ac_b_ex.fe_group,
3896 (unsigned long)ac->ac_b_ex.fe_start,
3897 (unsigned long)ac->ac_b_ex.fe_len,
3898 (unsigned long)ac->ac_b_ex.fe_logical,
3899 (int)ac->ac_criteria);
3900 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3901 ac->ac_found);
3902 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003903 ngroups = ext4_get_groups_count(sb);
3904 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003905 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3906 struct ext4_prealloc_space *pa;
3907 ext4_grpblk_t start;
3908 struct list_head *cur;
3909 ext4_lock_group(sb, i);
3910 list_for_each(cur, &grp->bb_prealloc_list) {
3911 pa = list_entry(cur, struct ext4_prealloc_space,
3912 pa_group_list);
3913 spin_lock(&pa->pa_lock);
3914 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3915 NULL, &start);
3916 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003917 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3918 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003919 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003920 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003921
3922 if (grp->bb_free == 0)
3923 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003924 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003925 i, grp->bb_free, grp->bb_fragments);
3926 }
3927 printk(KERN_ERR "\n");
3928}
3929#else
3930static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3931{
3932 return;
3933}
3934#endif
3935
3936/*
3937 * We use locality group preallocation for small size file. The size of the
3938 * file is determined by the current size or the resulting size after
3939 * allocation which ever is larger
3940 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003941 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003942 */
3943static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3944{
3945 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3946 int bsbits = ac->ac_sb->s_blocksize_bits;
3947 loff_t size, isize;
3948
3949 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3950 return;
3951
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003952 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3953 return;
3954
Alex Tomasc9de5602008-01-29 00:19:52 -05003955 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04003956 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3957 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003958
Theodore Ts'o50797482009-09-18 13:34:02 -04003959 if ((size == isize) &&
3960 !ext4_fs_is_busy(sbi) &&
3961 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3962 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3963 return;
3964 }
3965
Alex Tomasc9de5602008-01-29 00:19:52 -05003966 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003967 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003968 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003969 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003970 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003971 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003972
3973 BUG_ON(ac->ac_lg != NULL);
3974 /*
3975 * locality group prealloc space are per cpu. The reason for having
3976 * per cpu locality group is to reduce the contention between block
3977 * request from multiple CPUs.
3978 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003979 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003980
3981 /* we're going to use group allocation */
3982 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3983
3984 /* serialize all allocations in the group */
3985 mutex_lock(&ac->ac_lg->lg_mutex);
3986}
3987
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003988static noinline_for_stack int
3989ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003990 struct ext4_allocation_request *ar)
3991{
3992 struct super_block *sb = ar->inode->i_sb;
3993 struct ext4_sb_info *sbi = EXT4_SB(sb);
3994 struct ext4_super_block *es = sbi->s_es;
3995 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003996 unsigned int len;
3997 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05003998 ext4_grpblk_t block;
3999
4000 /* we can't allocate > group size */
4001 len = ar->len;
4002
4003 /* just a dirty hack to filter too big requests */
4004 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4005 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4006
4007 /* start searching from the goal */
4008 goal = ar->goal;
4009 if (goal < le32_to_cpu(es->s_first_data_block) ||
4010 goal >= ext4_blocks_count(es))
4011 goal = le32_to_cpu(es->s_first_data_block);
4012 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4013
4014 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04004015 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05004016 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004017 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004018 ac->ac_sb = sb;
4019 ac->ac_inode = ar->inode;
4020 ac->ac_o_ex.fe_logical = ar->logical;
4021 ac->ac_o_ex.fe_group = group;
4022 ac->ac_o_ex.fe_start = block;
4023 ac->ac_o_ex.fe_len = len;
4024 ac->ac_g_ex.fe_logical = ar->logical;
4025 ac->ac_g_ex.fe_group = group;
4026 ac->ac_g_ex.fe_start = block;
4027 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004028 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004029
4030 /* we have to define context: we'll we work with a file or
4031 * locality group. this is a policy, actually */
4032 ext4_mb_group_or_file(ac);
4033
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004034 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004035 "left: %u/%u, right %u/%u to %swritable\n",
4036 (unsigned) ar->len, (unsigned) ar->logical,
4037 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4038 (unsigned) ar->lleft, (unsigned) ar->pleft,
4039 (unsigned) ar->lright, (unsigned) ar->pright,
4040 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4041 return 0;
4042
4043}
4044
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004045static noinline_for_stack void
4046ext4_mb_discard_lg_preallocations(struct super_block *sb,
4047 struct ext4_locality_group *lg,
4048 int order, int total_entries)
4049{
4050 ext4_group_t group = 0;
4051 struct ext4_buddy e4b;
4052 struct list_head discard_list;
4053 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004054
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004055 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004056
4057 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004058
4059 spin_lock(&lg->lg_prealloc_lock);
4060 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4061 pa_inode_list) {
4062 spin_lock(&pa->pa_lock);
4063 if (atomic_read(&pa->pa_count)) {
4064 /*
4065 * This is the pa that we just used
4066 * for block allocation. So don't
4067 * free that
4068 */
4069 spin_unlock(&pa->pa_lock);
4070 continue;
4071 }
4072 if (pa->pa_deleted) {
4073 spin_unlock(&pa->pa_lock);
4074 continue;
4075 }
4076 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004077 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004078
4079 /* seems this one can be freed ... */
4080 pa->pa_deleted = 1;
4081 spin_unlock(&pa->pa_lock);
4082
4083 list_del_rcu(&pa->pa_inode_list);
4084 list_add(&pa->u.pa_tmp_list, &discard_list);
4085
4086 total_entries--;
4087 if (total_entries <= 5) {
4088 /*
4089 * we want to keep only 5 entries
4090 * allowing it to grow to 8. This
4091 * mak sure we don't call discard
4092 * soon for this list.
4093 */
4094 break;
4095 }
4096 }
4097 spin_unlock(&lg->lg_prealloc_lock);
4098
4099 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4100
4101 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4102 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004103 ext4_error(sb, "Error loading buddy information for %u",
4104 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004105 continue;
4106 }
4107 ext4_lock_group(sb, group);
4108 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004109 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004110 ext4_unlock_group(sb, group);
4111
Jing Zhange39e07f2010-05-14 00:00:00 -04004112 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004113 list_del(&pa->u.pa_tmp_list);
4114 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4115 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004116}
4117
4118/*
4119 * We have incremented pa_count. So it cannot be freed at this
4120 * point. Also we hold lg_mutex. So no parallel allocation is
4121 * possible from this lg. That means pa_free cannot be updated.
4122 *
4123 * A parallel ext4_mb_discard_group_preallocations is possible.
4124 * which can cause the lg_prealloc_list to be updated.
4125 */
4126
4127static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4128{
4129 int order, added = 0, lg_prealloc_count = 1;
4130 struct super_block *sb = ac->ac_sb;
4131 struct ext4_locality_group *lg = ac->ac_lg;
4132 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4133
4134 order = fls(pa->pa_free) - 1;
4135 if (order > PREALLOC_TB_SIZE - 1)
4136 /* The max size of hash table is PREALLOC_TB_SIZE */
4137 order = PREALLOC_TB_SIZE - 1;
4138 /* Add the prealloc space to lg */
4139 rcu_read_lock();
4140 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4141 pa_inode_list) {
4142 spin_lock(&tmp_pa->pa_lock);
4143 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004144 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004145 continue;
4146 }
4147 if (!added && pa->pa_free < tmp_pa->pa_free) {
4148 /* Add to the tail of the previous entry */
4149 list_add_tail_rcu(&pa->pa_inode_list,
4150 &tmp_pa->pa_inode_list);
4151 added = 1;
4152 /*
4153 * we want to count the total
4154 * number of entries in the list
4155 */
4156 }
4157 spin_unlock(&tmp_pa->pa_lock);
4158 lg_prealloc_count++;
4159 }
4160 if (!added)
4161 list_add_tail_rcu(&pa->pa_inode_list,
4162 &lg->lg_prealloc_list[order]);
4163 rcu_read_unlock();
4164
4165 /* Now trim the list to be not more than 8 elements */
4166 if (lg_prealloc_count > 8) {
4167 ext4_mb_discard_lg_preallocations(sb, lg,
4168 order, lg_prealloc_count);
4169 return;
4170 }
4171 return ;
4172}
4173
Alex Tomasc9de5602008-01-29 00:19:52 -05004174/*
4175 * release all resource we used in allocation
4176 */
4177static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4178{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004179 struct ext4_prealloc_space *pa = ac->ac_pa;
4180 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004181 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004182 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004183 spin_lock(&pa->pa_lock);
4184 pa->pa_pstart += ac->ac_b_ex.fe_len;
4185 pa->pa_lstart += ac->ac_b_ex.fe_len;
4186 pa->pa_free -= ac->ac_b_ex.fe_len;
4187 pa->pa_len -= ac->ac_b_ex.fe_len;
4188 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004189 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004190 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004191 if (ac->alloc_semp)
4192 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004193 if (pa) {
4194 /*
4195 * We want to add the pa to the right bucket.
4196 * Remove it from the list and while adding
4197 * make sure the list to which we are adding
4198 * doesn't grow big. We need to release
4199 * alloc_semp before calling ext4_mb_add_n_trim()
4200 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004201 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004202 spin_lock(pa->pa_obj_lock);
4203 list_del_rcu(&pa->pa_inode_list);
4204 spin_unlock(pa->pa_obj_lock);
4205 ext4_mb_add_n_trim(ac);
4206 }
4207 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4208 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004209 if (ac->ac_bitmap_page)
4210 page_cache_release(ac->ac_bitmap_page);
4211 if (ac->ac_buddy_page)
4212 page_cache_release(ac->ac_buddy_page);
4213 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4214 mutex_unlock(&ac->ac_lg->lg_mutex);
4215 ext4_mb_collect_stats(ac);
4216 return 0;
4217}
4218
4219static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4220{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004221 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004222 int ret;
4223 int freed = 0;
4224
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004225 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004226 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004227 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4228 freed += ret;
4229 needed -= ret;
4230 }
4231
4232 return freed;
4233}
4234
4235/*
4236 * Main entry point into mballoc to allocate blocks
4237 * it tries to use preallocation first, then falls back
4238 * to usual allocation
4239 */
4240ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004241 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004242{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004243 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004244 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004245 struct ext4_sb_info *sbi;
4246 struct super_block *sb;
4247 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004248 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004249 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004250
4251 sb = ar->inode->i_sb;
4252 sbi = EXT4_SB(sb);
4253
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004254 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004255
Mingming Cao60e58e02009-01-22 18:13:05 +01004256 /*
4257 * For delayed allocation, we could skip the ENOSPC and
4258 * EDQUOT check, as blocks and quotas have been already
4259 * reserved when data being copied into pagecache.
4260 */
Theodore Ts'of2321092011-01-10 12:12:36 -05004261 if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
Mingming Cao60e58e02009-01-22 18:13:05 +01004262 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4263 else {
4264 /* Without delayed allocation we need to verify
4265 * there is enough free blocks to do block allocation
4266 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004267 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004268 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4269 /* let others to free the space */
4270 yield();
4271 ar->len = ar->len >> 1;
4272 }
4273 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004274 *errp = -ENOSPC;
4275 return 0;
4276 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004277 reserv_blks = ar->len;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004278 while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004279 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4280 ar->len--;
4281 }
4282 inquota = ar->len;
4283 if (ar->len == 0) {
4284 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004285 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004286 }
Mingming Caod2a17632008-07-14 17:52:37 -04004287 }
Mingming Caod2a17632008-07-14 17:52:37 -04004288
Eric Sandeen256bdb42008-02-10 01:13:33 -05004289 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004290 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004291 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004292 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004293 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004294 }
4295
Eric Sandeen256bdb42008-02-10 01:13:33 -05004296 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004297 if (*errp) {
4298 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004299 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004300 }
4301
Eric Sandeen256bdb42008-02-10 01:13:33 -05004302 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4303 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004304 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4305 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004306repeat:
4307 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004308 *errp = ext4_mb_regular_allocator(ac);
4309 if (*errp)
4310 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004311
4312 /* as we've just preallocated more space than
4313 * user requested orinally, we store allocated
4314 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004315 if (ac->ac_status == AC_STATUS_FOUND &&
4316 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4317 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004318 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004319 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004320 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004321 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004322 /*
4323 * drop the reference that we took
4324 * in ext4_mb_use_best_found
4325 */
4326 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004327 ac->ac_b_ex.fe_group = 0;
4328 ac->ac_b_ex.fe_start = 0;
4329 ac->ac_b_ex.fe_len = 0;
4330 ac->ac_status = AC_STATUS_CONTINUE;
4331 goto repeat;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004332 } else if (*errp)
4333 errout:
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004334 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004335 else {
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004336 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4337 ar->len = ac->ac_b_ex.fe_len;
4338 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004339 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004340 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004341 if (freed)
4342 goto repeat;
4343 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004344 }
4345
4346 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004347 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004348 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004349 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004350 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004351 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004352out:
4353 if (ac)
4354 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004355 if (inquota && ar->len < inquota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004356 dquot_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004357 if (!ar->len) {
Theodore Ts'of2321092011-01-10 12:12:36 -05004358 if (!ext4_test_inode_state(ar->inode,
4359 EXT4_STATE_DELALLOC_RESERVED))
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004360 /* release all the reserved blocks if non delalloc */
4361 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4362 reserv_blks);
4363 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004364
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004365 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004366
Alex Tomasc9de5602008-01-29 00:19:52 -05004367 return block;
4368}
Alex Tomasc9de5602008-01-29 00:19:52 -05004369
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004370/*
4371 * We can merge two free data extents only if the physical blocks
4372 * are contiguous, AND the extents were freed by the same transaction,
4373 * AND the blocks are associated with the same group.
4374 */
4375static int can_merge(struct ext4_free_data *entry1,
4376 struct ext4_free_data *entry2)
4377{
4378 if ((entry1->t_tid == entry2->t_tid) &&
4379 (entry1->group == entry2->group) &&
4380 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4381 return 1;
4382 return 0;
4383}
4384
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004385static noinline_for_stack int
4386ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004387 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004388{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004389 ext4_group_t group = e4b->bd_group;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004390 ext4_grpblk_t block;
4391 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004392 struct ext4_group_info *db = e4b->bd_info;
4393 struct super_block *sb = e4b->bd_sb;
4394 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004395 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4396 struct rb_node *parent = NULL, *new_node;
4397
Frank Mayhar03901312009-01-07 00:06:22 -05004398 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004399 BUG_ON(e4b->bd_bitmap_page == NULL);
4400 BUG_ON(e4b->bd_buddy_page == NULL);
4401
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004402 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004403 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004404
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004405 if (!*n) {
4406 /* first free block exent. We need to
4407 protect buddy cache from being freed,
4408 * otherwise we'll refresh it from
4409 * on-disk bitmap and lose not-yet-available
4410 * blocks */
4411 page_cache_get(e4b->bd_buddy_page);
4412 page_cache_get(e4b->bd_bitmap_page);
4413 }
4414 while (*n) {
4415 parent = *n;
4416 entry = rb_entry(parent, struct ext4_free_data, node);
4417 if (block < entry->start_blk)
4418 n = &(*n)->rb_left;
4419 else if (block >= (entry->start_blk + entry->count))
4420 n = &(*n)->rb_right;
4421 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004422 ext4_grp_locked_error(sb, group, 0,
4423 ext4_group_first_block_no(sb, group) + block,
4424 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004425 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004426 }
4427 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004428
4429 rb_link_node(new_node, parent, n);
4430 rb_insert_color(new_node, &db->bb_free_root);
4431
4432 /* Now try to see the extent can be merged to left and right */
4433 node = rb_prev(new_node);
4434 if (node) {
4435 entry = rb_entry(node, struct ext4_free_data, node);
4436 if (can_merge(entry, new_entry)) {
4437 new_entry->start_blk = entry->start_blk;
4438 new_entry->count += entry->count;
4439 rb_erase(node, &(db->bb_free_root));
4440 spin_lock(&sbi->s_md_lock);
4441 list_del(&entry->list);
4442 spin_unlock(&sbi->s_md_lock);
4443 kmem_cache_free(ext4_free_ext_cachep, entry);
4444 }
4445 }
4446
4447 node = rb_next(new_node);
4448 if (node) {
4449 entry = rb_entry(node, struct ext4_free_data, node);
4450 if (can_merge(new_entry, entry)) {
4451 new_entry->count += entry->count;
4452 rb_erase(node, &(db->bb_free_root));
4453 spin_lock(&sbi->s_md_lock);
4454 list_del(&entry->list);
4455 spin_unlock(&sbi->s_md_lock);
4456 kmem_cache_free(ext4_free_ext_cachep, entry);
4457 }
4458 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004459 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004460 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004461 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004462 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004463 return 0;
4464}
4465
Theodore Ts'o44338712009-11-22 07:44:56 -05004466/**
4467 * ext4_free_blocks() -- Free given blocks and update quota
4468 * @handle: handle for this transaction
4469 * @inode: inode
4470 * @block: start physical block to free
4471 * @count: number of blocks to count
4472 * @metadata: Are these metadata blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004473 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004474void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004475 struct buffer_head *bh, ext4_fsblk_t block,
4476 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004477{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004478 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004479 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004480 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004481 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004482 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004483 ext4_grpblk_t bit;
4484 struct buffer_head *gd_bh;
4485 ext4_group_t block_group;
4486 struct ext4_sb_info *sbi;
4487 struct ext4_buddy e4b;
4488 int err = 0;
4489 int ret;
4490
Theodore Ts'oe6362602009-11-23 07:17:05 -05004491 if (bh) {
4492 if (block)
4493 BUG_ON(block != bh->b_blocknr);
4494 else
4495 block = bh->b_blocknr;
4496 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004497
Alex Tomasc9de5602008-01-29 00:19:52 -05004498 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004499 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4500 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004501 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004502 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004503 goto error_return;
4504 }
4505
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004506 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004507 trace_ext4_free_blocks(inode, block, count, flags);
4508
4509 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4510 struct buffer_head *tbh = bh;
4511 int i;
4512
4513 BUG_ON(bh && (count > 1));
4514
4515 for (i = 0; i < count; i++) {
4516 if (!bh)
4517 tbh = sb_find_get_block(inode->i_sb,
4518 block + i);
Namhyung Kim87783692010-10-27 21:30:11 -04004519 if (unlikely(!tbh))
4520 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004521 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004522 inode, tbh, block + i);
4523 }
4524 }
4525
Theodore Ts'o60e66792010-05-17 07:00:00 -04004526 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004527 * We need to make sure we don't reuse the freed block until
4528 * after the transaction is committed, which we can do by
4529 * treating the block as metadata, below. We make an
4530 * exception if the inode is to be written in writeback mode
4531 * since writeback mode has weak data consistency guarantees.
4532 */
4533 if (!ext4_should_writeback_data(inode))
4534 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004535
Alex Tomasc9de5602008-01-29 00:19:52 -05004536do_more:
4537 overflow = 0;
4538 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4539
4540 /*
4541 * Check to see if we are freeing blocks across a group
4542 * boundary.
4543 */
4544 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4545 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4546 count -= overflow;
4547 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004548 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004549 if (!bitmap_bh) {
4550 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004551 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004552 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004553 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004554 if (!gdp) {
4555 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004556 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004557 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004558
4559 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4560 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4561 in_range(block, ext4_inode_table(sb, gdp),
4562 EXT4_SB(sb)->s_itb_per_group) ||
4563 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4564 EXT4_SB(sb)->s_itb_per_group)) {
4565
Eric Sandeen12062dd2010-02-15 14:19:27 -05004566 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004567 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004568 /* err = 0. ext4_std_error should be a no op */
4569 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004570 }
4571
4572 BUFFER_TRACE(bitmap_bh, "getting write access");
4573 err = ext4_journal_get_write_access(handle, bitmap_bh);
4574 if (err)
4575 goto error_return;
4576
4577 /*
4578 * We are about to modify some metadata. Call the journal APIs
4579 * to unshare ->b_data if a currently-committing transaction is
4580 * using it
4581 */
4582 BUFFER_TRACE(gd_bh, "get_write_access");
4583 err = ext4_journal_get_write_access(handle, gd_bh);
4584 if (err)
4585 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004586#ifdef AGGRESSIVE_CHECK
4587 {
4588 int i;
4589 for (i = 0; i < count; i++)
4590 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4591 }
4592#endif
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004593 trace_ext4_mballoc_free(sb, inode, block_group, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004594
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004595 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4596 if (err)
4597 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004598
4599 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004600 struct ext4_free_data *new_entry;
4601 /*
4602 * blocks being freed are metadata. these blocks shouldn't
4603 * be used until this transaction is committed
4604 */
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004605 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4606 if (!new_entry) {
4607 err = -ENOMEM;
4608 goto error_return;
4609 }
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004610 new_entry->start_blk = bit;
4611 new_entry->group = block_group;
4612 new_entry->count = count;
4613 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004614
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004615 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004616 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004617 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004618 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004619 /* need to update group_info->bb_free and bitmap
4620 * with group lock held. generate_buddy look at
4621 * them with group lock_held
4622 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004623 ext4_lock_group(sb, block_group);
4624 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004625 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004626 }
4627
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004628 ret = ext4_free_blks_count(sb, gdp) + count;
4629 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004630 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004631 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004632 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4633
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004634 if (sbi->s_log_groups_per_flex) {
4635 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004636 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004637 }
4638
Jing Zhange39e07f2010-05-14 00:00:00 -04004639 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004640
Theodore Ts'o44338712009-11-22 07:44:56 -05004641 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004642
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004643 /* We dirtied the bitmap block */
4644 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4645 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4646
Alex Tomasc9de5602008-01-29 00:19:52 -05004647 /* And the group descriptor block */
4648 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004649 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004650 if (!err)
4651 err = ret;
4652
4653 if (overflow && !err) {
4654 block += count;
4655 count = overflow;
4656 put_bh(bitmap_bh);
4657 goto do_more;
4658 }
Theodore Ts'oa0375152010-06-11 23:14:04 -04004659 ext4_mark_super_dirty(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004660error_return:
Theodore Ts'o44338712009-11-22 07:44:56 -05004661 if (freed)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004662 dquot_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004663 brelse(bitmap_bh);
4664 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004665 return;
4666}
Lukas Czerner7360d172010-10-27 21:30:12 -04004667
4668/**
Amir Goldstein2846e822011-05-09 10:46:41 -04004669 * ext4_add_groupblocks() -- Add given blocks to an existing group
4670 * @handle: handle to this transaction
4671 * @sb: super block
4672 * @block: start physcial block to add to the block group
4673 * @count: number of blocks to free
4674 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004675 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004676 */
4677void ext4_add_groupblocks(handle_t *handle, struct super_block *sb,
4678 ext4_fsblk_t block, unsigned long count)
4679{
4680 struct buffer_head *bitmap_bh = NULL;
4681 struct buffer_head *gd_bh;
4682 ext4_group_t block_group;
4683 ext4_grpblk_t bit;
4684 unsigned int i;
4685 struct ext4_group_desc *desc;
4686 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004687 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004688 int err = 0, ret, blk_free_count;
4689 ext4_grpblk_t blocks_freed;
4690 struct ext4_group_info *grp;
4691
4692 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4693
4694 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4695 grp = ext4_get_group_info(sb, block_group);
4696 /*
4697 * Check to see if we are freeing blocks across a group
4698 * boundary.
4699 */
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004700 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb))
Amir Goldstein2846e822011-05-09 10:46:41 -04004701 goto error_return;
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004702
Amir Goldstein2846e822011-05-09 10:46:41 -04004703 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
4704 if (!bitmap_bh)
4705 goto error_return;
4706 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
4707 if (!desc)
4708 goto error_return;
4709
4710 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4711 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4712 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4713 in_range(block + count - 1, ext4_inode_table(sb, desc),
4714 sbi->s_itb_per_group)) {
4715 ext4_error(sb, "Adding blocks in system zones - "
4716 "Block = %llu, count = %lu",
4717 block, count);
4718 goto error_return;
4719 }
4720
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004721 BUFFER_TRACE(bitmap_bh, "getting write access");
4722 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004723 if (err)
4724 goto error_return;
4725
4726 /*
4727 * We are about to modify some metadata. Call the journal APIs
4728 * to unshare ->b_data if a currently-committing transaction is
4729 * using it
4730 */
4731 BUFFER_TRACE(gd_bh, "get_write_access");
4732 err = ext4_journal_get_write_access(handle, gd_bh);
4733 if (err)
4734 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04004735
Amir Goldstein2846e822011-05-09 10:46:41 -04004736 for (i = 0, blocks_freed = 0; i < count; i++) {
4737 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04004738 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04004739 ext4_error(sb, "bit already cleared for block %llu",
4740 (ext4_fsblk_t)(block + i));
4741 BUFFER_TRACE(bitmap_bh, "bit already cleared");
4742 } else {
4743 blocks_freed++;
4744 }
4745 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004746
4747 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4748 if (err)
4749 goto error_return;
4750
4751 /*
4752 * need to update group_info->bb_free and bitmap
4753 * with group lock held. generate_buddy look at
4754 * them with group lock_held
4755 */
Amir Goldstein2846e822011-05-09 10:46:41 -04004756 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004757 mb_clear_bits(bitmap_bh->b_data, bit, count);
4758 mb_free_blocks(NULL, &e4b, bit, count);
Amir Goldstein2846e822011-05-09 10:46:41 -04004759 blk_free_count = blocks_freed + ext4_free_blks_count(sb, desc);
4760 ext4_free_blks_set(sb, desc, blk_free_count);
4761 desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
4762 ext4_unlock_group(sb, block_group);
4763 percpu_counter_add(&sbi->s_freeblocks_counter, blocks_freed);
4764
4765 if (sbi->s_log_groups_per_flex) {
4766 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
4767 atomic_add(blocks_freed,
4768 &sbi->s_flex_groups[flex_group].free_blocks);
4769 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004770
4771 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04004772
4773 /* We dirtied the bitmap block */
4774 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4775 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4776
4777 /* And the group descriptor block */
4778 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4779 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4780 if (!err)
4781 err = ret;
4782
4783error_return:
4784 brelse(bitmap_bh);
4785 ext4_std_error(sb, err);
4786 return;
4787}
4788
4789/**
Lukas Czerner7360d172010-10-27 21:30:12 -04004790 * ext4_trim_extent -- function to TRIM one single free extent in the group
4791 * @sb: super block for the file system
4792 * @start: starting block of the free extent in the alloc. group
4793 * @count: number of blocks to TRIM
4794 * @group: alloc. group we are working with
4795 * @e4b: ext4 buddy for the group
4796 *
4797 * Trim "count" blocks starting at "start" in the "group". To assure that no
4798 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4799 * be called with under the group lock.
4800 */
Theodore Ts'od9f34502011-04-30 13:47:24 -04004801static void ext4_trim_extent(struct super_block *sb, int start, int count,
4802 ext4_group_t group, struct ext4_buddy *e4b)
Lukas Czerner7360d172010-10-27 21:30:12 -04004803{
4804 struct ext4_free_extent ex;
Lukas Czerner7360d172010-10-27 21:30:12 -04004805
4806 assert_spin_locked(ext4_group_lock_ptr(sb, group));
4807
4808 ex.fe_start = start;
4809 ex.fe_group = group;
4810 ex.fe_len = count;
4811
4812 /*
4813 * Mark blocks used, so no one can reuse them while
4814 * being trimmed.
4815 */
4816 mb_mark_used(e4b, &ex);
4817 ext4_unlock_group(sb, group);
Theodore Ts'od9f34502011-04-30 13:47:24 -04004818 ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04004819 ext4_lock_group(sb, group);
4820 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czerner7360d172010-10-27 21:30:12 -04004821}
4822
4823/**
4824 * ext4_trim_all_free -- function to trim all free space in alloc. group
4825 * @sb: super block for file system
4826 * @e4b: ext4 buddy
4827 * @start: first group block to examine
4828 * @max: last group block to examine
4829 * @minblocks: minimum extent block count
4830 *
4831 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4832 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4833 * the extent.
4834 *
4835 *
4836 * ext4_trim_all_free walks through group's block bitmap searching for free
4837 * extents. When the free extent is found, mark it as used in group buddy
4838 * bitmap. Then issue a TRIM command on this extent and free the extent in
4839 * the group buddy bitmap. This is done until whole group is scanned.
4840 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05004841static ext4_grpblk_t
4842ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
Lukas Czerner7360d172010-10-27 21:30:12 -04004843 ext4_grpblk_t start, ext4_grpblk_t max, ext4_grpblk_t minblocks)
4844{
4845 void *bitmap;
4846 ext4_grpblk_t next, count = 0;
4847 ext4_group_t group;
Lukas Czerner7360d172010-10-27 21:30:12 -04004848
4849 BUG_ON(e4b == NULL);
4850
4851 bitmap = e4b->bd_bitmap;
4852 group = e4b->bd_group;
4853 start = (e4b->bd_info->bb_first_free > start) ?
4854 e4b->bd_info->bb_first_free : start;
4855 ext4_lock_group(sb, group);
4856
4857 while (start < max) {
4858 start = mb_find_next_zero_bit(bitmap, max, start);
4859 if (start >= max)
4860 break;
4861 next = mb_find_next_bit(bitmap, max, start);
4862
4863 if ((next - start) >= minblocks) {
Theodore Ts'od9f34502011-04-30 13:47:24 -04004864 ext4_trim_extent(sb, start,
4865 next - start, group, e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04004866 count += next - start;
4867 }
4868 start = next + 1;
4869
4870 if (fatal_signal_pending(current)) {
4871 count = -ERESTARTSYS;
4872 break;
4873 }
4874
4875 if (need_resched()) {
4876 ext4_unlock_group(sb, group);
4877 cond_resched();
4878 ext4_lock_group(sb, group);
4879 }
4880
4881 if ((e4b->bd_info->bb_free - count) < minblocks)
4882 break;
4883 }
4884 ext4_unlock_group(sb, group);
4885
4886 ext4_debug("trimmed %d blocks in the group %d\n",
4887 count, group);
4888
Lukas Czerner7360d172010-10-27 21:30:12 -04004889 return count;
4890}
4891
4892/**
4893 * ext4_trim_fs() -- trim ioctl handle function
4894 * @sb: superblock for filesystem
4895 * @range: fstrim_range structure
4896 *
4897 * start: First Byte to trim
4898 * len: number of Bytes to trim from start
4899 * minlen: minimum extent length in Bytes
4900 * ext4_trim_fs goes through all allocation groups containing Bytes from
4901 * start to start+len. For each such a group ext4_trim_all_free function
4902 * is invoked to trim all free space.
4903 */
4904int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
4905{
4906 struct ext4_buddy e4b;
4907 ext4_group_t first_group, last_group;
4908 ext4_group_t group, ngroups = ext4_get_groups_count(sb);
4909 ext4_grpblk_t cnt = 0, first_block, last_block;
4910 uint64_t start, len, minlen, trimmed;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004911 ext4_fsblk_t first_data_blk =
4912 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner7360d172010-10-27 21:30:12 -04004913 int ret = 0;
4914
4915 start = range->start >> sb->s_blocksize_bits;
4916 len = range->len >> sb->s_blocksize_bits;
4917 minlen = range->minlen >> sb->s_blocksize_bits;
4918 trimmed = 0;
4919
4920 if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
4921 return -EINVAL;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004922 if (start < first_data_blk) {
4923 len -= first_data_blk - start;
4924 start = first_data_blk;
4925 }
Lukas Czerner7360d172010-10-27 21:30:12 -04004926
4927 /* Determine first and last group to examine based on start and len */
4928 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
4929 &first_group, &first_block);
4930 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) (start + len),
4931 &last_group, &last_block);
4932 last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
4933 last_block = EXT4_BLOCKS_PER_GROUP(sb);
4934
4935 if (first_group > last_group)
4936 return -EINVAL;
4937
4938 for (group = first_group; group <= last_group; group++) {
4939 ret = ext4_mb_load_buddy(sb, group, &e4b);
4940 if (ret) {
4941 ext4_error(sb, "Error in loading buddy "
4942 "information for %u", group);
4943 break;
4944 }
4945
Tao Ma0ba08512011-03-23 15:48:11 -04004946 /*
4947 * For all the groups except the last one, last block will
4948 * always be EXT4_BLOCKS_PER_GROUP(sb), so we only need to
4949 * change it for the last group in which case start +
4950 * len < EXT4_BLOCKS_PER_GROUP(sb).
4951 */
4952 if (first_block + len < EXT4_BLOCKS_PER_GROUP(sb))
Jan Karaca6e9092011-01-10 12:30:39 -05004953 last_block = first_block + len;
Tao Ma0ba08512011-03-23 15:48:11 -04004954 len -= last_block - first_block;
Lukas Czerner7360d172010-10-27 21:30:12 -04004955
4956 if (e4b.bd_info->bb_free >= minlen) {
4957 cnt = ext4_trim_all_free(sb, &e4b, first_block,
4958 last_block, minlen);
4959 if (cnt < 0) {
4960 ret = cnt;
4961 ext4_mb_unload_buddy(&e4b);
4962 break;
4963 }
4964 }
4965 ext4_mb_unload_buddy(&e4b);
4966 trimmed += cnt;
4967 first_block = 0;
4968 }
4969 range->len = trimmed * sb->s_blocksize;
4970
4971 return ret;
4972}