1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun * Copyright (c) 2013 Red Hat, Inc.
5*4882a593Smuzhiyun * All Rights Reserved.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun #include "xfs.h"
8*4882a593Smuzhiyun #include "xfs_fs.h"
9*4882a593Smuzhiyun #include "xfs_shared.h"
10*4882a593Smuzhiyun #include "xfs_format.h"
11*4882a593Smuzhiyun #include "xfs_log_format.h"
12*4882a593Smuzhiyun #include "xfs_trans_resv.h"
13*4882a593Smuzhiyun #include "xfs_mount.h"
14*4882a593Smuzhiyun #include "xfs_inode.h"
15*4882a593Smuzhiyun #include "xfs_bmap.h"
16*4882a593Smuzhiyun #include "xfs_dir2.h"
17*4882a593Smuzhiyun #include "xfs_dir2_priv.h"
18*4882a593Smuzhiyun #include "xfs_error.h"
19*4882a593Smuzhiyun #include "xfs_trace.h"
20*4882a593Smuzhiyun #include "xfs_trans.h"
21*4882a593Smuzhiyun #include "xfs_buf_item.h"
22*4882a593Smuzhiyun #include "xfs_log.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun * Function declarations.
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
28*4882a593Smuzhiyun int index);
29*4882a593Smuzhiyun static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
30*4882a593Smuzhiyun xfs_da_state_blk_t *blk1,
31*4882a593Smuzhiyun xfs_da_state_blk_t *blk2);
32*4882a593Smuzhiyun static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
33*4882a593Smuzhiyun int index, xfs_da_state_blk_t *dblk,
34*4882a593Smuzhiyun int *rval);
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /*
37*4882a593Smuzhiyun * Convert data space db to the corresponding free db.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun static xfs_dir2_db_t
xfs_dir2_db_to_fdb(struct xfs_da_geometry * geo,xfs_dir2_db_t db)40*4882a593Smuzhiyun xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
43*4882a593Smuzhiyun (db / geo->free_max_bests);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * Convert data space db to the corresponding index in a free db.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun static int
xfs_dir2_db_to_fdindex(struct xfs_da_geometry * geo,xfs_dir2_db_t db)50*4882a593Smuzhiyun xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun return db % geo->free_max_bests;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun * Check internal consistency of a leafn block.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun #ifdef DEBUG
59*4882a593Smuzhiyun static xfs_failaddr_t
xfs_dir3_leafn_check(struct xfs_inode * dp,struct xfs_buf * bp)60*4882a593Smuzhiyun xfs_dir3_leafn_check(
61*4882a593Smuzhiyun struct xfs_inode *dp,
62*4882a593Smuzhiyun struct xfs_buf *bp)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf = bp->b_addr;
65*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
70*4882a593Smuzhiyun struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
71*4882a593Smuzhiyun if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
72*4882a593Smuzhiyun return __this_address;
73*4882a593Smuzhiyun } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
74*4882a593Smuzhiyun return __this_address;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun static inline void
xfs_dir3_leaf_check(struct xfs_inode * dp,struct xfs_buf * bp)80*4882a593Smuzhiyun xfs_dir3_leaf_check(
81*4882a593Smuzhiyun struct xfs_inode *dp,
82*4882a593Smuzhiyun struct xfs_buf *bp)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun xfs_failaddr_t fa;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun fa = xfs_dir3_leafn_check(dp, bp);
87*4882a593Smuzhiyun if (!fa)
88*4882a593Smuzhiyun return;
89*4882a593Smuzhiyun xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
90*4882a593Smuzhiyun bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
91*4882a593Smuzhiyun fa);
92*4882a593Smuzhiyun ASSERT(0);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun #else
95*4882a593Smuzhiyun #define xfs_dir3_leaf_check(dp, bp)
96*4882a593Smuzhiyun #endif
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun static xfs_failaddr_t
xfs_dir3_free_verify(struct xfs_buf * bp)99*4882a593Smuzhiyun xfs_dir3_free_verify(
100*4882a593Smuzhiyun struct xfs_buf *bp)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
103*4882a593Smuzhiyun struct xfs_dir2_free_hdr *hdr = bp->b_addr;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun if (!xfs_verify_magic(bp, hdr->magic))
106*4882a593Smuzhiyun return __this_address;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
109*4882a593Smuzhiyun struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
112*4882a593Smuzhiyun return __this_address;
113*4882a593Smuzhiyun if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
114*4882a593Smuzhiyun return __this_address;
115*4882a593Smuzhiyun if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
116*4882a593Smuzhiyun return __this_address;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun return NULL;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun static void
xfs_dir3_free_read_verify(struct xfs_buf * bp)125*4882a593Smuzhiyun xfs_dir3_free_read_verify(
126*4882a593Smuzhiyun struct xfs_buf *bp)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
129*4882a593Smuzhiyun xfs_failaddr_t fa;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb) &&
132*4882a593Smuzhiyun !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
133*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSBADCRC, __this_address);
134*4882a593Smuzhiyun else {
135*4882a593Smuzhiyun fa = xfs_dir3_free_verify(bp);
136*4882a593Smuzhiyun if (fa)
137*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSCORRUPTED, fa);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun static void
xfs_dir3_free_write_verify(struct xfs_buf * bp)142*4882a593Smuzhiyun xfs_dir3_free_write_verify(
143*4882a593Smuzhiyun struct xfs_buf *bp)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
146*4882a593Smuzhiyun struct xfs_buf_log_item *bip = bp->b_log_item;
147*4882a593Smuzhiyun struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
148*4882a593Smuzhiyun xfs_failaddr_t fa;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun fa = xfs_dir3_free_verify(bp);
151*4882a593Smuzhiyun if (fa) {
152*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSCORRUPTED, fa);
153*4882a593Smuzhiyun return;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun if (!xfs_sb_version_hascrc(&mp->m_sb))
157*4882a593Smuzhiyun return;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun if (bip)
160*4882a593Smuzhiyun hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
166*4882a593Smuzhiyun .name = "xfs_dir3_free",
167*4882a593Smuzhiyun .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
168*4882a593Smuzhiyun cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
169*4882a593Smuzhiyun .verify_read = xfs_dir3_free_read_verify,
170*4882a593Smuzhiyun .verify_write = xfs_dir3_free_write_verify,
171*4882a593Smuzhiyun .verify_struct = xfs_dir3_free_verify,
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* Everything ok in the free block header? */
175*4882a593Smuzhiyun static xfs_failaddr_t
xfs_dir3_free_header_check(struct xfs_inode * dp,xfs_dablk_t fbno,struct xfs_buf * bp)176*4882a593Smuzhiyun xfs_dir3_free_header_check(
177*4882a593Smuzhiyun struct xfs_inode *dp,
178*4882a593Smuzhiyun xfs_dablk_t fbno,
179*4882a593Smuzhiyun struct xfs_buf *bp)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun struct xfs_mount *mp = dp->i_mount;
182*4882a593Smuzhiyun int maxbests = mp->m_dir_geo->free_max_bests;
183*4882a593Smuzhiyun unsigned int firstdb;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
186*4882a593Smuzhiyun xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
187*4882a593Smuzhiyun maxbests;
188*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
189*4882a593Smuzhiyun struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (be32_to_cpu(hdr3->firstdb) != firstdb)
192*4882a593Smuzhiyun return __this_address;
193*4882a593Smuzhiyun if (be32_to_cpu(hdr3->nvalid) > maxbests)
194*4882a593Smuzhiyun return __this_address;
195*4882a593Smuzhiyun if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
196*4882a593Smuzhiyun return __this_address;
197*4882a593Smuzhiyun if (be64_to_cpu(hdr3->hdr.owner) != dp->i_ino)
198*4882a593Smuzhiyun return __this_address;
199*4882a593Smuzhiyun } else {
200*4882a593Smuzhiyun struct xfs_dir2_free_hdr *hdr = bp->b_addr;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (be32_to_cpu(hdr->firstdb) != firstdb)
203*4882a593Smuzhiyun return __this_address;
204*4882a593Smuzhiyun if (be32_to_cpu(hdr->nvalid) > maxbests)
205*4882a593Smuzhiyun return __this_address;
206*4882a593Smuzhiyun if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
207*4882a593Smuzhiyun return __this_address;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun return NULL;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun 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)213*4882a593Smuzhiyun __xfs_dir3_free_read(
214*4882a593Smuzhiyun struct xfs_trans *tp,
215*4882a593Smuzhiyun struct xfs_inode *dp,
216*4882a593Smuzhiyun xfs_dablk_t fbno,
217*4882a593Smuzhiyun unsigned int flags,
218*4882a593Smuzhiyun struct xfs_buf **bpp)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun xfs_failaddr_t fa;
221*4882a593Smuzhiyun int err;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun err = xfs_da_read_buf(tp, dp, fbno, flags, bpp, XFS_DATA_FORK,
224*4882a593Smuzhiyun &xfs_dir3_free_buf_ops);
225*4882a593Smuzhiyun if (err || !*bpp)
226*4882a593Smuzhiyun return err;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /* Check things that we can't do in the verifier. */
229*4882a593Smuzhiyun fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
230*4882a593Smuzhiyun if (fa) {
231*4882a593Smuzhiyun __xfs_buf_mark_corrupt(*bpp, fa);
232*4882a593Smuzhiyun xfs_trans_brelse(tp, *bpp);
233*4882a593Smuzhiyun *bpp = NULL;
234*4882a593Smuzhiyun return -EFSCORRUPTED;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /* try read returns without an error or *bpp if it lands in a hole */
238*4882a593Smuzhiyun if (tp)
239*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun return 0;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun void
xfs_dir2_free_hdr_from_disk(struct xfs_mount * mp,struct xfs_dir3_icfree_hdr * to,struct xfs_dir2_free * from)245*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(
246*4882a593Smuzhiyun struct xfs_mount *mp,
247*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr *to,
248*4882a593Smuzhiyun struct xfs_dir2_free *from)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
251*4882a593Smuzhiyun struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun to->magic = be32_to_cpu(from3->hdr.hdr.magic);
254*4882a593Smuzhiyun to->firstdb = be32_to_cpu(from3->hdr.firstdb);
255*4882a593Smuzhiyun to->nvalid = be32_to_cpu(from3->hdr.nvalid);
256*4882a593Smuzhiyun to->nused = be32_to_cpu(from3->hdr.nused);
257*4882a593Smuzhiyun to->bests = from3->bests;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
260*4882a593Smuzhiyun } else {
261*4882a593Smuzhiyun to->magic = be32_to_cpu(from->hdr.magic);
262*4882a593Smuzhiyun to->firstdb = be32_to_cpu(from->hdr.firstdb);
263*4882a593Smuzhiyun to->nvalid = be32_to_cpu(from->hdr.nvalid);
264*4882a593Smuzhiyun to->nused = be32_to_cpu(from->hdr.nused);
265*4882a593Smuzhiyun to->bests = from->bests;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun static void
xfs_dir2_free_hdr_to_disk(struct xfs_mount * mp,struct xfs_dir2_free * to,struct xfs_dir3_icfree_hdr * from)272*4882a593Smuzhiyun xfs_dir2_free_hdr_to_disk(
273*4882a593Smuzhiyun struct xfs_mount *mp,
274*4882a593Smuzhiyun struct xfs_dir2_free *to,
275*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr *from)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
278*4882a593Smuzhiyun struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun to3->hdr.hdr.magic = cpu_to_be32(from->magic);
283*4882a593Smuzhiyun to3->hdr.firstdb = cpu_to_be32(from->firstdb);
284*4882a593Smuzhiyun to3->hdr.nvalid = cpu_to_be32(from->nvalid);
285*4882a593Smuzhiyun to3->hdr.nused = cpu_to_be32(from->nused);
286*4882a593Smuzhiyun } else {
287*4882a593Smuzhiyun ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun to->hdr.magic = cpu_to_be32(from->magic);
290*4882a593Smuzhiyun to->hdr.firstdb = cpu_to_be32(from->firstdb);
291*4882a593Smuzhiyun to->hdr.nvalid = cpu_to_be32(from->nvalid);
292*4882a593Smuzhiyun to->hdr.nused = cpu_to_be32(from->nused);
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun int
xfs_dir2_free_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t fbno,struct xfs_buf ** bpp)297*4882a593Smuzhiyun xfs_dir2_free_read(
298*4882a593Smuzhiyun struct xfs_trans *tp,
299*4882a593Smuzhiyun struct xfs_inode *dp,
300*4882a593Smuzhiyun xfs_dablk_t fbno,
301*4882a593Smuzhiyun struct xfs_buf **bpp)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun return __xfs_dir3_free_read(tp, dp, fbno, 0, bpp);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun static int
xfs_dir2_free_try_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t fbno,struct xfs_buf ** bpp)307*4882a593Smuzhiyun xfs_dir2_free_try_read(
308*4882a593Smuzhiyun struct xfs_trans *tp,
309*4882a593Smuzhiyun struct xfs_inode *dp,
310*4882a593Smuzhiyun xfs_dablk_t fbno,
311*4882a593Smuzhiyun struct xfs_buf **bpp)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun return __xfs_dir3_free_read(tp, dp, fbno, XFS_DABUF_MAP_HOLE_OK, bpp);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun static int
xfs_dir3_free_get_buf(xfs_da_args_t * args,xfs_dir2_db_t fbno,struct xfs_buf ** bpp)317*4882a593Smuzhiyun xfs_dir3_free_get_buf(
318*4882a593Smuzhiyun xfs_da_args_t *args,
319*4882a593Smuzhiyun xfs_dir2_db_t fbno,
320*4882a593Smuzhiyun struct xfs_buf **bpp)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun struct xfs_trans *tp = args->trans;
323*4882a593Smuzhiyun struct xfs_inode *dp = args->dp;
324*4882a593Smuzhiyun struct xfs_mount *mp = dp->i_mount;
325*4882a593Smuzhiyun struct xfs_buf *bp;
326*4882a593Smuzhiyun int error;
327*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr hdr;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
330*4882a593Smuzhiyun &bp, XFS_DATA_FORK);
331*4882a593Smuzhiyun if (error)
332*4882a593Smuzhiyun return error;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
335*4882a593Smuzhiyun bp->b_ops = &xfs_dir3_free_buf_ops;
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /*
338*4882a593Smuzhiyun * Initialize the new block to be empty, and remember
339*4882a593Smuzhiyun * its first slot as our empty slot.
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
342*4882a593Smuzhiyun memset(&hdr, 0, sizeof(hdr));
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
345*4882a593Smuzhiyun struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun hdr.magic = XFS_DIR3_FREE_MAGIC;
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
350*4882a593Smuzhiyun hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
351*4882a593Smuzhiyun uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
352*4882a593Smuzhiyun } else
353*4882a593Smuzhiyun hdr.magic = XFS_DIR2_FREE_MAGIC;
354*4882a593Smuzhiyun xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
355*4882a593Smuzhiyun *bpp = bp;
356*4882a593Smuzhiyun return 0;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /*
360*4882a593Smuzhiyun * Log entries from a freespace block.
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun 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)363*4882a593Smuzhiyun xfs_dir2_free_log_bests(
364*4882a593Smuzhiyun struct xfs_da_args *args,
365*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr *hdr,
366*4882a593Smuzhiyun struct xfs_buf *bp,
367*4882a593Smuzhiyun int first, /* first entry to log */
368*4882a593Smuzhiyun int last) /* last entry to log */
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun struct xfs_dir2_free *free = bp->b_addr;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
373*4882a593Smuzhiyun free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
374*4882a593Smuzhiyun xfs_trans_log_buf(args->trans, bp,
375*4882a593Smuzhiyun (char *)&hdr->bests[first] - (char *)free,
376*4882a593Smuzhiyun (char *)&hdr->bests[last] - (char *)free +
377*4882a593Smuzhiyun sizeof(hdr->bests[0]) - 1);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /*
381*4882a593Smuzhiyun * Log header from a freespace block.
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun static void
xfs_dir2_free_log_header(struct xfs_da_args * args,struct xfs_buf * bp)384*4882a593Smuzhiyun xfs_dir2_free_log_header(
385*4882a593Smuzhiyun struct xfs_da_args *args,
386*4882a593Smuzhiyun struct xfs_buf *bp)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun #ifdef DEBUG
389*4882a593Smuzhiyun xfs_dir2_free_t *free; /* freespace structure */
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun free = bp->b_addr;
392*4882a593Smuzhiyun ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
393*4882a593Smuzhiyun free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
394*4882a593Smuzhiyun #endif
395*4882a593Smuzhiyun xfs_trans_log_buf(args->trans, bp, 0,
396*4882a593Smuzhiyun args->geo->free_hdr_size - 1);
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun /*
400*4882a593Smuzhiyun * Convert a leaf-format directory to a node-format directory.
401*4882a593Smuzhiyun * We need to change the magic number of the leaf block, and copy
402*4882a593Smuzhiyun * the freespace table out of the leaf block into its own block.
403*4882a593Smuzhiyun */
404*4882a593Smuzhiyun int /* error */
xfs_dir2_leaf_to_node(xfs_da_args_t * args,struct xfs_buf * lbp)405*4882a593Smuzhiyun xfs_dir2_leaf_to_node(
406*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
407*4882a593Smuzhiyun struct xfs_buf *lbp) /* leaf buffer */
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
410*4882a593Smuzhiyun int error; /* error return value */
411*4882a593Smuzhiyun struct xfs_buf *fbp; /* freespace buffer */
412*4882a593Smuzhiyun xfs_dir2_db_t fdb; /* freespace block number */
413*4882a593Smuzhiyun __be16 *from; /* pointer to freespace entry */
414*4882a593Smuzhiyun int i; /* leaf freespace index */
415*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
416*4882a593Smuzhiyun xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
417*4882a593Smuzhiyun int n; /* count of live freespc ents */
418*4882a593Smuzhiyun xfs_dir2_data_off_t off; /* freespace entry value */
419*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
420*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr freehdr;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun trace_xfs_dir2_leaf_to_node(args);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun dp = args->dp;
425*4882a593Smuzhiyun tp = args->trans;
426*4882a593Smuzhiyun /*
427*4882a593Smuzhiyun * Add a freespace block to the directory.
428*4882a593Smuzhiyun */
429*4882a593Smuzhiyun if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
430*4882a593Smuzhiyun return error;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun * Get the buffer for the new freespace block.
435*4882a593Smuzhiyun */
436*4882a593Smuzhiyun error = xfs_dir3_free_get_buf(args, fdb, &fbp);
437*4882a593Smuzhiyun if (error)
438*4882a593Smuzhiyun return error;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
441*4882a593Smuzhiyun leaf = lbp->b_addr;
442*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
443*4882a593Smuzhiyun if (be32_to_cpu(ltp->bestcount) >
444*4882a593Smuzhiyun (uint)dp->i_d.di_size / args->geo->blksize) {
445*4882a593Smuzhiyun xfs_buf_mark_corrupt(lbp);
446*4882a593Smuzhiyun return -EFSCORRUPTED;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /*
450*4882a593Smuzhiyun * Copy freespace entries from the leaf block to the new block.
451*4882a593Smuzhiyun * Count active entries.
452*4882a593Smuzhiyun */
453*4882a593Smuzhiyun from = xfs_dir2_leaf_bests_p(ltp);
454*4882a593Smuzhiyun for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
455*4882a593Smuzhiyun off = be16_to_cpu(*from);
456*4882a593Smuzhiyun if (off != NULLDATAOFF)
457*4882a593Smuzhiyun n++;
458*4882a593Smuzhiyun freehdr.bests[i] = cpu_to_be16(off);
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun /*
462*4882a593Smuzhiyun * Now initialize the freespace block header.
463*4882a593Smuzhiyun */
464*4882a593Smuzhiyun freehdr.nused = n;
465*4882a593Smuzhiyun freehdr.nvalid = be32_to_cpu(ltp->bestcount);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
468*4882a593Smuzhiyun xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
469*4882a593Smuzhiyun xfs_dir2_free_log_header(args, fbp);
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /*
472*4882a593Smuzhiyun * Converting the leaf to a leafnode is just a matter of changing the
473*4882a593Smuzhiyun * magic number and the ops. Do the change directly to the buffer as
474*4882a593Smuzhiyun * it's less work (and less code) than decoding the header to host
475*4882a593Smuzhiyun * format and back again.
476*4882a593Smuzhiyun */
477*4882a593Smuzhiyun if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
478*4882a593Smuzhiyun leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
479*4882a593Smuzhiyun else
480*4882a593Smuzhiyun leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
481*4882a593Smuzhiyun lbp->b_ops = &xfs_dir3_leafn_buf_ops;
482*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
483*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, lbp);
484*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
485*4882a593Smuzhiyun return 0;
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * Add a leaf entry to a leaf block in a node-form directory.
490*4882a593Smuzhiyun * The other work necessary is done from the caller.
491*4882a593Smuzhiyun */
492*4882a593Smuzhiyun static int /* error */
xfs_dir2_leafn_add(struct xfs_buf * bp,struct xfs_da_args * args,int index)493*4882a593Smuzhiyun xfs_dir2_leafn_add(
494*4882a593Smuzhiyun struct xfs_buf *bp, /* leaf buffer */
495*4882a593Smuzhiyun struct xfs_da_args *args, /* operation arguments */
496*4882a593Smuzhiyun int index) /* insertion pt for new entry */
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
499*4882a593Smuzhiyun struct xfs_inode *dp = args->dp;
500*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf = bp->b_addr;
501*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *lep;
502*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents;
503*4882a593Smuzhiyun int compact; /* compacting stale leaves */
504*4882a593Smuzhiyun int highstale = 0; /* next stale entry */
505*4882a593Smuzhiyun int lfloghigh; /* high leaf entry logging */
506*4882a593Smuzhiyun int lfloglow; /* low leaf entry logging */
507*4882a593Smuzhiyun int lowstale = 0; /* previous stale entry */
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun trace_xfs_dir2_leafn_add(args, index);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
512*4882a593Smuzhiyun ents = leafhdr.ents;
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun /*
515*4882a593Smuzhiyun * Quick check just to make sure we are not going to index
516*4882a593Smuzhiyun * into other peoples memory
517*4882a593Smuzhiyun */
518*4882a593Smuzhiyun if (index < 0) {
519*4882a593Smuzhiyun xfs_buf_mark_corrupt(bp);
520*4882a593Smuzhiyun return -EFSCORRUPTED;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun /*
524*4882a593Smuzhiyun * If there are already the maximum number of leaf entries in
525*4882a593Smuzhiyun * the block, if there are no stale entries it won't fit.
526*4882a593Smuzhiyun * Caller will do a split. If there are stale entries we'll do
527*4882a593Smuzhiyun * a compact.
528*4882a593Smuzhiyun */
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun if (leafhdr.count == args->geo->leaf_max_ents) {
531*4882a593Smuzhiyun if (!leafhdr.stale)
532*4882a593Smuzhiyun return -ENOSPC;
533*4882a593Smuzhiyun compact = leafhdr.stale > 1;
534*4882a593Smuzhiyun } else
535*4882a593Smuzhiyun compact = 0;
536*4882a593Smuzhiyun ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
537*4882a593Smuzhiyun ASSERT(index == leafhdr.count ||
538*4882a593Smuzhiyun be32_to_cpu(ents[index].hashval) >= args->hashval);
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun if (args->op_flags & XFS_DA_OP_JUSTCHECK)
541*4882a593Smuzhiyun return 0;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun * Compact out all but one stale leaf entry. Leaves behind
545*4882a593Smuzhiyun * the entry closest to index.
546*4882a593Smuzhiyun */
547*4882a593Smuzhiyun if (compact)
548*4882a593Smuzhiyun xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
549*4882a593Smuzhiyun &highstale, &lfloglow, &lfloghigh);
550*4882a593Smuzhiyun else if (leafhdr.stale) {
551*4882a593Smuzhiyun /*
552*4882a593Smuzhiyun * Set impossible logging indices for this case.
553*4882a593Smuzhiyun */
554*4882a593Smuzhiyun lfloglow = leafhdr.count;
555*4882a593Smuzhiyun lfloghigh = -1;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun /*
559*4882a593Smuzhiyun * Insert the new entry, log everything.
560*4882a593Smuzhiyun */
561*4882a593Smuzhiyun lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
562*4882a593Smuzhiyun highstale, &lfloglow, &lfloghigh);
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun lep->hashval = cpu_to_be32(args->hashval);
565*4882a593Smuzhiyun lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
566*4882a593Smuzhiyun args->blkno, args->index));
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
569*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, bp);
570*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
571*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, bp);
572*4882a593Smuzhiyun return 0;
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun #ifdef DEBUG
576*4882a593Smuzhiyun static void
xfs_dir2_free_hdr_check(struct xfs_inode * dp,struct xfs_buf * bp,xfs_dir2_db_t db)577*4882a593Smuzhiyun xfs_dir2_free_hdr_check(
578*4882a593Smuzhiyun struct xfs_inode *dp,
579*4882a593Smuzhiyun struct xfs_buf *bp,
580*4882a593Smuzhiyun xfs_dir2_db_t db)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr hdr;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
587*4882a593Smuzhiyun ASSERT(hdr.firstdb <= db);
588*4882a593Smuzhiyun ASSERT(db < hdr.firstdb + hdr.nvalid);
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun #else
591*4882a593Smuzhiyun #define xfs_dir2_free_hdr_check(dp, bp, db)
592*4882a593Smuzhiyun #endif /* DEBUG */
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun /*
595*4882a593Smuzhiyun * Return the last hash value in the leaf.
596*4882a593Smuzhiyun * Stale entries are ok.
597*4882a593Smuzhiyun */
598*4882a593Smuzhiyun xfs_dahash_t /* hash value */
xfs_dir2_leaf_lasthash(struct xfs_inode * dp,struct xfs_buf * bp,int * count)599*4882a593Smuzhiyun xfs_dir2_leaf_lasthash(
600*4882a593Smuzhiyun struct xfs_inode *dp,
601*4882a593Smuzhiyun struct xfs_buf *bp, /* leaf buffer */
602*4882a593Smuzhiyun int *count) /* count of entries in leaf */
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
609*4882a593Smuzhiyun leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
610*4882a593Smuzhiyun leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
611*4882a593Smuzhiyun leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun if (count)
614*4882a593Smuzhiyun *count = leafhdr.count;
615*4882a593Smuzhiyun if (!leafhdr.count)
616*4882a593Smuzhiyun return 0;
617*4882a593Smuzhiyun return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun /*
621*4882a593Smuzhiyun * Look up a leaf entry for space to add a name in a node-format leaf block.
622*4882a593Smuzhiyun * The extrablk in state is a freespace block.
623*4882a593Smuzhiyun */
624*4882a593Smuzhiyun STATIC int
xfs_dir2_leafn_lookup_for_addname(struct xfs_buf * bp,xfs_da_args_t * args,int * indexp,xfs_da_state_t * state)625*4882a593Smuzhiyun xfs_dir2_leafn_lookup_for_addname(
626*4882a593Smuzhiyun struct xfs_buf *bp, /* leaf buffer */
627*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
628*4882a593Smuzhiyun int *indexp, /* out: leaf entry index */
629*4882a593Smuzhiyun xfs_da_state_t *state) /* state to fill in */
630*4882a593Smuzhiyun {
631*4882a593Smuzhiyun struct xfs_buf *curbp = NULL; /* current data/free buffer */
632*4882a593Smuzhiyun xfs_dir2_db_t curdb = -1; /* current data block number */
633*4882a593Smuzhiyun xfs_dir2_db_t curfdb = -1; /* current free block number */
634*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
635*4882a593Smuzhiyun int error; /* error return value */
636*4882a593Smuzhiyun int fi; /* free entry index */
637*4882a593Smuzhiyun xfs_dir2_free_t *free = NULL; /* free block structure */
638*4882a593Smuzhiyun int index; /* leaf entry index */
639*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
640*4882a593Smuzhiyun int length; /* length of new data entry */
641*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry */
642*4882a593Smuzhiyun xfs_mount_t *mp; /* filesystem mount point */
643*4882a593Smuzhiyun xfs_dir2_db_t newdb; /* new data block number */
644*4882a593Smuzhiyun xfs_dir2_db_t newfdb; /* new free block number */
645*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
646*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun dp = args->dp;
649*4882a593Smuzhiyun tp = args->trans;
650*4882a593Smuzhiyun mp = dp->i_mount;
651*4882a593Smuzhiyun leaf = bp->b_addr;
652*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, bp);
655*4882a593Smuzhiyun ASSERT(leafhdr.count > 0);
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun /*
658*4882a593Smuzhiyun * Look up the hash value in the leaf entries.
659*4882a593Smuzhiyun */
660*4882a593Smuzhiyun index = xfs_dir2_leaf_search_hash(args, bp);
661*4882a593Smuzhiyun /*
662*4882a593Smuzhiyun * Do we have a buffer coming in?
663*4882a593Smuzhiyun */
664*4882a593Smuzhiyun if (state->extravalid) {
665*4882a593Smuzhiyun /* If so, it's a free block buffer, get the block number. */
666*4882a593Smuzhiyun curbp = state->extrablk.bp;
667*4882a593Smuzhiyun curfdb = state->extrablk.blkno;
668*4882a593Smuzhiyun free = curbp->b_addr;
669*4882a593Smuzhiyun ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
670*4882a593Smuzhiyun free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun length = xfs_dir2_data_entsize(mp, args->namelen);
673*4882a593Smuzhiyun /*
674*4882a593Smuzhiyun * Loop over leaf entries with the right hash value.
675*4882a593Smuzhiyun */
676*4882a593Smuzhiyun for (lep = &leafhdr.ents[index];
677*4882a593Smuzhiyun index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
678*4882a593Smuzhiyun lep++, index++) {
679*4882a593Smuzhiyun /*
680*4882a593Smuzhiyun * Skip stale leaf entries.
681*4882a593Smuzhiyun */
682*4882a593Smuzhiyun if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
683*4882a593Smuzhiyun continue;
684*4882a593Smuzhiyun /*
685*4882a593Smuzhiyun * Pull the data block number from the entry.
686*4882a593Smuzhiyun */
687*4882a593Smuzhiyun newdb = xfs_dir2_dataptr_to_db(args->geo,
688*4882a593Smuzhiyun be32_to_cpu(lep->address));
689*4882a593Smuzhiyun /*
690*4882a593Smuzhiyun * For addname, we're looking for a place to put the new entry.
691*4882a593Smuzhiyun * We want to use a data block with an entry of equal
692*4882a593Smuzhiyun * hash value to ours if there is one with room.
693*4882a593Smuzhiyun *
694*4882a593Smuzhiyun * If this block isn't the data block we already have
695*4882a593Smuzhiyun * in hand, take a look at it.
696*4882a593Smuzhiyun */
697*4882a593Smuzhiyun if (newdb != curdb) {
698*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr freehdr;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun curdb = newdb;
701*4882a593Smuzhiyun /*
702*4882a593Smuzhiyun * Convert the data block to the free block
703*4882a593Smuzhiyun * holding its freespace information.
704*4882a593Smuzhiyun */
705*4882a593Smuzhiyun newfdb = xfs_dir2_db_to_fdb(args->geo, newdb);
706*4882a593Smuzhiyun /*
707*4882a593Smuzhiyun * If it's not the one we have in hand, read it in.
708*4882a593Smuzhiyun */
709*4882a593Smuzhiyun if (newfdb != curfdb) {
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun * If we had one before, drop it.
712*4882a593Smuzhiyun */
713*4882a593Smuzhiyun if (curbp)
714*4882a593Smuzhiyun xfs_trans_brelse(tp, curbp);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun error = xfs_dir2_free_read(tp, dp,
717*4882a593Smuzhiyun xfs_dir2_db_to_da(args->geo,
718*4882a593Smuzhiyun newfdb),
719*4882a593Smuzhiyun &curbp);
720*4882a593Smuzhiyun if (error)
721*4882a593Smuzhiyun return error;
722*4882a593Smuzhiyun free = curbp->b_addr;
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun xfs_dir2_free_hdr_check(dp, curbp, curdb);
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun /*
727*4882a593Smuzhiyun * Get the index for our entry.
728*4882a593Smuzhiyun */
729*4882a593Smuzhiyun fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
730*4882a593Smuzhiyun /*
731*4882a593Smuzhiyun * If it has room, return it.
732*4882a593Smuzhiyun */
733*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
734*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp,
735*4882a593Smuzhiyun freehdr.bests[fi] ==
736*4882a593Smuzhiyun cpu_to_be16(NULLDATAOFF))) {
737*4882a593Smuzhiyun if (curfdb != newfdb)
738*4882a593Smuzhiyun xfs_trans_brelse(tp, curbp);
739*4882a593Smuzhiyun return -EFSCORRUPTED;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun curfdb = newfdb;
742*4882a593Smuzhiyun if (be16_to_cpu(freehdr.bests[fi]) >= length)
743*4882a593Smuzhiyun goto out;
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun /* Didn't find any space */
747*4882a593Smuzhiyun fi = -1;
748*4882a593Smuzhiyun out:
749*4882a593Smuzhiyun ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
750*4882a593Smuzhiyun if (curbp) {
751*4882a593Smuzhiyun /* Giving back a free block. */
752*4882a593Smuzhiyun state->extravalid = 1;
753*4882a593Smuzhiyun state->extrablk.bp = curbp;
754*4882a593Smuzhiyun state->extrablk.index = fi;
755*4882a593Smuzhiyun state->extrablk.blkno = curfdb;
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun /*
758*4882a593Smuzhiyun * Important: this magic number is not in the buffer - it's for
759*4882a593Smuzhiyun * buffer type information and therefore only the free/data type
760*4882a593Smuzhiyun * matters here, not whether CRCs are enabled or not.
761*4882a593Smuzhiyun */
762*4882a593Smuzhiyun state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
763*4882a593Smuzhiyun } else {
764*4882a593Smuzhiyun state->extravalid = 0;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun /*
767*4882a593Smuzhiyun * Return the index, that will be the insertion point.
768*4882a593Smuzhiyun */
769*4882a593Smuzhiyun *indexp = index;
770*4882a593Smuzhiyun return -ENOENT;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun /*
774*4882a593Smuzhiyun * Look up a leaf entry in a node-format leaf block.
775*4882a593Smuzhiyun * The extrablk in state a data block.
776*4882a593Smuzhiyun */
777*4882a593Smuzhiyun STATIC int
xfs_dir2_leafn_lookup_for_entry(struct xfs_buf * bp,xfs_da_args_t * args,int * indexp,xfs_da_state_t * state)778*4882a593Smuzhiyun xfs_dir2_leafn_lookup_for_entry(
779*4882a593Smuzhiyun struct xfs_buf *bp, /* leaf buffer */
780*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
781*4882a593Smuzhiyun int *indexp, /* out: leaf entry index */
782*4882a593Smuzhiyun xfs_da_state_t *state) /* state to fill in */
783*4882a593Smuzhiyun {
784*4882a593Smuzhiyun struct xfs_buf *curbp = NULL; /* current data/free buffer */
785*4882a593Smuzhiyun xfs_dir2_db_t curdb = -1; /* current data block number */
786*4882a593Smuzhiyun xfs_dir2_data_entry_t *dep; /* data block entry */
787*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
788*4882a593Smuzhiyun int error; /* error return value */
789*4882a593Smuzhiyun int index; /* leaf entry index */
790*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
791*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry */
792*4882a593Smuzhiyun xfs_mount_t *mp; /* filesystem mount point */
793*4882a593Smuzhiyun xfs_dir2_db_t newdb; /* new data block number */
794*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
795*4882a593Smuzhiyun enum xfs_dacmp cmp; /* comparison result */
796*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun dp = args->dp;
799*4882a593Smuzhiyun tp = args->trans;
800*4882a593Smuzhiyun mp = dp->i_mount;
801*4882a593Smuzhiyun leaf = bp->b_addr;
802*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, bp);
805*4882a593Smuzhiyun if (leafhdr.count <= 0) {
806*4882a593Smuzhiyun xfs_buf_mark_corrupt(bp);
807*4882a593Smuzhiyun return -EFSCORRUPTED;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun /*
811*4882a593Smuzhiyun * Look up the hash value in the leaf entries.
812*4882a593Smuzhiyun */
813*4882a593Smuzhiyun index = xfs_dir2_leaf_search_hash(args, bp);
814*4882a593Smuzhiyun /*
815*4882a593Smuzhiyun * Do we have a buffer coming in?
816*4882a593Smuzhiyun */
817*4882a593Smuzhiyun if (state->extravalid) {
818*4882a593Smuzhiyun curbp = state->extrablk.bp;
819*4882a593Smuzhiyun curdb = state->extrablk.blkno;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun /*
822*4882a593Smuzhiyun * Loop over leaf entries with the right hash value.
823*4882a593Smuzhiyun */
824*4882a593Smuzhiyun for (lep = &leafhdr.ents[index];
825*4882a593Smuzhiyun index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
826*4882a593Smuzhiyun lep++, index++) {
827*4882a593Smuzhiyun /*
828*4882a593Smuzhiyun * Skip stale leaf entries.
829*4882a593Smuzhiyun */
830*4882a593Smuzhiyun if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
831*4882a593Smuzhiyun continue;
832*4882a593Smuzhiyun /*
833*4882a593Smuzhiyun * Pull the data block number from the entry.
834*4882a593Smuzhiyun */
835*4882a593Smuzhiyun newdb = xfs_dir2_dataptr_to_db(args->geo,
836*4882a593Smuzhiyun be32_to_cpu(lep->address));
837*4882a593Smuzhiyun /*
838*4882a593Smuzhiyun * Not adding a new entry, so we really want to find
839*4882a593Smuzhiyun * the name given to us.
840*4882a593Smuzhiyun *
841*4882a593Smuzhiyun * If it's a different data block, go get it.
842*4882a593Smuzhiyun */
843*4882a593Smuzhiyun if (newdb != curdb) {
844*4882a593Smuzhiyun /*
845*4882a593Smuzhiyun * If we had a block before that we aren't saving
846*4882a593Smuzhiyun * for a CI name, drop it
847*4882a593Smuzhiyun */
848*4882a593Smuzhiyun if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
849*4882a593Smuzhiyun curdb != state->extrablk.blkno))
850*4882a593Smuzhiyun xfs_trans_brelse(tp, curbp);
851*4882a593Smuzhiyun /*
852*4882a593Smuzhiyun * If needing the block that is saved with a CI match,
853*4882a593Smuzhiyun * use it otherwise read in the new data block.
854*4882a593Smuzhiyun */
855*4882a593Smuzhiyun if (args->cmpresult != XFS_CMP_DIFFERENT &&
856*4882a593Smuzhiyun newdb == state->extrablk.blkno) {
857*4882a593Smuzhiyun ASSERT(state->extravalid);
858*4882a593Smuzhiyun curbp = state->extrablk.bp;
859*4882a593Smuzhiyun } else {
860*4882a593Smuzhiyun error = xfs_dir3_data_read(tp, dp,
861*4882a593Smuzhiyun xfs_dir2_db_to_da(args->geo,
862*4882a593Smuzhiyun newdb),
863*4882a593Smuzhiyun 0, &curbp);
864*4882a593Smuzhiyun if (error)
865*4882a593Smuzhiyun return error;
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun xfs_dir3_data_check(dp, curbp);
868*4882a593Smuzhiyun curdb = newdb;
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun /*
871*4882a593Smuzhiyun * Point to the data entry.
872*4882a593Smuzhiyun */
873*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
874*4882a593Smuzhiyun xfs_dir2_dataptr_to_off(args->geo,
875*4882a593Smuzhiyun be32_to_cpu(lep->address)));
876*4882a593Smuzhiyun /*
877*4882a593Smuzhiyun * Compare the entry and if it's an exact match, return
878*4882a593Smuzhiyun * EEXIST immediately. If it's the first case-insensitive
879*4882a593Smuzhiyun * match, store the block & inode number and continue looking.
880*4882a593Smuzhiyun */
881*4882a593Smuzhiyun cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
882*4882a593Smuzhiyun if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
883*4882a593Smuzhiyun /* If there is a CI match block, drop it */
884*4882a593Smuzhiyun if (args->cmpresult != XFS_CMP_DIFFERENT &&
885*4882a593Smuzhiyun curdb != state->extrablk.blkno)
886*4882a593Smuzhiyun xfs_trans_brelse(tp, state->extrablk.bp);
887*4882a593Smuzhiyun args->cmpresult = cmp;
888*4882a593Smuzhiyun args->inumber = be64_to_cpu(dep->inumber);
889*4882a593Smuzhiyun args->filetype = xfs_dir2_data_get_ftype(mp, dep);
890*4882a593Smuzhiyun *indexp = index;
891*4882a593Smuzhiyun state->extravalid = 1;
892*4882a593Smuzhiyun state->extrablk.bp = curbp;
893*4882a593Smuzhiyun state->extrablk.blkno = curdb;
894*4882a593Smuzhiyun state->extrablk.index = (int)((char *)dep -
895*4882a593Smuzhiyun (char *)curbp->b_addr);
896*4882a593Smuzhiyun state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
897*4882a593Smuzhiyun curbp->b_ops = &xfs_dir3_data_buf_ops;
898*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
899*4882a593Smuzhiyun if (cmp == XFS_CMP_EXACT)
900*4882a593Smuzhiyun return -EEXIST;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
904*4882a593Smuzhiyun if (curbp) {
905*4882a593Smuzhiyun if (args->cmpresult == XFS_CMP_DIFFERENT) {
906*4882a593Smuzhiyun /* Giving back last used data block. */
907*4882a593Smuzhiyun state->extravalid = 1;
908*4882a593Smuzhiyun state->extrablk.bp = curbp;
909*4882a593Smuzhiyun state->extrablk.index = -1;
910*4882a593Smuzhiyun state->extrablk.blkno = curdb;
911*4882a593Smuzhiyun state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
912*4882a593Smuzhiyun curbp->b_ops = &xfs_dir3_data_buf_ops;
913*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
914*4882a593Smuzhiyun } else {
915*4882a593Smuzhiyun /* If the curbp is not the CI match block, drop it */
916*4882a593Smuzhiyun if (state->extrablk.bp != curbp)
917*4882a593Smuzhiyun xfs_trans_brelse(tp, curbp);
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun } else {
920*4882a593Smuzhiyun state->extravalid = 0;
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun *indexp = index;
923*4882a593Smuzhiyun return -ENOENT;
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun /*
927*4882a593Smuzhiyun * Look up a leaf entry in a node-format leaf block.
928*4882a593Smuzhiyun * If this is an addname then the extrablk in state is a freespace block,
929*4882a593Smuzhiyun * otherwise it's a data block.
930*4882a593Smuzhiyun */
931*4882a593Smuzhiyun int
xfs_dir2_leafn_lookup_int(struct xfs_buf * bp,xfs_da_args_t * args,int * indexp,xfs_da_state_t * state)932*4882a593Smuzhiyun xfs_dir2_leafn_lookup_int(
933*4882a593Smuzhiyun struct xfs_buf *bp, /* leaf buffer */
934*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
935*4882a593Smuzhiyun int *indexp, /* out: leaf entry index */
936*4882a593Smuzhiyun xfs_da_state_t *state) /* state to fill in */
937*4882a593Smuzhiyun {
938*4882a593Smuzhiyun if (args->op_flags & XFS_DA_OP_ADDNAME)
939*4882a593Smuzhiyun return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
940*4882a593Smuzhiyun state);
941*4882a593Smuzhiyun return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun /*
945*4882a593Smuzhiyun * Move count leaf entries from source to destination leaf.
946*4882a593Smuzhiyun * Log entries and headers. Stale entries are preserved.
947*4882a593Smuzhiyun */
948*4882a593Smuzhiyun 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)949*4882a593Smuzhiyun xfs_dir3_leafn_moveents(
950*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
951*4882a593Smuzhiyun struct xfs_buf *bp_s, /* source */
952*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *shdr,
953*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *sents,
954*4882a593Smuzhiyun int start_s,/* source leaf index */
955*4882a593Smuzhiyun struct xfs_buf *bp_d, /* destination */
956*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *dhdr,
957*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *dents,
958*4882a593Smuzhiyun int start_d,/* destination leaf index */
959*4882a593Smuzhiyun int count) /* count of leaves to copy */
960*4882a593Smuzhiyun {
961*4882a593Smuzhiyun int stale; /* count stale leaves copied */
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun /*
966*4882a593Smuzhiyun * Silently return if nothing to do.
967*4882a593Smuzhiyun */
968*4882a593Smuzhiyun if (count == 0)
969*4882a593Smuzhiyun return;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun /*
972*4882a593Smuzhiyun * If the destination index is not the end of the current
973*4882a593Smuzhiyun * destination leaf entries, open up a hole in the destination
974*4882a593Smuzhiyun * to hold the new entries.
975*4882a593Smuzhiyun */
976*4882a593Smuzhiyun if (start_d < dhdr->count) {
977*4882a593Smuzhiyun memmove(&dents[start_d + count], &dents[start_d],
978*4882a593Smuzhiyun (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
979*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
980*4882a593Smuzhiyun count + dhdr->count - 1);
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun /*
983*4882a593Smuzhiyun * If the source has stale leaves, count the ones in the copy range
984*4882a593Smuzhiyun * so we can update the header correctly.
985*4882a593Smuzhiyun */
986*4882a593Smuzhiyun if (shdr->stale) {
987*4882a593Smuzhiyun int i; /* temp leaf index */
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun for (i = start_s, stale = 0; i < start_s + count; i++) {
990*4882a593Smuzhiyun if (sents[i].address ==
991*4882a593Smuzhiyun cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
992*4882a593Smuzhiyun stale++;
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun } else
995*4882a593Smuzhiyun stale = 0;
996*4882a593Smuzhiyun /*
997*4882a593Smuzhiyun * Copy the leaf entries from source to destination.
998*4882a593Smuzhiyun */
999*4882a593Smuzhiyun memcpy(&dents[start_d], &sents[start_s],
1000*4882a593Smuzhiyun count * sizeof(xfs_dir2_leaf_entry_t));
1001*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun /*
1004*4882a593Smuzhiyun * If there are source entries after the ones we copied,
1005*4882a593Smuzhiyun * delete the ones we copied by sliding the next ones down.
1006*4882a593Smuzhiyun */
1007*4882a593Smuzhiyun if (start_s + count < shdr->count) {
1008*4882a593Smuzhiyun memmove(&sents[start_s], &sents[start_s + count],
1009*4882a593Smuzhiyun count * sizeof(xfs_dir2_leaf_entry_t));
1010*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
1011*4882a593Smuzhiyun start_s + count - 1);
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun /*
1015*4882a593Smuzhiyun * Update the headers and log them.
1016*4882a593Smuzhiyun */
1017*4882a593Smuzhiyun shdr->count -= count;
1018*4882a593Smuzhiyun shdr->stale -= stale;
1019*4882a593Smuzhiyun dhdr->count += count;
1020*4882a593Smuzhiyun dhdr->stale += stale;
1021*4882a593Smuzhiyun }
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun /*
1024*4882a593Smuzhiyun * Determine the sort order of two leaf blocks.
1025*4882a593Smuzhiyun * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
1026*4882a593Smuzhiyun */
1027*4882a593Smuzhiyun int /* sort order */
xfs_dir2_leafn_order(struct xfs_inode * dp,struct xfs_buf * leaf1_bp,struct xfs_buf * leaf2_bp)1028*4882a593Smuzhiyun xfs_dir2_leafn_order(
1029*4882a593Smuzhiyun struct xfs_inode *dp,
1030*4882a593Smuzhiyun struct xfs_buf *leaf1_bp, /* leaf1 buffer */
1031*4882a593Smuzhiyun struct xfs_buf *leaf2_bp) /* leaf2 buffer */
1032*4882a593Smuzhiyun {
1033*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
1034*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
1035*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents1;
1036*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents2;
1037*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr hdr1;
1038*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr hdr2;
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1041*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1042*4882a593Smuzhiyun ents1 = hdr1.ents;
1043*4882a593Smuzhiyun ents2 = hdr2.ents;
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun if (hdr1.count > 0 && hdr2.count > 0 &&
1046*4882a593Smuzhiyun (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
1047*4882a593Smuzhiyun be32_to_cpu(ents2[hdr2.count - 1].hashval) <
1048*4882a593Smuzhiyun be32_to_cpu(ents1[hdr1.count - 1].hashval)))
1049*4882a593Smuzhiyun return 1;
1050*4882a593Smuzhiyun return 0;
1051*4882a593Smuzhiyun }
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun /*
1054*4882a593Smuzhiyun * Rebalance leaf entries between two leaf blocks.
1055*4882a593Smuzhiyun * This is actually only called when the second block is new,
1056*4882a593Smuzhiyun * though the code deals with the general case.
1057*4882a593Smuzhiyun * A new entry will be inserted in one of the blocks, and that
1058*4882a593Smuzhiyun * entry is taken into account when balancing.
1059*4882a593Smuzhiyun */
1060*4882a593Smuzhiyun static void
xfs_dir2_leafn_rebalance(xfs_da_state_t * state,xfs_da_state_blk_t * blk1,xfs_da_state_blk_t * blk2)1061*4882a593Smuzhiyun xfs_dir2_leafn_rebalance(
1062*4882a593Smuzhiyun xfs_da_state_t *state, /* btree cursor */
1063*4882a593Smuzhiyun xfs_da_state_blk_t *blk1, /* first btree block */
1064*4882a593Smuzhiyun xfs_da_state_blk_t *blk2) /* second btree block */
1065*4882a593Smuzhiyun {
1066*4882a593Smuzhiyun xfs_da_args_t *args; /* operation arguments */
1067*4882a593Smuzhiyun int count; /* count (& direction) leaves */
1068*4882a593Smuzhiyun int isleft; /* new goes in left leaf */
1069*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1070*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1071*4882a593Smuzhiyun int mid; /* midpoint leaf index */
1072*4882a593Smuzhiyun #if defined(DEBUG) || defined(XFS_WARN)
1073*4882a593Smuzhiyun int oldstale; /* old count of stale leaves */
1074*4882a593Smuzhiyun #endif
1075*4882a593Smuzhiyun int oldsum; /* old total leaf count */
1076*4882a593Smuzhiyun int swap_blocks; /* swapped leaf blocks */
1077*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents1;
1078*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents2;
1079*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr hdr1;
1080*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr hdr2;
1081*4882a593Smuzhiyun struct xfs_inode *dp = state->args->dp;
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun args = state->args;
1084*4882a593Smuzhiyun /*
1085*4882a593Smuzhiyun * If the block order is wrong, swap the arguments.
1086*4882a593Smuzhiyun */
1087*4882a593Smuzhiyun swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1088*4882a593Smuzhiyun if (swap_blocks)
1089*4882a593Smuzhiyun swap(blk1, blk2);
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun leaf1 = blk1->bp->b_addr;
1092*4882a593Smuzhiyun leaf2 = blk2->bp->b_addr;
1093*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1094*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1095*4882a593Smuzhiyun ents1 = hdr1.ents;
1096*4882a593Smuzhiyun ents2 = hdr2.ents;
1097*4882a593Smuzhiyun
1098*4882a593Smuzhiyun oldsum = hdr1.count + hdr2.count;
1099*4882a593Smuzhiyun #if defined(DEBUG) || defined(XFS_WARN)
1100*4882a593Smuzhiyun oldstale = hdr1.stale + hdr2.stale;
1101*4882a593Smuzhiyun #endif
1102*4882a593Smuzhiyun mid = oldsum >> 1;
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun /*
1105*4882a593Smuzhiyun * If the old leaf count was odd then the new one will be even,
1106*4882a593Smuzhiyun * so we need to divide the new count evenly.
1107*4882a593Smuzhiyun */
1108*4882a593Smuzhiyun if (oldsum & 1) {
1109*4882a593Smuzhiyun xfs_dahash_t midhash; /* middle entry hash value */
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun if (mid >= hdr1.count)
1112*4882a593Smuzhiyun midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
1113*4882a593Smuzhiyun else
1114*4882a593Smuzhiyun midhash = be32_to_cpu(ents1[mid].hashval);
1115*4882a593Smuzhiyun isleft = args->hashval <= midhash;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun /*
1118*4882a593Smuzhiyun * If the old count is even then the new count is odd, so there's
1119*4882a593Smuzhiyun * no preferred side for the new entry.
1120*4882a593Smuzhiyun * Pick the left one.
1121*4882a593Smuzhiyun */
1122*4882a593Smuzhiyun else
1123*4882a593Smuzhiyun isleft = 1;
1124*4882a593Smuzhiyun /*
1125*4882a593Smuzhiyun * Calculate moved entry count. Positive means left-to-right,
1126*4882a593Smuzhiyun * negative means right-to-left. Then move the entries.
1127*4882a593Smuzhiyun */
1128*4882a593Smuzhiyun count = hdr1.count - mid + (isleft == 0);
1129*4882a593Smuzhiyun if (count > 0)
1130*4882a593Smuzhiyun xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1131*4882a593Smuzhiyun hdr1.count - count, blk2->bp,
1132*4882a593Smuzhiyun &hdr2, ents2, 0, count);
1133*4882a593Smuzhiyun else if (count < 0)
1134*4882a593Smuzhiyun xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1135*4882a593Smuzhiyun blk1->bp, &hdr1, ents1,
1136*4882a593Smuzhiyun hdr1.count, count);
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun ASSERT(hdr1.count + hdr2.count == oldsum);
1139*4882a593Smuzhiyun ASSERT(hdr1.stale + hdr2.stale == oldstale);
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun /* log the changes made when moving the entries */
1142*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1143*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
1144*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, blk1->bp);
1145*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, blk2->bp);
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, blk1->bp);
1148*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, blk2->bp);
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun /*
1151*4882a593Smuzhiyun * Mark whether we're inserting into the old or new leaf.
1152*4882a593Smuzhiyun */
1153*4882a593Smuzhiyun if (hdr1.count < hdr2.count)
1154*4882a593Smuzhiyun state->inleaf = swap_blocks;
1155*4882a593Smuzhiyun else if (hdr1.count > hdr2.count)
1156*4882a593Smuzhiyun state->inleaf = !swap_blocks;
1157*4882a593Smuzhiyun else
1158*4882a593Smuzhiyun state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
1159*4882a593Smuzhiyun /*
1160*4882a593Smuzhiyun * Adjust the expected index for insertion.
1161*4882a593Smuzhiyun */
1162*4882a593Smuzhiyun if (!state->inleaf)
1163*4882a593Smuzhiyun blk2->index = blk1->index - hdr1.count;
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun /*
1166*4882a593Smuzhiyun * Finally sanity check just to make sure we are not returning a
1167*4882a593Smuzhiyun * negative index
1168*4882a593Smuzhiyun */
1169*4882a593Smuzhiyun if (blk2->index < 0) {
1170*4882a593Smuzhiyun state->inleaf = 1;
1171*4882a593Smuzhiyun blk2->index = 0;
1172*4882a593Smuzhiyun xfs_alert(dp->i_mount,
1173*4882a593Smuzhiyun "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1174*4882a593Smuzhiyun __func__, blk1->index);
1175*4882a593Smuzhiyun }
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun 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)1179*4882a593Smuzhiyun xfs_dir3_data_block_free(
1180*4882a593Smuzhiyun xfs_da_args_t *args,
1181*4882a593Smuzhiyun struct xfs_dir2_data_hdr *hdr,
1182*4882a593Smuzhiyun struct xfs_dir2_free *free,
1183*4882a593Smuzhiyun xfs_dir2_db_t fdb,
1184*4882a593Smuzhiyun int findex,
1185*4882a593Smuzhiyun struct xfs_buf *fbp,
1186*4882a593Smuzhiyun int longest)
1187*4882a593Smuzhiyun {
1188*4882a593Smuzhiyun int logfree = 0;
1189*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr freehdr;
1190*4882a593Smuzhiyun struct xfs_inode *dp = args->dp;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1193*4882a593Smuzhiyun if (hdr) {
1194*4882a593Smuzhiyun /*
1195*4882a593Smuzhiyun * Data block is not empty, just set the free entry to the new
1196*4882a593Smuzhiyun * value.
1197*4882a593Smuzhiyun */
1198*4882a593Smuzhiyun freehdr.bests[findex] = cpu_to_be16(longest);
1199*4882a593Smuzhiyun xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
1200*4882a593Smuzhiyun return 0;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun /* One less used entry in the free table. */
1204*4882a593Smuzhiyun freehdr.nused--;
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun /*
1207*4882a593Smuzhiyun * If this was the last entry in the table, we can trim the table size
1208*4882a593Smuzhiyun * back. There might be other entries at the end referring to
1209*4882a593Smuzhiyun * non-existent data blocks, get those too.
1210*4882a593Smuzhiyun */
1211*4882a593Smuzhiyun if (findex == freehdr.nvalid - 1) {
1212*4882a593Smuzhiyun int i; /* free entry index */
1213*4882a593Smuzhiyun
1214*4882a593Smuzhiyun for (i = findex - 1; i >= 0; i--) {
1215*4882a593Smuzhiyun if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
1216*4882a593Smuzhiyun break;
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun freehdr.nvalid = i + 1;
1219*4882a593Smuzhiyun logfree = 0;
1220*4882a593Smuzhiyun } else {
1221*4882a593Smuzhiyun /* Not the last entry, just punch it out. */
1222*4882a593Smuzhiyun freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
1223*4882a593Smuzhiyun logfree = 1;
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
1227*4882a593Smuzhiyun xfs_dir2_free_log_header(args, fbp);
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun /*
1230*4882a593Smuzhiyun * If there are no useful entries left in the block, get rid of the
1231*4882a593Smuzhiyun * block if we can.
1232*4882a593Smuzhiyun */
1233*4882a593Smuzhiyun if (!freehdr.nused) {
1234*4882a593Smuzhiyun int error;
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun error = xfs_dir2_shrink_inode(args, fdb, fbp);
1237*4882a593Smuzhiyun if (error == 0) {
1238*4882a593Smuzhiyun fbp = NULL;
1239*4882a593Smuzhiyun logfree = 0;
1240*4882a593Smuzhiyun } else if (error != -ENOSPC || args->total != 0)
1241*4882a593Smuzhiyun return error;
1242*4882a593Smuzhiyun /*
1243*4882a593Smuzhiyun * It's possible to get ENOSPC if there is no
1244*4882a593Smuzhiyun * space reservation. In this case some one
1245*4882a593Smuzhiyun * else will eventually get rid of this block.
1246*4882a593Smuzhiyun */
1247*4882a593Smuzhiyun }
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun /* Log the free entry that changed, unless we got rid of it. */
1250*4882a593Smuzhiyun if (logfree)
1251*4882a593Smuzhiyun xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
1252*4882a593Smuzhiyun return 0;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun /*
1256*4882a593Smuzhiyun * Remove an entry from a node directory.
1257*4882a593Smuzhiyun * This removes the leaf entry and the data entry,
1258*4882a593Smuzhiyun * and updates the free block if necessary.
1259*4882a593Smuzhiyun */
1260*4882a593Smuzhiyun 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)1261*4882a593Smuzhiyun xfs_dir2_leafn_remove(
1262*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
1263*4882a593Smuzhiyun struct xfs_buf *bp, /* leaf buffer */
1264*4882a593Smuzhiyun int index, /* leaf entry index */
1265*4882a593Smuzhiyun xfs_da_state_blk_t *dblk, /* data block */
1266*4882a593Smuzhiyun int *rval) /* resulting block needs join */
1267*4882a593Smuzhiyun {
1268*4882a593Smuzhiyun struct xfs_da_geometry *geo = args->geo;
1269*4882a593Smuzhiyun xfs_dir2_data_hdr_t *hdr; /* data block header */
1270*4882a593Smuzhiyun xfs_dir2_db_t db; /* data block number */
1271*4882a593Smuzhiyun struct xfs_buf *dbp; /* data block buffer */
1272*4882a593Smuzhiyun xfs_dir2_data_entry_t *dep; /* data block entry */
1273*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
1274*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
1275*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1276*4882a593Smuzhiyun int longest; /* longest data free entry */
1277*4882a593Smuzhiyun int off; /* data block entry offset */
1278*4882a593Smuzhiyun int needlog; /* need to log data header */
1279*4882a593Smuzhiyun int needscan; /* need to rescan data frees */
1280*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
1281*4882a593Smuzhiyun struct xfs_dir2_data_free *bf; /* bestfree table */
1282*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun trace_xfs_dir2_leafn_remove(args, index);
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun dp = args->dp;
1287*4882a593Smuzhiyun tp = args->trans;
1288*4882a593Smuzhiyun leaf = bp->b_addr;
1289*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1290*4882a593Smuzhiyun
1291*4882a593Smuzhiyun /*
1292*4882a593Smuzhiyun * Point to the entry we're removing.
1293*4882a593Smuzhiyun */
1294*4882a593Smuzhiyun lep = &leafhdr.ents[index];
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun /*
1297*4882a593Smuzhiyun * Extract the data block and offset from the entry.
1298*4882a593Smuzhiyun */
1299*4882a593Smuzhiyun db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1300*4882a593Smuzhiyun ASSERT(dblk->blkno == db);
1301*4882a593Smuzhiyun off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
1302*4882a593Smuzhiyun ASSERT(dblk->index == off);
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun /*
1305*4882a593Smuzhiyun * Kill the leaf entry by marking it stale.
1306*4882a593Smuzhiyun * Log the leaf block changes.
1307*4882a593Smuzhiyun */
1308*4882a593Smuzhiyun leafhdr.stale++;
1309*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1310*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, bp);
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1313*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun /*
1316*4882a593Smuzhiyun * Make the data entry free. Keep track of the longest freespace
1317*4882a593Smuzhiyun * in the data block in case it changes.
1318*4882a593Smuzhiyun */
1319*4882a593Smuzhiyun dbp = dblk->bp;
1320*4882a593Smuzhiyun hdr = dbp->b_addr;
1321*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1322*4882a593Smuzhiyun bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1323*4882a593Smuzhiyun longest = be16_to_cpu(bf[0].length);
1324*4882a593Smuzhiyun needlog = needscan = 0;
1325*4882a593Smuzhiyun xfs_dir2_data_make_free(args, dbp, off,
1326*4882a593Smuzhiyun xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
1327*4882a593Smuzhiyun &needscan);
1328*4882a593Smuzhiyun /*
1329*4882a593Smuzhiyun * Rescan the data block freespaces for bestfree.
1330*4882a593Smuzhiyun * Log the data block header if needed.
1331*4882a593Smuzhiyun */
1332*4882a593Smuzhiyun if (needscan)
1333*4882a593Smuzhiyun xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1334*4882a593Smuzhiyun if (needlog)
1335*4882a593Smuzhiyun xfs_dir2_data_log_header(args, dbp);
1336*4882a593Smuzhiyun xfs_dir3_data_check(dp, dbp);
1337*4882a593Smuzhiyun /*
1338*4882a593Smuzhiyun * If the longest data block freespace changes, need to update
1339*4882a593Smuzhiyun * the corresponding freeblock entry.
1340*4882a593Smuzhiyun */
1341*4882a593Smuzhiyun if (longest < be16_to_cpu(bf[0].length)) {
1342*4882a593Smuzhiyun int error; /* error return value */
1343*4882a593Smuzhiyun struct xfs_buf *fbp; /* freeblock buffer */
1344*4882a593Smuzhiyun xfs_dir2_db_t fdb; /* freeblock block number */
1345*4882a593Smuzhiyun int findex; /* index in freeblock entries */
1346*4882a593Smuzhiyun xfs_dir2_free_t *free; /* freeblock structure */
1347*4882a593Smuzhiyun
1348*4882a593Smuzhiyun /*
1349*4882a593Smuzhiyun * Convert the data block number to a free block,
1350*4882a593Smuzhiyun * read in the free block.
1351*4882a593Smuzhiyun */
1352*4882a593Smuzhiyun fdb = xfs_dir2_db_to_fdb(geo, db);
1353*4882a593Smuzhiyun error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(geo, fdb),
1354*4882a593Smuzhiyun &fbp);
1355*4882a593Smuzhiyun if (error)
1356*4882a593Smuzhiyun return error;
1357*4882a593Smuzhiyun free = fbp->b_addr;
1358*4882a593Smuzhiyun #ifdef DEBUG
1359*4882a593Smuzhiyun {
1360*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr freehdr;
1361*4882a593Smuzhiyun
1362*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1363*4882a593Smuzhiyun ASSERT(freehdr.firstdb == geo->free_max_bests *
1364*4882a593Smuzhiyun (fdb - xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET)));
1365*4882a593Smuzhiyun }
1366*4882a593Smuzhiyun #endif
1367*4882a593Smuzhiyun /*
1368*4882a593Smuzhiyun * Calculate which entry we need to fix.
1369*4882a593Smuzhiyun */
1370*4882a593Smuzhiyun findex = xfs_dir2_db_to_fdindex(geo, db);
1371*4882a593Smuzhiyun longest = be16_to_cpu(bf[0].length);
1372*4882a593Smuzhiyun /*
1373*4882a593Smuzhiyun * If the data block is now empty we can get rid of it
1374*4882a593Smuzhiyun * (usually).
1375*4882a593Smuzhiyun */
1376*4882a593Smuzhiyun if (longest == geo->blksize - geo->data_entry_offset) {
1377*4882a593Smuzhiyun /*
1378*4882a593Smuzhiyun * Try to punch out the data block.
1379*4882a593Smuzhiyun */
1380*4882a593Smuzhiyun error = xfs_dir2_shrink_inode(args, db, dbp);
1381*4882a593Smuzhiyun if (error == 0) {
1382*4882a593Smuzhiyun dblk->bp = NULL;
1383*4882a593Smuzhiyun hdr = NULL;
1384*4882a593Smuzhiyun }
1385*4882a593Smuzhiyun /*
1386*4882a593Smuzhiyun * We can get ENOSPC if there's no space reservation.
1387*4882a593Smuzhiyun * In this case just drop the buffer and some one else
1388*4882a593Smuzhiyun * will eventually get rid of the empty block.
1389*4882a593Smuzhiyun */
1390*4882a593Smuzhiyun else if (!(error == -ENOSPC && args->total == 0))
1391*4882a593Smuzhiyun return error;
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun /*
1394*4882a593Smuzhiyun * If we got rid of the data block, we can eliminate that entry
1395*4882a593Smuzhiyun * in the free block.
1396*4882a593Smuzhiyun */
1397*4882a593Smuzhiyun error = xfs_dir3_data_block_free(args, hdr, free,
1398*4882a593Smuzhiyun fdb, findex, fbp, longest);
1399*4882a593Smuzhiyun if (error)
1400*4882a593Smuzhiyun return error;
1401*4882a593Smuzhiyun }
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, bp);
1404*4882a593Smuzhiyun /*
1405*4882a593Smuzhiyun * Return indication of whether this leaf block is empty enough
1406*4882a593Smuzhiyun * to justify trying to join it with a neighbor.
1407*4882a593Smuzhiyun */
1408*4882a593Smuzhiyun *rval = (geo->leaf_hdr_size +
1409*4882a593Smuzhiyun (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
1410*4882a593Smuzhiyun geo->magicpct;
1411*4882a593Smuzhiyun return 0;
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun
1414*4882a593Smuzhiyun /*
1415*4882a593Smuzhiyun * Split the leaf entries in the old block into old and new blocks.
1416*4882a593Smuzhiyun */
1417*4882a593Smuzhiyun int /* error */
xfs_dir2_leafn_split(xfs_da_state_t * state,xfs_da_state_blk_t * oldblk,xfs_da_state_blk_t * newblk)1418*4882a593Smuzhiyun xfs_dir2_leafn_split(
1419*4882a593Smuzhiyun xfs_da_state_t *state, /* btree cursor */
1420*4882a593Smuzhiyun xfs_da_state_blk_t *oldblk, /* original block */
1421*4882a593Smuzhiyun xfs_da_state_blk_t *newblk) /* newly created block */
1422*4882a593Smuzhiyun {
1423*4882a593Smuzhiyun xfs_da_args_t *args; /* operation arguments */
1424*4882a593Smuzhiyun xfs_dablk_t blkno; /* new leaf block number */
1425*4882a593Smuzhiyun int error; /* error return value */
1426*4882a593Smuzhiyun struct xfs_inode *dp;
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun /*
1429*4882a593Smuzhiyun * Allocate space for a new leaf node.
1430*4882a593Smuzhiyun */
1431*4882a593Smuzhiyun args = state->args;
1432*4882a593Smuzhiyun dp = args->dp;
1433*4882a593Smuzhiyun ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1434*4882a593Smuzhiyun error = xfs_da_grow_inode(args, &blkno);
1435*4882a593Smuzhiyun if (error) {
1436*4882a593Smuzhiyun return error;
1437*4882a593Smuzhiyun }
1438*4882a593Smuzhiyun /*
1439*4882a593Smuzhiyun * Initialize the new leaf block.
1440*4882a593Smuzhiyun */
1441*4882a593Smuzhiyun error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1442*4882a593Smuzhiyun &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1443*4882a593Smuzhiyun if (error)
1444*4882a593Smuzhiyun return error;
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun newblk->blkno = blkno;
1447*4882a593Smuzhiyun newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1448*4882a593Smuzhiyun /*
1449*4882a593Smuzhiyun * Rebalance the entries across the two leaves, link the new
1450*4882a593Smuzhiyun * block into the leaves.
1451*4882a593Smuzhiyun */
1452*4882a593Smuzhiyun xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1453*4882a593Smuzhiyun error = xfs_da3_blk_link(state, oldblk, newblk);
1454*4882a593Smuzhiyun if (error) {
1455*4882a593Smuzhiyun return error;
1456*4882a593Smuzhiyun }
1457*4882a593Smuzhiyun /*
1458*4882a593Smuzhiyun * Insert the new entry in the correct block.
1459*4882a593Smuzhiyun */
1460*4882a593Smuzhiyun if (state->inleaf)
1461*4882a593Smuzhiyun error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1462*4882a593Smuzhiyun else
1463*4882a593Smuzhiyun error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1464*4882a593Smuzhiyun /*
1465*4882a593Smuzhiyun * Update last hashval in each block since we added the name.
1466*4882a593Smuzhiyun */
1467*4882a593Smuzhiyun oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1468*4882a593Smuzhiyun newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
1469*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, oldblk->bp);
1470*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, newblk->bp);
1471*4882a593Smuzhiyun return error;
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun /*
1475*4882a593Smuzhiyun * Check a leaf block and its neighbors to see if the block should be
1476*4882a593Smuzhiyun * collapsed into one or the other neighbor. Always keep the block
1477*4882a593Smuzhiyun * with the smaller block number.
1478*4882a593Smuzhiyun * If the current block is over 50% full, don't try to join it, return 0.
1479*4882a593Smuzhiyun * If the block is empty, fill in the state structure and return 2.
1480*4882a593Smuzhiyun * If it can be collapsed, fill in the state structure and return 1.
1481*4882a593Smuzhiyun * If nothing can be done, return 0.
1482*4882a593Smuzhiyun */
1483*4882a593Smuzhiyun int /* error */
xfs_dir2_leafn_toosmall(xfs_da_state_t * state,int * action)1484*4882a593Smuzhiyun xfs_dir2_leafn_toosmall(
1485*4882a593Smuzhiyun xfs_da_state_t *state, /* btree cursor */
1486*4882a593Smuzhiyun int *action) /* resulting action to take */
1487*4882a593Smuzhiyun {
1488*4882a593Smuzhiyun xfs_da_state_blk_t *blk; /* leaf block */
1489*4882a593Smuzhiyun xfs_dablk_t blkno; /* leaf block number */
1490*4882a593Smuzhiyun struct xfs_buf *bp; /* leaf buffer */
1491*4882a593Smuzhiyun int bytes; /* bytes in use */
1492*4882a593Smuzhiyun int count; /* leaf live entry count */
1493*4882a593Smuzhiyun int error; /* error return value */
1494*4882a593Smuzhiyun int forward; /* sibling block direction */
1495*4882a593Smuzhiyun int i; /* sibling counter */
1496*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
1497*4882a593Smuzhiyun int rval; /* result from path_shift */
1498*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
1499*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents;
1500*4882a593Smuzhiyun struct xfs_inode *dp = state->args->dp;
1501*4882a593Smuzhiyun
1502*4882a593Smuzhiyun /*
1503*4882a593Smuzhiyun * Check for the degenerate case of the block being over 50% full.
1504*4882a593Smuzhiyun * If so, it's not worth even looking to see if we might be able
1505*4882a593Smuzhiyun * to coalesce with a sibling.
1506*4882a593Smuzhiyun */
1507*4882a593Smuzhiyun blk = &state->path.blk[state->path.active - 1];
1508*4882a593Smuzhiyun leaf = blk->bp->b_addr;
1509*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1510*4882a593Smuzhiyun ents = leafhdr.ents;
1511*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, blk->bp);
1512*4882a593Smuzhiyun
1513*4882a593Smuzhiyun count = leafhdr.count - leafhdr.stale;
1514*4882a593Smuzhiyun bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
1515*4882a593Smuzhiyun if (bytes > (state->args->geo->blksize >> 1)) {
1516*4882a593Smuzhiyun /*
1517*4882a593Smuzhiyun * Blk over 50%, don't try to join.
1518*4882a593Smuzhiyun */
1519*4882a593Smuzhiyun *action = 0;
1520*4882a593Smuzhiyun return 0;
1521*4882a593Smuzhiyun }
1522*4882a593Smuzhiyun /*
1523*4882a593Smuzhiyun * Check for the degenerate case of the block being empty.
1524*4882a593Smuzhiyun * If the block is empty, we'll simply delete it, no need to
1525*4882a593Smuzhiyun * coalesce it with a sibling block. We choose (arbitrarily)
1526*4882a593Smuzhiyun * to merge with the forward block unless it is NULL.
1527*4882a593Smuzhiyun */
1528*4882a593Smuzhiyun if (count == 0) {
1529*4882a593Smuzhiyun /*
1530*4882a593Smuzhiyun * Make altpath point to the block we want to keep and
1531*4882a593Smuzhiyun * path point to the block we want to drop (this one).
1532*4882a593Smuzhiyun */
1533*4882a593Smuzhiyun forward = (leafhdr.forw != 0);
1534*4882a593Smuzhiyun memcpy(&state->altpath, &state->path, sizeof(state->path));
1535*4882a593Smuzhiyun error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1536*4882a593Smuzhiyun &rval);
1537*4882a593Smuzhiyun if (error)
1538*4882a593Smuzhiyun return error;
1539*4882a593Smuzhiyun *action = rval ? 2 : 0;
1540*4882a593Smuzhiyun return 0;
1541*4882a593Smuzhiyun }
1542*4882a593Smuzhiyun /*
1543*4882a593Smuzhiyun * Examine each sibling block to see if we can coalesce with
1544*4882a593Smuzhiyun * at least 25% free space to spare. We need to figure out
1545*4882a593Smuzhiyun * whether to merge with the forward or the backward block.
1546*4882a593Smuzhiyun * We prefer coalescing with the lower numbered sibling so as
1547*4882a593Smuzhiyun * to shrink a directory over time.
1548*4882a593Smuzhiyun */
1549*4882a593Smuzhiyun forward = leafhdr.forw < leafhdr.back;
1550*4882a593Smuzhiyun for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1551*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr hdr2;
1552*4882a593Smuzhiyun
1553*4882a593Smuzhiyun blkno = forward ? leafhdr.forw : leafhdr.back;
1554*4882a593Smuzhiyun if (blkno == 0)
1555*4882a593Smuzhiyun continue;
1556*4882a593Smuzhiyun /*
1557*4882a593Smuzhiyun * Read the sibling leaf block.
1558*4882a593Smuzhiyun */
1559*4882a593Smuzhiyun error = xfs_dir3_leafn_read(state->args->trans, dp, blkno, &bp);
1560*4882a593Smuzhiyun if (error)
1561*4882a593Smuzhiyun return error;
1562*4882a593Smuzhiyun
1563*4882a593Smuzhiyun /*
1564*4882a593Smuzhiyun * Count bytes in the two blocks combined.
1565*4882a593Smuzhiyun */
1566*4882a593Smuzhiyun count = leafhdr.count - leafhdr.stale;
1567*4882a593Smuzhiyun bytes = state->args->geo->blksize -
1568*4882a593Smuzhiyun (state->args->geo->blksize >> 2);
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun leaf = bp->b_addr;
1571*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
1572*4882a593Smuzhiyun ents = hdr2.ents;
1573*4882a593Smuzhiyun count += hdr2.count - hdr2.stale;
1574*4882a593Smuzhiyun bytes -= count * sizeof(ents[0]);
1575*4882a593Smuzhiyun
1576*4882a593Smuzhiyun /*
1577*4882a593Smuzhiyun * Fits with at least 25% to spare.
1578*4882a593Smuzhiyun */
1579*4882a593Smuzhiyun if (bytes >= 0)
1580*4882a593Smuzhiyun break;
1581*4882a593Smuzhiyun xfs_trans_brelse(state->args->trans, bp);
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun /*
1584*4882a593Smuzhiyun * Didn't like either block, give up.
1585*4882a593Smuzhiyun */
1586*4882a593Smuzhiyun if (i >= 2) {
1587*4882a593Smuzhiyun *action = 0;
1588*4882a593Smuzhiyun return 0;
1589*4882a593Smuzhiyun }
1590*4882a593Smuzhiyun
1591*4882a593Smuzhiyun /*
1592*4882a593Smuzhiyun * Make altpath point to the block we want to keep (the lower
1593*4882a593Smuzhiyun * numbered block) and path point to the block we want to drop.
1594*4882a593Smuzhiyun */
1595*4882a593Smuzhiyun memcpy(&state->altpath, &state->path, sizeof(state->path));
1596*4882a593Smuzhiyun if (blkno < blk->blkno)
1597*4882a593Smuzhiyun error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1598*4882a593Smuzhiyun &rval);
1599*4882a593Smuzhiyun else
1600*4882a593Smuzhiyun error = xfs_da3_path_shift(state, &state->path, forward, 0,
1601*4882a593Smuzhiyun &rval);
1602*4882a593Smuzhiyun if (error) {
1603*4882a593Smuzhiyun return error;
1604*4882a593Smuzhiyun }
1605*4882a593Smuzhiyun *action = rval ? 0 : 1;
1606*4882a593Smuzhiyun return 0;
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun /*
1610*4882a593Smuzhiyun * Move all the leaf entries from drop_blk to save_blk.
1611*4882a593Smuzhiyun * This is done as part of a join operation.
1612*4882a593Smuzhiyun */
1613*4882a593Smuzhiyun void
xfs_dir2_leafn_unbalance(xfs_da_state_t * state,xfs_da_state_blk_t * drop_blk,xfs_da_state_blk_t * save_blk)1614*4882a593Smuzhiyun xfs_dir2_leafn_unbalance(
1615*4882a593Smuzhiyun xfs_da_state_t *state, /* cursor */
1616*4882a593Smuzhiyun xfs_da_state_blk_t *drop_blk, /* dead block */
1617*4882a593Smuzhiyun xfs_da_state_blk_t *save_blk) /* surviving block */
1618*4882a593Smuzhiyun {
1619*4882a593Smuzhiyun xfs_da_args_t *args; /* operation arguments */
1620*4882a593Smuzhiyun xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1621*4882a593Smuzhiyun xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1622*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr savehdr;
1623*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr drophdr;
1624*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *sents;
1625*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *dents;
1626*4882a593Smuzhiyun struct xfs_inode *dp = state->args->dp;
1627*4882a593Smuzhiyun
1628*4882a593Smuzhiyun args = state->args;
1629*4882a593Smuzhiyun ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1630*4882a593Smuzhiyun ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1631*4882a593Smuzhiyun drop_leaf = drop_blk->bp->b_addr;
1632*4882a593Smuzhiyun save_leaf = save_blk->bp->b_addr;
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1635*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
1636*4882a593Smuzhiyun sents = savehdr.ents;
1637*4882a593Smuzhiyun dents = drophdr.ents;
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun /*
1640*4882a593Smuzhiyun * If there are any stale leaf entries, take this opportunity
1641*4882a593Smuzhiyun * to purge them.
1642*4882a593Smuzhiyun */
1643*4882a593Smuzhiyun if (drophdr.stale)
1644*4882a593Smuzhiyun xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1645*4882a593Smuzhiyun if (savehdr.stale)
1646*4882a593Smuzhiyun xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1647*4882a593Smuzhiyun
1648*4882a593Smuzhiyun /*
1649*4882a593Smuzhiyun * Move the entries from drop to the appropriate end of save.
1650*4882a593Smuzhiyun */
1651*4882a593Smuzhiyun drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1652*4882a593Smuzhiyun if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1653*4882a593Smuzhiyun xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1654*4882a593Smuzhiyun save_blk->bp, &savehdr, sents, 0,
1655*4882a593Smuzhiyun drophdr.count);
1656*4882a593Smuzhiyun else
1657*4882a593Smuzhiyun xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1658*4882a593Smuzhiyun save_blk->bp, &savehdr, sents,
1659*4882a593Smuzhiyun savehdr.count, drophdr.count);
1660*4882a593Smuzhiyun save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1661*4882a593Smuzhiyun
1662*4882a593Smuzhiyun /* log the changes made when moving the entries */
1663*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1664*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
1665*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, save_blk->bp);
1666*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, drop_blk->bp);
1667*4882a593Smuzhiyun
1668*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, save_blk->bp);
1669*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, drop_blk->bp);
1670*4882a593Smuzhiyun }
1671*4882a593Smuzhiyun
1672*4882a593Smuzhiyun /*
1673*4882a593Smuzhiyun * Add a new data block to the directory at the free space index that the caller
1674*4882a593Smuzhiyun * has specified.
1675*4882a593Smuzhiyun */
1676*4882a593Smuzhiyun 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)1677*4882a593Smuzhiyun xfs_dir2_node_add_datablk(
1678*4882a593Smuzhiyun struct xfs_da_args *args,
1679*4882a593Smuzhiyun struct xfs_da_state_blk *fblk,
1680*4882a593Smuzhiyun xfs_dir2_db_t *dbno,
1681*4882a593Smuzhiyun struct xfs_buf **dbpp,
1682*4882a593Smuzhiyun struct xfs_buf **fbpp,
1683*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr *hdr,
1684*4882a593Smuzhiyun int *findex)
1685*4882a593Smuzhiyun {
1686*4882a593Smuzhiyun struct xfs_inode *dp = args->dp;
1687*4882a593Smuzhiyun struct xfs_trans *tp = args->trans;
1688*4882a593Smuzhiyun struct xfs_mount *mp = dp->i_mount;
1689*4882a593Smuzhiyun struct xfs_dir2_data_free *bf;
1690*4882a593Smuzhiyun xfs_dir2_db_t fbno;
1691*4882a593Smuzhiyun struct xfs_buf *fbp;
1692*4882a593Smuzhiyun struct xfs_buf *dbp;
1693*4882a593Smuzhiyun int error;
1694*4882a593Smuzhiyun
1695*4882a593Smuzhiyun /* Not allowed to allocate, return failure. */
1696*4882a593Smuzhiyun if (args->total == 0)
1697*4882a593Smuzhiyun return -ENOSPC;
1698*4882a593Smuzhiyun
1699*4882a593Smuzhiyun /* Allocate and initialize the new data block. */
1700*4882a593Smuzhiyun error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1701*4882a593Smuzhiyun if (error)
1702*4882a593Smuzhiyun return error;
1703*4882a593Smuzhiyun error = xfs_dir3_data_init(args, *dbno, &dbp);
1704*4882a593Smuzhiyun if (error)
1705*4882a593Smuzhiyun return error;
1706*4882a593Smuzhiyun
1707*4882a593Smuzhiyun /*
1708*4882a593Smuzhiyun * Get the freespace block corresponding to the data block
1709*4882a593Smuzhiyun * that was just allocated.
1710*4882a593Smuzhiyun */
1711*4882a593Smuzhiyun fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
1712*4882a593Smuzhiyun error = xfs_dir2_free_try_read(tp, dp,
1713*4882a593Smuzhiyun xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1714*4882a593Smuzhiyun if (error)
1715*4882a593Smuzhiyun return error;
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun /*
1718*4882a593Smuzhiyun * If there wasn't a freespace block, the read will
1719*4882a593Smuzhiyun * return a NULL fbp. Allocate and initialize a new one.
1720*4882a593Smuzhiyun */
1721*4882a593Smuzhiyun if (!fbp) {
1722*4882a593Smuzhiyun error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1723*4882a593Smuzhiyun if (error)
1724*4882a593Smuzhiyun return error;
1725*4882a593Smuzhiyun
1726*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp,
1727*4882a593Smuzhiyun xfs_dir2_db_to_fdb(args->geo, *dbno) !=
1728*4882a593Smuzhiyun fbno)) {
1729*4882a593Smuzhiyun xfs_alert(mp,
1730*4882a593Smuzhiyun "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1731*4882a593Smuzhiyun __func__, (unsigned long long)dp->i_ino,
1732*4882a593Smuzhiyun (long long)xfs_dir2_db_to_fdb(args->geo, *dbno),
1733*4882a593Smuzhiyun (long long)*dbno, (long long)fbno);
1734*4882a593Smuzhiyun if (fblk) {
1735*4882a593Smuzhiyun xfs_alert(mp,
1736*4882a593Smuzhiyun " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1737*4882a593Smuzhiyun fblk, (unsigned long long)fblk->blkno,
1738*4882a593Smuzhiyun fblk->index, fblk->magic);
1739*4882a593Smuzhiyun } else {
1740*4882a593Smuzhiyun xfs_alert(mp, " ... fblk is NULL");
1741*4882a593Smuzhiyun }
1742*4882a593Smuzhiyun return -EFSCORRUPTED;
1743*4882a593Smuzhiyun }
1744*4882a593Smuzhiyun
1745*4882a593Smuzhiyun /* Get a buffer for the new block. */
1746*4882a593Smuzhiyun error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1747*4882a593Smuzhiyun if (error)
1748*4882a593Smuzhiyun return error;
1749*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
1750*4882a593Smuzhiyun
1751*4882a593Smuzhiyun /* Remember the first slot as our empty slot. */
1752*4882a593Smuzhiyun hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1753*4882a593Smuzhiyun XFS_DIR2_FREE_OFFSET)) *
1754*4882a593Smuzhiyun args->geo->free_max_bests;
1755*4882a593Smuzhiyun } else {
1756*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
1757*4882a593Smuzhiyun }
1758*4882a593Smuzhiyun
1759*4882a593Smuzhiyun /* Set the freespace block index from the data block number. */
1760*4882a593Smuzhiyun *findex = xfs_dir2_db_to_fdindex(args->geo, *dbno);
1761*4882a593Smuzhiyun
1762*4882a593Smuzhiyun /* Extend the freespace table if the new data block is off the end. */
1763*4882a593Smuzhiyun if (*findex >= hdr->nvalid) {
1764*4882a593Smuzhiyun ASSERT(*findex < args->geo->free_max_bests);
1765*4882a593Smuzhiyun hdr->nvalid = *findex + 1;
1766*4882a593Smuzhiyun hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF);
1767*4882a593Smuzhiyun }
1768*4882a593Smuzhiyun
1769*4882a593Smuzhiyun /*
1770*4882a593Smuzhiyun * If this entry was for an empty data block (this should always be
1771*4882a593Smuzhiyun * true) then update the header.
1772*4882a593Smuzhiyun */
1773*4882a593Smuzhiyun if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1774*4882a593Smuzhiyun hdr->nused++;
1775*4882a593Smuzhiyun xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
1776*4882a593Smuzhiyun xfs_dir2_free_log_header(args, fbp);
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun /* Update the freespace value for the new block in the table. */
1780*4882a593Smuzhiyun bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
1781*4882a593Smuzhiyun hdr->bests[*findex] = bf[0].length;
1782*4882a593Smuzhiyun
1783*4882a593Smuzhiyun *dbpp = dbp;
1784*4882a593Smuzhiyun *fbpp = fbp;
1785*4882a593Smuzhiyun return 0;
1786*4882a593Smuzhiyun }
1787*4882a593Smuzhiyun
1788*4882a593Smuzhiyun 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)1789*4882a593Smuzhiyun xfs_dir2_node_find_freeblk(
1790*4882a593Smuzhiyun struct xfs_da_args *args,
1791*4882a593Smuzhiyun struct xfs_da_state_blk *fblk,
1792*4882a593Smuzhiyun xfs_dir2_db_t *dbnop,
1793*4882a593Smuzhiyun struct xfs_buf **fbpp,
1794*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr *hdr,
1795*4882a593Smuzhiyun int *findexp,
1796*4882a593Smuzhiyun int length)
1797*4882a593Smuzhiyun {
1798*4882a593Smuzhiyun struct xfs_inode *dp = args->dp;
1799*4882a593Smuzhiyun struct xfs_trans *tp = args->trans;
1800*4882a593Smuzhiyun struct xfs_buf *fbp = NULL;
1801*4882a593Smuzhiyun xfs_dir2_db_t firstfbno;
1802*4882a593Smuzhiyun xfs_dir2_db_t lastfbno;
1803*4882a593Smuzhiyun xfs_dir2_db_t ifbno = -1;
1804*4882a593Smuzhiyun xfs_dir2_db_t dbno = -1;
1805*4882a593Smuzhiyun xfs_dir2_db_t fbno;
1806*4882a593Smuzhiyun xfs_fileoff_t fo;
1807*4882a593Smuzhiyun int findex = 0;
1808*4882a593Smuzhiyun int error;
1809*4882a593Smuzhiyun
1810*4882a593Smuzhiyun /*
1811*4882a593Smuzhiyun * If we came in with a freespace block that means that lookup
1812*4882a593Smuzhiyun * found an entry with our hash value. This is the freespace
1813*4882a593Smuzhiyun * block for that data entry.
1814*4882a593Smuzhiyun */
1815*4882a593Smuzhiyun if (fblk) {
1816*4882a593Smuzhiyun fbp = fblk->bp;
1817*4882a593Smuzhiyun findex = fblk->index;
1818*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
1819*4882a593Smuzhiyun if (findex >= 0) {
1820*4882a593Smuzhiyun /* caller already found the freespace for us. */
1821*4882a593Smuzhiyun ASSERT(findex < hdr->nvalid);
1822*4882a593Smuzhiyun ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
1823*4882a593Smuzhiyun ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
1824*4882a593Smuzhiyun dbno = hdr->firstdb + findex;
1825*4882a593Smuzhiyun goto found_block;
1826*4882a593Smuzhiyun }
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun /*
1829*4882a593Smuzhiyun * The data block looked at didn't have enough room.
1830*4882a593Smuzhiyun * We'll start at the beginning of the freespace entries.
1831*4882a593Smuzhiyun */
1832*4882a593Smuzhiyun ifbno = fblk->blkno;
1833*4882a593Smuzhiyun xfs_trans_brelse(tp, fbp);
1834*4882a593Smuzhiyun fbp = NULL;
1835*4882a593Smuzhiyun fblk->bp = NULL;
1836*4882a593Smuzhiyun }
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun /*
1839*4882a593Smuzhiyun * If we don't have a data block yet, we're going to scan the freespace
1840*4882a593Smuzhiyun * data for a data block with enough free space in it.
1841*4882a593Smuzhiyun */
1842*4882a593Smuzhiyun error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1843*4882a593Smuzhiyun if (error)
1844*4882a593Smuzhiyun return error;
1845*4882a593Smuzhiyun lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1846*4882a593Smuzhiyun firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
1847*4882a593Smuzhiyun
1848*4882a593Smuzhiyun for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
1849*4882a593Smuzhiyun /* If it's ifbno we already looked at it. */
1850*4882a593Smuzhiyun if (fbno == ifbno)
1851*4882a593Smuzhiyun continue;
1852*4882a593Smuzhiyun
1853*4882a593Smuzhiyun /*
1854*4882a593Smuzhiyun * Read the block. There can be holes in the freespace blocks,
1855*4882a593Smuzhiyun * so this might not succeed. This should be really rare, so
1856*4882a593Smuzhiyun * there's no reason to avoid it.
1857*4882a593Smuzhiyun */
1858*4882a593Smuzhiyun error = xfs_dir2_free_try_read(tp, dp,
1859*4882a593Smuzhiyun xfs_dir2_db_to_da(args->geo, fbno),
1860*4882a593Smuzhiyun &fbp);
1861*4882a593Smuzhiyun if (error)
1862*4882a593Smuzhiyun return error;
1863*4882a593Smuzhiyun if (!fbp)
1864*4882a593Smuzhiyun continue;
1865*4882a593Smuzhiyun
1866*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
1867*4882a593Smuzhiyun
1868*4882a593Smuzhiyun /* Scan the free entry array for a large enough free space. */
1869*4882a593Smuzhiyun for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
1870*4882a593Smuzhiyun if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
1871*4882a593Smuzhiyun be16_to_cpu(hdr->bests[findex]) >= length) {
1872*4882a593Smuzhiyun dbno = hdr->firstdb + findex;
1873*4882a593Smuzhiyun goto found_block;
1874*4882a593Smuzhiyun }
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun
1877*4882a593Smuzhiyun /* Didn't find free space, go on to next free block */
1878*4882a593Smuzhiyun xfs_trans_brelse(tp, fbp);
1879*4882a593Smuzhiyun }
1880*4882a593Smuzhiyun
1881*4882a593Smuzhiyun found_block:
1882*4882a593Smuzhiyun *dbnop = dbno;
1883*4882a593Smuzhiyun *fbpp = fbp;
1884*4882a593Smuzhiyun *findexp = findex;
1885*4882a593Smuzhiyun return 0;
1886*4882a593Smuzhiyun }
1887*4882a593Smuzhiyun
1888*4882a593Smuzhiyun /*
1889*4882a593Smuzhiyun * Add the data entry for a node-format directory name addition.
1890*4882a593Smuzhiyun * The leaf entry is added in xfs_dir2_leafn_add.
1891*4882a593Smuzhiyun * We may enter with a freespace block that the lookup found.
1892*4882a593Smuzhiyun */
1893*4882a593Smuzhiyun static int
xfs_dir2_node_addname_int(struct xfs_da_args * args,struct xfs_da_state_blk * fblk)1894*4882a593Smuzhiyun xfs_dir2_node_addname_int(
1895*4882a593Smuzhiyun struct xfs_da_args *args, /* operation arguments */
1896*4882a593Smuzhiyun struct xfs_da_state_blk *fblk) /* optional freespace block */
1897*4882a593Smuzhiyun {
1898*4882a593Smuzhiyun struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1899*4882a593Smuzhiyun struct xfs_dir2_data_entry *dep; /* data entry pointer */
1900*4882a593Smuzhiyun struct xfs_dir2_data_hdr *hdr; /* data block header */
1901*4882a593Smuzhiyun struct xfs_dir2_data_free *bf;
1902*4882a593Smuzhiyun struct xfs_trans *tp = args->trans;
1903*4882a593Smuzhiyun struct xfs_inode *dp = args->dp;
1904*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr freehdr;
1905*4882a593Smuzhiyun struct xfs_buf *dbp; /* data block buffer */
1906*4882a593Smuzhiyun struct xfs_buf *fbp; /* freespace buffer */
1907*4882a593Smuzhiyun xfs_dir2_data_aoff_t aoff;
1908*4882a593Smuzhiyun xfs_dir2_db_t dbno; /* data block number */
1909*4882a593Smuzhiyun int error; /* error return value */
1910*4882a593Smuzhiyun int findex; /* freespace entry index */
1911*4882a593Smuzhiyun int length; /* length of the new entry */
1912*4882a593Smuzhiyun int logfree = 0; /* need to log free entry */
1913*4882a593Smuzhiyun int needlog = 0; /* need to log data header */
1914*4882a593Smuzhiyun int needscan = 0; /* need to rescan data frees */
1915*4882a593Smuzhiyun __be16 *tagp; /* data entry tag pointer */
1916*4882a593Smuzhiyun
1917*4882a593Smuzhiyun length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
1918*4882a593Smuzhiyun error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
1919*4882a593Smuzhiyun &findex, length);
1920*4882a593Smuzhiyun if (error)
1921*4882a593Smuzhiyun return error;
1922*4882a593Smuzhiyun
1923*4882a593Smuzhiyun /*
1924*4882a593Smuzhiyun * Now we know if we must allocate blocks, so if we are checking whether
1925*4882a593Smuzhiyun * we can insert without allocation then we can return now.
1926*4882a593Smuzhiyun */
1927*4882a593Smuzhiyun if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1928*4882a593Smuzhiyun if (dbno == -1)
1929*4882a593Smuzhiyun return -ENOSPC;
1930*4882a593Smuzhiyun return 0;
1931*4882a593Smuzhiyun }
1932*4882a593Smuzhiyun
1933*4882a593Smuzhiyun /*
1934*4882a593Smuzhiyun * If we don't have a data block, we need to allocate one and make
1935*4882a593Smuzhiyun * the freespace entries refer to it.
1936*4882a593Smuzhiyun */
1937*4882a593Smuzhiyun if (dbno == -1) {
1938*4882a593Smuzhiyun /* we're going to have to log the free block index later */
1939*4882a593Smuzhiyun logfree = 1;
1940*4882a593Smuzhiyun error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1941*4882a593Smuzhiyun &freehdr, &findex);
1942*4882a593Smuzhiyun } else {
1943*4882a593Smuzhiyun /* Read the data block in. */
1944*4882a593Smuzhiyun error = xfs_dir3_data_read(tp, dp,
1945*4882a593Smuzhiyun xfs_dir2_db_to_da(args->geo, dbno),
1946*4882a593Smuzhiyun 0, &dbp);
1947*4882a593Smuzhiyun }
1948*4882a593Smuzhiyun if (error)
1949*4882a593Smuzhiyun return error;
1950*4882a593Smuzhiyun
1951*4882a593Smuzhiyun /* setup for data block up now */
1952*4882a593Smuzhiyun hdr = dbp->b_addr;
1953*4882a593Smuzhiyun bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1954*4882a593Smuzhiyun ASSERT(be16_to_cpu(bf[0].length) >= length);
1955*4882a593Smuzhiyun
1956*4882a593Smuzhiyun /* Point to the existing unused space. */
1957*4882a593Smuzhiyun dup = (xfs_dir2_data_unused_t *)
1958*4882a593Smuzhiyun ((char *)hdr + be16_to_cpu(bf[0].offset));
1959*4882a593Smuzhiyun
1960*4882a593Smuzhiyun /* Mark the first part of the unused space, inuse for us. */
1961*4882a593Smuzhiyun aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1962*4882a593Smuzhiyun error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1963*4882a593Smuzhiyun &needlog, &needscan);
1964*4882a593Smuzhiyun if (error) {
1965*4882a593Smuzhiyun xfs_trans_brelse(tp, dbp);
1966*4882a593Smuzhiyun return error;
1967*4882a593Smuzhiyun }
1968*4882a593Smuzhiyun
1969*4882a593Smuzhiyun /* Fill in the new entry and log it. */
1970*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)dup;
1971*4882a593Smuzhiyun dep->inumber = cpu_to_be64(args->inumber);
1972*4882a593Smuzhiyun dep->namelen = args->namelen;
1973*4882a593Smuzhiyun memcpy(dep->name, args->name, dep->namelen);
1974*4882a593Smuzhiyun xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1975*4882a593Smuzhiyun tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
1976*4882a593Smuzhiyun *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1977*4882a593Smuzhiyun xfs_dir2_data_log_entry(args, dbp, dep);
1978*4882a593Smuzhiyun
1979*4882a593Smuzhiyun /* Rescan the freespace and log the data block if needed. */
1980*4882a593Smuzhiyun if (needscan)
1981*4882a593Smuzhiyun xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1982*4882a593Smuzhiyun if (needlog)
1983*4882a593Smuzhiyun xfs_dir2_data_log_header(args, dbp);
1984*4882a593Smuzhiyun
1985*4882a593Smuzhiyun /* If the freespace block entry is now wrong, update it. */
1986*4882a593Smuzhiyun if (freehdr.bests[findex] != bf[0].length) {
1987*4882a593Smuzhiyun freehdr.bests[findex] = bf[0].length;
1988*4882a593Smuzhiyun logfree = 1;
1989*4882a593Smuzhiyun }
1990*4882a593Smuzhiyun
1991*4882a593Smuzhiyun /* Log the freespace entry if needed. */
1992*4882a593Smuzhiyun if (logfree)
1993*4882a593Smuzhiyun xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
1994*4882a593Smuzhiyun
1995*4882a593Smuzhiyun /* Return the data block and offset in args. */
1996*4882a593Smuzhiyun args->blkno = (xfs_dablk_t)dbno;
1997*4882a593Smuzhiyun args->index = be16_to_cpu(*tagp);
1998*4882a593Smuzhiyun return 0;
1999*4882a593Smuzhiyun }
2000*4882a593Smuzhiyun
2001*4882a593Smuzhiyun /*
2002*4882a593Smuzhiyun * Top-level node form directory addname routine.
2003*4882a593Smuzhiyun */
2004*4882a593Smuzhiyun int /* error */
xfs_dir2_node_addname(xfs_da_args_t * args)2005*4882a593Smuzhiyun xfs_dir2_node_addname(
2006*4882a593Smuzhiyun xfs_da_args_t *args) /* operation arguments */
2007*4882a593Smuzhiyun {
2008*4882a593Smuzhiyun xfs_da_state_blk_t *blk; /* leaf block for insert */
2009*4882a593Smuzhiyun int error; /* error return value */
2010*4882a593Smuzhiyun int rval; /* sub-return value */
2011*4882a593Smuzhiyun xfs_da_state_t *state; /* btree cursor */
2012*4882a593Smuzhiyun
2013*4882a593Smuzhiyun trace_xfs_dir2_node_addname(args);
2014*4882a593Smuzhiyun
2015*4882a593Smuzhiyun /*
2016*4882a593Smuzhiyun * Allocate and initialize the state (btree cursor).
2017*4882a593Smuzhiyun */
2018*4882a593Smuzhiyun state = xfs_da_state_alloc(args);
2019*4882a593Smuzhiyun /*
2020*4882a593Smuzhiyun * Look up the name. We're not supposed to find it, but
2021*4882a593Smuzhiyun * this gives us the insertion point.
2022*4882a593Smuzhiyun */
2023*4882a593Smuzhiyun error = xfs_da3_node_lookup_int(state, &rval);
2024*4882a593Smuzhiyun if (error)
2025*4882a593Smuzhiyun rval = error;
2026*4882a593Smuzhiyun if (rval != -ENOENT) {
2027*4882a593Smuzhiyun goto done;
2028*4882a593Smuzhiyun }
2029*4882a593Smuzhiyun /*
2030*4882a593Smuzhiyun * Add the data entry to a data block.
2031*4882a593Smuzhiyun * Extravalid is set to a freeblock found by lookup.
2032*4882a593Smuzhiyun */
2033*4882a593Smuzhiyun rval = xfs_dir2_node_addname_int(args,
2034*4882a593Smuzhiyun state->extravalid ? &state->extrablk : NULL);
2035*4882a593Smuzhiyun if (rval) {
2036*4882a593Smuzhiyun goto done;
2037*4882a593Smuzhiyun }
2038*4882a593Smuzhiyun blk = &state->path.blk[state->path.active - 1];
2039*4882a593Smuzhiyun ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2040*4882a593Smuzhiyun /*
2041*4882a593Smuzhiyun * Add the new leaf entry.
2042*4882a593Smuzhiyun */
2043*4882a593Smuzhiyun rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2044*4882a593Smuzhiyun if (rval == 0) {
2045*4882a593Smuzhiyun /*
2046*4882a593Smuzhiyun * It worked, fix the hash values up the btree.
2047*4882a593Smuzhiyun */
2048*4882a593Smuzhiyun if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2049*4882a593Smuzhiyun xfs_da3_fixhashpath(state, &state->path);
2050*4882a593Smuzhiyun } else {
2051*4882a593Smuzhiyun /*
2052*4882a593Smuzhiyun * It didn't work, we need to split the leaf block.
2053*4882a593Smuzhiyun */
2054*4882a593Smuzhiyun if (args->total == 0) {
2055*4882a593Smuzhiyun ASSERT(rval == -ENOSPC);
2056*4882a593Smuzhiyun goto done;
2057*4882a593Smuzhiyun }
2058*4882a593Smuzhiyun /*
2059*4882a593Smuzhiyun * Split the leaf block and insert the new entry.
2060*4882a593Smuzhiyun */
2061*4882a593Smuzhiyun rval = xfs_da3_split(state);
2062*4882a593Smuzhiyun }
2063*4882a593Smuzhiyun done:
2064*4882a593Smuzhiyun xfs_da_state_free(state);
2065*4882a593Smuzhiyun return rval;
2066*4882a593Smuzhiyun }
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun /*
2069*4882a593Smuzhiyun * Lookup an entry in a node-format directory.
2070*4882a593Smuzhiyun * All the real work happens in xfs_da3_node_lookup_int.
2071*4882a593Smuzhiyun * The only real output is the inode number of the entry.
2072*4882a593Smuzhiyun */
2073*4882a593Smuzhiyun int /* error */
xfs_dir2_node_lookup(xfs_da_args_t * args)2074*4882a593Smuzhiyun xfs_dir2_node_lookup(
2075*4882a593Smuzhiyun xfs_da_args_t *args) /* operation arguments */
2076*4882a593Smuzhiyun {
2077*4882a593Smuzhiyun int error; /* error return value */
2078*4882a593Smuzhiyun int i; /* btree level */
2079*4882a593Smuzhiyun int rval; /* operation return value */
2080*4882a593Smuzhiyun xfs_da_state_t *state; /* btree cursor */
2081*4882a593Smuzhiyun
2082*4882a593Smuzhiyun trace_xfs_dir2_node_lookup(args);
2083*4882a593Smuzhiyun
2084*4882a593Smuzhiyun /*
2085*4882a593Smuzhiyun * Allocate and initialize the btree cursor.
2086*4882a593Smuzhiyun */
2087*4882a593Smuzhiyun state = xfs_da_state_alloc(args);
2088*4882a593Smuzhiyun
2089*4882a593Smuzhiyun /*
2090*4882a593Smuzhiyun * Fill in the path to the entry in the cursor.
2091*4882a593Smuzhiyun */
2092*4882a593Smuzhiyun error = xfs_da3_node_lookup_int(state, &rval);
2093*4882a593Smuzhiyun if (error)
2094*4882a593Smuzhiyun rval = error;
2095*4882a593Smuzhiyun else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2096*4882a593Smuzhiyun /* If a CI match, dup the actual name and return -EEXIST */
2097*4882a593Smuzhiyun xfs_dir2_data_entry_t *dep;
2098*4882a593Smuzhiyun
2099*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)
2100*4882a593Smuzhiyun ((char *)state->extrablk.bp->b_addr +
2101*4882a593Smuzhiyun state->extrablk.index);
2102*4882a593Smuzhiyun rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2103*4882a593Smuzhiyun }
2104*4882a593Smuzhiyun /*
2105*4882a593Smuzhiyun * Release the btree blocks and leaf block.
2106*4882a593Smuzhiyun */
2107*4882a593Smuzhiyun for (i = 0; i < state->path.active; i++) {
2108*4882a593Smuzhiyun xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2109*4882a593Smuzhiyun state->path.blk[i].bp = NULL;
2110*4882a593Smuzhiyun }
2111*4882a593Smuzhiyun /*
2112*4882a593Smuzhiyun * Release the data block if we have it.
2113*4882a593Smuzhiyun */
2114*4882a593Smuzhiyun if (state->extravalid && state->extrablk.bp) {
2115*4882a593Smuzhiyun xfs_trans_brelse(args->trans, state->extrablk.bp);
2116*4882a593Smuzhiyun state->extrablk.bp = NULL;
2117*4882a593Smuzhiyun }
2118*4882a593Smuzhiyun xfs_da_state_free(state);
2119*4882a593Smuzhiyun return rval;
2120*4882a593Smuzhiyun }
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun /*
2123*4882a593Smuzhiyun * Remove an entry from a node-format directory.
2124*4882a593Smuzhiyun */
2125*4882a593Smuzhiyun int /* error */
xfs_dir2_node_removename(struct xfs_da_args * args)2126*4882a593Smuzhiyun xfs_dir2_node_removename(
2127*4882a593Smuzhiyun struct xfs_da_args *args) /* operation arguments */
2128*4882a593Smuzhiyun {
2129*4882a593Smuzhiyun struct xfs_da_state_blk *blk; /* leaf block */
2130*4882a593Smuzhiyun int error; /* error return value */
2131*4882a593Smuzhiyun int rval; /* operation return value */
2132*4882a593Smuzhiyun struct xfs_da_state *state; /* btree cursor */
2133*4882a593Smuzhiyun
2134*4882a593Smuzhiyun trace_xfs_dir2_node_removename(args);
2135*4882a593Smuzhiyun
2136*4882a593Smuzhiyun /*
2137*4882a593Smuzhiyun * Allocate and initialize the btree cursor.
2138*4882a593Smuzhiyun */
2139*4882a593Smuzhiyun state = xfs_da_state_alloc(args);
2140*4882a593Smuzhiyun
2141*4882a593Smuzhiyun /* Look up the entry we're deleting, set up the cursor. */
2142*4882a593Smuzhiyun error = xfs_da3_node_lookup_int(state, &rval);
2143*4882a593Smuzhiyun if (error)
2144*4882a593Smuzhiyun goto out_free;
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun /* Didn't find it, upper layer screwed up. */
2147*4882a593Smuzhiyun if (rval != -EEXIST) {
2148*4882a593Smuzhiyun error = rval;
2149*4882a593Smuzhiyun goto out_free;
2150*4882a593Smuzhiyun }
2151*4882a593Smuzhiyun
2152*4882a593Smuzhiyun blk = &state->path.blk[state->path.active - 1];
2153*4882a593Smuzhiyun ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2154*4882a593Smuzhiyun ASSERT(state->extravalid);
2155*4882a593Smuzhiyun /*
2156*4882a593Smuzhiyun * Remove the leaf and data entries.
2157*4882a593Smuzhiyun * Extrablk refers to the data block.
2158*4882a593Smuzhiyun */
2159*4882a593Smuzhiyun error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2160*4882a593Smuzhiyun &state->extrablk, &rval);
2161*4882a593Smuzhiyun if (error)
2162*4882a593Smuzhiyun goto out_free;
2163*4882a593Smuzhiyun /*
2164*4882a593Smuzhiyun * Fix the hash values up the btree.
2165*4882a593Smuzhiyun */
2166*4882a593Smuzhiyun xfs_da3_fixhashpath(state, &state->path);
2167*4882a593Smuzhiyun /*
2168*4882a593Smuzhiyun * If we need to join leaf blocks, do it.
2169*4882a593Smuzhiyun */
2170*4882a593Smuzhiyun if (rval && state->path.active > 1)
2171*4882a593Smuzhiyun error = xfs_da3_join(state);
2172*4882a593Smuzhiyun /*
2173*4882a593Smuzhiyun * If no errors so far, try conversion to leaf format.
2174*4882a593Smuzhiyun */
2175*4882a593Smuzhiyun if (!error)
2176*4882a593Smuzhiyun error = xfs_dir2_node_to_leaf(state);
2177*4882a593Smuzhiyun out_free:
2178*4882a593Smuzhiyun xfs_da_state_free(state);
2179*4882a593Smuzhiyun return error;
2180*4882a593Smuzhiyun }
2181*4882a593Smuzhiyun
2182*4882a593Smuzhiyun /*
2183*4882a593Smuzhiyun * Replace an entry's inode number in a node-format directory.
2184*4882a593Smuzhiyun */
2185*4882a593Smuzhiyun int /* error */
xfs_dir2_node_replace(xfs_da_args_t * args)2186*4882a593Smuzhiyun xfs_dir2_node_replace(
2187*4882a593Smuzhiyun xfs_da_args_t *args) /* operation arguments */
2188*4882a593Smuzhiyun {
2189*4882a593Smuzhiyun xfs_da_state_blk_t *blk; /* leaf block */
2190*4882a593Smuzhiyun xfs_dir2_data_hdr_t *hdr; /* data block header */
2191*4882a593Smuzhiyun xfs_dir2_data_entry_t *dep; /* data entry changed */
2192*4882a593Smuzhiyun int error; /* error return value */
2193*4882a593Smuzhiyun int i; /* btree level */
2194*4882a593Smuzhiyun xfs_ino_t inum; /* new inode number */
2195*4882a593Smuzhiyun int ftype; /* new file type */
2196*4882a593Smuzhiyun int rval; /* internal return value */
2197*4882a593Smuzhiyun xfs_da_state_t *state; /* btree cursor */
2198*4882a593Smuzhiyun
2199*4882a593Smuzhiyun trace_xfs_dir2_node_replace(args);
2200*4882a593Smuzhiyun
2201*4882a593Smuzhiyun /*
2202*4882a593Smuzhiyun * Allocate and initialize the btree cursor.
2203*4882a593Smuzhiyun */
2204*4882a593Smuzhiyun state = xfs_da_state_alloc(args);
2205*4882a593Smuzhiyun
2206*4882a593Smuzhiyun /*
2207*4882a593Smuzhiyun * We have to save new inode number and ftype since
2208*4882a593Smuzhiyun * xfs_da3_node_lookup_int() is going to overwrite them
2209*4882a593Smuzhiyun */
2210*4882a593Smuzhiyun inum = args->inumber;
2211*4882a593Smuzhiyun ftype = args->filetype;
2212*4882a593Smuzhiyun
2213*4882a593Smuzhiyun /*
2214*4882a593Smuzhiyun * Lookup the entry to change in the btree.
2215*4882a593Smuzhiyun */
2216*4882a593Smuzhiyun error = xfs_da3_node_lookup_int(state, &rval);
2217*4882a593Smuzhiyun if (error) {
2218*4882a593Smuzhiyun rval = error;
2219*4882a593Smuzhiyun }
2220*4882a593Smuzhiyun /*
2221*4882a593Smuzhiyun * It should be found, since the vnodeops layer has looked it up
2222*4882a593Smuzhiyun * and locked it. But paranoia is good.
2223*4882a593Smuzhiyun */
2224*4882a593Smuzhiyun if (rval == -EEXIST) {
2225*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
2226*4882a593Smuzhiyun
2227*4882a593Smuzhiyun /*
2228*4882a593Smuzhiyun * Find the leaf entry.
2229*4882a593Smuzhiyun */
2230*4882a593Smuzhiyun blk = &state->path.blk[state->path.active - 1];
2231*4882a593Smuzhiyun ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2232*4882a593Smuzhiyun ASSERT(state->extravalid);
2233*4882a593Smuzhiyun
2234*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2235*4882a593Smuzhiyun blk->bp->b_addr);
2236*4882a593Smuzhiyun /*
2237*4882a593Smuzhiyun * Point to the data entry.
2238*4882a593Smuzhiyun */
2239*4882a593Smuzhiyun hdr = state->extrablk.bp->b_addr;
2240*4882a593Smuzhiyun ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2241*4882a593Smuzhiyun hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2242*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)
2243*4882a593Smuzhiyun ((char *)hdr +
2244*4882a593Smuzhiyun xfs_dir2_dataptr_to_off(args->geo,
2245*4882a593Smuzhiyun be32_to_cpu(leafhdr.ents[blk->index].address)));
2246*4882a593Smuzhiyun ASSERT(inum != be64_to_cpu(dep->inumber));
2247*4882a593Smuzhiyun /*
2248*4882a593Smuzhiyun * Fill in the new inode number and log the entry.
2249*4882a593Smuzhiyun */
2250*4882a593Smuzhiyun dep->inumber = cpu_to_be64(inum);
2251*4882a593Smuzhiyun xfs_dir2_data_put_ftype(state->mp, dep, ftype);
2252*4882a593Smuzhiyun xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2253*4882a593Smuzhiyun rval = 0;
2254*4882a593Smuzhiyun }
2255*4882a593Smuzhiyun /*
2256*4882a593Smuzhiyun * Didn't find it, and we're holding a data block. Drop it.
2257*4882a593Smuzhiyun */
2258*4882a593Smuzhiyun else if (state->extravalid) {
2259*4882a593Smuzhiyun xfs_trans_brelse(args->trans, state->extrablk.bp);
2260*4882a593Smuzhiyun state->extrablk.bp = NULL;
2261*4882a593Smuzhiyun }
2262*4882a593Smuzhiyun /*
2263*4882a593Smuzhiyun * Release all the buffers in the cursor.
2264*4882a593Smuzhiyun */
2265*4882a593Smuzhiyun for (i = 0; i < state->path.active; i++) {
2266*4882a593Smuzhiyun xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2267*4882a593Smuzhiyun state->path.blk[i].bp = NULL;
2268*4882a593Smuzhiyun }
2269*4882a593Smuzhiyun xfs_da_state_free(state);
2270*4882a593Smuzhiyun return rval;
2271*4882a593Smuzhiyun }
2272*4882a593Smuzhiyun
2273*4882a593Smuzhiyun /*
2274*4882a593Smuzhiyun * Trim off a trailing empty freespace block.
2275*4882a593Smuzhiyun * Return (in rvalp) 1 if we did it, 0 if not.
2276*4882a593Smuzhiyun */
2277*4882a593Smuzhiyun int /* error */
xfs_dir2_node_trim_free(xfs_da_args_t * args,xfs_fileoff_t fo,int * rvalp)2278*4882a593Smuzhiyun xfs_dir2_node_trim_free(
2279*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
2280*4882a593Smuzhiyun xfs_fileoff_t fo, /* free block number */
2281*4882a593Smuzhiyun int *rvalp) /* out: did something */
2282*4882a593Smuzhiyun {
2283*4882a593Smuzhiyun struct xfs_buf *bp; /* freespace buffer */
2284*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
2285*4882a593Smuzhiyun int error; /* error return code */
2286*4882a593Smuzhiyun xfs_dir2_free_t *free; /* freespace structure */
2287*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
2288*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr freehdr;
2289*4882a593Smuzhiyun
2290*4882a593Smuzhiyun dp = args->dp;
2291*4882a593Smuzhiyun tp = args->trans;
2292*4882a593Smuzhiyun
2293*4882a593Smuzhiyun *rvalp = 0;
2294*4882a593Smuzhiyun
2295*4882a593Smuzhiyun /*
2296*4882a593Smuzhiyun * Read the freespace block.
2297*4882a593Smuzhiyun */
2298*4882a593Smuzhiyun error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
2299*4882a593Smuzhiyun if (error)
2300*4882a593Smuzhiyun return error;
2301*4882a593Smuzhiyun /*
2302*4882a593Smuzhiyun * There can be holes in freespace. If fo is a hole, there's
2303*4882a593Smuzhiyun * nothing to do.
2304*4882a593Smuzhiyun */
2305*4882a593Smuzhiyun if (!bp)
2306*4882a593Smuzhiyun return 0;
2307*4882a593Smuzhiyun free = bp->b_addr;
2308*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
2309*4882a593Smuzhiyun
2310*4882a593Smuzhiyun /*
2311*4882a593Smuzhiyun * If there are used entries, there's nothing to do.
2312*4882a593Smuzhiyun */
2313*4882a593Smuzhiyun if (freehdr.nused > 0) {
2314*4882a593Smuzhiyun xfs_trans_brelse(tp, bp);
2315*4882a593Smuzhiyun return 0;
2316*4882a593Smuzhiyun }
2317*4882a593Smuzhiyun /*
2318*4882a593Smuzhiyun * Blow the block away.
2319*4882a593Smuzhiyun */
2320*4882a593Smuzhiyun error = xfs_dir2_shrink_inode(args,
2321*4882a593Smuzhiyun xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2322*4882a593Smuzhiyun if (error) {
2323*4882a593Smuzhiyun /*
2324*4882a593Smuzhiyun * Can't fail with ENOSPC since that only happens with no
2325*4882a593Smuzhiyun * space reservation, when breaking up an extent into two
2326*4882a593Smuzhiyun * pieces. This is the last block of an extent.
2327*4882a593Smuzhiyun */
2328*4882a593Smuzhiyun ASSERT(error != -ENOSPC);
2329*4882a593Smuzhiyun xfs_trans_brelse(tp, bp);
2330*4882a593Smuzhiyun return error;
2331*4882a593Smuzhiyun }
2332*4882a593Smuzhiyun /*
2333*4882a593Smuzhiyun * Return that we succeeded.
2334*4882a593Smuzhiyun */
2335*4882a593Smuzhiyun *rvalp = 1;
2336*4882a593Smuzhiyun return 0;
2337*4882a593Smuzhiyun }
2338