blob: b726e10d2c1c18bafa355663d822f8b9ef441d67 [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"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_alloc_btree.h"
32#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_dir2_sf.h"
34#include "xfs_attr_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_btree.h"
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110038#include "xfs_btree_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_ialloc.h"
40#include "xfs_alloc.h"
41#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Christoph Hellwig561f7d12008-10-30 16:53:59 +110045STATIC struct xfs_btree_cur *
46xfs_allocbt_dup_cursor(
47 struct xfs_btree_cur *cur)
48{
49 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
50 cur->bc_private.a.agbp, cur->bc_private.a.agno,
51 cur->bc_btnum);
52}
53
Christoph Hellwig344207c2008-10-30 16:57:16 +110054STATIC void
55xfs_allocbt_set_root(
56 struct xfs_btree_cur *cur,
57 union xfs_btree_ptr *ptr,
58 int inc)
59{
60 struct xfs_buf *agbp = cur->bc_private.a.agbp;
61 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
62 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
63 int btnum = cur->bc_btnum;
Dave Chinnera862e0f2010-01-11 11:47:41 +000064 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
Christoph Hellwig344207c2008-10-30 16:57:16 +110065
66 ASSERT(ptr->s != 0);
67
68 agf->agf_roots[btnum] = ptr->s;
69 be32_add_cpu(&agf->agf_levels[btnum], inc);
Dave Chinnera862e0f2010-01-11 11:47:41 +000070 pag->pagf_levels[btnum] += inc;
71 xfs_perag_put(pag);
Christoph Hellwig344207c2008-10-30 16:57:16 +110072
73 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
74}
75
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110076STATIC int
77xfs_allocbt_alloc_block(
78 struct xfs_btree_cur *cur,
79 union xfs_btree_ptr *start,
80 union xfs_btree_ptr *new,
81 int length,
82 int *stat)
83{
84 int error;
85 xfs_agblock_t bno;
86
87 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
88
89 /* Allocate the new block from the freelist. If we can't, give up. */
90 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
91 &bno, 1);
92 if (error) {
93 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
94 return error;
95 }
96
97 if (bno == NULLAGBLOCK) {
98 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
99 *stat = 0;
100 return 0;
101 }
102
103 xfs_trans_agbtree_delta(cur->bc_tp, 1);
104 new->s = cpu_to_be32(bno);
105
106 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
107 *stat = 1;
108 return 0;
109}
110
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100111STATIC int
112xfs_allocbt_free_block(
113 struct xfs_btree_cur *cur,
114 struct xfs_buf *bp)
115{
116 struct xfs_buf *agbp = cur->bc_private.a.agbp;
117 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
118 xfs_agblock_t bno;
119 int error;
120
Eric Sandeenb6e32222009-01-14 23:22:07 -0600121 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100122 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
123 if (error)
124 return error;
125
126 /*
127 * Since blocks move to the free list without the coordination used in
128 * xfs_bmap_finish, we can't allow block to be available for
129 * reallocation and non-transaction writing (user data) until we know
130 * that the transaction that moved it to the free list is permanently
131 * on disk. We track the blocks by declaring these blocks as "busy";
132 * the busy list is maintained on a per-ag basis and each transaction
133 * records which entries should be removed when the iclog commits to
134 * disk. If a busy block is allocated, the iclog is pushed up to the
135 * LSN that freed the block.
136 */
137 xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
138 xfs_trans_agbtree_delta(cur->bc_tp, -1);
139 return 0;
140}
141
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100142/*
143 * Update the longest extent in the AGF
144 */
145STATIC void
146xfs_allocbt_update_lastrec(
147 struct xfs_btree_cur *cur,
148 struct xfs_btree_block *block,
149 union xfs_btree_rec *rec,
150 int ptr,
151 int reason)
152{
153 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
154 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
Dave Chinnera862e0f2010-01-11 11:47:41 +0000155 struct xfs_perag *pag;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100156 __be32 len;
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100157 int numrecs;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100158
159 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
160
161 switch (reason) {
162 case LASTREC_UPDATE:
163 /*
164 * If this is the last leaf block and it's the last record,
165 * then update the size of the longest extent in the AG.
166 */
167 if (ptr != xfs_btree_get_numrecs(block))
168 return;
169 len = rec->alloc.ar_blockcount;
170 break;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100171 case LASTREC_INSREC:
172 if (be32_to_cpu(rec->alloc.ar_blockcount) <=
173 be32_to_cpu(agf->agf_longest))
174 return;
175 len = rec->alloc.ar_blockcount;
176 break;
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100177 case LASTREC_DELREC:
178 numrecs = xfs_btree_get_numrecs(block);
179 if (ptr <= numrecs)
180 return;
181 ASSERT(ptr == numrecs + 1);
182
183 if (numrecs) {
184 xfs_alloc_rec_t *rrp;
185
Christoph Hellwig136341b2008-10-30 17:11:40 +1100186 rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100187 len = rrp->ar_blockcount;
188 } else {
189 len = 0;
190 }
191
192 break;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100193 default:
194 ASSERT(0);
195 return;
196 }
197
198 agf->agf_longest = len;
Dave Chinnera862e0f2010-01-11 11:47:41 +0000199 pag = xfs_perag_get(cur->bc_mp, seqno);
200 pag->pagf_longest = be32_to_cpu(len);
201 xfs_perag_put(pag);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100202 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
203}
204
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100205STATIC int
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100206xfs_allocbt_get_minrecs(
207 struct xfs_btree_cur *cur,
208 int level)
209{
210 return cur->bc_mp->m_alloc_mnr[level != 0];
211}
212
213STATIC int
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100214xfs_allocbt_get_maxrecs(
215 struct xfs_btree_cur *cur,
216 int level)
217{
218 return cur->bc_mp->m_alloc_mxr[level != 0];
219}
220
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100221STATIC void
222xfs_allocbt_init_key_from_rec(
223 union xfs_btree_key *key,
224 union xfs_btree_rec *rec)
225{
226 ASSERT(rec->alloc.ar_startblock != 0);
227
228 key->alloc.ar_startblock = rec->alloc.ar_startblock;
229 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
230}
231
232STATIC void
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100233xfs_allocbt_init_rec_from_key(
234 union xfs_btree_key *key,
235 union xfs_btree_rec *rec)
236{
237 ASSERT(key->alloc.ar_startblock != 0);
238
239 rec->alloc.ar_startblock = key->alloc.ar_startblock;
240 rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
241}
242
243STATIC void
244xfs_allocbt_init_rec_from_cur(
245 struct xfs_btree_cur *cur,
246 union xfs_btree_rec *rec)
247{
248 ASSERT(cur->bc_rec.a.ar_startblock != 0);
249
250 rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
251 rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
252}
253
254STATIC void
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100255xfs_allocbt_init_ptr_from_cur(
256 struct xfs_btree_cur *cur,
257 union xfs_btree_ptr *ptr)
258{
259 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
260
261 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
262 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
263
264 ptr->s = agf->agf_roots[cur->bc_btnum];
265}
266
267STATIC __int64_t
268xfs_allocbt_key_diff(
269 struct xfs_btree_cur *cur,
270 union xfs_btree_key *key)
271{
272 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
273 xfs_alloc_key_t *kp = &key->alloc;
274 __int64_t diff;
275
276 if (cur->bc_btnum == XFS_BTNUM_BNO) {
277 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
278 rec->ar_startblock;
279 }
280
281 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
282 if (diff)
283 return diff;
284
285 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
286}
287
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100288STATIC int
289xfs_allocbt_kill_root(
290 struct xfs_btree_cur *cur,
291 struct xfs_buf *bp,
292 int level,
293 union xfs_btree_ptr *newroot)
294{
295 int error;
296
297 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
298 XFS_BTREE_STATS_INC(cur, killroot);
299
300 /*
301 * Update the root pointer, decreasing the level by 1 and then
302 * free the old root.
303 */
304 xfs_allocbt_set_root(cur, newroot, -1);
305 error = xfs_allocbt_free_block(cur, bp);
306 if (error) {
307 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
308 return error;
309 }
310
311 XFS_BTREE_STATS_INC(cur, free);
312
313 xfs_btree_setbuf(cur, level, NULL);
314 cur->bc_nlevels--;
315
316 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
317 return 0;
318}
319
Christoph Hellwig4a26e662008-10-30 16:58:32 +1100320#ifdef DEBUG
321STATIC int
322xfs_allocbt_keys_inorder(
323 struct xfs_btree_cur *cur,
324 union xfs_btree_key *k1,
325 union xfs_btree_key *k2)
326{
327 if (cur->bc_btnum == XFS_BTNUM_BNO) {
328 return be32_to_cpu(k1->alloc.ar_startblock) <
329 be32_to_cpu(k2->alloc.ar_startblock);
330 } else {
331 return be32_to_cpu(k1->alloc.ar_blockcount) <
332 be32_to_cpu(k2->alloc.ar_blockcount) ||
333 (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
334 be32_to_cpu(k1->alloc.ar_startblock) <
335 be32_to_cpu(k2->alloc.ar_startblock));
336 }
337}
338
339STATIC int
340xfs_allocbt_recs_inorder(
341 struct xfs_btree_cur *cur,
342 union xfs_btree_rec *r1,
343 union xfs_btree_rec *r2)
344{
345 if (cur->bc_btnum == XFS_BTNUM_BNO) {
346 return be32_to_cpu(r1->alloc.ar_startblock) +
347 be32_to_cpu(r1->alloc.ar_blockcount) <=
348 be32_to_cpu(r2->alloc.ar_startblock);
349 } else {
350 return be32_to_cpu(r1->alloc.ar_blockcount) <
351 be32_to_cpu(r2->alloc.ar_blockcount) ||
352 (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
353 be32_to_cpu(r1->alloc.ar_startblock) <
354 be32_to_cpu(r2->alloc.ar_startblock));
355 }
356}
357#endif /* DEBUG */
358
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100359#ifdef XFS_BTREE_TRACE
360ktrace_t *xfs_allocbt_trace_buf;
361
362STATIC void
363xfs_allocbt_trace_enter(
364 struct xfs_btree_cur *cur,
365 const char *func,
366 char *s,
367 int type,
368 int line,
369 __psunsigned_t a0,
370 __psunsigned_t a1,
371 __psunsigned_t a2,
372 __psunsigned_t a3,
373 __psunsigned_t a4,
374 __psunsigned_t a5,
375 __psunsigned_t a6,
376 __psunsigned_t a7,
377 __psunsigned_t a8,
378 __psunsigned_t a9,
379 __psunsigned_t a10)
380{
381 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
382 (void *)func, (void *)s, NULL, (void *)cur,
383 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
384 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
385 (void *)a8, (void *)a9, (void *)a10);
386}
387
388STATIC void
389xfs_allocbt_trace_cursor(
390 struct xfs_btree_cur *cur,
391 __uint32_t *s0,
392 __uint64_t *l0,
393 __uint64_t *l1)
394{
395 *s0 = cur->bc_private.a.agno;
396 *l0 = cur->bc_rec.a.ar_startblock;
397 *l1 = cur->bc_rec.a.ar_blockcount;
398}
399
400STATIC void
401xfs_allocbt_trace_key(
402 struct xfs_btree_cur *cur,
403 union xfs_btree_key *key,
404 __uint64_t *l0,
405 __uint64_t *l1)
406{
407 *l0 = be32_to_cpu(key->alloc.ar_startblock);
408 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
409}
410
411STATIC void
412xfs_allocbt_trace_record(
413 struct xfs_btree_cur *cur,
414 union xfs_btree_rec *rec,
415 __uint64_t *l0,
416 __uint64_t *l1,
417 __uint64_t *l2)
418{
419 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
420 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
421 *l2 = 0;
422}
423#endif /* XFS_BTREE_TRACE */
424
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100425static const struct xfs_btree_ops xfs_allocbt_ops = {
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100426 .rec_len = sizeof(xfs_alloc_rec_t),
427 .key_len = sizeof(xfs_alloc_key_t),
428
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100429 .dup_cursor = xfs_allocbt_dup_cursor,
Christoph Hellwig344207c2008-10-30 16:57:16 +1100430 .set_root = xfs_allocbt_set_root,
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100431 .kill_root = xfs_allocbt_kill_root,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100432 .alloc_block = xfs_allocbt_alloc_block,
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100433 .free_block = xfs_allocbt_free_block,
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100434 .update_lastrec = xfs_allocbt_update_lastrec,
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100435 .get_minrecs = xfs_allocbt_get_minrecs,
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100436 .get_maxrecs = xfs_allocbt_get_maxrecs,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100437 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100438 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
439 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100440 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
441 .key_diff = xfs_allocbt_key_diff,
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100442
Christoph Hellwig4a26e662008-10-30 16:58:32 +1100443#ifdef DEBUG
444 .keys_inorder = xfs_allocbt_keys_inorder,
445 .recs_inorder = xfs_allocbt_recs_inorder,
446#endif
447
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100448#ifdef XFS_BTREE_TRACE
449 .trace_enter = xfs_allocbt_trace_enter,
450 .trace_cursor = xfs_allocbt_trace_cursor,
451 .trace_key = xfs_allocbt_trace_key,
452 .trace_record = xfs_allocbt_trace_record,
453#endif
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100454};
455
456/*
457 * Allocate a new allocation btree cursor.
458 */
459struct xfs_btree_cur * /* new alloc btree cursor */
460xfs_allocbt_init_cursor(
461 struct xfs_mount *mp, /* file system mount point */
462 struct xfs_trans *tp, /* transaction pointer */
463 struct xfs_buf *agbp, /* buffer for agf structure */
464 xfs_agnumber_t agno, /* allocation group number */
465 xfs_btnum_t btnum) /* btree identifier */
466{
467 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
468 struct xfs_btree_cur *cur;
469
470 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
471
472 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
473
474 cur->bc_tp = tp;
475 cur->bc_mp = mp;
476 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
477 cur->bc_btnum = btnum;
478 cur->bc_blocklog = mp->m_sb.sb_blocklog;
479
480 cur->bc_ops = &xfs_allocbt_ops;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100481 if (btnum == XFS_BTNUM_CNT)
482 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100483
484 cur->bc_private.a.agbp = agbp;
485 cur->bc_private.a.agno = agno;
486
487 return cur;
488}
Christoph Hellwig60197e82008-10-30 17:11:19 +1100489
490/*
491 * Calculate number of records in an alloc btree block.
492 */
493int
494xfs_allocbt_maxrecs(
495 struct xfs_mount *mp,
496 int blocklen,
497 int leaf)
498{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100499 blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100500
501 if (leaf)
502 return blocklen / sizeof(xfs_alloc_rec_t);
503 return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
504}