blob: 440987c8ba9ef2dc27eee2092e244d6f4c32bb7b [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
Bobi Jam18aadd42012-02-20 17:53:02 -050024#include "ext4_jbd2.h"
Mingming Cao8f6e39a2008-04-29 22:01:31 -040025#include "mballoc.h"
Theodore Ts'o28623c22012-09-05 01:31:50 -040026#include <linux/log2.h>
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050027#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040029#include <linux/backing-dev.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040030#include <trace/events/ext4.h>
31
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050032#ifdef CONFIG_EXT4_DEBUG
33ushort ext4_mballoc_debug __read_mostly;
34
35module_param_named(mballoc_debug, ext4_mballoc_debug, ushort, 0644);
36MODULE_PARM_DESC(mballoc_debug, "Debugging level for ext4's mballoc");
37#endif
38
Alex Tomasc9de5602008-01-29 00:19:52 -050039/*
40 * MUSTDO:
41 * - test ext4_ext_search_left() and ext4_ext_search_right()
42 * - search for metadata in few groups
43 *
44 * TODO v4:
45 * - normalization should take into account whether file is still open
46 * - discard preallocations if no free space left (policy?)
47 * - don't normalize tails
48 * - quota
49 * - reservation for superuser
50 *
51 * TODO v3:
52 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
53 * - track min/max extents in each group for better group selection
54 * - mb_mark_used() may allocate chunk right after splitting buddy
55 * - tree of groups sorted by number of free blocks
56 * - error handling
57 */
58
59/*
60 * The allocation request involve request for multiple number of blocks
61 * near to the goal(block) value specified.
62 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040063 * During initialization phase of the allocator we decide to use the
64 * group preallocation or inode preallocation depending on the size of
65 * the file. The size of the file could be the resulting file size we
66 * would have after allocation, or the current file size, which ever
67 * is larger. If the size is less than sbi->s_mb_stream_request we
68 * select to use the group preallocation. The default value of
69 * s_mb_stream_request is 16 blocks. This can also be tuned via
70 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
71 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050072 *
73 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040074 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050075 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040076 * First stage the allocator looks at the inode prealloc list,
77 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
78 * spaces for this particular inode. The inode prealloc space is
79 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050080 *
81 * pa_lstart -> the logical start block for this prealloc space
82 * pa_pstart -> the physical start block for this prealloc space
Theodore Ts'o53accfa2011-09-09 18:48:51 -040083 * pa_len -> length for this prealloc space (in clusters)
84 * pa_free -> free space available in this prealloc space (in clusters)
Alex Tomasc9de5602008-01-29 00:19:52 -050085 *
86 * The inode preallocation space is used looking at the _logical_ start
87 * block. If only the logical file block falls within the range of prealloc
Tao Macaaf7a22011-07-11 18:42:42 -040088 * space we will consume the particular prealloc space. This makes sure that
89 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050090 *
91 * The important thing to be noted in case of inode prealloc space is that
92 * we don't modify the values associated to inode prealloc space except
93 * pa_free.
94 *
95 * If we are not able to find blocks in the inode prealloc space and if we
96 * have the group allocation flag set then we look at the locality group
Tao Macaaf7a22011-07-11 18:42:42 -040097 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050098 *
99 * ext4_sb_info.s_locality_groups[smp_processor_id()]
100 *
101 * The reason for having a per cpu locality group is to reduce the contention
102 * between CPUs. It is possible to get scheduled at this point.
103 *
104 * The locality group prealloc space is used looking at whether we have
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300105 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -0500106 *
107 * If we can't allocate blocks via inode prealloc or/and locality group
108 * prealloc then we look at the buddy cache. The buddy cache is represented
109 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
110 * mapped to the buddy and bitmap information regarding different
111 * groups. The buddy information is attached to buddy cache inode so that
112 * we can access them through the page cache. The information regarding
113 * each group is loaded via ext4_mb_load_buddy. The information involve
114 * block bitmap and buddy information. The information are stored in the
115 * inode as:
116 *
117 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500118 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500119 *
120 *
121 * one block each for bitmap and buddy information. So for each group we
122 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
123 * blocksize) blocks. So it can have information regarding groups_per_page
124 * which is blocks_per_page/2
125 *
126 * The buddy cache inode is not stored on disk. The inode is thrown
127 * away when the filesystem is unmounted.
128 *
129 * We look for count number of blocks in the buddy cache. If we were able
130 * to locate that many free blocks we return with additional information
131 * regarding rest of the contiguous physical block available
132 *
133 * Before allocating blocks via buddy cache we normalize the request
134 * blocks. This ensure we ask for more blocks that we needed. The extra
135 * blocks that we get after allocation is added to the respective prealloc
136 * list. In case of inode preallocation we follow a list of heuristics
137 * based on file size. This can be found in ext4_mb_normalize_request. If
138 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'o27baebb2011-09-09 19:02:51 -0400139 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
140 * dependent on the cluster size; for non-bigalloc file systems, it is
Alex Tomasc9de5602008-01-29 00:19:52 -0500141 * 512 blocks. This can be tuned via
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400142 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500143 * terms of number of blocks. If we have mounted the file system with -O
144 * stripe=<value> option the group prealloc request is normalized to the
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400145 * the smallest multiple of the stripe value (sbi->s_stripe) which is
146 * greater than the default mb_group_prealloc.
Alex Tomasc9de5602008-01-29 00:19:52 -0500147 *
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400148 * The regular allocator (using the buddy cache) supports a few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500149 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400150 * /sys/fs/ext4/<partition>/mb_min_to_scan
151 * /sys/fs/ext4/<partition>/mb_max_to_scan
152 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500153 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400154 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500155 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
156 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400157 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200158 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400159 * stripe size. This should result in better allocation on RAID setups. If
160 * not, we search in the specific group using bitmap for best extents. The
161 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500162 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400163 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500164 * best extent in the found extents. Searching for the blocks starts with
165 * the group specified as the goal value in allocation context via
166 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400167 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500168 * checked.
169 *
170 * Both the prealloc space are getting populated as above. So for the first
171 * request we will hit the buddy cache which will result in this prealloc
172 * space getting filled. The prealloc space is then later used for the
173 * subsequent request.
174 */
175
176/*
177 * mballoc operates on the following data:
178 * - on-disk bitmap
179 * - in-core buddy (actually includes buddy and bitmap)
180 * - preallocation descriptors (PAs)
181 *
182 * there are two types of preallocations:
183 * - inode
184 * assiged to specific inode and can be used for this inode only.
185 * it describes part of inode's space preallocated to specific
186 * physical blocks. any block from that preallocated can be used
187 * independent. the descriptor just tracks number of blocks left
188 * unused. so, before taking some block from descriptor, one must
189 * make sure corresponded logical block isn't allocated yet. this
190 * also means that freeing any block within descriptor's range
191 * must discard all preallocated blocks.
192 * - locality group
193 * assigned to specific locality group which does not translate to
194 * permanent set of inodes: inode can join and leave group. space
195 * from this type of preallocation can be used for any inode. thus
196 * it's consumed from the beginning to the end.
197 *
198 * relation between them can be expressed as:
199 * in-core buddy = on-disk bitmap + preallocation descriptors
200 *
201 * this mean blocks mballoc considers used are:
202 * - allocated blocks (persistent)
203 * - preallocated blocks (non-persistent)
204 *
205 * consistency in mballoc world means that at any time a block is either
206 * free or used in ALL structures. notice: "any time" should not be read
207 * literally -- time is discrete and delimited by locks.
208 *
209 * to keep it simple, we don't use block numbers, instead we count number of
210 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
211 *
212 * all operations can be expressed as:
213 * - init buddy: buddy = on-disk + PAs
214 * - new PA: buddy += N; PA = N
215 * - use inode PA: on-disk += N; PA -= N
216 * - discard inode PA buddy -= on-disk - PA; PA = 0
217 * - use locality group PA on-disk += N; PA -= N
218 * - discard locality group PA buddy -= PA; PA = 0
219 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
220 * is used in real operation because we can't know actual used
221 * bits from PA, only from on-disk bitmap
222 *
223 * if we follow this strict logic, then all operations above should be atomic.
224 * given some of them can block, we'd have to use something like semaphores
225 * killing performance on high-end SMP hardware. let's try to relax it using
226 * the following knowledge:
227 * 1) if buddy is referenced, it's already initialized
228 * 2) while block is used in buddy and the buddy is referenced,
229 * nobody can re-allocate that block
230 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
231 * bit set and PA claims same block, it's OK. IOW, one can set bit in
232 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
233 * block
234 *
235 * so, now we're building a concurrency table:
236 * - init buddy vs.
237 * - new PA
238 * blocks for PA are allocated in the buddy, buddy must be referenced
239 * until PA is linked to allocation group to avoid concurrent buddy init
240 * - use inode PA
241 * we need to make sure that either on-disk bitmap or PA has uptodate data
242 * given (3) we care that PA-=N operation doesn't interfere with init
243 * - discard inode PA
244 * the simplest way would be to have buddy initialized by the discard
245 * - use locality group PA
246 * again PA-=N must be serialized with init
247 * - discard locality group PA
248 * the simplest way would be to have buddy initialized by the discard
249 * - new PA vs.
250 * - use inode PA
251 * i_data_sem serializes them
252 * - discard inode PA
253 * discard process must wait until PA isn't used by another process
254 * - use locality group PA
255 * some mutex should serialize them
256 * - discard locality group PA
257 * discard process must wait until PA isn't used by another process
258 * - use inode PA
259 * - use inode PA
260 * i_data_sem or another mutex should serializes them
261 * - discard inode PA
262 * discard process must wait until PA isn't used by another process
263 * - use locality group PA
264 * nothing wrong here -- they're different PAs covering different blocks
265 * - discard locality group PA
266 * discard process must wait until PA isn't used by another process
267 *
268 * now we're ready to make few consequences:
269 * - PA is referenced and while it is no discard is possible
270 * - PA is referenced until block isn't marked in on-disk bitmap
271 * - PA changes only after on-disk bitmap
272 * - discard must not compete with init. either init is done before
273 * any discard or they're serialized somehow
274 * - buddy init as sum of on-disk bitmap and PAs is done atomically
275 *
276 * a special case when we've used PA to emptiness. no need to modify buddy
277 * in this case, but we should care about concurrent init
278 *
279 */
280
281 /*
282 * Logic in few words:
283 *
284 * - allocation:
285 * load group
286 * find blocks
287 * mark bits in on-disk bitmap
288 * release group
289 *
290 * - use preallocation:
291 * find proper PA (per-inode or group)
292 * load group
293 * mark bits in on-disk bitmap
294 * release group
295 * release PA
296 *
297 * - free:
298 * load group
299 * mark bits in on-disk bitmap
300 * release group
301 *
302 * - discard preallocations in group:
303 * mark PAs deleted
304 * move them onto local list
305 * load on-disk bitmap
306 * load group
307 * remove PA from object (inode or locality group)
308 * mark free blocks in-core
309 *
310 * - discard inode's preallocations:
311 */
312
313/*
314 * Locking rules
315 *
316 * Locks:
317 * - bitlock on a group (group)
318 * - object (inode/locality) (object)
319 * - per-pa lock (pa)
320 *
321 * Paths:
322 * - new pa
323 * object
324 * group
325 *
326 * - find and use pa:
327 * pa
328 *
329 * - release consumed pa:
330 * pa
331 * group
332 * object
333 *
334 * - generate in-core bitmap:
335 * group
336 * pa
337 *
338 * - discard all for given object (inode, locality group):
339 * object
340 * pa
341 * group
342 *
343 * - discard all for given group:
344 * group
345 * pa
346 * group
347 * object
348 *
349 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500350static struct kmem_cache *ext4_pspace_cachep;
351static struct kmem_cache *ext4_ac_cachep;
Bobi Jam18aadd42012-02-20 17:53:02 -0500352static struct kmem_cache *ext4_free_data_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400353
354/* We create slab caches for groupinfo data structures based on the
355 * superblock block size. There will be one per mounted filesystem for
356 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500357#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400358static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
359
Eric Sandeen2892c152011-02-12 08:12:18 -0500360static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
361 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
362 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
363 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
364};
365
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500366static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
367 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500368static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
369 ext4_group_t group);
Bobi Jam18aadd42012-02-20 17:53:02 -0500370static void ext4_free_data_callback(struct super_block *sb,
371 struct ext4_journal_cb_entry *jce, int rc);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500372
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500373static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
374{
Alex Tomasc9de5602008-01-29 00:19:52 -0500375#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500376 *bit += ((unsigned long) addr & 7UL) << 3;
377 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500378#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500379 *bit += ((unsigned long) addr & 3UL) << 3;
380 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500381#else
382#error "how many bits you are?!"
383#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500384 return addr;
385}
Alex Tomasc9de5602008-01-29 00:19:52 -0500386
387static inline int mb_test_bit(int bit, void *addr)
388{
389 /*
390 * ext4_test_bit on architecture like powerpc
391 * needs unsigned long aligned address
392 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500393 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500394 return ext4_test_bit(bit, addr);
395}
396
397static inline void mb_set_bit(int bit, void *addr)
398{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500399 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500400 ext4_set_bit(bit, addr);
401}
402
Alex Tomasc9de5602008-01-29 00:19:52 -0500403static inline void mb_clear_bit(int bit, void *addr)
404{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500405 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500406 ext4_clear_bit(bit, addr);
407}
408
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400409static inline int mb_test_and_clear_bit(int bit, void *addr)
410{
411 addr = mb_correct_addr_and_bit(&bit, addr);
412 return ext4_test_and_clear_bit(bit, addr);
413}
414
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500415static inline int mb_find_next_zero_bit(void *addr, int max, int start)
416{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400417 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500418 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400419 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500420 start += fix;
421
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400422 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
423 if (ret > max)
424 return max;
425 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500426}
427
428static inline int mb_find_next_bit(void *addr, int max, int start)
429{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400430 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500431 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400432 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500433 start += fix;
434
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400435 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
436 if (ret > max)
437 return max;
438 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500439}
440
Alex Tomasc9de5602008-01-29 00:19:52 -0500441static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
442{
443 char *bb;
444
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500445 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -0500446 BUG_ON(max == NULL);
447
448 if (order > e4b->bd_blkbits + 1) {
449 *max = 0;
450 return NULL;
451 }
452
453 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500454 if (order == 0) {
455 *max = 1 << (e4b->bd_blkbits + 3);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500456 return e4b->bd_bitmap;
Coly Li84b775a2011-02-24 12:51:59 -0500457 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500458
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500459 bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
Alex Tomasc9de5602008-01-29 00:19:52 -0500460 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
461
462 return bb;
463}
464
465#ifdef DOUBLE_CHECK
466static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
467 int first, int count)
468{
469 int i;
470 struct super_block *sb = e4b->bd_sb;
471
472 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
473 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400474 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500475 for (i = 0; i < count; i++) {
476 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
477 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500478
479 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400480 blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500481 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400482 inode ? inode->i_ino : 0,
483 blocknr,
484 "freeing block already freed "
485 "(bit %u)",
486 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500487 }
488 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
489 }
490}
491
492static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
493{
494 int i;
495
496 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
497 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400498 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500499 for (i = 0; i < count; i++) {
500 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
501 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
502 }
503}
504
505static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
506{
507 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
508 unsigned char *b1, *b2;
509 int i;
510 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
511 b2 = (unsigned char *) bitmap;
512 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
513 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400514 ext4_msg(e4b->bd_sb, KERN_ERR,
515 "corruption in group %u "
516 "at byte %u(%u): %x in copy != %x "
517 "on disk/prealloc",
518 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500519 BUG();
520 }
521 }
522 }
523}
524
525#else
526static inline void mb_free_blocks_double(struct inode *inode,
527 struct ext4_buddy *e4b, int first, int count)
528{
529 return;
530}
531static inline void mb_mark_used_double(struct ext4_buddy *e4b,
532 int first, int count)
533{
534 return;
535}
536static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
537{
538 return;
539}
540#endif
541
542#ifdef AGGRESSIVE_CHECK
543
544#define MB_CHECK_ASSERT(assert) \
545do { \
546 if (!(assert)) { \
547 printk(KERN_EMERG \
548 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
549 function, file, line, # assert); \
550 BUG(); \
551 } \
552} while (0)
553
554static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
555 const char *function, int line)
556{
557 struct super_block *sb = e4b->bd_sb;
558 int order = e4b->bd_blkbits + 1;
559 int max;
560 int max2;
561 int i;
562 int j;
563 int k;
564 int count;
565 struct ext4_group_info *grp;
566 int fragments = 0;
567 int fstart;
568 struct list_head *cur;
569 void *buddy;
570 void *buddy2;
571
Alex Tomasc9de5602008-01-29 00:19:52 -0500572 {
573 static int mb_check_counter;
574 if (mb_check_counter++ % 100 != 0)
575 return 0;
576 }
577
578 while (order > 1) {
579 buddy = mb_find_buddy(e4b, order, &max);
580 MB_CHECK_ASSERT(buddy);
581 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
582 MB_CHECK_ASSERT(buddy2);
583 MB_CHECK_ASSERT(buddy != buddy2);
584 MB_CHECK_ASSERT(max * 2 == max2);
585
586 count = 0;
587 for (i = 0; i < max; i++) {
588
589 if (mb_test_bit(i, buddy)) {
590 /* only single bit in buddy2 may be 1 */
591 if (!mb_test_bit(i << 1, buddy2)) {
592 MB_CHECK_ASSERT(
593 mb_test_bit((i<<1)+1, buddy2));
594 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
595 MB_CHECK_ASSERT(
596 mb_test_bit(i << 1, buddy2));
597 }
598 continue;
599 }
600
Robin Dong0a10da72011-10-26 08:48:54 -0400601 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500602 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
603 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
604
605 for (j = 0; j < (1 << order); j++) {
606 k = (i * (1 << order)) + j;
607 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500608 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500609 }
610 count++;
611 }
612 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
613 order--;
614 }
615
616 fstart = -1;
617 buddy = mb_find_buddy(e4b, 0, &max);
618 for (i = 0; i < max; i++) {
619 if (!mb_test_bit(i, buddy)) {
620 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
621 if (fstart == -1) {
622 fragments++;
623 fstart = i;
624 }
625 continue;
626 }
627 fstart = -1;
628 /* check used bits only */
629 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
630 buddy2 = mb_find_buddy(e4b, j, &max2);
631 k = i >> j;
632 MB_CHECK_ASSERT(k < max2);
633 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
634 }
635 }
636 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
637 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
638
639 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500640 list_for_each(cur, &grp->bb_prealloc_list) {
641 ext4_group_t groupnr;
642 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400643 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
644 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500645 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400646 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500647 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
648 }
649 return 0;
650}
651#undef MB_CHECK_ASSERT
652#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400653 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500654#else
655#define mb_check_buddy(e4b)
656#endif
657
Coly Li7c786052011-02-24 13:24:25 -0500658/*
659 * Divide blocks started from @first with length @len into
660 * smaller chunks with power of 2 blocks.
661 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
662 * then increase bb_counters[] for corresponded chunk size.
663 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500664static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400665 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500666 struct ext4_group_info *grp)
667{
668 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400669 ext4_grpblk_t min;
670 ext4_grpblk_t max;
671 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500672 unsigned short border;
673
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400674 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500675
676 border = 2 << sb->s_blocksize_bits;
677
678 while (len > 0) {
679 /* find how many blocks can be covered since this position */
680 max = ffs(first | border) - 1;
681
682 /* find how many blocks of power 2 we need to mark */
683 min = fls(len) - 1;
684
685 if (max < min)
686 min = max;
687 chunk = 1 << min;
688
689 /* mark multiblock chunks only */
690 grp->bb_counters[min]++;
691 if (min > 0)
692 mb_clear_bit(first >> min,
693 buddy + sbi->s_mb_offsets[min]);
694
695 len -= chunk;
696 first += chunk;
697 }
698}
699
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400700/*
701 * Cache the order of the largest free extent we have available in this block
702 * group.
703 */
704static void
705mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
706{
707 int i;
708 int bits;
709
710 grp->bb_largest_free_order = -1; /* uninit */
711
712 bits = sb->s_blocksize_bits + 1;
713 for (i = bits; i >= 0; i--) {
714 if (grp->bb_counters[i] > 0) {
715 grp->bb_largest_free_order = i;
716 break;
717 }
718 }
719}
720
Eric Sandeen089ceec2009-07-05 22:17:31 -0400721static noinline_for_stack
722void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500723 void *buddy, void *bitmap, ext4_group_t group)
724{
725 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400726 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400727 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400728 ext4_grpblk_t i = 0;
729 ext4_grpblk_t first;
730 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500731 unsigned free = 0;
732 unsigned fragments = 0;
733 unsigned long long period = get_cycles();
734
735 /* initialize buddy from bitmap which is aggregation
736 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500737 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500738 grp->bb_first_free = i;
739 while (i < max) {
740 fragments++;
741 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500742 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500743 len = i - first;
744 free += len;
745 if (len > 1)
746 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
747 else
748 grp->bb_counters[0]++;
749 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500750 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500751 }
752 grp->bb_fragments = fragments;
753
754 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400755 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o94d4c062014-07-05 19:15:50 -0400756 "block bitmap and bg descriptor "
757 "inconsistent: %u vs %u free clusters",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400758 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500759 /*
Darrick J. Wong163a2032013-08-28 17:35:51 -0400760 * If we intend to continue, we consider group descriptor
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500761 * corrupt and update bb_free using bitmap value
762 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500763 grp->bb_free = free;
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400764 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
765 percpu_counter_sub(&sbi->s_freeclusters_counter,
766 grp->bb_free);
Darrick J. Wong163a2032013-08-28 17:35:51 -0400767 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
Alex Tomasc9de5602008-01-29 00:19:52 -0500768 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400769 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500770
771 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
772
773 period = get_cycles() - period;
774 spin_lock(&EXT4_SB(sb)->s_bal_lock);
775 EXT4_SB(sb)->s_mb_buddies_generated++;
776 EXT4_SB(sb)->s_mb_generation_time += period;
777 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
778}
779
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400780static void mb_regenerate_buddy(struct ext4_buddy *e4b)
781{
782 int count;
783 int order = 1;
784 void *buddy;
785
786 while ((buddy = mb_find_buddy(e4b, order++, &count))) {
787 ext4_set_bits(buddy, 0, count);
788 }
789 e4b->bd_info->bb_fragments = 0;
790 memset(e4b->bd_info->bb_counters, 0,
791 sizeof(*e4b->bd_info->bb_counters) *
792 (e4b->bd_sb->s_blocksize_bits + 2));
793
794 ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy,
795 e4b->bd_bitmap, e4b->bd_group);
796}
797
Alex Tomasc9de5602008-01-29 00:19:52 -0500798/* The buddy information is attached the buddy cache inode
799 * for convenience. The information regarding each group
800 * is loaded via ext4_mb_load_buddy. The information involve
801 * block bitmap and buddy information. The information are
802 * stored in the inode as
803 *
804 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500805 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500806 *
807 *
808 * one block each for bitmap and buddy information.
809 * So for each group we take up 2 blocks. A page can
810 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
811 * So it can have information regarding groups_per_page which
812 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400813 *
814 * Locking note: This routine takes the block group lock of all groups
815 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500816 */
817
818static int ext4_mb_init_cache(struct page *page, char *incore)
819{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400820 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500821 int blocksize;
822 int blocks_per_page;
823 int groups_per_page;
824 int err = 0;
825 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500826 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500827 int first_block;
828 struct super_block *sb;
829 struct buffer_head *bhs;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -0400830 struct buffer_head **bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500831 struct inode *inode;
832 char *data;
833 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400834 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500835
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400836 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500837
838 inode = page->mapping->host;
839 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400840 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500841 blocksize = 1 << inode->i_blkbits;
842 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
843
844 groups_per_page = blocks_per_page >> 1;
845 if (groups_per_page == 0)
846 groups_per_page = 1;
847
848 /* allocate buffer_heads to read bitmaps */
849 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500850 i = sizeof(struct buffer_head *) * groups_per_page;
851 bh = kzalloc(i, GFP_NOFS);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500852 if (bh == NULL) {
853 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500854 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500855 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500856 } else
857 bh = &bhs;
858
859 first_group = page->index * blocks_per_page / 2;
860
861 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500862 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
863 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500864 break;
865
Theodore Ts'o813e5722012-02-20 17:52:46 -0500866 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400867 /*
868 * If page is uptodate then we came here after online resize
869 * which added some new uninitialized group info structs, so
870 * we must skip all initialized uptodate buddies on the page,
871 * which may be currently in use by an allocating task.
872 */
873 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
874 bh[i] = NULL;
875 continue;
876 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500877 if (!(bh[i] = ext4_read_block_bitmap_nowait(sb, group))) {
878 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500879 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500880 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500881 mb_debug(1, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500882 }
883
884 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500885 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
886 if (bh[i] && ext4_wait_block_bitmap(sb, group, bh[i])) {
887 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500888 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500889 }
890 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500891
892 first_block = page->index * blocks_per_page;
893 for (i = 0; i < blocks_per_page; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500894 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400895 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500896 break;
897
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400898 if (!bh[group - first_group])
899 /* skip initialized uptodate buddy */
900 continue;
901
Alex Tomasc9de5602008-01-29 00:19:52 -0500902 /*
903 * data carry information regarding this
904 * particular group in the format specified
905 * above
906 *
907 */
908 data = page_address(page) + (i * blocksize);
909 bitmap = bh[group - first_group]->b_data;
910
911 /*
912 * We place the buddy block and bitmap block
913 * close together
914 */
915 if ((first_block + i) & 1) {
916 /* this is block of buddy */
917 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400918 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500919 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400920 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500921 grinfo = ext4_get_group_info(sb, group);
922 grinfo->bb_fragments = 0;
923 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400924 sizeof(*grinfo->bb_counters) *
925 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500926 /*
927 * incore got set to the group block bitmap below
928 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500929 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400930 /* init the buddy */
931 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500932 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500933 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500934 incore = NULL;
935 } else {
936 /* this is block of bitmap */
937 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400938 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500939 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400940 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500941
942 /* see comments in ext4_mb_put_pa() */
943 ext4_lock_group(sb, group);
944 memcpy(data, bitmap, blocksize);
945
946 /* mark all preallocated blks used in in-core bitmap */
947 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500948 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500949 ext4_unlock_group(sb, group);
950
951 /* set incore so that the buddy information can be
952 * generated using this
953 */
954 incore = data;
955 }
956 }
957 SetPageUptodate(page);
958
959out:
960 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400961 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500962 brelse(bh[i]);
963 if (bh != &bhs)
964 kfree(bh);
965 }
966 return err;
967}
968
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400969/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400970 * Lock the buddy and bitmap pages. This make sure other parallel init_group
971 * on the same buddy page doesn't happen whild holding the buddy page lock.
972 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
973 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400974 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400975static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
976 ext4_group_t group, struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400977{
Amir Goldstein2de88072011-05-09 21:48:13 -0400978 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
979 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400980 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400981 struct page *page;
982
983 e4b->bd_buddy_page = NULL;
984 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400985
986 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
987 /*
988 * the buddy cache inode stores the block bitmap
989 * and buddy information in consecutive blocks.
990 * So for each group we need two blocks.
991 */
992 block = group * 2;
993 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400994 poff = block % blocks_per_page;
995 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
996 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -0400997 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -0400998 BUG_ON(page->mapping != inode->i_mapping);
999 e4b->bd_bitmap_page = page;
1000 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001001
Amir Goldstein2de88072011-05-09 21:48:13 -04001002 if (blocks_per_page >= 2) {
1003 /* buddy and bitmap are on the same page */
1004 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001005 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001006
1007 block++;
1008 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -04001009 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1010 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001011 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001012 BUG_ON(page->mapping != inode->i_mapping);
1013 e4b->bd_buddy_page = page;
1014 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001015}
1016
Amir Goldstein2de88072011-05-09 21:48:13 -04001017static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001018{
Amir Goldstein2de88072011-05-09 21:48:13 -04001019 if (e4b->bd_bitmap_page) {
1020 unlock_page(e4b->bd_bitmap_page);
1021 page_cache_release(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001022 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001023 if (e4b->bd_buddy_page) {
1024 unlock_page(e4b->bd_buddy_page);
1025 page_cache_release(e4b->bd_buddy_page);
1026 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001027}
1028
1029/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001030 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1031 * block group lock of all groups for this page; do not hold the BG lock when
1032 * calling this routine!
1033 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001034static noinline_for_stack
1035int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1036{
1037
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001038 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001039 struct ext4_buddy e4b;
1040 struct page *page;
1041 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001042
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001043 might_sleep();
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001044 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001045 this_grp = ext4_get_group_info(sb, group);
1046 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001047 * This ensures that we don't reinit the buddy cache
1048 * page which map to the group from which we are already
1049 * allocating. If we are looking at the buddy cache we would
1050 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001051 * would have pinned buddy page to page cache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001052 * The call to ext4_mb_get_buddy_page_lock will mark the
1053 * page accessed.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001054 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001055 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b);
1056 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001057 /*
1058 * somebody initialized the group
1059 * return without doing anything
1060 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001061 goto err;
1062 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001063
1064 page = e4b.bd_bitmap_page;
1065 ret = ext4_mb_init_cache(page, NULL);
1066 if (ret)
1067 goto err;
1068 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001069 ret = -EIO;
1070 goto err;
1071 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001072
Amir Goldstein2de88072011-05-09 21:48:13 -04001073 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001074 /*
1075 * If both the bitmap and buddy are in
1076 * the same page we don't need to force
1077 * init the buddy
1078 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001079 ret = 0;
1080 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001081 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001082 /* init buddy cache */
1083 page = e4b.bd_buddy_page;
1084 ret = ext4_mb_init_cache(page, e4b.bd_bitmap);
1085 if (ret)
1086 goto err;
1087 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001088 ret = -EIO;
1089 goto err;
1090 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001091err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001092 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001093 return ret;
1094}
1095
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001096/*
1097 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1098 * block group lock of all groups for this page; do not hold the BG lock when
1099 * calling this routine!
1100 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001101static noinline_for_stack int
1102ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1103 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001104{
Alex Tomasc9de5602008-01-29 00:19:52 -05001105 int blocks_per_page;
1106 int block;
1107 int pnum;
1108 int poff;
1109 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001110 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001111 struct ext4_group_info *grp;
1112 struct ext4_sb_info *sbi = EXT4_SB(sb);
1113 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001114
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001115 might_sleep();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001116 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001117
1118 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001119 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001120
1121 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001122 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001123 e4b->bd_sb = sb;
1124 e4b->bd_group = group;
1125 e4b->bd_buddy_page = NULL;
1126 e4b->bd_bitmap_page = NULL;
1127
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001128 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001129 /*
1130 * we need full data about the group
1131 * to make a good selection
1132 */
1133 ret = ext4_mb_init_group(sb, group);
1134 if (ret)
1135 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001136 }
1137
Alex Tomasc9de5602008-01-29 00:19:52 -05001138 /*
1139 * the buddy cache inode stores the block bitmap
1140 * and buddy information in consecutive blocks.
1141 * So for each group we need two blocks.
1142 */
1143 block = group * 2;
1144 pnum = block / blocks_per_page;
1145 poff = block % blocks_per_page;
1146
1147 /* we could use find_or_create_page(), but it locks page
1148 * what we'd like to avoid in fast path ... */
Mel Gorman2457aec2014-06-04 16:10:31 -07001149 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001150 if (page == NULL || !PageUptodate(page)) {
1151 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001152 /*
1153 * drop the page reference and try
1154 * to get the page with lock. If we
1155 * are not uptodate that implies
1156 * somebody just created the page but
1157 * is yet to initialize the same. So
1158 * wait for it to initialize.
1159 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001160 page_cache_release(page);
1161 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1162 if (page) {
1163 BUG_ON(page->mapping != inode->i_mapping);
1164 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001165 ret = ext4_mb_init_cache(page, NULL);
1166 if (ret) {
1167 unlock_page(page);
1168 goto err;
1169 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001170 mb_cmp_bitmaps(e4b, page_address(page) +
1171 (poff * sb->s_blocksize));
1172 }
1173 unlock_page(page);
1174 }
1175 }
Younger Liuc57ab392014-04-10 23:03:43 -04001176 if (page == NULL) {
1177 ret = -ENOMEM;
1178 goto err;
1179 }
1180 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001181 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001182 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001183 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001184
1185 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001186 e4b->bd_bitmap_page = page;
1187 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001188
1189 block++;
1190 pnum = block / blocks_per_page;
1191 poff = block % blocks_per_page;
1192
Mel Gorman2457aec2014-06-04 16:10:31 -07001193 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001194 if (page == NULL || !PageUptodate(page)) {
1195 if (page)
1196 page_cache_release(page);
1197 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1198 if (page) {
1199 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001200 if (!PageUptodate(page)) {
1201 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1202 if (ret) {
1203 unlock_page(page);
1204 goto err;
1205 }
1206 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001207 unlock_page(page);
1208 }
1209 }
Younger Liuc57ab392014-04-10 23:03:43 -04001210 if (page == NULL) {
1211 ret = -ENOMEM;
1212 goto err;
1213 }
1214 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001215 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001216 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001217 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001218
1219 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001220 e4b->bd_buddy_page = page;
1221 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001222
1223 BUG_ON(e4b->bd_bitmap_page == NULL);
1224 BUG_ON(e4b->bd_buddy_page == NULL);
1225
1226 return 0;
1227
1228err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001229 if (page)
1230 page_cache_release(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001231 if (e4b->bd_bitmap_page)
1232 page_cache_release(e4b->bd_bitmap_page);
1233 if (e4b->bd_buddy_page)
1234 page_cache_release(e4b->bd_buddy_page);
1235 e4b->bd_buddy = NULL;
1236 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001237 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001238}
1239
Jing Zhange39e07f2010-05-14 00:00:00 -04001240static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001241{
1242 if (e4b->bd_bitmap_page)
1243 page_cache_release(e4b->bd_bitmap_page);
1244 if (e4b->bd_buddy_page)
1245 page_cache_release(e4b->bd_buddy_page);
1246}
1247
1248
1249static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1250{
1251 int order = 1;
1252 void *bb;
1253
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001254 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -05001255 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1256
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001257 bb = e4b->bd_buddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05001258 while (order <= e4b->bd_blkbits + 1) {
1259 block = block >> 1;
1260 if (!mb_test_bit(block, bb)) {
1261 /* this block is part of buddy of order 'order' */
1262 return order;
1263 }
1264 bb += 1 << (e4b->bd_blkbits - order);
1265 order++;
1266 }
1267 return 0;
1268}
1269
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001270static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001271{
1272 __u32 *addr;
1273
1274 len = cur + len;
1275 while (cur < len) {
1276 if ((cur & 31) == 0 && (len - cur) >= 32) {
1277 /* fast path: clear whole word at once */
1278 addr = bm + (cur >> 3);
1279 *addr = 0;
1280 cur += 32;
1281 continue;
1282 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001283 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001284 cur++;
1285 }
1286}
1287
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001288/* clear bits in given range
1289 * will return first found zero bit if any, -1 otherwise
1290 */
1291static int mb_test_and_clear_bits(void *bm, int cur, int len)
1292{
1293 __u32 *addr;
1294 int zero_bit = -1;
1295
1296 len = cur + len;
1297 while (cur < len) {
1298 if ((cur & 31) == 0 && (len - cur) >= 32) {
1299 /* fast path: clear whole word at once */
1300 addr = bm + (cur >> 3);
1301 if (*addr != (__u32)(-1) && zero_bit == -1)
1302 zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0);
1303 *addr = 0;
1304 cur += 32;
1305 continue;
1306 }
1307 if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1)
1308 zero_bit = cur;
1309 cur++;
1310 }
1311
1312 return zero_bit;
1313}
1314
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001315void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001316{
1317 __u32 *addr;
1318
1319 len = cur + len;
1320 while (cur < len) {
1321 if ((cur & 31) == 0 && (len - cur) >= 32) {
1322 /* fast path: set whole word at once */
1323 addr = bm + (cur >> 3);
1324 *addr = 0xffffffff;
1325 cur += 32;
1326 continue;
1327 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001328 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001329 cur++;
1330 }
1331}
1332
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001333/*
1334 * _________________________________________________________________ */
1335
1336static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side)
Alex Tomasc9de5602008-01-29 00:19:52 -05001337{
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001338 if (mb_test_bit(*bit + side, bitmap)) {
1339 mb_clear_bit(*bit, bitmap);
1340 (*bit) -= side;
1341 return 1;
1342 }
1343 else {
1344 (*bit) += side;
1345 mb_set_bit(*bit, bitmap);
1346 return -1;
1347 }
1348}
1349
1350static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last)
1351{
1352 int max;
1353 int order = 1;
1354 void *buddy = mb_find_buddy(e4b, order, &max);
1355
1356 while (buddy) {
1357 void *buddy2;
1358
1359 /* Bits in range [first; last] are known to be set since
1360 * corresponding blocks were allocated. Bits in range
1361 * (first; last) will stay set because they form buddies on
1362 * upper layer. We just deal with borders if they don't
1363 * align with upper layer and then go up.
1364 * Releasing entire group is all about clearing
1365 * single bit of highest order buddy.
1366 */
1367
1368 /* Example:
1369 * ---------------------------------
1370 * | 1 | 1 | 1 | 1 |
1371 * ---------------------------------
1372 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1373 * ---------------------------------
1374 * 0 1 2 3 4 5 6 7
1375 * \_____________________/
1376 *
1377 * Neither [1] nor [6] is aligned to above layer.
1378 * Left neighbour [0] is free, so mark it busy,
1379 * decrease bb_counters and extend range to
1380 * [0; 6]
1381 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1382 * mark [6] free, increase bb_counters and shrink range to
1383 * [0; 5].
1384 * Then shift range to [0; 2], go up and do the same.
1385 */
1386
1387
1388 if (first & 1)
1389 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1);
1390 if (!(last & 1))
1391 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1);
1392 if (first > last)
1393 break;
1394 order++;
1395
1396 if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) {
1397 mb_clear_bits(buddy, first, last - first + 1);
1398 e4b->bd_info->bb_counters[order - 1] += last - first + 1;
1399 break;
1400 }
1401 first >>= 1;
1402 last >>= 1;
1403 buddy = buddy2;
1404 }
1405}
1406
1407static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1408 int first, int count)
1409{
1410 int left_is_free = 0;
1411 int right_is_free = 0;
1412 int block;
1413 int last = first + count - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001414 struct super_block *sb = e4b->bd_sb;
1415
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04001416 if (WARN_ON(count == 0))
1417 return;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001418 BUG_ON(last >= (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001419 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Darrick J. Wong163a2032013-08-28 17:35:51 -04001420 /* Don't bother if the block group is corrupt. */
1421 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
1422 return;
1423
Alex Tomasc9de5602008-01-29 00:19:52 -05001424 mb_check_buddy(e4b);
1425 mb_free_blocks_double(inode, e4b, first, count);
1426
1427 e4b->bd_info->bb_free += count;
1428 if (first < e4b->bd_info->bb_first_free)
1429 e4b->bd_info->bb_first_free = first;
1430
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001431 /* access memory sequentially: check left neighbour,
1432 * clear range and then check right neighbour
1433 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001434 if (first != 0)
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001435 left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap);
1436 block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count);
1437 if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0])
1438 right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap);
1439
1440 if (unlikely(block != -1)) {
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001441 struct ext4_sb_info *sbi = EXT4_SB(sb);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001442 ext4_fsblk_t blocknr;
1443
1444 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
1445 blocknr += EXT4_C2B(EXT4_SB(sb), block);
1446 ext4_grp_locked_error(sb, e4b->bd_group,
1447 inode ? inode->i_ino : 0,
1448 blocknr,
1449 "freeing already freed block "
Darrick J. Wong163a2032013-08-28 17:35:51 -04001450 "(bit %u); block bitmap corrupt.",
1451 block);
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001452 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))
1453 percpu_counter_sub(&sbi->s_freeclusters_counter,
1454 e4b->bd_info->bb_free);
Darrick J. Wong163a2032013-08-28 17:35:51 -04001455 /* Mark the block group as corrupt. */
1456 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1457 &e4b->bd_info->bb_state);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001458 mb_regenerate_buddy(e4b);
1459 goto done;
1460 }
1461
1462 /* let's maintain fragments counter */
1463 if (left_is_free && right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001464 e4b->bd_info->bb_fragments--;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001465 else if (!left_is_free && !right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001466 e4b->bd_info->bb_fragments++;
1467
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001468 /* buddy[0] == bd_bitmap is a special case, so handle
1469 * it right away and let mb_buddy_mark_free stay free of
1470 * zero order checks.
1471 * Check if neighbours are to be coaleasced,
1472 * adjust bitmap bb_counters and borders appropriately.
1473 */
1474 if (first & 1) {
1475 first += !left_is_free;
1476 e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001477 }
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001478 if (!(last & 1)) {
1479 last -= !right_is_free;
1480 e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1;
1481 }
1482
1483 if (first <= last)
1484 mb_buddy_mark_free(e4b, first >> 1, last >> 1);
1485
1486done:
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001487 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001488 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001489}
1490
Robin Dong15c006a2012-08-17 10:02:17 -04001491static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001492 int needed, struct ext4_free_extent *ex)
1493{
1494 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001495 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001496 void *buddy;
1497
Vincent Minetbc8e6742009-05-15 08:33:18 -04001498 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001499 BUG_ON(ex == NULL);
1500
Robin Dong15c006a2012-08-17 10:02:17 -04001501 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001502 BUG_ON(buddy == NULL);
1503 BUG_ON(block >= max);
1504 if (mb_test_bit(block, buddy)) {
1505 ex->fe_len = 0;
1506 ex->fe_start = 0;
1507 ex->fe_group = 0;
1508 return 0;
1509 }
1510
Robin Dong15c006a2012-08-17 10:02:17 -04001511 /* find actual order */
1512 order = mb_find_order_for_block(e4b, block);
1513 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001514
1515 ex->fe_len = 1 << order;
1516 ex->fe_start = block << order;
1517 ex->fe_group = e4b->bd_group;
1518
1519 /* calc difference from given start */
1520 next = next - ex->fe_start;
1521 ex->fe_len -= next;
1522 ex->fe_start += next;
1523
1524 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001525 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001526
1527 if (block + 1 >= max)
1528 break;
1529
1530 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001531 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001532 break;
1533
Robin Dongb051d8d2011-10-26 05:30:30 -04001534 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001535
Alex Tomasc9de5602008-01-29 00:19:52 -05001536 block = next >> order;
1537 ex->fe_len += 1 << order;
1538 }
1539
1540 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1541 return ex->fe_len;
1542}
1543
1544static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1545{
1546 int ord;
1547 int mlen = 0;
1548 int max = 0;
1549 int cur;
1550 int start = ex->fe_start;
1551 int len = ex->fe_len;
1552 unsigned ret = 0;
1553 int len0 = len;
1554 void *buddy;
1555
1556 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1557 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001558 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001559 mb_check_buddy(e4b);
1560 mb_mark_used_double(e4b, start, len);
1561
1562 e4b->bd_info->bb_free -= len;
1563 if (e4b->bd_info->bb_first_free == start)
1564 e4b->bd_info->bb_first_free += len;
1565
1566 /* let's maintain fragments counter */
1567 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001568 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001569 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001570 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001571 if (mlen && max)
1572 e4b->bd_info->bb_fragments++;
1573 else if (!mlen && !max)
1574 e4b->bd_info->bb_fragments--;
1575
1576 /* let's maintain buddy itself */
1577 while (len) {
1578 ord = mb_find_order_for_block(e4b, start);
1579
1580 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1581 /* the whole chunk may be allocated at once! */
1582 mlen = 1 << ord;
1583 buddy = mb_find_buddy(e4b, ord, &max);
1584 BUG_ON((start >> ord) >= max);
1585 mb_set_bit(start >> ord, buddy);
1586 e4b->bd_info->bb_counters[ord]--;
1587 start += mlen;
1588 len -= mlen;
1589 BUG_ON(len < 0);
1590 continue;
1591 }
1592
1593 /* store for history */
1594 if (ret == 0)
1595 ret = len | (ord << 16);
1596
1597 /* we have to split large buddy */
1598 BUG_ON(ord <= 0);
1599 buddy = mb_find_buddy(e4b, ord, &max);
1600 mb_set_bit(start >> ord, buddy);
1601 e4b->bd_info->bb_counters[ord]--;
1602
1603 ord--;
1604 cur = (start >> ord) & ~1U;
1605 buddy = mb_find_buddy(e4b, ord, &max);
1606 mb_clear_bit(cur, buddy);
1607 mb_clear_bit(cur + 1, buddy);
1608 e4b->bd_info->bb_counters[ord]++;
1609 e4b->bd_info->bb_counters[ord]++;
1610 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001611 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001612
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001613 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001614 mb_check_buddy(e4b);
1615
1616 return ret;
1617}
1618
1619/*
1620 * Must be called under group lock!
1621 */
1622static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1623 struct ext4_buddy *e4b)
1624{
1625 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1626 int ret;
1627
1628 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1629 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1630
1631 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1632 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1633 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1634
1635 /* preallocation can change ac_b_ex, thus we store actually
1636 * allocated blocks for history */
1637 ac->ac_f_ex = ac->ac_b_ex;
1638
1639 ac->ac_status = AC_STATUS_FOUND;
1640 ac->ac_tail = ret & 0xffff;
1641 ac->ac_buddy = ret >> 16;
1642
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001643 /*
1644 * take the page reference. We want the page to be pinned
1645 * so that we don't get a ext4_mb_init_cache_call for this
1646 * group until we update the bitmap. That would mean we
1647 * double allocate blocks. The reference is dropped
1648 * in ext4_mb_release_context
1649 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001650 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1651 get_page(ac->ac_bitmap_page);
1652 ac->ac_buddy_page = e4b->bd_buddy_page;
1653 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001654 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001655 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001656 spin_lock(&sbi->s_md_lock);
1657 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1658 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1659 spin_unlock(&sbi->s_md_lock);
1660 }
1661}
1662
1663/*
1664 * regular allocator, for general purposes allocation
1665 */
1666
1667static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1668 struct ext4_buddy *e4b,
1669 int finish_group)
1670{
1671 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1672 struct ext4_free_extent *bex = &ac->ac_b_ex;
1673 struct ext4_free_extent *gex = &ac->ac_g_ex;
1674 struct ext4_free_extent ex;
1675 int max;
1676
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001677 if (ac->ac_status == AC_STATUS_FOUND)
1678 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001679 /*
1680 * We don't want to scan for a whole year
1681 */
1682 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1683 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1684 ac->ac_status = AC_STATUS_BREAK;
1685 return;
1686 }
1687
1688 /*
1689 * Haven't found good chunk so far, let's continue
1690 */
1691 if (bex->fe_len < gex->fe_len)
1692 return;
1693
1694 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1695 && bex->fe_group == e4b->bd_group) {
1696 /* recheck chunk's availability - we don't know
1697 * when it was found (within this lock-unlock
1698 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001699 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001700 if (max >= gex->fe_len) {
1701 ext4_mb_use_best_found(ac, e4b);
1702 return;
1703 }
1704 }
1705}
1706
1707/*
1708 * The routine checks whether found extent is good enough. If it is,
1709 * then the extent gets marked used and flag is set to the context
1710 * to stop scanning. Otherwise, the extent is compared with the
1711 * previous found extent and if new one is better, then it's stored
1712 * in the context. Later, the best found extent will be used, if
1713 * mballoc can't find good enough extent.
1714 *
1715 * FIXME: real allocation policy is to be designed yet!
1716 */
1717static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1718 struct ext4_free_extent *ex,
1719 struct ext4_buddy *e4b)
1720{
1721 struct ext4_free_extent *bex = &ac->ac_b_ex;
1722 struct ext4_free_extent *gex = &ac->ac_g_ex;
1723
1724 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001725 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1726 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001727 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1728
1729 ac->ac_found++;
1730
1731 /*
1732 * The special case - take what you catch first
1733 */
1734 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1735 *bex = *ex;
1736 ext4_mb_use_best_found(ac, e4b);
1737 return;
1738 }
1739
1740 /*
1741 * Let's check whether the chuck is good enough
1742 */
1743 if (ex->fe_len == gex->fe_len) {
1744 *bex = *ex;
1745 ext4_mb_use_best_found(ac, e4b);
1746 return;
1747 }
1748
1749 /*
1750 * If this is first found extent, just store it in the context
1751 */
1752 if (bex->fe_len == 0) {
1753 *bex = *ex;
1754 return;
1755 }
1756
1757 /*
1758 * If new found extent is better, store it in the context
1759 */
1760 if (bex->fe_len < gex->fe_len) {
1761 /* if the request isn't satisfied, any found extent
1762 * larger than previous best one is better */
1763 if (ex->fe_len > bex->fe_len)
1764 *bex = *ex;
1765 } else if (ex->fe_len > gex->fe_len) {
1766 /* if the request is satisfied, then we try to find
1767 * an extent that still satisfy the request, but is
1768 * smaller than previous one */
1769 if (ex->fe_len < bex->fe_len)
1770 *bex = *ex;
1771 }
1772
1773 ext4_mb_check_limits(ac, e4b, 0);
1774}
1775
Eric Sandeen089ceec2009-07-05 22:17:31 -04001776static noinline_for_stack
1777int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001778 struct ext4_buddy *e4b)
1779{
1780 struct ext4_free_extent ex = ac->ac_b_ex;
1781 ext4_group_t group = ex.fe_group;
1782 int max;
1783 int err;
1784
1785 BUG_ON(ex.fe_len <= 0);
1786 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1787 if (err)
1788 return err;
1789
1790 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001791 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001792
1793 if (max > 0) {
1794 ac->ac_b_ex = ex;
1795 ext4_mb_use_best_found(ac, e4b);
1796 }
1797
1798 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001799 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001800
1801 return 0;
1802}
1803
Eric Sandeen089ceec2009-07-05 22:17:31 -04001804static noinline_for_stack
1805int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001806 struct ext4_buddy *e4b)
1807{
1808 ext4_group_t group = ac->ac_g_ex.fe_group;
1809 int max;
1810 int err;
1811 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001812 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001813 struct ext4_free_extent ex;
1814
1815 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1816 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001817 if (grp->bb_free == 0)
1818 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001819
1820 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1821 if (err)
1822 return err;
1823
Darrick J. Wong163a2032013-08-28 17:35:51 -04001824 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) {
1825 ext4_mb_unload_buddy(e4b);
1826 return 0;
1827 }
1828
Alex Tomasc9de5602008-01-29 00:19:52 -05001829 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001830 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001831 ac->ac_g_ex.fe_len, &ex);
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001832 ex.fe_logical = 0xDEADFA11; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001833
1834 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1835 ext4_fsblk_t start;
1836
Akinobu Mita5661bd62010-03-03 23:53:39 -05001837 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1838 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001839 /* use do_div to get remainder (would be 64-bit modulo) */
1840 if (do_div(start, sbi->s_stripe) == 0) {
1841 ac->ac_found++;
1842 ac->ac_b_ex = ex;
1843 ext4_mb_use_best_found(ac, e4b);
1844 }
1845 } else if (max >= ac->ac_g_ex.fe_len) {
1846 BUG_ON(ex.fe_len <= 0);
1847 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1848 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1849 ac->ac_found++;
1850 ac->ac_b_ex = ex;
1851 ext4_mb_use_best_found(ac, e4b);
1852 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1853 /* Sometimes, caller may want to merge even small
1854 * number of blocks to an existing extent */
1855 BUG_ON(ex.fe_len <= 0);
1856 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1857 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1858 ac->ac_found++;
1859 ac->ac_b_ex = ex;
1860 ext4_mb_use_best_found(ac, e4b);
1861 }
1862 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001863 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001864
1865 return 0;
1866}
1867
1868/*
1869 * The routine scans buddy structures (not bitmap!) from given order
1870 * to max order and tries to find big enough chunk to satisfy the req
1871 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001872static noinline_for_stack
1873void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001874 struct ext4_buddy *e4b)
1875{
1876 struct super_block *sb = ac->ac_sb;
1877 struct ext4_group_info *grp = e4b->bd_info;
1878 void *buddy;
1879 int i;
1880 int k;
1881 int max;
1882
1883 BUG_ON(ac->ac_2order <= 0);
1884 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1885 if (grp->bb_counters[i] == 0)
1886 continue;
1887
1888 buddy = mb_find_buddy(e4b, i, &max);
1889 BUG_ON(buddy == NULL);
1890
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001891 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001892 BUG_ON(k >= max);
1893
1894 ac->ac_found++;
1895
1896 ac->ac_b_ex.fe_len = 1 << i;
1897 ac->ac_b_ex.fe_start = k << i;
1898 ac->ac_b_ex.fe_group = e4b->bd_group;
1899
1900 ext4_mb_use_best_found(ac, e4b);
1901
1902 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1903
1904 if (EXT4_SB(sb)->s_mb_stats)
1905 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1906
1907 break;
1908 }
1909}
1910
1911/*
1912 * The routine scans the group and measures all found extents.
1913 * In order to optimize scanning, caller must pass number of
1914 * free blocks in the group, so the routine can know upper limit.
1915 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001916static noinline_for_stack
1917void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001918 struct ext4_buddy *e4b)
1919{
1920 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001921 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001922 struct ext4_free_extent ex;
1923 int i;
1924 int free;
1925
1926 free = e4b->bd_info->bb_free;
1927 BUG_ON(free <= 0);
1928
1929 i = e4b->bd_info->bb_first_free;
1930
1931 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001932 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001933 EXT4_CLUSTERS_PER_GROUP(sb), i);
1934 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001935 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001936 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001937 * free blocks even though group info says we
1938 * we have free blocks
1939 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001940 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001941 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001942 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001943 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001944 break;
1945 }
1946
Robin Dong15c006a2012-08-17 10:02:17 -04001947 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001948 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001949 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001950 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001951 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001952 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001953 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001954 /*
1955 * The number of free blocks differs. This mostly
1956 * indicate that the bitmap is corrupt. So exit
1957 * without claiming the space.
1958 */
1959 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001960 }
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001961 ex.fe_logical = 0xDEADC0DE; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001962 ext4_mb_measure_extent(ac, &ex, e4b);
1963
1964 i += ex.fe_len;
1965 free -= ex.fe_len;
1966 }
1967
1968 ext4_mb_check_limits(ac, e4b, 1);
1969}
1970
1971/*
1972 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001973 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001974 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001975static noinline_for_stack
1976void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001977 struct ext4_buddy *e4b)
1978{
1979 struct super_block *sb = ac->ac_sb;
1980 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001981 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001982 struct ext4_free_extent ex;
1983 ext4_fsblk_t first_group_block;
1984 ext4_fsblk_t a;
1985 ext4_grpblk_t i;
1986 int max;
1987
1988 BUG_ON(sbi->s_stripe == 0);
1989
1990 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001991 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1992
Alex Tomasc9de5602008-01-29 00:19:52 -05001993 a = first_group_block + sbi->s_stripe - 1;
1994 do_div(a, sbi->s_stripe);
1995 i = (a * sbi->s_stripe) - first_group_block;
1996
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001997 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001998 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04001999 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002000 if (max >= sbi->s_stripe) {
2001 ac->ac_found++;
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002002 ex.fe_logical = 0xDEADF00D; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002003 ac->ac_b_ex = ex;
2004 ext4_mb_use_best_found(ac, e4b);
2005 break;
2006 }
2007 }
2008 i += sbi->s_stripe;
2009 }
2010}
2011
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002012/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05002013static int ext4_mb_good_group(struct ext4_allocation_context *ac,
2014 ext4_group_t group, int cr)
2015{
2016 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002017 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002018 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2019
2020 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002021
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002022 free = grp->bb_free;
2023 if (free == 0)
2024 return 0;
2025 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
2026 return 0;
2027
Darrick J. Wong163a2032013-08-28 17:35:51 -04002028 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
2029 return 0;
2030
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002031 /* We only do this if the grp has never been initialized */
2032 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
2033 int ret = ext4_mb_init_group(ac->ac_sb, group);
2034 if (ret)
2035 return 0;
2036 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002037
Alex Tomasc9de5602008-01-29 00:19:52 -05002038 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05002039 if (fragments == 0)
2040 return 0;
2041
2042 switch (cr) {
2043 case 0:
2044 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05002045
Theodore Ts'oa4912122009-03-12 12:18:34 -04002046 /* Avoid using the first bg of a flexgroup for data files */
2047 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
2048 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
2049 ((group % flex_size) == 0))
2050 return 0;
2051
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002052 if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) ||
2053 (free / fragments) >= ac->ac_g_ex.fe_len)
2054 return 1;
2055
2056 if (grp->bb_largest_free_order < ac->ac_2order)
2057 return 0;
2058
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002059 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002060 case 1:
2061 if ((free / fragments) >= ac->ac_g_ex.fe_len)
2062 return 1;
2063 break;
2064 case 2:
2065 if (free >= ac->ac_g_ex.fe_len)
2066 return 1;
2067 break;
2068 case 3:
2069 return 1;
2070 default:
2071 BUG();
2072 }
2073
2074 return 0;
2075}
2076
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002077static noinline_for_stack int
2078ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002079{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002080 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002081 int cr;
2082 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002083 struct ext4_sb_info *sbi;
2084 struct super_block *sb;
2085 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002086
2087 sb = ac->ac_sb;
2088 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002089 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002090 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002091 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002092 ngroups = sbi->s_blockfile_groups;
2093
Alex Tomasc9de5602008-01-29 00:19:52 -05002094 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2095
2096 /* first, try the goal */
2097 err = ext4_mb_find_by_goal(ac, &e4b);
2098 if (err || ac->ac_status == AC_STATUS_FOUND)
2099 goto out;
2100
2101 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2102 goto out;
2103
2104 /*
2105 * ac->ac2_order is set only if the fe_len is a power of 2
2106 * if ac2_order is set we also set criteria to 0 so that we
2107 * try exact allocation using buddy.
2108 */
2109 i = fls(ac->ac_g_ex.fe_len);
2110 ac->ac_2order = 0;
2111 /*
2112 * We search using buddy data only if the order of the request
2113 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002114 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05002115 */
2116 if (i >= sbi->s_mb_order2_reqs) {
2117 /*
2118 * This should tell if fe_len is exactly power of 2
2119 */
2120 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2121 ac->ac_2order = i - 1;
2122 }
2123
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002124 /* if stream allocation is enabled, use global goal */
2125 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002126 /* TBD: may be hot point */
2127 spin_lock(&sbi->s_md_lock);
2128 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2129 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2130 spin_unlock(&sbi->s_md_lock);
2131 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002132
Alex Tomasc9de5602008-01-29 00:19:52 -05002133 /* Let's just scan groups to find more-less suitable blocks */
2134 cr = ac->ac_2order ? 0 : 1;
2135 /*
2136 * cr == 0 try to get exact allocation,
2137 * cr == 3 try to get anything
2138 */
2139repeat:
2140 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2141 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002142 /*
2143 * searching for the right group start
2144 * from the goal value specified
2145 */
2146 group = ac->ac_g_ex.fe_group;
2147
Theodore Ts'o8df96752009-05-01 08:50:38 -04002148 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o2ed57242013-06-12 11:43:02 -04002149 cond_resched();
Lachlan McIlroye6155732013-05-05 23:10:00 -04002150 /*
2151 * Artificially restricted ngroups for non-extent
2152 * files makes group > ngroups possible on first loop.
2153 */
2154 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002155 group = 0;
2156
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002157 /* This now checks without needing the buddy page */
2158 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002159 continue;
2160
Alex Tomasc9de5602008-01-29 00:19:52 -05002161 err = ext4_mb_load_buddy(sb, group, &e4b);
2162 if (err)
2163 goto out;
2164
2165 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002166
2167 /*
2168 * We need to check again after locking the
2169 * block group
2170 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002171 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002172 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002173 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002174 continue;
2175 }
2176
2177 ac->ac_groups_scanned++;
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002178 if (cr == 0 && ac->ac_2order < sb->s_blocksize_bits+2)
Alex Tomasc9de5602008-01-29 00:19:52 -05002179 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002180 else if (cr == 1 && sbi->s_stripe &&
2181 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002182 ext4_mb_scan_aligned(ac, &e4b);
2183 else
2184 ext4_mb_complex_scan_group(ac, &e4b);
2185
2186 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002187 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002188
2189 if (ac->ac_status != AC_STATUS_CONTINUE)
2190 break;
2191 }
2192 }
2193
2194 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2195 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2196 /*
2197 * We've been searching too long. Let's try to allocate
2198 * the best chunk we've found so far
2199 */
2200
2201 ext4_mb_try_best_found(ac, &e4b);
2202 if (ac->ac_status != AC_STATUS_FOUND) {
2203 /*
2204 * Someone more lucky has already allocated it.
2205 * The only thing we can do is just take first
2206 * found block(s)
2207 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2208 */
2209 ac->ac_b_ex.fe_group = 0;
2210 ac->ac_b_ex.fe_start = 0;
2211 ac->ac_b_ex.fe_len = 0;
2212 ac->ac_status = AC_STATUS_CONTINUE;
2213 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2214 cr = 3;
2215 atomic_inc(&sbi->s_mb_lost_chunks);
2216 goto repeat;
2217 }
2218 }
2219out:
2220 return err;
2221}
2222
Alex Tomasc9de5602008-01-29 00:19:52 -05002223static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2224{
2225 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002226 ext4_group_t group;
2227
Theodore Ts'o8df96752009-05-01 08:50:38 -04002228 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002229 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002230 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002231 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002232}
2233
2234static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2235{
2236 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002237 ext4_group_t group;
2238
2239 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002240 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002241 return NULL;
2242 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002243 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002244}
2245
2246static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2247{
2248 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002249 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002250 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002251 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002252 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002253 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -05002254 struct sg {
2255 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002256 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002257 } sg;
2258
2259 group--;
2260 if (group == 0)
2261 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2262 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2263 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2264 "group", "free", "frags", "first",
2265 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2266 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2267
2268 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2269 sizeof(struct ext4_group_info);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002270 grinfo = ext4_get_group_info(sb, group);
2271 /* Load the group info in memory only if not already loaded. */
2272 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2273 err = ext4_mb_load_buddy(sb, group, &e4b);
2274 if (err) {
2275 seq_printf(seq, "#%-5u: I/O error\n", group);
2276 return 0;
2277 }
2278 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002279 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002280
Alex Tomasc9de5602008-01-29 00:19:52 -05002281 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002282
2283 if (buddy_loaded)
2284 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002285
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002286 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002287 sg.info.bb_fragments, sg.info.bb_first_free);
2288 for (i = 0; i <= 13; i++)
2289 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2290 sg.info.bb_counters[i] : 0);
2291 seq_printf(seq, " ]\n");
2292
2293 return 0;
2294}
2295
2296static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2297{
2298}
2299
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002300static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002301 .start = ext4_mb_seq_groups_start,
2302 .next = ext4_mb_seq_groups_next,
2303 .stop = ext4_mb_seq_groups_stop,
2304 .show = ext4_mb_seq_groups_show,
2305};
2306
2307static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2308{
Al Virod9dda782013-03-31 18:16:14 -04002309 struct super_block *sb = PDE_DATA(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05002310 int rc;
2311
2312 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2313 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002314 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002315 m->private = sb;
2316 }
2317 return rc;
2318
2319}
2320
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002321static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002322 .owner = THIS_MODULE,
2323 .open = ext4_mb_seq_groups_open,
2324 .read = seq_read,
2325 .llseek = seq_lseek,
2326 .release = seq_release,
2327};
2328
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002329static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2330{
2331 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2332 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2333
2334 BUG_ON(!cachep);
2335 return cachep;
2336}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002337
Theodore Ts'o28623c22012-09-05 01:31:50 -04002338/*
2339 * Allocate the top-level s_group_info array for the specified number
2340 * of groups
2341 */
2342int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2343{
2344 struct ext4_sb_info *sbi = EXT4_SB(sb);
2345 unsigned size;
2346 struct ext4_group_info ***new_groupinfo;
2347
2348 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2349 EXT4_DESC_PER_BLOCK_BITS(sb);
2350 if (size <= sbi->s_group_info_size)
2351 return 0;
2352
2353 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
2354 new_groupinfo = ext4_kvzalloc(size, GFP_KERNEL);
2355 if (!new_groupinfo) {
2356 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2357 return -ENOMEM;
2358 }
2359 if (sbi->s_group_info) {
2360 memcpy(new_groupinfo, sbi->s_group_info,
2361 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
Al Virob93b41d2014-11-20 12:19:11 -05002362 kvfree(sbi->s_group_info);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002363 }
2364 sbi->s_group_info = new_groupinfo;
2365 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
2366 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2367 sbi->s_group_info_size);
2368 return 0;
2369}
2370
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002371/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002372int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002373 struct ext4_group_desc *desc)
2374{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002375 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002376 int metalen = 0;
2377 struct ext4_sb_info *sbi = EXT4_SB(sb);
2378 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002379 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002380
2381 /*
2382 * First check if this group is the first of a reserved block.
2383 * If it's true, we have to allocate a new table of pointers
2384 * to ext4_group_info structures
2385 */
2386 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2387 metalen = sizeof(*meta_group_info) <<
2388 EXT4_DESC_PER_BLOCK_BITS(sb);
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002389 meta_group_info = kmalloc(metalen, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002390 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002391 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002392 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002393 goto exit_meta_group_info;
2394 }
2395 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2396 meta_group_info;
2397 }
2398
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002399 meta_group_info =
2400 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2401 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2402
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002403 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002404 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002405 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002406 goto exit_group_info;
2407 }
2408 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2409 &(meta_group_info[i]->bb_state));
2410
2411 /*
2412 * initialize bb_free to be able to skip
2413 * empty groups without initialization
2414 */
2415 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2416 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002417 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002418 } else {
2419 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002420 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002421 }
2422
2423 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002424 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002425 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002426 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002427
2428#ifdef DOUBLE_CHECK
2429 {
2430 struct buffer_head *bh;
2431 meta_group_info[i]->bb_bitmap =
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002432 kmalloc(sb->s_blocksize, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002433 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2434 bh = ext4_read_block_bitmap(sb, group);
2435 BUG_ON(bh == NULL);
2436 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2437 sb->s_blocksize);
2438 put_bh(bh);
2439 }
2440#endif
2441
2442 return 0;
2443
2444exit_group_info:
2445 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002446 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002447 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002448 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2449 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002450exit_meta_group_info:
2451 return -ENOMEM;
2452} /* ext4_mb_add_groupinfo */
2453
Alex Tomasc9de5602008-01-29 00:19:52 -05002454static int ext4_mb_init_backend(struct super_block *sb)
2455{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002456 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002457 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002458 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002459 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002460 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002461 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002462
Theodore Ts'o28623c22012-09-05 01:31:50 -04002463 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2464 if (err)
2465 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002466
Alex Tomasc9de5602008-01-29 00:19:52 -05002467 sbi->s_buddy_cache = new_inode(sb);
2468 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002469 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002470 goto err_freesgi;
2471 }
Yu Jian48e60612011-08-01 17:41:39 -04002472 /* To avoid potentially colliding with an valid on-disk inode number,
2473 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2474 * not in the inode hash, so it should never be found by iget(), but
2475 * this will avoid confusion if it ever shows up during debugging. */
2476 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002477 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002478 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002479 desc = ext4_get_group_desc(sb, i, NULL);
2480 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002481 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002482 goto err_freebuddy;
2483 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002484 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2485 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002486 }
2487
2488 return 0;
2489
2490err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002491 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002492 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002493 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002494 i = sbi->s_group_info_size;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002495 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002496 kfree(sbi->s_group_info[i]);
2497 iput(sbi->s_buddy_cache);
2498err_freesgi:
Al Virob93b41d2014-11-20 12:19:11 -05002499 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002500 return -ENOMEM;
2501}
2502
Eric Sandeen2892c152011-02-12 08:12:18 -05002503static void ext4_groupinfo_destroy_slabs(void)
2504{
2505 int i;
2506
2507 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2508 if (ext4_groupinfo_caches[i])
2509 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2510 ext4_groupinfo_caches[i] = NULL;
2511 }
2512}
2513
2514static int ext4_groupinfo_create_slab(size_t size)
2515{
2516 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2517 int slab_size;
2518 int blocksize_bits = order_base_2(size);
2519 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2520 struct kmem_cache *cachep;
2521
2522 if (cache_index >= NR_GRPINFO_CACHES)
2523 return -EINVAL;
2524
2525 if (unlikely(cache_index < 0))
2526 cache_index = 0;
2527
2528 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2529 if (ext4_groupinfo_caches[cache_index]) {
2530 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2531 return 0; /* Already created */
2532 }
2533
2534 slab_size = offsetof(struct ext4_group_info,
2535 bb_counters[blocksize_bits + 2]);
2536
2537 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2538 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2539 NULL);
2540
Tao Ma823ba012011-07-11 18:26:01 -04002541 ext4_groupinfo_caches[cache_index] = cachep;
2542
Eric Sandeen2892c152011-02-12 08:12:18 -05002543 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2544 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002545 printk(KERN_EMERG
2546 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002547 return -ENOMEM;
2548 }
2549
Eric Sandeen2892c152011-02-12 08:12:18 -05002550 return 0;
2551}
2552
Akira Fujita9d990122012-05-28 14:19:25 -04002553int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002554{
2555 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002556 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002557 unsigned offset;
2558 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002559 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002560
Eric Sandeen19278052009-08-25 22:36:25 -04002561 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002562
2563 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2564 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002565 ret = -ENOMEM;
2566 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002567 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002568
Eric Sandeen19278052009-08-25 22:36:25 -04002569 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002570 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2571 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002572 ret = -ENOMEM;
2573 goto out;
2574 }
2575
Eric Sandeen2892c152011-02-12 08:12:18 -05002576 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2577 if (ret < 0)
2578 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002579
2580 /* order 0 is regular bitmap */
2581 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2582 sbi->s_mb_offsets[0] = 0;
2583
2584 i = 1;
2585 offset = 0;
2586 max = sb->s_blocksize << 2;
2587 do {
2588 sbi->s_mb_offsets[i] = offset;
2589 sbi->s_mb_maxs[i] = max;
2590 offset += 1 << (sb->s_blocksize_bits - i);
2591 max = max >> 1;
2592 i++;
2593 } while (i <= sb->s_blocksize_bits + 1);
2594
Alex Tomasc9de5602008-01-29 00:19:52 -05002595 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002596 spin_lock_init(&sbi->s_bal_lock);
2597
2598 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2599 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2600 sbi->s_mb_stats = MB_DEFAULT_STATS;
2601 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2602 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002603 /*
2604 * The default group preallocation is 512, which for 4k block
2605 * sizes translates to 2 megabytes. However for bigalloc file
2606 * systems, this is probably too big (i.e, if the cluster size
2607 * is 1 megabyte, then group preallocation size becomes half a
2608 * gigabyte!). As a default, we will keep a two megabyte
2609 * group pralloc size for cluster sizes up to 64k, and after
2610 * that, we will force a minimum group preallocation size of
2611 * 32 clusters. This translates to 8 megs when the cluster
2612 * size is 256k, and 32 megs when the cluster size is 1 meg,
2613 * which seems reasonable as a default.
2614 */
2615 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2616 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002617 /*
2618 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2619 * to the lowest multiple of s_stripe which is bigger than
2620 * the s_mb_group_prealloc as determined above. We want
2621 * the preallocation size to be an exact multiple of the
2622 * RAID stripe size so that preallocations don't fragment
2623 * the stripes.
2624 */
2625 if (sbi->s_stripe > 1) {
2626 sbi->s_mb_group_prealloc = roundup(
2627 sbi->s_mb_group_prealloc, sbi->s_stripe);
2628 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002629
Eric Sandeen730c2132008-09-13 15:23:29 -04002630 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002631 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002632 ret = -ENOMEM;
Andrey Tsyvarev029b10c2014-05-12 12:34:21 -04002633 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002634 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002635 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002636 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002637 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002638 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002639 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2640 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002641 spin_lock_init(&lg->lg_prealloc_lock);
2642 }
2643
Yu Jian79a77c52011-08-01 17:41:46 -04002644 /* init file for buddy data */
2645 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002646 if (ret != 0)
2647 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002648
Theodore Ts'o296c3552009-09-30 00:32:42 -04002649 if (sbi->s_proc)
2650 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2651 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002652
Tao Ma7aa0bae2011-10-06 10:22:28 -04002653 return 0;
2654
2655out_free_locality_groups:
2656 free_percpu(sbi->s_locality_groups);
2657 sbi->s_locality_groups = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002658out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002659 kfree(sbi->s_mb_offsets);
2660 sbi->s_mb_offsets = NULL;
2661 kfree(sbi->s_mb_maxs);
2662 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002663 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002664}
2665
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002666/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002667static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2668{
2669 struct ext4_prealloc_space *pa;
2670 struct list_head *cur, *tmp;
2671 int count = 0;
2672
2673 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2674 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2675 list_del(&pa->pa_group_list);
2676 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002677 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002678 }
2679 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002680 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002681
2682}
2683
2684int ext4_mb_release(struct super_block *sb)
2685{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002686 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002687 ext4_group_t i;
2688 int num_meta_group_infos;
2689 struct ext4_group_info *grinfo;
2690 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002691 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002692
Salman Qazi95599962012-05-31 23:52:14 -04002693 if (sbi->s_proc)
2694 remove_proc_entry("mb_groups", sbi->s_proc);
2695
Alex Tomasc9de5602008-01-29 00:19:52 -05002696 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002697 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002698 grinfo = ext4_get_group_info(sb, i);
2699#ifdef DOUBLE_CHECK
2700 kfree(grinfo->bb_bitmap);
2701#endif
2702 ext4_lock_group(sb, i);
2703 ext4_mb_cleanup_pa(grinfo);
2704 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002705 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002706 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002707 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002708 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2709 EXT4_DESC_PER_BLOCK_BITS(sb);
2710 for (i = 0; i < num_meta_group_infos; i++)
2711 kfree(sbi->s_group_info[i]);
Al Virob93b41d2014-11-20 12:19:11 -05002712 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002713 }
2714 kfree(sbi->s_mb_offsets);
2715 kfree(sbi->s_mb_maxs);
Markus Elfringbfcba2d2014-11-25 20:01:37 -05002716 iput(sbi->s_buddy_cache);
Alex Tomasc9de5602008-01-29 00:19:52 -05002717 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002718 ext4_msg(sb, KERN_INFO,
2719 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002720 atomic_read(&sbi->s_bal_allocated),
2721 atomic_read(&sbi->s_bal_reqs),
2722 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002723 ext4_msg(sb, KERN_INFO,
2724 "mballoc: %u extents scanned, %u goal hits, "
2725 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002726 atomic_read(&sbi->s_bal_ex_scanned),
2727 atomic_read(&sbi->s_bal_goals),
2728 atomic_read(&sbi->s_bal_2orders),
2729 atomic_read(&sbi->s_bal_breaks),
2730 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002731 ext4_msg(sb, KERN_INFO,
2732 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002733 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002734 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002735 ext4_msg(sb, KERN_INFO,
2736 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002737 atomic_read(&sbi->s_mb_preallocated),
2738 atomic_read(&sbi->s_mb_discarded));
2739 }
2740
Eric Sandeen730c2132008-09-13 15:23:29 -04002741 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002742
2743 return 0;
2744}
2745
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002746static inline int ext4_issue_discard(struct super_block *sb,
Theodore Ts'o84130192011-09-09 18:50:51 -04002747 ext4_group_t block_group, ext4_grpblk_t cluster, int count)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002748{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002749 ext4_fsblk_t discard_block;
2750
Theodore Ts'o84130192011-09-09 18:50:51 -04002751 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2752 ext4_group_first_block_no(sb, block_group));
2753 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002754 trace_ext4_discard_blocks(sb,
2755 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002756 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002757}
2758
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002759/*
2760 * This function is called by the jbd2 layer once the commit has finished,
2761 * so we know we can free the blocks that were released with that commit.
2762 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002763static void ext4_free_data_callback(struct super_block *sb,
2764 struct ext4_journal_cb_entry *jce,
2765 int rc)
Alex Tomasc9de5602008-01-29 00:19:52 -05002766{
Bobi Jam18aadd42012-02-20 17:53:02 -05002767 struct ext4_free_data *entry = (struct ext4_free_data *)jce;
Alex Tomasc9de5602008-01-29 00:19:52 -05002768 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002769 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002770 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002771
Bobi Jam18aadd42012-02-20 17:53:02 -05002772 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2773 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002774
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002775 if (test_opt(sb, DISCARD)) {
2776 err = ext4_issue_discard(sb, entry->efd_group,
2777 entry->efd_start_cluster,
2778 entry->efd_count);
2779 if (err && err != -EOPNOTSUPP)
2780 ext4_msg(sb, KERN_WARNING, "discard request in"
2781 " group:%d block:%d count:%d failed"
2782 " with %d", entry->efd_group,
2783 entry->efd_start_cluster,
2784 entry->efd_count, err);
2785 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002786
Bobi Jam18aadd42012-02-20 17:53:02 -05002787 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2788 /* we expect to find existing buddy because it's pinned */
2789 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002790
Alex Tomasc9de5602008-01-29 00:19:52 -05002791
Bobi Jam18aadd42012-02-20 17:53:02 -05002792 db = e4b.bd_info;
2793 /* there are blocks to put in buddy to make them really free */
2794 count += entry->efd_count;
2795 count2++;
2796 ext4_lock_group(sb, entry->efd_group);
2797 /* Take it out of per group rb tree */
2798 rb_erase(&entry->efd_node, &(db->bb_free_root));
2799 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002800
Bobi Jam18aadd42012-02-20 17:53:02 -05002801 /*
2802 * Clear the trimmed flag for the group so that the next
2803 * ext4_trim_fs can trim it.
2804 * If the volume is mounted with -o discard, online discard
2805 * is supported and the free blocks will be trimmed online.
2806 */
2807 if (!test_opt(sb, DISCARD))
2808 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2809
2810 if (!db->bb_free_root.rb_node) {
2811 /* No more items in the per group rb tree
2812 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002813 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002814 page_cache_release(e4b.bd_buddy_page);
2815 page_cache_release(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002816 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002817 ext4_unlock_group(sb, entry->efd_group);
2818 kmem_cache_free(ext4_free_data_cachep, entry);
2819 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002820
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002821 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002822}
2823
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002824int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002825{
Theodore Ts'o16828082010-10-27 21:30:09 -04002826 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2827 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002828 if (ext4_pspace_cachep == NULL)
2829 return -ENOMEM;
2830
Theodore Ts'o16828082010-10-27 21:30:09 -04002831 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2832 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002833 if (ext4_ac_cachep == NULL) {
2834 kmem_cache_destroy(ext4_pspace_cachep);
2835 return -ENOMEM;
2836 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002837
Bobi Jam18aadd42012-02-20 17:53:02 -05002838 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2839 SLAB_RECLAIM_ACCOUNT);
2840 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002841 kmem_cache_destroy(ext4_pspace_cachep);
2842 kmem_cache_destroy(ext4_ac_cachep);
2843 return -ENOMEM;
2844 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002845 return 0;
2846}
2847
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002848void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002849{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002850 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002851 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2852 * before destroying the slab cache.
2853 */
2854 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002855 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002856 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002857 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002858 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002859}
2860
2861
2862/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002863 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002864 * Returns 0 if success or error code
2865 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002866static noinline_for_stack int
2867ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002868 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002869{
2870 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002871 struct ext4_group_desc *gdp;
2872 struct buffer_head *gdp_bh;
2873 struct ext4_sb_info *sbi;
2874 struct super_block *sb;
2875 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002876 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002877
2878 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2879 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2880
2881 sb = ac->ac_sb;
2882 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002883
2884 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002885 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002886 if (!bitmap_bh)
2887 goto out_err;
2888
liang xie5d601252014-05-12 22:06:43 -04002889 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002890 err = ext4_journal_get_write_access(handle, bitmap_bh);
2891 if (err)
2892 goto out_err;
2893
2894 err = -EIO;
2895 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2896 if (!gdp)
2897 goto out_err;
2898
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002899 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002900 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002901
liang xie5d601252014-05-12 22:06:43 -04002902 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002903 err = ext4_journal_get_write_access(handle, gdp_bh);
2904 if (err)
2905 goto out_err;
2906
Akinobu Mitabda00de2010-03-03 23:53:25 -05002907 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002908
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002909 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002910 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002911 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002912 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002913 /* File system mounted not to panic on error
2914 * Fix the bitmap and repeat the block allocation
2915 * We leak some of the blocks here.
2916 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002917 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002918 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2919 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002920 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002921 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002922 if (!err)
2923 err = -EAGAIN;
2924 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002925 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002926
2927 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002928#ifdef AGGRESSIVE_CHECK
2929 {
2930 int i;
2931 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2932 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2933 bitmap_bh->b_data));
2934 }
2935 }
2936#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002937 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2938 ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002939 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2940 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002941 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002942 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002943 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002944 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002945 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
2946 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04002947 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002948 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002949
2950 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04002951 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002952 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002953 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002954 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002955 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2956 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04002957 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
2958 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002959
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002960 if (sbi->s_log_groups_per_flex) {
2961 ext4_group_t flex_group = ext4_flex_group(sbi,
2962 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04002963 atomic64_sub(ac->ac_b_ex.fe_len,
2964 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002965 }
2966
Frank Mayhar03901312009-01-07 00:06:22 -05002967 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002968 if (err)
2969 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002970 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002971
2972out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002973 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002974 return err;
2975}
2976
2977/*
2978 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002979 * Group request are normalized to s_mb_group_prealloc, which goes to
2980 * s_strip if we set the same via mount option.
2981 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002982 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002983 *
2984 * XXX: should we try to preallocate more than the group has now?
2985 */
2986static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2987{
2988 struct super_block *sb = ac->ac_sb;
2989 struct ext4_locality_group *lg = ac->ac_lg;
2990
2991 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002992 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002993 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002994 current->pid, ac->ac_g_ex.fe_len);
2995}
2996
2997/*
2998 * Normalization means making request better in terms of
2999 * size and alignment
3000 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003001static noinline_for_stack void
3002ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003003 struct ext4_allocation_request *ar)
3004{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003005 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003006 int bsbits, max;
3007 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003008 loff_t size, start_off;
3009 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003010 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003011 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003012 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003013
3014 /* do normalize only data requests, metadata requests
3015 do not need preallocation */
3016 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3017 return;
3018
3019 /* sometime caller may want exact blocks */
3020 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3021 return;
3022
3023 /* caller may indicate that preallocation isn't
3024 * required (it's a tail, for example) */
3025 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3026 return;
3027
3028 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3029 ext4_mb_normalize_group_request(ac);
3030 return ;
3031 }
3032
3033 bsbits = ac->ac_sb->s_blocksize_bits;
3034
3035 /* first, let's learn actual file size
3036 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003037 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003038 size = size << bsbits;
3039 if (size < i_size_read(ac->ac_inode))
3040 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003041 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003042
Valerie Clement19304792008-05-13 19:31:14 -04003043 /* max size of free chunks */
3044 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003045
Valerie Clement19304792008-05-13 19:31:14 -04003046#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3047 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003048
3049 /* first, try to predict filesize */
3050 /* XXX: should this table be tunable? */
3051 start_off = 0;
3052 if (size <= 16 * 1024) {
3053 size = 16 * 1024;
3054 } else if (size <= 32 * 1024) {
3055 size = 32 * 1024;
3056 } else if (size <= 64 * 1024) {
3057 size = 64 * 1024;
3058 } else if (size <= 128 * 1024) {
3059 size = 128 * 1024;
3060 } else if (size <= 256 * 1024) {
3061 size = 256 * 1024;
3062 } else if (size <= 512 * 1024) {
3063 size = 512 * 1024;
3064 } else if (size <= 1024 * 1024) {
3065 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003066 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003067 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003068 (21 - bsbits)) << 21;
3069 size = 2 * 1024 * 1024;
3070 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003071 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3072 (22 - bsbits)) << 22;
3073 size = 4 * 1024 * 1024;
3074 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003075 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003076 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3077 (23 - bsbits)) << 23;
3078 size = 8 * 1024 * 1024;
3079 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003080 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3081 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3082 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003083 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003084 size = size >> bsbits;
3085 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003086
3087 /* don't cover already allocated blocks in selected range */
3088 if (ar->pleft && start <= ar->lleft) {
3089 size -= ar->lleft + 1 - start;
3090 start = ar->lleft + 1;
3091 }
3092 if (ar->pright && start + size - 1 >= ar->lright)
3093 size -= start + size - ar->lright;
3094
3095 end = start + size;
3096
3097 /* check we don't cross already preallocated blocks */
3098 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003099 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003100 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003101
Alex Tomasc9de5602008-01-29 00:19:52 -05003102 if (pa->pa_deleted)
3103 continue;
3104 spin_lock(&pa->pa_lock);
3105 if (pa->pa_deleted) {
3106 spin_unlock(&pa->pa_lock);
3107 continue;
3108 }
3109
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003110 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3111 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003112
3113 /* PA must not overlap original request */
3114 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3115 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3116
Eric Sandeen38877f42009-08-17 23:55:24 -04003117 /* skip PAs this normalized request doesn't overlap with */
3118 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003119 spin_unlock(&pa->pa_lock);
3120 continue;
3121 }
3122 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3123
Eric Sandeen38877f42009-08-17 23:55:24 -04003124 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003125 if (pa_end <= ac->ac_o_ex.fe_logical) {
3126 BUG_ON(pa_end < start);
3127 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003128 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003129 BUG_ON(pa->pa_lstart > end);
3130 end = pa->pa_lstart;
3131 }
3132 spin_unlock(&pa->pa_lock);
3133 }
3134 rcu_read_unlock();
3135 size = end - start;
3136
3137 /* XXX: extra loop to check we really don't overlap preallocations */
3138 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003139 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003140 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003141
Alex Tomasc9de5602008-01-29 00:19:52 -05003142 spin_lock(&pa->pa_lock);
3143 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003144 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3145 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003146 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3147 }
3148 spin_unlock(&pa->pa_lock);
3149 }
3150 rcu_read_unlock();
3151
3152 if (start + size <= ac->ac_o_ex.fe_logical &&
3153 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003154 ext4_msg(ac->ac_sb, KERN_ERR,
3155 "start %lu, size %lu, fe_logical %lu",
3156 (unsigned long) start, (unsigned long) size,
3157 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003158 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003159 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003160 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003161
3162 /* now prepare goal request */
3163
3164 /* XXX: is it better to align blocks WRT to logical
3165 * placement or satisfy big request as is */
3166 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003167 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003168
3169 /* define goal start in order to merge */
3170 if (ar->pright && (ar->lright == (start + size))) {
3171 /* merge to the right */
3172 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3173 &ac->ac_f_ex.fe_group,
3174 &ac->ac_f_ex.fe_start);
3175 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3176 }
3177 if (ar->pleft && (ar->lleft + 1 == start)) {
3178 /* merge to the left */
3179 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3180 &ac->ac_f_ex.fe_group,
3181 &ac->ac_f_ex.fe_start);
3182 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3183 }
3184
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003185 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003186 (unsigned) orig_size, (unsigned) start);
3187}
3188
3189static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3190{
3191 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3192
3193 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3194 atomic_inc(&sbi->s_bal_reqs);
3195 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003196 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003197 atomic_inc(&sbi->s_bal_success);
3198 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3199 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3200 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3201 atomic_inc(&sbi->s_bal_goals);
3202 if (ac->ac_found > sbi->s_mb_max_to_scan)
3203 atomic_inc(&sbi->s_bal_breaks);
3204 }
3205
Theodore Ts'o296c3552009-09-30 00:32:42 -04003206 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3207 trace_ext4_mballoc_alloc(ac);
3208 else
3209 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003210}
3211
3212/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003213 * Called on failure; free up any blocks from the inode PA for this
3214 * context. We don't need this for MB_GROUP_PA because we only change
3215 * pa_free in ext4_mb_release_context(), but on failure, we've already
3216 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3217 */
3218static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3219{
3220 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003221 struct ext4_buddy e4b;
3222 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003223
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003224 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003225 if (ac->ac_f_ex.fe_len == 0)
3226 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003227 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3228 if (err) {
3229 /*
3230 * This should never happen since we pin the
3231 * pages in the ext4_allocation_context so
3232 * ext4_mb_load_buddy() should never fail.
3233 */
3234 WARN(1, "mb_load_buddy failed (%d)", err);
3235 return;
3236 }
3237 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3238 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3239 ac->ac_f_ex.fe_len);
3240 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003241 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003242 return;
3243 }
3244 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003245 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003246}
3247
3248/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003249 * use blocks preallocated to inode
3250 */
3251static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3252 struct ext4_prealloc_space *pa)
3253{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003254 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003255 ext4_fsblk_t start;
3256 ext4_fsblk_t end;
3257 int len;
3258
3259 /* found preallocated blocks, use them */
3260 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003261 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3262 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3263 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003264 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3265 &ac->ac_b_ex.fe_start);
3266 ac->ac_b_ex.fe_len = len;
3267 ac->ac_status = AC_STATUS_FOUND;
3268 ac->ac_pa = pa;
3269
3270 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003271 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003272 BUG_ON(pa->pa_free < len);
3273 pa->pa_free -= len;
3274
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003275 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003276}
3277
3278/*
3279 * use blocks preallocated to locality group
3280 */
3281static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3282 struct ext4_prealloc_space *pa)
3283{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003284 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003285
Alex Tomasc9de5602008-01-29 00:19:52 -05003286 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3287 &ac->ac_b_ex.fe_group,
3288 &ac->ac_b_ex.fe_start);
3289 ac->ac_b_ex.fe_len = len;
3290 ac->ac_status = AC_STATUS_FOUND;
3291 ac->ac_pa = pa;
3292
3293 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003294 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003295 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003296 * in on-disk bitmap -- see ext4_mb_release_context()
3297 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003298 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003299 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003300}
3301
3302/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003303 * Return the prealloc space that have minimal distance
3304 * from the goal block. @cpa is the prealloc
3305 * space that is having currently known minimal distance
3306 * from the goal block.
3307 */
3308static struct ext4_prealloc_space *
3309ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3310 struct ext4_prealloc_space *pa,
3311 struct ext4_prealloc_space *cpa)
3312{
3313 ext4_fsblk_t cur_distance, new_distance;
3314
3315 if (cpa == NULL) {
3316 atomic_inc(&pa->pa_count);
3317 return pa;
3318 }
3319 cur_distance = abs(goal_block - cpa->pa_pstart);
3320 new_distance = abs(goal_block - pa->pa_pstart);
3321
Coly Li5a54b2f2011-02-24 14:10:05 -05003322 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003323 return cpa;
3324
3325 /* drop the previous reference */
3326 atomic_dec(&cpa->pa_count);
3327 atomic_inc(&pa->pa_count);
3328 return pa;
3329}
3330
3331/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003332 * search goal blocks in preallocated space
3333 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003334static noinline_for_stack int
3335ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003336{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003337 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003338 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003339 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3340 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003341 struct ext4_prealloc_space *pa, *cpa = NULL;
3342 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003343
3344 /* only data can be preallocated */
3345 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3346 return 0;
3347
3348 /* first, try per-file preallocation */
3349 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003350 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003351
3352 /* all fields in this condition don't change,
3353 * so we can skip locking for them */
3354 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003355 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3356 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003357 continue;
3358
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003359 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003360 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003361 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3362 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003363 continue;
3364
Alex Tomasc9de5602008-01-29 00:19:52 -05003365 /* found preallocated blocks, use them */
3366 spin_lock(&pa->pa_lock);
3367 if (pa->pa_deleted == 0 && pa->pa_free) {
3368 atomic_inc(&pa->pa_count);
3369 ext4_mb_use_inode_pa(ac, pa);
3370 spin_unlock(&pa->pa_lock);
3371 ac->ac_criteria = 10;
3372 rcu_read_unlock();
3373 return 1;
3374 }
3375 spin_unlock(&pa->pa_lock);
3376 }
3377 rcu_read_unlock();
3378
3379 /* can we use group allocation? */
3380 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3381 return 0;
3382
3383 /* inode may have no locality group for some reason */
3384 lg = ac->ac_lg;
3385 if (lg == NULL)
3386 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003387 order = fls(ac->ac_o_ex.fe_len) - 1;
3388 if (order > PREALLOC_TB_SIZE - 1)
3389 /* The max size of hash table is PREALLOC_TB_SIZE */
3390 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003391
Akinobu Mitabda00de2010-03-03 23:53:25 -05003392 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003393 /*
3394 * search for the prealloc space that is having
3395 * minimal distance from the goal block.
3396 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003397 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3398 rcu_read_lock();
3399 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3400 pa_inode_list) {
3401 spin_lock(&pa->pa_lock);
3402 if (pa->pa_deleted == 0 &&
3403 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003404
3405 cpa = ext4_mb_check_group_pa(goal_block,
3406 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003407 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003408 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003409 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003410 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003411 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003412 if (cpa) {
3413 ext4_mb_use_group_pa(ac, cpa);
3414 ac->ac_criteria = 20;
3415 return 1;
3416 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003417 return 0;
3418}
3419
3420/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003421 * the function goes through all block freed in the group
3422 * but not yet committed and marks them used in in-core bitmap.
3423 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003424 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003425 */
3426static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3427 ext4_group_t group)
3428{
3429 struct rb_node *n;
3430 struct ext4_group_info *grp;
3431 struct ext4_free_data *entry;
3432
3433 grp = ext4_get_group_info(sb, group);
3434 n = rb_first(&(grp->bb_free_root));
3435
3436 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003437 entry = rb_entry(n, struct ext4_free_data, efd_node);
3438 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003439 n = rb_next(n);
3440 }
3441 return;
3442}
3443
3444/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003445 * the function goes through all preallocation in this group and marks them
3446 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003447 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003448 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003449static noinline_for_stack
3450void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003451 ext4_group_t group)
3452{
3453 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3454 struct ext4_prealloc_space *pa;
3455 struct list_head *cur;
3456 ext4_group_t groupnr;
3457 ext4_grpblk_t start;
3458 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003459 int len;
3460
3461 /* all form of preallocation discards first load group,
3462 * so the only competing code is preallocation use.
3463 * we don't need any locking here
3464 * notice we do NOT ignore preallocations with pa_deleted
3465 * otherwise we could leave used blocks available for
3466 * allocation in buddy when concurrent ext4_mb_put_pa()
3467 * is dropping preallocation
3468 */
3469 list_for_each(cur, &grp->bb_prealloc_list) {
3470 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3471 spin_lock(&pa->pa_lock);
3472 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3473 &groupnr, &start);
3474 len = pa->pa_len;
3475 spin_unlock(&pa->pa_lock);
3476 if (unlikely(len == 0))
3477 continue;
3478 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003479 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003480 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003481 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003482 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003483}
3484
3485static void ext4_mb_pa_callback(struct rcu_head *head)
3486{
3487 struct ext4_prealloc_space *pa;
3488 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003489
3490 BUG_ON(atomic_read(&pa->pa_count));
3491 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003492 kmem_cache_free(ext4_pspace_cachep, pa);
3493}
3494
3495/*
3496 * drops a reference to preallocated space descriptor
3497 * if this was the last reference and the space is consumed
3498 */
3499static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3500 struct super_block *sb, struct ext4_prealloc_space *pa)
3501{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003502 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003503 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003504
Alex Tomasc9de5602008-01-29 00:19:52 -05003505 /* in this short window concurrent discard can set pa_deleted */
3506 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003507 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3508 spin_unlock(&pa->pa_lock);
3509 return;
3510 }
3511
Alex Tomasc9de5602008-01-29 00:19:52 -05003512 if (pa->pa_deleted == 1) {
3513 spin_unlock(&pa->pa_lock);
3514 return;
3515 }
3516
3517 pa->pa_deleted = 1;
3518 spin_unlock(&pa->pa_lock);
3519
Eric Sandeend33a1972009-03-16 23:25:40 -04003520 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003521 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003522 * If doing group-based preallocation, pa_pstart may be in the
3523 * next group when pa is used up
3524 */
3525 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003526 grp_blk--;
3527
Lukas Czernerbd862982013-04-03 23:32:34 -04003528 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003529
3530 /*
3531 * possible race:
3532 *
3533 * P1 (buddy init) P2 (regular allocation)
3534 * find block B in PA
3535 * copy on-disk bitmap to buddy
3536 * mark B in on-disk bitmap
3537 * drop PA from group
3538 * mark all PAs in buddy
3539 *
3540 * thus, P1 initializes buddy with B available. to prevent this
3541 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3542 * against that pair
3543 */
3544 ext4_lock_group(sb, grp);
3545 list_del(&pa->pa_group_list);
3546 ext4_unlock_group(sb, grp);
3547
3548 spin_lock(pa->pa_obj_lock);
3549 list_del_rcu(&pa->pa_inode_list);
3550 spin_unlock(pa->pa_obj_lock);
3551
3552 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3553}
3554
3555/*
3556 * creates new preallocated space for given inode
3557 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003558static noinline_for_stack int
3559ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003560{
3561 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003562 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003563 struct ext4_prealloc_space *pa;
3564 struct ext4_group_info *grp;
3565 struct ext4_inode_info *ei;
3566
3567 /* preallocate only when found space is larger then requested */
3568 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3569 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3570 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3571
3572 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3573 if (pa == NULL)
3574 return -ENOMEM;
3575
3576 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3577 int winl;
3578 int wins;
3579 int win;
3580 int offs;
3581
3582 /* we can't allocate as much as normalizer wants.
3583 * so, found space must get proper lstart
3584 * to cover original request */
3585 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3586 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3587
3588 /* we're limited by original request in that
3589 * logical block must be covered any way
3590 * winl is window we can move our chunk within */
3591 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3592
3593 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003594 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003595
3596 /* the smallest one defines real window */
3597 win = min(winl, wins);
3598
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003599 offs = ac->ac_o_ex.fe_logical %
3600 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003601 if (offs && offs < win)
3602 win = offs;
3603
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003604 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003605 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003606 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3607 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3608 }
3609
3610 /* preallocation can change ac_b_ex, thus we store actually
3611 * allocated blocks for history */
3612 ac->ac_f_ex = ac->ac_b_ex;
3613
3614 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3615 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3616 pa->pa_len = ac->ac_b_ex.fe_len;
3617 pa->pa_free = pa->pa_len;
3618 atomic_set(&pa->pa_count, 1);
3619 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003620 INIT_LIST_HEAD(&pa->pa_inode_list);
3621 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003622 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003623 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003624
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003625 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003626 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003627 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003628
3629 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003630 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003631
3632 ei = EXT4_I(ac->ac_inode);
3633 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3634
3635 pa->pa_obj_lock = &ei->i_prealloc_lock;
3636 pa->pa_inode = ac->ac_inode;
3637
3638 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3639 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3640 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3641
3642 spin_lock(pa->pa_obj_lock);
3643 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3644 spin_unlock(pa->pa_obj_lock);
3645
3646 return 0;
3647}
3648
3649/*
3650 * creates new preallocated space for locality group inodes belongs to
3651 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003652static noinline_for_stack int
3653ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003654{
3655 struct super_block *sb = ac->ac_sb;
3656 struct ext4_locality_group *lg;
3657 struct ext4_prealloc_space *pa;
3658 struct ext4_group_info *grp;
3659
3660 /* preallocate only when found space is larger then requested */
3661 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3662 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3663 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3664
3665 BUG_ON(ext4_pspace_cachep == NULL);
3666 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3667 if (pa == NULL)
3668 return -ENOMEM;
3669
3670 /* preallocation can change ac_b_ex, thus we store actually
3671 * allocated blocks for history */
3672 ac->ac_f_ex = ac->ac_b_ex;
3673
3674 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3675 pa->pa_lstart = pa->pa_pstart;
3676 pa->pa_len = ac->ac_b_ex.fe_len;
3677 pa->pa_free = pa->pa_len;
3678 atomic_set(&pa->pa_count, 1);
3679 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003680 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003681 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003682 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003683 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003684
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003685 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003686 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3687 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003688
3689 ext4_mb_use_group_pa(ac, pa);
3690 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3691
3692 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3693 lg = ac->ac_lg;
3694 BUG_ON(lg == NULL);
3695
3696 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3697 pa->pa_inode = NULL;
3698
3699 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3700 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3701 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3702
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003703 /*
3704 * We will later add the new pa to the right bucket
3705 * after updating the pa_free in ext4_mb_release_context
3706 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003707 return 0;
3708}
3709
3710static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3711{
3712 int err;
3713
3714 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3715 err = ext4_mb_new_group_pa(ac);
3716 else
3717 err = ext4_mb_new_inode_pa(ac);
3718 return err;
3719}
3720
3721/*
3722 * finds all unused blocks in on-disk bitmap, frees them in
3723 * in-core bitmap and buddy.
3724 * @pa must be unlinked from inode and group lists, so that
3725 * nobody else can find/use it.
3726 * the caller MUST hold group/inode locks.
3727 * TODO: optimize the case when there are no in-core structures yet
3728 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003729static noinline_for_stack int
3730ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003731 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003732{
Alex Tomasc9de5602008-01-29 00:19:52 -05003733 struct super_block *sb = e4b->bd_sb;
3734 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003735 unsigned int end;
3736 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003737 ext4_group_t group;
3738 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003739 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003740 int err = 0;
3741 int free = 0;
3742
3743 BUG_ON(pa->pa_deleted == 0);
3744 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003745 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003746 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3747 end = bit + pa->pa_len;
3748
Alex Tomasc9de5602008-01-29 00:19:52 -05003749 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003750 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003751 if (bit >= end)
3752 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003753 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003754 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003755 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3756 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003757 free += next - bit;
3758
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003759 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003760 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3761 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003762 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003763 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3764 bit = next + 1;
3765 }
3766 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003767 ext4_msg(e4b->bd_sb, KERN_CRIT,
3768 "pa %p: logic %lu, phys. %lu, len %lu",
3769 pa, (unsigned long) pa->pa_lstart,
3770 (unsigned long) pa->pa_pstart,
3771 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003772 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003773 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003774 /*
3775 * pa is already deleted so we use the value obtained
3776 * from the bitmap and continue.
3777 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003778 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003779 atomic_add(free, &sbi->s_mb_discarded);
3780
3781 return err;
3782}
3783
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003784static noinline_for_stack int
3785ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003786 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003787{
Alex Tomasc9de5602008-01-29 00:19:52 -05003788 struct super_block *sb = e4b->bd_sb;
3789 ext4_group_t group;
3790 ext4_grpblk_t bit;
3791
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003792 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003793 BUG_ON(pa->pa_deleted == 0);
3794 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3795 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3796 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3797 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003798 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003799
3800 return 0;
3801}
3802
3803/*
3804 * releases all preallocations in given group
3805 *
3806 * first, we need to decide discard policy:
3807 * - when do we discard
3808 * 1) ENOSPC
3809 * - how many do we discard
3810 * 1) how many requested
3811 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003812static noinline_for_stack int
3813ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003814 ext4_group_t group, int needed)
3815{
3816 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3817 struct buffer_head *bitmap_bh = NULL;
3818 struct ext4_prealloc_space *pa, *tmp;
3819 struct list_head list;
3820 struct ext4_buddy e4b;
3821 int err;
3822 int busy = 0;
3823 int free = 0;
3824
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003825 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003826
3827 if (list_empty(&grp->bb_prealloc_list))
3828 return 0;
3829
Theodore Ts'o574ca172008-07-11 19:27:31 -04003830 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003831 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003832 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003833 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003834 }
3835
3836 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003837 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003838 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003839 put_bh(bitmap_bh);
3840 return 0;
3841 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003842
3843 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003844 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003845
Alex Tomasc9de5602008-01-29 00:19:52 -05003846 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003847repeat:
3848 ext4_lock_group(sb, group);
3849 list_for_each_entry_safe(pa, tmp,
3850 &grp->bb_prealloc_list, pa_group_list) {
3851 spin_lock(&pa->pa_lock);
3852 if (atomic_read(&pa->pa_count)) {
3853 spin_unlock(&pa->pa_lock);
3854 busy = 1;
3855 continue;
3856 }
3857 if (pa->pa_deleted) {
3858 spin_unlock(&pa->pa_lock);
3859 continue;
3860 }
3861
3862 /* seems this one can be freed ... */
3863 pa->pa_deleted = 1;
3864
3865 /* we can trust pa_free ... */
3866 free += pa->pa_free;
3867
3868 spin_unlock(&pa->pa_lock);
3869
3870 list_del(&pa->pa_group_list);
3871 list_add(&pa->u.pa_tmp_list, &list);
3872 }
3873
3874 /* if we still need more blocks and some PAs were used, try again */
3875 if (free < needed && busy) {
3876 busy = 0;
3877 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003878 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003879 goto repeat;
3880 }
3881
3882 /* found anything to free? */
3883 if (list_empty(&list)) {
3884 BUG_ON(free != 0);
3885 goto out;
3886 }
3887
3888 /* now free all selected PAs */
3889 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3890
3891 /* remove from object (inode or locality group) */
3892 spin_lock(pa->pa_obj_lock);
3893 list_del_rcu(&pa->pa_inode_list);
3894 spin_unlock(pa->pa_obj_lock);
3895
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003896 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003897 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003898 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003899 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003900
3901 list_del(&pa->u.pa_tmp_list);
3902 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3903 }
3904
3905out:
3906 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003907 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003908 put_bh(bitmap_bh);
3909 return free;
3910}
3911
3912/*
3913 * releases all non-used preallocated blocks for given inode
3914 *
3915 * It's important to discard preallocations under i_data_sem
3916 * We don't want another block to be served from the prealloc
3917 * space when we are discarding the inode prealloc space.
3918 *
3919 * FIXME!! Make sure it is valid at all the call sites
3920 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003921void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003922{
3923 struct ext4_inode_info *ei = EXT4_I(inode);
3924 struct super_block *sb = inode->i_sb;
3925 struct buffer_head *bitmap_bh = NULL;
3926 struct ext4_prealloc_space *pa, *tmp;
3927 ext4_group_t group = 0;
3928 struct list_head list;
3929 struct ext4_buddy e4b;
3930 int err;
3931
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003932 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003933 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3934 return;
3935 }
3936
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003937 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003938 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003939
3940 INIT_LIST_HEAD(&list);
3941
3942repeat:
3943 /* first, collect all pa's in the inode */
3944 spin_lock(&ei->i_prealloc_lock);
3945 while (!list_empty(&ei->i_prealloc_list)) {
3946 pa = list_entry(ei->i_prealloc_list.next,
3947 struct ext4_prealloc_space, pa_inode_list);
3948 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3949 spin_lock(&pa->pa_lock);
3950 if (atomic_read(&pa->pa_count)) {
3951 /* this shouldn't happen often - nobody should
3952 * use preallocation while we're discarding it */
3953 spin_unlock(&pa->pa_lock);
3954 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003955 ext4_msg(sb, KERN_ERR,
3956 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05003957 WARN_ON(1);
3958 schedule_timeout_uninterruptible(HZ);
3959 goto repeat;
3960
3961 }
3962 if (pa->pa_deleted == 0) {
3963 pa->pa_deleted = 1;
3964 spin_unlock(&pa->pa_lock);
3965 list_del_rcu(&pa->pa_inode_list);
3966 list_add(&pa->u.pa_tmp_list, &list);
3967 continue;
3968 }
3969
3970 /* someone is deleting pa right now */
3971 spin_unlock(&pa->pa_lock);
3972 spin_unlock(&ei->i_prealloc_lock);
3973
3974 /* we have to wait here because pa_deleted
3975 * doesn't mean pa is already unlinked from
3976 * the list. as we might be called from
3977 * ->clear_inode() the inode will get freed
3978 * and concurrent thread which is unlinking
3979 * pa from inode's list may access already
3980 * freed memory, bad-bad-bad */
3981
3982 /* XXX: if this happens too often, we can
3983 * add a flag to force wait only in case
3984 * of ->clear_inode(), but not in case of
3985 * regular truncate */
3986 schedule_timeout_uninterruptible(HZ);
3987 goto repeat;
3988 }
3989 spin_unlock(&ei->i_prealloc_lock);
3990
3991 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003992 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04003993 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05003994
3995 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003996 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003997 ext4_error(sb, "Error loading buddy information for %u",
3998 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003999 continue;
4000 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004001
Theodore Ts'o574ca172008-07-11 19:27:31 -04004002 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004003 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004004 ext4_error(sb, "Error reading block bitmap for %u",
4005 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004006 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004007 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004008 }
4009
4010 ext4_lock_group(sb, group);
4011 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004012 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004013 ext4_unlock_group(sb, group);
4014
Jing Zhange39e07f2010-05-14 00:00:00 -04004015 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004016 put_bh(bitmap_bh);
4017
4018 list_del(&pa->u.pa_tmp_list);
4019 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4020 }
4021}
4022
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004023#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004024static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4025{
4026 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004027 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004028
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05004029 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05004030 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004031 return;
4032
Joe Perches7f6a11e2012-03-19 23:09:43 -04004033 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004034 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04004035 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004036 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004037 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004038 "goal %lu/%lu/%lu@%lu, "
4039 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004040 (unsigned long)ac->ac_o_ex.fe_group,
4041 (unsigned long)ac->ac_o_ex.fe_start,
4042 (unsigned long)ac->ac_o_ex.fe_len,
4043 (unsigned long)ac->ac_o_ex.fe_logical,
4044 (unsigned long)ac->ac_g_ex.fe_group,
4045 (unsigned long)ac->ac_g_ex.fe_start,
4046 (unsigned long)ac->ac_g_ex.fe_len,
4047 (unsigned long)ac->ac_g_ex.fe_logical,
4048 (unsigned long)ac->ac_b_ex.fe_group,
4049 (unsigned long)ac->ac_b_ex.fe_start,
4050 (unsigned long)ac->ac_b_ex.fe_len,
4051 (unsigned long)ac->ac_b_ex.fe_logical,
4052 (int)ac->ac_criteria);
Eric Sandeendc9ddd92014-02-20 13:32:10 -05004053 ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004054 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004055 ngroups = ext4_get_groups_count(sb);
4056 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004057 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4058 struct ext4_prealloc_space *pa;
4059 ext4_grpblk_t start;
4060 struct list_head *cur;
4061 ext4_lock_group(sb, i);
4062 list_for_each(cur, &grp->bb_prealloc_list) {
4063 pa = list_entry(cur, struct ext4_prealloc_space,
4064 pa_group_list);
4065 spin_lock(&pa->pa_lock);
4066 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4067 NULL, &start);
4068 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004069 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4070 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004071 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004072 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004073
4074 if (grp->bb_free == 0)
4075 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004076 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004077 i, grp->bb_free, grp->bb_fragments);
4078 }
4079 printk(KERN_ERR "\n");
4080}
4081#else
4082static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4083{
4084 return;
4085}
4086#endif
4087
4088/*
4089 * We use locality group preallocation for small size file. The size of the
4090 * file is determined by the current size or the resulting size after
4091 * allocation which ever is larger
4092 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004093 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004094 */
4095static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4096{
4097 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4098 int bsbits = ac->ac_sb->s_blocksize_bits;
4099 loff_t size, isize;
4100
4101 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4102 return;
4103
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004104 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4105 return;
4106
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004107 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004108 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4109 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004110
Theodore Ts'o50797482009-09-18 13:34:02 -04004111 if ((size == isize) &&
4112 !ext4_fs_is_busy(sbi) &&
4113 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4114 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4115 return;
4116 }
4117
Robin Dongebbe0272011-10-26 05:14:27 -04004118 if (sbi->s_mb_group_prealloc <= 0) {
4119 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4120 return;
4121 }
4122
Alex Tomasc9de5602008-01-29 00:19:52 -05004123 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004124 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004125 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004126 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004127 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004128 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004129
4130 BUG_ON(ac->ac_lg != NULL);
4131 /*
4132 * locality group prealloc space are per cpu. The reason for having
4133 * per cpu locality group is to reduce the contention between block
4134 * request from multiple CPUs.
4135 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004136 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004137
4138 /* we're going to use group allocation */
4139 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4140
4141 /* serialize all allocations in the group */
4142 mutex_lock(&ac->ac_lg->lg_mutex);
4143}
4144
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004145static noinline_for_stack int
4146ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004147 struct ext4_allocation_request *ar)
4148{
4149 struct super_block *sb = ar->inode->i_sb;
4150 struct ext4_sb_info *sbi = EXT4_SB(sb);
4151 struct ext4_super_block *es = sbi->s_es;
4152 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004153 unsigned int len;
4154 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004155 ext4_grpblk_t block;
4156
4157 /* we can't allocate > group size */
4158 len = ar->len;
4159
4160 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004161 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4162 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004163
4164 /* start searching from the goal */
4165 goal = ar->goal;
4166 if (goal < le32_to_cpu(es->s_first_data_block) ||
4167 goal >= ext4_blocks_count(es))
4168 goal = le32_to_cpu(es->s_first_data_block);
4169 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4170
4171 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004172 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004173 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004174 ac->ac_sb = sb;
4175 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004176 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004177 ac->ac_o_ex.fe_group = group;
4178 ac->ac_o_ex.fe_start = block;
4179 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004180 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004181 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004182
4183 /* we have to define context: we'll we work with a file or
4184 * locality group. this is a policy, actually */
4185 ext4_mb_group_or_file(ac);
4186
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004187 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004188 "left: %u/%u, right %u/%u to %swritable\n",
4189 (unsigned) ar->len, (unsigned) ar->logical,
4190 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4191 (unsigned) ar->lleft, (unsigned) ar->pleft,
4192 (unsigned) ar->lright, (unsigned) ar->pright,
4193 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4194 return 0;
4195
4196}
4197
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004198static noinline_for_stack void
4199ext4_mb_discard_lg_preallocations(struct super_block *sb,
4200 struct ext4_locality_group *lg,
4201 int order, int total_entries)
4202{
4203 ext4_group_t group = 0;
4204 struct ext4_buddy e4b;
4205 struct list_head discard_list;
4206 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004207
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004208 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004209
4210 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004211
4212 spin_lock(&lg->lg_prealloc_lock);
4213 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4214 pa_inode_list) {
4215 spin_lock(&pa->pa_lock);
4216 if (atomic_read(&pa->pa_count)) {
4217 /*
4218 * This is the pa that we just used
4219 * for block allocation. So don't
4220 * free that
4221 */
4222 spin_unlock(&pa->pa_lock);
4223 continue;
4224 }
4225 if (pa->pa_deleted) {
4226 spin_unlock(&pa->pa_lock);
4227 continue;
4228 }
4229 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004230 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004231
4232 /* seems this one can be freed ... */
4233 pa->pa_deleted = 1;
4234 spin_unlock(&pa->pa_lock);
4235
4236 list_del_rcu(&pa->pa_inode_list);
4237 list_add(&pa->u.pa_tmp_list, &discard_list);
4238
4239 total_entries--;
4240 if (total_entries <= 5) {
4241 /*
4242 * we want to keep only 5 entries
4243 * allowing it to grow to 8. This
4244 * mak sure we don't call discard
4245 * soon for this list.
4246 */
4247 break;
4248 }
4249 }
4250 spin_unlock(&lg->lg_prealloc_lock);
4251
4252 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4253
Lukas Czernerbd862982013-04-03 23:32:34 -04004254 group = ext4_get_group_number(sb, pa->pa_pstart);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004255 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004256 ext4_error(sb, "Error loading buddy information for %u",
4257 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004258 continue;
4259 }
4260 ext4_lock_group(sb, group);
4261 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004262 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004263 ext4_unlock_group(sb, group);
4264
Jing Zhange39e07f2010-05-14 00:00:00 -04004265 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004266 list_del(&pa->u.pa_tmp_list);
4267 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4268 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004269}
4270
4271/*
4272 * We have incremented pa_count. So it cannot be freed at this
4273 * point. Also we hold lg_mutex. So no parallel allocation is
4274 * possible from this lg. That means pa_free cannot be updated.
4275 *
4276 * A parallel ext4_mb_discard_group_preallocations is possible.
4277 * which can cause the lg_prealloc_list to be updated.
4278 */
4279
4280static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4281{
4282 int order, added = 0, lg_prealloc_count = 1;
4283 struct super_block *sb = ac->ac_sb;
4284 struct ext4_locality_group *lg = ac->ac_lg;
4285 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4286
4287 order = fls(pa->pa_free) - 1;
4288 if (order > PREALLOC_TB_SIZE - 1)
4289 /* The max size of hash table is PREALLOC_TB_SIZE */
4290 order = PREALLOC_TB_SIZE - 1;
4291 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004292 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004293 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4294 pa_inode_list) {
4295 spin_lock(&tmp_pa->pa_lock);
4296 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004297 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004298 continue;
4299 }
4300 if (!added && pa->pa_free < tmp_pa->pa_free) {
4301 /* Add to the tail of the previous entry */
4302 list_add_tail_rcu(&pa->pa_inode_list,
4303 &tmp_pa->pa_inode_list);
4304 added = 1;
4305 /*
4306 * we want to count the total
4307 * number of entries in the list
4308 */
4309 }
4310 spin_unlock(&tmp_pa->pa_lock);
4311 lg_prealloc_count++;
4312 }
4313 if (!added)
4314 list_add_tail_rcu(&pa->pa_inode_list,
4315 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004316 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004317
4318 /* Now trim the list to be not more than 8 elements */
4319 if (lg_prealloc_count > 8) {
4320 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004321 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004322 return;
4323 }
4324 return ;
4325}
4326
Alex Tomasc9de5602008-01-29 00:19:52 -05004327/*
4328 * release all resource we used in allocation
4329 */
4330static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4331{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004332 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004333 struct ext4_prealloc_space *pa = ac->ac_pa;
4334 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004335 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004336 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004337 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004338 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4339 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004340 pa->pa_free -= ac->ac_b_ex.fe_len;
4341 pa->pa_len -= ac->ac_b_ex.fe_len;
4342 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004343 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004344 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004345 if (pa) {
4346 /*
4347 * We want to add the pa to the right bucket.
4348 * Remove it from the list and while adding
4349 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004350 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004351 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004352 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004353 spin_lock(pa->pa_obj_lock);
4354 list_del_rcu(&pa->pa_inode_list);
4355 spin_unlock(pa->pa_obj_lock);
4356 ext4_mb_add_n_trim(ac);
4357 }
4358 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4359 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004360 if (ac->ac_bitmap_page)
4361 page_cache_release(ac->ac_bitmap_page);
4362 if (ac->ac_buddy_page)
4363 page_cache_release(ac->ac_buddy_page);
4364 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4365 mutex_unlock(&ac->ac_lg->lg_mutex);
4366 ext4_mb_collect_stats(ac);
4367 return 0;
4368}
4369
4370static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4371{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004372 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004373 int ret;
4374 int freed = 0;
4375
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004376 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004377 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004378 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4379 freed += ret;
4380 needed -= ret;
4381 }
4382
4383 return freed;
4384}
4385
4386/*
4387 * Main entry point into mballoc to allocate blocks
4388 * it tries to use preallocation first, then falls back
4389 * to usual allocation
4390 */
4391ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004392 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004393{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004394 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004395 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004396 struct ext4_sb_info *sbi;
4397 struct super_block *sb;
4398 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004399 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004400 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004401
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004402 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004403 sb = ar->inode->i_sb;
4404 sbi = EXT4_SB(sb);
4405
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004406 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004407
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004408 /* Allow to use superuser reservation for quota file */
4409 if (IS_NOQUOTA(ar->inode))
4410 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4411
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004412 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004413 /* Without delayed allocation we need to verify
4414 * there is enough free blocks to do block allocation
4415 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004416 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004417 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004418 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004419
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004420 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004421 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004422 ar->len = ar->len >> 1;
4423 }
4424 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004425 *errp = -ENOSPC;
4426 return 0;
4427 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004428 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004429 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004430 dquot_alloc_block_nofail(ar->inode,
4431 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004432 } else {
4433 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004434 dquot_alloc_block(ar->inode,
4435 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004436
4437 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4438 ar->len--;
4439 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004440 }
4441 inquota = ar->len;
4442 if (ar->len == 0) {
4443 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004444 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004445 }
Mingming Caod2a17632008-07-14 17:52:37 -04004446 }
Mingming Caod2a17632008-07-14 17:52:37 -04004447
Wei Yongjun85556c92012-09-26 20:43:37 -04004448 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004449 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004450 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004451 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004452 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004453 }
4454
Eric Sandeen256bdb42008-02-10 01:13:33 -05004455 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004456 if (*errp) {
4457 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004458 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004459 }
4460
Eric Sandeen256bdb42008-02-10 01:13:33 -05004461 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4462 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004463 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4464 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004465repeat:
4466 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004467 *errp = ext4_mb_regular_allocator(ac);
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004468 if (*errp)
4469 goto discard_and_exit;
4470
4471 /* as we've just preallocated more space than
4472 * user requested originally, we store allocated
4473 * space in a special descriptor */
4474 if (ac->ac_status == AC_STATUS_FOUND &&
4475 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4476 *errp = ext4_mb_new_preallocation(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004477 if (*errp) {
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004478 discard_and_exit:
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004479 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004480 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004481 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004482 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004483 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004484 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004485 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004486 /*
4487 * drop the reference that we took
4488 * in ext4_mb_use_best_found
4489 */
4490 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004491 ac->ac_b_ex.fe_group = 0;
4492 ac->ac_b_ex.fe_start = 0;
4493 ac->ac_b_ex.fe_len = 0;
4494 ac->ac_status = AC_STATUS_CONTINUE;
4495 goto repeat;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004496 } else if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004497 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004498 goto errout;
4499 } else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004500 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4501 ar->len = ac->ac_b_ex.fe_len;
4502 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004503 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004504 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004505 if (freed)
4506 goto repeat;
4507 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004508 }
4509
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004510errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004511 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004512 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004513 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004514 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004515 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004516 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004517out:
4518 if (ac)
4519 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004520 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004521 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004522 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004523 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004524 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004525 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004526 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004527 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004528
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004529 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004530
Alex Tomasc9de5602008-01-29 00:19:52 -05004531 return block;
4532}
Alex Tomasc9de5602008-01-29 00:19:52 -05004533
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004534/*
4535 * We can merge two free data extents only if the physical blocks
4536 * are contiguous, AND the extents were freed by the same transaction,
4537 * AND the blocks are associated with the same group.
4538 */
4539static int can_merge(struct ext4_free_data *entry1,
4540 struct ext4_free_data *entry2)
4541{
Bobi Jam18aadd42012-02-20 17:53:02 -05004542 if ((entry1->efd_tid == entry2->efd_tid) &&
4543 (entry1->efd_group == entry2->efd_group) &&
4544 ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004545 return 1;
4546 return 0;
4547}
4548
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004549static noinline_for_stack int
4550ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004551 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004552{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004553 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004554 ext4_grpblk_t cluster;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004555 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004556 struct ext4_group_info *db = e4b->bd_info;
4557 struct super_block *sb = e4b->bd_sb;
4558 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004559 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4560 struct rb_node *parent = NULL, *new_node;
4561
Frank Mayhar03901312009-01-07 00:06:22 -05004562 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004563 BUG_ON(e4b->bd_bitmap_page == NULL);
4564 BUG_ON(e4b->bd_buddy_page == NULL);
4565
Bobi Jam18aadd42012-02-20 17:53:02 -05004566 new_node = &new_entry->efd_node;
4567 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004568
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004569 if (!*n) {
4570 /* first free block exent. We need to
4571 protect buddy cache from being freed,
4572 * otherwise we'll refresh it from
4573 * on-disk bitmap and lose not-yet-available
4574 * blocks */
4575 page_cache_get(e4b->bd_buddy_page);
4576 page_cache_get(e4b->bd_bitmap_page);
4577 }
4578 while (*n) {
4579 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004580 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4581 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004582 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004583 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004584 n = &(*n)->rb_right;
4585 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004586 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004587 ext4_group_first_block_no(sb, group) +
4588 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004589 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004590 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004591 }
4592 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004593
4594 rb_link_node(new_node, parent, n);
4595 rb_insert_color(new_node, &db->bb_free_root);
4596
4597 /* Now try to see the extent can be merged to left and right */
4598 node = rb_prev(new_node);
4599 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004600 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004601 if (can_merge(entry, new_entry) &&
4602 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004603 new_entry->efd_start_cluster = entry->efd_start_cluster;
4604 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004605 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004606 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004607 }
4608 }
4609
4610 node = rb_next(new_node);
4611 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004612 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004613 if (can_merge(new_entry, entry) &&
4614 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004615 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004616 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004617 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004618 }
4619 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004620 /* Add the extent to transaction's private list */
Bobi Jam18aadd42012-02-20 17:53:02 -05004621 ext4_journal_callback_add(handle, ext4_free_data_callback,
4622 &new_entry->efd_jce);
Alex Tomasc9de5602008-01-29 00:19:52 -05004623 return 0;
4624}
4625
Theodore Ts'o44338712009-11-22 07:44:56 -05004626/**
4627 * ext4_free_blocks() -- Free given blocks and update quota
4628 * @handle: handle for this transaction
4629 * @inode: inode
4630 * @block: start physical block to free
4631 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004632 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004633 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004634void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004635 struct buffer_head *bh, ext4_fsblk_t block,
4636 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004637{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004638 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004639 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004640 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004641 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004642 ext4_grpblk_t bit;
4643 struct buffer_head *gd_bh;
4644 ext4_group_t block_group;
4645 struct ext4_sb_info *sbi;
4646 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004647 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004648 int err = 0;
4649 int ret;
4650
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004651 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004652 if (bh) {
4653 if (block)
4654 BUG_ON(block != bh->b_blocknr);
4655 else
4656 block = bh->b_blocknr;
4657 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004658
Alex Tomasc9de5602008-01-29 00:19:52 -05004659 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004660 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4661 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004662 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004663 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004664 goto error_return;
4665 }
4666
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004667 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004668 trace_ext4_free_blocks(inode, block, count, flags);
4669
4670 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4671 struct buffer_head *tbh = bh;
4672 int i;
4673
4674 BUG_ON(bh && (count > 1));
4675
4676 for (i = 0; i < count; i++) {
Theodore Ts'o2ed57242013-06-12 11:43:02 -04004677 cond_resched();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004678 if (!bh)
4679 tbh = sb_find_get_block(inode->i_sb,
4680 block + i);
Theodore Ts'o2ed57242013-06-12 11:43:02 -04004681 if (!tbh)
Namhyung Kim87783692010-10-27 21:30:11 -04004682 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004683 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004684 inode, tbh, block + i);
4685 }
4686 }
4687
Theodore Ts'o60e66792010-05-17 07:00:00 -04004688 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004689 * We need to make sure we don't reuse the freed block until
4690 * after the transaction is committed, which we can do by
4691 * treating the block as metadata, below. We make an
4692 * exception if the inode is to be written in writeback mode
4693 * since writeback mode has weak data consistency guarantees.
4694 */
4695 if (!ext4_should_writeback_data(inode))
4696 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004697
Theodore Ts'o84130192011-09-09 18:50:51 -04004698 /*
4699 * If the extent to be freed does not begin on a cluster
4700 * boundary, we need to deal with partial clusters at the
4701 * beginning and end of the extent. Normally we will free
4702 * blocks at the beginning or the end unless we are explicitly
4703 * requested to avoid doing so.
4704 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004705 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004706 if (overflow) {
4707 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4708 overflow = sbi->s_cluster_ratio - overflow;
4709 block += overflow;
4710 if (count > overflow)
4711 count -= overflow;
4712 else
4713 return;
4714 } else {
4715 block -= overflow;
4716 count += overflow;
4717 }
4718 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004719 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004720 if (overflow) {
4721 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4722 if (count > overflow)
4723 count -= overflow;
4724 else
4725 return;
4726 } else
4727 count += sbi->s_cluster_ratio - overflow;
4728 }
4729
Alex Tomasc9de5602008-01-29 00:19:52 -05004730do_more:
4731 overflow = 0;
4732 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4733
Darrick J. Wong163a2032013-08-28 17:35:51 -04004734 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4735 ext4_get_group_info(sb, block_group))))
4736 return;
4737
Alex Tomasc9de5602008-01-29 00:19:52 -05004738 /*
4739 * Check to see if we are freeing blocks across a group
4740 * boundary.
4741 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004742 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4743 overflow = EXT4_C2B(sbi, bit) + count -
4744 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004745 count -= overflow;
4746 }
Lukas Czerner810da242013-03-02 17:18:58 -05004747 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004748 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004749 if (!bitmap_bh) {
4750 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004751 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004752 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004753 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004754 if (!gdp) {
4755 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004756 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004757 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004758
4759 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4760 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4761 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004762 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004763 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004764 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004765
Eric Sandeen12062dd2010-02-15 14:19:27 -05004766 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004767 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004768 /* err = 0. ext4_std_error should be a no op */
4769 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004770 }
4771
4772 BUFFER_TRACE(bitmap_bh, "getting write access");
4773 err = ext4_journal_get_write_access(handle, bitmap_bh);
4774 if (err)
4775 goto error_return;
4776
4777 /*
4778 * We are about to modify some metadata. Call the journal APIs
4779 * to unshare ->b_data if a currently-committing transaction is
4780 * using it
4781 */
4782 BUFFER_TRACE(gd_bh, "get_write_access");
4783 err = ext4_journal_get_write_access(handle, gd_bh);
4784 if (err)
4785 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004786#ifdef AGGRESSIVE_CHECK
4787 {
4788 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004789 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004790 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4791 }
4792#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004793 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004794
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004795 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4796 if (err)
4797 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004798
4799 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004800 struct ext4_free_data *new_entry;
4801 /*
4802 * blocks being freed are metadata. these blocks shouldn't
4803 * be used until this transaction is committed
4804 */
Theodore Ts'oe7676a72013-07-13 00:40:35 -04004805 retry:
Bobi Jam18aadd42012-02-20 17:53:02 -05004806 new_entry = kmem_cache_alloc(ext4_free_data_cachep, GFP_NOFS);
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004807 if (!new_entry) {
Theodore Ts'oe7676a72013-07-13 00:40:35 -04004808 /*
4809 * We use a retry loop because
4810 * ext4_free_blocks() is not allowed to fail.
4811 */
4812 cond_resched();
4813 congestion_wait(BLK_RW_ASYNC, HZ/50);
4814 goto retry;
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004815 }
Bobi Jam18aadd42012-02-20 17:53:02 -05004816 new_entry->efd_start_cluster = bit;
4817 new_entry->efd_group = block_group;
4818 new_entry->efd_count = count_clusters;
4819 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004820
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004821 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004822 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004823 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004824 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004825 /* need to update group_info->bb_free and bitmap
4826 * with group lock held. generate_buddy look at
4827 * them with group lock_held
4828 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004829 if (test_opt(sb, DISCARD)) {
4830 err = ext4_issue_discard(sb, block_group, bit, count);
4831 if (err && err != -EOPNOTSUPP)
4832 ext4_msg(sb, KERN_WARNING, "discard request in"
4833 " group:%d block:%d count:%lu failed"
4834 " with %d", block_group, bit, count,
4835 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04004836 } else
4837 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004838
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004839 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004840 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4841 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004842 }
4843
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004844 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4845 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004846 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004847 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004848 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004849
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004850 if (sbi->s_log_groups_per_flex) {
4851 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004852 atomic64_add(count_clusters,
4853 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004854 }
4855
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04004856 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004857 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
Jan Kara7d734532013-08-17 09:36:54 -04004858 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
4859
4860 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004861
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004862 /* We dirtied the bitmap block */
4863 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4864 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4865
Alex Tomasc9de5602008-01-29 00:19:52 -05004866 /* And the group descriptor block */
4867 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004868 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004869 if (!err)
4870 err = ret;
4871
4872 if (overflow && !err) {
4873 block += count;
4874 count = overflow;
4875 put_bh(bitmap_bh);
4876 goto do_more;
4877 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004878error_return:
4879 brelse(bitmap_bh);
4880 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004881 return;
4882}
Lukas Czerner7360d172010-10-27 21:30:12 -04004883
4884/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004885 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004886 * @handle: handle to this transaction
4887 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004888 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004889 * @count: number of blocks to free
4890 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004891 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004892 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004893int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004894 ext4_fsblk_t block, unsigned long count)
4895{
4896 struct buffer_head *bitmap_bh = NULL;
4897 struct buffer_head *gd_bh;
4898 ext4_group_t block_group;
4899 ext4_grpblk_t bit;
4900 unsigned int i;
4901 struct ext4_group_desc *desc;
4902 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004903 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004904 int err = 0, ret, blk_free_count;
4905 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004906
4907 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4908
Yongqiang Yang4740b832011-07-26 21:51:08 -04004909 if (count == 0)
4910 return 0;
4911
Amir Goldstein2846e822011-05-09 10:46:41 -04004912 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004913 /*
4914 * Check to see if we are freeing blocks across a group
4915 * boundary.
4916 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004917 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4918 ext4_warning(sb, "too much blocks added to group %u\n",
4919 block_group);
4920 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004921 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004922 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004923
Amir Goldstein2846e822011-05-09 10:46:41 -04004924 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004925 if (!bitmap_bh) {
4926 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004927 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004928 }
4929
Amir Goldstein2846e822011-05-09 10:46:41 -04004930 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004931 if (!desc) {
4932 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004933 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004934 }
Amir Goldstein2846e822011-05-09 10:46:41 -04004935
4936 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4937 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4938 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4939 in_range(block + count - 1, ext4_inode_table(sb, desc),
4940 sbi->s_itb_per_group)) {
4941 ext4_error(sb, "Adding blocks in system zones - "
4942 "Block = %llu, count = %lu",
4943 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004944 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004945 goto error_return;
4946 }
4947
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004948 BUFFER_TRACE(bitmap_bh, "getting write access");
4949 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004950 if (err)
4951 goto error_return;
4952
4953 /*
4954 * We are about to modify some metadata. Call the journal APIs
4955 * to unshare ->b_data if a currently-committing transaction is
4956 * using it
4957 */
4958 BUFFER_TRACE(gd_bh, "get_write_access");
4959 err = ext4_journal_get_write_access(handle, gd_bh);
4960 if (err)
4961 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04004962
Amir Goldstein2846e822011-05-09 10:46:41 -04004963 for (i = 0, blocks_freed = 0; i < count; i++) {
4964 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04004965 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04004966 ext4_error(sb, "bit already cleared for block %llu",
4967 (ext4_fsblk_t)(block + i));
4968 BUFFER_TRACE(bitmap_bh, "bit already cleared");
4969 } else {
4970 blocks_freed++;
4971 }
4972 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004973
4974 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4975 if (err)
4976 goto error_return;
4977
4978 /*
4979 * need to update group_info->bb_free and bitmap
4980 * with group lock held. generate_buddy look at
4981 * them with group lock_held
4982 */
Amir Goldstein2846e822011-05-09 10:46:41 -04004983 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004984 mb_clear_bits(bitmap_bh->b_data, bit, count);
4985 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004986 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
4987 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04004988 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004989 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04004990 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04004991 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05004992 EXT4_NUM_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04004993
4994 if (sbi->s_log_groups_per_flex) {
4995 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004996 atomic64_add(EXT4_NUM_B2C(sbi, blocks_freed),
4997 &sbi->s_flex_groups[flex_group].free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04004998 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004999
5000 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005001
5002 /* We dirtied the bitmap block */
5003 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5004 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5005
5006 /* And the group descriptor block */
5007 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5008 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5009 if (!err)
5010 err = ret;
5011
5012error_return:
5013 brelse(bitmap_bh);
5014 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005015 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005016}
5017
5018/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005019 * ext4_trim_extent -- function to TRIM one single free extent in the group
5020 * @sb: super block for the file system
5021 * @start: starting block of the free extent in the alloc. group
5022 * @count: number of blocks to TRIM
5023 * @group: alloc. group we are working with
5024 * @e4b: ext4 buddy for the group
5025 *
5026 * Trim "count" blocks starting at "start" in the "group". To assure that no
5027 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5028 * be called with under the group lock.
5029 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005030static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04005031 ext4_group_t group, struct ext4_buddy *e4b)
jon ernste2cbd582014-04-12 23:01:28 -04005032__releases(bitlock)
5033__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005034{
5035 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005036 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005037
Tao Mab3d4c2b2011-07-11 00:01:52 -04005038 trace_ext4_trim_extent(sb, group, start, count);
5039
Lukas Czerner7360d172010-10-27 21:30:12 -04005040 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5041
5042 ex.fe_start = start;
5043 ex.fe_group = group;
5044 ex.fe_len = count;
5045
5046 /*
5047 * Mark blocks used, so no one can reuse them while
5048 * being trimmed.
5049 */
5050 mb_mark_used(e4b, &ex);
5051 ext4_unlock_group(sb, group);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005052 ret = ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04005053 ext4_lock_group(sb, group);
5054 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005055 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005056}
5057
5058/**
5059 * ext4_trim_all_free -- function to trim all free space in alloc. group
5060 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005061 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005062 * @start: first group block to examine
5063 * @max: last group block to examine
5064 * @minblocks: minimum extent block count
5065 *
5066 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5067 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5068 * the extent.
5069 *
5070 *
5071 * ext4_trim_all_free walks through group's block bitmap searching for free
5072 * extents. When the free extent is found, mark it as used in group buddy
5073 * bitmap. Then issue a TRIM command on this extent and free the extent in
5074 * the group buddy bitmap. This is done until whole group is scanned.
5075 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005076static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005077ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5078 ext4_grpblk_t start, ext4_grpblk_t max,
5079 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005080{
5081 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005082 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005083 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005084 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005085
Tao Mab3d4c2b2011-07-11 00:01:52 -04005086 trace_ext4_trim_all_free(sb, group, start, max);
5087
Lukas Czerner78944082011-05-24 18:16:27 -04005088 ret = ext4_mb_load_buddy(sb, group, &e4b);
5089 if (ret) {
5090 ext4_error(sb, "Error in loading buddy "
5091 "information for %u", group);
5092 return ret;
5093 }
Lukas Czerner78944082011-05-24 18:16:27 -04005094 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005095
5096 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005097 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5098 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5099 goto out;
5100
Lukas Czerner78944082011-05-24 18:16:27 -04005101 start = (e4b.bd_info->bb_first_free > start) ?
5102 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005103
Lukas Czerner913eed832012-03-21 21:22:22 -04005104 while (start <= max) {
5105 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5106 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005107 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005108 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005109
5110 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005111 ret = ext4_trim_extent(sb, start,
5112 next - start, group, &e4b);
5113 if (ret && ret != -EOPNOTSUPP)
5114 break;
5115 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005116 count += next - start;
5117 }
Tao Ma169ddc32011-07-11 00:00:07 -04005118 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005119 start = next + 1;
5120
5121 if (fatal_signal_pending(current)) {
5122 count = -ERESTARTSYS;
5123 break;
5124 }
5125
5126 if (need_resched()) {
5127 ext4_unlock_group(sb, group);
5128 cond_resched();
5129 ext4_lock_group(sb, group);
5130 }
5131
Tao Ma169ddc32011-07-11 00:00:07 -04005132 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005133 break;
5134 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005135
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005136 if (!ret) {
5137 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005138 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005139 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005140out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005141 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005142 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005143
5144 ext4_debug("trimmed %d blocks in the group %d\n",
5145 count, group);
5146
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005147 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005148}
5149
5150/**
5151 * ext4_trim_fs() -- trim ioctl handle function
5152 * @sb: superblock for filesystem
5153 * @range: fstrim_range structure
5154 *
5155 * start: First Byte to trim
5156 * len: number of Bytes to trim from start
5157 * minlen: minimum extent length in Bytes
5158 * ext4_trim_fs goes through all allocation groups containing Bytes from
5159 * start to start+len. For each such a group ext4_trim_all_free function
5160 * is invoked to trim all free space.
5161 */
5162int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
5163{
Lukas Czerner78944082011-05-24 18:16:27 -04005164 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005165 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005166 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005167 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005168 ext4_fsblk_t first_data_blk =
5169 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005170 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005171 int ret = 0;
5172
5173 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005174 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005175 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5176 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005177
Lukas Czerner5de35e82012-10-22 18:01:19 -04005178 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5179 start >= max_blks ||
5180 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005181 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005182 if (end >= max_blks)
5183 end = max_blks - 1;
5184 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005185 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005186 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005187 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005188
Lukas Czerner913eed832012-03-21 21:22:22 -04005189 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005190 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005191 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005192 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005193 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005194
Lukas Czerner913eed832012-03-21 21:22:22 -04005195 /* end now represents the last cluster to discard in this group */
5196 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005197
5198 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005199 grp = ext4_get_group_info(sb, group);
5200 /* We only do this if the grp has never been initialized */
5201 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
5202 ret = ext4_mb_init_group(sb, group);
5203 if (ret)
5204 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005205 }
5206
Tao Ma0ba08512011-03-23 15:48:11 -04005207 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005208 * For all the groups except the last one, last cluster will
5209 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5210 * change it for the last group, note that last_cluster is
5211 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005212 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005213 if (group == last_group)
5214 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005215
Lukas Czerner78944082011-05-24 18:16:27 -04005216 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005217 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005218 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005219 if (cnt < 0) {
5220 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005221 break;
5222 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005223 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005224 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005225
5226 /*
5227 * For every group except the first one, we are sure
5228 * that the first cluster to discard will be cluster #0.
5229 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005230 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005231 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005232
Tao Ma3d56b8d2011-07-11 00:03:38 -04005233 if (!ret)
5234 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5235
Tao Ma22f10452011-07-10 23:52:37 -04005236out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005237 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005238 return ret;
5239}