blob: 545fa0256606fec5e94998cf3331108b7d3258b5 [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
Theodore Ts'o53accfa2011-09-09 18:48:51 -040073 * pa_len -> length for this prealloc space (in clusters)
74 * pa_free -> free space available in this prealloc space (in clusters)
Alex Tomasc9de5602008-01-29 00:19:52 -050075 *
76 * The inode preallocation space is used looking at the _logical_ start
77 * block. If only the logical file block falls within the range of prealloc
Tao Macaaf7a22011-07-11 18:42:42 -040078 * space we will consume the particular prealloc space. This makes sure that
79 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050080 *
81 * The important thing to be noted in case of inode prealloc space is that
82 * we don't modify the values associated to inode prealloc space except
83 * pa_free.
84 *
85 * If we are not able to find blocks in the inode prealloc space and if we
86 * have the group allocation flag set then we look at the locality group
Tao Macaaf7a22011-07-11 18:42:42 -040087 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050088 *
89 * ext4_sb_info.s_locality_groups[smp_processor_id()]
90 *
91 * The reason for having a per cpu locality group is to reduce the contention
92 * between CPUs. It is possible to get scheduled at this point.
93 *
94 * The locality group prealloc space is used looking at whether we have
Lucas De Marchi25985ed2011-03-30 22:57:33 -030095 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -050096 *
97 * If we can't allocate blocks via inode prealloc or/and locality group
98 * prealloc then we look at the buddy cache. The buddy cache is represented
99 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
100 * mapped to the buddy and bitmap information regarding different
101 * groups. The buddy information is attached to buddy cache inode so that
102 * we can access them through the page cache. The information regarding
103 * each group is loaded via ext4_mb_load_buddy. The information involve
104 * block bitmap and buddy information. The information are stored in the
105 * inode as:
106 *
107 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500108 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500109 *
110 *
111 * one block each for bitmap and buddy information. So for each group we
112 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
113 * blocksize) blocks. So it can have information regarding groups_per_page
114 * which is blocks_per_page/2
115 *
116 * The buddy cache inode is not stored on disk. The inode is thrown
117 * away when the filesystem is unmounted.
118 *
119 * We look for count number of blocks in the buddy cache. If we were able
120 * to locate that many free blocks we return with additional information
121 * regarding rest of the contiguous physical block available
122 *
123 * Before allocating blocks via buddy cache we normalize the request
124 * blocks. This ensure we ask for more blocks that we needed. The extra
125 * blocks that we get after allocation is added to the respective prealloc
126 * list. In case of inode preallocation we follow a list of heuristics
127 * based on file size. This can be found in ext4_mb_normalize_request. If
128 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'o27baebb2011-09-09 19:02:51 -0400129 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
130 * dependent on the cluster size; for non-bigalloc file systems, it is
Alex Tomasc9de5602008-01-29 00:19:52 -0500131 * 512 blocks. This can be tuned via
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400132 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500133 * terms of number of blocks. If we have mounted the file system with -O
134 * stripe=<value> option the group prealloc request is normalized to the
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400135 * the smallest multiple of the stripe value (sbi->s_stripe) which is
136 * greater than the default mb_group_prealloc.
Alex Tomasc9de5602008-01-29 00:19:52 -0500137 *
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400138 * The regular allocator (using the buddy cache) supports a few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500139 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400140 * /sys/fs/ext4/<partition>/mb_min_to_scan
141 * /sys/fs/ext4/<partition>/mb_max_to_scan
142 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500143 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400144 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500145 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
146 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400147 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200148 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400149 * stripe size. This should result in better allocation on RAID setups. If
150 * not, we search in the specific group using bitmap for best extents. The
151 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500152 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400153 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500154 * best extent in the found extents. Searching for the blocks starts with
155 * the group specified as the goal value in allocation context via
156 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400157 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500158 * checked.
159 *
160 * Both the prealloc space are getting populated as above. So for the first
161 * request we will hit the buddy cache which will result in this prealloc
162 * space getting filled. The prealloc space is then later used for the
163 * subsequent request.
164 */
165
166/*
167 * mballoc operates on the following data:
168 * - on-disk bitmap
169 * - in-core buddy (actually includes buddy and bitmap)
170 * - preallocation descriptors (PAs)
171 *
172 * there are two types of preallocations:
173 * - inode
174 * assiged to specific inode and can be used for this inode only.
175 * it describes part of inode's space preallocated to specific
176 * physical blocks. any block from that preallocated can be used
177 * independent. the descriptor just tracks number of blocks left
178 * unused. so, before taking some block from descriptor, one must
179 * make sure corresponded logical block isn't allocated yet. this
180 * also means that freeing any block within descriptor's range
181 * must discard all preallocated blocks.
182 * - locality group
183 * assigned to specific locality group which does not translate to
184 * permanent set of inodes: inode can join and leave group. space
185 * from this type of preallocation can be used for any inode. thus
186 * it's consumed from the beginning to the end.
187 *
188 * relation between them can be expressed as:
189 * in-core buddy = on-disk bitmap + preallocation descriptors
190 *
191 * this mean blocks mballoc considers used are:
192 * - allocated blocks (persistent)
193 * - preallocated blocks (non-persistent)
194 *
195 * consistency in mballoc world means that at any time a block is either
196 * free or used in ALL structures. notice: "any time" should not be read
197 * literally -- time is discrete and delimited by locks.
198 *
199 * to keep it simple, we don't use block numbers, instead we count number of
200 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
201 *
202 * all operations can be expressed as:
203 * - init buddy: buddy = on-disk + PAs
204 * - new PA: buddy += N; PA = N
205 * - use inode PA: on-disk += N; PA -= N
206 * - discard inode PA buddy -= on-disk - PA; PA = 0
207 * - use locality group PA on-disk += N; PA -= N
208 * - discard locality group PA buddy -= PA; PA = 0
209 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
210 * is used in real operation because we can't know actual used
211 * bits from PA, only from on-disk bitmap
212 *
213 * if we follow this strict logic, then all operations above should be atomic.
214 * given some of them can block, we'd have to use something like semaphores
215 * killing performance on high-end SMP hardware. let's try to relax it using
216 * the following knowledge:
217 * 1) if buddy is referenced, it's already initialized
218 * 2) while block is used in buddy and the buddy is referenced,
219 * nobody can re-allocate that block
220 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
221 * bit set and PA claims same block, it's OK. IOW, one can set bit in
222 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
223 * block
224 *
225 * so, now we're building a concurrency table:
226 * - init buddy vs.
227 * - new PA
228 * blocks for PA are allocated in the buddy, buddy must be referenced
229 * until PA is linked to allocation group to avoid concurrent buddy init
230 * - use inode PA
231 * we need to make sure that either on-disk bitmap or PA has uptodate data
232 * given (3) we care that PA-=N operation doesn't interfere with init
233 * - discard inode PA
234 * the simplest way would be to have buddy initialized by the discard
235 * - use locality group PA
236 * again PA-=N must be serialized with init
237 * - discard locality group PA
238 * the simplest way would be to have buddy initialized by the discard
239 * - new PA vs.
240 * - use inode PA
241 * i_data_sem serializes them
242 * - discard inode PA
243 * discard process must wait until PA isn't used by another process
244 * - use locality group PA
245 * some mutex should serialize them
246 * - discard locality group PA
247 * discard process must wait until PA isn't used by another process
248 * - use inode PA
249 * - use inode PA
250 * i_data_sem or another mutex should serializes them
251 * - discard inode PA
252 * discard process must wait until PA isn't used by another process
253 * - use locality group PA
254 * nothing wrong here -- they're different PAs covering different blocks
255 * - discard locality group PA
256 * discard process must wait until PA isn't used by another process
257 *
258 * now we're ready to make few consequences:
259 * - PA is referenced and while it is no discard is possible
260 * - PA is referenced until block isn't marked in on-disk bitmap
261 * - PA changes only after on-disk bitmap
262 * - discard must not compete with init. either init is done before
263 * any discard or they're serialized somehow
264 * - buddy init as sum of on-disk bitmap and PAs is done atomically
265 *
266 * a special case when we've used PA to emptiness. no need to modify buddy
267 * in this case, but we should care about concurrent init
268 *
269 */
270
271 /*
272 * Logic in few words:
273 *
274 * - allocation:
275 * load group
276 * find blocks
277 * mark bits in on-disk bitmap
278 * release group
279 *
280 * - use preallocation:
281 * find proper PA (per-inode or group)
282 * load group
283 * mark bits in on-disk bitmap
284 * release group
285 * release PA
286 *
287 * - free:
288 * load group
289 * mark bits in on-disk bitmap
290 * release group
291 *
292 * - discard preallocations in group:
293 * mark PAs deleted
294 * move them onto local list
295 * load on-disk bitmap
296 * load group
297 * remove PA from object (inode or locality group)
298 * mark free blocks in-core
299 *
300 * - discard inode's preallocations:
301 */
302
303/*
304 * Locking rules
305 *
306 * Locks:
307 * - bitlock on a group (group)
308 * - object (inode/locality) (object)
309 * - per-pa lock (pa)
310 *
311 * Paths:
312 * - new pa
313 * object
314 * group
315 *
316 * - find and use pa:
317 * pa
318 *
319 * - release consumed pa:
320 * pa
321 * group
322 * object
323 *
324 * - generate in-core bitmap:
325 * group
326 * pa
327 *
328 * - discard all for given object (inode, locality group):
329 * object
330 * pa
331 * group
332 *
333 * - discard all for given group:
334 * group
335 * pa
336 * group
337 * object
338 *
339 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500340static struct kmem_cache *ext4_pspace_cachep;
341static struct kmem_cache *ext4_ac_cachep;
342static struct kmem_cache *ext4_free_ext_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400343
344/* We create slab caches for groupinfo data structures based on the
345 * superblock block size. There will be one per mounted filesystem for
346 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500347#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400348static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
349
Eric Sandeen2892c152011-02-12 08:12:18 -0500350static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
351 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
352 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
353 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
354};
355
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500356static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
357 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500358static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
359 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500360static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
361
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500362static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
363{
Alex Tomasc9de5602008-01-29 00:19:52 -0500364#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500365 *bit += ((unsigned long) addr & 7UL) << 3;
366 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500367#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500368 *bit += ((unsigned long) addr & 3UL) << 3;
369 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500370#else
371#error "how many bits you are?!"
372#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500373 return addr;
374}
Alex Tomasc9de5602008-01-29 00:19:52 -0500375
376static inline int mb_test_bit(int bit, void *addr)
377{
378 /*
379 * ext4_test_bit on architecture like powerpc
380 * needs unsigned long aligned address
381 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500382 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500383 return ext4_test_bit(bit, addr);
384}
385
386static inline void mb_set_bit(int bit, void *addr)
387{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500388 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500389 ext4_set_bit(bit, addr);
390}
391
Alex Tomasc9de5602008-01-29 00:19:52 -0500392static inline void mb_clear_bit(int bit, void *addr)
393{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500394 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500395 ext4_clear_bit(bit, addr);
396}
397
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500398static inline int mb_find_next_zero_bit(void *addr, int max, int start)
399{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400400 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500401 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400402 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500403 start += fix;
404
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400405 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
406 if (ret > max)
407 return max;
408 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500409}
410
411static inline int mb_find_next_bit(void *addr, int max, int start)
412{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400413 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500414 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400415 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500416 start += fix;
417
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400418 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
419 if (ret > max)
420 return max;
421 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500422}
423
Alex Tomasc9de5602008-01-29 00:19:52 -0500424static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
425{
426 char *bb;
427
Alex Tomasc9de5602008-01-29 00:19:52 -0500428 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
429 BUG_ON(max == NULL);
430
431 if (order > e4b->bd_blkbits + 1) {
432 *max = 0;
433 return NULL;
434 }
435
436 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500437 if (order == 0) {
438 *max = 1 << (e4b->bd_blkbits + 3);
Alex Tomasc9de5602008-01-29 00:19:52 -0500439 return EXT4_MB_BITMAP(e4b);
Coly Li84b775a2011-02-24 12:51:59 -0500440 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500441
442 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
443 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
444
445 return bb;
446}
447
448#ifdef DOUBLE_CHECK
449static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
450 int first, int count)
451{
452 int i;
453 struct super_block *sb = e4b->bd_sb;
454
455 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
456 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400457 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500458 for (i = 0; i < count; i++) {
459 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
460 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500461
462 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400463 blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500464 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400465 inode ? inode->i_ino : 0,
466 blocknr,
467 "freeing block already freed "
468 "(bit %u)",
469 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500470 }
471 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
472 }
473}
474
475static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
476{
477 int i;
478
479 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
480 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400481 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500482 for (i = 0; i < count; i++) {
483 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
484 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
485 }
486}
487
488static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
489{
490 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
491 unsigned char *b1, *b2;
492 int i;
493 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
494 b2 = (unsigned char *) bitmap;
495 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
496 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400497 ext4_msg(e4b->bd_sb, KERN_ERR,
498 "corruption in group %u "
499 "at byte %u(%u): %x in copy != %x "
500 "on disk/prealloc",
501 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500502 BUG();
503 }
504 }
505 }
506}
507
508#else
509static inline void mb_free_blocks_double(struct inode *inode,
510 struct ext4_buddy *e4b, int first, int count)
511{
512 return;
513}
514static inline void mb_mark_used_double(struct ext4_buddy *e4b,
515 int first, int count)
516{
517 return;
518}
519static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
520{
521 return;
522}
523#endif
524
525#ifdef AGGRESSIVE_CHECK
526
527#define MB_CHECK_ASSERT(assert) \
528do { \
529 if (!(assert)) { \
530 printk(KERN_EMERG \
531 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
532 function, file, line, # assert); \
533 BUG(); \
534 } \
535} while (0)
536
537static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
538 const char *function, int line)
539{
540 struct super_block *sb = e4b->bd_sb;
541 int order = e4b->bd_blkbits + 1;
542 int max;
543 int max2;
544 int i;
545 int j;
546 int k;
547 int count;
548 struct ext4_group_info *grp;
549 int fragments = 0;
550 int fstart;
551 struct list_head *cur;
552 void *buddy;
553 void *buddy2;
554
Alex Tomasc9de5602008-01-29 00:19:52 -0500555 {
556 static int mb_check_counter;
557 if (mb_check_counter++ % 100 != 0)
558 return 0;
559 }
560
561 while (order > 1) {
562 buddy = mb_find_buddy(e4b, order, &max);
563 MB_CHECK_ASSERT(buddy);
564 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
565 MB_CHECK_ASSERT(buddy2);
566 MB_CHECK_ASSERT(buddy != buddy2);
567 MB_CHECK_ASSERT(max * 2 == max2);
568
569 count = 0;
570 for (i = 0; i < max; i++) {
571
572 if (mb_test_bit(i, buddy)) {
573 /* only single bit in buddy2 may be 1 */
574 if (!mb_test_bit(i << 1, buddy2)) {
575 MB_CHECK_ASSERT(
576 mb_test_bit((i<<1)+1, buddy2));
577 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
578 MB_CHECK_ASSERT(
579 mb_test_bit(i << 1, buddy2));
580 }
581 continue;
582 }
583
Robin Dong0a10da72011-10-26 08:48:54 -0400584 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500585 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
586 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
587
588 for (j = 0; j < (1 << order); j++) {
589 k = (i * (1 << order)) + j;
590 MB_CHECK_ASSERT(
591 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
592 }
593 count++;
594 }
595 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
596 order--;
597 }
598
599 fstart = -1;
600 buddy = mb_find_buddy(e4b, 0, &max);
601 for (i = 0; i < max; i++) {
602 if (!mb_test_bit(i, buddy)) {
603 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
604 if (fstart == -1) {
605 fragments++;
606 fstart = i;
607 }
608 continue;
609 }
610 fstart = -1;
611 /* check used bits only */
612 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
613 buddy2 = mb_find_buddy(e4b, j, &max2);
614 k = i >> j;
615 MB_CHECK_ASSERT(k < max2);
616 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
617 }
618 }
619 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
620 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
621
622 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500623 list_for_each(cur, &grp->bb_prealloc_list) {
624 ext4_group_t groupnr;
625 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400626 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
627 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500628 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400629 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500630 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
631 }
632 return 0;
633}
634#undef MB_CHECK_ASSERT
635#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400636 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500637#else
638#define mb_check_buddy(e4b)
639#endif
640
Coly Li7c786052011-02-24 13:24:25 -0500641/*
642 * Divide blocks started from @first with length @len into
643 * smaller chunks with power of 2 blocks.
644 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
645 * then increase bb_counters[] for corresponded chunk size.
646 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500647static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400648 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500649 struct ext4_group_info *grp)
650{
651 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400652 ext4_grpblk_t min;
653 ext4_grpblk_t max;
654 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500655 unsigned short border;
656
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400657 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500658
659 border = 2 << sb->s_blocksize_bits;
660
661 while (len > 0) {
662 /* find how many blocks can be covered since this position */
663 max = ffs(first | border) - 1;
664
665 /* find how many blocks of power 2 we need to mark */
666 min = fls(len) - 1;
667
668 if (max < min)
669 min = max;
670 chunk = 1 << min;
671
672 /* mark multiblock chunks only */
673 grp->bb_counters[min]++;
674 if (min > 0)
675 mb_clear_bit(first >> min,
676 buddy + sbi->s_mb_offsets[min]);
677
678 len -= chunk;
679 first += chunk;
680 }
681}
682
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400683/*
684 * Cache the order of the largest free extent we have available in this block
685 * group.
686 */
687static void
688mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
689{
690 int i;
691 int bits;
692
693 grp->bb_largest_free_order = -1; /* uninit */
694
695 bits = sb->s_blocksize_bits + 1;
696 for (i = bits; i >= 0; i--) {
697 if (grp->bb_counters[i] > 0) {
698 grp->bb_largest_free_order = i;
699 break;
700 }
701 }
702}
703
Eric Sandeen089ceec2009-07-05 22:17:31 -0400704static noinline_for_stack
705void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500706 void *buddy, void *bitmap, ext4_group_t group)
707{
708 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400709 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400710 ext4_grpblk_t i = 0;
711 ext4_grpblk_t first;
712 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500713 unsigned free = 0;
714 unsigned fragments = 0;
715 unsigned long long period = get_cycles();
716
717 /* initialize buddy from bitmap which is aggregation
718 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500719 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500720 grp->bb_first_free = i;
721 while (i < max) {
722 fragments++;
723 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500724 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500725 len = i - first;
726 free += len;
727 if (len > 1)
728 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
729 else
730 grp->bb_counters[0]++;
731 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500732 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500733 }
734 grp->bb_fragments = fragments;
735
736 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400737 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400738 "%u clusters in bitmap, %u in gd",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400739 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500740 /*
741 * If we intent to continue, we consider group descritor
742 * corrupt and update bb_free using bitmap value
743 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500744 grp->bb_free = free;
745 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400746 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500747
748 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
749
750 period = get_cycles() - period;
751 spin_lock(&EXT4_SB(sb)->s_bal_lock);
752 EXT4_SB(sb)->s_mb_buddies_generated++;
753 EXT4_SB(sb)->s_mb_generation_time += period;
754 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
755}
756
757/* The buddy information is attached the buddy cache inode
758 * for convenience. The information regarding each group
759 * is loaded via ext4_mb_load_buddy. The information involve
760 * block bitmap and buddy information. The information are
761 * stored in the inode as
762 *
763 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500764 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500765 *
766 *
767 * one block each for bitmap and buddy information.
768 * So for each group we take up 2 blocks. A page can
769 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
770 * So it can have information regarding groups_per_page which
771 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400772 *
773 * Locking note: This routine takes the block group lock of all groups
774 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500775 */
776
777static int ext4_mb_init_cache(struct page *page, char *incore)
778{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400779 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500780 int blocksize;
781 int blocks_per_page;
782 int groups_per_page;
783 int err = 0;
784 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500785 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500786 int first_block;
787 struct super_block *sb;
788 struct buffer_head *bhs;
789 struct buffer_head **bh;
790 struct inode *inode;
791 char *data;
792 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400793 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500794
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400795 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500796
797 inode = page->mapping->host;
798 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400799 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500800 blocksize = 1 << inode->i_blkbits;
801 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
802
803 groups_per_page = blocks_per_page >> 1;
804 if (groups_per_page == 0)
805 groups_per_page = 1;
806
807 /* allocate buffer_heads to read bitmaps */
808 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500809 i = sizeof(struct buffer_head *) * groups_per_page;
810 bh = kzalloc(i, GFP_NOFS);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500811 if (bh == NULL) {
812 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500813 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500814 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500815 } else
816 bh = &bhs;
817
818 first_group = page->index * blocks_per_page / 2;
819
820 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500821 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
822 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500823 break;
824
Theodore Ts'o813e5722012-02-20 17:52:46 -0500825 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400826 /*
827 * If page is uptodate then we came here after online resize
828 * which added some new uninitialized group info structs, so
829 * we must skip all initialized uptodate buddies on the page,
830 * which may be currently in use by an allocating task.
831 */
832 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
833 bh[i] = NULL;
834 continue;
835 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500836 if (!(bh[i] = ext4_read_block_bitmap_nowait(sb, group))) {
837 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500838 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500839 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500840 mb_debug(1, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500841 }
842
843 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500844 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
845 if (bh[i] && ext4_wait_block_bitmap(sb, group, bh[i])) {
846 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500847 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500848 }
849 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500850
851 first_block = page->index * blocks_per_page;
852 for (i = 0; i < blocks_per_page; i++) {
853 int group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500854
855 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400856 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500857 break;
858
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400859 if (!bh[group - first_group])
860 /* skip initialized uptodate buddy */
861 continue;
862
Alex Tomasc9de5602008-01-29 00:19:52 -0500863 /*
864 * data carry information regarding this
865 * particular group in the format specified
866 * above
867 *
868 */
869 data = page_address(page) + (i * blocksize);
870 bitmap = bh[group - first_group]->b_data;
871
872 /*
873 * We place the buddy block and bitmap block
874 * close together
875 */
876 if ((first_block + i) & 1) {
877 /* this is block of buddy */
878 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400879 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500880 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400881 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500882 grinfo = ext4_get_group_info(sb, group);
883 grinfo->bb_fragments = 0;
884 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400885 sizeof(*grinfo->bb_counters) *
886 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500887 /*
888 * incore got set to the group block bitmap below
889 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500890 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400891 /* init the buddy */
892 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500893 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500894 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500895 incore = NULL;
896 } else {
897 /* this is block of bitmap */
898 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400899 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500900 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400901 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500902
903 /* see comments in ext4_mb_put_pa() */
904 ext4_lock_group(sb, group);
905 memcpy(data, bitmap, blocksize);
906
907 /* mark all preallocated blks used in in-core bitmap */
908 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500909 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500910 ext4_unlock_group(sb, group);
911
912 /* set incore so that the buddy information can be
913 * generated using this
914 */
915 incore = data;
916 }
917 }
918 SetPageUptodate(page);
919
920out:
921 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400922 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500923 brelse(bh[i]);
924 if (bh != &bhs)
925 kfree(bh);
926 }
927 return err;
928}
929
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400930/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400931 * Lock the buddy and bitmap pages. This make sure other parallel init_group
932 * on the same buddy page doesn't happen whild holding the buddy page lock.
933 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
934 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400935 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400936static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
937 ext4_group_t group, struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400938{
Amir Goldstein2de88072011-05-09 21:48:13 -0400939 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
940 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400941 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400942 struct page *page;
943
944 e4b->bd_buddy_page = NULL;
945 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400946
947 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
948 /*
949 * the buddy cache inode stores the block bitmap
950 * and buddy information in consecutive blocks.
951 * So for each group we need two blocks.
952 */
953 block = group * 2;
954 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400955 poff = block % blocks_per_page;
956 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
957 if (!page)
958 return -EIO;
959 BUG_ON(page->mapping != inode->i_mapping);
960 e4b->bd_bitmap_page = page;
961 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400962
Amir Goldstein2de88072011-05-09 21:48:13 -0400963 if (blocks_per_page >= 2) {
964 /* buddy and bitmap are on the same page */
965 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400966 }
Amir Goldstein2de88072011-05-09 21:48:13 -0400967
968 block++;
969 pnum = block / blocks_per_page;
970 poff = block % blocks_per_page;
971 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
972 if (!page)
973 return -EIO;
974 BUG_ON(page->mapping != inode->i_mapping);
975 e4b->bd_buddy_page = page;
976 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400977}
978
Amir Goldstein2de88072011-05-09 21:48:13 -0400979static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400980{
Amir Goldstein2de88072011-05-09 21:48:13 -0400981 if (e4b->bd_bitmap_page) {
982 unlock_page(e4b->bd_bitmap_page);
983 page_cache_release(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400984 }
Amir Goldstein2de88072011-05-09 21:48:13 -0400985 if (e4b->bd_buddy_page) {
986 unlock_page(e4b->bd_buddy_page);
987 page_cache_release(e4b->bd_buddy_page);
988 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400989}
990
991/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400992 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
993 * block group lock of all groups for this page; do not hold the BG lock when
994 * calling this routine!
995 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400996static noinline_for_stack
997int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
998{
999
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001000 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001001 struct ext4_buddy e4b;
1002 struct page *page;
1003 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001004
1005 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001006 this_grp = ext4_get_group_info(sb, group);
1007 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001008 * This ensures that we don't reinit the buddy cache
1009 * page which map to the group from which we are already
1010 * allocating. If we are looking at the buddy cache we would
1011 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001012 * would have pinned buddy page to page cache.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001013 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001014 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b);
1015 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001016 /*
1017 * somebody initialized the group
1018 * return without doing anything
1019 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001020 goto err;
1021 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001022
1023 page = e4b.bd_bitmap_page;
1024 ret = ext4_mb_init_cache(page, NULL);
1025 if (ret)
1026 goto err;
1027 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001028 ret = -EIO;
1029 goto err;
1030 }
1031 mark_page_accessed(page);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001032
Amir Goldstein2de88072011-05-09 21:48:13 -04001033 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001034 /*
1035 * If both the bitmap and buddy are in
1036 * the same page we don't need to force
1037 * init the buddy
1038 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001039 ret = 0;
1040 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001041 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001042 /* init buddy cache */
1043 page = e4b.bd_buddy_page;
1044 ret = ext4_mb_init_cache(page, e4b.bd_bitmap);
1045 if (ret)
1046 goto err;
1047 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001048 ret = -EIO;
1049 goto err;
1050 }
1051 mark_page_accessed(page);
1052err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001053 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001054 return ret;
1055}
1056
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001057/*
1058 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1059 * block group lock of all groups for this page; do not hold the BG lock when
1060 * calling this routine!
1061 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001062static noinline_for_stack int
1063ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1064 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001065{
Alex Tomasc9de5602008-01-29 00:19:52 -05001066 int blocks_per_page;
1067 int block;
1068 int pnum;
1069 int poff;
1070 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001071 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001072 struct ext4_group_info *grp;
1073 struct ext4_sb_info *sbi = EXT4_SB(sb);
1074 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001075
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001076 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001077
1078 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001079 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001080
1081 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001082 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001083 e4b->bd_sb = sb;
1084 e4b->bd_group = group;
1085 e4b->bd_buddy_page = NULL;
1086 e4b->bd_bitmap_page = NULL;
1087
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001088 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001089 /*
1090 * we need full data about the group
1091 * to make a good selection
1092 */
1093 ret = ext4_mb_init_group(sb, group);
1094 if (ret)
1095 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001096 }
1097
Alex Tomasc9de5602008-01-29 00:19:52 -05001098 /*
1099 * the buddy cache inode stores the block bitmap
1100 * and buddy information in consecutive blocks.
1101 * So for each group we need two blocks.
1102 */
1103 block = group * 2;
1104 pnum = block / blocks_per_page;
1105 poff = block % blocks_per_page;
1106
1107 /* we could use find_or_create_page(), but it locks page
1108 * what we'd like to avoid in fast path ... */
1109 page = find_get_page(inode->i_mapping, pnum);
1110 if (page == NULL || !PageUptodate(page)) {
1111 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001112 /*
1113 * drop the page reference and try
1114 * to get the page with lock. If we
1115 * are not uptodate that implies
1116 * somebody just created the page but
1117 * is yet to initialize the same. So
1118 * wait for it to initialize.
1119 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001120 page_cache_release(page);
1121 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1122 if (page) {
1123 BUG_ON(page->mapping != inode->i_mapping);
1124 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001125 ret = ext4_mb_init_cache(page, NULL);
1126 if (ret) {
1127 unlock_page(page);
1128 goto err;
1129 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001130 mb_cmp_bitmaps(e4b, page_address(page) +
1131 (poff * sb->s_blocksize));
1132 }
1133 unlock_page(page);
1134 }
1135 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001136 if (page == NULL || !PageUptodate(page)) {
1137 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001138 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001139 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001140 e4b->bd_bitmap_page = page;
1141 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1142 mark_page_accessed(page);
1143
1144 block++;
1145 pnum = block / blocks_per_page;
1146 poff = block % blocks_per_page;
1147
1148 page = find_get_page(inode->i_mapping, pnum);
1149 if (page == NULL || !PageUptodate(page)) {
1150 if (page)
1151 page_cache_release(page);
1152 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1153 if (page) {
1154 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001155 if (!PageUptodate(page)) {
1156 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1157 if (ret) {
1158 unlock_page(page);
1159 goto err;
1160 }
1161 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001162 unlock_page(page);
1163 }
1164 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001165 if (page == NULL || !PageUptodate(page)) {
1166 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001167 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001168 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001169 e4b->bd_buddy_page = page;
1170 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1171 mark_page_accessed(page);
1172
1173 BUG_ON(e4b->bd_bitmap_page == NULL);
1174 BUG_ON(e4b->bd_buddy_page == NULL);
1175
1176 return 0;
1177
1178err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001179 if (page)
1180 page_cache_release(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001181 if (e4b->bd_bitmap_page)
1182 page_cache_release(e4b->bd_bitmap_page);
1183 if (e4b->bd_buddy_page)
1184 page_cache_release(e4b->bd_buddy_page);
1185 e4b->bd_buddy = NULL;
1186 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001187 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001188}
1189
Jing Zhange39e07f2010-05-14 00:00:00 -04001190static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001191{
1192 if (e4b->bd_bitmap_page)
1193 page_cache_release(e4b->bd_bitmap_page);
1194 if (e4b->bd_buddy_page)
1195 page_cache_release(e4b->bd_buddy_page);
1196}
1197
1198
1199static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1200{
1201 int order = 1;
1202 void *bb;
1203
1204 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1205 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1206
1207 bb = EXT4_MB_BUDDY(e4b);
1208 while (order <= e4b->bd_blkbits + 1) {
1209 block = block >> 1;
1210 if (!mb_test_bit(block, bb)) {
1211 /* this block is part of buddy of order 'order' */
1212 return order;
1213 }
1214 bb += 1 << (e4b->bd_blkbits - order);
1215 order++;
1216 }
1217 return 0;
1218}
1219
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001220static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001221{
1222 __u32 *addr;
1223
1224 len = cur + len;
1225 while (cur < len) {
1226 if ((cur & 31) == 0 && (len - cur) >= 32) {
1227 /* fast path: clear whole word at once */
1228 addr = bm + (cur >> 3);
1229 *addr = 0;
1230 cur += 32;
1231 continue;
1232 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001233 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001234 cur++;
1235 }
1236}
1237
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001238void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001239{
1240 __u32 *addr;
1241
1242 len = cur + len;
1243 while (cur < len) {
1244 if ((cur & 31) == 0 && (len - cur) >= 32) {
1245 /* fast path: set whole word at once */
1246 addr = bm + (cur >> 3);
1247 *addr = 0xffffffff;
1248 cur += 32;
1249 continue;
1250 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001251 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001252 cur++;
1253 }
1254}
1255
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001256static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001257 int first, int count)
1258{
1259 int block = 0;
1260 int max = 0;
1261 int order;
1262 void *buddy;
1263 void *buddy2;
1264 struct super_block *sb = e4b->bd_sb;
1265
1266 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001267 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001268 mb_check_buddy(e4b);
1269 mb_free_blocks_double(inode, e4b, first, count);
1270
1271 e4b->bd_info->bb_free += count;
1272 if (first < e4b->bd_info->bb_first_free)
1273 e4b->bd_info->bb_first_free = first;
1274
1275 /* let's maintain fragments counter */
1276 if (first != 0)
1277 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1278 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1279 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1280 if (block && max)
1281 e4b->bd_info->bb_fragments--;
1282 else if (!block && !max)
1283 e4b->bd_info->bb_fragments++;
1284
1285 /* let's maintain buddy itself */
1286 while (count-- > 0) {
1287 block = first++;
1288 order = 0;
1289
1290 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1291 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001292
1293 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001294 blocknr += EXT4_C2B(EXT4_SB(sb), block);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001295 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001296 inode ? inode->i_ino : 0,
1297 blocknr,
1298 "freeing already freed block "
1299 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001300 }
1301 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1302 e4b->bd_info->bb_counters[order]++;
1303
1304 /* start of the buddy */
1305 buddy = mb_find_buddy(e4b, order, &max);
1306
1307 do {
1308 block &= ~1UL;
1309 if (mb_test_bit(block, buddy) ||
1310 mb_test_bit(block + 1, buddy))
1311 break;
1312
1313 /* both the buddies are free, try to coalesce them */
1314 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1315
1316 if (!buddy2)
1317 break;
1318
1319 if (order > 0) {
1320 /* for special purposes, we don't set
1321 * free bits in bitmap */
1322 mb_set_bit(block, buddy);
1323 mb_set_bit(block + 1, buddy);
1324 }
1325 e4b->bd_info->bb_counters[order]--;
1326 e4b->bd_info->bb_counters[order]--;
1327
1328 block = block >> 1;
1329 order++;
1330 e4b->bd_info->bb_counters[order]++;
1331
1332 mb_clear_bit(block, buddy2);
1333 buddy = buddy2;
1334 } while (1);
1335 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001336 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001337 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001338}
1339
1340static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1341 int needed, struct ext4_free_extent *ex)
1342{
1343 int next = block;
1344 int max;
Alex Tomasc9de5602008-01-29 00:19:52 -05001345 void *buddy;
1346
Vincent Minetbc8e6742009-05-15 08:33:18 -04001347 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001348 BUG_ON(ex == NULL);
1349
1350 buddy = mb_find_buddy(e4b, order, &max);
1351 BUG_ON(buddy == NULL);
1352 BUG_ON(block >= max);
1353 if (mb_test_bit(block, buddy)) {
1354 ex->fe_len = 0;
1355 ex->fe_start = 0;
1356 ex->fe_group = 0;
1357 return 0;
1358 }
1359
1360 /* FIXME dorp order completely ? */
1361 if (likely(order == 0)) {
1362 /* find actual order */
1363 order = mb_find_order_for_block(e4b, block);
1364 block = block >> order;
1365 }
1366
1367 ex->fe_len = 1 << order;
1368 ex->fe_start = block << order;
1369 ex->fe_group = e4b->bd_group;
1370
1371 /* calc difference from given start */
1372 next = next - ex->fe_start;
1373 ex->fe_len -= next;
1374 ex->fe_start += next;
1375
1376 while (needed > ex->fe_len &&
1377 (buddy = mb_find_buddy(e4b, order, &max))) {
1378
1379 if (block + 1 >= max)
1380 break;
1381
1382 next = (block + 1) * (1 << order);
1383 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1384 break;
1385
Robin Dongb051d8d2011-10-26 05:30:30 -04001386 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001387
Alex Tomasc9de5602008-01-29 00:19:52 -05001388 block = next >> order;
1389 ex->fe_len += 1 << order;
1390 }
1391
1392 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1393 return ex->fe_len;
1394}
1395
1396static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1397{
1398 int ord;
1399 int mlen = 0;
1400 int max = 0;
1401 int cur;
1402 int start = ex->fe_start;
1403 int len = ex->fe_len;
1404 unsigned ret = 0;
1405 int len0 = len;
1406 void *buddy;
1407
1408 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1409 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001410 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001411 mb_check_buddy(e4b);
1412 mb_mark_used_double(e4b, start, len);
1413
1414 e4b->bd_info->bb_free -= len;
1415 if (e4b->bd_info->bb_first_free == start)
1416 e4b->bd_info->bb_first_free += len;
1417
1418 /* let's maintain fragments counter */
1419 if (start != 0)
1420 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1421 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1422 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1423 if (mlen && max)
1424 e4b->bd_info->bb_fragments++;
1425 else if (!mlen && !max)
1426 e4b->bd_info->bb_fragments--;
1427
1428 /* let's maintain buddy itself */
1429 while (len) {
1430 ord = mb_find_order_for_block(e4b, start);
1431
1432 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1433 /* the whole chunk may be allocated at once! */
1434 mlen = 1 << ord;
1435 buddy = mb_find_buddy(e4b, ord, &max);
1436 BUG_ON((start >> ord) >= max);
1437 mb_set_bit(start >> ord, buddy);
1438 e4b->bd_info->bb_counters[ord]--;
1439 start += mlen;
1440 len -= mlen;
1441 BUG_ON(len < 0);
1442 continue;
1443 }
1444
1445 /* store for history */
1446 if (ret == 0)
1447 ret = len | (ord << 16);
1448
1449 /* we have to split large buddy */
1450 BUG_ON(ord <= 0);
1451 buddy = mb_find_buddy(e4b, ord, &max);
1452 mb_set_bit(start >> ord, buddy);
1453 e4b->bd_info->bb_counters[ord]--;
1454
1455 ord--;
1456 cur = (start >> ord) & ~1U;
1457 buddy = mb_find_buddy(e4b, ord, &max);
1458 mb_clear_bit(cur, buddy);
1459 mb_clear_bit(cur + 1, buddy);
1460 e4b->bd_info->bb_counters[ord]++;
1461 e4b->bd_info->bb_counters[ord]++;
1462 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001463 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001464
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001465 ext4_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001466 mb_check_buddy(e4b);
1467
1468 return ret;
1469}
1470
1471/*
1472 * Must be called under group lock!
1473 */
1474static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1475 struct ext4_buddy *e4b)
1476{
1477 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1478 int ret;
1479
1480 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1481 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1482
1483 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1484 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1485 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1486
1487 /* preallocation can change ac_b_ex, thus we store actually
1488 * allocated blocks for history */
1489 ac->ac_f_ex = ac->ac_b_ex;
1490
1491 ac->ac_status = AC_STATUS_FOUND;
1492 ac->ac_tail = ret & 0xffff;
1493 ac->ac_buddy = ret >> 16;
1494
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001495 /*
1496 * take the page reference. We want the page to be pinned
1497 * so that we don't get a ext4_mb_init_cache_call for this
1498 * group until we update the bitmap. That would mean we
1499 * double allocate blocks. The reference is dropped
1500 * in ext4_mb_release_context
1501 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001502 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1503 get_page(ac->ac_bitmap_page);
1504 ac->ac_buddy_page = e4b->bd_buddy_page;
1505 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001506 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001507 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001508 spin_lock(&sbi->s_md_lock);
1509 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1510 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1511 spin_unlock(&sbi->s_md_lock);
1512 }
1513}
1514
1515/*
1516 * regular allocator, for general purposes allocation
1517 */
1518
1519static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1520 struct ext4_buddy *e4b,
1521 int finish_group)
1522{
1523 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1524 struct ext4_free_extent *bex = &ac->ac_b_ex;
1525 struct ext4_free_extent *gex = &ac->ac_g_ex;
1526 struct ext4_free_extent ex;
1527 int max;
1528
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001529 if (ac->ac_status == AC_STATUS_FOUND)
1530 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001531 /*
1532 * We don't want to scan for a whole year
1533 */
1534 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1535 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1536 ac->ac_status = AC_STATUS_BREAK;
1537 return;
1538 }
1539
1540 /*
1541 * Haven't found good chunk so far, let's continue
1542 */
1543 if (bex->fe_len < gex->fe_len)
1544 return;
1545
1546 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1547 && bex->fe_group == e4b->bd_group) {
1548 /* recheck chunk's availability - we don't know
1549 * when it was found (within this lock-unlock
1550 * period or not) */
1551 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1552 if (max >= gex->fe_len) {
1553 ext4_mb_use_best_found(ac, e4b);
1554 return;
1555 }
1556 }
1557}
1558
1559/*
1560 * The routine checks whether found extent is good enough. If it is,
1561 * then the extent gets marked used and flag is set to the context
1562 * to stop scanning. Otherwise, the extent is compared with the
1563 * previous found extent and if new one is better, then it's stored
1564 * in the context. Later, the best found extent will be used, if
1565 * mballoc can't find good enough extent.
1566 *
1567 * FIXME: real allocation policy is to be designed yet!
1568 */
1569static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1570 struct ext4_free_extent *ex,
1571 struct ext4_buddy *e4b)
1572{
1573 struct ext4_free_extent *bex = &ac->ac_b_ex;
1574 struct ext4_free_extent *gex = &ac->ac_g_ex;
1575
1576 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001577 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1578 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001579 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1580
1581 ac->ac_found++;
1582
1583 /*
1584 * The special case - take what you catch first
1585 */
1586 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1587 *bex = *ex;
1588 ext4_mb_use_best_found(ac, e4b);
1589 return;
1590 }
1591
1592 /*
1593 * Let's check whether the chuck is good enough
1594 */
1595 if (ex->fe_len == gex->fe_len) {
1596 *bex = *ex;
1597 ext4_mb_use_best_found(ac, e4b);
1598 return;
1599 }
1600
1601 /*
1602 * If this is first found extent, just store it in the context
1603 */
1604 if (bex->fe_len == 0) {
1605 *bex = *ex;
1606 return;
1607 }
1608
1609 /*
1610 * If new found extent is better, store it in the context
1611 */
1612 if (bex->fe_len < gex->fe_len) {
1613 /* if the request isn't satisfied, any found extent
1614 * larger than previous best one is better */
1615 if (ex->fe_len > bex->fe_len)
1616 *bex = *ex;
1617 } else if (ex->fe_len > gex->fe_len) {
1618 /* if the request is satisfied, then we try to find
1619 * an extent that still satisfy the request, but is
1620 * smaller than previous one */
1621 if (ex->fe_len < bex->fe_len)
1622 *bex = *ex;
1623 }
1624
1625 ext4_mb_check_limits(ac, e4b, 0);
1626}
1627
Eric Sandeen089ceec2009-07-05 22:17:31 -04001628static noinline_for_stack
1629int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001630 struct ext4_buddy *e4b)
1631{
1632 struct ext4_free_extent ex = ac->ac_b_ex;
1633 ext4_group_t group = ex.fe_group;
1634 int max;
1635 int err;
1636
1637 BUG_ON(ex.fe_len <= 0);
1638 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1639 if (err)
1640 return err;
1641
1642 ext4_lock_group(ac->ac_sb, group);
1643 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1644
1645 if (max > 0) {
1646 ac->ac_b_ex = ex;
1647 ext4_mb_use_best_found(ac, e4b);
1648 }
1649
1650 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001651 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001652
1653 return 0;
1654}
1655
Eric Sandeen089ceec2009-07-05 22:17:31 -04001656static noinline_for_stack
1657int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001658 struct ext4_buddy *e4b)
1659{
1660 ext4_group_t group = ac->ac_g_ex.fe_group;
1661 int max;
1662 int err;
1663 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001664 struct ext4_free_extent ex;
1665
1666 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1667 return 0;
1668
1669 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1670 if (err)
1671 return err;
1672
1673 ext4_lock_group(ac->ac_sb, group);
1674 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1675 ac->ac_g_ex.fe_len, &ex);
1676
1677 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1678 ext4_fsblk_t start;
1679
Akinobu Mita5661bd62010-03-03 23:53:39 -05001680 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1681 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001682 /* use do_div to get remainder (would be 64-bit modulo) */
1683 if (do_div(start, sbi->s_stripe) == 0) {
1684 ac->ac_found++;
1685 ac->ac_b_ex = ex;
1686 ext4_mb_use_best_found(ac, e4b);
1687 }
1688 } else if (max >= ac->ac_g_ex.fe_len) {
1689 BUG_ON(ex.fe_len <= 0);
1690 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1691 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1692 ac->ac_found++;
1693 ac->ac_b_ex = ex;
1694 ext4_mb_use_best_found(ac, e4b);
1695 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1696 /* Sometimes, caller may want to merge even small
1697 * number of blocks to an existing extent */
1698 BUG_ON(ex.fe_len <= 0);
1699 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1700 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1701 ac->ac_found++;
1702 ac->ac_b_ex = ex;
1703 ext4_mb_use_best_found(ac, e4b);
1704 }
1705 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001706 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001707
1708 return 0;
1709}
1710
1711/*
1712 * The routine scans buddy structures (not bitmap!) from given order
1713 * to max order and tries to find big enough chunk to satisfy the req
1714 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001715static noinline_for_stack
1716void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001717 struct ext4_buddy *e4b)
1718{
1719 struct super_block *sb = ac->ac_sb;
1720 struct ext4_group_info *grp = e4b->bd_info;
1721 void *buddy;
1722 int i;
1723 int k;
1724 int max;
1725
1726 BUG_ON(ac->ac_2order <= 0);
1727 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1728 if (grp->bb_counters[i] == 0)
1729 continue;
1730
1731 buddy = mb_find_buddy(e4b, i, &max);
1732 BUG_ON(buddy == NULL);
1733
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001734 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001735 BUG_ON(k >= max);
1736
1737 ac->ac_found++;
1738
1739 ac->ac_b_ex.fe_len = 1 << i;
1740 ac->ac_b_ex.fe_start = k << i;
1741 ac->ac_b_ex.fe_group = e4b->bd_group;
1742
1743 ext4_mb_use_best_found(ac, e4b);
1744
1745 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1746
1747 if (EXT4_SB(sb)->s_mb_stats)
1748 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1749
1750 break;
1751 }
1752}
1753
1754/*
1755 * The routine scans the group and measures all found extents.
1756 * In order to optimize scanning, caller must pass number of
1757 * free blocks in the group, so the routine can know upper limit.
1758 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001759static noinline_for_stack
1760void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001761 struct ext4_buddy *e4b)
1762{
1763 struct super_block *sb = ac->ac_sb;
1764 void *bitmap = EXT4_MB_BITMAP(e4b);
1765 struct ext4_free_extent ex;
1766 int i;
1767 int free;
1768
1769 free = e4b->bd_info->bb_free;
1770 BUG_ON(free <= 0);
1771
1772 i = e4b->bd_info->bb_first_free;
1773
1774 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001775 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001776 EXT4_CLUSTERS_PER_GROUP(sb), i);
1777 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001778 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001779 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001780 * free blocks even though group info says we
1781 * we have free blocks
1782 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001783 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001784 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001785 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001786 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001787 break;
1788 }
1789
1790 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1791 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001792 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001793 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001794 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001795 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001796 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001797 /*
1798 * The number of free blocks differs. This mostly
1799 * indicate that the bitmap is corrupt. So exit
1800 * without claiming the space.
1801 */
1802 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001803 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001804
1805 ext4_mb_measure_extent(ac, &ex, e4b);
1806
1807 i += ex.fe_len;
1808 free -= ex.fe_len;
1809 }
1810
1811 ext4_mb_check_limits(ac, e4b, 1);
1812}
1813
1814/*
1815 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001816 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001817 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001818static noinline_for_stack
1819void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001820 struct ext4_buddy *e4b)
1821{
1822 struct super_block *sb = ac->ac_sb;
1823 struct ext4_sb_info *sbi = EXT4_SB(sb);
1824 void *bitmap = EXT4_MB_BITMAP(e4b);
1825 struct ext4_free_extent ex;
1826 ext4_fsblk_t first_group_block;
1827 ext4_fsblk_t a;
1828 ext4_grpblk_t i;
1829 int max;
1830
1831 BUG_ON(sbi->s_stripe == 0);
1832
1833 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001834 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1835
Alex Tomasc9de5602008-01-29 00:19:52 -05001836 a = first_group_block + sbi->s_stripe - 1;
1837 do_div(a, sbi->s_stripe);
1838 i = (a * sbi->s_stripe) - first_group_block;
1839
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001840 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001841 if (!mb_test_bit(i, bitmap)) {
1842 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1843 if (max >= sbi->s_stripe) {
1844 ac->ac_found++;
1845 ac->ac_b_ex = ex;
1846 ext4_mb_use_best_found(ac, e4b);
1847 break;
1848 }
1849 }
1850 i += sbi->s_stripe;
1851 }
1852}
1853
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001854/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001855static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1856 ext4_group_t group, int cr)
1857{
1858 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001859 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001860 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1861
1862 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001863
1864 /* We only do this if the grp has never been initialized */
1865 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1866 int ret = ext4_mb_init_group(ac->ac_sb, group);
1867 if (ret)
1868 return 0;
1869 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001870
1871 free = grp->bb_free;
1872 fragments = grp->bb_fragments;
1873 if (free == 0)
1874 return 0;
1875 if (fragments == 0)
1876 return 0;
1877
1878 switch (cr) {
1879 case 0:
1880 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001881
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001882 if (grp->bb_largest_free_order < ac->ac_2order)
1883 return 0;
1884
Theodore Ts'oa4912122009-03-12 12:18:34 -04001885 /* Avoid using the first bg of a flexgroup for data files */
1886 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1887 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1888 ((group % flex_size) == 0))
1889 return 0;
1890
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001891 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001892 case 1:
1893 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1894 return 1;
1895 break;
1896 case 2:
1897 if (free >= ac->ac_g_ex.fe_len)
1898 return 1;
1899 break;
1900 case 3:
1901 return 1;
1902 default:
1903 BUG();
1904 }
1905
1906 return 0;
1907}
1908
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001909static noinline_for_stack int
1910ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001911{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001912 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001913 int cr;
1914 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001915 struct ext4_sb_info *sbi;
1916 struct super_block *sb;
1917 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001918
1919 sb = ac->ac_sb;
1920 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001921 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001922 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001923 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001924 ngroups = sbi->s_blockfile_groups;
1925
Alex Tomasc9de5602008-01-29 00:19:52 -05001926 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1927
1928 /* first, try the goal */
1929 err = ext4_mb_find_by_goal(ac, &e4b);
1930 if (err || ac->ac_status == AC_STATUS_FOUND)
1931 goto out;
1932
1933 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1934 goto out;
1935
1936 /*
1937 * ac->ac2_order is set only if the fe_len is a power of 2
1938 * if ac2_order is set we also set criteria to 0 so that we
1939 * try exact allocation using buddy.
1940 */
1941 i = fls(ac->ac_g_ex.fe_len);
1942 ac->ac_2order = 0;
1943 /*
1944 * We search using buddy data only if the order of the request
1945 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04001946 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05001947 */
1948 if (i >= sbi->s_mb_order2_reqs) {
1949 /*
1950 * This should tell if fe_len is exactly power of 2
1951 */
1952 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1953 ac->ac_2order = i - 1;
1954 }
1955
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001956 /* if stream allocation is enabled, use global goal */
1957 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001958 /* TBD: may be hot point */
1959 spin_lock(&sbi->s_md_lock);
1960 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
1961 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
1962 spin_unlock(&sbi->s_md_lock);
1963 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001964
Alex Tomasc9de5602008-01-29 00:19:52 -05001965 /* Let's just scan groups to find more-less suitable blocks */
1966 cr = ac->ac_2order ? 0 : 1;
1967 /*
1968 * cr == 0 try to get exact allocation,
1969 * cr == 3 try to get anything
1970 */
1971repeat:
1972 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
1973 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04001974 /*
1975 * searching for the right group start
1976 * from the goal value specified
1977 */
1978 group = ac->ac_g_ex.fe_group;
1979
Theodore Ts'o8df96752009-05-01 08:50:38 -04001980 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04001981 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05001982 group = 0;
1983
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001984 /* This now checks without needing the buddy page */
1985 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05001986 continue;
1987
Alex Tomasc9de5602008-01-29 00:19:52 -05001988 err = ext4_mb_load_buddy(sb, group, &e4b);
1989 if (err)
1990 goto out;
1991
1992 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001993
1994 /*
1995 * We need to check again after locking the
1996 * block group
1997 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001998 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001999 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002000 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002001 continue;
2002 }
2003
2004 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002005 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002006 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002007 else if (cr == 1 && sbi->s_stripe &&
2008 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002009 ext4_mb_scan_aligned(ac, &e4b);
2010 else
2011 ext4_mb_complex_scan_group(ac, &e4b);
2012
2013 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002014 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002015
2016 if (ac->ac_status != AC_STATUS_CONTINUE)
2017 break;
2018 }
2019 }
2020
2021 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2022 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2023 /*
2024 * We've been searching too long. Let's try to allocate
2025 * the best chunk we've found so far
2026 */
2027
2028 ext4_mb_try_best_found(ac, &e4b);
2029 if (ac->ac_status != AC_STATUS_FOUND) {
2030 /*
2031 * Someone more lucky has already allocated it.
2032 * The only thing we can do is just take first
2033 * found block(s)
2034 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2035 */
2036 ac->ac_b_ex.fe_group = 0;
2037 ac->ac_b_ex.fe_start = 0;
2038 ac->ac_b_ex.fe_len = 0;
2039 ac->ac_status = AC_STATUS_CONTINUE;
2040 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2041 cr = 3;
2042 atomic_inc(&sbi->s_mb_lost_chunks);
2043 goto repeat;
2044 }
2045 }
2046out:
2047 return err;
2048}
2049
Alex Tomasc9de5602008-01-29 00:19:52 -05002050static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2051{
2052 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002053 ext4_group_t group;
2054
Theodore Ts'o8df96752009-05-01 08:50:38 -04002055 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002056 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002057 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002058 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002059}
2060
2061static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2062{
2063 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002064 ext4_group_t group;
2065
2066 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002067 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002068 return NULL;
2069 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002070 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002071}
2072
2073static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2074{
2075 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002076 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002077 int i;
2078 int err;
2079 struct ext4_buddy e4b;
2080 struct sg {
2081 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002082 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002083 } sg;
2084
2085 group--;
2086 if (group == 0)
2087 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2088 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2089 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2090 "group", "free", "frags", "first",
2091 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2092 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2093
2094 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2095 sizeof(struct ext4_group_info);
2096 err = ext4_mb_load_buddy(sb, group, &e4b);
2097 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002098 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002099 return 0;
2100 }
2101 ext4_lock_group(sb, group);
2102 memcpy(&sg, ext4_get_group_info(sb, group), i);
2103 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002104 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002105
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002106 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002107 sg.info.bb_fragments, sg.info.bb_first_free);
2108 for (i = 0; i <= 13; i++)
2109 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2110 sg.info.bb_counters[i] : 0);
2111 seq_printf(seq, " ]\n");
2112
2113 return 0;
2114}
2115
2116static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2117{
2118}
2119
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002120static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002121 .start = ext4_mb_seq_groups_start,
2122 .next = ext4_mb_seq_groups_next,
2123 .stop = ext4_mb_seq_groups_stop,
2124 .show = ext4_mb_seq_groups_show,
2125};
2126
2127static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2128{
2129 struct super_block *sb = PDE(inode)->data;
2130 int rc;
2131
2132 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2133 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002134 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002135 m->private = sb;
2136 }
2137 return rc;
2138
2139}
2140
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002141static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002142 .owner = THIS_MODULE,
2143 .open = ext4_mb_seq_groups_open,
2144 .read = seq_read,
2145 .llseek = seq_lseek,
2146 .release = seq_release,
2147};
2148
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002149static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2150{
2151 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2152 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2153
2154 BUG_ON(!cachep);
2155 return cachep;
2156}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002157
2158/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002159int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002160 struct ext4_group_desc *desc)
2161{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002162 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002163 int metalen = 0;
2164 struct ext4_sb_info *sbi = EXT4_SB(sb);
2165 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002166 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002167
2168 /*
2169 * First check if this group is the first of a reserved block.
2170 * If it's true, we have to allocate a new table of pointers
2171 * to ext4_group_info structures
2172 */
2173 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2174 metalen = sizeof(*meta_group_info) <<
2175 EXT4_DESC_PER_BLOCK_BITS(sb);
2176 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2177 if (meta_group_info == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002178 ext4_msg(sb, KERN_ERR, "EXT4-fs: can't allocate mem "
2179 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002180 goto exit_meta_group_info;
2181 }
2182 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2183 meta_group_info;
2184 }
2185
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002186 meta_group_info =
2187 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2188 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2189
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002190 meta_group_info[i] = kmem_cache_alloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002191 if (meta_group_info[i] == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002192 ext4_msg(sb, KERN_ERR, "EXT4-fs: can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002193 goto exit_group_info;
2194 }
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002195 memset(meta_group_info[i], 0, kmem_cache_size(cachep));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002196 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2197 &(meta_group_info[i]->bb_state));
2198
2199 /*
2200 * initialize bb_free to be able to skip
2201 * empty groups without initialization
2202 */
2203 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2204 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002205 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002206 } else {
2207 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002208 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002209 }
2210
2211 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002212 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002213 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002214 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002215
2216#ifdef DOUBLE_CHECK
2217 {
2218 struct buffer_head *bh;
2219 meta_group_info[i]->bb_bitmap =
2220 kmalloc(sb->s_blocksize, GFP_KERNEL);
2221 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2222 bh = ext4_read_block_bitmap(sb, group);
2223 BUG_ON(bh == NULL);
2224 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2225 sb->s_blocksize);
2226 put_bh(bh);
2227 }
2228#endif
2229
2230 return 0;
2231
2232exit_group_info:
2233 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002234 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002235 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002236 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2237 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002238exit_meta_group_info:
2239 return -ENOMEM;
2240} /* ext4_mb_add_groupinfo */
2241
Alex Tomasc9de5602008-01-29 00:19:52 -05002242static int ext4_mb_init_backend(struct super_block *sb)
2243{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002244 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002245 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002246 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002247 struct ext4_super_block *es = sbi->s_es;
2248 int num_meta_group_infos;
2249 int num_meta_group_infos_max;
2250 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002251 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002252 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002253
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002254 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002255 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002256 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2257
2258 /*
2259 * This is the total number of blocks used by GDT including
2260 * the number of reserved blocks for GDT.
2261 * The s_group_info array is allocated with this value
2262 * to allow a clean online resize without a complex
2263 * manipulation of pointer.
2264 * The drawback is the unused memory when no resize
2265 * occurs but it's very low in terms of pages
2266 * (see comments below)
2267 * Need to handle this properly when META_BG resizing is allowed
2268 */
2269 num_meta_group_infos_max = num_meta_group_infos +
2270 le16_to_cpu(es->s_reserved_gdt_blocks);
2271
2272 /*
2273 * array_size is the size of s_group_info array. We round it
2274 * to the next power of two because this approximation is done
2275 * internally by kmalloc so we can have some more memory
2276 * for free here (e.g. may be used for META_BG resize).
2277 */
2278 array_size = 1;
2279 while (array_size < sizeof(*sbi->s_group_info) *
2280 num_meta_group_infos_max)
2281 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002282 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2283 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2284 * So a two level scheme suffices for now. */
Theodore Ts'of18a5f22011-08-01 08:45:38 -04002285 sbi->s_group_info = ext4_kvzalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002286 if (sbi->s_group_info == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002287 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
Alex Tomasc9de5602008-01-29 00:19:52 -05002288 return -ENOMEM;
2289 }
2290 sbi->s_buddy_cache = new_inode(sb);
2291 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002292 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002293 goto err_freesgi;
2294 }
Yu Jian48e60612011-08-01 17:41:39 -04002295 /* To avoid potentially colliding with an valid on-disk inode number,
2296 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2297 * not in the inode hash, so it should never be found by iget(), but
2298 * this will avoid confusion if it ever shows up during debugging. */
2299 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002300 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002301 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002302 desc = ext4_get_group_desc(sb, i, NULL);
2303 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002304 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002305 goto err_freebuddy;
2306 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002307 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2308 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002309 }
2310
2311 return 0;
2312
2313err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002314 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002315 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002316 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002317 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002318 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002319 kfree(sbi->s_group_info[i]);
2320 iput(sbi->s_buddy_cache);
2321err_freesgi:
Theodore Ts'of18a5f22011-08-01 08:45:38 -04002322 ext4_kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002323 return -ENOMEM;
2324}
2325
Eric Sandeen2892c152011-02-12 08:12:18 -05002326static void ext4_groupinfo_destroy_slabs(void)
2327{
2328 int i;
2329
2330 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2331 if (ext4_groupinfo_caches[i])
2332 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2333 ext4_groupinfo_caches[i] = NULL;
2334 }
2335}
2336
2337static int ext4_groupinfo_create_slab(size_t size)
2338{
2339 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2340 int slab_size;
2341 int blocksize_bits = order_base_2(size);
2342 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2343 struct kmem_cache *cachep;
2344
2345 if (cache_index >= NR_GRPINFO_CACHES)
2346 return -EINVAL;
2347
2348 if (unlikely(cache_index < 0))
2349 cache_index = 0;
2350
2351 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2352 if (ext4_groupinfo_caches[cache_index]) {
2353 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2354 return 0; /* Already created */
2355 }
2356
2357 slab_size = offsetof(struct ext4_group_info,
2358 bb_counters[blocksize_bits + 2]);
2359
2360 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2361 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2362 NULL);
2363
Tao Ma823ba012011-07-11 18:26:01 -04002364 ext4_groupinfo_caches[cache_index] = cachep;
2365
Eric Sandeen2892c152011-02-12 08:12:18 -05002366 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2367 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002368 printk(KERN_EMERG
2369 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002370 return -ENOMEM;
2371 }
2372
Eric Sandeen2892c152011-02-12 08:12:18 -05002373 return 0;
2374}
2375
Alex Tomasc9de5602008-01-29 00:19:52 -05002376int ext4_mb_init(struct super_block *sb, int needs_recovery)
2377{
2378 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002379 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002380 unsigned offset;
2381 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002382 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002383
Eric Sandeen19278052009-08-25 22:36:25 -04002384 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002385
2386 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2387 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002388 ret = -ENOMEM;
2389 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002390 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002391
Eric Sandeen19278052009-08-25 22:36:25 -04002392 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002393 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2394 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002395 ret = -ENOMEM;
2396 goto out;
2397 }
2398
Eric Sandeen2892c152011-02-12 08:12:18 -05002399 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2400 if (ret < 0)
2401 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002402
2403 /* order 0 is regular bitmap */
2404 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2405 sbi->s_mb_offsets[0] = 0;
2406
2407 i = 1;
2408 offset = 0;
2409 max = sb->s_blocksize << 2;
2410 do {
2411 sbi->s_mb_offsets[i] = offset;
2412 sbi->s_mb_maxs[i] = max;
2413 offset += 1 << (sb->s_blocksize_bits - i);
2414 max = max >> 1;
2415 i++;
2416 } while (i <= sb->s_blocksize_bits + 1);
2417
Alex Tomasc9de5602008-01-29 00:19:52 -05002418 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002419 spin_lock_init(&sbi->s_bal_lock);
2420
2421 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2422 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2423 sbi->s_mb_stats = MB_DEFAULT_STATS;
2424 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2425 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002426 /*
2427 * The default group preallocation is 512, which for 4k block
2428 * sizes translates to 2 megabytes. However for bigalloc file
2429 * systems, this is probably too big (i.e, if the cluster size
2430 * is 1 megabyte, then group preallocation size becomes half a
2431 * gigabyte!). As a default, we will keep a two megabyte
2432 * group pralloc size for cluster sizes up to 64k, and after
2433 * that, we will force a minimum group preallocation size of
2434 * 32 clusters. This translates to 8 megs when the cluster
2435 * size is 256k, and 32 megs when the cluster size is 1 meg,
2436 * which seems reasonable as a default.
2437 */
2438 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2439 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002440 /*
2441 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2442 * to the lowest multiple of s_stripe which is bigger than
2443 * the s_mb_group_prealloc as determined above. We want
2444 * the preallocation size to be an exact multiple of the
2445 * RAID stripe size so that preallocations don't fragment
2446 * the stripes.
2447 */
2448 if (sbi->s_stripe > 1) {
2449 sbi->s_mb_group_prealloc = roundup(
2450 sbi->s_mb_group_prealloc, sbi->s_stripe);
2451 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002452
Eric Sandeen730c2132008-09-13 15:23:29 -04002453 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002454 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002455 ret = -ENOMEM;
Tao Ma7aa0bae2011-10-06 10:22:28 -04002456 goto out_free_groupinfo_slab;
Alex Tomasc9de5602008-01-29 00:19:52 -05002457 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002458 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002459 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002460 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002461 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002462 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2463 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002464 spin_lock_init(&lg->lg_prealloc_lock);
2465 }
2466
Yu Jian79a77c52011-08-01 17:41:46 -04002467 /* init file for buddy data */
2468 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002469 if (ret != 0)
2470 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002471
Theodore Ts'o296c3552009-09-30 00:32:42 -04002472 if (sbi->s_proc)
2473 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2474 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002475
Frank Mayhar03901312009-01-07 00:06:22 -05002476 if (sbi->s_journal)
2477 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Tao Ma7aa0bae2011-10-06 10:22:28 -04002478
2479 return 0;
2480
2481out_free_locality_groups:
2482 free_percpu(sbi->s_locality_groups);
2483 sbi->s_locality_groups = NULL;
2484out_free_groupinfo_slab:
2485 ext4_groupinfo_destroy_slabs();
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002486out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002487 kfree(sbi->s_mb_offsets);
2488 sbi->s_mb_offsets = NULL;
2489 kfree(sbi->s_mb_maxs);
2490 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002491 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002492}
2493
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002494/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002495static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2496{
2497 struct ext4_prealloc_space *pa;
2498 struct list_head *cur, *tmp;
2499 int count = 0;
2500
2501 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2502 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2503 list_del(&pa->pa_group_list);
2504 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002505 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002506 }
2507 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002508 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002509
2510}
2511
2512int ext4_mb_release(struct super_block *sb)
2513{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002514 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002515 ext4_group_t i;
2516 int num_meta_group_infos;
2517 struct ext4_group_info *grinfo;
2518 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002519 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002520
Alex Tomasc9de5602008-01-29 00:19:52 -05002521 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002522 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002523 grinfo = ext4_get_group_info(sb, i);
2524#ifdef DOUBLE_CHECK
2525 kfree(grinfo->bb_bitmap);
2526#endif
2527 ext4_lock_group(sb, i);
2528 ext4_mb_cleanup_pa(grinfo);
2529 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002530 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002531 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002532 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002533 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2534 EXT4_DESC_PER_BLOCK_BITS(sb);
2535 for (i = 0; i < num_meta_group_infos; i++)
2536 kfree(sbi->s_group_info[i]);
Theodore Ts'of18a5f22011-08-01 08:45:38 -04002537 ext4_kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002538 }
2539 kfree(sbi->s_mb_offsets);
2540 kfree(sbi->s_mb_maxs);
2541 if (sbi->s_buddy_cache)
2542 iput(sbi->s_buddy_cache);
2543 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002544 ext4_msg(sb, KERN_INFO,
2545 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002546 atomic_read(&sbi->s_bal_allocated),
2547 atomic_read(&sbi->s_bal_reqs),
2548 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002549 ext4_msg(sb, KERN_INFO,
2550 "mballoc: %u extents scanned, %u goal hits, "
2551 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002552 atomic_read(&sbi->s_bal_ex_scanned),
2553 atomic_read(&sbi->s_bal_goals),
2554 atomic_read(&sbi->s_bal_2orders),
2555 atomic_read(&sbi->s_bal_breaks),
2556 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002557 ext4_msg(sb, KERN_INFO,
2558 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002559 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002560 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002561 ext4_msg(sb, KERN_INFO,
2562 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002563 atomic_read(&sbi->s_mb_preallocated),
2564 atomic_read(&sbi->s_mb_discarded));
2565 }
2566
Eric Sandeen730c2132008-09-13 15:23:29 -04002567 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002568 if (sbi->s_proc)
2569 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002570
2571 return 0;
2572}
2573
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002574static inline int ext4_issue_discard(struct super_block *sb,
Theodore Ts'o84130192011-09-09 18:50:51 -04002575 ext4_group_t block_group, ext4_grpblk_t cluster, int count)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002576{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002577 ext4_fsblk_t discard_block;
2578
Theodore Ts'o84130192011-09-09 18:50:51 -04002579 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2580 ext4_group_first_block_no(sb, block_group));
2581 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002582 trace_ext4_discard_blocks(sb,
2583 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002584 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002585}
2586
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002587/*
2588 * This function is called by the jbd2 layer once the commit has finished,
2589 * so we know we can free the blocks that were released with that commit.
2590 */
2591static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002592{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002593 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002594 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002595 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002596 int err, count = 0, count2 = 0;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002597 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002598 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002599
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002600 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2601 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002602
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002603 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002604 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002605
Theodore Ts'od9f34502011-04-30 13:47:24 -04002606 if (test_opt(sb, DISCARD))
2607 ext4_issue_discard(sb, entry->group,
Theodore Ts'o84130192011-09-09 18:50:51 -04002608 entry->start_cluster, entry->count);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002609
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002610 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002611 /* we expect to find existing buddy because it's pinned */
2612 BUG_ON(err != 0);
2613
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002614 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002615 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002616 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002617 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002618 ext4_lock_group(sb, entry->group);
2619 /* Take it out of per group rb tree */
2620 rb_erase(&entry->node, &(db->bb_free_root));
Theodore Ts'o84130192011-09-09 18:50:51 -04002621 mb_free_blocks(NULL, &e4b, entry->start_cluster, entry->count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002622
Tao Ma3d56b8d2011-07-11 00:03:38 -04002623 /*
2624 * Clear the trimmed flag for the group so that the next
2625 * ext4_trim_fs can trim it.
2626 * If the volume is mounted with -o discard, online discard
2627 * is supported and the free blocks will be trimmed online.
2628 */
2629 if (!test_opt(sb, DISCARD))
2630 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2631
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002632 if (!db->bb_free_root.rb_node) {
2633 /* No more items in the per group rb tree
2634 * balance refcounts from ext4_mb_free_metadata()
2635 */
2636 page_cache_release(e4b.bd_buddy_page);
2637 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002638 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002639 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002640 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002641 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002642 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002643
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002644 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002645}
2646
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002647#ifdef CONFIG_EXT4_DEBUG
2648u8 mb_enable_debug __read_mostly;
2649
2650static struct dentry *debugfs_dir;
2651static struct dentry *debugfs_debug;
2652
2653static void __init ext4_create_debugfs_entry(void)
2654{
2655 debugfs_dir = debugfs_create_dir("ext4", NULL);
2656 if (debugfs_dir)
2657 debugfs_debug = debugfs_create_u8("mballoc-debug",
2658 S_IRUGO | S_IWUSR,
2659 debugfs_dir,
2660 &mb_enable_debug);
2661}
2662
2663static void ext4_remove_debugfs_entry(void)
2664{
2665 debugfs_remove(debugfs_debug);
2666 debugfs_remove(debugfs_dir);
2667}
2668
2669#else
2670
2671static void __init ext4_create_debugfs_entry(void)
2672{
2673}
2674
2675static void ext4_remove_debugfs_entry(void)
2676{
2677}
2678
2679#endif
2680
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002681int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002682{
Theodore Ts'o16828082010-10-27 21:30:09 -04002683 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2684 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002685 if (ext4_pspace_cachep == NULL)
2686 return -ENOMEM;
2687
Theodore Ts'o16828082010-10-27 21:30:09 -04002688 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2689 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002690 if (ext4_ac_cachep == NULL) {
2691 kmem_cache_destroy(ext4_pspace_cachep);
2692 return -ENOMEM;
2693 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002694
Theodore Ts'o16828082010-10-27 21:30:09 -04002695 ext4_free_ext_cachep = KMEM_CACHE(ext4_free_data,
2696 SLAB_RECLAIM_ACCOUNT);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002697 if (ext4_free_ext_cachep == NULL) {
2698 kmem_cache_destroy(ext4_pspace_cachep);
2699 kmem_cache_destroy(ext4_ac_cachep);
2700 return -ENOMEM;
2701 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002702 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002703 return 0;
2704}
2705
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002706void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002707{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002708 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002709 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2710 * before destroying the slab cache.
2711 */
2712 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002713 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002714 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002715 kmem_cache_destroy(ext4_free_ext_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002716 ext4_groupinfo_destroy_slabs();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002717 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002718}
2719
2720
2721/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002722 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002723 * Returns 0 if success or error code
2724 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002725static noinline_for_stack int
2726ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002727 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002728{
2729 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002730 struct ext4_group_desc *gdp;
2731 struct buffer_head *gdp_bh;
2732 struct ext4_sb_info *sbi;
2733 struct super_block *sb;
2734 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002735 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002736
2737 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2738 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2739
2740 sb = ac->ac_sb;
2741 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002742
2743 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002744 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002745 if (!bitmap_bh)
2746 goto out_err;
2747
2748 err = ext4_journal_get_write_access(handle, bitmap_bh);
2749 if (err)
2750 goto out_err;
2751
2752 err = -EIO;
2753 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2754 if (!gdp)
2755 goto out_err;
2756
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002757 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002758 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002759
Alex Tomasc9de5602008-01-29 00:19:52 -05002760 err = ext4_journal_get_write_access(handle, gdp_bh);
2761 if (err)
2762 goto out_err;
2763
Akinobu Mitabda00de2010-03-03 23:53:25 -05002764 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002765
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002766 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002767 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002768 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002769 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002770 /* File system mounted not to panic on error
2771 * Fix the bitmap and repeat the block allocation
2772 * We leak some of the blocks here.
2773 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002774 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002775 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2776 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002777 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002778 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002779 if (!err)
2780 err = -EAGAIN;
2781 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002782 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002783
2784 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002785#ifdef AGGRESSIVE_CHECK
2786 {
2787 int i;
2788 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2789 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2790 bitmap_bh->b_data));
2791 }
2792 }
2793#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002794 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2795 ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002796 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2797 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002798 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002799 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002800 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002801 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002802 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
2803 ext4_free_group_clusters_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002804 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002805
2806 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04002807 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002808 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002809 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002810 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002811 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2812 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04002813 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
2814 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002815
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002816 if (sbi->s_log_groups_per_flex) {
2817 ext4_group_t flex_group = ext4_flex_group(sbi,
2818 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002819 atomic_sub(ac->ac_b_ex.fe_len,
Theodore Ts'o24aaa8e2011-09-09 18:58:51 -04002820 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002821 }
2822
Frank Mayhar03901312009-01-07 00:06:22 -05002823 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002824 if (err)
2825 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002826 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002827
2828out_err:
Theodore Ts'oa0375152010-06-11 23:14:04 -04002829 ext4_mark_super_dirty(sb);
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002830 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002831 return err;
2832}
2833
2834/*
2835 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002836 * Group request are normalized to s_mb_group_prealloc, which goes to
2837 * s_strip if we set the same via mount option.
2838 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002839 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002840 *
2841 * XXX: should we try to preallocate more than the group has now?
2842 */
2843static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2844{
2845 struct super_block *sb = ac->ac_sb;
2846 struct ext4_locality_group *lg = ac->ac_lg;
2847
2848 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002849 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002850 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002851 current->pid, ac->ac_g_ex.fe_len);
2852}
2853
2854/*
2855 * Normalization means making request better in terms of
2856 * size and alignment
2857 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002858static noinline_for_stack void
2859ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002860 struct ext4_allocation_request *ar)
2861{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002862 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002863 int bsbits, max;
2864 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002865 loff_t size, orig_size, start_off;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002866 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002867 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002868 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002869
2870 /* do normalize only data requests, metadata requests
2871 do not need preallocation */
2872 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2873 return;
2874
2875 /* sometime caller may want exact blocks */
2876 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2877 return;
2878
2879 /* caller may indicate that preallocation isn't
2880 * required (it's a tail, for example) */
2881 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2882 return;
2883
2884 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2885 ext4_mb_normalize_group_request(ac);
2886 return ;
2887 }
2888
2889 bsbits = ac->ac_sb->s_blocksize_bits;
2890
2891 /* first, let's learn actual file size
2892 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002893 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002894 size = size << bsbits;
2895 if (size < i_size_read(ac->ac_inode))
2896 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002897 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002898
Valerie Clement19304792008-05-13 19:31:14 -04002899 /* max size of free chunks */
2900 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002901
Valerie Clement19304792008-05-13 19:31:14 -04002902#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2903 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002904
2905 /* first, try to predict filesize */
2906 /* XXX: should this table be tunable? */
2907 start_off = 0;
2908 if (size <= 16 * 1024) {
2909 size = 16 * 1024;
2910 } else if (size <= 32 * 1024) {
2911 size = 32 * 1024;
2912 } else if (size <= 64 * 1024) {
2913 size = 64 * 1024;
2914 } else if (size <= 128 * 1024) {
2915 size = 128 * 1024;
2916 } else if (size <= 256 * 1024) {
2917 size = 256 * 1024;
2918 } else if (size <= 512 * 1024) {
2919 size = 512 * 1024;
2920 } else if (size <= 1024 * 1024) {
2921 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002922 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002923 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002924 (21 - bsbits)) << 21;
2925 size = 2 * 1024 * 1024;
2926 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002927 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2928 (22 - bsbits)) << 22;
2929 size = 4 * 1024 * 1024;
2930 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002931 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002932 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2933 (23 - bsbits)) << 23;
2934 size = 8 * 1024 * 1024;
2935 } else {
2936 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2937 size = ac->ac_o_ex.fe_len << bsbits;
2938 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002939 size = size >> bsbits;
2940 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002941
2942 /* don't cover already allocated blocks in selected range */
2943 if (ar->pleft && start <= ar->lleft) {
2944 size -= ar->lleft + 1 - start;
2945 start = ar->lleft + 1;
2946 }
2947 if (ar->pright && start + size - 1 >= ar->lright)
2948 size -= start + size - ar->lright;
2949
2950 end = start + size;
2951
2952 /* check we don't cross already preallocated blocks */
2953 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002954 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002955 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002956
Alex Tomasc9de5602008-01-29 00:19:52 -05002957 if (pa->pa_deleted)
2958 continue;
2959 spin_lock(&pa->pa_lock);
2960 if (pa->pa_deleted) {
2961 spin_unlock(&pa->pa_lock);
2962 continue;
2963 }
2964
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002965 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
2966 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002967
2968 /* PA must not overlap original request */
2969 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2970 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2971
Eric Sandeen38877f42009-08-17 23:55:24 -04002972 /* skip PAs this normalized request doesn't overlap with */
2973 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002974 spin_unlock(&pa->pa_lock);
2975 continue;
2976 }
2977 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2978
Eric Sandeen38877f42009-08-17 23:55:24 -04002979 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05002980 if (pa_end <= ac->ac_o_ex.fe_logical) {
2981 BUG_ON(pa_end < start);
2982 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04002983 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002984 BUG_ON(pa->pa_lstart > end);
2985 end = pa->pa_lstart;
2986 }
2987 spin_unlock(&pa->pa_lock);
2988 }
2989 rcu_read_unlock();
2990 size = end - start;
2991
2992 /* XXX: extra loop to check we really don't overlap preallocations */
2993 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002994 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002995 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002996
Alex Tomasc9de5602008-01-29 00:19:52 -05002997 spin_lock(&pa->pa_lock);
2998 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002999 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3000 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003001 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3002 }
3003 spin_unlock(&pa->pa_lock);
3004 }
3005 rcu_read_unlock();
3006
3007 if (start + size <= ac->ac_o_ex.fe_logical &&
3008 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003009 ext4_msg(ac->ac_sb, KERN_ERR,
3010 "start %lu, size %lu, fe_logical %lu",
3011 (unsigned long) start, (unsigned long) size,
3012 (unsigned long) ac->ac_o_ex.fe_logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05003013 }
3014 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3015 start > ac->ac_o_ex.fe_logical);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003016 BUG_ON(size <= 0 || size > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003017
3018 /* now prepare goal request */
3019
3020 /* XXX: is it better to align blocks WRT to logical
3021 * placement or satisfy big request as is */
3022 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003023 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003024
3025 /* define goal start in order to merge */
3026 if (ar->pright && (ar->lright == (start + size))) {
3027 /* merge to the right */
3028 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3029 &ac->ac_f_ex.fe_group,
3030 &ac->ac_f_ex.fe_start);
3031 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3032 }
3033 if (ar->pleft && (ar->lleft + 1 == start)) {
3034 /* merge to the left */
3035 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3036 &ac->ac_f_ex.fe_group,
3037 &ac->ac_f_ex.fe_start);
3038 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3039 }
3040
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003041 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003042 (unsigned) orig_size, (unsigned) start);
3043}
3044
3045static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3046{
3047 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3048
3049 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3050 atomic_inc(&sbi->s_bal_reqs);
3051 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003052 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003053 atomic_inc(&sbi->s_bal_success);
3054 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3055 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3056 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3057 atomic_inc(&sbi->s_bal_goals);
3058 if (ac->ac_found > sbi->s_mb_max_to_scan)
3059 atomic_inc(&sbi->s_bal_breaks);
3060 }
3061
Theodore Ts'o296c3552009-09-30 00:32:42 -04003062 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3063 trace_ext4_mballoc_alloc(ac);
3064 else
3065 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003066}
3067
3068/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003069 * Called on failure; free up any blocks from the inode PA for this
3070 * context. We don't need this for MB_GROUP_PA because we only change
3071 * pa_free in ext4_mb_release_context(), but on failure, we've already
3072 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3073 */
3074static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3075{
3076 struct ext4_prealloc_space *pa = ac->ac_pa;
3077 int len;
3078
3079 if (pa && pa->pa_type == MB_INODE_PA) {
3080 len = ac->ac_b_ex.fe_len;
3081 pa->pa_free += len;
3082 }
3083
3084}
3085
3086/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003087 * use blocks preallocated to inode
3088 */
3089static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3090 struct ext4_prealloc_space *pa)
3091{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003092 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003093 ext4_fsblk_t start;
3094 ext4_fsblk_t end;
3095 int len;
3096
3097 /* found preallocated blocks, use them */
3098 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003099 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3100 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3101 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003102 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3103 &ac->ac_b_ex.fe_start);
3104 ac->ac_b_ex.fe_len = len;
3105 ac->ac_status = AC_STATUS_FOUND;
3106 ac->ac_pa = pa;
3107
3108 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003109 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003110 BUG_ON(pa->pa_free < len);
3111 pa->pa_free -= len;
3112
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003113 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003114}
3115
3116/*
3117 * use blocks preallocated to locality group
3118 */
3119static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3120 struct ext4_prealloc_space *pa)
3121{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003122 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003123
Alex Tomasc9de5602008-01-29 00:19:52 -05003124 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3125 &ac->ac_b_ex.fe_group,
3126 &ac->ac_b_ex.fe_start);
3127 ac->ac_b_ex.fe_len = len;
3128 ac->ac_status = AC_STATUS_FOUND;
3129 ac->ac_pa = pa;
3130
3131 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003132 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003133 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003134 * in on-disk bitmap -- see ext4_mb_release_context()
3135 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003136 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003137 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003138}
3139
3140/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003141 * Return the prealloc space that have minimal distance
3142 * from the goal block. @cpa is the prealloc
3143 * space that is having currently known minimal distance
3144 * from the goal block.
3145 */
3146static struct ext4_prealloc_space *
3147ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3148 struct ext4_prealloc_space *pa,
3149 struct ext4_prealloc_space *cpa)
3150{
3151 ext4_fsblk_t cur_distance, new_distance;
3152
3153 if (cpa == NULL) {
3154 atomic_inc(&pa->pa_count);
3155 return pa;
3156 }
3157 cur_distance = abs(goal_block - cpa->pa_pstart);
3158 new_distance = abs(goal_block - pa->pa_pstart);
3159
Coly Li5a54b2f2011-02-24 14:10:05 -05003160 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003161 return cpa;
3162
3163 /* drop the previous reference */
3164 atomic_dec(&cpa->pa_count);
3165 atomic_inc(&pa->pa_count);
3166 return pa;
3167}
3168
3169/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003170 * search goal blocks in preallocated space
3171 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003172static noinline_for_stack int
3173ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003174{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003175 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003176 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003177 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3178 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003179 struct ext4_prealloc_space *pa, *cpa = NULL;
3180 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003181
3182 /* only data can be preallocated */
3183 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3184 return 0;
3185
3186 /* first, try per-file preallocation */
3187 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003188 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003189
3190 /* all fields in this condition don't change,
3191 * so we can skip locking for them */
3192 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003193 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3194 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003195 continue;
3196
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003197 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003198 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003199 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3200 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003201 continue;
3202
Alex Tomasc9de5602008-01-29 00:19:52 -05003203 /* found preallocated blocks, use them */
3204 spin_lock(&pa->pa_lock);
3205 if (pa->pa_deleted == 0 && pa->pa_free) {
3206 atomic_inc(&pa->pa_count);
3207 ext4_mb_use_inode_pa(ac, pa);
3208 spin_unlock(&pa->pa_lock);
3209 ac->ac_criteria = 10;
3210 rcu_read_unlock();
3211 return 1;
3212 }
3213 spin_unlock(&pa->pa_lock);
3214 }
3215 rcu_read_unlock();
3216
3217 /* can we use group allocation? */
3218 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3219 return 0;
3220
3221 /* inode may have no locality group for some reason */
3222 lg = ac->ac_lg;
3223 if (lg == NULL)
3224 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003225 order = fls(ac->ac_o_ex.fe_len) - 1;
3226 if (order > PREALLOC_TB_SIZE - 1)
3227 /* The max size of hash table is PREALLOC_TB_SIZE */
3228 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003229
Akinobu Mitabda00de2010-03-03 23:53:25 -05003230 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003231 /*
3232 * search for the prealloc space that is having
3233 * minimal distance from the goal block.
3234 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003235 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3236 rcu_read_lock();
3237 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3238 pa_inode_list) {
3239 spin_lock(&pa->pa_lock);
3240 if (pa->pa_deleted == 0 &&
3241 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003242
3243 cpa = ext4_mb_check_group_pa(goal_block,
3244 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003245 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003246 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003247 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003248 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003249 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003250 if (cpa) {
3251 ext4_mb_use_group_pa(ac, cpa);
3252 ac->ac_criteria = 20;
3253 return 1;
3254 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003255 return 0;
3256}
3257
3258/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003259 * the function goes through all block freed in the group
3260 * but not yet committed and marks them used in in-core bitmap.
3261 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003262 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003263 */
3264static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3265 ext4_group_t group)
3266{
3267 struct rb_node *n;
3268 struct ext4_group_info *grp;
3269 struct ext4_free_data *entry;
3270
3271 grp = ext4_get_group_info(sb, group);
3272 n = rb_first(&(grp->bb_free_root));
3273
3274 while (n) {
3275 entry = rb_entry(n, struct ext4_free_data, node);
Theodore Ts'o84130192011-09-09 18:50:51 -04003276 ext4_set_bits(bitmap, entry->start_cluster, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003277 n = rb_next(n);
3278 }
3279 return;
3280}
3281
3282/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003283 * the function goes through all preallocation in this group and marks them
3284 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003285 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003286 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003287static noinline_for_stack
3288void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003289 ext4_group_t group)
3290{
3291 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3292 struct ext4_prealloc_space *pa;
3293 struct list_head *cur;
3294 ext4_group_t groupnr;
3295 ext4_grpblk_t start;
3296 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003297 int len;
3298
3299 /* all form of preallocation discards first load group,
3300 * so the only competing code is preallocation use.
3301 * we don't need any locking here
3302 * notice we do NOT ignore preallocations with pa_deleted
3303 * otherwise we could leave used blocks available for
3304 * allocation in buddy when concurrent ext4_mb_put_pa()
3305 * is dropping preallocation
3306 */
3307 list_for_each(cur, &grp->bb_prealloc_list) {
3308 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3309 spin_lock(&pa->pa_lock);
3310 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3311 &groupnr, &start);
3312 len = pa->pa_len;
3313 spin_unlock(&pa->pa_lock);
3314 if (unlikely(len == 0))
3315 continue;
3316 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003317 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003318 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003319 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003320 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003321}
3322
3323static void ext4_mb_pa_callback(struct rcu_head *head)
3324{
3325 struct ext4_prealloc_space *pa;
3326 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3327 kmem_cache_free(ext4_pspace_cachep, pa);
3328}
3329
3330/*
3331 * drops a reference to preallocated space descriptor
3332 * if this was the last reference and the space is consumed
3333 */
3334static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3335 struct super_block *sb, struct ext4_prealloc_space *pa)
3336{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003337 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003338 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003339
3340 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3341 return;
3342
3343 /* in this short window concurrent discard can set pa_deleted */
3344 spin_lock(&pa->pa_lock);
3345 if (pa->pa_deleted == 1) {
3346 spin_unlock(&pa->pa_lock);
3347 return;
3348 }
3349
3350 pa->pa_deleted = 1;
3351 spin_unlock(&pa->pa_lock);
3352
Eric Sandeend33a1972009-03-16 23:25:40 -04003353 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003354 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003355 * If doing group-based preallocation, pa_pstart may be in the
3356 * next group when pa is used up
3357 */
3358 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003359 grp_blk--;
3360
3361 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003362
3363 /*
3364 * possible race:
3365 *
3366 * P1 (buddy init) P2 (regular allocation)
3367 * find block B in PA
3368 * copy on-disk bitmap to buddy
3369 * mark B in on-disk bitmap
3370 * drop PA from group
3371 * mark all PAs in buddy
3372 *
3373 * thus, P1 initializes buddy with B available. to prevent this
3374 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3375 * against that pair
3376 */
3377 ext4_lock_group(sb, grp);
3378 list_del(&pa->pa_group_list);
3379 ext4_unlock_group(sb, grp);
3380
3381 spin_lock(pa->pa_obj_lock);
3382 list_del_rcu(&pa->pa_inode_list);
3383 spin_unlock(pa->pa_obj_lock);
3384
3385 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3386}
3387
3388/*
3389 * creates new preallocated space for given inode
3390 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003391static noinline_for_stack int
3392ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003393{
3394 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003395 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003396 struct ext4_prealloc_space *pa;
3397 struct ext4_group_info *grp;
3398 struct ext4_inode_info *ei;
3399
3400 /* preallocate only when found space is larger then requested */
3401 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3402 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3403 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3404
3405 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3406 if (pa == NULL)
3407 return -ENOMEM;
3408
3409 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3410 int winl;
3411 int wins;
3412 int win;
3413 int offs;
3414
3415 /* we can't allocate as much as normalizer wants.
3416 * so, found space must get proper lstart
3417 * to cover original request */
3418 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3419 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3420
3421 /* we're limited by original request in that
3422 * logical block must be covered any way
3423 * winl is window we can move our chunk within */
3424 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3425
3426 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003427 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003428
3429 /* the smallest one defines real window */
3430 win = min(winl, wins);
3431
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003432 offs = ac->ac_o_ex.fe_logical %
3433 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003434 if (offs && offs < win)
3435 win = offs;
3436
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003437 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
3438 EXT4_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003439 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3440 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3441 }
3442
3443 /* preallocation can change ac_b_ex, thus we store actually
3444 * allocated blocks for history */
3445 ac->ac_f_ex = ac->ac_b_ex;
3446
3447 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3448 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3449 pa->pa_len = ac->ac_b_ex.fe_len;
3450 pa->pa_free = pa->pa_len;
3451 atomic_set(&pa->pa_count, 1);
3452 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003453 INIT_LIST_HEAD(&pa->pa_inode_list);
3454 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003455 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003456 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003457
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003458 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003459 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003460 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003461
3462 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003463 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003464
3465 ei = EXT4_I(ac->ac_inode);
3466 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3467
3468 pa->pa_obj_lock = &ei->i_prealloc_lock;
3469 pa->pa_inode = ac->ac_inode;
3470
3471 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3472 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3473 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3474
3475 spin_lock(pa->pa_obj_lock);
3476 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3477 spin_unlock(pa->pa_obj_lock);
3478
3479 return 0;
3480}
3481
3482/*
3483 * creates new preallocated space for locality group inodes belongs to
3484 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003485static noinline_for_stack int
3486ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003487{
3488 struct super_block *sb = ac->ac_sb;
3489 struct ext4_locality_group *lg;
3490 struct ext4_prealloc_space *pa;
3491 struct ext4_group_info *grp;
3492
3493 /* preallocate only when found space is larger then requested */
3494 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3495 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3496 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3497
3498 BUG_ON(ext4_pspace_cachep == NULL);
3499 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3500 if (pa == NULL)
3501 return -ENOMEM;
3502
3503 /* preallocation can change ac_b_ex, thus we store actually
3504 * allocated blocks for history */
3505 ac->ac_f_ex = ac->ac_b_ex;
3506
3507 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3508 pa->pa_lstart = pa->pa_pstart;
3509 pa->pa_len = ac->ac_b_ex.fe_len;
3510 pa->pa_free = pa->pa_len;
3511 atomic_set(&pa->pa_count, 1);
3512 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003513 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003514 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003515 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003516 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003517
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003518 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003519 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3520 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003521
3522 ext4_mb_use_group_pa(ac, pa);
3523 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3524
3525 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3526 lg = ac->ac_lg;
3527 BUG_ON(lg == NULL);
3528
3529 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3530 pa->pa_inode = NULL;
3531
3532 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3533 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3534 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3535
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003536 /*
3537 * We will later add the new pa to the right bucket
3538 * after updating the pa_free in ext4_mb_release_context
3539 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003540 return 0;
3541}
3542
3543static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3544{
3545 int err;
3546
3547 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3548 err = ext4_mb_new_group_pa(ac);
3549 else
3550 err = ext4_mb_new_inode_pa(ac);
3551 return err;
3552}
3553
3554/*
3555 * finds all unused blocks in on-disk bitmap, frees them in
3556 * in-core bitmap and buddy.
3557 * @pa must be unlinked from inode and group lists, so that
3558 * nobody else can find/use it.
3559 * the caller MUST hold group/inode locks.
3560 * TODO: optimize the case when there are no in-core structures yet
3561 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003562static noinline_for_stack int
3563ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003564 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003565{
Alex Tomasc9de5602008-01-29 00:19:52 -05003566 struct super_block *sb = e4b->bd_sb;
3567 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003568 unsigned int end;
3569 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003570 ext4_group_t group;
3571 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003572 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003573 int err = 0;
3574 int free = 0;
3575
3576 BUG_ON(pa->pa_deleted == 0);
3577 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003578 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003579 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3580 end = bit + pa->pa_len;
3581
Alex Tomasc9de5602008-01-29 00:19:52 -05003582 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003583 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003584 if (bit >= end)
3585 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003586 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003587 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003588 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3589 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003590 free += next - bit;
3591
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003592 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003593 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3594 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003595 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003596 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3597 bit = next + 1;
3598 }
3599 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003600 ext4_msg(e4b->bd_sb, KERN_CRIT,
3601 "pa %p: logic %lu, phys. %lu, len %lu",
3602 pa, (unsigned long) pa->pa_lstart,
3603 (unsigned long) pa->pa_pstart,
3604 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003605 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003606 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003607 /*
3608 * pa is already deleted so we use the value obtained
3609 * from the bitmap and continue.
3610 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003611 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003612 atomic_add(free, &sbi->s_mb_discarded);
3613
3614 return err;
3615}
3616
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003617static noinline_for_stack int
3618ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003619 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003620{
Alex Tomasc9de5602008-01-29 00:19:52 -05003621 struct super_block *sb = e4b->bd_sb;
3622 ext4_group_t group;
3623 ext4_grpblk_t bit;
3624
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003625 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003626 BUG_ON(pa->pa_deleted == 0);
3627 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3628 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3629 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3630 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003631 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003632
3633 return 0;
3634}
3635
3636/*
3637 * releases all preallocations in given group
3638 *
3639 * first, we need to decide discard policy:
3640 * - when do we discard
3641 * 1) ENOSPC
3642 * - how many do we discard
3643 * 1) how many requested
3644 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003645static noinline_for_stack int
3646ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003647 ext4_group_t group, int needed)
3648{
3649 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3650 struct buffer_head *bitmap_bh = NULL;
3651 struct ext4_prealloc_space *pa, *tmp;
3652 struct list_head list;
3653 struct ext4_buddy e4b;
3654 int err;
3655 int busy = 0;
3656 int free = 0;
3657
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003658 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003659
3660 if (list_empty(&grp->bb_prealloc_list))
3661 return 0;
3662
Theodore Ts'o574ca172008-07-11 19:27:31 -04003663 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003664 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003665 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003666 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003667 }
3668
3669 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003670 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003671 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003672 put_bh(bitmap_bh);
3673 return 0;
3674 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003675
3676 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003677 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003678
Alex Tomasc9de5602008-01-29 00:19:52 -05003679 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003680repeat:
3681 ext4_lock_group(sb, group);
3682 list_for_each_entry_safe(pa, tmp,
3683 &grp->bb_prealloc_list, pa_group_list) {
3684 spin_lock(&pa->pa_lock);
3685 if (atomic_read(&pa->pa_count)) {
3686 spin_unlock(&pa->pa_lock);
3687 busy = 1;
3688 continue;
3689 }
3690 if (pa->pa_deleted) {
3691 spin_unlock(&pa->pa_lock);
3692 continue;
3693 }
3694
3695 /* seems this one can be freed ... */
3696 pa->pa_deleted = 1;
3697
3698 /* we can trust pa_free ... */
3699 free += pa->pa_free;
3700
3701 spin_unlock(&pa->pa_lock);
3702
3703 list_del(&pa->pa_group_list);
3704 list_add(&pa->u.pa_tmp_list, &list);
3705 }
3706
3707 /* if we still need more blocks and some PAs were used, try again */
3708 if (free < needed && busy) {
3709 busy = 0;
3710 ext4_unlock_group(sb, group);
3711 /*
3712 * Yield the CPU here so that we don't get soft lockup
3713 * in non preempt case.
3714 */
3715 yield();
3716 goto repeat;
3717 }
3718
3719 /* found anything to free? */
3720 if (list_empty(&list)) {
3721 BUG_ON(free != 0);
3722 goto out;
3723 }
3724
3725 /* now free all selected PAs */
3726 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3727
3728 /* remove from object (inode or locality group) */
3729 spin_lock(pa->pa_obj_lock);
3730 list_del_rcu(&pa->pa_inode_list);
3731 spin_unlock(pa->pa_obj_lock);
3732
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003733 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003734 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003735 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003736 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003737
3738 list_del(&pa->u.pa_tmp_list);
3739 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3740 }
3741
3742out:
3743 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003744 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003745 put_bh(bitmap_bh);
3746 return free;
3747}
3748
3749/*
3750 * releases all non-used preallocated blocks for given inode
3751 *
3752 * It's important to discard preallocations under i_data_sem
3753 * We don't want another block to be served from the prealloc
3754 * space when we are discarding the inode prealloc space.
3755 *
3756 * FIXME!! Make sure it is valid at all the call sites
3757 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003758void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003759{
3760 struct ext4_inode_info *ei = EXT4_I(inode);
3761 struct super_block *sb = inode->i_sb;
3762 struct buffer_head *bitmap_bh = NULL;
3763 struct ext4_prealloc_space *pa, *tmp;
3764 ext4_group_t group = 0;
3765 struct list_head list;
3766 struct ext4_buddy e4b;
3767 int err;
3768
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003769 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003770 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3771 return;
3772 }
3773
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003774 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003775 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003776
3777 INIT_LIST_HEAD(&list);
3778
3779repeat:
3780 /* first, collect all pa's in the inode */
3781 spin_lock(&ei->i_prealloc_lock);
3782 while (!list_empty(&ei->i_prealloc_list)) {
3783 pa = list_entry(ei->i_prealloc_list.next,
3784 struct ext4_prealloc_space, pa_inode_list);
3785 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3786 spin_lock(&pa->pa_lock);
3787 if (atomic_read(&pa->pa_count)) {
3788 /* this shouldn't happen often - nobody should
3789 * use preallocation while we're discarding it */
3790 spin_unlock(&pa->pa_lock);
3791 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003792 ext4_msg(sb, KERN_ERR,
3793 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05003794 WARN_ON(1);
3795 schedule_timeout_uninterruptible(HZ);
3796 goto repeat;
3797
3798 }
3799 if (pa->pa_deleted == 0) {
3800 pa->pa_deleted = 1;
3801 spin_unlock(&pa->pa_lock);
3802 list_del_rcu(&pa->pa_inode_list);
3803 list_add(&pa->u.pa_tmp_list, &list);
3804 continue;
3805 }
3806
3807 /* someone is deleting pa right now */
3808 spin_unlock(&pa->pa_lock);
3809 spin_unlock(&ei->i_prealloc_lock);
3810
3811 /* we have to wait here because pa_deleted
3812 * doesn't mean pa is already unlinked from
3813 * the list. as we might be called from
3814 * ->clear_inode() the inode will get freed
3815 * and concurrent thread which is unlinking
3816 * pa from inode's list may access already
3817 * freed memory, bad-bad-bad */
3818
3819 /* XXX: if this happens too often, we can
3820 * add a flag to force wait only in case
3821 * of ->clear_inode(), but not in case of
3822 * regular truncate */
3823 schedule_timeout_uninterruptible(HZ);
3824 goto repeat;
3825 }
3826 spin_unlock(&ei->i_prealloc_lock);
3827
3828 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003829 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003830 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3831
3832 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003833 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003834 ext4_error(sb, "Error loading buddy information for %u",
3835 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003836 continue;
3837 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003838
Theodore Ts'o574ca172008-07-11 19:27:31 -04003839 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003840 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003841 ext4_error(sb, "Error reading block bitmap for %u",
3842 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003843 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003844 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003845 }
3846
3847 ext4_lock_group(sb, group);
3848 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003849 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003850 ext4_unlock_group(sb, group);
3851
Jing Zhange39e07f2010-05-14 00:00:00 -04003852 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003853 put_bh(bitmap_bh);
3854
3855 list_del(&pa->u.pa_tmp_list);
3856 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3857 }
3858}
3859
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003860#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003861static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3862{
3863 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003864 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003865
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05003866 if (!mb_enable_debug ||
3867 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04003868 return;
3869
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003870 ext4_msg(ac->ac_sb, KERN_ERR, "EXT4-fs: Can't allocate:"
3871 " Allocation context details:");
3872 ext4_msg(ac->ac_sb, KERN_ERR, "EXT4-fs: status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05003873 ac->ac_status, ac->ac_flags);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003874 ext4_msg(ac->ac_sb, KERN_ERR, "EXT4-fs: orig %lu/%lu/%lu@%lu, "
3875 "goal %lu/%lu/%lu@%lu, "
3876 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05003877 (unsigned long)ac->ac_o_ex.fe_group,
3878 (unsigned long)ac->ac_o_ex.fe_start,
3879 (unsigned long)ac->ac_o_ex.fe_len,
3880 (unsigned long)ac->ac_o_ex.fe_logical,
3881 (unsigned long)ac->ac_g_ex.fe_group,
3882 (unsigned long)ac->ac_g_ex.fe_start,
3883 (unsigned long)ac->ac_g_ex.fe_len,
3884 (unsigned long)ac->ac_g_ex.fe_logical,
3885 (unsigned long)ac->ac_b_ex.fe_group,
3886 (unsigned long)ac->ac_b_ex.fe_start,
3887 (unsigned long)ac->ac_b_ex.fe_len,
3888 (unsigned long)ac->ac_b_ex.fe_logical,
3889 (int)ac->ac_criteria);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003890 ext4_msg(ac->ac_sb, KERN_ERR, "EXT4-fs: %lu scanned, %d found",
3891 ac->ac_ex_scanned, ac->ac_found);
3892 ext4_msg(ac->ac_sb, KERN_ERR, "EXT4-fs: groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003893 ngroups = ext4_get_groups_count(sb);
3894 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003895 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3896 struct ext4_prealloc_space *pa;
3897 ext4_grpblk_t start;
3898 struct list_head *cur;
3899 ext4_lock_group(sb, i);
3900 list_for_each(cur, &grp->bb_prealloc_list) {
3901 pa = list_entry(cur, struct ext4_prealloc_space,
3902 pa_group_list);
3903 spin_lock(&pa->pa_lock);
3904 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3905 NULL, &start);
3906 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003907 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3908 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003909 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003910 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003911
3912 if (grp->bb_free == 0)
3913 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003914 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003915 i, grp->bb_free, grp->bb_fragments);
3916 }
3917 printk(KERN_ERR "\n");
3918}
3919#else
3920static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3921{
3922 return;
3923}
3924#endif
3925
3926/*
3927 * We use locality group preallocation for small size file. The size of the
3928 * file is determined by the current size or the resulting size after
3929 * allocation which ever is larger
3930 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003931 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003932 */
3933static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3934{
3935 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3936 int bsbits = ac->ac_sb->s_blocksize_bits;
3937 loff_t size, isize;
3938
3939 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3940 return;
3941
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003942 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3943 return;
3944
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003945 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04003946 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3947 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003948
Theodore Ts'o50797482009-09-18 13:34:02 -04003949 if ((size == isize) &&
3950 !ext4_fs_is_busy(sbi) &&
3951 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3952 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3953 return;
3954 }
3955
Robin Dongebbe0272011-10-26 05:14:27 -04003956 if (sbi->s_mb_group_prealloc <= 0) {
3957 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
3958 return;
3959 }
3960
Alex Tomasc9de5602008-01-29 00:19:52 -05003961 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003962 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003963 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003964 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003965 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003966 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003967
3968 BUG_ON(ac->ac_lg != NULL);
3969 /*
3970 * locality group prealloc space are per cpu. The reason for having
3971 * per cpu locality group is to reduce the contention between block
3972 * request from multiple CPUs.
3973 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003974 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003975
3976 /* we're going to use group allocation */
3977 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3978
3979 /* serialize all allocations in the group */
3980 mutex_lock(&ac->ac_lg->lg_mutex);
3981}
3982
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003983static noinline_for_stack int
3984ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003985 struct ext4_allocation_request *ar)
3986{
3987 struct super_block *sb = ar->inode->i_sb;
3988 struct ext4_sb_info *sbi = EXT4_SB(sb);
3989 struct ext4_super_block *es = sbi->s_es;
3990 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003991 unsigned int len;
3992 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05003993 ext4_grpblk_t block;
3994
3995 /* we can't allocate > group size */
3996 len = ar->len;
3997
3998 /* just a dirty hack to filter too big requests */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003999 if (len >= EXT4_CLUSTERS_PER_GROUP(sb) - 10)
4000 len = EXT4_CLUSTERS_PER_GROUP(sb) - 10;
Alex Tomasc9de5602008-01-29 00:19:52 -05004001
4002 /* start searching from the goal */
4003 goal = ar->goal;
4004 if (goal < le32_to_cpu(es->s_first_data_block) ||
4005 goal >= ext4_blocks_count(es))
4006 goal = le32_to_cpu(es->s_first_data_block);
4007 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4008
4009 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04004010 memset(ac, 0, sizeof(struct ext4_allocation_context));
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004011 ac->ac_b_ex.fe_logical = ar->logical & ~(sbi->s_cluster_ratio - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05004012 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004013 ac->ac_sb = sb;
4014 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004015 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004016 ac->ac_o_ex.fe_group = group;
4017 ac->ac_o_ex.fe_start = block;
4018 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004019 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004020 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004021
4022 /* we have to define context: we'll we work with a file or
4023 * locality group. this is a policy, actually */
4024 ext4_mb_group_or_file(ac);
4025
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004026 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004027 "left: %u/%u, right %u/%u to %swritable\n",
4028 (unsigned) ar->len, (unsigned) ar->logical,
4029 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4030 (unsigned) ar->lleft, (unsigned) ar->pleft,
4031 (unsigned) ar->lright, (unsigned) ar->pright,
4032 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4033 return 0;
4034
4035}
4036
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004037static noinline_for_stack void
4038ext4_mb_discard_lg_preallocations(struct super_block *sb,
4039 struct ext4_locality_group *lg,
4040 int order, int total_entries)
4041{
4042 ext4_group_t group = 0;
4043 struct ext4_buddy e4b;
4044 struct list_head discard_list;
4045 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004046
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004047 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004048
4049 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004050
4051 spin_lock(&lg->lg_prealloc_lock);
4052 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4053 pa_inode_list) {
4054 spin_lock(&pa->pa_lock);
4055 if (atomic_read(&pa->pa_count)) {
4056 /*
4057 * This is the pa that we just used
4058 * for block allocation. So don't
4059 * free that
4060 */
4061 spin_unlock(&pa->pa_lock);
4062 continue;
4063 }
4064 if (pa->pa_deleted) {
4065 spin_unlock(&pa->pa_lock);
4066 continue;
4067 }
4068 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004069 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004070
4071 /* seems this one can be freed ... */
4072 pa->pa_deleted = 1;
4073 spin_unlock(&pa->pa_lock);
4074
4075 list_del_rcu(&pa->pa_inode_list);
4076 list_add(&pa->u.pa_tmp_list, &discard_list);
4077
4078 total_entries--;
4079 if (total_entries <= 5) {
4080 /*
4081 * we want to keep only 5 entries
4082 * allowing it to grow to 8. This
4083 * mak sure we don't call discard
4084 * soon for this list.
4085 */
4086 break;
4087 }
4088 }
4089 spin_unlock(&lg->lg_prealloc_lock);
4090
4091 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4092
4093 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4094 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004095 ext4_error(sb, "Error loading buddy information for %u",
4096 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004097 continue;
4098 }
4099 ext4_lock_group(sb, group);
4100 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004101 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004102 ext4_unlock_group(sb, group);
4103
Jing Zhange39e07f2010-05-14 00:00:00 -04004104 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004105 list_del(&pa->u.pa_tmp_list);
4106 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4107 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004108}
4109
4110/*
4111 * We have incremented pa_count. So it cannot be freed at this
4112 * point. Also we hold lg_mutex. So no parallel allocation is
4113 * possible from this lg. That means pa_free cannot be updated.
4114 *
4115 * A parallel ext4_mb_discard_group_preallocations is possible.
4116 * which can cause the lg_prealloc_list to be updated.
4117 */
4118
4119static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4120{
4121 int order, added = 0, lg_prealloc_count = 1;
4122 struct super_block *sb = ac->ac_sb;
4123 struct ext4_locality_group *lg = ac->ac_lg;
4124 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4125
4126 order = fls(pa->pa_free) - 1;
4127 if (order > PREALLOC_TB_SIZE - 1)
4128 /* The max size of hash table is PREALLOC_TB_SIZE */
4129 order = PREALLOC_TB_SIZE - 1;
4130 /* Add the prealloc space to lg */
4131 rcu_read_lock();
4132 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4133 pa_inode_list) {
4134 spin_lock(&tmp_pa->pa_lock);
4135 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004136 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004137 continue;
4138 }
4139 if (!added && pa->pa_free < tmp_pa->pa_free) {
4140 /* Add to the tail of the previous entry */
4141 list_add_tail_rcu(&pa->pa_inode_list,
4142 &tmp_pa->pa_inode_list);
4143 added = 1;
4144 /*
4145 * we want to count the total
4146 * number of entries in the list
4147 */
4148 }
4149 spin_unlock(&tmp_pa->pa_lock);
4150 lg_prealloc_count++;
4151 }
4152 if (!added)
4153 list_add_tail_rcu(&pa->pa_inode_list,
4154 &lg->lg_prealloc_list[order]);
4155 rcu_read_unlock();
4156
4157 /* Now trim the list to be not more than 8 elements */
4158 if (lg_prealloc_count > 8) {
4159 ext4_mb_discard_lg_preallocations(sb, lg,
4160 order, lg_prealloc_count);
4161 return;
4162 }
4163 return ;
4164}
4165
Alex Tomasc9de5602008-01-29 00:19:52 -05004166/*
4167 * release all resource we used in allocation
4168 */
4169static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4170{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004171 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004172 struct ext4_prealloc_space *pa = ac->ac_pa;
4173 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004174 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004175 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004176 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004177 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4178 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004179 pa->pa_free -= ac->ac_b_ex.fe_len;
4180 pa->pa_len -= ac->ac_b_ex.fe_len;
4181 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004182 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004183 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004184 if (pa) {
4185 /*
4186 * We want to add the pa to the right bucket.
4187 * Remove it from the list and while adding
4188 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004189 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004190 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004191 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004192 spin_lock(pa->pa_obj_lock);
4193 list_del_rcu(&pa->pa_inode_list);
4194 spin_unlock(pa->pa_obj_lock);
4195 ext4_mb_add_n_trim(ac);
4196 }
4197 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4198 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004199 if (ac->ac_bitmap_page)
4200 page_cache_release(ac->ac_bitmap_page);
4201 if (ac->ac_buddy_page)
4202 page_cache_release(ac->ac_buddy_page);
4203 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4204 mutex_unlock(&ac->ac_lg->lg_mutex);
4205 ext4_mb_collect_stats(ac);
4206 return 0;
4207}
4208
4209static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4210{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004211 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004212 int ret;
4213 int freed = 0;
4214
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004215 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004216 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004217 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4218 freed += ret;
4219 needed -= ret;
4220 }
4221
4222 return freed;
4223}
4224
4225/*
4226 * Main entry point into mballoc to allocate blocks
4227 * it tries to use preallocation first, then falls back
4228 * to usual allocation
4229 */
4230ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004231 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004232{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004233 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004234 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004235 struct ext4_sb_info *sbi;
4236 struct super_block *sb;
4237 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004238 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004239 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004240
4241 sb = ar->inode->i_sb;
4242 sbi = EXT4_SB(sb);
4243
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004244 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004245
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004246 /* Allow to use superuser reservation for quota file */
4247 if (IS_NOQUOTA(ar->inode))
4248 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4249
Mingming Cao60e58e02009-01-22 18:13:05 +01004250 /*
4251 * For delayed allocation, we could skip the ENOSPC and
4252 * EDQUOT check, as blocks and quotas have been already
4253 * reserved when data being copied into pagecache.
4254 */
Theodore Ts'of2321092011-01-10 12:12:36 -05004255 if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
Mingming Cao60e58e02009-01-22 18:13:05 +01004256 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4257 else {
4258 /* Without delayed allocation we need to verify
4259 * there is enough free blocks to do block allocation
4260 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004261 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004262 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004263 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004264
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004265 /* let others to free the space */
4266 yield();
4267 ar->len = ar->len >> 1;
4268 }
4269 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004270 *errp = -ENOSPC;
4271 return 0;
4272 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004273 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004274 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004275 dquot_alloc_block_nofail(ar->inode,
4276 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004277 } else {
4278 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004279 dquot_alloc_block(ar->inode,
4280 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004281
4282 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4283 ar->len--;
4284 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004285 }
4286 inquota = ar->len;
4287 if (ar->len == 0) {
4288 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004289 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004290 }
Mingming Caod2a17632008-07-14 17:52:37 -04004291 }
Mingming Caod2a17632008-07-14 17:52:37 -04004292
Eric Sandeen256bdb42008-02-10 01:13:33 -05004293 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004294 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004295 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004296 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004297 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004298 }
4299
Eric Sandeen256bdb42008-02-10 01:13:33 -05004300 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004301 if (*errp) {
4302 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004303 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004304 }
4305
Eric Sandeen256bdb42008-02-10 01:13:33 -05004306 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4307 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004308 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4309 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004310repeat:
4311 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004312 *errp = ext4_mb_regular_allocator(ac);
4313 if (*errp)
4314 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004315
4316 /* as we've just preallocated more space than
4317 * user requested orinally, we store allocated
4318 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004319 if (ac->ac_status == AC_STATUS_FOUND &&
4320 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4321 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004322 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004323 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004324 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004325 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004326 /*
4327 * drop the reference that we took
4328 * in ext4_mb_use_best_found
4329 */
4330 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004331 ac->ac_b_ex.fe_group = 0;
4332 ac->ac_b_ex.fe_start = 0;
4333 ac->ac_b_ex.fe_len = 0;
4334 ac->ac_status = AC_STATUS_CONTINUE;
4335 goto repeat;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004336 } else if (*errp)
4337 errout:
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004338 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004339 else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004340 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4341 ar->len = ac->ac_b_ex.fe_len;
4342 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004343 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004344 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004345 if (freed)
4346 goto repeat;
4347 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004348 }
4349
4350 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004351 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004352 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004353 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004354 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004355 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004356out:
4357 if (ac)
4358 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004359 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004360 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004361 if (!ar->len) {
Theodore Ts'of2321092011-01-10 12:12:36 -05004362 if (!ext4_test_inode_state(ar->inode,
4363 EXT4_STATE_DELALLOC_RESERVED))
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004364 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004365 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004366 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004367 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004368
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004369 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004370
Alex Tomasc9de5602008-01-29 00:19:52 -05004371 return block;
4372}
Alex Tomasc9de5602008-01-29 00:19:52 -05004373
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004374/*
4375 * We can merge two free data extents only if the physical blocks
4376 * are contiguous, AND the extents were freed by the same transaction,
4377 * AND the blocks are associated with the same group.
4378 */
4379static int can_merge(struct ext4_free_data *entry1,
4380 struct ext4_free_data *entry2)
4381{
4382 if ((entry1->t_tid == entry2->t_tid) &&
4383 (entry1->group == entry2->group) &&
Theodore Ts'o84130192011-09-09 18:50:51 -04004384 ((entry1->start_cluster + entry1->count) == entry2->start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004385 return 1;
4386 return 0;
4387}
4388
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004389static noinline_for_stack int
4390ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004391 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004392{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004393 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004394 ext4_grpblk_t cluster;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004395 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004396 struct ext4_group_info *db = e4b->bd_info;
4397 struct super_block *sb = e4b->bd_sb;
4398 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004399 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4400 struct rb_node *parent = NULL, *new_node;
4401
Frank Mayhar03901312009-01-07 00:06:22 -05004402 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004403 BUG_ON(e4b->bd_bitmap_page == NULL);
4404 BUG_ON(e4b->bd_buddy_page == NULL);
4405
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004406 new_node = &new_entry->node;
Theodore Ts'o84130192011-09-09 18:50:51 -04004407 cluster = new_entry->start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004408
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004409 if (!*n) {
4410 /* first free block exent. We need to
4411 protect buddy cache from being freed,
4412 * otherwise we'll refresh it from
4413 * on-disk bitmap and lose not-yet-available
4414 * blocks */
4415 page_cache_get(e4b->bd_buddy_page);
4416 page_cache_get(e4b->bd_bitmap_page);
4417 }
4418 while (*n) {
4419 parent = *n;
4420 entry = rb_entry(parent, struct ext4_free_data, node);
Theodore Ts'o84130192011-09-09 18:50:51 -04004421 if (cluster < entry->start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004422 n = &(*n)->rb_left;
Theodore Ts'o84130192011-09-09 18:50:51 -04004423 else if (cluster >= (entry->start_cluster + entry->count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004424 n = &(*n)->rb_right;
4425 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004426 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004427 ext4_group_first_block_no(sb, group) +
4428 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004429 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004430 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004431 }
4432 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004433
4434 rb_link_node(new_node, parent, n);
4435 rb_insert_color(new_node, &db->bb_free_root);
4436
4437 /* Now try to see the extent can be merged to left and right */
4438 node = rb_prev(new_node);
4439 if (node) {
4440 entry = rb_entry(node, struct ext4_free_data, node);
4441 if (can_merge(entry, new_entry)) {
Theodore Ts'o84130192011-09-09 18:50:51 -04004442 new_entry->start_cluster = entry->start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004443 new_entry->count += entry->count;
4444 rb_erase(node, &(db->bb_free_root));
4445 spin_lock(&sbi->s_md_lock);
4446 list_del(&entry->list);
4447 spin_unlock(&sbi->s_md_lock);
4448 kmem_cache_free(ext4_free_ext_cachep, entry);
4449 }
4450 }
4451
4452 node = rb_next(new_node);
4453 if (node) {
4454 entry = rb_entry(node, struct ext4_free_data, node);
4455 if (can_merge(new_entry, entry)) {
4456 new_entry->count += entry->count;
4457 rb_erase(node, &(db->bb_free_root));
4458 spin_lock(&sbi->s_md_lock);
4459 list_del(&entry->list);
4460 spin_unlock(&sbi->s_md_lock);
4461 kmem_cache_free(ext4_free_ext_cachep, entry);
4462 }
4463 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004464 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004465 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004466 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004467 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004468 return 0;
4469}
4470
Theodore Ts'o44338712009-11-22 07:44:56 -05004471/**
4472 * ext4_free_blocks() -- Free given blocks and update quota
4473 * @handle: handle for this transaction
4474 * @inode: inode
4475 * @block: start physical block to free
4476 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004477 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004478 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004479void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004480 struct buffer_head *bh, ext4_fsblk_t block,
4481 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004482{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004483 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004484 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004485 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004486 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004487 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004488 ext4_grpblk_t bit;
4489 struct buffer_head *gd_bh;
4490 ext4_group_t block_group;
4491 struct ext4_sb_info *sbi;
4492 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004493 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004494 int err = 0;
4495 int ret;
4496
Theodore Ts'oe6362602009-11-23 07:17:05 -05004497 if (bh) {
4498 if (block)
4499 BUG_ON(block != bh->b_blocknr);
4500 else
4501 block = bh->b_blocknr;
4502 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004503
Alex Tomasc9de5602008-01-29 00:19:52 -05004504 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004505 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4506 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004507 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004508 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004509 goto error_return;
4510 }
4511
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004512 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004513 trace_ext4_free_blocks(inode, block, count, flags);
4514
4515 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4516 struct buffer_head *tbh = bh;
4517 int i;
4518
4519 BUG_ON(bh && (count > 1));
4520
4521 for (i = 0; i < count; i++) {
4522 if (!bh)
4523 tbh = sb_find_get_block(inode->i_sb,
4524 block + i);
Namhyung Kim87783692010-10-27 21:30:11 -04004525 if (unlikely(!tbh))
4526 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004527 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004528 inode, tbh, block + i);
4529 }
4530 }
4531
Theodore Ts'o60e66792010-05-17 07:00:00 -04004532 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004533 * We need to make sure we don't reuse the freed block until
4534 * after the transaction is committed, which we can do by
4535 * treating the block as metadata, below. We make an
4536 * exception if the inode is to be written in writeback mode
4537 * since writeback mode has weak data consistency guarantees.
4538 */
4539 if (!ext4_should_writeback_data(inode))
4540 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004541
Theodore Ts'o84130192011-09-09 18:50:51 -04004542 /*
4543 * If the extent to be freed does not begin on a cluster
4544 * boundary, we need to deal with partial clusters at the
4545 * beginning and end of the extent. Normally we will free
4546 * blocks at the beginning or the end unless we are explicitly
4547 * requested to avoid doing so.
4548 */
4549 overflow = block & (sbi->s_cluster_ratio - 1);
4550 if (overflow) {
4551 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4552 overflow = sbi->s_cluster_ratio - overflow;
4553 block += overflow;
4554 if (count > overflow)
4555 count -= overflow;
4556 else
4557 return;
4558 } else {
4559 block -= overflow;
4560 count += overflow;
4561 }
4562 }
4563 overflow = count & (sbi->s_cluster_ratio - 1);
4564 if (overflow) {
4565 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4566 if (count > overflow)
4567 count -= overflow;
4568 else
4569 return;
4570 } else
4571 count += sbi->s_cluster_ratio - overflow;
4572 }
4573
Alex Tomasc9de5602008-01-29 00:19:52 -05004574do_more:
4575 overflow = 0;
4576 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4577
4578 /*
4579 * Check to see if we are freeing blocks across a group
4580 * boundary.
4581 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004582 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4583 overflow = EXT4_C2B(sbi, bit) + count -
4584 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004585 count -= overflow;
4586 }
Theodore Ts'o84130192011-09-09 18:50:51 -04004587 count_clusters = EXT4_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004588 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004589 if (!bitmap_bh) {
4590 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004591 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004592 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004593 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004594 if (!gdp) {
4595 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004596 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004597 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004598
4599 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4600 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4601 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004602 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004603 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004604 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004605
Eric Sandeen12062dd2010-02-15 14:19:27 -05004606 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004607 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004608 /* err = 0. ext4_std_error should be a no op */
4609 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004610 }
4611
4612 BUFFER_TRACE(bitmap_bh, "getting write access");
4613 err = ext4_journal_get_write_access(handle, bitmap_bh);
4614 if (err)
4615 goto error_return;
4616
4617 /*
4618 * We are about to modify some metadata. Call the journal APIs
4619 * to unshare ->b_data if a currently-committing transaction is
4620 * using it
4621 */
4622 BUFFER_TRACE(gd_bh, "get_write_access");
4623 err = ext4_journal_get_write_access(handle, gd_bh);
4624 if (err)
4625 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004626#ifdef AGGRESSIVE_CHECK
4627 {
4628 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004629 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004630 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4631 }
4632#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004633 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004634
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004635 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4636 if (err)
4637 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004638
4639 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004640 struct ext4_free_data *new_entry;
4641 /*
4642 * blocks being freed are metadata. these blocks shouldn't
4643 * be used until this transaction is committed
4644 */
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004645 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4646 if (!new_entry) {
4647 err = -ENOMEM;
4648 goto error_return;
4649 }
Theodore Ts'o84130192011-09-09 18:50:51 -04004650 new_entry->start_cluster = bit;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004651 new_entry->group = block_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004652 new_entry->count = count_clusters;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004653 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004654
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004655 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004656 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004657 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004658 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004659 /* need to update group_info->bb_free and bitmap
4660 * with group lock held. generate_buddy look at
4661 * them with group lock_held
4662 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004663 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004664 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4665 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004666 }
4667
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004668 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4669 ext4_free_group_clusters_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004670 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004671 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04004672 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004673
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004674 if (sbi->s_log_groups_per_flex) {
4675 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o24aaa8e2011-09-09 18:58:51 -04004676 atomic_add(count_clusters,
4677 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004678 }
4679
Jing Zhange39e07f2010-05-14 00:00:00 -04004680 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004681
Theodore Ts'o44338712009-11-22 07:44:56 -05004682 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004683
Aditya Kali7b415bf2011-09-09 19:04:51 -04004684 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
4685 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
4686
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004687 /* We dirtied the bitmap block */
4688 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4689 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4690
Alex Tomasc9de5602008-01-29 00:19:52 -05004691 /* And the group descriptor block */
4692 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004693 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004694 if (!err)
4695 err = ret;
4696
4697 if (overflow && !err) {
4698 block += count;
4699 count = overflow;
4700 put_bh(bitmap_bh);
4701 goto do_more;
4702 }
Theodore Ts'oa0375152010-06-11 23:14:04 -04004703 ext4_mark_super_dirty(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004704error_return:
4705 brelse(bitmap_bh);
4706 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004707 return;
4708}
Lukas Czerner7360d172010-10-27 21:30:12 -04004709
4710/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004711 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004712 * @handle: handle to this transaction
4713 * @sb: super block
4714 * @block: start physcial block to add to the block group
4715 * @count: number of blocks to free
4716 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004717 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004718 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004719int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004720 ext4_fsblk_t block, unsigned long count)
4721{
4722 struct buffer_head *bitmap_bh = NULL;
4723 struct buffer_head *gd_bh;
4724 ext4_group_t block_group;
4725 ext4_grpblk_t bit;
4726 unsigned int i;
4727 struct ext4_group_desc *desc;
4728 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004729 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004730 int err = 0, ret, blk_free_count;
4731 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004732
4733 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4734
Yongqiang Yang4740b832011-07-26 21:51:08 -04004735 if (count == 0)
4736 return 0;
4737
Amir Goldstein2846e822011-05-09 10:46:41 -04004738 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004739 /*
4740 * Check to see if we are freeing blocks across a group
4741 * boundary.
4742 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004743 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4744 ext4_warning(sb, "too much blocks added to group %u\n",
4745 block_group);
4746 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004747 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004748 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004749
Amir Goldstein2846e822011-05-09 10:46:41 -04004750 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004751 if (!bitmap_bh) {
4752 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004753 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004754 }
4755
Amir Goldstein2846e822011-05-09 10:46:41 -04004756 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004757 if (!desc) {
4758 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004759 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004760 }
Amir Goldstein2846e822011-05-09 10:46:41 -04004761
4762 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4763 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4764 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4765 in_range(block + count - 1, ext4_inode_table(sb, desc),
4766 sbi->s_itb_per_group)) {
4767 ext4_error(sb, "Adding blocks in system zones - "
4768 "Block = %llu, count = %lu",
4769 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004770 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004771 goto error_return;
4772 }
4773
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004774 BUFFER_TRACE(bitmap_bh, "getting write access");
4775 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004776 if (err)
4777 goto error_return;
4778
4779 /*
4780 * We are about to modify some metadata. Call the journal APIs
4781 * to unshare ->b_data if a currently-committing transaction is
4782 * using it
4783 */
4784 BUFFER_TRACE(gd_bh, "get_write_access");
4785 err = ext4_journal_get_write_access(handle, gd_bh);
4786 if (err)
4787 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04004788
Amir Goldstein2846e822011-05-09 10:46:41 -04004789 for (i = 0, blocks_freed = 0; i < count; i++) {
4790 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04004791 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04004792 ext4_error(sb, "bit already cleared for block %llu",
4793 (ext4_fsblk_t)(block + i));
4794 BUFFER_TRACE(bitmap_bh, "bit already cleared");
4795 } else {
4796 blocks_freed++;
4797 }
4798 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004799
4800 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4801 if (err)
4802 goto error_return;
4803
4804 /*
4805 * need to update group_info->bb_free and bitmap
4806 * with group lock held. generate_buddy look at
4807 * them with group lock_held
4808 */
Amir Goldstein2846e822011-05-09 10:46:41 -04004809 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004810 mb_clear_bits(bitmap_bh->b_data, bit, count);
4811 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004812 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
4813 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Amir Goldstein2846e822011-05-09 10:46:41 -04004814 desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
4815 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04004816 percpu_counter_add(&sbi->s_freeclusters_counter,
4817 EXT4_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04004818
4819 if (sbi->s_log_groups_per_flex) {
4820 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o24aaa8e2011-09-09 18:58:51 -04004821 atomic_add(EXT4_B2C(sbi, blocks_freed),
4822 &sbi->s_flex_groups[flex_group].free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04004823 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004824
4825 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04004826
4827 /* We dirtied the bitmap block */
4828 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4829 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4830
4831 /* And the group descriptor block */
4832 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4833 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4834 if (!err)
4835 err = ret;
4836
4837error_return:
4838 brelse(bitmap_bh);
4839 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004840 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04004841}
4842
4843/**
Lukas Czerner7360d172010-10-27 21:30:12 -04004844 * ext4_trim_extent -- function to TRIM one single free extent in the group
4845 * @sb: super block for the file system
4846 * @start: starting block of the free extent in the alloc. group
4847 * @count: number of blocks to TRIM
4848 * @group: alloc. group we are working with
4849 * @e4b: ext4 buddy for the group
4850 *
4851 * Trim "count" blocks starting at "start" in the "group". To assure that no
4852 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4853 * be called with under the group lock.
4854 */
Theodore Ts'od9f34502011-04-30 13:47:24 -04004855static void ext4_trim_extent(struct super_block *sb, int start, int count,
4856 ext4_group_t group, struct ext4_buddy *e4b)
Lukas Czerner7360d172010-10-27 21:30:12 -04004857{
4858 struct ext4_free_extent ex;
Lukas Czerner7360d172010-10-27 21:30:12 -04004859
Tao Mab3d4c2b2011-07-11 00:01:52 -04004860 trace_ext4_trim_extent(sb, group, start, count);
4861
Lukas Czerner7360d172010-10-27 21:30:12 -04004862 assert_spin_locked(ext4_group_lock_ptr(sb, group));
4863
4864 ex.fe_start = start;
4865 ex.fe_group = group;
4866 ex.fe_len = count;
4867
4868 /*
4869 * Mark blocks used, so no one can reuse them while
4870 * being trimmed.
4871 */
4872 mb_mark_used(e4b, &ex);
4873 ext4_unlock_group(sb, group);
Theodore Ts'od9f34502011-04-30 13:47:24 -04004874 ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04004875 ext4_lock_group(sb, group);
4876 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czerner7360d172010-10-27 21:30:12 -04004877}
4878
4879/**
4880 * ext4_trim_all_free -- function to trim all free space in alloc. group
4881 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04004882 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04004883 * @start: first group block to examine
4884 * @max: last group block to examine
4885 * @minblocks: minimum extent block count
4886 *
4887 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4888 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4889 * the extent.
4890 *
4891 *
4892 * ext4_trim_all_free walks through group's block bitmap searching for free
4893 * extents. When the free extent is found, mark it as used in group buddy
4894 * bitmap. Then issue a TRIM command on this extent and free the extent in
4895 * the group buddy bitmap. This is done until whole group is scanned.
4896 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05004897static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04004898ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
4899 ext4_grpblk_t start, ext4_grpblk_t max,
4900 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04004901{
4902 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04004903 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04004904 struct ext4_buddy e4b;
4905 int ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04004906
Tao Mab3d4c2b2011-07-11 00:01:52 -04004907 trace_ext4_trim_all_free(sb, group, start, max);
4908
Lukas Czerner78944082011-05-24 18:16:27 -04004909 ret = ext4_mb_load_buddy(sb, group, &e4b);
4910 if (ret) {
4911 ext4_error(sb, "Error in loading buddy "
4912 "information for %u", group);
4913 return ret;
4914 }
Lukas Czerner78944082011-05-24 18:16:27 -04004915 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04004916
4917 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04004918 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
4919 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
4920 goto out;
4921
Lukas Czerner78944082011-05-24 18:16:27 -04004922 start = (e4b.bd_info->bb_first_free > start) ?
4923 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04004924
4925 while (start < max) {
4926 start = mb_find_next_zero_bit(bitmap, max, start);
4927 if (start >= max)
4928 break;
4929 next = mb_find_next_bit(bitmap, max, start);
4930
4931 if ((next - start) >= minblocks) {
Theodore Ts'od9f34502011-04-30 13:47:24 -04004932 ext4_trim_extent(sb, start,
Lukas Czerner78944082011-05-24 18:16:27 -04004933 next - start, group, &e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04004934 count += next - start;
4935 }
Tao Ma169ddc32011-07-11 00:00:07 -04004936 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04004937 start = next + 1;
4938
4939 if (fatal_signal_pending(current)) {
4940 count = -ERESTARTSYS;
4941 break;
4942 }
4943
4944 if (need_resched()) {
4945 ext4_unlock_group(sb, group);
4946 cond_resched();
4947 ext4_lock_group(sb, group);
4948 }
4949
Tao Ma169ddc32011-07-11 00:00:07 -04004950 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04004951 break;
4952 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04004953
4954 if (!ret)
4955 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
4956out:
Lukas Czerner7360d172010-10-27 21:30:12 -04004957 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04004958 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04004959
4960 ext4_debug("trimmed %d blocks in the group %d\n",
4961 count, group);
4962
Lukas Czerner7360d172010-10-27 21:30:12 -04004963 return count;
4964}
4965
4966/**
4967 * ext4_trim_fs() -- trim ioctl handle function
4968 * @sb: superblock for filesystem
4969 * @range: fstrim_range structure
4970 *
4971 * start: First Byte to trim
4972 * len: number of Bytes to trim from start
4973 * minlen: minimum extent length in Bytes
4974 * ext4_trim_fs goes through all allocation groups containing Bytes from
4975 * start to start+len. For each such a group ext4_trim_all_free function
4976 * is invoked to trim all free space.
4977 */
4978int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
4979{
Lukas Czerner78944082011-05-24 18:16:27 -04004980 struct ext4_group_info *grp;
Lukas Czerner7360d172010-10-27 21:30:12 -04004981 ext4_group_t first_group, last_group;
4982 ext4_group_t group, ngroups = ext4_get_groups_count(sb);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04004983 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner78944082011-05-24 18:16:27 -04004984 uint64_t start, len, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004985 ext4_fsblk_t first_data_blk =
4986 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner7360d172010-10-27 21:30:12 -04004987 int ret = 0;
4988
4989 start = range->start >> sb->s_blocksize_bits;
4990 len = range->len >> sb->s_blocksize_bits;
4991 minlen = range->minlen >> sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04004992
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04004993 if (unlikely(minlen > EXT4_CLUSTERS_PER_GROUP(sb)))
Lukas Czerner7360d172010-10-27 21:30:12 -04004994 return -EINVAL;
Tao Ma22f10452011-07-10 23:52:37 -04004995 if (start + len <= first_data_blk)
4996 goto out;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004997 if (start < first_data_blk) {
4998 len -= first_data_blk - start;
4999 start = first_data_blk;
5000 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005001
5002 /* Determine first and last group to examine based on start and len */
5003 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005004 &first_group, &first_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005005 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) (start + len),
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005006 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005007 last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005008 last_cluster = EXT4_CLUSTERS_PER_GROUP(sb);
Lukas Czerner7360d172010-10-27 21:30:12 -04005009
5010 if (first_group > last_group)
5011 return -EINVAL;
5012
5013 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005014 grp = ext4_get_group_info(sb, group);
5015 /* We only do this if the grp has never been initialized */
5016 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
5017 ret = ext4_mb_init_group(sb, group);
5018 if (ret)
5019 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005020 }
5021
Tao Ma0ba08512011-03-23 15:48:11 -04005022 /*
5023 * For all the groups except the last one, last block will
5024 * always be EXT4_BLOCKS_PER_GROUP(sb), so we only need to
5025 * change it for the last group in which case start +
5026 * len < EXT4_BLOCKS_PER_GROUP(sb).
5027 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005028 if (first_cluster + len < EXT4_CLUSTERS_PER_GROUP(sb))
5029 last_cluster = first_cluster + len;
5030 len -= last_cluster - first_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005031
Lukas Czerner78944082011-05-24 18:16:27 -04005032 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005033 cnt = ext4_trim_all_free(sb, group, first_cluster,
5034 last_cluster, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005035 if (cnt < 0) {
5036 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005037 break;
5038 }
5039 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005040 trimmed += cnt;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005041 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005042 }
5043 range->len = trimmed * sb->s_blocksize;
5044
Tao Ma3d56b8d2011-07-11 00:03:38 -04005045 if (!ret)
5046 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5047
Tao Ma22f10452011-07-10 23:52:37 -04005048out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005049 return ret;
5050}