Blame view

fs/xfs/xfs_btree.c 24.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
7b7187698   Nathan Scott   [XFS] Update lice...
2
3
   * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
   * All Rights Reserved.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
   *
7b7187698   Nathan Scott   [XFS] Update lice...
5
6
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
   * published by the Free Software Foundation.
   *
7b7187698   Nathan Scott   [XFS] Update lice...
9
10
11
12
   * This program is distributed in the hope that it would be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
   *
7b7187698   Nathan Scott   [XFS] Update lice...
14
15
16
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write the Free Software Foundation,
   * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include "xfs.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
19
  #include "xfs_fs.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  #include "xfs_types.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
21
  #include "xfs_bit.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #include "xfs_log.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
23
  #include "xfs_inum.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
  #include "xfs_trans.h"
  #include "xfs_sb.h"
  #include "xfs_ag.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
  #include "xfs_dir2.h"
  #include "xfs_dmapi.h"
  #include "xfs_mount.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
  #include "xfs_bmap_btree.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
31
  #include "xfs_alloc_btree.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
  #include "xfs_ialloc_btree.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  #include "xfs_dir2_sf.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
34
  #include "xfs_attr_sf.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
  #include "xfs_dinode.h"
  #include "xfs_inode.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
37
38
  #include "xfs_btree.h"
  #include "xfs_ialloc.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include "xfs_error.h"
  
  /*
   * Cursor allocation zone.
   */
  kmem_zone_t	*xfs_btree_cur_zone;
  
  /*
   * Btree magic numbers.
   */
  const __uint32_t xfs_magics[XFS_BTNUM_MAX] =
  {
  	XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
  };
  
  /*
   * Prototypes for internal routines.
   */
  
  /*
   * Checking routine: return maxrecs for the block.
   */
  STATIC int				/* number of records fitting in block */
  xfs_btree_maxrecs(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	xfs_btree_block_t	*block);/* generic btree block pointer */
  
  /*
   * Internal routines.
   */
  
  /*
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
71
72
73
   * Retrieve the block pointer from the cursor at the given level.
   * This may be a bmap btree root or from a buffer.
   */
6add2c428   Eric Sandeen   [XFS] Fix up some...
74
  STATIC xfs_btree_block_t *			/* generic btree block pointer */
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
75
76
77
78
79
80
  xfs_btree_get_block(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	int			level,	/* level in btree */
  	struct xfs_buf		**bpp);	/* buffer containing the block */
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
85
86
87
88
89
90
   * Checking routine: return maxrecs for the block.
   */
  STATIC int				/* number of records fitting in block */
  xfs_btree_maxrecs(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	xfs_btree_block_t	*block)	/* generic btree block pointer */
  {
  	switch (cur->bc_btnum) {
  	case XFS_BTNUM_BNO:
  	case XFS_BTNUM_CNT:
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
91
92
  		return (int)XFS_ALLOC_BLOCK_MAXRECS(
  				be16_to_cpu(block->bb_h.bb_level), cur);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
  	case XFS_BTNUM_BMAP:
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
94
95
  		return (int)XFS_BMAP_BLOCK_IMAXRECS(
  				be16_to_cpu(block->bb_h.bb_level), cur);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
  	case XFS_BTNUM_INO:
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
97
98
  		return (int)XFS_INOBT_BLOCK_MAXRECS(
  				be16_to_cpu(block->bb_h.bb_level), cur);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  	default:
  		ASSERT(0);
  		return 0;
  	}
  }
  
  /*
   * External routines.
   */
  
  #ifdef DEBUG
  /*
   * Debug routine: check that block header is ok.
   */
  void
  xfs_btree_check_block(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	xfs_btree_block_t	*block,	/* generic btree block pointer */
  	int			level,	/* level of the btree block */
  	xfs_buf_t		*bp)	/* buffer containing block, if any */
  {
  	if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))
  		xfs_btree_check_lblock(cur, (xfs_btree_lblock_t *)block, level,
  			bp);
  	else
  		xfs_btree_check_sblock(cur, (xfs_btree_sblock_t *)block, level,
  			bp);
  }
  
  /*
   * Debug routine: check that keys are in the right order.
   */
  void
  xfs_btree_check_key(
  	xfs_btnum_t	btnum,		/* btree identifier */
  	void		*ak1,		/* pointer to left (lower) key */
  	void		*ak2)		/* pointer to right (higher) key */
  {
  	switch (btnum) {
  	case XFS_BTNUM_BNO: {
  		xfs_alloc_key_t	*k1;
  		xfs_alloc_key_t	*k2;
  
  		k1 = ak1;
  		k2 = ak2;
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
144
  		ASSERT(be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
147
148
149
150
151
152
  		break;
  	    }
  	case XFS_BTNUM_CNT: {
  		xfs_alloc_key_t	*k1;
  		xfs_alloc_key_t	*k2;
  
  		k1 = ak1;
  		k2 = ak2;
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
153
154
155
  		ASSERT(be32_to_cpu(k1->ar_blockcount) < be32_to_cpu(k2->ar_blockcount) ||
  		       (k1->ar_blockcount == k2->ar_blockcount &&
  			be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
161
162
163
  		break;
  	    }
  	case XFS_BTNUM_BMAP: {
  		xfs_bmbt_key_t	*k1;
  		xfs_bmbt_key_t	*k2;
  
  		k1 = ak1;
  		k2 = ak2;
8801bb99e   Christoph Hellwig   [XFS] endianess a...
164
  		ASSERT(be64_to_cpu(k1->br_startoff) < be64_to_cpu(k2->br_startoff));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
168
169
170
171
172
  		break;
  	    }
  	case XFS_BTNUM_INO: {
  		xfs_inobt_key_t	*k1;
  		xfs_inobt_key_t	*k2;
  
  		k1 = ak1;
  		k2 = ak2;
61a258486   Christoph Hellwig   [XFS] endianess a...
173
  		ASSERT(be32_to_cpu(k1->ir_startino) < be32_to_cpu(k2->ir_startino));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  		break;
  	    }
  	default:
  		ASSERT(0);
  	}
  }
  #endif	/* DEBUG */
  
  /*
   * Checking routine: check that long form block header is ok.
   */
  /* ARGSUSED */
  int					/* error (0 or EFSCORRUPTED) */
  xfs_btree_check_lblock(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	xfs_btree_lblock_t	*block,	/* btree long form block pointer */
  	int			level,	/* level of the btree block */
  	xfs_buf_t		*bp)	/* buffer for block, if any */
  {
  	int			lblock_ok; /* block passes checks */
  	xfs_mount_t		*mp;	/* file system mount point */
  
  	mp = cur->bc_mp;
  	lblock_ok =
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
198
199
200
  		be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
  		be16_to_cpu(block->bb_level) == level &&
  		be16_to_cpu(block->bb_numrecs) <=
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
202
  			xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&
  		block->bb_leftsib &&
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
203
204
  		(be64_to_cpu(block->bb_leftsib) == NULLDFSBNO ||
  		 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  		block->bb_rightsib &&
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
206
207
  		(be64_to_cpu(block->bb_rightsib) == NULLDFSBNO ||
  		 XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  	if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK,
  			XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
  		if (bp)
  			xfs_buftrace("LBTREE ERROR", bp);
  		XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
  				 mp);
  		return XFS_ERROR(EFSCORRUPTED);
  	}
  	return 0;
  }
  
  /*
   * Checking routine: check that (long) pointer is ok.
   */
  int					/* error (0 or EFSCORRUPTED) */
  xfs_btree_check_lptr(
  	xfs_btree_cur_t	*cur,		/* btree cursor */
  	xfs_dfsbno_t	ptr,		/* btree block disk address */
  	int		level)		/* btree block level */
  {
  	xfs_mount_t	*mp;		/* file system mount point */
  
  	mp = cur->bc_mp;
  	XFS_WANT_CORRUPTED_RETURN(
  		level > 0 &&
  		ptr != NULLDFSBNO &&
  		XFS_FSB_SANITY_CHECK(mp, ptr));
  	return 0;
  }
  
  #ifdef DEBUG
  /*
   * Debug routine: check that records are in the right order.
   */
  void
  xfs_btree_check_rec(
  	xfs_btnum_t	btnum,		/* btree identifier */
  	void		*ar1,		/* pointer to left (lower) record */
  	void		*ar2)		/* pointer to right (higher) record */
  {
  	switch (btnum) {
  	case XFS_BTNUM_BNO: {
  		xfs_alloc_rec_t	*r1;
  		xfs_alloc_rec_t	*r2;
  
  		r1 = ar1;
  		r2 = ar2;
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
255
256
257
  		ASSERT(be32_to_cpu(r1->ar_startblock) +
  		       be32_to_cpu(r1->ar_blockcount) <=
  		       be32_to_cpu(r2->ar_startblock));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
260
261
262
263
264
265
  		break;
  	    }
  	case XFS_BTNUM_CNT: {
  		xfs_alloc_rec_t	*r1;
  		xfs_alloc_rec_t	*r2;
  
  		r1 = ar1;
  		r2 = ar2;
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
266
267
268
  		ASSERT(be32_to_cpu(r1->ar_blockcount) < be32_to_cpu(r2->ar_blockcount) ||
  		       (r1->ar_blockcount == r2->ar_blockcount &&
  			be32_to_cpu(r1->ar_startblock) < be32_to_cpu(r2->ar_startblock)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  		break;
  	    }
  	case XFS_BTNUM_BMAP: {
  		xfs_bmbt_rec_t	*r1;
  		xfs_bmbt_rec_t	*r2;
  
  		r1 = ar1;
  		r2 = ar2;
  		ASSERT(xfs_bmbt_disk_get_startoff(r1) +
  		       xfs_bmbt_disk_get_blockcount(r1) <=
  		       xfs_bmbt_disk_get_startoff(r2));
  		break;
  	    }
  	case XFS_BTNUM_INO: {
  		xfs_inobt_rec_t	*r1;
  		xfs_inobt_rec_t	*r2;
  
  		r1 = ar1;
  		r2 = ar2;
61a258486   Christoph Hellwig   [XFS] endianess a...
288
289
  		ASSERT(be32_to_cpu(r1->ir_startino) + XFS_INODES_PER_CHUNK <=
  		       be32_to_cpu(r2->ir_startino));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  		break;
  	    }
  	default:
  		ASSERT(0);
  	}
  }
  #endif	/* DEBUG */
  
  /*
   * Checking routine: check that block header is ok.
   */
  /* ARGSUSED */
  int					/* error (0 or EFSCORRUPTED) */
  xfs_btree_check_sblock(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	xfs_btree_sblock_t	*block,	/* btree short form block pointer */
  	int			level,	/* level of the btree block */
  	xfs_buf_t		*bp)	/* buffer containing block */
  {
  	xfs_buf_t		*agbp;	/* buffer for ag. freespace struct */
  	xfs_agf_t		*agf;	/* ag. freespace structure */
  	xfs_agblock_t		agflen;	/* native ag. freespace length */
  	int			sblock_ok; /* block passes checks */
  
  	agbp = cur->bc_private.a.agbp;
  	agf = XFS_BUF_TO_AGF(agbp);
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
316
  	agflen = be32_to_cpu(agf->agf_length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
  	sblock_ok =
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
318
319
320
  		be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
  		be16_to_cpu(block->bb_level) == level &&
  		be16_to_cpu(block->bb_numrecs) <=
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
  			xfs_btree_maxrecs(cur, (xfs_btree_block_t *)block) &&
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
322
323
  		(be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK ||
  		 be32_to_cpu(block->bb_leftsib) < agflen) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
  		block->bb_leftsib &&
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
325
326
  		(be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK ||
  		 be32_to_cpu(block->bb_rightsib) < agflen) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  		block->bb_rightsib;
  	if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
  			XFS_ERRTAG_BTREE_CHECK_SBLOCK,
  			XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
  		if (bp)
  			xfs_buftrace("SBTREE ERROR", bp);
  		XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
  				 cur->bc_mp);
  		return XFS_ERROR(EFSCORRUPTED);
  	}
  	return 0;
  }
  
  /*
   * Checking routine: check that (short) pointer is ok.
   */
  int					/* error (0 or EFSCORRUPTED) */
  xfs_btree_check_sptr(
  	xfs_btree_cur_t	*cur,		/* btree cursor */
  	xfs_agblock_t	ptr,		/* btree block disk address */
  	int		level)		/* btree block level */
  {
  	xfs_buf_t	*agbp;		/* buffer for ag. freespace struct */
  	xfs_agf_t	*agf;		/* ag. freespace structure */
  
  	agbp = cur->bc_private.a.agbp;
  	agf = XFS_BUF_TO_AGF(agbp);
  	XFS_WANT_CORRUPTED_RETURN(
  		level > 0 &&
  		ptr != NULLAGBLOCK && ptr != 0 &&
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
357
  		ptr < be32_to_cpu(agf->agf_length));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
  	return 0;
  }
  
  /*
   * Delete the btree cursor.
   */
  void
  xfs_btree_del_cursor(
  	xfs_btree_cur_t	*cur,		/* btree cursor */
  	int		error)		/* del because of error */
  {
  	int		i;		/* btree level */
  
  	/*
  	 * Clear the buffer pointers, and release the buffers.
  	 * If we're doing this in the face of an error, we
  	 * need to make sure to inspect all of the entries
  	 * in the bc_bufs array for buffers to be unlocked.
  	 * This is because some of the btree code works from
  	 * level n down to 0, and if we get an error along
  	 * the way we won't have initialized all the entries
  	 * down to 0.
  	 */
  	for (i = 0; i < cur->bc_nlevels; i++) {
  		if (cur->bc_bufs[i])
  			xfs_btree_setbuf(cur, i, NULL);
  		else if (!error)
  			break;
  	}
  	/*
  	 * Can't free a bmap cursor without having dealt with the
  	 * allocated indirect blocks' accounting.
  	 */
  	ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
  	       cur->bc_private.b.allocated == 0);
  	/*
  	 * Free the cursor.
  	 */
  	kmem_zone_free(xfs_btree_cur_zone, cur);
  }
  
  /*
   * Duplicate the btree cursor.
   * Allocate a new one, copy the record, re-get the buffers.
   */
  int					/* error */
  xfs_btree_dup_cursor(
  	xfs_btree_cur_t	*cur,		/* input cursor */
  	xfs_btree_cur_t	**ncur)		/* output cursor */
  {
  	xfs_buf_t	*bp;		/* btree block's buffer pointer */
  	int		error;		/* error return value */
  	int		i;		/* level number of btree block */
  	xfs_mount_t	*mp;		/* mount structure for filesystem */
  	xfs_btree_cur_t	*new;		/* new cursor value */
  	xfs_trans_t	*tp;		/* transaction pointer, can be NULL */
  
  	tp = cur->bc_tp;
  	mp = cur->bc_mp;
  	/*
  	 * Allocate a new cursor like the old one.
  	 */
  	new = xfs_btree_init_cursor(mp, tp, cur->bc_private.a.agbp,
  		cur->bc_private.a.agno, cur->bc_btnum, cur->bc_private.b.ip,
  		cur->bc_private.b.whichfork);
  	/*
  	 * Copy the record currently in the cursor.
  	 */
  	new->bc_rec = cur->bc_rec;
  	/*
  	 * For each level current, re-get the buffer and copy the ptr value.
  	 */
  	for (i = 0; i < new->bc_nlevels; i++) {
  		new->bc_ptrs[i] = cur->bc_ptrs[i];
  		new->bc_ra[i] = cur->bc_ra[i];
  		if ((bp = cur->bc_bufs[i])) {
  			if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  				XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
  				xfs_btree_del_cursor(new, error);
  				*ncur = NULL;
  				return error;
  			}
  			new->bc_bufs[i] = bp;
  			ASSERT(bp);
  			ASSERT(!XFS_BUF_GETERROR(bp));
  		} else
  			new->bc_bufs[i] = NULL;
  	}
  	/*
  	 * For bmap btrees, copy the firstblock, flist, and flags values,
  	 * since init cursor doesn't get them.
  	 */
  	if (new->bc_btnum == XFS_BTNUM_BMAP) {
  		new->bc_private.b.firstblock = cur->bc_private.b.firstblock;
  		new->bc_private.b.flist = cur->bc_private.b.flist;
  		new->bc_private.b.flags = cur->bc_private.b.flags;
  	}
  	*ncur = new;
  	return 0;
  }
  
  /*
   * Change the cursor to point to the first record at the given level.
   * Other levels are unaffected.
   */
  int					/* success=1, failure=0 */
  xfs_btree_firstrec(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	int			level)	/* level to change */
  {
  	xfs_btree_block_t	*block;	/* generic btree block pointer */
  	xfs_buf_t		*bp;	/* buffer containing block */
  
  	/*
  	 * Get the block pointer for this level.
  	 */
  	block = xfs_btree_get_block(cur, level, &bp);
  	xfs_btree_check_block(cur, block, level, bp);
  	/*
  	 * It's empty, there is no such record.
  	 */
  	if (!block->bb_h.bb_numrecs)
  		return 0;
  	/*
  	 * Set the ptr value to 1, that's the first record/key.
  	 */
  	cur->bc_ptrs[level] = 1;
  	return 1;
  }
  
  /*
   * Retrieve the block pointer from the cursor at the given level.
   * This may be a bmap btree root or from a buffer.
   */
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
492
  STATIC xfs_btree_block_t *		/* generic btree block pointer */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  xfs_btree_get_block(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	int			level,	/* level in btree */
  	xfs_buf_t		**bpp)	/* buffer containing the block */
  {
  	xfs_btree_block_t	*block;	/* return value */
  	xfs_buf_t		*bp;	/* return buffer */
  	xfs_ifork_t		*ifp;	/* inode fork pointer */
  	int			whichfork; /* data or attr fork */
  
  	if (cur->bc_btnum == XFS_BTNUM_BMAP && level == cur->bc_nlevels - 1) {
  		whichfork = cur->bc_private.b.whichfork;
  		ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, whichfork);
  		block = (xfs_btree_block_t *)ifp->if_broot;
  		bp = NULL;
  	} else {
  		bp = cur->bc_bufs[level];
  		block = XFS_BUF_TO_BLOCK(bp);
  	}
  	ASSERT(block != NULL);
  	*bpp = bp;
  	return block;
  }
  
  /*
   * Get a buffer for the block, return it with no data read.
   * Long-form addressing.
   */
  xfs_buf_t *				/* buffer for fsbno */
  xfs_btree_get_bufl(
  	xfs_mount_t	*mp,		/* file system mount point */
  	xfs_trans_t	*tp,		/* transaction pointer */
  	xfs_fsblock_t	fsbno,		/* file system block number */
  	uint		lock)		/* lock flags for get_buf */
  {
  	xfs_buf_t	*bp;		/* buffer pointer (return value) */
  	xfs_daddr_t		d;		/* real disk block address */
  
  	ASSERT(fsbno != NULLFSBLOCK);
  	d = XFS_FSB_TO_DADDR(mp, fsbno);
  	bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  	ASSERT(bp);
  	ASSERT(!XFS_BUF_GETERROR(bp));
  	return bp;
  }
  
  /*
   * Get a buffer for the block, return it with no data read.
   * Short-form addressing.
   */
  xfs_buf_t *				/* buffer for agno/agbno */
  xfs_btree_get_bufs(
  	xfs_mount_t	*mp,		/* file system mount point */
  	xfs_trans_t	*tp,		/* transaction pointer */
  	xfs_agnumber_t	agno,		/* allocation group number */
  	xfs_agblock_t	agbno,		/* allocation group block number */
  	uint		lock)		/* lock flags for get_buf */
  {
  	xfs_buf_t	*bp;		/* buffer pointer (return value) */
  	xfs_daddr_t		d;		/* real disk block address */
  
  	ASSERT(agno != NULLAGNUMBER);
  	ASSERT(agbno != NULLAGBLOCK);
  	d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  	bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  	ASSERT(bp);
  	ASSERT(!XFS_BUF_GETERROR(bp));
  	return bp;
  }
  
  /*
   * Allocate a new btree cursor.
   * The cursor is either for allocation (A) or bmap (B) or inodes (I).
   */
  xfs_btree_cur_t *			/* new btree cursor */
  xfs_btree_init_cursor(
  	xfs_mount_t	*mp,		/* file system mount point */
  	xfs_trans_t	*tp,		/* transaction pointer */
  	xfs_buf_t	*agbp,		/* (A only) buffer for agf structure */
  					/* (I only) buffer for agi structure */
  	xfs_agnumber_t	agno,		/* (AI only) allocation group number */
  	xfs_btnum_t	btnum,		/* btree identifier */
  	xfs_inode_t	*ip,		/* (B only) inode owning the btree */
  	int		whichfork)	/* (B only) data or attr fork */
  {
  	xfs_agf_t	*agf;		/* (A) allocation group freespace */
  	xfs_agi_t	*agi;		/* (I) allocation group inodespace */
  	xfs_btree_cur_t	*cur;		/* return value */
  	xfs_ifork_t	*ifp;		/* (I) inode fork pointer */
  	int		nlevels=0;	/* number of levels in the btree */
  
  	ASSERT(xfs_btree_cur_zone != NULL);
  	/*
  	 * Allocate a new cursor.
  	 */
  	cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  	/*
  	 * Deduce the number of btree levels from the arguments.
  	 */
  	switch (btnum) {
  	case XFS_BTNUM_BNO:
  	case XFS_BTNUM_CNT:
  		agf = XFS_BUF_TO_AGF(agbp);
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
596
  		nlevels = be32_to_cpu(agf->agf_levels[btnum]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
598
599
  		break;
  	case XFS_BTNUM_BMAP:
  		ifp = XFS_IFORK_PTR(ip, whichfork);
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
600
  		nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
601
602
603
  		break;
  	case XFS_BTNUM_INO:
  		agi = XFS_BUF_TO_AGI(agbp);
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
604
  		nlevels = be32_to_cpu(agi->agi_level);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
  		break;
  	default:
  		ASSERT(0);
  	}
  	/*
  	 * Fill in the common fields.
  	 */
  	cur->bc_tp = tp;
  	cur->bc_mp = mp;
  	cur->bc_nlevels = nlevels;
  	cur->bc_btnum = btnum;
  	cur->bc_blocklog = mp->m_sb.sb_blocklog;
  	/*
  	 * Fill in private fields.
  	 */
  	switch (btnum) {
  	case XFS_BTNUM_BNO:
  	case XFS_BTNUM_CNT:
  		/*
  		 * Allocation btree fields.
  		 */
  		cur->bc_private.a.agbp = agbp;
  		cur->bc_private.a.agno = agno;
  		break;
  	case XFS_BTNUM_BMAP:
  		/*
  		 * Bmap btree fields.
  		 */
  		cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork);
  		cur->bc_private.b.ip = ip;
  		cur->bc_private.b.firstblock = NULLFSBLOCK;
  		cur->bc_private.b.flist = NULL;
  		cur->bc_private.b.allocated = 0;
  		cur->bc_private.b.flags = 0;
  		cur->bc_private.b.whichfork = whichfork;
  		break;
  	case XFS_BTNUM_INO:
  		/*
  		 * Inode allocation btree fields.
  		 */
  		cur->bc_private.i.agbp = agbp;
  		cur->bc_private.i.agno = agno;
  		break;
  	default:
  		ASSERT(0);
  	}
  	return cur;
  }
  
  /*
   * Check for the cursor referring to the last block at the given level.
   */
  int					/* 1=is last block, 0=not last block */
  xfs_btree_islastblock(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	int			level)	/* level to check */
  {
  	xfs_btree_block_t	*block;	/* generic btree block pointer */
  	xfs_buf_t		*bp;	/* buffer containing block */
  
  	block = xfs_btree_get_block(cur, level, &bp);
  	xfs_btree_check_block(cur, block, level, bp);
  	if (XFS_BTREE_LONG_PTRS(cur->bc_btnum))
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
668
  		return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669
  	else
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
670
  		return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
  }
  
  /*
   * Change the cursor to point to the last record in the current block
   * at the given level.  Other levels are unaffected.
   */
  int					/* success=1, failure=0 */
  xfs_btree_lastrec(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	int			level)	/* level to change */
  {
  	xfs_btree_block_t	*block;	/* generic btree block pointer */
  	xfs_buf_t		*bp;	/* buffer containing block */
  
  	/*
  	 * Get the block pointer for this level.
  	 */
  	block = xfs_btree_get_block(cur, level, &bp);
  	xfs_btree_check_block(cur, block, level, bp);
  	/*
  	 * It's empty, there is no such record.
  	 */
  	if (!block->bb_h.bb_numrecs)
  		return 0;
  	/*
  	 * Set the ptr value to numrecs, that's the last record/key.
  	 */
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
698
  	cur->bc_ptrs[level] = be16_to_cpu(block->bb_h.bb_numrecs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
  	return 1;
  }
  
  /*
   * Compute first and last byte offsets for the fields given.
   * Interprets the offsets table, which contains struct field offsets.
   */
  void
  xfs_btree_offsets(
  	__int64_t	fields,		/* bitmask of fields */
  	const short	*offsets,	/* table of field offsets */
  	int		nbits,		/* number of bits to inspect */
  	int		*first,		/* output: first byte offset */
  	int		*last)		/* output: last byte offset */
  {
  	int		i;		/* current bit number */
  	__int64_t	imask;		/* mask for current bit number */
  
  	ASSERT(fields != 0);
  	/*
  	 * Find the lowest bit, so the first byte offset.
  	 */
  	for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
  		if (imask & fields) {
  			*first = offsets[i];
  			break;
  		}
  	}
  	/*
  	 * Find the highest bit, so the last byte offset.
  	 */
  	for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
  		if (imask & fields) {
  			*last = offsets[i + 1] - 1;
  			break;
  		}
  	}
  }
  
  /*
   * Get a buffer for the block, return it read in.
   * Long-form addressing.
   */
  int					/* error */
  xfs_btree_read_bufl(
  	xfs_mount_t	*mp,		/* file system mount point */
  	xfs_trans_t	*tp,		/* transaction pointer */
  	xfs_fsblock_t	fsbno,		/* file system block number */
  	uint		lock,		/* lock flags for read_buf */
  	xfs_buf_t	**bpp,		/* buffer for fsbno */
  	int		refval)		/* ref count value for buffer */
  {
  	xfs_buf_t	*bp;		/* return value */
  	xfs_daddr_t		d;		/* real disk block address */
  	int		error;
  
  	ASSERT(fsbno != NULLFSBLOCK);
  	d = XFS_FSB_TO_DADDR(mp, fsbno);
  	if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  			mp->m_bsize, lock, &bp))) {
  		return error;
  	}
  	ASSERT(!bp || !XFS_BUF_GETERROR(bp));
  	if (bp != NULL) {
  		XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
  	}
  	*bpp = bp;
  	return 0;
  }
  
  /*
   * Get a buffer for the block, return it read in.
   * Short-form addressing.
   */
  int					/* error */
  xfs_btree_read_bufs(
  	xfs_mount_t	*mp,		/* file system mount point */
  	xfs_trans_t	*tp,		/* transaction pointer */
  	xfs_agnumber_t	agno,		/* allocation group number */
  	xfs_agblock_t	agbno,		/* allocation group block number */
  	uint		lock,		/* lock flags for read_buf */
  	xfs_buf_t	**bpp,		/* buffer for agno/agbno */
  	int		refval)		/* ref count value for buffer */
  {
  	xfs_buf_t	*bp;		/* return value */
  	xfs_daddr_t	d;		/* real disk block address */
  	int		error;
  
  	ASSERT(agno != NULLAGNUMBER);
  	ASSERT(agbno != NULLAGBLOCK);
  	d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  	if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  					mp->m_bsize, lock, &bp))) {
  		return error;
  	}
  	ASSERT(!bp || !XFS_BUF_GETERROR(bp));
  	if (bp != NULL) {
  		switch (refval) {
  		case XFS_ALLOC_BTREE_REF:
  			XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
  			break;
  		case XFS_INO_BTREE_REF:
  			XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
  			break;
  		}
  	}
  	*bpp = bp;
  	return 0;
  }
  
  /*
   * Read-ahead the block, don't wait for it, don't return a buffer.
   * Long-form addressing.
   */
  /* ARGSUSED */
  void
  xfs_btree_reada_bufl(
  	xfs_mount_t	*mp,		/* file system mount point */
  	xfs_fsblock_t	fsbno,		/* file system block number */
  	xfs_extlen_t	count)		/* count of filesystem blocks */
  {
  	xfs_daddr_t		d;
  
  	ASSERT(fsbno != NULLFSBLOCK);
  	d = XFS_FSB_TO_DADDR(mp, fsbno);
  	xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
  }
  
  /*
   * Read-ahead the block, don't wait for it, don't return a buffer.
   * Short-form addressing.
   */
  /* ARGSUSED */
  void
  xfs_btree_reada_bufs(
  	xfs_mount_t	*mp,		/* file system mount point */
  	xfs_agnumber_t	agno,		/* allocation group number */
  	xfs_agblock_t	agbno,		/* allocation group block number */
  	xfs_extlen_t	count)		/* count of filesystem blocks */
  {
  	xfs_daddr_t		d;
  
  	ASSERT(agno != NULLAGNUMBER);
  	ASSERT(agbno != NULLAGBLOCK);
  	d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  	xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
  }
  
  /*
   * Read-ahead btree blocks, at the given level.
   * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
   */
  int
  xfs_btree_readahead_core(
  	xfs_btree_cur_t		*cur,		/* btree cursor */
  	int			lev,		/* level in btree */
  	int			lr)		/* left/right bits */
  {
  	xfs_alloc_block_t	*a;
  	xfs_bmbt_block_t	*b;
  	xfs_inobt_block_t	*i;
  	int			rval = 0;
  
  	ASSERT(cur->bc_bufs[lev] != NULL);
  	cur->bc_ra[lev] |= lr;
  	switch (cur->bc_btnum) {
  	case XFS_BTNUM_BNO:
  	case XFS_BTNUM_CNT:
  		a = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]);
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
868
  		if ((lr & XFS_BTCUR_LEFTRA) && be32_to_cpu(a->bb_leftsib) != NULLAGBLOCK) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
  			xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
870
  				be32_to_cpu(a->bb_leftsib), 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
871
872
  			rval++;
  		}
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
873
  		if ((lr & XFS_BTCUR_RIGHTRA) && be32_to_cpu(a->bb_rightsib) != NULLAGBLOCK) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
874
  			xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
875
  				be32_to_cpu(a->bb_rightsib), 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
876
877
878
879
880
  			rval++;
  		}
  		break;
  	case XFS_BTNUM_BMAP:
  		b = XFS_BUF_TO_BMBT_BLOCK(cur->bc_bufs[lev]);
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
881
882
  		if ((lr & XFS_BTCUR_LEFTRA) && be64_to_cpu(b->bb_leftsib) != NULLDFSBNO) {
  			xfs_btree_reada_bufl(cur->bc_mp, be64_to_cpu(b->bb_leftsib), 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
883
884
  			rval++;
  		}
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
885
886
  		if ((lr & XFS_BTCUR_RIGHTRA) && be64_to_cpu(b->bb_rightsib) != NULLDFSBNO) {
  			xfs_btree_reada_bufl(cur->bc_mp, be64_to_cpu(b->bb_rightsib), 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887
888
889
890
891
  			rval++;
  		}
  		break;
  	case XFS_BTNUM_INO:
  		i = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[lev]);
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
892
  		if ((lr & XFS_BTCUR_LEFTRA) && be32_to_cpu(i->bb_leftsib) != NULLAGBLOCK) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
893
  			xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.i.agno,
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
894
  				be32_to_cpu(i->bb_leftsib), 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
895
896
  			rval++;
  		}
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
897
  		if ((lr & XFS_BTCUR_RIGHTRA) && be32_to_cpu(i->bb_rightsib) != NULLAGBLOCK) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
898
  			xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.i.agno,
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
899
  				be32_to_cpu(i->bb_rightsib), 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
  			rval++;
  		}
  		break;
  	default:
  		ASSERT(0);
  	}
  	return rval;
  }
  
  /*
   * Set the buffer for level "lev" in the cursor to bp, releasing
   * any previous buffer.
   */
  void
  xfs_btree_setbuf(
  	xfs_btree_cur_t		*cur,	/* btree cursor */
  	int			lev,	/* level in btree */
  	xfs_buf_t		*bp)	/* new buffer to set */
  {
  	xfs_btree_block_t	*b;	/* btree block */
  	xfs_buf_t		*obp;	/* old buffer pointer */
  
  	obp = cur->bc_bufs[lev];
  	if (obp)
  		xfs_trans_brelse(cur->bc_tp, obp);
  	cur->bc_bufs[lev] = bp;
  	cur->bc_ra[lev] = 0;
  	if (!bp)
  		return;
  	b = XFS_BUF_TO_BLOCK(bp);
  	if (XFS_BTREE_LONG_PTRS(cur->bc_btnum)) {
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
931
  		if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
932
  			cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
933
  		if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
934
935
  			cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  	} else {
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
936
  		if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937
  			cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
938
  		if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
940
941
  			cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  	}
  }