xref: /linux/fs/ext4/mballoc.c (revision 296c355cd6443d89fa251885a8d78778fe111dc4)
1c9de560dSAlex Tomas /*
2c9de560dSAlex Tomas  * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3c9de560dSAlex Tomas  * Written by Alex Tomas <alex@clusterfs.com>
4c9de560dSAlex Tomas  *
5c9de560dSAlex Tomas  * This program is free software; you can redistribute it and/or modify
6c9de560dSAlex Tomas  * it under the terms of the GNU General Public License version 2 as
7c9de560dSAlex Tomas  * published by the Free Software Foundation.
8c9de560dSAlex Tomas  *
9c9de560dSAlex Tomas  * This program is distributed in the hope that it will be useful,
10c9de560dSAlex Tomas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11c9de560dSAlex Tomas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12c9de560dSAlex Tomas  * GNU General Public License for more details.
13c9de560dSAlex Tomas  *
14c9de560dSAlex Tomas  * You should have received a copy of the GNU General Public Licens
15c9de560dSAlex Tomas  * along with this program; if not, write to the Free Software
16c9de560dSAlex Tomas  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
17c9de560dSAlex Tomas  */
18c9de560dSAlex Tomas 
19c9de560dSAlex Tomas 
20c9de560dSAlex Tomas /*
21c9de560dSAlex Tomas  * mballoc.c contains the multiblocks allocation routines
22c9de560dSAlex Tomas  */
23c9de560dSAlex Tomas 
248f6e39a7SMingming Cao #include "mballoc.h"
256ba495e9STheodore Ts'o #include <linux/debugfs.h>
269bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
279bffad1eSTheodore Ts'o 
28c9de560dSAlex Tomas /*
29c9de560dSAlex Tomas  * MUSTDO:
30c9de560dSAlex Tomas  *   - test ext4_ext_search_left() and ext4_ext_search_right()
31c9de560dSAlex Tomas  *   - search for metadata in few groups
32c9de560dSAlex Tomas  *
33c9de560dSAlex Tomas  * TODO v4:
34c9de560dSAlex Tomas  *   - normalization should take into account whether file is still open
35c9de560dSAlex Tomas  *   - discard preallocations if no free space left (policy?)
36c9de560dSAlex Tomas  *   - don't normalize tails
37c9de560dSAlex Tomas  *   - quota
38c9de560dSAlex Tomas  *   - reservation for superuser
39c9de560dSAlex Tomas  *
40c9de560dSAlex Tomas  * TODO v3:
41c9de560dSAlex Tomas  *   - bitmap read-ahead (proposed by Oleg Drokin aka green)
42c9de560dSAlex Tomas  *   - track min/max extents in each group for better group selection
43c9de560dSAlex Tomas  *   - mb_mark_used() may allocate chunk right after splitting buddy
44c9de560dSAlex Tomas  *   - tree of groups sorted by number of free blocks
45c9de560dSAlex Tomas  *   - error handling
46c9de560dSAlex Tomas  */
47c9de560dSAlex Tomas 
48c9de560dSAlex Tomas /*
49c9de560dSAlex Tomas  * The allocation request involve request for multiple number of blocks
50c9de560dSAlex Tomas  * near to the goal(block) value specified.
51c9de560dSAlex Tomas  *
52b713a5ecSTheodore Ts'o  * During initialization phase of the allocator we decide to use the
53b713a5ecSTheodore Ts'o  * group preallocation or inode preallocation depending on the size of
54b713a5ecSTheodore Ts'o  * the file. The size of the file could be the resulting file size we
55b713a5ecSTheodore Ts'o  * would have after allocation, or the current file size, which ever
56b713a5ecSTheodore Ts'o  * is larger. If the size is less than sbi->s_mb_stream_request we
57b713a5ecSTheodore Ts'o  * select to use the group preallocation. The default value of
58b713a5ecSTheodore Ts'o  * s_mb_stream_request is 16 blocks. This can also be tuned via
59b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
60b713a5ecSTheodore Ts'o  * terms of number of blocks.
61c9de560dSAlex Tomas  *
62c9de560dSAlex Tomas  * The main motivation for having small file use group preallocation is to
63b713a5ecSTheodore Ts'o  * ensure that we have small files closer together on the disk.
64c9de560dSAlex Tomas  *
65b713a5ecSTheodore Ts'o  * First stage the allocator looks at the inode prealloc list,
66b713a5ecSTheodore Ts'o  * ext4_inode_info->i_prealloc_list, which contains list of prealloc
67b713a5ecSTheodore Ts'o  * spaces for this particular inode. The inode prealloc space is
68b713a5ecSTheodore Ts'o  * represented as:
69c9de560dSAlex Tomas  *
70c9de560dSAlex Tomas  * pa_lstart -> the logical start block for this prealloc space
71c9de560dSAlex Tomas  * pa_pstart -> the physical start block for this prealloc space
72c9de560dSAlex Tomas  * pa_len    -> lenght for this prealloc space
73c9de560dSAlex Tomas  * pa_free   ->  free space available in this prealloc space
74c9de560dSAlex Tomas  *
75c9de560dSAlex Tomas  * The inode preallocation space is used looking at the _logical_ start
76c9de560dSAlex Tomas  * block. If only the logical file block falls within the range of prealloc
77c9de560dSAlex Tomas  * space we will consume the particular prealloc space. This make sure that
78c9de560dSAlex Tomas  * that the we have contiguous physical blocks representing the file blocks
79c9de560dSAlex Tomas  *
80c9de560dSAlex Tomas  * The important thing to be noted in case of inode prealloc space is that
81c9de560dSAlex Tomas  * we don't modify the values associated to inode prealloc space except
82c9de560dSAlex Tomas  * pa_free.
83c9de560dSAlex Tomas  *
84c9de560dSAlex Tomas  * If we are not able to find blocks in the inode prealloc space and if we
85c9de560dSAlex Tomas  * have the group allocation flag set then we look at the locality group
86c9de560dSAlex Tomas  * prealloc space. These are per CPU prealloc list repreasented as
87c9de560dSAlex Tomas  *
88c9de560dSAlex Tomas  * ext4_sb_info.s_locality_groups[smp_processor_id()]
89c9de560dSAlex Tomas  *
90c9de560dSAlex Tomas  * The reason for having a per cpu locality group is to reduce the contention
91c9de560dSAlex Tomas  * between CPUs. It is possible to get scheduled at this point.
92c9de560dSAlex Tomas  *
93c9de560dSAlex Tomas  * The locality group prealloc space is used looking at whether we have
94c9de560dSAlex Tomas  * enough free space (pa_free) withing the prealloc space.
95c9de560dSAlex Tomas  *
96c9de560dSAlex Tomas  * If we can't allocate blocks via inode prealloc or/and locality group
97c9de560dSAlex Tomas  * prealloc then we look at the buddy cache. The buddy cache is represented
98c9de560dSAlex Tomas  * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
99c9de560dSAlex Tomas  * mapped to the buddy and bitmap information regarding different
100c9de560dSAlex Tomas  * groups. The buddy information is attached to buddy cache inode so that
101c9de560dSAlex Tomas  * we can access them through the page cache. The information regarding
102c9de560dSAlex Tomas  * each group is loaded via ext4_mb_load_buddy.  The information involve
103c9de560dSAlex Tomas  * block bitmap and buddy information. The information are stored in the
104c9de560dSAlex Tomas  * inode as:
105c9de560dSAlex Tomas  *
106c9de560dSAlex Tomas  *  {                        page                        }
107c3a326a6SAneesh Kumar K.V  *  [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
108c9de560dSAlex Tomas  *
109c9de560dSAlex Tomas  *
110c9de560dSAlex Tomas  * one block each for bitmap and buddy information.  So for each group we
111c9de560dSAlex Tomas  * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
112c9de560dSAlex Tomas  * blocksize) blocks.  So it can have information regarding groups_per_page
113c9de560dSAlex Tomas  * which is blocks_per_page/2
114c9de560dSAlex Tomas  *
115c9de560dSAlex Tomas  * The buddy cache inode is not stored on disk. The inode is thrown
116c9de560dSAlex Tomas  * away when the filesystem is unmounted.
117c9de560dSAlex Tomas  *
118c9de560dSAlex Tomas  * We look for count number of blocks in the buddy cache. If we were able
119c9de560dSAlex Tomas  * to locate that many free blocks we return with additional information
120c9de560dSAlex Tomas  * regarding rest of the contiguous physical block available
121c9de560dSAlex Tomas  *
122c9de560dSAlex Tomas  * Before allocating blocks via buddy cache we normalize the request
123c9de560dSAlex Tomas  * blocks. This ensure we ask for more blocks that we needed. The extra
124c9de560dSAlex Tomas  * blocks that we get after allocation is added to the respective prealloc
125c9de560dSAlex Tomas  * list. In case of inode preallocation we follow a list of heuristics
126c9de560dSAlex Tomas  * based on file size. This can be found in ext4_mb_normalize_request. If
127c9de560dSAlex Tomas  * we are doing a group prealloc we try to normalize the request to
128b713a5ecSTheodore Ts'o  * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
129c9de560dSAlex Tomas  * 512 blocks. This can be tuned via
130b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
131c9de560dSAlex Tomas  * terms of number of blocks. If we have mounted the file system with -O
132c9de560dSAlex Tomas  * stripe=<value> option the group prealloc request is normalized to the
133c9de560dSAlex Tomas  * stripe value (sbi->s_stripe)
134c9de560dSAlex Tomas  *
135b713a5ecSTheodore Ts'o  * The regular allocator(using the buddy cache) supports few tunables.
136c9de560dSAlex Tomas  *
137b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_min_to_scan
138b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_max_to_scan
139b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_order2_req
140c9de560dSAlex Tomas  *
141b713a5ecSTheodore Ts'o  * The regular allocator uses buddy scan only if the request len is power of
142c9de560dSAlex Tomas  * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
143c9de560dSAlex Tomas  * value of s_mb_order2_reqs can be tuned via
144b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_order2_req.  If the request len is equal to
145c9de560dSAlex Tomas  * stripe size (sbi->s_stripe), we try to search for contigous block in
146b713a5ecSTheodore Ts'o  * stripe size. This should result in better allocation on RAID setups. If
147b713a5ecSTheodore Ts'o  * not, we search in the specific group using bitmap for best extents. The
148b713a5ecSTheodore Ts'o  * tunable min_to_scan and max_to_scan control the behaviour here.
149c9de560dSAlex Tomas  * min_to_scan indicate how long the mballoc __must__ look for a best
150b713a5ecSTheodore Ts'o  * extent and max_to_scan indicates how long the mballoc __can__ look for a
151c9de560dSAlex Tomas  * best extent in the found extents. Searching for the blocks starts with
152c9de560dSAlex Tomas  * the group specified as the goal value in allocation context via
153c9de560dSAlex Tomas  * ac_g_ex. Each group is first checked based on the criteria whether it
154c9de560dSAlex Tomas  * can used for allocation. ext4_mb_good_group explains how the groups are
155c9de560dSAlex Tomas  * checked.
156c9de560dSAlex Tomas  *
157c9de560dSAlex Tomas  * Both the prealloc space are getting populated as above. So for the first
158c9de560dSAlex Tomas  * request we will hit the buddy cache which will result in this prealloc
159c9de560dSAlex Tomas  * space getting filled. The prealloc space is then later used for the
160c9de560dSAlex Tomas  * subsequent request.
161c9de560dSAlex Tomas  */
162c9de560dSAlex Tomas 
163c9de560dSAlex Tomas /*
164c9de560dSAlex Tomas  * mballoc operates on the following data:
165c9de560dSAlex Tomas  *  - on-disk bitmap
166c9de560dSAlex Tomas  *  - in-core buddy (actually includes buddy and bitmap)
167c9de560dSAlex Tomas  *  - preallocation descriptors (PAs)
168c9de560dSAlex Tomas  *
169c9de560dSAlex Tomas  * there are two types of preallocations:
170c9de560dSAlex Tomas  *  - inode
171c9de560dSAlex Tomas  *    assiged to specific inode and can be used for this inode only.
172c9de560dSAlex Tomas  *    it describes part of inode's space preallocated to specific
173c9de560dSAlex Tomas  *    physical blocks. any block from that preallocated can be used
174c9de560dSAlex Tomas  *    independent. the descriptor just tracks number of blocks left
175c9de560dSAlex Tomas  *    unused. so, before taking some block from descriptor, one must
176c9de560dSAlex Tomas  *    make sure corresponded logical block isn't allocated yet. this
177c9de560dSAlex Tomas  *    also means that freeing any block within descriptor's range
178c9de560dSAlex Tomas  *    must discard all preallocated blocks.
179c9de560dSAlex Tomas  *  - locality group
180c9de560dSAlex Tomas  *    assigned to specific locality group which does not translate to
181c9de560dSAlex Tomas  *    permanent set of inodes: inode can join and leave group. space
182c9de560dSAlex Tomas  *    from this type of preallocation can be used for any inode. thus
183c9de560dSAlex Tomas  *    it's consumed from the beginning to the end.
184c9de560dSAlex Tomas  *
185c9de560dSAlex Tomas  * relation between them can be expressed as:
186c9de560dSAlex Tomas  *    in-core buddy = on-disk bitmap + preallocation descriptors
187c9de560dSAlex Tomas  *
188c9de560dSAlex Tomas  * this mean blocks mballoc considers used are:
189c9de560dSAlex Tomas  *  - allocated blocks (persistent)
190c9de560dSAlex Tomas  *  - preallocated blocks (non-persistent)
191c9de560dSAlex Tomas  *
192c9de560dSAlex Tomas  * consistency in mballoc world means that at any time a block is either
193c9de560dSAlex Tomas  * free or used in ALL structures. notice: "any time" should not be read
194c9de560dSAlex Tomas  * literally -- time is discrete and delimited by locks.
195c9de560dSAlex Tomas  *
196c9de560dSAlex Tomas  *  to keep it simple, we don't use block numbers, instead we count number of
197c9de560dSAlex Tomas  *  blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
198c9de560dSAlex Tomas  *
199c9de560dSAlex Tomas  * all operations can be expressed as:
200c9de560dSAlex Tomas  *  - init buddy:			buddy = on-disk + PAs
201c9de560dSAlex Tomas  *  - new PA:				buddy += N; PA = N
202c9de560dSAlex Tomas  *  - use inode PA:			on-disk += N; PA -= N
203c9de560dSAlex Tomas  *  - discard inode PA			buddy -= on-disk - PA; PA = 0
204c9de560dSAlex Tomas  *  - use locality group PA		on-disk += N; PA -= N
205c9de560dSAlex Tomas  *  - discard locality group PA		buddy -= PA; PA = 0
206c9de560dSAlex Tomas  *  note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
207c9de560dSAlex Tomas  *        is used in real operation because we can't know actual used
208c9de560dSAlex Tomas  *        bits from PA, only from on-disk bitmap
209c9de560dSAlex Tomas  *
210c9de560dSAlex Tomas  * if we follow this strict logic, then all operations above should be atomic.
211c9de560dSAlex Tomas  * given some of them can block, we'd have to use something like semaphores
212c9de560dSAlex Tomas  * killing performance on high-end SMP hardware. let's try to relax it using
213c9de560dSAlex Tomas  * the following knowledge:
214c9de560dSAlex Tomas  *  1) if buddy is referenced, it's already initialized
215c9de560dSAlex Tomas  *  2) while block is used in buddy and the buddy is referenced,
216c9de560dSAlex Tomas  *     nobody can re-allocate that block
217c9de560dSAlex Tomas  *  3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
218c9de560dSAlex Tomas  *     bit set and PA claims same block, it's OK. IOW, one can set bit in
219c9de560dSAlex Tomas  *     on-disk bitmap if buddy has same bit set or/and PA covers corresponded
220c9de560dSAlex Tomas  *     block
221c9de560dSAlex Tomas  *
222c9de560dSAlex Tomas  * so, now we're building a concurrency table:
223c9de560dSAlex Tomas  *  - init buddy vs.
224c9de560dSAlex Tomas  *    - new PA
225c9de560dSAlex Tomas  *      blocks for PA are allocated in the buddy, buddy must be referenced
226c9de560dSAlex Tomas  *      until PA is linked to allocation group to avoid concurrent buddy init
227c9de560dSAlex Tomas  *    - use inode PA
228c9de560dSAlex Tomas  *      we need to make sure that either on-disk bitmap or PA has uptodate data
229c9de560dSAlex Tomas  *      given (3) we care that PA-=N operation doesn't interfere with init
230c9de560dSAlex Tomas  *    - discard inode PA
231c9de560dSAlex Tomas  *      the simplest way would be to have buddy initialized by the discard
232c9de560dSAlex Tomas  *    - use locality group PA
233c9de560dSAlex Tomas  *      again PA-=N must be serialized with init
234c9de560dSAlex Tomas  *    - discard locality group PA
235c9de560dSAlex Tomas  *      the simplest way would be to have buddy initialized by the discard
236c9de560dSAlex Tomas  *  - new PA vs.
237c9de560dSAlex Tomas  *    - use inode PA
238c9de560dSAlex Tomas  *      i_data_sem serializes them
239c9de560dSAlex Tomas  *    - discard inode PA
240c9de560dSAlex Tomas  *      discard process must wait until PA isn't used by another process
241c9de560dSAlex Tomas  *    - use locality group PA
242c9de560dSAlex Tomas  *      some mutex should serialize them
243c9de560dSAlex Tomas  *    - discard locality group PA
244c9de560dSAlex Tomas  *      discard process must wait until PA isn't used by another process
245c9de560dSAlex Tomas  *  - use inode PA
246c9de560dSAlex Tomas  *    - use inode PA
247c9de560dSAlex Tomas  *      i_data_sem or another mutex should serializes them
248c9de560dSAlex Tomas  *    - discard inode PA
249c9de560dSAlex Tomas  *      discard process must wait until PA isn't used by another process
250c9de560dSAlex Tomas  *    - use locality group PA
251c9de560dSAlex Tomas  *      nothing wrong here -- they're different PAs covering different blocks
252c9de560dSAlex Tomas  *    - discard locality group PA
253c9de560dSAlex Tomas  *      discard process must wait until PA isn't used by another process
254c9de560dSAlex Tomas  *
255c9de560dSAlex Tomas  * now we're ready to make few consequences:
256c9de560dSAlex Tomas  *  - PA is referenced and while it is no discard is possible
257c9de560dSAlex Tomas  *  - PA is referenced until block isn't marked in on-disk bitmap
258c9de560dSAlex Tomas  *  - PA changes only after on-disk bitmap
259c9de560dSAlex Tomas  *  - discard must not compete with init. either init is done before
260c9de560dSAlex Tomas  *    any discard or they're serialized somehow
261c9de560dSAlex Tomas  *  - buddy init as sum of on-disk bitmap and PAs is done atomically
262c9de560dSAlex Tomas  *
263c9de560dSAlex Tomas  * a special case when we've used PA to emptiness. no need to modify buddy
264c9de560dSAlex Tomas  * in this case, but we should care about concurrent init
265c9de560dSAlex Tomas  *
266c9de560dSAlex Tomas  */
267c9de560dSAlex Tomas 
268c9de560dSAlex Tomas  /*
269c9de560dSAlex Tomas  * Logic in few words:
270c9de560dSAlex Tomas  *
271c9de560dSAlex Tomas  *  - allocation:
272c9de560dSAlex Tomas  *    load group
273c9de560dSAlex Tomas  *    find blocks
274c9de560dSAlex Tomas  *    mark bits in on-disk bitmap
275c9de560dSAlex Tomas  *    release group
276c9de560dSAlex Tomas  *
277c9de560dSAlex Tomas  *  - use preallocation:
278c9de560dSAlex Tomas  *    find proper PA (per-inode or group)
279c9de560dSAlex Tomas  *    load group
280c9de560dSAlex Tomas  *    mark bits in on-disk bitmap
281c9de560dSAlex Tomas  *    release group
282c9de560dSAlex Tomas  *    release PA
283c9de560dSAlex Tomas  *
284c9de560dSAlex Tomas  *  - free:
285c9de560dSAlex Tomas  *    load group
286c9de560dSAlex Tomas  *    mark bits in on-disk bitmap
287c9de560dSAlex Tomas  *    release group
288c9de560dSAlex Tomas  *
289c9de560dSAlex Tomas  *  - discard preallocations in group:
290c9de560dSAlex Tomas  *    mark PAs deleted
291c9de560dSAlex Tomas  *    move them onto local list
292c9de560dSAlex Tomas  *    load on-disk bitmap
293c9de560dSAlex Tomas  *    load group
294c9de560dSAlex Tomas  *    remove PA from object (inode or locality group)
295c9de560dSAlex Tomas  *    mark free blocks in-core
296c9de560dSAlex Tomas  *
297c9de560dSAlex Tomas  *  - discard inode's preallocations:
298c9de560dSAlex Tomas  */
299c9de560dSAlex Tomas 
300c9de560dSAlex Tomas /*
301c9de560dSAlex Tomas  * Locking rules
302c9de560dSAlex Tomas  *
303c9de560dSAlex Tomas  * Locks:
304c9de560dSAlex Tomas  *  - bitlock on a group	(group)
305c9de560dSAlex Tomas  *  - object (inode/locality)	(object)
306c9de560dSAlex Tomas  *  - per-pa lock		(pa)
307c9de560dSAlex Tomas  *
308c9de560dSAlex Tomas  * Paths:
309c9de560dSAlex Tomas  *  - new pa
310c9de560dSAlex Tomas  *    object
311c9de560dSAlex Tomas  *    group
312c9de560dSAlex Tomas  *
313c9de560dSAlex Tomas  *  - find and use pa:
314c9de560dSAlex Tomas  *    pa
315c9de560dSAlex Tomas  *
316c9de560dSAlex Tomas  *  - release consumed pa:
317c9de560dSAlex Tomas  *    pa
318c9de560dSAlex Tomas  *    group
319c9de560dSAlex Tomas  *    object
320c9de560dSAlex Tomas  *
321c9de560dSAlex Tomas  *  - generate in-core bitmap:
322c9de560dSAlex Tomas  *    group
323c9de560dSAlex Tomas  *        pa
324c9de560dSAlex Tomas  *
325c9de560dSAlex Tomas  *  - discard all for given object (inode, locality group):
326c9de560dSAlex Tomas  *    object
327c9de560dSAlex Tomas  *        pa
328c9de560dSAlex Tomas  *    group
329c9de560dSAlex Tomas  *
330c9de560dSAlex Tomas  *  - discard all for given group:
331c9de560dSAlex Tomas  *    group
332c9de560dSAlex Tomas  *        pa
333c9de560dSAlex Tomas  *    group
334c9de560dSAlex Tomas  *        object
335c9de560dSAlex Tomas  *
336c9de560dSAlex Tomas  */
337c3a326a6SAneesh Kumar K.V static struct kmem_cache *ext4_pspace_cachep;
338c3a326a6SAneesh Kumar K.V static struct kmem_cache *ext4_ac_cachep;
339c3a326a6SAneesh Kumar K.V static struct kmem_cache *ext4_free_ext_cachep;
340c3a326a6SAneesh Kumar K.V static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
341c3a326a6SAneesh Kumar K.V 					ext4_group_t group);
3427a2fcbf7SAneesh Kumar K.V static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3437a2fcbf7SAneesh Kumar K.V 						ext4_group_t group);
344c3a326a6SAneesh Kumar K.V static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
345c3a326a6SAneesh Kumar K.V 
346ffad0a44SAneesh Kumar K.V static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
347ffad0a44SAneesh Kumar K.V {
348c9de560dSAlex Tomas #if BITS_PER_LONG == 64
349ffad0a44SAneesh Kumar K.V 	*bit += ((unsigned long) addr & 7UL) << 3;
350ffad0a44SAneesh Kumar K.V 	addr = (void *) ((unsigned long) addr & ~7UL);
351c9de560dSAlex Tomas #elif BITS_PER_LONG == 32
352ffad0a44SAneesh Kumar K.V 	*bit += ((unsigned long) addr & 3UL) << 3;
353ffad0a44SAneesh Kumar K.V 	addr = (void *) ((unsigned long) addr & ~3UL);
354c9de560dSAlex Tomas #else
355c9de560dSAlex Tomas #error "how many bits you are?!"
356c9de560dSAlex Tomas #endif
357ffad0a44SAneesh Kumar K.V 	return addr;
358ffad0a44SAneesh Kumar K.V }
359c9de560dSAlex Tomas 
360c9de560dSAlex Tomas static inline int mb_test_bit(int bit, void *addr)
361c9de560dSAlex Tomas {
362c9de560dSAlex Tomas 	/*
363c9de560dSAlex Tomas 	 * ext4_test_bit on architecture like powerpc
364c9de560dSAlex Tomas 	 * needs unsigned long aligned address
365c9de560dSAlex Tomas 	 */
366ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&bit, addr);
367c9de560dSAlex Tomas 	return ext4_test_bit(bit, addr);
368c9de560dSAlex Tomas }
369c9de560dSAlex Tomas 
370c9de560dSAlex Tomas static inline void mb_set_bit(int bit, void *addr)
371c9de560dSAlex Tomas {
372ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&bit, addr);
373c9de560dSAlex Tomas 	ext4_set_bit(bit, addr);
374c9de560dSAlex Tomas }
375c9de560dSAlex Tomas 
376c9de560dSAlex Tomas static inline void mb_clear_bit(int bit, void *addr)
377c9de560dSAlex Tomas {
378ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&bit, addr);
379c9de560dSAlex Tomas 	ext4_clear_bit(bit, addr);
380c9de560dSAlex Tomas }
381c9de560dSAlex Tomas 
382ffad0a44SAneesh Kumar K.V static inline int mb_find_next_zero_bit(void *addr, int max, int start)
383ffad0a44SAneesh Kumar K.V {
384e7dfb246SAneesh Kumar K.V 	int fix = 0, ret, tmpmax;
385ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&fix, addr);
386e7dfb246SAneesh Kumar K.V 	tmpmax = max + fix;
387ffad0a44SAneesh Kumar K.V 	start += fix;
388ffad0a44SAneesh Kumar K.V 
389e7dfb246SAneesh Kumar K.V 	ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
390e7dfb246SAneesh Kumar K.V 	if (ret > max)
391e7dfb246SAneesh Kumar K.V 		return max;
392e7dfb246SAneesh Kumar K.V 	return ret;
393ffad0a44SAneesh Kumar K.V }
394ffad0a44SAneesh Kumar K.V 
395ffad0a44SAneesh Kumar K.V static inline int mb_find_next_bit(void *addr, int max, int start)
396ffad0a44SAneesh Kumar K.V {
397e7dfb246SAneesh Kumar K.V 	int fix = 0, ret, tmpmax;
398ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&fix, addr);
399e7dfb246SAneesh Kumar K.V 	tmpmax = max + fix;
400ffad0a44SAneesh Kumar K.V 	start += fix;
401ffad0a44SAneesh Kumar K.V 
402e7dfb246SAneesh Kumar K.V 	ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
403e7dfb246SAneesh Kumar K.V 	if (ret > max)
404e7dfb246SAneesh Kumar K.V 		return max;
405e7dfb246SAneesh Kumar K.V 	return ret;
406ffad0a44SAneesh Kumar K.V }
407ffad0a44SAneesh Kumar K.V 
408c9de560dSAlex Tomas static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
409c9de560dSAlex Tomas {
410c9de560dSAlex Tomas 	char *bb;
411c9de560dSAlex Tomas 
412c9de560dSAlex Tomas 	BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
413c9de560dSAlex Tomas 	BUG_ON(max == NULL);
414c9de560dSAlex Tomas 
415c9de560dSAlex Tomas 	if (order > e4b->bd_blkbits + 1) {
416c9de560dSAlex Tomas 		*max = 0;
417c9de560dSAlex Tomas 		return NULL;
418c9de560dSAlex Tomas 	}
419c9de560dSAlex Tomas 
420c9de560dSAlex Tomas 	/* at order 0 we see each particular block */
421c9de560dSAlex Tomas 	*max = 1 << (e4b->bd_blkbits + 3);
422c9de560dSAlex Tomas 	if (order == 0)
423c9de560dSAlex Tomas 		return EXT4_MB_BITMAP(e4b);
424c9de560dSAlex Tomas 
425c9de560dSAlex Tomas 	bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
426c9de560dSAlex Tomas 	*max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
427c9de560dSAlex Tomas 
428c9de560dSAlex Tomas 	return bb;
429c9de560dSAlex Tomas }
430c9de560dSAlex Tomas 
431c9de560dSAlex Tomas #ifdef DOUBLE_CHECK
432c9de560dSAlex Tomas static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
433c9de560dSAlex Tomas 			   int first, int count)
434c9de560dSAlex Tomas {
435c9de560dSAlex Tomas 	int i;
436c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
437c9de560dSAlex Tomas 
438c9de560dSAlex Tomas 	if (unlikely(e4b->bd_info->bb_bitmap == NULL))
439c9de560dSAlex Tomas 		return;
440bc8e6740SVincent Minet 	assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
441c9de560dSAlex Tomas 	for (i = 0; i < count; i++) {
442c9de560dSAlex Tomas 		if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
443c9de560dSAlex Tomas 			ext4_fsblk_t blocknr;
444c9de560dSAlex Tomas 			blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
445c9de560dSAlex Tomas 			blocknr += first + i;
446c9de560dSAlex Tomas 			blocknr +=
447c9de560dSAlex Tomas 			    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
4485d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group,
4495d1b1b3fSAneesh Kumar K.V 				   __func__, "double-free of inode"
450a9df9a49STheodore Ts'o 				   " %lu's block %llu(bit %u in group %u)",
451c9de560dSAlex Tomas 				   inode ? inode->i_ino : 0, blocknr,
452c9de560dSAlex Tomas 				   first + i, e4b->bd_group);
453c9de560dSAlex Tomas 		}
454c9de560dSAlex Tomas 		mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
455c9de560dSAlex Tomas 	}
456c9de560dSAlex Tomas }
457c9de560dSAlex Tomas 
458c9de560dSAlex Tomas static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
459c9de560dSAlex Tomas {
460c9de560dSAlex Tomas 	int i;
461c9de560dSAlex Tomas 
462c9de560dSAlex Tomas 	if (unlikely(e4b->bd_info->bb_bitmap == NULL))
463c9de560dSAlex Tomas 		return;
464bc8e6740SVincent Minet 	assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
465c9de560dSAlex Tomas 	for (i = 0; i < count; i++) {
466c9de560dSAlex Tomas 		BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
467c9de560dSAlex Tomas 		mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
468c9de560dSAlex Tomas 	}
469c9de560dSAlex Tomas }
470c9de560dSAlex Tomas 
471c9de560dSAlex Tomas static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
472c9de560dSAlex Tomas {
473c9de560dSAlex Tomas 	if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
474c9de560dSAlex Tomas 		unsigned char *b1, *b2;
475c9de560dSAlex Tomas 		int i;
476c9de560dSAlex Tomas 		b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
477c9de560dSAlex Tomas 		b2 = (unsigned char *) bitmap;
478c9de560dSAlex Tomas 		for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
479c9de560dSAlex Tomas 			if (b1[i] != b2[i]) {
480a9df9a49STheodore Ts'o 				printk(KERN_ERR "corruption in group %u "
4814776004fSTheodore Ts'o 				       "at byte %u(%u): %x in copy != %x "
4824776004fSTheodore Ts'o 				       "on disk/prealloc\n",
483c9de560dSAlex Tomas 				       e4b->bd_group, i, i * 8, b1[i], b2[i]);
484c9de560dSAlex Tomas 				BUG();
485c9de560dSAlex Tomas 			}
486c9de560dSAlex Tomas 		}
487c9de560dSAlex Tomas 	}
488c9de560dSAlex Tomas }
489c9de560dSAlex Tomas 
490c9de560dSAlex Tomas #else
491c9de560dSAlex Tomas static inline void mb_free_blocks_double(struct inode *inode,
492c9de560dSAlex Tomas 				struct ext4_buddy *e4b, int first, int count)
493c9de560dSAlex Tomas {
494c9de560dSAlex Tomas 	return;
495c9de560dSAlex Tomas }
496c9de560dSAlex Tomas static inline void mb_mark_used_double(struct ext4_buddy *e4b,
497c9de560dSAlex Tomas 						int first, int count)
498c9de560dSAlex Tomas {
499c9de560dSAlex Tomas 	return;
500c9de560dSAlex Tomas }
501c9de560dSAlex Tomas static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
502c9de560dSAlex Tomas {
503c9de560dSAlex Tomas 	return;
504c9de560dSAlex Tomas }
505c9de560dSAlex Tomas #endif
506c9de560dSAlex Tomas 
507c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
508c9de560dSAlex Tomas 
509c9de560dSAlex Tomas #define MB_CHECK_ASSERT(assert)						\
510c9de560dSAlex Tomas do {									\
511c9de560dSAlex Tomas 	if (!(assert)) {						\
512c9de560dSAlex Tomas 		printk(KERN_EMERG					\
513c9de560dSAlex Tomas 			"Assertion failure in %s() at %s:%d: \"%s\"\n",	\
514c9de560dSAlex Tomas 			function, file, line, # assert);		\
515c9de560dSAlex Tomas 		BUG();							\
516c9de560dSAlex Tomas 	}								\
517c9de560dSAlex Tomas } while (0)
518c9de560dSAlex Tomas 
519c9de560dSAlex Tomas static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
520c9de560dSAlex Tomas 				const char *function, int line)
521c9de560dSAlex Tomas {
522c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
523c9de560dSAlex Tomas 	int order = e4b->bd_blkbits + 1;
524c9de560dSAlex Tomas 	int max;
525c9de560dSAlex Tomas 	int max2;
526c9de560dSAlex Tomas 	int i;
527c9de560dSAlex Tomas 	int j;
528c9de560dSAlex Tomas 	int k;
529c9de560dSAlex Tomas 	int count;
530c9de560dSAlex Tomas 	struct ext4_group_info *grp;
531c9de560dSAlex Tomas 	int fragments = 0;
532c9de560dSAlex Tomas 	int fstart;
533c9de560dSAlex Tomas 	struct list_head *cur;
534c9de560dSAlex Tomas 	void *buddy;
535c9de560dSAlex Tomas 	void *buddy2;
536c9de560dSAlex Tomas 
537c9de560dSAlex Tomas 	{
538c9de560dSAlex Tomas 		static int mb_check_counter;
539c9de560dSAlex Tomas 		if (mb_check_counter++ % 100 != 0)
540c9de560dSAlex Tomas 			return 0;
541c9de560dSAlex Tomas 	}
542c9de560dSAlex Tomas 
543c9de560dSAlex Tomas 	while (order > 1) {
544c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, order, &max);
545c9de560dSAlex Tomas 		MB_CHECK_ASSERT(buddy);
546c9de560dSAlex Tomas 		buddy2 = mb_find_buddy(e4b, order - 1, &max2);
547c9de560dSAlex Tomas 		MB_CHECK_ASSERT(buddy2);
548c9de560dSAlex Tomas 		MB_CHECK_ASSERT(buddy != buddy2);
549c9de560dSAlex Tomas 		MB_CHECK_ASSERT(max * 2 == max2);
550c9de560dSAlex Tomas 
551c9de560dSAlex Tomas 		count = 0;
552c9de560dSAlex Tomas 		for (i = 0; i < max; i++) {
553c9de560dSAlex Tomas 
554c9de560dSAlex Tomas 			if (mb_test_bit(i, buddy)) {
555c9de560dSAlex Tomas 				/* only single bit in buddy2 may be 1 */
556c9de560dSAlex Tomas 				if (!mb_test_bit(i << 1, buddy2)) {
557c9de560dSAlex Tomas 					MB_CHECK_ASSERT(
558c9de560dSAlex Tomas 						mb_test_bit((i<<1)+1, buddy2));
559c9de560dSAlex Tomas 				} else if (!mb_test_bit((i << 1) + 1, buddy2)) {
560c9de560dSAlex Tomas 					MB_CHECK_ASSERT(
561c9de560dSAlex Tomas 						mb_test_bit(i << 1, buddy2));
562c9de560dSAlex Tomas 				}
563c9de560dSAlex Tomas 				continue;
564c9de560dSAlex Tomas 			}
565c9de560dSAlex Tomas 
566c9de560dSAlex Tomas 			/* both bits in buddy2 must be 0 */
567c9de560dSAlex Tomas 			MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
568c9de560dSAlex Tomas 			MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
569c9de560dSAlex Tomas 
570c9de560dSAlex Tomas 			for (j = 0; j < (1 << order); j++) {
571c9de560dSAlex Tomas 				k = (i * (1 << order)) + j;
572c9de560dSAlex Tomas 				MB_CHECK_ASSERT(
573c9de560dSAlex Tomas 					!mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
574c9de560dSAlex Tomas 			}
575c9de560dSAlex Tomas 			count++;
576c9de560dSAlex Tomas 		}
577c9de560dSAlex Tomas 		MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
578c9de560dSAlex Tomas 		order--;
579c9de560dSAlex Tomas 	}
580c9de560dSAlex Tomas 
581c9de560dSAlex Tomas 	fstart = -1;
582c9de560dSAlex Tomas 	buddy = mb_find_buddy(e4b, 0, &max);
583c9de560dSAlex Tomas 	for (i = 0; i < max; i++) {
584c9de560dSAlex Tomas 		if (!mb_test_bit(i, buddy)) {
585c9de560dSAlex Tomas 			MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
586c9de560dSAlex Tomas 			if (fstart == -1) {
587c9de560dSAlex Tomas 				fragments++;
588c9de560dSAlex Tomas 				fstart = i;
589c9de560dSAlex Tomas 			}
590c9de560dSAlex Tomas 			continue;
591c9de560dSAlex Tomas 		}
592c9de560dSAlex Tomas 		fstart = -1;
593c9de560dSAlex Tomas 		/* check used bits only */
594c9de560dSAlex Tomas 		for (j = 0; j < e4b->bd_blkbits + 1; j++) {
595c9de560dSAlex Tomas 			buddy2 = mb_find_buddy(e4b, j, &max2);
596c9de560dSAlex Tomas 			k = i >> j;
597c9de560dSAlex Tomas 			MB_CHECK_ASSERT(k < max2);
598c9de560dSAlex Tomas 			MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
599c9de560dSAlex Tomas 		}
600c9de560dSAlex Tomas 	}
601c9de560dSAlex Tomas 	MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
602c9de560dSAlex Tomas 	MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
603c9de560dSAlex Tomas 
604c9de560dSAlex Tomas 	grp = ext4_get_group_info(sb, e4b->bd_group);
605c9de560dSAlex Tomas 	buddy = mb_find_buddy(e4b, 0, &max);
606c9de560dSAlex Tomas 	list_for_each(cur, &grp->bb_prealloc_list) {
607c9de560dSAlex Tomas 		ext4_group_t groupnr;
608c9de560dSAlex Tomas 		struct ext4_prealloc_space *pa;
60960bd63d1SSolofo Ramangalahy 		pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
61060bd63d1SSolofo Ramangalahy 		ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
611c9de560dSAlex Tomas 		MB_CHECK_ASSERT(groupnr == e4b->bd_group);
61260bd63d1SSolofo Ramangalahy 		for (i = 0; i < pa->pa_len; i++)
613c9de560dSAlex Tomas 			MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
614c9de560dSAlex Tomas 	}
615c9de560dSAlex Tomas 	return 0;
616c9de560dSAlex Tomas }
617c9de560dSAlex Tomas #undef MB_CHECK_ASSERT
618c9de560dSAlex Tomas #define mb_check_buddy(e4b) __mb_check_buddy(e4b,	\
61946e665e9SHarvey Harrison 					__FILE__, __func__, __LINE__)
620c9de560dSAlex Tomas #else
621c9de560dSAlex Tomas #define mb_check_buddy(e4b)
622c9de560dSAlex Tomas #endif
623c9de560dSAlex Tomas 
624c9de560dSAlex Tomas /* FIXME!! need more doc */
625c9de560dSAlex Tomas static void ext4_mb_mark_free_simple(struct super_block *sb,
626a36b4498SEric Sandeen 				void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
627c9de560dSAlex Tomas 					struct ext4_group_info *grp)
628c9de560dSAlex Tomas {
629c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
630a36b4498SEric Sandeen 	ext4_grpblk_t min;
631a36b4498SEric Sandeen 	ext4_grpblk_t max;
632a36b4498SEric Sandeen 	ext4_grpblk_t chunk;
633c9de560dSAlex Tomas 	unsigned short border;
634c9de560dSAlex Tomas 
635b73fce69SValerie Clement 	BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
636c9de560dSAlex Tomas 
637c9de560dSAlex Tomas 	border = 2 << sb->s_blocksize_bits;
638c9de560dSAlex Tomas 
639c9de560dSAlex Tomas 	while (len > 0) {
640c9de560dSAlex Tomas 		/* find how many blocks can be covered since this position */
641c9de560dSAlex Tomas 		max = ffs(first | border) - 1;
642c9de560dSAlex Tomas 
643c9de560dSAlex Tomas 		/* find how many blocks of power 2 we need to mark */
644c9de560dSAlex Tomas 		min = fls(len) - 1;
645c9de560dSAlex Tomas 
646c9de560dSAlex Tomas 		if (max < min)
647c9de560dSAlex Tomas 			min = max;
648c9de560dSAlex Tomas 		chunk = 1 << min;
649c9de560dSAlex Tomas 
650c9de560dSAlex Tomas 		/* mark multiblock chunks only */
651c9de560dSAlex Tomas 		grp->bb_counters[min]++;
652c9de560dSAlex Tomas 		if (min > 0)
653c9de560dSAlex Tomas 			mb_clear_bit(first >> min,
654c9de560dSAlex Tomas 				     buddy + sbi->s_mb_offsets[min]);
655c9de560dSAlex Tomas 
656c9de560dSAlex Tomas 		len -= chunk;
657c9de560dSAlex Tomas 		first += chunk;
658c9de560dSAlex Tomas 	}
659c9de560dSAlex Tomas }
660c9de560dSAlex Tomas 
661089ceeccSEric Sandeen static noinline_for_stack
662089ceeccSEric Sandeen void ext4_mb_generate_buddy(struct super_block *sb,
663c9de560dSAlex Tomas 				void *buddy, void *bitmap, ext4_group_t group)
664c9de560dSAlex Tomas {
665c9de560dSAlex Tomas 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
666a36b4498SEric Sandeen 	ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
667a36b4498SEric Sandeen 	ext4_grpblk_t i = 0;
668a36b4498SEric Sandeen 	ext4_grpblk_t first;
669a36b4498SEric Sandeen 	ext4_grpblk_t len;
670c9de560dSAlex Tomas 	unsigned free = 0;
671c9de560dSAlex Tomas 	unsigned fragments = 0;
672c9de560dSAlex Tomas 	unsigned long long period = get_cycles();
673c9de560dSAlex Tomas 
674c9de560dSAlex Tomas 	/* initialize buddy from bitmap which is aggregation
675c9de560dSAlex Tomas 	 * of on-disk bitmap and preallocations */
676ffad0a44SAneesh Kumar K.V 	i = mb_find_next_zero_bit(bitmap, max, 0);
677c9de560dSAlex Tomas 	grp->bb_first_free = i;
678c9de560dSAlex Tomas 	while (i < max) {
679c9de560dSAlex Tomas 		fragments++;
680c9de560dSAlex Tomas 		first = i;
681ffad0a44SAneesh Kumar K.V 		i = mb_find_next_bit(bitmap, max, i);
682c9de560dSAlex Tomas 		len = i - first;
683c9de560dSAlex Tomas 		free += len;
684c9de560dSAlex Tomas 		if (len > 1)
685c9de560dSAlex Tomas 			ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
686c9de560dSAlex Tomas 		else
687c9de560dSAlex Tomas 			grp->bb_counters[0]++;
688c9de560dSAlex Tomas 		if (i < max)
689ffad0a44SAneesh Kumar K.V 			i = mb_find_next_zero_bit(bitmap, max, i);
690c9de560dSAlex Tomas 	}
691c9de560dSAlex Tomas 	grp->bb_fragments = fragments;
692c9de560dSAlex Tomas 
693c9de560dSAlex Tomas 	if (free != grp->bb_free) {
6945d1b1b3fSAneesh Kumar K.V 		ext4_grp_locked_error(sb, group,  __func__,
695a9df9a49STheodore Ts'o 			"EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
696c9de560dSAlex Tomas 			group, free, grp->bb_free);
697e56eb659SAneesh Kumar K.V 		/*
698e56eb659SAneesh Kumar K.V 		 * If we intent to continue, we consider group descritor
699e56eb659SAneesh Kumar K.V 		 * corrupt and update bb_free using bitmap value
700e56eb659SAneesh Kumar K.V 		 */
701c9de560dSAlex Tomas 		grp->bb_free = free;
702c9de560dSAlex Tomas 	}
703c9de560dSAlex Tomas 
704c9de560dSAlex Tomas 	clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
705c9de560dSAlex Tomas 
706c9de560dSAlex Tomas 	period = get_cycles() - period;
707c9de560dSAlex Tomas 	spin_lock(&EXT4_SB(sb)->s_bal_lock);
708c9de560dSAlex Tomas 	EXT4_SB(sb)->s_mb_buddies_generated++;
709c9de560dSAlex Tomas 	EXT4_SB(sb)->s_mb_generation_time += period;
710c9de560dSAlex Tomas 	spin_unlock(&EXT4_SB(sb)->s_bal_lock);
711c9de560dSAlex Tomas }
712c9de560dSAlex Tomas 
713c9de560dSAlex Tomas /* The buddy information is attached the buddy cache inode
714c9de560dSAlex Tomas  * for convenience. The information regarding each group
715c9de560dSAlex Tomas  * is loaded via ext4_mb_load_buddy. The information involve
716c9de560dSAlex Tomas  * block bitmap and buddy information. The information are
717c9de560dSAlex Tomas  * stored in the inode as
718c9de560dSAlex Tomas  *
719c9de560dSAlex Tomas  * {                        page                        }
720c3a326a6SAneesh Kumar K.V  * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
721c9de560dSAlex Tomas  *
722c9de560dSAlex Tomas  *
723c9de560dSAlex Tomas  * one block each for bitmap and buddy information.
724c9de560dSAlex Tomas  * So for each group we take up 2 blocks. A page can
725c9de560dSAlex Tomas  * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize)  blocks.
726c9de560dSAlex Tomas  * So it can have information regarding groups_per_page which
727c9de560dSAlex Tomas  * is blocks_per_page/2
728c9de560dSAlex Tomas  */
729c9de560dSAlex Tomas 
730c9de560dSAlex Tomas static int ext4_mb_init_cache(struct page *page, char *incore)
731c9de560dSAlex Tomas {
7328df9675fSTheodore Ts'o 	ext4_group_t ngroups;
733c9de560dSAlex Tomas 	int blocksize;
734c9de560dSAlex Tomas 	int blocks_per_page;
735c9de560dSAlex Tomas 	int groups_per_page;
736c9de560dSAlex Tomas 	int err = 0;
737c9de560dSAlex Tomas 	int i;
738c9de560dSAlex Tomas 	ext4_group_t first_group;
739c9de560dSAlex Tomas 	int first_block;
740c9de560dSAlex Tomas 	struct super_block *sb;
741c9de560dSAlex Tomas 	struct buffer_head *bhs;
742c9de560dSAlex Tomas 	struct buffer_head **bh;
743c9de560dSAlex Tomas 	struct inode *inode;
744c9de560dSAlex Tomas 	char *data;
745c9de560dSAlex Tomas 	char *bitmap;
746c9de560dSAlex Tomas 
7476ba495e9STheodore Ts'o 	mb_debug(1, "init page %lu\n", page->index);
748c9de560dSAlex Tomas 
749c9de560dSAlex Tomas 	inode = page->mapping->host;
750c9de560dSAlex Tomas 	sb = inode->i_sb;
7518df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
752c9de560dSAlex Tomas 	blocksize = 1 << inode->i_blkbits;
753c9de560dSAlex Tomas 	blocks_per_page = PAGE_CACHE_SIZE / blocksize;
754c9de560dSAlex Tomas 
755c9de560dSAlex Tomas 	groups_per_page = blocks_per_page >> 1;
756c9de560dSAlex Tomas 	if (groups_per_page == 0)
757c9de560dSAlex Tomas 		groups_per_page = 1;
758c9de560dSAlex Tomas 
759c9de560dSAlex Tomas 	/* allocate buffer_heads to read bitmaps */
760c9de560dSAlex Tomas 	if (groups_per_page > 1) {
761c9de560dSAlex Tomas 		err = -ENOMEM;
762c9de560dSAlex Tomas 		i = sizeof(struct buffer_head *) * groups_per_page;
763c9de560dSAlex Tomas 		bh = kzalloc(i, GFP_NOFS);
764c9de560dSAlex Tomas 		if (bh == NULL)
765c9de560dSAlex Tomas 			goto out;
766c9de560dSAlex Tomas 	} else
767c9de560dSAlex Tomas 		bh = &bhs;
768c9de560dSAlex Tomas 
769c9de560dSAlex Tomas 	first_group = page->index * blocks_per_page / 2;
770c9de560dSAlex Tomas 
771c9de560dSAlex Tomas 	/* read all groups the page covers into the cache */
772c9de560dSAlex Tomas 	for (i = 0; i < groups_per_page; i++) {
773c9de560dSAlex Tomas 		struct ext4_group_desc *desc;
774c9de560dSAlex Tomas 
7758df9675fSTheodore Ts'o 		if (first_group + i >= ngroups)
776c9de560dSAlex Tomas 			break;
777c9de560dSAlex Tomas 
778c9de560dSAlex Tomas 		err = -EIO;
779c9de560dSAlex Tomas 		desc = ext4_get_group_desc(sb, first_group + i, NULL);
780c9de560dSAlex Tomas 		if (desc == NULL)
781c9de560dSAlex Tomas 			goto out;
782c9de560dSAlex Tomas 
783c9de560dSAlex Tomas 		err = -ENOMEM;
784c9de560dSAlex Tomas 		bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
785c9de560dSAlex Tomas 		if (bh[i] == NULL)
786c9de560dSAlex Tomas 			goto out;
787c9de560dSAlex Tomas 
7882ccb5fb9SAneesh Kumar K.V 		if (bitmap_uptodate(bh[i]))
789c9de560dSAlex Tomas 			continue;
790c9de560dSAlex Tomas 
791c806e68fSFrederic Bohe 		lock_buffer(bh[i]);
7922ccb5fb9SAneesh Kumar K.V 		if (bitmap_uptodate(bh[i])) {
7932ccb5fb9SAneesh Kumar K.V 			unlock_buffer(bh[i]);
7942ccb5fb9SAneesh Kumar K.V 			continue;
7952ccb5fb9SAneesh Kumar K.V 		}
796955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, first_group + i);
797c9de560dSAlex Tomas 		if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
798c9de560dSAlex Tomas 			ext4_init_block_bitmap(sb, bh[i],
799c9de560dSAlex Tomas 						first_group + i, desc);
8002ccb5fb9SAneesh Kumar K.V 			set_bitmap_uptodate(bh[i]);
801c9de560dSAlex Tomas 			set_buffer_uptodate(bh[i]);
802955ce5f5SAneesh Kumar K.V 			ext4_unlock_group(sb, first_group + i);
8033300bedaSAneesh Kumar K.V 			unlock_buffer(bh[i]);
804c9de560dSAlex Tomas 			continue;
805c9de560dSAlex Tomas 		}
806955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, first_group + i);
8072ccb5fb9SAneesh Kumar K.V 		if (buffer_uptodate(bh[i])) {
8082ccb5fb9SAneesh Kumar K.V 			/*
8092ccb5fb9SAneesh Kumar K.V 			 * if not uninit if bh is uptodate,
8102ccb5fb9SAneesh Kumar K.V 			 * bitmap is also uptodate
8112ccb5fb9SAneesh Kumar K.V 			 */
8122ccb5fb9SAneesh Kumar K.V 			set_bitmap_uptodate(bh[i]);
8132ccb5fb9SAneesh Kumar K.V 			unlock_buffer(bh[i]);
8142ccb5fb9SAneesh Kumar K.V 			continue;
8152ccb5fb9SAneesh Kumar K.V 		}
816c9de560dSAlex Tomas 		get_bh(bh[i]);
8172ccb5fb9SAneesh Kumar K.V 		/*
8182ccb5fb9SAneesh Kumar K.V 		 * submit the buffer_head for read. We can
8192ccb5fb9SAneesh Kumar K.V 		 * safely mark the bitmap as uptodate now.
8202ccb5fb9SAneesh Kumar K.V 		 * We do it here so the bitmap uptodate bit
8212ccb5fb9SAneesh Kumar K.V 		 * get set with buffer lock held.
8222ccb5fb9SAneesh Kumar K.V 		 */
8232ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh[i]);
824c9de560dSAlex Tomas 		bh[i]->b_end_io = end_buffer_read_sync;
825c9de560dSAlex Tomas 		submit_bh(READ, bh[i]);
8266ba495e9STheodore Ts'o 		mb_debug(1, "read bitmap for group %u\n", first_group + i);
827c9de560dSAlex Tomas 	}
828c9de560dSAlex Tomas 
829c9de560dSAlex Tomas 	/* wait for I/O completion */
830c9de560dSAlex Tomas 	for (i = 0; i < groups_per_page && bh[i]; i++)
831c9de560dSAlex Tomas 		wait_on_buffer(bh[i]);
832c9de560dSAlex Tomas 
833c9de560dSAlex Tomas 	err = -EIO;
834c9de560dSAlex Tomas 	for (i = 0; i < groups_per_page && bh[i]; i++)
835c9de560dSAlex Tomas 		if (!buffer_uptodate(bh[i]))
836c9de560dSAlex Tomas 			goto out;
837c9de560dSAlex Tomas 
83831b481dcSMingming Cao 	err = 0;
839c9de560dSAlex Tomas 	first_block = page->index * blocks_per_page;
84029eaf024SAneesh Kumar K.V 	/* init the page  */
84129eaf024SAneesh Kumar K.V 	memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
842c9de560dSAlex Tomas 	for (i = 0; i < blocks_per_page; i++) {
843c9de560dSAlex Tomas 		int group;
844c9de560dSAlex Tomas 		struct ext4_group_info *grinfo;
845c9de560dSAlex Tomas 
846c9de560dSAlex Tomas 		group = (first_block + i) >> 1;
8478df9675fSTheodore Ts'o 		if (group >= ngroups)
848c9de560dSAlex Tomas 			break;
849c9de560dSAlex Tomas 
850c9de560dSAlex Tomas 		/*
851c9de560dSAlex Tomas 		 * data carry information regarding this
852c9de560dSAlex Tomas 		 * particular group in the format specified
853c9de560dSAlex Tomas 		 * above
854c9de560dSAlex Tomas 		 *
855c9de560dSAlex Tomas 		 */
856c9de560dSAlex Tomas 		data = page_address(page) + (i * blocksize);
857c9de560dSAlex Tomas 		bitmap = bh[group - first_group]->b_data;
858c9de560dSAlex Tomas 
859c9de560dSAlex Tomas 		/*
860c9de560dSAlex Tomas 		 * We place the buddy block and bitmap block
861c9de560dSAlex Tomas 		 * close together
862c9de560dSAlex Tomas 		 */
863c9de560dSAlex Tomas 		if ((first_block + i) & 1) {
864c9de560dSAlex Tomas 			/* this is block of buddy */
865c9de560dSAlex Tomas 			BUG_ON(incore == NULL);
8666ba495e9STheodore Ts'o 			mb_debug(1, "put buddy for group %u in page %lu/%x\n",
867c9de560dSAlex Tomas 				group, page->index, i * blocksize);
868c9de560dSAlex Tomas 			grinfo = ext4_get_group_info(sb, group);
869c9de560dSAlex Tomas 			grinfo->bb_fragments = 0;
870c9de560dSAlex Tomas 			memset(grinfo->bb_counters, 0,
8711927805eSEric Sandeen 			       sizeof(*grinfo->bb_counters) *
8721927805eSEric Sandeen 				(sb->s_blocksize_bits+2));
873c9de560dSAlex Tomas 			/*
874c9de560dSAlex Tomas 			 * incore got set to the group block bitmap below
875c9de560dSAlex Tomas 			 */
8767a2fcbf7SAneesh Kumar K.V 			ext4_lock_group(sb, group);
877c9de560dSAlex Tomas 			ext4_mb_generate_buddy(sb, data, incore, group);
8787a2fcbf7SAneesh Kumar K.V 			ext4_unlock_group(sb, group);
879c9de560dSAlex Tomas 			incore = NULL;
880c9de560dSAlex Tomas 		} else {
881c9de560dSAlex Tomas 			/* this is block of bitmap */
882c9de560dSAlex Tomas 			BUG_ON(incore != NULL);
8836ba495e9STheodore Ts'o 			mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
884c9de560dSAlex Tomas 				group, page->index, i * blocksize);
885c9de560dSAlex Tomas 
886c9de560dSAlex Tomas 			/* see comments in ext4_mb_put_pa() */
887c9de560dSAlex Tomas 			ext4_lock_group(sb, group);
888c9de560dSAlex Tomas 			memcpy(data, bitmap, blocksize);
889c9de560dSAlex Tomas 
890c9de560dSAlex Tomas 			/* mark all preallocated blks used in in-core bitmap */
891c9de560dSAlex Tomas 			ext4_mb_generate_from_pa(sb, data, group);
8927a2fcbf7SAneesh Kumar K.V 			ext4_mb_generate_from_freelist(sb, data, group);
893c9de560dSAlex Tomas 			ext4_unlock_group(sb, group);
894c9de560dSAlex Tomas 
895c9de560dSAlex Tomas 			/* set incore so that the buddy information can be
896c9de560dSAlex Tomas 			 * generated using this
897c9de560dSAlex Tomas 			 */
898c9de560dSAlex Tomas 			incore = data;
899c9de560dSAlex Tomas 		}
900c9de560dSAlex Tomas 	}
901c9de560dSAlex Tomas 	SetPageUptodate(page);
902c9de560dSAlex Tomas 
903c9de560dSAlex Tomas out:
904c9de560dSAlex Tomas 	if (bh) {
905c9de560dSAlex Tomas 		for (i = 0; i < groups_per_page && bh[i]; i++)
906c9de560dSAlex Tomas 			brelse(bh[i]);
907c9de560dSAlex Tomas 		if (bh != &bhs)
908c9de560dSAlex Tomas 			kfree(bh);
909c9de560dSAlex Tomas 	}
910c9de560dSAlex Tomas 	return err;
911c9de560dSAlex Tomas }
912c9de560dSAlex Tomas 
913b6a758ecSAneesh Kumar K.V static noinline_for_stack
914b6a758ecSAneesh Kumar K.V int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
915b6a758ecSAneesh Kumar K.V {
916b6a758ecSAneesh Kumar K.V 
917b6a758ecSAneesh Kumar K.V 	int ret = 0;
918b6a758ecSAneesh Kumar K.V 	void *bitmap;
919b6a758ecSAneesh Kumar K.V 	int blocks_per_page;
920b6a758ecSAneesh Kumar K.V 	int block, pnum, poff;
921b6a758ecSAneesh Kumar K.V 	int num_grp_locked = 0;
922b6a758ecSAneesh Kumar K.V 	struct ext4_group_info *this_grp;
923b6a758ecSAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(sb);
924b6a758ecSAneesh Kumar K.V 	struct inode *inode = sbi->s_buddy_cache;
925b6a758ecSAneesh Kumar K.V 	struct page *page = NULL, *bitmap_page = NULL;
926b6a758ecSAneesh Kumar K.V 
927b6a758ecSAneesh Kumar K.V 	mb_debug(1, "init group %u\n", group);
928b6a758ecSAneesh Kumar K.V 	blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
929b6a758ecSAneesh Kumar K.V 	this_grp = ext4_get_group_info(sb, group);
930b6a758ecSAneesh Kumar K.V 	/*
93108c3a813SAneesh Kumar K.V 	 * This ensures that we don't reinit the buddy cache
93208c3a813SAneesh Kumar K.V 	 * page which map to the group from which we are already
93308c3a813SAneesh Kumar K.V 	 * allocating. If we are looking at the buddy cache we would
93408c3a813SAneesh Kumar K.V 	 * have taken a reference using ext4_mb_load_buddy and that
93508c3a813SAneesh Kumar K.V 	 * would have taken the alloc_sem lock.
936b6a758ecSAneesh Kumar K.V 	 */
937b6a758ecSAneesh Kumar K.V 	num_grp_locked =  ext4_mb_get_buddy_cache_lock(sb, group);
938b6a758ecSAneesh Kumar K.V 	if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
939b6a758ecSAneesh Kumar K.V 		/*
940b6a758ecSAneesh Kumar K.V 		 * somebody initialized the group
941b6a758ecSAneesh Kumar K.V 		 * return without doing anything
942b6a758ecSAneesh Kumar K.V 		 */
943b6a758ecSAneesh Kumar K.V 		ret = 0;
944b6a758ecSAneesh Kumar K.V 		goto err;
945b6a758ecSAneesh Kumar K.V 	}
946b6a758ecSAneesh Kumar K.V 	/*
947b6a758ecSAneesh Kumar K.V 	 * the buddy cache inode stores the block bitmap
948b6a758ecSAneesh Kumar K.V 	 * and buddy information in consecutive blocks.
949b6a758ecSAneesh Kumar K.V 	 * So for each group we need two blocks.
950b6a758ecSAneesh Kumar K.V 	 */
951b6a758ecSAneesh Kumar K.V 	block = group * 2;
952b6a758ecSAneesh Kumar K.V 	pnum = block / blocks_per_page;
953b6a758ecSAneesh Kumar K.V 	poff = block % blocks_per_page;
954b6a758ecSAneesh Kumar K.V 	page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
955b6a758ecSAneesh Kumar K.V 	if (page) {
956b6a758ecSAneesh Kumar K.V 		BUG_ON(page->mapping != inode->i_mapping);
957b6a758ecSAneesh Kumar K.V 		ret = ext4_mb_init_cache(page, NULL);
958b6a758ecSAneesh Kumar K.V 		if (ret) {
959b6a758ecSAneesh Kumar K.V 			unlock_page(page);
960b6a758ecSAneesh Kumar K.V 			goto err;
961b6a758ecSAneesh Kumar K.V 		}
962b6a758ecSAneesh Kumar K.V 		unlock_page(page);
963b6a758ecSAneesh Kumar K.V 	}
964b6a758ecSAneesh Kumar K.V 	if (page == NULL || !PageUptodate(page)) {
965b6a758ecSAneesh Kumar K.V 		ret = -EIO;
966b6a758ecSAneesh Kumar K.V 		goto err;
967b6a758ecSAneesh Kumar K.V 	}
968b6a758ecSAneesh Kumar K.V 	mark_page_accessed(page);
969b6a758ecSAneesh Kumar K.V 	bitmap_page = page;
970b6a758ecSAneesh Kumar K.V 	bitmap = page_address(page) + (poff * sb->s_blocksize);
971b6a758ecSAneesh Kumar K.V 
972b6a758ecSAneesh Kumar K.V 	/* init buddy cache */
973b6a758ecSAneesh Kumar K.V 	block++;
974b6a758ecSAneesh Kumar K.V 	pnum = block / blocks_per_page;
975b6a758ecSAneesh Kumar K.V 	poff = block % blocks_per_page;
976b6a758ecSAneesh Kumar K.V 	page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
977b6a758ecSAneesh Kumar K.V 	if (page == bitmap_page) {
978b6a758ecSAneesh Kumar K.V 		/*
979b6a758ecSAneesh Kumar K.V 		 * If both the bitmap and buddy are in
980b6a758ecSAneesh Kumar K.V 		 * the same page we don't need to force
981b6a758ecSAneesh Kumar K.V 		 * init the buddy
982b6a758ecSAneesh Kumar K.V 		 */
983b6a758ecSAneesh Kumar K.V 		unlock_page(page);
984b6a758ecSAneesh Kumar K.V 	} else if (page) {
985b6a758ecSAneesh Kumar K.V 		BUG_ON(page->mapping != inode->i_mapping);
986b6a758ecSAneesh Kumar K.V 		ret = ext4_mb_init_cache(page, bitmap);
987b6a758ecSAneesh Kumar K.V 		if (ret) {
988b6a758ecSAneesh Kumar K.V 			unlock_page(page);
989b6a758ecSAneesh Kumar K.V 			goto err;
990b6a758ecSAneesh Kumar K.V 		}
991b6a758ecSAneesh Kumar K.V 		unlock_page(page);
992b6a758ecSAneesh Kumar K.V 	}
993b6a758ecSAneesh Kumar K.V 	if (page == NULL || !PageUptodate(page)) {
994b6a758ecSAneesh Kumar K.V 		ret = -EIO;
995b6a758ecSAneesh Kumar K.V 		goto err;
996b6a758ecSAneesh Kumar K.V 	}
997b6a758ecSAneesh Kumar K.V 	mark_page_accessed(page);
998b6a758ecSAneesh Kumar K.V err:
999b6a758ecSAneesh Kumar K.V 	ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1000b6a758ecSAneesh Kumar K.V 	if (bitmap_page)
1001b6a758ecSAneesh Kumar K.V 		page_cache_release(bitmap_page);
1002b6a758ecSAneesh Kumar K.V 	if (page)
1003b6a758ecSAneesh Kumar K.V 		page_cache_release(page);
1004b6a758ecSAneesh Kumar K.V 	return ret;
1005b6a758ecSAneesh Kumar K.V }
1006b6a758ecSAneesh Kumar K.V 
10074ddfef7bSEric Sandeen static noinline_for_stack int
10084ddfef7bSEric Sandeen ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1009c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1010c9de560dSAlex Tomas {
1011c9de560dSAlex Tomas 	int blocks_per_page;
1012c9de560dSAlex Tomas 	int block;
1013c9de560dSAlex Tomas 	int pnum;
1014c9de560dSAlex Tomas 	int poff;
1015c9de560dSAlex Tomas 	struct page *page;
1016fdf6c7a7SShen Feng 	int ret;
1017920313a7SAneesh Kumar K.V 	struct ext4_group_info *grp;
1018920313a7SAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1019920313a7SAneesh Kumar K.V 	struct inode *inode = sbi->s_buddy_cache;
1020c9de560dSAlex Tomas 
10216ba495e9STheodore Ts'o 	mb_debug(1, "load group %u\n", group);
1022c9de560dSAlex Tomas 
1023c9de560dSAlex Tomas 	blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1024920313a7SAneesh Kumar K.V 	grp = ext4_get_group_info(sb, group);
1025c9de560dSAlex Tomas 
1026c9de560dSAlex Tomas 	e4b->bd_blkbits = sb->s_blocksize_bits;
1027c9de560dSAlex Tomas 	e4b->bd_info = ext4_get_group_info(sb, group);
1028c9de560dSAlex Tomas 	e4b->bd_sb = sb;
1029c9de560dSAlex Tomas 	e4b->bd_group = group;
1030c9de560dSAlex Tomas 	e4b->bd_buddy_page = NULL;
1031c9de560dSAlex Tomas 	e4b->bd_bitmap_page = NULL;
1032920313a7SAneesh Kumar K.V 	e4b->alloc_semp = &grp->alloc_sem;
1033920313a7SAneesh Kumar K.V 
1034920313a7SAneesh Kumar K.V 	/* Take the read lock on the group alloc
1035920313a7SAneesh Kumar K.V 	 * sem. This would make sure a parallel
1036920313a7SAneesh Kumar K.V 	 * ext4_mb_init_group happening on other
1037920313a7SAneesh Kumar K.V 	 * groups mapped by the page is blocked
1038920313a7SAneesh Kumar K.V 	 * till we are done with allocation
1039920313a7SAneesh Kumar K.V 	 */
1040f41c0750SAneesh Kumar K.V repeat_load_buddy:
1041920313a7SAneesh Kumar K.V 	down_read(e4b->alloc_semp);
1042c9de560dSAlex Tomas 
1043f41c0750SAneesh Kumar K.V 	if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1044f41c0750SAneesh Kumar K.V 		/* we need to check for group need init flag
1045f41c0750SAneesh Kumar K.V 		 * with alloc_semp held so that we can be sure
1046f41c0750SAneesh Kumar K.V 		 * that new blocks didn't get added to the group
1047f41c0750SAneesh Kumar K.V 		 * when we are loading the buddy cache
1048f41c0750SAneesh Kumar K.V 		 */
1049f41c0750SAneesh Kumar K.V 		up_read(e4b->alloc_semp);
1050f41c0750SAneesh Kumar K.V 		/*
1051f41c0750SAneesh Kumar K.V 		 * we need full data about the group
1052f41c0750SAneesh Kumar K.V 		 * to make a good selection
1053f41c0750SAneesh Kumar K.V 		 */
1054f41c0750SAneesh Kumar K.V 		ret = ext4_mb_init_group(sb, group);
1055f41c0750SAneesh Kumar K.V 		if (ret)
1056f41c0750SAneesh Kumar K.V 			return ret;
1057f41c0750SAneesh Kumar K.V 		goto repeat_load_buddy;
1058f41c0750SAneesh Kumar K.V 	}
1059f41c0750SAneesh Kumar K.V 
1060c9de560dSAlex Tomas 	/*
1061c9de560dSAlex Tomas 	 * the buddy cache inode stores the block bitmap
1062c9de560dSAlex Tomas 	 * and buddy information in consecutive blocks.
1063c9de560dSAlex Tomas 	 * So for each group we need two blocks.
1064c9de560dSAlex Tomas 	 */
1065c9de560dSAlex Tomas 	block = group * 2;
1066c9de560dSAlex Tomas 	pnum = block / blocks_per_page;
1067c9de560dSAlex Tomas 	poff = block % blocks_per_page;
1068c9de560dSAlex Tomas 
1069c9de560dSAlex Tomas 	/* we could use find_or_create_page(), but it locks page
1070c9de560dSAlex Tomas 	 * what we'd like to avoid in fast path ... */
1071c9de560dSAlex Tomas 	page = find_get_page(inode->i_mapping, pnum);
1072c9de560dSAlex Tomas 	if (page == NULL || !PageUptodate(page)) {
1073c9de560dSAlex Tomas 		if (page)
1074920313a7SAneesh Kumar K.V 			/*
1075920313a7SAneesh Kumar K.V 			 * drop the page reference and try
1076920313a7SAneesh Kumar K.V 			 * to get the page with lock. If we
1077920313a7SAneesh Kumar K.V 			 * are not uptodate that implies
1078920313a7SAneesh Kumar K.V 			 * somebody just created the page but
1079920313a7SAneesh Kumar K.V 			 * is yet to initialize the same. So
1080920313a7SAneesh Kumar K.V 			 * wait for it to initialize.
1081920313a7SAneesh Kumar K.V 			 */
1082c9de560dSAlex Tomas 			page_cache_release(page);
1083c9de560dSAlex Tomas 		page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1084c9de560dSAlex Tomas 		if (page) {
1085c9de560dSAlex Tomas 			BUG_ON(page->mapping != inode->i_mapping);
1086c9de560dSAlex Tomas 			if (!PageUptodate(page)) {
1087fdf6c7a7SShen Feng 				ret = ext4_mb_init_cache(page, NULL);
1088fdf6c7a7SShen Feng 				if (ret) {
1089fdf6c7a7SShen Feng 					unlock_page(page);
1090fdf6c7a7SShen Feng 					goto err;
1091fdf6c7a7SShen Feng 				}
1092c9de560dSAlex Tomas 				mb_cmp_bitmaps(e4b, page_address(page) +
1093c9de560dSAlex Tomas 					       (poff * sb->s_blocksize));
1094c9de560dSAlex Tomas 			}
1095c9de560dSAlex Tomas 			unlock_page(page);
1096c9de560dSAlex Tomas 		}
1097c9de560dSAlex Tomas 	}
1098fdf6c7a7SShen Feng 	if (page == NULL || !PageUptodate(page)) {
1099fdf6c7a7SShen Feng 		ret = -EIO;
1100c9de560dSAlex Tomas 		goto err;
1101fdf6c7a7SShen Feng 	}
1102c9de560dSAlex Tomas 	e4b->bd_bitmap_page = page;
1103c9de560dSAlex Tomas 	e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1104c9de560dSAlex Tomas 	mark_page_accessed(page);
1105c9de560dSAlex Tomas 
1106c9de560dSAlex Tomas 	block++;
1107c9de560dSAlex Tomas 	pnum = block / blocks_per_page;
1108c9de560dSAlex Tomas 	poff = block % blocks_per_page;
1109c9de560dSAlex Tomas 
1110c9de560dSAlex Tomas 	page = find_get_page(inode->i_mapping, pnum);
1111c9de560dSAlex Tomas 	if (page == NULL || !PageUptodate(page)) {
1112c9de560dSAlex Tomas 		if (page)
1113c9de560dSAlex Tomas 			page_cache_release(page);
1114c9de560dSAlex Tomas 		page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1115c9de560dSAlex Tomas 		if (page) {
1116c9de560dSAlex Tomas 			BUG_ON(page->mapping != inode->i_mapping);
1117fdf6c7a7SShen Feng 			if (!PageUptodate(page)) {
1118fdf6c7a7SShen Feng 				ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1119fdf6c7a7SShen Feng 				if (ret) {
1120fdf6c7a7SShen Feng 					unlock_page(page);
1121fdf6c7a7SShen Feng 					goto err;
1122fdf6c7a7SShen Feng 				}
1123fdf6c7a7SShen Feng 			}
1124c9de560dSAlex Tomas 			unlock_page(page);
1125c9de560dSAlex Tomas 		}
1126c9de560dSAlex Tomas 	}
1127fdf6c7a7SShen Feng 	if (page == NULL || !PageUptodate(page)) {
1128fdf6c7a7SShen Feng 		ret = -EIO;
1129c9de560dSAlex Tomas 		goto err;
1130fdf6c7a7SShen Feng 	}
1131c9de560dSAlex Tomas 	e4b->bd_buddy_page = page;
1132c9de560dSAlex Tomas 	e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1133c9de560dSAlex Tomas 	mark_page_accessed(page);
1134c9de560dSAlex Tomas 
1135c9de560dSAlex Tomas 	BUG_ON(e4b->bd_bitmap_page == NULL);
1136c9de560dSAlex Tomas 	BUG_ON(e4b->bd_buddy_page == NULL);
1137c9de560dSAlex Tomas 
1138c9de560dSAlex Tomas 	return 0;
1139c9de560dSAlex Tomas 
1140c9de560dSAlex Tomas err:
1141c9de560dSAlex Tomas 	if (e4b->bd_bitmap_page)
1142c9de560dSAlex Tomas 		page_cache_release(e4b->bd_bitmap_page);
1143c9de560dSAlex Tomas 	if (e4b->bd_buddy_page)
1144c9de560dSAlex Tomas 		page_cache_release(e4b->bd_buddy_page);
1145c9de560dSAlex Tomas 	e4b->bd_buddy = NULL;
1146c9de560dSAlex Tomas 	e4b->bd_bitmap = NULL;
1147920313a7SAneesh Kumar K.V 
1148920313a7SAneesh Kumar K.V 	/* Done with the buddy cache */
1149920313a7SAneesh Kumar K.V 	up_read(e4b->alloc_semp);
1150fdf6c7a7SShen Feng 	return ret;
1151c9de560dSAlex Tomas }
1152c9de560dSAlex Tomas 
1153c9de560dSAlex Tomas static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1154c9de560dSAlex Tomas {
1155c9de560dSAlex Tomas 	if (e4b->bd_bitmap_page)
1156c9de560dSAlex Tomas 		page_cache_release(e4b->bd_bitmap_page);
1157c9de560dSAlex Tomas 	if (e4b->bd_buddy_page)
1158c9de560dSAlex Tomas 		page_cache_release(e4b->bd_buddy_page);
1159920313a7SAneesh Kumar K.V 	/* Done with the buddy cache */
11608556e8f3SAneesh Kumar K.V 	if (e4b->alloc_semp)
1161920313a7SAneesh Kumar K.V 		up_read(e4b->alloc_semp);
1162c9de560dSAlex Tomas }
1163c9de560dSAlex Tomas 
1164c9de560dSAlex Tomas 
1165c9de560dSAlex Tomas static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1166c9de560dSAlex Tomas {
1167c9de560dSAlex Tomas 	int order = 1;
1168c9de560dSAlex Tomas 	void *bb;
1169c9de560dSAlex Tomas 
1170c9de560dSAlex Tomas 	BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1171c9de560dSAlex Tomas 	BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1172c9de560dSAlex Tomas 
1173c9de560dSAlex Tomas 	bb = EXT4_MB_BUDDY(e4b);
1174c9de560dSAlex Tomas 	while (order <= e4b->bd_blkbits + 1) {
1175c9de560dSAlex Tomas 		block = block >> 1;
1176c9de560dSAlex Tomas 		if (!mb_test_bit(block, bb)) {
1177c9de560dSAlex Tomas 			/* this block is part of buddy of order 'order' */
1178c9de560dSAlex Tomas 			return order;
1179c9de560dSAlex Tomas 		}
1180c9de560dSAlex Tomas 		bb += 1 << (e4b->bd_blkbits - order);
1181c9de560dSAlex Tomas 		order++;
1182c9de560dSAlex Tomas 	}
1183c9de560dSAlex Tomas 	return 0;
1184c9de560dSAlex Tomas }
1185c9de560dSAlex Tomas 
1186955ce5f5SAneesh Kumar K.V static void mb_clear_bits(void *bm, int cur, int len)
1187c9de560dSAlex Tomas {
1188c9de560dSAlex Tomas 	__u32 *addr;
1189c9de560dSAlex Tomas 
1190c9de560dSAlex Tomas 	len = cur + len;
1191c9de560dSAlex Tomas 	while (cur < len) {
1192c9de560dSAlex Tomas 		if ((cur & 31) == 0 && (len - cur) >= 32) {
1193c9de560dSAlex Tomas 			/* fast path: clear whole word at once */
1194c9de560dSAlex Tomas 			addr = bm + (cur >> 3);
1195c9de560dSAlex Tomas 			*addr = 0;
1196c9de560dSAlex Tomas 			cur += 32;
1197c9de560dSAlex Tomas 			continue;
1198c9de560dSAlex Tomas 		}
1199e8134b27SAneesh Kumar K.V 		mb_clear_bit(cur, bm);
1200c9de560dSAlex Tomas 		cur++;
1201c9de560dSAlex Tomas 	}
1202c9de560dSAlex Tomas }
1203c9de560dSAlex Tomas 
1204955ce5f5SAneesh Kumar K.V static void mb_set_bits(void *bm, int cur, int len)
1205c9de560dSAlex Tomas {
1206c9de560dSAlex Tomas 	__u32 *addr;
1207c9de560dSAlex Tomas 
1208c9de560dSAlex Tomas 	len = cur + len;
1209c9de560dSAlex Tomas 	while (cur < len) {
1210c9de560dSAlex Tomas 		if ((cur & 31) == 0 && (len - cur) >= 32) {
1211c9de560dSAlex Tomas 			/* fast path: set whole word at once */
1212c9de560dSAlex Tomas 			addr = bm + (cur >> 3);
1213c9de560dSAlex Tomas 			*addr = 0xffffffff;
1214c9de560dSAlex Tomas 			cur += 32;
1215c9de560dSAlex Tomas 			continue;
1216c9de560dSAlex Tomas 		}
1217e8134b27SAneesh Kumar K.V 		mb_set_bit(cur, bm);
1218c9de560dSAlex Tomas 		cur++;
1219c9de560dSAlex Tomas 	}
1220c9de560dSAlex Tomas }
1221c9de560dSAlex Tomas 
12227e5a8cddSShen Feng static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1223c9de560dSAlex Tomas 			  int first, int count)
1224c9de560dSAlex Tomas {
1225c9de560dSAlex Tomas 	int block = 0;
1226c9de560dSAlex Tomas 	int max = 0;
1227c9de560dSAlex Tomas 	int order;
1228c9de560dSAlex Tomas 	void *buddy;
1229c9de560dSAlex Tomas 	void *buddy2;
1230c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
1231c9de560dSAlex Tomas 
1232c9de560dSAlex Tomas 	BUG_ON(first + count > (sb->s_blocksize << 3));
1233bc8e6740SVincent Minet 	assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
1234c9de560dSAlex Tomas 	mb_check_buddy(e4b);
1235c9de560dSAlex Tomas 	mb_free_blocks_double(inode, e4b, first, count);
1236c9de560dSAlex Tomas 
1237c9de560dSAlex Tomas 	e4b->bd_info->bb_free += count;
1238c9de560dSAlex Tomas 	if (first < e4b->bd_info->bb_first_free)
1239c9de560dSAlex Tomas 		e4b->bd_info->bb_first_free = first;
1240c9de560dSAlex Tomas 
1241c9de560dSAlex Tomas 	/* let's maintain fragments counter */
1242c9de560dSAlex Tomas 	if (first != 0)
1243c9de560dSAlex Tomas 		block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1244c9de560dSAlex Tomas 	if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1245c9de560dSAlex Tomas 		max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1246c9de560dSAlex Tomas 	if (block && max)
1247c9de560dSAlex Tomas 		e4b->bd_info->bb_fragments--;
1248c9de560dSAlex Tomas 	else if (!block && !max)
1249c9de560dSAlex Tomas 		e4b->bd_info->bb_fragments++;
1250c9de560dSAlex Tomas 
1251c9de560dSAlex Tomas 	/* let's maintain buddy itself */
1252c9de560dSAlex Tomas 	while (count-- > 0) {
1253c9de560dSAlex Tomas 		block = first++;
1254c9de560dSAlex Tomas 		order = 0;
1255c9de560dSAlex Tomas 
1256c9de560dSAlex Tomas 		if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1257c9de560dSAlex Tomas 			ext4_fsblk_t blocknr;
1258c9de560dSAlex Tomas 			blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
1259c9de560dSAlex Tomas 			blocknr += block;
1260c9de560dSAlex Tomas 			blocknr +=
1261c9de560dSAlex Tomas 			    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
12625d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group,
12635d1b1b3fSAneesh Kumar K.V 				   __func__, "double-free of inode"
1264a9df9a49STheodore Ts'o 				   " %lu's block %llu(bit %u in group %u)",
1265c9de560dSAlex Tomas 				   inode ? inode->i_ino : 0, blocknr, block,
1266c9de560dSAlex Tomas 				   e4b->bd_group);
1267c9de560dSAlex Tomas 		}
1268c9de560dSAlex Tomas 		mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1269c9de560dSAlex Tomas 		e4b->bd_info->bb_counters[order]++;
1270c9de560dSAlex Tomas 
1271c9de560dSAlex Tomas 		/* start of the buddy */
1272c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, order, &max);
1273c9de560dSAlex Tomas 
1274c9de560dSAlex Tomas 		do {
1275c9de560dSAlex Tomas 			block &= ~1UL;
1276c9de560dSAlex Tomas 			if (mb_test_bit(block, buddy) ||
1277c9de560dSAlex Tomas 					mb_test_bit(block + 1, buddy))
1278c9de560dSAlex Tomas 				break;
1279c9de560dSAlex Tomas 
1280c9de560dSAlex Tomas 			/* both the buddies are free, try to coalesce them */
1281c9de560dSAlex Tomas 			buddy2 = mb_find_buddy(e4b, order + 1, &max);
1282c9de560dSAlex Tomas 
1283c9de560dSAlex Tomas 			if (!buddy2)
1284c9de560dSAlex Tomas 				break;
1285c9de560dSAlex Tomas 
1286c9de560dSAlex Tomas 			if (order > 0) {
1287c9de560dSAlex Tomas 				/* for special purposes, we don't set
1288c9de560dSAlex Tomas 				 * free bits in bitmap */
1289c9de560dSAlex Tomas 				mb_set_bit(block, buddy);
1290c9de560dSAlex Tomas 				mb_set_bit(block + 1, buddy);
1291c9de560dSAlex Tomas 			}
1292c9de560dSAlex Tomas 			e4b->bd_info->bb_counters[order]--;
1293c9de560dSAlex Tomas 			e4b->bd_info->bb_counters[order]--;
1294c9de560dSAlex Tomas 
1295c9de560dSAlex Tomas 			block = block >> 1;
1296c9de560dSAlex Tomas 			order++;
1297c9de560dSAlex Tomas 			e4b->bd_info->bb_counters[order]++;
1298c9de560dSAlex Tomas 
1299c9de560dSAlex Tomas 			mb_clear_bit(block, buddy2);
1300c9de560dSAlex Tomas 			buddy = buddy2;
1301c9de560dSAlex Tomas 		} while (1);
1302c9de560dSAlex Tomas 	}
1303c9de560dSAlex Tomas 	mb_check_buddy(e4b);
1304c9de560dSAlex Tomas }
1305c9de560dSAlex Tomas 
1306c9de560dSAlex Tomas static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1307c9de560dSAlex Tomas 				int needed, struct ext4_free_extent *ex)
1308c9de560dSAlex Tomas {
1309c9de560dSAlex Tomas 	int next = block;
1310c9de560dSAlex Tomas 	int max;
1311c9de560dSAlex Tomas 	int ord;
1312c9de560dSAlex Tomas 	void *buddy;
1313c9de560dSAlex Tomas 
1314bc8e6740SVincent Minet 	assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
1315c9de560dSAlex Tomas 	BUG_ON(ex == NULL);
1316c9de560dSAlex Tomas 
1317c9de560dSAlex Tomas 	buddy = mb_find_buddy(e4b, order, &max);
1318c9de560dSAlex Tomas 	BUG_ON(buddy == NULL);
1319c9de560dSAlex Tomas 	BUG_ON(block >= max);
1320c9de560dSAlex Tomas 	if (mb_test_bit(block, buddy)) {
1321c9de560dSAlex Tomas 		ex->fe_len = 0;
1322c9de560dSAlex Tomas 		ex->fe_start = 0;
1323c9de560dSAlex Tomas 		ex->fe_group = 0;
1324c9de560dSAlex Tomas 		return 0;
1325c9de560dSAlex Tomas 	}
1326c9de560dSAlex Tomas 
1327c9de560dSAlex Tomas 	/* FIXME dorp order completely ? */
1328c9de560dSAlex Tomas 	if (likely(order == 0)) {
1329c9de560dSAlex Tomas 		/* find actual order */
1330c9de560dSAlex Tomas 		order = mb_find_order_for_block(e4b, block);
1331c9de560dSAlex Tomas 		block = block >> order;
1332c9de560dSAlex Tomas 	}
1333c9de560dSAlex Tomas 
1334c9de560dSAlex Tomas 	ex->fe_len = 1 << order;
1335c9de560dSAlex Tomas 	ex->fe_start = block << order;
1336c9de560dSAlex Tomas 	ex->fe_group = e4b->bd_group;
1337c9de560dSAlex Tomas 
1338c9de560dSAlex Tomas 	/* calc difference from given start */
1339c9de560dSAlex Tomas 	next = next - ex->fe_start;
1340c9de560dSAlex Tomas 	ex->fe_len -= next;
1341c9de560dSAlex Tomas 	ex->fe_start += next;
1342c9de560dSAlex Tomas 
1343c9de560dSAlex Tomas 	while (needed > ex->fe_len &&
1344c9de560dSAlex Tomas 	       (buddy = mb_find_buddy(e4b, order, &max))) {
1345c9de560dSAlex Tomas 
1346c9de560dSAlex Tomas 		if (block + 1 >= max)
1347c9de560dSAlex Tomas 			break;
1348c9de560dSAlex Tomas 
1349c9de560dSAlex Tomas 		next = (block + 1) * (1 << order);
1350c9de560dSAlex Tomas 		if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1351c9de560dSAlex Tomas 			break;
1352c9de560dSAlex Tomas 
1353c9de560dSAlex Tomas 		ord = mb_find_order_for_block(e4b, next);
1354c9de560dSAlex Tomas 
1355c9de560dSAlex Tomas 		order = ord;
1356c9de560dSAlex Tomas 		block = next >> order;
1357c9de560dSAlex Tomas 		ex->fe_len += 1 << order;
1358c9de560dSAlex Tomas 	}
1359c9de560dSAlex Tomas 
1360c9de560dSAlex Tomas 	BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1361c9de560dSAlex Tomas 	return ex->fe_len;
1362c9de560dSAlex Tomas }
1363c9de560dSAlex Tomas 
1364c9de560dSAlex Tomas static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1365c9de560dSAlex Tomas {
1366c9de560dSAlex Tomas 	int ord;
1367c9de560dSAlex Tomas 	int mlen = 0;
1368c9de560dSAlex Tomas 	int max = 0;
1369c9de560dSAlex Tomas 	int cur;
1370c9de560dSAlex Tomas 	int start = ex->fe_start;
1371c9de560dSAlex Tomas 	int len = ex->fe_len;
1372c9de560dSAlex Tomas 	unsigned ret = 0;
1373c9de560dSAlex Tomas 	int len0 = len;
1374c9de560dSAlex Tomas 	void *buddy;
1375c9de560dSAlex Tomas 
1376c9de560dSAlex Tomas 	BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1377c9de560dSAlex Tomas 	BUG_ON(e4b->bd_group != ex->fe_group);
1378bc8e6740SVincent Minet 	assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
1379c9de560dSAlex Tomas 	mb_check_buddy(e4b);
1380c9de560dSAlex Tomas 	mb_mark_used_double(e4b, start, len);
1381c9de560dSAlex Tomas 
1382c9de560dSAlex Tomas 	e4b->bd_info->bb_free -= len;
1383c9de560dSAlex Tomas 	if (e4b->bd_info->bb_first_free == start)
1384c9de560dSAlex Tomas 		e4b->bd_info->bb_first_free += len;
1385c9de560dSAlex Tomas 
1386c9de560dSAlex Tomas 	/* let's maintain fragments counter */
1387c9de560dSAlex Tomas 	if (start != 0)
1388c9de560dSAlex Tomas 		mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1389c9de560dSAlex Tomas 	if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1390c9de560dSAlex Tomas 		max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1391c9de560dSAlex Tomas 	if (mlen && max)
1392c9de560dSAlex Tomas 		e4b->bd_info->bb_fragments++;
1393c9de560dSAlex Tomas 	else if (!mlen && !max)
1394c9de560dSAlex Tomas 		e4b->bd_info->bb_fragments--;
1395c9de560dSAlex Tomas 
1396c9de560dSAlex Tomas 	/* let's maintain buddy itself */
1397c9de560dSAlex Tomas 	while (len) {
1398c9de560dSAlex Tomas 		ord = mb_find_order_for_block(e4b, start);
1399c9de560dSAlex Tomas 
1400c9de560dSAlex Tomas 		if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1401c9de560dSAlex Tomas 			/* the whole chunk may be allocated at once! */
1402c9de560dSAlex Tomas 			mlen = 1 << ord;
1403c9de560dSAlex Tomas 			buddy = mb_find_buddy(e4b, ord, &max);
1404c9de560dSAlex Tomas 			BUG_ON((start >> ord) >= max);
1405c9de560dSAlex Tomas 			mb_set_bit(start >> ord, buddy);
1406c9de560dSAlex Tomas 			e4b->bd_info->bb_counters[ord]--;
1407c9de560dSAlex Tomas 			start += mlen;
1408c9de560dSAlex Tomas 			len -= mlen;
1409c9de560dSAlex Tomas 			BUG_ON(len < 0);
1410c9de560dSAlex Tomas 			continue;
1411c9de560dSAlex Tomas 		}
1412c9de560dSAlex Tomas 
1413c9de560dSAlex Tomas 		/* store for history */
1414c9de560dSAlex Tomas 		if (ret == 0)
1415c9de560dSAlex Tomas 			ret = len | (ord << 16);
1416c9de560dSAlex Tomas 
1417c9de560dSAlex Tomas 		/* we have to split large buddy */
1418c9de560dSAlex Tomas 		BUG_ON(ord <= 0);
1419c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, ord, &max);
1420c9de560dSAlex Tomas 		mb_set_bit(start >> ord, buddy);
1421c9de560dSAlex Tomas 		e4b->bd_info->bb_counters[ord]--;
1422c9de560dSAlex Tomas 
1423c9de560dSAlex Tomas 		ord--;
1424c9de560dSAlex Tomas 		cur = (start >> ord) & ~1U;
1425c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, ord, &max);
1426c9de560dSAlex Tomas 		mb_clear_bit(cur, buddy);
1427c9de560dSAlex Tomas 		mb_clear_bit(cur + 1, buddy);
1428c9de560dSAlex Tomas 		e4b->bd_info->bb_counters[ord]++;
1429c9de560dSAlex Tomas 		e4b->bd_info->bb_counters[ord]++;
1430c9de560dSAlex Tomas 	}
1431c9de560dSAlex Tomas 
1432955ce5f5SAneesh Kumar K.V 	mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
1433c9de560dSAlex Tomas 	mb_check_buddy(e4b);
1434c9de560dSAlex Tomas 
1435c9de560dSAlex Tomas 	return ret;
1436c9de560dSAlex Tomas }
1437c9de560dSAlex Tomas 
1438c9de560dSAlex Tomas /*
1439c9de560dSAlex Tomas  * Must be called under group lock!
1440c9de560dSAlex Tomas  */
1441c9de560dSAlex Tomas static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1442c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1443c9de560dSAlex Tomas {
1444c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1445c9de560dSAlex Tomas 	int ret;
1446c9de560dSAlex Tomas 
1447c9de560dSAlex Tomas 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1448c9de560dSAlex Tomas 	BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1449c9de560dSAlex Tomas 
1450c9de560dSAlex Tomas 	ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1451c9de560dSAlex Tomas 	ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1452c9de560dSAlex Tomas 	ret = mb_mark_used(e4b, &ac->ac_b_ex);
1453c9de560dSAlex Tomas 
1454c9de560dSAlex Tomas 	/* preallocation can change ac_b_ex, thus we store actually
1455c9de560dSAlex Tomas 	 * allocated blocks for history */
1456c9de560dSAlex Tomas 	ac->ac_f_ex = ac->ac_b_ex;
1457c9de560dSAlex Tomas 
1458c9de560dSAlex Tomas 	ac->ac_status = AC_STATUS_FOUND;
1459c9de560dSAlex Tomas 	ac->ac_tail = ret & 0xffff;
1460c9de560dSAlex Tomas 	ac->ac_buddy = ret >> 16;
1461c9de560dSAlex Tomas 
1462c3a326a6SAneesh Kumar K.V 	/*
1463c3a326a6SAneesh Kumar K.V 	 * take the page reference. We want the page to be pinned
1464c3a326a6SAneesh Kumar K.V 	 * so that we don't get a ext4_mb_init_cache_call for this
1465c3a326a6SAneesh Kumar K.V 	 * group until we update the bitmap. That would mean we
1466c3a326a6SAneesh Kumar K.V 	 * double allocate blocks. The reference is dropped
1467c3a326a6SAneesh Kumar K.V 	 * in ext4_mb_release_context
1468c3a326a6SAneesh Kumar K.V 	 */
1469c9de560dSAlex Tomas 	ac->ac_bitmap_page = e4b->bd_bitmap_page;
1470c9de560dSAlex Tomas 	get_page(ac->ac_bitmap_page);
1471c9de560dSAlex Tomas 	ac->ac_buddy_page = e4b->bd_buddy_page;
1472c9de560dSAlex Tomas 	get_page(ac->ac_buddy_page);
14738556e8f3SAneesh Kumar K.V 	/* on allocation we use ac to track the held semaphore */
14748556e8f3SAneesh Kumar K.V 	ac->alloc_semp =  e4b->alloc_semp;
14758556e8f3SAneesh Kumar K.V 	e4b->alloc_semp = NULL;
1476c9de560dSAlex Tomas 	/* store last allocated for subsequent stream allocation */
14774ba74d00STheodore Ts'o 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
1478c9de560dSAlex Tomas 		spin_lock(&sbi->s_md_lock);
1479c9de560dSAlex Tomas 		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1480c9de560dSAlex Tomas 		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1481c9de560dSAlex Tomas 		spin_unlock(&sbi->s_md_lock);
1482c9de560dSAlex Tomas 	}
1483c9de560dSAlex Tomas }
1484c9de560dSAlex Tomas 
1485c9de560dSAlex Tomas /*
1486c9de560dSAlex Tomas  * regular allocator, for general purposes allocation
1487c9de560dSAlex Tomas  */
1488c9de560dSAlex Tomas 
1489c9de560dSAlex Tomas static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1490c9de560dSAlex Tomas 					struct ext4_buddy *e4b,
1491c9de560dSAlex Tomas 					int finish_group)
1492c9de560dSAlex Tomas {
1493c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1494c9de560dSAlex Tomas 	struct ext4_free_extent *bex = &ac->ac_b_ex;
1495c9de560dSAlex Tomas 	struct ext4_free_extent *gex = &ac->ac_g_ex;
1496c9de560dSAlex Tomas 	struct ext4_free_extent ex;
1497c9de560dSAlex Tomas 	int max;
1498c9de560dSAlex Tomas 
1499032115fcSAneesh Kumar K.V 	if (ac->ac_status == AC_STATUS_FOUND)
1500032115fcSAneesh Kumar K.V 		return;
1501c9de560dSAlex Tomas 	/*
1502c9de560dSAlex Tomas 	 * We don't want to scan for a whole year
1503c9de560dSAlex Tomas 	 */
1504c9de560dSAlex Tomas 	if (ac->ac_found > sbi->s_mb_max_to_scan &&
1505c9de560dSAlex Tomas 			!(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1506c9de560dSAlex Tomas 		ac->ac_status = AC_STATUS_BREAK;
1507c9de560dSAlex Tomas 		return;
1508c9de560dSAlex Tomas 	}
1509c9de560dSAlex Tomas 
1510c9de560dSAlex Tomas 	/*
1511c9de560dSAlex Tomas 	 * Haven't found good chunk so far, let's continue
1512c9de560dSAlex Tomas 	 */
1513c9de560dSAlex Tomas 	if (bex->fe_len < gex->fe_len)
1514c9de560dSAlex Tomas 		return;
1515c9de560dSAlex Tomas 
1516c9de560dSAlex Tomas 	if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1517c9de560dSAlex Tomas 			&& bex->fe_group == e4b->bd_group) {
1518c9de560dSAlex Tomas 		/* recheck chunk's availability - we don't know
1519c9de560dSAlex Tomas 		 * when it was found (within this lock-unlock
1520c9de560dSAlex Tomas 		 * period or not) */
1521c9de560dSAlex Tomas 		max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1522c9de560dSAlex Tomas 		if (max >= gex->fe_len) {
1523c9de560dSAlex Tomas 			ext4_mb_use_best_found(ac, e4b);
1524c9de560dSAlex Tomas 			return;
1525c9de560dSAlex Tomas 		}
1526c9de560dSAlex Tomas 	}
1527c9de560dSAlex Tomas }
1528c9de560dSAlex Tomas 
1529c9de560dSAlex Tomas /*
1530c9de560dSAlex Tomas  * The routine checks whether found extent is good enough. If it is,
1531c9de560dSAlex Tomas  * then the extent gets marked used and flag is set to the context
1532c9de560dSAlex Tomas  * to stop scanning. Otherwise, the extent is compared with the
1533c9de560dSAlex Tomas  * previous found extent and if new one is better, then it's stored
1534c9de560dSAlex Tomas  * in the context. Later, the best found extent will be used, if
1535c9de560dSAlex Tomas  * mballoc can't find good enough extent.
1536c9de560dSAlex Tomas  *
1537c9de560dSAlex Tomas  * FIXME: real allocation policy is to be designed yet!
1538c9de560dSAlex Tomas  */
1539c9de560dSAlex Tomas static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1540c9de560dSAlex Tomas 					struct ext4_free_extent *ex,
1541c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1542c9de560dSAlex Tomas {
1543c9de560dSAlex Tomas 	struct ext4_free_extent *bex = &ac->ac_b_ex;
1544c9de560dSAlex Tomas 	struct ext4_free_extent *gex = &ac->ac_g_ex;
1545c9de560dSAlex Tomas 
1546c9de560dSAlex Tomas 	BUG_ON(ex->fe_len <= 0);
15478d03c7a0SEric Sandeen 	BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1548c9de560dSAlex Tomas 	BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1549c9de560dSAlex Tomas 	BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1550c9de560dSAlex Tomas 
1551c9de560dSAlex Tomas 	ac->ac_found++;
1552c9de560dSAlex Tomas 
1553c9de560dSAlex Tomas 	/*
1554c9de560dSAlex Tomas 	 * The special case - take what you catch first
1555c9de560dSAlex Tomas 	 */
1556c9de560dSAlex Tomas 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1557c9de560dSAlex Tomas 		*bex = *ex;
1558c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1559c9de560dSAlex Tomas 		return;
1560c9de560dSAlex Tomas 	}
1561c9de560dSAlex Tomas 
1562c9de560dSAlex Tomas 	/*
1563c9de560dSAlex Tomas 	 * Let's check whether the chuck is good enough
1564c9de560dSAlex Tomas 	 */
1565c9de560dSAlex Tomas 	if (ex->fe_len == gex->fe_len) {
1566c9de560dSAlex Tomas 		*bex = *ex;
1567c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1568c9de560dSAlex Tomas 		return;
1569c9de560dSAlex Tomas 	}
1570c9de560dSAlex Tomas 
1571c9de560dSAlex Tomas 	/*
1572c9de560dSAlex Tomas 	 * If this is first found extent, just store it in the context
1573c9de560dSAlex Tomas 	 */
1574c9de560dSAlex Tomas 	if (bex->fe_len == 0) {
1575c9de560dSAlex Tomas 		*bex = *ex;
1576c9de560dSAlex Tomas 		return;
1577c9de560dSAlex Tomas 	}
1578c9de560dSAlex Tomas 
1579c9de560dSAlex Tomas 	/*
1580c9de560dSAlex Tomas 	 * If new found extent is better, store it in the context
1581c9de560dSAlex Tomas 	 */
1582c9de560dSAlex Tomas 	if (bex->fe_len < gex->fe_len) {
1583c9de560dSAlex Tomas 		/* if the request isn't satisfied, any found extent
1584c9de560dSAlex Tomas 		 * larger than previous best one is better */
1585c9de560dSAlex Tomas 		if (ex->fe_len > bex->fe_len)
1586c9de560dSAlex Tomas 			*bex = *ex;
1587c9de560dSAlex Tomas 	} else if (ex->fe_len > gex->fe_len) {
1588c9de560dSAlex Tomas 		/* if the request is satisfied, then we try to find
1589c9de560dSAlex Tomas 		 * an extent that still satisfy the request, but is
1590c9de560dSAlex Tomas 		 * smaller than previous one */
1591c9de560dSAlex Tomas 		if (ex->fe_len < bex->fe_len)
1592c9de560dSAlex Tomas 			*bex = *ex;
1593c9de560dSAlex Tomas 	}
1594c9de560dSAlex Tomas 
1595c9de560dSAlex Tomas 	ext4_mb_check_limits(ac, e4b, 0);
1596c9de560dSAlex Tomas }
1597c9de560dSAlex Tomas 
1598089ceeccSEric Sandeen static noinline_for_stack
1599089ceeccSEric Sandeen int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
1600c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1601c9de560dSAlex Tomas {
1602c9de560dSAlex Tomas 	struct ext4_free_extent ex = ac->ac_b_ex;
1603c9de560dSAlex Tomas 	ext4_group_t group = ex.fe_group;
1604c9de560dSAlex Tomas 	int max;
1605c9de560dSAlex Tomas 	int err;
1606c9de560dSAlex Tomas 
1607c9de560dSAlex Tomas 	BUG_ON(ex.fe_len <= 0);
1608c9de560dSAlex Tomas 	err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1609c9de560dSAlex Tomas 	if (err)
1610c9de560dSAlex Tomas 		return err;
1611c9de560dSAlex Tomas 
1612c9de560dSAlex Tomas 	ext4_lock_group(ac->ac_sb, group);
1613c9de560dSAlex Tomas 	max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1614c9de560dSAlex Tomas 
1615c9de560dSAlex Tomas 	if (max > 0) {
1616c9de560dSAlex Tomas 		ac->ac_b_ex = ex;
1617c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1618c9de560dSAlex Tomas 	}
1619c9de560dSAlex Tomas 
1620c9de560dSAlex Tomas 	ext4_unlock_group(ac->ac_sb, group);
1621c9de560dSAlex Tomas 	ext4_mb_release_desc(e4b);
1622c9de560dSAlex Tomas 
1623c9de560dSAlex Tomas 	return 0;
1624c9de560dSAlex Tomas }
1625c9de560dSAlex Tomas 
1626089ceeccSEric Sandeen static noinline_for_stack
1627089ceeccSEric Sandeen int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
1628c9de560dSAlex Tomas 				struct ext4_buddy *e4b)
1629c9de560dSAlex Tomas {
1630c9de560dSAlex Tomas 	ext4_group_t group = ac->ac_g_ex.fe_group;
1631c9de560dSAlex Tomas 	int max;
1632c9de560dSAlex Tomas 	int err;
1633c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1634c9de560dSAlex Tomas 	struct ext4_super_block *es = sbi->s_es;
1635c9de560dSAlex Tomas 	struct ext4_free_extent ex;
1636c9de560dSAlex Tomas 
1637c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1638c9de560dSAlex Tomas 		return 0;
1639c9de560dSAlex Tomas 
1640c9de560dSAlex Tomas 	err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1641c9de560dSAlex Tomas 	if (err)
1642c9de560dSAlex Tomas 		return err;
1643c9de560dSAlex Tomas 
1644c9de560dSAlex Tomas 	ext4_lock_group(ac->ac_sb, group);
1645c9de560dSAlex Tomas 	max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1646c9de560dSAlex Tomas 			     ac->ac_g_ex.fe_len, &ex);
1647c9de560dSAlex Tomas 
1648c9de560dSAlex Tomas 	if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1649c9de560dSAlex Tomas 		ext4_fsblk_t start;
1650c9de560dSAlex Tomas 
1651c9de560dSAlex Tomas 		start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
1652c9de560dSAlex Tomas 			ex.fe_start + le32_to_cpu(es->s_first_data_block);
1653c9de560dSAlex Tomas 		/* use do_div to get remainder (would be 64-bit modulo) */
1654c9de560dSAlex Tomas 		if (do_div(start, sbi->s_stripe) == 0) {
1655c9de560dSAlex Tomas 			ac->ac_found++;
1656c9de560dSAlex Tomas 			ac->ac_b_ex = ex;
1657c9de560dSAlex Tomas 			ext4_mb_use_best_found(ac, e4b);
1658c9de560dSAlex Tomas 		}
1659c9de560dSAlex Tomas 	} else if (max >= ac->ac_g_ex.fe_len) {
1660c9de560dSAlex Tomas 		BUG_ON(ex.fe_len <= 0);
1661c9de560dSAlex Tomas 		BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1662c9de560dSAlex Tomas 		BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1663c9de560dSAlex Tomas 		ac->ac_found++;
1664c9de560dSAlex Tomas 		ac->ac_b_ex = ex;
1665c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1666c9de560dSAlex Tomas 	} else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1667c9de560dSAlex Tomas 		/* Sometimes, caller may want to merge even small
1668c9de560dSAlex Tomas 		 * number of blocks to an existing extent */
1669c9de560dSAlex Tomas 		BUG_ON(ex.fe_len <= 0);
1670c9de560dSAlex Tomas 		BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1671c9de560dSAlex Tomas 		BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1672c9de560dSAlex Tomas 		ac->ac_found++;
1673c9de560dSAlex Tomas 		ac->ac_b_ex = ex;
1674c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1675c9de560dSAlex Tomas 	}
1676c9de560dSAlex Tomas 	ext4_unlock_group(ac->ac_sb, group);
1677c9de560dSAlex Tomas 	ext4_mb_release_desc(e4b);
1678c9de560dSAlex Tomas 
1679c9de560dSAlex Tomas 	return 0;
1680c9de560dSAlex Tomas }
1681c9de560dSAlex Tomas 
1682c9de560dSAlex Tomas /*
1683c9de560dSAlex Tomas  * The routine scans buddy structures (not bitmap!) from given order
1684c9de560dSAlex Tomas  * to max order and tries to find big enough chunk to satisfy the req
1685c9de560dSAlex Tomas  */
1686089ceeccSEric Sandeen static noinline_for_stack
1687089ceeccSEric Sandeen void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
1688c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1689c9de560dSAlex Tomas {
1690c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
1691c9de560dSAlex Tomas 	struct ext4_group_info *grp = e4b->bd_info;
1692c9de560dSAlex Tomas 	void *buddy;
1693c9de560dSAlex Tomas 	int i;
1694c9de560dSAlex Tomas 	int k;
1695c9de560dSAlex Tomas 	int max;
1696c9de560dSAlex Tomas 
1697c9de560dSAlex Tomas 	BUG_ON(ac->ac_2order <= 0);
1698c9de560dSAlex Tomas 	for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1699c9de560dSAlex Tomas 		if (grp->bb_counters[i] == 0)
1700c9de560dSAlex Tomas 			continue;
1701c9de560dSAlex Tomas 
1702c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, i, &max);
1703c9de560dSAlex Tomas 		BUG_ON(buddy == NULL);
1704c9de560dSAlex Tomas 
1705ffad0a44SAneesh Kumar K.V 		k = mb_find_next_zero_bit(buddy, max, 0);
1706c9de560dSAlex Tomas 		BUG_ON(k >= max);
1707c9de560dSAlex Tomas 
1708c9de560dSAlex Tomas 		ac->ac_found++;
1709c9de560dSAlex Tomas 
1710c9de560dSAlex Tomas 		ac->ac_b_ex.fe_len = 1 << i;
1711c9de560dSAlex Tomas 		ac->ac_b_ex.fe_start = k << i;
1712c9de560dSAlex Tomas 		ac->ac_b_ex.fe_group = e4b->bd_group;
1713c9de560dSAlex Tomas 
1714c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1715c9de560dSAlex Tomas 
1716c9de560dSAlex Tomas 		BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1717c9de560dSAlex Tomas 
1718c9de560dSAlex Tomas 		if (EXT4_SB(sb)->s_mb_stats)
1719c9de560dSAlex Tomas 			atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1720c9de560dSAlex Tomas 
1721c9de560dSAlex Tomas 		break;
1722c9de560dSAlex Tomas 	}
1723c9de560dSAlex Tomas }
1724c9de560dSAlex Tomas 
1725c9de560dSAlex Tomas /*
1726c9de560dSAlex Tomas  * The routine scans the group and measures all found extents.
1727c9de560dSAlex Tomas  * In order to optimize scanning, caller must pass number of
1728c9de560dSAlex Tomas  * free blocks in the group, so the routine can know upper limit.
1729c9de560dSAlex Tomas  */
1730089ceeccSEric Sandeen static noinline_for_stack
1731089ceeccSEric Sandeen void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
1732c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1733c9de560dSAlex Tomas {
1734c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
1735c9de560dSAlex Tomas 	void *bitmap = EXT4_MB_BITMAP(e4b);
1736c9de560dSAlex Tomas 	struct ext4_free_extent ex;
1737c9de560dSAlex Tomas 	int i;
1738c9de560dSAlex Tomas 	int free;
1739c9de560dSAlex Tomas 
1740c9de560dSAlex Tomas 	free = e4b->bd_info->bb_free;
1741c9de560dSAlex Tomas 	BUG_ON(free <= 0);
1742c9de560dSAlex Tomas 
1743c9de560dSAlex Tomas 	i = e4b->bd_info->bb_first_free;
1744c9de560dSAlex Tomas 
1745c9de560dSAlex Tomas 	while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1746ffad0a44SAneesh Kumar K.V 		i = mb_find_next_zero_bit(bitmap,
1747c9de560dSAlex Tomas 						EXT4_BLOCKS_PER_GROUP(sb), i);
1748c9de560dSAlex Tomas 		if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
174926346ff6SAneesh Kumar K.V 			/*
1750e56eb659SAneesh Kumar K.V 			 * IF we have corrupt bitmap, we won't find any
175126346ff6SAneesh Kumar K.V 			 * free blocks even though group info says we
175226346ff6SAneesh Kumar K.V 			 * we have free blocks
175326346ff6SAneesh Kumar K.V 			 */
17545d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group,
17555d1b1b3fSAneesh Kumar K.V 					__func__, "%d free blocks as per "
1756fde4d95aSTheodore Ts'o 					"group info. But bitmap says 0",
175726346ff6SAneesh Kumar K.V 					free);
1758c9de560dSAlex Tomas 			break;
1759c9de560dSAlex Tomas 		}
1760c9de560dSAlex Tomas 
1761c9de560dSAlex Tomas 		mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1762c9de560dSAlex Tomas 		BUG_ON(ex.fe_len <= 0);
176326346ff6SAneesh Kumar K.V 		if (free < ex.fe_len) {
17645d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group,
17655d1b1b3fSAneesh Kumar K.V 					__func__, "%d free blocks as per "
1766fde4d95aSTheodore Ts'o 					"group info. But got %d blocks",
176726346ff6SAneesh Kumar K.V 					free, ex.fe_len);
1768e56eb659SAneesh Kumar K.V 			/*
1769e56eb659SAneesh Kumar K.V 			 * The number of free blocks differs. This mostly
1770e56eb659SAneesh Kumar K.V 			 * indicate that the bitmap is corrupt. So exit
1771e56eb659SAneesh Kumar K.V 			 * without claiming the space.
1772e56eb659SAneesh Kumar K.V 			 */
1773e56eb659SAneesh Kumar K.V 			break;
177426346ff6SAneesh Kumar K.V 		}
1775c9de560dSAlex Tomas 
1776c9de560dSAlex Tomas 		ext4_mb_measure_extent(ac, &ex, e4b);
1777c9de560dSAlex Tomas 
1778c9de560dSAlex Tomas 		i += ex.fe_len;
1779c9de560dSAlex Tomas 		free -= ex.fe_len;
1780c9de560dSAlex Tomas 	}
1781c9de560dSAlex Tomas 
1782c9de560dSAlex Tomas 	ext4_mb_check_limits(ac, e4b, 1);
1783c9de560dSAlex Tomas }
1784c9de560dSAlex Tomas 
1785c9de560dSAlex Tomas /*
1786c9de560dSAlex Tomas  * This is a special case for storages like raid5
1787c9de560dSAlex Tomas  * we try to find stripe-aligned chunks for stripe-size requests
1788c9de560dSAlex Tomas  * XXX should do so at least for multiples of stripe size as well
1789c9de560dSAlex Tomas  */
1790089ceeccSEric Sandeen static noinline_for_stack
1791089ceeccSEric Sandeen void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
1792c9de560dSAlex Tomas 				 struct ext4_buddy *e4b)
1793c9de560dSAlex Tomas {
1794c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
1795c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1796c9de560dSAlex Tomas 	void *bitmap = EXT4_MB_BITMAP(e4b);
1797c9de560dSAlex Tomas 	struct ext4_free_extent ex;
1798c9de560dSAlex Tomas 	ext4_fsblk_t first_group_block;
1799c9de560dSAlex Tomas 	ext4_fsblk_t a;
1800c9de560dSAlex Tomas 	ext4_grpblk_t i;
1801c9de560dSAlex Tomas 	int max;
1802c9de560dSAlex Tomas 
1803c9de560dSAlex Tomas 	BUG_ON(sbi->s_stripe == 0);
1804c9de560dSAlex Tomas 
1805c9de560dSAlex Tomas 	/* find first stripe-aligned block in group */
1806c9de560dSAlex Tomas 	first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
1807c9de560dSAlex Tomas 		+ le32_to_cpu(sbi->s_es->s_first_data_block);
1808c9de560dSAlex Tomas 	a = first_group_block + sbi->s_stripe - 1;
1809c9de560dSAlex Tomas 	do_div(a, sbi->s_stripe);
1810c9de560dSAlex Tomas 	i = (a * sbi->s_stripe) - first_group_block;
1811c9de560dSAlex Tomas 
1812c9de560dSAlex Tomas 	while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1813c9de560dSAlex Tomas 		if (!mb_test_bit(i, bitmap)) {
1814c9de560dSAlex Tomas 			max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1815c9de560dSAlex Tomas 			if (max >= sbi->s_stripe) {
1816c9de560dSAlex Tomas 				ac->ac_found++;
1817c9de560dSAlex Tomas 				ac->ac_b_ex = ex;
1818c9de560dSAlex Tomas 				ext4_mb_use_best_found(ac, e4b);
1819c9de560dSAlex Tomas 				break;
1820c9de560dSAlex Tomas 			}
1821c9de560dSAlex Tomas 		}
1822c9de560dSAlex Tomas 		i += sbi->s_stripe;
1823c9de560dSAlex Tomas 	}
1824c9de560dSAlex Tomas }
1825c9de560dSAlex Tomas 
1826c9de560dSAlex Tomas static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1827c9de560dSAlex Tomas 				ext4_group_t group, int cr)
1828c9de560dSAlex Tomas {
1829c9de560dSAlex Tomas 	unsigned free, fragments;
1830c9de560dSAlex Tomas 	unsigned i, bits;
1831a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
1832c9de560dSAlex Tomas 	struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1833c9de560dSAlex Tomas 
1834c9de560dSAlex Tomas 	BUG_ON(cr < 0 || cr >= 4);
1835c9de560dSAlex Tomas 	BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1836c9de560dSAlex Tomas 
1837c9de560dSAlex Tomas 	free = grp->bb_free;
1838c9de560dSAlex Tomas 	fragments = grp->bb_fragments;
1839c9de560dSAlex Tomas 	if (free == 0)
1840c9de560dSAlex Tomas 		return 0;
1841c9de560dSAlex Tomas 	if (fragments == 0)
1842c9de560dSAlex Tomas 		return 0;
1843c9de560dSAlex Tomas 
1844c9de560dSAlex Tomas 	switch (cr) {
1845c9de560dSAlex Tomas 	case 0:
1846c9de560dSAlex Tomas 		BUG_ON(ac->ac_2order == 0);
1847c9de560dSAlex Tomas 
1848a4912123STheodore Ts'o 		/* Avoid using the first bg of a flexgroup for data files */
1849a4912123STheodore Ts'o 		if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1850a4912123STheodore Ts'o 		    (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1851a4912123STheodore Ts'o 		    ((group % flex_size) == 0))
1852a4912123STheodore Ts'o 			return 0;
1853a4912123STheodore Ts'o 
1854c9de560dSAlex Tomas 		bits = ac->ac_sb->s_blocksize_bits + 1;
1855c9de560dSAlex Tomas 		for (i = ac->ac_2order; i <= bits; i++)
1856c9de560dSAlex Tomas 			if (grp->bb_counters[i] > 0)
1857c9de560dSAlex Tomas 				return 1;
1858c9de560dSAlex Tomas 		break;
1859c9de560dSAlex Tomas 	case 1:
1860c9de560dSAlex Tomas 		if ((free / fragments) >= ac->ac_g_ex.fe_len)
1861c9de560dSAlex Tomas 			return 1;
1862c9de560dSAlex Tomas 		break;
1863c9de560dSAlex Tomas 	case 2:
1864c9de560dSAlex Tomas 		if (free >= ac->ac_g_ex.fe_len)
1865c9de560dSAlex Tomas 			return 1;
1866c9de560dSAlex Tomas 		break;
1867c9de560dSAlex Tomas 	case 3:
1868c9de560dSAlex Tomas 		return 1;
1869c9de560dSAlex Tomas 	default:
1870c9de560dSAlex Tomas 		BUG();
1871c9de560dSAlex Tomas 	}
1872c9de560dSAlex Tomas 
1873c9de560dSAlex Tomas 	return 0;
1874c9de560dSAlex Tomas }
1875c9de560dSAlex Tomas 
1876920313a7SAneesh Kumar K.V /*
1877920313a7SAneesh Kumar K.V  * lock the group_info alloc_sem of all the groups
1878920313a7SAneesh Kumar K.V  * belonging to the same buddy cache page. This
1879920313a7SAneesh Kumar K.V  * make sure other parallel operation on the buddy
1880920313a7SAneesh Kumar K.V  * cache doesn't happen  whild holding the buddy cache
1881920313a7SAneesh Kumar K.V  * lock
1882920313a7SAneesh Kumar K.V  */
1883920313a7SAneesh Kumar K.V int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1884920313a7SAneesh Kumar K.V {
1885920313a7SAneesh Kumar K.V 	int i;
1886920313a7SAneesh Kumar K.V 	int block, pnum;
1887920313a7SAneesh Kumar K.V 	int blocks_per_page;
1888920313a7SAneesh Kumar K.V 	int groups_per_page;
18898df9675fSTheodore Ts'o 	ext4_group_t ngroups = ext4_get_groups_count(sb);
1890920313a7SAneesh Kumar K.V 	ext4_group_t first_group;
1891920313a7SAneesh Kumar K.V 	struct ext4_group_info *grp;
1892920313a7SAneesh Kumar K.V 
1893920313a7SAneesh Kumar K.V 	blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1894920313a7SAneesh Kumar K.V 	/*
1895920313a7SAneesh Kumar K.V 	 * the buddy cache inode stores the block bitmap
1896920313a7SAneesh Kumar K.V 	 * and buddy information in consecutive blocks.
1897920313a7SAneesh Kumar K.V 	 * So for each group we need two blocks.
1898920313a7SAneesh Kumar K.V 	 */
1899920313a7SAneesh Kumar K.V 	block = group * 2;
1900920313a7SAneesh Kumar K.V 	pnum = block / blocks_per_page;
1901920313a7SAneesh Kumar K.V 	first_group = pnum * blocks_per_page / 2;
1902920313a7SAneesh Kumar K.V 
1903920313a7SAneesh Kumar K.V 	groups_per_page = blocks_per_page >> 1;
1904920313a7SAneesh Kumar K.V 	if (groups_per_page == 0)
1905920313a7SAneesh Kumar K.V 		groups_per_page = 1;
1906920313a7SAneesh Kumar K.V 	/* read all groups the page covers into the cache */
1907920313a7SAneesh Kumar K.V 	for (i = 0; i < groups_per_page; i++) {
1908920313a7SAneesh Kumar K.V 
19098df9675fSTheodore Ts'o 		if ((first_group + i) >= ngroups)
1910920313a7SAneesh Kumar K.V 			break;
1911920313a7SAneesh Kumar K.V 		grp = ext4_get_group_info(sb, first_group + i);
1912920313a7SAneesh Kumar K.V 		/* take all groups write allocation
1913920313a7SAneesh Kumar K.V 		 * semaphore. This make sure there is
1914920313a7SAneesh Kumar K.V 		 * no block allocation going on in any
1915920313a7SAneesh Kumar K.V 		 * of that groups
1916920313a7SAneesh Kumar K.V 		 */
1917b7be019eSAneesh Kumar K.V 		down_write_nested(&grp->alloc_sem, i);
1918920313a7SAneesh Kumar K.V 	}
1919920313a7SAneesh Kumar K.V 	return i;
1920920313a7SAneesh Kumar K.V }
1921920313a7SAneesh Kumar K.V 
1922920313a7SAneesh Kumar K.V void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1923920313a7SAneesh Kumar K.V 					ext4_group_t group, int locked_group)
1924920313a7SAneesh Kumar K.V {
1925920313a7SAneesh Kumar K.V 	int i;
1926920313a7SAneesh Kumar K.V 	int block, pnum;
1927920313a7SAneesh Kumar K.V 	int blocks_per_page;
1928920313a7SAneesh Kumar K.V 	ext4_group_t first_group;
1929920313a7SAneesh Kumar K.V 	struct ext4_group_info *grp;
1930920313a7SAneesh Kumar K.V 
1931920313a7SAneesh Kumar K.V 	blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1932920313a7SAneesh Kumar K.V 	/*
1933920313a7SAneesh Kumar K.V 	 * the buddy cache inode stores the block bitmap
1934920313a7SAneesh Kumar K.V 	 * and buddy information in consecutive blocks.
1935920313a7SAneesh Kumar K.V 	 * So for each group we need two blocks.
1936920313a7SAneesh Kumar K.V 	 */
1937920313a7SAneesh Kumar K.V 	block = group * 2;
1938920313a7SAneesh Kumar K.V 	pnum = block / blocks_per_page;
1939920313a7SAneesh Kumar K.V 	first_group = pnum * blocks_per_page / 2;
1940920313a7SAneesh Kumar K.V 	/* release locks on all the groups */
1941920313a7SAneesh Kumar K.V 	for (i = 0; i < locked_group; i++) {
1942920313a7SAneesh Kumar K.V 
1943920313a7SAneesh Kumar K.V 		grp = ext4_get_group_info(sb, first_group + i);
1944920313a7SAneesh Kumar K.V 		/* take all groups write allocation
1945920313a7SAneesh Kumar K.V 		 * semaphore. This make sure there is
1946920313a7SAneesh Kumar K.V 		 * no block allocation going on in any
1947920313a7SAneesh Kumar K.V 		 * of that groups
1948920313a7SAneesh Kumar K.V 		 */
1949920313a7SAneesh Kumar K.V 		up_write(&grp->alloc_sem);
1950920313a7SAneesh Kumar K.V 	}
1951920313a7SAneesh Kumar K.V 
1952920313a7SAneesh Kumar K.V }
1953920313a7SAneesh Kumar K.V 
19544ddfef7bSEric Sandeen static noinline_for_stack int
19554ddfef7bSEric Sandeen ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
1956c9de560dSAlex Tomas {
19578df9675fSTheodore Ts'o 	ext4_group_t ngroups, group, i;
1958c9de560dSAlex Tomas 	int cr;
1959c9de560dSAlex Tomas 	int err = 0;
1960c9de560dSAlex Tomas 	int bsbits;
1961c9de560dSAlex Tomas 	struct ext4_sb_info *sbi;
1962c9de560dSAlex Tomas 	struct super_block *sb;
1963c9de560dSAlex Tomas 	struct ext4_buddy e4b;
1964c9de560dSAlex Tomas 
1965c9de560dSAlex Tomas 	sb = ac->ac_sb;
1966c9de560dSAlex Tomas 	sbi = EXT4_SB(sb);
19678df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
1968fb0a387dSEric Sandeen 	/* non-extent files are limited to low blocks/groups */
1969fb0a387dSEric Sandeen 	if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL))
1970fb0a387dSEric Sandeen 		ngroups = sbi->s_blockfile_groups;
1971fb0a387dSEric Sandeen 
1972c9de560dSAlex Tomas 	BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1973c9de560dSAlex Tomas 
1974c9de560dSAlex Tomas 	/* first, try the goal */
1975c9de560dSAlex Tomas 	err = ext4_mb_find_by_goal(ac, &e4b);
1976c9de560dSAlex Tomas 	if (err || ac->ac_status == AC_STATUS_FOUND)
1977c9de560dSAlex Tomas 		goto out;
1978c9de560dSAlex Tomas 
1979c9de560dSAlex Tomas 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1980c9de560dSAlex Tomas 		goto out;
1981c9de560dSAlex Tomas 
1982c9de560dSAlex Tomas 	/*
1983c9de560dSAlex Tomas 	 * ac->ac2_order is set only if the fe_len is a power of 2
1984c9de560dSAlex Tomas 	 * if ac2_order is set we also set criteria to 0 so that we
1985c9de560dSAlex Tomas 	 * try exact allocation using buddy.
1986c9de560dSAlex Tomas 	 */
1987c9de560dSAlex Tomas 	i = fls(ac->ac_g_ex.fe_len);
1988c9de560dSAlex Tomas 	ac->ac_2order = 0;
1989c9de560dSAlex Tomas 	/*
1990c9de560dSAlex Tomas 	 * We search using buddy data only if the order of the request
1991c9de560dSAlex Tomas 	 * is greater than equal to the sbi_s_mb_order2_reqs
1992b713a5ecSTheodore Ts'o 	 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1993c9de560dSAlex Tomas 	 */
1994c9de560dSAlex Tomas 	if (i >= sbi->s_mb_order2_reqs) {
1995c9de560dSAlex Tomas 		/*
1996c9de560dSAlex Tomas 		 * This should tell if fe_len is exactly power of 2
1997c9de560dSAlex Tomas 		 */
1998c9de560dSAlex Tomas 		if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1999c9de560dSAlex Tomas 			ac->ac_2order = i - 1;
2000c9de560dSAlex Tomas 	}
2001c9de560dSAlex Tomas 
2002c9de560dSAlex Tomas 	bsbits = ac->ac_sb->s_blocksize_bits;
2003c9de560dSAlex Tomas 
20044ba74d00STheodore Ts'o 	/* if stream allocation is enabled, use global goal */
20054ba74d00STheodore Ts'o 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
2006c9de560dSAlex Tomas 		/* TBD: may be hot point */
2007c9de560dSAlex Tomas 		spin_lock(&sbi->s_md_lock);
2008c9de560dSAlex Tomas 		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2009c9de560dSAlex Tomas 		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2010c9de560dSAlex Tomas 		spin_unlock(&sbi->s_md_lock);
2011c9de560dSAlex Tomas 	}
20124ba74d00STheodore Ts'o 
2013c9de560dSAlex Tomas 	/* Let's just scan groups to find more-less suitable blocks */
2014c9de560dSAlex Tomas 	cr = ac->ac_2order ? 0 : 1;
2015c9de560dSAlex Tomas 	/*
2016c9de560dSAlex Tomas 	 * cr == 0 try to get exact allocation,
2017c9de560dSAlex Tomas 	 * cr == 3  try to get anything
2018c9de560dSAlex Tomas 	 */
2019c9de560dSAlex Tomas repeat:
2020c9de560dSAlex Tomas 	for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2021c9de560dSAlex Tomas 		ac->ac_criteria = cr;
2022ed8f9c75SAneesh Kumar K.V 		/*
2023ed8f9c75SAneesh Kumar K.V 		 * searching for the right group start
2024ed8f9c75SAneesh Kumar K.V 		 * from the goal value specified
2025ed8f9c75SAneesh Kumar K.V 		 */
2026ed8f9c75SAneesh Kumar K.V 		group = ac->ac_g_ex.fe_group;
2027ed8f9c75SAneesh Kumar K.V 
20288df9675fSTheodore Ts'o 		for (i = 0; i < ngroups; group++, i++) {
2029c9de560dSAlex Tomas 			struct ext4_group_info *grp;
2030c9de560dSAlex Tomas 			struct ext4_group_desc *desc;
2031c9de560dSAlex Tomas 
20328df9675fSTheodore Ts'o 			if (group == ngroups)
2033c9de560dSAlex Tomas 				group = 0;
2034c9de560dSAlex Tomas 
2035c9de560dSAlex Tomas 			/* quick check to skip empty groups */
2036920313a7SAneesh Kumar K.V 			grp = ext4_get_group_info(sb, group);
2037c9de560dSAlex Tomas 			if (grp->bb_free == 0)
2038c9de560dSAlex Tomas 				continue;
2039c9de560dSAlex Tomas 
2040c9de560dSAlex Tomas 			err = ext4_mb_load_buddy(sb, group, &e4b);
2041c9de560dSAlex Tomas 			if (err)
2042c9de560dSAlex Tomas 				goto out;
2043c9de560dSAlex Tomas 
2044c9de560dSAlex Tomas 			ext4_lock_group(sb, group);
2045c9de560dSAlex Tomas 			if (!ext4_mb_good_group(ac, group, cr)) {
2046c9de560dSAlex Tomas 				/* someone did allocation from this group */
2047c9de560dSAlex Tomas 				ext4_unlock_group(sb, group);
2048c9de560dSAlex Tomas 				ext4_mb_release_desc(&e4b);
2049c9de560dSAlex Tomas 				continue;
2050c9de560dSAlex Tomas 			}
2051c9de560dSAlex Tomas 
2052c9de560dSAlex Tomas 			ac->ac_groups_scanned++;
2053c9de560dSAlex Tomas 			desc = ext4_get_group_desc(sb, group, NULL);
205475507efbSTheodore Ts'o 			if (cr == 0)
2055c9de560dSAlex Tomas 				ext4_mb_simple_scan_group(ac, &e4b);
2056c9de560dSAlex Tomas 			else if (cr == 1 &&
2057c9de560dSAlex Tomas 					ac->ac_g_ex.fe_len == sbi->s_stripe)
2058c9de560dSAlex Tomas 				ext4_mb_scan_aligned(ac, &e4b);
2059c9de560dSAlex Tomas 			else
2060c9de560dSAlex Tomas 				ext4_mb_complex_scan_group(ac, &e4b);
2061c9de560dSAlex Tomas 
2062c9de560dSAlex Tomas 			ext4_unlock_group(sb, group);
2063c9de560dSAlex Tomas 			ext4_mb_release_desc(&e4b);
2064c9de560dSAlex Tomas 
2065c9de560dSAlex Tomas 			if (ac->ac_status != AC_STATUS_CONTINUE)
2066c9de560dSAlex Tomas 				break;
2067c9de560dSAlex Tomas 		}
2068c9de560dSAlex Tomas 	}
2069c9de560dSAlex Tomas 
2070c9de560dSAlex Tomas 	if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2071c9de560dSAlex Tomas 	    !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2072c9de560dSAlex Tomas 		/*
2073c9de560dSAlex Tomas 		 * We've been searching too long. Let's try to allocate
2074c9de560dSAlex Tomas 		 * the best chunk we've found so far
2075c9de560dSAlex Tomas 		 */
2076c9de560dSAlex Tomas 
2077c9de560dSAlex Tomas 		ext4_mb_try_best_found(ac, &e4b);
2078c9de560dSAlex Tomas 		if (ac->ac_status != AC_STATUS_FOUND) {
2079c9de560dSAlex Tomas 			/*
2080c9de560dSAlex Tomas 			 * Someone more lucky has already allocated it.
2081c9de560dSAlex Tomas 			 * The only thing we can do is just take first
2082c9de560dSAlex Tomas 			 * found block(s)
2083c9de560dSAlex Tomas 			printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2084c9de560dSAlex Tomas 			 */
2085c9de560dSAlex Tomas 			ac->ac_b_ex.fe_group = 0;
2086c9de560dSAlex Tomas 			ac->ac_b_ex.fe_start = 0;
2087c9de560dSAlex Tomas 			ac->ac_b_ex.fe_len = 0;
2088c9de560dSAlex Tomas 			ac->ac_status = AC_STATUS_CONTINUE;
2089c9de560dSAlex Tomas 			ac->ac_flags |= EXT4_MB_HINT_FIRST;
2090c9de560dSAlex Tomas 			cr = 3;
2091c9de560dSAlex Tomas 			atomic_inc(&sbi->s_mb_lost_chunks);
2092c9de560dSAlex Tomas 			goto repeat;
2093c9de560dSAlex Tomas 		}
2094c9de560dSAlex Tomas 	}
2095c9de560dSAlex Tomas out:
2096c9de560dSAlex Tomas 	return err;
2097c9de560dSAlex Tomas }
2098c9de560dSAlex Tomas 
2099c9de560dSAlex Tomas static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2100c9de560dSAlex Tomas {
2101c9de560dSAlex Tomas 	struct super_block *sb = seq->private;
2102c9de560dSAlex Tomas 	ext4_group_t group;
2103c9de560dSAlex Tomas 
21048df9675fSTheodore Ts'o 	if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
2105c9de560dSAlex Tomas 		return NULL;
2106c9de560dSAlex Tomas 	group = *pos + 1;
2107a9df9a49STheodore Ts'o 	return (void *) ((unsigned long) group);
2108c9de560dSAlex Tomas }
2109c9de560dSAlex Tomas 
2110c9de560dSAlex Tomas static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2111c9de560dSAlex Tomas {
2112c9de560dSAlex Tomas 	struct super_block *sb = seq->private;
2113c9de560dSAlex Tomas 	ext4_group_t group;
2114c9de560dSAlex Tomas 
2115c9de560dSAlex Tomas 	++*pos;
21168df9675fSTheodore Ts'o 	if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
2117c9de560dSAlex Tomas 		return NULL;
2118c9de560dSAlex Tomas 	group = *pos + 1;
2119a9df9a49STheodore Ts'o 	return (void *) ((unsigned long) group);
2120c9de560dSAlex Tomas }
2121c9de560dSAlex Tomas 
2122c9de560dSAlex Tomas static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2123c9de560dSAlex Tomas {
2124c9de560dSAlex Tomas 	struct super_block *sb = seq->private;
2125a9df9a49STheodore Ts'o 	ext4_group_t group = (ext4_group_t) ((unsigned long) v);
2126c9de560dSAlex Tomas 	int i;
2127c9de560dSAlex Tomas 	int err;
2128c9de560dSAlex Tomas 	struct ext4_buddy e4b;
2129c9de560dSAlex Tomas 	struct sg {
2130c9de560dSAlex Tomas 		struct ext4_group_info info;
2131a36b4498SEric Sandeen 		ext4_grpblk_t counters[16];
2132c9de560dSAlex Tomas 	} sg;
2133c9de560dSAlex Tomas 
2134c9de560dSAlex Tomas 	group--;
2135c9de560dSAlex Tomas 	if (group == 0)
2136c9de560dSAlex Tomas 		seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2137c9de560dSAlex Tomas 				"[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2138c9de560dSAlex Tomas 				  "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2139c9de560dSAlex Tomas 			   "group", "free", "frags", "first",
2140c9de560dSAlex Tomas 			   "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2141c9de560dSAlex Tomas 			   "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2142c9de560dSAlex Tomas 
2143c9de560dSAlex Tomas 	i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2144c9de560dSAlex Tomas 		sizeof(struct ext4_group_info);
2145c9de560dSAlex Tomas 	err = ext4_mb_load_buddy(sb, group, &e4b);
2146c9de560dSAlex Tomas 	if (err) {
2147a9df9a49STheodore Ts'o 		seq_printf(seq, "#%-5u: I/O error\n", group);
2148c9de560dSAlex Tomas 		return 0;
2149c9de560dSAlex Tomas 	}
2150c9de560dSAlex Tomas 	ext4_lock_group(sb, group);
2151c9de560dSAlex Tomas 	memcpy(&sg, ext4_get_group_info(sb, group), i);
2152c9de560dSAlex Tomas 	ext4_unlock_group(sb, group);
2153c9de560dSAlex Tomas 	ext4_mb_release_desc(&e4b);
2154c9de560dSAlex Tomas 
2155a9df9a49STheodore Ts'o 	seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
2156c9de560dSAlex Tomas 			sg.info.bb_fragments, sg.info.bb_first_free);
2157c9de560dSAlex Tomas 	for (i = 0; i <= 13; i++)
2158c9de560dSAlex Tomas 		seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2159c9de560dSAlex Tomas 				sg.info.bb_counters[i] : 0);
2160c9de560dSAlex Tomas 	seq_printf(seq, " ]\n");
2161c9de560dSAlex Tomas 
2162c9de560dSAlex Tomas 	return 0;
2163c9de560dSAlex Tomas }
2164c9de560dSAlex Tomas 
2165c9de560dSAlex Tomas static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2166c9de560dSAlex Tomas {
2167c9de560dSAlex Tomas }
2168c9de560dSAlex Tomas 
21697f1346a9STobias Klauser static const struct seq_operations ext4_mb_seq_groups_ops = {
2170c9de560dSAlex Tomas 	.start  = ext4_mb_seq_groups_start,
2171c9de560dSAlex Tomas 	.next   = ext4_mb_seq_groups_next,
2172c9de560dSAlex Tomas 	.stop   = ext4_mb_seq_groups_stop,
2173c9de560dSAlex Tomas 	.show   = ext4_mb_seq_groups_show,
2174c9de560dSAlex Tomas };
2175c9de560dSAlex Tomas 
2176c9de560dSAlex Tomas static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2177c9de560dSAlex Tomas {
2178c9de560dSAlex Tomas 	struct super_block *sb = PDE(inode)->data;
2179c9de560dSAlex Tomas 	int rc;
2180c9de560dSAlex Tomas 
2181c9de560dSAlex Tomas 	rc = seq_open(file, &ext4_mb_seq_groups_ops);
2182c9de560dSAlex Tomas 	if (rc == 0) {
2183c9de560dSAlex Tomas 		struct seq_file *m = (struct seq_file *)file->private_data;
2184c9de560dSAlex Tomas 		m->private = sb;
2185c9de560dSAlex Tomas 	}
2186c9de560dSAlex Tomas 	return rc;
2187c9de560dSAlex Tomas 
2188c9de560dSAlex Tomas }
2189c9de560dSAlex Tomas 
21907f1346a9STobias Klauser static const struct file_operations ext4_mb_seq_groups_fops = {
2191c9de560dSAlex Tomas 	.owner		= THIS_MODULE,
2192c9de560dSAlex Tomas 	.open		= ext4_mb_seq_groups_open,
2193c9de560dSAlex Tomas 	.read		= seq_read,
2194c9de560dSAlex Tomas 	.llseek		= seq_lseek,
2195c9de560dSAlex Tomas 	.release	= seq_release,
2196c9de560dSAlex Tomas };
2197c9de560dSAlex Tomas 
21985f21b0e6SFrederic Bohe 
21995f21b0e6SFrederic Bohe /* Create and initialize ext4_group_info data for the given group. */
2200920313a7SAneesh Kumar K.V int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
22015f21b0e6SFrederic Bohe 			  struct ext4_group_desc *desc)
22025f21b0e6SFrederic Bohe {
22035f21b0e6SFrederic Bohe 	int i, len;
22045f21b0e6SFrederic Bohe 	int metalen = 0;
22055f21b0e6SFrederic Bohe 	struct ext4_sb_info *sbi = EXT4_SB(sb);
22065f21b0e6SFrederic Bohe 	struct ext4_group_info **meta_group_info;
22075f21b0e6SFrederic Bohe 
22085f21b0e6SFrederic Bohe 	/*
22095f21b0e6SFrederic Bohe 	 * First check if this group is the first of a reserved block.
22105f21b0e6SFrederic Bohe 	 * If it's true, we have to allocate a new table of pointers
22115f21b0e6SFrederic Bohe 	 * to ext4_group_info structures
22125f21b0e6SFrederic Bohe 	 */
22135f21b0e6SFrederic Bohe 	if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
22145f21b0e6SFrederic Bohe 		metalen = sizeof(*meta_group_info) <<
22155f21b0e6SFrederic Bohe 			EXT4_DESC_PER_BLOCK_BITS(sb);
22165f21b0e6SFrederic Bohe 		meta_group_info = kmalloc(metalen, GFP_KERNEL);
22175f21b0e6SFrederic Bohe 		if (meta_group_info == NULL) {
22185f21b0e6SFrederic Bohe 			printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
22195f21b0e6SFrederic Bohe 			       "buddy group\n");
22205f21b0e6SFrederic Bohe 			goto exit_meta_group_info;
22215f21b0e6SFrederic Bohe 		}
22225f21b0e6SFrederic Bohe 		sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
22235f21b0e6SFrederic Bohe 			meta_group_info;
22245f21b0e6SFrederic Bohe 	}
22255f21b0e6SFrederic Bohe 
22265f21b0e6SFrederic Bohe 	/*
22275f21b0e6SFrederic Bohe 	 * calculate needed size. if change bb_counters size,
22285f21b0e6SFrederic Bohe 	 * don't forget about ext4_mb_generate_buddy()
22295f21b0e6SFrederic Bohe 	 */
22305f21b0e6SFrederic Bohe 	len = offsetof(typeof(**meta_group_info),
22315f21b0e6SFrederic Bohe 		       bb_counters[sb->s_blocksize_bits + 2]);
22325f21b0e6SFrederic Bohe 
22335f21b0e6SFrederic Bohe 	meta_group_info =
22345f21b0e6SFrederic Bohe 		sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
22355f21b0e6SFrederic Bohe 	i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
22365f21b0e6SFrederic Bohe 
22375f21b0e6SFrederic Bohe 	meta_group_info[i] = kzalloc(len, GFP_KERNEL);
22385f21b0e6SFrederic Bohe 	if (meta_group_info[i] == NULL) {
22395f21b0e6SFrederic Bohe 		printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
22405f21b0e6SFrederic Bohe 		goto exit_group_info;
22415f21b0e6SFrederic Bohe 	}
22425f21b0e6SFrederic Bohe 	set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
22435f21b0e6SFrederic Bohe 		&(meta_group_info[i]->bb_state));
22445f21b0e6SFrederic Bohe 
22455f21b0e6SFrederic Bohe 	/*
22465f21b0e6SFrederic Bohe 	 * initialize bb_free to be able to skip
22475f21b0e6SFrederic Bohe 	 * empty groups without initialization
22485f21b0e6SFrederic Bohe 	 */
22495f21b0e6SFrederic Bohe 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
22505f21b0e6SFrederic Bohe 		meta_group_info[i]->bb_free =
22515f21b0e6SFrederic Bohe 			ext4_free_blocks_after_init(sb, group, desc);
22525f21b0e6SFrederic Bohe 	} else {
22535f21b0e6SFrederic Bohe 		meta_group_info[i]->bb_free =
2254560671a0SAneesh Kumar K.V 			ext4_free_blks_count(sb, desc);
22555f21b0e6SFrederic Bohe 	}
22565f21b0e6SFrederic Bohe 
22575f21b0e6SFrederic Bohe 	INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
2258920313a7SAneesh Kumar K.V 	init_rwsem(&meta_group_info[i]->alloc_sem);
22595a4a7989SJoe Perches 	meta_group_info[i]->bb_free_root.rb_node = NULL;
22605f21b0e6SFrederic Bohe 
22615f21b0e6SFrederic Bohe #ifdef DOUBLE_CHECK
22625f21b0e6SFrederic Bohe 	{
22635f21b0e6SFrederic Bohe 		struct buffer_head *bh;
22645f21b0e6SFrederic Bohe 		meta_group_info[i]->bb_bitmap =
22655f21b0e6SFrederic Bohe 			kmalloc(sb->s_blocksize, GFP_KERNEL);
22665f21b0e6SFrederic Bohe 		BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
22675f21b0e6SFrederic Bohe 		bh = ext4_read_block_bitmap(sb, group);
22685f21b0e6SFrederic Bohe 		BUG_ON(bh == NULL);
22695f21b0e6SFrederic Bohe 		memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
22705f21b0e6SFrederic Bohe 			sb->s_blocksize);
22715f21b0e6SFrederic Bohe 		put_bh(bh);
22725f21b0e6SFrederic Bohe 	}
22735f21b0e6SFrederic Bohe #endif
22745f21b0e6SFrederic Bohe 
22755f21b0e6SFrederic Bohe 	return 0;
22765f21b0e6SFrederic Bohe 
22775f21b0e6SFrederic Bohe exit_group_info:
22785f21b0e6SFrederic Bohe 	/* If a meta_group_info table has been allocated, release it now */
22795f21b0e6SFrederic Bohe 	if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
22805f21b0e6SFrederic Bohe 		kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
22815f21b0e6SFrederic Bohe exit_meta_group_info:
22825f21b0e6SFrederic Bohe 	return -ENOMEM;
22835f21b0e6SFrederic Bohe } /* ext4_mb_add_groupinfo */
22845f21b0e6SFrederic Bohe 
2285c9de560dSAlex Tomas static int ext4_mb_init_backend(struct super_block *sb)
2286c9de560dSAlex Tomas {
22878df9675fSTheodore Ts'o 	ext4_group_t ngroups = ext4_get_groups_count(sb);
2288c9de560dSAlex Tomas 	ext4_group_t i;
2289c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
22905f21b0e6SFrederic Bohe 	struct ext4_super_block *es = sbi->s_es;
22915f21b0e6SFrederic Bohe 	int num_meta_group_infos;
22925f21b0e6SFrederic Bohe 	int num_meta_group_infos_max;
22935f21b0e6SFrederic Bohe 	int array_size;
22945f21b0e6SFrederic Bohe 	struct ext4_group_desc *desc;
2295c9de560dSAlex Tomas 
22965f21b0e6SFrederic Bohe 	/* This is the number of blocks used by GDT */
22978df9675fSTheodore Ts'o 	num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
22985f21b0e6SFrederic Bohe 				1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
22995f21b0e6SFrederic Bohe 
23005f21b0e6SFrederic Bohe 	/*
23015f21b0e6SFrederic Bohe 	 * This is the total number of blocks used by GDT including
23025f21b0e6SFrederic Bohe 	 * the number of reserved blocks for GDT.
23035f21b0e6SFrederic Bohe 	 * The s_group_info array is allocated with this value
23045f21b0e6SFrederic Bohe 	 * to allow a clean online resize without a complex
23055f21b0e6SFrederic Bohe 	 * manipulation of pointer.
23065f21b0e6SFrederic Bohe 	 * The drawback is the unused memory when no resize
23075f21b0e6SFrederic Bohe 	 * occurs but it's very low in terms of pages
23085f21b0e6SFrederic Bohe 	 * (see comments below)
23095f21b0e6SFrederic Bohe 	 * Need to handle this properly when META_BG resizing is allowed
23105f21b0e6SFrederic Bohe 	 */
23115f21b0e6SFrederic Bohe 	num_meta_group_infos_max = num_meta_group_infos +
23125f21b0e6SFrederic Bohe 				le16_to_cpu(es->s_reserved_gdt_blocks);
23135f21b0e6SFrederic Bohe 
23145f21b0e6SFrederic Bohe 	/*
23155f21b0e6SFrederic Bohe 	 * array_size is the size of s_group_info array. We round it
23165f21b0e6SFrederic Bohe 	 * to the next power of two because this approximation is done
23175f21b0e6SFrederic Bohe 	 * internally by kmalloc so we can have some more memory
23185f21b0e6SFrederic Bohe 	 * for free here (e.g. may be used for META_BG resize).
23195f21b0e6SFrederic Bohe 	 */
23205f21b0e6SFrederic Bohe 	array_size = 1;
23215f21b0e6SFrederic Bohe 	while (array_size < sizeof(*sbi->s_group_info) *
23225f21b0e6SFrederic Bohe 	       num_meta_group_infos_max)
23235f21b0e6SFrederic Bohe 		array_size = array_size << 1;
2324c9de560dSAlex Tomas 	/* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2325c9de560dSAlex Tomas 	 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2326c9de560dSAlex Tomas 	 * So a two level scheme suffices for now. */
23275f21b0e6SFrederic Bohe 	sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
2328c9de560dSAlex Tomas 	if (sbi->s_group_info == NULL) {
2329c9de560dSAlex Tomas 		printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2330c9de560dSAlex Tomas 		return -ENOMEM;
2331c9de560dSAlex Tomas 	}
2332c9de560dSAlex Tomas 	sbi->s_buddy_cache = new_inode(sb);
2333c9de560dSAlex Tomas 	if (sbi->s_buddy_cache == NULL) {
2334c9de560dSAlex Tomas 		printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2335c9de560dSAlex Tomas 		goto err_freesgi;
2336c9de560dSAlex Tomas 	}
2337c9de560dSAlex Tomas 	EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
23388df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
2339c9de560dSAlex Tomas 		desc = ext4_get_group_desc(sb, i, NULL);
2340c9de560dSAlex Tomas 		if (desc == NULL) {
2341c9de560dSAlex Tomas 			printk(KERN_ERR
2342a9df9a49STheodore Ts'o 				"EXT4-fs: can't read descriptor %u\n", i);
2343c9de560dSAlex Tomas 			goto err_freebuddy;
2344c9de560dSAlex Tomas 		}
23455f21b0e6SFrederic Bohe 		if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
23465f21b0e6SFrederic Bohe 			goto err_freebuddy;
2347c9de560dSAlex Tomas 	}
2348c9de560dSAlex Tomas 
2349c9de560dSAlex Tomas 	return 0;
2350c9de560dSAlex Tomas 
2351c9de560dSAlex Tomas err_freebuddy:
2352f1fa3342SRoel Kluin 	while (i-- > 0)
2353c9de560dSAlex Tomas 		kfree(ext4_get_group_info(sb, i));
2354c9de560dSAlex Tomas 	i = num_meta_group_infos;
2355f1fa3342SRoel Kluin 	while (i-- > 0)
2356c9de560dSAlex Tomas 		kfree(sbi->s_group_info[i]);
2357c9de560dSAlex Tomas 	iput(sbi->s_buddy_cache);
2358c9de560dSAlex Tomas err_freesgi:
2359c9de560dSAlex Tomas 	kfree(sbi->s_group_info);
2360c9de560dSAlex Tomas 	return -ENOMEM;
2361c9de560dSAlex Tomas }
2362c9de560dSAlex Tomas 
2363c9de560dSAlex Tomas int ext4_mb_init(struct super_block *sb, int needs_recovery)
2364c9de560dSAlex Tomas {
2365c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
23666be2ded1SAneesh Kumar K.V 	unsigned i, j;
2367c9de560dSAlex Tomas 	unsigned offset;
2368c9de560dSAlex Tomas 	unsigned max;
236974767c5aSShen Feng 	int ret;
2370c9de560dSAlex Tomas 
23711927805eSEric Sandeen 	i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
2372c9de560dSAlex Tomas 
2373c9de560dSAlex Tomas 	sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2374c9de560dSAlex Tomas 	if (sbi->s_mb_offsets == NULL) {
2375c9de560dSAlex Tomas 		return -ENOMEM;
2376c9de560dSAlex Tomas 	}
2377ff7ef329SYasunori Goto 
23781927805eSEric Sandeen 	i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
2379c9de560dSAlex Tomas 	sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2380c9de560dSAlex Tomas 	if (sbi->s_mb_maxs == NULL) {
2381a7b19448SDan Carpenter 		kfree(sbi->s_mb_offsets);
2382c9de560dSAlex Tomas 		return -ENOMEM;
2383c9de560dSAlex Tomas 	}
2384c9de560dSAlex Tomas 
2385c9de560dSAlex Tomas 	/* order 0 is regular bitmap */
2386c9de560dSAlex Tomas 	sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2387c9de560dSAlex Tomas 	sbi->s_mb_offsets[0] = 0;
2388c9de560dSAlex Tomas 
2389c9de560dSAlex Tomas 	i = 1;
2390c9de560dSAlex Tomas 	offset = 0;
2391c9de560dSAlex Tomas 	max = sb->s_blocksize << 2;
2392c9de560dSAlex Tomas 	do {
2393c9de560dSAlex Tomas 		sbi->s_mb_offsets[i] = offset;
2394c9de560dSAlex Tomas 		sbi->s_mb_maxs[i] = max;
2395c9de560dSAlex Tomas 		offset += 1 << (sb->s_blocksize_bits - i);
2396c9de560dSAlex Tomas 		max = max >> 1;
2397c9de560dSAlex Tomas 		i++;
2398c9de560dSAlex Tomas 	} while (i <= sb->s_blocksize_bits + 1);
2399c9de560dSAlex Tomas 
2400c9de560dSAlex Tomas 	/* init file for buddy data */
240174767c5aSShen Feng 	ret = ext4_mb_init_backend(sb);
240274767c5aSShen Feng 	if (ret != 0) {
2403c9de560dSAlex Tomas 		kfree(sbi->s_mb_offsets);
2404c9de560dSAlex Tomas 		kfree(sbi->s_mb_maxs);
240574767c5aSShen Feng 		return ret;
2406c9de560dSAlex Tomas 	}
2407c9de560dSAlex Tomas 
2408c9de560dSAlex Tomas 	spin_lock_init(&sbi->s_md_lock);
2409c9de560dSAlex Tomas 	spin_lock_init(&sbi->s_bal_lock);
2410c9de560dSAlex Tomas 
2411c9de560dSAlex Tomas 	sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2412c9de560dSAlex Tomas 	sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2413c9de560dSAlex Tomas 	sbi->s_mb_stats = MB_DEFAULT_STATS;
2414c9de560dSAlex Tomas 	sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2415c9de560dSAlex Tomas 	sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2416c9de560dSAlex Tomas 	sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2417c9de560dSAlex Tomas 
2418730c213cSEric Sandeen 	sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
2419c9de560dSAlex Tomas 	if (sbi->s_locality_groups == NULL) {
2420c9de560dSAlex Tomas 		kfree(sbi->s_mb_offsets);
2421c9de560dSAlex Tomas 		kfree(sbi->s_mb_maxs);
2422c9de560dSAlex Tomas 		return -ENOMEM;
2423c9de560dSAlex Tomas 	}
2424730c213cSEric Sandeen 	for_each_possible_cpu(i) {
2425c9de560dSAlex Tomas 		struct ext4_locality_group *lg;
2426730c213cSEric Sandeen 		lg = per_cpu_ptr(sbi->s_locality_groups, i);
2427c9de560dSAlex Tomas 		mutex_init(&lg->lg_mutex);
24286be2ded1SAneesh Kumar K.V 		for (j = 0; j < PREALLOC_TB_SIZE; j++)
24296be2ded1SAneesh Kumar K.V 			INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
2430c9de560dSAlex Tomas 		spin_lock_init(&lg->lg_prealloc_lock);
2431c9de560dSAlex Tomas 	}
2432c9de560dSAlex Tomas 
2433*296c355cSTheodore Ts'o 	if (sbi->s_proc)
2434*296c355cSTheodore Ts'o 		proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2435*296c355cSTheodore Ts'o 				 &ext4_mb_seq_groups_fops, sb);
2436c9de560dSAlex Tomas 
24370390131bSFrank Mayhar 	if (sbi->s_journal)
24383e624fc7STheodore Ts'o 		sbi->s_journal->j_commit_callback = release_blocks_on_commit;
2439c9de560dSAlex Tomas 	return 0;
2440c9de560dSAlex Tomas }
2441c9de560dSAlex Tomas 
2442955ce5f5SAneesh Kumar K.V /* need to called with the ext4 group lock held */
2443c9de560dSAlex Tomas static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2444c9de560dSAlex Tomas {
2445c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
2446c9de560dSAlex Tomas 	struct list_head *cur, *tmp;
2447c9de560dSAlex Tomas 	int count = 0;
2448c9de560dSAlex Tomas 
2449c9de560dSAlex Tomas 	list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2450c9de560dSAlex Tomas 		pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2451c9de560dSAlex Tomas 		list_del(&pa->pa_group_list);
2452c9de560dSAlex Tomas 		count++;
2453688f05a0SAneesh Kumar K.V 		kmem_cache_free(ext4_pspace_cachep, pa);
2454c9de560dSAlex Tomas 	}
2455c9de560dSAlex Tomas 	if (count)
24566ba495e9STheodore Ts'o 		mb_debug(1, "mballoc: %u PAs left\n", count);
2457c9de560dSAlex Tomas 
2458c9de560dSAlex Tomas }
2459c9de560dSAlex Tomas 
2460c9de560dSAlex Tomas int ext4_mb_release(struct super_block *sb)
2461c9de560dSAlex Tomas {
24628df9675fSTheodore Ts'o 	ext4_group_t ngroups = ext4_get_groups_count(sb);
2463c9de560dSAlex Tomas 	ext4_group_t i;
2464c9de560dSAlex Tomas 	int num_meta_group_infos;
2465c9de560dSAlex Tomas 	struct ext4_group_info *grinfo;
2466c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2467c9de560dSAlex Tomas 
2468c9de560dSAlex Tomas 	if (sbi->s_group_info) {
24698df9675fSTheodore Ts'o 		for (i = 0; i < ngroups; i++) {
2470c9de560dSAlex Tomas 			grinfo = ext4_get_group_info(sb, i);
2471c9de560dSAlex Tomas #ifdef DOUBLE_CHECK
2472c9de560dSAlex Tomas 			kfree(grinfo->bb_bitmap);
2473c9de560dSAlex Tomas #endif
2474c9de560dSAlex Tomas 			ext4_lock_group(sb, i);
2475c9de560dSAlex Tomas 			ext4_mb_cleanup_pa(grinfo);
2476c9de560dSAlex Tomas 			ext4_unlock_group(sb, i);
2477c9de560dSAlex Tomas 			kfree(grinfo);
2478c9de560dSAlex Tomas 		}
24798df9675fSTheodore Ts'o 		num_meta_group_infos = (ngroups +
2480c9de560dSAlex Tomas 				EXT4_DESC_PER_BLOCK(sb) - 1) >>
2481c9de560dSAlex Tomas 			EXT4_DESC_PER_BLOCK_BITS(sb);
2482c9de560dSAlex Tomas 		for (i = 0; i < num_meta_group_infos; i++)
2483c9de560dSAlex Tomas 			kfree(sbi->s_group_info[i]);
2484c9de560dSAlex Tomas 		kfree(sbi->s_group_info);
2485c9de560dSAlex Tomas 	}
2486c9de560dSAlex Tomas 	kfree(sbi->s_mb_offsets);
2487c9de560dSAlex Tomas 	kfree(sbi->s_mb_maxs);
2488c9de560dSAlex Tomas 	if (sbi->s_buddy_cache)
2489c9de560dSAlex Tomas 		iput(sbi->s_buddy_cache);
2490c9de560dSAlex Tomas 	if (sbi->s_mb_stats) {
2491c9de560dSAlex Tomas 		printk(KERN_INFO
2492c9de560dSAlex Tomas 		       "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2493c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_allocated),
2494c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_reqs),
2495c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_success));
2496c9de560dSAlex Tomas 		printk(KERN_INFO
2497c9de560dSAlex Tomas 		      "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2498c9de560dSAlex Tomas 				"%u 2^N hits, %u breaks, %u lost\n",
2499c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_ex_scanned),
2500c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_goals),
2501c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_2orders),
2502c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_breaks),
2503c9de560dSAlex Tomas 				atomic_read(&sbi->s_mb_lost_chunks));
2504c9de560dSAlex Tomas 		printk(KERN_INFO
2505c9de560dSAlex Tomas 		       "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2506c9de560dSAlex Tomas 				sbi->s_mb_buddies_generated++,
2507c9de560dSAlex Tomas 				sbi->s_mb_generation_time);
2508c9de560dSAlex Tomas 		printk(KERN_INFO
2509c9de560dSAlex Tomas 		       "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2510c9de560dSAlex Tomas 				atomic_read(&sbi->s_mb_preallocated),
2511c9de560dSAlex Tomas 				atomic_read(&sbi->s_mb_discarded));
2512c9de560dSAlex Tomas 	}
2513c9de560dSAlex Tomas 
2514730c213cSEric Sandeen 	free_percpu(sbi->s_locality_groups);
2515*296c355cSTheodore Ts'o 	if (sbi->s_proc)
2516*296c355cSTheodore Ts'o 		remove_proc_entry("mb_groups", sbi->s_proc);
2517c9de560dSAlex Tomas 
2518c9de560dSAlex Tomas 	return 0;
2519c9de560dSAlex Tomas }
2520c9de560dSAlex Tomas 
25213e624fc7STheodore Ts'o /*
25223e624fc7STheodore Ts'o  * This function is called by the jbd2 layer once the commit has finished,
25233e624fc7STheodore Ts'o  * so we know we can free the blocks that were released with that commit.
25243e624fc7STheodore Ts'o  */
25253e624fc7STheodore Ts'o static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
2526c9de560dSAlex Tomas {
25273e624fc7STheodore Ts'o 	struct super_block *sb = journal->j_private;
2528c9de560dSAlex Tomas 	struct ext4_buddy e4b;
2529c894058dSAneesh Kumar K.V 	struct ext4_group_info *db;
2530c894058dSAneesh Kumar K.V 	int err, count = 0, count2 = 0;
2531c894058dSAneesh Kumar K.V 	struct ext4_free_data *entry;
25328a0aba73STheodore Ts'o 	ext4_fsblk_t discard_block;
25333e624fc7STheodore Ts'o 	struct list_head *l, *ltmp;
2534c9de560dSAlex Tomas 
25353e624fc7STheodore Ts'o 	list_for_each_safe(l, ltmp, &txn->t_private_list) {
25363e624fc7STheodore Ts'o 		entry = list_entry(l, struct ext4_free_data, list);
2537c9de560dSAlex Tomas 
25386ba495e9STheodore Ts'o 		mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2539c894058dSAneesh Kumar K.V 			 entry->count, entry->group, entry);
2540c9de560dSAlex Tomas 
2541c894058dSAneesh Kumar K.V 		err = ext4_mb_load_buddy(sb, entry->group, &e4b);
2542c9de560dSAlex Tomas 		/* we expect to find existing buddy because it's pinned */
2543c9de560dSAlex Tomas 		BUG_ON(err != 0);
2544c9de560dSAlex Tomas 
2545c894058dSAneesh Kumar K.V 		db = e4b.bd_info;
2546c9de560dSAlex Tomas 		/* there are blocks to put in buddy to make them really free */
2547c894058dSAneesh Kumar K.V 		count += entry->count;
2548c9de560dSAlex Tomas 		count2++;
2549c894058dSAneesh Kumar K.V 		ext4_lock_group(sb, entry->group);
2550c894058dSAneesh Kumar K.V 		/* Take it out of per group rb tree */
2551c894058dSAneesh Kumar K.V 		rb_erase(&entry->node, &(db->bb_free_root));
2552c894058dSAneesh Kumar K.V 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2553c9de560dSAlex Tomas 
2554c894058dSAneesh Kumar K.V 		if (!db->bb_free_root.rb_node) {
2555c894058dSAneesh Kumar K.V 			/* No more items in the per group rb tree
2556c894058dSAneesh Kumar K.V 			 * balance refcounts from ext4_mb_free_metadata()
2557c894058dSAneesh Kumar K.V 			 */
2558c9de560dSAlex Tomas 			page_cache_release(e4b.bd_buddy_page);
2559c9de560dSAlex Tomas 			page_cache_release(e4b.bd_bitmap_page);
2560c894058dSAneesh Kumar K.V 		}
2561c894058dSAneesh Kumar K.V 		ext4_unlock_group(sb, entry->group);
25628a0aba73STheodore Ts'o 		discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
25638a0aba73STheodore Ts'o 			+ entry->start_blk
25648a0aba73STheodore Ts'o 			+ le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
25659bffad1eSTheodore Ts'o 		trace_ext4_discard_blocks(sb, (unsigned long long)discard_block,
2566ba80b101STheodore Ts'o 					  entry->count);
25678a0aba73STheodore Ts'o 		sb_issue_discard(sb, discard_block, entry->count);
2568c9de560dSAlex Tomas 
2569c894058dSAneesh Kumar K.V 		kmem_cache_free(ext4_free_ext_cachep, entry);
2570c9de560dSAlex Tomas 		ext4_mb_release_desc(&e4b);
25713e624fc7STheodore Ts'o 	}
2572c9de560dSAlex Tomas 
25736ba495e9STheodore Ts'o 	mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
2574c9de560dSAlex Tomas }
2575c9de560dSAlex Tomas 
25766ba495e9STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
25776ba495e9STheodore Ts'o u8 mb_enable_debug __read_mostly;
25786ba495e9STheodore Ts'o 
25796ba495e9STheodore Ts'o static struct dentry *debugfs_dir;
25806ba495e9STheodore Ts'o static struct dentry *debugfs_debug;
25816ba495e9STheodore Ts'o 
25826ba495e9STheodore Ts'o static void __init ext4_create_debugfs_entry(void)
25836ba495e9STheodore Ts'o {
25846ba495e9STheodore Ts'o 	debugfs_dir = debugfs_create_dir("ext4", NULL);
25856ba495e9STheodore Ts'o 	if (debugfs_dir)
25866ba495e9STheodore Ts'o 		debugfs_debug = debugfs_create_u8("mballoc-debug",
25876ba495e9STheodore Ts'o 						  S_IRUGO | S_IWUSR,
25886ba495e9STheodore Ts'o 						  debugfs_dir,
25896ba495e9STheodore Ts'o 						  &mb_enable_debug);
25906ba495e9STheodore Ts'o }
25916ba495e9STheodore Ts'o 
25926ba495e9STheodore Ts'o static void ext4_remove_debugfs_entry(void)
25936ba495e9STheodore Ts'o {
25946ba495e9STheodore Ts'o 	debugfs_remove(debugfs_debug);
25956ba495e9STheodore Ts'o 	debugfs_remove(debugfs_dir);
25966ba495e9STheodore Ts'o }
25976ba495e9STheodore Ts'o 
25986ba495e9STheodore Ts'o #else
25996ba495e9STheodore Ts'o 
26006ba495e9STheodore Ts'o static void __init ext4_create_debugfs_entry(void)
26016ba495e9STheodore Ts'o {
26026ba495e9STheodore Ts'o }
26036ba495e9STheodore Ts'o 
26046ba495e9STheodore Ts'o static void ext4_remove_debugfs_entry(void)
26056ba495e9STheodore Ts'o {
26066ba495e9STheodore Ts'o }
26076ba495e9STheodore Ts'o 
26086ba495e9STheodore Ts'o #endif
26096ba495e9STheodore Ts'o 
2610c9de560dSAlex Tomas int __init init_ext4_mballoc(void)
2611c9de560dSAlex Tomas {
2612c9de560dSAlex Tomas 	ext4_pspace_cachep =
2613c9de560dSAlex Tomas 		kmem_cache_create("ext4_prealloc_space",
2614c9de560dSAlex Tomas 				     sizeof(struct ext4_prealloc_space),
2615c9de560dSAlex Tomas 				     0, SLAB_RECLAIM_ACCOUNT, NULL);
2616c9de560dSAlex Tomas 	if (ext4_pspace_cachep == NULL)
2617c9de560dSAlex Tomas 		return -ENOMEM;
2618c9de560dSAlex Tomas 
2619256bdb49SEric Sandeen 	ext4_ac_cachep =
2620256bdb49SEric Sandeen 		kmem_cache_create("ext4_alloc_context",
2621256bdb49SEric Sandeen 				     sizeof(struct ext4_allocation_context),
2622256bdb49SEric Sandeen 				     0, SLAB_RECLAIM_ACCOUNT, NULL);
2623256bdb49SEric Sandeen 	if (ext4_ac_cachep == NULL) {
2624256bdb49SEric Sandeen 		kmem_cache_destroy(ext4_pspace_cachep);
2625256bdb49SEric Sandeen 		return -ENOMEM;
2626256bdb49SEric Sandeen 	}
2627c894058dSAneesh Kumar K.V 
2628c894058dSAneesh Kumar K.V 	ext4_free_ext_cachep =
2629c894058dSAneesh Kumar K.V 		kmem_cache_create("ext4_free_block_extents",
2630c894058dSAneesh Kumar K.V 				     sizeof(struct ext4_free_data),
2631c894058dSAneesh Kumar K.V 				     0, SLAB_RECLAIM_ACCOUNT, NULL);
2632c894058dSAneesh Kumar K.V 	if (ext4_free_ext_cachep == NULL) {
2633c894058dSAneesh Kumar K.V 		kmem_cache_destroy(ext4_pspace_cachep);
2634c894058dSAneesh Kumar K.V 		kmem_cache_destroy(ext4_ac_cachep);
2635c894058dSAneesh Kumar K.V 		return -ENOMEM;
2636c894058dSAneesh Kumar K.V 	}
26376ba495e9STheodore Ts'o 	ext4_create_debugfs_entry();
2638c9de560dSAlex Tomas 	return 0;
2639c9de560dSAlex Tomas }
2640c9de560dSAlex Tomas 
2641c9de560dSAlex Tomas void exit_ext4_mballoc(void)
2642c9de560dSAlex Tomas {
26433e03f9caSJesper Dangaard Brouer 	/*
26443e03f9caSJesper Dangaard Brouer 	 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
26453e03f9caSJesper Dangaard Brouer 	 * before destroying the slab cache.
26463e03f9caSJesper Dangaard Brouer 	 */
26473e03f9caSJesper Dangaard Brouer 	rcu_barrier();
2648c9de560dSAlex Tomas 	kmem_cache_destroy(ext4_pspace_cachep);
2649256bdb49SEric Sandeen 	kmem_cache_destroy(ext4_ac_cachep);
2650c894058dSAneesh Kumar K.V 	kmem_cache_destroy(ext4_free_ext_cachep);
26516ba495e9STheodore Ts'o 	ext4_remove_debugfs_entry();
2652c9de560dSAlex Tomas }
2653c9de560dSAlex Tomas 
2654c9de560dSAlex Tomas 
2655c9de560dSAlex Tomas /*
2656c9de560dSAlex Tomas  * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2657c9de560dSAlex Tomas  * Returns 0 if success or error code
2658c9de560dSAlex Tomas  */
26594ddfef7bSEric Sandeen static noinline_for_stack int
26604ddfef7bSEric Sandeen ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
2661498e5f24STheodore Ts'o 				handle_t *handle, unsigned int reserv_blks)
2662c9de560dSAlex Tomas {
2663c9de560dSAlex Tomas 	struct buffer_head *bitmap_bh = NULL;
2664c9de560dSAlex Tomas 	struct ext4_super_block *es;
2665c9de560dSAlex Tomas 	struct ext4_group_desc *gdp;
2666c9de560dSAlex Tomas 	struct buffer_head *gdp_bh;
2667c9de560dSAlex Tomas 	struct ext4_sb_info *sbi;
2668c9de560dSAlex Tomas 	struct super_block *sb;
2669c9de560dSAlex Tomas 	ext4_fsblk_t block;
2670519deca0SAneesh Kumar K.V 	int err, len;
2671c9de560dSAlex Tomas 
2672c9de560dSAlex Tomas 	BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2673c9de560dSAlex Tomas 	BUG_ON(ac->ac_b_ex.fe_len <= 0);
2674c9de560dSAlex Tomas 
2675c9de560dSAlex Tomas 	sb = ac->ac_sb;
2676c9de560dSAlex Tomas 	sbi = EXT4_SB(sb);
2677c9de560dSAlex Tomas 	es = sbi->s_es;
2678c9de560dSAlex Tomas 
2679c9de560dSAlex Tomas 
2680c9de560dSAlex Tomas 	err = -EIO;
2681574ca174STheodore Ts'o 	bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
2682c9de560dSAlex Tomas 	if (!bitmap_bh)
2683c9de560dSAlex Tomas 		goto out_err;
2684c9de560dSAlex Tomas 
2685c9de560dSAlex Tomas 	err = ext4_journal_get_write_access(handle, bitmap_bh);
2686c9de560dSAlex Tomas 	if (err)
2687c9de560dSAlex Tomas 		goto out_err;
2688c9de560dSAlex Tomas 
2689c9de560dSAlex Tomas 	err = -EIO;
2690c9de560dSAlex Tomas 	gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2691c9de560dSAlex Tomas 	if (!gdp)
2692c9de560dSAlex Tomas 		goto out_err;
2693c9de560dSAlex Tomas 
2694a9df9a49STheodore Ts'o 	ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
26959fd9784cSThadeu Lima de Souza Cascardo 			ext4_free_blks_count(sb, gdp));
269603cddb80SAneesh Kumar K.V 
2697c9de560dSAlex Tomas 	err = ext4_journal_get_write_access(handle, gdp_bh);
2698c9de560dSAlex Tomas 	if (err)
2699c9de560dSAlex Tomas 		goto out_err;
2700c9de560dSAlex Tomas 
2701c9de560dSAlex Tomas 	block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
2702c9de560dSAlex Tomas 		+ ac->ac_b_ex.fe_start
2703c9de560dSAlex Tomas 		+ le32_to_cpu(es->s_first_data_block);
2704c9de560dSAlex Tomas 
2705519deca0SAneesh Kumar K.V 	len = ac->ac_b_ex.fe_len;
27066fd058f7STheodore Ts'o 	if (!ext4_data_block_valid(sbi, block, len)) {
270746e665e9SHarvey Harrison 		ext4_error(sb, __func__,
27086fd058f7STheodore Ts'o 			   "Allocating blocks %llu-%llu which overlap "
27096fd058f7STheodore Ts'o 			   "fs metadata\n", block, block+len);
2710519deca0SAneesh Kumar K.V 		/* File system mounted not to panic on error
2711519deca0SAneesh Kumar K.V 		 * Fix the bitmap and repeat the block allocation
2712519deca0SAneesh Kumar K.V 		 * We leak some of the blocks here.
2713519deca0SAneesh Kumar K.V 		 */
2714955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2715955ce5f5SAneesh Kumar K.V 		mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2716519deca0SAneesh Kumar K.V 			    ac->ac_b_ex.fe_len);
2717955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
27180390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
2719519deca0SAneesh Kumar K.V 		if (!err)
2720519deca0SAneesh Kumar K.V 			err = -EAGAIN;
2721519deca0SAneesh Kumar K.V 		goto out_err;
2722c9de560dSAlex Tomas 	}
2723955ce5f5SAneesh Kumar K.V 
2724955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2725c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
2726c9de560dSAlex Tomas 	{
2727c9de560dSAlex Tomas 		int i;
2728c9de560dSAlex Tomas 		for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2729c9de560dSAlex Tomas 			BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2730c9de560dSAlex Tomas 						bitmap_bh->b_data));
2731c9de560dSAlex Tomas 		}
2732c9de560dSAlex Tomas 	}
2733c9de560dSAlex Tomas #endif
2734955ce5f5SAneesh Kumar K.V 	mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
2735c9de560dSAlex Tomas 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2736c9de560dSAlex Tomas 		gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
2737560671a0SAneesh Kumar K.V 		ext4_free_blks_set(sb, gdp,
2738560671a0SAneesh Kumar K.V 					ext4_free_blocks_after_init(sb,
2739560671a0SAneesh Kumar K.V 					ac->ac_b_ex.fe_group, gdp));
2740c9de560dSAlex Tomas 	}
2741560671a0SAneesh Kumar K.V 	len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2742560671a0SAneesh Kumar K.V 	ext4_free_blks_set(sb, gdp, len);
2743c9de560dSAlex Tomas 	gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
2744955ce5f5SAneesh Kumar K.V 
2745955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
27466bc6e63fSAneesh Kumar K.V 	percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
2747d2a17637SMingming Cao 	/*
27486bc6e63fSAneesh Kumar K.V 	 * Now reduce the dirty block count also. Should not go negative
2749d2a17637SMingming Cao 	 */
27506bc6e63fSAneesh Kumar K.V 	if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
27516bc6e63fSAneesh Kumar K.V 		/* release all the reserved blocks if non delalloc */
27526bc6e63fSAneesh Kumar K.V 		percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
275360e58e0fSMingming Cao 	else {
27546bc6e63fSAneesh Kumar K.V 		percpu_counter_sub(&sbi->s_dirtyblocks_counter,
27556bc6e63fSAneesh Kumar K.V 						ac->ac_b_ex.fe_len);
275660e58e0fSMingming Cao 		/* convert reserved quota blocks to real quota blocks */
275760e58e0fSMingming Cao 		vfs_dq_claim_block(ac->ac_inode, ac->ac_b_ex.fe_len);
275860e58e0fSMingming Cao 	}
2759c9de560dSAlex Tomas 
2760772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
2761772cb7c8SJose R. Santos 		ext4_group_t flex_group = ext4_flex_group(sbi,
2762772cb7c8SJose R. Santos 							  ac->ac_b_ex.fe_group);
27639f24e420STheodore Ts'o 		atomic_sub(ac->ac_b_ex.fe_len,
27649f24e420STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_blocks);
2765772cb7c8SJose R. Santos 	}
2766772cb7c8SJose R. Santos 
27670390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
2768c9de560dSAlex Tomas 	if (err)
2769c9de560dSAlex Tomas 		goto out_err;
27700390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
2771c9de560dSAlex Tomas 
2772c9de560dSAlex Tomas out_err:
2773c9de560dSAlex Tomas 	sb->s_dirt = 1;
277442a10addSAneesh Kumar K.V 	brelse(bitmap_bh);
2775c9de560dSAlex Tomas 	return err;
2776c9de560dSAlex Tomas }
2777c9de560dSAlex Tomas 
2778c9de560dSAlex Tomas /*
2779c9de560dSAlex Tomas  * here we normalize request for locality group
2780c9de560dSAlex Tomas  * Group request are normalized to s_strip size if we set the same via mount
2781c9de560dSAlex Tomas  * option. If not we set it to s_mb_group_prealloc which can be configured via
2782b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_group_prealloc
2783c9de560dSAlex Tomas  *
2784c9de560dSAlex Tomas  * XXX: should we try to preallocate more than the group has now?
2785c9de560dSAlex Tomas  */
2786c9de560dSAlex Tomas static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2787c9de560dSAlex Tomas {
2788c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
2789c9de560dSAlex Tomas 	struct ext4_locality_group *lg = ac->ac_lg;
2790c9de560dSAlex Tomas 
2791c9de560dSAlex Tomas 	BUG_ON(lg == NULL);
2792c9de560dSAlex Tomas 	if (EXT4_SB(sb)->s_stripe)
2793c9de560dSAlex Tomas 		ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2794c9de560dSAlex Tomas 	else
2795c9de560dSAlex Tomas 		ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
27966ba495e9STheodore Ts'o 	mb_debug(1, "#%u: goal %u blocks for locality group\n",
2797c9de560dSAlex Tomas 		current->pid, ac->ac_g_ex.fe_len);
2798c9de560dSAlex Tomas }
2799c9de560dSAlex Tomas 
2800c9de560dSAlex Tomas /*
2801c9de560dSAlex Tomas  * Normalization means making request better in terms of
2802c9de560dSAlex Tomas  * size and alignment
2803c9de560dSAlex Tomas  */
28044ddfef7bSEric Sandeen static noinline_for_stack void
28054ddfef7bSEric Sandeen ext4_mb_normalize_request(struct ext4_allocation_context *ac,
2806c9de560dSAlex Tomas 				struct ext4_allocation_request *ar)
2807c9de560dSAlex Tomas {
2808c9de560dSAlex Tomas 	int bsbits, max;
2809c9de560dSAlex Tomas 	ext4_lblk_t end;
2810c9de560dSAlex Tomas 	loff_t size, orig_size, start_off;
2811c9de560dSAlex Tomas 	ext4_lblk_t start, orig_start;
2812c9de560dSAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
28139a0762c5SAneesh Kumar K.V 	struct ext4_prealloc_space *pa;
2814c9de560dSAlex Tomas 
2815c9de560dSAlex Tomas 	/* do normalize only data requests, metadata requests
2816c9de560dSAlex Tomas 	   do not need preallocation */
2817c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2818c9de560dSAlex Tomas 		return;
2819c9de560dSAlex Tomas 
2820c9de560dSAlex Tomas 	/* sometime caller may want exact blocks */
2821c9de560dSAlex Tomas 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2822c9de560dSAlex Tomas 		return;
2823c9de560dSAlex Tomas 
2824c9de560dSAlex Tomas 	/* caller may indicate that preallocation isn't
2825c9de560dSAlex Tomas 	 * required (it's a tail, for example) */
2826c9de560dSAlex Tomas 	if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2827c9de560dSAlex Tomas 		return;
2828c9de560dSAlex Tomas 
2829c9de560dSAlex Tomas 	if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2830c9de560dSAlex Tomas 		ext4_mb_normalize_group_request(ac);
2831c9de560dSAlex Tomas 		return ;
2832c9de560dSAlex Tomas 	}
2833c9de560dSAlex Tomas 
2834c9de560dSAlex Tomas 	bsbits = ac->ac_sb->s_blocksize_bits;
2835c9de560dSAlex Tomas 
2836c9de560dSAlex Tomas 	/* first, let's learn actual file size
2837c9de560dSAlex Tomas 	 * given current request is allocated */
2838c9de560dSAlex Tomas 	size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2839c9de560dSAlex Tomas 	size = size << bsbits;
2840c9de560dSAlex Tomas 	if (size < i_size_read(ac->ac_inode))
2841c9de560dSAlex Tomas 		size = i_size_read(ac->ac_inode);
2842c9de560dSAlex Tomas 
28431930479cSValerie Clement 	/* max size of free chunks */
28441930479cSValerie Clement 	max = 2 << bsbits;
2845c9de560dSAlex Tomas 
28461930479cSValerie Clement #define NRL_CHECK_SIZE(req, size, max, chunk_size)	\
28471930479cSValerie Clement 		(req <= (size) || max <= (chunk_size))
2848c9de560dSAlex Tomas 
2849c9de560dSAlex Tomas 	/* first, try to predict filesize */
2850c9de560dSAlex Tomas 	/* XXX: should this table be tunable? */
2851c9de560dSAlex Tomas 	start_off = 0;
2852c9de560dSAlex Tomas 	if (size <= 16 * 1024) {
2853c9de560dSAlex Tomas 		size = 16 * 1024;
2854c9de560dSAlex Tomas 	} else if (size <= 32 * 1024) {
2855c9de560dSAlex Tomas 		size = 32 * 1024;
2856c9de560dSAlex Tomas 	} else if (size <= 64 * 1024) {
2857c9de560dSAlex Tomas 		size = 64 * 1024;
2858c9de560dSAlex Tomas 	} else if (size <= 128 * 1024) {
2859c9de560dSAlex Tomas 		size = 128 * 1024;
2860c9de560dSAlex Tomas 	} else if (size <= 256 * 1024) {
2861c9de560dSAlex Tomas 		size = 256 * 1024;
2862c9de560dSAlex Tomas 	} else if (size <= 512 * 1024) {
2863c9de560dSAlex Tomas 		size = 512 * 1024;
2864c9de560dSAlex Tomas 	} else if (size <= 1024 * 1024) {
2865c9de560dSAlex Tomas 		size = 1024 * 1024;
28661930479cSValerie Clement 	} else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
2867c9de560dSAlex Tomas 		start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
28681930479cSValerie Clement 						(21 - bsbits)) << 21;
28691930479cSValerie Clement 		size = 2 * 1024 * 1024;
28701930479cSValerie Clement 	} else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
2871c9de560dSAlex Tomas 		start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2872c9de560dSAlex Tomas 							(22 - bsbits)) << 22;
2873c9de560dSAlex Tomas 		size = 4 * 1024 * 1024;
2874c9de560dSAlex Tomas 	} else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
28751930479cSValerie Clement 					(8<<20)>>bsbits, max, 8 * 1024)) {
2876c9de560dSAlex Tomas 		start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2877c9de560dSAlex Tomas 							(23 - bsbits)) << 23;
2878c9de560dSAlex Tomas 		size = 8 * 1024 * 1024;
2879c9de560dSAlex Tomas 	} else {
2880c9de560dSAlex Tomas 		start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2881c9de560dSAlex Tomas 		size	  = ac->ac_o_ex.fe_len << bsbits;
2882c9de560dSAlex Tomas 	}
2883c9de560dSAlex Tomas 	orig_size = size = size >> bsbits;
2884c9de560dSAlex Tomas 	orig_start = start = start_off >> bsbits;
2885c9de560dSAlex Tomas 
2886c9de560dSAlex Tomas 	/* don't cover already allocated blocks in selected range */
2887c9de560dSAlex Tomas 	if (ar->pleft && start <= ar->lleft) {
2888c9de560dSAlex Tomas 		size -= ar->lleft + 1 - start;
2889c9de560dSAlex Tomas 		start = ar->lleft + 1;
2890c9de560dSAlex Tomas 	}
2891c9de560dSAlex Tomas 	if (ar->pright && start + size - 1 >= ar->lright)
2892c9de560dSAlex Tomas 		size -= start + size - ar->lright;
2893c9de560dSAlex Tomas 
2894c9de560dSAlex Tomas 	end = start + size;
2895c9de560dSAlex Tomas 
2896c9de560dSAlex Tomas 	/* check we don't cross already preallocated blocks */
2897c9de560dSAlex Tomas 	rcu_read_lock();
28989a0762c5SAneesh Kumar K.V 	list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
2899498e5f24STheodore Ts'o 		ext4_lblk_t pa_end;
2900c9de560dSAlex Tomas 
2901c9de560dSAlex Tomas 		if (pa->pa_deleted)
2902c9de560dSAlex Tomas 			continue;
2903c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
2904c9de560dSAlex Tomas 		if (pa->pa_deleted) {
2905c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
2906c9de560dSAlex Tomas 			continue;
2907c9de560dSAlex Tomas 		}
2908c9de560dSAlex Tomas 
2909c9de560dSAlex Tomas 		pa_end = pa->pa_lstart + pa->pa_len;
2910c9de560dSAlex Tomas 
2911c9de560dSAlex Tomas 		/* PA must not overlap original request */
2912c9de560dSAlex Tomas 		BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2913c9de560dSAlex Tomas 			ac->ac_o_ex.fe_logical < pa->pa_lstart));
2914c9de560dSAlex Tomas 
291538877f4eSEric Sandeen 		/* skip PAs this normalized request doesn't overlap with */
291638877f4eSEric Sandeen 		if (pa->pa_lstart >= end || pa_end <= start) {
2917c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
2918c9de560dSAlex Tomas 			continue;
2919c9de560dSAlex Tomas 		}
2920c9de560dSAlex Tomas 		BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2921c9de560dSAlex Tomas 
292238877f4eSEric Sandeen 		/* adjust start or end to be adjacent to this pa */
2923c9de560dSAlex Tomas 		if (pa_end <= ac->ac_o_ex.fe_logical) {
2924c9de560dSAlex Tomas 			BUG_ON(pa_end < start);
2925c9de560dSAlex Tomas 			start = pa_end;
292638877f4eSEric Sandeen 		} else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
2927c9de560dSAlex Tomas 			BUG_ON(pa->pa_lstart > end);
2928c9de560dSAlex Tomas 			end = pa->pa_lstart;
2929c9de560dSAlex Tomas 		}
2930c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
2931c9de560dSAlex Tomas 	}
2932c9de560dSAlex Tomas 	rcu_read_unlock();
2933c9de560dSAlex Tomas 	size = end - start;
2934c9de560dSAlex Tomas 
2935c9de560dSAlex Tomas 	/* XXX: extra loop to check we really don't overlap preallocations */
2936c9de560dSAlex Tomas 	rcu_read_lock();
29379a0762c5SAneesh Kumar K.V 	list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
2938498e5f24STheodore Ts'o 		ext4_lblk_t pa_end;
2939c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
2940c9de560dSAlex Tomas 		if (pa->pa_deleted == 0) {
2941c9de560dSAlex Tomas 			pa_end = pa->pa_lstart + pa->pa_len;
2942c9de560dSAlex Tomas 			BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
2943c9de560dSAlex Tomas 		}
2944c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
2945c9de560dSAlex Tomas 	}
2946c9de560dSAlex Tomas 	rcu_read_unlock();
2947c9de560dSAlex Tomas 
2948c9de560dSAlex Tomas 	if (start + size <= ac->ac_o_ex.fe_logical &&
2949c9de560dSAlex Tomas 			start > ac->ac_o_ex.fe_logical) {
2950c9de560dSAlex Tomas 		printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
2951c9de560dSAlex Tomas 			(unsigned long) start, (unsigned long) size,
2952c9de560dSAlex Tomas 			(unsigned long) ac->ac_o_ex.fe_logical);
2953c9de560dSAlex Tomas 	}
2954c9de560dSAlex Tomas 	BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
2955c9de560dSAlex Tomas 			start > ac->ac_o_ex.fe_logical);
29568d03c7a0SEric Sandeen 	BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
2957c9de560dSAlex Tomas 
2958c9de560dSAlex Tomas 	/* now prepare goal request */
2959c9de560dSAlex Tomas 
2960c9de560dSAlex Tomas 	/* XXX: is it better to align blocks WRT to logical
2961c9de560dSAlex Tomas 	 * placement or satisfy big request as is */
2962c9de560dSAlex Tomas 	ac->ac_g_ex.fe_logical = start;
2963c9de560dSAlex Tomas 	ac->ac_g_ex.fe_len = size;
2964c9de560dSAlex Tomas 
2965c9de560dSAlex Tomas 	/* define goal start in order to merge */
2966c9de560dSAlex Tomas 	if (ar->pright && (ar->lright == (start + size))) {
2967c9de560dSAlex Tomas 		/* merge to the right */
2968c9de560dSAlex Tomas 		ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
2969c9de560dSAlex Tomas 						&ac->ac_f_ex.fe_group,
2970c9de560dSAlex Tomas 						&ac->ac_f_ex.fe_start);
2971c9de560dSAlex Tomas 		ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
2972c9de560dSAlex Tomas 	}
2973c9de560dSAlex Tomas 	if (ar->pleft && (ar->lleft + 1 == start)) {
2974c9de560dSAlex Tomas 		/* merge to the left */
2975c9de560dSAlex Tomas 		ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
2976c9de560dSAlex Tomas 						&ac->ac_f_ex.fe_group,
2977c9de560dSAlex Tomas 						&ac->ac_f_ex.fe_start);
2978c9de560dSAlex Tomas 		ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
2979c9de560dSAlex Tomas 	}
2980c9de560dSAlex Tomas 
29816ba495e9STheodore Ts'o 	mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
2982c9de560dSAlex Tomas 		(unsigned) orig_size, (unsigned) start);
2983c9de560dSAlex Tomas }
2984c9de560dSAlex Tomas 
2985c9de560dSAlex Tomas static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
2986c9de560dSAlex Tomas {
2987c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2988c9de560dSAlex Tomas 
2989c9de560dSAlex Tomas 	if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
2990c9de560dSAlex Tomas 		atomic_inc(&sbi->s_bal_reqs);
2991c9de560dSAlex Tomas 		atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
2992c9de560dSAlex Tomas 		if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
2993c9de560dSAlex Tomas 			atomic_inc(&sbi->s_bal_success);
2994c9de560dSAlex Tomas 		atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
2995c9de560dSAlex Tomas 		if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2996c9de560dSAlex Tomas 				ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2997c9de560dSAlex Tomas 			atomic_inc(&sbi->s_bal_goals);
2998c9de560dSAlex Tomas 		if (ac->ac_found > sbi->s_mb_max_to_scan)
2999c9de560dSAlex Tomas 			atomic_inc(&sbi->s_bal_breaks);
3000c9de560dSAlex Tomas 	}
3001c9de560dSAlex Tomas 
3002*296c355cSTheodore Ts'o 	if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3003*296c355cSTheodore Ts'o 		trace_ext4_mballoc_alloc(ac);
3004*296c355cSTheodore Ts'o 	else
3005*296c355cSTheodore Ts'o 		trace_ext4_mballoc_prealloc(ac);
3006c9de560dSAlex Tomas }
3007c9de560dSAlex Tomas 
3008c9de560dSAlex Tomas /*
3009c9de560dSAlex Tomas  * use blocks preallocated to inode
3010c9de560dSAlex Tomas  */
3011c9de560dSAlex Tomas static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3012c9de560dSAlex Tomas 				struct ext4_prealloc_space *pa)
3013c9de560dSAlex Tomas {
3014c9de560dSAlex Tomas 	ext4_fsblk_t start;
3015c9de560dSAlex Tomas 	ext4_fsblk_t end;
3016c9de560dSAlex Tomas 	int len;
3017c9de560dSAlex Tomas 
3018c9de560dSAlex Tomas 	/* found preallocated blocks, use them */
3019c9de560dSAlex Tomas 	start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3020c9de560dSAlex Tomas 	end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3021c9de560dSAlex Tomas 	len = end - start;
3022c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3023c9de560dSAlex Tomas 					&ac->ac_b_ex.fe_start);
3024c9de560dSAlex Tomas 	ac->ac_b_ex.fe_len = len;
3025c9de560dSAlex Tomas 	ac->ac_status = AC_STATUS_FOUND;
3026c9de560dSAlex Tomas 	ac->ac_pa = pa;
3027c9de560dSAlex Tomas 
3028c9de560dSAlex Tomas 	BUG_ON(start < pa->pa_pstart);
3029c9de560dSAlex Tomas 	BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3030c9de560dSAlex Tomas 	BUG_ON(pa->pa_free < len);
3031c9de560dSAlex Tomas 	pa->pa_free -= len;
3032c9de560dSAlex Tomas 
30336ba495e9STheodore Ts'o 	mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
3034c9de560dSAlex Tomas }
3035c9de560dSAlex Tomas 
3036c9de560dSAlex Tomas /*
3037c9de560dSAlex Tomas  * use blocks preallocated to locality group
3038c9de560dSAlex Tomas  */
3039c9de560dSAlex Tomas static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3040c9de560dSAlex Tomas 				struct ext4_prealloc_space *pa)
3041c9de560dSAlex Tomas {
304203cddb80SAneesh Kumar K.V 	unsigned int len = ac->ac_o_ex.fe_len;
30436be2ded1SAneesh Kumar K.V 
3044c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3045c9de560dSAlex Tomas 					&ac->ac_b_ex.fe_group,
3046c9de560dSAlex Tomas 					&ac->ac_b_ex.fe_start);
3047c9de560dSAlex Tomas 	ac->ac_b_ex.fe_len = len;
3048c9de560dSAlex Tomas 	ac->ac_status = AC_STATUS_FOUND;
3049c9de560dSAlex Tomas 	ac->ac_pa = pa;
3050c9de560dSAlex Tomas 
3051c9de560dSAlex Tomas 	/* we don't correct pa_pstart or pa_plen here to avoid
305226346ff6SAneesh Kumar K.V 	 * possible race when the group is being loaded concurrently
3053c9de560dSAlex Tomas 	 * instead we correct pa later, after blocks are marked
305426346ff6SAneesh Kumar K.V 	 * in on-disk bitmap -- see ext4_mb_release_context()
305526346ff6SAneesh Kumar K.V 	 * Other CPUs are prevented from allocating from this pa by lg_mutex
3056c9de560dSAlex Tomas 	 */
30576ba495e9STheodore Ts'o 	mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
3058c9de560dSAlex Tomas }
3059c9de560dSAlex Tomas 
3060c9de560dSAlex Tomas /*
30615e745b04SAneesh Kumar K.V  * Return the prealloc space that have minimal distance
30625e745b04SAneesh Kumar K.V  * from the goal block. @cpa is the prealloc
30635e745b04SAneesh Kumar K.V  * space that is having currently known minimal distance
30645e745b04SAneesh Kumar K.V  * from the goal block.
30655e745b04SAneesh Kumar K.V  */
30665e745b04SAneesh Kumar K.V static struct ext4_prealloc_space *
30675e745b04SAneesh Kumar K.V ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
30685e745b04SAneesh Kumar K.V 			struct ext4_prealloc_space *pa,
30695e745b04SAneesh Kumar K.V 			struct ext4_prealloc_space *cpa)
30705e745b04SAneesh Kumar K.V {
30715e745b04SAneesh Kumar K.V 	ext4_fsblk_t cur_distance, new_distance;
30725e745b04SAneesh Kumar K.V 
30735e745b04SAneesh Kumar K.V 	if (cpa == NULL) {
30745e745b04SAneesh Kumar K.V 		atomic_inc(&pa->pa_count);
30755e745b04SAneesh Kumar K.V 		return pa;
30765e745b04SAneesh Kumar K.V 	}
30775e745b04SAneesh Kumar K.V 	cur_distance = abs(goal_block - cpa->pa_pstart);
30785e745b04SAneesh Kumar K.V 	new_distance = abs(goal_block - pa->pa_pstart);
30795e745b04SAneesh Kumar K.V 
30805e745b04SAneesh Kumar K.V 	if (cur_distance < new_distance)
30815e745b04SAneesh Kumar K.V 		return cpa;
30825e745b04SAneesh Kumar K.V 
30835e745b04SAneesh Kumar K.V 	/* drop the previous reference */
30845e745b04SAneesh Kumar K.V 	atomic_dec(&cpa->pa_count);
30855e745b04SAneesh Kumar K.V 	atomic_inc(&pa->pa_count);
30865e745b04SAneesh Kumar K.V 	return pa;
30875e745b04SAneesh Kumar K.V }
30885e745b04SAneesh Kumar K.V 
30895e745b04SAneesh Kumar K.V /*
3090c9de560dSAlex Tomas  * search goal blocks in preallocated space
3091c9de560dSAlex Tomas  */
30924ddfef7bSEric Sandeen static noinline_for_stack int
30934ddfef7bSEric Sandeen ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
3094c9de560dSAlex Tomas {
30956be2ded1SAneesh Kumar K.V 	int order, i;
3096c9de560dSAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3097c9de560dSAlex Tomas 	struct ext4_locality_group *lg;
30985e745b04SAneesh Kumar K.V 	struct ext4_prealloc_space *pa, *cpa = NULL;
30995e745b04SAneesh Kumar K.V 	ext4_fsblk_t goal_block;
3100c9de560dSAlex Tomas 
3101c9de560dSAlex Tomas 	/* only data can be preallocated */
3102c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3103c9de560dSAlex Tomas 		return 0;
3104c9de560dSAlex Tomas 
3105c9de560dSAlex Tomas 	/* first, try per-file preallocation */
3106c9de560dSAlex Tomas 	rcu_read_lock();
31079a0762c5SAneesh Kumar K.V 	list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
3108c9de560dSAlex Tomas 
3109c9de560dSAlex Tomas 		/* all fields in this condition don't change,
3110c9de560dSAlex Tomas 		 * so we can skip locking for them */
3111c9de560dSAlex Tomas 		if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3112c9de560dSAlex Tomas 			ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3113c9de560dSAlex Tomas 			continue;
3114c9de560dSAlex Tomas 
3115fb0a387dSEric Sandeen 		/* non-extent files can't have physical blocks past 2^32 */
3116fb0a387dSEric Sandeen 		if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL) &&
3117fb0a387dSEric Sandeen 			pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3118fb0a387dSEric Sandeen 			continue;
3119fb0a387dSEric Sandeen 
3120c9de560dSAlex Tomas 		/* found preallocated blocks, use them */
3121c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3122c9de560dSAlex Tomas 		if (pa->pa_deleted == 0 && pa->pa_free) {
3123c9de560dSAlex Tomas 			atomic_inc(&pa->pa_count);
3124c9de560dSAlex Tomas 			ext4_mb_use_inode_pa(ac, pa);
3125c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3126c9de560dSAlex Tomas 			ac->ac_criteria = 10;
3127c9de560dSAlex Tomas 			rcu_read_unlock();
3128c9de560dSAlex Tomas 			return 1;
3129c9de560dSAlex Tomas 		}
3130c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3131c9de560dSAlex Tomas 	}
3132c9de560dSAlex Tomas 	rcu_read_unlock();
3133c9de560dSAlex Tomas 
3134c9de560dSAlex Tomas 	/* can we use group allocation? */
3135c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3136c9de560dSAlex Tomas 		return 0;
3137c9de560dSAlex Tomas 
3138c9de560dSAlex Tomas 	/* inode may have no locality group for some reason */
3139c9de560dSAlex Tomas 	lg = ac->ac_lg;
3140c9de560dSAlex Tomas 	if (lg == NULL)
3141c9de560dSAlex Tomas 		return 0;
31426be2ded1SAneesh Kumar K.V 	order  = fls(ac->ac_o_ex.fe_len) - 1;
31436be2ded1SAneesh Kumar K.V 	if (order > PREALLOC_TB_SIZE - 1)
31446be2ded1SAneesh Kumar K.V 		/* The max size of hash table is PREALLOC_TB_SIZE */
31456be2ded1SAneesh Kumar K.V 		order = PREALLOC_TB_SIZE - 1;
3146c9de560dSAlex Tomas 
31475e745b04SAneesh Kumar K.V 	goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
31485e745b04SAneesh Kumar K.V 		     ac->ac_g_ex.fe_start +
31495e745b04SAneesh Kumar K.V 		     le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
31505e745b04SAneesh Kumar K.V 	/*
31515e745b04SAneesh Kumar K.V 	 * search for the prealloc space that is having
31525e745b04SAneesh Kumar K.V 	 * minimal distance from the goal block.
31535e745b04SAneesh Kumar K.V 	 */
31546be2ded1SAneesh Kumar K.V 	for (i = order; i < PREALLOC_TB_SIZE; i++) {
3155c9de560dSAlex Tomas 		rcu_read_lock();
31566be2ded1SAneesh Kumar K.V 		list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
31576be2ded1SAneesh Kumar K.V 					pa_inode_list) {
3158c9de560dSAlex Tomas 			spin_lock(&pa->pa_lock);
31596be2ded1SAneesh Kumar K.V 			if (pa->pa_deleted == 0 &&
31606be2ded1SAneesh Kumar K.V 					pa->pa_free >= ac->ac_o_ex.fe_len) {
31615e745b04SAneesh Kumar K.V 
31625e745b04SAneesh Kumar K.V 				cpa = ext4_mb_check_group_pa(goal_block,
31635e745b04SAneesh Kumar K.V 								pa, cpa);
31645e745b04SAneesh Kumar K.V 			}
3165c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
31665e745b04SAneesh Kumar K.V 		}
31675e745b04SAneesh Kumar K.V 		rcu_read_unlock();
31685e745b04SAneesh Kumar K.V 	}
31695e745b04SAneesh Kumar K.V 	if (cpa) {
31705e745b04SAneesh Kumar K.V 		ext4_mb_use_group_pa(ac, cpa);
3171c9de560dSAlex Tomas 		ac->ac_criteria = 20;
3172c9de560dSAlex Tomas 		return 1;
3173c9de560dSAlex Tomas 	}
3174c9de560dSAlex Tomas 	return 0;
3175c9de560dSAlex Tomas }
3176c9de560dSAlex Tomas 
3177c9de560dSAlex Tomas /*
31787a2fcbf7SAneesh Kumar K.V  * the function goes through all block freed in the group
31797a2fcbf7SAneesh Kumar K.V  * but not yet committed and marks them used in in-core bitmap.
31807a2fcbf7SAneesh Kumar K.V  * buddy must be generated from this bitmap
3181955ce5f5SAneesh Kumar K.V  * Need to be called with the ext4 group lock held
31827a2fcbf7SAneesh Kumar K.V  */
31837a2fcbf7SAneesh Kumar K.V static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
31847a2fcbf7SAneesh Kumar K.V 						ext4_group_t group)
31857a2fcbf7SAneesh Kumar K.V {
31867a2fcbf7SAneesh Kumar K.V 	struct rb_node *n;
31877a2fcbf7SAneesh Kumar K.V 	struct ext4_group_info *grp;
31887a2fcbf7SAneesh Kumar K.V 	struct ext4_free_data *entry;
31897a2fcbf7SAneesh Kumar K.V 
31907a2fcbf7SAneesh Kumar K.V 	grp = ext4_get_group_info(sb, group);
31917a2fcbf7SAneesh Kumar K.V 	n = rb_first(&(grp->bb_free_root));
31927a2fcbf7SAneesh Kumar K.V 
31937a2fcbf7SAneesh Kumar K.V 	while (n) {
31947a2fcbf7SAneesh Kumar K.V 		entry = rb_entry(n, struct ext4_free_data, node);
3195955ce5f5SAneesh Kumar K.V 		mb_set_bits(bitmap, entry->start_blk, entry->count);
31967a2fcbf7SAneesh Kumar K.V 		n = rb_next(n);
31977a2fcbf7SAneesh Kumar K.V 	}
31987a2fcbf7SAneesh Kumar K.V 	return;
31997a2fcbf7SAneesh Kumar K.V }
32007a2fcbf7SAneesh Kumar K.V 
32017a2fcbf7SAneesh Kumar K.V /*
3202c9de560dSAlex Tomas  * the function goes through all preallocation in this group and marks them
3203c9de560dSAlex Tomas  * used in in-core bitmap. buddy must be generated from this bitmap
3204955ce5f5SAneesh Kumar K.V  * Need to be called with ext4 group lock held
3205c9de560dSAlex Tomas  */
3206089ceeccSEric Sandeen static noinline_for_stack
3207089ceeccSEric Sandeen void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
3208c9de560dSAlex Tomas 					ext4_group_t group)
3209c9de560dSAlex Tomas {
3210c9de560dSAlex Tomas 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3211c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
3212c9de560dSAlex Tomas 	struct list_head *cur;
3213c9de560dSAlex Tomas 	ext4_group_t groupnr;
3214c9de560dSAlex Tomas 	ext4_grpblk_t start;
3215c9de560dSAlex Tomas 	int preallocated = 0;
3216c9de560dSAlex Tomas 	int count = 0;
3217c9de560dSAlex Tomas 	int len;
3218c9de560dSAlex Tomas 
3219c9de560dSAlex Tomas 	/* all form of preallocation discards first load group,
3220c9de560dSAlex Tomas 	 * so the only competing code is preallocation use.
3221c9de560dSAlex Tomas 	 * we don't need any locking here
3222c9de560dSAlex Tomas 	 * notice we do NOT ignore preallocations with pa_deleted
3223c9de560dSAlex Tomas 	 * otherwise we could leave used blocks available for
3224c9de560dSAlex Tomas 	 * allocation in buddy when concurrent ext4_mb_put_pa()
3225c9de560dSAlex Tomas 	 * is dropping preallocation
3226c9de560dSAlex Tomas 	 */
3227c9de560dSAlex Tomas 	list_for_each(cur, &grp->bb_prealloc_list) {
3228c9de560dSAlex Tomas 		pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3229c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3230c9de560dSAlex Tomas 		ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3231c9de560dSAlex Tomas 					     &groupnr, &start);
3232c9de560dSAlex Tomas 		len = pa->pa_len;
3233c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3234c9de560dSAlex Tomas 		if (unlikely(len == 0))
3235c9de560dSAlex Tomas 			continue;
3236c9de560dSAlex Tomas 		BUG_ON(groupnr != group);
3237955ce5f5SAneesh Kumar K.V 		mb_set_bits(bitmap, start, len);
3238c9de560dSAlex Tomas 		preallocated += len;
3239c9de560dSAlex Tomas 		count++;
3240c9de560dSAlex Tomas 	}
32416ba495e9STheodore Ts'o 	mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
3242c9de560dSAlex Tomas }
3243c9de560dSAlex Tomas 
3244c9de560dSAlex Tomas static void ext4_mb_pa_callback(struct rcu_head *head)
3245c9de560dSAlex Tomas {
3246c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
3247c9de560dSAlex Tomas 	pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3248c9de560dSAlex Tomas 	kmem_cache_free(ext4_pspace_cachep, pa);
3249c9de560dSAlex Tomas }
3250c9de560dSAlex Tomas 
3251c9de560dSAlex Tomas /*
3252c9de560dSAlex Tomas  * drops a reference to preallocated space descriptor
3253c9de560dSAlex Tomas  * if this was the last reference and the space is consumed
3254c9de560dSAlex Tomas  */
3255c9de560dSAlex Tomas static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3256c9de560dSAlex Tomas 			struct super_block *sb, struct ext4_prealloc_space *pa)
3257c9de560dSAlex Tomas {
3258a9df9a49STheodore Ts'o 	ext4_group_t grp;
3259d33a1976SEric Sandeen 	ext4_fsblk_t grp_blk;
3260c9de560dSAlex Tomas 
3261c9de560dSAlex Tomas 	if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3262c9de560dSAlex Tomas 		return;
3263c9de560dSAlex Tomas 
3264c9de560dSAlex Tomas 	/* in this short window concurrent discard can set pa_deleted */
3265c9de560dSAlex Tomas 	spin_lock(&pa->pa_lock);
3266c9de560dSAlex Tomas 	if (pa->pa_deleted == 1) {
3267c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3268c9de560dSAlex Tomas 		return;
3269c9de560dSAlex Tomas 	}
3270c9de560dSAlex Tomas 
3271c9de560dSAlex Tomas 	pa->pa_deleted = 1;
3272c9de560dSAlex Tomas 	spin_unlock(&pa->pa_lock);
3273c9de560dSAlex Tomas 
3274d33a1976SEric Sandeen 	grp_blk = pa->pa_pstart;
3275cc0fb9adSAneesh Kumar K.V 	/*
3276cc0fb9adSAneesh Kumar K.V 	 * If doing group-based preallocation, pa_pstart may be in the
3277cc0fb9adSAneesh Kumar K.V 	 * next group when pa is used up
3278cc0fb9adSAneesh Kumar K.V 	 */
3279cc0fb9adSAneesh Kumar K.V 	if (pa->pa_type == MB_GROUP_PA)
3280d33a1976SEric Sandeen 		grp_blk--;
3281d33a1976SEric Sandeen 
3282d33a1976SEric Sandeen 	ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
3283c9de560dSAlex Tomas 
3284c9de560dSAlex Tomas 	/*
3285c9de560dSAlex Tomas 	 * possible race:
3286c9de560dSAlex Tomas 	 *
3287c9de560dSAlex Tomas 	 *  P1 (buddy init)			P2 (regular allocation)
3288c9de560dSAlex Tomas 	 *					find block B in PA
3289c9de560dSAlex Tomas 	 *  copy on-disk bitmap to buddy
3290c9de560dSAlex Tomas 	 *  					mark B in on-disk bitmap
3291c9de560dSAlex Tomas 	 *					drop PA from group
3292c9de560dSAlex Tomas 	 *  mark all PAs in buddy
3293c9de560dSAlex Tomas 	 *
3294c9de560dSAlex Tomas 	 * thus, P1 initializes buddy with B available. to prevent this
3295c9de560dSAlex Tomas 	 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3296c9de560dSAlex Tomas 	 * against that pair
3297c9de560dSAlex Tomas 	 */
3298c9de560dSAlex Tomas 	ext4_lock_group(sb, grp);
3299c9de560dSAlex Tomas 	list_del(&pa->pa_group_list);
3300c9de560dSAlex Tomas 	ext4_unlock_group(sb, grp);
3301c9de560dSAlex Tomas 
3302c9de560dSAlex Tomas 	spin_lock(pa->pa_obj_lock);
3303c9de560dSAlex Tomas 	list_del_rcu(&pa->pa_inode_list);
3304c9de560dSAlex Tomas 	spin_unlock(pa->pa_obj_lock);
3305c9de560dSAlex Tomas 
3306c9de560dSAlex Tomas 	call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3307c9de560dSAlex Tomas }
3308c9de560dSAlex Tomas 
3309c9de560dSAlex Tomas /*
3310c9de560dSAlex Tomas  * creates new preallocated space for given inode
3311c9de560dSAlex Tomas  */
33124ddfef7bSEric Sandeen static noinline_for_stack int
33134ddfef7bSEric Sandeen ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
3314c9de560dSAlex Tomas {
3315c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
3316c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
3317c9de560dSAlex Tomas 	struct ext4_group_info *grp;
3318c9de560dSAlex Tomas 	struct ext4_inode_info *ei;
3319c9de560dSAlex Tomas 
3320c9de560dSAlex Tomas 	/* preallocate only when found space is larger then requested */
3321c9de560dSAlex Tomas 	BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3322c9de560dSAlex Tomas 	BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3323c9de560dSAlex Tomas 	BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3324c9de560dSAlex Tomas 
3325c9de560dSAlex Tomas 	pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3326c9de560dSAlex Tomas 	if (pa == NULL)
3327c9de560dSAlex Tomas 		return -ENOMEM;
3328c9de560dSAlex Tomas 
3329c9de560dSAlex Tomas 	if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3330c9de560dSAlex Tomas 		int winl;
3331c9de560dSAlex Tomas 		int wins;
3332c9de560dSAlex Tomas 		int win;
3333c9de560dSAlex Tomas 		int offs;
3334c9de560dSAlex Tomas 
3335c9de560dSAlex Tomas 		/* we can't allocate as much as normalizer wants.
3336c9de560dSAlex Tomas 		 * so, found space must get proper lstart
3337c9de560dSAlex Tomas 		 * to cover original request */
3338c9de560dSAlex Tomas 		BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3339c9de560dSAlex Tomas 		BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3340c9de560dSAlex Tomas 
3341c9de560dSAlex Tomas 		/* we're limited by original request in that
3342c9de560dSAlex Tomas 		 * logical block must be covered any way
3343c9de560dSAlex Tomas 		 * winl is window we can move our chunk within */
3344c9de560dSAlex Tomas 		winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3345c9de560dSAlex Tomas 
3346c9de560dSAlex Tomas 		/* also, we should cover whole original request */
3347c9de560dSAlex Tomas 		wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3348c9de560dSAlex Tomas 
3349c9de560dSAlex Tomas 		/* the smallest one defines real window */
3350c9de560dSAlex Tomas 		win = min(winl, wins);
3351c9de560dSAlex Tomas 
3352c9de560dSAlex Tomas 		offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3353c9de560dSAlex Tomas 		if (offs && offs < win)
3354c9de560dSAlex Tomas 			win = offs;
3355c9de560dSAlex Tomas 
3356c9de560dSAlex Tomas 		ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3357c9de560dSAlex Tomas 		BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3358c9de560dSAlex Tomas 		BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3359c9de560dSAlex Tomas 	}
3360c9de560dSAlex Tomas 
3361c9de560dSAlex Tomas 	/* preallocation can change ac_b_ex, thus we store actually
3362c9de560dSAlex Tomas 	 * allocated blocks for history */
3363c9de560dSAlex Tomas 	ac->ac_f_ex = ac->ac_b_ex;
3364c9de560dSAlex Tomas 
3365c9de560dSAlex Tomas 	pa->pa_lstart = ac->ac_b_ex.fe_logical;
3366c9de560dSAlex Tomas 	pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3367c9de560dSAlex Tomas 	pa->pa_len = ac->ac_b_ex.fe_len;
3368c9de560dSAlex Tomas 	pa->pa_free = pa->pa_len;
3369c9de560dSAlex Tomas 	atomic_set(&pa->pa_count, 1);
3370c9de560dSAlex Tomas 	spin_lock_init(&pa->pa_lock);
3371d794bf8eSAneesh Kumar K.V 	INIT_LIST_HEAD(&pa->pa_inode_list);
3372d794bf8eSAneesh Kumar K.V 	INIT_LIST_HEAD(&pa->pa_group_list);
3373c9de560dSAlex Tomas 	pa->pa_deleted = 0;
3374cc0fb9adSAneesh Kumar K.V 	pa->pa_type = MB_INODE_PA;
3375c9de560dSAlex Tomas 
33766ba495e9STheodore Ts'o 	mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
3377c9de560dSAlex Tomas 			pa->pa_pstart, pa->pa_len, pa->pa_lstart);
33789bffad1eSTheodore Ts'o 	trace_ext4_mb_new_inode_pa(ac, pa);
3379c9de560dSAlex Tomas 
3380c9de560dSAlex Tomas 	ext4_mb_use_inode_pa(ac, pa);
3381c9de560dSAlex Tomas 	atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3382c9de560dSAlex Tomas 
3383c9de560dSAlex Tomas 	ei = EXT4_I(ac->ac_inode);
3384c9de560dSAlex Tomas 	grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3385c9de560dSAlex Tomas 
3386c9de560dSAlex Tomas 	pa->pa_obj_lock = &ei->i_prealloc_lock;
3387c9de560dSAlex Tomas 	pa->pa_inode = ac->ac_inode;
3388c9de560dSAlex Tomas 
3389c9de560dSAlex Tomas 	ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3390c9de560dSAlex Tomas 	list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3391c9de560dSAlex Tomas 	ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3392c9de560dSAlex Tomas 
3393c9de560dSAlex Tomas 	spin_lock(pa->pa_obj_lock);
3394c9de560dSAlex Tomas 	list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3395c9de560dSAlex Tomas 	spin_unlock(pa->pa_obj_lock);
3396c9de560dSAlex Tomas 
3397c9de560dSAlex Tomas 	return 0;
3398c9de560dSAlex Tomas }
3399c9de560dSAlex Tomas 
3400c9de560dSAlex Tomas /*
3401c9de560dSAlex Tomas  * creates new preallocated space for locality group inodes belongs to
3402c9de560dSAlex Tomas  */
34034ddfef7bSEric Sandeen static noinline_for_stack int
34044ddfef7bSEric Sandeen ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
3405c9de560dSAlex Tomas {
3406c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
3407c9de560dSAlex Tomas 	struct ext4_locality_group *lg;
3408c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
3409c9de560dSAlex Tomas 	struct ext4_group_info *grp;
3410c9de560dSAlex Tomas 
3411c9de560dSAlex Tomas 	/* preallocate only when found space is larger then requested */
3412c9de560dSAlex Tomas 	BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3413c9de560dSAlex Tomas 	BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3414c9de560dSAlex Tomas 	BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3415c9de560dSAlex Tomas 
3416c9de560dSAlex Tomas 	BUG_ON(ext4_pspace_cachep == NULL);
3417c9de560dSAlex Tomas 	pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3418c9de560dSAlex Tomas 	if (pa == NULL)
3419c9de560dSAlex Tomas 		return -ENOMEM;
3420c9de560dSAlex Tomas 
3421c9de560dSAlex Tomas 	/* preallocation can change ac_b_ex, thus we store actually
3422c9de560dSAlex Tomas 	 * allocated blocks for history */
3423c9de560dSAlex Tomas 	ac->ac_f_ex = ac->ac_b_ex;
3424c9de560dSAlex Tomas 
3425c9de560dSAlex Tomas 	pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3426c9de560dSAlex Tomas 	pa->pa_lstart = pa->pa_pstart;
3427c9de560dSAlex Tomas 	pa->pa_len = ac->ac_b_ex.fe_len;
3428c9de560dSAlex Tomas 	pa->pa_free = pa->pa_len;
3429c9de560dSAlex Tomas 	atomic_set(&pa->pa_count, 1);
3430c9de560dSAlex Tomas 	spin_lock_init(&pa->pa_lock);
34316be2ded1SAneesh Kumar K.V 	INIT_LIST_HEAD(&pa->pa_inode_list);
3432d794bf8eSAneesh Kumar K.V 	INIT_LIST_HEAD(&pa->pa_group_list);
3433c9de560dSAlex Tomas 	pa->pa_deleted = 0;
3434cc0fb9adSAneesh Kumar K.V 	pa->pa_type = MB_GROUP_PA;
3435c9de560dSAlex Tomas 
34366ba495e9STheodore Ts'o 	mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
3437c9de560dSAlex Tomas 			pa->pa_pstart, pa->pa_len, pa->pa_lstart);
34389bffad1eSTheodore Ts'o 	trace_ext4_mb_new_group_pa(ac, pa);
3439c9de560dSAlex Tomas 
3440c9de560dSAlex Tomas 	ext4_mb_use_group_pa(ac, pa);
3441c9de560dSAlex Tomas 	atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3442c9de560dSAlex Tomas 
3443c9de560dSAlex Tomas 	grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3444c9de560dSAlex Tomas 	lg = ac->ac_lg;
3445c9de560dSAlex Tomas 	BUG_ON(lg == NULL);
3446c9de560dSAlex Tomas 
3447c9de560dSAlex Tomas 	pa->pa_obj_lock = &lg->lg_prealloc_lock;
3448c9de560dSAlex Tomas 	pa->pa_inode = NULL;
3449c9de560dSAlex Tomas 
3450c9de560dSAlex Tomas 	ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3451c9de560dSAlex Tomas 	list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3452c9de560dSAlex Tomas 	ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3453c9de560dSAlex Tomas 
34546be2ded1SAneesh Kumar K.V 	/*
34556be2ded1SAneesh Kumar K.V 	 * We will later add the new pa to the right bucket
34566be2ded1SAneesh Kumar K.V 	 * after updating the pa_free in ext4_mb_release_context
34576be2ded1SAneesh Kumar K.V 	 */
3458c9de560dSAlex Tomas 	return 0;
3459c9de560dSAlex Tomas }
3460c9de560dSAlex Tomas 
3461c9de560dSAlex Tomas static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3462c9de560dSAlex Tomas {
3463c9de560dSAlex Tomas 	int err;
3464c9de560dSAlex Tomas 
3465c9de560dSAlex Tomas 	if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3466c9de560dSAlex Tomas 		err = ext4_mb_new_group_pa(ac);
3467c9de560dSAlex Tomas 	else
3468c9de560dSAlex Tomas 		err = ext4_mb_new_inode_pa(ac);
3469c9de560dSAlex Tomas 	return err;
3470c9de560dSAlex Tomas }
3471c9de560dSAlex Tomas 
3472c9de560dSAlex Tomas /*
3473c9de560dSAlex Tomas  * finds all unused blocks in on-disk bitmap, frees them in
3474c9de560dSAlex Tomas  * in-core bitmap and buddy.
3475c9de560dSAlex Tomas  * @pa must be unlinked from inode and group lists, so that
3476c9de560dSAlex Tomas  * nobody else can find/use it.
3477c9de560dSAlex Tomas  * the caller MUST hold group/inode locks.
3478c9de560dSAlex Tomas  * TODO: optimize the case when there are no in-core structures yet
3479c9de560dSAlex Tomas  */
34804ddfef7bSEric Sandeen static noinline_for_stack int
34814ddfef7bSEric Sandeen ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
3482c83617dbSAneesh Kumar K.V 			struct ext4_prealloc_space *pa,
3483c83617dbSAneesh Kumar K.V 			struct ext4_allocation_context *ac)
3484c9de560dSAlex Tomas {
3485c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
3486c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3487498e5f24STheodore Ts'o 	unsigned int end;
3488498e5f24STheodore Ts'o 	unsigned int next;
3489c9de560dSAlex Tomas 	ext4_group_t group;
3490c9de560dSAlex Tomas 	ext4_grpblk_t bit;
3491ba80b101STheodore Ts'o 	unsigned long long grp_blk_start;
3492c9de560dSAlex Tomas 	sector_t start;
3493c9de560dSAlex Tomas 	int err = 0;
3494c9de560dSAlex Tomas 	int free = 0;
3495c9de560dSAlex Tomas 
3496c9de560dSAlex Tomas 	BUG_ON(pa->pa_deleted == 0);
3497c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3498ba80b101STheodore Ts'o 	grp_blk_start = pa->pa_pstart - bit;
3499c9de560dSAlex Tomas 	BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3500c9de560dSAlex Tomas 	end = bit + pa->pa_len;
3501c9de560dSAlex Tomas 
3502256bdb49SEric Sandeen 	if (ac) {
3503256bdb49SEric Sandeen 		ac->ac_sb = sb;
3504256bdb49SEric Sandeen 		ac->ac_inode = pa->pa_inode;
3505256bdb49SEric Sandeen 	}
3506c9de560dSAlex Tomas 
3507c9de560dSAlex Tomas 	while (bit < end) {
3508ffad0a44SAneesh Kumar K.V 		bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
3509c9de560dSAlex Tomas 		if (bit >= end)
3510c9de560dSAlex Tomas 			break;
3511ffad0a44SAneesh Kumar K.V 		next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
3512c9de560dSAlex Tomas 		start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
3513c9de560dSAlex Tomas 				le32_to_cpu(sbi->s_es->s_first_data_block);
35146ba495e9STheodore Ts'o 		mb_debug(1, "    free preallocated %u/%u in group %u\n",
3515c9de560dSAlex Tomas 				(unsigned) start, (unsigned) next - bit,
3516c9de560dSAlex Tomas 				(unsigned) group);
3517c9de560dSAlex Tomas 		free += next - bit;
3518c9de560dSAlex Tomas 
3519256bdb49SEric Sandeen 		if (ac) {
3520256bdb49SEric Sandeen 			ac->ac_b_ex.fe_group = group;
3521256bdb49SEric Sandeen 			ac->ac_b_ex.fe_start = bit;
3522256bdb49SEric Sandeen 			ac->ac_b_ex.fe_len = next - bit;
3523256bdb49SEric Sandeen 			ac->ac_b_ex.fe_logical = 0;
3524*296c355cSTheodore Ts'o 			trace_ext4_mballoc_discard(ac);
3525256bdb49SEric Sandeen 		}
3526c9de560dSAlex Tomas 
35279bffad1eSTheodore Ts'o 		trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
3528ba80b101STheodore Ts'o 					       next - bit);
3529c9de560dSAlex Tomas 		mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3530c9de560dSAlex Tomas 		bit = next + 1;
3531c9de560dSAlex Tomas 	}
3532c9de560dSAlex Tomas 	if (free != pa->pa_free) {
353326346ff6SAneesh Kumar K.V 		printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
3534c9de560dSAlex Tomas 			pa, (unsigned long) pa->pa_lstart,
3535c9de560dSAlex Tomas 			(unsigned long) pa->pa_pstart,
3536c9de560dSAlex Tomas 			(unsigned long) pa->pa_len);
35375d1b1b3fSAneesh Kumar K.V 		ext4_grp_locked_error(sb, group,
35385d1b1b3fSAneesh Kumar K.V 					__func__, "free %u, pa_free %u",
353926346ff6SAneesh Kumar K.V 					free, pa->pa_free);
3540e56eb659SAneesh Kumar K.V 		/*
3541e56eb659SAneesh Kumar K.V 		 * pa is already deleted so we use the value obtained
3542e56eb659SAneesh Kumar K.V 		 * from the bitmap and continue.
3543e56eb659SAneesh Kumar K.V 		 */
3544c9de560dSAlex Tomas 	}
3545c9de560dSAlex Tomas 	atomic_add(free, &sbi->s_mb_discarded);
3546c9de560dSAlex Tomas 
3547c9de560dSAlex Tomas 	return err;
3548c9de560dSAlex Tomas }
3549c9de560dSAlex Tomas 
35504ddfef7bSEric Sandeen static noinline_for_stack int
35514ddfef7bSEric Sandeen ext4_mb_release_group_pa(struct ext4_buddy *e4b,
3552c83617dbSAneesh Kumar K.V 				struct ext4_prealloc_space *pa,
3553c83617dbSAneesh Kumar K.V 				struct ext4_allocation_context *ac)
3554c9de560dSAlex Tomas {
3555c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
3556c9de560dSAlex Tomas 	ext4_group_t group;
3557c9de560dSAlex Tomas 	ext4_grpblk_t bit;
3558c9de560dSAlex Tomas 
35599bffad1eSTheodore Ts'o 	trace_ext4_mb_release_group_pa(ac, pa);
3560c9de560dSAlex Tomas 	BUG_ON(pa->pa_deleted == 0);
3561c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3562c9de560dSAlex Tomas 	BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3563c9de560dSAlex Tomas 	mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3564c9de560dSAlex Tomas 	atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3565c9de560dSAlex Tomas 
3566256bdb49SEric Sandeen 	if (ac) {
3567256bdb49SEric Sandeen 		ac->ac_sb = sb;
3568256bdb49SEric Sandeen 		ac->ac_inode = NULL;
3569256bdb49SEric Sandeen 		ac->ac_b_ex.fe_group = group;
3570256bdb49SEric Sandeen 		ac->ac_b_ex.fe_start = bit;
3571256bdb49SEric Sandeen 		ac->ac_b_ex.fe_len = pa->pa_len;
3572256bdb49SEric Sandeen 		ac->ac_b_ex.fe_logical = 0;
3573*296c355cSTheodore Ts'o 		trace_ext4_mballoc_discard(ac);
3574256bdb49SEric Sandeen 	}
3575c9de560dSAlex Tomas 
3576c9de560dSAlex Tomas 	return 0;
3577c9de560dSAlex Tomas }
3578c9de560dSAlex Tomas 
3579c9de560dSAlex Tomas /*
3580c9de560dSAlex Tomas  * releases all preallocations in given group
3581c9de560dSAlex Tomas  *
3582c9de560dSAlex Tomas  * first, we need to decide discard policy:
3583c9de560dSAlex Tomas  * - when do we discard
3584c9de560dSAlex Tomas  *   1) ENOSPC
3585c9de560dSAlex Tomas  * - how many do we discard
3586c9de560dSAlex Tomas  *   1) how many requested
3587c9de560dSAlex Tomas  */
35884ddfef7bSEric Sandeen static noinline_for_stack int
35894ddfef7bSEric Sandeen ext4_mb_discard_group_preallocations(struct super_block *sb,
3590c9de560dSAlex Tomas 					ext4_group_t group, int needed)
3591c9de560dSAlex Tomas {
3592c9de560dSAlex Tomas 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3593c9de560dSAlex Tomas 	struct buffer_head *bitmap_bh = NULL;
3594c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa, *tmp;
3595c83617dbSAneesh Kumar K.V 	struct ext4_allocation_context *ac;
3596c9de560dSAlex Tomas 	struct list_head list;
3597c9de560dSAlex Tomas 	struct ext4_buddy e4b;
3598c9de560dSAlex Tomas 	int err;
3599c9de560dSAlex Tomas 	int busy = 0;
3600c9de560dSAlex Tomas 	int free = 0;
3601c9de560dSAlex Tomas 
36026ba495e9STheodore Ts'o 	mb_debug(1, "discard preallocation for group %u\n", group);
3603c9de560dSAlex Tomas 
3604c9de560dSAlex Tomas 	if (list_empty(&grp->bb_prealloc_list))
3605c9de560dSAlex Tomas 		return 0;
3606c9de560dSAlex Tomas 
3607574ca174STheodore Ts'o 	bitmap_bh = ext4_read_block_bitmap(sb, group);
3608c9de560dSAlex Tomas 	if (bitmap_bh == NULL) {
3609ce89f46cSAneesh Kumar K.V 		ext4_error(sb, __func__, "Error in reading block "
3610a9df9a49STheodore Ts'o 				"bitmap for %u", group);
3611ce89f46cSAneesh Kumar K.V 		return 0;
3612c9de560dSAlex Tomas 	}
3613c9de560dSAlex Tomas 
3614c9de560dSAlex Tomas 	err = ext4_mb_load_buddy(sb, group, &e4b);
3615ce89f46cSAneesh Kumar K.V 	if (err) {
3616ce89f46cSAneesh Kumar K.V 		ext4_error(sb, __func__, "Error in loading buddy "
3617a9df9a49STheodore Ts'o 				"information for %u", group);
3618ce89f46cSAneesh Kumar K.V 		put_bh(bitmap_bh);
3619ce89f46cSAneesh Kumar K.V 		return 0;
3620ce89f46cSAneesh Kumar K.V 	}
3621c9de560dSAlex Tomas 
3622c9de560dSAlex Tomas 	if (needed == 0)
3623c9de560dSAlex Tomas 		needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3624c9de560dSAlex Tomas 
3625c9de560dSAlex Tomas 	INIT_LIST_HEAD(&list);
3626c83617dbSAneesh Kumar K.V 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
36279bffad1eSTheodore Ts'o 	if (ac)
36289bffad1eSTheodore Ts'o 		ac->ac_sb = sb;
3629c9de560dSAlex Tomas repeat:
3630c9de560dSAlex Tomas 	ext4_lock_group(sb, group);
3631c9de560dSAlex Tomas 	list_for_each_entry_safe(pa, tmp,
3632c9de560dSAlex Tomas 				&grp->bb_prealloc_list, pa_group_list) {
3633c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3634c9de560dSAlex Tomas 		if (atomic_read(&pa->pa_count)) {
3635c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3636c9de560dSAlex Tomas 			busy = 1;
3637c9de560dSAlex Tomas 			continue;
3638c9de560dSAlex Tomas 		}
3639c9de560dSAlex Tomas 		if (pa->pa_deleted) {
3640c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3641c9de560dSAlex Tomas 			continue;
3642c9de560dSAlex Tomas 		}
3643c9de560dSAlex Tomas 
3644c9de560dSAlex Tomas 		/* seems this one can be freed ... */
3645c9de560dSAlex Tomas 		pa->pa_deleted = 1;
3646c9de560dSAlex Tomas 
3647c9de560dSAlex Tomas 		/* we can trust pa_free ... */
3648c9de560dSAlex Tomas 		free += pa->pa_free;
3649c9de560dSAlex Tomas 
3650c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3651c9de560dSAlex Tomas 
3652c9de560dSAlex Tomas 		list_del(&pa->pa_group_list);
3653c9de560dSAlex Tomas 		list_add(&pa->u.pa_tmp_list, &list);
3654c9de560dSAlex Tomas 	}
3655c9de560dSAlex Tomas 
3656c9de560dSAlex Tomas 	/* if we still need more blocks and some PAs were used, try again */
3657c9de560dSAlex Tomas 	if (free < needed && busy) {
3658c9de560dSAlex Tomas 		busy = 0;
3659c9de560dSAlex Tomas 		ext4_unlock_group(sb, group);
3660c9de560dSAlex Tomas 		/*
3661c9de560dSAlex Tomas 		 * Yield the CPU here so that we don't get soft lockup
3662c9de560dSAlex Tomas 		 * in non preempt case.
3663c9de560dSAlex Tomas 		 */
3664c9de560dSAlex Tomas 		yield();
3665c9de560dSAlex Tomas 		goto repeat;
3666c9de560dSAlex Tomas 	}
3667c9de560dSAlex Tomas 
3668c9de560dSAlex Tomas 	/* found anything to free? */
3669c9de560dSAlex Tomas 	if (list_empty(&list)) {
3670c9de560dSAlex Tomas 		BUG_ON(free != 0);
3671c9de560dSAlex Tomas 		goto out;
3672c9de560dSAlex Tomas 	}
3673c9de560dSAlex Tomas 
3674c9de560dSAlex Tomas 	/* now free all selected PAs */
3675c9de560dSAlex Tomas 	list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3676c9de560dSAlex Tomas 
3677c9de560dSAlex Tomas 		/* remove from object (inode or locality group) */
3678c9de560dSAlex Tomas 		spin_lock(pa->pa_obj_lock);
3679c9de560dSAlex Tomas 		list_del_rcu(&pa->pa_inode_list);
3680c9de560dSAlex Tomas 		spin_unlock(pa->pa_obj_lock);
3681c9de560dSAlex Tomas 
3682cc0fb9adSAneesh Kumar K.V 		if (pa->pa_type == MB_GROUP_PA)
3683c83617dbSAneesh Kumar K.V 			ext4_mb_release_group_pa(&e4b, pa, ac);
3684c9de560dSAlex Tomas 		else
3685c83617dbSAneesh Kumar K.V 			ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
3686c9de560dSAlex Tomas 
3687c9de560dSAlex Tomas 		list_del(&pa->u.pa_tmp_list);
3688c9de560dSAlex Tomas 		call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3689c9de560dSAlex Tomas 	}
3690c9de560dSAlex Tomas 
3691c9de560dSAlex Tomas out:
3692c9de560dSAlex Tomas 	ext4_unlock_group(sb, group);
3693c83617dbSAneesh Kumar K.V 	if (ac)
3694c83617dbSAneesh Kumar K.V 		kmem_cache_free(ext4_ac_cachep, ac);
3695c9de560dSAlex Tomas 	ext4_mb_release_desc(&e4b);
3696c9de560dSAlex Tomas 	put_bh(bitmap_bh);
3697c9de560dSAlex Tomas 	return free;
3698c9de560dSAlex Tomas }
3699c9de560dSAlex Tomas 
3700c9de560dSAlex Tomas /*
3701c9de560dSAlex Tomas  * releases all non-used preallocated blocks for given inode
3702c9de560dSAlex Tomas  *
3703c9de560dSAlex Tomas  * It's important to discard preallocations under i_data_sem
3704c9de560dSAlex Tomas  * We don't want another block to be served from the prealloc
3705c9de560dSAlex Tomas  * space when we are discarding the inode prealloc space.
3706c9de560dSAlex Tomas  *
3707c9de560dSAlex Tomas  * FIXME!! Make sure it is valid at all the call sites
3708c9de560dSAlex Tomas  */
3709c2ea3fdeSTheodore Ts'o void ext4_discard_preallocations(struct inode *inode)
3710c9de560dSAlex Tomas {
3711c9de560dSAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(inode);
3712c9de560dSAlex Tomas 	struct super_block *sb = inode->i_sb;
3713c9de560dSAlex Tomas 	struct buffer_head *bitmap_bh = NULL;
3714c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa, *tmp;
3715c83617dbSAneesh Kumar K.V 	struct ext4_allocation_context *ac;
3716c9de560dSAlex Tomas 	ext4_group_t group = 0;
3717c9de560dSAlex Tomas 	struct list_head list;
3718c9de560dSAlex Tomas 	struct ext4_buddy e4b;
3719c9de560dSAlex Tomas 	int err;
3720c9de560dSAlex Tomas 
3721c2ea3fdeSTheodore Ts'o 	if (!S_ISREG(inode->i_mode)) {
3722c9de560dSAlex Tomas 		/*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3723c9de560dSAlex Tomas 		return;
3724c9de560dSAlex Tomas 	}
3725c9de560dSAlex Tomas 
37266ba495e9STheodore Ts'o 	mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
37279bffad1eSTheodore Ts'o 	trace_ext4_discard_preallocations(inode);
3728c9de560dSAlex Tomas 
3729c9de560dSAlex Tomas 	INIT_LIST_HEAD(&list);
3730c9de560dSAlex Tomas 
3731c83617dbSAneesh Kumar K.V 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
37329bffad1eSTheodore Ts'o 	if (ac) {
37339bffad1eSTheodore Ts'o 		ac->ac_sb = sb;
37349bffad1eSTheodore Ts'o 		ac->ac_inode = inode;
37359bffad1eSTheodore Ts'o 	}
3736c9de560dSAlex Tomas repeat:
3737c9de560dSAlex Tomas 	/* first, collect all pa's in the inode */
3738c9de560dSAlex Tomas 	spin_lock(&ei->i_prealloc_lock);
3739c9de560dSAlex Tomas 	while (!list_empty(&ei->i_prealloc_list)) {
3740c9de560dSAlex Tomas 		pa = list_entry(ei->i_prealloc_list.next,
3741c9de560dSAlex Tomas 				struct ext4_prealloc_space, pa_inode_list);
3742c9de560dSAlex Tomas 		BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3743c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3744c9de560dSAlex Tomas 		if (atomic_read(&pa->pa_count)) {
3745c9de560dSAlex Tomas 			/* this shouldn't happen often - nobody should
3746c9de560dSAlex Tomas 			 * use preallocation while we're discarding it */
3747c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3748c9de560dSAlex Tomas 			spin_unlock(&ei->i_prealloc_lock);
3749c9de560dSAlex Tomas 			printk(KERN_ERR "uh-oh! used pa while discarding\n");
3750c9de560dSAlex Tomas 			WARN_ON(1);
3751c9de560dSAlex Tomas 			schedule_timeout_uninterruptible(HZ);
3752c9de560dSAlex Tomas 			goto repeat;
3753c9de560dSAlex Tomas 
3754c9de560dSAlex Tomas 		}
3755c9de560dSAlex Tomas 		if (pa->pa_deleted == 0) {
3756c9de560dSAlex Tomas 			pa->pa_deleted = 1;
3757c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3758c9de560dSAlex Tomas 			list_del_rcu(&pa->pa_inode_list);
3759c9de560dSAlex Tomas 			list_add(&pa->u.pa_tmp_list, &list);
3760c9de560dSAlex Tomas 			continue;
3761c9de560dSAlex Tomas 		}
3762c9de560dSAlex Tomas 
3763c9de560dSAlex Tomas 		/* someone is deleting pa right now */
3764c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3765c9de560dSAlex Tomas 		spin_unlock(&ei->i_prealloc_lock);
3766c9de560dSAlex Tomas 
3767c9de560dSAlex Tomas 		/* we have to wait here because pa_deleted
3768c9de560dSAlex Tomas 		 * doesn't mean pa is already unlinked from
3769c9de560dSAlex Tomas 		 * the list. as we might be called from
3770c9de560dSAlex Tomas 		 * ->clear_inode() the inode will get freed
3771c9de560dSAlex Tomas 		 * and concurrent thread which is unlinking
3772c9de560dSAlex Tomas 		 * pa from inode's list may access already
3773c9de560dSAlex Tomas 		 * freed memory, bad-bad-bad */
3774c9de560dSAlex Tomas 
3775c9de560dSAlex Tomas 		/* XXX: if this happens too often, we can
3776c9de560dSAlex Tomas 		 * add a flag to force wait only in case
3777c9de560dSAlex Tomas 		 * of ->clear_inode(), but not in case of
3778c9de560dSAlex Tomas 		 * regular truncate */
3779c9de560dSAlex Tomas 		schedule_timeout_uninterruptible(HZ);
3780c9de560dSAlex Tomas 		goto repeat;
3781c9de560dSAlex Tomas 	}
3782c9de560dSAlex Tomas 	spin_unlock(&ei->i_prealloc_lock);
3783c9de560dSAlex Tomas 
3784c9de560dSAlex Tomas 	list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3785cc0fb9adSAneesh Kumar K.V 		BUG_ON(pa->pa_type != MB_INODE_PA);
3786c9de560dSAlex Tomas 		ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3787c9de560dSAlex Tomas 
3788c9de560dSAlex Tomas 		err = ext4_mb_load_buddy(sb, group, &e4b);
3789ce89f46cSAneesh Kumar K.V 		if (err) {
3790ce89f46cSAneesh Kumar K.V 			ext4_error(sb, __func__, "Error in loading buddy "
3791a9df9a49STheodore Ts'o 					"information for %u", group);
3792ce89f46cSAneesh Kumar K.V 			continue;
3793ce89f46cSAneesh Kumar K.V 		}
3794c9de560dSAlex Tomas 
3795574ca174STheodore Ts'o 		bitmap_bh = ext4_read_block_bitmap(sb, group);
3796c9de560dSAlex Tomas 		if (bitmap_bh == NULL) {
3797ce89f46cSAneesh Kumar K.V 			ext4_error(sb, __func__, "Error in reading block "
3798a9df9a49STheodore Ts'o 					"bitmap for %u", group);
3799c9de560dSAlex Tomas 			ext4_mb_release_desc(&e4b);
3800ce89f46cSAneesh Kumar K.V 			continue;
3801c9de560dSAlex Tomas 		}
3802c9de560dSAlex Tomas 
3803c9de560dSAlex Tomas 		ext4_lock_group(sb, group);
3804c9de560dSAlex Tomas 		list_del(&pa->pa_group_list);
3805c83617dbSAneesh Kumar K.V 		ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
3806c9de560dSAlex Tomas 		ext4_unlock_group(sb, group);
3807c9de560dSAlex Tomas 
3808c9de560dSAlex Tomas 		ext4_mb_release_desc(&e4b);
3809c9de560dSAlex Tomas 		put_bh(bitmap_bh);
3810c9de560dSAlex Tomas 
3811c9de560dSAlex Tomas 		list_del(&pa->u.pa_tmp_list);
3812c9de560dSAlex Tomas 		call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3813c9de560dSAlex Tomas 	}
3814c83617dbSAneesh Kumar K.V 	if (ac)
3815c83617dbSAneesh Kumar K.V 		kmem_cache_free(ext4_ac_cachep, ac);
3816c9de560dSAlex Tomas }
3817c9de560dSAlex Tomas 
3818c9de560dSAlex Tomas /*
3819c9de560dSAlex Tomas  * finds all preallocated spaces and return blocks being freed to them
3820c9de560dSAlex Tomas  * if preallocated space becomes full (no block is used from the space)
3821c9de560dSAlex Tomas  * then the function frees space in buddy
3822c9de560dSAlex Tomas  * XXX: at the moment, truncate (which is the only way to free blocks)
3823c9de560dSAlex Tomas  * discards all preallocations
3824c9de560dSAlex Tomas  */
3825c9de560dSAlex Tomas static void ext4_mb_return_to_preallocation(struct inode *inode,
3826c9de560dSAlex Tomas 					struct ext4_buddy *e4b,
3827c9de560dSAlex Tomas 					sector_t block, int count)
3828c9de560dSAlex Tomas {
3829c9de560dSAlex Tomas 	BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
3830c9de560dSAlex Tomas }
38316ba495e9STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
3832c9de560dSAlex Tomas static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3833c9de560dSAlex Tomas {
3834c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
38358df9675fSTheodore Ts'o 	ext4_group_t ngroups, i;
3836c9de560dSAlex Tomas 
3837c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: Can't allocate:"
3838c9de560dSAlex Tomas 			" Allocation context details:\n");
3839c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3840c9de560dSAlex Tomas 			ac->ac_status, ac->ac_flags);
3841c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3842c9de560dSAlex Tomas 			"best %lu/%lu/%lu@%lu cr %d\n",
3843c9de560dSAlex Tomas 			(unsigned long)ac->ac_o_ex.fe_group,
3844c9de560dSAlex Tomas 			(unsigned long)ac->ac_o_ex.fe_start,
3845c9de560dSAlex Tomas 			(unsigned long)ac->ac_o_ex.fe_len,
3846c9de560dSAlex Tomas 			(unsigned long)ac->ac_o_ex.fe_logical,
3847c9de560dSAlex Tomas 			(unsigned long)ac->ac_g_ex.fe_group,
3848c9de560dSAlex Tomas 			(unsigned long)ac->ac_g_ex.fe_start,
3849c9de560dSAlex Tomas 			(unsigned long)ac->ac_g_ex.fe_len,
3850c9de560dSAlex Tomas 			(unsigned long)ac->ac_g_ex.fe_logical,
3851c9de560dSAlex Tomas 			(unsigned long)ac->ac_b_ex.fe_group,
3852c9de560dSAlex Tomas 			(unsigned long)ac->ac_b_ex.fe_start,
3853c9de560dSAlex Tomas 			(unsigned long)ac->ac_b_ex.fe_len,
3854c9de560dSAlex Tomas 			(unsigned long)ac->ac_b_ex.fe_logical,
3855c9de560dSAlex Tomas 			(int)ac->ac_criteria);
3856c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3857c9de560dSAlex Tomas 		ac->ac_found);
3858c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: groups: \n");
38598df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
38608df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3861c9de560dSAlex Tomas 		struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3862c9de560dSAlex Tomas 		struct ext4_prealloc_space *pa;
3863c9de560dSAlex Tomas 		ext4_grpblk_t start;
3864c9de560dSAlex Tomas 		struct list_head *cur;
3865c9de560dSAlex Tomas 		ext4_lock_group(sb, i);
3866c9de560dSAlex Tomas 		list_for_each(cur, &grp->bb_prealloc_list) {
3867c9de560dSAlex Tomas 			pa = list_entry(cur, struct ext4_prealloc_space,
3868c9de560dSAlex Tomas 					pa_group_list);
3869c9de560dSAlex Tomas 			spin_lock(&pa->pa_lock);
3870c9de560dSAlex Tomas 			ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3871c9de560dSAlex Tomas 						     NULL, &start);
3872c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
38731c718505SAkira Fujita 			printk(KERN_ERR "PA:%u:%d:%u \n", i,
3874c9de560dSAlex Tomas 			       start, pa->pa_len);
3875c9de560dSAlex Tomas 		}
387660bd63d1SSolofo Ramangalahy 		ext4_unlock_group(sb, i);
3877c9de560dSAlex Tomas 
3878c9de560dSAlex Tomas 		if (grp->bb_free == 0)
3879c9de560dSAlex Tomas 			continue;
38801c718505SAkira Fujita 		printk(KERN_ERR "%u: %d/%d \n",
3881c9de560dSAlex Tomas 		       i, grp->bb_free, grp->bb_fragments);
3882c9de560dSAlex Tomas 	}
3883c9de560dSAlex Tomas 	printk(KERN_ERR "\n");
3884c9de560dSAlex Tomas }
3885c9de560dSAlex Tomas #else
3886c9de560dSAlex Tomas static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3887c9de560dSAlex Tomas {
3888c9de560dSAlex Tomas 	return;
3889c9de560dSAlex Tomas }
3890c9de560dSAlex Tomas #endif
3891c9de560dSAlex Tomas 
3892c9de560dSAlex Tomas /*
3893c9de560dSAlex Tomas  * We use locality group preallocation for small size file. The size of the
3894c9de560dSAlex Tomas  * file is determined by the current size or the resulting size after
3895c9de560dSAlex Tomas  * allocation which ever is larger
3896c9de560dSAlex Tomas  *
3897b713a5ecSTheodore Ts'o  * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
3898c9de560dSAlex Tomas  */
3899c9de560dSAlex Tomas static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3900c9de560dSAlex Tomas {
3901c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3902c9de560dSAlex Tomas 	int bsbits = ac->ac_sb->s_blocksize_bits;
3903c9de560dSAlex Tomas 	loff_t size, isize;
3904c9de560dSAlex Tomas 
3905c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3906c9de560dSAlex Tomas 		return;
3907c9de560dSAlex Tomas 
39084ba74d00STheodore Ts'o 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
39094ba74d00STheodore Ts'o 		return;
39104ba74d00STheodore Ts'o 
3911c9de560dSAlex Tomas 	size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
391250797481STheodore Ts'o 	isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
391350797481STheodore Ts'o 		>> bsbits;
3914c9de560dSAlex Tomas 
391550797481STheodore Ts'o 	if ((size == isize) &&
391650797481STheodore Ts'o 	    !ext4_fs_is_busy(sbi) &&
391750797481STheodore Ts'o 	    (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
391850797481STheodore Ts'o 		ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
391950797481STheodore Ts'o 		return;
392050797481STheodore Ts'o 	}
392150797481STheodore Ts'o 
3922c9de560dSAlex Tomas 	/* don't use group allocation for large files */
392371780577STheodore Ts'o 	size = max(size, isize);
39244ba74d00STheodore Ts'o 	if (size >= sbi->s_mb_stream_request) {
39254ba74d00STheodore Ts'o 		ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
3926c9de560dSAlex Tomas 		return;
39274ba74d00STheodore Ts'o 	}
3928c9de560dSAlex Tomas 
3929c9de560dSAlex Tomas 	BUG_ON(ac->ac_lg != NULL);
3930c9de560dSAlex Tomas 	/*
3931c9de560dSAlex Tomas 	 * locality group prealloc space are per cpu. The reason for having
3932c9de560dSAlex Tomas 	 * per cpu locality group is to reduce the contention between block
3933c9de560dSAlex Tomas 	 * request from multiple CPUs.
3934c9de560dSAlex Tomas 	 */
3935730c213cSEric Sandeen 	ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
3936c9de560dSAlex Tomas 
3937c9de560dSAlex Tomas 	/* we're going to use group allocation */
3938c9de560dSAlex Tomas 	ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3939c9de560dSAlex Tomas 
3940c9de560dSAlex Tomas 	/* serialize all allocations in the group */
3941c9de560dSAlex Tomas 	mutex_lock(&ac->ac_lg->lg_mutex);
3942c9de560dSAlex Tomas }
3943c9de560dSAlex Tomas 
39444ddfef7bSEric Sandeen static noinline_for_stack int
39454ddfef7bSEric Sandeen ext4_mb_initialize_context(struct ext4_allocation_context *ac,
3946c9de560dSAlex Tomas 				struct ext4_allocation_request *ar)
3947c9de560dSAlex Tomas {
3948c9de560dSAlex Tomas 	struct super_block *sb = ar->inode->i_sb;
3949c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3950c9de560dSAlex Tomas 	struct ext4_super_block *es = sbi->s_es;
3951c9de560dSAlex Tomas 	ext4_group_t group;
3952498e5f24STheodore Ts'o 	unsigned int len;
3953498e5f24STheodore Ts'o 	ext4_fsblk_t goal;
3954c9de560dSAlex Tomas 	ext4_grpblk_t block;
3955c9de560dSAlex Tomas 
3956c9de560dSAlex Tomas 	/* we can't allocate > group size */
3957c9de560dSAlex Tomas 	len = ar->len;
3958c9de560dSAlex Tomas 
3959c9de560dSAlex Tomas 	/* just a dirty hack to filter too big requests  */
3960c9de560dSAlex Tomas 	if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
3961c9de560dSAlex Tomas 		len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
3962c9de560dSAlex Tomas 
3963c9de560dSAlex Tomas 	/* start searching from the goal */
3964c9de560dSAlex Tomas 	goal = ar->goal;
3965c9de560dSAlex Tomas 	if (goal < le32_to_cpu(es->s_first_data_block) ||
3966c9de560dSAlex Tomas 			goal >= ext4_blocks_count(es))
3967c9de560dSAlex Tomas 		goal = le32_to_cpu(es->s_first_data_block);
3968c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(sb, goal, &group, &block);
3969c9de560dSAlex Tomas 
3970c9de560dSAlex Tomas 	/* set up allocation goals */
3971833576b3STheodore Ts'o 	memset(ac, 0, sizeof(struct ext4_allocation_context));
3972c9de560dSAlex Tomas 	ac->ac_b_ex.fe_logical = ar->logical;
3973c9de560dSAlex Tomas 	ac->ac_status = AC_STATUS_CONTINUE;
3974c9de560dSAlex Tomas 	ac->ac_sb = sb;
3975c9de560dSAlex Tomas 	ac->ac_inode = ar->inode;
3976c9de560dSAlex Tomas 	ac->ac_o_ex.fe_logical = ar->logical;
3977c9de560dSAlex Tomas 	ac->ac_o_ex.fe_group = group;
3978c9de560dSAlex Tomas 	ac->ac_o_ex.fe_start = block;
3979c9de560dSAlex Tomas 	ac->ac_o_ex.fe_len = len;
3980c9de560dSAlex Tomas 	ac->ac_g_ex.fe_logical = ar->logical;
3981c9de560dSAlex Tomas 	ac->ac_g_ex.fe_group = group;
3982c9de560dSAlex Tomas 	ac->ac_g_ex.fe_start = block;
3983c9de560dSAlex Tomas 	ac->ac_g_ex.fe_len = len;
3984c9de560dSAlex Tomas 	ac->ac_flags = ar->flags;
3985c9de560dSAlex Tomas 
3986c9de560dSAlex Tomas 	/* we have to define context: we'll we work with a file or
3987c9de560dSAlex Tomas 	 * locality group. this is a policy, actually */
3988c9de560dSAlex Tomas 	ext4_mb_group_or_file(ac);
3989c9de560dSAlex Tomas 
39906ba495e9STheodore Ts'o 	mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
3991c9de560dSAlex Tomas 			"left: %u/%u, right %u/%u to %swritable\n",
3992c9de560dSAlex Tomas 			(unsigned) ar->len, (unsigned) ar->logical,
3993c9de560dSAlex Tomas 			(unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
3994c9de560dSAlex Tomas 			(unsigned) ar->lleft, (unsigned) ar->pleft,
3995c9de560dSAlex Tomas 			(unsigned) ar->lright, (unsigned) ar->pright,
3996c9de560dSAlex Tomas 			atomic_read(&ar->inode->i_writecount) ? "" : "non-");
3997c9de560dSAlex Tomas 	return 0;
3998c9de560dSAlex Tomas 
3999c9de560dSAlex Tomas }
4000c9de560dSAlex Tomas 
40016be2ded1SAneesh Kumar K.V static noinline_for_stack void
40026be2ded1SAneesh Kumar K.V ext4_mb_discard_lg_preallocations(struct super_block *sb,
40036be2ded1SAneesh Kumar K.V 					struct ext4_locality_group *lg,
40046be2ded1SAneesh Kumar K.V 					int order, int total_entries)
40056be2ded1SAneesh Kumar K.V {
40066be2ded1SAneesh Kumar K.V 	ext4_group_t group = 0;
40076be2ded1SAneesh Kumar K.V 	struct ext4_buddy e4b;
40086be2ded1SAneesh Kumar K.V 	struct list_head discard_list;
40096be2ded1SAneesh Kumar K.V 	struct ext4_prealloc_space *pa, *tmp;
40106be2ded1SAneesh Kumar K.V 	struct ext4_allocation_context *ac;
40116be2ded1SAneesh Kumar K.V 
40126ba495e9STheodore Ts'o 	mb_debug(1, "discard locality group preallocation\n");
40136be2ded1SAneesh Kumar K.V 
40146be2ded1SAneesh Kumar K.V 	INIT_LIST_HEAD(&discard_list);
40156be2ded1SAneesh Kumar K.V 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
40169bffad1eSTheodore Ts'o 	if (ac)
40179bffad1eSTheodore Ts'o 		ac->ac_sb = sb;
40186be2ded1SAneesh Kumar K.V 
40196be2ded1SAneesh Kumar K.V 	spin_lock(&lg->lg_prealloc_lock);
40206be2ded1SAneesh Kumar K.V 	list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
40216be2ded1SAneesh Kumar K.V 						pa_inode_list) {
40226be2ded1SAneesh Kumar K.V 		spin_lock(&pa->pa_lock);
40236be2ded1SAneesh Kumar K.V 		if (atomic_read(&pa->pa_count)) {
40246be2ded1SAneesh Kumar K.V 			/*
40256be2ded1SAneesh Kumar K.V 			 * This is the pa that we just used
40266be2ded1SAneesh Kumar K.V 			 * for block allocation. So don't
40276be2ded1SAneesh Kumar K.V 			 * free that
40286be2ded1SAneesh Kumar K.V 			 */
40296be2ded1SAneesh Kumar K.V 			spin_unlock(&pa->pa_lock);
40306be2ded1SAneesh Kumar K.V 			continue;
40316be2ded1SAneesh Kumar K.V 		}
40326be2ded1SAneesh Kumar K.V 		if (pa->pa_deleted) {
40336be2ded1SAneesh Kumar K.V 			spin_unlock(&pa->pa_lock);
40346be2ded1SAneesh Kumar K.V 			continue;
40356be2ded1SAneesh Kumar K.V 		}
40366be2ded1SAneesh Kumar K.V 		/* only lg prealloc space */
4037cc0fb9adSAneesh Kumar K.V 		BUG_ON(pa->pa_type != MB_GROUP_PA);
40386be2ded1SAneesh Kumar K.V 
40396be2ded1SAneesh Kumar K.V 		/* seems this one can be freed ... */
40406be2ded1SAneesh Kumar K.V 		pa->pa_deleted = 1;
40416be2ded1SAneesh Kumar K.V 		spin_unlock(&pa->pa_lock);
40426be2ded1SAneesh Kumar K.V 
40436be2ded1SAneesh Kumar K.V 		list_del_rcu(&pa->pa_inode_list);
40446be2ded1SAneesh Kumar K.V 		list_add(&pa->u.pa_tmp_list, &discard_list);
40456be2ded1SAneesh Kumar K.V 
40466be2ded1SAneesh Kumar K.V 		total_entries--;
40476be2ded1SAneesh Kumar K.V 		if (total_entries <= 5) {
40486be2ded1SAneesh Kumar K.V 			/*
40496be2ded1SAneesh Kumar K.V 			 * we want to keep only 5 entries
40506be2ded1SAneesh Kumar K.V 			 * allowing it to grow to 8. This
40516be2ded1SAneesh Kumar K.V 			 * mak sure we don't call discard
40526be2ded1SAneesh Kumar K.V 			 * soon for this list.
40536be2ded1SAneesh Kumar K.V 			 */
40546be2ded1SAneesh Kumar K.V 			break;
40556be2ded1SAneesh Kumar K.V 		}
40566be2ded1SAneesh Kumar K.V 	}
40576be2ded1SAneesh Kumar K.V 	spin_unlock(&lg->lg_prealloc_lock);
40586be2ded1SAneesh Kumar K.V 
40596be2ded1SAneesh Kumar K.V 	list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
40606be2ded1SAneesh Kumar K.V 
40616be2ded1SAneesh Kumar K.V 		ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
40626be2ded1SAneesh Kumar K.V 		if (ext4_mb_load_buddy(sb, group, &e4b)) {
40636be2ded1SAneesh Kumar K.V 			ext4_error(sb, __func__, "Error in loading buddy "
4064a9df9a49STheodore Ts'o 					"information for %u", group);
40656be2ded1SAneesh Kumar K.V 			continue;
40666be2ded1SAneesh Kumar K.V 		}
40676be2ded1SAneesh Kumar K.V 		ext4_lock_group(sb, group);
40686be2ded1SAneesh Kumar K.V 		list_del(&pa->pa_group_list);
40696be2ded1SAneesh Kumar K.V 		ext4_mb_release_group_pa(&e4b, pa, ac);
40706be2ded1SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
40716be2ded1SAneesh Kumar K.V 
40726be2ded1SAneesh Kumar K.V 		ext4_mb_release_desc(&e4b);
40736be2ded1SAneesh Kumar K.V 		list_del(&pa->u.pa_tmp_list);
40746be2ded1SAneesh Kumar K.V 		call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
40756be2ded1SAneesh Kumar K.V 	}
40766be2ded1SAneesh Kumar K.V 	if (ac)
40776be2ded1SAneesh Kumar K.V 		kmem_cache_free(ext4_ac_cachep, ac);
40786be2ded1SAneesh Kumar K.V }
40796be2ded1SAneesh Kumar K.V 
40806be2ded1SAneesh Kumar K.V /*
40816be2ded1SAneesh Kumar K.V  * We have incremented pa_count. So it cannot be freed at this
40826be2ded1SAneesh Kumar K.V  * point. Also we hold lg_mutex. So no parallel allocation is
40836be2ded1SAneesh Kumar K.V  * possible from this lg. That means pa_free cannot be updated.
40846be2ded1SAneesh Kumar K.V  *
40856be2ded1SAneesh Kumar K.V  * A parallel ext4_mb_discard_group_preallocations is possible.
40866be2ded1SAneesh Kumar K.V  * which can cause the lg_prealloc_list to be updated.
40876be2ded1SAneesh Kumar K.V  */
40886be2ded1SAneesh Kumar K.V 
40896be2ded1SAneesh Kumar K.V static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
40906be2ded1SAneesh Kumar K.V {
40916be2ded1SAneesh Kumar K.V 	int order, added = 0, lg_prealloc_count = 1;
40926be2ded1SAneesh Kumar K.V 	struct super_block *sb = ac->ac_sb;
40936be2ded1SAneesh Kumar K.V 	struct ext4_locality_group *lg = ac->ac_lg;
40946be2ded1SAneesh Kumar K.V 	struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
40956be2ded1SAneesh Kumar K.V 
40966be2ded1SAneesh Kumar K.V 	order = fls(pa->pa_free) - 1;
40976be2ded1SAneesh Kumar K.V 	if (order > PREALLOC_TB_SIZE - 1)
40986be2ded1SAneesh Kumar K.V 		/* The max size of hash table is PREALLOC_TB_SIZE */
40996be2ded1SAneesh Kumar K.V 		order = PREALLOC_TB_SIZE - 1;
41006be2ded1SAneesh Kumar K.V 	/* Add the prealloc space to lg */
41016be2ded1SAneesh Kumar K.V 	rcu_read_lock();
41026be2ded1SAneesh Kumar K.V 	list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
41036be2ded1SAneesh Kumar K.V 						pa_inode_list) {
41046be2ded1SAneesh Kumar K.V 		spin_lock(&tmp_pa->pa_lock);
41056be2ded1SAneesh Kumar K.V 		if (tmp_pa->pa_deleted) {
4106e7c9e3e9STheodore Ts'o 			spin_unlock(&tmp_pa->pa_lock);
41076be2ded1SAneesh Kumar K.V 			continue;
41086be2ded1SAneesh Kumar K.V 		}
41096be2ded1SAneesh Kumar K.V 		if (!added && pa->pa_free < tmp_pa->pa_free) {
41106be2ded1SAneesh Kumar K.V 			/* Add to the tail of the previous entry */
41116be2ded1SAneesh Kumar K.V 			list_add_tail_rcu(&pa->pa_inode_list,
41126be2ded1SAneesh Kumar K.V 						&tmp_pa->pa_inode_list);
41136be2ded1SAneesh Kumar K.V 			added = 1;
41146be2ded1SAneesh Kumar K.V 			/*
41156be2ded1SAneesh Kumar K.V 			 * we want to count the total
41166be2ded1SAneesh Kumar K.V 			 * number of entries in the list
41176be2ded1SAneesh Kumar K.V 			 */
41186be2ded1SAneesh Kumar K.V 		}
41196be2ded1SAneesh Kumar K.V 		spin_unlock(&tmp_pa->pa_lock);
41206be2ded1SAneesh Kumar K.V 		lg_prealloc_count++;
41216be2ded1SAneesh Kumar K.V 	}
41226be2ded1SAneesh Kumar K.V 	if (!added)
41236be2ded1SAneesh Kumar K.V 		list_add_tail_rcu(&pa->pa_inode_list,
41246be2ded1SAneesh Kumar K.V 					&lg->lg_prealloc_list[order]);
41256be2ded1SAneesh Kumar K.V 	rcu_read_unlock();
41266be2ded1SAneesh Kumar K.V 
41276be2ded1SAneesh Kumar K.V 	/* Now trim the list to be not more than 8 elements */
41286be2ded1SAneesh Kumar K.V 	if (lg_prealloc_count > 8) {
41296be2ded1SAneesh Kumar K.V 		ext4_mb_discard_lg_preallocations(sb, lg,
41306be2ded1SAneesh Kumar K.V 						order, lg_prealloc_count);
41316be2ded1SAneesh Kumar K.V 		return;
41326be2ded1SAneesh Kumar K.V 	}
41336be2ded1SAneesh Kumar K.V 	return ;
41346be2ded1SAneesh Kumar K.V }
41356be2ded1SAneesh Kumar K.V 
4136c9de560dSAlex Tomas /*
4137c9de560dSAlex Tomas  * release all resource we used in allocation
4138c9de560dSAlex Tomas  */
4139c9de560dSAlex Tomas static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4140c9de560dSAlex Tomas {
41416be2ded1SAneesh Kumar K.V 	struct ext4_prealloc_space *pa = ac->ac_pa;
41426be2ded1SAneesh Kumar K.V 	if (pa) {
4143cc0fb9adSAneesh Kumar K.V 		if (pa->pa_type == MB_GROUP_PA) {
4144c9de560dSAlex Tomas 			/* see comment in ext4_mb_use_group_pa() */
41456be2ded1SAneesh Kumar K.V 			spin_lock(&pa->pa_lock);
41466be2ded1SAneesh Kumar K.V 			pa->pa_pstart += ac->ac_b_ex.fe_len;
41476be2ded1SAneesh Kumar K.V 			pa->pa_lstart += ac->ac_b_ex.fe_len;
41486be2ded1SAneesh Kumar K.V 			pa->pa_free -= ac->ac_b_ex.fe_len;
41496be2ded1SAneesh Kumar K.V 			pa->pa_len -= ac->ac_b_ex.fe_len;
41506be2ded1SAneesh Kumar K.V 			spin_unlock(&pa->pa_lock);
4151ba443916SAneesh Kumar K.V 		}
4152ba443916SAneesh Kumar K.V 	}
4153ba443916SAneesh Kumar K.V 	if (ac->alloc_semp)
4154ba443916SAneesh Kumar K.V 		up_read(ac->alloc_semp);
4155ba443916SAneesh Kumar K.V 	if (pa) {
41566be2ded1SAneesh Kumar K.V 		/*
41576be2ded1SAneesh Kumar K.V 		 * We want to add the pa to the right bucket.
41586be2ded1SAneesh Kumar K.V 		 * Remove it from the list and while adding
41596be2ded1SAneesh Kumar K.V 		 * make sure the list to which we are adding
4160ba443916SAneesh Kumar K.V 		 * doesn't grow big.  We need to release
4161ba443916SAneesh Kumar K.V 		 * alloc_semp before calling ext4_mb_add_n_trim()
41626be2ded1SAneesh Kumar K.V 		 */
4163cc0fb9adSAneesh Kumar K.V 		if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
41646be2ded1SAneesh Kumar K.V 			spin_lock(pa->pa_obj_lock);
41656be2ded1SAneesh Kumar K.V 			list_del_rcu(&pa->pa_inode_list);
41666be2ded1SAneesh Kumar K.V 			spin_unlock(pa->pa_obj_lock);
41676be2ded1SAneesh Kumar K.V 			ext4_mb_add_n_trim(ac);
4168c9de560dSAlex Tomas 		}
41696be2ded1SAneesh Kumar K.V 		ext4_mb_put_pa(ac, ac->ac_sb, pa);
4170c9de560dSAlex Tomas 	}
4171c9de560dSAlex Tomas 	if (ac->ac_bitmap_page)
4172c9de560dSAlex Tomas 		page_cache_release(ac->ac_bitmap_page);
4173c9de560dSAlex Tomas 	if (ac->ac_buddy_page)
4174c9de560dSAlex Tomas 		page_cache_release(ac->ac_buddy_page);
4175c9de560dSAlex Tomas 	if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4176c9de560dSAlex Tomas 		mutex_unlock(&ac->ac_lg->lg_mutex);
4177c9de560dSAlex Tomas 	ext4_mb_collect_stats(ac);
4178c9de560dSAlex Tomas 	return 0;
4179c9de560dSAlex Tomas }
4180c9de560dSAlex Tomas 
4181c9de560dSAlex Tomas static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4182c9de560dSAlex Tomas {
41838df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4184c9de560dSAlex Tomas 	int ret;
4185c9de560dSAlex Tomas 	int freed = 0;
4186c9de560dSAlex Tomas 
41879bffad1eSTheodore Ts'o 	trace_ext4_mb_discard_preallocations(sb, needed);
41888df9675fSTheodore Ts'o 	for (i = 0; i < ngroups && needed > 0; i++) {
4189c9de560dSAlex Tomas 		ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4190c9de560dSAlex Tomas 		freed += ret;
4191c9de560dSAlex Tomas 		needed -= ret;
4192c9de560dSAlex Tomas 	}
4193c9de560dSAlex Tomas 
4194c9de560dSAlex Tomas 	return freed;
4195c9de560dSAlex Tomas }
4196c9de560dSAlex Tomas 
4197c9de560dSAlex Tomas /*
4198c9de560dSAlex Tomas  * Main entry point into mballoc to allocate blocks
4199c9de560dSAlex Tomas  * it tries to use preallocation first, then falls back
4200c9de560dSAlex Tomas  * to usual allocation
4201c9de560dSAlex Tomas  */
4202c9de560dSAlex Tomas ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4203c9de560dSAlex Tomas 				 struct ext4_allocation_request *ar, int *errp)
4204c9de560dSAlex Tomas {
42056bc6e63fSAneesh Kumar K.V 	int freed;
4206256bdb49SEric Sandeen 	struct ext4_allocation_context *ac = NULL;
4207c9de560dSAlex Tomas 	struct ext4_sb_info *sbi;
4208c9de560dSAlex Tomas 	struct super_block *sb;
4209c9de560dSAlex Tomas 	ext4_fsblk_t block = 0;
421060e58e0fSMingming Cao 	unsigned int inquota = 0;
4211498e5f24STheodore Ts'o 	unsigned int reserv_blks = 0;
4212c9de560dSAlex Tomas 
4213c9de560dSAlex Tomas 	sb = ar->inode->i_sb;
4214c9de560dSAlex Tomas 	sbi = EXT4_SB(sb);
4215c9de560dSAlex Tomas 
42169bffad1eSTheodore Ts'o 	trace_ext4_request_blocks(ar);
4217ba80b101STheodore Ts'o 
4218d2a17637SMingming Cao 	/*
421960e58e0fSMingming Cao 	 * For delayed allocation, we could skip the ENOSPC and
422060e58e0fSMingming Cao 	 * EDQUOT check, as blocks and quotas have been already
422160e58e0fSMingming Cao 	 * reserved when data being copied into pagecache.
422260e58e0fSMingming Cao 	 */
422360e58e0fSMingming Cao 	if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
422460e58e0fSMingming Cao 		ar->flags |= EXT4_MB_DELALLOC_RESERVED;
422560e58e0fSMingming Cao 	else {
422660e58e0fSMingming Cao 		/* Without delayed allocation we need to verify
422760e58e0fSMingming Cao 		 * there is enough free blocks to do block allocation
422860e58e0fSMingming Cao 		 * and verify allocation doesn't exceed the quota limits.
4229d2a17637SMingming Cao 		 */
4230030ba6bcSAneesh Kumar K.V 		while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4231030ba6bcSAneesh Kumar K.V 			/* let others to free the space */
4232030ba6bcSAneesh Kumar K.V 			yield();
4233030ba6bcSAneesh Kumar K.V 			ar->len = ar->len >> 1;
4234030ba6bcSAneesh Kumar K.V 		}
4235030ba6bcSAneesh Kumar K.V 		if (!ar->len) {
423607031431SMingming Cao 			*errp = -ENOSPC;
423707031431SMingming Cao 			return 0;
423807031431SMingming Cao 		}
42396bc6e63fSAneesh Kumar K.V 		reserv_blks = ar->len;
4240a269eb18SJan Kara 		while (ar->len && vfs_dq_alloc_block(ar->inode, ar->len)) {
4241c9de560dSAlex Tomas 			ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4242c9de560dSAlex Tomas 			ar->len--;
4243c9de560dSAlex Tomas 		}
424460e58e0fSMingming Cao 		inquota = ar->len;
4245c9de560dSAlex Tomas 		if (ar->len == 0) {
4246c9de560dSAlex Tomas 			*errp = -EDQUOT;
42470087d9fbSAneesh Kumar K.V 			goto out3;
4248c9de560dSAlex Tomas 		}
424960e58e0fSMingming Cao 	}
4250d2a17637SMingming Cao 
4251256bdb49SEric Sandeen 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4252833576b3STheodore Ts'o 	if (!ac) {
4253363d4251SShen Feng 		ar->len = 0;
4254256bdb49SEric Sandeen 		*errp = -ENOMEM;
4255363d4251SShen Feng 		goto out1;
4256256bdb49SEric Sandeen 	}
4257256bdb49SEric Sandeen 
4258256bdb49SEric Sandeen 	*errp = ext4_mb_initialize_context(ac, ar);
4259c9de560dSAlex Tomas 	if (*errp) {
4260c9de560dSAlex Tomas 		ar->len = 0;
4261363d4251SShen Feng 		goto out2;
4262c9de560dSAlex Tomas 	}
4263c9de560dSAlex Tomas 
4264256bdb49SEric Sandeen 	ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4265256bdb49SEric Sandeen 	if (!ext4_mb_use_preallocated(ac)) {
4266256bdb49SEric Sandeen 		ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4267256bdb49SEric Sandeen 		ext4_mb_normalize_request(ac, ar);
4268c9de560dSAlex Tomas repeat:
4269c9de560dSAlex Tomas 		/* allocate space in core */
4270256bdb49SEric Sandeen 		ext4_mb_regular_allocator(ac);
4271c9de560dSAlex Tomas 
4272c9de560dSAlex Tomas 		/* as we've just preallocated more space than
4273c9de560dSAlex Tomas 		 * user requested orinally, we store allocated
4274c9de560dSAlex Tomas 		 * space in a special descriptor */
4275256bdb49SEric Sandeen 		if (ac->ac_status == AC_STATUS_FOUND &&
4276256bdb49SEric Sandeen 				ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4277256bdb49SEric Sandeen 			ext4_mb_new_preallocation(ac);
4278c9de560dSAlex Tomas 	}
4279256bdb49SEric Sandeen 	if (likely(ac->ac_status == AC_STATUS_FOUND)) {
42806bc6e63fSAneesh Kumar K.V 		*errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
4281519deca0SAneesh Kumar K.V 		if (*errp ==  -EAGAIN) {
42828556e8f3SAneesh Kumar K.V 			/*
42838556e8f3SAneesh Kumar K.V 			 * drop the reference that we took
42848556e8f3SAneesh Kumar K.V 			 * in ext4_mb_use_best_found
42858556e8f3SAneesh Kumar K.V 			 */
42868556e8f3SAneesh Kumar K.V 			ext4_mb_release_context(ac);
4287519deca0SAneesh Kumar K.V 			ac->ac_b_ex.fe_group = 0;
4288519deca0SAneesh Kumar K.V 			ac->ac_b_ex.fe_start = 0;
4289519deca0SAneesh Kumar K.V 			ac->ac_b_ex.fe_len = 0;
4290519deca0SAneesh Kumar K.V 			ac->ac_status = AC_STATUS_CONTINUE;
4291519deca0SAneesh Kumar K.V 			goto repeat;
4292519deca0SAneesh Kumar K.V 		} else if (*errp) {
4293519deca0SAneesh Kumar K.V 			ac->ac_b_ex.fe_len = 0;
4294519deca0SAneesh Kumar K.V 			ar->len = 0;
4295519deca0SAneesh Kumar K.V 			ext4_mb_show_ac(ac);
4296519deca0SAneesh Kumar K.V 		} else {
4297256bdb49SEric Sandeen 			block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4298256bdb49SEric Sandeen 			ar->len = ac->ac_b_ex.fe_len;
4299519deca0SAneesh Kumar K.V 		}
4300c9de560dSAlex Tomas 	} else {
4301256bdb49SEric Sandeen 		freed  = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
4302c9de560dSAlex Tomas 		if (freed)
4303c9de560dSAlex Tomas 			goto repeat;
4304c9de560dSAlex Tomas 		*errp = -ENOSPC;
4305256bdb49SEric Sandeen 		ac->ac_b_ex.fe_len = 0;
4306c9de560dSAlex Tomas 		ar->len = 0;
4307256bdb49SEric Sandeen 		ext4_mb_show_ac(ac);
4308c9de560dSAlex Tomas 	}
4309c9de560dSAlex Tomas 
4310256bdb49SEric Sandeen 	ext4_mb_release_context(ac);
4311c9de560dSAlex Tomas 
4312363d4251SShen Feng out2:
4313363d4251SShen Feng 	kmem_cache_free(ext4_ac_cachep, ac);
4314363d4251SShen Feng out1:
431560e58e0fSMingming Cao 	if (inquota && ar->len < inquota)
4316a269eb18SJan Kara 		vfs_dq_free_block(ar->inode, inquota - ar->len);
43170087d9fbSAneesh Kumar K.V out3:
43180087d9fbSAneesh Kumar K.V 	if (!ar->len) {
43190087d9fbSAneesh Kumar K.V 		if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
43200087d9fbSAneesh Kumar K.V 			/* release all the reserved blocks if non delalloc */
43210087d9fbSAneesh Kumar K.V 			percpu_counter_sub(&sbi->s_dirtyblocks_counter,
43220087d9fbSAneesh Kumar K.V 						reserv_blks);
43230087d9fbSAneesh Kumar K.V 	}
4324c9de560dSAlex Tomas 
43259bffad1eSTheodore Ts'o 	trace_ext4_allocate_blocks(ar, (unsigned long long)block);
4326ba80b101STheodore Ts'o 
4327c9de560dSAlex Tomas 	return block;
4328c9de560dSAlex Tomas }
4329c9de560dSAlex Tomas 
4330c894058dSAneesh Kumar K.V /*
4331c894058dSAneesh Kumar K.V  * We can merge two free data extents only if the physical blocks
4332c894058dSAneesh Kumar K.V  * are contiguous, AND the extents were freed by the same transaction,
4333c894058dSAneesh Kumar K.V  * AND the blocks are associated with the same group.
4334c894058dSAneesh Kumar K.V  */
4335c894058dSAneesh Kumar K.V static int can_merge(struct ext4_free_data *entry1,
4336c894058dSAneesh Kumar K.V 			struct ext4_free_data *entry2)
4337c894058dSAneesh Kumar K.V {
4338c894058dSAneesh Kumar K.V 	if ((entry1->t_tid == entry2->t_tid) &&
4339c894058dSAneesh Kumar K.V 	    (entry1->group == entry2->group) &&
4340c894058dSAneesh Kumar K.V 	    ((entry1->start_blk + entry1->count) == entry2->start_blk))
4341c894058dSAneesh Kumar K.V 		return 1;
4342c894058dSAneesh Kumar K.V 	return 0;
4343c894058dSAneesh Kumar K.V }
4344c894058dSAneesh Kumar K.V 
43454ddfef7bSEric Sandeen static noinline_for_stack int
43464ddfef7bSEric Sandeen ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
43477a2fcbf7SAneesh Kumar K.V 		      struct ext4_free_data *new_entry)
4348c9de560dSAlex Tomas {
43497a2fcbf7SAneesh Kumar K.V 	ext4_grpblk_t block;
43507a2fcbf7SAneesh Kumar K.V 	struct ext4_free_data *entry;
4351c9de560dSAlex Tomas 	struct ext4_group_info *db = e4b->bd_info;
4352c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
4353c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4354c894058dSAneesh Kumar K.V 	struct rb_node **n = &db->bb_free_root.rb_node, *node;
4355c894058dSAneesh Kumar K.V 	struct rb_node *parent = NULL, *new_node;
4356c894058dSAneesh Kumar K.V 
43570390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
4358c9de560dSAlex Tomas 	BUG_ON(e4b->bd_bitmap_page == NULL);
4359c9de560dSAlex Tomas 	BUG_ON(e4b->bd_buddy_page == NULL);
4360c9de560dSAlex Tomas 
4361c894058dSAneesh Kumar K.V 	new_node = &new_entry->node;
43627a2fcbf7SAneesh Kumar K.V 	block = new_entry->start_blk;
4363c9de560dSAlex Tomas 
4364c894058dSAneesh Kumar K.V 	if (!*n) {
4365c894058dSAneesh Kumar K.V 		/* first free block exent. We need to
4366c894058dSAneesh Kumar K.V 		   protect buddy cache from being freed,
4367c9de560dSAlex Tomas 		 * otherwise we'll refresh it from
4368c9de560dSAlex Tomas 		 * on-disk bitmap and lose not-yet-available
4369c9de560dSAlex Tomas 		 * blocks */
4370c9de560dSAlex Tomas 		page_cache_get(e4b->bd_buddy_page);
4371c9de560dSAlex Tomas 		page_cache_get(e4b->bd_bitmap_page);
4372c894058dSAneesh Kumar K.V 	}
4373c894058dSAneesh Kumar K.V 	while (*n) {
4374c894058dSAneesh Kumar K.V 		parent = *n;
4375c894058dSAneesh Kumar K.V 		entry = rb_entry(parent, struct ext4_free_data, node);
4376c894058dSAneesh Kumar K.V 		if (block < entry->start_blk)
4377c894058dSAneesh Kumar K.V 			n = &(*n)->rb_left;
4378c894058dSAneesh Kumar K.V 		else if (block >= (entry->start_blk + entry->count))
4379c894058dSAneesh Kumar K.V 			n = &(*n)->rb_right;
4380c894058dSAneesh Kumar K.V 		else {
43815d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group, __func__,
4382fde4d95aSTheodore Ts'o 					"Double free of blocks %d (%d %d)",
4383c894058dSAneesh Kumar K.V 					block, entry->start_blk, entry->count);
4384c894058dSAneesh Kumar K.V 			return 0;
4385c9de560dSAlex Tomas 		}
4386c9de560dSAlex Tomas 	}
4387c9de560dSAlex Tomas 
4388c894058dSAneesh Kumar K.V 	rb_link_node(new_node, parent, n);
4389c894058dSAneesh Kumar K.V 	rb_insert_color(new_node, &db->bb_free_root);
4390c894058dSAneesh Kumar K.V 
4391c894058dSAneesh Kumar K.V 	/* Now try to see the extent can be merged to left and right */
4392c894058dSAneesh Kumar K.V 	node = rb_prev(new_node);
4393c894058dSAneesh Kumar K.V 	if (node) {
4394c894058dSAneesh Kumar K.V 		entry = rb_entry(node, struct ext4_free_data, node);
4395c894058dSAneesh Kumar K.V 		if (can_merge(entry, new_entry)) {
4396c894058dSAneesh Kumar K.V 			new_entry->start_blk = entry->start_blk;
4397c894058dSAneesh Kumar K.V 			new_entry->count += entry->count;
4398c894058dSAneesh Kumar K.V 			rb_erase(node, &(db->bb_free_root));
4399c894058dSAneesh Kumar K.V 			spin_lock(&sbi->s_md_lock);
4400c894058dSAneesh Kumar K.V 			list_del(&entry->list);
4401c894058dSAneesh Kumar K.V 			spin_unlock(&sbi->s_md_lock);
4402c894058dSAneesh Kumar K.V 			kmem_cache_free(ext4_free_ext_cachep, entry);
4403c9de560dSAlex Tomas 		}
4404c9de560dSAlex Tomas 	}
4405c894058dSAneesh Kumar K.V 
4406c894058dSAneesh Kumar K.V 	node = rb_next(new_node);
4407c894058dSAneesh Kumar K.V 	if (node) {
4408c894058dSAneesh Kumar K.V 		entry = rb_entry(node, struct ext4_free_data, node);
4409c894058dSAneesh Kumar K.V 		if (can_merge(new_entry, entry)) {
4410c894058dSAneesh Kumar K.V 			new_entry->count += entry->count;
4411c894058dSAneesh Kumar K.V 			rb_erase(node, &(db->bb_free_root));
4412c894058dSAneesh Kumar K.V 			spin_lock(&sbi->s_md_lock);
4413c894058dSAneesh Kumar K.V 			list_del(&entry->list);
4414c894058dSAneesh Kumar K.V 			spin_unlock(&sbi->s_md_lock);
4415c894058dSAneesh Kumar K.V 			kmem_cache_free(ext4_free_ext_cachep, entry);
4416c894058dSAneesh Kumar K.V 		}
4417c894058dSAneesh Kumar K.V 	}
44183e624fc7STheodore Ts'o 	/* Add the extent to transaction's private list */
4419c894058dSAneesh Kumar K.V 	spin_lock(&sbi->s_md_lock);
44203e624fc7STheodore Ts'o 	list_add(&new_entry->list, &handle->h_transaction->t_private_list);
4421c894058dSAneesh Kumar K.V 	spin_unlock(&sbi->s_md_lock);
4422c9de560dSAlex Tomas 	return 0;
4423c9de560dSAlex Tomas }
4424c9de560dSAlex Tomas 
4425c9de560dSAlex Tomas /*
4426c9de560dSAlex Tomas  * Main entry point into mballoc to free blocks
4427c9de560dSAlex Tomas  */
4428c9de560dSAlex Tomas void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
44290610b6e9STheodore Ts'o 			ext4_fsblk_t block, unsigned long count,
4430c9de560dSAlex Tomas 			int metadata, unsigned long *freed)
4431c9de560dSAlex Tomas {
443226346ff6SAneesh Kumar K.V 	struct buffer_head *bitmap_bh = NULL;
4433c9de560dSAlex Tomas 	struct super_block *sb = inode->i_sb;
4434256bdb49SEric Sandeen 	struct ext4_allocation_context *ac = NULL;
4435c9de560dSAlex Tomas 	struct ext4_group_desc *gdp;
4436c9de560dSAlex Tomas 	struct ext4_super_block *es;
4437498e5f24STheodore Ts'o 	unsigned int overflow;
4438c9de560dSAlex Tomas 	ext4_grpblk_t bit;
4439c9de560dSAlex Tomas 	struct buffer_head *gd_bh;
4440c9de560dSAlex Tomas 	ext4_group_t block_group;
4441c9de560dSAlex Tomas 	struct ext4_sb_info *sbi;
4442c9de560dSAlex Tomas 	struct ext4_buddy e4b;
4443c9de560dSAlex Tomas 	int err = 0;
4444c9de560dSAlex Tomas 	int ret;
4445c9de560dSAlex Tomas 
4446c9de560dSAlex Tomas 	*freed = 0;
4447c9de560dSAlex Tomas 
4448c9de560dSAlex Tomas 	sbi = EXT4_SB(sb);
4449c9de560dSAlex Tomas 	es = EXT4_SB(sb)->s_es;
4450c9de560dSAlex Tomas 	if (block < le32_to_cpu(es->s_first_data_block) ||
4451c9de560dSAlex Tomas 	    block + count < block ||
4452c9de560dSAlex Tomas 	    block + count > ext4_blocks_count(es)) {
445346e665e9SHarvey Harrison 		ext4_error(sb, __func__,
4454c9de560dSAlex Tomas 			    "Freeing blocks not in datazone - "
44550610b6e9STheodore Ts'o 			    "block = %llu, count = %lu", block, count);
4456c9de560dSAlex Tomas 		goto error_return;
4457c9de560dSAlex Tomas 	}
4458c9de560dSAlex Tomas 
44590610b6e9STheodore Ts'o 	ext4_debug("freeing block %llu\n", block);
44609bffad1eSTheodore Ts'o 	trace_ext4_free_blocks(inode, block, count, metadata);
4461c9de560dSAlex Tomas 
4462256bdb49SEric Sandeen 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4463256bdb49SEric Sandeen 	if (ac) {
4464256bdb49SEric Sandeen 		ac->ac_inode = inode;
4465256bdb49SEric Sandeen 		ac->ac_sb = sb;
4466256bdb49SEric Sandeen 	}
4467c9de560dSAlex Tomas 
4468c9de560dSAlex Tomas do_more:
4469c9de560dSAlex Tomas 	overflow = 0;
4470c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4471c9de560dSAlex Tomas 
4472c9de560dSAlex Tomas 	/*
4473c9de560dSAlex Tomas 	 * Check to see if we are freeing blocks across a group
4474c9de560dSAlex Tomas 	 * boundary.
4475c9de560dSAlex Tomas 	 */
4476c9de560dSAlex Tomas 	if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4477c9de560dSAlex Tomas 		overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4478c9de560dSAlex Tomas 		count -= overflow;
4479c9de560dSAlex Tomas 	}
4480574ca174STheodore Ts'o 	bitmap_bh = ext4_read_block_bitmap(sb, block_group);
4481ce89f46cSAneesh Kumar K.V 	if (!bitmap_bh) {
4482ce89f46cSAneesh Kumar K.V 		err = -EIO;
4483c9de560dSAlex Tomas 		goto error_return;
4484ce89f46cSAneesh Kumar K.V 	}
4485c9de560dSAlex Tomas 	gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
4486ce89f46cSAneesh Kumar K.V 	if (!gdp) {
4487ce89f46cSAneesh Kumar K.V 		err = -EIO;
4488c9de560dSAlex Tomas 		goto error_return;
4489ce89f46cSAneesh Kumar K.V 	}
4490c9de560dSAlex Tomas 
4491c9de560dSAlex Tomas 	if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4492c9de560dSAlex Tomas 	    in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4493c9de560dSAlex Tomas 	    in_range(block, ext4_inode_table(sb, gdp),
4494c9de560dSAlex Tomas 		      EXT4_SB(sb)->s_itb_per_group) ||
4495c9de560dSAlex Tomas 	    in_range(block + count - 1, ext4_inode_table(sb, gdp),
4496c9de560dSAlex Tomas 		      EXT4_SB(sb)->s_itb_per_group)) {
4497c9de560dSAlex Tomas 
449846e665e9SHarvey Harrison 		ext4_error(sb, __func__,
4499c9de560dSAlex Tomas 			   "Freeing blocks in system zone - "
45000610b6e9STheodore Ts'o 			   "Block = %llu, count = %lu", block, count);
4501519deca0SAneesh Kumar K.V 		/* err = 0. ext4_std_error should be a no op */
4502519deca0SAneesh Kumar K.V 		goto error_return;
4503c9de560dSAlex Tomas 	}
4504c9de560dSAlex Tomas 
4505c9de560dSAlex Tomas 	BUFFER_TRACE(bitmap_bh, "getting write access");
4506c9de560dSAlex Tomas 	err = ext4_journal_get_write_access(handle, bitmap_bh);
4507c9de560dSAlex Tomas 	if (err)
4508c9de560dSAlex Tomas 		goto error_return;
4509c9de560dSAlex Tomas 
4510c9de560dSAlex Tomas 	/*
4511c9de560dSAlex Tomas 	 * We are about to modify some metadata.  Call the journal APIs
4512c9de560dSAlex Tomas 	 * to unshare ->b_data if a currently-committing transaction is
4513c9de560dSAlex Tomas 	 * using it
4514c9de560dSAlex Tomas 	 */
4515c9de560dSAlex Tomas 	BUFFER_TRACE(gd_bh, "get_write_access");
4516c9de560dSAlex Tomas 	err = ext4_journal_get_write_access(handle, gd_bh);
4517c9de560dSAlex Tomas 	if (err)
4518c9de560dSAlex Tomas 		goto error_return;
4519c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
4520c9de560dSAlex Tomas 	{
4521c9de560dSAlex Tomas 		int i;
4522c9de560dSAlex Tomas 		for (i = 0; i < count; i++)
4523c9de560dSAlex Tomas 			BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4524c9de560dSAlex Tomas 	}
4525c9de560dSAlex Tomas #endif
4526256bdb49SEric Sandeen 	if (ac) {
4527256bdb49SEric Sandeen 		ac->ac_b_ex.fe_group = block_group;
4528256bdb49SEric Sandeen 		ac->ac_b_ex.fe_start = bit;
4529256bdb49SEric Sandeen 		ac->ac_b_ex.fe_len = count;
4530*296c355cSTheodore Ts'o 		trace_ext4_mballoc_free(ac);
4531256bdb49SEric Sandeen 	}
4532c9de560dSAlex Tomas 
4533920313a7SAneesh Kumar K.V 	err = ext4_mb_load_buddy(sb, block_group, &e4b);
4534920313a7SAneesh Kumar K.V 	if (err)
4535920313a7SAneesh Kumar K.V 		goto error_return;
45360390131bSFrank Mayhar 	if (metadata && ext4_handle_valid(handle)) {
45377a2fcbf7SAneesh Kumar K.V 		struct ext4_free_data *new_entry;
45387a2fcbf7SAneesh Kumar K.V 		/*
45397a2fcbf7SAneesh Kumar K.V 		 * blocks being freed are metadata. these blocks shouldn't
45407a2fcbf7SAneesh Kumar K.V 		 * be used until this transaction is committed
45417a2fcbf7SAneesh Kumar K.V 		 */
45427a2fcbf7SAneesh Kumar K.V 		new_entry  = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
45437a2fcbf7SAneesh Kumar K.V 		new_entry->start_blk = bit;
45447a2fcbf7SAneesh Kumar K.V 		new_entry->group  = block_group;
45457a2fcbf7SAneesh Kumar K.V 		new_entry->count = count;
45467a2fcbf7SAneesh Kumar K.V 		new_entry->t_tid = handle->h_transaction->t_tid;
4547955ce5f5SAneesh Kumar K.V 
45487a2fcbf7SAneesh Kumar K.V 		ext4_lock_group(sb, block_group);
4549955ce5f5SAneesh Kumar K.V 		mb_clear_bits(bitmap_bh->b_data, bit, count);
45507a2fcbf7SAneesh Kumar K.V 		ext4_mb_free_metadata(handle, &e4b, new_entry);
4551c9de560dSAlex Tomas 	} else {
45527a2fcbf7SAneesh Kumar K.V 		/* need to update group_info->bb_free and bitmap
45537a2fcbf7SAneesh Kumar K.V 		 * with group lock held. generate_buddy look at
45547a2fcbf7SAneesh Kumar K.V 		 * them with group lock_held
45557a2fcbf7SAneesh Kumar K.V 		 */
4556955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, block_group);
4557955ce5f5SAneesh Kumar K.V 		mb_clear_bits(bitmap_bh->b_data, bit, count);
45587e5a8cddSShen Feng 		mb_free_blocks(inode, &e4b, bit, count);
4559c9de560dSAlex Tomas 		ext4_mb_return_to_preallocation(inode, &e4b, block, count);
4560c9de560dSAlex Tomas 	}
4561c9de560dSAlex Tomas 
4562560671a0SAneesh Kumar K.V 	ret = ext4_free_blks_count(sb, gdp) + count;
4563560671a0SAneesh Kumar K.V 	ext4_free_blks_set(sb, gdp, ret);
4564c9de560dSAlex Tomas 	gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
4565955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
4566c9de560dSAlex Tomas 	percpu_counter_add(&sbi->s_freeblocks_counter, count);
4567c9de560dSAlex Tomas 
4568772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
4569772cb7c8SJose R. Santos 		ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
45709f24e420STheodore Ts'o 		atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
4571772cb7c8SJose R. Santos 	}
4572772cb7c8SJose R. Santos 
4573c9de560dSAlex Tomas 	ext4_mb_release_desc(&e4b);
4574c9de560dSAlex Tomas 
4575c9de560dSAlex Tomas 	*freed += count;
4576c9de560dSAlex Tomas 
45777a2fcbf7SAneesh Kumar K.V 	/* We dirtied the bitmap block */
45787a2fcbf7SAneesh Kumar K.V 	BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
45797a2fcbf7SAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
45807a2fcbf7SAneesh Kumar K.V 
4581c9de560dSAlex Tomas 	/* And the group descriptor block */
4582c9de560dSAlex Tomas 	BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
45830390131bSFrank Mayhar 	ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4584c9de560dSAlex Tomas 	if (!err)
4585c9de560dSAlex Tomas 		err = ret;
4586c9de560dSAlex Tomas 
4587c9de560dSAlex Tomas 	if (overflow && !err) {
4588c9de560dSAlex Tomas 		block += count;
4589c9de560dSAlex Tomas 		count = overflow;
4590c9de560dSAlex Tomas 		put_bh(bitmap_bh);
4591c9de560dSAlex Tomas 		goto do_more;
4592c9de560dSAlex Tomas 	}
4593c9de560dSAlex Tomas 	sb->s_dirt = 1;
4594c9de560dSAlex Tomas error_return:
4595c9de560dSAlex Tomas 	brelse(bitmap_bh);
4596c9de560dSAlex Tomas 	ext4_std_error(sb, err);
4597256bdb49SEric Sandeen 	if (ac)
4598256bdb49SEric Sandeen 		kmem_cache_free(ext4_ac_cachep, ac);
4599c9de560dSAlex Tomas 	return;
4600c9de560dSAlex Tomas }
4601