blob: 1791dd4207d5f8af0ca2ede799a5cefbd2b7a187 [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 */
Eric Sandeen2892c152011-02-12 08:12:18 -0500345#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400346static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
347
Eric Sandeen2892c152011-02-12 08:12:18 -0500348static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
349 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
350 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
351 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
352};
353
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500354static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
355 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500356static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
357 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500358static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
359
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500360static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
361{
Alex Tomasc9de5602008-01-29 00:19:52 -0500362#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500363 *bit += ((unsigned long) addr & 7UL) << 3;
364 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500365#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500366 *bit += ((unsigned long) addr & 3UL) << 3;
367 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500368#else
369#error "how many bits you are?!"
370#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500371 return addr;
372}
Alex Tomasc9de5602008-01-29 00:19:52 -0500373
374static inline int mb_test_bit(int bit, void *addr)
375{
376 /*
377 * ext4_test_bit on architecture like powerpc
378 * needs unsigned long aligned address
379 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500380 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500381 return ext4_test_bit(bit, addr);
382}
383
384static inline void mb_set_bit(int bit, void *addr)
385{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500386 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500387 ext4_set_bit(bit, addr);
388}
389
Alex Tomasc9de5602008-01-29 00:19:52 -0500390static inline void mb_clear_bit(int bit, void *addr)
391{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500392 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500393 ext4_clear_bit(bit, addr);
394}
395
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500396static inline int mb_find_next_zero_bit(void *addr, int max, int start)
397{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400398 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500399 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400400 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500401 start += fix;
402
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400403 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
404 if (ret > max)
405 return max;
406 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500407}
408
409static inline int mb_find_next_bit(void *addr, int max, int start)
410{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400411 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500412 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400413 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500414 start += fix;
415
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400416 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
417 if (ret > max)
418 return max;
419 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500420}
421
Alex Tomasc9de5602008-01-29 00:19:52 -0500422static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
423{
424 char *bb;
425
Alex Tomasc9de5602008-01-29 00:19:52 -0500426 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
427 BUG_ON(max == NULL);
428
429 if (order > e4b->bd_blkbits + 1) {
430 *max = 0;
431 return NULL;
432 }
433
434 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500435 if (order == 0) {
436 *max = 1 << (e4b->bd_blkbits + 3);
Alex Tomasc9de5602008-01-29 00:19:52 -0500437 return EXT4_MB_BITMAP(e4b);
Coly Li84b775a2011-02-24 12:51:59 -0500438 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500439
440 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
441 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
442
443 return bb;
444}
445
446#ifdef DOUBLE_CHECK
447static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
448 int first, int count)
449{
450 int i;
451 struct super_block *sb = e4b->bd_sb;
452
453 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
454 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400455 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500456 for (i = 0; i < count; i++) {
457 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
458 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500459
460 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500461 blocknr += first + i;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500462 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400463 inode ? inode->i_ino : 0,
464 blocknr,
465 "freeing block already freed "
466 "(bit %u)",
467 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500468 }
469 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
470 }
471}
472
473static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
474{
475 int i;
476
477 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
478 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400479 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500480 for (i = 0; i < count; i++) {
481 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
482 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
483 }
484}
485
486static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
487{
488 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
489 unsigned char *b1, *b2;
490 int i;
491 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
492 b2 = (unsigned char *) bitmap;
493 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
494 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500495 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400496 "at byte %u(%u): %x in copy != %x "
497 "on disk/prealloc\n",
498 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500499 BUG();
500 }
501 }
502 }
503}
504
505#else
506static inline void mb_free_blocks_double(struct inode *inode,
507 struct ext4_buddy *e4b, int first, int count)
508{
509 return;
510}
511static inline void mb_mark_used_double(struct ext4_buddy *e4b,
512 int first, int count)
513{
514 return;
515}
516static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
517{
518 return;
519}
520#endif
521
522#ifdef AGGRESSIVE_CHECK
523
524#define MB_CHECK_ASSERT(assert) \
525do { \
526 if (!(assert)) { \
527 printk(KERN_EMERG \
528 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
529 function, file, line, # assert); \
530 BUG(); \
531 } \
532} while (0)
533
534static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
535 const char *function, int line)
536{
537 struct super_block *sb = e4b->bd_sb;
538 int order = e4b->bd_blkbits + 1;
539 int max;
540 int max2;
541 int i;
542 int j;
543 int k;
544 int count;
545 struct ext4_group_info *grp;
546 int fragments = 0;
547 int fstart;
548 struct list_head *cur;
549 void *buddy;
550 void *buddy2;
551
Alex Tomasc9de5602008-01-29 00:19:52 -0500552 {
553 static int mb_check_counter;
554 if (mb_check_counter++ % 100 != 0)
555 return 0;
556 }
557
558 while (order > 1) {
559 buddy = mb_find_buddy(e4b, order, &max);
560 MB_CHECK_ASSERT(buddy);
561 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
562 MB_CHECK_ASSERT(buddy2);
563 MB_CHECK_ASSERT(buddy != buddy2);
564 MB_CHECK_ASSERT(max * 2 == max2);
565
566 count = 0;
567 for (i = 0; i < max; i++) {
568
569 if (mb_test_bit(i, buddy)) {
570 /* only single bit in buddy2 may be 1 */
571 if (!mb_test_bit(i << 1, buddy2)) {
572 MB_CHECK_ASSERT(
573 mb_test_bit((i<<1)+1, buddy2));
574 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
575 MB_CHECK_ASSERT(
576 mb_test_bit(i << 1, buddy2));
577 }
578 continue;
579 }
580
581 /* both bits in buddy2 must be 0 */
582 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
583 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
584
585 for (j = 0; j < (1 << order); j++) {
586 k = (i * (1 << order)) + j;
587 MB_CHECK_ASSERT(
588 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
589 }
590 count++;
591 }
592 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
593 order--;
594 }
595
596 fstart = -1;
597 buddy = mb_find_buddy(e4b, 0, &max);
598 for (i = 0; i < max; i++) {
599 if (!mb_test_bit(i, buddy)) {
600 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
601 if (fstart == -1) {
602 fragments++;
603 fstart = i;
604 }
605 continue;
606 }
607 fstart = -1;
608 /* check used bits only */
609 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
610 buddy2 = mb_find_buddy(e4b, j, &max2);
611 k = i >> j;
612 MB_CHECK_ASSERT(k < max2);
613 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
614 }
615 }
616 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
617 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
618
619 grp = ext4_get_group_info(sb, e4b->bd_group);
620 buddy = mb_find_buddy(e4b, 0, &max);
621 list_for_each(cur, &grp->bb_prealloc_list) {
622 ext4_group_t groupnr;
623 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400624 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
625 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500626 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400627 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500628 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
629 }
630 return 0;
631}
632#undef MB_CHECK_ASSERT
633#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400634 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500635#else
636#define mb_check_buddy(e4b)
637#endif
638
639/* FIXME!! need more doc */
640static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400641 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500642 struct ext4_group_info *grp)
643{
644 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400645 ext4_grpblk_t min;
646 ext4_grpblk_t max;
647 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500648 unsigned short border;
649
Valerie Clementb73fce62008-02-15 13:48:51 -0500650 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500651
652 border = 2 << sb->s_blocksize_bits;
653
654 while (len > 0) {
655 /* find how many blocks can be covered since this position */
656 max = ffs(first | border) - 1;
657
658 /* find how many blocks of power 2 we need to mark */
659 min = fls(len) - 1;
660
661 if (max < min)
662 min = max;
663 chunk = 1 << min;
664
665 /* mark multiblock chunks only */
666 grp->bb_counters[min]++;
667 if (min > 0)
668 mb_clear_bit(first >> min,
669 buddy + sbi->s_mb_offsets[min]);
670
671 len -= chunk;
672 first += chunk;
673 }
674}
675
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400676/*
677 * Cache the order of the largest free extent we have available in this block
678 * group.
679 */
680static void
681mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
682{
683 int i;
684 int bits;
685
686 grp->bb_largest_free_order = -1; /* uninit */
687
688 bits = sb->s_blocksize_bits + 1;
689 for (i = bits; i >= 0; i--) {
690 if (grp->bb_counters[i] > 0) {
691 grp->bb_largest_free_order = i;
692 break;
693 }
694 }
695}
696
Eric Sandeen089ceec2009-07-05 22:17:31 -0400697static noinline_for_stack
698void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500699 void *buddy, void *bitmap, ext4_group_t group)
700{
701 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Eric Sandeena36b4492009-08-25 22:36:45 -0400702 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
703 ext4_grpblk_t i = 0;
704 ext4_grpblk_t first;
705 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500706 unsigned free = 0;
707 unsigned fragments = 0;
708 unsigned long long period = get_cycles();
709
710 /* initialize buddy from bitmap which is aggregation
711 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500712 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500713 grp->bb_first_free = i;
714 while (i < max) {
715 fragments++;
716 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500717 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500718 len = i - first;
719 free += len;
720 if (len > 1)
721 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
722 else
723 grp->bb_counters[0]++;
724 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500725 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500726 }
727 grp->bb_fragments = fragments;
728
729 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400730 ext4_grp_locked_error(sb, group, 0, 0,
731 "%u blocks in bitmap, %u in gd",
732 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500733 /*
734 * If we intent to continue, we consider group descritor
735 * corrupt and update bb_free using bitmap value
736 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500737 grp->bb_free = free;
738 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400739 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500740
741 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
742
743 period = get_cycles() - period;
744 spin_lock(&EXT4_SB(sb)->s_bal_lock);
745 EXT4_SB(sb)->s_mb_buddies_generated++;
746 EXT4_SB(sb)->s_mb_generation_time += period;
747 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
748}
749
750/* The buddy information is attached the buddy cache inode
751 * for convenience. The information regarding each group
752 * is loaded via ext4_mb_load_buddy. The information involve
753 * block bitmap and buddy information. The information are
754 * stored in the inode as
755 *
756 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500757 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500758 *
759 *
760 * one block each for bitmap and buddy information.
761 * So for each group we take up 2 blocks. A page can
762 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
763 * So it can have information regarding groups_per_page which
764 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400765 *
766 * Locking note: This routine takes the block group lock of all groups
767 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500768 */
769
770static int ext4_mb_init_cache(struct page *page, char *incore)
771{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400772 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500773 int blocksize;
774 int blocks_per_page;
775 int groups_per_page;
776 int err = 0;
777 int i;
778 ext4_group_t first_group;
779 int first_block;
780 struct super_block *sb;
781 struct buffer_head *bhs;
782 struct buffer_head **bh;
783 struct inode *inode;
784 char *data;
785 char *bitmap;
786
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400787 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500788
789 inode = page->mapping->host;
790 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400791 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500792 blocksize = 1 << inode->i_blkbits;
793 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
794
795 groups_per_page = blocks_per_page >> 1;
796 if (groups_per_page == 0)
797 groups_per_page = 1;
798
799 /* allocate buffer_heads to read bitmaps */
800 if (groups_per_page > 1) {
801 err = -ENOMEM;
802 i = sizeof(struct buffer_head *) * groups_per_page;
803 bh = kzalloc(i, GFP_NOFS);
804 if (bh == NULL)
805 goto out;
806 } else
807 bh = &bhs;
808
809 first_group = page->index * blocks_per_page / 2;
810
811 /* read all groups the page covers into the cache */
812 for (i = 0; i < groups_per_page; i++) {
813 struct ext4_group_desc *desc;
814
Theodore Ts'o8df96752009-05-01 08:50:38 -0400815 if (first_group + i >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500816 break;
817
818 err = -EIO;
819 desc = ext4_get_group_desc(sb, first_group + i, NULL);
820 if (desc == NULL)
821 goto out;
822
823 err = -ENOMEM;
824 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
825 if (bh[i] == NULL)
826 goto out;
827
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500828 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500829 continue;
830
Frederic Bohec806e682008-10-10 08:09:18 -0400831 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500832 if (bitmap_uptodate(bh[i])) {
833 unlock_buffer(bh[i]);
834 continue;
835 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400836 ext4_lock_group(sb, first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500837 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
838 ext4_init_block_bitmap(sb, bh[i],
839 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500840 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500841 set_buffer_uptodate(bh[i]);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400842 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500843 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500844 continue;
845 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400846 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500847 if (buffer_uptodate(bh[i])) {
848 /*
849 * if not uninit if bh is uptodate,
850 * bitmap is also uptodate
851 */
852 set_bitmap_uptodate(bh[i]);
853 unlock_buffer(bh[i]);
854 continue;
855 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500856 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500857 /*
858 * submit the buffer_head for read. We can
859 * safely mark the bitmap as uptodate now.
860 * We do it here so the bitmap uptodate bit
861 * get set with buffer lock held.
862 */
863 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500864 bh[i]->b_end_io = end_buffer_read_sync;
865 submit_bh(READ, bh[i]);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400866 mb_debug(1, "read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500867 }
868
869 /* wait for I/O completion */
870 for (i = 0; i < groups_per_page && bh[i]; i++)
871 wait_on_buffer(bh[i]);
872
873 err = -EIO;
874 for (i = 0; i < groups_per_page && bh[i]; i++)
875 if (!buffer_uptodate(bh[i]))
876 goto out;
877
Mingming Cao31b481d2008-07-11 19:27:31 -0400878 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500879 first_block = page->index * blocks_per_page;
Aneesh Kumar K.V29eaf022009-01-05 21:48:56 -0500880 /* init the page */
881 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -0500882 for (i = 0; i < blocks_per_page; i++) {
883 int group;
884 struct ext4_group_info *grinfo;
885
886 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400887 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500888 break;
889
890 /*
891 * data carry information regarding this
892 * particular group in the format specified
893 * above
894 *
895 */
896 data = page_address(page) + (i * blocksize);
897 bitmap = bh[group - first_group]->b_data;
898
899 /*
900 * We place the buddy block and bitmap block
901 * close together
902 */
903 if ((first_block + i) & 1) {
904 /* this is block of buddy */
905 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400906 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500907 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400908 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500909 grinfo = ext4_get_group_info(sb, group);
910 grinfo->bb_fragments = 0;
911 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400912 sizeof(*grinfo->bb_counters) *
913 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500914 /*
915 * incore got set to the group block bitmap below
916 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500917 ext4_lock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500918 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500919 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500920 incore = NULL;
921 } else {
922 /* this is block of bitmap */
923 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400924 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500925 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400926 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500927
928 /* see comments in ext4_mb_put_pa() */
929 ext4_lock_group(sb, group);
930 memcpy(data, bitmap, blocksize);
931
932 /* mark all preallocated blks used in in-core bitmap */
933 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500934 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500935 ext4_unlock_group(sb, group);
936
937 /* set incore so that the buddy information can be
938 * generated using this
939 */
940 incore = data;
941 }
942 }
943 SetPageUptodate(page);
944
945out:
946 if (bh) {
947 for (i = 0; i < groups_per_page && bh[i]; i++)
948 brelse(bh[i]);
949 if (bh != &bhs)
950 kfree(bh);
951 }
952 return err;
953}
954
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400955/*
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400956 * lock the group_info alloc_sem of all the groups
957 * belonging to the same buddy cache page. This
958 * make sure other parallel operation on the buddy
959 * cache doesn't happen whild holding the buddy cache
960 * lock
961 */
962static int ext4_mb_get_buddy_cache_lock(struct super_block *sb,
963 ext4_group_t group)
964{
965 int i;
966 int block, pnum;
967 int blocks_per_page;
968 int groups_per_page;
969 ext4_group_t ngroups = ext4_get_groups_count(sb);
970 ext4_group_t first_group;
971 struct ext4_group_info *grp;
972
973 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
974 /*
975 * the buddy cache inode stores the block bitmap
976 * and buddy information in consecutive blocks.
977 * So for each group we need two blocks.
978 */
979 block = group * 2;
980 pnum = block / blocks_per_page;
981 first_group = pnum * blocks_per_page / 2;
982
983 groups_per_page = blocks_per_page >> 1;
984 if (groups_per_page == 0)
985 groups_per_page = 1;
986 /* read all groups the page covers into the cache */
987 for (i = 0; i < groups_per_page; i++) {
988
989 if ((first_group + i) >= ngroups)
990 break;
991 grp = ext4_get_group_info(sb, first_group + i);
992 /* take all groups write allocation
993 * semaphore. This make sure there is
994 * no block allocation going on in any
995 * of that groups
996 */
997 down_write_nested(&grp->alloc_sem, i);
998 }
999 return i;
1000}
1001
1002static void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1003 ext4_group_t group, int locked_group)
1004{
1005 int i;
1006 int block, pnum;
1007 int blocks_per_page;
1008 ext4_group_t first_group;
1009 struct ext4_group_info *grp;
1010
1011 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1012 /*
1013 * the buddy cache inode stores the block bitmap
1014 * and buddy information in consecutive blocks.
1015 * So for each group we need two blocks.
1016 */
1017 block = group * 2;
1018 pnum = block / blocks_per_page;
1019 first_group = pnum * blocks_per_page / 2;
1020 /* release locks on all the groups */
1021 for (i = 0; i < locked_group; i++) {
1022
1023 grp = ext4_get_group_info(sb, first_group + i);
1024 /* take all groups write allocation
1025 * semaphore. This make sure there is
1026 * no block allocation going on in any
1027 * of that groups
1028 */
1029 up_write(&grp->alloc_sem);
1030 }
1031
1032}
1033
1034/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001035 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1036 * block group lock of all groups for this page; do not hold the BG lock when
1037 * calling this routine!
1038 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001039static noinline_for_stack
1040int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1041{
1042
1043 int ret = 0;
1044 void *bitmap;
1045 int blocks_per_page;
1046 int block, pnum, poff;
1047 int num_grp_locked = 0;
1048 struct ext4_group_info *this_grp;
1049 struct ext4_sb_info *sbi = EXT4_SB(sb);
1050 struct inode *inode = sbi->s_buddy_cache;
1051 struct page *page = NULL, *bitmap_page = NULL;
1052
1053 mb_debug(1, "init group %u\n", group);
1054 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1055 this_grp = ext4_get_group_info(sb, group);
1056 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001057 * This ensures that we don't reinit the buddy cache
1058 * page which map to the group from which we are already
1059 * allocating. If we are looking at the buddy cache we would
1060 * have taken a reference using ext4_mb_load_buddy and that
1061 * would have taken the alloc_sem lock.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001062 */
1063 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
1064 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
1065 /*
1066 * somebody initialized the group
1067 * return without doing anything
1068 */
1069 ret = 0;
1070 goto err;
1071 }
1072 /*
1073 * the buddy cache inode stores the block bitmap
1074 * and buddy information in consecutive blocks.
1075 * So for each group we need two blocks.
1076 */
1077 block = group * 2;
1078 pnum = block / blocks_per_page;
1079 poff = block % blocks_per_page;
1080 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1081 if (page) {
1082 BUG_ON(page->mapping != inode->i_mapping);
1083 ret = ext4_mb_init_cache(page, NULL);
1084 if (ret) {
1085 unlock_page(page);
1086 goto err;
1087 }
1088 unlock_page(page);
1089 }
1090 if (page == NULL || !PageUptodate(page)) {
1091 ret = -EIO;
1092 goto err;
1093 }
1094 mark_page_accessed(page);
1095 bitmap_page = page;
1096 bitmap = page_address(page) + (poff * sb->s_blocksize);
1097
1098 /* init buddy cache */
1099 block++;
1100 pnum = block / blocks_per_page;
1101 poff = block % blocks_per_page;
1102 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1103 if (page == bitmap_page) {
1104 /*
1105 * If both the bitmap and buddy are in
1106 * the same page we don't need to force
1107 * init the buddy
1108 */
1109 unlock_page(page);
1110 } else if (page) {
1111 BUG_ON(page->mapping != inode->i_mapping);
1112 ret = ext4_mb_init_cache(page, bitmap);
1113 if (ret) {
1114 unlock_page(page);
1115 goto err;
1116 }
1117 unlock_page(page);
1118 }
1119 if (page == NULL || !PageUptodate(page)) {
1120 ret = -EIO;
1121 goto err;
1122 }
1123 mark_page_accessed(page);
1124err:
1125 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1126 if (bitmap_page)
1127 page_cache_release(bitmap_page);
1128 if (page)
1129 page_cache_release(page);
1130 return ret;
1131}
1132
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001133/*
1134 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1135 * block group lock of all groups for this page; do not hold the BG lock when
1136 * calling this routine!
1137 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001138static noinline_for_stack int
1139ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1140 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001141{
Alex Tomasc9de5602008-01-29 00:19:52 -05001142 int blocks_per_page;
1143 int block;
1144 int pnum;
1145 int poff;
1146 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001147 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001148 struct ext4_group_info *grp;
1149 struct ext4_sb_info *sbi = EXT4_SB(sb);
1150 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001151
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001152 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001153
1154 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001155 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001156
1157 e4b->bd_blkbits = sb->s_blocksize_bits;
1158 e4b->bd_info = ext4_get_group_info(sb, group);
1159 e4b->bd_sb = sb;
1160 e4b->bd_group = group;
1161 e4b->bd_buddy_page = NULL;
1162 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001163 e4b->alloc_semp = &grp->alloc_sem;
1164
1165 /* Take the read lock on the group alloc
1166 * sem. This would make sure a parallel
1167 * ext4_mb_init_group happening on other
1168 * groups mapped by the page is blocked
1169 * till we are done with allocation
1170 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001171repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001172 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001173
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001174 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1175 /* we need to check for group need init flag
1176 * with alloc_semp held so that we can be sure
1177 * that new blocks didn't get added to the group
1178 * when we are loading the buddy cache
1179 */
1180 up_read(e4b->alloc_semp);
1181 /*
1182 * we need full data about the group
1183 * to make a good selection
1184 */
1185 ret = ext4_mb_init_group(sb, group);
1186 if (ret)
1187 return ret;
1188 goto repeat_load_buddy;
1189 }
1190
Alex Tomasc9de5602008-01-29 00:19:52 -05001191 /*
1192 * the buddy cache inode stores the block bitmap
1193 * and buddy information in consecutive blocks.
1194 * So for each group we need two blocks.
1195 */
1196 block = group * 2;
1197 pnum = block / blocks_per_page;
1198 poff = block % blocks_per_page;
1199
1200 /* we could use find_or_create_page(), but it locks page
1201 * what we'd like to avoid in fast path ... */
1202 page = find_get_page(inode->i_mapping, pnum);
1203 if (page == NULL || !PageUptodate(page)) {
1204 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001205 /*
1206 * drop the page reference and try
1207 * to get the page with lock. If we
1208 * are not uptodate that implies
1209 * somebody just created the page but
1210 * is yet to initialize the same. So
1211 * wait for it to initialize.
1212 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001213 page_cache_release(page);
1214 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1215 if (page) {
1216 BUG_ON(page->mapping != inode->i_mapping);
1217 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001218 ret = ext4_mb_init_cache(page, NULL);
1219 if (ret) {
1220 unlock_page(page);
1221 goto err;
1222 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001223 mb_cmp_bitmaps(e4b, page_address(page) +
1224 (poff * sb->s_blocksize));
1225 }
1226 unlock_page(page);
1227 }
1228 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001229 if (page == NULL || !PageUptodate(page)) {
1230 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001231 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001232 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001233 e4b->bd_bitmap_page = page;
1234 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1235 mark_page_accessed(page);
1236
1237 block++;
1238 pnum = block / blocks_per_page;
1239 poff = block % blocks_per_page;
1240
1241 page = find_get_page(inode->i_mapping, pnum);
1242 if (page == NULL || !PageUptodate(page)) {
1243 if (page)
1244 page_cache_release(page);
1245 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1246 if (page) {
1247 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001248 if (!PageUptodate(page)) {
1249 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1250 if (ret) {
1251 unlock_page(page);
1252 goto err;
1253 }
1254 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001255 unlock_page(page);
1256 }
1257 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001258 if (page == NULL || !PageUptodate(page)) {
1259 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001260 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001261 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001262 e4b->bd_buddy_page = page;
1263 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1264 mark_page_accessed(page);
1265
1266 BUG_ON(e4b->bd_bitmap_page == NULL);
1267 BUG_ON(e4b->bd_buddy_page == NULL);
1268
1269 return 0;
1270
1271err:
1272 if (e4b->bd_bitmap_page)
1273 page_cache_release(e4b->bd_bitmap_page);
1274 if (e4b->bd_buddy_page)
1275 page_cache_release(e4b->bd_buddy_page);
1276 e4b->bd_buddy = NULL;
1277 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001278
1279 /* Done with the buddy cache */
1280 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001281 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001282}
1283
Jing Zhange39e07f2010-05-14 00:00:00 -04001284static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001285{
1286 if (e4b->bd_bitmap_page)
1287 page_cache_release(e4b->bd_bitmap_page);
1288 if (e4b->bd_buddy_page)
1289 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001290 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001291 if (e4b->alloc_semp)
1292 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001293}
1294
1295
1296static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1297{
1298 int order = 1;
1299 void *bb;
1300
1301 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1302 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1303
1304 bb = EXT4_MB_BUDDY(e4b);
1305 while (order <= e4b->bd_blkbits + 1) {
1306 block = block >> 1;
1307 if (!mb_test_bit(block, bb)) {
1308 /* this block is part of buddy of order 'order' */
1309 return order;
1310 }
1311 bb += 1 << (e4b->bd_blkbits - order);
1312 order++;
1313 }
1314 return 0;
1315}
1316
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001317static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001318{
1319 __u32 *addr;
1320
1321 len = cur + len;
1322 while (cur < len) {
1323 if ((cur & 31) == 0 && (len - cur) >= 32) {
1324 /* fast path: clear whole word at once */
1325 addr = bm + (cur >> 3);
1326 *addr = 0;
1327 cur += 32;
1328 continue;
1329 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001330 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001331 cur++;
1332 }
1333}
1334
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001335static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001336{
1337 __u32 *addr;
1338
1339 len = cur + len;
1340 while (cur < len) {
1341 if ((cur & 31) == 0 && (len - cur) >= 32) {
1342 /* fast path: set whole word at once */
1343 addr = bm + (cur >> 3);
1344 *addr = 0xffffffff;
1345 cur += 32;
1346 continue;
1347 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001348 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001349 cur++;
1350 }
1351}
1352
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001353static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001354 int first, int count)
1355{
1356 int block = 0;
1357 int max = 0;
1358 int order;
1359 void *buddy;
1360 void *buddy2;
1361 struct super_block *sb = e4b->bd_sb;
1362
1363 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001364 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001365 mb_check_buddy(e4b);
1366 mb_free_blocks_double(inode, e4b, first, count);
1367
1368 e4b->bd_info->bb_free += count;
1369 if (first < e4b->bd_info->bb_first_free)
1370 e4b->bd_info->bb_first_free = first;
1371
1372 /* let's maintain fragments counter */
1373 if (first != 0)
1374 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1375 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1376 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1377 if (block && max)
1378 e4b->bd_info->bb_fragments--;
1379 else if (!block && !max)
1380 e4b->bd_info->bb_fragments++;
1381
1382 /* let's maintain buddy itself */
1383 while (count-- > 0) {
1384 block = first++;
1385 order = 0;
1386
1387 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1388 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001389
1390 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001391 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001392 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001393 inode ? inode->i_ino : 0,
1394 blocknr,
1395 "freeing already freed block "
1396 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001397 }
1398 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1399 e4b->bd_info->bb_counters[order]++;
1400
1401 /* start of the buddy */
1402 buddy = mb_find_buddy(e4b, order, &max);
1403
1404 do {
1405 block &= ~1UL;
1406 if (mb_test_bit(block, buddy) ||
1407 mb_test_bit(block + 1, buddy))
1408 break;
1409
1410 /* both the buddies are free, try to coalesce them */
1411 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1412
1413 if (!buddy2)
1414 break;
1415
1416 if (order > 0) {
1417 /* for special purposes, we don't set
1418 * free bits in bitmap */
1419 mb_set_bit(block, buddy);
1420 mb_set_bit(block + 1, buddy);
1421 }
1422 e4b->bd_info->bb_counters[order]--;
1423 e4b->bd_info->bb_counters[order]--;
1424
1425 block = block >> 1;
1426 order++;
1427 e4b->bd_info->bb_counters[order]++;
1428
1429 mb_clear_bit(block, buddy2);
1430 buddy = buddy2;
1431 } while (1);
1432 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001433 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001434 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001435}
1436
1437static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1438 int needed, struct ext4_free_extent *ex)
1439{
1440 int next = block;
1441 int max;
1442 int ord;
1443 void *buddy;
1444
Vincent Minetbc8e6742009-05-15 08:33:18 -04001445 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001446 BUG_ON(ex == NULL);
1447
1448 buddy = mb_find_buddy(e4b, order, &max);
1449 BUG_ON(buddy == NULL);
1450 BUG_ON(block >= max);
1451 if (mb_test_bit(block, buddy)) {
1452 ex->fe_len = 0;
1453 ex->fe_start = 0;
1454 ex->fe_group = 0;
1455 return 0;
1456 }
1457
1458 /* FIXME dorp order completely ? */
1459 if (likely(order == 0)) {
1460 /* find actual order */
1461 order = mb_find_order_for_block(e4b, block);
1462 block = block >> order;
1463 }
1464
1465 ex->fe_len = 1 << order;
1466 ex->fe_start = block << order;
1467 ex->fe_group = e4b->bd_group;
1468
1469 /* calc difference from given start */
1470 next = next - ex->fe_start;
1471 ex->fe_len -= next;
1472 ex->fe_start += next;
1473
1474 while (needed > ex->fe_len &&
1475 (buddy = mb_find_buddy(e4b, order, &max))) {
1476
1477 if (block + 1 >= max)
1478 break;
1479
1480 next = (block + 1) * (1 << order);
1481 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1482 break;
1483
1484 ord = mb_find_order_for_block(e4b, next);
1485
1486 order = ord;
1487 block = next >> order;
1488 ex->fe_len += 1 << order;
1489 }
1490
1491 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1492 return ex->fe_len;
1493}
1494
1495static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1496{
1497 int ord;
1498 int mlen = 0;
1499 int max = 0;
1500 int cur;
1501 int start = ex->fe_start;
1502 int len = ex->fe_len;
1503 unsigned ret = 0;
1504 int len0 = len;
1505 void *buddy;
1506
1507 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1508 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001509 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001510 mb_check_buddy(e4b);
1511 mb_mark_used_double(e4b, start, len);
1512
1513 e4b->bd_info->bb_free -= len;
1514 if (e4b->bd_info->bb_first_free == start)
1515 e4b->bd_info->bb_first_free += len;
1516
1517 /* let's maintain fragments counter */
1518 if (start != 0)
1519 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1520 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1521 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1522 if (mlen && max)
1523 e4b->bd_info->bb_fragments++;
1524 else if (!mlen && !max)
1525 e4b->bd_info->bb_fragments--;
1526
1527 /* let's maintain buddy itself */
1528 while (len) {
1529 ord = mb_find_order_for_block(e4b, start);
1530
1531 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1532 /* the whole chunk may be allocated at once! */
1533 mlen = 1 << ord;
1534 buddy = mb_find_buddy(e4b, ord, &max);
1535 BUG_ON((start >> ord) >= max);
1536 mb_set_bit(start >> ord, buddy);
1537 e4b->bd_info->bb_counters[ord]--;
1538 start += mlen;
1539 len -= mlen;
1540 BUG_ON(len < 0);
1541 continue;
1542 }
1543
1544 /* store for history */
1545 if (ret == 0)
1546 ret = len | (ord << 16);
1547
1548 /* we have to split large buddy */
1549 BUG_ON(ord <= 0);
1550 buddy = mb_find_buddy(e4b, ord, &max);
1551 mb_set_bit(start >> ord, buddy);
1552 e4b->bd_info->bb_counters[ord]--;
1553
1554 ord--;
1555 cur = (start >> ord) & ~1U;
1556 buddy = mb_find_buddy(e4b, ord, &max);
1557 mb_clear_bit(cur, buddy);
1558 mb_clear_bit(cur + 1, buddy);
1559 e4b->bd_info->bb_counters[ord]++;
1560 e4b->bd_info->bb_counters[ord]++;
1561 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001562 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001563
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001564 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001565 mb_check_buddy(e4b);
1566
1567 return ret;
1568}
1569
1570/*
1571 * Must be called under group lock!
1572 */
1573static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1574 struct ext4_buddy *e4b)
1575{
1576 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1577 int ret;
1578
1579 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1580 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1581
1582 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1583 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1584 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1585
1586 /* preallocation can change ac_b_ex, thus we store actually
1587 * allocated blocks for history */
1588 ac->ac_f_ex = ac->ac_b_ex;
1589
1590 ac->ac_status = AC_STATUS_FOUND;
1591 ac->ac_tail = ret & 0xffff;
1592 ac->ac_buddy = ret >> 16;
1593
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001594 /*
1595 * take the page reference. We want the page to be pinned
1596 * so that we don't get a ext4_mb_init_cache_call for this
1597 * group until we update the bitmap. That would mean we
1598 * double allocate blocks. The reference is dropped
1599 * in ext4_mb_release_context
1600 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001601 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1602 get_page(ac->ac_bitmap_page);
1603 ac->ac_buddy_page = e4b->bd_buddy_page;
1604 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001605 /* on allocation we use ac to track the held semaphore */
1606 ac->alloc_semp = e4b->alloc_semp;
1607 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001608 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001609 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001610 spin_lock(&sbi->s_md_lock);
1611 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1612 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1613 spin_unlock(&sbi->s_md_lock);
1614 }
1615}
1616
1617/*
1618 * regular allocator, for general purposes allocation
1619 */
1620
1621static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1622 struct ext4_buddy *e4b,
1623 int finish_group)
1624{
1625 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1626 struct ext4_free_extent *bex = &ac->ac_b_ex;
1627 struct ext4_free_extent *gex = &ac->ac_g_ex;
1628 struct ext4_free_extent ex;
1629 int max;
1630
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001631 if (ac->ac_status == AC_STATUS_FOUND)
1632 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001633 /*
1634 * We don't want to scan for a whole year
1635 */
1636 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1637 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1638 ac->ac_status = AC_STATUS_BREAK;
1639 return;
1640 }
1641
1642 /*
1643 * Haven't found good chunk so far, let's continue
1644 */
1645 if (bex->fe_len < gex->fe_len)
1646 return;
1647
1648 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1649 && bex->fe_group == e4b->bd_group) {
1650 /* recheck chunk's availability - we don't know
1651 * when it was found (within this lock-unlock
1652 * period or not) */
1653 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1654 if (max >= gex->fe_len) {
1655 ext4_mb_use_best_found(ac, e4b);
1656 return;
1657 }
1658 }
1659}
1660
1661/*
1662 * The routine checks whether found extent is good enough. If it is,
1663 * then the extent gets marked used and flag is set to the context
1664 * to stop scanning. Otherwise, the extent is compared with the
1665 * previous found extent and if new one is better, then it's stored
1666 * in the context. Later, the best found extent will be used, if
1667 * mballoc can't find good enough extent.
1668 *
1669 * FIXME: real allocation policy is to be designed yet!
1670 */
1671static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1672 struct ext4_free_extent *ex,
1673 struct ext4_buddy *e4b)
1674{
1675 struct ext4_free_extent *bex = &ac->ac_b_ex;
1676 struct ext4_free_extent *gex = &ac->ac_g_ex;
1677
1678 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001679 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001680 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1681 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1682
1683 ac->ac_found++;
1684
1685 /*
1686 * The special case - take what you catch first
1687 */
1688 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1689 *bex = *ex;
1690 ext4_mb_use_best_found(ac, e4b);
1691 return;
1692 }
1693
1694 /*
1695 * Let's check whether the chuck is good enough
1696 */
1697 if (ex->fe_len == gex->fe_len) {
1698 *bex = *ex;
1699 ext4_mb_use_best_found(ac, e4b);
1700 return;
1701 }
1702
1703 /*
1704 * If this is first found extent, just store it in the context
1705 */
1706 if (bex->fe_len == 0) {
1707 *bex = *ex;
1708 return;
1709 }
1710
1711 /*
1712 * If new found extent is better, store it in the context
1713 */
1714 if (bex->fe_len < gex->fe_len) {
1715 /* if the request isn't satisfied, any found extent
1716 * larger than previous best one is better */
1717 if (ex->fe_len > bex->fe_len)
1718 *bex = *ex;
1719 } else if (ex->fe_len > gex->fe_len) {
1720 /* if the request is satisfied, then we try to find
1721 * an extent that still satisfy the request, but is
1722 * smaller than previous one */
1723 if (ex->fe_len < bex->fe_len)
1724 *bex = *ex;
1725 }
1726
1727 ext4_mb_check_limits(ac, e4b, 0);
1728}
1729
Eric Sandeen089ceec2009-07-05 22:17:31 -04001730static noinline_for_stack
1731int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001732 struct ext4_buddy *e4b)
1733{
1734 struct ext4_free_extent ex = ac->ac_b_ex;
1735 ext4_group_t group = ex.fe_group;
1736 int max;
1737 int err;
1738
1739 BUG_ON(ex.fe_len <= 0);
1740 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1741 if (err)
1742 return err;
1743
1744 ext4_lock_group(ac->ac_sb, group);
1745 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1746
1747 if (max > 0) {
1748 ac->ac_b_ex = ex;
1749 ext4_mb_use_best_found(ac, e4b);
1750 }
1751
1752 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001753 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001754
1755 return 0;
1756}
1757
Eric Sandeen089ceec2009-07-05 22:17:31 -04001758static noinline_for_stack
1759int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001760 struct ext4_buddy *e4b)
1761{
1762 ext4_group_t group = ac->ac_g_ex.fe_group;
1763 int max;
1764 int err;
1765 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001766 struct ext4_free_extent ex;
1767
1768 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1769 return 0;
1770
1771 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1772 if (err)
1773 return err;
1774
1775 ext4_lock_group(ac->ac_sb, group);
1776 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1777 ac->ac_g_ex.fe_len, &ex);
1778
1779 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1780 ext4_fsblk_t start;
1781
Akinobu Mita5661bd62010-03-03 23:53:39 -05001782 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1783 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001784 /* use do_div to get remainder (would be 64-bit modulo) */
1785 if (do_div(start, sbi->s_stripe) == 0) {
1786 ac->ac_found++;
1787 ac->ac_b_ex = ex;
1788 ext4_mb_use_best_found(ac, e4b);
1789 }
1790 } else if (max >= ac->ac_g_ex.fe_len) {
1791 BUG_ON(ex.fe_len <= 0);
1792 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1793 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1794 ac->ac_found++;
1795 ac->ac_b_ex = ex;
1796 ext4_mb_use_best_found(ac, e4b);
1797 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1798 /* Sometimes, caller may want to merge even small
1799 * number of blocks to an existing extent */
1800 BUG_ON(ex.fe_len <= 0);
1801 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1802 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1803 ac->ac_found++;
1804 ac->ac_b_ex = ex;
1805 ext4_mb_use_best_found(ac, e4b);
1806 }
1807 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001808 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001809
1810 return 0;
1811}
1812
1813/*
1814 * The routine scans buddy structures (not bitmap!) from given order
1815 * to max order and tries to find big enough chunk to satisfy the req
1816 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001817static noinline_for_stack
1818void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001819 struct ext4_buddy *e4b)
1820{
1821 struct super_block *sb = ac->ac_sb;
1822 struct ext4_group_info *grp = e4b->bd_info;
1823 void *buddy;
1824 int i;
1825 int k;
1826 int max;
1827
1828 BUG_ON(ac->ac_2order <= 0);
1829 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1830 if (grp->bb_counters[i] == 0)
1831 continue;
1832
1833 buddy = mb_find_buddy(e4b, i, &max);
1834 BUG_ON(buddy == NULL);
1835
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001836 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001837 BUG_ON(k >= max);
1838
1839 ac->ac_found++;
1840
1841 ac->ac_b_ex.fe_len = 1 << i;
1842 ac->ac_b_ex.fe_start = k << i;
1843 ac->ac_b_ex.fe_group = e4b->bd_group;
1844
1845 ext4_mb_use_best_found(ac, e4b);
1846
1847 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1848
1849 if (EXT4_SB(sb)->s_mb_stats)
1850 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1851
1852 break;
1853 }
1854}
1855
1856/*
1857 * The routine scans the group and measures all found extents.
1858 * In order to optimize scanning, caller must pass number of
1859 * free blocks in the group, so the routine can know upper limit.
1860 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001861static noinline_for_stack
1862void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001863 struct ext4_buddy *e4b)
1864{
1865 struct super_block *sb = ac->ac_sb;
1866 void *bitmap = EXT4_MB_BITMAP(e4b);
1867 struct ext4_free_extent ex;
1868 int i;
1869 int free;
1870
1871 free = e4b->bd_info->bb_free;
1872 BUG_ON(free <= 0);
1873
1874 i = e4b->bd_info->bb_first_free;
1875
1876 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001877 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001878 EXT4_BLOCKS_PER_GROUP(sb), i);
1879 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001880 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001881 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001882 * free blocks even though group info says we
1883 * we have free blocks
1884 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001885 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1886 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001887 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001888 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001889 break;
1890 }
1891
1892 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1893 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001894 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001895 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1896 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001897 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001898 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001899 /*
1900 * The number of free blocks differs. This mostly
1901 * indicate that the bitmap is corrupt. So exit
1902 * without claiming the space.
1903 */
1904 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001905 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001906
1907 ext4_mb_measure_extent(ac, &ex, e4b);
1908
1909 i += ex.fe_len;
1910 free -= ex.fe_len;
1911 }
1912
1913 ext4_mb_check_limits(ac, e4b, 1);
1914}
1915
1916/*
1917 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001918 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001919 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001920static noinline_for_stack
1921void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001922 struct ext4_buddy *e4b)
1923{
1924 struct super_block *sb = ac->ac_sb;
1925 struct ext4_sb_info *sbi = EXT4_SB(sb);
1926 void *bitmap = EXT4_MB_BITMAP(e4b);
1927 struct ext4_free_extent ex;
1928 ext4_fsblk_t first_group_block;
1929 ext4_fsblk_t a;
1930 ext4_grpblk_t i;
1931 int max;
1932
1933 BUG_ON(sbi->s_stripe == 0);
1934
1935 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001936 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1937
Alex Tomasc9de5602008-01-29 00:19:52 -05001938 a = first_group_block + sbi->s_stripe - 1;
1939 do_div(a, sbi->s_stripe);
1940 i = (a * sbi->s_stripe) - first_group_block;
1941
1942 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1943 if (!mb_test_bit(i, bitmap)) {
1944 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1945 if (max >= sbi->s_stripe) {
1946 ac->ac_found++;
1947 ac->ac_b_ex = ex;
1948 ext4_mb_use_best_found(ac, e4b);
1949 break;
1950 }
1951 }
1952 i += sbi->s_stripe;
1953 }
1954}
1955
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001956/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001957static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1958 ext4_group_t group, int cr)
1959{
1960 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001961 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001962 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1963
1964 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001965
1966 /* We only do this if the grp has never been initialized */
1967 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1968 int ret = ext4_mb_init_group(ac->ac_sb, group);
1969 if (ret)
1970 return 0;
1971 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001972
1973 free = grp->bb_free;
1974 fragments = grp->bb_fragments;
1975 if (free == 0)
1976 return 0;
1977 if (fragments == 0)
1978 return 0;
1979
1980 switch (cr) {
1981 case 0:
1982 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001983
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001984 if (grp->bb_largest_free_order < ac->ac_2order)
1985 return 0;
1986
Theodore Ts'oa4912122009-03-12 12:18:34 -04001987 /* Avoid using the first bg of a flexgroup for data files */
1988 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1989 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1990 ((group % flex_size) == 0))
1991 return 0;
1992
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001993 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001994 case 1:
1995 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1996 return 1;
1997 break;
1998 case 2:
1999 if (free >= ac->ac_g_ex.fe_len)
2000 return 1;
2001 break;
2002 case 3:
2003 return 1;
2004 default:
2005 BUG();
2006 }
2007
2008 return 0;
2009}
2010
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002011static noinline_for_stack int
2012ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002013{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002014 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002015 int cr;
2016 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002017 struct ext4_sb_info *sbi;
2018 struct super_block *sb;
2019 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002020
2021 sb = ac->ac_sb;
2022 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002023 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002024 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002025 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002026 ngroups = sbi->s_blockfile_groups;
2027
Alex Tomasc9de5602008-01-29 00:19:52 -05002028 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2029
2030 /* first, try the goal */
2031 err = ext4_mb_find_by_goal(ac, &e4b);
2032 if (err || ac->ac_status == AC_STATUS_FOUND)
2033 goto out;
2034
2035 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2036 goto out;
2037
2038 /*
2039 * ac->ac2_order is set only if the fe_len is a power of 2
2040 * if ac2_order is set we also set criteria to 0 so that we
2041 * try exact allocation using buddy.
2042 */
2043 i = fls(ac->ac_g_ex.fe_len);
2044 ac->ac_2order = 0;
2045 /*
2046 * We search using buddy data only if the order of the request
2047 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002048 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05002049 */
2050 if (i >= sbi->s_mb_order2_reqs) {
2051 /*
2052 * This should tell if fe_len is exactly power of 2
2053 */
2054 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2055 ac->ac_2order = i - 1;
2056 }
2057
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002058 /* if stream allocation is enabled, use global goal */
2059 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002060 /* TBD: may be hot point */
2061 spin_lock(&sbi->s_md_lock);
2062 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2063 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2064 spin_unlock(&sbi->s_md_lock);
2065 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002066
Alex Tomasc9de5602008-01-29 00:19:52 -05002067 /* Let's just scan groups to find more-less suitable blocks */
2068 cr = ac->ac_2order ? 0 : 1;
2069 /*
2070 * cr == 0 try to get exact allocation,
2071 * cr == 3 try to get anything
2072 */
2073repeat:
2074 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2075 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002076 /*
2077 * searching for the right group start
2078 * from the goal value specified
2079 */
2080 group = ac->ac_g_ex.fe_group;
2081
Theodore Ts'o8df96752009-05-01 08:50:38 -04002082 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002083 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002084 group = 0;
2085
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002086 /* This now checks without needing the buddy page */
2087 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002088 continue;
2089
Alex Tomasc9de5602008-01-29 00:19:52 -05002090 err = ext4_mb_load_buddy(sb, group, &e4b);
2091 if (err)
2092 goto out;
2093
2094 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002095
2096 /*
2097 * We need to check again after locking the
2098 * block group
2099 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002100 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002101 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002102 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002103 continue;
2104 }
2105
2106 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002107 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002108 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002109 else if (cr == 1 && sbi->s_stripe &&
2110 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002111 ext4_mb_scan_aligned(ac, &e4b);
2112 else
2113 ext4_mb_complex_scan_group(ac, &e4b);
2114
2115 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002116 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002117
2118 if (ac->ac_status != AC_STATUS_CONTINUE)
2119 break;
2120 }
2121 }
2122
2123 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2124 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2125 /*
2126 * We've been searching too long. Let's try to allocate
2127 * the best chunk we've found so far
2128 */
2129
2130 ext4_mb_try_best_found(ac, &e4b);
2131 if (ac->ac_status != AC_STATUS_FOUND) {
2132 /*
2133 * Someone more lucky has already allocated it.
2134 * The only thing we can do is just take first
2135 * found block(s)
2136 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2137 */
2138 ac->ac_b_ex.fe_group = 0;
2139 ac->ac_b_ex.fe_start = 0;
2140 ac->ac_b_ex.fe_len = 0;
2141 ac->ac_status = AC_STATUS_CONTINUE;
2142 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2143 cr = 3;
2144 atomic_inc(&sbi->s_mb_lost_chunks);
2145 goto repeat;
2146 }
2147 }
2148out:
2149 return err;
2150}
2151
Alex Tomasc9de5602008-01-29 00:19:52 -05002152static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2153{
2154 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002155 ext4_group_t group;
2156
Theodore Ts'o8df96752009-05-01 08:50:38 -04002157 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002158 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002159 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002160 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002161}
2162
2163static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2164{
2165 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002166 ext4_group_t group;
2167
2168 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002169 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002170 return NULL;
2171 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002172 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002173}
2174
2175static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2176{
2177 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002178 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002179 int i;
2180 int err;
2181 struct ext4_buddy e4b;
2182 struct sg {
2183 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002184 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002185 } sg;
2186
2187 group--;
2188 if (group == 0)
2189 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2190 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2191 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2192 "group", "free", "frags", "first",
2193 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2194 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2195
2196 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2197 sizeof(struct ext4_group_info);
2198 err = ext4_mb_load_buddy(sb, group, &e4b);
2199 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002200 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002201 return 0;
2202 }
2203 ext4_lock_group(sb, group);
2204 memcpy(&sg, ext4_get_group_info(sb, group), i);
2205 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002206 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002207
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002208 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002209 sg.info.bb_fragments, sg.info.bb_first_free);
2210 for (i = 0; i <= 13; i++)
2211 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2212 sg.info.bb_counters[i] : 0);
2213 seq_printf(seq, " ]\n");
2214
2215 return 0;
2216}
2217
2218static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2219{
2220}
2221
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002222static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002223 .start = ext4_mb_seq_groups_start,
2224 .next = ext4_mb_seq_groups_next,
2225 .stop = ext4_mb_seq_groups_stop,
2226 .show = ext4_mb_seq_groups_show,
2227};
2228
2229static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2230{
2231 struct super_block *sb = PDE(inode)->data;
2232 int rc;
2233
2234 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2235 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002236 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002237 m->private = sb;
2238 }
2239 return rc;
2240
2241}
2242
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002243static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002244 .owner = THIS_MODULE,
2245 .open = ext4_mb_seq_groups_open,
2246 .read = seq_read,
2247 .llseek = seq_lseek,
2248 .release = seq_release,
2249};
2250
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002251static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2252{
2253 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2254 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2255
2256 BUG_ON(!cachep);
2257 return cachep;
2258}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002259
2260/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002261int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002262 struct ext4_group_desc *desc)
2263{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002264 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002265 int metalen = 0;
2266 struct ext4_sb_info *sbi = EXT4_SB(sb);
2267 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002268 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002269
2270 /*
2271 * First check if this group is the first of a reserved block.
2272 * If it's true, we have to allocate a new table of pointers
2273 * to ext4_group_info structures
2274 */
2275 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2276 metalen = sizeof(*meta_group_info) <<
2277 EXT4_DESC_PER_BLOCK_BITS(sb);
2278 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2279 if (meta_group_info == NULL) {
2280 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2281 "buddy group\n");
2282 goto exit_meta_group_info;
2283 }
2284 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2285 meta_group_info;
2286 }
2287
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002288 meta_group_info =
2289 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2290 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2291
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002292 meta_group_info[i] = kmem_cache_alloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002293 if (meta_group_info[i] == NULL) {
2294 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2295 goto exit_group_info;
2296 }
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002297 memset(meta_group_info[i], 0, kmem_cache_size(cachep));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002298 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2299 &(meta_group_info[i]->bb_state));
2300
2301 /*
2302 * initialize bb_free to be able to skip
2303 * empty groups without initialization
2304 */
2305 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2306 meta_group_info[i]->bb_free =
2307 ext4_free_blocks_after_init(sb, group, desc);
2308 } else {
2309 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002310 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002311 }
2312
2313 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002314 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002315 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002316 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002317
2318#ifdef DOUBLE_CHECK
2319 {
2320 struct buffer_head *bh;
2321 meta_group_info[i]->bb_bitmap =
2322 kmalloc(sb->s_blocksize, GFP_KERNEL);
2323 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2324 bh = ext4_read_block_bitmap(sb, group);
2325 BUG_ON(bh == NULL);
2326 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2327 sb->s_blocksize);
2328 put_bh(bh);
2329 }
2330#endif
2331
2332 return 0;
2333
2334exit_group_info:
2335 /* If a meta_group_info table has been allocated, release it now */
2336 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2337 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2338exit_meta_group_info:
2339 return -ENOMEM;
2340} /* ext4_mb_add_groupinfo */
2341
Alex Tomasc9de5602008-01-29 00:19:52 -05002342static int ext4_mb_init_backend(struct super_block *sb)
2343{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002344 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002345 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002346 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002347 struct ext4_super_block *es = sbi->s_es;
2348 int num_meta_group_infos;
2349 int num_meta_group_infos_max;
2350 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002351 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002352 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002353
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002354 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002355 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002356 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2357
2358 /*
2359 * This is the total number of blocks used by GDT including
2360 * the number of reserved blocks for GDT.
2361 * The s_group_info array is allocated with this value
2362 * to allow a clean online resize without a complex
2363 * manipulation of pointer.
2364 * The drawback is the unused memory when no resize
2365 * occurs but it's very low in terms of pages
2366 * (see comments below)
2367 * Need to handle this properly when META_BG resizing is allowed
2368 */
2369 num_meta_group_infos_max = num_meta_group_infos +
2370 le16_to_cpu(es->s_reserved_gdt_blocks);
2371
2372 /*
2373 * array_size is the size of s_group_info array. We round it
2374 * to the next power of two because this approximation is done
2375 * internally by kmalloc so we can have some more memory
2376 * for free here (e.g. may be used for META_BG resize).
2377 */
2378 array_size = 1;
2379 while (array_size < sizeof(*sbi->s_group_info) *
2380 num_meta_group_infos_max)
2381 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002382 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2383 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2384 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002385 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002386 if (sbi->s_group_info == NULL) {
2387 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2388 return -ENOMEM;
2389 }
2390 sbi->s_buddy_cache = new_inode(sb);
2391 if (sbi->s_buddy_cache == NULL) {
2392 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2393 goto err_freesgi;
2394 }
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002395 sbi->s_buddy_cache->i_ino = get_next_ino();
Alex Tomasc9de5602008-01-29 00:19:52 -05002396 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002397 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002398 desc = ext4_get_group_desc(sb, i, NULL);
2399 if (desc == NULL) {
2400 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002401 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002402 goto err_freebuddy;
2403 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002404 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2405 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002406 }
2407
2408 return 0;
2409
2410err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002411 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002412 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002413 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002414 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002415 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002416 kfree(sbi->s_group_info[i]);
2417 iput(sbi->s_buddy_cache);
2418err_freesgi:
2419 kfree(sbi->s_group_info);
2420 return -ENOMEM;
2421}
2422
Eric Sandeen2892c152011-02-12 08:12:18 -05002423static void ext4_groupinfo_destroy_slabs(void)
2424{
2425 int i;
2426
2427 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2428 if (ext4_groupinfo_caches[i])
2429 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2430 ext4_groupinfo_caches[i] = NULL;
2431 }
2432}
2433
2434static int ext4_groupinfo_create_slab(size_t size)
2435{
2436 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2437 int slab_size;
2438 int blocksize_bits = order_base_2(size);
2439 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2440 struct kmem_cache *cachep;
2441
2442 if (cache_index >= NR_GRPINFO_CACHES)
2443 return -EINVAL;
2444
2445 if (unlikely(cache_index < 0))
2446 cache_index = 0;
2447
2448 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2449 if (ext4_groupinfo_caches[cache_index]) {
2450 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2451 return 0; /* Already created */
2452 }
2453
2454 slab_size = offsetof(struct ext4_group_info,
2455 bb_counters[blocksize_bits + 2]);
2456
2457 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2458 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2459 NULL);
2460
2461 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2462 if (!cachep) {
2463 printk(KERN_EMERG "EXT4: no memory for groupinfo slab cache\n");
2464 return -ENOMEM;
2465 }
2466
2467 ext4_groupinfo_caches[cache_index] = cachep;
2468
2469 return 0;
2470}
2471
Alex Tomasc9de5602008-01-29 00:19:52 -05002472int ext4_mb_init(struct super_block *sb, int needs_recovery)
2473{
2474 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002475 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002476 unsigned offset;
2477 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002478 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002479
Eric Sandeen19278052009-08-25 22:36:25 -04002480 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002481
2482 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2483 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002484 ret = -ENOMEM;
2485 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002486 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002487
Eric Sandeen19278052009-08-25 22:36:25 -04002488 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002489 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2490 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002491 ret = -ENOMEM;
2492 goto out;
2493 }
2494
Eric Sandeen2892c152011-02-12 08:12:18 -05002495 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2496 if (ret < 0)
2497 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002498
2499 /* order 0 is regular bitmap */
2500 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2501 sbi->s_mb_offsets[0] = 0;
2502
2503 i = 1;
2504 offset = 0;
2505 max = sb->s_blocksize << 2;
2506 do {
2507 sbi->s_mb_offsets[i] = offset;
2508 sbi->s_mb_maxs[i] = max;
2509 offset += 1 << (sb->s_blocksize_bits - i);
2510 max = max >> 1;
2511 i++;
2512 } while (i <= sb->s_blocksize_bits + 1);
2513
2514 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002515 ret = ext4_mb_init_backend(sb);
2516 if (ret != 0) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002517 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002518 }
2519
2520 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002521 spin_lock_init(&sbi->s_bal_lock);
2522
2523 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2524 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2525 sbi->s_mb_stats = MB_DEFAULT_STATS;
2526 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2527 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002528 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2529
Eric Sandeen730c2132008-09-13 15:23:29 -04002530 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002531 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002532 ret = -ENOMEM;
2533 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002534 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002535 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002536 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002537 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002538 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002539 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2540 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002541 spin_lock_init(&lg->lg_prealloc_lock);
2542 }
2543
Theodore Ts'o296c3552009-09-30 00:32:42 -04002544 if (sbi->s_proc)
2545 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2546 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002547
Frank Mayhar03901312009-01-07 00:06:22 -05002548 if (sbi->s_journal)
2549 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002550out:
2551 if (ret) {
2552 kfree(sbi->s_mb_offsets);
2553 kfree(sbi->s_mb_maxs);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002554 }
2555 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002556}
2557
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002558/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002559static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2560{
2561 struct ext4_prealloc_space *pa;
2562 struct list_head *cur, *tmp;
2563 int count = 0;
2564
2565 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2566 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2567 list_del(&pa->pa_group_list);
2568 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002569 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002570 }
2571 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002572 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002573
2574}
2575
2576int ext4_mb_release(struct super_block *sb)
2577{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002578 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002579 ext4_group_t i;
2580 int num_meta_group_infos;
2581 struct ext4_group_info *grinfo;
2582 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002583 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002584
Alex Tomasc9de5602008-01-29 00:19:52 -05002585 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002586 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002587 grinfo = ext4_get_group_info(sb, i);
2588#ifdef DOUBLE_CHECK
2589 kfree(grinfo->bb_bitmap);
2590#endif
2591 ext4_lock_group(sb, i);
2592 ext4_mb_cleanup_pa(grinfo);
2593 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002594 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002595 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002596 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002597 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2598 EXT4_DESC_PER_BLOCK_BITS(sb);
2599 for (i = 0; i < num_meta_group_infos; i++)
2600 kfree(sbi->s_group_info[i]);
2601 kfree(sbi->s_group_info);
2602 }
2603 kfree(sbi->s_mb_offsets);
2604 kfree(sbi->s_mb_maxs);
2605 if (sbi->s_buddy_cache)
2606 iput(sbi->s_buddy_cache);
2607 if (sbi->s_mb_stats) {
2608 printk(KERN_INFO
2609 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2610 atomic_read(&sbi->s_bal_allocated),
2611 atomic_read(&sbi->s_bal_reqs),
2612 atomic_read(&sbi->s_bal_success));
2613 printk(KERN_INFO
2614 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2615 "%u 2^N hits, %u breaks, %u lost\n",
2616 atomic_read(&sbi->s_bal_ex_scanned),
2617 atomic_read(&sbi->s_bal_goals),
2618 atomic_read(&sbi->s_bal_2orders),
2619 atomic_read(&sbi->s_bal_breaks),
2620 atomic_read(&sbi->s_mb_lost_chunks));
2621 printk(KERN_INFO
2622 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2623 sbi->s_mb_buddies_generated++,
2624 sbi->s_mb_generation_time);
2625 printk(KERN_INFO
2626 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2627 atomic_read(&sbi->s_mb_preallocated),
2628 atomic_read(&sbi->s_mb_discarded));
2629 }
2630
Eric Sandeen730c2132008-09-13 15:23:29 -04002631 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002632 if (sbi->s_proc)
2633 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002634
2635 return 0;
2636}
2637
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002638static inline int ext4_issue_discard(struct super_block *sb,
Jiaying Zhang5c521832010-07-27 11:56:05 -04002639 ext4_group_t block_group, ext4_grpblk_t block, int count)
2640{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002641 ext4_fsblk_t discard_block;
2642
2643 discard_block = block + ext4_group_first_block_no(sb, block_group);
2644 trace_ext4_discard_blocks(sb,
2645 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002646 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002647}
2648
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002649/*
2650 * This function is called by the jbd2 layer once the commit has finished,
2651 * so we know we can free the blocks that were released with that commit.
2652 */
2653static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002654{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002655 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002656 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002657 struct ext4_group_info *db;
Lukas Czerner93259632011-01-10 12:09:59 -05002658 int err, ret, count = 0, count2 = 0;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002659 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002660 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002661
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002662 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2663 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002664
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002665 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002666 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002667
Lukas Czerner93259632011-01-10 12:09:59 -05002668 if (test_opt(sb, DISCARD)) {
2669 ret = ext4_issue_discard(sb, entry->group,
Jiaying Zhang5c521832010-07-27 11:56:05 -04002670 entry->start_blk, entry->count);
Lukas Czerner93259632011-01-10 12:09:59 -05002671 if (unlikely(ret == -EOPNOTSUPP)) {
2672 ext4_warning(sb, "discard not supported, "
2673 "disabling");
2674 clear_opt(sb, DISCARD);
2675 }
2676 }
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002677
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002678 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002679 /* we expect to find existing buddy because it's pinned */
2680 BUG_ON(err != 0);
2681
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002682 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002683 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002684 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002685 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002686 ext4_lock_group(sb, entry->group);
2687 /* Take it out of per group rb tree */
2688 rb_erase(&entry->node, &(db->bb_free_root));
2689 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2690
2691 if (!db->bb_free_root.rb_node) {
2692 /* No more items in the per group rb tree
2693 * balance refcounts from ext4_mb_free_metadata()
2694 */
2695 page_cache_release(e4b.bd_buddy_page);
2696 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002697 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002698 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002699 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002700 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002701 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002702
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002703 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002704}
2705
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002706#ifdef CONFIG_EXT4_DEBUG
2707u8 mb_enable_debug __read_mostly;
2708
2709static struct dentry *debugfs_dir;
2710static struct dentry *debugfs_debug;
2711
2712static void __init ext4_create_debugfs_entry(void)
2713{
2714 debugfs_dir = debugfs_create_dir("ext4", NULL);
2715 if (debugfs_dir)
2716 debugfs_debug = debugfs_create_u8("mballoc-debug",
2717 S_IRUGO | S_IWUSR,
2718 debugfs_dir,
2719 &mb_enable_debug);
2720}
2721
2722static void ext4_remove_debugfs_entry(void)
2723{
2724 debugfs_remove(debugfs_debug);
2725 debugfs_remove(debugfs_dir);
2726}
2727
2728#else
2729
2730static void __init ext4_create_debugfs_entry(void)
2731{
2732}
2733
2734static void ext4_remove_debugfs_entry(void)
2735{
2736}
2737
2738#endif
2739
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002740int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002741{
Theodore Ts'o16828082010-10-27 21:30:09 -04002742 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2743 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002744 if (ext4_pspace_cachep == NULL)
2745 return -ENOMEM;
2746
Theodore Ts'o16828082010-10-27 21:30:09 -04002747 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2748 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002749 if (ext4_ac_cachep == NULL) {
2750 kmem_cache_destroy(ext4_pspace_cachep);
2751 return -ENOMEM;
2752 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002753
Theodore Ts'o16828082010-10-27 21:30:09 -04002754 ext4_free_ext_cachep = KMEM_CACHE(ext4_free_data,
2755 SLAB_RECLAIM_ACCOUNT);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002756 if (ext4_free_ext_cachep == NULL) {
2757 kmem_cache_destroy(ext4_pspace_cachep);
2758 kmem_cache_destroy(ext4_ac_cachep);
2759 return -ENOMEM;
2760 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002761 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002762 return 0;
2763}
2764
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002765void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002766{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002767 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002768 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2769 * before destroying the slab cache.
2770 */
2771 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002772 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002773 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002774 kmem_cache_destroy(ext4_free_ext_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002775 ext4_groupinfo_destroy_slabs();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002776 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002777}
2778
2779
2780/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002781 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002782 * Returns 0 if success or error code
2783 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002784static noinline_for_stack int
2785ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002786 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002787{
2788 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002789 struct ext4_group_desc *gdp;
2790 struct buffer_head *gdp_bh;
2791 struct ext4_sb_info *sbi;
2792 struct super_block *sb;
2793 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002794 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002795
2796 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2797 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2798
2799 sb = ac->ac_sb;
2800 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002801
2802 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002803 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002804 if (!bitmap_bh)
2805 goto out_err;
2806
2807 err = ext4_journal_get_write_access(handle, bitmap_bh);
2808 if (err)
2809 goto out_err;
2810
2811 err = -EIO;
2812 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2813 if (!gdp)
2814 goto out_err;
2815
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002816 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002817 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002818
Alex Tomasc9de5602008-01-29 00:19:52 -05002819 err = ext4_journal_get_write_access(handle, gdp_bh);
2820 if (err)
2821 goto out_err;
2822
Akinobu Mitabda00de2010-03-03 23:53:25 -05002823 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002824
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002825 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002826 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002827 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002828 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002829 /* File system mounted not to panic on error
2830 * Fix the bitmap and repeat the block allocation
2831 * We leak some of the blocks here.
2832 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002833 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2834 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2835 ac->ac_b_ex.fe_len);
2836 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002837 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002838 if (!err)
2839 err = -EAGAIN;
2840 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002841 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002842
2843 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002844#ifdef AGGRESSIVE_CHECK
2845 {
2846 int i;
2847 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2848 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2849 bitmap_bh->b_data));
2850 }
2851 }
2852#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002853 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 -05002854 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2855 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002856 ext4_free_blks_set(sb, gdp,
2857 ext4_free_blocks_after_init(sb,
2858 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002859 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002860 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2861 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002862 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002863
2864 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002865 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002866 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002867 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002868 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002869 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2870 /* release all the reserved blocks if non delalloc */
2871 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002872
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002873 if (sbi->s_log_groups_per_flex) {
2874 ext4_group_t flex_group = ext4_flex_group(sbi,
2875 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002876 atomic_sub(ac->ac_b_ex.fe_len,
2877 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002878 }
2879
Frank Mayhar03901312009-01-07 00:06:22 -05002880 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002881 if (err)
2882 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002883 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002884
2885out_err:
Theodore Ts'oa0375152010-06-11 23:14:04 -04002886 ext4_mark_super_dirty(sb);
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002887 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002888 return err;
2889}
2890
2891/*
2892 * here we normalize request for locality group
2893 * Group request are normalized to s_strip size if we set the same via mount
2894 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002895 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002896 *
2897 * XXX: should we try to preallocate more than the group has now?
2898 */
2899static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2900{
2901 struct super_block *sb = ac->ac_sb;
2902 struct ext4_locality_group *lg = ac->ac_lg;
2903
2904 BUG_ON(lg == NULL);
2905 if (EXT4_SB(sb)->s_stripe)
2906 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2907 else
2908 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002909 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002910 current->pid, ac->ac_g_ex.fe_len);
2911}
2912
2913/*
2914 * Normalization means making request better in terms of
2915 * size and alignment
2916 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002917static noinline_for_stack void
2918ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002919 struct ext4_allocation_request *ar)
2920{
2921 int bsbits, max;
2922 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002923 loff_t size, orig_size, start_off;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002924 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002925 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002926 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002927
2928 /* do normalize only data requests, metadata requests
2929 do not need preallocation */
2930 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2931 return;
2932
2933 /* sometime caller may want exact blocks */
2934 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2935 return;
2936
2937 /* caller may indicate that preallocation isn't
2938 * required (it's a tail, for example) */
2939 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2940 return;
2941
2942 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2943 ext4_mb_normalize_group_request(ac);
2944 return ;
2945 }
2946
2947 bsbits = ac->ac_sb->s_blocksize_bits;
2948
2949 /* first, let's learn actual file size
2950 * given current request is allocated */
2951 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2952 size = size << bsbits;
2953 if (size < i_size_read(ac->ac_inode))
2954 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002955 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002956
Valerie Clement19304792008-05-13 19:31:14 -04002957 /* max size of free chunks */
2958 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002959
Valerie Clement19304792008-05-13 19:31:14 -04002960#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2961 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002962
2963 /* first, try to predict filesize */
2964 /* XXX: should this table be tunable? */
2965 start_off = 0;
2966 if (size <= 16 * 1024) {
2967 size = 16 * 1024;
2968 } else if (size <= 32 * 1024) {
2969 size = 32 * 1024;
2970 } else if (size <= 64 * 1024) {
2971 size = 64 * 1024;
2972 } else if (size <= 128 * 1024) {
2973 size = 128 * 1024;
2974 } else if (size <= 256 * 1024) {
2975 size = 256 * 1024;
2976 } else if (size <= 512 * 1024) {
2977 size = 512 * 1024;
2978 } else if (size <= 1024 * 1024) {
2979 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002980 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002981 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002982 (21 - bsbits)) << 21;
2983 size = 2 * 1024 * 1024;
2984 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002985 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2986 (22 - bsbits)) << 22;
2987 size = 4 * 1024 * 1024;
2988 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002989 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002990 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2991 (23 - bsbits)) << 23;
2992 size = 8 * 1024 * 1024;
2993 } else {
2994 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2995 size = ac->ac_o_ex.fe_len << bsbits;
2996 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002997 size = size >> bsbits;
2998 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002999
3000 /* don't cover already allocated blocks in selected range */
3001 if (ar->pleft && start <= ar->lleft) {
3002 size -= ar->lleft + 1 - start;
3003 start = ar->lleft + 1;
3004 }
3005 if (ar->pright && start + size - 1 >= ar->lright)
3006 size -= start + size - ar->lright;
3007
3008 end = start + size;
3009
3010 /* check we don't cross already preallocated blocks */
3011 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003012 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003013 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003014
Alex Tomasc9de5602008-01-29 00:19:52 -05003015 if (pa->pa_deleted)
3016 continue;
3017 spin_lock(&pa->pa_lock);
3018 if (pa->pa_deleted) {
3019 spin_unlock(&pa->pa_lock);
3020 continue;
3021 }
3022
3023 pa_end = pa->pa_lstart + pa->pa_len;
3024
3025 /* PA must not overlap original request */
3026 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3027 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3028
Eric Sandeen38877f42009-08-17 23:55:24 -04003029 /* skip PAs this normalized request doesn't overlap with */
3030 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003031 spin_unlock(&pa->pa_lock);
3032 continue;
3033 }
3034 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3035
Eric Sandeen38877f42009-08-17 23:55:24 -04003036 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003037 if (pa_end <= ac->ac_o_ex.fe_logical) {
3038 BUG_ON(pa_end < start);
3039 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003040 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003041 BUG_ON(pa->pa_lstart > end);
3042 end = pa->pa_lstart;
3043 }
3044 spin_unlock(&pa->pa_lock);
3045 }
3046 rcu_read_unlock();
3047 size = end - start;
3048
3049 /* XXX: extra loop to check we really don't overlap preallocations */
3050 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003051 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003052 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003053 spin_lock(&pa->pa_lock);
3054 if (pa->pa_deleted == 0) {
3055 pa_end = pa->pa_lstart + pa->pa_len;
3056 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3057 }
3058 spin_unlock(&pa->pa_lock);
3059 }
3060 rcu_read_unlock();
3061
3062 if (start + size <= ac->ac_o_ex.fe_logical &&
3063 start > ac->ac_o_ex.fe_logical) {
3064 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3065 (unsigned long) start, (unsigned long) size,
3066 (unsigned long) ac->ac_o_ex.fe_logical);
3067 }
3068 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3069 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04003070 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003071
3072 /* now prepare goal request */
3073
3074 /* XXX: is it better to align blocks WRT to logical
3075 * placement or satisfy big request as is */
3076 ac->ac_g_ex.fe_logical = start;
3077 ac->ac_g_ex.fe_len = size;
3078
3079 /* define goal start in order to merge */
3080 if (ar->pright && (ar->lright == (start + size))) {
3081 /* merge to the right */
3082 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3083 &ac->ac_f_ex.fe_group,
3084 &ac->ac_f_ex.fe_start);
3085 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3086 }
3087 if (ar->pleft && (ar->lleft + 1 == start)) {
3088 /* merge to the left */
3089 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3090 &ac->ac_f_ex.fe_group,
3091 &ac->ac_f_ex.fe_start);
3092 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3093 }
3094
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003095 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003096 (unsigned) orig_size, (unsigned) start);
3097}
3098
3099static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3100{
3101 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3102
3103 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3104 atomic_inc(&sbi->s_bal_reqs);
3105 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003106 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003107 atomic_inc(&sbi->s_bal_success);
3108 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3109 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3110 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3111 atomic_inc(&sbi->s_bal_goals);
3112 if (ac->ac_found > sbi->s_mb_max_to_scan)
3113 atomic_inc(&sbi->s_bal_breaks);
3114 }
3115
Theodore Ts'o296c3552009-09-30 00:32:42 -04003116 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3117 trace_ext4_mballoc_alloc(ac);
3118 else
3119 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003120}
3121
3122/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003123 * Called on failure; free up any blocks from the inode PA for this
3124 * context. We don't need this for MB_GROUP_PA because we only change
3125 * pa_free in ext4_mb_release_context(), but on failure, we've already
3126 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3127 */
3128static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3129{
3130 struct ext4_prealloc_space *pa = ac->ac_pa;
3131 int len;
3132
3133 if (pa && pa->pa_type == MB_INODE_PA) {
3134 len = ac->ac_b_ex.fe_len;
3135 pa->pa_free += len;
3136 }
3137
3138}
3139
3140/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003141 * use blocks preallocated to inode
3142 */
3143static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3144 struct ext4_prealloc_space *pa)
3145{
3146 ext4_fsblk_t start;
3147 ext4_fsblk_t end;
3148 int len;
3149
3150 /* found preallocated blocks, use them */
3151 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3152 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3153 len = end - start;
3154 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3155 &ac->ac_b_ex.fe_start);
3156 ac->ac_b_ex.fe_len = len;
3157 ac->ac_status = AC_STATUS_FOUND;
3158 ac->ac_pa = pa;
3159
3160 BUG_ON(start < pa->pa_pstart);
3161 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3162 BUG_ON(pa->pa_free < len);
3163 pa->pa_free -= len;
3164
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003165 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003166}
3167
3168/*
3169 * use blocks preallocated to locality group
3170 */
3171static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3172 struct ext4_prealloc_space *pa)
3173{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003174 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003175
Alex Tomasc9de5602008-01-29 00:19:52 -05003176 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3177 &ac->ac_b_ex.fe_group,
3178 &ac->ac_b_ex.fe_start);
3179 ac->ac_b_ex.fe_len = len;
3180 ac->ac_status = AC_STATUS_FOUND;
3181 ac->ac_pa = pa;
3182
3183 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003184 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003185 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003186 * in on-disk bitmap -- see ext4_mb_release_context()
3187 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003188 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003189 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003190}
3191
3192/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003193 * Return the prealloc space that have minimal distance
3194 * from the goal block. @cpa is the prealloc
3195 * space that is having currently known minimal distance
3196 * from the goal block.
3197 */
3198static struct ext4_prealloc_space *
3199ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3200 struct ext4_prealloc_space *pa,
3201 struct ext4_prealloc_space *cpa)
3202{
3203 ext4_fsblk_t cur_distance, new_distance;
3204
3205 if (cpa == NULL) {
3206 atomic_inc(&pa->pa_count);
3207 return pa;
3208 }
3209 cur_distance = abs(goal_block - cpa->pa_pstart);
3210 new_distance = abs(goal_block - pa->pa_pstart);
3211
3212 if (cur_distance < new_distance)
3213 return cpa;
3214
3215 /* drop the previous reference */
3216 atomic_dec(&cpa->pa_count);
3217 atomic_inc(&pa->pa_count);
3218 return pa;
3219}
3220
3221/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003222 * search goal blocks in preallocated space
3223 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003224static noinline_for_stack int
3225ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003226{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003227 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003228 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3229 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003230 struct ext4_prealloc_space *pa, *cpa = NULL;
3231 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003232
3233 /* only data can be preallocated */
3234 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3235 return 0;
3236
3237 /* first, try per-file preallocation */
3238 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003239 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003240
3241 /* all fields in this condition don't change,
3242 * so we can skip locking for them */
3243 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3244 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3245 continue;
3246
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003247 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003248 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003249 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3250 continue;
3251
Alex Tomasc9de5602008-01-29 00:19:52 -05003252 /* found preallocated blocks, use them */
3253 spin_lock(&pa->pa_lock);
3254 if (pa->pa_deleted == 0 && pa->pa_free) {
3255 atomic_inc(&pa->pa_count);
3256 ext4_mb_use_inode_pa(ac, pa);
3257 spin_unlock(&pa->pa_lock);
3258 ac->ac_criteria = 10;
3259 rcu_read_unlock();
3260 return 1;
3261 }
3262 spin_unlock(&pa->pa_lock);
3263 }
3264 rcu_read_unlock();
3265
3266 /* can we use group allocation? */
3267 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3268 return 0;
3269
3270 /* inode may have no locality group for some reason */
3271 lg = ac->ac_lg;
3272 if (lg == NULL)
3273 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003274 order = fls(ac->ac_o_ex.fe_len) - 1;
3275 if (order > PREALLOC_TB_SIZE - 1)
3276 /* The max size of hash table is PREALLOC_TB_SIZE */
3277 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003278
Akinobu Mitabda00de2010-03-03 23:53:25 -05003279 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003280 /*
3281 * search for the prealloc space that is having
3282 * minimal distance from the goal block.
3283 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003284 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3285 rcu_read_lock();
3286 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3287 pa_inode_list) {
3288 spin_lock(&pa->pa_lock);
3289 if (pa->pa_deleted == 0 &&
3290 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003291
3292 cpa = ext4_mb_check_group_pa(goal_block,
3293 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003294 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003295 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003296 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003297 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003298 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003299 if (cpa) {
3300 ext4_mb_use_group_pa(ac, cpa);
3301 ac->ac_criteria = 20;
3302 return 1;
3303 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003304 return 0;
3305}
3306
3307/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003308 * the function goes through all block freed in the group
3309 * but not yet committed and marks them used in in-core bitmap.
3310 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003311 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003312 */
3313static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3314 ext4_group_t group)
3315{
3316 struct rb_node *n;
3317 struct ext4_group_info *grp;
3318 struct ext4_free_data *entry;
3319
3320 grp = ext4_get_group_info(sb, group);
3321 n = rb_first(&(grp->bb_free_root));
3322
3323 while (n) {
3324 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003325 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003326 n = rb_next(n);
3327 }
3328 return;
3329}
3330
3331/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003332 * the function goes through all preallocation in this group and marks them
3333 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003334 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003335 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003336static noinline_for_stack
3337void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003338 ext4_group_t group)
3339{
3340 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3341 struct ext4_prealloc_space *pa;
3342 struct list_head *cur;
3343 ext4_group_t groupnr;
3344 ext4_grpblk_t start;
3345 int preallocated = 0;
3346 int count = 0;
3347 int len;
3348
3349 /* all form of preallocation discards first load group,
3350 * so the only competing code is preallocation use.
3351 * we don't need any locking here
3352 * notice we do NOT ignore preallocations with pa_deleted
3353 * otherwise we could leave used blocks available for
3354 * allocation in buddy when concurrent ext4_mb_put_pa()
3355 * is dropping preallocation
3356 */
3357 list_for_each(cur, &grp->bb_prealloc_list) {
3358 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3359 spin_lock(&pa->pa_lock);
3360 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3361 &groupnr, &start);
3362 len = pa->pa_len;
3363 spin_unlock(&pa->pa_lock);
3364 if (unlikely(len == 0))
3365 continue;
3366 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003367 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003368 preallocated += len;
3369 count++;
3370 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003371 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003372}
3373
3374static void ext4_mb_pa_callback(struct rcu_head *head)
3375{
3376 struct ext4_prealloc_space *pa;
3377 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3378 kmem_cache_free(ext4_pspace_cachep, pa);
3379}
3380
3381/*
3382 * drops a reference to preallocated space descriptor
3383 * if this was the last reference and the space is consumed
3384 */
3385static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3386 struct super_block *sb, struct ext4_prealloc_space *pa)
3387{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003388 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003389 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003390
3391 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3392 return;
3393
3394 /* in this short window concurrent discard can set pa_deleted */
3395 spin_lock(&pa->pa_lock);
3396 if (pa->pa_deleted == 1) {
3397 spin_unlock(&pa->pa_lock);
3398 return;
3399 }
3400
3401 pa->pa_deleted = 1;
3402 spin_unlock(&pa->pa_lock);
3403
Eric Sandeend33a1972009-03-16 23:25:40 -04003404 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003405 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003406 * If doing group-based preallocation, pa_pstart may be in the
3407 * next group when pa is used up
3408 */
3409 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003410 grp_blk--;
3411
3412 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003413
3414 /*
3415 * possible race:
3416 *
3417 * P1 (buddy init) P2 (regular allocation)
3418 * find block B in PA
3419 * copy on-disk bitmap to buddy
3420 * mark B in on-disk bitmap
3421 * drop PA from group
3422 * mark all PAs in buddy
3423 *
3424 * thus, P1 initializes buddy with B available. to prevent this
3425 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3426 * against that pair
3427 */
3428 ext4_lock_group(sb, grp);
3429 list_del(&pa->pa_group_list);
3430 ext4_unlock_group(sb, grp);
3431
3432 spin_lock(pa->pa_obj_lock);
3433 list_del_rcu(&pa->pa_inode_list);
3434 spin_unlock(pa->pa_obj_lock);
3435
3436 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3437}
3438
3439/*
3440 * creates new preallocated space for given inode
3441 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003442static noinline_for_stack int
3443ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003444{
3445 struct super_block *sb = ac->ac_sb;
3446 struct ext4_prealloc_space *pa;
3447 struct ext4_group_info *grp;
3448 struct ext4_inode_info *ei;
3449
3450 /* preallocate only when found space is larger then requested */
3451 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3452 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3453 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3454
3455 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3456 if (pa == NULL)
3457 return -ENOMEM;
3458
3459 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3460 int winl;
3461 int wins;
3462 int win;
3463 int offs;
3464
3465 /* we can't allocate as much as normalizer wants.
3466 * so, found space must get proper lstart
3467 * to cover original request */
3468 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3469 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3470
3471 /* we're limited by original request in that
3472 * logical block must be covered any way
3473 * winl is window we can move our chunk within */
3474 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3475
3476 /* also, we should cover whole original request */
3477 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3478
3479 /* the smallest one defines real window */
3480 win = min(winl, wins);
3481
3482 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3483 if (offs && offs < win)
3484 win = offs;
3485
3486 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3487 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3488 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3489 }
3490
3491 /* preallocation can change ac_b_ex, thus we store actually
3492 * allocated blocks for history */
3493 ac->ac_f_ex = ac->ac_b_ex;
3494
3495 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3496 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3497 pa->pa_len = ac->ac_b_ex.fe_len;
3498 pa->pa_free = pa->pa_len;
3499 atomic_set(&pa->pa_count, 1);
3500 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003501 INIT_LIST_HEAD(&pa->pa_inode_list);
3502 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003503 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003504 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003505
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003506 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003507 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003508 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003509
3510 ext4_mb_use_inode_pa(ac, pa);
3511 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3512
3513 ei = EXT4_I(ac->ac_inode);
3514 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3515
3516 pa->pa_obj_lock = &ei->i_prealloc_lock;
3517 pa->pa_inode = ac->ac_inode;
3518
3519 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3520 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3521 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3522
3523 spin_lock(pa->pa_obj_lock);
3524 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3525 spin_unlock(pa->pa_obj_lock);
3526
3527 return 0;
3528}
3529
3530/*
3531 * creates new preallocated space for locality group inodes belongs to
3532 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003533static noinline_for_stack int
3534ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003535{
3536 struct super_block *sb = ac->ac_sb;
3537 struct ext4_locality_group *lg;
3538 struct ext4_prealloc_space *pa;
3539 struct ext4_group_info *grp;
3540
3541 /* preallocate only when found space is larger then requested */
3542 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3543 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3544 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3545
3546 BUG_ON(ext4_pspace_cachep == NULL);
3547 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3548 if (pa == NULL)
3549 return -ENOMEM;
3550
3551 /* preallocation can change ac_b_ex, thus we store actually
3552 * allocated blocks for history */
3553 ac->ac_f_ex = ac->ac_b_ex;
3554
3555 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3556 pa->pa_lstart = pa->pa_pstart;
3557 pa->pa_len = ac->ac_b_ex.fe_len;
3558 pa->pa_free = pa->pa_len;
3559 atomic_set(&pa->pa_count, 1);
3560 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003561 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003562 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003563 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003564 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003565
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003566 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003567 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3568 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003569
3570 ext4_mb_use_group_pa(ac, pa);
3571 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3572
3573 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3574 lg = ac->ac_lg;
3575 BUG_ON(lg == NULL);
3576
3577 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3578 pa->pa_inode = NULL;
3579
3580 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3581 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3582 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3583
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003584 /*
3585 * We will later add the new pa to the right bucket
3586 * after updating the pa_free in ext4_mb_release_context
3587 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003588 return 0;
3589}
3590
3591static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3592{
3593 int err;
3594
3595 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3596 err = ext4_mb_new_group_pa(ac);
3597 else
3598 err = ext4_mb_new_inode_pa(ac);
3599 return err;
3600}
3601
3602/*
3603 * finds all unused blocks in on-disk bitmap, frees them in
3604 * in-core bitmap and buddy.
3605 * @pa must be unlinked from inode and group lists, so that
3606 * nobody else can find/use it.
3607 * the caller MUST hold group/inode locks.
3608 * TODO: optimize the case when there are no in-core structures yet
3609 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003610static noinline_for_stack int
3611ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003612 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003613{
Alex Tomasc9de5602008-01-29 00:19:52 -05003614 struct super_block *sb = e4b->bd_sb;
3615 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003616 unsigned int end;
3617 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003618 ext4_group_t group;
3619 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003620 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003621 int err = 0;
3622 int free = 0;
3623
3624 BUG_ON(pa->pa_deleted == 0);
3625 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003626 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003627 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3628 end = bit + pa->pa_len;
3629
Alex Tomasc9de5602008-01-29 00:19:52 -05003630 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003631 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003632 if (bit >= end)
3633 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003634 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003635 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003636 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3637 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003638 free += next - bit;
3639
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003640 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
3641 trace_ext4_mb_release_inode_pa(sb, pa->pa_inode, pa,
3642 grp_blk_start + bit, next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003643 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3644 bit = next + 1;
3645 }
3646 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003647 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003648 pa, (unsigned long) pa->pa_lstart,
3649 (unsigned long) pa->pa_pstart,
3650 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003651 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003652 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003653 /*
3654 * pa is already deleted so we use the value obtained
3655 * from the bitmap and continue.
3656 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003657 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003658 atomic_add(free, &sbi->s_mb_discarded);
3659
3660 return err;
3661}
3662
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003663static noinline_for_stack int
3664ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003665 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003666{
Alex Tomasc9de5602008-01-29 00:19:52 -05003667 struct super_block *sb = e4b->bd_sb;
3668 ext4_group_t group;
3669 ext4_grpblk_t bit;
3670
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003671 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003672 BUG_ON(pa->pa_deleted == 0);
3673 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3674 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3675 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3676 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003677 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003678
3679 return 0;
3680}
3681
3682/*
3683 * releases all preallocations in given group
3684 *
3685 * first, we need to decide discard policy:
3686 * - when do we discard
3687 * 1) ENOSPC
3688 * - how many do we discard
3689 * 1) how many requested
3690 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003691static noinline_for_stack int
3692ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003693 ext4_group_t group, int needed)
3694{
3695 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3696 struct buffer_head *bitmap_bh = NULL;
3697 struct ext4_prealloc_space *pa, *tmp;
3698 struct list_head list;
3699 struct ext4_buddy e4b;
3700 int err;
3701 int busy = 0;
3702 int free = 0;
3703
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003704 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003705
3706 if (list_empty(&grp->bb_prealloc_list))
3707 return 0;
3708
Theodore Ts'o574ca172008-07-11 19:27:31 -04003709 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003710 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003711 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003712 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003713 }
3714
3715 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003716 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003717 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003718 put_bh(bitmap_bh);
3719 return 0;
3720 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003721
3722 if (needed == 0)
3723 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3724
Alex Tomasc9de5602008-01-29 00:19:52 -05003725 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003726repeat:
3727 ext4_lock_group(sb, group);
3728 list_for_each_entry_safe(pa, tmp,
3729 &grp->bb_prealloc_list, pa_group_list) {
3730 spin_lock(&pa->pa_lock);
3731 if (atomic_read(&pa->pa_count)) {
3732 spin_unlock(&pa->pa_lock);
3733 busy = 1;
3734 continue;
3735 }
3736 if (pa->pa_deleted) {
3737 spin_unlock(&pa->pa_lock);
3738 continue;
3739 }
3740
3741 /* seems this one can be freed ... */
3742 pa->pa_deleted = 1;
3743
3744 /* we can trust pa_free ... */
3745 free += pa->pa_free;
3746
3747 spin_unlock(&pa->pa_lock);
3748
3749 list_del(&pa->pa_group_list);
3750 list_add(&pa->u.pa_tmp_list, &list);
3751 }
3752
3753 /* if we still need more blocks and some PAs were used, try again */
3754 if (free < needed && busy) {
3755 busy = 0;
3756 ext4_unlock_group(sb, group);
3757 /*
3758 * Yield the CPU here so that we don't get soft lockup
3759 * in non preempt case.
3760 */
3761 yield();
3762 goto repeat;
3763 }
3764
3765 /* found anything to free? */
3766 if (list_empty(&list)) {
3767 BUG_ON(free != 0);
3768 goto out;
3769 }
3770
3771 /* now free all selected PAs */
3772 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3773
3774 /* remove from object (inode or locality group) */
3775 spin_lock(pa->pa_obj_lock);
3776 list_del_rcu(&pa->pa_inode_list);
3777 spin_unlock(pa->pa_obj_lock);
3778
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003779 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003780 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003781 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003782 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003783
3784 list_del(&pa->u.pa_tmp_list);
3785 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3786 }
3787
3788out:
3789 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003790 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003791 put_bh(bitmap_bh);
3792 return free;
3793}
3794
3795/*
3796 * releases all non-used preallocated blocks for given inode
3797 *
3798 * It's important to discard preallocations under i_data_sem
3799 * We don't want another block to be served from the prealloc
3800 * space when we are discarding the inode prealloc space.
3801 *
3802 * FIXME!! Make sure it is valid at all the call sites
3803 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003804void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003805{
3806 struct ext4_inode_info *ei = EXT4_I(inode);
3807 struct super_block *sb = inode->i_sb;
3808 struct buffer_head *bitmap_bh = NULL;
3809 struct ext4_prealloc_space *pa, *tmp;
3810 ext4_group_t group = 0;
3811 struct list_head list;
3812 struct ext4_buddy e4b;
3813 int err;
3814
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003815 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003816 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3817 return;
3818 }
3819
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003820 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003821 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003822
3823 INIT_LIST_HEAD(&list);
3824
3825repeat:
3826 /* first, collect all pa's in the inode */
3827 spin_lock(&ei->i_prealloc_lock);
3828 while (!list_empty(&ei->i_prealloc_list)) {
3829 pa = list_entry(ei->i_prealloc_list.next,
3830 struct ext4_prealloc_space, pa_inode_list);
3831 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3832 spin_lock(&pa->pa_lock);
3833 if (atomic_read(&pa->pa_count)) {
3834 /* this shouldn't happen often - nobody should
3835 * use preallocation while we're discarding it */
3836 spin_unlock(&pa->pa_lock);
3837 spin_unlock(&ei->i_prealloc_lock);
3838 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3839 WARN_ON(1);
3840 schedule_timeout_uninterruptible(HZ);
3841 goto repeat;
3842
3843 }
3844 if (pa->pa_deleted == 0) {
3845 pa->pa_deleted = 1;
3846 spin_unlock(&pa->pa_lock);
3847 list_del_rcu(&pa->pa_inode_list);
3848 list_add(&pa->u.pa_tmp_list, &list);
3849 continue;
3850 }
3851
3852 /* someone is deleting pa right now */
3853 spin_unlock(&pa->pa_lock);
3854 spin_unlock(&ei->i_prealloc_lock);
3855
3856 /* we have to wait here because pa_deleted
3857 * doesn't mean pa is already unlinked from
3858 * the list. as we might be called from
3859 * ->clear_inode() the inode will get freed
3860 * and concurrent thread which is unlinking
3861 * pa from inode's list may access already
3862 * freed memory, bad-bad-bad */
3863
3864 /* XXX: if this happens too often, we can
3865 * add a flag to force wait only in case
3866 * of ->clear_inode(), but not in case of
3867 * regular truncate */
3868 schedule_timeout_uninterruptible(HZ);
3869 goto repeat;
3870 }
3871 spin_unlock(&ei->i_prealloc_lock);
3872
3873 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003874 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003875 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3876
3877 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003878 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003879 ext4_error(sb, "Error loading buddy information for %u",
3880 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003881 continue;
3882 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003883
Theodore Ts'o574ca172008-07-11 19:27:31 -04003884 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003885 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003886 ext4_error(sb, "Error reading block bitmap for %u",
3887 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003888 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003889 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003890 }
3891
3892 ext4_lock_group(sb, group);
3893 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003894 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003895 ext4_unlock_group(sb, group);
3896
Jing Zhange39e07f2010-05-14 00:00:00 -04003897 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003898 put_bh(bitmap_bh);
3899
3900 list_del(&pa->u.pa_tmp_list);
3901 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3902 }
3903}
3904
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003905#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003906static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3907{
3908 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003909 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003910
Eric Sandeene3570632010-07-27 11:56:08 -04003911 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
3912 return;
3913
Alex Tomasc9de5602008-01-29 00:19:52 -05003914 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3915 " Allocation context details:\n");
3916 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3917 ac->ac_status, ac->ac_flags);
3918 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3919 "best %lu/%lu/%lu@%lu cr %d\n",
3920 (unsigned long)ac->ac_o_ex.fe_group,
3921 (unsigned long)ac->ac_o_ex.fe_start,
3922 (unsigned long)ac->ac_o_ex.fe_len,
3923 (unsigned long)ac->ac_o_ex.fe_logical,
3924 (unsigned long)ac->ac_g_ex.fe_group,
3925 (unsigned long)ac->ac_g_ex.fe_start,
3926 (unsigned long)ac->ac_g_ex.fe_len,
3927 (unsigned long)ac->ac_g_ex.fe_logical,
3928 (unsigned long)ac->ac_b_ex.fe_group,
3929 (unsigned long)ac->ac_b_ex.fe_start,
3930 (unsigned long)ac->ac_b_ex.fe_len,
3931 (unsigned long)ac->ac_b_ex.fe_logical,
3932 (int)ac->ac_criteria);
3933 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3934 ac->ac_found);
3935 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003936 ngroups = ext4_get_groups_count(sb);
3937 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003938 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3939 struct ext4_prealloc_space *pa;
3940 ext4_grpblk_t start;
3941 struct list_head *cur;
3942 ext4_lock_group(sb, i);
3943 list_for_each(cur, &grp->bb_prealloc_list) {
3944 pa = list_entry(cur, struct ext4_prealloc_space,
3945 pa_group_list);
3946 spin_lock(&pa->pa_lock);
3947 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3948 NULL, &start);
3949 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003950 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3951 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003952 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003953 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003954
3955 if (grp->bb_free == 0)
3956 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003957 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003958 i, grp->bb_free, grp->bb_fragments);
3959 }
3960 printk(KERN_ERR "\n");
3961}
3962#else
3963static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3964{
3965 return;
3966}
3967#endif
3968
3969/*
3970 * We use locality group preallocation for small size file. The size of the
3971 * file is determined by the current size or the resulting size after
3972 * allocation which ever is larger
3973 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003974 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003975 */
3976static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3977{
3978 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3979 int bsbits = ac->ac_sb->s_blocksize_bits;
3980 loff_t size, isize;
3981
3982 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3983 return;
3984
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003985 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3986 return;
3987
Alex Tomasc9de5602008-01-29 00:19:52 -05003988 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04003989 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3990 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003991
Theodore Ts'o50797482009-09-18 13:34:02 -04003992 if ((size == isize) &&
3993 !ext4_fs_is_busy(sbi) &&
3994 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3995 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3996 return;
3997 }
3998
Alex Tomasc9de5602008-01-29 00:19:52 -05003999 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004000 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004001 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004002 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004003 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004004 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004005
4006 BUG_ON(ac->ac_lg != NULL);
4007 /*
4008 * locality group prealloc space are per cpu. The reason for having
4009 * per cpu locality group is to reduce the contention between block
4010 * request from multiple CPUs.
4011 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09004012 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004013
4014 /* we're going to use group allocation */
4015 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4016
4017 /* serialize all allocations in the group */
4018 mutex_lock(&ac->ac_lg->lg_mutex);
4019}
4020
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004021static noinline_for_stack int
4022ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004023 struct ext4_allocation_request *ar)
4024{
4025 struct super_block *sb = ar->inode->i_sb;
4026 struct ext4_sb_info *sbi = EXT4_SB(sb);
4027 struct ext4_super_block *es = sbi->s_es;
4028 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004029 unsigned int len;
4030 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004031 ext4_grpblk_t block;
4032
4033 /* we can't allocate > group size */
4034 len = ar->len;
4035
4036 /* just a dirty hack to filter too big requests */
4037 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4038 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4039
4040 /* start searching from the goal */
4041 goal = ar->goal;
4042 if (goal < le32_to_cpu(es->s_first_data_block) ||
4043 goal >= ext4_blocks_count(es))
4044 goal = le32_to_cpu(es->s_first_data_block);
4045 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4046
4047 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04004048 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05004049 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004050 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004051 ac->ac_sb = sb;
4052 ac->ac_inode = ar->inode;
4053 ac->ac_o_ex.fe_logical = ar->logical;
4054 ac->ac_o_ex.fe_group = group;
4055 ac->ac_o_ex.fe_start = block;
4056 ac->ac_o_ex.fe_len = len;
4057 ac->ac_g_ex.fe_logical = ar->logical;
4058 ac->ac_g_ex.fe_group = group;
4059 ac->ac_g_ex.fe_start = block;
4060 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004061 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004062
4063 /* we have to define context: we'll we work with a file or
4064 * locality group. this is a policy, actually */
4065 ext4_mb_group_or_file(ac);
4066
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004067 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004068 "left: %u/%u, right %u/%u to %swritable\n",
4069 (unsigned) ar->len, (unsigned) ar->logical,
4070 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4071 (unsigned) ar->lleft, (unsigned) ar->pleft,
4072 (unsigned) ar->lright, (unsigned) ar->pright,
4073 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4074 return 0;
4075
4076}
4077
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004078static noinline_for_stack void
4079ext4_mb_discard_lg_preallocations(struct super_block *sb,
4080 struct ext4_locality_group *lg,
4081 int order, int total_entries)
4082{
4083 ext4_group_t group = 0;
4084 struct ext4_buddy e4b;
4085 struct list_head discard_list;
4086 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004087
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004088 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004089
4090 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004091
4092 spin_lock(&lg->lg_prealloc_lock);
4093 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4094 pa_inode_list) {
4095 spin_lock(&pa->pa_lock);
4096 if (atomic_read(&pa->pa_count)) {
4097 /*
4098 * This is the pa that we just used
4099 * for block allocation. So don't
4100 * free that
4101 */
4102 spin_unlock(&pa->pa_lock);
4103 continue;
4104 }
4105 if (pa->pa_deleted) {
4106 spin_unlock(&pa->pa_lock);
4107 continue;
4108 }
4109 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004110 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004111
4112 /* seems this one can be freed ... */
4113 pa->pa_deleted = 1;
4114 spin_unlock(&pa->pa_lock);
4115
4116 list_del_rcu(&pa->pa_inode_list);
4117 list_add(&pa->u.pa_tmp_list, &discard_list);
4118
4119 total_entries--;
4120 if (total_entries <= 5) {
4121 /*
4122 * we want to keep only 5 entries
4123 * allowing it to grow to 8. This
4124 * mak sure we don't call discard
4125 * soon for this list.
4126 */
4127 break;
4128 }
4129 }
4130 spin_unlock(&lg->lg_prealloc_lock);
4131
4132 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4133
4134 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4135 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004136 ext4_error(sb, "Error loading buddy information for %u",
4137 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004138 continue;
4139 }
4140 ext4_lock_group(sb, group);
4141 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004142 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004143 ext4_unlock_group(sb, group);
4144
Jing Zhange39e07f2010-05-14 00:00:00 -04004145 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004146 list_del(&pa->u.pa_tmp_list);
4147 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4148 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004149}
4150
4151/*
4152 * We have incremented pa_count. So it cannot be freed at this
4153 * point. Also we hold lg_mutex. So no parallel allocation is
4154 * possible from this lg. That means pa_free cannot be updated.
4155 *
4156 * A parallel ext4_mb_discard_group_preallocations is possible.
4157 * which can cause the lg_prealloc_list to be updated.
4158 */
4159
4160static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4161{
4162 int order, added = 0, lg_prealloc_count = 1;
4163 struct super_block *sb = ac->ac_sb;
4164 struct ext4_locality_group *lg = ac->ac_lg;
4165 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4166
4167 order = fls(pa->pa_free) - 1;
4168 if (order > PREALLOC_TB_SIZE - 1)
4169 /* The max size of hash table is PREALLOC_TB_SIZE */
4170 order = PREALLOC_TB_SIZE - 1;
4171 /* Add the prealloc space to lg */
4172 rcu_read_lock();
4173 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4174 pa_inode_list) {
4175 spin_lock(&tmp_pa->pa_lock);
4176 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004177 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004178 continue;
4179 }
4180 if (!added && pa->pa_free < tmp_pa->pa_free) {
4181 /* Add to the tail of the previous entry */
4182 list_add_tail_rcu(&pa->pa_inode_list,
4183 &tmp_pa->pa_inode_list);
4184 added = 1;
4185 /*
4186 * we want to count the total
4187 * number of entries in the list
4188 */
4189 }
4190 spin_unlock(&tmp_pa->pa_lock);
4191 lg_prealloc_count++;
4192 }
4193 if (!added)
4194 list_add_tail_rcu(&pa->pa_inode_list,
4195 &lg->lg_prealloc_list[order]);
4196 rcu_read_unlock();
4197
4198 /* Now trim the list to be not more than 8 elements */
4199 if (lg_prealloc_count > 8) {
4200 ext4_mb_discard_lg_preallocations(sb, lg,
4201 order, lg_prealloc_count);
4202 return;
4203 }
4204 return ;
4205}
4206
Alex Tomasc9de5602008-01-29 00:19:52 -05004207/*
4208 * release all resource we used in allocation
4209 */
4210static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4211{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004212 struct ext4_prealloc_space *pa = ac->ac_pa;
4213 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004214 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004215 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004216 spin_lock(&pa->pa_lock);
4217 pa->pa_pstart += ac->ac_b_ex.fe_len;
4218 pa->pa_lstart += ac->ac_b_ex.fe_len;
4219 pa->pa_free -= ac->ac_b_ex.fe_len;
4220 pa->pa_len -= ac->ac_b_ex.fe_len;
4221 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004222 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004223 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004224 if (ac->alloc_semp)
4225 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004226 if (pa) {
4227 /*
4228 * We want to add the pa to the right bucket.
4229 * Remove it from the list and while adding
4230 * make sure the list to which we are adding
4231 * doesn't grow big. We need to release
4232 * alloc_semp before calling ext4_mb_add_n_trim()
4233 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004234 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004235 spin_lock(pa->pa_obj_lock);
4236 list_del_rcu(&pa->pa_inode_list);
4237 spin_unlock(pa->pa_obj_lock);
4238 ext4_mb_add_n_trim(ac);
4239 }
4240 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4241 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004242 if (ac->ac_bitmap_page)
4243 page_cache_release(ac->ac_bitmap_page);
4244 if (ac->ac_buddy_page)
4245 page_cache_release(ac->ac_buddy_page);
4246 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4247 mutex_unlock(&ac->ac_lg->lg_mutex);
4248 ext4_mb_collect_stats(ac);
4249 return 0;
4250}
4251
4252static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4253{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004254 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004255 int ret;
4256 int freed = 0;
4257
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004258 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004259 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004260 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4261 freed += ret;
4262 needed -= ret;
4263 }
4264
4265 return freed;
4266}
4267
4268/*
4269 * Main entry point into mballoc to allocate blocks
4270 * it tries to use preallocation first, then falls back
4271 * to usual allocation
4272 */
4273ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004274 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004275{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004276 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004277 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004278 struct ext4_sb_info *sbi;
4279 struct super_block *sb;
4280 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004281 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004282 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004283
4284 sb = ar->inode->i_sb;
4285 sbi = EXT4_SB(sb);
4286
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004287 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004288
Mingming Cao60e58e02009-01-22 18:13:05 +01004289 /*
4290 * For delayed allocation, we could skip the ENOSPC and
4291 * EDQUOT check, as blocks and quotas have been already
4292 * reserved when data being copied into pagecache.
4293 */
Theodore Ts'of2321092011-01-10 12:12:36 -05004294 if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
Mingming Cao60e58e02009-01-22 18:13:05 +01004295 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4296 else {
4297 /* Without delayed allocation we need to verify
4298 * there is enough free blocks to do block allocation
4299 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004300 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004301 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4302 /* let others to free the space */
4303 yield();
4304 ar->len = ar->len >> 1;
4305 }
4306 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004307 *errp = -ENOSPC;
4308 return 0;
4309 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004310 reserv_blks = ar->len;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004311 while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004312 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4313 ar->len--;
4314 }
4315 inquota = ar->len;
4316 if (ar->len == 0) {
4317 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004318 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004319 }
Mingming Caod2a17632008-07-14 17:52:37 -04004320 }
Mingming Caod2a17632008-07-14 17:52:37 -04004321
Eric Sandeen256bdb42008-02-10 01:13:33 -05004322 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004323 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004324 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004325 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004326 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004327 }
4328
Eric Sandeen256bdb42008-02-10 01:13:33 -05004329 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004330 if (*errp) {
4331 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004332 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004333 }
4334
Eric Sandeen256bdb42008-02-10 01:13:33 -05004335 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4336 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004337 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4338 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004339repeat:
4340 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004341 *errp = ext4_mb_regular_allocator(ac);
4342 if (*errp)
4343 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004344
4345 /* as we've just preallocated more space than
4346 * user requested orinally, we store allocated
4347 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004348 if (ac->ac_status == AC_STATUS_FOUND &&
4349 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4350 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004351 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004352 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004353 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004354 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004355 /*
4356 * drop the reference that we took
4357 * in ext4_mb_use_best_found
4358 */
4359 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004360 ac->ac_b_ex.fe_group = 0;
4361 ac->ac_b_ex.fe_start = 0;
4362 ac->ac_b_ex.fe_len = 0;
4363 ac->ac_status = AC_STATUS_CONTINUE;
4364 goto repeat;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004365 } else if (*errp)
4366 errout:
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004367 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004368 else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004369 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4370 ar->len = ac->ac_b_ex.fe_len;
4371 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004372 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004373 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004374 if (freed)
4375 goto repeat;
4376 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004377 }
4378
4379 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004380 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004381 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004382 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004383 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004384 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004385out:
4386 if (ac)
4387 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004388 if (inquota && ar->len < inquota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004389 dquot_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004390 if (!ar->len) {
Theodore Ts'of2321092011-01-10 12:12:36 -05004391 if (!ext4_test_inode_state(ar->inode,
4392 EXT4_STATE_DELALLOC_RESERVED))
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004393 /* release all the reserved blocks if non delalloc */
4394 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4395 reserv_blks);
4396 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004397
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004398 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004399
Alex Tomasc9de5602008-01-29 00:19:52 -05004400 return block;
4401}
Alex Tomasc9de5602008-01-29 00:19:52 -05004402
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004403/*
4404 * We can merge two free data extents only if the physical blocks
4405 * are contiguous, AND the extents were freed by the same transaction,
4406 * AND the blocks are associated with the same group.
4407 */
4408static int can_merge(struct ext4_free_data *entry1,
4409 struct ext4_free_data *entry2)
4410{
4411 if ((entry1->t_tid == entry2->t_tid) &&
4412 (entry1->group == entry2->group) &&
4413 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4414 return 1;
4415 return 0;
4416}
4417
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004418static noinline_for_stack int
4419ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004420 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004421{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004422 ext4_group_t group = e4b->bd_group;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004423 ext4_grpblk_t block;
4424 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004425 struct ext4_group_info *db = e4b->bd_info;
4426 struct super_block *sb = e4b->bd_sb;
4427 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004428 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4429 struct rb_node *parent = NULL, *new_node;
4430
Frank Mayhar03901312009-01-07 00:06:22 -05004431 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004432 BUG_ON(e4b->bd_bitmap_page == NULL);
4433 BUG_ON(e4b->bd_buddy_page == NULL);
4434
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004435 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004436 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004437
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004438 if (!*n) {
4439 /* first free block exent. We need to
4440 protect buddy cache from being freed,
4441 * otherwise we'll refresh it from
4442 * on-disk bitmap and lose not-yet-available
4443 * blocks */
4444 page_cache_get(e4b->bd_buddy_page);
4445 page_cache_get(e4b->bd_bitmap_page);
4446 }
4447 while (*n) {
4448 parent = *n;
4449 entry = rb_entry(parent, struct ext4_free_data, node);
4450 if (block < entry->start_blk)
4451 n = &(*n)->rb_left;
4452 else if (block >= (entry->start_blk + entry->count))
4453 n = &(*n)->rb_right;
4454 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004455 ext4_grp_locked_error(sb, group, 0,
4456 ext4_group_first_block_no(sb, group) + block,
4457 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004458 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004459 }
4460 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004461
4462 rb_link_node(new_node, parent, n);
4463 rb_insert_color(new_node, &db->bb_free_root);
4464
4465 /* Now try to see the extent can be merged to left and right */
4466 node = rb_prev(new_node);
4467 if (node) {
4468 entry = rb_entry(node, struct ext4_free_data, node);
4469 if (can_merge(entry, new_entry)) {
4470 new_entry->start_blk = entry->start_blk;
4471 new_entry->count += entry->count;
4472 rb_erase(node, &(db->bb_free_root));
4473 spin_lock(&sbi->s_md_lock);
4474 list_del(&entry->list);
4475 spin_unlock(&sbi->s_md_lock);
4476 kmem_cache_free(ext4_free_ext_cachep, entry);
4477 }
4478 }
4479
4480 node = rb_next(new_node);
4481 if (node) {
4482 entry = rb_entry(node, struct ext4_free_data, node);
4483 if (can_merge(new_entry, entry)) {
4484 new_entry->count += entry->count;
4485 rb_erase(node, &(db->bb_free_root));
4486 spin_lock(&sbi->s_md_lock);
4487 list_del(&entry->list);
4488 spin_unlock(&sbi->s_md_lock);
4489 kmem_cache_free(ext4_free_ext_cachep, entry);
4490 }
4491 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004492 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004493 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004494 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004495 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004496 return 0;
4497}
4498
Theodore Ts'o44338712009-11-22 07:44:56 -05004499/**
4500 * ext4_free_blocks() -- Free given blocks and update quota
4501 * @handle: handle for this transaction
4502 * @inode: inode
4503 * @block: start physical block to free
4504 * @count: number of blocks to count
4505 * @metadata: Are these metadata blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004506 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004507void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004508 struct buffer_head *bh, ext4_fsblk_t block,
4509 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004510{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004511 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004512 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004513 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004514 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004515 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004516 ext4_grpblk_t bit;
4517 struct buffer_head *gd_bh;
4518 ext4_group_t block_group;
4519 struct ext4_sb_info *sbi;
4520 struct ext4_buddy e4b;
4521 int err = 0;
4522 int ret;
4523
Theodore Ts'oe6362602009-11-23 07:17:05 -05004524 if (bh) {
4525 if (block)
4526 BUG_ON(block != bh->b_blocknr);
4527 else
4528 block = bh->b_blocknr;
4529 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004530
Alex Tomasc9de5602008-01-29 00:19:52 -05004531 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004532 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4533 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004534 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004535 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004536 goto error_return;
4537 }
4538
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004539 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004540 trace_ext4_free_blocks(inode, block, count, flags);
4541
4542 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4543 struct buffer_head *tbh = bh;
4544 int i;
4545
4546 BUG_ON(bh && (count > 1));
4547
4548 for (i = 0; i < count; i++) {
4549 if (!bh)
4550 tbh = sb_find_get_block(inode->i_sb,
4551 block + i);
Namhyung Kim87783692010-10-27 21:30:11 -04004552 if (unlikely(!tbh))
4553 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004554 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004555 inode, tbh, block + i);
4556 }
4557 }
4558
Theodore Ts'o60e66792010-05-17 07:00:00 -04004559 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004560 * We need to make sure we don't reuse the freed block until
4561 * after the transaction is committed, which we can do by
4562 * treating the block as metadata, below. We make an
4563 * exception if the inode is to be written in writeback mode
4564 * since writeback mode has weak data consistency guarantees.
4565 */
4566 if (!ext4_should_writeback_data(inode))
4567 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004568
Alex Tomasc9de5602008-01-29 00:19:52 -05004569do_more:
4570 overflow = 0;
4571 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4572
4573 /*
4574 * Check to see if we are freeing blocks across a group
4575 * boundary.
4576 */
4577 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4578 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4579 count -= overflow;
4580 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004581 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004582 if (!bitmap_bh) {
4583 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004584 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004585 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004586 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004587 if (!gdp) {
4588 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004589 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004590 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004591
4592 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4593 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4594 in_range(block, ext4_inode_table(sb, gdp),
4595 EXT4_SB(sb)->s_itb_per_group) ||
4596 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4597 EXT4_SB(sb)->s_itb_per_group)) {
4598
Eric Sandeen12062dd2010-02-15 14:19:27 -05004599 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004600 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004601 /* err = 0. ext4_std_error should be a no op */
4602 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004603 }
4604
4605 BUFFER_TRACE(bitmap_bh, "getting write access");
4606 err = ext4_journal_get_write_access(handle, bitmap_bh);
4607 if (err)
4608 goto error_return;
4609
4610 /*
4611 * We are about to modify some metadata. Call the journal APIs
4612 * to unshare ->b_data if a currently-committing transaction is
4613 * using it
4614 */
4615 BUFFER_TRACE(gd_bh, "get_write_access");
4616 err = ext4_journal_get_write_access(handle, gd_bh);
4617 if (err)
4618 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004619#ifdef AGGRESSIVE_CHECK
4620 {
4621 int i;
4622 for (i = 0; i < count; i++)
4623 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4624 }
4625#endif
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004626 trace_ext4_mballoc_free(sb, inode, block_group, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004627
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004628 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4629 if (err)
4630 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004631
4632 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004633 struct ext4_free_data *new_entry;
4634 /*
4635 * blocks being freed are metadata. these blocks shouldn't
4636 * be used until this transaction is committed
4637 */
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004638 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4639 if (!new_entry) {
4640 err = -ENOMEM;
4641 goto error_return;
4642 }
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004643 new_entry->start_blk = bit;
4644 new_entry->group = block_group;
4645 new_entry->count = count;
4646 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004647
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004648 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004649 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004650 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004651 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004652 /* need to update group_info->bb_free and bitmap
4653 * with group lock held. generate_buddy look at
4654 * them with group lock_held
4655 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004656 ext4_lock_group(sb, block_group);
4657 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004658 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004659 }
4660
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004661 ret = ext4_free_blks_count(sb, gdp) + count;
4662 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004663 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004664 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004665 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4666
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004667 if (sbi->s_log_groups_per_flex) {
4668 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004669 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004670 }
4671
Jing Zhange39e07f2010-05-14 00:00:00 -04004672 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004673
Theodore Ts'o44338712009-11-22 07:44:56 -05004674 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004675
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004676 /* We dirtied the bitmap block */
4677 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4678 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4679
Alex Tomasc9de5602008-01-29 00:19:52 -05004680 /* And the group descriptor block */
4681 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004682 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004683 if (!err)
4684 err = ret;
4685
4686 if (overflow && !err) {
4687 block += count;
4688 count = overflow;
4689 put_bh(bitmap_bh);
4690 goto do_more;
4691 }
Theodore Ts'oa0375152010-06-11 23:14:04 -04004692 ext4_mark_super_dirty(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004693error_return:
Theodore Ts'o44338712009-11-22 07:44:56 -05004694 if (freed)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004695 dquot_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004696 brelse(bitmap_bh);
4697 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004698 return;
4699}
Lukas Czerner7360d172010-10-27 21:30:12 -04004700
4701/**
4702 * ext4_trim_extent -- function to TRIM one single free extent in the group
4703 * @sb: super block for the file system
4704 * @start: starting block of the free extent in the alloc. group
4705 * @count: number of blocks to TRIM
4706 * @group: alloc. group we are working with
4707 * @e4b: ext4 buddy for the group
4708 *
4709 * Trim "count" blocks starting at "start" in the "group". To assure that no
4710 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4711 * be called with under the group lock.
4712 */
4713static int ext4_trim_extent(struct super_block *sb, int start, int count,
4714 ext4_group_t group, struct ext4_buddy *e4b)
4715{
4716 struct ext4_free_extent ex;
4717 int ret = 0;
4718
4719 assert_spin_locked(ext4_group_lock_ptr(sb, group));
4720
4721 ex.fe_start = start;
4722 ex.fe_group = group;
4723 ex.fe_len = count;
4724
4725 /*
4726 * Mark blocks used, so no one can reuse them while
4727 * being trimmed.
4728 */
4729 mb_mark_used(e4b, &ex);
4730 ext4_unlock_group(sb, group);
4731
4732 ret = ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04004733
4734 ext4_lock_group(sb, group);
4735 mb_free_blocks(NULL, e4b, start, ex.fe_len);
4736 return ret;
4737}
4738
4739/**
4740 * ext4_trim_all_free -- function to trim all free space in alloc. group
4741 * @sb: super block for file system
4742 * @e4b: ext4 buddy
4743 * @start: first group block to examine
4744 * @max: last group block to examine
4745 * @minblocks: minimum extent block count
4746 *
4747 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4748 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4749 * the extent.
4750 *
4751 *
4752 * ext4_trim_all_free walks through group's block bitmap searching for free
4753 * extents. When the free extent is found, mark it as used in group buddy
4754 * bitmap. Then issue a TRIM command on this extent and free the extent in
4755 * the group buddy bitmap. This is done until whole group is scanned.
4756 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05004757static ext4_grpblk_t
4758ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
Lukas Czerner7360d172010-10-27 21:30:12 -04004759 ext4_grpblk_t start, ext4_grpblk_t max, ext4_grpblk_t minblocks)
4760{
4761 void *bitmap;
4762 ext4_grpblk_t next, count = 0;
4763 ext4_group_t group;
4764 int ret = 0;
4765
4766 BUG_ON(e4b == NULL);
4767
4768 bitmap = e4b->bd_bitmap;
4769 group = e4b->bd_group;
4770 start = (e4b->bd_info->bb_first_free > start) ?
4771 e4b->bd_info->bb_first_free : start;
4772 ext4_lock_group(sb, group);
4773
4774 while (start < max) {
4775 start = mb_find_next_zero_bit(bitmap, max, start);
4776 if (start >= max)
4777 break;
4778 next = mb_find_next_bit(bitmap, max, start);
4779
4780 if ((next - start) >= minblocks) {
4781 ret = ext4_trim_extent(sb, start,
4782 next - start, group, e4b);
4783 if (ret < 0)
4784 break;
4785 count += next - start;
4786 }
4787 start = next + 1;
4788
4789 if (fatal_signal_pending(current)) {
4790 count = -ERESTARTSYS;
4791 break;
4792 }
4793
4794 if (need_resched()) {
4795 ext4_unlock_group(sb, group);
4796 cond_resched();
4797 ext4_lock_group(sb, group);
4798 }
4799
4800 if ((e4b->bd_info->bb_free - count) < minblocks)
4801 break;
4802 }
4803 ext4_unlock_group(sb, group);
4804
4805 ext4_debug("trimmed %d blocks in the group %d\n",
4806 count, group);
4807
4808 if (ret < 0)
4809 count = ret;
4810
4811 return count;
4812}
4813
4814/**
4815 * ext4_trim_fs() -- trim ioctl handle function
4816 * @sb: superblock for filesystem
4817 * @range: fstrim_range structure
4818 *
4819 * start: First Byte to trim
4820 * len: number of Bytes to trim from start
4821 * minlen: minimum extent length in Bytes
4822 * ext4_trim_fs goes through all allocation groups containing Bytes from
4823 * start to start+len. For each such a group ext4_trim_all_free function
4824 * is invoked to trim all free space.
4825 */
4826int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
4827{
4828 struct ext4_buddy e4b;
4829 ext4_group_t first_group, last_group;
4830 ext4_group_t group, ngroups = ext4_get_groups_count(sb);
4831 ext4_grpblk_t cnt = 0, first_block, last_block;
4832 uint64_t start, len, minlen, trimmed;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004833 ext4_fsblk_t first_data_blk =
4834 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner7360d172010-10-27 21:30:12 -04004835 int ret = 0;
4836
4837 start = range->start >> sb->s_blocksize_bits;
4838 len = range->len >> sb->s_blocksize_bits;
4839 minlen = range->minlen >> sb->s_blocksize_bits;
4840 trimmed = 0;
4841
4842 if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
4843 return -EINVAL;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004844 if (start < first_data_blk) {
4845 len -= first_data_blk - start;
4846 start = first_data_blk;
4847 }
Lukas Czerner7360d172010-10-27 21:30:12 -04004848
4849 /* Determine first and last group to examine based on start and len */
4850 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
4851 &first_group, &first_block);
4852 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) (start + len),
4853 &last_group, &last_block);
4854 last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
4855 last_block = EXT4_BLOCKS_PER_GROUP(sb);
4856
4857 if (first_group > last_group)
4858 return -EINVAL;
4859
4860 for (group = first_group; group <= last_group; group++) {
4861 ret = ext4_mb_load_buddy(sb, group, &e4b);
4862 if (ret) {
4863 ext4_error(sb, "Error in loading buddy "
4864 "information for %u", group);
4865 break;
4866 }
4867
4868 if (len >= EXT4_BLOCKS_PER_GROUP(sb))
4869 len -= (EXT4_BLOCKS_PER_GROUP(sb) - first_block);
4870 else
Jan Karaca6e9092011-01-10 12:30:39 -05004871 last_block = first_block + len;
Lukas Czerner7360d172010-10-27 21:30:12 -04004872
4873 if (e4b.bd_info->bb_free >= minlen) {
4874 cnt = ext4_trim_all_free(sb, &e4b, first_block,
4875 last_block, minlen);
4876 if (cnt < 0) {
4877 ret = cnt;
4878 ext4_mb_unload_buddy(&e4b);
4879 break;
4880 }
4881 }
4882 ext4_mb_unload_buddy(&e4b);
4883 trimmed += cnt;
4884 first_block = 0;
4885 }
4886 range->len = trimmed * sb->s_blocksize;
4887
4888 return ret;
4889}