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
19 #include "libxfs_priv.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_da_format.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_inode.h"
30 #include "xfs_dir2_priv.h"
31 #include "xfs_trace.h"
32 #include "xfs_trans.h"
33 #include "xfs_cksum.h"
36 * Function declarations.
38 static int xfs_dir2_leafn_add(struct xfs_buf
*bp
, xfs_da_args_t
*args
,
40 static void xfs_dir2_leafn_rebalance(xfs_da_state_t
*state
,
41 xfs_da_state_blk_t
*blk1
,
42 xfs_da_state_blk_t
*blk2
);
43 static int xfs_dir2_leafn_remove(xfs_da_args_t
*args
, struct xfs_buf
*bp
,
44 int index
, xfs_da_state_blk_t
*dblk
,
46 static int xfs_dir2_node_addname_int(xfs_da_args_t
*args
,
47 xfs_da_state_blk_t
*fblk
);
50 * Check internal consistency of a leafn block.
58 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
59 struct xfs_dir3_icleaf_hdr leafhdr
;
61 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
63 if (leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
) {
64 struct xfs_dir3_leaf_hdr
*leaf3
= bp
->b_addr
;
65 if (be64_to_cpu(leaf3
->info
.blkno
) != bp
->b_bn
)
66 return __this_address
;
67 } else if (leafhdr
.magic
!= XFS_DIR2_LEAFN_MAGIC
)
68 return __this_address
;
70 return xfs_dir3_leaf_check_int(dp
->i_mount
, dp
, &leafhdr
, leaf
);
80 fa
= xfs_dir3_leafn_check(dp
, bp
);
83 xfs_corruption_error(__func__
, XFS_ERRLEVEL_LOW
, dp
->i_mount
,
84 bp
->b_addr
, BBTOB(bp
->b_length
), __FILE__
, __LINE__
,
89 #define xfs_dir3_leaf_check(dp, bp)
96 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
97 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
99 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
100 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
102 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_FREE_MAGIC
))
103 return __this_address
;
104 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
105 return __this_address
;
106 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
107 return __this_address
;
108 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
109 return __this_address
;
111 if (hdr
->magic
!= cpu_to_be32(XFS_DIR2_FREE_MAGIC
))
112 return __this_address
;
115 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
121 xfs_dir3_free_read_verify(
124 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
127 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
128 !xfs_buf_verify_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
))
129 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
131 fa
= xfs_dir3_free_verify(bp
);
133 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
138 xfs_dir3_free_write_verify(
141 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
142 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
143 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
146 fa
= xfs_dir3_free_verify(bp
);
148 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
152 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
156 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
158 xfs_buf_update_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
);
161 const struct xfs_buf_ops xfs_dir3_free_buf_ops
= {
162 .name
= "xfs_dir3_free",
163 .verify_read
= xfs_dir3_free_read_verify
,
164 .verify_write
= xfs_dir3_free_write_verify
,
165 .verify_struct
= xfs_dir3_free_verify
,
168 /* Everything ok in the free block header? */
169 static xfs_failaddr_t
170 xfs_dir3_free_header_check(
171 struct xfs_inode
*dp
,
175 struct xfs_mount
*mp
= dp
->i_mount
;
176 unsigned int firstdb
;
179 maxbests
= dp
->d_ops
->free_max_bests(mp
->m_dir_geo
);
180 firstdb
= (xfs_dir2_da_to_db(mp
->m_dir_geo
, fbno
) -
181 xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
183 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
184 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
186 if (be32_to_cpu(hdr3
->firstdb
) != firstdb
)
187 return __this_address
;
188 if (be32_to_cpu(hdr3
->nvalid
) > maxbests
)
189 return __this_address
;
190 if (be32_to_cpu(hdr3
->nvalid
) < be32_to_cpu(hdr3
->nused
))
191 return __this_address
;
193 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
195 if (be32_to_cpu(hdr
->firstdb
) != firstdb
)
196 return __this_address
;
197 if (be32_to_cpu(hdr
->nvalid
) > maxbests
)
198 return __this_address
;
199 if (be32_to_cpu(hdr
->nvalid
) < be32_to_cpu(hdr
->nused
))
200 return __this_address
;
206 __xfs_dir3_free_read(
207 struct xfs_trans
*tp
,
208 struct xfs_inode
*dp
,
210 xfs_daddr_t mappedbno
,
211 struct xfs_buf
**bpp
)
216 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
217 XFS_DATA_FORK
, &xfs_dir3_free_buf_ops
);
221 /* Check things that we can't do in the verifier. */
222 fa
= xfs_dir3_free_header_check(dp
, fbno
, *bpp
);
224 xfs_verifier_error(*bpp
, -EFSCORRUPTED
, fa
);
225 xfs_trans_brelse(tp
, *bpp
);
226 return -EFSCORRUPTED
;
229 /* try read returns without an error or *bpp if it lands in a hole */
231 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_FREE_BUF
);
238 struct xfs_trans
*tp
,
239 struct xfs_inode
*dp
,
241 struct xfs_buf
**bpp
)
243 return __xfs_dir3_free_read(tp
, dp
, fbno
, -1, bpp
);
247 xfs_dir2_free_try_read(
248 struct xfs_trans
*tp
,
249 struct xfs_inode
*dp
,
251 struct xfs_buf
**bpp
)
253 return __xfs_dir3_free_read(tp
, dp
, fbno
, -2, bpp
);
257 xfs_dir3_free_get_buf(
260 struct xfs_buf
**bpp
)
262 struct xfs_trans
*tp
= args
->trans
;
263 struct xfs_inode
*dp
= args
->dp
;
264 struct xfs_mount
*mp
= dp
->i_mount
;
267 struct xfs_dir3_icfree_hdr hdr
;
269 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, fbno
),
270 -1, &bp
, XFS_DATA_FORK
);
274 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_FREE_BUF
);
275 bp
->b_ops
= &xfs_dir3_free_buf_ops
;
278 * Initialize the new block to be empty, and remember
279 * its first slot as our empty slot.
281 memset(bp
->b_addr
, 0, sizeof(struct xfs_dir3_free_hdr
));
282 memset(&hdr
, 0, sizeof(hdr
));
284 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
285 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
287 hdr
.magic
= XFS_DIR3_FREE_MAGIC
;
289 hdr3
->hdr
.blkno
= cpu_to_be64(bp
->b_bn
);
290 hdr3
->hdr
.owner
= cpu_to_be64(dp
->i_ino
);
291 uuid_copy(&hdr3
->hdr
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
293 hdr
.magic
= XFS_DIR2_FREE_MAGIC
;
294 dp
->d_ops
->free_hdr_to_disk(bp
->b_addr
, &hdr
);
300 * Log entries from a freespace block.
303 xfs_dir2_free_log_bests(
304 struct xfs_da_args
*args
,
306 int first
, /* first entry to log */
307 int last
) /* last entry to log */
309 xfs_dir2_free_t
*free
; /* freespace structure */
313 bests
= args
->dp
->d_ops
->free_bests_p(free
);
314 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
315 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
316 xfs_trans_log_buf(args
->trans
, bp
,
317 (uint
)((char *)&bests
[first
] - (char *)free
),
318 (uint
)((char *)&bests
[last
] - (char *)free
+
319 sizeof(bests
[0]) - 1));
323 * Log header from a freespace block.
326 xfs_dir2_free_log_header(
327 struct xfs_da_args
*args
,
331 xfs_dir2_free_t
*free
; /* freespace structure */
334 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
335 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
337 xfs_trans_log_buf(args
->trans
, bp
, 0,
338 args
->dp
->d_ops
->free_hdr_size
- 1);
342 * Convert a leaf-format directory to a node-format directory.
343 * We need to change the magic number of the leaf block, and copy
344 * the freespace table out of the leaf block into its own block.
347 xfs_dir2_leaf_to_node(
348 xfs_da_args_t
*args
, /* operation arguments */
349 struct xfs_buf
*lbp
) /* leaf buffer */
351 xfs_inode_t
*dp
; /* incore directory inode */
352 int error
; /* error return value */
353 struct xfs_buf
*fbp
; /* freespace buffer */
354 xfs_dir2_db_t fdb
; /* freespace block number */
355 xfs_dir2_free_t
*free
; /* freespace structure */
356 __be16
*from
; /* pointer to freespace entry */
357 int i
; /* leaf freespace index */
358 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
359 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
360 int n
; /* count of live freespc ents */
361 xfs_dir2_data_off_t off
; /* freespace entry value */
362 __be16
*to
; /* pointer to freespace entry */
363 xfs_trans_t
*tp
; /* transaction pointer */
364 struct xfs_dir3_icfree_hdr freehdr
;
366 trace_xfs_dir2_leaf_to_node(args
);
371 * Add a freespace block to the directory.
373 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
376 ASSERT(fdb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
378 * Get the buffer for the new freespace block.
380 error
= xfs_dir3_free_get_buf(args
, fdb
, &fbp
);
385 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
387 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
388 if (be32_to_cpu(ltp
->bestcount
) >
389 (uint
)dp
->i_d
.di_size
/ args
->geo
->blksize
)
390 return -EFSCORRUPTED
;
393 * Copy freespace entries from the leaf block to the new block.
394 * Count active entries.
396 from
= xfs_dir2_leaf_bests_p(ltp
);
397 to
= dp
->d_ops
->free_bests_p(free
);
398 for (i
= n
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
399 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
401 *to
= cpu_to_be16(off
);
405 * Now initialize the freespace block header.
408 freehdr
.nvalid
= be32_to_cpu(ltp
->bestcount
);
410 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
411 xfs_dir2_free_log_bests(args
, fbp
, 0, freehdr
.nvalid
- 1);
412 xfs_dir2_free_log_header(args
, fbp
);
415 * Converting the leaf to a leafnode is just a matter of changing the
416 * magic number and the ops. Do the change directly to the buffer as
417 * it's less work (and less code) than decoding the header to host
418 * format and back again.
420 if (leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
))
421 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
423 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
424 lbp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
425 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAFN_BUF
);
426 xfs_dir3_leaf_log_header(args
, lbp
);
427 xfs_dir3_leaf_check(dp
, lbp
);
432 * Add a leaf entry to a leaf block in a node-form directory.
433 * The other work necessary is done from the caller.
435 static int /* error */
437 struct xfs_buf
*bp
, /* leaf buffer */
438 xfs_da_args_t
*args
, /* operation arguments */
439 int index
) /* insertion pt for new entry */
441 int compact
; /* compacting stale leaves */
442 xfs_inode_t
*dp
; /* incore directory inode */
443 int highstale
; /* next stale entry */
444 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
445 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
446 int lfloghigh
; /* high leaf entry logging */
447 int lfloglow
; /* low leaf entry logging */
448 int lowstale
; /* previous stale entry */
449 struct xfs_dir3_icleaf_hdr leafhdr
;
450 struct xfs_dir2_leaf_entry
*ents
;
452 trace_xfs_dir2_leafn_add(args
, index
);
456 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
457 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
460 * Quick check just to make sure we are not going to index
461 * into other peoples memory
464 return -EFSCORRUPTED
;
467 * If there are already the maximum number of leaf entries in
468 * the block, if there are no stale entries it won't fit.
469 * Caller will do a split. If there are stale entries we'll do
473 if (leafhdr
.count
== dp
->d_ops
->leaf_max_ents(args
->geo
)) {
476 compact
= leafhdr
.stale
> 1;
479 ASSERT(index
== 0 || be32_to_cpu(ents
[index
- 1].hashval
) <= args
->hashval
);
480 ASSERT(index
== leafhdr
.count
||
481 be32_to_cpu(ents
[index
].hashval
) >= args
->hashval
);
483 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
487 * Compact out all but one stale leaf entry. Leaves behind
488 * the entry closest to index.
491 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
492 &highstale
, &lfloglow
, &lfloghigh
);
493 else if (leafhdr
.stale
) {
495 * Set impossible logging indices for this case.
497 lfloglow
= leafhdr
.count
;
502 * Insert the new entry, log everything.
504 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
505 highstale
, &lfloglow
, &lfloghigh
);
507 lep
->hashval
= cpu_to_be32(args
->hashval
);
508 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(args
->geo
,
509 args
->blkno
, args
->index
));
511 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
512 xfs_dir3_leaf_log_header(args
, bp
);
513 xfs_dir3_leaf_log_ents(args
, bp
, lfloglow
, lfloghigh
);
514 xfs_dir3_leaf_check(dp
, bp
);
520 xfs_dir2_free_hdr_check(
521 struct xfs_inode
*dp
,
525 struct xfs_dir3_icfree_hdr hdr
;
527 dp
->d_ops
->free_hdr_from_disk(&hdr
, bp
->b_addr
);
529 ASSERT((hdr
.firstdb
%
530 dp
->d_ops
->free_max_bests(dp
->i_mount
->m_dir_geo
)) == 0);
531 ASSERT(hdr
.firstdb
<= db
);
532 ASSERT(db
< hdr
.firstdb
+ hdr
.nvalid
);
535 #define xfs_dir2_free_hdr_check(dp, bp, db)
539 * Return the last hash value in the leaf.
540 * Stale entries are ok.
542 xfs_dahash_t
/* hash value */
543 xfs_dir2_leaf_lasthash(
544 struct xfs_inode
*dp
,
545 struct xfs_buf
*bp
, /* leaf buffer */
546 int *count
) /* count of entries in leaf */
548 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
549 struct xfs_dir2_leaf_entry
*ents
;
550 struct xfs_dir3_icleaf_hdr leafhdr
;
552 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
554 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
555 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
556 leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
557 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
560 *count
= leafhdr
.count
;
564 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
565 return be32_to_cpu(ents
[leafhdr
.count
- 1].hashval
);
569 * Look up a leaf entry for space to add a name in a node-format leaf block.
570 * The extrablk in state is a freespace block.
573 xfs_dir2_leafn_lookup_for_addname(
574 struct xfs_buf
*bp
, /* leaf buffer */
575 xfs_da_args_t
*args
, /* operation arguments */
576 int *indexp
, /* out: leaf entry index */
577 xfs_da_state_t
*state
) /* state to fill in */
579 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
580 xfs_dir2_db_t curdb
= -1; /* current data block number */
581 xfs_dir2_db_t curfdb
= -1; /* current free block number */
582 xfs_inode_t
*dp
; /* incore directory inode */
583 int error
; /* error return value */
584 int fi
; /* free entry index */
585 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
586 int index
; /* leaf entry index */
587 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
588 int length
; /* length of new data entry */
589 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
590 xfs_mount_t
*mp
; /* filesystem mount point */
591 xfs_dir2_db_t newdb
; /* new data block number */
592 xfs_dir2_db_t newfdb
; /* new free block number */
593 xfs_trans_t
*tp
; /* transaction pointer */
594 struct xfs_dir2_leaf_entry
*ents
;
595 struct xfs_dir3_icleaf_hdr leafhdr
;
601 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
602 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
604 xfs_dir3_leaf_check(dp
, bp
);
605 ASSERT(leafhdr
.count
> 0);
608 * Look up the hash value in the leaf entries.
610 index
= xfs_dir2_leaf_search_hash(args
, bp
);
612 * Do we have a buffer coming in?
614 if (state
->extravalid
) {
615 /* If so, it's a free block buffer, get the block number. */
616 curbp
= state
->extrablk
.bp
;
617 curfdb
= state
->extrablk
.blkno
;
618 free
= curbp
->b_addr
;
619 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
620 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
622 length
= dp
->d_ops
->data_entsize(args
->namelen
);
624 * Loop over leaf entries with the right hash value.
626 for (lep
= &ents
[index
];
627 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
630 * Skip stale leaf entries.
632 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
635 * Pull the data block number from the entry.
637 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
638 be32_to_cpu(lep
->address
));
640 * For addname, we're looking for a place to put the new entry.
641 * We want to use a data block with an entry of equal
642 * hash value to ours if there is one with room.
644 * If this block isn't the data block we already have
645 * in hand, take a look at it.
647 if (newdb
!= curdb
) {
652 * Convert the data block to the free block
653 * holding its freespace information.
655 newfdb
= dp
->d_ops
->db_to_fdb(args
->geo
, newdb
);
657 * If it's not the one we have in hand, read it in.
659 if (newfdb
!= curfdb
) {
661 * If we had one before, drop it.
664 xfs_trans_brelse(tp
, curbp
);
666 error
= xfs_dir2_free_read(tp
, dp
,
667 xfs_dir2_db_to_da(args
->geo
,
672 free
= curbp
->b_addr
;
674 xfs_dir2_free_hdr_check(dp
, curbp
, curdb
);
677 * Get the index for our entry.
679 fi
= dp
->d_ops
->db_to_fdindex(args
->geo
, curdb
);
681 * If it has room, return it.
683 bests
= dp
->d_ops
->free_bests_p(free
);
684 if (unlikely(bests
[fi
] == cpu_to_be16(NULLDATAOFF
))) {
685 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
686 XFS_ERRLEVEL_LOW
, mp
);
687 if (curfdb
!= newfdb
)
688 xfs_trans_brelse(tp
, curbp
);
689 return -EFSCORRUPTED
;
692 if (be16_to_cpu(bests
[fi
]) >= length
)
696 /* Didn't find any space */
699 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
701 /* Giving back a free block. */
702 state
->extravalid
= 1;
703 state
->extrablk
.bp
= curbp
;
704 state
->extrablk
.index
= fi
;
705 state
->extrablk
.blkno
= curfdb
;
708 * Important: this magic number is not in the buffer - it's for
709 * buffer type information and therefore only the free/data type
710 * matters here, not whether CRCs are enabled or not.
712 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
714 state
->extravalid
= 0;
717 * Return the index, that will be the insertion point.
724 * Look up a leaf entry in a node-format leaf block.
725 * The extrablk in state a data block.
728 xfs_dir2_leafn_lookup_for_entry(
729 struct xfs_buf
*bp
, /* leaf buffer */
730 xfs_da_args_t
*args
, /* operation arguments */
731 int *indexp
, /* out: leaf entry index */
732 xfs_da_state_t
*state
) /* state to fill in */
734 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
735 xfs_dir2_db_t curdb
= -1; /* current data block number */
736 xfs_dir2_data_entry_t
*dep
; /* data block entry */
737 xfs_inode_t
*dp
; /* incore directory inode */
738 int error
; /* error return value */
739 int index
; /* leaf entry index */
740 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
741 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
742 xfs_mount_t
*mp
; /* filesystem mount point */
743 xfs_dir2_db_t newdb
; /* new data block number */
744 xfs_trans_t
*tp
; /* transaction pointer */
745 enum xfs_dacmp cmp
; /* comparison result */
746 struct xfs_dir2_leaf_entry
*ents
;
747 struct xfs_dir3_icleaf_hdr leafhdr
;
753 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
754 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
756 xfs_dir3_leaf_check(dp
, bp
);
757 ASSERT(leafhdr
.count
> 0);
760 * Look up the hash value in the leaf entries.
762 index
= xfs_dir2_leaf_search_hash(args
, bp
);
764 * Do we have a buffer coming in?
766 if (state
->extravalid
) {
767 curbp
= state
->extrablk
.bp
;
768 curdb
= state
->extrablk
.blkno
;
771 * Loop over leaf entries with the right hash value.
773 for (lep
= &ents
[index
];
774 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
777 * Skip stale leaf entries.
779 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
782 * Pull the data block number from the entry.
784 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
785 be32_to_cpu(lep
->address
));
787 * Not adding a new entry, so we really want to find
788 * the name given to us.
790 * If it's a different data block, go get it.
792 if (newdb
!= curdb
) {
794 * If we had a block before that we aren't saving
795 * for a CI name, drop it
797 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
798 curdb
!= state
->extrablk
.blkno
))
799 xfs_trans_brelse(tp
, curbp
);
801 * If needing the block that is saved with a CI match,
802 * use it otherwise read in the new data block.
804 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
805 newdb
== state
->extrablk
.blkno
) {
806 ASSERT(state
->extravalid
);
807 curbp
= state
->extrablk
.bp
;
809 error
= xfs_dir3_data_read(tp
, dp
,
810 xfs_dir2_db_to_da(args
->geo
,
816 xfs_dir3_data_check(dp
, curbp
);
820 * Point to the data entry.
822 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
823 xfs_dir2_dataptr_to_off(args
->geo
,
824 be32_to_cpu(lep
->address
)));
826 * Compare the entry and if it's an exact match, return
827 * EEXIST immediately. If it's the first case-insensitive
828 * match, store the block & inode number and continue looking.
830 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
831 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
832 /* If there is a CI match block, drop it */
833 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
834 curdb
!= state
->extrablk
.blkno
)
835 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
836 args
->cmpresult
= cmp
;
837 args
->inumber
= be64_to_cpu(dep
->inumber
);
838 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
840 state
->extravalid
= 1;
841 state
->extrablk
.bp
= curbp
;
842 state
->extrablk
.blkno
= curdb
;
843 state
->extrablk
.index
= (int)((char *)dep
-
844 (char *)curbp
->b_addr
);
845 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
846 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
847 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
848 if (cmp
== XFS_CMP_EXACT
)
852 ASSERT(index
== leafhdr
.count
|| (args
->op_flags
& XFS_DA_OP_OKNOENT
));
854 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
855 /* Giving back last used data block. */
856 state
->extravalid
= 1;
857 state
->extrablk
.bp
= curbp
;
858 state
->extrablk
.index
= -1;
859 state
->extrablk
.blkno
= curdb
;
860 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
861 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
862 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
864 /* If the curbp is not the CI match block, drop it */
865 if (state
->extrablk
.bp
!= curbp
)
866 xfs_trans_brelse(tp
, curbp
);
869 state
->extravalid
= 0;
876 * Look up a leaf entry in a node-format leaf block.
877 * If this is an addname then the extrablk in state is a freespace block,
878 * otherwise it's a data block.
881 xfs_dir2_leafn_lookup_int(
882 struct xfs_buf
*bp
, /* leaf buffer */
883 xfs_da_args_t
*args
, /* operation arguments */
884 int *indexp
, /* out: leaf entry index */
885 xfs_da_state_t
*state
) /* state to fill in */
887 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
888 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
890 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
894 * Move count leaf entries from source to destination leaf.
895 * Log entries and headers. Stale entries are preserved.
898 xfs_dir3_leafn_moveents(
899 xfs_da_args_t
*args
, /* operation arguments */
900 struct xfs_buf
*bp_s
, /* source */
901 struct xfs_dir3_icleaf_hdr
*shdr
,
902 struct xfs_dir2_leaf_entry
*sents
,
903 int start_s
,/* source leaf index */
904 struct xfs_buf
*bp_d
, /* destination */
905 struct xfs_dir3_icleaf_hdr
*dhdr
,
906 struct xfs_dir2_leaf_entry
*dents
,
907 int start_d
,/* destination leaf index */
908 int count
) /* count of leaves to copy */
910 int stale
; /* count stale leaves copied */
912 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
915 * Silently return if nothing to do.
921 * If the destination index is not the end of the current
922 * destination leaf entries, open up a hole in the destination
923 * to hold the new entries.
925 if (start_d
< dhdr
->count
) {
926 memmove(&dents
[start_d
+ count
], &dents
[start_d
],
927 (dhdr
->count
- start_d
) * sizeof(xfs_dir2_leaf_entry_t
));
928 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
+ count
,
929 count
+ dhdr
->count
- 1);
932 * If the source has stale leaves, count the ones in the copy range
933 * so we can update the header correctly.
936 int i
; /* temp leaf index */
938 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
939 if (sents
[i
].address
==
940 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
946 * Copy the leaf entries from source to destination.
948 memcpy(&dents
[start_d
], &sents
[start_s
],
949 count
* sizeof(xfs_dir2_leaf_entry_t
));
950 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
, start_d
+ count
- 1);
953 * If there are source entries after the ones we copied,
954 * delete the ones we copied by sliding the next ones down.
956 if (start_s
+ count
< shdr
->count
) {
957 memmove(&sents
[start_s
], &sents
[start_s
+ count
],
958 count
* sizeof(xfs_dir2_leaf_entry_t
));
959 xfs_dir3_leaf_log_ents(args
, bp_s
, start_s
, start_s
+ count
- 1);
963 * Update the headers and log them.
965 shdr
->count
-= count
;
966 shdr
->stale
-= stale
;
967 dhdr
->count
+= count
;
968 dhdr
->stale
+= stale
;
972 * Determine the sort order of two leaf blocks.
973 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
976 xfs_dir2_leafn_order(
977 struct xfs_inode
*dp
,
978 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
979 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
981 struct xfs_dir2_leaf
*leaf1
= leaf1_bp
->b_addr
;
982 struct xfs_dir2_leaf
*leaf2
= leaf2_bp
->b_addr
;
983 struct xfs_dir2_leaf_entry
*ents1
;
984 struct xfs_dir2_leaf_entry
*ents2
;
985 struct xfs_dir3_icleaf_hdr hdr1
;
986 struct xfs_dir3_icleaf_hdr hdr2
;
988 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
989 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
990 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
991 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
993 if (hdr1
.count
> 0 && hdr2
.count
> 0 &&
994 (be32_to_cpu(ents2
[0].hashval
) < be32_to_cpu(ents1
[0].hashval
) ||
995 be32_to_cpu(ents2
[hdr2
.count
- 1].hashval
) <
996 be32_to_cpu(ents1
[hdr1
.count
- 1].hashval
)))
1002 * Rebalance leaf entries between two leaf blocks.
1003 * This is actually only called when the second block is new,
1004 * though the code deals with the general case.
1005 * A new entry will be inserted in one of the blocks, and that
1006 * entry is taken into account when balancing.
1009 xfs_dir2_leafn_rebalance(
1010 xfs_da_state_t
*state
, /* btree cursor */
1011 xfs_da_state_blk_t
*blk1
, /* first btree block */
1012 xfs_da_state_blk_t
*blk2
) /* second btree block */
1014 xfs_da_args_t
*args
; /* operation arguments */
1015 int count
; /* count (& direction) leaves */
1016 int isleft
; /* new goes in left leaf */
1017 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
1018 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
1019 int mid
; /* midpoint leaf index */
1020 #if defined(DEBUG) || defined(XFS_WARN)
1021 int oldstale
; /* old count of stale leaves */
1023 int oldsum
; /* old total leaf count */
1024 int swap
; /* swapped leaf blocks */
1025 struct xfs_dir2_leaf_entry
*ents1
;
1026 struct xfs_dir2_leaf_entry
*ents2
;
1027 struct xfs_dir3_icleaf_hdr hdr1
;
1028 struct xfs_dir3_icleaf_hdr hdr2
;
1029 struct xfs_inode
*dp
= state
->args
->dp
;
1033 * If the block order is wrong, swap the arguments.
1035 if ((swap
= xfs_dir2_leafn_order(dp
, blk1
->bp
, blk2
->bp
))) {
1036 xfs_da_state_blk_t
*tmp
; /* temp for block swap */
1042 leaf1
= blk1
->bp
->b_addr
;
1043 leaf2
= blk2
->bp
->b_addr
;
1044 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
1045 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
1046 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
1047 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
1049 oldsum
= hdr1
.count
+ hdr2
.count
;
1050 #if defined(DEBUG) || defined(XFS_WARN)
1051 oldstale
= hdr1
.stale
+ hdr2
.stale
;
1056 * If the old leaf count was odd then the new one will be even,
1057 * so we need to divide the new count evenly.
1060 xfs_dahash_t midhash
; /* middle entry hash value */
1062 if (mid
>= hdr1
.count
)
1063 midhash
= be32_to_cpu(ents2
[mid
- hdr1
.count
].hashval
);
1065 midhash
= be32_to_cpu(ents1
[mid
].hashval
);
1066 isleft
= args
->hashval
<= midhash
;
1069 * If the old count is even then the new count is odd, so there's
1070 * no preferred side for the new entry.
1071 * Pick the left one.
1076 * Calculate moved entry count. Positive means left-to-right,
1077 * negative means right-to-left. Then move the entries.
1079 count
= hdr1
.count
- mid
+ (isleft
== 0);
1081 xfs_dir3_leafn_moveents(args
, blk1
->bp
, &hdr1
, ents1
,
1082 hdr1
.count
- count
, blk2
->bp
,
1083 &hdr2
, ents2
, 0, count
);
1085 xfs_dir3_leafn_moveents(args
, blk2
->bp
, &hdr2
, ents2
, 0,
1086 blk1
->bp
, &hdr1
, ents1
,
1089 ASSERT(hdr1
.count
+ hdr2
.count
== oldsum
);
1090 ASSERT(hdr1
.stale
+ hdr2
.stale
== oldstale
);
1092 /* log the changes made when moving the entries */
1093 dp
->d_ops
->leaf_hdr_to_disk(leaf1
, &hdr1
);
1094 dp
->d_ops
->leaf_hdr_to_disk(leaf2
, &hdr2
);
1095 xfs_dir3_leaf_log_header(args
, blk1
->bp
);
1096 xfs_dir3_leaf_log_header(args
, blk2
->bp
);
1098 xfs_dir3_leaf_check(dp
, blk1
->bp
);
1099 xfs_dir3_leaf_check(dp
, blk2
->bp
);
1102 * Mark whether we're inserting into the old or new leaf.
1104 if (hdr1
.count
< hdr2
.count
)
1105 state
->inleaf
= swap
;
1106 else if (hdr1
.count
> hdr2
.count
)
1107 state
->inleaf
= !swap
;
1109 state
->inleaf
= swap
^ (blk1
->index
<= hdr1
.count
);
1111 * Adjust the expected index for insertion.
1114 blk2
->index
= blk1
->index
- hdr1
.count
;
1117 * Finally sanity check just to make sure we are not returning a
1120 if (blk2
->index
< 0) {
1123 xfs_alert(dp
->i_mount
,
1124 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1125 __func__
, blk1
->index
);
1130 xfs_dir3_data_block_free(
1131 xfs_da_args_t
*args
,
1132 struct xfs_dir2_data_hdr
*hdr
,
1133 struct xfs_dir2_free
*free
,
1136 struct xfs_buf
*fbp
,
1141 struct xfs_dir3_icfree_hdr freehdr
;
1142 struct xfs_inode
*dp
= args
->dp
;
1144 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1145 bests
= dp
->d_ops
->free_bests_p(free
);
1148 * Data block is not empty, just set the free entry to the new
1151 bests
[findex
] = cpu_to_be16(longest
);
1152 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1156 /* One less used entry in the free table. */
1160 * If this was the last entry in the table, we can trim the table size
1161 * back. There might be other entries at the end referring to
1162 * non-existent data blocks, get those too.
1164 if (findex
== freehdr
.nvalid
- 1) {
1165 int i
; /* free entry index */
1167 for (i
= findex
- 1; i
>= 0; i
--) {
1168 if (bests
[i
] != cpu_to_be16(NULLDATAOFF
))
1171 freehdr
.nvalid
= i
+ 1;
1174 /* Not the last entry, just punch it out. */
1175 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1179 dp
->d_ops
->free_hdr_to_disk(free
, &freehdr
);
1180 xfs_dir2_free_log_header(args
, fbp
);
1183 * If there are no useful entries left in the block, get rid of the
1186 if (!freehdr
.nused
) {
1189 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1193 } else if (error
!= -ENOSPC
|| args
->total
!= 0)
1196 * It's possible to get ENOSPC if there is no
1197 * space reservation. In this case some one
1198 * else will eventually get rid of this block.
1202 /* Log the free entry that changed, unless we got rid of it. */
1204 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1209 * Remove an entry from a node directory.
1210 * This removes the leaf entry and the data entry,
1211 * and updates the free block if necessary.
1213 static int /* error */
1214 xfs_dir2_leafn_remove(
1215 xfs_da_args_t
*args
, /* operation arguments */
1216 struct xfs_buf
*bp
, /* leaf buffer */
1217 int index
, /* leaf entry index */
1218 xfs_da_state_blk_t
*dblk
, /* data block */
1219 int *rval
) /* resulting block needs join */
1221 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1222 xfs_dir2_db_t db
; /* data block number */
1223 struct xfs_buf
*dbp
; /* data block buffer */
1224 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1225 xfs_inode_t
*dp
; /* incore directory inode */
1226 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1227 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1228 int longest
; /* longest data free entry */
1229 int off
; /* data block entry offset */
1230 int needlog
; /* need to log data header */
1231 int needscan
; /* need to rescan data frees */
1232 xfs_trans_t
*tp
; /* transaction pointer */
1233 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1234 struct xfs_dir3_icleaf_hdr leafhdr
;
1235 struct xfs_dir2_leaf_entry
*ents
;
1237 trace_xfs_dir2_leafn_remove(args
, index
);
1242 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1243 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1246 * Point to the entry we're removing.
1251 * Extract the data block and offset from the entry.
1253 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1254 ASSERT(dblk
->blkno
== db
);
1255 off
= xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
));
1256 ASSERT(dblk
->index
== off
);
1259 * Kill the leaf entry by marking it stale.
1260 * Log the leaf block changes.
1263 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1264 xfs_dir3_leaf_log_header(args
, bp
);
1266 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1267 xfs_dir3_leaf_log_ents(args
, bp
, index
, index
);
1270 * Make the data entry free. Keep track of the longest freespace
1271 * in the data block in case it changes.
1275 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1276 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1277 longest
= be16_to_cpu(bf
[0].length
);
1278 needlog
= needscan
= 0;
1279 xfs_dir2_data_make_free(args
, dbp
, off
,
1280 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1282 * Rescan the data block freespaces for bestfree.
1283 * Log the data block header if needed.
1286 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1288 xfs_dir2_data_log_header(args
, dbp
);
1289 xfs_dir3_data_check(dp
, dbp
);
1291 * If the longest data block freespace changes, need to update
1292 * the corresponding freeblock entry.
1294 if (longest
< be16_to_cpu(bf
[0].length
)) {
1295 int error
; /* error return value */
1296 struct xfs_buf
*fbp
; /* freeblock buffer */
1297 xfs_dir2_db_t fdb
; /* freeblock block number */
1298 int findex
; /* index in freeblock entries */
1299 xfs_dir2_free_t
*free
; /* freeblock structure */
1302 * Convert the data block number to a free block,
1303 * read in the free block.
1305 fdb
= dp
->d_ops
->db_to_fdb(args
->geo
, db
);
1306 error
= xfs_dir2_free_read(tp
, dp
,
1307 xfs_dir2_db_to_da(args
->geo
, fdb
),
1314 struct xfs_dir3_icfree_hdr freehdr
;
1315 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1316 ASSERT(freehdr
.firstdb
== dp
->d_ops
->free_max_bests(args
->geo
) *
1317 (fdb
- xfs_dir2_byte_to_db(args
->geo
,
1318 XFS_DIR2_FREE_OFFSET
)));
1322 * Calculate which entry we need to fix.
1324 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, db
);
1325 longest
= be16_to_cpu(bf
[0].length
);
1327 * If the data block is now empty we can get rid of it
1330 if (longest
== args
->geo
->blksize
-
1331 dp
->d_ops
->data_entry_offset
) {
1333 * Try to punch out the data block.
1335 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1341 * We can get ENOSPC if there's no space reservation.
1342 * In this case just drop the buffer and some one else
1343 * will eventually get rid of the empty block.
1345 else if (!(error
== -ENOSPC
&& args
->total
== 0))
1349 * If we got rid of the data block, we can eliminate that entry
1350 * in the free block.
1352 error
= xfs_dir3_data_block_free(args
, hdr
, free
,
1353 fdb
, findex
, fbp
, longest
);
1358 xfs_dir3_leaf_check(dp
, bp
);
1360 * Return indication of whether this leaf block is empty enough
1361 * to justify trying to join it with a neighbor.
1363 *rval
= (dp
->d_ops
->leaf_hdr_size
+
1364 (uint
)sizeof(ents
[0]) * (leafhdr
.count
- leafhdr
.stale
)) <
1365 args
->geo
->magicpct
;
1370 * Split the leaf entries in the old block into old and new blocks.
1373 xfs_dir2_leafn_split(
1374 xfs_da_state_t
*state
, /* btree cursor */
1375 xfs_da_state_blk_t
*oldblk
, /* original block */
1376 xfs_da_state_blk_t
*newblk
) /* newly created block */
1378 xfs_da_args_t
*args
; /* operation arguments */
1379 xfs_dablk_t blkno
; /* new leaf block number */
1380 int error
; /* error return value */
1381 struct xfs_inode
*dp
;
1384 * Allocate space for a new leaf node.
1388 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1389 error
= xfs_da_grow_inode(args
, &blkno
);
1394 * Initialize the new leaf block.
1396 error
= xfs_dir3_leaf_get_buf(args
, xfs_dir2_da_to_db(args
->geo
, blkno
),
1397 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1401 newblk
->blkno
= blkno
;
1402 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1404 * Rebalance the entries across the two leaves, link the new
1405 * block into the leaves.
1407 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1408 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1413 * Insert the new entry in the correct block.
1416 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1418 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1420 * Update last hashval in each block since we added the name.
1422 oldblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, oldblk
->bp
, NULL
);
1423 newblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, newblk
->bp
, NULL
);
1424 xfs_dir3_leaf_check(dp
, oldblk
->bp
);
1425 xfs_dir3_leaf_check(dp
, newblk
->bp
);
1430 * Check a leaf block and its neighbors to see if the block should be
1431 * collapsed into one or the other neighbor. Always keep the block
1432 * with the smaller block number.
1433 * If the current block is over 50% full, don't try to join it, return 0.
1434 * If the block is empty, fill in the state structure and return 2.
1435 * If it can be collapsed, fill in the state structure and return 1.
1436 * If nothing can be done, return 0.
1439 xfs_dir2_leafn_toosmall(
1440 xfs_da_state_t
*state
, /* btree cursor */
1441 int *action
) /* resulting action to take */
1443 xfs_da_state_blk_t
*blk
; /* leaf block */
1444 xfs_dablk_t blkno
; /* leaf block number */
1445 struct xfs_buf
*bp
; /* leaf buffer */
1446 int bytes
; /* bytes in use */
1447 int count
; /* leaf live entry count */
1448 int error
; /* error return value */
1449 int forward
; /* sibling block direction */
1450 int i
; /* sibling counter */
1451 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1452 int rval
; /* result from path_shift */
1453 struct xfs_dir3_icleaf_hdr leafhdr
;
1454 struct xfs_dir2_leaf_entry
*ents
;
1455 struct xfs_inode
*dp
= state
->args
->dp
;
1458 * Check for the degenerate case of the block being over 50% full.
1459 * If so, it's not worth even looking to see if we might be able
1460 * to coalesce with a sibling.
1462 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1463 leaf
= blk
->bp
->b_addr
;
1464 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1465 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1466 xfs_dir3_leaf_check(dp
, blk
->bp
);
1468 count
= leafhdr
.count
- leafhdr
.stale
;
1469 bytes
= dp
->d_ops
->leaf_hdr_size
+ count
* sizeof(ents
[0]);
1470 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1472 * Blk over 50%, don't try to join.
1478 * Check for the degenerate case of the block being empty.
1479 * If the block is empty, we'll simply delete it, no need to
1480 * coalesce it with a sibling block. We choose (arbitrarily)
1481 * to merge with the forward block unless it is NULL.
1485 * Make altpath point to the block we want to keep and
1486 * path point to the block we want to drop (this one).
1488 forward
= (leafhdr
.forw
!= 0);
1489 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1490 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1494 *action
= rval
? 2 : 0;
1498 * Examine each sibling block to see if we can coalesce with
1499 * at least 25% free space to spare. We need to figure out
1500 * whether to merge with the forward or the backward block.
1501 * We prefer coalescing with the lower numbered sibling so as
1502 * to shrink a directory over time.
1504 forward
= leafhdr
.forw
< leafhdr
.back
;
1505 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1506 struct xfs_dir3_icleaf_hdr hdr2
;
1508 blkno
= forward
? leafhdr
.forw
: leafhdr
.back
;
1512 * Read the sibling leaf block.
1514 error
= xfs_dir3_leafn_read(state
->args
->trans
, dp
,
1520 * Count bytes in the two blocks combined.
1522 count
= leafhdr
.count
- leafhdr
.stale
;
1523 bytes
= state
->args
->geo
->blksize
-
1524 (state
->args
->geo
->blksize
>> 2);
1527 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf
);
1528 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1529 count
+= hdr2
.count
- hdr2
.stale
;
1530 bytes
-= count
* sizeof(ents
[0]);
1533 * Fits with at least 25% to spare.
1537 xfs_trans_brelse(state
->args
->trans
, bp
);
1540 * Didn't like either block, give up.
1548 * Make altpath point to the block we want to keep (the lower
1549 * numbered block) and path point to the block we want to drop.
1551 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1552 if (blkno
< blk
->blkno
)
1553 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1556 error
= xfs_da3_path_shift(state
, &state
->path
, forward
, 0,
1561 *action
= rval
? 0 : 1;
1566 * Move all the leaf entries from drop_blk to save_blk.
1567 * This is done as part of a join operation.
1570 xfs_dir2_leafn_unbalance(
1571 xfs_da_state_t
*state
, /* cursor */
1572 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1573 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1575 xfs_da_args_t
*args
; /* operation arguments */
1576 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1577 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1578 struct xfs_dir3_icleaf_hdr savehdr
;
1579 struct xfs_dir3_icleaf_hdr drophdr
;
1580 struct xfs_dir2_leaf_entry
*sents
;
1581 struct xfs_dir2_leaf_entry
*dents
;
1582 struct xfs_inode
*dp
= state
->args
->dp
;
1585 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1586 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1587 drop_leaf
= drop_blk
->bp
->b_addr
;
1588 save_leaf
= save_blk
->bp
->b_addr
;
1590 dp
->d_ops
->leaf_hdr_from_disk(&savehdr
, save_leaf
);
1591 dp
->d_ops
->leaf_hdr_from_disk(&drophdr
, drop_leaf
);
1592 sents
= dp
->d_ops
->leaf_ents_p(save_leaf
);
1593 dents
= dp
->d_ops
->leaf_ents_p(drop_leaf
);
1596 * If there are any stale leaf entries, take this opportunity
1600 xfs_dir3_leaf_compact(args
, &drophdr
, drop_blk
->bp
);
1602 xfs_dir3_leaf_compact(args
, &savehdr
, save_blk
->bp
);
1605 * Move the entries from drop to the appropriate end of save.
1607 drop_blk
->hashval
= be32_to_cpu(dents
[drophdr
.count
- 1].hashval
);
1608 if (xfs_dir2_leafn_order(dp
, save_blk
->bp
, drop_blk
->bp
))
1609 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1610 save_blk
->bp
, &savehdr
, sents
, 0,
1613 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1614 save_blk
->bp
, &savehdr
, sents
,
1615 savehdr
.count
, drophdr
.count
);
1616 save_blk
->hashval
= be32_to_cpu(sents
[savehdr
.count
- 1].hashval
);
1618 /* log the changes made when moving the entries */
1619 dp
->d_ops
->leaf_hdr_to_disk(save_leaf
, &savehdr
);
1620 dp
->d_ops
->leaf_hdr_to_disk(drop_leaf
, &drophdr
);
1621 xfs_dir3_leaf_log_header(args
, save_blk
->bp
);
1622 xfs_dir3_leaf_log_header(args
, drop_blk
->bp
);
1624 xfs_dir3_leaf_check(dp
, save_blk
->bp
);
1625 xfs_dir3_leaf_check(dp
, drop_blk
->bp
);
1629 * Top-level node form directory addname routine.
1632 xfs_dir2_node_addname(
1633 xfs_da_args_t
*args
) /* operation arguments */
1635 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1636 int error
; /* error return value */
1637 int rval
; /* sub-return value */
1638 xfs_da_state_t
*state
; /* btree cursor */
1640 trace_xfs_dir2_node_addname(args
);
1643 * Allocate and initialize the state (btree cursor).
1645 state
= xfs_da_state_alloc();
1647 state
->mp
= args
->dp
->i_mount
;
1649 * Look up the name. We're not supposed to find it, but
1650 * this gives us the insertion point.
1652 error
= xfs_da3_node_lookup_int(state
, &rval
);
1655 if (rval
!= -ENOENT
) {
1659 * Add the data entry to a data block.
1660 * Extravalid is set to a freeblock found by lookup.
1662 rval
= xfs_dir2_node_addname_int(args
,
1663 state
->extravalid
? &state
->extrablk
: NULL
);
1667 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1668 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1670 * Add the new leaf entry.
1672 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1675 * It worked, fix the hash values up the btree.
1677 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
1678 xfs_da3_fixhashpath(state
, &state
->path
);
1681 * It didn't work, we need to split the leaf block.
1683 if (args
->total
== 0) {
1684 ASSERT(rval
== -ENOSPC
);
1688 * Split the leaf block and insert the new entry.
1690 rval
= xfs_da3_split(state
);
1693 xfs_da_state_free(state
);
1698 * Add the data entry for a node-format directory name addition.
1699 * The leaf entry is added in xfs_dir2_leafn_add.
1700 * We may enter with a freespace block that the lookup found.
1702 static int /* error */
1703 xfs_dir2_node_addname_int(
1704 xfs_da_args_t
*args
, /* operation arguments */
1705 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1707 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1708 xfs_dir2_db_t dbno
; /* data block number */
1709 struct xfs_buf
*dbp
; /* data block buffer */
1710 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1711 xfs_inode_t
*dp
; /* incore directory inode */
1712 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1713 int error
; /* error return value */
1714 xfs_dir2_db_t fbno
; /* freespace block number */
1715 struct xfs_buf
*fbp
; /* freespace buffer */
1716 int findex
; /* freespace entry index */
1717 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1718 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1719 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1720 int length
; /* length of the new entry */
1721 int logfree
; /* need to log free entry */
1722 xfs_mount_t
*mp
; /* filesystem mount point */
1723 int needlog
; /* need to log data header */
1724 int needscan
; /* need to rescan data frees */
1725 __be16
*tagp
; /* data entry tag pointer */
1726 xfs_trans_t
*tp
; /* transaction pointer */
1728 struct xfs_dir3_icfree_hdr freehdr
;
1729 struct xfs_dir2_data_free
*bf
;
1730 xfs_dir2_data_aoff_t aoff
;
1735 length
= dp
->d_ops
->data_entsize(args
->namelen
);
1737 * If we came in with a freespace block that means that lookup
1738 * found an entry with our hash value. This is the freespace
1739 * block for that data entry.
1744 * Remember initial freespace block number.
1746 ifbno
= fblk
->blkno
;
1748 findex
= fblk
->index
;
1749 bests
= dp
->d_ops
->free_bests_p(free
);
1750 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1753 * This means the free entry showed that the data block had
1754 * space for our entry, so we remembered it.
1755 * Use that data block.
1758 ASSERT(findex
< freehdr
.nvalid
);
1759 ASSERT(be16_to_cpu(bests
[findex
]) != NULLDATAOFF
);
1760 ASSERT(be16_to_cpu(bests
[findex
]) >= length
);
1761 dbno
= freehdr
.firstdb
+ findex
;
1764 * The data block looked at didn't have enough room.
1765 * We'll start at the beginning of the freespace entries.
1772 * Didn't come in with a freespace block, so no data block.
1780 * If we don't have a data block yet, we're going to scan the
1781 * freespace blocks looking for one. Figure out what the
1782 * highest freespace block number is.
1785 xfs_fileoff_t fo
; /* freespace block number */
1787 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
)))
1789 lastfbno
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
);
1793 * While we haven't identified a data block, search the freeblock
1794 * data for a good data block. If we find a null freeblock entry,
1795 * indicating a hole in the data blocks, remember that.
1797 while (dbno
== -1) {
1799 * If we don't have a freeblock in hand, get the next one.
1803 * Happens the first time through unless lookup gave
1804 * us a freespace block to start with.
1807 fbno
= xfs_dir2_byte_to_db(args
->geo
,
1808 XFS_DIR2_FREE_OFFSET
);
1810 * If it's ifbno we already looked at it.
1815 * If it's off the end we're done.
1817 if (fbno
>= lastfbno
)
1820 * Read the block. There can be holes in the
1821 * freespace blocks, so this might not succeed.
1822 * This should be really rare, so there's no reason
1825 error
= xfs_dir2_free_try_read(tp
, dp
,
1826 xfs_dir2_db_to_da(args
->geo
, fbno
),
1836 * Look at the current free entry. Is it good enough?
1838 * The bests initialisation should be where the bufer is read in
1839 * the above branch. But gcc is too stupid to realise that bests
1840 * and the freehdr are actually initialised if they are placed
1841 * there, so we have to do it here to avoid warnings. Blech.
1843 bests
= dp
->d_ops
->free_bests_p(free
);
1844 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1845 if (be16_to_cpu(bests
[findex
]) != NULLDATAOFF
&&
1846 be16_to_cpu(bests
[findex
]) >= length
)
1847 dbno
= freehdr
.firstdb
+ findex
;
1850 * Are we done with the freeblock?
1852 if (++findex
== freehdr
.nvalid
) {
1856 xfs_trans_brelse(tp
, fbp
);
1858 if (fblk
&& fblk
->bp
)
1864 * If we don't have a data block, we need to allocate one and make
1865 * the freespace entries refer to it.
1867 if (unlikely(dbno
== -1)) {
1869 * Not allowed to allocate, return failure.
1871 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) || args
->total
== 0)
1875 * Allocate and initialize the new data block.
1877 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1878 XFS_DIR2_DATA_SPACE
,
1880 (error
= xfs_dir3_data_init(args
, dbno
, &dbp
))))
1884 * If (somehow) we have a freespace block, get rid of it.
1887 xfs_trans_brelse(tp
, fbp
);
1888 if (fblk
&& fblk
->bp
)
1892 * Get the freespace block corresponding to the data block
1893 * that was just allocated.
1895 fbno
= dp
->d_ops
->db_to_fdb(args
->geo
, dbno
);
1896 error
= xfs_dir2_free_try_read(tp
, dp
,
1897 xfs_dir2_db_to_da(args
->geo
, fbno
),
1903 * If there wasn't a freespace block, the read will
1904 * return a NULL fbp. Allocate and initialize a new one.
1907 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1912 if (dp
->d_ops
->db_to_fdb(args
->geo
, dbno
) != fbno
) {
1914 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
1915 __func__
, (unsigned long long)dp
->i_ino
,
1916 (long long)dp
->d_ops
->db_to_fdb(
1918 (long long)dbno
, (long long)fbno
,
1919 (unsigned long long)ifbno
, lastfbno
);
1922 " fblk "PTR_FMT
" blkno %llu index %d magic 0x%x",
1924 (unsigned long long)fblk
->blkno
,
1928 xfs_alert(mp
, " ... fblk is NULL");
1930 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1931 XFS_ERRLEVEL_LOW
, mp
);
1932 return -EFSCORRUPTED
;
1936 * Get a buffer for the new block.
1938 error
= xfs_dir3_free_get_buf(args
, fbno
, &fbp
);
1942 bests
= dp
->d_ops
->free_bests_p(free
);
1943 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1946 * Remember the first slot as our empty slot.
1949 (fbno
- xfs_dir2_byte_to_db(args
->geo
,
1950 XFS_DIR2_FREE_OFFSET
)) *
1951 dp
->d_ops
->free_max_bests(args
->geo
);
1954 bests
= dp
->d_ops
->free_bests_p(free
);
1955 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1959 * Set the freespace block index from the data block number.
1961 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, dbno
);
1963 * If it's after the end of the current entries in the
1964 * freespace block, extend that table.
1966 if (findex
>= freehdr
.nvalid
) {
1967 ASSERT(findex
< dp
->d_ops
->free_max_bests(args
->geo
));
1968 freehdr
.nvalid
= findex
+ 1;
1970 * Tag new entry so nused will go up.
1972 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1975 * If this entry was for an empty data block
1976 * (this should always be true) then update the header.
1978 if (bests
[findex
] == cpu_to_be16(NULLDATAOFF
)) {
1980 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
1981 xfs_dir2_free_log_header(args
, fbp
);
1984 * Update the real value in the table.
1985 * We haven't allocated the data entry yet so this will
1989 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1990 bests
[findex
] = bf
[0].length
;
1994 * We had a data block so we don't have to make a new one.
1998 * If just checking, we succeeded.
2000 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
2004 * Read the data block in.
2006 error
= xfs_dir3_data_read(tp
, dp
,
2007 xfs_dir2_db_to_da(args
->geo
, dbno
),
2012 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
2015 ASSERT(be16_to_cpu(bf
[0].length
) >= length
);
2017 * Point to the existing unused space.
2019 dup
= (xfs_dir2_data_unused_t
*)
2020 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
2021 needscan
= needlog
= 0;
2023 * Mark the first part of the unused space, inuse for us.
2025 aoff
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
2026 error
= xfs_dir2_data_use_free(args
, dbp
, dup
, aoff
, length
,
2027 &needlog
, &needscan
);
2029 xfs_trans_brelse(tp
, dbp
);
2033 * Fill in the new entry and log it.
2035 dep
= (xfs_dir2_data_entry_t
*)dup
;
2036 dep
->inumber
= cpu_to_be64(args
->inumber
);
2037 dep
->namelen
= args
->namelen
;
2038 memcpy(dep
->name
, args
->name
, dep
->namelen
);
2039 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
2040 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
2041 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
2042 xfs_dir2_data_log_entry(args
, dbp
, dep
);
2044 * Rescan the block for bestfree if needed.
2047 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
2049 * Log the data block header if needed.
2052 xfs_dir2_data_log_header(args
, dbp
);
2054 * If the freespace entry is now wrong, update it.
2056 bests
= dp
->d_ops
->free_bests_p(free
); /* gcc is so stupid */
2057 if (be16_to_cpu(bests
[findex
]) != be16_to_cpu(bf
[0].length
)) {
2058 bests
[findex
] = bf
[0].length
;
2062 * Log the freespace entry if needed.
2065 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
2067 * Return the data block and offset in args, then drop the data block.
2069 args
->blkno
= (xfs_dablk_t
)dbno
;
2070 args
->index
= be16_to_cpu(*tagp
);
2075 * Lookup an entry in a node-format directory.
2076 * All the real work happens in xfs_da3_node_lookup_int.
2077 * The only real output is the inode number of the entry.
2080 xfs_dir2_node_lookup(
2081 xfs_da_args_t
*args
) /* operation arguments */
2083 int error
; /* error return value */
2084 int i
; /* btree level */
2085 int rval
; /* operation return value */
2086 xfs_da_state_t
*state
; /* btree cursor */
2088 trace_xfs_dir2_node_lookup(args
);
2091 * Allocate and initialize the btree cursor.
2093 state
= xfs_da_state_alloc();
2095 state
->mp
= args
->dp
->i_mount
;
2097 * Fill in the path to the entry in the cursor.
2099 error
= xfs_da3_node_lookup_int(state
, &rval
);
2102 else if (rval
== -ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
2103 /* If a CI match, dup the actual name and return -EEXIST */
2104 xfs_dir2_data_entry_t
*dep
;
2106 dep
= (xfs_dir2_data_entry_t
*)
2107 ((char *)state
->extrablk
.bp
->b_addr
+
2108 state
->extrablk
.index
);
2109 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
2112 * Release the btree blocks and leaf block.
2114 for (i
= 0; i
< state
->path
.active
; i
++) {
2115 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2116 state
->path
.blk
[i
].bp
= NULL
;
2119 * Release the data block if we have it.
2121 if (state
->extravalid
&& state
->extrablk
.bp
) {
2122 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2123 state
->extrablk
.bp
= NULL
;
2125 xfs_da_state_free(state
);
2130 * Remove an entry from a node-format directory.
2133 xfs_dir2_node_removename(
2134 struct xfs_da_args
*args
) /* operation arguments */
2136 struct xfs_da_state_blk
*blk
; /* leaf block */
2137 int error
; /* error return value */
2138 int rval
; /* operation return value */
2139 struct xfs_da_state
*state
; /* btree cursor */
2141 trace_xfs_dir2_node_removename(args
);
2144 * Allocate and initialize the btree cursor.
2146 state
= xfs_da_state_alloc();
2148 state
->mp
= args
->dp
->i_mount
;
2150 /* Look up the entry we're deleting, set up the cursor. */
2151 error
= xfs_da3_node_lookup_int(state
, &rval
);
2155 /* Didn't find it, upper layer screwed up. */
2156 if (rval
!= -EEXIST
) {
2161 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2162 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2163 ASSERT(state
->extravalid
);
2165 * Remove the leaf and data entries.
2166 * Extrablk refers to the data block.
2168 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
2169 &state
->extrablk
, &rval
);
2173 * Fix the hash values up the btree.
2175 xfs_da3_fixhashpath(state
, &state
->path
);
2177 * If we need to join leaf blocks, do it.
2179 if (rval
&& state
->path
.active
> 1)
2180 error
= xfs_da3_join(state
);
2182 * If no errors so far, try conversion to leaf format.
2185 error
= xfs_dir2_node_to_leaf(state
);
2187 xfs_da_state_free(state
);
2192 * Replace an entry's inode number in a node-format directory.
2195 xfs_dir2_node_replace(
2196 xfs_da_args_t
*args
) /* operation arguments */
2198 xfs_da_state_blk_t
*blk
; /* leaf block */
2199 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
2200 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
2201 int error
; /* error return value */
2202 int i
; /* btree level */
2203 xfs_ino_t inum
; /* new inode number */
2204 int ftype
; /* new file type */
2205 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
2206 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
2207 int rval
; /* internal return value */
2208 xfs_da_state_t
*state
; /* btree cursor */
2210 trace_xfs_dir2_node_replace(args
);
2213 * Allocate and initialize the btree cursor.
2215 state
= xfs_da_state_alloc();
2217 state
->mp
= args
->dp
->i_mount
;
2220 * We have to save new inode number and ftype since
2221 * xfs_da3_node_lookup_int() is going to overwrite them
2223 inum
= args
->inumber
;
2224 ftype
= args
->filetype
;
2227 * Lookup the entry to change in the btree.
2229 error
= xfs_da3_node_lookup_int(state
, &rval
);
2234 * It should be found, since the vnodeops layer has looked it up
2235 * and locked it. But paranoia is good.
2237 if (rval
== -EEXIST
) {
2238 struct xfs_dir2_leaf_entry
*ents
;
2240 * Find the leaf entry.
2242 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2243 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2244 leaf
= blk
->bp
->b_addr
;
2245 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
2246 lep
= &ents
[blk
->index
];
2247 ASSERT(state
->extravalid
);
2249 * Point to the data entry.
2251 hdr
= state
->extrablk
.bp
->b_addr
;
2252 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
2253 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
2254 dep
= (xfs_dir2_data_entry_t
*)
2256 xfs_dir2_dataptr_to_off(args
->geo
,
2257 be32_to_cpu(lep
->address
)));
2258 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
2260 * Fill in the new inode number and log the entry.
2262 dep
->inumber
= cpu_to_be64(inum
);
2263 args
->dp
->d_ops
->data_put_ftype(dep
, ftype
);
2264 xfs_dir2_data_log_entry(args
, state
->extrablk
.bp
, dep
);
2268 * Didn't find it, and we're holding a data block. Drop it.
2270 else if (state
->extravalid
) {
2271 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2272 state
->extrablk
.bp
= NULL
;
2275 * Release all the buffers in the cursor.
2277 for (i
= 0; i
< state
->path
.active
; i
++) {
2278 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2279 state
->path
.blk
[i
].bp
= NULL
;
2281 xfs_da_state_free(state
);
2286 * Trim off a trailing empty freespace block.
2287 * Return (in rvalp) 1 if we did it, 0 if not.
2290 xfs_dir2_node_trim_free(
2291 xfs_da_args_t
*args
, /* operation arguments */
2292 xfs_fileoff_t fo
, /* free block number */
2293 int *rvalp
) /* out: did something */
2295 struct xfs_buf
*bp
; /* freespace buffer */
2296 xfs_inode_t
*dp
; /* incore directory inode */
2297 int error
; /* error return code */
2298 xfs_dir2_free_t
*free
; /* freespace structure */
2299 xfs_trans_t
*tp
; /* transaction pointer */
2300 struct xfs_dir3_icfree_hdr freehdr
;
2308 * Read the freespace block.
2310 error
= xfs_dir2_free_try_read(tp
, dp
, fo
, &bp
);
2314 * There can be holes in freespace. If fo is a hole, there's
2320 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
2323 * If there are used entries, there's nothing to do.
2325 if (freehdr
.nused
> 0) {
2326 xfs_trans_brelse(tp
, bp
);
2330 * Blow the block away.
2332 error
= xfs_dir2_shrink_inode(args
,
2333 xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
), bp
);
2336 * Can't fail with ENOSPC since that only happens with no
2337 * space reservation, when breaking up an extent into two
2338 * pieces. This is the last block of an extent.
2340 ASSERT(error
!= -ENOSPC
);
2341 xfs_trans_brelse(tp
, bp
);
2345 * Return that we succeeded.