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