blob: d4714d6cf7d92d495ff4a01b9ffaa47a0651bdfb [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
95 * enough free space (pa_free) withing the prealloc space.
96 *
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 */
345#define NR_GRPINFO_CACHES \
346 (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE + 1)
347static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
348
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500349static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
350 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500351static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
352 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500353static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
354
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500355static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
356{
Alex Tomasc9de5602008-01-29 00:19:52 -0500357#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500358 *bit += ((unsigned long) addr & 7UL) << 3;
359 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500360#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500361 *bit += ((unsigned long) addr & 3UL) << 3;
362 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500363#else
364#error "how many bits you are?!"
365#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500366 return addr;
367}
Alex Tomasc9de5602008-01-29 00:19:52 -0500368
369static inline int mb_test_bit(int bit, void *addr)
370{
371 /*
372 * ext4_test_bit on architecture like powerpc
373 * needs unsigned long aligned address
374 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500375 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500376 return ext4_test_bit(bit, addr);
377}
378
379static inline void mb_set_bit(int bit, void *addr)
380{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500381 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500382 ext4_set_bit(bit, addr);
383}
384
Alex Tomasc9de5602008-01-29 00:19:52 -0500385static inline void mb_clear_bit(int bit, void *addr)
386{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500387 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500388 ext4_clear_bit(bit, addr);
389}
390
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500391static inline int mb_find_next_zero_bit(void *addr, int max, int start)
392{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400393 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500394 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400395 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500396 start += fix;
397
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400398 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
399 if (ret > max)
400 return max;
401 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500402}
403
404static inline int mb_find_next_bit(void *addr, int max, int start)
405{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400406 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500407 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400408 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500409 start += fix;
410
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400411 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
412 if (ret > max)
413 return max;
414 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500415}
416
Alex Tomasc9de5602008-01-29 00:19:52 -0500417static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
418{
419 char *bb;
420
Alex Tomasc9de5602008-01-29 00:19:52 -0500421 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
422 BUG_ON(max == NULL);
423
424 if (order > e4b->bd_blkbits + 1) {
425 *max = 0;
426 return NULL;
427 }
428
429 /* at order 0 we see each particular block */
430 *max = 1 << (e4b->bd_blkbits + 3);
431 if (order == 0)
432 return EXT4_MB_BITMAP(e4b);
433
434 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
435 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
436
437 return bb;
438}
439
440#ifdef DOUBLE_CHECK
441static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
442 int first, int count)
443{
444 int i;
445 struct super_block *sb = e4b->bd_sb;
446
447 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
448 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400449 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500450 for (i = 0; i < count; i++) {
451 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
452 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500453
454 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500455 blocknr += first + i;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500456 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400457 inode ? inode->i_ino : 0,
458 blocknr,
459 "freeing block already freed "
460 "(bit %u)",
461 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500462 }
463 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
464 }
465}
466
467static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
468{
469 int i;
470
471 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
472 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400473 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500474 for (i = 0; i < count; i++) {
475 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
476 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
477 }
478}
479
480static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
481{
482 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
483 unsigned char *b1, *b2;
484 int i;
485 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
486 b2 = (unsigned char *) bitmap;
487 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
488 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500489 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400490 "at byte %u(%u): %x in copy != %x "
491 "on disk/prealloc\n",
492 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500493 BUG();
494 }
495 }
496 }
497}
498
499#else
500static inline void mb_free_blocks_double(struct inode *inode,
501 struct ext4_buddy *e4b, int first, int count)
502{
503 return;
504}
505static inline void mb_mark_used_double(struct ext4_buddy *e4b,
506 int first, int count)
507{
508 return;
509}
510static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
511{
512 return;
513}
514#endif
515
516#ifdef AGGRESSIVE_CHECK
517
518#define MB_CHECK_ASSERT(assert) \
519do { \
520 if (!(assert)) { \
521 printk(KERN_EMERG \
522 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
523 function, file, line, # assert); \
524 BUG(); \
525 } \
526} while (0)
527
528static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
529 const char *function, int line)
530{
531 struct super_block *sb = e4b->bd_sb;
532 int order = e4b->bd_blkbits + 1;
533 int max;
534 int max2;
535 int i;
536 int j;
537 int k;
538 int count;
539 struct ext4_group_info *grp;
540 int fragments = 0;
541 int fstart;
542 struct list_head *cur;
543 void *buddy;
544 void *buddy2;
545
Alex Tomasc9de5602008-01-29 00:19:52 -0500546 {
547 static int mb_check_counter;
548 if (mb_check_counter++ % 100 != 0)
549 return 0;
550 }
551
552 while (order > 1) {
553 buddy = mb_find_buddy(e4b, order, &max);
554 MB_CHECK_ASSERT(buddy);
555 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
556 MB_CHECK_ASSERT(buddy2);
557 MB_CHECK_ASSERT(buddy != buddy2);
558 MB_CHECK_ASSERT(max * 2 == max2);
559
560 count = 0;
561 for (i = 0; i < max; i++) {
562
563 if (mb_test_bit(i, buddy)) {
564 /* only single bit in buddy2 may be 1 */
565 if (!mb_test_bit(i << 1, buddy2)) {
566 MB_CHECK_ASSERT(
567 mb_test_bit((i<<1)+1, buddy2));
568 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
569 MB_CHECK_ASSERT(
570 mb_test_bit(i << 1, buddy2));
571 }
572 continue;
573 }
574
575 /* both bits in buddy2 must be 0 */
576 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
577 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
578
579 for (j = 0; j < (1 << order); j++) {
580 k = (i * (1 << order)) + j;
581 MB_CHECK_ASSERT(
582 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
583 }
584 count++;
585 }
586 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
587 order--;
588 }
589
590 fstart = -1;
591 buddy = mb_find_buddy(e4b, 0, &max);
592 for (i = 0; i < max; i++) {
593 if (!mb_test_bit(i, buddy)) {
594 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
595 if (fstart == -1) {
596 fragments++;
597 fstart = i;
598 }
599 continue;
600 }
601 fstart = -1;
602 /* check used bits only */
603 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
604 buddy2 = mb_find_buddy(e4b, j, &max2);
605 k = i >> j;
606 MB_CHECK_ASSERT(k < max2);
607 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
608 }
609 }
610 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
611 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
612
613 grp = ext4_get_group_info(sb, e4b->bd_group);
614 buddy = mb_find_buddy(e4b, 0, &max);
615 list_for_each(cur, &grp->bb_prealloc_list) {
616 ext4_group_t groupnr;
617 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400618 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
619 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500620 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400621 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500622 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
623 }
624 return 0;
625}
626#undef MB_CHECK_ASSERT
627#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400628 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500629#else
630#define mb_check_buddy(e4b)
631#endif
632
633/* FIXME!! need more doc */
634static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400635 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500636 struct ext4_group_info *grp)
637{
638 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400639 ext4_grpblk_t min;
640 ext4_grpblk_t max;
641 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500642 unsigned short border;
643
Valerie Clementb73fce62008-02-15 13:48:51 -0500644 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500645
646 border = 2 << sb->s_blocksize_bits;
647
648 while (len > 0) {
649 /* find how many blocks can be covered since this position */
650 max = ffs(first | border) - 1;
651
652 /* find how many blocks of power 2 we need to mark */
653 min = fls(len) - 1;
654
655 if (max < min)
656 min = max;
657 chunk = 1 << min;
658
659 /* mark multiblock chunks only */
660 grp->bb_counters[min]++;
661 if (min > 0)
662 mb_clear_bit(first >> min,
663 buddy + sbi->s_mb_offsets[min]);
664
665 len -= chunk;
666 first += chunk;
667 }
668}
669
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400670/*
671 * Cache the order of the largest free extent we have available in this block
672 * group.
673 */
674static void
675mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
676{
677 int i;
678 int bits;
679
680 grp->bb_largest_free_order = -1; /* uninit */
681
682 bits = sb->s_blocksize_bits + 1;
683 for (i = bits; i >= 0; i--) {
684 if (grp->bb_counters[i] > 0) {
685 grp->bb_largest_free_order = i;
686 break;
687 }
688 }
689}
690
Eric Sandeen089ceec2009-07-05 22:17:31 -0400691static noinline_for_stack
692void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500693 void *buddy, void *bitmap, ext4_group_t group)
694{
695 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Eric Sandeena36b4492009-08-25 22:36:45 -0400696 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
697 ext4_grpblk_t i = 0;
698 ext4_grpblk_t first;
699 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500700 unsigned free = 0;
701 unsigned fragments = 0;
702 unsigned long long period = get_cycles();
703
704 /* initialize buddy from bitmap which is aggregation
705 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500706 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500707 grp->bb_first_free = i;
708 while (i < max) {
709 fragments++;
710 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500711 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500712 len = i - first;
713 free += len;
714 if (len > 1)
715 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
716 else
717 grp->bb_counters[0]++;
718 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500719 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500720 }
721 grp->bb_fragments = fragments;
722
723 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400724 ext4_grp_locked_error(sb, group, 0, 0,
725 "%u blocks in bitmap, %u in gd",
726 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500727 /*
728 * If we intent to continue, we consider group descritor
729 * corrupt and update bb_free using bitmap value
730 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500731 grp->bb_free = free;
732 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400733 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500734
735 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
736
737 period = get_cycles() - period;
738 spin_lock(&EXT4_SB(sb)->s_bal_lock);
739 EXT4_SB(sb)->s_mb_buddies_generated++;
740 EXT4_SB(sb)->s_mb_generation_time += period;
741 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
742}
743
744/* The buddy information is attached the buddy cache inode
745 * for convenience. The information regarding each group
746 * is loaded via ext4_mb_load_buddy. The information involve
747 * block bitmap and buddy information. The information are
748 * stored in the inode as
749 *
750 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500751 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500752 *
753 *
754 * one block each for bitmap and buddy information.
755 * So for each group we take up 2 blocks. A page can
756 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
757 * So it can have information regarding groups_per_page which
758 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400759 *
760 * Locking note: This routine takes the block group lock of all groups
761 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500762 */
763
764static int ext4_mb_init_cache(struct page *page, char *incore)
765{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400766 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500767 int blocksize;
768 int blocks_per_page;
769 int groups_per_page;
770 int err = 0;
771 int i;
772 ext4_group_t first_group;
773 int first_block;
774 struct super_block *sb;
775 struct buffer_head *bhs;
776 struct buffer_head **bh;
777 struct inode *inode;
778 char *data;
779 char *bitmap;
780
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400781 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500782
783 inode = page->mapping->host;
784 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400785 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500786 blocksize = 1 << inode->i_blkbits;
787 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
788
789 groups_per_page = blocks_per_page >> 1;
790 if (groups_per_page == 0)
791 groups_per_page = 1;
792
793 /* allocate buffer_heads to read bitmaps */
794 if (groups_per_page > 1) {
795 err = -ENOMEM;
796 i = sizeof(struct buffer_head *) * groups_per_page;
797 bh = kzalloc(i, GFP_NOFS);
798 if (bh == NULL)
799 goto out;
800 } else
801 bh = &bhs;
802
803 first_group = page->index * blocks_per_page / 2;
804
805 /* read all groups the page covers into the cache */
806 for (i = 0; i < groups_per_page; i++) {
807 struct ext4_group_desc *desc;
808
Theodore Ts'o8df96752009-05-01 08:50:38 -0400809 if (first_group + i >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500810 break;
811
812 err = -EIO;
813 desc = ext4_get_group_desc(sb, first_group + i, NULL);
814 if (desc == NULL)
815 goto out;
816
817 err = -ENOMEM;
818 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
819 if (bh[i] == NULL)
820 goto out;
821
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500822 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500823 continue;
824
Frederic Bohec806e682008-10-10 08:09:18 -0400825 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500826 if (bitmap_uptodate(bh[i])) {
827 unlock_buffer(bh[i]);
828 continue;
829 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400830 ext4_lock_group(sb, first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500831 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
832 ext4_init_block_bitmap(sb, bh[i],
833 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500834 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500835 set_buffer_uptodate(bh[i]);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400836 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500837 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500838 continue;
839 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400840 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500841 if (buffer_uptodate(bh[i])) {
842 /*
843 * if not uninit if bh is uptodate,
844 * bitmap is also uptodate
845 */
846 set_bitmap_uptodate(bh[i]);
847 unlock_buffer(bh[i]);
848 continue;
849 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500850 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500851 /*
852 * submit the buffer_head for read. We can
853 * safely mark the bitmap as uptodate now.
854 * We do it here so the bitmap uptodate bit
855 * get set with buffer lock held.
856 */
857 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500858 bh[i]->b_end_io = end_buffer_read_sync;
859 submit_bh(READ, bh[i]);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400860 mb_debug(1, "read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500861 }
862
863 /* wait for I/O completion */
864 for (i = 0; i < groups_per_page && bh[i]; i++)
865 wait_on_buffer(bh[i]);
866
867 err = -EIO;
868 for (i = 0; i < groups_per_page && bh[i]; i++)
869 if (!buffer_uptodate(bh[i]))
870 goto out;
871
Mingming Cao31b481d2008-07-11 19:27:31 -0400872 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500873 first_block = page->index * blocks_per_page;
Aneesh Kumar K.V29eaf022009-01-05 21:48:56 -0500874 /* init the page */
875 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -0500876 for (i = 0; i < blocks_per_page; i++) {
877 int group;
878 struct ext4_group_info *grinfo;
879
880 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400881 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500882 break;
883
884 /*
885 * data carry information regarding this
886 * particular group in the format specified
887 * above
888 *
889 */
890 data = page_address(page) + (i * blocksize);
891 bitmap = bh[group - first_group]->b_data;
892
893 /*
894 * We place the buddy block and bitmap block
895 * close together
896 */
897 if ((first_block + i) & 1) {
898 /* this is block of buddy */
899 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400900 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500901 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400902 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500903 grinfo = ext4_get_group_info(sb, group);
904 grinfo->bb_fragments = 0;
905 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400906 sizeof(*grinfo->bb_counters) *
907 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500908 /*
909 * incore got set to the group block bitmap below
910 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500911 ext4_lock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500912 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500913 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500914 incore = NULL;
915 } else {
916 /* this is block of bitmap */
917 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400918 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500919 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400920 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500921
922 /* see comments in ext4_mb_put_pa() */
923 ext4_lock_group(sb, group);
924 memcpy(data, bitmap, blocksize);
925
926 /* mark all preallocated blks used in in-core bitmap */
927 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500928 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500929 ext4_unlock_group(sb, group);
930
931 /* set incore so that the buddy information can be
932 * generated using this
933 */
934 incore = data;
935 }
936 }
937 SetPageUptodate(page);
938
939out:
940 if (bh) {
941 for (i = 0; i < groups_per_page && bh[i]; i++)
942 brelse(bh[i]);
943 if (bh != &bhs)
944 kfree(bh);
945 }
946 return err;
947}
948
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400949/*
950 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
951 * block group lock of all groups for this page; do not hold the BG lock when
952 * calling this routine!
953 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400954static noinline_for_stack
955int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
956{
957
958 int ret = 0;
959 void *bitmap;
960 int blocks_per_page;
961 int block, pnum, poff;
962 int num_grp_locked = 0;
963 struct ext4_group_info *this_grp;
964 struct ext4_sb_info *sbi = EXT4_SB(sb);
965 struct inode *inode = sbi->s_buddy_cache;
966 struct page *page = NULL, *bitmap_page = NULL;
967
968 mb_debug(1, "init group %u\n", group);
969 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
970 this_grp = ext4_get_group_info(sb, group);
971 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -0400972 * This ensures that we don't reinit the buddy cache
973 * page which map to the group from which we are already
974 * allocating. If we are looking at the buddy cache we would
975 * have taken a reference using ext4_mb_load_buddy and that
976 * would have taken the alloc_sem lock.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400977 */
978 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
979 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
980 /*
981 * somebody initialized the group
982 * return without doing anything
983 */
984 ret = 0;
985 goto err;
986 }
987 /*
988 * the buddy cache inode stores the block bitmap
989 * and buddy information in consecutive blocks.
990 * So for each group we need two blocks.
991 */
992 block = group * 2;
993 pnum = block / blocks_per_page;
994 poff = block % blocks_per_page;
995 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
996 if (page) {
997 BUG_ON(page->mapping != inode->i_mapping);
998 ret = ext4_mb_init_cache(page, NULL);
999 if (ret) {
1000 unlock_page(page);
1001 goto err;
1002 }
1003 unlock_page(page);
1004 }
1005 if (page == NULL || !PageUptodate(page)) {
1006 ret = -EIO;
1007 goto err;
1008 }
1009 mark_page_accessed(page);
1010 bitmap_page = page;
1011 bitmap = page_address(page) + (poff * sb->s_blocksize);
1012
1013 /* init buddy cache */
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 == bitmap_page) {
1019 /*
1020 * If both the bitmap and buddy are in
1021 * the same page we don't need to force
1022 * init the buddy
1023 */
1024 unlock_page(page);
1025 } else if (page) {
1026 BUG_ON(page->mapping != inode->i_mapping);
1027 ret = ext4_mb_init_cache(page, bitmap);
1028 if (ret) {
1029 unlock_page(page);
1030 goto err;
1031 }
1032 unlock_page(page);
1033 }
1034 if (page == NULL || !PageUptodate(page)) {
1035 ret = -EIO;
1036 goto err;
1037 }
1038 mark_page_accessed(page);
1039err:
1040 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1041 if (bitmap_page)
1042 page_cache_release(bitmap_page);
1043 if (page)
1044 page_cache_release(page);
1045 return ret;
1046}
1047
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001048/*
1049 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1050 * block group lock of all groups for this page; do not hold the BG lock when
1051 * calling this routine!
1052 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001053static noinline_for_stack int
1054ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1055 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001056{
Alex Tomasc9de5602008-01-29 00:19:52 -05001057 int blocks_per_page;
1058 int block;
1059 int pnum;
1060 int poff;
1061 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001062 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001063 struct ext4_group_info *grp;
1064 struct ext4_sb_info *sbi = EXT4_SB(sb);
1065 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001066
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001067 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001068
1069 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001070 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001071
1072 e4b->bd_blkbits = sb->s_blocksize_bits;
1073 e4b->bd_info = ext4_get_group_info(sb, group);
1074 e4b->bd_sb = sb;
1075 e4b->bd_group = group;
1076 e4b->bd_buddy_page = NULL;
1077 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001078 e4b->alloc_semp = &grp->alloc_sem;
1079
1080 /* Take the read lock on the group alloc
1081 * sem. This would make sure a parallel
1082 * ext4_mb_init_group happening on other
1083 * groups mapped by the page is blocked
1084 * till we are done with allocation
1085 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001086repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001087 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001088
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001089 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1090 /* we need to check for group need init flag
1091 * with alloc_semp held so that we can be sure
1092 * that new blocks didn't get added to the group
1093 * when we are loading the buddy cache
1094 */
1095 up_read(e4b->alloc_semp);
1096 /*
1097 * we need full data about the group
1098 * to make a good selection
1099 */
1100 ret = ext4_mb_init_group(sb, group);
1101 if (ret)
1102 return ret;
1103 goto repeat_load_buddy;
1104 }
1105
Alex Tomasc9de5602008-01-29 00:19:52 -05001106 /*
1107 * the buddy cache inode stores the block bitmap
1108 * and buddy information in consecutive blocks.
1109 * So for each group we need two blocks.
1110 */
1111 block = group * 2;
1112 pnum = block / blocks_per_page;
1113 poff = block % blocks_per_page;
1114
1115 /* we could use find_or_create_page(), but it locks page
1116 * what we'd like to avoid in fast path ... */
1117 page = find_get_page(inode->i_mapping, pnum);
1118 if (page == NULL || !PageUptodate(page)) {
1119 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001120 /*
1121 * drop the page reference and try
1122 * to get the page with lock. If we
1123 * are not uptodate that implies
1124 * somebody just created the page but
1125 * is yet to initialize the same. So
1126 * wait for it to initialize.
1127 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001128 page_cache_release(page);
1129 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1130 if (page) {
1131 BUG_ON(page->mapping != inode->i_mapping);
1132 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001133 ret = ext4_mb_init_cache(page, NULL);
1134 if (ret) {
1135 unlock_page(page);
1136 goto err;
1137 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001138 mb_cmp_bitmaps(e4b, page_address(page) +
1139 (poff * sb->s_blocksize));
1140 }
1141 unlock_page(page);
1142 }
1143 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001144 if (page == NULL || !PageUptodate(page)) {
1145 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001146 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001147 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001148 e4b->bd_bitmap_page = page;
1149 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1150 mark_page_accessed(page);
1151
1152 block++;
1153 pnum = block / blocks_per_page;
1154 poff = block % blocks_per_page;
1155
1156 page = find_get_page(inode->i_mapping, pnum);
1157 if (page == NULL || !PageUptodate(page)) {
1158 if (page)
1159 page_cache_release(page);
1160 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1161 if (page) {
1162 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001163 if (!PageUptodate(page)) {
1164 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1165 if (ret) {
1166 unlock_page(page);
1167 goto err;
1168 }
1169 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001170 unlock_page(page);
1171 }
1172 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001173 if (page == NULL || !PageUptodate(page)) {
1174 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001175 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001176 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001177 e4b->bd_buddy_page = page;
1178 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1179 mark_page_accessed(page);
1180
1181 BUG_ON(e4b->bd_bitmap_page == NULL);
1182 BUG_ON(e4b->bd_buddy_page == NULL);
1183
1184 return 0;
1185
1186err:
1187 if (e4b->bd_bitmap_page)
1188 page_cache_release(e4b->bd_bitmap_page);
1189 if (e4b->bd_buddy_page)
1190 page_cache_release(e4b->bd_buddy_page);
1191 e4b->bd_buddy = NULL;
1192 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001193
1194 /* Done with the buddy cache */
1195 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001196 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001197}
1198
Jing Zhange39e07f2010-05-14 00:00:00 -04001199static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001200{
1201 if (e4b->bd_bitmap_page)
1202 page_cache_release(e4b->bd_bitmap_page);
1203 if (e4b->bd_buddy_page)
1204 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001205 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001206 if (e4b->alloc_semp)
1207 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001208}
1209
1210
1211static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1212{
1213 int order = 1;
1214 void *bb;
1215
1216 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1217 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1218
1219 bb = EXT4_MB_BUDDY(e4b);
1220 while (order <= e4b->bd_blkbits + 1) {
1221 block = block >> 1;
1222 if (!mb_test_bit(block, bb)) {
1223 /* this block is part of buddy of order 'order' */
1224 return order;
1225 }
1226 bb += 1 << (e4b->bd_blkbits - order);
1227 order++;
1228 }
1229 return 0;
1230}
1231
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001232static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001233{
1234 __u32 *addr;
1235
1236 len = cur + len;
1237 while (cur < len) {
1238 if ((cur & 31) == 0 && (len - cur) >= 32) {
1239 /* fast path: clear whole word at once */
1240 addr = bm + (cur >> 3);
1241 *addr = 0;
1242 cur += 32;
1243 continue;
1244 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001245 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001246 cur++;
1247 }
1248}
1249
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001250static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001251{
1252 __u32 *addr;
1253
1254 len = cur + len;
1255 while (cur < len) {
1256 if ((cur & 31) == 0 && (len - cur) >= 32) {
1257 /* fast path: set whole word at once */
1258 addr = bm + (cur >> 3);
1259 *addr = 0xffffffff;
1260 cur += 32;
1261 continue;
1262 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001263 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001264 cur++;
1265 }
1266}
1267
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001268static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001269 int first, int count)
1270{
1271 int block = 0;
1272 int max = 0;
1273 int order;
1274 void *buddy;
1275 void *buddy2;
1276 struct super_block *sb = e4b->bd_sb;
1277
1278 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001279 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001280 mb_check_buddy(e4b);
1281 mb_free_blocks_double(inode, e4b, first, count);
1282
1283 e4b->bd_info->bb_free += count;
1284 if (first < e4b->bd_info->bb_first_free)
1285 e4b->bd_info->bb_first_free = first;
1286
1287 /* let's maintain fragments counter */
1288 if (first != 0)
1289 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1290 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1291 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1292 if (block && max)
1293 e4b->bd_info->bb_fragments--;
1294 else if (!block && !max)
1295 e4b->bd_info->bb_fragments++;
1296
1297 /* let's maintain buddy itself */
1298 while (count-- > 0) {
1299 block = first++;
1300 order = 0;
1301
1302 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1303 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001304
1305 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001306 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001307 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001308 inode ? inode->i_ino : 0,
1309 blocknr,
1310 "freeing already freed block "
1311 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001312 }
1313 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1314 e4b->bd_info->bb_counters[order]++;
1315
1316 /* start of the buddy */
1317 buddy = mb_find_buddy(e4b, order, &max);
1318
1319 do {
1320 block &= ~1UL;
1321 if (mb_test_bit(block, buddy) ||
1322 mb_test_bit(block + 1, buddy))
1323 break;
1324
1325 /* both the buddies are free, try to coalesce them */
1326 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1327
1328 if (!buddy2)
1329 break;
1330
1331 if (order > 0) {
1332 /* for special purposes, we don't set
1333 * free bits in bitmap */
1334 mb_set_bit(block, buddy);
1335 mb_set_bit(block + 1, buddy);
1336 }
1337 e4b->bd_info->bb_counters[order]--;
1338 e4b->bd_info->bb_counters[order]--;
1339
1340 block = block >> 1;
1341 order++;
1342 e4b->bd_info->bb_counters[order]++;
1343
1344 mb_clear_bit(block, buddy2);
1345 buddy = buddy2;
1346 } while (1);
1347 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001348 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001349 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001350}
1351
1352static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1353 int needed, struct ext4_free_extent *ex)
1354{
1355 int next = block;
1356 int max;
1357 int ord;
1358 void *buddy;
1359
Vincent Minetbc8e6742009-05-15 08:33:18 -04001360 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001361 BUG_ON(ex == NULL);
1362
1363 buddy = mb_find_buddy(e4b, order, &max);
1364 BUG_ON(buddy == NULL);
1365 BUG_ON(block >= max);
1366 if (mb_test_bit(block, buddy)) {
1367 ex->fe_len = 0;
1368 ex->fe_start = 0;
1369 ex->fe_group = 0;
1370 return 0;
1371 }
1372
1373 /* FIXME dorp order completely ? */
1374 if (likely(order == 0)) {
1375 /* find actual order */
1376 order = mb_find_order_for_block(e4b, block);
1377 block = block >> order;
1378 }
1379
1380 ex->fe_len = 1 << order;
1381 ex->fe_start = block << order;
1382 ex->fe_group = e4b->bd_group;
1383
1384 /* calc difference from given start */
1385 next = next - ex->fe_start;
1386 ex->fe_len -= next;
1387 ex->fe_start += next;
1388
1389 while (needed > ex->fe_len &&
1390 (buddy = mb_find_buddy(e4b, order, &max))) {
1391
1392 if (block + 1 >= max)
1393 break;
1394
1395 next = (block + 1) * (1 << order);
1396 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1397 break;
1398
1399 ord = mb_find_order_for_block(e4b, next);
1400
1401 order = ord;
1402 block = next >> order;
1403 ex->fe_len += 1 << order;
1404 }
1405
1406 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1407 return ex->fe_len;
1408}
1409
1410static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1411{
1412 int ord;
1413 int mlen = 0;
1414 int max = 0;
1415 int cur;
1416 int start = ex->fe_start;
1417 int len = ex->fe_len;
1418 unsigned ret = 0;
1419 int len0 = len;
1420 void *buddy;
1421
1422 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1423 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001424 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001425 mb_check_buddy(e4b);
1426 mb_mark_used_double(e4b, start, len);
1427
1428 e4b->bd_info->bb_free -= len;
1429 if (e4b->bd_info->bb_first_free == start)
1430 e4b->bd_info->bb_first_free += len;
1431
1432 /* let's maintain fragments counter */
1433 if (start != 0)
1434 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1435 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1436 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1437 if (mlen && max)
1438 e4b->bd_info->bb_fragments++;
1439 else if (!mlen && !max)
1440 e4b->bd_info->bb_fragments--;
1441
1442 /* let's maintain buddy itself */
1443 while (len) {
1444 ord = mb_find_order_for_block(e4b, start);
1445
1446 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1447 /* the whole chunk may be allocated at once! */
1448 mlen = 1 << ord;
1449 buddy = mb_find_buddy(e4b, ord, &max);
1450 BUG_ON((start >> ord) >= max);
1451 mb_set_bit(start >> ord, buddy);
1452 e4b->bd_info->bb_counters[ord]--;
1453 start += mlen;
1454 len -= mlen;
1455 BUG_ON(len < 0);
1456 continue;
1457 }
1458
1459 /* store for history */
1460 if (ret == 0)
1461 ret = len | (ord << 16);
1462
1463 /* we have to split large buddy */
1464 BUG_ON(ord <= 0);
1465 buddy = mb_find_buddy(e4b, ord, &max);
1466 mb_set_bit(start >> ord, buddy);
1467 e4b->bd_info->bb_counters[ord]--;
1468
1469 ord--;
1470 cur = (start >> ord) & ~1U;
1471 buddy = mb_find_buddy(e4b, ord, &max);
1472 mb_clear_bit(cur, buddy);
1473 mb_clear_bit(cur + 1, buddy);
1474 e4b->bd_info->bb_counters[ord]++;
1475 e4b->bd_info->bb_counters[ord]++;
1476 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001477 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001478
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001479 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001480 mb_check_buddy(e4b);
1481
1482 return ret;
1483}
1484
1485/*
1486 * Must be called under group lock!
1487 */
1488static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1489 struct ext4_buddy *e4b)
1490{
1491 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1492 int ret;
1493
1494 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1495 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1496
1497 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1498 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1499 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1500
1501 /* preallocation can change ac_b_ex, thus we store actually
1502 * allocated blocks for history */
1503 ac->ac_f_ex = ac->ac_b_ex;
1504
1505 ac->ac_status = AC_STATUS_FOUND;
1506 ac->ac_tail = ret & 0xffff;
1507 ac->ac_buddy = ret >> 16;
1508
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001509 /*
1510 * take the page reference. We want the page to be pinned
1511 * so that we don't get a ext4_mb_init_cache_call for this
1512 * group until we update the bitmap. That would mean we
1513 * double allocate blocks. The reference is dropped
1514 * in ext4_mb_release_context
1515 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001516 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1517 get_page(ac->ac_bitmap_page);
1518 ac->ac_buddy_page = e4b->bd_buddy_page;
1519 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001520 /* on allocation we use ac to track the held semaphore */
1521 ac->alloc_semp = e4b->alloc_semp;
1522 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001523 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001524 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001525 spin_lock(&sbi->s_md_lock);
1526 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1527 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1528 spin_unlock(&sbi->s_md_lock);
1529 }
1530}
1531
1532/*
1533 * regular allocator, for general purposes allocation
1534 */
1535
1536static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1537 struct ext4_buddy *e4b,
1538 int finish_group)
1539{
1540 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1541 struct ext4_free_extent *bex = &ac->ac_b_ex;
1542 struct ext4_free_extent *gex = &ac->ac_g_ex;
1543 struct ext4_free_extent ex;
1544 int max;
1545
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001546 if (ac->ac_status == AC_STATUS_FOUND)
1547 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001548 /*
1549 * We don't want to scan for a whole year
1550 */
1551 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1552 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1553 ac->ac_status = AC_STATUS_BREAK;
1554 return;
1555 }
1556
1557 /*
1558 * Haven't found good chunk so far, let's continue
1559 */
1560 if (bex->fe_len < gex->fe_len)
1561 return;
1562
1563 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1564 && bex->fe_group == e4b->bd_group) {
1565 /* recheck chunk's availability - we don't know
1566 * when it was found (within this lock-unlock
1567 * period or not) */
1568 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1569 if (max >= gex->fe_len) {
1570 ext4_mb_use_best_found(ac, e4b);
1571 return;
1572 }
1573 }
1574}
1575
1576/*
1577 * The routine checks whether found extent is good enough. If it is,
1578 * then the extent gets marked used and flag is set to the context
1579 * to stop scanning. Otherwise, the extent is compared with the
1580 * previous found extent and if new one is better, then it's stored
1581 * in the context. Later, the best found extent will be used, if
1582 * mballoc can't find good enough extent.
1583 *
1584 * FIXME: real allocation policy is to be designed yet!
1585 */
1586static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1587 struct ext4_free_extent *ex,
1588 struct ext4_buddy *e4b)
1589{
1590 struct ext4_free_extent *bex = &ac->ac_b_ex;
1591 struct ext4_free_extent *gex = &ac->ac_g_ex;
1592
1593 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001594 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001595 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1596 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1597
1598 ac->ac_found++;
1599
1600 /*
1601 * The special case - take what you catch first
1602 */
1603 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1604 *bex = *ex;
1605 ext4_mb_use_best_found(ac, e4b);
1606 return;
1607 }
1608
1609 /*
1610 * Let's check whether the chuck is good enough
1611 */
1612 if (ex->fe_len == gex->fe_len) {
1613 *bex = *ex;
1614 ext4_mb_use_best_found(ac, e4b);
1615 return;
1616 }
1617
1618 /*
1619 * If this is first found extent, just store it in the context
1620 */
1621 if (bex->fe_len == 0) {
1622 *bex = *ex;
1623 return;
1624 }
1625
1626 /*
1627 * If new found extent is better, store it in the context
1628 */
1629 if (bex->fe_len < gex->fe_len) {
1630 /* if the request isn't satisfied, any found extent
1631 * larger than previous best one is better */
1632 if (ex->fe_len > bex->fe_len)
1633 *bex = *ex;
1634 } else if (ex->fe_len > gex->fe_len) {
1635 /* if the request is satisfied, then we try to find
1636 * an extent that still satisfy the request, but is
1637 * smaller than previous one */
1638 if (ex->fe_len < bex->fe_len)
1639 *bex = *ex;
1640 }
1641
1642 ext4_mb_check_limits(ac, e4b, 0);
1643}
1644
Eric Sandeen089ceec2009-07-05 22:17:31 -04001645static noinline_for_stack
1646int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001647 struct ext4_buddy *e4b)
1648{
1649 struct ext4_free_extent ex = ac->ac_b_ex;
1650 ext4_group_t group = ex.fe_group;
1651 int max;
1652 int err;
1653
1654 BUG_ON(ex.fe_len <= 0);
1655 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1656 if (err)
1657 return err;
1658
1659 ext4_lock_group(ac->ac_sb, group);
1660 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1661
1662 if (max > 0) {
1663 ac->ac_b_ex = ex;
1664 ext4_mb_use_best_found(ac, e4b);
1665 }
1666
1667 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001668 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001669
1670 return 0;
1671}
1672
Eric Sandeen089ceec2009-07-05 22:17:31 -04001673static noinline_for_stack
1674int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001675 struct ext4_buddy *e4b)
1676{
1677 ext4_group_t group = ac->ac_g_ex.fe_group;
1678 int max;
1679 int err;
1680 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001681 struct ext4_free_extent ex;
1682
1683 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1684 return 0;
1685
1686 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1687 if (err)
1688 return err;
1689
1690 ext4_lock_group(ac->ac_sb, group);
1691 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1692 ac->ac_g_ex.fe_len, &ex);
1693
1694 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1695 ext4_fsblk_t start;
1696
Akinobu Mita5661bd62010-03-03 23:53:39 -05001697 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1698 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001699 /* use do_div to get remainder (would be 64-bit modulo) */
1700 if (do_div(start, sbi->s_stripe) == 0) {
1701 ac->ac_found++;
1702 ac->ac_b_ex = ex;
1703 ext4_mb_use_best_found(ac, e4b);
1704 }
1705 } else if (max >= ac->ac_g_ex.fe_len) {
1706 BUG_ON(ex.fe_len <= 0);
1707 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1708 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1709 ac->ac_found++;
1710 ac->ac_b_ex = ex;
1711 ext4_mb_use_best_found(ac, e4b);
1712 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1713 /* Sometimes, caller may want to merge even small
1714 * number of blocks to an existing extent */
1715 BUG_ON(ex.fe_len <= 0);
1716 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1717 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1718 ac->ac_found++;
1719 ac->ac_b_ex = ex;
1720 ext4_mb_use_best_found(ac, e4b);
1721 }
1722 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001723 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001724
1725 return 0;
1726}
1727
1728/*
1729 * The routine scans buddy structures (not bitmap!) from given order
1730 * to max order and tries to find big enough chunk to satisfy the req
1731 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001732static noinline_for_stack
1733void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001734 struct ext4_buddy *e4b)
1735{
1736 struct super_block *sb = ac->ac_sb;
1737 struct ext4_group_info *grp = e4b->bd_info;
1738 void *buddy;
1739 int i;
1740 int k;
1741 int max;
1742
1743 BUG_ON(ac->ac_2order <= 0);
1744 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1745 if (grp->bb_counters[i] == 0)
1746 continue;
1747
1748 buddy = mb_find_buddy(e4b, i, &max);
1749 BUG_ON(buddy == NULL);
1750
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001751 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001752 BUG_ON(k >= max);
1753
1754 ac->ac_found++;
1755
1756 ac->ac_b_ex.fe_len = 1 << i;
1757 ac->ac_b_ex.fe_start = k << i;
1758 ac->ac_b_ex.fe_group = e4b->bd_group;
1759
1760 ext4_mb_use_best_found(ac, e4b);
1761
1762 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1763
1764 if (EXT4_SB(sb)->s_mb_stats)
1765 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1766
1767 break;
1768 }
1769}
1770
1771/*
1772 * The routine scans the group and measures all found extents.
1773 * In order to optimize scanning, caller must pass number of
1774 * free blocks in the group, so the routine can know upper limit.
1775 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001776static noinline_for_stack
1777void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001778 struct ext4_buddy *e4b)
1779{
1780 struct super_block *sb = ac->ac_sb;
1781 void *bitmap = EXT4_MB_BITMAP(e4b);
1782 struct ext4_free_extent ex;
1783 int i;
1784 int free;
1785
1786 free = e4b->bd_info->bb_free;
1787 BUG_ON(free <= 0);
1788
1789 i = e4b->bd_info->bb_first_free;
1790
1791 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001792 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001793 EXT4_BLOCKS_PER_GROUP(sb), i);
1794 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001795 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001796 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001797 * free blocks even though group info says we
1798 * we have free blocks
1799 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001800 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1801 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001802 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001803 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001804 break;
1805 }
1806
1807 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1808 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001809 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001810 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1811 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001812 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001813 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001814 /*
1815 * The number of free blocks differs. This mostly
1816 * indicate that the bitmap is corrupt. So exit
1817 * without claiming the space.
1818 */
1819 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001820 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001821
1822 ext4_mb_measure_extent(ac, &ex, e4b);
1823
1824 i += ex.fe_len;
1825 free -= ex.fe_len;
1826 }
1827
1828 ext4_mb_check_limits(ac, e4b, 1);
1829}
1830
1831/*
1832 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001833 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001834 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001835static noinline_for_stack
1836void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001837 struct ext4_buddy *e4b)
1838{
1839 struct super_block *sb = ac->ac_sb;
1840 struct ext4_sb_info *sbi = EXT4_SB(sb);
1841 void *bitmap = EXT4_MB_BITMAP(e4b);
1842 struct ext4_free_extent ex;
1843 ext4_fsblk_t first_group_block;
1844 ext4_fsblk_t a;
1845 ext4_grpblk_t i;
1846 int max;
1847
1848 BUG_ON(sbi->s_stripe == 0);
1849
1850 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001851 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1852
Alex Tomasc9de5602008-01-29 00:19:52 -05001853 a = first_group_block + sbi->s_stripe - 1;
1854 do_div(a, sbi->s_stripe);
1855 i = (a * sbi->s_stripe) - first_group_block;
1856
1857 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1858 if (!mb_test_bit(i, bitmap)) {
1859 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1860 if (max >= sbi->s_stripe) {
1861 ac->ac_found++;
1862 ac->ac_b_ex = ex;
1863 ext4_mb_use_best_found(ac, e4b);
1864 break;
1865 }
1866 }
1867 i += sbi->s_stripe;
1868 }
1869}
1870
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001871/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001872static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1873 ext4_group_t group, int cr)
1874{
1875 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001876 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001877 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1878
1879 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001880
1881 /* We only do this if the grp has never been initialized */
1882 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1883 int ret = ext4_mb_init_group(ac->ac_sb, group);
1884 if (ret)
1885 return 0;
1886 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001887
1888 free = grp->bb_free;
1889 fragments = grp->bb_fragments;
1890 if (free == 0)
1891 return 0;
1892 if (fragments == 0)
1893 return 0;
1894
1895 switch (cr) {
1896 case 0:
1897 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001898
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001899 if (grp->bb_largest_free_order < ac->ac_2order)
1900 return 0;
1901
Theodore Ts'oa4912122009-03-12 12:18:34 -04001902 /* Avoid using the first bg of a flexgroup for data files */
1903 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1904 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1905 ((group % flex_size) == 0))
1906 return 0;
1907
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001908 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001909 case 1:
1910 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1911 return 1;
1912 break;
1913 case 2:
1914 if (free >= ac->ac_g_ex.fe_len)
1915 return 1;
1916 break;
1917 case 3:
1918 return 1;
1919 default:
1920 BUG();
1921 }
1922
1923 return 0;
1924}
1925
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001926/*
1927 * lock the group_info alloc_sem of all the groups
1928 * belonging to the same buddy cache page. This
1929 * make sure other parallel operation on the buddy
1930 * cache doesn't happen whild holding the buddy cache
1931 * lock
1932 */
1933int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1934{
1935 int i;
1936 int block, pnum;
1937 int blocks_per_page;
1938 int groups_per_page;
Theodore Ts'o8df96752009-05-01 08:50:38 -04001939 ext4_group_t ngroups = ext4_get_groups_count(sb);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001940 ext4_group_t first_group;
1941 struct ext4_group_info *grp;
1942
1943 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1944 /*
1945 * the buddy cache inode stores the block bitmap
1946 * and buddy information in consecutive blocks.
1947 * So for each group we need two blocks.
1948 */
1949 block = group * 2;
1950 pnum = block / blocks_per_page;
1951 first_group = pnum * blocks_per_page / 2;
1952
1953 groups_per_page = blocks_per_page >> 1;
1954 if (groups_per_page == 0)
1955 groups_per_page = 1;
1956 /* read all groups the page covers into the cache */
1957 for (i = 0; i < groups_per_page; i++) {
1958
Theodore Ts'o8df96752009-05-01 08:50:38 -04001959 if ((first_group + i) >= ngroups)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001960 break;
1961 grp = ext4_get_group_info(sb, first_group + i);
1962 /* take all groups write allocation
1963 * semaphore. This make sure there is
1964 * no block allocation going on in any
1965 * of that groups
1966 */
Aneesh Kumar K.Vb7be0192008-11-23 23:51:53 -05001967 down_write_nested(&grp->alloc_sem, i);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001968 }
1969 return i;
1970}
1971
1972void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1973 ext4_group_t group, int locked_group)
1974{
1975 int i;
1976 int block, pnum;
1977 int blocks_per_page;
1978 ext4_group_t first_group;
1979 struct ext4_group_info *grp;
1980
1981 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1982 /*
1983 * the buddy cache inode stores the block bitmap
1984 * and buddy information in consecutive blocks.
1985 * So for each group we need two blocks.
1986 */
1987 block = group * 2;
1988 pnum = block / blocks_per_page;
1989 first_group = pnum * blocks_per_page / 2;
1990 /* release locks on all the groups */
1991 for (i = 0; i < locked_group; i++) {
1992
1993 grp = ext4_get_group_info(sb, first_group + i);
1994 /* take all groups write allocation
1995 * semaphore. This make sure there is
1996 * no block allocation going on in any
1997 * of that groups
1998 */
1999 up_write(&grp->alloc_sem);
2000 }
2001
2002}
2003
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002004static noinline_for_stack int
2005ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002006{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002007 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002008 int cr;
2009 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002010 struct ext4_sb_info *sbi;
2011 struct super_block *sb;
2012 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002013
2014 sb = ac->ac_sb;
2015 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002016 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002017 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002018 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002019 ngroups = sbi->s_blockfile_groups;
2020
Alex Tomasc9de5602008-01-29 00:19:52 -05002021 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2022
2023 /* first, try the goal */
2024 err = ext4_mb_find_by_goal(ac, &e4b);
2025 if (err || ac->ac_status == AC_STATUS_FOUND)
2026 goto out;
2027
2028 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2029 goto out;
2030
2031 /*
2032 * ac->ac2_order is set only if the fe_len is a power of 2
2033 * if ac2_order is set we also set criteria to 0 so that we
2034 * try exact allocation using buddy.
2035 */
2036 i = fls(ac->ac_g_ex.fe_len);
2037 ac->ac_2order = 0;
2038 /*
2039 * We search using buddy data only if the order of the request
2040 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002041 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05002042 */
2043 if (i >= sbi->s_mb_order2_reqs) {
2044 /*
2045 * This should tell if fe_len is exactly power of 2
2046 */
2047 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2048 ac->ac_2order = i - 1;
2049 }
2050
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002051 /* if stream allocation is enabled, use global goal */
2052 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002053 /* TBD: may be hot point */
2054 spin_lock(&sbi->s_md_lock);
2055 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2056 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2057 spin_unlock(&sbi->s_md_lock);
2058 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002059
Alex Tomasc9de5602008-01-29 00:19:52 -05002060 /* Let's just scan groups to find more-less suitable blocks */
2061 cr = ac->ac_2order ? 0 : 1;
2062 /*
2063 * cr == 0 try to get exact allocation,
2064 * cr == 3 try to get anything
2065 */
2066repeat:
2067 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2068 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002069 /*
2070 * searching for the right group start
2071 * from the goal value specified
2072 */
2073 group = ac->ac_g_ex.fe_group;
2074
Theodore Ts'o8df96752009-05-01 08:50:38 -04002075 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002076 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002077 group = 0;
2078
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002079 /* This now checks without needing the buddy page */
2080 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002081 continue;
2082
Alex Tomasc9de5602008-01-29 00:19:52 -05002083 err = ext4_mb_load_buddy(sb, group, &e4b);
2084 if (err)
2085 goto out;
2086
2087 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002088
2089 /*
2090 * We need to check again after locking the
2091 * block group
2092 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002093 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002094 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002095 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002096 continue;
2097 }
2098
2099 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002100 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002101 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002102 else if (cr == 1 && sbi->s_stripe &&
2103 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002104 ext4_mb_scan_aligned(ac, &e4b);
2105 else
2106 ext4_mb_complex_scan_group(ac, &e4b);
2107
2108 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002109 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002110
2111 if (ac->ac_status != AC_STATUS_CONTINUE)
2112 break;
2113 }
2114 }
2115
2116 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2117 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2118 /*
2119 * We've been searching too long. Let's try to allocate
2120 * the best chunk we've found so far
2121 */
2122
2123 ext4_mb_try_best_found(ac, &e4b);
2124 if (ac->ac_status != AC_STATUS_FOUND) {
2125 /*
2126 * Someone more lucky has already allocated it.
2127 * The only thing we can do is just take first
2128 * found block(s)
2129 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2130 */
2131 ac->ac_b_ex.fe_group = 0;
2132 ac->ac_b_ex.fe_start = 0;
2133 ac->ac_b_ex.fe_len = 0;
2134 ac->ac_status = AC_STATUS_CONTINUE;
2135 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2136 cr = 3;
2137 atomic_inc(&sbi->s_mb_lost_chunks);
2138 goto repeat;
2139 }
2140 }
2141out:
2142 return err;
2143}
2144
Alex Tomasc9de5602008-01-29 00:19:52 -05002145static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2146{
2147 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002148 ext4_group_t group;
2149
Theodore Ts'o8df96752009-05-01 08:50:38 -04002150 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002151 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002152 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002153 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002154}
2155
2156static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2157{
2158 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002159 ext4_group_t group;
2160
2161 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002162 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002163 return NULL;
2164 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002165 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002166}
2167
2168static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2169{
2170 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002171 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002172 int i;
2173 int err;
2174 struct ext4_buddy e4b;
2175 struct sg {
2176 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002177 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002178 } sg;
2179
2180 group--;
2181 if (group == 0)
2182 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2183 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2184 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2185 "group", "free", "frags", "first",
2186 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2187 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2188
2189 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2190 sizeof(struct ext4_group_info);
2191 err = ext4_mb_load_buddy(sb, group, &e4b);
2192 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002193 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002194 return 0;
2195 }
2196 ext4_lock_group(sb, group);
2197 memcpy(&sg, ext4_get_group_info(sb, group), i);
2198 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002199 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002200
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002201 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002202 sg.info.bb_fragments, sg.info.bb_first_free);
2203 for (i = 0; i <= 13; i++)
2204 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2205 sg.info.bb_counters[i] : 0);
2206 seq_printf(seq, " ]\n");
2207
2208 return 0;
2209}
2210
2211static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2212{
2213}
2214
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002215static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002216 .start = ext4_mb_seq_groups_start,
2217 .next = ext4_mb_seq_groups_next,
2218 .stop = ext4_mb_seq_groups_stop,
2219 .show = ext4_mb_seq_groups_show,
2220};
2221
2222static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2223{
2224 struct super_block *sb = PDE(inode)->data;
2225 int rc;
2226
2227 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2228 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002229 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002230 m->private = sb;
2231 }
2232 return rc;
2233
2234}
2235
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002236static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002237 .owner = THIS_MODULE,
2238 .open = ext4_mb_seq_groups_open,
2239 .read = seq_read,
2240 .llseek = seq_lseek,
2241 .release = seq_release,
2242};
2243
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002244static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2245{
2246 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2247 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2248
2249 BUG_ON(!cachep);
2250 return cachep;
2251}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002252
2253/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002254int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002255 struct ext4_group_desc *desc)
2256{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002257 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002258 int metalen = 0;
2259 struct ext4_sb_info *sbi = EXT4_SB(sb);
2260 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002261 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002262
2263 /*
2264 * First check if this group is the first of a reserved block.
2265 * If it's true, we have to allocate a new table of pointers
2266 * to ext4_group_info structures
2267 */
2268 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2269 metalen = sizeof(*meta_group_info) <<
2270 EXT4_DESC_PER_BLOCK_BITS(sb);
2271 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2272 if (meta_group_info == NULL) {
2273 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2274 "buddy group\n");
2275 goto exit_meta_group_info;
2276 }
2277 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2278 meta_group_info;
2279 }
2280
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002281 meta_group_info =
2282 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2283 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2284
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002285 meta_group_info[i] = kmem_cache_alloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002286 if (meta_group_info[i] == NULL) {
2287 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2288 goto exit_group_info;
2289 }
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002290 memset(meta_group_info[i], 0, kmem_cache_size(cachep));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002291 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2292 &(meta_group_info[i]->bb_state));
2293
2294 /*
2295 * initialize bb_free to be able to skip
2296 * empty groups without initialization
2297 */
2298 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2299 meta_group_info[i]->bb_free =
2300 ext4_free_blocks_after_init(sb, group, desc);
2301 } else {
2302 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002303 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002304 }
2305
2306 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002307 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002308 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002309 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002310
2311#ifdef DOUBLE_CHECK
2312 {
2313 struct buffer_head *bh;
2314 meta_group_info[i]->bb_bitmap =
2315 kmalloc(sb->s_blocksize, GFP_KERNEL);
2316 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2317 bh = ext4_read_block_bitmap(sb, group);
2318 BUG_ON(bh == NULL);
2319 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2320 sb->s_blocksize);
2321 put_bh(bh);
2322 }
2323#endif
2324
2325 return 0;
2326
2327exit_group_info:
2328 /* If a meta_group_info table has been allocated, release it now */
2329 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2330 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2331exit_meta_group_info:
2332 return -ENOMEM;
2333} /* ext4_mb_add_groupinfo */
2334
Alex Tomasc9de5602008-01-29 00:19:52 -05002335static int ext4_mb_init_backend(struct super_block *sb)
2336{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002337 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002338 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002339 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002340 struct ext4_super_block *es = sbi->s_es;
2341 int num_meta_group_infos;
2342 int num_meta_group_infos_max;
2343 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002344 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002345 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002346
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002347 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002348 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002349 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2350
2351 /*
2352 * This is the total number of blocks used by GDT including
2353 * the number of reserved blocks for GDT.
2354 * The s_group_info array is allocated with this value
2355 * to allow a clean online resize without a complex
2356 * manipulation of pointer.
2357 * The drawback is the unused memory when no resize
2358 * occurs but it's very low in terms of pages
2359 * (see comments below)
2360 * Need to handle this properly when META_BG resizing is allowed
2361 */
2362 num_meta_group_infos_max = num_meta_group_infos +
2363 le16_to_cpu(es->s_reserved_gdt_blocks);
2364
2365 /*
2366 * array_size is the size of s_group_info array. We round it
2367 * to the next power of two because this approximation is done
2368 * internally by kmalloc so we can have some more memory
2369 * for free here (e.g. may be used for META_BG resize).
2370 */
2371 array_size = 1;
2372 while (array_size < sizeof(*sbi->s_group_info) *
2373 num_meta_group_infos_max)
2374 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002375 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2376 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2377 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002378 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002379 if (sbi->s_group_info == NULL) {
2380 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2381 return -ENOMEM;
2382 }
2383 sbi->s_buddy_cache = new_inode(sb);
2384 if (sbi->s_buddy_cache == NULL) {
2385 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2386 goto err_freesgi;
2387 }
2388 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002389 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002390 desc = ext4_get_group_desc(sb, i, NULL);
2391 if (desc == NULL) {
2392 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002393 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002394 goto err_freebuddy;
2395 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002396 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2397 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002398 }
2399
2400 return 0;
2401
2402err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002403 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002404 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002405 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002406 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002407 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002408 kfree(sbi->s_group_info[i]);
2409 iput(sbi->s_buddy_cache);
2410err_freesgi:
2411 kfree(sbi->s_group_info);
2412 return -ENOMEM;
2413}
2414
2415int ext4_mb_init(struct super_block *sb, int needs_recovery)
2416{
2417 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002418 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002419 unsigned offset;
2420 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002421 int ret;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002422 int cache_index;
2423 struct kmem_cache *cachep;
2424 char *namep = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002425
Eric Sandeen19278052009-08-25 22:36:25 -04002426 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002427
2428 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2429 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002430 ret = -ENOMEM;
2431 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002432 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002433
Eric Sandeen19278052009-08-25 22:36:25 -04002434 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002435 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2436 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002437 ret = -ENOMEM;
2438 goto out;
2439 }
2440
2441 cache_index = sb->s_blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2442 cachep = ext4_groupinfo_caches[cache_index];
2443 if (!cachep) {
2444 char name[32];
2445 int len = offsetof(struct ext4_group_info,
2446 bb_counters[sb->s_blocksize_bits + 2]);
2447
2448 sprintf(name, "ext4_groupinfo_%d", sb->s_blocksize_bits);
2449 namep = kstrdup(name, GFP_KERNEL);
2450 if (!namep) {
2451 ret = -ENOMEM;
2452 goto out;
2453 }
2454
2455 /* Need to free the kmem_cache_name() when we
2456 * destroy the slab */
2457 cachep = kmem_cache_create(namep, len, 0,
2458 SLAB_RECLAIM_ACCOUNT, NULL);
2459 if (!cachep) {
2460 ret = -ENOMEM;
2461 goto out;
2462 }
2463 ext4_groupinfo_caches[cache_index] = cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002464 }
2465
2466 /* order 0 is regular bitmap */
2467 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2468 sbi->s_mb_offsets[0] = 0;
2469
2470 i = 1;
2471 offset = 0;
2472 max = sb->s_blocksize << 2;
2473 do {
2474 sbi->s_mb_offsets[i] = offset;
2475 sbi->s_mb_maxs[i] = max;
2476 offset += 1 << (sb->s_blocksize_bits - i);
2477 max = max >> 1;
2478 i++;
2479 } while (i <= sb->s_blocksize_bits + 1);
2480
2481 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002482 ret = ext4_mb_init_backend(sb);
2483 if (ret != 0) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002484 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002485 }
2486
2487 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002488 spin_lock_init(&sbi->s_bal_lock);
2489
2490 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2491 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2492 sbi->s_mb_stats = MB_DEFAULT_STATS;
2493 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2494 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002495 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2496
Eric Sandeen730c2132008-09-13 15:23:29 -04002497 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002498 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002499 ret = -ENOMEM;
2500 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002501 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002502 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002503 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002504 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002505 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002506 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2507 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002508 spin_lock_init(&lg->lg_prealloc_lock);
2509 }
2510
Theodore Ts'o296c3552009-09-30 00:32:42 -04002511 if (sbi->s_proc)
2512 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2513 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002514
Frank Mayhar03901312009-01-07 00:06:22 -05002515 if (sbi->s_journal)
2516 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002517out:
2518 if (ret) {
2519 kfree(sbi->s_mb_offsets);
2520 kfree(sbi->s_mb_maxs);
2521 kfree(namep);
2522 }
2523 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002524}
2525
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002526/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002527static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2528{
2529 struct ext4_prealloc_space *pa;
2530 struct list_head *cur, *tmp;
2531 int count = 0;
2532
2533 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2534 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2535 list_del(&pa->pa_group_list);
2536 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002537 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002538 }
2539 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002540 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002541
2542}
2543
2544int ext4_mb_release(struct super_block *sb)
2545{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002546 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002547 ext4_group_t i;
2548 int num_meta_group_infos;
2549 struct ext4_group_info *grinfo;
2550 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002551 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002552
Alex Tomasc9de5602008-01-29 00:19:52 -05002553 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002554 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002555 grinfo = ext4_get_group_info(sb, i);
2556#ifdef DOUBLE_CHECK
2557 kfree(grinfo->bb_bitmap);
2558#endif
2559 ext4_lock_group(sb, i);
2560 ext4_mb_cleanup_pa(grinfo);
2561 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002562 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002563 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002564 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002565 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2566 EXT4_DESC_PER_BLOCK_BITS(sb);
2567 for (i = 0; i < num_meta_group_infos; i++)
2568 kfree(sbi->s_group_info[i]);
2569 kfree(sbi->s_group_info);
2570 }
2571 kfree(sbi->s_mb_offsets);
2572 kfree(sbi->s_mb_maxs);
2573 if (sbi->s_buddy_cache)
2574 iput(sbi->s_buddy_cache);
2575 if (sbi->s_mb_stats) {
2576 printk(KERN_INFO
2577 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2578 atomic_read(&sbi->s_bal_allocated),
2579 atomic_read(&sbi->s_bal_reqs),
2580 atomic_read(&sbi->s_bal_success));
2581 printk(KERN_INFO
2582 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2583 "%u 2^N hits, %u breaks, %u lost\n",
2584 atomic_read(&sbi->s_bal_ex_scanned),
2585 atomic_read(&sbi->s_bal_goals),
2586 atomic_read(&sbi->s_bal_2orders),
2587 atomic_read(&sbi->s_bal_breaks),
2588 atomic_read(&sbi->s_mb_lost_chunks));
2589 printk(KERN_INFO
2590 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2591 sbi->s_mb_buddies_generated++,
2592 sbi->s_mb_generation_time);
2593 printk(KERN_INFO
2594 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2595 atomic_read(&sbi->s_mb_preallocated),
2596 atomic_read(&sbi->s_mb_discarded));
2597 }
2598
Eric Sandeen730c2132008-09-13 15:23:29 -04002599 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002600 if (sbi->s_proc)
2601 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002602
2603 return 0;
2604}
2605
Jiaying Zhang5c521832010-07-27 11:56:05 -04002606static inline void ext4_issue_discard(struct super_block *sb,
2607 ext4_group_t block_group, ext4_grpblk_t block, int count)
2608{
2609 int ret;
2610 ext4_fsblk_t discard_block;
2611
2612 discard_block = block + ext4_group_first_block_no(sb, block_group);
2613 trace_ext4_discard_blocks(sb,
2614 (unsigned long long) discard_block, count);
2615 ret = sb_issue_discard(sb, discard_block, count);
2616 if (ret == EOPNOTSUPP) {
2617 ext4_warning(sb, "discard not supported, disabling");
2618 clear_opt(EXT4_SB(sb)->s_mount_opt, DISCARD);
2619 }
2620}
2621
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002622/*
2623 * This function is called by the jbd2 layer once the commit has finished,
2624 * so we know we can free the blocks that were released with that commit.
2625 */
2626static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002627{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002628 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002629 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002630 struct ext4_group_info *db;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002631 int err, count = 0, count2 = 0;
2632 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002633 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002634
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002635 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2636 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002637
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002638 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002639 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002640
Jiaying Zhang5c521832010-07-27 11:56:05 -04002641 if (test_opt(sb, DISCARD))
2642 ext4_issue_discard(sb, entry->group,
2643 entry->start_blk, entry->count);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002644
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002645 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002646 /* we expect to find existing buddy because it's pinned */
2647 BUG_ON(err != 0);
2648
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002649 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002650 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002651 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002652 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002653 ext4_lock_group(sb, entry->group);
2654 /* Take it out of per group rb tree */
2655 rb_erase(&entry->node, &(db->bb_free_root));
2656 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2657
2658 if (!db->bb_free_root.rb_node) {
2659 /* No more items in the per group rb tree
2660 * balance refcounts from ext4_mb_free_metadata()
2661 */
2662 page_cache_release(e4b.bd_buddy_page);
2663 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002664 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002665 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002666 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002667 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002668 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002669
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002670 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002671}
2672
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002673#ifdef CONFIG_EXT4_DEBUG
2674u8 mb_enable_debug __read_mostly;
2675
2676static struct dentry *debugfs_dir;
2677static struct dentry *debugfs_debug;
2678
2679static void __init ext4_create_debugfs_entry(void)
2680{
2681 debugfs_dir = debugfs_create_dir("ext4", NULL);
2682 if (debugfs_dir)
2683 debugfs_debug = debugfs_create_u8("mballoc-debug",
2684 S_IRUGO | S_IWUSR,
2685 debugfs_dir,
2686 &mb_enable_debug);
2687}
2688
2689static void ext4_remove_debugfs_entry(void)
2690{
2691 debugfs_remove(debugfs_debug);
2692 debugfs_remove(debugfs_dir);
2693}
2694
2695#else
2696
2697static void __init ext4_create_debugfs_entry(void)
2698{
2699}
2700
2701static void ext4_remove_debugfs_entry(void)
2702{
2703}
2704
2705#endif
2706
Alex Tomasc9de5602008-01-29 00:19:52 -05002707int __init init_ext4_mballoc(void)
2708{
2709 ext4_pspace_cachep =
2710 kmem_cache_create("ext4_prealloc_space",
2711 sizeof(struct ext4_prealloc_space),
2712 0, SLAB_RECLAIM_ACCOUNT, NULL);
2713 if (ext4_pspace_cachep == NULL)
2714 return -ENOMEM;
2715
Eric Sandeen256bdb42008-02-10 01:13:33 -05002716 ext4_ac_cachep =
2717 kmem_cache_create("ext4_alloc_context",
2718 sizeof(struct ext4_allocation_context),
2719 0, SLAB_RECLAIM_ACCOUNT, NULL);
2720 if (ext4_ac_cachep == NULL) {
2721 kmem_cache_destroy(ext4_pspace_cachep);
2722 return -ENOMEM;
2723 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002724
2725 ext4_free_ext_cachep =
2726 kmem_cache_create("ext4_free_block_extents",
2727 sizeof(struct ext4_free_data),
2728 0, SLAB_RECLAIM_ACCOUNT, NULL);
2729 if (ext4_free_ext_cachep == NULL) {
2730 kmem_cache_destroy(ext4_pspace_cachep);
2731 kmem_cache_destroy(ext4_ac_cachep);
2732 return -ENOMEM;
2733 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002734 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002735 return 0;
2736}
2737
2738void exit_ext4_mballoc(void)
2739{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002740 int i;
Theodore Ts'o60e66792010-05-17 07:00:00 -04002741 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002742 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2743 * before destroying the slab cache.
2744 */
2745 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002746 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002747 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002748 kmem_cache_destroy(ext4_free_ext_cachep);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002749
2750 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2751 struct kmem_cache *cachep = ext4_groupinfo_caches[i];
2752 if (cachep) {
2753 char *name = (char *)kmem_cache_name(cachep);
2754 kmem_cache_destroy(cachep);
2755 kfree(name);
2756 }
2757 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002758 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002759}
2760
2761
2762/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002763 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002764 * Returns 0 if success or error code
2765 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002766static noinline_for_stack int
2767ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002768 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002769{
2770 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002771 struct ext4_group_desc *gdp;
2772 struct buffer_head *gdp_bh;
2773 struct ext4_sb_info *sbi;
2774 struct super_block *sb;
2775 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002776 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002777
2778 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2779 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2780
2781 sb = ac->ac_sb;
2782 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002783
2784 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002785 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002786 if (!bitmap_bh)
2787 goto out_err;
2788
2789 err = ext4_journal_get_write_access(handle, bitmap_bh);
2790 if (err)
2791 goto out_err;
2792
2793 err = -EIO;
2794 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2795 if (!gdp)
2796 goto out_err;
2797
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002798 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002799 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002800
Alex Tomasc9de5602008-01-29 00:19:52 -05002801 err = ext4_journal_get_write_access(handle, gdp_bh);
2802 if (err)
2803 goto out_err;
2804
Akinobu Mitabda00de2010-03-03 23:53:25 -05002805 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002806
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002807 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002808 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002809 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002810 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002811 /* File system mounted not to panic on error
2812 * Fix the bitmap and repeat the block allocation
2813 * We leak some of the blocks here.
2814 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002815 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2816 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2817 ac->ac_b_ex.fe_len);
2818 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002819 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002820 if (!err)
2821 err = -EAGAIN;
2822 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002823 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002824
2825 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002826#ifdef AGGRESSIVE_CHECK
2827 {
2828 int i;
2829 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2830 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2831 bitmap_bh->b_data));
2832 }
2833 }
2834#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002835 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 -05002836 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2837 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002838 ext4_free_blks_set(sb, gdp,
2839 ext4_free_blocks_after_init(sb,
2840 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002841 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002842 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2843 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002844 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002845
2846 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002847 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002848 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002849 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002850 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002851 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2852 /* release all the reserved blocks if non delalloc */
2853 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002854
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002855 if (sbi->s_log_groups_per_flex) {
2856 ext4_group_t flex_group = ext4_flex_group(sbi,
2857 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002858 atomic_sub(ac->ac_b_ex.fe_len,
2859 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002860 }
2861
Frank Mayhar03901312009-01-07 00:06:22 -05002862 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002863 if (err)
2864 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002865 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002866
2867out_err:
Theodore Ts'oa0375152010-06-11 23:14:04 -04002868 ext4_mark_super_dirty(sb);
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002869 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002870 return err;
2871}
2872
2873/*
2874 * here we normalize request for locality group
2875 * Group request are normalized to s_strip size if we set the same via mount
2876 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002877 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002878 *
2879 * XXX: should we try to preallocate more than the group has now?
2880 */
2881static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2882{
2883 struct super_block *sb = ac->ac_sb;
2884 struct ext4_locality_group *lg = ac->ac_lg;
2885
2886 BUG_ON(lg == NULL);
2887 if (EXT4_SB(sb)->s_stripe)
2888 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2889 else
2890 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002891 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002892 current->pid, ac->ac_g_ex.fe_len);
2893}
2894
2895/*
2896 * Normalization means making request better in terms of
2897 * size and alignment
2898 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002899static noinline_for_stack void
2900ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002901 struct ext4_allocation_request *ar)
2902{
2903 int bsbits, max;
2904 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002905 loff_t size, orig_size, start_off;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002906 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002907 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002908 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002909
2910 /* do normalize only data requests, metadata requests
2911 do not need preallocation */
2912 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2913 return;
2914
2915 /* sometime caller may want exact blocks */
2916 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2917 return;
2918
2919 /* caller may indicate that preallocation isn't
2920 * required (it's a tail, for example) */
2921 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2922 return;
2923
2924 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2925 ext4_mb_normalize_group_request(ac);
2926 return ;
2927 }
2928
2929 bsbits = ac->ac_sb->s_blocksize_bits;
2930
2931 /* first, let's learn actual file size
2932 * given current request is allocated */
2933 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2934 size = size << bsbits;
2935 if (size < i_size_read(ac->ac_inode))
2936 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002937 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002938
Valerie Clement19304792008-05-13 19:31:14 -04002939 /* max size of free chunks */
2940 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002941
Valerie Clement19304792008-05-13 19:31:14 -04002942#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2943 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002944
2945 /* first, try to predict filesize */
2946 /* XXX: should this table be tunable? */
2947 start_off = 0;
2948 if (size <= 16 * 1024) {
2949 size = 16 * 1024;
2950 } else if (size <= 32 * 1024) {
2951 size = 32 * 1024;
2952 } else if (size <= 64 * 1024) {
2953 size = 64 * 1024;
2954 } else if (size <= 128 * 1024) {
2955 size = 128 * 1024;
2956 } else if (size <= 256 * 1024) {
2957 size = 256 * 1024;
2958 } else if (size <= 512 * 1024) {
2959 size = 512 * 1024;
2960 } else if (size <= 1024 * 1024) {
2961 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002962 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002963 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002964 (21 - bsbits)) << 21;
2965 size = 2 * 1024 * 1024;
2966 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002967 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2968 (22 - bsbits)) << 22;
2969 size = 4 * 1024 * 1024;
2970 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002971 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002972 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2973 (23 - bsbits)) << 23;
2974 size = 8 * 1024 * 1024;
2975 } else {
2976 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2977 size = ac->ac_o_ex.fe_len << bsbits;
2978 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002979 size = size >> bsbits;
2980 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002981
2982 /* don't cover already allocated blocks in selected range */
2983 if (ar->pleft && start <= ar->lleft) {
2984 size -= ar->lleft + 1 - start;
2985 start = ar->lleft + 1;
2986 }
2987 if (ar->pright && start + size - 1 >= ar->lright)
2988 size -= start + size - ar->lright;
2989
2990 end = start + size;
2991
2992 /* check we don't cross already preallocated blocks */
2993 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002994 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002995 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002996
Alex Tomasc9de5602008-01-29 00:19:52 -05002997 if (pa->pa_deleted)
2998 continue;
2999 spin_lock(&pa->pa_lock);
3000 if (pa->pa_deleted) {
3001 spin_unlock(&pa->pa_lock);
3002 continue;
3003 }
3004
3005 pa_end = pa->pa_lstart + pa->pa_len;
3006
3007 /* PA must not overlap original request */
3008 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3009 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3010
Eric Sandeen38877f42009-08-17 23:55:24 -04003011 /* skip PAs this normalized request doesn't overlap with */
3012 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003013 spin_unlock(&pa->pa_lock);
3014 continue;
3015 }
3016 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3017
Eric Sandeen38877f42009-08-17 23:55:24 -04003018 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003019 if (pa_end <= ac->ac_o_ex.fe_logical) {
3020 BUG_ON(pa_end < start);
3021 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003022 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003023 BUG_ON(pa->pa_lstart > end);
3024 end = pa->pa_lstart;
3025 }
3026 spin_unlock(&pa->pa_lock);
3027 }
3028 rcu_read_unlock();
3029 size = end - start;
3030
3031 /* XXX: extra loop to check we really don't overlap preallocations */
3032 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003033 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003034 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003035 spin_lock(&pa->pa_lock);
3036 if (pa->pa_deleted == 0) {
3037 pa_end = pa->pa_lstart + pa->pa_len;
3038 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3039 }
3040 spin_unlock(&pa->pa_lock);
3041 }
3042 rcu_read_unlock();
3043
3044 if (start + size <= ac->ac_o_ex.fe_logical &&
3045 start > ac->ac_o_ex.fe_logical) {
3046 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3047 (unsigned long) start, (unsigned long) size,
3048 (unsigned long) ac->ac_o_ex.fe_logical);
3049 }
3050 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3051 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04003052 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003053
3054 /* now prepare goal request */
3055
3056 /* XXX: is it better to align blocks WRT to logical
3057 * placement or satisfy big request as is */
3058 ac->ac_g_ex.fe_logical = start;
3059 ac->ac_g_ex.fe_len = size;
3060
3061 /* define goal start in order to merge */
3062 if (ar->pright && (ar->lright == (start + size))) {
3063 /* merge to the right */
3064 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3065 &ac->ac_f_ex.fe_group,
3066 &ac->ac_f_ex.fe_start);
3067 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3068 }
3069 if (ar->pleft && (ar->lleft + 1 == start)) {
3070 /* merge to the left */
3071 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3072 &ac->ac_f_ex.fe_group,
3073 &ac->ac_f_ex.fe_start);
3074 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3075 }
3076
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003077 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003078 (unsigned) orig_size, (unsigned) start);
3079}
3080
3081static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3082{
3083 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3084
3085 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3086 atomic_inc(&sbi->s_bal_reqs);
3087 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003088 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003089 atomic_inc(&sbi->s_bal_success);
3090 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3091 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3092 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3093 atomic_inc(&sbi->s_bal_goals);
3094 if (ac->ac_found > sbi->s_mb_max_to_scan)
3095 atomic_inc(&sbi->s_bal_breaks);
3096 }
3097
Theodore Ts'o296c3552009-09-30 00:32:42 -04003098 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3099 trace_ext4_mballoc_alloc(ac);
3100 else
3101 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003102}
3103
3104/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003105 * Called on failure; free up any blocks from the inode PA for this
3106 * context. We don't need this for MB_GROUP_PA because we only change
3107 * pa_free in ext4_mb_release_context(), but on failure, we've already
3108 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3109 */
3110static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3111{
3112 struct ext4_prealloc_space *pa = ac->ac_pa;
3113 int len;
3114
3115 if (pa && pa->pa_type == MB_INODE_PA) {
3116 len = ac->ac_b_ex.fe_len;
3117 pa->pa_free += len;
3118 }
3119
3120}
3121
3122/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003123 * use blocks preallocated to inode
3124 */
3125static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3126 struct ext4_prealloc_space *pa)
3127{
3128 ext4_fsblk_t start;
3129 ext4_fsblk_t end;
3130 int len;
3131
3132 /* found preallocated blocks, use them */
3133 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3134 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3135 len = end - start;
3136 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3137 &ac->ac_b_ex.fe_start);
3138 ac->ac_b_ex.fe_len = len;
3139 ac->ac_status = AC_STATUS_FOUND;
3140 ac->ac_pa = pa;
3141
3142 BUG_ON(start < pa->pa_pstart);
3143 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3144 BUG_ON(pa->pa_free < len);
3145 pa->pa_free -= len;
3146
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003147 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003148}
3149
3150/*
3151 * use blocks preallocated to locality group
3152 */
3153static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3154 struct ext4_prealloc_space *pa)
3155{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003156 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003157
Alex Tomasc9de5602008-01-29 00:19:52 -05003158 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3159 &ac->ac_b_ex.fe_group,
3160 &ac->ac_b_ex.fe_start);
3161 ac->ac_b_ex.fe_len = len;
3162 ac->ac_status = AC_STATUS_FOUND;
3163 ac->ac_pa = pa;
3164
3165 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003166 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003167 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003168 * in on-disk bitmap -- see ext4_mb_release_context()
3169 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003170 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003171 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003172}
3173
3174/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003175 * Return the prealloc space that have minimal distance
3176 * from the goal block. @cpa is the prealloc
3177 * space that is having currently known minimal distance
3178 * from the goal block.
3179 */
3180static struct ext4_prealloc_space *
3181ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3182 struct ext4_prealloc_space *pa,
3183 struct ext4_prealloc_space *cpa)
3184{
3185 ext4_fsblk_t cur_distance, new_distance;
3186
3187 if (cpa == NULL) {
3188 atomic_inc(&pa->pa_count);
3189 return pa;
3190 }
3191 cur_distance = abs(goal_block - cpa->pa_pstart);
3192 new_distance = abs(goal_block - pa->pa_pstart);
3193
3194 if (cur_distance < new_distance)
3195 return cpa;
3196
3197 /* drop the previous reference */
3198 atomic_dec(&cpa->pa_count);
3199 atomic_inc(&pa->pa_count);
3200 return pa;
3201}
3202
3203/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003204 * search goal blocks in preallocated space
3205 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003206static noinline_for_stack int
3207ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003208{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003209 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003210 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3211 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003212 struct ext4_prealloc_space *pa, *cpa = NULL;
3213 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003214
3215 /* only data can be preallocated */
3216 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3217 return 0;
3218
3219 /* first, try per-file preallocation */
3220 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003221 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003222
3223 /* all fields in this condition don't change,
3224 * so we can skip locking for them */
3225 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3226 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3227 continue;
3228
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003229 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003230 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003231 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3232 continue;
3233
Alex Tomasc9de5602008-01-29 00:19:52 -05003234 /* found preallocated blocks, use them */
3235 spin_lock(&pa->pa_lock);
3236 if (pa->pa_deleted == 0 && pa->pa_free) {
3237 atomic_inc(&pa->pa_count);
3238 ext4_mb_use_inode_pa(ac, pa);
3239 spin_unlock(&pa->pa_lock);
3240 ac->ac_criteria = 10;
3241 rcu_read_unlock();
3242 return 1;
3243 }
3244 spin_unlock(&pa->pa_lock);
3245 }
3246 rcu_read_unlock();
3247
3248 /* can we use group allocation? */
3249 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3250 return 0;
3251
3252 /* inode may have no locality group for some reason */
3253 lg = ac->ac_lg;
3254 if (lg == NULL)
3255 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003256 order = fls(ac->ac_o_ex.fe_len) - 1;
3257 if (order > PREALLOC_TB_SIZE - 1)
3258 /* The max size of hash table is PREALLOC_TB_SIZE */
3259 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003260
Akinobu Mitabda00de2010-03-03 23:53:25 -05003261 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003262 /*
3263 * search for the prealloc space that is having
3264 * minimal distance from the goal block.
3265 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003266 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3267 rcu_read_lock();
3268 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3269 pa_inode_list) {
3270 spin_lock(&pa->pa_lock);
3271 if (pa->pa_deleted == 0 &&
3272 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003273
3274 cpa = ext4_mb_check_group_pa(goal_block,
3275 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003276 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003277 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003278 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003279 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003280 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003281 if (cpa) {
3282 ext4_mb_use_group_pa(ac, cpa);
3283 ac->ac_criteria = 20;
3284 return 1;
3285 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003286 return 0;
3287}
3288
3289/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003290 * the function goes through all block freed in the group
3291 * but not yet committed and marks them used in in-core bitmap.
3292 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003293 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003294 */
3295static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3296 ext4_group_t group)
3297{
3298 struct rb_node *n;
3299 struct ext4_group_info *grp;
3300 struct ext4_free_data *entry;
3301
3302 grp = ext4_get_group_info(sb, group);
3303 n = rb_first(&(grp->bb_free_root));
3304
3305 while (n) {
3306 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003307 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003308 n = rb_next(n);
3309 }
3310 return;
3311}
3312
3313/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003314 * the function goes through all preallocation in this group and marks them
3315 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003316 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003317 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003318static noinline_for_stack
3319void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003320 ext4_group_t group)
3321{
3322 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3323 struct ext4_prealloc_space *pa;
3324 struct list_head *cur;
3325 ext4_group_t groupnr;
3326 ext4_grpblk_t start;
3327 int preallocated = 0;
3328 int count = 0;
3329 int len;
3330
3331 /* all form of preallocation discards first load group,
3332 * so the only competing code is preallocation use.
3333 * we don't need any locking here
3334 * notice we do NOT ignore preallocations with pa_deleted
3335 * otherwise we could leave used blocks available for
3336 * allocation in buddy when concurrent ext4_mb_put_pa()
3337 * is dropping preallocation
3338 */
3339 list_for_each(cur, &grp->bb_prealloc_list) {
3340 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3341 spin_lock(&pa->pa_lock);
3342 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3343 &groupnr, &start);
3344 len = pa->pa_len;
3345 spin_unlock(&pa->pa_lock);
3346 if (unlikely(len == 0))
3347 continue;
3348 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003349 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003350 preallocated += len;
3351 count++;
3352 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003353 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003354}
3355
3356static void ext4_mb_pa_callback(struct rcu_head *head)
3357{
3358 struct ext4_prealloc_space *pa;
3359 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3360 kmem_cache_free(ext4_pspace_cachep, pa);
3361}
3362
3363/*
3364 * drops a reference to preallocated space descriptor
3365 * if this was the last reference and the space is consumed
3366 */
3367static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3368 struct super_block *sb, struct ext4_prealloc_space *pa)
3369{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003370 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003371 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003372
3373 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3374 return;
3375
3376 /* in this short window concurrent discard can set pa_deleted */
3377 spin_lock(&pa->pa_lock);
3378 if (pa->pa_deleted == 1) {
3379 spin_unlock(&pa->pa_lock);
3380 return;
3381 }
3382
3383 pa->pa_deleted = 1;
3384 spin_unlock(&pa->pa_lock);
3385
Eric Sandeend33a1972009-03-16 23:25:40 -04003386 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003387 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003388 * If doing group-based preallocation, pa_pstart may be in the
3389 * next group when pa is used up
3390 */
3391 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003392 grp_blk--;
3393
3394 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003395
3396 /*
3397 * possible race:
3398 *
3399 * P1 (buddy init) P2 (regular allocation)
3400 * find block B in PA
3401 * copy on-disk bitmap to buddy
3402 * mark B in on-disk bitmap
3403 * drop PA from group
3404 * mark all PAs in buddy
3405 *
3406 * thus, P1 initializes buddy with B available. to prevent this
3407 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3408 * against that pair
3409 */
3410 ext4_lock_group(sb, grp);
3411 list_del(&pa->pa_group_list);
3412 ext4_unlock_group(sb, grp);
3413
3414 spin_lock(pa->pa_obj_lock);
3415 list_del_rcu(&pa->pa_inode_list);
3416 spin_unlock(pa->pa_obj_lock);
3417
3418 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3419}
3420
3421/*
3422 * creates new preallocated space for given inode
3423 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003424static noinline_for_stack int
3425ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003426{
3427 struct super_block *sb = ac->ac_sb;
3428 struct ext4_prealloc_space *pa;
3429 struct ext4_group_info *grp;
3430 struct ext4_inode_info *ei;
3431
3432 /* preallocate only when found space is larger then requested */
3433 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3434 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3435 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3436
3437 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3438 if (pa == NULL)
3439 return -ENOMEM;
3440
3441 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3442 int winl;
3443 int wins;
3444 int win;
3445 int offs;
3446
3447 /* we can't allocate as much as normalizer wants.
3448 * so, found space must get proper lstart
3449 * to cover original request */
3450 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3451 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3452
3453 /* we're limited by original request in that
3454 * logical block must be covered any way
3455 * winl is window we can move our chunk within */
3456 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3457
3458 /* also, we should cover whole original request */
3459 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3460
3461 /* the smallest one defines real window */
3462 win = min(winl, wins);
3463
3464 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3465 if (offs && offs < win)
3466 win = offs;
3467
3468 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3469 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3470 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3471 }
3472
3473 /* preallocation can change ac_b_ex, thus we store actually
3474 * allocated blocks for history */
3475 ac->ac_f_ex = ac->ac_b_ex;
3476
3477 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3478 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3479 pa->pa_len = ac->ac_b_ex.fe_len;
3480 pa->pa_free = pa->pa_len;
3481 atomic_set(&pa->pa_count, 1);
3482 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003483 INIT_LIST_HEAD(&pa->pa_inode_list);
3484 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003485 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003486 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003487
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003488 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003489 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003490 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003491
3492 ext4_mb_use_inode_pa(ac, pa);
3493 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3494
3495 ei = EXT4_I(ac->ac_inode);
3496 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3497
3498 pa->pa_obj_lock = &ei->i_prealloc_lock;
3499 pa->pa_inode = ac->ac_inode;
3500
3501 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3502 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3503 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3504
3505 spin_lock(pa->pa_obj_lock);
3506 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3507 spin_unlock(pa->pa_obj_lock);
3508
3509 return 0;
3510}
3511
3512/*
3513 * creates new preallocated space for locality group inodes belongs to
3514 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003515static noinline_for_stack int
3516ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003517{
3518 struct super_block *sb = ac->ac_sb;
3519 struct ext4_locality_group *lg;
3520 struct ext4_prealloc_space *pa;
3521 struct ext4_group_info *grp;
3522
3523 /* preallocate only when found space is larger then requested */
3524 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3525 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3526 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3527
3528 BUG_ON(ext4_pspace_cachep == NULL);
3529 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3530 if (pa == NULL)
3531 return -ENOMEM;
3532
3533 /* preallocation can change ac_b_ex, thus we store actually
3534 * allocated blocks for history */
3535 ac->ac_f_ex = ac->ac_b_ex;
3536
3537 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3538 pa->pa_lstart = pa->pa_pstart;
3539 pa->pa_len = ac->ac_b_ex.fe_len;
3540 pa->pa_free = pa->pa_len;
3541 atomic_set(&pa->pa_count, 1);
3542 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003543 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003544 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003545 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003546 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003547
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003548 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003549 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3550 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003551
3552 ext4_mb_use_group_pa(ac, pa);
3553 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3554
3555 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3556 lg = ac->ac_lg;
3557 BUG_ON(lg == NULL);
3558
3559 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3560 pa->pa_inode = NULL;
3561
3562 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3563 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3564 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3565
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003566 /*
3567 * We will later add the new pa to the right bucket
3568 * after updating the pa_free in ext4_mb_release_context
3569 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003570 return 0;
3571}
3572
3573static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3574{
3575 int err;
3576
3577 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3578 err = ext4_mb_new_group_pa(ac);
3579 else
3580 err = ext4_mb_new_inode_pa(ac);
3581 return err;
3582}
3583
3584/*
3585 * finds all unused blocks in on-disk bitmap, frees them in
3586 * in-core bitmap and buddy.
3587 * @pa must be unlinked from inode and group lists, so that
3588 * nobody else can find/use it.
3589 * the caller MUST hold group/inode locks.
3590 * TODO: optimize the case when there are no in-core structures yet
3591 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003592static noinline_for_stack int
3593ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003594 struct ext4_prealloc_space *pa,
3595 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003596{
Alex Tomasc9de5602008-01-29 00:19:52 -05003597 struct super_block *sb = e4b->bd_sb;
3598 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003599 unsigned int end;
3600 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003601 ext4_group_t group;
3602 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003603 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003604 int err = 0;
3605 int free = 0;
3606
3607 BUG_ON(pa->pa_deleted == 0);
3608 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003609 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003610 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3611 end = bit + pa->pa_len;
3612
Eric Sandeen256bdb42008-02-10 01:13:33 -05003613 if (ac) {
3614 ac->ac_sb = sb;
3615 ac->ac_inode = pa->pa_inode;
Eric Sandeen256bdb42008-02-10 01:13:33 -05003616 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003617
3618 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003619 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003620 if (bit >= end)
3621 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003622 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003623 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003624 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3625 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003626 free += next - bit;
3627
Eric Sandeen256bdb42008-02-10 01:13:33 -05003628 if (ac) {
3629 ac->ac_b_ex.fe_group = group;
3630 ac->ac_b_ex.fe_start = bit;
3631 ac->ac_b_ex.fe_len = next - bit;
3632 ac->ac_b_ex.fe_logical = 0;
Theodore Ts'o296c3552009-09-30 00:32:42 -04003633 trace_ext4_mballoc_discard(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003634 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003635
Theodore Ts'oe5880d72010-07-27 11:56:04 -04003636 trace_ext4_mb_release_inode_pa(sb, ac, pa, grp_blk_start + bit,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003637 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003638 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3639 bit = next + 1;
3640 }
3641 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003642 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003643 pa, (unsigned long) pa->pa_lstart,
3644 (unsigned long) pa->pa_pstart,
3645 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003646 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003647 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003648 /*
3649 * pa is already deleted so we use the value obtained
3650 * from the bitmap and continue.
3651 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003652 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003653 atomic_add(free, &sbi->s_mb_discarded);
3654
3655 return err;
3656}
3657
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003658static noinline_for_stack int
3659ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003660 struct ext4_prealloc_space *pa,
3661 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003662{
Alex Tomasc9de5602008-01-29 00:19:52 -05003663 struct super_block *sb = e4b->bd_sb;
3664 ext4_group_t group;
3665 ext4_grpblk_t bit;
3666
Theodore Ts'oe5880d72010-07-27 11:56:04 -04003667 trace_ext4_mb_release_group_pa(sb, ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003668 BUG_ON(pa->pa_deleted == 0);
3669 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3670 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3671 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3672 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3673
Eric Sandeen256bdb42008-02-10 01:13:33 -05003674 if (ac) {
3675 ac->ac_sb = sb;
3676 ac->ac_inode = NULL;
3677 ac->ac_b_ex.fe_group = group;
3678 ac->ac_b_ex.fe_start = bit;
3679 ac->ac_b_ex.fe_len = pa->pa_len;
3680 ac->ac_b_ex.fe_logical = 0;
Theodore Ts'o296c3552009-09-30 00:32:42 -04003681 trace_ext4_mballoc_discard(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003682 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003683
3684 return 0;
3685}
3686
3687/*
3688 * releases all preallocations in given group
3689 *
3690 * first, we need to decide discard policy:
3691 * - when do we discard
3692 * 1) ENOSPC
3693 * - how many do we discard
3694 * 1) how many requested
3695 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003696static noinline_for_stack int
3697ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003698 ext4_group_t group, int needed)
3699{
3700 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3701 struct buffer_head *bitmap_bh = NULL;
3702 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003703 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003704 struct list_head list;
3705 struct ext4_buddy e4b;
3706 int err;
3707 int busy = 0;
3708 int free = 0;
3709
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003710 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003711
3712 if (list_empty(&grp->bb_prealloc_list))
3713 return 0;
3714
Theodore Ts'o574ca172008-07-11 19:27:31 -04003715 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003716 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003717 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003718 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003719 }
3720
3721 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003722 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003723 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003724 put_bh(bitmap_bh);
3725 return 0;
3726 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003727
3728 if (needed == 0)
3729 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3730
Alex Tomasc9de5602008-01-29 00:19:52 -05003731 INIT_LIST_HEAD(&list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003732 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003733 if (ac)
3734 ac->ac_sb = sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05003735repeat:
3736 ext4_lock_group(sb, group);
3737 list_for_each_entry_safe(pa, tmp,
3738 &grp->bb_prealloc_list, pa_group_list) {
3739 spin_lock(&pa->pa_lock);
3740 if (atomic_read(&pa->pa_count)) {
3741 spin_unlock(&pa->pa_lock);
3742 busy = 1;
3743 continue;
3744 }
3745 if (pa->pa_deleted) {
3746 spin_unlock(&pa->pa_lock);
3747 continue;
3748 }
3749
3750 /* seems this one can be freed ... */
3751 pa->pa_deleted = 1;
3752
3753 /* we can trust pa_free ... */
3754 free += pa->pa_free;
3755
3756 spin_unlock(&pa->pa_lock);
3757
3758 list_del(&pa->pa_group_list);
3759 list_add(&pa->u.pa_tmp_list, &list);
3760 }
3761
3762 /* if we still need more blocks and some PAs were used, try again */
3763 if (free < needed && busy) {
3764 busy = 0;
3765 ext4_unlock_group(sb, group);
3766 /*
3767 * Yield the CPU here so that we don't get soft lockup
3768 * in non preempt case.
3769 */
3770 yield();
3771 goto repeat;
3772 }
3773
3774 /* found anything to free? */
3775 if (list_empty(&list)) {
3776 BUG_ON(free != 0);
3777 goto out;
3778 }
3779
3780 /* now free all selected PAs */
3781 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3782
3783 /* remove from object (inode or locality group) */
3784 spin_lock(pa->pa_obj_lock);
3785 list_del_rcu(&pa->pa_inode_list);
3786 spin_unlock(pa->pa_obj_lock);
3787
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003788 if (pa->pa_type == MB_GROUP_PA)
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003789 ext4_mb_release_group_pa(&e4b, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003790 else
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003791 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003792
3793 list_del(&pa->u.pa_tmp_list);
3794 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3795 }
3796
3797out:
3798 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003799 if (ac)
3800 kmem_cache_free(ext4_ac_cachep, ac);
Jing Zhange39e07f2010-05-14 00:00:00 -04003801 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003802 put_bh(bitmap_bh);
3803 return free;
3804}
3805
3806/*
3807 * releases all non-used preallocated blocks for given inode
3808 *
3809 * It's important to discard preallocations under i_data_sem
3810 * We don't want another block to be served from the prealloc
3811 * space when we are discarding the inode prealloc space.
3812 *
3813 * FIXME!! Make sure it is valid at all the call sites
3814 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003815void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003816{
3817 struct ext4_inode_info *ei = EXT4_I(inode);
3818 struct super_block *sb = inode->i_sb;
3819 struct buffer_head *bitmap_bh = NULL;
3820 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003821 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003822 ext4_group_t group = 0;
3823 struct list_head list;
3824 struct ext4_buddy e4b;
3825 int err;
3826
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003827 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003828 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3829 return;
3830 }
3831
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003832 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003833 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003834
3835 INIT_LIST_HEAD(&list);
3836
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003837 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003838 if (ac) {
3839 ac->ac_sb = sb;
3840 ac->ac_inode = inode;
3841 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003842repeat:
3843 /* first, collect all pa's in the inode */
3844 spin_lock(&ei->i_prealloc_lock);
3845 while (!list_empty(&ei->i_prealloc_list)) {
3846 pa = list_entry(ei->i_prealloc_list.next,
3847 struct ext4_prealloc_space, pa_inode_list);
3848 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3849 spin_lock(&pa->pa_lock);
3850 if (atomic_read(&pa->pa_count)) {
3851 /* this shouldn't happen often - nobody should
3852 * use preallocation while we're discarding it */
3853 spin_unlock(&pa->pa_lock);
3854 spin_unlock(&ei->i_prealloc_lock);
3855 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3856 WARN_ON(1);
3857 schedule_timeout_uninterruptible(HZ);
3858 goto repeat;
3859
3860 }
3861 if (pa->pa_deleted == 0) {
3862 pa->pa_deleted = 1;
3863 spin_unlock(&pa->pa_lock);
3864 list_del_rcu(&pa->pa_inode_list);
3865 list_add(&pa->u.pa_tmp_list, &list);
3866 continue;
3867 }
3868
3869 /* someone is deleting pa right now */
3870 spin_unlock(&pa->pa_lock);
3871 spin_unlock(&ei->i_prealloc_lock);
3872
3873 /* we have to wait here because pa_deleted
3874 * doesn't mean pa is already unlinked from
3875 * the list. as we might be called from
3876 * ->clear_inode() the inode will get freed
3877 * and concurrent thread which is unlinking
3878 * pa from inode's list may access already
3879 * freed memory, bad-bad-bad */
3880
3881 /* XXX: if this happens too often, we can
3882 * add a flag to force wait only in case
3883 * of ->clear_inode(), but not in case of
3884 * regular truncate */
3885 schedule_timeout_uninterruptible(HZ);
3886 goto repeat;
3887 }
3888 spin_unlock(&ei->i_prealloc_lock);
3889
3890 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003891 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003892 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3893
3894 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003895 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003896 ext4_error(sb, "Error loading buddy information for %u",
3897 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003898 continue;
3899 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003900
Theodore Ts'o574ca172008-07-11 19:27:31 -04003901 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003902 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003903 ext4_error(sb, "Error reading block bitmap for %u",
3904 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003905 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003906 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003907 }
3908
3909 ext4_lock_group(sb, group);
3910 list_del(&pa->pa_group_list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003911 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003912 ext4_unlock_group(sb, group);
3913
Jing Zhange39e07f2010-05-14 00:00:00 -04003914 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003915 put_bh(bitmap_bh);
3916
3917 list_del(&pa->u.pa_tmp_list);
3918 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3919 }
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003920 if (ac)
3921 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003922}
3923
3924/*
3925 * finds all preallocated spaces and return blocks being freed to them
3926 * if preallocated space becomes full (no block is used from the space)
3927 * then the function frees space in buddy
3928 * XXX: at the moment, truncate (which is the only way to free blocks)
3929 * discards all preallocations
3930 */
3931static void ext4_mb_return_to_preallocation(struct inode *inode,
3932 struct ext4_buddy *e4b,
3933 sector_t block, int count)
3934{
3935 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
3936}
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003937#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003938static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3939{
3940 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003941 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003942
Eric Sandeene3570632010-07-27 11:56:08 -04003943 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
3944 return;
3945
Alex Tomasc9de5602008-01-29 00:19:52 -05003946 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3947 " Allocation context details:\n");
3948 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3949 ac->ac_status, ac->ac_flags);
3950 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3951 "best %lu/%lu/%lu@%lu cr %d\n",
3952 (unsigned long)ac->ac_o_ex.fe_group,
3953 (unsigned long)ac->ac_o_ex.fe_start,
3954 (unsigned long)ac->ac_o_ex.fe_len,
3955 (unsigned long)ac->ac_o_ex.fe_logical,
3956 (unsigned long)ac->ac_g_ex.fe_group,
3957 (unsigned long)ac->ac_g_ex.fe_start,
3958 (unsigned long)ac->ac_g_ex.fe_len,
3959 (unsigned long)ac->ac_g_ex.fe_logical,
3960 (unsigned long)ac->ac_b_ex.fe_group,
3961 (unsigned long)ac->ac_b_ex.fe_start,
3962 (unsigned long)ac->ac_b_ex.fe_len,
3963 (unsigned long)ac->ac_b_ex.fe_logical,
3964 (int)ac->ac_criteria);
3965 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3966 ac->ac_found);
3967 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003968 ngroups = ext4_get_groups_count(sb);
3969 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003970 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3971 struct ext4_prealloc_space *pa;
3972 ext4_grpblk_t start;
3973 struct list_head *cur;
3974 ext4_lock_group(sb, i);
3975 list_for_each(cur, &grp->bb_prealloc_list) {
3976 pa = list_entry(cur, struct ext4_prealloc_space,
3977 pa_group_list);
3978 spin_lock(&pa->pa_lock);
3979 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3980 NULL, &start);
3981 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003982 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3983 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003984 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003985 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003986
3987 if (grp->bb_free == 0)
3988 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003989 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003990 i, grp->bb_free, grp->bb_fragments);
3991 }
3992 printk(KERN_ERR "\n");
3993}
3994#else
3995static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3996{
3997 return;
3998}
3999#endif
4000
4001/*
4002 * We use locality group preallocation for small size file. The size of the
4003 * file is determined by the current size or the resulting size after
4004 * allocation which ever is larger
4005 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004006 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004007 */
4008static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4009{
4010 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4011 int bsbits = ac->ac_sb->s_blocksize_bits;
4012 loff_t size, isize;
4013
4014 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4015 return;
4016
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004017 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4018 return;
4019
Alex Tomasc9de5602008-01-29 00:19:52 -05004020 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04004021 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4022 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004023
Theodore Ts'o50797482009-09-18 13:34:02 -04004024 if ((size == isize) &&
4025 !ext4_fs_is_busy(sbi) &&
4026 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4027 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4028 return;
4029 }
4030
Alex Tomasc9de5602008-01-29 00:19:52 -05004031 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004032 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004033 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004034 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004035 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004036 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004037
4038 BUG_ON(ac->ac_lg != NULL);
4039 /*
4040 * locality group prealloc space are per cpu. The reason for having
4041 * per cpu locality group is to reduce the contention between block
4042 * request from multiple CPUs.
4043 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09004044 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004045
4046 /* we're going to use group allocation */
4047 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4048
4049 /* serialize all allocations in the group */
4050 mutex_lock(&ac->ac_lg->lg_mutex);
4051}
4052
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004053static noinline_for_stack int
4054ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004055 struct ext4_allocation_request *ar)
4056{
4057 struct super_block *sb = ar->inode->i_sb;
4058 struct ext4_sb_info *sbi = EXT4_SB(sb);
4059 struct ext4_super_block *es = sbi->s_es;
4060 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004061 unsigned int len;
4062 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004063 ext4_grpblk_t block;
4064
4065 /* we can't allocate > group size */
4066 len = ar->len;
4067
4068 /* just a dirty hack to filter too big requests */
4069 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4070 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4071
4072 /* start searching from the goal */
4073 goal = ar->goal;
4074 if (goal < le32_to_cpu(es->s_first_data_block) ||
4075 goal >= ext4_blocks_count(es))
4076 goal = le32_to_cpu(es->s_first_data_block);
4077 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4078
4079 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04004080 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05004081 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004082 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004083 ac->ac_sb = sb;
4084 ac->ac_inode = ar->inode;
4085 ac->ac_o_ex.fe_logical = ar->logical;
4086 ac->ac_o_ex.fe_group = group;
4087 ac->ac_o_ex.fe_start = block;
4088 ac->ac_o_ex.fe_len = len;
4089 ac->ac_g_ex.fe_logical = ar->logical;
4090 ac->ac_g_ex.fe_group = group;
4091 ac->ac_g_ex.fe_start = block;
4092 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004093 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004094
4095 /* we have to define context: we'll we work with a file or
4096 * locality group. this is a policy, actually */
4097 ext4_mb_group_or_file(ac);
4098
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004099 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004100 "left: %u/%u, right %u/%u to %swritable\n",
4101 (unsigned) ar->len, (unsigned) ar->logical,
4102 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4103 (unsigned) ar->lleft, (unsigned) ar->pleft,
4104 (unsigned) ar->lright, (unsigned) ar->pright,
4105 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4106 return 0;
4107
4108}
4109
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004110static noinline_for_stack void
4111ext4_mb_discard_lg_preallocations(struct super_block *sb,
4112 struct ext4_locality_group *lg,
4113 int order, int total_entries)
4114{
4115 ext4_group_t group = 0;
4116 struct ext4_buddy e4b;
4117 struct list_head discard_list;
4118 struct ext4_prealloc_space *pa, *tmp;
4119 struct ext4_allocation_context *ac;
4120
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004121 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004122
4123 INIT_LIST_HEAD(&discard_list);
4124 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004125 if (ac)
4126 ac->ac_sb = sb;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004127
4128 spin_lock(&lg->lg_prealloc_lock);
4129 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4130 pa_inode_list) {
4131 spin_lock(&pa->pa_lock);
4132 if (atomic_read(&pa->pa_count)) {
4133 /*
4134 * This is the pa that we just used
4135 * for block allocation. So don't
4136 * free that
4137 */
4138 spin_unlock(&pa->pa_lock);
4139 continue;
4140 }
4141 if (pa->pa_deleted) {
4142 spin_unlock(&pa->pa_lock);
4143 continue;
4144 }
4145 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004146 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004147
4148 /* seems this one can be freed ... */
4149 pa->pa_deleted = 1;
4150 spin_unlock(&pa->pa_lock);
4151
4152 list_del_rcu(&pa->pa_inode_list);
4153 list_add(&pa->u.pa_tmp_list, &discard_list);
4154
4155 total_entries--;
4156 if (total_entries <= 5) {
4157 /*
4158 * we want to keep only 5 entries
4159 * allowing it to grow to 8. This
4160 * mak sure we don't call discard
4161 * soon for this list.
4162 */
4163 break;
4164 }
4165 }
4166 spin_unlock(&lg->lg_prealloc_lock);
4167
4168 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4169
4170 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4171 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004172 ext4_error(sb, "Error loading buddy information for %u",
4173 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004174 continue;
4175 }
4176 ext4_lock_group(sb, group);
4177 list_del(&pa->pa_group_list);
4178 ext4_mb_release_group_pa(&e4b, pa, ac);
4179 ext4_unlock_group(sb, group);
4180
Jing Zhange39e07f2010-05-14 00:00:00 -04004181 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004182 list_del(&pa->u.pa_tmp_list);
4183 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4184 }
4185 if (ac)
4186 kmem_cache_free(ext4_ac_cachep, ac);
4187}
4188
4189/*
4190 * We have incremented pa_count. So it cannot be freed at this
4191 * point. Also we hold lg_mutex. So no parallel allocation is
4192 * possible from this lg. That means pa_free cannot be updated.
4193 *
4194 * A parallel ext4_mb_discard_group_preallocations is possible.
4195 * which can cause the lg_prealloc_list to be updated.
4196 */
4197
4198static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4199{
4200 int order, added = 0, lg_prealloc_count = 1;
4201 struct super_block *sb = ac->ac_sb;
4202 struct ext4_locality_group *lg = ac->ac_lg;
4203 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4204
4205 order = fls(pa->pa_free) - 1;
4206 if (order > PREALLOC_TB_SIZE - 1)
4207 /* The max size of hash table is PREALLOC_TB_SIZE */
4208 order = PREALLOC_TB_SIZE - 1;
4209 /* Add the prealloc space to lg */
4210 rcu_read_lock();
4211 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4212 pa_inode_list) {
4213 spin_lock(&tmp_pa->pa_lock);
4214 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004215 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004216 continue;
4217 }
4218 if (!added && pa->pa_free < tmp_pa->pa_free) {
4219 /* Add to the tail of the previous entry */
4220 list_add_tail_rcu(&pa->pa_inode_list,
4221 &tmp_pa->pa_inode_list);
4222 added = 1;
4223 /*
4224 * we want to count the total
4225 * number of entries in the list
4226 */
4227 }
4228 spin_unlock(&tmp_pa->pa_lock);
4229 lg_prealloc_count++;
4230 }
4231 if (!added)
4232 list_add_tail_rcu(&pa->pa_inode_list,
4233 &lg->lg_prealloc_list[order]);
4234 rcu_read_unlock();
4235
4236 /* Now trim the list to be not more than 8 elements */
4237 if (lg_prealloc_count > 8) {
4238 ext4_mb_discard_lg_preallocations(sb, lg,
4239 order, lg_prealloc_count);
4240 return;
4241 }
4242 return ;
4243}
4244
Alex Tomasc9de5602008-01-29 00:19:52 -05004245/*
4246 * release all resource we used in allocation
4247 */
4248static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4249{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004250 struct ext4_prealloc_space *pa = ac->ac_pa;
4251 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004252 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004253 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004254 spin_lock(&pa->pa_lock);
4255 pa->pa_pstart += ac->ac_b_ex.fe_len;
4256 pa->pa_lstart += ac->ac_b_ex.fe_len;
4257 pa->pa_free -= ac->ac_b_ex.fe_len;
4258 pa->pa_len -= ac->ac_b_ex.fe_len;
4259 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004260 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004261 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004262 if (ac->alloc_semp)
4263 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004264 if (pa) {
4265 /*
4266 * We want to add the pa to the right bucket.
4267 * Remove it from the list and while adding
4268 * make sure the list to which we are adding
4269 * doesn't grow big. We need to release
4270 * alloc_semp before calling ext4_mb_add_n_trim()
4271 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004272 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004273 spin_lock(pa->pa_obj_lock);
4274 list_del_rcu(&pa->pa_inode_list);
4275 spin_unlock(pa->pa_obj_lock);
4276 ext4_mb_add_n_trim(ac);
4277 }
4278 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4279 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004280 if (ac->ac_bitmap_page)
4281 page_cache_release(ac->ac_bitmap_page);
4282 if (ac->ac_buddy_page)
4283 page_cache_release(ac->ac_buddy_page);
4284 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4285 mutex_unlock(&ac->ac_lg->lg_mutex);
4286 ext4_mb_collect_stats(ac);
4287 return 0;
4288}
4289
4290static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4291{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004292 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004293 int ret;
4294 int freed = 0;
4295
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004296 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004297 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004298 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4299 freed += ret;
4300 needed -= ret;
4301 }
4302
4303 return freed;
4304}
4305
4306/*
4307 * Main entry point into mballoc to allocate blocks
4308 * it tries to use preallocation first, then falls back
4309 * to usual allocation
4310 */
4311ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004312 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004313{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004314 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004315 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004316 struct ext4_sb_info *sbi;
4317 struct super_block *sb;
4318 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004319 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004320 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004321
4322 sb = ar->inode->i_sb;
4323 sbi = EXT4_SB(sb);
4324
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004325 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004326
Mingming Cao60e58e02009-01-22 18:13:05 +01004327 /*
4328 * For delayed allocation, we could skip the ENOSPC and
4329 * EDQUOT check, as blocks and quotas have been already
4330 * reserved when data being copied into pagecache.
4331 */
4332 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4333 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4334 else {
4335 /* Without delayed allocation we need to verify
4336 * there is enough free blocks to do block allocation
4337 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004338 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004339 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4340 /* let others to free the space */
4341 yield();
4342 ar->len = ar->len >> 1;
4343 }
4344 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004345 *errp = -ENOSPC;
4346 return 0;
4347 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004348 reserv_blks = ar->len;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004349 while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004350 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4351 ar->len--;
4352 }
4353 inquota = ar->len;
4354 if (ar->len == 0) {
4355 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004356 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004357 }
Mingming Caod2a17632008-07-14 17:52:37 -04004358 }
Mingming Caod2a17632008-07-14 17:52:37 -04004359
Eric Sandeen256bdb42008-02-10 01:13:33 -05004360 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004361 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004362 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004363 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004364 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004365 }
4366
Eric Sandeen256bdb42008-02-10 01:13:33 -05004367 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004368 if (*errp) {
4369 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004370 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004371 }
4372
Eric Sandeen256bdb42008-02-10 01:13:33 -05004373 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4374 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004375 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4376 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004377repeat:
4378 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004379 *errp = ext4_mb_regular_allocator(ac);
4380 if (*errp)
4381 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004382
4383 /* as we've just preallocated more space than
4384 * user requested orinally, we store allocated
4385 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004386 if (ac->ac_status == AC_STATUS_FOUND &&
4387 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4388 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004389 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004390 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004391 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004392 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004393 /*
4394 * drop the reference that we took
4395 * in ext4_mb_use_best_found
4396 */
4397 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004398 ac->ac_b_ex.fe_group = 0;
4399 ac->ac_b_ex.fe_start = 0;
4400 ac->ac_b_ex.fe_len = 0;
4401 ac->ac_status = AC_STATUS_CONTINUE;
4402 goto repeat;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004403 } else if (*errp)
4404 errout:
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004405 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004406 else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004407 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4408 ar->len = ac->ac_b_ex.fe_len;
4409 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004410 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004411 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004412 if (freed)
4413 goto repeat;
4414 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004415 }
4416
4417 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004418 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004419 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004420 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004421 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004422 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004423out:
4424 if (ac)
4425 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004426 if (inquota && ar->len < inquota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004427 dquot_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004428 if (!ar->len) {
4429 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4430 /* release all the reserved blocks if non delalloc */
4431 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4432 reserv_blks);
4433 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004434
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004435 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004436
Alex Tomasc9de5602008-01-29 00:19:52 -05004437 return block;
4438}
Alex Tomasc9de5602008-01-29 00:19:52 -05004439
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004440/*
4441 * We can merge two free data extents only if the physical blocks
4442 * are contiguous, AND the extents were freed by the same transaction,
4443 * AND the blocks are associated with the same group.
4444 */
4445static int can_merge(struct ext4_free_data *entry1,
4446 struct ext4_free_data *entry2)
4447{
4448 if ((entry1->t_tid == entry2->t_tid) &&
4449 (entry1->group == entry2->group) &&
4450 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4451 return 1;
4452 return 0;
4453}
4454
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004455static noinline_for_stack int
4456ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004457 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004458{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004459 ext4_group_t group = e4b->bd_group;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004460 ext4_grpblk_t block;
4461 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004462 struct ext4_group_info *db = e4b->bd_info;
4463 struct super_block *sb = e4b->bd_sb;
4464 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004465 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4466 struct rb_node *parent = NULL, *new_node;
4467
Frank Mayhar03901312009-01-07 00:06:22 -05004468 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004469 BUG_ON(e4b->bd_bitmap_page == NULL);
4470 BUG_ON(e4b->bd_buddy_page == NULL);
4471
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004472 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004473 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004474
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004475 if (!*n) {
4476 /* first free block exent. We need to
4477 protect buddy cache from being freed,
4478 * otherwise we'll refresh it from
4479 * on-disk bitmap and lose not-yet-available
4480 * blocks */
4481 page_cache_get(e4b->bd_buddy_page);
4482 page_cache_get(e4b->bd_bitmap_page);
4483 }
4484 while (*n) {
4485 parent = *n;
4486 entry = rb_entry(parent, struct ext4_free_data, node);
4487 if (block < entry->start_blk)
4488 n = &(*n)->rb_left;
4489 else if (block >= (entry->start_blk + entry->count))
4490 n = &(*n)->rb_right;
4491 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004492 ext4_grp_locked_error(sb, group, 0,
4493 ext4_group_first_block_no(sb, group) + block,
4494 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004495 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004496 }
4497 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004498
4499 rb_link_node(new_node, parent, n);
4500 rb_insert_color(new_node, &db->bb_free_root);
4501
4502 /* Now try to see the extent can be merged to left and right */
4503 node = rb_prev(new_node);
4504 if (node) {
4505 entry = rb_entry(node, struct ext4_free_data, node);
4506 if (can_merge(entry, new_entry)) {
4507 new_entry->start_blk = entry->start_blk;
4508 new_entry->count += entry->count;
4509 rb_erase(node, &(db->bb_free_root));
4510 spin_lock(&sbi->s_md_lock);
4511 list_del(&entry->list);
4512 spin_unlock(&sbi->s_md_lock);
4513 kmem_cache_free(ext4_free_ext_cachep, entry);
4514 }
4515 }
4516
4517 node = rb_next(new_node);
4518 if (node) {
4519 entry = rb_entry(node, struct ext4_free_data, node);
4520 if (can_merge(new_entry, entry)) {
4521 new_entry->count += entry->count;
4522 rb_erase(node, &(db->bb_free_root));
4523 spin_lock(&sbi->s_md_lock);
4524 list_del(&entry->list);
4525 spin_unlock(&sbi->s_md_lock);
4526 kmem_cache_free(ext4_free_ext_cachep, entry);
4527 }
4528 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004529 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004530 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004531 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004532 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004533 return 0;
4534}
4535
Theodore Ts'o44338712009-11-22 07:44:56 -05004536/**
4537 * ext4_free_blocks() -- Free given blocks and update quota
4538 * @handle: handle for this transaction
4539 * @inode: inode
4540 * @block: start physical block to free
4541 * @count: number of blocks to count
4542 * @metadata: Are these metadata blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004543 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004544void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004545 struct buffer_head *bh, ext4_fsblk_t block,
4546 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004547{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004548 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004549 struct super_block *sb = inode->i_sb;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004550 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004551 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004552 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004553 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004554 ext4_grpblk_t bit;
4555 struct buffer_head *gd_bh;
4556 ext4_group_t block_group;
4557 struct ext4_sb_info *sbi;
4558 struct ext4_buddy e4b;
4559 int err = 0;
4560 int ret;
4561
Theodore Ts'oe6362602009-11-23 07:17:05 -05004562 if (bh) {
4563 if (block)
4564 BUG_ON(block != bh->b_blocknr);
4565 else
4566 block = bh->b_blocknr;
4567 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004568
Alex Tomasc9de5602008-01-29 00:19:52 -05004569 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004570 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4571 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004572 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004573 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004574 goto error_return;
4575 }
4576
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004577 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004578 trace_ext4_free_blocks(inode, block, count, flags);
4579
4580 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4581 struct buffer_head *tbh = bh;
4582 int i;
4583
4584 BUG_ON(bh && (count > 1));
4585
4586 for (i = 0; i < count; i++) {
4587 if (!bh)
4588 tbh = sb_find_get_block(inode->i_sb,
4589 block + i);
Theodore Ts'o60e66792010-05-17 07:00:00 -04004590 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004591 inode, tbh, block + i);
4592 }
4593 }
4594
Theodore Ts'o60e66792010-05-17 07:00:00 -04004595 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004596 * We need to make sure we don't reuse the freed block until
4597 * after the transaction is committed, which we can do by
4598 * treating the block as metadata, below. We make an
4599 * exception if the inode is to be written in writeback mode
4600 * since writeback mode has weak data consistency guarantees.
4601 */
4602 if (!ext4_should_writeback_data(inode))
4603 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004604
Eric Sandeen256bdb42008-02-10 01:13:33 -05004605 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4606 if (ac) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004607 ac->ac_inode = inode;
4608 ac->ac_sb = sb;
4609 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004610
4611do_more:
4612 overflow = 0;
4613 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4614
4615 /*
4616 * Check to see if we are freeing blocks across a group
4617 * boundary.
4618 */
4619 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4620 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4621 count -= overflow;
4622 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004623 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004624 if (!bitmap_bh) {
4625 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004626 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004627 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004628 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004629 if (!gdp) {
4630 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004631 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004632 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004633
4634 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4635 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4636 in_range(block, ext4_inode_table(sb, gdp),
4637 EXT4_SB(sb)->s_itb_per_group) ||
4638 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4639 EXT4_SB(sb)->s_itb_per_group)) {
4640
Eric Sandeen12062dd2010-02-15 14:19:27 -05004641 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004642 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004643 /* err = 0. ext4_std_error should be a no op */
4644 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004645 }
4646
4647 BUFFER_TRACE(bitmap_bh, "getting write access");
4648 err = ext4_journal_get_write_access(handle, bitmap_bh);
4649 if (err)
4650 goto error_return;
4651
4652 /*
4653 * We are about to modify some metadata. Call the journal APIs
4654 * to unshare ->b_data if a currently-committing transaction is
4655 * using it
4656 */
4657 BUFFER_TRACE(gd_bh, "get_write_access");
4658 err = ext4_journal_get_write_access(handle, gd_bh);
4659 if (err)
4660 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004661#ifdef AGGRESSIVE_CHECK
4662 {
4663 int i;
4664 for (i = 0; i < count; i++)
4665 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4666 }
4667#endif
Eric Sandeen256bdb42008-02-10 01:13:33 -05004668 if (ac) {
4669 ac->ac_b_ex.fe_group = block_group;
4670 ac->ac_b_ex.fe_start = bit;
4671 ac->ac_b_ex.fe_len = count;
Theodore Ts'o296c3552009-09-30 00:32:42 -04004672 trace_ext4_mballoc_free(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004673 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004674
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004675 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4676 if (err)
4677 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004678
4679 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004680 struct ext4_free_data *new_entry;
4681 /*
4682 * blocks being freed are metadata. these blocks shouldn't
4683 * be used until this transaction is committed
4684 */
4685 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4686 new_entry->start_blk = bit;
4687 new_entry->group = block_group;
4688 new_entry->count = count;
4689 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004690
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004691 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004692 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004693 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004694 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004695 /* need to update group_info->bb_free and bitmap
4696 * with group lock held. generate_buddy look at
4697 * them with group lock_held
4698 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004699 ext4_lock_group(sb, block_group);
4700 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004701 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004702 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04004703 if (test_opt(sb, DISCARD))
4704 ext4_issue_discard(sb, block_group, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004705 }
4706
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004707 ret = ext4_free_blks_count(sb, gdp) + count;
4708 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004709 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004710 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004711 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4712
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004713 if (sbi->s_log_groups_per_flex) {
4714 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004715 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004716 }
4717
Jing Zhange39e07f2010-05-14 00:00:00 -04004718 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004719
Theodore Ts'o44338712009-11-22 07:44:56 -05004720 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004721
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004722 /* We dirtied the bitmap block */
4723 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4724 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4725
Alex Tomasc9de5602008-01-29 00:19:52 -05004726 /* And the group descriptor block */
4727 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004728 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004729 if (!err)
4730 err = ret;
4731
4732 if (overflow && !err) {
4733 block += count;
4734 count = overflow;
4735 put_bh(bitmap_bh);
4736 goto do_more;
4737 }
Theodore Ts'oa0375152010-06-11 23:14:04 -04004738 ext4_mark_super_dirty(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004739error_return:
Theodore Ts'o44338712009-11-22 07:44:56 -05004740 if (freed)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004741 dquot_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004742 brelse(bitmap_bh);
4743 ext4_std_error(sb, err);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004744 if (ac)
4745 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004746 return;
4747}