blob: bcfe92f47edd840304f9e97c1971e5ac1cbdaea6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_alloc_btree.h"
30#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_dinode.h"
32#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_btree.h"
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110034#include "xfs_btree_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_alloc.h"
36#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000037#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Christoph Hellwig561f7d12008-10-30 16:53:59 +110040STATIC 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
Christoph Hellwig344207c2008-10-30 16:57:16 +110049STATIC 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;
Dave Chinnera862e0f2010-01-11 11:47:41 +000059 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
Christoph Hellwig344207c2008-10-30 16:57:16 +110060
61 ASSERT(ptr->s != 0);
62
63 agf->agf_roots[btnum] = ptr->s;
64 be32_add_cpu(&agf->agf_levels[btnum], inc);
Dave Chinnera862e0f2010-01-11 11:47:41 +000065 pag->pagf_levels[btnum] += inc;
66 xfs_perag_put(pag);
Christoph Hellwig344207c2008-10-30 16:57:16 +110067
68 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
69}
70
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110071STATIC 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 }
Christoph Hellwiga870acd2011-04-24 19:06:14 +000097 if (xfs_alloc_busy_search(cur->bc_mp, cur->bc_private.a.agno, bno, 1))
98 xfs_trans_set_sync(cur->bc_tp);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110099
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
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100108STATIC 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
Eric Sandeenb6e32222009-01-14 23:22:07 -0600118 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100119 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 */
Dave Chinnered3b4d62010-05-21 12:07:08 +1000134 xfs_alloc_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100135 xfs_trans_agbtree_delta(cur->bc_tp, -1);
136 return 0;
137}
138
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100139/*
140 * Update the longest extent in the AGF
141 */
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)
149{
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);
Dave Chinnera862e0f2010-01-11 11:47:41 +0000152 struct xfs_perag *pag;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100153 __be32 len;
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100154 int numrecs;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100155
156 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
157
158 switch (reason) {
159 case LASTREC_UPDATE:
160 /*
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.
163 */
164 if (ptr != xfs_btree_get_numrecs(block))
165 return;
166 len = rec->alloc.ar_blockcount;
167 break;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100168 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;
173 break;
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100174 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
Christoph Hellwig136341b2008-10-30 17:11:40 +1100183 rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100184 len = rrp->ar_blockcount;
185 } else {
186 len = 0;
187 }
188
189 break;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100190 default:
191 ASSERT(0);
192 return;
193 }
194
195 agf->agf_longest = len;
Dave Chinnera862e0f2010-01-11 11:47:41 +0000196 pag = xfs_perag_get(cur->bc_mp, seqno);
197 pag->pagf_longest = be32_to_cpu(len);
198 xfs_perag_put(pag);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100199 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
200}
201
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100202STATIC int
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100203xfs_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
210STATIC int
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100211xfs_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
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100218STATIC 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
229STATIC void
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100230xfs_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
251STATIC void
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100252xfs_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
Christoph Hellwig4a26e662008-10-30 16:58:32 +1100285#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
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100324#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
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100390static const struct xfs_btree_ops xfs_allocbt_ops = {
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100391 .rec_len = sizeof(xfs_alloc_rec_t),
392 .key_len = sizeof(xfs_alloc_key_t),
393
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100394 .dup_cursor = xfs_allocbt_dup_cursor,
Christoph Hellwig344207c2008-10-30 16:57:16 +1100395 .set_root = xfs_allocbt_set_root,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100396 .alloc_block = xfs_allocbt_alloc_block,
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100397 .free_block = xfs_allocbt_free_block,
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100398 .update_lastrec = xfs_allocbt_update_lastrec,
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100399 .get_minrecs = xfs_allocbt_get_minrecs,
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100400 .get_maxrecs = xfs_allocbt_get_maxrecs,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100401 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100402 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
403 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100404 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
405 .key_diff = xfs_allocbt_key_diff,
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100406
Christoph Hellwig4a26e662008-10-30 16:58:32 +1100407#ifdef DEBUG
408 .keys_inorder = xfs_allocbt_keys_inorder,
409 .recs_inorder = xfs_allocbt_recs_inorder,
410#endif
411
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100412#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
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100418};
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;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100445 if (btnum == XFS_BTNUM_CNT)
446 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100447
448 cur->bc_private.a.agbp = agbp;
449 cur->bc_private.a.agno = agno;
450
451 return cur;
452}
Christoph Hellwig60197e82008-10-30 17:11:19 +1100453
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{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100463 blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100464
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}