2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
32 #include "xfs_dir2_format.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
36 #include "xfs_buf_item.h"
37 #include "xfs_cksum.h"
40 * Function declarations.
42 static int xfs_dir2_leafn_add(struct xfs_buf
*bp
, xfs_da_args_t
*args
,
45 static void xfs_dir2_leafn_check(struct xfs_inode
*dp
, struct xfs_buf
*bp
);
47 #define xfs_dir2_leafn_check(dp, bp)
49 static void xfs_dir2_leafn_moveents(xfs_da_args_t
*args
, struct xfs_buf
*bp_s
,
50 int start_s
, struct xfs_buf
*bp_d
,
51 int start_d
, int count
);
52 static void xfs_dir2_leafn_rebalance(xfs_da_state_t
*state
,
53 xfs_da_state_blk_t
*blk1
,
54 xfs_da_state_blk_t
*blk2
);
55 static int xfs_dir2_leafn_remove(xfs_da_args_t
*args
, struct xfs_buf
*bp
,
56 int index
, xfs_da_state_blk_t
*dblk
,
58 static int xfs_dir2_node_addname_int(xfs_da_args_t
*args
,
59 xfs_da_state_blk_t
*fblk
);
65 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
66 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
68 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
69 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
71 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_FREE_MAGIC
))
73 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_uuid
))
75 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
78 if (hdr
->magic
!= cpu_to_be32(XFS_DIR2_FREE_MAGIC
))
82 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
88 xfs_dir3_free_read_verify(
91 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
93 if ((xfs_sb_version_hascrc(&mp
->m_sb
) &&
94 !xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
95 XFS_DIR3_FREE_CRC_OFF
)) ||
96 !xfs_dir3_free_verify(bp
)) {
97 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
98 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
103 xfs_dir3_free_write_verify(
106 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
107 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
108 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
110 if (!xfs_dir3_free_verify(bp
)) {
111 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
112 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
116 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
120 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
122 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
), XFS_DIR3_FREE_CRC_OFF
);
125 static const struct xfs_buf_ops xfs_dir3_free_buf_ops
= {
126 .verify_read
= xfs_dir3_free_read_verify
,
127 .verify_write
= xfs_dir3_free_write_verify
,
132 __xfs_dir3_free_read(
133 struct xfs_trans
*tp
,
134 struct xfs_inode
*dp
,
136 xfs_daddr_t mappedbno
,
137 struct xfs_buf
**bpp
)
139 return xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
140 XFS_DATA_FORK
, &xfs_dir3_free_buf_ops
);
145 struct xfs_trans
*tp
,
146 struct xfs_inode
*dp
,
148 struct xfs_buf
**bpp
)
150 return __xfs_dir3_free_read(tp
, dp
, fbno
, -1, bpp
);
154 xfs_dir2_free_try_read(
155 struct xfs_trans
*tp
,
156 struct xfs_inode
*dp
,
158 struct xfs_buf
**bpp
)
160 return __xfs_dir3_free_read(tp
, dp
, fbno
, -2, bpp
);
165 xfs_dir3_free_hdr_from_disk(
166 struct xfs_dir3_icfree_hdr
*to
,
167 struct xfs_dir2_free
*from
)
169 if (from
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
)) {
170 to
->magic
= be32_to_cpu(from
->hdr
.magic
);
171 to
->firstdb
= be32_to_cpu(from
->hdr
.firstdb
);
172 to
->nvalid
= be32_to_cpu(from
->hdr
.nvalid
);
173 to
->nused
= be32_to_cpu(from
->hdr
.nused
);
175 struct xfs_dir3_free_hdr
*hdr3
= (struct xfs_dir3_free_hdr
*)from
;
177 to
->magic
= be32_to_cpu(hdr3
->hdr
.magic
);
178 to
->firstdb
= be32_to_cpu(hdr3
->firstdb
);
179 to
->nvalid
= be32_to_cpu(hdr3
->nvalid
);
180 to
->nused
= be32_to_cpu(hdr3
->nused
);
183 ASSERT(to
->magic
== XFS_DIR2_FREE_MAGIC
||
184 to
->magic
== XFS_DIR3_FREE_MAGIC
);
188 xfs_dir3_free_hdr_to_disk(
189 struct xfs_dir2_free
*to
,
190 struct xfs_dir3_icfree_hdr
*from
)
192 ASSERT(from
->magic
== XFS_DIR2_FREE_MAGIC
||
193 from
->magic
== XFS_DIR3_FREE_MAGIC
);
195 if (from
->magic
== XFS_DIR2_FREE_MAGIC
) {
196 to
->hdr
.magic
= cpu_to_be32(from
->magic
);
197 to
->hdr
.firstdb
= cpu_to_be32(from
->firstdb
);
198 to
->hdr
.nvalid
= cpu_to_be32(from
->nvalid
);
199 to
->hdr
.nused
= cpu_to_be32(from
->nused
);
201 struct xfs_dir3_free_hdr
*hdr3
= (struct xfs_dir3_free_hdr
*)to
;
203 hdr3
->hdr
.magic
= cpu_to_be32(from
->magic
);
204 hdr3
->firstdb
= cpu_to_be32(from
->firstdb
);
205 hdr3
->nvalid
= cpu_to_be32(from
->nvalid
);
206 hdr3
->nused
= cpu_to_be32(from
->nused
);
211 xfs_dir3_free_get_buf(
212 struct xfs_trans
*tp
,
213 struct xfs_inode
*dp
,
215 struct xfs_buf
**bpp
)
217 struct xfs_mount
*mp
= dp
->i_mount
;
220 struct xfs_dir3_icfree_hdr hdr
;
222 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, fbno
),
223 -1, &bp
, XFS_DATA_FORK
);
227 bp
->b_ops
= &xfs_dir3_free_buf_ops
;
230 * Initialize the new block to be empty, and remember
231 * its first slot as our empty slot.
233 hdr
.magic
= XFS_DIR2_FREE_MAGIC
;
237 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
238 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
240 hdr
.magic
= XFS_DIR3_FREE_MAGIC
;
241 hdr3
->hdr
.blkno
= cpu_to_be64(bp
->b_bn
);
242 hdr3
->hdr
.owner
= cpu_to_be64(dp
->i_ino
);
243 uuid_copy(&hdr3
->hdr
.uuid
, &mp
->m_sb
.sb_uuid
);
245 xfs_dir3_free_hdr_to_disk(bp
->b_addr
, &hdr
);
251 * Log entries from a freespace block.
254 xfs_dir2_free_log_bests(
255 struct xfs_trans
*tp
,
257 int first
, /* first entry to log */
258 int last
) /* last entry to log */
260 xfs_dir2_free_t
*free
; /* freespace structure */
264 bests
= xfs_dir3_free_bests_p(tp
->t_mountp
, free
);
265 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
266 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
267 xfs_trans_log_buf(tp
, bp
,
268 (uint
)((char *)&bests
[first
] - (char *)free
),
269 (uint
)((char *)&bests
[last
] - (char *)free
+
270 sizeof(bests
[0]) - 1));
274 * Log header from a freespace block.
277 xfs_dir2_free_log_header(
278 struct xfs_trans
*tp
,
281 xfs_dir2_free_t
*free
; /* freespace structure */
284 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
285 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
286 xfs_trans_log_buf(tp
, bp
, 0, xfs_dir3_free_hdr_size(tp
->t_mountp
) - 1);
290 * Convert a leaf-format directory to a node-format directory.
291 * We need to change the magic number of the leaf block, and copy
292 * the freespace table out of the leaf block into its own block.
295 xfs_dir2_leaf_to_node(
296 xfs_da_args_t
*args
, /* operation arguments */
297 struct xfs_buf
*lbp
) /* leaf buffer */
299 xfs_inode_t
*dp
; /* incore directory inode */
300 int error
; /* error return value */
301 struct xfs_buf
*fbp
; /* freespace buffer */
302 xfs_dir2_db_t fdb
; /* freespace block number */
303 xfs_dir2_free_t
*free
; /* freespace structure */
304 __be16
*from
; /* pointer to freespace entry */
305 int i
; /* leaf freespace index */
306 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
307 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
308 xfs_mount_t
*mp
; /* filesystem mount point */
309 int n
; /* count of live freespc ents */
310 xfs_dir2_data_off_t off
; /* freespace entry value */
311 __be16
*to
; /* pointer to freespace entry */
312 xfs_trans_t
*tp
; /* transaction pointer */
313 struct xfs_dir3_icfree_hdr freehdr
;
315 trace_xfs_dir2_leaf_to_node(args
);
321 * Add a freespace block to the directory.
323 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
326 ASSERT(fdb
== XFS_DIR2_FREE_FIRSTDB(mp
));
328 * Get the buffer for the new freespace block.
330 error
= xfs_dir3_free_get_buf(tp
, dp
, fdb
, &fbp
);
335 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
337 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
338 ASSERT(be32_to_cpu(ltp
->bestcount
) <=
339 (uint
)dp
->i_d
.di_size
/ mp
->m_dirblksize
);
342 * Copy freespace entries from the leaf block to the new block.
343 * Count active entries.
345 from
= xfs_dir2_leaf_bests_p(ltp
);
346 to
= xfs_dir3_free_bests_p(mp
, free
);
347 for (i
= n
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
348 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
350 *to
= cpu_to_be16(off
);
354 * Now initialize the freespace block header.
357 freehdr
.nvalid
= be32_to_cpu(ltp
->bestcount
);
359 xfs_dir3_free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
360 xfs_dir2_free_log_bests(tp
, fbp
, 0, freehdr
.nvalid
- 1);
361 xfs_dir2_free_log_header(tp
, fbp
);
363 /* convert the leaf to a leafnode */
364 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
365 lbp
->b_ops
= &xfs_dir2_leafn_buf_ops
;
366 xfs_dir2_leaf_log_header(tp
, lbp
);
367 xfs_dir2_leafn_check(dp
, lbp
);
372 * Add a leaf entry to a leaf block in a node-form directory.
373 * The other work necessary is done from the caller.
375 static int /* error */
377 struct xfs_buf
*bp
, /* leaf buffer */
378 xfs_da_args_t
*args
, /* operation arguments */
379 int index
) /* insertion pt for new entry */
381 int compact
; /* compacting stale leaves */
382 xfs_inode_t
*dp
; /* incore directory inode */
383 int highstale
; /* next stale entry */
384 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
385 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
386 int lfloghigh
; /* high leaf entry logging */
387 int lfloglow
; /* low leaf entry logging */
388 int lowstale
; /* previous stale entry */
389 xfs_mount_t
*mp
; /* filesystem mount point */
390 xfs_trans_t
*tp
; /* transaction pointer */
392 trace_xfs_dir2_leafn_add(args
, index
);
400 * Quick check just to make sure we are not going to index
401 * into other peoples memory
404 return XFS_ERROR(EFSCORRUPTED
);
407 * If there are already the maximum number of leaf entries in
408 * the block, if there are no stale entries it won't fit.
409 * Caller will do a split. If there are stale entries we'll do
413 if (be16_to_cpu(leaf
->hdr
.count
) == xfs_dir2_max_leaf_ents(mp
)) {
414 if (!leaf
->hdr
.stale
)
415 return XFS_ERROR(ENOSPC
);
416 compact
= be16_to_cpu(leaf
->hdr
.stale
) > 1;
419 ASSERT(index
== 0 || be32_to_cpu(leaf
->ents
[index
- 1].hashval
) <= args
->hashval
);
420 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
421 be32_to_cpu(leaf
->ents
[index
].hashval
) >= args
->hashval
);
423 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
427 * Compact out all but one stale leaf entry. Leaves behind
428 * the entry closest to index.
431 xfs_dir2_leaf_compact_x1(bp
, &index
, &lowstale
, &highstale
,
432 &lfloglow
, &lfloghigh
);
435 * Set impossible logging indices for this case.
437 else if (leaf
->hdr
.stale
) {
438 lfloglow
= be16_to_cpu(leaf
->hdr
.count
);
443 * Insert the new entry, log everything.
445 lep
= xfs_dir2_leaf_find_entry(leaf
, index
, compact
, lowstale
,
446 highstale
, &lfloglow
, &lfloghigh
);
448 lep
->hashval
= cpu_to_be32(args
->hashval
);
449 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp
,
450 args
->blkno
, args
->index
));
451 xfs_dir2_leaf_log_header(tp
, bp
);
452 xfs_dir2_leaf_log_ents(tp
, bp
, lfloglow
, lfloghigh
);
453 xfs_dir2_leafn_check(dp
, bp
);
459 * Check internal consistency of a leafn block.
462 xfs_dir2_leafn_check(
463 struct xfs_inode
*dp
,
466 int i
; /* leaf index */
467 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
468 xfs_mount_t
*mp
; /* filesystem mount point */
469 int stale
; /* count of stale leaves */
473 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
474 ASSERT(be16_to_cpu(leaf
->hdr
.count
) <= xfs_dir2_max_leaf_ents(mp
));
475 for (i
= stale
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); i
++) {
476 if (i
+ 1 < be16_to_cpu(leaf
->hdr
.count
)) {
477 ASSERT(be32_to_cpu(leaf
->ents
[i
].hashval
) <=
478 be32_to_cpu(leaf
->ents
[i
+ 1].hashval
));
480 if (leaf
->ents
[i
].address
== cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
483 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == stale
);
487 xfs_dir2_free_hdr_check(
488 struct xfs_mount
*mp
,
492 struct xfs_dir3_icfree_hdr hdr
;
494 xfs_dir3_free_hdr_from_disk(&hdr
, bp
->b_addr
);
496 ASSERT((hdr
.firstdb
% xfs_dir3_free_max_bests(mp
)) == 0);
497 ASSERT(hdr
.firstdb
<= db
);
498 ASSERT(db
< hdr
.firstdb
+ hdr
.nvalid
);
501 #define xfs_dir2_free_hdr_check(mp, dp, db)
505 * Return the last hash value in the leaf.
506 * Stale entries are ok.
508 xfs_dahash_t
/* hash value */
509 xfs_dir2_leafn_lasthash(
510 struct xfs_buf
*bp
, /* leaf buffer */
511 int *count
) /* count of entries in leaf */
513 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
516 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
518 *count
= be16_to_cpu(leaf
->hdr
.count
);
519 if (!leaf
->hdr
.count
)
521 return be32_to_cpu(leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
) - 1].hashval
);
525 * Look up a leaf entry for space to add a name in a node-format leaf block.
526 * The extrablk in state is a freespace block.
529 xfs_dir2_leafn_lookup_for_addname(
530 struct xfs_buf
*bp
, /* leaf buffer */
531 xfs_da_args_t
*args
, /* operation arguments */
532 int *indexp
, /* out: leaf entry index */
533 xfs_da_state_t
*state
) /* state to fill in */
535 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
536 xfs_dir2_db_t curdb
= -1; /* current data block number */
537 xfs_dir2_db_t curfdb
= -1; /* current free block number */
538 xfs_inode_t
*dp
; /* incore directory inode */
539 int error
; /* error return value */
540 int fi
; /* free entry index */
541 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
542 int index
; /* leaf entry index */
543 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
544 int length
; /* length of new data entry */
545 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
546 xfs_mount_t
*mp
; /* filesystem mount point */
547 xfs_dir2_db_t newdb
; /* new data block number */
548 xfs_dir2_db_t newfdb
; /* new free block number */
549 xfs_trans_t
*tp
; /* transaction pointer */
555 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
557 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
559 xfs_dir2_leafn_check(dp
, bp
);
561 * Look up the hash value in the leaf entries.
563 index
= xfs_dir2_leaf_search_hash(args
, bp
);
565 * Do we have a buffer coming in?
567 if (state
->extravalid
) {
568 /* If so, it's a free block buffer, get the block number. */
569 curbp
= state
->extrablk
.bp
;
570 curfdb
= state
->extrablk
.blkno
;
571 free
= curbp
->b_addr
;
572 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
573 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
575 length
= xfs_dir2_data_entsize(args
->namelen
);
577 * Loop over leaf entries with the right hash value.
579 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
580 be32_to_cpu(lep
->hashval
) == args
->hashval
;
583 * Skip stale leaf entries.
585 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
588 * Pull the data block number from the entry.
590 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
592 * For addname, we're looking for a place to put the new entry.
593 * We want to use a data block with an entry of equal
594 * hash value to ours if there is one with room.
596 * If this block isn't the data block we already have
597 * in hand, take a look at it.
599 if (newdb
!= curdb
) {
604 * Convert the data block to the free block
605 * holding its freespace information.
607 newfdb
= xfs_dir2_db_to_fdb(mp
, newdb
);
609 * If it's not the one we have in hand, read it in.
611 if (newfdb
!= curfdb
) {
613 * If we had one before, drop it.
616 xfs_trans_brelse(tp
, curbp
);
618 error
= xfs_dir2_free_read(tp
, dp
,
619 xfs_dir2_db_to_da(mp
, newfdb
),
623 free
= curbp
->b_addr
;
625 xfs_dir2_free_hdr_check(mp
, curbp
, curdb
);
628 * Get the index for our entry.
630 fi
= xfs_dir2_db_to_fdindex(mp
, curdb
);
632 * If it has room, return it.
634 bests
= xfs_dir3_free_bests_p(mp
, free
);
635 if (unlikely(bests
[fi
] == cpu_to_be16(NULLDATAOFF
))) {
636 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
637 XFS_ERRLEVEL_LOW
, mp
);
638 if (curfdb
!= newfdb
)
639 xfs_trans_brelse(tp
, curbp
);
640 return XFS_ERROR(EFSCORRUPTED
);
643 if (be16_to_cpu(bests
[fi
]) >= length
)
647 /* Didn't find any space */
650 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
652 /* Giving back a free block. */
653 state
->extravalid
= 1;
654 state
->extrablk
.bp
= curbp
;
655 state
->extrablk
.index
= fi
;
656 state
->extrablk
.blkno
= curfdb
;
659 * Important: this magic number is not in the buffer - it's for
660 * buffer type information and therefore only the free/data type
661 * matters here, not whether CRCs are enabled or not.
663 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
665 state
->extravalid
= 0;
668 * Return the index, that will be the insertion point.
671 return XFS_ERROR(ENOENT
);
675 * Look up a leaf entry in a node-format leaf block.
676 * The extrablk in state a data block.
679 xfs_dir2_leafn_lookup_for_entry(
680 struct xfs_buf
*bp
, /* leaf buffer */
681 xfs_da_args_t
*args
, /* operation arguments */
682 int *indexp
, /* out: leaf entry index */
683 xfs_da_state_t
*state
) /* state to fill in */
685 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
686 xfs_dir2_db_t curdb
= -1; /* current data block number */
687 xfs_dir2_data_entry_t
*dep
; /* data block entry */
688 xfs_inode_t
*dp
; /* incore directory inode */
689 int error
; /* error return value */
690 int index
; /* leaf entry index */
691 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
692 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
693 xfs_mount_t
*mp
; /* filesystem mount point */
694 xfs_dir2_db_t newdb
; /* new data block number */
695 xfs_trans_t
*tp
; /* transaction pointer */
696 enum xfs_dacmp cmp
; /* comparison result */
702 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
704 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
706 xfs_dir2_leafn_check(dp
, bp
);
708 * Look up the hash value in the leaf entries.
710 index
= xfs_dir2_leaf_search_hash(args
, bp
);
712 * Do we have a buffer coming in?
714 if (state
->extravalid
) {
715 curbp
= state
->extrablk
.bp
;
716 curdb
= state
->extrablk
.blkno
;
719 * Loop over leaf entries with the right hash value.
721 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
722 be32_to_cpu(lep
->hashval
) == args
->hashval
;
725 * Skip stale leaf entries.
727 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
730 * Pull the data block number from the entry.
732 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
734 * Not adding a new entry, so we really want to find
735 * the name given to us.
737 * If it's a different data block, go get it.
739 if (newdb
!= curdb
) {
741 * If we had a block before that we aren't saving
742 * for a CI name, drop it
744 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
745 curdb
!= state
->extrablk
.blkno
))
746 xfs_trans_brelse(tp
, curbp
);
748 * If needing the block that is saved with a CI match,
749 * use it otherwise read in the new data block.
751 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
752 newdb
== state
->extrablk
.blkno
) {
753 ASSERT(state
->extravalid
);
754 curbp
= state
->extrablk
.bp
;
756 error
= xfs_dir2_data_read(tp
, dp
,
757 xfs_dir2_db_to_da(mp
, newdb
),
762 xfs_dir2_data_check(dp
, curbp
);
766 * Point to the data entry.
768 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
769 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
771 * Compare the entry and if it's an exact match, return
772 * EEXIST immediately. If it's the first case-insensitive
773 * match, store the block & inode number and continue looking.
775 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
776 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
777 /* If there is a CI match block, drop it */
778 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
779 curdb
!= state
->extrablk
.blkno
)
780 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
781 args
->cmpresult
= cmp
;
782 args
->inumber
= be64_to_cpu(dep
->inumber
);
784 state
->extravalid
= 1;
785 state
->extrablk
.bp
= curbp
;
786 state
->extrablk
.blkno
= curdb
;
787 state
->extrablk
.index
= (int)((char *)dep
-
788 (char *)curbp
->b_addr
);
789 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
790 curbp
->b_ops
= &xfs_dir2_data_buf_ops
;
791 if (cmp
== XFS_CMP_EXACT
)
792 return XFS_ERROR(EEXIST
);
795 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
796 (args
->op_flags
& XFS_DA_OP_OKNOENT
));
798 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
799 /* Giving back last used data block. */
800 state
->extravalid
= 1;
801 state
->extrablk
.bp
= curbp
;
802 state
->extrablk
.index
= -1;
803 state
->extrablk
.blkno
= curdb
;
804 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
805 curbp
->b_ops
= &xfs_dir2_data_buf_ops
;
807 /* If the curbp is not the CI match block, drop it */
808 if (state
->extrablk
.bp
!= curbp
)
809 xfs_trans_brelse(tp
, curbp
);
812 state
->extravalid
= 0;
815 return XFS_ERROR(ENOENT
);
819 * Look up a leaf entry in a node-format leaf block.
820 * If this is an addname then the extrablk in state is a freespace block,
821 * otherwise it's a data block.
824 xfs_dir2_leafn_lookup_int(
825 struct xfs_buf
*bp
, /* leaf buffer */
826 xfs_da_args_t
*args
, /* operation arguments */
827 int *indexp
, /* out: leaf entry index */
828 xfs_da_state_t
*state
) /* state to fill in */
830 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
831 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
833 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
837 * Move count leaf entries from source to destination leaf.
838 * Log entries and headers. Stale entries are preserved.
841 xfs_dir2_leafn_moveents(
842 xfs_da_args_t
*args
, /* operation arguments */
843 struct xfs_buf
*bp_s
, /* source leaf buffer */
844 int start_s
, /* source leaf index */
845 struct xfs_buf
*bp_d
, /* destination leaf buffer */
846 int start_d
, /* destination leaf index */
847 int count
) /* count of leaves to copy */
849 xfs_dir2_leaf_t
*leaf_d
; /* destination leaf structure */
850 xfs_dir2_leaf_t
*leaf_s
; /* source leaf structure */
851 int stale
; /* count stale leaves copied */
852 xfs_trans_t
*tp
; /* transaction pointer */
854 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
857 * Silently return if nothing to do.
863 leaf_s
= bp_s
->b_addr
;
864 leaf_d
= bp_d
->b_addr
;
866 * If the destination index is not the end of the current
867 * destination leaf entries, open up a hole in the destination
868 * to hold the new entries.
870 if (start_d
< be16_to_cpu(leaf_d
->hdr
.count
)) {
871 memmove(&leaf_d
->ents
[start_d
+ count
], &leaf_d
->ents
[start_d
],
872 (be16_to_cpu(leaf_d
->hdr
.count
) - start_d
) *
873 sizeof(xfs_dir2_leaf_entry_t
));
874 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
+ count
,
875 count
+ be16_to_cpu(leaf_d
->hdr
.count
) - 1);
878 * If the source has stale leaves, count the ones in the copy range
879 * so we can update the header correctly.
881 if (leaf_s
->hdr
.stale
) {
882 int i
; /* temp leaf index */
884 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
885 if (leaf_s
->ents
[i
].address
==
886 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
892 * Copy the leaf entries from source to destination.
894 memcpy(&leaf_d
->ents
[start_d
], &leaf_s
->ents
[start_s
],
895 count
* sizeof(xfs_dir2_leaf_entry_t
));
896 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
, start_d
+ count
- 1);
898 * If there are source entries after the ones we copied,
899 * delete the ones we copied by sliding the next ones down.
901 if (start_s
+ count
< be16_to_cpu(leaf_s
->hdr
.count
)) {
902 memmove(&leaf_s
->ents
[start_s
], &leaf_s
->ents
[start_s
+ count
],
903 count
* sizeof(xfs_dir2_leaf_entry_t
));
904 xfs_dir2_leaf_log_ents(tp
, bp_s
, start_s
, start_s
+ count
- 1);
907 * Update the headers and log them.
909 be16_add_cpu(&leaf_s
->hdr
.count
, -(count
));
910 be16_add_cpu(&leaf_s
->hdr
.stale
, -(stale
));
911 be16_add_cpu(&leaf_d
->hdr
.count
, count
);
912 be16_add_cpu(&leaf_d
->hdr
.stale
, stale
);
913 xfs_dir2_leaf_log_header(tp
, bp_s
);
914 xfs_dir2_leaf_log_header(tp
, bp_d
);
915 xfs_dir2_leafn_check(args
->dp
, bp_s
);
916 xfs_dir2_leafn_check(args
->dp
, bp_d
);
920 * Determine the sort order of two leaf blocks.
921 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
924 xfs_dir2_leafn_order(
925 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
926 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
928 xfs_dir2_leaf_t
*leaf1
; /* leaf1 structure */
929 xfs_dir2_leaf_t
*leaf2
; /* leaf2 structure */
931 leaf1
= leaf1_bp
->b_addr
;
932 leaf2
= leaf2_bp
->b_addr
;
933 ASSERT(leaf1
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
934 ASSERT(leaf2
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
935 if (be16_to_cpu(leaf1
->hdr
.count
) > 0 &&
936 be16_to_cpu(leaf2
->hdr
.count
) > 0 &&
937 (be32_to_cpu(leaf2
->ents
[0].hashval
) < be32_to_cpu(leaf1
->ents
[0].hashval
) ||
938 be32_to_cpu(leaf2
->ents
[be16_to_cpu(leaf2
->hdr
.count
) - 1].hashval
) <
939 be32_to_cpu(leaf1
->ents
[be16_to_cpu(leaf1
->hdr
.count
) - 1].hashval
)))
945 * Rebalance leaf entries between two leaf blocks.
946 * This is actually only called when the second block is new,
947 * though the code deals with the general case.
948 * A new entry will be inserted in one of the blocks, and that
949 * entry is taken into account when balancing.
952 xfs_dir2_leafn_rebalance(
953 xfs_da_state_t
*state
, /* btree cursor */
954 xfs_da_state_blk_t
*blk1
, /* first btree block */
955 xfs_da_state_blk_t
*blk2
) /* second btree block */
957 xfs_da_args_t
*args
; /* operation arguments */
958 int count
; /* count (& direction) leaves */
959 int isleft
; /* new goes in left leaf */
960 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
961 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
962 int mid
; /* midpoint leaf index */
964 int oldstale
; /* old count of stale leaves */
966 int oldsum
; /* old total leaf count */
967 int swap
; /* swapped leaf blocks */
971 * If the block order is wrong, swap the arguments.
973 if ((swap
= xfs_dir2_leafn_order(blk1
->bp
, blk2
->bp
))) {
974 xfs_da_state_blk_t
*tmp
; /* temp for block swap */
980 leaf1
= blk1
->bp
->b_addr
;
981 leaf2
= blk2
->bp
->b_addr
;
982 oldsum
= be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
);
984 oldstale
= be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
);
988 * If the old leaf count was odd then the new one will be even,
989 * so we need to divide the new count evenly.
992 xfs_dahash_t midhash
; /* middle entry hash value */
994 if (mid
>= be16_to_cpu(leaf1
->hdr
.count
))
995 midhash
= be32_to_cpu(leaf2
->ents
[mid
- be16_to_cpu(leaf1
->hdr
.count
)].hashval
);
997 midhash
= be32_to_cpu(leaf1
->ents
[mid
].hashval
);
998 isleft
= args
->hashval
<= midhash
;
1001 * If the old count is even then the new count is odd, so there's
1002 * no preferred side for the new entry.
1003 * Pick the left one.
1008 * Calculate moved entry count. Positive means left-to-right,
1009 * negative means right-to-left. Then move the entries.
1011 count
= be16_to_cpu(leaf1
->hdr
.count
) - mid
+ (isleft
== 0);
1013 xfs_dir2_leafn_moveents(args
, blk1
->bp
,
1014 be16_to_cpu(leaf1
->hdr
.count
) - count
, blk2
->bp
, 0, count
);
1016 xfs_dir2_leafn_moveents(args
, blk2
->bp
, 0, blk1
->bp
,
1017 be16_to_cpu(leaf1
->hdr
.count
), count
);
1018 ASSERT(be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
) == oldsum
);
1019 ASSERT(be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
) == oldstale
);
1021 * Mark whether we're inserting into the old or new leaf.
1023 if (be16_to_cpu(leaf1
->hdr
.count
) < be16_to_cpu(leaf2
->hdr
.count
))
1024 state
->inleaf
= swap
;
1025 else if (be16_to_cpu(leaf1
->hdr
.count
) > be16_to_cpu(leaf2
->hdr
.count
))
1026 state
->inleaf
= !swap
;
1029 swap
^ (blk1
->index
<= be16_to_cpu(leaf1
->hdr
.count
));
1031 * Adjust the expected index for insertion.
1034 blk2
->index
= blk1
->index
- be16_to_cpu(leaf1
->hdr
.count
);
1037 * Finally sanity check just to make sure we are not returning a
1040 if(blk2
->index
< 0) {
1043 xfs_alert(args
->dp
->i_mount
,
1044 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
1045 __func__
, blk1
->index
);
1050 xfs_dir3_data_block_free(
1051 xfs_da_args_t
*args
,
1052 struct xfs_dir2_data_hdr
*hdr
,
1053 struct xfs_dir2_free
*free
,
1056 struct xfs_buf
*fbp
,
1059 struct xfs_trans
*tp
= args
->trans
;
1062 struct xfs_dir3_icfree_hdr freehdr
;
1064 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
1066 bests
= xfs_dir3_free_bests_p(tp
->t_mountp
, free
);
1069 * Data block is not empty, just set the free entry to the new
1072 bests
[findex
] = cpu_to_be16(longest
);
1073 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1077 /* One less used entry in the free table. */
1081 * If this was the last entry in the table, we can trim the table size
1082 * back. There might be other entries at the end referring to
1083 * non-existent data blocks, get those too.
1085 if (findex
== freehdr
.nvalid
- 1) {
1086 int i
; /* free entry index */
1088 for (i
= findex
- 1; i
>= 0; i
--) {
1089 if (bests
[i
] != cpu_to_be16(NULLDATAOFF
))
1092 freehdr
.nvalid
= i
+ 1;
1095 /* Not the last entry, just punch it out. */
1096 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1100 xfs_dir3_free_hdr_to_disk(free
, &freehdr
);
1101 xfs_dir2_free_log_header(tp
, fbp
);
1104 * If there are no useful entries left in the block, get rid of the
1107 if (!freehdr
.nused
) {
1110 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1114 } else if (error
!= ENOSPC
|| args
->total
!= 0)
1117 * It's possible to get ENOSPC if there is no
1118 * space reservation. In this case some one
1119 * else will eventually get rid of this block.
1123 /* Log the free entry that changed, unless we got rid of it. */
1125 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1130 * Remove an entry from a node directory.
1131 * This removes the leaf entry and the data entry,
1132 * and updates the free block if necessary.
1134 static int /* error */
1135 xfs_dir2_leafn_remove(
1136 xfs_da_args_t
*args
, /* operation arguments */
1137 struct xfs_buf
*bp
, /* leaf buffer */
1138 int index
, /* leaf entry index */
1139 xfs_da_state_blk_t
*dblk
, /* data block */
1140 int *rval
) /* resulting block needs join */
1142 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1143 xfs_dir2_db_t db
; /* data block number */
1144 struct xfs_buf
*dbp
; /* data block buffer */
1145 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1146 xfs_inode_t
*dp
; /* incore directory inode */
1147 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1148 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1149 int longest
; /* longest data free entry */
1150 int off
; /* data block entry offset */
1151 xfs_mount_t
*mp
; /* filesystem mount point */
1152 int needlog
; /* need to log data header */
1153 int needscan
; /* need to rescan data frees */
1154 xfs_trans_t
*tp
; /* transaction pointer */
1156 trace_xfs_dir2_leafn_remove(args
, index
);
1162 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1164 * Point to the entry we're removing.
1166 lep
= &leaf
->ents
[index
];
1168 * Extract the data block and offset from the entry.
1170 db
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
1171 ASSERT(dblk
->blkno
== db
);
1172 off
= xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
));
1173 ASSERT(dblk
->index
== off
);
1175 * Kill the leaf entry by marking it stale.
1176 * Log the leaf block changes.
1178 be16_add_cpu(&leaf
->hdr
.stale
, 1);
1179 xfs_dir2_leaf_log_header(tp
, bp
);
1180 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1181 xfs_dir2_leaf_log_ents(tp
, bp
, index
, index
);
1183 * Make the data entry free. Keep track of the longest freespace
1184 * in the data block in case it changes.
1188 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1189 longest
= be16_to_cpu(hdr
->bestfree
[0].length
);
1190 needlog
= needscan
= 0;
1191 xfs_dir2_data_make_free(tp
, dbp
, off
,
1192 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
1194 * Rescan the data block freespaces for bestfree.
1195 * Log the data block header if needed.
1198 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1200 xfs_dir2_data_log_header(tp
, dbp
);
1201 xfs_dir2_data_check(dp
, dbp
);
1203 * If the longest data block freespace changes, need to update
1204 * the corresponding freeblock entry.
1206 if (longest
< be16_to_cpu(hdr
->bestfree
[0].length
)) {
1207 int error
; /* error return value */
1208 struct xfs_buf
*fbp
; /* freeblock buffer */
1209 xfs_dir2_db_t fdb
; /* freeblock block number */
1210 int findex
; /* index in freeblock entries */
1211 xfs_dir2_free_t
*free
; /* freeblock structure */
1214 * Convert the data block number to a free block,
1215 * read in the free block.
1217 fdb
= xfs_dir2_db_to_fdb(mp
, db
);
1218 error
= xfs_dir2_free_read(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
),
1225 struct xfs_dir3_icfree_hdr freehdr
;
1226 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
1227 ASSERT(freehdr
.firstdb
== xfs_dir3_free_max_bests(mp
) *
1228 (fdb
- XFS_DIR2_FREE_FIRSTDB(mp
)));
1232 * Calculate which entry we need to fix.
1234 findex
= xfs_dir2_db_to_fdindex(mp
, db
);
1235 longest
= be16_to_cpu(hdr
->bestfree
[0].length
);
1237 * If the data block is now empty we can get rid of it
1240 if (longest
== mp
->m_dirblksize
- (uint
)sizeof(*hdr
)) {
1242 * Try to punch out the data block.
1244 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1250 * We can get ENOSPC if there's no space reservation.
1251 * In this case just drop the buffer and some one else
1252 * will eventually get rid of the empty block.
1254 else if (!(error
== ENOSPC
&& args
->total
== 0))
1258 * If we got rid of the data block, we can eliminate that entry
1259 * in the free block.
1261 error
= xfs_dir3_data_block_free(args
, hdr
, free
,
1262 fdb
, findex
, fbp
, longest
);
1267 xfs_dir2_leafn_check(dp
, bp
);
1269 * Return indication of whether this leaf block is empty enough
1270 * to justify trying to join it with a neighbor.
1273 ((uint
)sizeof(leaf
->hdr
) +
1274 (uint
)sizeof(leaf
->ents
[0]) *
1275 (be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
))) <
1281 * Split the leaf entries in the old block into old and new blocks.
1284 xfs_dir2_leafn_split(
1285 xfs_da_state_t
*state
, /* btree cursor */
1286 xfs_da_state_blk_t
*oldblk
, /* original block */
1287 xfs_da_state_blk_t
*newblk
) /* newly created block */
1289 xfs_da_args_t
*args
; /* operation arguments */
1290 xfs_dablk_t blkno
; /* new leaf block number */
1291 int error
; /* error return value */
1292 xfs_mount_t
*mp
; /* filesystem mount point */
1295 * Allocate space for a new leaf node.
1298 mp
= args
->dp
->i_mount
;
1299 ASSERT(args
!= NULL
);
1300 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1301 error
= xfs_da_grow_inode(args
, &blkno
);
1306 * Initialize the new leaf block.
1308 error
= xfs_dir2_leaf_init(args
, xfs_dir2_da_to_db(mp
, blkno
),
1309 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1313 newblk
->blkno
= blkno
;
1314 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1316 * Rebalance the entries across the two leaves, link the new
1317 * block into the leaves.
1319 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1320 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
1325 * Insert the new entry in the correct block.
1328 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1330 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1332 * Update last hashval in each block since we added the name.
1334 oldblk
->hashval
= xfs_dir2_leafn_lasthash(oldblk
->bp
, NULL
);
1335 newblk
->hashval
= xfs_dir2_leafn_lasthash(newblk
->bp
, NULL
);
1336 xfs_dir2_leafn_check(args
->dp
, oldblk
->bp
);
1337 xfs_dir2_leafn_check(args
->dp
, newblk
->bp
);
1342 * Check a leaf block and its neighbors to see if the block should be
1343 * collapsed into one or the other neighbor. Always keep the block
1344 * with the smaller block number.
1345 * If the current block is over 50% full, don't try to join it, return 0.
1346 * If the block is empty, fill in the state structure and return 2.
1347 * If it can be collapsed, fill in the state structure and return 1.
1348 * If nothing can be done, return 0.
1351 xfs_dir2_leafn_toosmall(
1352 xfs_da_state_t
*state
, /* btree cursor */
1353 int *action
) /* resulting action to take */
1355 xfs_da_state_blk_t
*blk
; /* leaf block */
1356 xfs_dablk_t blkno
; /* leaf block number */
1357 struct xfs_buf
*bp
; /* leaf buffer */
1358 int bytes
; /* bytes in use */
1359 int count
; /* leaf live entry count */
1360 int error
; /* error return value */
1361 int forward
; /* sibling block direction */
1362 int i
; /* sibling counter */
1363 xfs_da_blkinfo_t
*info
; /* leaf block header */
1364 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1365 int rval
; /* result from path_shift */
1368 * Check for the degenerate case of the block being over 50% full.
1369 * If so, it's not worth even looking to see if we might be able
1370 * to coalesce with a sibling.
1372 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1373 info
= blk
->bp
->b_addr
;
1374 ASSERT(info
->magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1375 leaf
= (xfs_dir2_leaf_t
*)info
;
1376 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1377 bytes
= (uint
)sizeof(leaf
->hdr
) + count
* (uint
)sizeof(leaf
->ents
[0]);
1378 if (bytes
> (state
->blocksize
>> 1)) {
1380 * Blk over 50%, don't try to join.
1386 * Check for the degenerate case of the block being empty.
1387 * If the block is empty, we'll simply delete it, no need to
1388 * coalesce it with a sibling block. We choose (arbitrarily)
1389 * to merge with the forward block unless it is NULL.
1393 * Make altpath point to the block we want to keep and
1394 * path point to the block we want to drop (this one).
1396 forward
= (info
->forw
!= 0);
1397 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1398 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1402 *action
= rval
? 2 : 0;
1406 * Examine each sibling block to see if we can coalesce with
1407 * at least 25% free space to spare. We need to figure out
1408 * whether to merge with the forward or the backward block.
1409 * We prefer coalescing with the lower numbered sibling so as
1410 * to shrink a directory over time.
1412 forward
= be32_to_cpu(info
->forw
) < be32_to_cpu(info
->back
);
1413 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1414 blkno
= forward
? be32_to_cpu(info
->forw
) : be32_to_cpu(info
->back
);
1418 * Read the sibling leaf block.
1420 error
= xfs_dir2_leafn_read(state
->args
->trans
, state
->args
->dp
,
1426 * Count bytes in the two blocks combined.
1428 leaf
= (xfs_dir2_leaf_t
*)info
;
1429 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1430 bytes
= state
->blocksize
- (state
->blocksize
>> 2);
1432 ASSERT(leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1433 count
+= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1434 bytes
-= count
* (uint
)sizeof(leaf
->ents
[0]);
1436 * Fits with at least 25% to spare.
1440 xfs_trans_brelse(state
->args
->trans
, bp
);
1443 * Didn't like either block, give up.
1451 * Make altpath point to the block we want to keep (the lower
1452 * numbered block) and path point to the block we want to drop.
1454 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1455 if (blkno
< blk
->blkno
)
1456 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1459 error
= xfs_da_path_shift(state
, &state
->path
, forward
, 0,
1464 *action
= rval
? 0 : 1;
1469 * Move all the leaf entries from drop_blk to save_blk.
1470 * This is done as part of a join operation.
1473 xfs_dir2_leafn_unbalance(
1474 xfs_da_state_t
*state
, /* cursor */
1475 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1476 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1478 xfs_da_args_t
*args
; /* operation arguments */
1479 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1480 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1483 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1484 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1485 drop_leaf
= drop_blk
->bp
->b_addr
;
1486 save_leaf
= save_blk
->bp
->b_addr
;
1487 ASSERT(drop_leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1488 ASSERT(save_leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
));
1490 * If there are any stale leaf entries, take this opportunity
1493 if (drop_leaf
->hdr
.stale
)
1494 xfs_dir2_leaf_compact(args
, drop_blk
->bp
);
1495 if (save_leaf
->hdr
.stale
)
1496 xfs_dir2_leaf_compact(args
, save_blk
->bp
);
1498 * Move the entries from drop to the appropriate end of save.
1500 drop_blk
->hashval
= be32_to_cpu(drop_leaf
->ents
[be16_to_cpu(drop_leaf
->hdr
.count
) - 1].hashval
);
1501 if (xfs_dir2_leafn_order(save_blk
->bp
, drop_blk
->bp
))
1502 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
, 0,
1503 be16_to_cpu(drop_leaf
->hdr
.count
));
1505 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
,
1506 be16_to_cpu(save_leaf
->hdr
.count
), be16_to_cpu(drop_leaf
->hdr
.count
));
1507 save_blk
->hashval
= be32_to_cpu(save_leaf
->ents
[be16_to_cpu(save_leaf
->hdr
.count
) - 1].hashval
);
1508 xfs_dir2_leafn_check(args
->dp
, save_blk
->bp
);
1512 * Top-level node form directory addname routine.
1515 xfs_dir2_node_addname(
1516 xfs_da_args_t
*args
) /* operation arguments */
1518 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1519 int error
; /* error return value */
1520 int rval
; /* sub-return value */
1521 xfs_da_state_t
*state
; /* btree cursor */
1523 trace_xfs_dir2_node_addname(args
);
1526 * Allocate and initialize the state (btree cursor).
1528 state
= xfs_da_state_alloc();
1530 state
->mp
= args
->dp
->i_mount
;
1531 state
->blocksize
= state
->mp
->m_dirblksize
;
1532 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1534 * Look up the name. We're not supposed to find it, but
1535 * this gives us the insertion point.
1537 error
= xfs_da_node_lookup_int(state
, &rval
);
1540 if (rval
!= ENOENT
) {
1544 * Add the data entry to a data block.
1545 * Extravalid is set to a freeblock found by lookup.
1547 rval
= xfs_dir2_node_addname_int(args
,
1548 state
->extravalid
? &state
->extrablk
: NULL
);
1552 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1553 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1555 * Add the new leaf entry.
1557 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1560 * It worked, fix the hash values up the btree.
1562 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
1563 xfs_da_fixhashpath(state
, &state
->path
);
1566 * It didn't work, we need to split the leaf block.
1568 if (args
->total
== 0) {
1569 ASSERT(rval
== ENOSPC
);
1573 * Split the leaf block and insert the new entry.
1575 rval
= xfs_da_split(state
);
1578 xfs_da_state_free(state
);
1583 * Add the data entry for a node-format directory name addition.
1584 * The leaf entry is added in xfs_dir2_leafn_add.
1585 * We may enter with a freespace block that the lookup found.
1587 static int /* error */
1588 xfs_dir2_node_addname_int(
1589 xfs_da_args_t
*args
, /* operation arguments */
1590 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1592 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1593 xfs_dir2_db_t dbno
; /* data block number */
1594 struct xfs_buf
*dbp
; /* data block buffer */
1595 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1596 xfs_inode_t
*dp
; /* incore directory inode */
1597 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1598 int error
; /* error return value */
1599 xfs_dir2_db_t fbno
; /* freespace block number */
1600 struct xfs_buf
*fbp
; /* freespace buffer */
1601 int findex
; /* freespace entry index */
1602 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1603 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1604 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1605 int length
; /* length of the new entry */
1606 int logfree
; /* need to log free entry */
1607 xfs_mount_t
*mp
; /* filesystem mount point */
1608 int needlog
; /* need to log data header */
1609 int needscan
; /* need to rescan data frees */
1610 __be16
*tagp
; /* data entry tag pointer */
1611 xfs_trans_t
*tp
; /* transaction pointer */
1613 struct xfs_dir3_icfree_hdr freehdr
;
1618 length
= xfs_dir2_data_entsize(args
->namelen
);
1620 * If we came in with a freespace block that means that lookup
1621 * found an entry with our hash value. This is the freespace
1622 * block for that data entry.
1627 * Remember initial freespace block number.
1629 ifbno
= fblk
->blkno
;
1631 findex
= fblk
->index
;
1632 bests
= xfs_dir3_free_bests_p(mp
, free
);
1633 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
1636 * This means the free entry showed that the data block had
1637 * space for our entry, so we remembered it.
1638 * Use that data block.
1641 ASSERT(findex
< freehdr
.nvalid
);
1642 ASSERT(be16_to_cpu(bests
[findex
]) != NULLDATAOFF
);
1643 ASSERT(be16_to_cpu(bests
[findex
]) >= length
);
1644 dbno
= freehdr
.firstdb
+ findex
;
1647 * The data block looked at didn't have enough room.
1648 * We'll start at the beginning of the freespace entries.
1655 * Didn't come in with a freespace block, so no data block.
1663 * If we don't have a data block yet, we're going to scan the
1664 * freespace blocks looking for one. Figure out what the
1665 * highest freespace block number is.
1668 xfs_fileoff_t fo
; /* freespace block number */
1670 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
)))
1672 lastfbno
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
);
1676 * While we haven't identified a data block, search the freeblock
1677 * data for a good data block. If we find a null freeblock entry,
1678 * indicating a hole in the data blocks, remember that.
1680 while (dbno
== -1) {
1682 * If we don't have a freeblock in hand, get the next one.
1686 * Happens the first time through unless lookup gave
1687 * us a freespace block to start with.
1690 fbno
= XFS_DIR2_FREE_FIRSTDB(mp
);
1692 * If it's ifbno we already looked at it.
1697 * If it's off the end we're done.
1699 if (fbno
>= lastfbno
)
1702 * Read the block. There can be holes in the
1703 * freespace blocks, so this might not succeed.
1704 * This should be really rare, so there's no reason
1707 error
= xfs_dir2_free_try_read(tp
, dp
,
1708 xfs_dir2_db_to_da(mp
, fbno
),
1718 * Look at the current free entry. Is it good enough?
1720 * The bests initialisation should be where the bufer is read in
1721 * the above branch. But gcc is too stupid to realise that bests
1722 * and the freehdr are actually initialised if they are placed
1723 * there, so we have to do it here to avoid warnings. Blech.
1725 bests
= xfs_dir3_free_bests_p(mp
, free
);
1726 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
1727 if (be16_to_cpu(bests
[findex
]) != NULLDATAOFF
&&
1728 be16_to_cpu(bests
[findex
]) >= length
)
1729 dbno
= freehdr
.firstdb
+ findex
;
1732 * Are we done with the freeblock?
1734 if (++findex
== freehdr
.nvalid
) {
1738 xfs_trans_brelse(tp
, fbp
);
1740 if (fblk
&& fblk
->bp
)
1746 * If we don't have a data block, we need to allocate one and make
1747 * the freespace entries refer to it.
1749 if (unlikely(dbno
== -1)) {
1751 * Not allowed to allocate, return failure.
1753 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) || args
->total
== 0)
1754 return XFS_ERROR(ENOSPC
);
1757 * Allocate and initialize the new data block.
1759 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1760 XFS_DIR2_DATA_SPACE
,
1762 (error
= xfs_dir3_data_init(args
, dbno
, &dbp
))))
1766 * If (somehow) we have a freespace block, get rid of it.
1769 xfs_trans_brelse(tp
, fbp
);
1770 if (fblk
&& fblk
->bp
)
1774 * Get the freespace block corresponding to the data block
1775 * that was just allocated.
1777 fbno
= xfs_dir2_db_to_fdb(mp
, dbno
);
1778 error
= xfs_dir2_free_try_read(tp
, dp
,
1779 xfs_dir2_db_to_da(mp
, fbno
),
1785 * If there wasn't a freespace block, the read will
1786 * return a NULL fbp. Allocate and initialize a new one.
1789 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1794 if (unlikely(xfs_dir2_db_to_fdb(mp
, dbno
) != fbno
)) {
1796 "%s: dir ino %llu needed freesp block %lld for\n"
1797 " data block %lld, got %lld ifbno %llu lastfbno %d",
1798 __func__
, (unsigned long long)dp
->i_ino
,
1799 (long long)xfs_dir2_db_to_fdb(mp
, dbno
),
1800 (long long)dbno
, (long long)fbno
,
1801 (unsigned long long)ifbno
, lastfbno
);
1804 " fblk 0x%p blkno %llu index %d magic 0x%x",
1806 (unsigned long long)fblk
->blkno
,
1810 xfs_alert(mp
, " ... fblk is NULL");
1812 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1813 XFS_ERRLEVEL_LOW
, mp
);
1814 return XFS_ERROR(EFSCORRUPTED
);
1818 * Get a buffer for the new block.
1820 error
= xfs_dir3_free_get_buf(tp
, dp
, fbno
, &fbp
);
1824 bests
= xfs_dir3_free_bests_p(mp
, free
);
1825 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
1828 * Remember the first slot as our empty slot.
1830 freehdr
.firstdb
= (fbno
- XFS_DIR2_FREE_FIRSTDB(mp
)) *
1831 xfs_dir3_free_max_bests(mp
);
1832 free
->hdr
.nvalid
= 0;
1833 free
->hdr
.nused
= 0;
1836 bests
= xfs_dir3_free_bests_p(mp
, free
);
1837 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
1841 * Set the freespace block index from the data block number.
1843 findex
= xfs_dir2_db_to_fdindex(mp
, dbno
);
1845 * If it's after the end of the current entries in the
1846 * freespace block, extend that table.
1848 if (findex
>= freehdr
.nvalid
) {
1849 ASSERT(findex
< xfs_dir3_free_max_bests(mp
));
1850 freehdr
.nvalid
= findex
+ 1;
1852 * Tag new entry so nused will go up.
1854 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1857 * If this entry was for an empty data block
1858 * (this should always be true) then update the header.
1860 if (bests
[findex
] == cpu_to_be16(NULLDATAOFF
)) {
1862 xfs_dir3_free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
1863 xfs_dir2_free_log_header(tp
, fbp
);
1866 * Update the real value in the table.
1867 * We haven't allocated the data entry yet so this will
1871 bests
[findex
] = hdr
->bestfree
[0].length
;
1875 * We had a data block so we don't have to make a new one.
1879 * If just checking, we succeeded.
1881 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
1885 * Read the data block in.
1887 error
= xfs_dir2_data_read(tp
, dp
, xfs_dir2_db_to_da(mp
, dbno
),
1894 ASSERT(be16_to_cpu(hdr
->bestfree
[0].length
) >= length
);
1896 * Point to the existing unused space.
1898 dup
= (xfs_dir2_data_unused_t
*)
1899 ((char *)hdr
+ be16_to_cpu(hdr
->bestfree
[0].offset
));
1900 needscan
= needlog
= 0;
1902 * Mark the first part of the unused space, inuse for us.
1904 xfs_dir2_data_use_free(tp
, dbp
, dup
,
1905 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
), length
,
1906 &needlog
, &needscan
);
1908 * Fill in the new entry and log it.
1910 dep
= (xfs_dir2_data_entry_t
*)dup
;
1911 dep
->inumber
= cpu_to_be64(args
->inumber
);
1912 dep
->namelen
= args
->namelen
;
1913 memcpy(dep
->name
, args
->name
, dep
->namelen
);
1914 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1915 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
1916 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1918 * Rescan the block for bestfree if needed.
1921 xfs_dir2_data_freescan(mp
, hdr
, &needlog
);
1923 * Log the data block header if needed.
1926 xfs_dir2_data_log_header(tp
, dbp
);
1928 * If the freespace entry is now wrong, update it.
1930 bests
= xfs_dir3_free_bests_p(mp
, free
); /* gcc is so stupid */
1931 if (be16_to_cpu(bests
[findex
]) != be16_to_cpu(hdr
->bestfree
[0].length
)) {
1932 bests
[findex
] = hdr
->bestfree
[0].length
;
1936 * Log the freespace entry if needed.
1939 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1941 * Return the data block and offset in args, then drop the data block.
1943 args
->blkno
= (xfs_dablk_t
)dbno
;
1944 args
->index
= be16_to_cpu(*tagp
);
1949 * Lookup an entry in a node-format directory.
1950 * All the real work happens in xfs_da_node_lookup_int.
1951 * The only real output is the inode number of the entry.
1954 xfs_dir2_node_lookup(
1955 xfs_da_args_t
*args
) /* operation arguments */
1957 int error
; /* error return value */
1958 int i
; /* btree level */
1959 int rval
; /* operation return value */
1960 xfs_da_state_t
*state
; /* btree cursor */
1962 trace_xfs_dir2_node_lookup(args
);
1965 * Allocate and initialize the btree cursor.
1967 state
= xfs_da_state_alloc();
1969 state
->mp
= args
->dp
->i_mount
;
1970 state
->blocksize
= state
->mp
->m_dirblksize
;
1971 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1973 * Fill in the path to the entry in the cursor.
1975 error
= xfs_da_node_lookup_int(state
, &rval
);
1978 else if (rval
== ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
1979 /* If a CI match, dup the actual name and return EEXIST */
1980 xfs_dir2_data_entry_t
*dep
;
1982 dep
= (xfs_dir2_data_entry_t
*)
1983 ((char *)state
->extrablk
.bp
->b_addr
+
1984 state
->extrablk
.index
);
1985 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
1988 * Release the btree blocks and leaf block.
1990 for (i
= 0; i
< state
->path
.active
; i
++) {
1991 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1992 state
->path
.blk
[i
].bp
= NULL
;
1995 * Release the data block if we have it.
1997 if (state
->extravalid
&& state
->extrablk
.bp
) {
1998 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
1999 state
->extrablk
.bp
= NULL
;
2001 xfs_da_state_free(state
);
2006 * Remove an entry from a node-format directory.
2009 xfs_dir2_node_removename(
2010 xfs_da_args_t
*args
) /* operation arguments */
2012 xfs_da_state_blk_t
*blk
; /* leaf block */
2013 int error
; /* error return value */
2014 int rval
; /* operation return value */
2015 xfs_da_state_t
*state
; /* btree cursor */
2017 trace_xfs_dir2_node_removename(args
);
2020 * Allocate and initialize the btree cursor.
2022 state
= xfs_da_state_alloc();
2024 state
->mp
= args
->dp
->i_mount
;
2025 state
->blocksize
= state
->mp
->m_dirblksize
;
2026 state
->node_ents
= state
->mp
->m_dir_node_ents
;
2028 * Look up the entry we're deleting, set up the cursor.
2030 error
= xfs_da_node_lookup_int(state
, &rval
);
2034 * Didn't find it, upper layer screwed up.
2036 if (rval
!= EEXIST
) {
2037 xfs_da_state_free(state
);
2040 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2041 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2042 ASSERT(state
->extravalid
);
2044 * Remove the leaf and data entries.
2045 * Extrablk refers to the data block.
2047 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
2048 &state
->extrablk
, &rval
);
2052 * Fix the hash values up the btree.
2054 xfs_da_fixhashpath(state
, &state
->path
);
2056 * If we need to join leaf blocks, do it.
2058 if (rval
&& state
->path
.active
> 1)
2059 error
= xfs_da_join(state
);
2061 * If no errors so far, try conversion to leaf format.
2064 error
= xfs_dir2_node_to_leaf(state
);
2065 xfs_da_state_free(state
);
2070 * Replace an entry's inode number in a node-format directory.
2073 xfs_dir2_node_replace(
2074 xfs_da_args_t
*args
) /* operation arguments */
2076 xfs_da_state_blk_t
*blk
; /* leaf block */
2077 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
2078 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
2079 int error
; /* error return value */
2080 int i
; /* btree level */
2081 xfs_ino_t inum
; /* new inode number */
2082 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
2083 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
2084 int rval
; /* internal return value */
2085 xfs_da_state_t
*state
; /* btree cursor */
2087 trace_xfs_dir2_node_replace(args
);
2090 * Allocate and initialize the btree cursor.
2092 state
= xfs_da_state_alloc();
2094 state
->mp
= args
->dp
->i_mount
;
2095 state
->blocksize
= state
->mp
->m_dirblksize
;
2096 state
->node_ents
= state
->mp
->m_dir_node_ents
;
2097 inum
= args
->inumber
;
2099 * Lookup the entry to change in the btree.
2101 error
= xfs_da_node_lookup_int(state
, &rval
);
2106 * It should be found, since the vnodeops layer has looked it up
2107 * and locked it. But paranoia is good.
2109 if (rval
== EEXIST
) {
2111 * Find the leaf entry.
2113 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2114 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2115 leaf
= blk
->bp
->b_addr
;
2116 lep
= &leaf
->ents
[blk
->index
];
2117 ASSERT(state
->extravalid
);
2119 * Point to the data entry.
2121 hdr
= state
->extrablk
.bp
->b_addr
;
2122 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
));
2123 dep
= (xfs_dir2_data_entry_t
*)
2125 xfs_dir2_dataptr_to_off(state
->mp
, be32_to_cpu(lep
->address
)));
2126 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
2128 * Fill in the new inode number and log the entry.
2130 dep
->inumber
= cpu_to_be64(inum
);
2131 xfs_dir2_data_log_entry(args
->trans
, state
->extrablk
.bp
, dep
);
2135 * Didn't find it, and we're holding a data block. Drop it.
2137 else if (state
->extravalid
) {
2138 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2139 state
->extrablk
.bp
= NULL
;
2142 * Release all the buffers in the cursor.
2144 for (i
= 0; i
< state
->path
.active
; i
++) {
2145 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2146 state
->path
.blk
[i
].bp
= NULL
;
2148 xfs_da_state_free(state
);
2153 * Trim off a trailing empty freespace block.
2154 * Return (in rvalp) 1 if we did it, 0 if not.
2157 xfs_dir2_node_trim_free(
2158 xfs_da_args_t
*args
, /* operation arguments */
2159 xfs_fileoff_t fo
, /* free block number */
2160 int *rvalp
) /* out: did something */
2162 struct xfs_buf
*bp
; /* freespace buffer */
2163 xfs_inode_t
*dp
; /* incore directory inode */
2164 int error
; /* error return code */
2165 xfs_dir2_free_t
*free
; /* freespace structure */
2166 xfs_mount_t
*mp
; /* filesystem mount point */
2167 xfs_trans_t
*tp
; /* transaction pointer */
2168 struct xfs_dir3_icfree_hdr freehdr
;
2174 * Read the freespace block.
2176 error
= xfs_dir2_free_try_read(tp
, dp
, fo
, &bp
);
2180 * There can be holes in freespace. If fo is a hole, there's
2186 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
2189 * If there are used entries, there's nothing to do.
2191 if (freehdr
.nused
> 0) {
2192 xfs_trans_brelse(tp
, bp
);
2197 * Blow the block away.
2200 xfs_dir2_shrink_inode(args
, xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
),
2203 * Can't fail with ENOSPC since that only happens with no
2204 * space reservation, when breaking up an extent into two
2205 * pieces. This is the last block of an extent.
2207 ASSERT(error
!= ENOSPC
);
2208 xfs_trans_brelse(tp
, bp
);
2212 * Return that we succeeded.