]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/xfs_alloc_btree.c
xfs: do not immediately reuse busy extent ranges
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / xfs_alloc_btree.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4 27#include "xfs_mount.h"
a844f451 28#include "xfs_bmap_btree.h"
1da177e4
LT
29#include "xfs_alloc_btree.h"
30#include "xfs_ialloc_btree.h"
a844f451
NS
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
1da177e4 33#include "xfs_btree.h"
f5eb8e7c 34#include "xfs_btree_trace.h"
1da177e4
LT
35#include "xfs_alloc.h"
36#include "xfs_error.h"
0b1b213f 37#include "xfs_trace.h"
1da177e4 38
1da177e4 39
278d0ca1
CH
40STATIC struct xfs_btree_cur *
41xfs_allocbt_dup_cursor(
42 struct xfs_btree_cur *cur)
43{
44 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
45 cur->bc_private.a.agbp, cur->bc_private.a.agno,
46 cur->bc_btnum);
47}
48
344207ce
CH
49STATIC void
50xfs_allocbt_set_root(
51 struct xfs_btree_cur *cur,
52 union xfs_btree_ptr *ptr,
53 int inc)
54{
55 struct xfs_buf *agbp = cur->bc_private.a.agbp;
56 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
57 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
58 int btnum = cur->bc_btnum;
a862e0fd 59 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
344207ce
CH
60
61 ASSERT(ptr->s != 0);
62
63 agf->agf_roots[btnum] = ptr->s;
64 be32_add_cpu(&agf->agf_levels[btnum], inc);
a862e0fd
DC
65 pag->pagf_levels[btnum] += inc;
66 xfs_perag_put(pag);
344207ce
CH
67
68 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
69}
70
f5eb8e7c
CH
71STATIC int
72xfs_allocbt_alloc_block(
73 struct xfs_btree_cur *cur,
74 union xfs_btree_ptr *start,
75 union xfs_btree_ptr *new,
76 int length,
77 int *stat)
78{
79 int error;
80 xfs_agblock_t bno;
81
82 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
83
84 /* Allocate the new block from the freelist. If we can't, give up. */
85 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
86 &bno, 1);
87 if (error) {
88 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
89 return error;
90 }
91
92 if (bno == NULLAGBLOCK) {
93 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
94 *stat = 0;
95 return 0;
96 }
a870acd9
CH
97 if (xfs_alloc_busy_search(cur->bc_mp, cur->bc_private.a.agno, bno, 1))
98 xfs_trans_set_sync(cur->bc_tp);
f5eb8e7c
CH
99
100 xfs_trans_agbtree_delta(cur->bc_tp, 1);
101 new->s = cpu_to_be32(bno);
102
103 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
104 *stat = 1;
105 return 0;
106}
107
d4b3a4b7
CH
108STATIC int
109xfs_allocbt_free_block(
110 struct xfs_btree_cur *cur,
111 struct xfs_buf *bp)
112{
113 struct xfs_buf *agbp = cur->bc_private.a.agbp;
114 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
115 xfs_agblock_t bno;
116 int error;
117
b6e32227 118 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
d4b3a4b7
CH
119 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
120 if (error)
121 return error;
122
123 /*
124 * Since blocks move to the free list without the coordination used in
125 * xfs_bmap_finish, we can't allow block to be available for
126 * reallocation and non-transaction writing (user data) until we know
127 * that the transaction that moved it to the free list is permanently
128 * on disk. We track the blocks by declaring these blocks as "busy";
129 * the busy list is maintained on a per-ag basis and each transaction
130 * records which entries should be removed when the iclog commits to
131 * disk. If a busy block is allocated, the iclog is pushed up to the
132 * LSN that freed the block.
133 */
ed3b4d6c 134 xfs_alloc_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
d4b3a4b7
CH
135 xfs_trans_agbtree_delta(cur->bc_tp, -1);
136 return 0;
137}
138
1da177e4 139/*
278d0ca1 140 * Update the longest extent in the AGF
1da177e4 141 */
278d0ca1
CH
142STATIC void
143xfs_allocbt_update_lastrec(
144 struct xfs_btree_cur *cur,
145 struct xfs_btree_block *block,
146 union xfs_btree_rec *rec,
147 int ptr,
148 int reason)
1da177e4 149{
278d0ca1
CH
150 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
151 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
a862e0fd 152 struct xfs_perag *pag;
278d0ca1 153 __be32 len;
91cca5df 154 int numrecs;
1da177e4 155
278d0ca1 156 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
1da177e4 157
278d0ca1
CH
158 switch (reason) {
159 case LASTREC_UPDATE:
1da177e4 160 /*
278d0ca1
CH
161 * If this is the last leaf block and it's the last record,
162 * then update the size of the longest extent in the AG.
1da177e4 163 */
278d0ca1
CH
164 if (ptr != xfs_btree_get_numrecs(block))
165 return;
166 len = rec->alloc.ar_blockcount;
167 break;
4b22a571
CH
168 case LASTREC_INSREC:
169 if (be32_to_cpu(rec->alloc.ar_blockcount) <=
170 be32_to_cpu(agf->agf_longest))
171 return;
172 len = rec->alloc.ar_blockcount;
91cca5df
CH
173 break;
174 case LASTREC_DELREC:
175 numrecs = xfs_btree_get_numrecs(block);
176 if (ptr <= numrecs)
177 return;
178 ASSERT(ptr == numrecs + 1);
179
180 if (numrecs) {
181 xfs_alloc_rec_t *rrp;
182
136341b4 183 rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
91cca5df
CH
184 len = rrp->ar_blockcount;
185 } else {
186 len = 0;
187 }
188
4b22a571 189 break;
278d0ca1
CH
190 default:
191 ASSERT(0);
192 return;
1da177e4 193 }
561f7d17 194
278d0ca1 195 agf->agf_longest = len;
a862e0fd
DC
196 pag = xfs_perag_get(cur->bc_mp, seqno);
197 pag->pagf_longest = be32_to_cpu(len);
198 xfs_perag_put(pag);
278d0ca1 199 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
561f7d17
CH
200}
201
91cca5df
CH
202STATIC int
203xfs_allocbt_get_minrecs(
204 struct xfs_btree_cur *cur,
205 int level)
206{
207 return cur->bc_mp->m_alloc_mnr[level != 0];
208}
209
ce5e42db
CH
210STATIC int
211xfs_allocbt_get_maxrecs(
212 struct xfs_btree_cur *cur,
213 int level)
214{
215 return cur->bc_mp->m_alloc_mxr[level != 0];
216}
217
fe033cc8
CH
218STATIC void
219xfs_allocbt_init_key_from_rec(
220 union xfs_btree_key *key,
221 union xfs_btree_rec *rec)
222{
223 ASSERT(rec->alloc.ar_startblock != 0);
224
225 key->alloc.ar_startblock = rec->alloc.ar_startblock;
226 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
227}
228
4b22a571
CH
229STATIC void
230xfs_allocbt_init_rec_from_key(
231 union xfs_btree_key *key,
232 union xfs_btree_rec *rec)
233{
234 ASSERT(key->alloc.ar_startblock != 0);
235
236 rec->alloc.ar_startblock = key->alloc.ar_startblock;
237 rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
238}
239
240STATIC void
241xfs_allocbt_init_rec_from_cur(
242 struct xfs_btree_cur *cur,
243 union xfs_btree_rec *rec)
244{
245 ASSERT(cur->bc_rec.a.ar_startblock != 0);
246
247 rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
248 rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
249}
250
fe033cc8
CH
251STATIC void
252xfs_allocbt_init_ptr_from_cur(
253 struct xfs_btree_cur *cur,
254 union xfs_btree_ptr *ptr)
255{
256 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
257
258 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
259 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
260
261 ptr->s = agf->agf_roots[cur->bc_btnum];
262}
263
264STATIC __int64_t
265xfs_allocbt_key_diff(
266 struct xfs_btree_cur *cur,
267 union xfs_btree_key *key)
268{
269 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
270 xfs_alloc_key_t *kp = &key->alloc;
271 __int64_t diff;
272
273 if (cur->bc_btnum == XFS_BTNUM_BNO) {
274 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
275 rec->ar_startblock;
276 }
277
278 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
279 if (diff)
280 return diff;
281
282 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
283}
284
4a26e66e
CH
285#ifdef DEBUG
286STATIC int
287xfs_allocbt_keys_inorder(
288 struct xfs_btree_cur *cur,
289 union xfs_btree_key *k1,
290 union xfs_btree_key *k2)
291{
292 if (cur->bc_btnum == XFS_BTNUM_BNO) {
293 return be32_to_cpu(k1->alloc.ar_startblock) <
294 be32_to_cpu(k2->alloc.ar_startblock);
295 } else {
296 return be32_to_cpu(k1->alloc.ar_blockcount) <
297 be32_to_cpu(k2->alloc.ar_blockcount) ||
298 (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
299 be32_to_cpu(k1->alloc.ar_startblock) <
300 be32_to_cpu(k2->alloc.ar_startblock));
301 }
302}
303
304STATIC int
305xfs_allocbt_recs_inorder(
306 struct xfs_btree_cur *cur,
307 union xfs_btree_rec *r1,
308 union xfs_btree_rec *r2)
309{
310 if (cur->bc_btnum == XFS_BTNUM_BNO) {
311 return be32_to_cpu(r1->alloc.ar_startblock) +
312 be32_to_cpu(r1->alloc.ar_blockcount) <=
313 be32_to_cpu(r2->alloc.ar_startblock);
314 } else {
315 return be32_to_cpu(r1->alloc.ar_blockcount) <
316 be32_to_cpu(r2->alloc.ar_blockcount) ||
317 (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
318 be32_to_cpu(r1->alloc.ar_startblock) <
319 be32_to_cpu(r2->alloc.ar_startblock));
320 }
321}
322#endif /* DEBUG */
323
8c4ed633
CH
324#ifdef XFS_BTREE_TRACE
325ktrace_t *xfs_allocbt_trace_buf;
326
327STATIC void
328xfs_allocbt_trace_enter(
329 struct xfs_btree_cur *cur,
330 const char *func,
331 char *s,
332 int type,
333 int line,
334 __psunsigned_t a0,
335 __psunsigned_t a1,
336 __psunsigned_t a2,
337 __psunsigned_t a3,
338 __psunsigned_t a4,
339 __psunsigned_t a5,
340 __psunsigned_t a6,
341 __psunsigned_t a7,
342 __psunsigned_t a8,
343 __psunsigned_t a9,
344 __psunsigned_t a10)
345{
346 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
347 (void *)func, (void *)s, NULL, (void *)cur,
348 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
349 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
350 (void *)a8, (void *)a9, (void *)a10);
351}
352
353STATIC void
354xfs_allocbt_trace_cursor(
355 struct xfs_btree_cur *cur,
356 __uint32_t *s0,
357 __uint64_t *l0,
358 __uint64_t *l1)
359{
360 *s0 = cur->bc_private.a.agno;
361 *l0 = cur->bc_rec.a.ar_startblock;
362 *l1 = cur->bc_rec.a.ar_blockcount;
363}
364
365STATIC void
366xfs_allocbt_trace_key(
367 struct xfs_btree_cur *cur,
368 union xfs_btree_key *key,
369 __uint64_t *l0,
370 __uint64_t *l1)
371{
372 *l0 = be32_to_cpu(key->alloc.ar_startblock);
373 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
374}
375
376STATIC void
377xfs_allocbt_trace_record(
378 struct xfs_btree_cur *cur,
379 union xfs_btree_rec *rec,
380 __uint64_t *l0,
381 __uint64_t *l1,
382 __uint64_t *l2)
383{
384 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
385 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
386 *l2 = 0;
387}
388#endif /* XFS_BTREE_TRACE */
389
561f7d17 390static const struct xfs_btree_ops xfs_allocbt_ops = {
65f1eaea
CH
391 .rec_len = sizeof(xfs_alloc_rec_t),
392 .key_len = sizeof(xfs_alloc_key_t),
393
561f7d17 394 .dup_cursor = xfs_allocbt_dup_cursor,
344207ce 395 .set_root = xfs_allocbt_set_root,
f5eb8e7c 396 .alloc_block = xfs_allocbt_alloc_block,
d4b3a4b7 397 .free_block = xfs_allocbt_free_block,
278d0ca1 398 .update_lastrec = xfs_allocbt_update_lastrec,
91cca5df 399 .get_minrecs = xfs_allocbt_get_minrecs,
ce5e42db 400 .get_maxrecs = xfs_allocbt_get_maxrecs,
fe033cc8 401 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
4b22a571
CH
402 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
403 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
fe033cc8
CH
404 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
405 .key_diff = xfs_allocbt_key_diff,
8c4ed633 406
4a26e66e
CH
407#ifdef DEBUG
408 .keys_inorder = xfs_allocbt_keys_inorder,
409 .recs_inorder = xfs_allocbt_recs_inorder,
410#endif
411
8c4ed633
CH
412#ifdef XFS_BTREE_TRACE
413 .trace_enter = xfs_allocbt_trace_enter,
414 .trace_cursor = xfs_allocbt_trace_cursor,
415 .trace_key = xfs_allocbt_trace_key,
416 .trace_record = xfs_allocbt_trace_record,
417#endif
561f7d17
CH
418};
419
420/*
421 * Allocate a new allocation btree cursor.
422 */
423struct xfs_btree_cur * /* new alloc btree cursor */
424xfs_allocbt_init_cursor(
425 struct xfs_mount *mp, /* file system mount point */
426 struct xfs_trans *tp, /* transaction pointer */
427 struct xfs_buf *agbp, /* buffer for agf structure */
428 xfs_agnumber_t agno, /* allocation group number */
429 xfs_btnum_t btnum) /* btree identifier */
430{
431 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
432 struct xfs_btree_cur *cur;
433
434 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
435
436 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
437
438 cur->bc_tp = tp;
439 cur->bc_mp = mp;
440 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
441 cur->bc_btnum = btnum;
442 cur->bc_blocklog = mp->m_sb.sb_blocklog;
443
444 cur->bc_ops = &xfs_allocbt_ops;
278d0ca1
CH
445 if (btnum == XFS_BTNUM_CNT)
446 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
561f7d17
CH
447
448 cur->bc_private.a.agbp = agbp;
449 cur->bc_private.a.agno = agno;
450
451 return cur;
452}
60197e8d
CH
453
454/*
455 * Calculate number of records in an alloc btree block.
456 */
457int
458xfs_allocbt_maxrecs(
459 struct xfs_mount *mp,
460 int blocklen,
461 int leaf)
462{
7cc95a82 463 blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
60197e8d
CH
464
465 if (leaf)
466 return blocklen / sizeof(xfs_alloc_rec_t);
467 return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
468}