blob: 4d44f03858b05c36548099d6a307d3cf6140d425 [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"
42
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;
63
64 ASSERT(ptr->s != 0);
65
66 agf->agf_roots[btnum] = ptr->s;
67 be32_add_cpu(&agf->agf_levels[btnum], inc);
68 cur->bc_mp->m_perag[seqno].pagf_levels[btnum] += inc;
69
70 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
71}
72
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110073STATIC int
74xfs_allocbt_alloc_block(
75 struct xfs_btree_cur *cur,
76 union xfs_btree_ptr *start,
77 union xfs_btree_ptr *new,
78 int length,
79 int *stat)
80{
81 int error;
82 xfs_agblock_t bno;
83
84 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
85
86 /* Allocate the new block from the freelist. If we can't, give up. */
87 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
88 &bno, 1);
89 if (error) {
90 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
91 return error;
92 }
93
94 if (bno == NULLAGBLOCK) {
95 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
96 *stat = 0;
97 return 0;
98 }
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
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
118 bno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(bp));
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 */
134 xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
135 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);
152 __be32 len;
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100153 int numrecs;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100154
155 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
156
157 switch (reason) {
158 case LASTREC_UPDATE:
159 /*
160 * If this is the last leaf block and it's the last record,
161 * then update the size of the longest extent in the AG.
162 */
163 if (ptr != xfs_btree_get_numrecs(block))
164 return;
165 len = rec->alloc.ar_blockcount;
166 break;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100167 case LASTREC_INSREC:
168 if (be32_to_cpu(rec->alloc.ar_blockcount) <=
169 be32_to_cpu(agf->agf_longest))
170 return;
171 len = rec->alloc.ar_blockcount;
172 break;
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100173 case LASTREC_DELREC:
174 numrecs = xfs_btree_get_numrecs(block);
175 if (ptr <= numrecs)
176 return;
177 ASSERT(ptr == numrecs + 1);
178
179 if (numrecs) {
180 xfs_alloc_rec_t *rrp;
181
182 rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
183 len = rrp->ar_blockcount;
184 } else {
185 len = 0;
186 }
187
188 break;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100189 default:
190 ASSERT(0);
191 return;
192 }
193
194 agf->agf_longest = len;
195 cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
196 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
197}
198
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100199STATIC int
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100200xfs_allocbt_get_minrecs(
201 struct xfs_btree_cur *cur,
202 int level)
203{
204 return cur->bc_mp->m_alloc_mnr[level != 0];
205}
206
207STATIC int
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100208xfs_allocbt_get_maxrecs(
209 struct xfs_btree_cur *cur,
210 int level)
211{
212 return cur->bc_mp->m_alloc_mxr[level != 0];
213}
214
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100215STATIC void
216xfs_allocbt_init_key_from_rec(
217 union xfs_btree_key *key,
218 union xfs_btree_rec *rec)
219{
220 ASSERT(rec->alloc.ar_startblock != 0);
221
222 key->alloc.ar_startblock = rec->alloc.ar_startblock;
223 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
224}
225
226STATIC void
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100227xfs_allocbt_init_rec_from_key(
228 union xfs_btree_key *key,
229 union xfs_btree_rec *rec)
230{
231 ASSERT(key->alloc.ar_startblock != 0);
232
233 rec->alloc.ar_startblock = key->alloc.ar_startblock;
234 rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
235}
236
237STATIC void
238xfs_allocbt_init_rec_from_cur(
239 struct xfs_btree_cur *cur,
240 union xfs_btree_rec *rec)
241{
242 ASSERT(cur->bc_rec.a.ar_startblock != 0);
243
244 rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
245 rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
246}
247
248STATIC void
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100249xfs_allocbt_init_ptr_from_cur(
250 struct xfs_btree_cur *cur,
251 union xfs_btree_ptr *ptr)
252{
253 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
254
255 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
256 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
257
258 ptr->s = agf->agf_roots[cur->bc_btnum];
259}
260
261STATIC __int64_t
262xfs_allocbt_key_diff(
263 struct xfs_btree_cur *cur,
264 union xfs_btree_key *key)
265{
266 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
267 xfs_alloc_key_t *kp = &key->alloc;
268 __int64_t diff;
269
270 if (cur->bc_btnum == XFS_BTNUM_BNO) {
271 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
272 rec->ar_startblock;
273 }
274
275 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
276 if (diff)
277 return diff;
278
279 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
280}
281
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100282STATIC int
283xfs_allocbt_kill_root(
284 struct xfs_btree_cur *cur,
285 struct xfs_buf *bp,
286 int level,
287 union xfs_btree_ptr *newroot)
288{
289 int error;
290
291 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
292 XFS_BTREE_STATS_INC(cur, killroot);
293
294 /*
295 * Update the root pointer, decreasing the level by 1 and then
296 * free the old root.
297 */
298 xfs_allocbt_set_root(cur, newroot, -1);
299 error = xfs_allocbt_free_block(cur, bp);
300 if (error) {
301 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
302 return error;
303 }
304
305 XFS_BTREE_STATS_INC(cur, free);
306
307 xfs_btree_setbuf(cur, level, NULL);
308 cur->bc_nlevels--;
309
310 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
311 return 0;
312}
313
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100314#ifdef XFS_BTREE_TRACE
315ktrace_t *xfs_allocbt_trace_buf;
316
317STATIC void
318xfs_allocbt_trace_enter(
319 struct xfs_btree_cur *cur,
320 const char *func,
321 char *s,
322 int type,
323 int line,
324 __psunsigned_t a0,
325 __psunsigned_t a1,
326 __psunsigned_t a2,
327 __psunsigned_t a3,
328 __psunsigned_t a4,
329 __psunsigned_t a5,
330 __psunsigned_t a6,
331 __psunsigned_t a7,
332 __psunsigned_t a8,
333 __psunsigned_t a9,
334 __psunsigned_t a10)
335{
336 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
337 (void *)func, (void *)s, NULL, (void *)cur,
338 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
339 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
340 (void *)a8, (void *)a9, (void *)a10);
341}
342
343STATIC void
344xfs_allocbt_trace_cursor(
345 struct xfs_btree_cur *cur,
346 __uint32_t *s0,
347 __uint64_t *l0,
348 __uint64_t *l1)
349{
350 *s0 = cur->bc_private.a.agno;
351 *l0 = cur->bc_rec.a.ar_startblock;
352 *l1 = cur->bc_rec.a.ar_blockcount;
353}
354
355STATIC void
356xfs_allocbt_trace_key(
357 struct xfs_btree_cur *cur,
358 union xfs_btree_key *key,
359 __uint64_t *l0,
360 __uint64_t *l1)
361{
362 *l0 = be32_to_cpu(key->alloc.ar_startblock);
363 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
364}
365
366STATIC void
367xfs_allocbt_trace_record(
368 struct xfs_btree_cur *cur,
369 union xfs_btree_rec *rec,
370 __uint64_t *l0,
371 __uint64_t *l1,
372 __uint64_t *l2)
373{
374 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
375 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
376 *l2 = 0;
377}
378#endif /* XFS_BTREE_TRACE */
379
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100380static const struct xfs_btree_ops xfs_allocbt_ops = {
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100381 .rec_len = sizeof(xfs_alloc_rec_t),
382 .key_len = sizeof(xfs_alloc_key_t),
383
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100384 .dup_cursor = xfs_allocbt_dup_cursor,
Christoph Hellwig344207c2008-10-30 16:57:16 +1100385 .set_root = xfs_allocbt_set_root,
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100386 .kill_root = xfs_allocbt_kill_root,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100387 .alloc_block = xfs_allocbt_alloc_block,
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100388 .free_block = xfs_allocbt_free_block,
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100389 .update_lastrec = xfs_allocbt_update_lastrec,
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100390 .get_minrecs = xfs_allocbt_get_minrecs,
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100391 .get_maxrecs = xfs_allocbt_get_maxrecs,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100392 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100393 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
394 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100395 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
396 .key_diff = xfs_allocbt_key_diff,
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100397
398#ifdef XFS_BTREE_TRACE
399 .trace_enter = xfs_allocbt_trace_enter,
400 .trace_cursor = xfs_allocbt_trace_cursor,
401 .trace_key = xfs_allocbt_trace_key,
402 .trace_record = xfs_allocbt_trace_record,
403#endif
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100404};
405
406/*
407 * Allocate a new allocation btree cursor.
408 */
409struct xfs_btree_cur * /* new alloc btree cursor */
410xfs_allocbt_init_cursor(
411 struct xfs_mount *mp, /* file system mount point */
412 struct xfs_trans *tp, /* transaction pointer */
413 struct xfs_buf *agbp, /* buffer for agf structure */
414 xfs_agnumber_t agno, /* allocation group number */
415 xfs_btnum_t btnum) /* btree identifier */
416{
417 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
418 struct xfs_btree_cur *cur;
419
420 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
421
422 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
423
424 cur->bc_tp = tp;
425 cur->bc_mp = mp;
426 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
427 cur->bc_btnum = btnum;
428 cur->bc_blocklog = mp->m_sb.sb_blocklog;
429
430 cur->bc_ops = &xfs_allocbt_ops;
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100431 if (btnum == XFS_BTNUM_CNT)
432 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100433
434 cur->bc_private.a.agbp = agbp;
435 cur->bc_private.a.agno = agno;
436
437 return cur;
438}