10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
230f712c9SDave Chinner /*
330f712c9SDave Chinner * Copyright (c) 2000-2005 Silicon Graphics, Inc.
430f712c9SDave Chinner * Copyright (c) 2013 Red Hat, Inc.
530f712c9SDave Chinner * All Rights Reserved.
630f712c9SDave Chinner */
730f712c9SDave Chinner #include "xfs.h"
830f712c9SDave Chinner #include "xfs_fs.h"
95467b34bSDarrick J. Wong #include "xfs_shared.h"
1030f712c9SDave Chinner #include "xfs_format.h"
1130f712c9SDave Chinner #include "xfs_log_format.h"
1230f712c9SDave Chinner #include "xfs_trans_resv.h"
1330f712c9SDave Chinner #include "xfs_mount.h"
1430f712c9SDave Chinner #include "xfs_inode.h"
1530f712c9SDave Chinner #include "xfs_bmap.h"
1630f712c9SDave Chinner #include "xfs_dir2.h"
1730f712c9SDave Chinner #include "xfs_dir2_priv.h"
1830f712c9SDave Chinner #include "xfs_error.h"
1930f712c9SDave Chinner #include "xfs_trace.h"
2030f712c9SDave Chinner #include "xfs_trans.h"
2130f712c9SDave Chinner #include "xfs_buf_item.h"
22a45086e2SBrian Foster #include "xfs_log.h"
2330f712c9SDave Chinner
2430f712c9SDave Chinner /*
2530f712c9SDave Chinner * Function declarations.
2630f712c9SDave Chinner */
2730f712c9SDave Chinner static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
2830f712c9SDave Chinner int index);
2930f712c9SDave Chinner static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
3030f712c9SDave Chinner xfs_da_state_blk_t *blk1,
3130f712c9SDave Chinner xfs_da_state_blk_t *blk2);
3230f712c9SDave Chinner static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
3330f712c9SDave Chinner int index, xfs_da_state_blk_t *dblk,
3430f712c9SDave Chinner int *rval);
3530f712c9SDave Chinner
3630f712c9SDave Chinner /*
373d92c93bSChristoph Hellwig * Convert data space db to the corresponding free db.
383d92c93bSChristoph Hellwig */
393d92c93bSChristoph Hellwig static xfs_dir2_db_t
xfs_dir2_db_to_fdb(struct xfs_da_geometry * geo,xfs_dir2_db_t db)403d92c93bSChristoph Hellwig xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
413d92c93bSChristoph Hellwig {
423d92c93bSChristoph Hellwig return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
433d92c93bSChristoph Hellwig (db / geo->free_max_bests);
443d92c93bSChristoph Hellwig }
453d92c93bSChristoph Hellwig
463d92c93bSChristoph Hellwig /*
473d92c93bSChristoph Hellwig * Convert data space db to the corresponding index in a free db.
483d92c93bSChristoph Hellwig */
493d92c93bSChristoph Hellwig static int
xfs_dir2_db_to_fdindex(struct xfs_da_geometry * geo,xfs_dir2_db_t db)503d92c93bSChristoph Hellwig xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
513d92c93bSChristoph Hellwig {
523d92c93bSChristoph Hellwig return db % geo->free_max_bests;
533d92c93bSChristoph Hellwig }
543d92c93bSChristoph Hellwig
553d92c93bSChristoph Hellwig /*
5630f712c9SDave Chinner * Check internal consistency of a leafn block.
5730f712c9SDave Chinner */
5830f712c9SDave Chinner #ifdef DEBUG
59a6a781a5SDarrick J. Wong static xfs_failaddr_t
xfs_dir3_leafn_check(struct xfs_inode * dp,struct xfs_buf * bp)6030f712c9SDave Chinner xfs_dir3_leafn_check(
6130f712c9SDave Chinner struct xfs_inode *dp,
6230f712c9SDave Chinner struct xfs_buf *bp)
6330f712c9SDave Chinner {
6430f712c9SDave Chinner struct xfs_dir2_leaf *leaf = bp->b_addr;
6530f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr;
6630f712c9SDave Chinner
6751842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
6830f712c9SDave Chinner
6930f712c9SDave Chinner if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
7030f712c9SDave Chinner struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
71*9343ee76SDave Chinner if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp))
72a6a781a5SDarrick J. Wong return __this_address;
7330f712c9SDave Chinner } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
74a6a781a5SDarrick J. Wong return __this_address;
7530f712c9SDave Chinner
761fea323fSDave Chinner return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
7730f712c9SDave Chinner }
78a6a781a5SDarrick J. Wong
79a6a781a5SDarrick J. Wong static inline void
xfs_dir3_leaf_check(struct xfs_inode * dp,struct xfs_buf * bp)80a6a781a5SDarrick J. Wong xfs_dir3_leaf_check(
81a6a781a5SDarrick J. Wong struct xfs_inode *dp,
82a6a781a5SDarrick J. Wong struct xfs_buf *bp)
83a6a781a5SDarrick J. Wong {
84a6a781a5SDarrick J. Wong xfs_failaddr_t fa;
85a6a781a5SDarrick J. Wong
86a6a781a5SDarrick J. Wong fa = xfs_dir3_leafn_check(dp, bp);
87a6a781a5SDarrick J. Wong if (!fa)
88a6a781a5SDarrick J. Wong return;
89a6a781a5SDarrick J. Wong xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
902551a530SDarrick J. Wong bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
912551a530SDarrick J. Wong fa);
92a6a781a5SDarrick J. Wong ASSERT(0);
93a6a781a5SDarrick J. Wong }
9430f712c9SDave Chinner #else
9530f712c9SDave Chinner #define xfs_dir3_leaf_check(dp, bp)
9630f712c9SDave Chinner #endif
9730f712c9SDave Chinner
98a6a781a5SDarrick J. Wong static xfs_failaddr_t
xfs_dir3_free_verify(struct xfs_buf * bp)9930f712c9SDave Chinner xfs_dir3_free_verify(
10030f712c9SDave Chinner struct xfs_buf *bp)
10130f712c9SDave Chinner {
102dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount;
10330f712c9SDave Chinner struct xfs_dir2_free_hdr *hdr = bp->b_addr;
10430f712c9SDave Chinner
10539708c20SBrian Foster if (!xfs_verify_magic(bp, hdr->magic))
10639708c20SBrian Foster return __this_address;
10739708c20SBrian Foster
10838c26bfdSDave Chinner if (xfs_has_crc(mp)) {
10930f712c9SDave Chinner struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
11030f712c9SDave Chinner
111ce748eaaSEric Sandeen if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
112a6a781a5SDarrick J. Wong return __this_address;
113*9343ee76SDave Chinner if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
114a6a781a5SDarrick J. Wong return __this_address;
115a45086e2SBrian Foster if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
116a6a781a5SDarrick J. Wong return __this_address;
11730f712c9SDave Chinner }
11830f712c9SDave Chinner
11930f712c9SDave Chinner /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
12030f712c9SDave Chinner
121a6a781a5SDarrick J. Wong return NULL;
12230f712c9SDave Chinner }
12330f712c9SDave Chinner
12430f712c9SDave Chinner static void
xfs_dir3_free_read_verify(struct xfs_buf * bp)12530f712c9SDave Chinner xfs_dir3_free_read_verify(
12630f712c9SDave Chinner struct xfs_buf *bp)
12730f712c9SDave Chinner {
128dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount;
129bc1a09b8SDarrick J. Wong xfs_failaddr_t fa;
13030f712c9SDave Chinner
13138c26bfdSDave Chinner if (xfs_has_crc(mp) &&
13230f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
133bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSBADCRC, __this_address);
134bc1a09b8SDarrick J. Wong else {
135bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_verify(bp);
136bc1a09b8SDarrick J. Wong if (fa)
137bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa);
138bc1a09b8SDarrick J. Wong }
13930f712c9SDave Chinner }
14030f712c9SDave Chinner
14130f712c9SDave Chinner static void
xfs_dir3_free_write_verify(struct xfs_buf * bp)14230f712c9SDave Chinner xfs_dir3_free_write_verify(
14330f712c9SDave Chinner struct xfs_buf *bp)
14430f712c9SDave Chinner {
145dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount;
146fb1755a6SCarlos Maiolino struct xfs_buf_log_item *bip = bp->b_log_item;
14730f712c9SDave Chinner struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
148bc1a09b8SDarrick J. Wong xfs_failaddr_t fa;
14930f712c9SDave Chinner
150bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_verify(bp);
151bc1a09b8SDarrick J. Wong if (fa) {
152bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa);
15330f712c9SDave Chinner return;
15430f712c9SDave Chinner }
15530f712c9SDave Chinner
15638c26bfdSDave Chinner if (!xfs_has_crc(mp))
15730f712c9SDave Chinner return;
15830f712c9SDave Chinner
15930f712c9SDave Chinner if (bip)
16030f712c9SDave Chinner hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
16130f712c9SDave Chinner
16230f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
16330f712c9SDave Chinner }
16430f712c9SDave Chinner
16530f712c9SDave Chinner const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
166233135b7SEric Sandeen .name = "xfs_dir3_free",
16739708c20SBrian Foster .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
16839708c20SBrian Foster cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
16930f712c9SDave Chinner .verify_read = xfs_dir3_free_read_verify,
17030f712c9SDave Chinner .verify_write = xfs_dir3_free_write_verify,
171b5572597SDarrick J. Wong .verify_struct = xfs_dir3_free_verify,
17230f712c9SDave Chinner };
17330f712c9SDave Chinner
174de14c5f5SDarrick J. Wong /* Everything ok in the free block header? */
175bc1a09b8SDarrick J. Wong static xfs_failaddr_t
xfs_dir3_free_header_check(struct xfs_inode * dp,xfs_dablk_t fbno,struct xfs_buf * bp)176de14c5f5SDarrick J. Wong xfs_dir3_free_header_check(
177de14c5f5SDarrick J. Wong struct xfs_inode *dp,
178de14c5f5SDarrick J. Wong xfs_dablk_t fbno,
179de14c5f5SDarrick J. Wong struct xfs_buf *bp)
180de14c5f5SDarrick J. Wong {
181de14c5f5SDarrick J. Wong struct xfs_mount *mp = dp->i_mount;
1825893e4feSChristoph Hellwig int maxbests = mp->m_dir_geo->free_max_bests;
183de14c5f5SDarrick J. Wong unsigned int firstdb;
184de14c5f5SDarrick J. Wong
185de14c5f5SDarrick J. Wong firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
186de14c5f5SDarrick J. Wong xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
187de14c5f5SDarrick J. Wong maxbests;
18838c26bfdSDave Chinner if (xfs_has_crc(mp)) {
189de14c5f5SDarrick J. Wong struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
190de14c5f5SDarrick J. Wong
191de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->firstdb) != firstdb)
192a6a781a5SDarrick J. Wong return __this_address;
193de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->nvalid) > maxbests)
194a6a781a5SDarrick J. Wong return __this_address;
195de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
196a6a781a5SDarrick J. Wong return __this_address;
1976fb5aac7SDarrick J. Wong if (be64_to_cpu(hdr3->hdr.owner) != dp->i_ino)
1986fb5aac7SDarrick J. Wong return __this_address;
199de14c5f5SDarrick J. Wong } else {
200de14c5f5SDarrick J. Wong struct xfs_dir2_free_hdr *hdr = bp->b_addr;
201de14c5f5SDarrick J. Wong
202de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->firstdb) != firstdb)
203a6a781a5SDarrick J. Wong return __this_address;
204de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->nvalid) > maxbests)
205a6a781a5SDarrick J. Wong return __this_address;
206de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
207a6a781a5SDarrick J. Wong return __this_address;
208de14c5f5SDarrick J. Wong }
209a6a781a5SDarrick J. Wong return NULL;
210de14c5f5SDarrick J. Wong }
21130f712c9SDave Chinner
21230f712c9SDave Chinner static int
__xfs_dir3_free_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t fbno,unsigned int flags,struct xfs_buf ** bpp)21330f712c9SDave Chinner __xfs_dir3_free_read(
21430f712c9SDave Chinner struct xfs_trans *tp,
21530f712c9SDave Chinner struct xfs_inode *dp,
21630f712c9SDave Chinner xfs_dablk_t fbno,
217cd2c9f1bSChristoph Hellwig unsigned int flags,
21830f712c9SDave Chinner struct xfs_buf **bpp)
21930f712c9SDave Chinner {
220bc1a09b8SDarrick J. Wong xfs_failaddr_t fa;
22130f712c9SDave Chinner int err;
22230f712c9SDave Chinner
223cd2c9f1bSChristoph Hellwig err = xfs_da_read_buf(tp, dp, fbno, flags, bpp, XFS_DATA_FORK,
224cd2c9f1bSChristoph Hellwig &xfs_dir3_free_buf_ops);
225de14c5f5SDarrick J. Wong if (err || !*bpp)
226de14c5f5SDarrick J. Wong return err;
227de14c5f5SDarrick J. Wong
228de14c5f5SDarrick J. Wong /* Check things that we can't do in the verifier. */
229bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
230bc1a09b8SDarrick J. Wong if (fa) {
231ce99494cSDarrick J. Wong __xfs_buf_mark_corrupt(*bpp, fa);
232de14c5f5SDarrick J. Wong xfs_trans_brelse(tp, *bpp);
2331cb5deb5SDarrick J. Wong *bpp = NULL;
234de14c5f5SDarrick J. Wong return -EFSCORRUPTED;
235de14c5f5SDarrick J. Wong }
23630f712c9SDave Chinner
23730f712c9SDave Chinner /* try read returns without an error or *bpp if it lands in a hole */
238de14c5f5SDarrick J. Wong if (tp)
23930f712c9SDave Chinner xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
240de14c5f5SDarrick J. Wong
241de14c5f5SDarrick J. Wong return 0;
24230f712c9SDave Chinner }
24330f712c9SDave Chinner
2445ba30919SChristoph Hellwig void
xfs_dir2_free_hdr_from_disk(struct xfs_mount * mp,struct xfs_dir3_icfree_hdr * to,struct xfs_dir2_free * from)2455ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(
2465ba30919SChristoph Hellwig struct xfs_mount *mp,
2475ba30919SChristoph Hellwig struct xfs_dir3_icfree_hdr *to,
2485ba30919SChristoph Hellwig struct xfs_dir2_free *from)
2495ba30919SChristoph Hellwig {
250ebd9027dSDave Chinner if (xfs_has_crc(mp)) {
2515ba30919SChristoph Hellwig struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
2525ba30919SChristoph Hellwig
2535ba30919SChristoph Hellwig to->magic = be32_to_cpu(from3->hdr.hdr.magic);
2545ba30919SChristoph Hellwig to->firstdb = be32_to_cpu(from3->hdr.firstdb);
2555ba30919SChristoph Hellwig to->nvalid = be32_to_cpu(from3->hdr.nvalid);
2565ba30919SChristoph Hellwig to->nused = be32_to_cpu(from3->hdr.nused);
257a84f3d5cSChristoph Hellwig to->bests = from3->bests;
2585ba30919SChristoph Hellwig
2595ba30919SChristoph Hellwig ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
2605ba30919SChristoph Hellwig } else {
2615ba30919SChristoph Hellwig to->magic = be32_to_cpu(from->hdr.magic);
2625ba30919SChristoph Hellwig to->firstdb = be32_to_cpu(from->hdr.firstdb);
2635ba30919SChristoph Hellwig to->nvalid = be32_to_cpu(from->hdr.nvalid);
2645ba30919SChristoph Hellwig to->nused = be32_to_cpu(from->hdr.nused);
265a84f3d5cSChristoph Hellwig to->bests = from->bests;
266a84f3d5cSChristoph Hellwig
2675ba30919SChristoph Hellwig ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
2685ba30919SChristoph Hellwig }
2695ba30919SChristoph Hellwig }
2705ba30919SChristoph Hellwig
271200dada7SChristoph Hellwig static void
xfs_dir2_free_hdr_to_disk(struct xfs_mount * mp,struct xfs_dir2_free * to,struct xfs_dir3_icfree_hdr * from)272200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(
273200dada7SChristoph Hellwig struct xfs_mount *mp,
274200dada7SChristoph Hellwig struct xfs_dir2_free *to,
275200dada7SChristoph Hellwig struct xfs_dir3_icfree_hdr *from)
276200dada7SChristoph Hellwig {
277ebd9027dSDave Chinner if (xfs_has_crc(mp)) {
278200dada7SChristoph Hellwig struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
279200dada7SChristoph Hellwig
280200dada7SChristoph Hellwig ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
281200dada7SChristoph Hellwig
282200dada7SChristoph Hellwig to3->hdr.hdr.magic = cpu_to_be32(from->magic);
283200dada7SChristoph Hellwig to3->hdr.firstdb = cpu_to_be32(from->firstdb);
284200dada7SChristoph Hellwig to3->hdr.nvalid = cpu_to_be32(from->nvalid);
285200dada7SChristoph Hellwig to3->hdr.nused = cpu_to_be32(from->nused);
286200dada7SChristoph Hellwig } else {
287200dada7SChristoph Hellwig ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
288200dada7SChristoph Hellwig
289200dada7SChristoph Hellwig to->hdr.magic = cpu_to_be32(from->magic);
290200dada7SChristoph Hellwig to->hdr.firstdb = cpu_to_be32(from->firstdb);
291200dada7SChristoph Hellwig to->hdr.nvalid = cpu_to_be32(from->nvalid);
292200dada7SChristoph Hellwig to->hdr.nused = cpu_to_be32(from->nused);
293200dada7SChristoph Hellwig }
294200dada7SChristoph Hellwig }
295200dada7SChristoph Hellwig
29630f712c9SDave Chinner int
xfs_dir2_free_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t fbno,struct xfs_buf ** bpp)29730f712c9SDave Chinner xfs_dir2_free_read(
29830f712c9SDave Chinner struct xfs_trans *tp,
29930f712c9SDave Chinner struct xfs_inode *dp,
30030f712c9SDave Chinner xfs_dablk_t fbno,
30130f712c9SDave Chinner struct xfs_buf **bpp)
30230f712c9SDave Chinner {
303cd2c9f1bSChristoph Hellwig return __xfs_dir3_free_read(tp, dp, fbno, 0, bpp);
30430f712c9SDave Chinner }
30530f712c9SDave Chinner
30630f712c9SDave Chinner static int
xfs_dir2_free_try_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t fbno,struct xfs_buf ** bpp)30730f712c9SDave Chinner xfs_dir2_free_try_read(
30830f712c9SDave Chinner struct xfs_trans *tp,
30930f712c9SDave Chinner struct xfs_inode *dp,
31030f712c9SDave Chinner xfs_dablk_t fbno,
31130f712c9SDave Chinner struct xfs_buf **bpp)
31230f712c9SDave Chinner {
313cd2c9f1bSChristoph Hellwig return __xfs_dir3_free_read(tp, dp, fbno, XFS_DABUF_MAP_HOLE_OK, bpp);
31430f712c9SDave Chinner }
31530f712c9SDave Chinner
31630f712c9SDave Chinner static int
xfs_dir3_free_get_buf(xfs_da_args_t * args,xfs_dir2_db_t fbno,struct xfs_buf ** bpp)31730f712c9SDave Chinner xfs_dir3_free_get_buf(
31830f712c9SDave Chinner xfs_da_args_t *args,
31930f712c9SDave Chinner xfs_dir2_db_t fbno,
32030f712c9SDave Chinner struct xfs_buf **bpp)
32130f712c9SDave Chinner {
32230f712c9SDave Chinner struct xfs_trans *tp = args->trans;
32330f712c9SDave Chinner struct xfs_inode *dp = args->dp;
32430f712c9SDave Chinner struct xfs_mount *mp = dp->i_mount;
32530f712c9SDave Chinner struct xfs_buf *bp;
32630f712c9SDave Chinner int error;
32730f712c9SDave Chinner struct xfs_dir3_icfree_hdr hdr;
32830f712c9SDave Chinner
32930f712c9SDave Chinner error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
3302911edb6SChristoph Hellwig &bp, XFS_DATA_FORK);
33130f712c9SDave Chinner if (error)
33230f712c9SDave Chinner return error;
33330f712c9SDave Chinner
33430f712c9SDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
33530f712c9SDave Chinner bp->b_ops = &xfs_dir3_free_buf_ops;
33630f712c9SDave Chinner
33730f712c9SDave Chinner /*
33830f712c9SDave Chinner * Initialize the new block to be empty, and remember
33930f712c9SDave Chinner * its first slot as our empty slot.
34030f712c9SDave Chinner */
34130f712c9SDave Chinner memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
34230f712c9SDave Chinner memset(&hdr, 0, sizeof(hdr));
34330f712c9SDave Chinner
34438c26bfdSDave Chinner if (xfs_has_crc(mp)) {
34530f712c9SDave Chinner struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
34630f712c9SDave Chinner
34730f712c9SDave Chinner hdr.magic = XFS_DIR3_FREE_MAGIC;
34830f712c9SDave Chinner
349*9343ee76SDave Chinner hdr3->hdr.blkno = cpu_to_be64(xfs_buf_daddr(bp));
35030f712c9SDave Chinner hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
351ce748eaaSEric Sandeen uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
35230f712c9SDave Chinner } else
35330f712c9SDave Chinner hdr.magic = XFS_DIR2_FREE_MAGIC;
354200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
35530f712c9SDave Chinner *bpp = bp;
35630f712c9SDave Chinner return 0;
35730f712c9SDave Chinner }
35830f712c9SDave Chinner
35930f712c9SDave Chinner /*
36030f712c9SDave Chinner * Log entries from a freespace block.
36130f712c9SDave Chinner */
36230f712c9SDave Chinner STATIC void
xfs_dir2_free_log_bests(struct xfs_da_args * args,struct xfs_dir3_icfree_hdr * hdr,struct xfs_buf * bp,int first,int last)36330f712c9SDave Chinner xfs_dir2_free_log_bests(
36430f712c9SDave Chinner struct xfs_da_args *args,
365a84f3d5cSChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr,
36630f712c9SDave Chinner struct xfs_buf *bp,
36730f712c9SDave Chinner int first, /* first entry to log */
36830f712c9SDave Chinner int last) /* last entry to log */
36930f712c9SDave Chinner {
370a84f3d5cSChristoph Hellwig struct xfs_dir2_free *free = bp->b_addr;
37130f712c9SDave Chinner
37230f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
37330f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
37430f712c9SDave Chinner xfs_trans_log_buf(args->trans, bp,
375a84f3d5cSChristoph Hellwig (char *)&hdr->bests[first] - (char *)free,
376a84f3d5cSChristoph Hellwig (char *)&hdr->bests[last] - (char *)free +
377a84f3d5cSChristoph Hellwig sizeof(hdr->bests[0]) - 1);
37830f712c9SDave Chinner }
37930f712c9SDave Chinner
38030f712c9SDave Chinner /*
38130f712c9SDave Chinner * Log header from a freespace block.
38230f712c9SDave Chinner */
38330f712c9SDave Chinner static void
xfs_dir2_free_log_header(struct xfs_da_args * args,struct xfs_buf * bp)38430f712c9SDave Chinner xfs_dir2_free_log_header(
38530f712c9SDave Chinner struct xfs_da_args *args,
38630f712c9SDave Chinner struct xfs_buf *bp)
38730f712c9SDave Chinner {
38830f712c9SDave Chinner #ifdef DEBUG
38930f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */
39030f712c9SDave Chinner
39130f712c9SDave Chinner free = bp->b_addr;
39230f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
39330f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
39430f712c9SDave Chinner #endif
39530f712c9SDave Chinner xfs_trans_log_buf(args->trans, bp, 0,
396ed1d612fSChristoph Hellwig args->geo->free_hdr_size - 1);
39730f712c9SDave Chinner }
39830f712c9SDave Chinner
39930f712c9SDave Chinner /*
40030f712c9SDave Chinner * Convert a leaf-format directory to a node-format directory.
40130f712c9SDave Chinner * We need to change the magic number of the leaf block, and copy
40230f712c9SDave Chinner * the freespace table out of the leaf block into its own block.
40330f712c9SDave Chinner */
40430f712c9SDave Chinner int /* error */
xfs_dir2_leaf_to_node(xfs_da_args_t * args,struct xfs_buf * lbp)40530f712c9SDave Chinner xfs_dir2_leaf_to_node(
40630f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */
40730f712c9SDave Chinner struct xfs_buf *lbp) /* leaf buffer */
40830f712c9SDave Chinner {
40930f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */
41030f712c9SDave Chinner int error; /* error return value */
41130f712c9SDave Chinner struct xfs_buf *fbp; /* freespace buffer */
41230f712c9SDave Chinner xfs_dir2_db_t fdb; /* freespace block number */
41330f712c9SDave Chinner __be16 *from; /* pointer to freespace entry */
41430f712c9SDave Chinner int i; /* leaf freespace index */
41530f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */
41630f712c9SDave Chinner xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
41730f712c9SDave Chinner int n; /* count of live freespc ents */
41830f712c9SDave Chinner xfs_dir2_data_off_t off; /* freespace entry value */
41930f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */
42030f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr;
42130f712c9SDave Chinner
42230f712c9SDave Chinner trace_xfs_dir2_leaf_to_node(args);
42330f712c9SDave Chinner
42430f712c9SDave Chinner dp = args->dp;
42530f712c9SDave Chinner tp = args->trans;
42630f712c9SDave Chinner /*
42730f712c9SDave Chinner * Add a freespace block to the directory.
42830f712c9SDave Chinner */
42930f712c9SDave Chinner if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
43030f712c9SDave Chinner return error;
43130f712c9SDave Chinner }
43230f712c9SDave Chinner ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
43330f712c9SDave Chinner /*
43430f712c9SDave Chinner * Get the buffer for the new freespace block.
43530f712c9SDave Chinner */
43630f712c9SDave Chinner error = xfs_dir3_free_get_buf(args, fdb, &fbp);
43730f712c9SDave Chinner if (error)
43830f712c9SDave Chinner return error;
43930f712c9SDave Chinner
440a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
44130f712c9SDave Chinner leaf = lbp->b_addr;
44230f712c9SDave Chinner ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
4433f883f5bSDarrick J. Wong if (be32_to_cpu(ltp->bestcount) >
44413d2c10bSChristoph Hellwig (uint)dp->i_disk_size / args->geo->blksize) {
4458d57c216SDarrick J. Wong xfs_buf_mark_corrupt(lbp);
4463f883f5bSDarrick J. Wong return -EFSCORRUPTED;
447a5155b87SDarrick J. Wong }
44830f712c9SDave Chinner
44930f712c9SDave Chinner /*
45030f712c9SDave Chinner * Copy freespace entries from the leaf block to the new block.
45130f712c9SDave Chinner * Count active entries.
45230f712c9SDave Chinner */
45330f712c9SDave Chinner from = xfs_dir2_leaf_bests_p(ltp);
454a84f3d5cSChristoph Hellwig for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
455a84f3d5cSChristoph Hellwig off = be16_to_cpu(*from);
456a84f3d5cSChristoph Hellwig if (off != NULLDATAOFF)
45730f712c9SDave Chinner n++;
458a84f3d5cSChristoph Hellwig freehdr.bests[i] = cpu_to_be16(off);
45930f712c9SDave Chinner }
46030f712c9SDave Chinner
46130f712c9SDave Chinner /*
46230f712c9SDave Chinner * Now initialize the freespace block header.
46330f712c9SDave Chinner */
46430f712c9SDave Chinner freehdr.nused = n;
46530f712c9SDave Chinner freehdr.nvalid = be32_to_cpu(ltp->bestcount);
46630f712c9SDave Chinner
467200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
468a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
46930f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp);
47030f712c9SDave Chinner
47130f712c9SDave Chinner /*
47230f712c9SDave Chinner * Converting the leaf to a leafnode is just a matter of changing the
47330f712c9SDave Chinner * magic number and the ops. Do the change directly to the buffer as
47430f712c9SDave Chinner * it's less work (and less code) than decoding the header to host
47530f712c9SDave Chinner * format and back again.
47630f712c9SDave Chinner */
47730f712c9SDave Chinner if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
47830f712c9SDave Chinner leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
47930f712c9SDave Chinner else
48030f712c9SDave Chinner leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
48130f712c9SDave Chinner lbp->b_ops = &xfs_dir3_leafn_buf_ops;
48230f712c9SDave Chinner xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
48330f712c9SDave Chinner xfs_dir3_leaf_log_header(args, lbp);
48430f712c9SDave Chinner xfs_dir3_leaf_check(dp, lbp);
48530f712c9SDave Chinner return 0;
48630f712c9SDave Chinner }
48730f712c9SDave Chinner
48830f712c9SDave Chinner /*
48930f712c9SDave Chinner * Add a leaf entry to a leaf block in a node-form directory.
49030f712c9SDave Chinner * The other work necessary is done from the caller.
49130f712c9SDave Chinner */
49230f712c9SDave Chinner static int /* error */
xfs_dir2_leafn_add(struct xfs_buf * bp,struct xfs_da_args * args,int index)49330f712c9SDave Chinner xfs_dir2_leafn_add(
49430f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */
49579622c7cSDarrick J. Wong struct xfs_da_args *args, /* operation arguments */
49630f712c9SDave Chinner int index) /* insertion pt for new entry */
49730f712c9SDave Chinner {
49879622c7cSDarrick J. Wong struct xfs_dir3_icleaf_hdr leafhdr;
49979622c7cSDarrick J. Wong struct xfs_inode *dp = args->dp;
50079622c7cSDarrick J. Wong struct xfs_dir2_leaf *leaf = bp->b_addr;
50179622c7cSDarrick J. Wong struct xfs_dir2_leaf_entry *lep;
50279622c7cSDarrick J. Wong struct xfs_dir2_leaf_entry *ents;
50330f712c9SDave Chinner int compact; /* compacting stale leaves */
50479622c7cSDarrick J. Wong int highstale = 0; /* next stale entry */
50530f712c9SDave Chinner int lfloghigh; /* high leaf entry logging */
50630f712c9SDave Chinner int lfloglow; /* low leaf entry logging */
50779622c7cSDarrick J. Wong int lowstale = 0; /* previous stale entry */
50830f712c9SDave Chinner
50930f712c9SDave Chinner trace_xfs_dir2_leafn_add(args, index);
51030f712c9SDave Chinner
51151842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
512787b0893SChristoph Hellwig ents = leafhdr.ents;
51330f712c9SDave Chinner
51430f712c9SDave Chinner /*
51530f712c9SDave Chinner * Quick check just to make sure we are not going to index
51630f712c9SDave Chinner * into other peoples memory
51730f712c9SDave Chinner */
518a5155b87SDarrick J. Wong if (index < 0) {
5198d57c216SDarrick J. Wong xfs_buf_mark_corrupt(bp);
5202451337dSDave Chinner return -EFSCORRUPTED;
521a5155b87SDarrick J. Wong }
52230f712c9SDave Chinner
52330f712c9SDave Chinner /*
52430f712c9SDave Chinner * If there are already the maximum number of leaf entries in
52530f712c9SDave Chinner * the block, if there are no stale entries it won't fit.
52630f712c9SDave Chinner * Caller will do a split. If there are stale entries we'll do
52730f712c9SDave Chinner * a compact.
52830f712c9SDave Chinner */
52930f712c9SDave Chinner
530478c7835SChristoph Hellwig if (leafhdr.count == args->geo->leaf_max_ents) {
53130f712c9SDave Chinner if (!leafhdr.stale)
5322451337dSDave Chinner return -ENOSPC;
53330f712c9SDave Chinner compact = leafhdr.stale > 1;
53430f712c9SDave Chinner } else
53530f712c9SDave Chinner compact = 0;
53630f712c9SDave Chinner ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
53730f712c9SDave Chinner ASSERT(index == leafhdr.count ||
53830f712c9SDave Chinner be32_to_cpu(ents[index].hashval) >= args->hashval);
53930f712c9SDave Chinner
54030f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_JUSTCHECK)
54130f712c9SDave Chinner return 0;
54230f712c9SDave Chinner
54330f712c9SDave Chinner /*
54430f712c9SDave Chinner * Compact out all but one stale leaf entry. Leaves behind
54530f712c9SDave Chinner * the entry closest to index.
54630f712c9SDave Chinner */
54730f712c9SDave Chinner if (compact)
54830f712c9SDave Chinner xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
54930f712c9SDave Chinner &highstale, &lfloglow, &lfloghigh);
55030f712c9SDave Chinner else if (leafhdr.stale) {
55130f712c9SDave Chinner /*
55230f712c9SDave Chinner * Set impossible logging indices for this case.
55330f712c9SDave Chinner */
55430f712c9SDave Chinner lfloglow = leafhdr.count;
55530f712c9SDave Chinner lfloghigh = -1;
55630f712c9SDave Chinner }
55730f712c9SDave Chinner
55830f712c9SDave Chinner /*
55930f712c9SDave Chinner * Insert the new entry, log everything.
56030f712c9SDave Chinner */
56130f712c9SDave Chinner lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
56230f712c9SDave Chinner highstale, &lfloglow, &lfloghigh);
56330f712c9SDave Chinner
56430f712c9SDave Chinner lep->hashval = cpu_to_be32(args->hashval);
56530f712c9SDave Chinner lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
56630f712c9SDave Chinner args->blkno, args->index));
56730f712c9SDave Chinner
568163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
56930f712c9SDave Chinner xfs_dir3_leaf_log_header(args, bp);
570787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
57130f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp);
57230f712c9SDave Chinner return 0;
57330f712c9SDave Chinner }
57430f712c9SDave Chinner
57530f712c9SDave Chinner #ifdef DEBUG
57630f712c9SDave Chinner static void
xfs_dir2_free_hdr_check(struct xfs_inode * dp,struct xfs_buf * bp,xfs_dir2_db_t db)57730f712c9SDave Chinner xfs_dir2_free_hdr_check(
57830f712c9SDave Chinner struct xfs_inode *dp,
57930f712c9SDave Chinner struct xfs_buf *bp,
58030f712c9SDave Chinner xfs_dir2_db_t db)
58130f712c9SDave Chinner {
58230f712c9SDave Chinner struct xfs_dir3_icfree_hdr hdr;
58330f712c9SDave Chinner
5845ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
58530f712c9SDave Chinner
5865893e4feSChristoph Hellwig ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
58730f712c9SDave Chinner ASSERT(hdr.firstdb <= db);
58830f712c9SDave Chinner ASSERT(db < hdr.firstdb + hdr.nvalid);
58930f712c9SDave Chinner }
59030f712c9SDave Chinner #else
59130f712c9SDave Chinner #define xfs_dir2_free_hdr_check(dp, bp, db)
59230f712c9SDave Chinner #endif /* DEBUG */
59330f712c9SDave Chinner
59430f712c9SDave Chinner /*
59530f712c9SDave Chinner * Return the last hash value in the leaf.
59630f712c9SDave Chinner * Stale entries are ok.
59730f712c9SDave Chinner */
59830f712c9SDave Chinner xfs_dahash_t /* hash value */
xfs_dir2_leaf_lasthash(struct xfs_inode * dp,struct xfs_buf * bp,int * count)5998e8877e6SDarrick J. Wong xfs_dir2_leaf_lasthash(
60030f712c9SDave Chinner struct xfs_inode *dp,
60130f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */
60230f712c9SDave Chinner int *count) /* count of entries in leaf */
60330f712c9SDave Chinner {
60430f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr;
60530f712c9SDave Chinner
606787b0893SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
60730f712c9SDave Chinner
60830f712c9SDave Chinner ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
6098e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
6108e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
6118e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
61230f712c9SDave Chinner
61330f712c9SDave Chinner if (count)
61430f712c9SDave Chinner *count = leafhdr.count;
61530f712c9SDave Chinner if (!leafhdr.count)
61630f712c9SDave Chinner return 0;
617787b0893SChristoph Hellwig return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval);
61830f712c9SDave Chinner }
61930f712c9SDave Chinner
62030f712c9SDave Chinner /*
62130f712c9SDave Chinner * Look up a leaf entry for space to add a name in a node-format leaf block.
62230f712c9SDave Chinner * The extrablk in state is a freespace block.
62330f712c9SDave Chinner */
62430f712c9SDave Chinner STATIC int
xfs_dir2_leafn_lookup_for_addname(struct xfs_buf * bp,xfs_da_args_t * args,int * indexp,xfs_da_state_t * state)62530f712c9SDave Chinner xfs_dir2_leafn_lookup_for_addname(
62630f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */
62730f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */
62830f712c9SDave Chinner int *indexp, /* out: leaf entry index */
62930f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */
63030f712c9SDave Chinner {
63130f712c9SDave Chinner struct xfs_buf *curbp = NULL; /* current data/free buffer */
63230f712c9SDave Chinner xfs_dir2_db_t curdb = -1; /* current data block number */
63330f712c9SDave Chinner xfs_dir2_db_t curfdb = -1; /* current free block number */
63430f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */
63530f712c9SDave Chinner int error; /* error return value */
63630f712c9SDave Chinner int fi; /* free entry index */
63730f712c9SDave Chinner xfs_dir2_free_t *free = NULL; /* free block structure */
63830f712c9SDave Chinner int index; /* leaf entry index */
63930f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */
64030f712c9SDave Chinner int length; /* length of new data entry */
64130f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */
64230f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */
64330f712c9SDave Chinner xfs_dir2_db_t newdb; /* new data block number */
64430f712c9SDave Chinner xfs_dir2_db_t newfdb; /* new free block number */
64530f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */
64630f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr;
64730f712c9SDave Chinner
64830f712c9SDave Chinner dp = args->dp;
64930f712c9SDave Chinner tp = args->trans;
65030f712c9SDave Chinner mp = dp->i_mount;
65130f712c9SDave Chinner leaf = bp->b_addr;
65251842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
65330f712c9SDave Chinner
65430f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp);
65530f712c9SDave Chinner ASSERT(leafhdr.count > 0);
65630f712c9SDave Chinner
65730f712c9SDave Chinner /*
65830f712c9SDave Chinner * Look up the hash value in the leaf entries.
65930f712c9SDave Chinner */
66030f712c9SDave Chinner index = xfs_dir2_leaf_search_hash(args, bp);
66130f712c9SDave Chinner /*
66230f712c9SDave Chinner * Do we have a buffer coming in?
66330f712c9SDave Chinner */
66430f712c9SDave Chinner if (state->extravalid) {
66530f712c9SDave Chinner /* If so, it's a free block buffer, get the block number. */
66630f712c9SDave Chinner curbp = state->extrablk.bp;
66730f712c9SDave Chinner curfdb = state->extrablk.blkno;
66830f712c9SDave Chinner free = curbp->b_addr;
66930f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
67030f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
67130f712c9SDave Chinner }
672fdbb8c5bSChristoph Hellwig length = xfs_dir2_data_entsize(mp, args->namelen);
67330f712c9SDave Chinner /*
67430f712c9SDave Chinner * Loop over leaf entries with the right hash value.
67530f712c9SDave Chinner */
676787b0893SChristoph Hellwig for (lep = &leafhdr.ents[index];
67730f712c9SDave Chinner index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
67830f712c9SDave Chinner lep++, index++) {
67930f712c9SDave Chinner /*
68030f712c9SDave Chinner * Skip stale leaf entries.
68130f712c9SDave Chinner */
68230f712c9SDave Chinner if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
68330f712c9SDave Chinner continue;
68430f712c9SDave Chinner /*
68530f712c9SDave Chinner * Pull the data block number from the entry.
68630f712c9SDave Chinner */
68730f712c9SDave Chinner newdb = xfs_dir2_dataptr_to_db(args->geo,
68830f712c9SDave Chinner be32_to_cpu(lep->address));
68930f712c9SDave Chinner /*
69030f712c9SDave Chinner * For addname, we're looking for a place to put the new entry.
69130f712c9SDave Chinner * We want to use a data block with an entry of equal
69230f712c9SDave Chinner * hash value to ours if there is one with room.
69330f712c9SDave Chinner *
69430f712c9SDave Chinner * If this block isn't the data block we already have
69530f712c9SDave Chinner * in hand, take a look at it.
69630f712c9SDave Chinner */
69730f712c9SDave Chinner if (newdb != curdb) {
698a84f3d5cSChristoph Hellwig struct xfs_dir3_icfree_hdr freehdr;
69930f712c9SDave Chinner
70030f712c9SDave Chinner curdb = newdb;
70130f712c9SDave Chinner /*
70230f712c9SDave Chinner * Convert the data block to the free block
70330f712c9SDave Chinner * holding its freespace information.
70430f712c9SDave Chinner */
7053d92c93bSChristoph Hellwig newfdb = xfs_dir2_db_to_fdb(args->geo, newdb);
70630f712c9SDave Chinner /*
70730f712c9SDave Chinner * If it's not the one we have in hand, read it in.
70830f712c9SDave Chinner */
70930f712c9SDave Chinner if (newfdb != curfdb) {
71030f712c9SDave Chinner /*
71130f712c9SDave Chinner * If we had one before, drop it.
71230f712c9SDave Chinner */
71330f712c9SDave Chinner if (curbp)
71430f712c9SDave Chinner xfs_trans_brelse(tp, curbp);
71530f712c9SDave Chinner
71630f712c9SDave Chinner error = xfs_dir2_free_read(tp, dp,
71730f712c9SDave Chinner xfs_dir2_db_to_da(args->geo,
71830f712c9SDave Chinner newfdb),
71930f712c9SDave Chinner &curbp);
72030f712c9SDave Chinner if (error)
72130f712c9SDave Chinner return error;
72230f712c9SDave Chinner free = curbp->b_addr;
72330f712c9SDave Chinner
72430f712c9SDave Chinner xfs_dir2_free_hdr_check(dp, curbp, curdb);
72530f712c9SDave Chinner }
72630f712c9SDave Chinner /*
72730f712c9SDave Chinner * Get the index for our entry.
72830f712c9SDave Chinner */
7293d92c93bSChristoph Hellwig fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
73030f712c9SDave Chinner /*
73130f712c9SDave Chinner * If it has room, return it.
73230f712c9SDave Chinner */
733a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
734a71895c5SDarrick J. Wong if (XFS_IS_CORRUPT(mp,
735a71895c5SDarrick J. Wong freehdr.bests[fi] ==
736a71895c5SDarrick J. Wong cpu_to_be16(NULLDATAOFF))) {
73730f712c9SDave Chinner if (curfdb != newfdb)
73830f712c9SDave Chinner xfs_trans_brelse(tp, curbp);
7392451337dSDave Chinner return -EFSCORRUPTED;
74030f712c9SDave Chinner }
74130f712c9SDave Chinner curfdb = newfdb;
742a84f3d5cSChristoph Hellwig if (be16_to_cpu(freehdr.bests[fi]) >= length)
74330f712c9SDave Chinner goto out;
74430f712c9SDave Chinner }
74530f712c9SDave Chinner }
74630f712c9SDave Chinner /* Didn't find any space */
74730f712c9SDave Chinner fi = -1;
74830f712c9SDave Chinner out:
74930f712c9SDave Chinner ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
75030f712c9SDave Chinner if (curbp) {
75130f712c9SDave Chinner /* Giving back a free block. */
75230f712c9SDave Chinner state->extravalid = 1;
75330f712c9SDave Chinner state->extrablk.bp = curbp;
75430f712c9SDave Chinner state->extrablk.index = fi;
75530f712c9SDave Chinner state->extrablk.blkno = curfdb;
75630f712c9SDave Chinner
75730f712c9SDave Chinner /*
75830f712c9SDave Chinner * Important: this magic number is not in the buffer - it's for
75930f712c9SDave Chinner * buffer type information and therefore only the free/data type
76030f712c9SDave Chinner * matters here, not whether CRCs are enabled or not.
76130f712c9SDave Chinner */
76230f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
76330f712c9SDave Chinner } else {
76430f712c9SDave Chinner state->extravalid = 0;
76530f712c9SDave Chinner }
76630f712c9SDave Chinner /*
76730f712c9SDave Chinner * Return the index, that will be the insertion point.
76830f712c9SDave Chinner */
76930f712c9SDave Chinner *indexp = index;
7702451337dSDave Chinner return -ENOENT;
77130f712c9SDave Chinner }
77230f712c9SDave Chinner
77330f712c9SDave Chinner /*
77430f712c9SDave Chinner * Look up a leaf entry in a node-format leaf block.
77530f712c9SDave Chinner * The extrablk in state a data block.
77630f712c9SDave Chinner */
77730f712c9SDave Chinner STATIC int
xfs_dir2_leafn_lookup_for_entry(struct xfs_buf * bp,xfs_da_args_t * args,int * indexp,xfs_da_state_t * state)77830f712c9SDave Chinner xfs_dir2_leafn_lookup_for_entry(
77930f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */
78030f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */
78130f712c9SDave Chinner int *indexp, /* out: leaf entry index */
78230f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */
78330f712c9SDave Chinner {
78430f712c9SDave Chinner struct xfs_buf *curbp = NULL; /* current data/free buffer */
78530f712c9SDave Chinner xfs_dir2_db_t curdb = -1; /* current data block number */
78630f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data block entry */
78730f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */
78830f712c9SDave Chinner int error; /* error return value */
78930f712c9SDave Chinner int index; /* leaf entry index */
79030f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */
79130f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */
79230f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */
79330f712c9SDave Chinner xfs_dir2_db_t newdb; /* new data block number */
79430f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */
79530f712c9SDave Chinner enum xfs_dacmp cmp; /* comparison result */
79630f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr;
79730f712c9SDave Chinner
79830f712c9SDave Chinner dp = args->dp;
79930f712c9SDave Chinner tp = args->trans;
80030f712c9SDave Chinner mp = dp->i_mount;
80130f712c9SDave Chinner leaf = bp->b_addr;
80251842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
80330f712c9SDave Chinner
80430f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp);
805a5155b87SDarrick J. Wong if (leafhdr.count <= 0) {
8068d57c216SDarrick J. Wong xfs_buf_mark_corrupt(bp);
807858b44dcSDarrick J. Wong return -EFSCORRUPTED;
808a5155b87SDarrick J. Wong }
80930f712c9SDave Chinner
81030f712c9SDave Chinner /*
81130f712c9SDave Chinner * Look up the hash value in the leaf entries.
81230f712c9SDave Chinner */
81330f712c9SDave Chinner index = xfs_dir2_leaf_search_hash(args, bp);
81430f712c9SDave Chinner /*
81530f712c9SDave Chinner * Do we have a buffer coming in?
81630f712c9SDave Chinner */
81730f712c9SDave Chinner if (state->extravalid) {
81830f712c9SDave Chinner curbp = state->extrablk.bp;
81930f712c9SDave Chinner curdb = state->extrablk.blkno;
82030f712c9SDave Chinner }
82130f712c9SDave Chinner /*
82230f712c9SDave Chinner * Loop over leaf entries with the right hash value.
82330f712c9SDave Chinner */
824787b0893SChristoph Hellwig for (lep = &leafhdr.ents[index];
82530f712c9SDave Chinner index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
82630f712c9SDave Chinner lep++, index++) {
82730f712c9SDave Chinner /*
82830f712c9SDave Chinner * Skip stale leaf entries.
82930f712c9SDave Chinner */
83030f712c9SDave Chinner if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
83130f712c9SDave Chinner continue;
83230f712c9SDave Chinner /*
83330f712c9SDave Chinner * Pull the data block number from the entry.
83430f712c9SDave Chinner */
83530f712c9SDave Chinner newdb = xfs_dir2_dataptr_to_db(args->geo,
83630f712c9SDave Chinner be32_to_cpu(lep->address));
83730f712c9SDave Chinner /*
83830f712c9SDave Chinner * Not adding a new entry, so we really want to find
83930f712c9SDave Chinner * the name given to us.
84030f712c9SDave Chinner *
84130f712c9SDave Chinner * If it's a different data block, go get it.
84230f712c9SDave Chinner */
84330f712c9SDave Chinner if (newdb != curdb) {
84430f712c9SDave Chinner /*
84530f712c9SDave Chinner * If we had a block before that we aren't saving
84630f712c9SDave Chinner * for a CI name, drop it
84730f712c9SDave Chinner */
84830f712c9SDave Chinner if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
84930f712c9SDave Chinner curdb != state->extrablk.blkno))
85030f712c9SDave Chinner xfs_trans_brelse(tp, curbp);
85130f712c9SDave Chinner /*
85230f712c9SDave Chinner * If needing the block that is saved with a CI match,
85330f712c9SDave Chinner * use it otherwise read in the new data block.
85430f712c9SDave Chinner */
85530f712c9SDave Chinner if (args->cmpresult != XFS_CMP_DIFFERENT &&
85630f712c9SDave Chinner newdb == state->extrablk.blkno) {
85730f712c9SDave Chinner ASSERT(state->extravalid);
85830f712c9SDave Chinner curbp = state->extrablk.bp;
85930f712c9SDave Chinner } else {
86030f712c9SDave Chinner error = xfs_dir3_data_read(tp, dp,
86130f712c9SDave Chinner xfs_dir2_db_to_da(args->geo,
86230f712c9SDave Chinner newdb),
863cd2c9f1bSChristoph Hellwig 0, &curbp);
86430f712c9SDave Chinner if (error)
86530f712c9SDave Chinner return error;
86630f712c9SDave Chinner }
86730f712c9SDave Chinner xfs_dir3_data_check(dp, curbp);
86830f712c9SDave Chinner curdb = newdb;
86930f712c9SDave Chinner }
87030f712c9SDave Chinner /*
87130f712c9SDave Chinner * Point to the data entry.
87230f712c9SDave Chinner */
87330f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
87430f712c9SDave Chinner xfs_dir2_dataptr_to_off(args->geo,
87530f712c9SDave Chinner be32_to_cpu(lep->address)));
87630f712c9SDave Chinner /*
87730f712c9SDave Chinner * Compare the entry and if it's an exact match, return
87830f712c9SDave Chinner * EEXIST immediately. If it's the first case-insensitive
87930f712c9SDave Chinner * match, store the block & inode number and continue looking.
88030f712c9SDave Chinner */
881d8d11fc7SChristoph Hellwig cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
88230f712c9SDave Chinner if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
88330f712c9SDave Chinner /* If there is a CI match block, drop it */
88430f712c9SDave Chinner if (args->cmpresult != XFS_CMP_DIFFERENT &&
88530f712c9SDave Chinner curdb != state->extrablk.blkno)
88630f712c9SDave Chinner xfs_trans_brelse(tp, state->extrablk.bp);
88730f712c9SDave Chinner args->cmpresult = cmp;
88830f712c9SDave Chinner args->inumber = be64_to_cpu(dep->inumber);
88959b8b465SChristoph Hellwig args->filetype = xfs_dir2_data_get_ftype(mp, dep);
89030f712c9SDave Chinner *indexp = index;
89130f712c9SDave Chinner state->extravalid = 1;
89230f712c9SDave Chinner state->extrablk.bp = curbp;
89330f712c9SDave Chinner state->extrablk.blkno = curdb;
89430f712c9SDave Chinner state->extrablk.index = (int)((char *)dep -
89530f712c9SDave Chinner (char *)curbp->b_addr);
89630f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
89730f712c9SDave Chinner curbp->b_ops = &xfs_dir3_data_buf_ops;
89830f712c9SDave Chinner xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
89930f712c9SDave Chinner if (cmp == XFS_CMP_EXACT)
9002451337dSDave Chinner return -EEXIST;
90130f712c9SDave Chinner }
90230f712c9SDave Chinner }
90330f712c9SDave Chinner ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
90430f712c9SDave Chinner if (curbp) {
90530f712c9SDave Chinner if (args->cmpresult == XFS_CMP_DIFFERENT) {
90630f712c9SDave Chinner /* Giving back last used data block. */
90730f712c9SDave Chinner state->extravalid = 1;
90830f712c9SDave Chinner state->extrablk.bp = curbp;
90930f712c9SDave Chinner state->extrablk.index = -1;
91030f712c9SDave Chinner state->extrablk.blkno = curdb;
91130f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
91230f712c9SDave Chinner curbp->b_ops = &xfs_dir3_data_buf_ops;
91330f712c9SDave Chinner xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
91430f712c9SDave Chinner } else {
91530f712c9SDave Chinner /* If the curbp is not the CI match block, drop it */
91630f712c9SDave Chinner if (state->extrablk.bp != curbp)
91730f712c9SDave Chinner xfs_trans_brelse(tp, curbp);
91830f712c9SDave Chinner }
91930f712c9SDave Chinner } else {
92030f712c9SDave Chinner state->extravalid = 0;
92130f712c9SDave Chinner }
92230f712c9SDave Chinner *indexp = index;
9232451337dSDave Chinner return -ENOENT;
92430f712c9SDave Chinner }
92530f712c9SDave Chinner
92630f712c9SDave Chinner /*
92730f712c9SDave Chinner * Look up a leaf entry in a node-format leaf block.
92830f712c9SDave Chinner * If this is an addname then the extrablk in state is a freespace block,
92930f712c9SDave Chinner * otherwise it's a data block.
93030f712c9SDave Chinner */
93130f712c9SDave Chinner int
xfs_dir2_leafn_lookup_int(struct xfs_buf * bp,xfs_da_args_t * args,int * indexp,xfs_da_state_t * state)93230f712c9SDave Chinner xfs_dir2_leafn_lookup_int(
93330f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */
93430f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */
93530f712c9SDave Chinner int *indexp, /* out: leaf entry index */
93630f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */
93730f712c9SDave Chinner {
93830f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_ADDNAME)
93930f712c9SDave Chinner return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
94030f712c9SDave Chinner state);
94130f712c9SDave Chinner return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
94230f712c9SDave Chinner }
94330f712c9SDave Chinner
94430f712c9SDave Chinner /*
94530f712c9SDave Chinner * Move count leaf entries from source to destination leaf.
94630f712c9SDave Chinner * Log entries and headers. Stale entries are preserved.
94730f712c9SDave Chinner */
94830f712c9SDave Chinner static void
xfs_dir3_leafn_moveents(xfs_da_args_t * args,struct xfs_buf * bp_s,struct xfs_dir3_icleaf_hdr * shdr,struct xfs_dir2_leaf_entry * sents,int start_s,struct xfs_buf * bp_d,struct xfs_dir3_icleaf_hdr * dhdr,struct xfs_dir2_leaf_entry * dents,int start_d,int count)94930f712c9SDave Chinner xfs_dir3_leafn_moveents(
95030f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */
95130f712c9SDave Chinner struct xfs_buf *bp_s, /* source */
95230f712c9SDave Chinner struct xfs_dir3_icleaf_hdr *shdr,
95330f712c9SDave Chinner struct xfs_dir2_leaf_entry *sents,
95430f712c9SDave Chinner int start_s,/* source leaf index */
95530f712c9SDave Chinner struct xfs_buf *bp_d, /* destination */
95630f712c9SDave Chinner struct xfs_dir3_icleaf_hdr *dhdr,
95730f712c9SDave Chinner struct xfs_dir2_leaf_entry *dents,
95830f712c9SDave Chinner int start_d,/* destination leaf index */
95930f712c9SDave Chinner int count) /* count of leaves to copy */
96030f712c9SDave Chinner {
96130f712c9SDave Chinner int stale; /* count stale leaves copied */
96230f712c9SDave Chinner
96330f712c9SDave Chinner trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
96430f712c9SDave Chinner
96530f712c9SDave Chinner /*
96630f712c9SDave Chinner * Silently return if nothing to do.
96730f712c9SDave Chinner */
96830f712c9SDave Chinner if (count == 0)
96930f712c9SDave Chinner return;
97030f712c9SDave Chinner
97130f712c9SDave Chinner /*
97230f712c9SDave Chinner * If the destination index is not the end of the current
97330f712c9SDave Chinner * destination leaf entries, open up a hole in the destination
97430f712c9SDave Chinner * to hold the new entries.
97530f712c9SDave Chinner */
97630f712c9SDave Chinner if (start_d < dhdr->count) {
97730f712c9SDave Chinner memmove(&dents[start_d + count], &dents[start_d],
97830f712c9SDave Chinner (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
979787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
98030f712c9SDave Chinner count + dhdr->count - 1);
98130f712c9SDave Chinner }
98230f712c9SDave Chinner /*
98330f712c9SDave Chinner * If the source has stale leaves, count the ones in the copy range
98430f712c9SDave Chinner * so we can update the header correctly.
98530f712c9SDave Chinner */
98630f712c9SDave Chinner if (shdr->stale) {
98730f712c9SDave Chinner int i; /* temp leaf index */
98830f712c9SDave Chinner
98930f712c9SDave Chinner for (i = start_s, stale = 0; i < start_s + count; i++) {
99030f712c9SDave Chinner if (sents[i].address ==
99130f712c9SDave Chinner cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
99230f712c9SDave Chinner stale++;
99330f712c9SDave Chinner }
99430f712c9SDave Chinner } else
99530f712c9SDave Chinner stale = 0;
99630f712c9SDave Chinner /*
99730f712c9SDave Chinner * Copy the leaf entries from source to destination.
99830f712c9SDave Chinner */
99930f712c9SDave Chinner memcpy(&dents[start_d], &sents[start_s],
100030f712c9SDave Chinner count * sizeof(xfs_dir2_leaf_entry_t));
1001787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
100230f712c9SDave Chinner
100330f712c9SDave Chinner /*
100430f712c9SDave Chinner * If there are source entries after the ones we copied,
100530f712c9SDave Chinner * delete the ones we copied by sliding the next ones down.
100630f712c9SDave Chinner */
100730f712c9SDave Chinner if (start_s + count < shdr->count) {
100830f712c9SDave Chinner memmove(&sents[start_s], &sents[start_s + count],
100930f712c9SDave Chinner count * sizeof(xfs_dir2_leaf_entry_t));
1010787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
1011787b0893SChristoph Hellwig start_s + count - 1);
101230f712c9SDave Chinner }
101330f712c9SDave Chinner
101430f712c9SDave Chinner /*
101530f712c9SDave Chinner * Update the headers and log them.
101630f712c9SDave Chinner */
101730f712c9SDave Chinner shdr->count -= count;
101830f712c9SDave Chinner shdr->stale -= stale;
101930f712c9SDave Chinner dhdr->count += count;
102030f712c9SDave Chinner dhdr->stale += stale;
102130f712c9SDave Chinner }
102230f712c9SDave Chinner
102330f712c9SDave Chinner /*
102430f712c9SDave Chinner * Determine the sort order of two leaf blocks.
102530f712c9SDave Chinner * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
102630f712c9SDave Chinner */
102730f712c9SDave Chinner int /* sort order */
xfs_dir2_leafn_order(struct xfs_inode * dp,struct xfs_buf * leaf1_bp,struct xfs_buf * leaf2_bp)102830f712c9SDave Chinner xfs_dir2_leafn_order(
102930f712c9SDave Chinner struct xfs_inode *dp,
103030f712c9SDave Chinner struct xfs_buf *leaf1_bp, /* leaf1 buffer */
103130f712c9SDave Chinner struct xfs_buf *leaf2_bp) /* leaf2 buffer */
103230f712c9SDave Chinner {
103330f712c9SDave Chinner struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
103430f712c9SDave Chinner struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
103530f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents1;
103630f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents2;
103730f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr1;
103830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2;
103930f712c9SDave Chinner
104051842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
104151842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1042787b0893SChristoph Hellwig ents1 = hdr1.ents;
1043787b0893SChristoph Hellwig ents2 = hdr2.ents;
104430f712c9SDave Chinner
104530f712c9SDave Chinner if (hdr1.count > 0 && hdr2.count > 0 &&
104630f712c9SDave Chinner (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
104730f712c9SDave Chinner be32_to_cpu(ents2[hdr2.count - 1].hashval) <
104830f712c9SDave Chinner be32_to_cpu(ents1[hdr1.count - 1].hashval)))
104930f712c9SDave Chinner return 1;
105030f712c9SDave Chinner return 0;
105130f712c9SDave Chinner }
105230f712c9SDave Chinner
105330f712c9SDave Chinner /*
105430f712c9SDave Chinner * Rebalance leaf entries between two leaf blocks.
105530f712c9SDave Chinner * This is actually only called when the second block is new,
105630f712c9SDave Chinner * though the code deals with the general case.
105730f712c9SDave Chinner * A new entry will be inserted in one of the blocks, and that
105830f712c9SDave Chinner * entry is taken into account when balancing.
105930f712c9SDave Chinner */
106030f712c9SDave Chinner static void
xfs_dir2_leafn_rebalance(xfs_da_state_t * state,xfs_da_state_blk_t * blk1,xfs_da_state_blk_t * blk2)106130f712c9SDave Chinner xfs_dir2_leafn_rebalance(
106230f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */
106330f712c9SDave Chinner xfs_da_state_blk_t *blk1, /* first btree block */
106430f712c9SDave Chinner xfs_da_state_blk_t *blk2) /* second btree block */
106530f712c9SDave Chinner {
106630f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */
106730f712c9SDave Chinner int count; /* count (& direction) leaves */
106830f712c9SDave Chinner int isleft; /* new goes in left leaf */
106930f712c9SDave Chinner xfs_dir2_leaf_t *leaf1; /* first leaf structure */
107030f712c9SDave Chinner xfs_dir2_leaf_t *leaf2; /* second leaf structure */
107130f712c9SDave Chinner int mid; /* midpoint leaf index */
107230f712c9SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
107330f712c9SDave Chinner int oldstale; /* old count of stale leaves */
107430f712c9SDave Chinner #endif
107530f712c9SDave Chinner int oldsum; /* old total leaf count */
1076e4e542a6SGustavo A. R. Silva int swap_blocks; /* swapped leaf blocks */
107730f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents1;
107830f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents2;
107930f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr1;
108030f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2;
108130f712c9SDave Chinner struct xfs_inode *dp = state->args->dp;
108230f712c9SDave Chinner
108330f712c9SDave Chinner args = state->args;
108430f712c9SDave Chinner /*
108530f712c9SDave Chinner * If the block order is wrong, swap the arguments.
108630f712c9SDave Chinner */
1087e4e542a6SGustavo A. R. Silva swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1088e4e542a6SGustavo A. R. Silva if (swap_blocks)
1089e4e542a6SGustavo A. R. Silva swap(blk1, blk2);
109030f712c9SDave Chinner
109130f712c9SDave Chinner leaf1 = blk1->bp->b_addr;
109230f712c9SDave Chinner leaf2 = blk2->bp->b_addr;
109351842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
109451842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1095787b0893SChristoph Hellwig ents1 = hdr1.ents;
1096787b0893SChristoph Hellwig ents2 = hdr2.ents;
109730f712c9SDave Chinner
109830f712c9SDave Chinner oldsum = hdr1.count + hdr2.count;
109930f712c9SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
110030f712c9SDave Chinner oldstale = hdr1.stale + hdr2.stale;
110130f712c9SDave Chinner #endif
110230f712c9SDave Chinner mid = oldsum >> 1;
110330f712c9SDave Chinner
110430f712c9SDave Chinner /*
110530f712c9SDave Chinner * If the old leaf count was odd then the new one will be even,
110630f712c9SDave Chinner * so we need to divide the new count evenly.
110730f712c9SDave Chinner */
110830f712c9SDave Chinner if (oldsum & 1) {
110930f712c9SDave Chinner xfs_dahash_t midhash; /* middle entry hash value */
111030f712c9SDave Chinner
111130f712c9SDave Chinner if (mid >= hdr1.count)
111230f712c9SDave Chinner midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
111330f712c9SDave Chinner else
111430f712c9SDave Chinner midhash = be32_to_cpu(ents1[mid].hashval);
111530f712c9SDave Chinner isleft = args->hashval <= midhash;
111630f712c9SDave Chinner }
111730f712c9SDave Chinner /*
111830f712c9SDave Chinner * If the old count is even then the new count is odd, so there's
111930f712c9SDave Chinner * no preferred side for the new entry.
112030f712c9SDave Chinner * Pick the left one.
112130f712c9SDave Chinner */
112230f712c9SDave Chinner else
112330f712c9SDave Chinner isleft = 1;
112430f712c9SDave Chinner /*
112530f712c9SDave Chinner * Calculate moved entry count. Positive means left-to-right,
112630f712c9SDave Chinner * negative means right-to-left. Then move the entries.
112730f712c9SDave Chinner */
112830f712c9SDave Chinner count = hdr1.count - mid + (isleft == 0);
112930f712c9SDave Chinner if (count > 0)
113030f712c9SDave Chinner xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
113130f712c9SDave Chinner hdr1.count - count, blk2->bp,
113230f712c9SDave Chinner &hdr2, ents2, 0, count);
113330f712c9SDave Chinner else if (count < 0)
113430f712c9SDave Chinner xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
113530f712c9SDave Chinner blk1->bp, &hdr1, ents1,
113630f712c9SDave Chinner hdr1.count, count);
113730f712c9SDave Chinner
113830f712c9SDave Chinner ASSERT(hdr1.count + hdr2.count == oldsum);
113930f712c9SDave Chinner ASSERT(hdr1.stale + hdr2.stale == oldstale);
114030f712c9SDave Chinner
114130f712c9SDave Chinner /* log the changes made when moving the entries */
1142163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1143163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
114430f712c9SDave Chinner xfs_dir3_leaf_log_header(args, blk1->bp);
114530f712c9SDave Chinner xfs_dir3_leaf_log_header(args, blk2->bp);
114630f712c9SDave Chinner
114730f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk1->bp);
114830f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk2->bp);
114930f712c9SDave Chinner
115030f712c9SDave Chinner /*
115130f712c9SDave Chinner * Mark whether we're inserting into the old or new leaf.
115230f712c9SDave Chinner */
115330f712c9SDave Chinner if (hdr1.count < hdr2.count)
1154e4e542a6SGustavo A. R. Silva state->inleaf = swap_blocks;
115530f712c9SDave Chinner else if (hdr1.count > hdr2.count)
1156e4e542a6SGustavo A. R. Silva state->inleaf = !swap_blocks;
115730f712c9SDave Chinner else
1158e4e542a6SGustavo A. R. Silva state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
115930f712c9SDave Chinner /*
116030f712c9SDave Chinner * Adjust the expected index for insertion.
116130f712c9SDave Chinner */
116230f712c9SDave Chinner if (!state->inleaf)
116330f712c9SDave Chinner blk2->index = blk1->index - hdr1.count;
116430f712c9SDave Chinner
116530f712c9SDave Chinner /*
116630f712c9SDave Chinner * Finally sanity check just to make sure we are not returning a
116730f712c9SDave Chinner * negative index
116830f712c9SDave Chinner */
116930f712c9SDave Chinner if (blk2->index < 0) {
117030f712c9SDave Chinner state->inleaf = 1;
117130f712c9SDave Chinner blk2->index = 0;
117230f712c9SDave Chinner xfs_alert(dp->i_mount,
117330f712c9SDave Chinner "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
117430f712c9SDave Chinner __func__, blk1->index);
117530f712c9SDave Chinner }
117630f712c9SDave Chinner }
117730f712c9SDave Chinner
117830f712c9SDave Chinner static int
xfs_dir3_data_block_free(xfs_da_args_t * args,struct xfs_dir2_data_hdr * hdr,struct xfs_dir2_free * free,xfs_dir2_db_t fdb,int findex,struct xfs_buf * fbp,int longest)117930f712c9SDave Chinner xfs_dir3_data_block_free(
118030f712c9SDave Chinner xfs_da_args_t *args,
118130f712c9SDave Chinner struct xfs_dir2_data_hdr *hdr,
118230f712c9SDave Chinner struct xfs_dir2_free *free,
118330f712c9SDave Chinner xfs_dir2_db_t fdb,
118430f712c9SDave Chinner int findex,
118530f712c9SDave Chinner struct xfs_buf *fbp,
118630f712c9SDave Chinner int longest)
118730f712c9SDave Chinner {
118830f712c9SDave Chinner int logfree = 0;
118930f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr;
119030f712c9SDave Chinner struct xfs_inode *dp = args->dp;
119130f712c9SDave Chinner
11925ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
119330f712c9SDave Chinner if (hdr) {
119430f712c9SDave Chinner /*
119530f712c9SDave Chinner * Data block is not empty, just set the free entry to the new
119630f712c9SDave Chinner * value.
119730f712c9SDave Chinner */
1198a84f3d5cSChristoph Hellwig freehdr.bests[findex] = cpu_to_be16(longest);
1199a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
120030f712c9SDave Chinner return 0;
120130f712c9SDave Chinner }
120230f712c9SDave Chinner
120330f712c9SDave Chinner /* One less used entry in the free table. */
120430f712c9SDave Chinner freehdr.nused--;
120530f712c9SDave Chinner
120630f712c9SDave Chinner /*
120730f712c9SDave Chinner * If this was the last entry in the table, we can trim the table size
120830f712c9SDave Chinner * back. There might be other entries at the end referring to
120930f712c9SDave Chinner * non-existent data blocks, get those too.
121030f712c9SDave Chinner */
121130f712c9SDave Chinner if (findex == freehdr.nvalid - 1) {
121230f712c9SDave Chinner int i; /* free entry index */
121330f712c9SDave Chinner
121430f712c9SDave Chinner for (i = findex - 1; i >= 0; i--) {
1215a84f3d5cSChristoph Hellwig if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
121630f712c9SDave Chinner break;
121730f712c9SDave Chinner }
121830f712c9SDave Chinner freehdr.nvalid = i + 1;
121930f712c9SDave Chinner logfree = 0;
122030f712c9SDave Chinner } else {
122130f712c9SDave Chinner /* Not the last entry, just punch it out. */
1222a84f3d5cSChristoph Hellwig freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
122330f712c9SDave Chinner logfree = 1;
122430f712c9SDave Chinner }
122530f712c9SDave Chinner
1226200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
122730f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp);
122830f712c9SDave Chinner
122930f712c9SDave Chinner /*
123030f712c9SDave Chinner * If there are no useful entries left in the block, get rid of the
123130f712c9SDave Chinner * block if we can.
123230f712c9SDave Chinner */
123330f712c9SDave Chinner if (!freehdr.nused) {
123430f712c9SDave Chinner int error;
123530f712c9SDave Chinner
123630f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, fdb, fbp);
123730f712c9SDave Chinner if (error == 0) {
123830f712c9SDave Chinner fbp = NULL;
123930f712c9SDave Chinner logfree = 0;
12402451337dSDave Chinner } else if (error != -ENOSPC || args->total != 0)
124130f712c9SDave Chinner return error;
124230f712c9SDave Chinner /*
124330f712c9SDave Chinner * It's possible to get ENOSPC if there is no
124430f712c9SDave Chinner * space reservation. In this case some one
124530f712c9SDave Chinner * else will eventually get rid of this block.
124630f712c9SDave Chinner */
124730f712c9SDave Chinner }
124830f712c9SDave Chinner
124930f712c9SDave Chinner /* Log the free entry that changed, unless we got rid of it. */
125030f712c9SDave Chinner if (logfree)
1251a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
125230f712c9SDave Chinner return 0;
125330f712c9SDave Chinner }
125430f712c9SDave Chinner
125530f712c9SDave Chinner /*
125630f712c9SDave Chinner * Remove an entry from a node directory.
125730f712c9SDave Chinner * This removes the leaf entry and the data entry,
125830f712c9SDave Chinner * and updates the free block if necessary.
125930f712c9SDave Chinner */
126030f712c9SDave Chinner static int /* error */
xfs_dir2_leafn_remove(xfs_da_args_t * args,struct xfs_buf * bp,int index,xfs_da_state_blk_t * dblk,int * rval)126130f712c9SDave Chinner xfs_dir2_leafn_remove(
126230f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */
126330f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */
126430f712c9SDave Chinner int index, /* leaf entry index */
126530f712c9SDave Chinner xfs_da_state_blk_t *dblk, /* data block */
126630f712c9SDave Chinner int *rval) /* resulting block needs join */
126730f712c9SDave Chinner {
1268d73e1ceeSChristoph Hellwig struct xfs_da_geometry *geo = args->geo;
126930f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */
127030f712c9SDave Chinner xfs_dir2_db_t db; /* data block number */
127130f712c9SDave Chinner struct xfs_buf *dbp; /* data block buffer */
127230f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data block entry */
127330f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */
127430f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */
127530f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */
127630f712c9SDave Chinner int longest; /* longest data free entry */
127730f712c9SDave Chinner int off; /* data block entry offset */
127830f712c9SDave Chinner int needlog; /* need to log data header */
127930f712c9SDave Chinner int needscan; /* need to rescan data frees */
128030f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */
128130f712c9SDave Chinner struct xfs_dir2_data_free *bf; /* bestfree table */
128230f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr;
128330f712c9SDave Chinner
128430f712c9SDave Chinner trace_xfs_dir2_leafn_remove(args, index);
128530f712c9SDave Chinner
128630f712c9SDave Chinner dp = args->dp;
128730f712c9SDave Chinner tp = args->trans;
128830f712c9SDave Chinner leaf = bp->b_addr;
128951842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
129030f712c9SDave Chinner
129130f712c9SDave Chinner /*
129230f712c9SDave Chinner * Point to the entry we're removing.
129330f712c9SDave Chinner */
1294787b0893SChristoph Hellwig lep = &leafhdr.ents[index];
129530f712c9SDave Chinner
129630f712c9SDave Chinner /*
129730f712c9SDave Chinner * Extract the data block and offset from the entry.
129830f712c9SDave Chinner */
1299d73e1ceeSChristoph Hellwig db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
130030f712c9SDave Chinner ASSERT(dblk->blkno == db);
1301d73e1ceeSChristoph Hellwig off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
130230f712c9SDave Chinner ASSERT(dblk->index == off);
130330f712c9SDave Chinner
130430f712c9SDave Chinner /*
130530f712c9SDave Chinner * Kill the leaf entry by marking it stale.
130630f712c9SDave Chinner * Log the leaf block changes.
130730f712c9SDave Chinner */
130830f712c9SDave Chinner leafhdr.stale++;
1309163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
131030f712c9SDave Chinner xfs_dir3_leaf_log_header(args, bp);
131130f712c9SDave Chinner
131230f712c9SDave Chinner lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1313787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
131430f712c9SDave Chinner
131530f712c9SDave Chinner /*
131630f712c9SDave Chinner * Make the data entry free. Keep track of the longest freespace
131730f712c9SDave Chinner * in the data block in case it changes.
131830f712c9SDave Chinner */
131930f712c9SDave Chinner dbp = dblk->bp;
132030f712c9SDave Chinner hdr = dbp->b_addr;
132130f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
13221848b607SChristoph Hellwig bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
132330f712c9SDave Chinner longest = be16_to_cpu(bf[0].length);
132430f712c9SDave Chinner needlog = needscan = 0;
132530f712c9SDave Chinner xfs_dir2_data_make_free(args, dbp, off,
1326fdbb8c5bSChristoph Hellwig xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
1327fdbb8c5bSChristoph Hellwig &needscan);
132830f712c9SDave Chinner /*
132930f712c9SDave Chinner * Rescan the data block freespaces for bestfree.
133030f712c9SDave Chinner * Log the data block header if needed.
133130f712c9SDave Chinner */
133230f712c9SDave Chinner if (needscan)
1333ae42976dSChristoph Hellwig xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
133430f712c9SDave Chinner if (needlog)
133530f712c9SDave Chinner xfs_dir2_data_log_header(args, dbp);
133630f712c9SDave Chinner xfs_dir3_data_check(dp, dbp);
133730f712c9SDave Chinner /*
133830f712c9SDave Chinner * If the longest data block freespace changes, need to update
133930f712c9SDave Chinner * the corresponding freeblock entry.
134030f712c9SDave Chinner */
134130f712c9SDave Chinner if (longest < be16_to_cpu(bf[0].length)) {
134230f712c9SDave Chinner int error; /* error return value */
134330f712c9SDave Chinner struct xfs_buf *fbp; /* freeblock buffer */
134430f712c9SDave Chinner xfs_dir2_db_t fdb; /* freeblock block number */
134530f712c9SDave Chinner int findex; /* index in freeblock entries */
134630f712c9SDave Chinner xfs_dir2_free_t *free; /* freeblock structure */
134730f712c9SDave Chinner
134830f712c9SDave Chinner /*
134930f712c9SDave Chinner * Convert the data block number to a free block,
135030f712c9SDave Chinner * read in the free block.
135130f712c9SDave Chinner */
1352d73e1ceeSChristoph Hellwig fdb = xfs_dir2_db_to_fdb(geo, db);
1353d73e1ceeSChristoph Hellwig error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(geo, fdb),
135430f712c9SDave Chinner &fbp);
135530f712c9SDave Chinner if (error)
135630f712c9SDave Chinner return error;
135730f712c9SDave Chinner free = fbp->b_addr;
135830f712c9SDave Chinner #ifdef DEBUG
135930f712c9SDave Chinner {
136030f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr;
13615ba30919SChristoph Hellwig
13625ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1363d73e1ceeSChristoph Hellwig ASSERT(freehdr.firstdb == geo->free_max_bests *
1364d73e1ceeSChristoph Hellwig (fdb - xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET)));
136530f712c9SDave Chinner }
136630f712c9SDave Chinner #endif
136730f712c9SDave Chinner /*
136830f712c9SDave Chinner * Calculate which entry we need to fix.
136930f712c9SDave Chinner */
1370d73e1ceeSChristoph Hellwig findex = xfs_dir2_db_to_fdindex(geo, db);
137130f712c9SDave Chinner longest = be16_to_cpu(bf[0].length);
137230f712c9SDave Chinner /*
137330f712c9SDave Chinner * If the data block is now empty we can get rid of it
137430f712c9SDave Chinner * (usually).
137530f712c9SDave Chinner */
1376d73e1ceeSChristoph Hellwig if (longest == geo->blksize - geo->data_entry_offset) {
137730f712c9SDave Chinner /*
137830f712c9SDave Chinner * Try to punch out the data block.
137930f712c9SDave Chinner */
138030f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, db, dbp);
138130f712c9SDave Chinner if (error == 0) {
138230f712c9SDave Chinner dblk->bp = NULL;
138330f712c9SDave Chinner hdr = NULL;
138430f712c9SDave Chinner }
138530f712c9SDave Chinner /*
138630f712c9SDave Chinner * We can get ENOSPC if there's no space reservation.
138730f712c9SDave Chinner * In this case just drop the buffer and some one else
138830f712c9SDave Chinner * will eventually get rid of the empty block.
138930f712c9SDave Chinner */
13902451337dSDave Chinner else if (!(error == -ENOSPC && args->total == 0))
139130f712c9SDave Chinner return error;
139230f712c9SDave Chinner }
139330f712c9SDave Chinner /*
139430f712c9SDave Chinner * If we got rid of the data block, we can eliminate that entry
139530f712c9SDave Chinner * in the free block.
139630f712c9SDave Chinner */
139730f712c9SDave Chinner error = xfs_dir3_data_block_free(args, hdr, free,
139830f712c9SDave Chinner fdb, findex, fbp, longest);
139930f712c9SDave Chinner if (error)
140030f712c9SDave Chinner return error;
140130f712c9SDave Chinner }
140230f712c9SDave Chinner
140330f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp);
140430f712c9SDave Chinner /*
140530f712c9SDave Chinner * Return indication of whether this leaf block is empty enough
140630f712c9SDave Chinner * to justify trying to join it with a neighbor.
140730f712c9SDave Chinner */
1408d73e1ceeSChristoph Hellwig *rval = (geo->leaf_hdr_size +
1409787b0893SChristoph Hellwig (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
1410d73e1ceeSChristoph Hellwig geo->magicpct;
141130f712c9SDave Chinner return 0;
141230f712c9SDave Chinner }
141330f712c9SDave Chinner
141430f712c9SDave Chinner /*
141530f712c9SDave Chinner * Split the leaf entries in the old block into old and new blocks.
141630f712c9SDave Chinner */
141730f712c9SDave Chinner int /* error */
xfs_dir2_leafn_split(xfs_da_state_t * state,xfs_da_state_blk_t * oldblk,xfs_da_state_blk_t * newblk)141830f712c9SDave Chinner xfs_dir2_leafn_split(
141930f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */
142030f712c9SDave Chinner xfs_da_state_blk_t *oldblk, /* original block */
142130f712c9SDave Chinner xfs_da_state_blk_t *newblk) /* newly created block */
142230f712c9SDave Chinner {
142330f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */
142430f712c9SDave Chinner xfs_dablk_t blkno; /* new leaf block number */
142530f712c9SDave Chinner int error; /* error return value */
142630f712c9SDave Chinner struct xfs_inode *dp;
142730f712c9SDave Chinner
142830f712c9SDave Chinner /*
142930f712c9SDave Chinner * Allocate space for a new leaf node.
143030f712c9SDave Chinner */
143130f712c9SDave Chinner args = state->args;
143230f712c9SDave Chinner dp = args->dp;
143330f712c9SDave Chinner ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
143430f712c9SDave Chinner error = xfs_da_grow_inode(args, &blkno);
143530f712c9SDave Chinner if (error) {
143630f712c9SDave Chinner return error;
143730f712c9SDave Chinner }
143830f712c9SDave Chinner /*
143930f712c9SDave Chinner * Initialize the new leaf block.
144030f712c9SDave Chinner */
144130f712c9SDave Chinner error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
144230f712c9SDave Chinner &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
144330f712c9SDave Chinner if (error)
144430f712c9SDave Chinner return error;
144530f712c9SDave Chinner
144630f712c9SDave Chinner newblk->blkno = blkno;
144730f712c9SDave Chinner newblk->magic = XFS_DIR2_LEAFN_MAGIC;
144830f712c9SDave Chinner /*
144930f712c9SDave Chinner * Rebalance the entries across the two leaves, link the new
145030f712c9SDave Chinner * block into the leaves.
145130f712c9SDave Chinner */
145230f712c9SDave Chinner xfs_dir2_leafn_rebalance(state, oldblk, newblk);
145330f712c9SDave Chinner error = xfs_da3_blk_link(state, oldblk, newblk);
145430f712c9SDave Chinner if (error) {
145530f712c9SDave Chinner return error;
145630f712c9SDave Chinner }
145730f712c9SDave Chinner /*
145830f712c9SDave Chinner * Insert the new entry in the correct block.
145930f712c9SDave Chinner */
146030f712c9SDave Chinner if (state->inleaf)
146130f712c9SDave Chinner error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
146230f712c9SDave Chinner else
146330f712c9SDave Chinner error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
146430f712c9SDave Chinner /*
146530f712c9SDave Chinner * Update last hashval in each block since we added the name.
146630f712c9SDave Chinner */
14678e8877e6SDarrick J. Wong oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
14688e8877e6SDarrick J. Wong newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
146930f712c9SDave Chinner xfs_dir3_leaf_check(dp, oldblk->bp);
147030f712c9SDave Chinner xfs_dir3_leaf_check(dp, newblk->bp);
147130f712c9SDave Chinner return error;
147230f712c9SDave Chinner }
147330f712c9SDave Chinner
147430f712c9SDave Chinner /*
147530f712c9SDave Chinner * Check a leaf block and its neighbors to see if the block should be
147630f712c9SDave Chinner * collapsed into one or the other neighbor. Always keep the block
147730f712c9SDave Chinner * with the smaller block number.
147830f712c9SDave Chinner * If the current block is over 50% full, don't try to join it, return 0.
147930f712c9SDave Chinner * If the block is empty, fill in the state structure and return 2.
148030f712c9SDave Chinner * If it can be collapsed, fill in the state structure and return 1.
148130f712c9SDave Chinner * If nothing can be done, return 0.
148230f712c9SDave Chinner */
148330f712c9SDave Chinner int /* error */
xfs_dir2_leafn_toosmall(xfs_da_state_t * state,int * action)148430f712c9SDave Chinner xfs_dir2_leafn_toosmall(
148530f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */
148630f712c9SDave Chinner int *action) /* resulting action to take */
148730f712c9SDave Chinner {
148830f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block */
148930f712c9SDave Chinner xfs_dablk_t blkno; /* leaf block number */
149030f712c9SDave Chinner struct xfs_buf *bp; /* leaf buffer */
149130f712c9SDave Chinner int bytes; /* bytes in use */
149230f712c9SDave Chinner int count; /* leaf live entry count */
149330f712c9SDave Chinner int error; /* error return value */
149430f712c9SDave Chinner int forward; /* sibling block direction */
149530f712c9SDave Chinner int i; /* sibling counter */
149630f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */
149730f712c9SDave Chinner int rval; /* result from path_shift */
149830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr;
149930f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents;
150030f712c9SDave Chinner struct xfs_inode *dp = state->args->dp;
150130f712c9SDave Chinner
150230f712c9SDave Chinner /*
150330f712c9SDave Chinner * Check for the degenerate case of the block being over 50% full.
150430f712c9SDave Chinner * If so, it's not worth even looking to see if we might be able
150530f712c9SDave Chinner * to coalesce with a sibling.
150630f712c9SDave Chinner */
150730f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1];
150830f712c9SDave Chinner leaf = blk->bp->b_addr;
150951842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1510787b0893SChristoph Hellwig ents = leafhdr.ents;
151130f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk->bp);
151230f712c9SDave Chinner
151330f712c9SDave Chinner count = leafhdr.count - leafhdr.stale;
1514545910bcSChristoph Hellwig bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
151530f712c9SDave Chinner if (bytes > (state->args->geo->blksize >> 1)) {
151630f712c9SDave Chinner /*
151730f712c9SDave Chinner * Blk over 50%, don't try to join.
151830f712c9SDave Chinner */
151930f712c9SDave Chinner *action = 0;
152030f712c9SDave Chinner return 0;
152130f712c9SDave Chinner }
152230f712c9SDave Chinner /*
152330f712c9SDave Chinner * Check for the degenerate case of the block being empty.
152430f712c9SDave Chinner * If the block is empty, we'll simply delete it, no need to
152530f712c9SDave Chinner * coalesce it with a sibling block. We choose (arbitrarily)
152630f712c9SDave Chinner * to merge with the forward block unless it is NULL.
152730f712c9SDave Chinner */
152830f712c9SDave Chinner if (count == 0) {
152930f712c9SDave Chinner /*
153030f712c9SDave Chinner * Make altpath point to the block we want to keep and
153130f712c9SDave Chinner * path point to the block we want to drop (this one).
153230f712c9SDave Chinner */
153330f712c9SDave Chinner forward = (leafhdr.forw != 0);
153430f712c9SDave Chinner memcpy(&state->altpath, &state->path, sizeof(state->path));
153530f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
153630f712c9SDave Chinner &rval);
153730f712c9SDave Chinner if (error)
153830f712c9SDave Chinner return error;
153930f712c9SDave Chinner *action = rval ? 2 : 0;
154030f712c9SDave Chinner return 0;
154130f712c9SDave Chinner }
154230f712c9SDave Chinner /*
154330f712c9SDave Chinner * Examine each sibling block to see if we can coalesce with
154430f712c9SDave Chinner * at least 25% free space to spare. We need to figure out
154530f712c9SDave Chinner * whether to merge with the forward or the backward block.
154630f712c9SDave Chinner * We prefer coalescing with the lower numbered sibling so as
154730f712c9SDave Chinner * to shrink a directory over time.
154830f712c9SDave Chinner */
154930f712c9SDave Chinner forward = leafhdr.forw < leafhdr.back;
155030f712c9SDave Chinner for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
155130f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2;
155230f712c9SDave Chinner
155330f712c9SDave Chinner blkno = forward ? leafhdr.forw : leafhdr.back;
155430f712c9SDave Chinner if (blkno == 0)
155530f712c9SDave Chinner continue;
155630f712c9SDave Chinner /*
155730f712c9SDave Chinner * Read the sibling leaf block.
155830f712c9SDave Chinner */
1559f3fcb314SChristoph Hellwig error = xfs_dir3_leafn_read(state->args->trans, dp, blkno, &bp);
156030f712c9SDave Chinner if (error)
156130f712c9SDave Chinner return error;
156230f712c9SDave Chinner
156330f712c9SDave Chinner /*
156430f712c9SDave Chinner * Count bytes in the two blocks combined.
156530f712c9SDave Chinner */
156630f712c9SDave Chinner count = leafhdr.count - leafhdr.stale;
156730f712c9SDave Chinner bytes = state->args->geo->blksize -
156830f712c9SDave Chinner (state->args->geo->blksize >> 2);
156930f712c9SDave Chinner
157030f712c9SDave Chinner leaf = bp->b_addr;
157151842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
1572787b0893SChristoph Hellwig ents = hdr2.ents;
157330f712c9SDave Chinner count += hdr2.count - hdr2.stale;
157430f712c9SDave Chinner bytes -= count * sizeof(ents[0]);
157530f712c9SDave Chinner
157630f712c9SDave Chinner /*
157730f712c9SDave Chinner * Fits with at least 25% to spare.
157830f712c9SDave Chinner */
157930f712c9SDave Chinner if (bytes >= 0)
158030f712c9SDave Chinner break;
158130f712c9SDave Chinner xfs_trans_brelse(state->args->trans, bp);
158230f712c9SDave Chinner }
158330f712c9SDave Chinner /*
158430f712c9SDave Chinner * Didn't like either block, give up.
158530f712c9SDave Chinner */
158630f712c9SDave Chinner if (i >= 2) {
158730f712c9SDave Chinner *action = 0;
158830f712c9SDave Chinner return 0;
158930f712c9SDave Chinner }
159030f712c9SDave Chinner
159130f712c9SDave Chinner /*
159230f712c9SDave Chinner * Make altpath point to the block we want to keep (the lower
159330f712c9SDave Chinner * numbered block) and path point to the block we want to drop.
159430f712c9SDave Chinner */
159530f712c9SDave Chinner memcpy(&state->altpath, &state->path, sizeof(state->path));
159630f712c9SDave Chinner if (blkno < blk->blkno)
159730f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
159830f712c9SDave Chinner &rval);
159930f712c9SDave Chinner else
160030f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->path, forward, 0,
160130f712c9SDave Chinner &rval);
160230f712c9SDave Chinner if (error) {
160330f712c9SDave Chinner return error;
160430f712c9SDave Chinner }
160530f712c9SDave Chinner *action = rval ? 0 : 1;
160630f712c9SDave Chinner return 0;
160730f712c9SDave Chinner }
160830f712c9SDave Chinner
160930f712c9SDave Chinner /*
161030f712c9SDave Chinner * Move all the leaf entries from drop_blk to save_blk.
161130f712c9SDave Chinner * This is done as part of a join operation.
161230f712c9SDave Chinner */
161330f712c9SDave Chinner void
xfs_dir2_leafn_unbalance(xfs_da_state_t * state,xfs_da_state_blk_t * drop_blk,xfs_da_state_blk_t * save_blk)161430f712c9SDave Chinner xfs_dir2_leafn_unbalance(
161530f712c9SDave Chinner xfs_da_state_t *state, /* cursor */
161630f712c9SDave Chinner xfs_da_state_blk_t *drop_blk, /* dead block */
161730f712c9SDave Chinner xfs_da_state_blk_t *save_blk) /* surviving block */
161830f712c9SDave Chinner {
161930f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */
162030f712c9SDave Chinner xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
162130f712c9SDave Chinner xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
162230f712c9SDave Chinner struct xfs_dir3_icleaf_hdr savehdr;
162330f712c9SDave Chinner struct xfs_dir3_icleaf_hdr drophdr;
162430f712c9SDave Chinner struct xfs_dir2_leaf_entry *sents;
162530f712c9SDave Chinner struct xfs_dir2_leaf_entry *dents;
162630f712c9SDave Chinner struct xfs_inode *dp = state->args->dp;
162730f712c9SDave Chinner
162830f712c9SDave Chinner args = state->args;
162930f712c9SDave Chinner ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
163030f712c9SDave Chinner ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
163130f712c9SDave Chinner drop_leaf = drop_blk->bp->b_addr;
163230f712c9SDave Chinner save_leaf = save_blk->bp->b_addr;
163330f712c9SDave Chinner
163451842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
163551842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
1636787b0893SChristoph Hellwig sents = savehdr.ents;
1637787b0893SChristoph Hellwig dents = drophdr.ents;
163830f712c9SDave Chinner
163930f712c9SDave Chinner /*
164030f712c9SDave Chinner * If there are any stale leaf entries, take this opportunity
164130f712c9SDave Chinner * to purge them.
164230f712c9SDave Chinner */
164330f712c9SDave Chinner if (drophdr.stale)
164430f712c9SDave Chinner xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
164530f712c9SDave Chinner if (savehdr.stale)
164630f712c9SDave Chinner xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
164730f712c9SDave Chinner
164830f712c9SDave Chinner /*
164930f712c9SDave Chinner * Move the entries from drop to the appropriate end of save.
165030f712c9SDave Chinner */
165130f712c9SDave Chinner drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
165230f712c9SDave Chinner if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
165330f712c9SDave Chinner xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
165430f712c9SDave Chinner save_blk->bp, &savehdr, sents, 0,
165530f712c9SDave Chinner drophdr.count);
165630f712c9SDave Chinner else
165730f712c9SDave Chinner xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
165830f712c9SDave Chinner save_blk->bp, &savehdr, sents,
165930f712c9SDave Chinner savehdr.count, drophdr.count);
166030f712c9SDave Chinner save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
166130f712c9SDave Chinner
166230f712c9SDave Chinner /* log the changes made when moving the entries */
1663163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1664163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
166530f712c9SDave Chinner xfs_dir3_leaf_log_header(args, save_blk->bp);
166630f712c9SDave Chinner xfs_dir3_leaf_log_header(args, drop_blk->bp);
166730f712c9SDave Chinner
166830f712c9SDave Chinner xfs_dir3_leaf_check(dp, save_blk->bp);
166930f712c9SDave Chinner xfs_dir3_leaf_check(dp, drop_blk->bp);
167030f712c9SDave Chinner }
167130f712c9SDave Chinner
167230f712c9SDave Chinner /*
1673a07258a6SDave Chinner * Add a new data block to the directory at the free space index that the caller
1674a07258a6SDave Chinner * has specified.
1675a07258a6SDave Chinner */
1676a07258a6SDave Chinner static int
xfs_dir2_node_add_datablk(struct xfs_da_args * args,struct xfs_da_state_blk * fblk,xfs_dir2_db_t * dbno,struct xfs_buf ** dbpp,struct xfs_buf ** fbpp,struct xfs_dir3_icfree_hdr * hdr,int * findex)1677a07258a6SDave Chinner xfs_dir2_node_add_datablk(
1678a07258a6SDave Chinner struct xfs_da_args *args,
1679a07258a6SDave Chinner struct xfs_da_state_blk *fblk,
1680a07258a6SDave Chinner xfs_dir2_db_t *dbno,
1681a07258a6SDave Chinner struct xfs_buf **dbpp,
1682a07258a6SDave Chinner struct xfs_buf **fbpp,
1683195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr,
1684a07258a6SDave Chinner int *findex)
1685a07258a6SDave Chinner {
1686a07258a6SDave Chinner struct xfs_inode *dp = args->dp;
1687a07258a6SDave Chinner struct xfs_trans *tp = args->trans;
1688a07258a6SDave Chinner struct xfs_mount *mp = dp->i_mount;
1689a07258a6SDave Chinner struct xfs_dir2_data_free *bf;
1690a07258a6SDave Chinner xfs_dir2_db_t fbno;
1691a07258a6SDave Chinner struct xfs_buf *fbp;
1692a07258a6SDave Chinner struct xfs_buf *dbp;
1693a07258a6SDave Chinner int error;
1694a07258a6SDave Chinner
1695a07258a6SDave Chinner /* Not allowed to allocate, return failure. */
16960e822255SDave Chinner if (args->total == 0)
1697a07258a6SDave Chinner return -ENOSPC;
1698a07258a6SDave Chinner
1699a07258a6SDave Chinner /* Allocate and initialize the new data block. */
1700a07258a6SDave Chinner error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1701a07258a6SDave Chinner if (error)
1702a07258a6SDave Chinner return error;
1703a07258a6SDave Chinner error = xfs_dir3_data_init(args, *dbno, &dbp);
1704a07258a6SDave Chinner if (error)
1705a07258a6SDave Chinner return error;
1706a07258a6SDave Chinner
1707a07258a6SDave Chinner /*
1708a07258a6SDave Chinner * Get the freespace block corresponding to the data block
1709a07258a6SDave Chinner * that was just allocated.
1710a07258a6SDave Chinner */
17113d92c93bSChristoph Hellwig fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
1712a07258a6SDave Chinner error = xfs_dir2_free_try_read(tp, dp,
1713a07258a6SDave Chinner xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1714a07258a6SDave Chinner if (error)
1715a07258a6SDave Chinner return error;
1716a07258a6SDave Chinner
1717a07258a6SDave Chinner /*
1718a07258a6SDave Chinner * If there wasn't a freespace block, the read will
1719a07258a6SDave Chinner * return a NULL fbp. Allocate and initialize a new one.
1720a07258a6SDave Chinner */
1721a07258a6SDave Chinner if (!fbp) {
1722a07258a6SDave Chinner error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1723a07258a6SDave Chinner if (error)
1724a07258a6SDave Chinner return error;
1725a07258a6SDave Chinner
1726a71895c5SDarrick J. Wong if (XFS_IS_CORRUPT(mp,
1727a71895c5SDarrick J. Wong xfs_dir2_db_to_fdb(args->geo, *dbno) !=
1728a71895c5SDarrick J. Wong fbno)) {
1729a07258a6SDave Chinner xfs_alert(mp,
1730a07258a6SDave Chinner "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1731a07258a6SDave Chinner __func__, (unsigned long long)dp->i_ino,
17323d92c93bSChristoph Hellwig (long long)xfs_dir2_db_to_fdb(args->geo, *dbno),
1733a07258a6SDave Chinner (long long)*dbno, (long long)fbno);
1734a07258a6SDave Chinner if (fblk) {
1735a07258a6SDave Chinner xfs_alert(mp,
1736a07258a6SDave Chinner " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1737a07258a6SDave Chinner fblk, (unsigned long long)fblk->blkno,
1738a07258a6SDave Chinner fblk->index, fblk->magic);
1739a07258a6SDave Chinner } else {
1740a07258a6SDave Chinner xfs_alert(mp, " ... fblk is NULL");
1741a07258a6SDave Chinner }
1742a07258a6SDave Chinner return -EFSCORRUPTED;
1743a07258a6SDave Chinner }
1744a07258a6SDave Chinner
1745a07258a6SDave Chinner /* Get a buffer for the new block. */
1746a07258a6SDave Chinner error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1747a07258a6SDave Chinner if (error)
1748a07258a6SDave Chinner return error;
1749a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
1750a07258a6SDave Chinner
1751a07258a6SDave Chinner /* Remember the first slot as our empty slot. */
1752195b0a44SChristoph Hellwig hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1753a07258a6SDave Chinner XFS_DIR2_FREE_OFFSET)) *
17545893e4feSChristoph Hellwig args->geo->free_max_bests;
1755a07258a6SDave Chinner } else {
1756a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
1757a07258a6SDave Chinner }
1758a07258a6SDave Chinner
1759a07258a6SDave Chinner /* Set the freespace block index from the data block number. */
17603d92c93bSChristoph Hellwig *findex = xfs_dir2_db_to_fdindex(args->geo, *dbno);
1761a07258a6SDave Chinner
1762a07258a6SDave Chinner /* Extend the freespace table if the new data block is off the end. */
1763195b0a44SChristoph Hellwig if (*findex >= hdr->nvalid) {
17645893e4feSChristoph Hellwig ASSERT(*findex < args->geo->free_max_bests);
1765195b0a44SChristoph Hellwig hdr->nvalid = *findex + 1;
1766a84f3d5cSChristoph Hellwig hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF);
1767a07258a6SDave Chinner }
1768a07258a6SDave Chinner
1769a07258a6SDave Chinner /*
1770a07258a6SDave Chinner * If this entry was for an empty data block (this should always be
1771a07258a6SDave Chinner * true) then update the header.
1772a07258a6SDave Chinner */
1773a84f3d5cSChristoph Hellwig if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1774195b0a44SChristoph Hellwig hdr->nused++;
1775195b0a44SChristoph Hellwig xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
1776a07258a6SDave Chinner xfs_dir2_free_log_header(args, fbp);
1777a07258a6SDave Chinner }
1778a07258a6SDave Chinner
1779a07258a6SDave Chinner /* Update the freespace value for the new block in the table. */
17801848b607SChristoph Hellwig bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
1781a84f3d5cSChristoph Hellwig hdr->bests[*findex] = bf[0].length;
1782a07258a6SDave Chinner
1783a07258a6SDave Chinner *dbpp = dbp;
1784a07258a6SDave Chinner *fbpp = fbp;
1785a07258a6SDave Chinner return 0;
1786a07258a6SDave Chinner }
1787a07258a6SDave Chinner
17880e822255SDave Chinner static int
xfs_dir2_node_find_freeblk(struct xfs_da_args * args,struct xfs_da_state_blk * fblk,xfs_dir2_db_t * dbnop,struct xfs_buf ** fbpp,struct xfs_dir3_icfree_hdr * hdr,int * findexp,int length)17890e822255SDave Chinner xfs_dir2_node_find_freeblk(
17900e822255SDave Chinner struct xfs_da_args *args,
17910e822255SDave Chinner struct xfs_da_state_blk *fblk,
17920e822255SDave Chinner xfs_dir2_db_t *dbnop,
17930e822255SDave Chinner struct xfs_buf **fbpp,
1794195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr,
17950e822255SDave Chinner int *findexp,
17960e822255SDave Chinner int length)
179730f712c9SDave Chinner {
17980e822255SDave Chinner struct xfs_inode *dp = args->dp;
17990e822255SDave Chinner struct xfs_trans *tp = args->trans;
18000e822255SDave Chinner struct xfs_buf *fbp = NULL;
1801756c6f0fSDave Chinner xfs_dir2_db_t firstfbno;
18020e822255SDave Chinner xfs_dir2_db_t lastfbno;
18030e822255SDave Chinner xfs_dir2_db_t ifbno = -1;
18040e822255SDave Chinner xfs_dir2_db_t dbno = -1;
1805756c6f0fSDave Chinner xfs_dir2_db_t fbno;
18060e822255SDave Chinner xfs_fileoff_t fo;
1807610125abSDave Chinner int findex = 0;
18080e822255SDave Chinner int error;
180930f712c9SDave Chinner
181030f712c9SDave Chinner /*
181130f712c9SDave Chinner * If we came in with a freespace block that means that lookup
181230f712c9SDave Chinner * found an entry with our hash value. This is the freespace
181330f712c9SDave Chinner * block for that data entry.
181430f712c9SDave Chinner */
181530f712c9SDave Chinner if (fblk) {
181630f712c9SDave Chinner fbp = fblk->bp;
181730f712c9SDave Chinner findex = fblk->index;
1818a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
18190e822255SDave Chinner if (findex >= 0) {
18200e822255SDave Chinner /* caller already found the freespace for us. */
1821195b0a44SChristoph Hellwig ASSERT(findex < hdr->nvalid);
1822a84f3d5cSChristoph Hellwig ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
1823a84f3d5cSChristoph Hellwig ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
1824195b0a44SChristoph Hellwig dbno = hdr->firstdb + findex;
1825a07258a6SDave Chinner goto found_block;
18260e822255SDave Chinner }
18270e822255SDave Chinner
182830f712c9SDave Chinner /*
182930f712c9SDave Chinner * The data block looked at didn't have enough room.
183030f712c9SDave Chinner * We'll start at the beginning of the freespace entries.
183130f712c9SDave Chinner */
18320e822255SDave Chinner ifbno = fblk->blkno;
1833610125abSDave Chinner xfs_trans_brelse(tp, fbp);
1834610125abSDave Chinner fbp = NULL;
1835610125abSDave Chinner fblk->bp = NULL;
183630f712c9SDave Chinner }
18370e822255SDave Chinner
18380e822255SDave Chinner /*
18390e822255SDave Chinner * If we don't have a data block yet, we're going to scan the freespace
1840610125abSDave Chinner * data for a data block with enough free space in it.
18410e822255SDave Chinner */
18420e822255SDave Chinner error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
18430e822255SDave Chinner if (error)
18440e822255SDave Chinner return error;
18450e822255SDave Chinner lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1846756c6f0fSDave Chinner firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
18470e822255SDave Chinner
1848756c6f0fSDave Chinner for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
1849610125abSDave Chinner /* If it's ifbno we already looked at it. */
1850610125abSDave Chinner if (fbno == ifbno)
1851610125abSDave Chinner continue;
1852610125abSDave Chinner
185330f712c9SDave Chinner /*
1854610125abSDave Chinner * Read the block. There can be holes in the freespace blocks,
1855610125abSDave Chinner * so this might not succeed. This should be really rare, so
1856610125abSDave Chinner * there's no reason to avoid it.
185730f712c9SDave Chinner */
185830f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp,
185930f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, fbno),
186030f712c9SDave Chinner &fbp);
186130f712c9SDave Chinner if (error)
186230f712c9SDave Chinner return error;
186330f712c9SDave Chinner if (!fbp)
186430f712c9SDave Chinner continue;
1865610125abSDave Chinner
1866a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
1867610125abSDave Chinner
1868610125abSDave Chinner /* Scan the free entry array for a large enough free space. */
1869195b0a44SChristoph Hellwig for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
1870a84f3d5cSChristoph Hellwig if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
1871a84f3d5cSChristoph Hellwig be16_to_cpu(hdr->bests[findex]) >= length) {
1872195b0a44SChristoph Hellwig dbno = hdr->firstdb + findex;
1873610125abSDave Chinner goto found_block;
1874610125abSDave Chinner }
1875610125abSDave Chinner }
1876610125abSDave Chinner
1877610125abSDave Chinner /* Didn't find free space, go on to next free block */
187830f712c9SDave Chinner xfs_trans_brelse(tp, fbp);
187930f712c9SDave Chinner }
1880610125abSDave Chinner
18810e822255SDave Chinner found_block:
18820e822255SDave Chinner *dbnop = dbno;
18830e822255SDave Chinner *fbpp = fbp;
18840e822255SDave Chinner *findexp = findex;
18850e822255SDave Chinner return 0;
18860e822255SDave Chinner }
18870e822255SDave Chinner
18880e822255SDave Chinner /*
18890e822255SDave Chinner * Add the data entry for a node-format directory name addition.
18900e822255SDave Chinner * The leaf entry is added in xfs_dir2_leafn_add.
18910e822255SDave Chinner * We may enter with a freespace block that the lookup found.
18920e822255SDave Chinner */
18930e822255SDave Chinner static int
xfs_dir2_node_addname_int(struct xfs_da_args * args,struct xfs_da_state_blk * fblk)18940e822255SDave Chinner xfs_dir2_node_addname_int(
18950e822255SDave Chinner struct xfs_da_args *args, /* operation arguments */
18960e822255SDave Chinner struct xfs_da_state_blk *fblk) /* optional freespace block */
18970e822255SDave Chinner {
18980e822255SDave Chinner struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
18990e822255SDave Chinner struct xfs_dir2_data_entry *dep; /* data entry pointer */
19000e822255SDave Chinner struct xfs_dir2_data_hdr *hdr; /* data block header */
19010e822255SDave Chinner struct xfs_dir2_data_free *bf;
19020e822255SDave Chinner struct xfs_trans *tp = args->trans;
19030e822255SDave Chinner struct xfs_inode *dp = args->dp;
1904195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr freehdr;
19050e822255SDave Chinner struct xfs_buf *dbp; /* data block buffer */
19060e822255SDave Chinner struct xfs_buf *fbp; /* freespace buffer */
19070e822255SDave Chinner xfs_dir2_data_aoff_t aoff;
19080e822255SDave Chinner xfs_dir2_db_t dbno; /* data block number */
19090e822255SDave Chinner int error; /* error return value */
19100e822255SDave Chinner int findex; /* freespace entry index */
19110e822255SDave Chinner int length; /* length of the new entry */
19120e822255SDave Chinner int logfree = 0; /* need to log free entry */
19130e822255SDave Chinner int needlog = 0; /* need to log data header */
19140e822255SDave Chinner int needscan = 0; /* need to rescan data frees */
19150e822255SDave Chinner __be16 *tagp; /* data entry tag pointer */
19160e822255SDave Chinner
1917fdbb8c5bSChristoph Hellwig length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
1918195b0a44SChristoph Hellwig error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
1919195b0a44SChristoph Hellwig &findex, length);
19200e822255SDave Chinner if (error)
19210e822255SDave Chinner return error;
19220e822255SDave Chinner
19230e822255SDave Chinner /*
19240e822255SDave Chinner * Now we know if we must allocate blocks, so if we are checking whether
19250e822255SDave Chinner * we can insert without allocation then we can return now.
19260e822255SDave Chinner */
19270e822255SDave Chinner if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
19280e822255SDave Chinner if (dbno == -1)
19290e822255SDave Chinner return -ENOSPC;
19300e822255SDave Chinner return 0;
19310e822255SDave Chinner }
1932a07258a6SDave Chinner
193330f712c9SDave Chinner /*
193430f712c9SDave Chinner * If we don't have a data block, we need to allocate one and make
193530f712c9SDave Chinner * the freespace entries refer to it.
193630f712c9SDave Chinner */
1937a07258a6SDave Chinner if (dbno == -1) {
1938a07258a6SDave Chinner /* we're going to have to log the free block index later */
193930f712c9SDave Chinner logfree = 1;
19400e822255SDave Chinner error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1941195b0a44SChristoph Hellwig &freehdr, &findex);
1942a07258a6SDave Chinner } else {
1943a07258a6SDave Chinner /* Read the data block in. */
194430f712c9SDave Chinner error = xfs_dir3_data_read(tp, dp,
194530f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, dbno),
1946cd2c9f1bSChristoph Hellwig 0, &dbp);
19470e822255SDave Chinner }
194830f712c9SDave Chinner if (error)
194930f712c9SDave Chinner return error;
1950a07258a6SDave Chinner
1951a07258a6SDave Chinner /* setup for data block up now */
195230f712c9SDave Chinner hdr = dbp->b_addr;
19531848b607SChristoph Hellwig bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
195430f712c9SDave Chinner ASSERT(be16_to_cpu(bf[0].length) >= length);
1955a07258a6SDave Chinner
1956a07258a6SDave Chinner /* Point to the existing unused space. */
195730f712c9SDave Chinner dup = (xfs_dir2_data_unused_t *)
195830f712c9SDave Chinner ((char *)hdr + be16_to_cpu(bf[0].offset));
1959a07258a6SDave Chinner
1960a07258a6SDave Chinner /* Mark the first part of the unused space, inuse for us. */
19616915ef35SDarrick J. Wong aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
19626915ef35SDarrick J. Wong error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
196330f712c9SDave Chinner &needlog, &needscan);
19646915ef35SDarrick J. Wong if (error) {
19656915ef35SDarrick J. Wong xfs_trans_brelse(tp, dbp);
19666915ef35SDarrick J. Wong return error;
19676915ef35SDarrick J. Wong }
1968a07258a6SDave Chinner
1969a07258a6SDave Chinner /* Fill in the new entry and log it. */
197030f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)dup;
197130f712c9SDave Chinner dep->inumber = cpu_to_be64(args->inumber);
197230f712c9SDave Chinner dep->namelen = args->namelen;
197330f712c9SDave Chinner memcpy(dep->name, args->name, dep->namelen);
197459b8b465SChristoph Hellwig xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
19757e8ae7bdSChristoph Hellwig tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
197630f712c9SDave Chinner *tagp = cpu_to_be16((char *)dep - (char *)hdr);
197730f712c9SDave Chinner xfs_dir2_data_log_entry(args, dbp, dep);
1978a07258a6SDave Chinner
1979a07258a6SDave Chinner /* Rescan the freespace and log the data block if needed. */
198030f712c9SDave Chinner if (needscan)
1981ae42976dSChristoph Hellwig xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
198230f712c9SDave Chinner if (needlog)
198330f712c9SDave Chinner xfs_dir2_data_log_header(args, dbp);
1984a07258a6SDave Chinner
1985a07258a6SDave Chinner /* If the freespace block entry is now wrong, update it. */
1986a84f3d5cSChristoph Hellwig if (freehdr.bests[findex] != bf[0].length) {
1987a84f3d5cSChristoph Hellwig freehdr.bests[findex] = bf[0].length;
198830f712c9SDave Chinner logfree = 1;
198930f712c9SDave Chinner }
1990a07258a6SDave Chinner
1991a07258a6SDave Chinner /* Log the freespace entry if needed. */
199230f712c9SDave Chinner if (logfree)
1993a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
1994a07258a6SDave Chinner
1995a07258a6SDave Chinner /* Return the data block and offset in args. */
199630f712c9SDave Chinner args->blkno = (xfs_dablk_t)dbno;
199730f712c9SDave Chinner args->index = be16_to_cpu(*tagp);
199830f712c9SDave Chinner return 0;
199930f712c9SDave Chinner }
200030f712c9SDave Chinner
200130f712c9SDave Chinner /*
2002aee7754bSDave Chinner * Top-level node form directory addname routine.
2003aee7754bSDave Chinner */
2004aee7754bSDave Chinner int /* error */
xfs_dir2_node_addname(xfs_da_args_t * args)2005aee7754bSDave Chinner xfs_dir2_node_addname(
2006aee7754bSDave Chinner xfs_da_args_t *args) /* operation arguments */
2007aee7754bSDave Chinner {
2008aee7754bSDave Chinner xfs_da_state_blk_t *blk; /* leaf block for insert */
2009aee7754bSDave Chinner int error; /* error return value */
2010aee7754bSDave Chinner int rval; /* sub-return value */
2011aee7754bSDave Chinner xfs_da_state_t *state; /* btree cursor */
2012aee7754bSDave Chinner
2013aee7754bSDave Chinner trace_xfs_dir2_node_addname(args);
2014aee7754bSDave Chinner
2015aee7754bSDave Chinner /*
2016aee7754bSDave Chinner * Allocate and initialize the state (btree cursor).
2017aee7754bSDave Chinner */
20184491a3ddSCarlos Maiolino state = xfs_da_state_alloc(args);
2019aee7754bSDave Chinner /*
2020aee7754bSDave Chinner * Look up the name. We're not supposed to find it, but
2021aee7754bSDave Chinner * this gives us the insertion point.
2022aee7754bSDave Chinner */
2023aee7754bSDave Chinner error = xfs_da3_node_lookup_int(state, &rval);
2024aee7754bSDave Chinner if (error)
2025aee7754bSDave Chinner rval = error;
2026aee7754bSDave Chinner if (rval != -ENOENT) {
2027aee7754bSDave Chinner goto done;
2028aee7754bSDave Chinner }
2029aee7754bSDave Chinner /*
2030aee7754bSDave Chinner * Add the data entry to a data block.
2031aee7754bSDave Chinner * Extravalid is set to a freeblock found by lookup.
2032aee7754bSDave Chinner */
2033aee7754bSDave Chinner rval = xfs_dir2_node_addname_int(args,
2034aee7754bSDave Chinner state->extravalid ? &state->extrablk : NULL);
2035aee7754bSDave Chinner if (rval) {
2036aee7754bSDave Chinner goto done;
2037aee7754bSDave Chinner }
2038aee7754bSDave Chinner blk = &state->path.blk[state->path.active - 1];
2039aee7754bSDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2040aee7754bSDave Chinner /*
2041aee7754bSDave Chinner * Add the new leaf entry.
2042aee7754bSDave Chinner */
2043aee7754bSDave Chinner rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2044aee7754bSDave Chinner if (rval == 0) {
2045aee7754bSDave Chinner /*
2046aee7754bSDave Chinner * It worked, fix the hash values up the btree.
2047aee7754bSDave Chinner */
2048aee7754bSDave Chinner if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2049aee7754bSDave Chinner xfs_da3_fixhashpath(state, &state->path);
2050aee7754bSDave Chinner } else {
2051aee7754bSDave Chinner /*
2052aee7754bSDave Chinner * It didn't work, we need to split the leaf block.
2053aee7754bSDave Chinner */
2054aee7754bSDave Chinner if (args->total == 0) {
2055aee7754bSDave Chinner ASSERT(rval == -ENOSPC);
2056aee7754bSDave Chinner goto done;
2057aee7754bSDave Chinner }
2058aee7754bSDave Chinner /*
2059aee7754bSDave Chinner * Split the leaf block and insert the new entry.
2060aee7754bSDave Chinner */
2061aee7754bSDave Chinner rval = xfs_da3_split(state);
2062aee7754bSDave Chinner }
2063aee7754bSDave Chinner done:
2064aee7754bSDave Chinner xfs_da_state_free(state);
2065aee7754bSDave Chinner return rval;
2066aee7754bSDave Chinner }
2067aee7754bSDave Chinner
2068aee7754bSDave Chinner /*
206930f712c9SDave Chinner * Lookup an entry in a node-format directory.
207030f712c9SDave Chinner * All the real work happens in xfs_da3_node_lookup_int.
207130f712c9SDave Chinner * The only real output is the inode number of the entry.
207230f712c9SDave Chinner */
207330f712c9SDave Chinner int /* error */
xfs_dir2_node_lookup(xfs_da_args_t * args)207430f712c9SDave Chinner xfs_dir2_node_lookup(
207530f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */
207630f712c9SDave Chinner {
207730f712c9SDave Chinner int error; /* error return value */
207830f712c9SDave Chinner int i; /* btree level */
207930f712c9SDave Chinner int rval; /* operation return value */
208030f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */
208130f712c9SDave Chinner
208230f712c9SDave Chinner trace_xfs_dir2_node_lookup(args);
208330f712c9SDave Chinner
208430f712c9SDave Chinner /*
208530f712c9SDave Chinner * Allocate and initialize the btree cursor.
208630f712c9SDave Chinner */
20874491a3ddSCarlos Maiolino state = xfs_da_state_alloc(args);
20884491a3ddSCarlos Maiolino
208930f712c9SDave Chinner /*
209030f712c9SDave Chinner * Fill in the path to the entry in the cursor.
209130f712c9SDave Chinner */
209230f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval);
209330f712c9SDave Chinner if (error)
209430f712c9SDave Chinner rval = error;
20952451337dSDave Chinner else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
20962451337dSDave Chinner /* If a CI match, dup the actual name and return -EEXIST */
209730f712c9SDave Chinner xfs_dir2_data_entry_t *dep;
209830f712c9SDave Chinner
209930f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)
210030f712c9SDave Chinner ((char *)state->extrablk.bp->b_addr +
210130f712c9SDave Chinner state->extrablk.index);
210230f712c9SDave Chinner rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
210330f712c9SDave Chinner }
210430f712c9SDave Chinner /*
210530f712c9SDave Chinner * Release the btree blocks and leaf block.
210630f712c9SDave Chinner */
210730f712c9SDave Chinner for (i = 0; i < state->path.active; i++) {
210830f712c9SDave Chinner xfs_trans_brelse(args->trans, state->path.blk[i].bp);
210930f712c9SDave Chinner state->path.blk[i].bp = NULL;
211030f712c9SDave Chinner }
211130f712c9SDave Chinner /*
211230f712c9SDave Chinner * Release the data block if we have it.
211330f712c9SDave Chinner */
211430f712c9SDave Chinner if (state->extravalid && state->extrablk.bp) {
211530f712c9SDave Chinner xfs_trans_brelse(args->trans, state->extrablk.bp);
211630f712c9SDave Chinner state->extrablk.bp = NULL;
211730f712c9SDave Chinner }
211830f712c9SDave Chinner xfs_da_state_free(state);
211930f712c9SDave Chinner return rval;
212030f712c9SDave Chinner }
212130f712c9SDave Chinner
212230f712c9SDave Chinner /*
212330f712c9SDave Chinner * Remove an entry from a node-format directory.
212430f712c9SDave Chinner */
212530f712c9SDave Chinner int /* error */
xfs_dir2_node_removename(struct xfs_da_args * args)212630f712c9SDave Chinner xfs_dir2_node_removename(
212730f712c9SDave Chinner struct xfs_da_args *args) /* operation arguments */
212830f712c9SDave Chinner {
212930f712c9SDave Chinner struct xfs_da_state_blk *blk; /* leaf block */
213030f712c9SDave Chinner int error; /* error return value */
213130f712c9SDave Chinner int rval; /* operation return value */
213230f712c9SDave Chinner struct xfs_da_state *state; /* btree cursor */
213330f712c9SDave Chinner
213430f712c9SDave Chinner trace_xfs_dir2_node_removename(args);
213530f712c9SDave Chinner
213630f712c9SDave Chinner /*
213730f712c9SDave Chinner * Allocate and initialize the btree cursor.
213830f712c9SDave Chinner */
21394491a3ddSCarlos Maiolino state = xfs_da_state_alloc(args);
214030f712c9SDave Chinner
214130f712c9SDave Chinner /* Look up the entry we're deleting, set up the cursor. */
214230f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval);
214330f712c9SDave Chinner if (error)
214430f712c9SDave Chinner goto out_free;
214530f712c9SDave Chinner
214630f712c9SDave Chinner /* Didn't find it, upper layer screwed up. */
21472451337dSDave Chinner if (rval != -EEXIST) {
214830f712c9SDave Chinner error = rval;
214930f712c9SDave Chinner goto out_free;
215030f712c9SDave Chinner }
215130f712c9SDave Chinner
215230f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1];
215330f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
215430f712c9SDave Chinner ASSERT(state->extravalid);
215530f712c9SDave Chinner /*
215630f712c9SDave Chinner * Remove the leaf and data entries.
215730f712c9SDave Chinner * Extrablk refers to the data block.
215830f712c9SDave Chinner */
215930f712c9SDave Chinner error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
216030f712c9SDave Chinner &state->extrablk, &rval);
216130f712c9SDave Chinner if (error)
216230f712c9SDave Chinner goto out_free;
216330f712c9SDave Chinner /*
216430f712c9SDave Chinner * Fix the hash values up the btree.
216530f712c9SDave Chinner */
216630f712c9SDave Chinner xfs_da3_fixhashpath(state, &state->path);
216730f712c9SDave Chinner /*
216830f712c9SDave Chinner * If we need to join leaf blocks, do it.
216930f712c9SDave Chinner */
217030f712c9SDave Chinner if (rval && state->path.active > 1)
217130f712c9SDave Chinner error = xfs_da3_join(state);
217230f712c9SDave Chinner /*
217330f712c9SDave Chinner * If no errors so far, try conversion to leaf format.
217430f712c9SDave Chinner */
217530f712c9SDave Chinner if (!error)
217630f712c9SDave Chinner error = xfs_dir2_node_to_leaf(state);
217730f712c9SDave Chinner out_free:
217830f712c9SDave Chinner xfs_da_state_free(state);
217930f712c9SDave Chinner return error;
218030f712c9SDave Chinner }
218130f712c9SDave Chinner
218230f712c9SDave Chinner /*
218330f712c9SDave Chinner * Replace an entry's inode number in a node-format directory.
218430f712c9SDave Chinner */
218530f712c9SDave Chinner int /* error */
xfs_dir2_node_replace(xfs_da_args_t * args)218630f712c9SDave Chinner xfs_dir2_node_replace(
218730f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */
218830f712c9SDave Chinner {
218930f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block */
219030f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */
219130f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data entry changed */
219230f712c9SDave Chinner int error; /* error return value */
219330f712c9SDave Chinner int i; /* btree level */
219430f712c9SDave Chinner xfs_ino_t inum; /* new inode number */
219503754234SJan Kara int ftype; /* new file type */
219630f712c9SDave Chinner int rval; /* internal return value */
219730f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */
219830f712c9SDave Chinner
219930f712c9SDave Chinner trace_xfs_dir2_node_replace(args);
220030f712c9SDave Chinner
220130f712c9SDave Chinner /*
220230f712c9SDave Chinner * Allocate and initialize the btree cursor.
220330f712c9SDave Chinner */
22044491a3ddSCarlos Maiolino state = xfs_da_state_alloc(args);
220503754234SJan Kara
220603754234SJan Kara /*
220703754234SJan Kara * We have to save new inode number and ftype since
220803754234SJan Kara * xfs_da3_node_lookup_int() is going to overwrite them
220903754234SJan Kara */
221030f712c9SDave Chinner inum = args->inumber;
221103754234SJan Kara ftype = args->filetype;
221203754234SJan Kara
221330f712c9SDave Chinner /*
221430f712c9SDave Chinner * Lookup the entry to change in the btree.
221530f712c9SDave Chinner */
221630f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval);
221730f712c9SDave Chinner if (error) {
221830f712c9SDave Chinner rval = error;
221930f712c9SDave Chinner }
222030f712c9SDave Chinner /*
222130f712c9SDave Chinner * It should be found, since the vnodeops layer has looked it up
222230f712c9SDave Chinner * and locked it. But paranoia is good.
222330f712c9SDave Chinner */
22242451337dSDave Chinner if (rval == -EEXIST) {
2225787b0893SChristoph Hellwig struct xfs_dir3_icleaf_hdr leafhdr;
2226787b0893SChristoph Hellwig
222730f712c9SDave Chinner /*
222830f712c9SDave Chinner * Find the leaf entry.
222930f712c9SDave Chinner */
223030f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1];
223130f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
223230f712c9SDave Chinner ASSERT(state->extravalid);
2233787b0893SChristoph Hellwig
2234787b0893SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2235787b0893SChristoph Hellwig blk->bp->b_addr);
223630f712c9SDave Chinner /*
223730f712c9SDave Chinner * Point to the data entry.
223830f712c9SDave Chinner */
223930f712c9SDave Chinner hdr = state->extrablk.bp->b_addr;
224030f712c9SDave Chinner ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
224130f712c9SDave Chinner hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
224230f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)
224330f712c9SDave Chinner ((char *)hdr +
224430f712c9SDave Chinner xfs_dir2_dataptr_to_off(args->geo,
2245787b0893SChristoph Hellwig be32_to_cpu(leafhdr.ents[blk->index].address)));
224630f712c9SDave Chinner ASSERT(inum != be64_to_cpu(dep->inumber));
224730f712c9SDave Chinner /*
224830f712c9SDave Chinner * Fill in the new inode number and log the entry.
224930f712c9SDave Chinner */
225030f712c9SDave Chinner dep->inumber = cpu_to_be64(inum);
225159b8b465SChristoph Hellwig xfs_dir2_data_put_ftype(state->mp, dep, ftype);
225230f712c9SDave Chinner xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
225330f712c9SDave Chinner rval = 0;
225430f712c9SDave Chinner }
225530f712c9SDave Chinner /*
225630f712c9SDave Chinner * Didn't find it, and we're holding a data block. Drop it.
225730f712c9SDave Chinner */
225830f712c9SDave Chinner else if (state->extravalid) {
225930f712c9SDave Chinner xfs_trans_brelse(args->trans, state->extrablk.bp);
226030f712c9SDave Chinner state->extrablk.bp = NULL;
226130f712c9SDave Chinner }
226230f712c9SDave Chinner /*
226330f712c9SDave Chinner * Release all the buffers in the cursor.
226430f712c9SDave Chinner */
226530f712c9SDave Chinner for (i = 0; i < state->path.active; i++) {
226630f712c9SDave Chinner xfs_trans_brelse(args->trans, state->path.blk[i].bp);
226730f712c9SDave Chinner state->path.blk[i].bp = NULL;
226830f712c9SDave Chinner }
226930f712c9SDave Chinner xfs_da_state_free(state);
227030f712c9SDave Chinner return rval;
227130f712c9SDave Chinner }
227230f712c9SDave Chinner
227330f712c9SDave Chinner /*
227430f712c9SDave Chinner * Trim off a trailing empty freespace block.
227530f712c9SDave Chinner * Return (in rvalp) 1 if we did it, 0 if not.
227630f712c9SDave Chinner */
227730f712c9SDave Chinner int /* error */
xfs_dir2_node_trim_free(xfs_da_args_t * args,xfs_fileoff_t fo,int * rvalp)227830f712c9SDave Chinner xfs_dir2_node_trim_free(
227930f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */
228030f712c9SDave Chinner xfs_fileoff_t fo, /* free block number */
228130f712c9SDave Chinner int *rvalp) /* out: did something */
228230f712c9SDave Chinner {
228330f712c9SDave Chinner struct xfs_buf *bp; /* freespace buffer */
228430f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */
228530f712c9SDave Chinner int error; /* error return code */
228630f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */
228730f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */
228830f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr;
228930f712c9SDave Chinner
229030f712c9SDave Chinner dp = args->dp;
229130f712c9SDave Chinner tp = args->trans;
2292355cced4SChristoph Hellwig
2293355cced4SChristoph Hellwig *rvalp = 0;
2294355cced4SChristoph Hellwig
229530f712c9SDave Chinner /*
229630f712c9SDave Chinner * Read the freespace block.
229730f712c9SDave Chinner */
229830f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
229930f712c9SDave Chinner if (error)
230030f712c9SDave Chinner return error;
230130f712c9SDave Chinner /*
230230f712c9SDave Chinner * There can be holes in freespace. If fo is a hole, there's
230330f712c9SDave Chinner * nothing to do.
230430f712c9SDave Chinner */
230530f712c9SDave Chinner if (!bp)
230630f712c9SDave Chinner return 0;
230730f712c9SDave Chinner free = bp->b_addr;
23085ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
230930f712c9SDave Chinner
231030f712c9SDave Chinner /*
231130f712c9SDave Chinner * If there are used entries, there's nothing to do.
231230f712c9SDave Chinner */
231330f712c9SDave Chinner if (freehdr.nused > 0) {
231430f712c9SDave Chinner xfs_trans_brelse(tp, bp);
231530f712c9SDave Chinner return 0;
231630f712c9SDave Chinner }
231730f712c9SDave Chinner /*
231830f712c9SDave Chinner * Blow the block away.
231930f712c9SDave Chinner */
232030f712c9SDave Chinner error = xfs_dir2_shrink_inode(args,
232130f712c9SDave Chinner xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
232230f712c9SDave Chinner if (error) {
232330f712c9SDave Chinner /*
232430f712c9SDave Chinner * Can't fail with ENOSPC since that only happens with no
232530f712c9SDave Chinner * space reservation, when breaking up an extent into two
232630f712c9SDave Chinner * pieces. This is the last block of an extent.
232730f712c9SDave Chinner */
23282451337dSDave Chinner ASSERT(error != -ENOSPC);
232930f712c9SDave Chinner xfs_trans_brelse(tp, bp);
233030f712c9SDave Chinner return error;
233130f712c9SDave Chinner }
233230f712c9SDave Chinner /*
233330f712c9SDave Chinner * Return that we succeeded.
233430f712c9SDave Chinner */
233530f712c9SDave Chinner *rvalp = 1;
233630f712c9SDave Chinner return 0;
233730f712c9SDave Chinner }
2338