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
, __FILE__
, __LINE__
, fa
);
88 #define xfs_dir3_leaf_check(dp, bp)
95 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
96 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
98 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
99 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
101 if (hdr3
->magic
!= cpu_to_be32(XFS_DIR3_FREE_MAGIC
))
102 return __this_address
;
103 if (!uuid_equal(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
))
104 return __this_address
;
105 if (be64_to_cpu(hdr3
->blkno
) != bp
->b_bn
)
106 return __this_address
;
107 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->lsn
)))
108 return __this_address
;
110 if (hdr
->magic
!= cpu_to_be32(XFS_DIR2_FREE_MAGIC
))
111 return __this_address
;
114 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
120 xfs_dir3_free_read_verify(
123 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
126 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
127 !xfs_buf_verify_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
))
128 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
130 fa
= xfs_dir3_free_verify(bp
);
132 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
137 xfs_dir3_free_write_verify(
140 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
141 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
142 struct xfs_dir3_blk_hdr
*hdr3
= bp
->b_addr
;
145 fa
= xfs_dir3_free_verify(bp
);
147 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
151 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
155 hdr3
->lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
157 xfs_buf_update_cksum(bp
, XFS_DIR3_FREE_CRC_OFF
);
160 const struct xfs_buf_ops xfs_dir3_free_buf_ops
= {
161 .name
= "xfs_dir3_free",
162 .verify_read
= xfs_dir3_free_read_verify
,
163 .verify_write
= xfs_dir3_free_write_verify
,
164 .verify_struct
= xfs_dir3_free_verify
,
167 /* Everything ok in the free block header? */
168 static xfs_failaddr_t
169 xfs_dir3_free_header_check(
170 struct xfs_inode
*dp
,
174 struct xfs_mount
*mp
= dp
->i_mount
;
175 unsigned int firstdb
;
178 maxbests
= dp
->d_ops
->free_max_bests(mp
->m_dir_geo
);
179 firstdb
= (xfs_dir2_da_to_db(mp
->m_dir_geo
, fbno
) -
180 xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
182 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
183 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
185 if (be32_to_cpu(hdr3
->firstdb
) != firstdb
)
186 return __this_address
;
187 if (be32_to_cpu(hdr3
->nvalid
) > maxbests
)
188 return __this_address
;
189 if (be32_to_cpu(hdr3
->nvalid
) < be32_to_cpu(hdr3
->nused
))
190 return __this_address
;
192 struct xfs_dir2_free_hdr
*hdr
= bp
->b_addr
;
194 if (be32_to_cpu(hdr
->firstdb
) != firstdb
)
195 return __this_address
;
196 if (be32_to_cpu(hdr
->nvalid
) > maxbests
)
197 return __this_address
;
198 if (be32_to_cpu(hdr
->nvalid
) < be32_to_cpu(hdr
->nused
))
199 return __this_address
;
205 __xfs_dir3_free_read(
206 struct xfs_trans
*tp
,
207 struct xfs_inode
*dp
,
209 xfs_daddr_t mappedbno
,
210 struct xfs_buf
**bpp
)
215 err
= xfs_da_read_buf(tp
, dp
, fbno
, mappedbno
, bpp
,
216 XFS_DATA_FORK
, &xfs_dir3_free_buf_ops
);
220 /* Check things that we can't do in the verifier. */
221 fa
= xfs_dir3_free_header_check(dp
, fbno
, *bpp
);
223 xfs_verifier_error(*bpp
, -EFSCORRUPTED
, fa
);
224 xfs_trans_brelse(tp
, *bpp
);
225 return -EFSCORRUPTED
;
228 /* try read returns without an error or *bpp if it lands in a hole */
230 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_DIR_FREE_BUF
);
237 struct xfs_trans
*tp
,
238 struct xfs_inode
*dp
,
240 struct xfs_buf
**bpp
)
242 return __xfs_dir3_free_read(tp
, dp
, fbno
, -1, bpp
);
246 xfs_dir2_free_try_read(
247 struct xfs_trans
*tp
,
248 struct xfs_inode
*dp
,
250 struct xfs_buf
**bpp
)
252 return __xfs_dir3_free_read(tp
, dp
, fbno
, -2, bpp
);
256 xfs_dir3_free_get_buf(
259 struct xfs_buf
**bpp
)
261 struct xfs_trans
*tp
= args
->trans
;
262 struct xfs_inode
*dp
= args
->dp
;
263 struct xfs_mount
*mp
= dp
->i_mount
;
266 struct xfs_dir3_icfree_hdr hdr
;
268 error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(args
->geo
, fbno
),
269 -1, &bp
, XFS_DATA_FORK
);
273 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_DIR_FREE_BUF
);
274 bp
->b_ops
= &xfs_dir3_free_buf_ops
;
277 * Initialize the new block to be empty, and remember
278 * its first slot as our empty slot.
280 memset(bp
->b_addr
, 0, sizeof(struct xfs_dir3_free_hdr
));
281 memset(&hdr
, 0, sizeof(hdr
));
283 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
284 struct xfs_dir3_free_hdr
*hdr3
= bp
->b_addr
;
286 hdr
.magic
= XFS_DIR3_FREE_MAGIC
;
288 hdr3
->hdr
.blkno
= cpu_to_be64(bp
->b_bn
);
289 hdr3
->hdr
.owner
= cpu_to_be64(dp
->i_ino
);
290 uuid_copy(&hdr3
->hdr
.uuid
, &mp
->m_sb
.sb_meta_uuid
);
292 hdr
.magic
= XFS_DIR2_FREE_MAGIC
;
293 dp
->d_ops
->free_hdr_to_disk(bp
->b_addr
, &hdr
);
299 * Log entries from a freespace block.
302 xfs_dir2_free_log_bests(
303 struct xfs_da_args
*args
,
305 int first
, /* first entry to log */
306 int last
) /* last entry to log */
308 xfs_dir2_free_t
*free
; /* freespace structure */
312 bests
= args
->dp
->d_ops
->free_bests_p(free
);
313 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
314 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
315 xfs_trans_log_buf(args
->trans
, bp
,
316 (uint
)((char *)&bests
[first
] - (char *)free
),
317 (uint
)((char *)&bests
[last
] - (char *)free
+
318 sizeof(bests
[0]) - 1));
322 * Log header from a freespace block.
325 xfs_dir2_free_log_header(
326 struct xfs_da_args
*args
,
330 xfs_dir2_free_t
*free
; /* freespace structure */
333 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
334 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
336 xfs_trans_log_buf(args
->trans
, bp
, 0,
337 args
->dp
->d_ops
->free_hdr_size
- 1);
341 * Convert a leaf-format directory to a node-format directory.
342 * We need to change the magic number of the leaf block, and copy
343 * the freespace table out of the leaf block into its own block.
346 xfs_dir2_leaf_to_node(
347 xfs_da_args_t
*args
, /* operation arguments */
348 struct xfs_buf
*lbp
) /* leaf buffer */
350 xfs_inode_t
*dp
; /* incore directory inode */
351 int error
; /* error return value */
352 struct xfs_buf
*fbp
; /* freespace buffer */
353 xfs_dir2_db_t fdb
; /* freespace block number */
354 xfs_dir2_free_t
*free
; /* freespace structure */
355 __be16
*from
; /* pointer to freespace entry */
356 int i
; /* leaf freespace index */
357 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
358 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
359 int n
; /* count of live freespc ents */
360 xfs_dir2_data_off_t off
; /* freespace entry value */
361 __be16
*to
; /* pointer to freespace entry */
362 xfs_trans_t
*tp
; /* transaction pointer */
363 struct xfs_dir3_icfree_hdr freehdr
;
365 trace_xfs_dir2_leaf_to_node(args
);
370 * Add a freespace block to the directory.
372 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
375 ASSERT(fdb
== xfs_dir2_byte_to_db(args
->geo
, XFS_DIR2_FREE_OFFSET
));
377 * Get the buffer for the new freespace block.
379 error
= xfs_dir3_free_get_buf(args
, fdb
, &fbp
);
384 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
386 ltp
= xfs_dir2_leaf_tail_p(args
->geo
, leaf
);
387 if (be32_to_cpu(ltp
->bestcount
) >
388 (uint
)dp
->i_d
.di_size
/ args
->geo
->blksize
)
389 return -EFSCORRUPTED
;
392 * Copy freespace entries from the leaf block to the new block.
393 * Count active entries.
395 from
= xfs_dir2_leaf_bests_p(ltp
);
396 to
= dp
->d_ops
->free_bests_p(free
);
397 for (i
= n
= 0; i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
398 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
400 *to
= cpu_to_be16(off
);
404 * Now initialize the freespace block header.
407 freehdr
.nvalid
= be32_to_cpu(ltp
->bestcount
);
409 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
410 xfs_dir2_free_log_bests(args
, fbp
, 0, freehdr
.nvalid
- 1);
411 xfs_dir2_free_log_header(args
, fbp
);
414 * Converting the leaf to a leafnode is just a matter of changing the
415 * magic number and the ops. Do the change directly to the buffer as
416 * it's less work (and less code) than decoding the header to host
417 * format and back again.
419 if (leaf
->hdr
.info
.magic
== cpu_to_be16(XFS_DIR2_LEAF1_MAGIC
))
420 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
422 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR3_LEAFN_MAGIC
);
423 lbp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
424 xfs_trans_buf_set_type(tp
, lbp
, XFS_BLFT_DIR_LEAFN_BUF
);
425 xfs_dir3_leaf_log_header(args
, lbp
);
426 xfs_dir3_leaf_check(dp
, lbp
);
431 * Add a leaf entry to a leaf block in a node-form directory.
432 * The other work necessary is done from the caller.
434 static int /* error */
436 struct xfs_buf
*bp
, /* leaf buffer */
437 xfs_da_args_t
*args
, /* operation arguments */
438 int index
) /* insertion pt for new entry */
440 int compact
; /* compacting stale leaves */
441 xfs_inode_t
*dp
; /* incore directory inode */
442 int highstale
; /* next stale entry */
443 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
444 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
445 int lfloghigh
; /* high leaf entry logging */
446 int lfloglow
; /* low leaf entry logging */
447 int lowstale
; /* previous stale entry */
448 struct xfs_dir3_icleaf_hdr leafhdr
;
449 struct xfs_dir2_leaf_entry
*ents
;
451 trace_xfs_dir2_leafn_add(args
, index
);
455 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
456 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
459 * Quick check just to make sure we are not going to index
460 * into other peoples memory
463 return -EFSCORRUPTED
;
466 * If there are already the maximum number of leaf entries in
467 * the block, if there are no stale entries it won't fit.
468 * Caller will do a split. If there are stale entries we'll do
472 if (leafhdr
.count
== dp
->d_ops
->leaf_max_ents(args
->geo
)) {
475 compact
= leafhdr
.stale
> 1;
478 ASSERT(index
== 0 || be32_to_cpu(ents
[index
- 1].hashval
) <= args
->hashval
);
479 ASSERT(index
== leafhdr
.count
||
480 be32_to_cpu(ents
[index
].hashval
) >= args
->hashval
);
482 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
486 * Compact out all but one stale leaf entry. Leaves behind
487 * the entry closest to index.
490 xfs_dir3_leaf_compact_x1(&leafhdr
, ents
, &index
, &lowstale
,
491 &highstale
, &lfloglow
, &lfloghigh
);
492 else if (leafhdr
.stale
) {
494 * Set impossible logging indices for this case.
496 lfloglow
= leafhdr
.count
;
501 * Insert the new entry, log everything.
503 lep
= xfs_dir3_leaf_find_entry(&leafhdr
, ents
, index
, compact
, lowstale
,
504 highstale
, &lfloglow
, &lfloghigh
);
506 lep
->hashval
= cpu_to_be32(args
->hashval
);
507 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(args
->geo
,
508 args
->blkno
, args
->index
));
510 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
511 xfs_dir3_leaf_log_header(args
, bp
);
512 xfs_dir3_leaf_log_ents(args
, bp
, lfloglow
, lfloghigh
);
513 xfs_dir3_leaf_check(dp
, bp
);
519 xfs_dir2_free_hdr_check(
520 struct xfs_inode
*dp
,
524 struct xfs_dir3_icfree_hdr hdr
;
526 dp
->d_ops
->free_hdr_from_disk(&hdr
, bp
->b_addr
);
528 ASSERT((hdr
.firstdb
%
529 dp
->d_ops
->free_max_bests(dp
->i_mount
->m_dir_geo
)) == 0);
530 ASSERT(hdr
.firstdb
<= db
);
531 ASSERT(db
< hdr
.firstdb
+ hdr
.nvalid
);
534 #define xfs_dir2_free_hdr_check(dp, bp, db)
538 * Return the last hash value in the leaf.
539 * Stale entries are ok.
541 xfs_dahash_t
/* hash value */
542 xfs_dir2_leaf_lasthash(
543 struct xfs_inode
*dp
,
544 struct xfs_buf
*bp
, /* leaf buffer */
545 int *count
) /* count of entries in leaf */
547 struct xfs_dir2_leaf
*leaf
= bp
->b_addr
;
548 struct xfs_dir2_leaf_entry
*ents
;
549 struct xfs_dir3_icleaf_hdr leafhdr
;
551 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
553 ASSERT(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
554 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
555 leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
556 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
);
559 *count
= leafhdr
.count
;
563 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
564 return be32_to_cpu(ents
[leafhdr
.count
- 1].hashval
);
568 * Look up a leaf entry for space to add a name in a node-format leaf block.
569 * The extrablk in state is a freespace block.
572 xfs_dir2_leafn_lookup_for_addname(
573 struct xfs_buf
*bp
, /* leaf buffer */
574 xfs_da_args_t
*args
, /* operation arguments */
575 int *indexp
, /* out: leaf entry index */
576 xfs_da_state_t
*state
) /* state to fill in */
578 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
579 xfs_dir2_db_t curdb
= -1; /* current data block number */
580 xfs_dir2_db_t curfdb
= -1; /* current free block number */
581 xfs_inode_t
*dp
; /* incore directory inode */
582 int error
; /* error return value */
583 int fi
; /* free entry index */
584 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
585 int index
; /* leaf entry index */
586 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
587 int length
; /* length of new data entry */
588 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
589 xfs_mount_t
*mp
; /* filesystem mount point */
590 xfs_dir2_db_t newdb
; /* new data block number */
591 xfs_dir2_db_t newfdb
; /* new free block number */
592 xfs_trans_t
*tp
; /* transaction pointer */
593 struct xfs_dir2_leaf_entry
*ents
;
594 struct xfs_dir3_icleaf_hdr leafhdr
;
600 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
601 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
603 xfs_dir3_leaf_check(dp
, bp
);
604 ASSERT(leafhdr
.count
> 0);
607 * Look up the hash value in the leaf entries.
609 index
= xfs_dir2_leaf_search_hash(args
, bp
);
611 * Do we have a buffer coming in?
613 if (state
->extravalid
) {
614 /* If so, it's a free block buffer, get the block number. */
615 curbp
= state
->extrablk
.bp
;
616 curfdb
= state
->extrablk
.blkno
;
617 free
= curbp
->b_addr
;
618 ASSERT(free
->hdr
.magic
== cpu_to_be32(XFS_DIR2_FREE_MAGIC
) ||
619 free
->hdr
.magic
== cpu_to_be32(XFS_DIR3_FREE_MAGIC
));
621 length
= dp
->d_ops
->data_entsize(args
->namelen
);
623 * Loop over leaf entries with the right hash value.
625 for (lep
= &ents
[index
];
626 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
629 * Skip stale leaf entries.
631 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
634 * Pull the data block number from the entry.
636 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
637 be32_to_cpu(lep
->address
));
639 * For addname, we're looking for a place to put the new entry.
640 * We want to use a data block with an entry of equal
641 * hash value to ours if there is one with room.
643 * If this block isn't the data block we already have
644 * in hand, take a look at it.
646 if (newdb
!= curdb
) {
651 * Convert the data block to the free block
652 * holding its freespace information.
654 newfdb
= dp
->d_ops
->db_to_fdb(args
->geo
, newdb
);
656 * If it's not the one we have in hand, read it in.
658 if (newfdb
!= curfdb
) {
660 * If we had one before, drop it.
663 xfs_trans_brelse(tp
, curbp
);
665 error
= xfs_dir2_free_read(tp
, dp
,
666 xfs_dir2_db_to_da(args
->geo
,
671 free
= curbp
->b_addr
;
673 xfs_dir2_free_hdr_check(dp
, curbp
, curdb
);
676 * Get the index for our entry.
678 fi
= dp
->d_ops
->db_to_fdindex(args
->geo
, curdb
);
680 * If it has room, return it.
682 bests
= dp
->d_ops
->free_bests_p(free
);
683 if (unlikely(bests
[fi
] == cpu_to_be16(NULLDATAOFF
))) {
684 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
685 XFS_ERRLEVEL_LOW
, mp
);
686 if (curfdb
!= newfdb
)
687 xfs_trans_brelse(tp
, curbp
);
688 return -EFSCORRUPTED
;
691 if (be16_to_cpu(bests
[fi
]) >= length
)
695 /* Didn't find any space */
698 ASSERT(args
->op_flags
& XFS_DA_OP_OKNOENT
);
700 /* Giving back a free block. */
701 state
->extravalid
= 1;
702 state
->extrablk
.bp
= curbp
;
703 state
->extrablk
.index
= fi
;
704 state
->extrablk
.blkno
= curfdb
;
707 * Important: this magic number is not in the buffer - it's for
708 * buffer type information and therefore only the free/data type
709 * matters here, not whether CRCs are enabled or not.
711 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
713 state
->extravalid
= 0;
716 * Return the index, that will be the insertion point.
723 * Look up a leaf entry in a node-format leaf block.
724 * The extrablk in state a data block.
727 xfs_dir2_leafn_lookup_for_entry(
728 struct xfs_buf
*bp
, /* leaf buffer */
729 xfs_da_args_t
*args
, /* operation arguments */
730 int *indexp
, /* out: leaf entry index */
731 xfs_da_state_t
*state
) /* state to fill in */
733 struct xfs_buf
*curbp
= NULL
; /* current data/free buffer */
734 xfs_dir2_db_t curdb
= -1; /* current data block number */
735 xfs_dir2_data_entry_t
*dep
; /* data block entry */
736 xfs_inode_t
*dp
; /* incore directory inode */
737 int error
; /* error return value */
738 int index
; /* leaf entry index */
739 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
740 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
741 xfs_mount_t
*mp
; /* filesystem mount point */
742 xfs_dir2_db_t newdb
; /* new data block number */
743 xfs_trans_t
*tp
; /* transaction pointer */
744 enum xfs_dacmp cmp
; /* comparison result */
745 struct xfs_dir2_leaf_entry
*ents
;
746 struct xfs_dir3_icleaf_hdr leafhdr
;
752 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
753 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
755 xfs_dir3_leaf_check(dp
, bp
);
756 ASSERT(leafhdr
.count
> 0);
759 * Look up the hash value in the leaf entries.
761 index
= xfs_dir2_leaf_search_hash(args
, bp
);
763 * Do we have a buffer coming in?
765 if (state
->extravalid
) {
766 curbp
= state
->extrablk
.bp
;
767 curdb
= state
->extrablk
.blkno
;
770 * Loop over leaf entries with the right hash value.
772 for (lep
= &ents
[index
];
773 index
< leafhdr
.count
&& be32_to_cpu(lep
->hashval
) == args
->hashval
;
776 * Skip stale leaf entries.
778 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
781 * Pull the data block number from the entry.
783 newdb
= xfs_dir2_dataptr_to_db(args
->geo
,
784 be32_to_cpu(lep
->address
));
786 * Not adding a new entry, so we really want to find
787 * the name given to us.
789 * If it's a different data block, go get it.
791 if (newdb
!= curdb
) {
793 * If we had a block before that we aren't saving
794 * for a CI name, drop it
796 if (curbp
&& (args
->cmpresult
== XFS_CMP_DIFFERENT
||
797 curdb
!= state
->extrablk
.blkno
))
798 xfs_trans_brelse(tp
, curbp
);
800 * If needing the block that is saved with a CI match,
801 * use it otherwise read in the new data block.
803 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
804 newdb
== state
->extrablk
.blkno
) {
805 ASSERT(state
->extravalid
);
806 curbp
= state
->extrablk
.bp
;
808 error
= xfs_dir3_data_read(tp
, dp
,
809 xfs_dir2_db_to_da(args
->geo
,
815 xfs_dir3_data_check(dp
, curbp
);
819 * Point to the data entry.
821 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->b_addr
+
822 xfs_dir2_dataptr_to_off(args
->geo
,
823 be32_to_cpu(lep
->address
)));
825 * Compare the entry and if it's an exact match, return
826 * EEXIST immediately. If it's the first case-insensitive
827 * match, store the block & inode number and continue looking.
829 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
830 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
831 /* If there is a CI match block, drop it */
832 if (args
->cmpresult
!= XFS_CMP_DIFFERENT
&&
833 curdb
!= state
->extrablk
.blkno
)
834 xfs_trans_brelse(tp
, state
->extrablk
.bp
);
835 args
->cmpresult
= cmp
;
836 args
->inumber
= be64_to_cpu(dep
->inumber
);
837 args
->filetype
= dp
->d_ops
->data_get_ftype(dep
);
839 state
->extravalid
= 1;
840 state
->extrablk
.bp
= curbp
;
841 state
->extrablk
.blkno
= curdb
;
842 state
->extrablk
.index
= (int)((char *)dep
-
843 (char *)curbp
->b_addr
);
844 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
845 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
846 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
847 if (cmp
== XFS_CMP_EXACT
)
851 ASSERT(index
== leafhdr
.count
|| (args
->op_flags
& XFS_DA_OP_OKNOENT
));
853 if (args
->cmpresult
== XFS_CMP_DIFFERENT
) {
854 /* Giving back last used data block. */
855 state
->extravalid
= 1;
856 state
->extrablk
.bp
= curbp
;
857 state
->extrablk
.index
= -1;
858 state
->extrablk
.blkno
= curdb
;
859 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
860 curbp
->b_ops
= &xfs_dir3_data_buf_ops
;
861 xfs_trans_buf_set_type(tp
, curbp
, XFS_BLFT_DIR_DATA_BUF
);
863 /* If the curbp is not the CI match block, drop it */
864 if (state
->extrablk
.bp
!= curbp
)
865 xfs_trans_brelse(tp
, curbp
);
868 state
->extravalid
= 0;
875 * Look up a leaf entry in a node-format leaf block.
876 * If this is an addname then the extrablk in state is a freespace block,
877 * otherwise it's a data block.
880 xfs_dir2_leafn_lookup_int(
881 struct xfs_buf
*bp
, /* leaf buffer */
882 xfs_da_args_t
*args
, /* operation arguments */
883 int *indexp
, /* out: leaf entry index */
884 xfs_da_state_t
*state
) /* state to fill in */
886 if (args
->op_flags
& XFS_DA_OP_ADDNAME
)
887 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
889 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
893 * Move count leaf entries from source to destination leaf.
894 * Log entries and headers. Stale entries are preserved.
897 xfs_dir3_leafn_moveents(
898 xfs_da_args_t
*args
, /* operation arguments */
899 struct xfs_buf
*bp_s
, /* source */
900 struct xfs_dir3_icleaf_hdr
*shdr
,
901 struct xfs_dir2_leaf_entry
*sents
,
902 int start_s
,/* source leaf index */
903 struct xfs_buf
*bp_d
, /* destination */
904 struct xfs_dir3_icleaf_hdr
*dhdr
,
905 struct xfs_dir2_leaf_entry
*dents
,
906 int start_d
,/* destination leaf index */
907 int count
) /* count of leaves to copy */
909 int stale
; /* count stale leaves copied */
911 trace_xfs_dir2_leafn_moveents(args
, start_s
, start_d
, count
);
914 * Silently return if nothing to do.
920 * If the destination index is not the end of the current
921 * destination leaf entries, open up a hole in the destination
922 * to hold the new entries.
924 if (start_d
< dhdr
->count
) {
925 memmove(&dents
[start_d
+ count
], &dents
[start_d
],
926 (dhdr
->count
- start_d
) * sizeof(xfs_dir2_leaf_entry_t
));
927 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
+ count
,
928 count
+ dhdr
->count
- 1);
931 * If the source has stale leaves, count the ones in the copy range
932 * so we can update the header correctly.
935 int i
; /* temp leaf index */
937 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
938 if (sents
[i
].address
==
939 cpu_to_be32(XFS_DIR2_NULL_DATAPTR
))
945 * Copy the leaf entries from source to destination.
947 memcpy(&dents
[start_d
], &sents
[start_s
],
948 count
* sizeof(xfs_dir2_leaf_entry_t
));
949 xfs_dir3_leaf_log_ents(args
, bp_d
, start_d
, start_d
+ count
- 1);
952 * If there are source entries after the ones we copied,
953 * delete the ones we copied by sliding the next ones down.
955 if (start_s
+ count
< shdr
->count
) {
956 memmove(&sents
[start_s
], &sents
[start_s
+ count
],
957 count
* sizeof(xfs_dir2_leaf_entry_t
));
958 xfs_dir3_leaf_log_ents(args
, bp_s
, start_s
, start_s
+ count
- 1);
962 * Update the headers and log them.
964 shdr
->count
-= count
;
965 shdr
->stale
-= stale
;
966 dhdr
->count
+= count
;
967 dhdr
->stale
+= stale
;
971 * Determine the sort order of two leaf blocks.
972 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
975 xfs_dir2_leafn_order(
976 struct xfs_inode
*dp
,
977 struct xfs_buf
*leaf1_bp
, /* leaf1 buffer */
978 struct xfs_buf
*leaf2_bp
) /* leaf2 buffer */
980 struct xfs_dir2_leaf
*leaf1
= leaf1_bp
->b_addr
;
981 struct xfs_dir2_leaf
*leaf2
= leaf2_bp
->b_addr
;
982 struct xfs_dir2_leaf_entry
*ents1
;
983 struct xfs_dir2_leaf_entry
*ents2
;
984 struct xfs_dir3_icleaf_hdr hdr1
;
985 struct xfs_dir3_icleaf_hdr hdr2
;
987 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
988 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
989 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
990 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
992 if (hdr1
.count
> 0 && hdr2
.count
> 0 &&
993 (be32_to_cpu(ents2
[0].hashval
) < be32_to_cpu(ents1
[0].hashval
) ||
994 be32_to_cpu(ents2
[hdr2
.count
- 1].hashval
) <
995 be32_to_cpu(ents1
[hdr1
.count
- 1].hashval
)))
1001 * Rebalance leaf entries between two leaf blocks.
1002 * This is actually only called when the second block is new,
1003 * though the code deals with the general case.
1004 * A new entry will be inserted in one of the blocks, and that
1005 * entry is taken into account when balancing.
1008 xfs_dir2_leafn_rebalance(
1009 xfs_da_state_t
*state
, /* btree cursor */
1010 xfs_da_state_blk_t
*blk1
, /* first btree block */
1011 xfs_da_state_blk_t
*blk2
) /* second btree block */
1013 xfs_da_args_t
*args
; /* operation arguments */
1014 int count
; /* count (& direction) leaves */
1015 int isleft
; /* new goes in left leaf */
1016 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
1017 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
1018 int mid
; /* midpoint leaf index */
1019 #if defined(DEBUG) || defined(XFS_WARN)
1020 int oldstale
; /* old count of stale leaves */
1022 int oldsum
; /* old total leaf count */
1023 int swap
; /* swapped leaf blocks */
1024 struct xfs_dir2_leaf_entry
*ents1
;
1025 struct xfs_dir2_leaf_entry
*ents2
;
1026 struct xfs_dir3_icleaf_hdr hdr1
;
1027 struct xfs_dir3_icleaf_hdr hdr2
;
1028 struct xfs_inode
*dp
= state
->args
->dp
;
1032 * If the block order is wrong, swap the arguments.
1034 if ((swap
= xfs_dir2_leafn_order(dp
, blk1
->bp
, blk2
->bp
))) {
1035 xfs_da_state_blk_t
*tmp
; /* temp for block swap */
1041 leaf1
= blk1
->bp
->b_addr
;
1042 leaf2
= blk2
->bp
->b_addr
;
1043 dp
->d_ops
->leaf_hdr_from_disk(&hdr1
, leaf1
);
1044 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf2
);
1045 ents1
= dp
->d_ops
->leaf_ents_p(leaf1
);
1046 ents2
= dp
->d_ops
->leaf_ents_p(leaf2
);
1048 oldsum
= hdr1
.count
+ hdr2
.count
;
1049 #if defined(DEBUG) || defined(XFS_WARN)
1050 oldstale
= hdr1
.stale
+ hdr2
.stale
;
1055 * If the old leaf count was odd then the new one will be even,
1056 * so we need to divide the new count evenly.
1059 xfs_dahash_t midhash
; /* middle entry hash value */
1061 if (mid
>= hdr1
.count
)
1062 midhash
= be32_to_cpu(ents2
[mid
- hdr1
.count
].hashval
);
1064 midhash
= be32_to_cpu(ents1
[mid
].hashval
);
1065 isleft
= args
->hashval
<= midhash
;
1068 * If the old count is even then the new count is odd, so there's
1069 * no preferred side for the new entry.
1070 * Pick the left one.
1075 * Calculate moved entry count. Positive means left-to-right,
1076 * negative means right-to-left. Then move the entries.
1078 count
= hdr1
.count
- mid
+ (isleft
== 0);
1080 xfs_dir3_leafn_moveents(args
, blk1
->bp
, &hdr1
, ents1
,
1081 hdr1
.count
- count
, blk2
->bp
,
1082 &hdr2
, ents2
, 0, count
);
1084 xfs_dir3_leafn_moveents(args
, blk2
->bp
, &hdr2
, ents2
, 0,
1085 blk1
->bp
, &hdr1
, ents1
,
1088 ASSERT(hdr1
.count
+ hdr2
.count
== oldsum
);
1089 ASSERT(hdr1
.stale
+ hdr2
.stale
== oldstale
);
1091 /* log the changes made when moving the entries */
1092 dp
->d_ops
->leaf_hdr_to_disk(leaf1
, &hdr1
);
1093 dp
->d_ops
->leaf_hdr_to_disk(leaf2
, &hdr2
);
1094 xfs_dir3_leaf_log_header(args
, blk1
->bp
);
1095 xfs_dir3_leaf_log_header(args
, blk2
->bp
);
1097 xfs_dir3_leaf_check(dp
, blk1
->bp
);
1098 xfs_dir3_leaf_check(dp
, blk2
->bp
);
1101 * Mark whether we're inserting into the old or new leaf.
1103 if (hdr1
.count
< hdr2
.count
)
1104 state
->inleaf
= swap
;
1105 else if (hdr1
.count
> hdr2
.count
)
1106 state
->inleaf
= !swap
;
1108 state
->inleaf
= swap
^ (blk1
->index
<= hdr1
.count
);
1110 * Adjust the expected index for insertion.
1113 blk2
->index
= blk1
->index
- hdr1
.count
;
1116 * Finally sanity check just to make sure we are not returning a
1119 if (blk2
->index
< 0) {
1122 xfs_alert(dp
->i_mount
,
1123 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1124 __func__
, blk1
->index
);
1129 xfs_dir3_data_block_free(
1130 xfs_da_args_t
*args
,
1131 struct xfs_dir2_data_hdr
*hdr
,
1132 struct xfs_dir2_free
*free
,
1135 struct xfs_buf
*fbp
,
1140 struct xfs_dir3_icfree_hdr freehdr
;
1141 struct xfs_inode
*dp
= args
->dp
;
1143 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1144 bests
= dp
->d_ops
->free_bests_p(free
);
1147 * Data block is not empty, just set the free entry to the new
1150 bests
[findex
] = cpu_to_be16(longest
);
1151 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1155 /* One less used entry in the free table. */
1159 * If this was the last entry in the table, we can trim the table size
1160 * back. There might be other entries at the end referring to
1161 * non-existent data blocks, get those too.
1163 if (findex
== freehdr
.nvalid
- 1) {
1164 int i
; /* free entry index */
1166 for (i
= findex
- 1; i
>= 0; i
--) {
1167 if (bests
[i
] != cpu_to_be16(NULLDATAOFF
))
1170 freehdr
.nvalid
= i
+ 1;
1173 /* Not the last entry, just punch it out. */
1174 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1178 dp
->d_ops
->free_hdr_to_disk(free
, &freehdr
);
1179 xfs_dir2_free_log_header(args
, fbp
);
1182 * If there are no useful entries left in the block, get rid of the
1185 if (!freehdr
.nused
) {
1188 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1192 } else if (error
!= -ENOSPC
|| args
->total
!= 0)
1195 * It's possible to get ENOSPC if there is no
1196 * space reservation. In this case some one
1197 * else will eventually get rid of this block.
1201 /* Log the free entry that changed, unless we got rid of it. */
1203 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
1208 * Remove an entry from a node directory.
1209 * This removes the leaf entry and the data entry,
1210 * and updates the free block if necessary.
1212 static int /* error */
1213 xfs_dir2_leafn_remove(
1214 xfs_da_args_t
*args
, /* operation arguments */
1215 struct xfs_buf
*bp
, /* leaf buffer */
1216 int index
, /* leaf entry index */
1217 xfs_da_state_blk_t
*dblk
, /* data block */
1218 int *rval
) /* resulting block needs join */
1220 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1221 xfs_dir2_db_t db
; /* data block number */
1222 struct xfs_buf
*dbp
; /* data block buffer */
1223 xfs_dir2_data_entry_t
*dep
; /* data block entry */
1224 xfs_inode_t
*dp
; /* incore directory inode */
1225 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1226 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
1227 int longest
; /* longest data free entry */
1228 int off
; /* data block entry offset */
1229 int needlog
; /* need to log data header */
1230 int needscan
; /* need to rescan data frees */
1231 xfs_trans_t
*tp
; /* transaction pointer */
1232 struct xfs_dir2_data_free
*bf
; /* bestfree table */
1233 struct xfs_dir3_icleaf_hdr leafhdr
;
1234 struct xfs_dir2_leaf_entry
*ents
;
1236 trace_xfs_dir2_leafn_remove(args
, index
);
1241 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1242 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1245 * Point to the entry we're removing.
1250 * Extract the data block and offset from the entry.
1252 db
= xfs_dir2_dataptr_to_db(args
->geo
, be32_to_cpu(lep
->address
));
1253 ASSERT(dblk
->blkno
== db
);
1254 off
= xfs_dir2_dataptr_to_off(args
->geo
, be32_to_cpu(lep
->address
));
1255 ASSERT(dblk
->index
== off
);
1258 * Kill the leaf entry by marking it stale.
1259 * Log the leaf block changes.
1262 dp
->d_ops
->leaf_hdr_to_disk(leaf
, &leafhdr
);
1263 xfs_dir3_leaf_log_header(args
, bp
);
1265 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
1266 xfs_dir3_leaf_log_ents(args
, bp
, index
, index
);
1269 * Make the data entry free. Keep track of the longest freespace
1270 * in the data block in case it changes.
1274 dep
= (xfs_dir2_data_entry_t
*)((char *)hdr
+ off
);
1275 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1276 longest
= be16_to_cpu(bf
[0].length
);
1277 needlog
= needscan
= 0;
1278 xfs_dir2_data_make_free(args
, dbp
, off
,
1279 dp
->d_ops
->data_entsize(dep
->namelen
), &needlog
, &needscan
);
1281 * Rescan the data block freespaces for bestfree.
1282 * Log the data block header if needed.
1285 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
1287 xfs_dir2_data_log_header(args
, dbp
);
1288 xfs_dir3_data_check(dp
, dbp
);
1290 * If the longest data block freespace changes, need to update
1291 * the corresponding freeblock entry.
1293 if (longest
< be16_to_cpu(bf
[0].length
)) {
1294 int error
; /* error return value */
1295 struct xfs_buf
*fbp
; /* freeblock buffer */
1296 xfs_dir2_db_t fdb
; /* freeblock block number */
1297 int findex
; /* index in freeblock entries */
1298 xfs_dir2_free_t
*free
; /* freeblock structure */
1301 * Convert the data block number to a free block,
1302 * read in the free block.
1304 fdb
= dp
->d_ops
->db_to_fdb(args
->geo
, db
);
1305 error
= xfs_dir2_free_read(tp
, dp
,
1306 xfs_dir2_db_to_da(args
->geo
, fdb
),
1313 struct xfs_dir3_icfree_hdr freehdr
;
1314 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1315 ASSERT(freehdr
.firstdb
== dp
->d_ops
->free_max_bests(args
->geo
) *
1316 (fdb
- xfs_dir2_byte_to_db(args
->geo
,
1317 XFS_DIR2_FREE_OFFSET
)));
1321 * Calculate which entry we need to fix.
1323 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, db
);
1324 longest
= be16_to_cpu(bf
[0].length
);
1326 * If the data block is now empty we can get rid of it
1329 if (longest
== args
->geo
->blksize
-
1330 dp
->d_ops
->data_entry_offset
) {
1332 * Try to punch out the data block.
1334 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1340 * We can get ENOSPC if there's no space reservation.
1341 * In this case just drop the buffer and some one else
1342 * will eventually get rid of the empty block.
1344 else if (!(error
== -ENOSPC
&& args
->total
== 0))
1348 * If we got rid of the data block, we can eliminate that entry
1349 * in the free block.
1351 error
= xfs_dir3_data_block_free(args
, hdr
, free
,
1352 fdb
, findex
, fbp
, longest
);
1357 xfs_dir3_leaf_check(dp
, bp
);
1359 * Return indication of whether this leaf block is empty enough
1360 * to justify trying to join it with a neighbor.
1362 *rval
= (dp
->d_ops
->leaf_hdr_size
+
1363 (uint
)sizeof(ents
[0]) * (leafhdr
.count
- leafhdr
.stale
)) <
1364 args
->geo
->magicpct
;
1369 * Split the leaf entries in the old block into old and new blocks.
1372 xfs_dir2_leafn_split(
1373 xfs_da_state_t
*state
, /* btree cursor */
1374 xfs_da_state_blk_t
*oldblk
, /* original block */
1375 xfs_da_state_blk_t
*newblk
) /* newly created block */
1377 xfs_da_args_t
*args
; /* operation arguments */
1378 xfs_dablk_t blkno
; /* new leaf block number */
1379 int error
; /* error return value */
1380 struct xfs_inode
*dp
;
1383 * Allocate space for a new leaf node.
1387 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1388 error
= xfs_da_grow_inode(args
, &blkno
);
1393 * Initialize the new leaf block.
1395 error
= xfs_dir3_leaf_get_buf(args
, xfs_dir2_da_to_db(args
->geo
, blkno
),
1396 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1400 newblk
->blkno
= blkno
;
1401 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1403 * Rebalance the entries across the two leaves, link the new
1404 * block into the leaves.
1406 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1407 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1412 * Insert the new entry in the correct block.
1415 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1417 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1419 * Update last hashval in each block since we added the name.
1421 oldblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, oldblk
->bp
, NULL
);
1422 newblk
->hashval
= xfs_dir2_leaf_lasthash(dp
, newblk
->bp
, NULL
);
1423 xfs_dir3_leaf_check(dp
, oldblk
->bp
);
1424 xfs_dir3_leaf_check(dp
, newblk
->bp
);
1429 * Check a leaf block and its neighbors to see if the block should be
1430 * collapsed into one or the other neighbor. Always keep the block
1431 * with the smaller block number.
1432 * If the current block is over 50% full, don't try to join it, return 0.
1433 * If the block is empty, fill in the state structure and return 2.
1434 * If it can be collapsed, fill in the state structure and return 1.
1435 * If nothing can be done, return 0.
1438 xfs_dir2_leafn_toosmall(
1439 xfs_da_state_t
*state
, /* btree cursor */
1440 int *action
) /* resulting action to take */
1442 xfs_da_state_blk_t
*blk
; /* leaf block */
1443 xfs_dablk_t blkno
; /* leaf block number */
1444 struct xfs_buf
*bp
; /* leaf buffer */
1445 int bytes
; /* bytes in use */
1446 int count
; /* leaf live entry count */
1447 int error
; /* error return value */
1448 int forward
; /* sibling block direction */
1449 int i
; /* sibling counter */
1450 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1451 int rval
; /* result from path_shift */
1452 struct xfs_dir3_icleaf_hdr leafhdr
;
1453 struct xfs_dir2_leaf_entry
*ents
;
1454 struct xfs_inode
*dp
= state
->args
->dp
;
1457 * Check for the degenerate case of the block being over 50% full.
1458 * If so, it's not worth even looking to see if we might be able
1459 * to coalesce with a sibling.
1461 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1462 leaf
= blk
->bp
->b_addr
;
1463 dp
->d_ops
->leaf_hdr_from_disk(&leafhdr
, leaf
);
1464 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1465 xfs_dir3_leaf_check(dp
, blk
->bp
);
1467 count
= leafhdr
.count
- leafhdr
.stale
;
1468 bytes
= dp
->d_ops
->leaf_hdr_size
+ count
* sizeof(ents
[0]);
1469 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1471 * Blk over 50%, don't try to join.
1477 * Check for the degenerate case of the block being empty.
1478 * If the block is empty, we'll simply delete it, no need to
1479 * coalesce it with a sibling block. We choose (arbitrarily)
1480 * to merge with the forward block unless it is NULL.
1484 * Make altpath point to the block we want to keep and
1485 * path point to the block we want to drop (this one).
1487 forward
= (leafhdr
.forw
!= 0);
1488 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1489 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1493 *action
= rval
? 2 : 0;
1497 * Examine each sibling block to see if we can coalesce with
1498 * at least 25% free space to spare. We need to figure out
1499 * whether to merge with the forward or the backward block.
1500 * We prefer coalescing with the lower numbered sibling so as
1501 * to shrink a directory over time.
1503 forward
= leafhdr
.forw
< leafhdr
.back
;
1504 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1505 struct xfs_dir3_icleaf_hdr hdr2
;
1507 blkno
= forward
? leafhdr
.forw
: leafhdr
.back
;
1511 * Read the sibling leaf block.
1513 error
= xfs_dir3_leafn_read(state
->args
->trans
, dp
,
1519 * Count bytes in the two blocks combined.
1521 count
= leafhdr
.count
- leafhdr
.stale
;
1522 bytes
= state
->args
->geo
->blksize
-
1523 (state
->args
->geo
->blksize
>> 2);
1526 dp
->d_ops
->leaf_hdr_from_disk(&hdr2
, leaf
);
1527 ents
= dp
->d_ops
->leaf_ents_p(leaf
);
1528 count
+= hdr2
.count
- hdr2
.stale
;
1529 bytes
-= count
* sizeof(ents
[0]);
1532 * Fits with at least 25% to spare.
1536 xfs_trans_brelse(state
->args
->trans
, bp
);
1539 * Didn't like either block, give up.
1547 * Make altpath point to the block we want to keep (the lower
1548 * numbered block) and path point to the block we want to drop.
1550 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1551 if (blkno
< blk
->blkno
)
1552 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
, 0,
1555 error
= xfs_da3_path_shift(state
, &state
->path
, forward
, 0,
1560 *action
= rval
? 0 : 1;
1565 * Move all the leaf entries from drop_blk to save_blk.
1566 * This is done as part of a join operation.
1569 xfs_dir2_leafn_unbalance(
1570 xfs_da_state_t
*state
, /* cursor */
1571 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1572 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1574 xfs_da_args_t
*args
; /* operation arguments */
1575 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1576 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1577 struct xfs_dir3_icleaf_hdr savehdr
;
1578 struct xfs_dir3_icleaf_hdr drophdr
;
1579 struct xfs_dir2_leaf_entry
*sents
;
1580 struct xfs_dir2_leaf_entry
*dents
;
1581 struct xfs_inode
*dp
= state
->args
->dp
;
1584 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1585 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1586 drop_leaf
= drop_blk
->bp
->b_addr
;
1587 save_leaf
= save_blk
->bp
->b_addr
;
1589 dp
->d_ops
->leaf_hdr_from_disk(&savehdr
, save_leaf
);
1590 dp
->d_ops
->leaf_hdr_from_disk(&drophdr
, drop_leaf
);
1591 sents
= dp
->d_ops
->leaf_ents_p(save_leaf
);
1592 dents
= dp
->d_ops
->leaf_ents_p(drop_leaf
);
1595 * If there are any stale leaf entries, take this opportunity
1599 xfs_dir3_leaf_compact(args
, &drophdr
, drop_blk
->bp
);
1601 xfs_dir3_leaf_compact(args
, &savehdr
, save_blk
->bp
);
1604 * Move the entries from drop to the appropriate end of save.
1606 drop_blk
->hashval
= be32_to_cpu(dents
[drophdr
.count
- 1].hashval
);
1607 if (xfs_dir2_leafn_order(dp
, save_blk
->bp
, drop_blk
->bp
))
1608 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1609 save_blk
->bp
, &savehdr
, sents
, 0,
1612 xfs_dir3_leafn_moveents(args
, drop_blk
->bp
, &drophdr
, dents
, 0,
1613 save_blk
->bp
, &savehdr
, sents
,
1614 savehdr
.count
, drophdr
.count
);
1615 save_blk
->hashval
= be32_to_cpu(sents
[savehdr
.count
- 1].hashval
);
1617 /* log the changes made when moving the entries */
1618 dp
->d_ops
->leaf_hdr_to_disk(save_leaf
, &savehdr
);
1619 dp
->d_ops
->leaf_hdr_to_disk(drop_leaf
, &drophdr
);
1620 xfs_dir3_leaf_log_header(args
, save_blk
->bp
);
1621 xfs_dir3_leaf_log_header(args
, drop_blk
->bp
);
1623 xfs_dir3_leaf_check(dp
, save_blk
->bp
);
1624 xfs_dir3_leaf_check(dp
, drop_blk
->bp
);
1628 * Top-level node form directory addname routine.
1631 xfs_dir2_node_addname(
1632 xfs_da_args_t
*args
) /* operation arguments */
1634 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1635 int error
; /* error return value */
1636 int rval
; /* sub-return value */
1637 xfs_da_state_t
*state
; /* btree cursor */
1639 trace_xfs_dir2_node_addname(args
);
1642 * Allocate and initialize the state (btree cursor).
1644 state
= xfs_da_state_alloc();
1646 state
->mp
= args
->dp
->i_mount
;
1648 * Look up the name. We're not supposed to find it, but
1649 * this gives us the insertion point.
1651 error
= xfs_da3_node_lookup_int(state
, &rval
);
1654 if (rval
!= -ENOENT
) {
1658 * Add the data entry to a data block.
1659 * Extravalid is set to a freeblock found by lookup.
1661 rval
= xfs_dir2_node_addname_int(args
,
1662 state
->extravalid
? &state
->extrablk
: NULL
);
1666 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1667 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1669 * Add the new leaf entry.
1671 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1674 * It worked, fix the hash values up the btree.
1676 if (!(args
->op_flags
& XFS_DA_OP_JUSTCHECK
))
1677 xfs_da3_fixhashpath(state
, &state
->path
);
1680 * It didn't work, we need to split the leaf block.
1682 if (args
->total
== 0) {
1683 ASSERT(rval
== -ENOSPC
);
1687 * Split the leaf block and insert the new entry.
1689 rval
= xfs_da3_split(state
);
1692 xfs_da_state_free(state
);
1697 * Add the data entry for a node-format directory name addition.
1698 * The leaf entry is added in xfs_dir2_leafn_add.
1699 * We may enter with a freespace block that the lookup found.
1701 static int /* error */
1702 xfs_dir2_node_addname_int(
1703 xfs_da_args_t
*args
, /* operation arguments */
1704 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1706 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
1707 xfs_dir2_db_t dbno
; /* data block number */
1708 struct xfs_buf
*dbp
; /* data block buffer */
1709 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1710 xfs_inode_t
*dp
; /* incore directory inode */
1711 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1712 int error
; /* error return value */
1713 xfs_dir2_db_t fbno
; /* freespace block number */
1714 struct xfs_buf
*fbp
; /* freespace buffer */
1715 int findex
; /* freespace entry index */
1716 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1717 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1718 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1719 int length
; /* length of the new entry */
1720 int logfree
; /* need to log free entry */
1721 xfs_mount_t
*mp
; /* filesystem mount point */
1722 int needlog
; /* need to log data header */
1723 int needscan
; /* need to rescan data frees */
1724 __be16
*tagp
; /* data entry tag pointer */
1725 xfs_trans_t
*tp
; /* transaction pointer */
1727 struct xfs_dir3_icfree_hdr freehdr
;
1728 struct xfs_dir2_data_free
*bf
;
1729 xfs_dir2_data_aoff_t aoff
;
1734 length
= dp
->d_ops
->data_entsize(args
->namelen
);
1736 * If we came in with a freespace block that means that lookup
1737 * found an entry with our hash value. This is the freespace
1738 * block for that data entry.
1743 * Remember initial freespace block number.
1745 ifbno
= fblk
->blkno
;
1747 findex
= fblk
->index
;
1748 bests
= dp
->d_ops
->free_bests_p(free
);
1749 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1752 * This means the free entry showed that the data block had
1753 * space for our entry, so we remembered it.
1754 * Use that data block.
1757 ASSERT(findex
< freehdr
.nvalid
);
1758 ASSERT(be16_to_cpu(bests
[findex
]) != NULLDATAOFF
);
1759 ASSERT(be16_to_cpu(bests
[findex
]) >= length
);
1760 dbno
= freehdr
.firstdb
+ findex
;
1763 * The data block looked at didn't have enough room.
1764 * We'll start at the beginning of the freespace entries.
1771 * Didn't come in with a freespace block, so no data block.
1779 * If we don't have a data block yet, we're going to scan the
1780 * freespace blocks looking for one. Figure out what the
1781 * highest freespace block number is.
1784 xfs_fileoff_t fo
; /* freespace block number */
1786 if ((error
= xfs_bmap_last_offset(dp
, &fo
, XFS_DATA_FORK
)))
1788 lastfbno
= xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
);
1792 * While we haven't identified a data block, search the freeblock
1793 * data for a good data block. If we find a null freeblock entry,
1794 * indicating a hole in the data blocks, remember that.
1796 while (dbno
== -1) {
1798 * If we don't have a freeblock in hand, get the next one.
1802 * Happens the first time through unless lookup gave
1803 * us a freespace block to start with.
1806 fbno
= xfs_dir2_byte_to_db(args
->geo
,
1807 XFS_DIR2_FREE_OFFSET
);
1809 * If it's ifbno we already looked at it.
1814 * If it's off the end we're done.
1816 if (fbno
>= lastfbno
)
1819 * Read the block. There can be holes in the
1820 * freespace blocks, so this might not succeed.
1821 * This should be really rare, so there's no reason
1824 error
= xfs_dir2_free_try_read(tp
, dp
,
1825 xfs_dir2_db_to_da(args
->geo
, fbno
),
1835 * Look at the current free entry. Is it good enough?
1837 * The bests initialisation should be where the bufer is read in
1838 * the above branch. But gcc is too stupid to realise that bests
1839 * and the freehdr are actually initialised if they are placed
1840 * there, so we have to do it here to avoid warnings. Blech.
1842 bests
= dp
->d_ops
->free_bests_p(free
);
1843 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1844 if (be16_to_cpu(bests
[findex
]) != NULLDATAOFF
&&
1845 be16_to_cpu(bests
[findex
]) >= length
)
1846 dbno
= freehdr
.firstdb
+ findex
;
1849 * Are we done with the freeblock?
1851 if (++findex
== freehdr
.nvalid
) {
1855 xfs_trans_brelse(tp
, fbp
);
1857 if (fblk
&& fblk
->bp
)
1863 * If we don't have a data block, we need to allocate one and make
1864 * the freespace entries refer to it.
1866 if (unlikely(dbno
== -1)) {
1868 * Not allowed to allocate, return failure.
1870 if ((args
->op_flags
& XFS_DA_OP_JUSTCHECK
) || args
->total
== 0)
1874 * Allocate and initialize the new data block.
1876 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1877 XFS_DIR2_DATA_SPACE
,
1879 (error
= xfs_dir3_data_init(args
, dbno
, &dbp
))))
1883 * If (somehow) we have a freespace block, get rid of it.
1886 xfs_trans_brelse(tp
, fbp
);
1887 if (fblk
&& fblk
->bp
)
1891 * Get the freespace block corresponding to the data block
1892 * that was just allocated.
1894 fbno
= dp
->d_ops
->db_to_fdb(args
->geo
, dbno
);
1895 error
= xfs_dir2_free_try_read(tp
, dp
,
1896 xfs_dir2_db_to_da(args
->geo
, fbno
),
1902 * If there wasn't a freespace block, the read will
1903 * return a NULL fbp. Allocate and initialize a new one.
1906 error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1911 if (dp
->d_ops
->db_to_fdb(args
->geo
, dbno
) != fbno
) {
1913 "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
1914 __func__
, (unsigned long long)dp
->i_ino
,
1915 (long long)dp
->d_ops
->db_to_fdb(
1917 (long long)dbno
, (long long)fbno
,
1918 (unsigned long long)ifbno
, lastfbno
);
1921 " fblk "PTR_FMT
" blkno %llu index %d magic 0x%x",
1923 (unsigned long long)fblk
->blkno
,
1927 xfs_alert(mp
, " ... fblk is NULL");
1929 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1930 XFS_ERRLEVEL_LOW
, mp
);
1931 return -EFSCORRUPTED
;
1935 * Get a buffer for the new block.
1937 error
= xfs_dir3_free_get_buf(args
, fbno
, &fbp
);
1941 bests
= dp
->d_ops
->free_bests_p(free
);
1942 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1945 * Remember the first slot as our empty slot.
1948 (fbno
- xfs_dir2_byte_to_db(args
->geo
,
1949 XFS_DIR2_FREE_OFFSET
)) *
1950 dp
->d_ops
->free_max_bests(args
->geo
);
1953 bests
= dp
->d_ops
->free_bests_p(free
);
1954 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
1958 * Set the freespace block index from the data block number.
1960 findex
= dp
->d_ops
->db_to_fdindex(args
->geo
, dbno
);
1962 * If it's after the end of the current entries in the
1963 * freespace block, extend that table.
1965 if (findex
>= freehdr
.nvalid
) {
1966 ASSERT(findex
< dp
->d_ops
->free_max_bests(args
->geo
));
1967 freehdr
.nvalid
= findex
+ 1;
1969 * Tag new entry so nused will go up.
1971 bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1974 * If this entry was for an empty data block
1975 * (this should always be true) then update the header.
1977 if (bests
[findex
] == cpu_to_be16(NULLDATAOFF
)) {
1979 dp
->d_ops
->free_hdr_to_disk(fbp
->b_addr
, &freehdr
);
1980 xfs_dir2_free_log_header(args
, fbp
);
1983 * Update the real value in the table.
1984 * We haven't allocated the data entry yet so this will
1988 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
1989 bests
[findex
] = bf
[0].length
;
1993 * We had a data block so we don't have to make a new one.
1997 * If just checking, we succeeded.
1999 if (args
->op_flags
& XFS_DA_OP_JUSTCHECK
)
2003 * Read the data block in.
2005 error
= xfs_dir3_data_read(tp
, dp
,
2006 xfs_dir2_db_to_da(args
->geo
, dbno
),
2011 bf
= dp
->d_ops
->data_bestfree_p(hdr
);
2014 ASSERT(be16_to_cpu(bf
[0].length
) >= length
);
2016 * Point to the existing unused space.
2018 dup
= (xfs_dir2_data_unused_t
*)
2019 ((char *)hdr
+ be16_to_cpu(bf
[0].offset
));
2020 needscan
= needlog
= 0;
2022 * Mark the first part of the unused space, inuse for us.
2024 aoff
= (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)hdr
);
2025 error
= xfs_dir2_data_use_free(args
, dbp
, dup
, aoff
, length
,
2026 &needlog
, &needscan
);
2028 xfs_trans_brelse(tp
, dbp
);
2032 * Fill in the new entry and log it.
2034 dep
= (xfs_dir2_data_entry_t
*)dup
;
2035 dep
->inumber
= cpu_to_be64(args
->inumber
);
2036 dep
->namelen
= args
->namelen
;
2037 memcpy(dep
->name
, args
->name
, dep
->namelen
);
2038 dp
->d_ops
->data_put_ftype(dep
, args
->filetype
);
2039 tagp
= dp
->d_ops
->data_entry_tag_p(dep
);
2040 *tagp
= cpu_to_be16((char *)dep
- (char *)hdr
);
2041 xfs_dir2_data_log_entry(args
, dbp
, dep
);
2043 * Rescan the block for bestfree if needed.
2046 xfs_dir2_data_freescan(dp
, hdr
, &needlog
);
2048 * Log the data block header if needed.
2051 xfs_dir2_data_log_header(args
, dbp
);
2053 * If the freespace entry is now wrong, update it.
2055 bests
= dp
->d_ops
->free_bests_p(free
); /* gcc is so stupid */
2056 if (be16_to_cpu(bests
[findex
]) != be16_to_cpu(bf
[0].length
)) {
2057 bests
[findex
] = bf
[0].length
;
2061 * Log the freespace entry if needed.
2064 xfs_dir2_free_log_bests(args
, fbp
, findex
, findex
);
2066 * Return the data block and offset in args, then drop the data block.
2068 args
->blkno
= (xfs_dablk_t
)dbno
;
2069 args
->index
= be16_to_cpu(*tagp
);
2074 * Lookup an entry in a node-format directory.
2075 * All the real work happens in xfs_da3_node_lookup_int.
2076 * The only real output is the inode number of the entry.
2079 xfs_dir2_node_lookup(
2080 xfs_da_args_t
*args
) /* operation arguments */
2082 int error
; /* error return value */
2083 int i
; /* btree level */
2084 int rval
; /* operation return value */
2085 xfs_da_state_t
*state
; /* btree cursor */
2087 trace_xfs_dir2_node_lookup(args
);
2090 * Allocate and initialize the btree cursor.
2092 state
= xfs_da_state_alloc();
2094 state
->mp
= args
->dp
->i_mount
;
2096 * Fill in the path to the entry in the cursor.
2098 error
= xfs_da3_node_lookup_int(state
, &rval
);
2101 else if (rval
== -ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
) {
2102 /* If a CI match, dup the actual name and return -EEXIST */
2103 xfs_dir2_data_entry_t
*dep
;
2105 dep
= (xfs_dir2_data_entry_t
*)
2106 ((char *)state
->extrablk
.bp
->b_addr
+
2107 state
->extrablk
.index
);
2108 rval
= xfs_dir_cilookup_result(args
, dep
->name
, dep
->namelen
);
2111 * Release the btree blocks and leaf block.
2113 for (i
= 0; i
< state
->path
.active
; i
++) {
2114 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2115 state
->path
.blk
[i
].bp
= NULL
;
2118 * Release the data block if we have it.
2120 if (state
->extravalid
&& state
->extrablk
.bp
) {
2121 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2122 state
->extrablk
.bp
= NULL
;
2124 xfs_da_state_free(state
);
2129 * Remove an entry from a node-format directory.
2132 xfs_dir2_node_removename(
2133 struct xfs_da_args
*args
) /* operation arguments */
2135 struct xfs_da_state_blk
*blk
; /* leaf block */
2136 int error
; /* error return value */
2137 int rval
; /* operation return value */
2138 struct xfs_da_state
*state
; /* btree cursor */
2140 trace_xfs_dir2_node_removename(args
);
2143 * Allocate and initialize the btree cursor.
2145 state
= xfs_da_state_alloc();
2147 state
->mp
= args
->dp
->i_mount
;
2149 /* Look up the entry we're deleting, set up the cursor. */
2150 error
= xfs_da3_node_lookup_int(state
, &rval
);
2154 /* Didn't find it, upper layer screwed up. */
2155 if (rval
!= -EEXIST
) {
2160 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2161 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2162 ASSERT(state
->extravalid
);
2164 * Remove the leaf and data entries.
2165 * Extrablk refers to the data block.
2167 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
2168 &state
->extrablk
, &rval
);
2172 * Fix the hash values up the btree.
2174 xfs_da3_fixhashpath(state
, &state
->path
);
2176 * If we need to join leaf blocks, do it.
2178 if (rval
&& state
->path
.active
> 1)
2179 error
= xfs_da3_join(state
);
2181 * If no errors so far, try conversion to leaf format.
2184 error
= xfs_dir2_node_to_leaf(state
);
2186 xfs_da_state_free(state
);
2191 * Replace an entry's inode number in a node-format directory.
2194 xfs_dir2_node_replace(
2195 xfs_da_args_t
*args
) /* operation arguments */
2197 xfs_da_state_blk_t
*blk
; /* leaf block */
2198 xfs_dir2_data_hdr_t
*hdr
; /* data block header */
2199 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
2200 int error
; /* error return value */
2201 int i
; /* btree level */
2202 xfs_ino_t inum
; /* new inode number */
2203 int ftype
; /* new file type */
2204 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
2205 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
2206 int rval
; /* internal return value */
2207 xfs_da_state_t
*state
; /* btree cursor */
2209 trace_xfs_dir2_node_replace(args
);
2212 * Allocate and initialize the btree cursor.
2214 state
= xfs_da_state_alloc();
2216 state
->mp
= args
->dp
->i_mount
;
2219 * We have to save new inode number and ftype since
2220 * xfs_da3_node_lookup_int() is going to overwrite them
2222 inum
= args
->inumber
;
2223 ftype
= args
->filetype
;
2226 * Lookup the entry to change in the btree.
2228 error
= xfs_da3_node_lookup_int(state
, &rval
);
2233 * It should be found, since the vnodeops layer has looked it up
2234 * and locked it. But paranoia is good.
2236 if (rval
== -EEXIST
) {
2237 struct xfs_dir2_leaf_entry
*ents
;
2239 * Find the leaf entry.
2241 blk
= &state
->path
.blk
[state
->path
.active
- 1];
2242 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
2243 leaf
= blk
->bp
->b_addr
;
2244 ents
= args
->dp
->d_ops
->leaf_ents_p(leaf
);
2245 lep
= &ents
[blk
->index
];
2246 ASSERT(state
->extravalid
);
2248 * Point to the data entry.
2250 hdr
= state
->extrablk
.bp
->b_addr
;
2251 ASSERT(hdr
->magic
== cpu_to_be32(XFS_DIR2_DATA_MAGIC
) ||
2252 hdr
->magic
== cpu_to_be32(XFS_DIR3_DATA_MAGIC
));
2253 dep
= (xfs_dir2_data_entry_t
*)
2255 xfs_dir2_dataptr_to_off(args
->geo
,
2256 be32_to_cpu(lep
->address
)));
2257 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
2259 * Fill in the new inode number and log the entry.
2261 dep
->inumber
= cpu_to_be64(inum
);
2262 args
->dp
->d_ops
->data_put_ftype(dep
, ftype
);
2263 xfs_dir2_data_log_entry(args
, state
->extrablk
.bp
, dep
);
2267 * Didn't find it, and we're holding a data block. Drop it.
2269 else if (state
->extravalid
) {
2270 xfs_trans_brelse(args
->trans
, state
->extrablk
.bp
);
2271 state
->extrablk
.bp
= NULL
;
2274 * Release all the buffers in the cursor.
2276 for (i
= 0; i
< state
->path
.active
; i
++) {
2277 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
2278 state
->path
.blk
[i
].bp
= NULL
;
2280 xfs_da_state_free(state
);
2285 * Trim off a trailing empty freespace block.
2286 * Return (in rvalp) 1 if we did it, 0 if not.
2289 xfs_dir2_node_trim_free(
2290 xfs_da_args_t
*args
, /* operation arguments */
2291 xfs_fileoff_t fo
, /* free block number */
2292 int *rvalp
) /* out: did something */
2294 struct xfs_buf
*bp
; /* freespace buffer */
2295 xfs_inode_t
*dp
; /* incore directory inode */
2296 int error
; /* error return code */
2297 xfs_dir2_free_t
*free
; /* freespace structure */
2298 xfs_trans_t
*tp
; /* transaction pointer */
2299 struct xfs_dir3_icfree_hdr freehdr
;
2307 * Read the freespace block.
2309 error
= xfs_dir2_free_try_read(tp
, dp
, fo
, &bp
);
2313 * There can be holes in freespace. If fo is a hole, there's
2319 dp
->d_ops
->free_hdr_from_disk(&freehdr
, free
);
2322 * If there are used entries, there's nothing to do.
2324 if (freehdr
.nused
> 0) {
2325 xfs_trans_brelse(tp
, bp
);
2329 * Blow the block away.
2331 error
= xfs_dir2_shrink_inode(args
,
2332 xfs_dir2_da_to_db(args
->geo
, (xfs_dablk_t
)fo
), bp
);
2335 * Can't fail with ENOSPC since that only happens with no
2336 * space reservation, when breaking up an extent into two
2337 * pieces. This is the last block of an extent.
2339 ASSERT(error
!= -ENOSPC
);
2340 xfs_trans_brelse(tp
, bp
);
2344 * Return that we succeeded.