]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/xfs_alloc_btree.c
xfs: Don't directly reference m_perag in allocation code
[net-next-2.6.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"
a844f451 27#include "xfs_dir2.h"
1da177e4
LT
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
a844f451 30#include "xfs_bmap_btree.h"
1da177e4
LT
31#include "xfs_alloc_btree.h"
32#include "xfs_ialloc_btree.h"
a844f451
NS
33#include "xfs_dir2_sf.h"
34#include "xfs_attr_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
1da177e4 37#include "xfs_btree.h"
f5eb8e7c 38#include "xfs_btree_trace.h"
1da177e4
LT
39#include "xfs_ialloc.h"
40#include "xfs_alloc.h"
41#include "xfs_error.h"
0b1b213f 42#include "xfs_trace.h"
1da177e4 43
1da177e4 44
278d0ca1
CH
45STATIC 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
344207ce
CH
54STATIC 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;
a862e0fd 64 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
344207ce
CH
65
66 ASSERT(ptr->s != 0);
67
68 agf->agf_roots[btnum] = ptr->s;
69 be32_add_cpu(&agf->agf_levels[btnum], inc);
a862e0fd
DC
70 pag->pagf_levels[btnum] += inc;
71 xfs_perag_put(pag);
344207ce
CH
72
73 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
74}
75
f5eb8e7c
CH
76STATIC 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
d4b3a4b7
CH
111STATIC 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
b6e32227 121 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
d4b3a4b7
CH
122 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
1da177e4 142/*
278d0ca1 143 * Update the longest extent in the AGF
1da177e4 144 */
278d0ca1
CH
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)
1da177e4 152{
278d0ca1
CH
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);
a862e0fd 155 struct xfs_perag *pag;
278d0ca1 156 __be32 len;
91cca5df 157 int numrecs;
1da177e4 158
278d0ca1 159 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
1da177e4 160
278d0ca1
CH
161 switch (reason) {
162 case LASTREC_UPDATE:
1da177e4 163 /*
278d0ca1
CH
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.
1da177e4 166 */
278d0ca1
CH
167 if (ptr != xfs_btree_get_numrecs(block))
168 return;
169 len = rec->alloc.ar_blockcount;
170 break;
4b22a571
CH
171 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;
91cca5df
CH
176 break;
177 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
136341b4 186 rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
91cca5df
CH
187 len = rrp->ar_blockcount;
188 } else {
189 len = 0;
190 }
191
4b22a571 192 break;
278d0ca1
CH
193 default:
194 ASSERT(0);
195 return;
1da177e4 196 }
561f7d17 197
278d0ca1 198 agf->agf_longest = len;
a862e0fd
DC
199 pag = xfs_perag_get(cur->bc_mp, seqno);
200 pag->pagf_longest = be32_to_cpu(len);
201 xfs_perag_put(pag);
278d0ca1 202 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
561f7d17
CH
203}
204
91cca5df
CH
205STATIC int
206xfs_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
ce5e42db
CH
213STATIC int
214xfs_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
fe033cc8
CH
221STATIC 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
4b22a571
CH
232STATIC void
233xfs_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
fe033cc8
CH
254STATIC void
255xfs_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
91cca5df
CH
288STATIC 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
4a26e66e
CH
320#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
8c4ed633
CH
359#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
561f7d17 425static const struct xfs_btree_ops xfs_allocbt_ops = {
65f1eaea
CH
426 .rec_len = sizeof(xfs_alloc_rec_t),
427 .key_len = sizeof(xfs_alloc_key_t),
428
561f7d17 429 .dup_cursor = xfs_allocbt_dup_cursor,
344207ce 430 .set_root = xfs_allocbt_set_root,
91cca5df 431 .kill_root = xfs_allocbt_kill_root,
f5eb8e7c 432 .alloc_block = xfs_allocbt_alloc_block,
d4b3a4b7 433 .free_block = xfs_allocbt_free_block,
278d0ca1 434 .update_lastrec = xfs_allocbt_update_lastrec,
91cca5df 435 .get_minrecs = xfs_allocbt_get_minrecs,
ce5e42db 436 .get_maxrecs = xfs_allocbt_get_maxrecs,
fe033cc8 437 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
4b22a571
CH
438 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
439 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
fe033cc8
CH
440 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
441 .key_diff = xfs_allocbt_key_diff,
8c4ed633 442
4a26e66e
CH
443#ifdef DEBUG
444 .keys_inorder = xfs_allocbt_keys_inorder,
445 .recs_inorder = xfs_allocbt_recs_inorder,
446#endif
447
8c4ed633
CH
448#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
561f7d17
CH
454};
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;
278d0ca1
CH
481 if (btnum == XFS_BTNUM_CNT)
482 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
561f7d17
CH
483
484 cur->bc_private.a.agbp = agbp;
485 cur->bc_private.a.agno = agno;
486
487 return cur;
488}
60197e8d
CH
489
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{
7cc95a82 499 blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
60197e8d
CH
500
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}