1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2000-2003,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
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * Local function declarations.
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
27*4882a593Smuzhiyun int *indexp, struct xfs_buf **dbpp,
28*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *leafhdr);
29*4882a593Smuzhiyun static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
30*4882a593Smuzhiyun struct xfs_buf *bp, int first, int last);
31*4882a593Smuzhiyun static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
32*4882a593Smuzhiyun struct xfs_buf *bp);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun void
xfs_dir2_leaf_hdr_from_disk(struct xfs_mount * mp,struct xfs_dir3_icleaf_hdr * to,struct xfs_dir2_leaf * from)35*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(
36*4882a593Smuzhiyun struct xfs_mount *mp,
37*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *to,
38*4882a593Smuzhiyun struct xfs_dir2_leaf *from)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
41*4882a593Smuzhiyun struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
44*4882a593Smuzhiyun to->back = be32_to_cpu(from3->hdr.info.hdr.back);
45*4882a593Smuzhiyun to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
46*4882a593Smuzhiyun to->count = be16_to_cpu(from3->hdr.count);
47*4882a593Smuzhiyun to->stale = be16_to_cpu(from3->hdr.stale);
48*4882a593Smuzhiyun to->ents = from3->__ents;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
51*4882a593Smuzhiyun to->magic == XFS_DIR3_LEAFN_MAGIC);
52*4882a593Smuzhiyun } else {
53*4882a593Smuzhiyun to->forw = be32_to_cpu(from->hdr.info.forw);
54*4882a593Smuzhiyun to->back = be32_to_cpu(from->hdr.info.back);
55*4882a593Smuzhiyun to->magic = be16_to_cpu(from->hdr.info.magic);
56*4882a593Smuzhiyun to->count = be16_to_cpu(from->hdr.count);
57*4882a593Smuzhiyun to->stale = be16_to_cpu(from->hdr.stale);
58*4882a593Smuzhiyun to->ents = from->__ents;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
61*4882a593Smuzhiyun to->magic == XFS_DIR2_LEAFN_MAGIC);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun void
xfs_dir2_leaf_hdr_to_disk(struct xfs_mount * mp,struct xfs_dir2_leaf * to,struct xfs_dir3_icleaf_hdr * from)66*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(
67*4882a593Smuzhiyun struct xfs_mount *mp,
68*4882a593Smuzhiyun struct xfs_dir2_leaf *to,
69*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *from)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
72*4882a593Smuzhiyun struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
75*4882a593Smuzhiyun from->magic == XFS_DIR3_LEAFN_MAGIC);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
78*4882a593Smuzhiyun to3->hdr.info.hdr.back = cpu_to_be32(from->back);
79*4882a593Smuzhiyun to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
80*4882a593Smuzhiyun to3->hdr.count = cpu_to_be16(from->count);
81*4882a593Smuzhiyun to3->hdr.stale = cpu_to_be16(from->stale);
82*4882a593Smuzhiyun } else {
83*4882a593Smuzhiyun ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
84*4882a593Smuzhiyun from->magic == XFS_DIR2_LEAFN_MAGIC);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun to->hdr.info.forw = cpu_to_be32(from->forw);
87*4882a593Smuzhiyun to->hdr.info.back = cpu_to_be32(from->back);
88*4882a593Smuzhiyun to->hdr.info.magic = cpu_to_be16(from->magic);
89*4882a593Smuzhiyun to->hdr.count = cpu_to_be16(from->count);
90*4882a593Smuzhiyun to->hdr.stale = cpu_to_be16(from->stale);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun * Check the internal consistency of a leaf1 block.
96*4882a593Smuzhiyun * Pop an assert if something is wrong.
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun #ifdef DEBUG
99*4882a593Smuzhiyun static xfs_failaddr_t
xfs_dir3_leaf1_check(struct xfs_inode * dp,struct xfs_buf * bp)100*4882a593Smuzhiyun xfs_dir3_leaf1_check(
101*4882a593Smuzhiyun struct xfs_inode *dp,
102*4882a593Smuzhiyun struct xfs_buf *bp)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf = bp->b_addr;
105*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
110*4882a593Smuzhiyun struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
111*4882a593Smuzhiyun if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
112*4882a593Smuzhiyun return __this_address;
113*4882a593Smuzhiyun } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
114*4882a593Smuzhiyun return __this_address;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun static inline void
xfs_dir3_leaf_check(struct xfs_inode * dp,struct xfs_buf * bp)120*4882a593Smuzhiyun xfs_dir3_leaf_check(
121*4882a593Smuzhiyun struct xfs_inode *dp,
122*4882a593Smuzhiyun struct xfs_buf *bp)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun xfs_failaddr_t fa;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun fa = xfs_dir3_leaf1_check(dp, bp);
127*4882a593Smuzhiyun if (!fa)
128*4882a593Smuzhiyun return;
129*4882a593Smuzhiyun xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
130*4882a593Smuzhiyun bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
131*4882a593Smuzhiyun fa);
132*4882a593Smuzhiyun ASSERT(0);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun #else
135*4882a593Smuzhiyun #define xfs_dir3_leaf_check(dp, bp)
136*4882a593Smuzhiyun #endif
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun xfs_failaddr_t
xfs_dir3_leaf_check_int(struct xfs_mount * mp,struct xfs_dir3_icleaf_hdr * hdr,struct xfs_dir2_leaf * leaf)139*4882a593Smuzhiyun xfs_dir3_leaf_check_int(
140*4882a593Smuzhiyun struct xfs_mount *mp,
141*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *hdr,
142*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun struct xfs_da_geometry *geo = mp->m_dir_geo;
145*4882a593Smuzhiyun xfs_dir2_leaf_tail_t *ltp;
146*4882a593Smuzhiyun int stale;
147*4882a593Smuzhiyun int i;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(geo, leaf);
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun * XXX (dgc): This value is not restrictive enough.
153*4882a593Smuzhiyun * Should factor in the size of the bests table as well.
154*4882a593Smuzhiyun * We can deduce a value for that from di_size.
155*4882a593Smuzhiyun */
156*4882a593Smuzhiyun if (hdr->count > geo->leaf_max_ents)
157*4882a593Smuzhiyun return __this_address;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* Leaves and bests don't overlap in leaf format. */
160*4882a593Smuzhiyun if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
161*4882a593Smuzhiyun hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
162*4882a593Smuzhiyun (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
163*4882a593Smuzhiyun return __this_address;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* Check hash value order, count stale entries. */
166*4882a593Smuzhiyun for (i = stale = 0; i < hdr->count; i++) {
167*4882a593Smuzhiyun if (i + 1 < hdr->count) {
168*4882a593Smuzhiyun if (be32_to_cpu(hdr->ents[i].hashval) >
169*4882a593Smuzhiyun be32_to_cpu(hdr->ents[i + 1].hashval))
170*4882a593Smuzhiyun return __this_address;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
173*4882a593Smuzhiyun stale++;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun if (hdr->stale != stale)
176*4882a593Smuzhiyun return __this_address;
177*4882a593Smuzhiyun return NULL;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * We verify the magic numbers before decoding the leaf header so that on debug
182*4882a593Smuzhiyun * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
183*4882a593Smuzhiyun * to incorrect magic numbers.
184*4882a593Smuzhiyun */
185*4882a593Smuzhiyun static xfs_failaddr_t
xfs_dir3_leaf_verify(struct xfs_buf * bp)186*4882a593Smuzhiyun xfs_dir3_leaf_verify(
187*4882a593Smuzhiyun struct xfs_buf *bp)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
190*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
191*4882a593Smuzhiyun xfs_failaddr_t fa;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
194*4882a593Smuzhiyun if (fa)
195*4882a593Smuzhiyun return fa;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr);
198*4882a593Smuzhiyun return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun static void
xfs_dir3_leaf_read_verify(struct xfs_buf * bp)202*4882a593Smuzhiyun xfs_dir3_leaf_read_verify(
203*4882a593Smuzhiyun struct xfs_buf *bp)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
206*4882a593Smuzhiyun xfs_failaddr_t fa;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb) &&
209*4882a593Smuzhiyun !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
210*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSBADCRC, __this_address);
211*4882a593Smuzhiyun else {
212*4882a593Smuzhiyun fa = xfs_dir3_leaf_verify(bp);
213*4882a593Smuzhiyun if (fa)
214*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSCORRUPTED, fa);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun static void
xfs_dir3_leaf_write_verify(struct xfs_buf * bp)219*4882a593Smuzhiyun xfs_dir3_leaf_write_verify(
220*4882a593Smuzhiyun struct xfs_buf *bp)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
223*4882a593Smuzhiyun struct xfs_buf_log_item *bip = bp->b_log_item;
224*4882a593Smuzhiyun struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
225*4882a593Smuzhiyun xfs_failaddr_t fa;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun fa = xfs_dir3_leaf_verify(bp);
228*4882a593Smuzhiyun if (fa) {
229*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSCORRUPTED, fa);
230*4882a593Smuzhiyun return;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun if (!xfs_sb_version_hascrc(&mp->m_sb))
234*4882a593Smuzhiyun return;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun if (bip)
237*4882a593Smuzhiyun hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
243*4882a593Smuzhiyun .name = "xfs_dir3_leaf1",
244*4882a593Smuzhiyun .magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC),
245*4882a593Smuzhiyun cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) },
246*4882a593Smuzhiyun .verify_read = xfs_dir3_leaf_read_verify,
247*4882a593Smuzhiyun .verify_write = xfs_dir3_leaf_write_verify,
248*4882a593Smuzhiyun .verify_struct = xfs_dir3_leaf_verify,
249*4882a593Smuzhiyun };
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
252*4882a593Smuzhiyun .name = "xfs_dir3_leafn",
253*4882a593Smuzhiyun .magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC),
254*4882a593Smuzhiyun cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) },
255*4882a593Smuzhiyun .verify_read = xfs_dir3_leaf_read_verify,
256*4882a593Smuzhiyun .verify_write = xfs_dir3_leaf_write_verify,
257*4882a593Smuzhiyun .verify_struct = xfs_dir3_leaf_verify,
258*4882a593Smuzhiyun };
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun int
xfs_dir3_leaf_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t fbno,struct xfs_buf ** bpp)261*4882a593Smuzhiyun xfs_dir3_leaf_read(
262*4882a593Smuzhiyun struct xfs_trans *tp,
263*4882a593Smuzhiyun struct xfs_inode *dp,
264*4882a593Smuzhiyun xfs_dablk_t fbno,
265*4882a593Smuzhiyun struct xfs_buf **bpp)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun int err;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
270*4882a593Smuzhiyun &xfs_dir3_leaf1_buf_ops);
271*4882a593Smuzhiyun if (!err && tp && *bpp)
272*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
273*4882a593Smuzhiyun return err;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun int
xfs_dir3_leafn_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t fbno,struct xfs_buf ** bpp)277*4882a593Smuzhiyun xfs_dir3_leafn_read(
278*4882a593Smuzhiyun struct xfs_trans *tp,
279*4882a593Smuzhiyun struct xfs_inode *dp,
280*4882a593Smuzhiyun xfs_dablk_t fbno,
281*4882a593Smuzhiyun struct xfs_buf **bpp)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun int err;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
286*4882a593Smuzhiyun &xfs_dir3_leafn_buf_ops);
287*4882a593Smuzhiyun if (!err && tp && *bpp)
288*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
289*4882a593Smuzhiyun return err;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /*
293*4882a593Smuzhiyun * Initialize a new leaf block, leaf1 or leafn magic accepted.
294*4882a593Smuzhiyun */
295*4882a593Smuzhiyun static void
xfs_dir3_leaf_init(struct xfs_mount * mp,struct xfs_trans * tp,struct xfs_buf * bp,xfs_ino_t owner,uint16_t type)296*4882a593Smuzhiyun xfs_dir3_leaf_init(
297*4882a593Smuzhiyun struct xfs_mount *mp,
298*4882a593Smuzhiyun struct xfs_trans *tp,
299*4882a593Smuzhiyun struct xfs_buf *bp,
300*4882a593Smuzhiyun xfs_ino_t owner,
301*4882a593Smuzhiyun uint16_t type)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf = bp->b_addr;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
308*4882a593Smuzhiyun struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun memset(leaf3, 0, sizeof(*leaf3));
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
313*4882a593Smuzhiyun ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
314*4882a593Smuzhiyun : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
315*4882a593Smuzhiyun leaf3->info.blkno = cpu_to_be64(bp->b_bn);
316*4882a593Smuzhiyun leaf3->info.owner = cpu_to_be64(owner);
317*4882a593Smuzhiyun uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
318*4882a593Smuzhiyun } else {
319*4882a593Smuzhiyun memset(leaf, 0, sizeof(*leaf));
320*4882a593Smuzhiyun leaf->hdr.info.magic = cpu_to_be16(type);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /*
324*4882a593Smuzhiyun * If it's a leaf-format directory initialize the tail.
325*4882a593Smuzhiyun * Caller is responsible for initialising the bests table.
326*4882a593Smuzhiyun */
327*4882a593Smuzhiyun if (type == XFS_DIR2_LEAF1_MAGIC) {
328*4882a593Smuzhiyun struct xfs_dir2_leaf_tail *ltp;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
331*4882a593Smuzhiyun ltp->bestcount = 0;
332*4882a593Smuzhiyun bp->b_ops = &xfs_dir3_leaf1_buf_ops;
333*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
334*4882a593Smuzhiyun } else {
335*4882a593Smuzhiyun bp->b_ops = &xfs_dir3_leafn_buf_ops;
336*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun int
xfs_dir3_leaf_get_buf(xfs_da_args_t * args,xfs_dir2_db_t bno,struct xfs_buf ** bpp,uint16_t magic)341*4882a593Smuzhiyun xfs_dir3_leaf_get_buf(
342*4882a593Smuzhiyun xfs_da_args_t *args,
343*4882a593Smuzhiyun xfs_dir2_db_t bno,
344*4882a593Smuzhiyun struct xfs_buf **bpp,
345*4882a593Smuzhiyun uint16_t magic)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun struct xfs_inode *dp = args->dp;
348*4882a593Smuzhiyun struct xfs_trans *tp = args->trans;
349*4882a593Smuzhiyun struct xfs_mount *mp = dp->i_mount;
350*4882a593Smuzhiyun struct xfs_buf *bp;
351*4882a593Smuzhiyun int error;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
354*4882a593Smuzhiyun ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
355*4882a593Smuzhiyun bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
358*4882a593Smuzhiyun &bp, XFS_DATA_FORK);
359*4882a593Smuzhiyun if (error)
360*4882a593Smuzhiyun return error;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
363*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, bp);
364*4882a593Smuzhiyun if (magic == XFS_DIR2_LEAF1_MAGIC)
365*4882a593Smuzhiyun xfs_dir3_leaf_log_tail(args, bp);
366*4882a593Smuzhiyun *bpp = bp;
367*4882a593Smuzhiyun return 0;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun * Convert a block form directory to a leaf form directory.
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun int /* error */
xfs_dir2_block_to_leaf(xfs_da_args_t * args,struct xfs_buf * dbp)374*4882a593Smuzhiyun xfs_dir2_block_to_leaf(
375*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
376*4882a593Smuzhiyun struct xfs_buf *dbp) /* input block's buffer */
377*4882a593Smuzhiyun {
378*4882a593Smuzhiyun __be16 *bestsp; /* leaf's bestsp entries */
379*4882a593Smuzhiyun xfs_dablk_t blkno; /* leaf block's bno */
380*4882a593Smuzhiyun xfs_dir2_data_hdr_t *hdr; /* block header */
381*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
382*4882a593Smuzhiyun xfs_dir2_block_tail_t *btp; /* block's tail */
383*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
384*4882a593Smuzhiyun int error; /* error return code */
385*4882a593Smuzhiyun struct xfs_buf *lbp; /* leaf block's buffer */
386*4882a593Smuzhiyun xfs_dir2_db_t ldb; /* leaf block's bno */
387*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
388*4882a593Smuzhiyun xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
389*4882a593Smuzhiyun int needlog; /* need to log block header */
390*4882a593Smuzhiyun int needscan; /* need to rescan bestfree */
391*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
392*4882a593Smuzhiyun struct xfs_dir2_data_free *bf;
393*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun trace_xfs_dir2_block_to_leaf(args);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun dp = args->dp;
398*4882a593Smuzhiyun tp = args->trans;
399*4882a593Smuzhiyun /*
400*4882a593Smuzhiyun * Add the leaf block to the inode.
401*4882a593Smuzhiyun * This interface will only put blocks in the leaf/node range.
402*4882a593Smuzhiyun * Since that's empty now, we'll get the root (block 0 in range).
403*4882a593Smuzhiyun */
404*4882a593Smuzhiyun if ((error = xfs_da_grow_inode(args, &blkno))) {
405*4882a593Smuzhiyun return error;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun ldb = xfs_dir2_da_to_db(args->geo, blkno);
408*4882a593Smuzhiyun ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
409*4882a593Smuzhiyun /*
410*4882a593Smuzhiyun * Initialize the leaf block, get a buffer for it.
411*4882a593Smuzhiyun */
412*4882a593Smuzhiyun error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
413*4882a593Smuzhiyun if (error)
414*4882a593Smuzhiyun return error;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun leaf = lbp->b_addr;
417*4882a593Smuzhiyun hdr = dbp->b_addr;
418*4882a593Smuzhiyun xfs_dir3_data_check(dp, dbp);
419*4882a593Smuzhiyun btp = xfs_dir2_block_tail_p(args->geo, hdr);
420*4882a593Smuzhiyun blp = xfs_dir2_block_leaf_p(btp);
421*4882a593Smuzhiyun bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /*
424*4882a593Smuzhiyun * Set the counts in the leaf header.
425*4882a593Smuzhiyun */
426*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
427*4882a593Smuzhiyun leafhdr.count = be32_to_cpu(btp->count);
428*4882a593Smuzhiyun leafhdr.stale = be32_to_cpu(btp->stale);
429*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
430*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, lbp);
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun /*
433*4882a593Smuzhiyun * Could compact these but I think we always do the conversion
434*4882a593Smuzhiyun * after squeezing out stale entries.
435*4882a593Smuzhiyun */
436*4882a593Smuzhiyun memcpy(leafhdr.ents, blp,
437*4882a593Smuzhiyun be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry));
438*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1);
439*4882a593Smuzhiyun needscan = 0;
440*4882a593Smuzhiyun needlog = 1;
441*4882a593Smuzhiyun /*
442*4882a593Smuzhiyun * Make the space formerly occupied by the leaf entries and block
443*4882a593Smuzhiyun * tail be free.
444*4882a593Smuzhiyun */
445*4882a593Smuzhiyun xfs_dir2_data_make_free(args, dbp,
446*4882a593Smuzhiyun (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
447*4882a593Smuzhiyun (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
448*4882a593Smuzhiyun (char *)blp),
449*4882a593Smuzhiyun &needlog, &needscan);
450*4882a593Smuzhiyun /*
451*4882a593Smuzhiyun * Fix up the block header, make it a data block.
452*4882a593Smuzhiyun */
453*4882a593Smuzhiyun dbp->b_ops = &xfs_dir3_data_buf_ops;
454*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
455*4882a593Smuzhiyun if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
456*4882a593Smuzhiyun hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
457*4882a593Smuzhiyun else
458*4882a593Smuzhiyun hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun if (needscan)
461*4882a593Smuzhiyun xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
462*4882a593Smuzhiyun /*
463*4882a593Smuzhiyun * Set up leaf tail and bests table.
464*4882a593Smuzhiyun */
465*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
466*4882a593Smuzhiyun ltp->bestcount = cpu_to_be32(1);
467*4882a593Smuzhiyun bestsp = xfs_dir2_leaf_bests_p(ltp);
468*4882a593Smuzhiyun bestsp[0] = bf[0].length;
469*4882a593Smuzhiyun /*
470*4882a593Smuzhiyun * Log the data header and leaf bests table.
471*4882a593Smuzhiyun */
472*4882a593Smuzhiyun if (needlog)
473*4882a593Smuzhiyun xfs_dir2_data_log_header(args, dbp);
474*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
475*4882a593Smuzhiyun xfs_dir3_data_check(dp, dbp);
476*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
477*4882a593Smuzhiyun return 0;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun STATIC void
xfs_dir3_leaf_find_stale(struct xfs_dir3_icleaf_hdr * leafhdr,struct xfs_dir2_leaf_entry * ents,int index,int * lowstale,int * highstale)481*4882a593Smuzhiyun xfs_dir3_leaf_find_stale(
482*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *leafhdr,
483*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents,
484*4882a593Smuzhiyun int index,
485*4882a593Smuzhiyun int *lowstale,
486*4882a593Smuzhiyun int *highstale)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * Find the first stale entry before our index, if any.
490*4882a593Smuzhiyun */
491*4882a593Smuzhiyun for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
492*4882a593Smuzhiyun if (ents[*lowstale].address ==
493*4882a593Smuzhiyun cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
494*4882a593Smuzhiyun break;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /*
498*4882a593Smuzhiyun * Find the first stale entry at or after our index, if any.
499*4882a593Smuzhiyun * Stop if the result would require moving more entries than using
500*4882a593Smuzhiyun * lowstale.
501*4882a593Smuzhiyun */
502*4882a593Smuzhiyun for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
503*4882a593Smuzhiyun if (ents[*highstale].address ==
504*4882a593Smuzhiyun cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
505*4882a593Smuzhiyun break;
506*4882a593Smuzhiyun if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
507*4882a593Smuzhiyun break;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *
xfs_dir3_leaf_find_entry(struct xfs_dir3_icleaf_hdr * leafhdr,struct xfs_dir2_leaf_entry * ents,int index,int compact,int lowstale,int highstale,int * lfloglow,int * lfloghigh)512*4882a593Smuzhiyun xfs_dir3_leaf_find_entry(
513*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *leafhdr,
514*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents,
515*4882a593Smuzhiyun int index, /* leaf table position */
516*4882a593Smuzhiyun int compact, /* need to compact leaves */
517*4882a593Smuzhiyun int lowstale, /* index of prev stale leaf */
518*4882a593Smuzhiyun int highstale, /* index of next stale leaf */
519*4882a593Smuzhiyun int *lfloglow, /* low leaf logging index */
520*4882a593Smuzhiyun int *lfloghigh) /* high leaf logging index */
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun if (!leafhdr->stale) {
523*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun /*
526*4882a593Smuzhiyun * Now we need to make room to insert the leaf entry.
527*4882a593Smuzhiyun *
528*4882a593Smuzhiyun * If there are no stale entries, just insert a hole at index.
529*4882a593Smuzhiyun */
530*4882a593Smuzhiyun lep = &ents[index];
531*4882a593Smuzhiyun if (index < leafhdr->count)
532*4882a593Smuzhiyun memmove(lep + 1, lep,
533*4882a593Smuzhiyun (leafhdr->count - index) * sizeof(*lep));
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun /*
536*4882a593Smuzhiyun * Record low and high logging indices for the leaf.
537*4882a593Smuzhiyun */
538*4882a593Smuzhiyun *lfloglow = index;
539*4882a593Smuzhiyun *lfloghigh = leafhdr->count++;
540*4882a593Smuzhiyun return lep;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun * There are stale entries.
545*4882a593Smuzhiyun *
546*4882a593Smuzhiyun * We will use one of them for the new entry. It's probably not at
547*4882a593Smuzhiyun * the right location, so we'll have to shift some up or down first.
548*4882a593Smuzhiyun *
549*4882a593Smuzhiyun * If we didn't compact before, we need to find the nearest stale
550*4882a593Smuzhiyun * entries before and after our insertion point.
551*4882a593Smuzhiyun */
552*4882a593Smuzhiyun if (compact == 0)
553*4882a593Smuzhiyun xfs_dir3_leaf_find_stale(leafhdr, ents, index,
554*4882a593Smuzhiyun &lowstale, &highstale);
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun /*
557*4882a593Smuzhiyun * If the low one is better, use it.
558*4882a593Smuzhiyun */
559*4882a593Smuzhiyun if (lowstale >= 0 &&
560*4882a593Smuzhiyun (highstale == leafhdr->count ||
561*4882a593Smuzhiyun index - lowstale - 1 < highstale - index)) {
562*4882a593Smuzhiyun ASSERT(index - lowstale - 1 >= 0);
563*4882a593Smuzhiyun ASSERT(ents[lowstale].address ==
564*4882a593Smuzhiyun cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun /*
567*4882a593Smuzhiyun * Copy entries up to cover the stale entry and make room
568*4882a593Smuzhiyun * for the new entry.
569*4882a593Smuzhiyun */
570*4882a593Smuzhiyun if (index - lowstale - 1 > 0) {
571*4882a593Smuzhiyun memmove(&ents[lowstale], &ents[lowstale + 1],
572*4882a593Smuzhiyun (index - lowstale - 1) *
573*4882a593Smuzhiyun sizeof(xfs_dir2_leaf_entry_t));
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun *lfloglow = min(lowstale, *lfloglow);
576*4882a593Smuzhiyun *lfloghigh = max(index - 1, *lfloghigh);
577*4882a593Smuzhiyun leafhdr->stale--;
578*4882a593Smuzhiyun return &ents[index - 1];
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /*
582*4882a593Smuzhiyun * The high one is better, so use that one.
583*4882a593Smuzhiyun */
584*4882a593Smuzhiyun ASSERT(highstale - index >= 0);
585*4882a593Smuzhiyun ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun /*
588*4882a593Smuzhiyun * Copy entries down to cover the stale entry and make room for the
589*4882a593Smuzhiyun * new entry.
590*4882a593Smuzhiyun */
591*4882a593Smuzhiyun if (highstale - index > 0) {
592*4882a593Smuzhiyun memmove(&ents[index + 1], &ents[index],
593*4882a593Smuzhiyun (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun *lfloglow = min(index, *lfloglow);
596*4882a593Smuzhiyun *lfloghigh = max(highstale, *lfloghigh);
597*4882a593Smuzhiyun leafhdr->stale--;
598*4882a593Smuzhiyun return &ents[index];
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun /*
602*4882a593Smuzhiyun * Add an entry to a leaf form directory.
603*4882a593Smuzhiyun */
604*4882a593Smuzhiyun int /* error */
xfs_dir2_leaf_addname(struct xfs_da_args * args)605*4882a593Smuzhiyun xfs_dir2_leaf_addname(
606*4882a593Smuzhiyun struct xfs_da_args *args) /* operation arguments */
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
609*4882a593Smuzhiyun struct xfs_trans *tp = args->trans;
610*4882a593Smuzhiyun __be16 *bestsp; /* freespace table in leaf */
611*4882a593Smuzhiyun __be16 *tagp; /* end of data entry */
612*4882a593Smuzhiyun struct xfs_buf *dbp; /* data block buffer */
613*4882a593Smuzhiyun struct xfs_buf *lbp; /* leaf's buffer */
614*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf; /* leaf structure */
615*4882a593Smuzhiyun struct xfs_inode *dp = args->dp; /* incore directory inode */
616*4882a593Smuzhiyun struct xfs_dir2_data_hdr *hdr; /* data block header */
617*4882a593Smuzhiyun struct xfs_dir2_data_entry *dep; /* data block entry */
618*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *lep; /* leaf entry table pointer */
619*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents;
620*4882a593Smuzhiyun struct xfs_dir2_data_unused *dup; /* data unused entry */
621*4882a593Smuzhiyun struct xfs_dir2_leaf_tail *ltp; /* leaf tail pointer */
622*4882a593Smuzhiyun struct xfs_dir2_data_free *bf; /* bestfree table */
623*4882a593Smuzhiyun int compact; /* need to compact leaves */
624*4882a593Smuzhiyun int error; /* error return value */
625*4882a593Smuzhiyun int grown; /* allocated new data block */
626*4882a593Smuzhiyun int highstale = 0; /* index of next stale leaf */
627*4882a593Smuzhiyun int i; /* temporary, index */
628*4882a593Smuzhiyun int index; /* leaf table position */
629*4882a593Smuzhiyun int length; /* length of new entry */
630*4882a593Smuzhiyun int lfloglow; /* low leaf logging index */
631*4882a593Smuzhiyun int lfloghigh; /* high leaf logging index */
632*4882a593Smuzhiyun int lowstale = 0; /* index of prev stale leaf */
633*4882a593Smuzhiyun int needbytes; /* leaf block bytes needed */
634*4882a593Smuzhiyun int needlog; /* need to log data header */
635*4882a593Smuzhiyun int needscan; /* need to rescan data free */
636*4882a593Smuzhiyun xfs_dir2_db_t use_block; /* data block number */
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun trace_xfs_dir2_leaf_addname(args);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, &lbp);
641*4882a593Smuzhiyun if (error)
642*4882a593Smuzhiyun return error;
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /*
645*4882a593Smuzhiyun * Look up the entry by hash value and name.
646*4882a593Smuzhiyun * We know it's not there, our caller has already done a lookup.
647*4882a593Smuzhiyun * So the index is of the entry to insert in front of.
648*4882a593Smuzhiyun * But if there are dup hash values the index is of the first of those.
649*4882a593Smuzhiyun */
650*4882a593Smuzhiyun index = xfs_dir2_leaf_search_hash(args, lbp);
651*4882a593Smuzhiyun leaf = lbp->b_addr;
652*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
653*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
654*4882a593Smuzhiyun ents = leafhdr.ents;
655*4882a593Smuzhiyun bestsp = xfs_dir2_leaf_bests_p(ltp);
656*4882a593Smuzhiyun length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun /*
659*4882a593Smuzhiyun * See if there are any entries with the same hash value
660*4882a593Smuzhiyun * and space in their block for the new entry.
661*4882a593Smuzhiyun * This is good because it puts multiple same-hash value entries
662*4882a593Smuzhiyun * in a data block, improving the lookup of those entries.
663*4882a593Smuzhiyun */
664*4882a593Smuzhiyun for (use_block = -1, lep = &ents[index];
665*4882a593Smuzhiyun index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
666*4882a593Smuzhiyun index++, lep++) {
667*4882a593Smuzhiyun if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
668*4882a593Smuzhiyun continue;
669*4882a593Smuzhiyun i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
670*4882a593Smuzhiyun ASSERT(i < be32_to_cpu(ltp->bestcount));
671*4882a593Smuzhiyun ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
672*4882a593Smuzhiyun if (be16_to_cpu(bestsp[i]) >= length) {
673*4882a593Smuzhiyun use_block = i;
674*4882a593Smuzhiyun break;
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun /*
678*4882a593Smuzhiyun * Didn't find a block yet, linear search all the data blocks.
679*4882a593Smuzhiyun */
680*4882a593Smuzhiyun if (use_block == -1) {
681*4882a593Smuzhiyun for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
682*4882a593Smuzhiyun /*
683*4882a593Smuzhiyun * Remember a block we see that's missing.
684*4882a593Smuzhiyun */
685*4882a593Smuzhiyun if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
686*4882a593Smuzhiyun use_block == -1)
687*4882a593Smuzhiyun use_block = i;
688*4882a593Smuzhiyun else if (be16_to_cpu(bestsp[i]) >= length) {
689*4882a593Smuzhiyun use_block = i;
690*4882a593Smuzhiyun break;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun /*
695*4882a593Smuzhiyun * How many bytes do we need in the leaf block?
696*4882a593Smuzhiyun */
697*4882a593Smuzhiyun needbytes = 0;
698*4882a593Smuzhiyun if (!leafhdr.stale)
699*4882a593Smuzhiyun needbytes += sizeof(xfs_dir2_leaf_entry_t);
700*4882a593Smuzhiyun if (use_block == -1)
701*4882a593Smuzhiyun needbytes += sizeof(xfs_dir2_data_off_t);
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /*
704*4882a593Smuzhiyun * Now kill use_block if it refers to a missing block, so we
705*4882a593Smuzhiyun * can use it as an indication of allocation needed.
706*4882a593Smuzhiyun */
707*4882a593Smuzhiyun if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
708*4882a593Smuzhiyun use_block = -1;
709*4882a593Smuzhiyun /*
710*4882a593Smuzhiyun * If we don't have enough free bytes but we can make enough
711*4882a593Smuzhiyun * by compacting out stale entries, we'll do that.
712*4882a593Smuzhiyun */
713*4882a593Smuzhiyun if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
714*4882a593Smuzhiyun leafhdr.stale > 1)
715*4882a593Smuzhiyun compact = 1;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun /*
718*4882a593Smuzhiyun * Otherwise if we don't have enough free bytes we need to
719*4882a593Smuzhiyun * convert to node form.
720*4882a593Smuzhiyun */
721*4882a593Smuzhiyun else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
722*4882a593Smuzhiyun /*
723*4882a593Smuzhiyun * Just checking or no space reservation, give up.
724*4882a593Smuzhiyun */
725*4882a593Smuzhiyun if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
726*4882a593Smuzhiyun args->total == 0) {
727*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
728*4882a593Smuzhiyun return -ENOSPC;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun /*
731*4882a593Smuzhiyun * Convert to node form.
732*4882a593Smuzhiyun */
733*4882a593Smuzhiyun error = xfs_dir2_leaf_to_node(args, lbp);
734*4882a593Smuzhiyun if (error)
735*4882a593Smuzhiyun return error;
736*4882a593Smuzhiyun /*
737*4882a593Smuzhiyun * Then add the new entry.
738*4882a593Smuzhiyun */
739*4882a593Smuzhiyun return xfs_dir2_node_addname(args);
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun /*
742*4882a593Smuzhiyun * Otherwise it will fit without compaction.
743*4882a593Smuzhiyun */
744*4882a593Smuzhiyun else
745*4882a593Smuzhiyun compact = 0;
746*4882a593Smuzhiyun /*
747*4882a593Smuzhiyun * If just checking, then it will fit unless we needed to allocate
748*4882a593Smuzhiyun * a new data block.
749*4882a593Smuzhiyun */
750*4882a593Smuzhiyun if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
751*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
752*4882a593Smuzhiyun return use_block == -1 ? -ENOSPC : 0;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun /*
755*4882a593Smuzhiyun * If no allocations are allowed, return now before we've
756*4882a593Smuzhiyun * changed anything.
757*4882a593Smuzhiyun */
758*4882a593Smuzhiyun if (args->total == 0 && use_block == -1) {
759*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
760*4882a593Smuzhiyun return -ENOSPC;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun /*
763*4882a593Smuzhiyun * Need to compact the leaf entries, removing stale ones.
764*4882a593Smuzhiyun * Leave one stale entry behind - the one closest to our
765*4882a593Smuzhiyun * insertion index - and we'll shift that one to our insertion
766*4882a593Smuzhiyun * point later.
767*4882a593Smuzhiyun */
768*4882a593Smuzhiyun if (compact) {
769*4882a593Smuzhiyun xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
770*4882a593Smuzhiyun &highstale, &lfloglow, &lfloghigh);
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun /*
773*4882a593Smuzhiyun * There are stale entries, so we'll need log-low and log-high
774*4882a593Smuzhiyun * impossibly bad values later.
775*4882a593Smuzhiyun */
776*4882a593Smuzhiyun else if (leafhdr.stale) {
777*4882a593Smuzhiyun lfloglow = leafhdr.count;
778*4882a593Smuzhiyun lfloghigh = -1;
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun /*
781*4882a593Smuzhiyun * If there was no data block space found, we need to allocate
782*4882a593Smuzhiyun * a new one.
783*4882a593Smuzhiyun */
784*4882a593Smuzhiyun if (use_block == -1) {
785*4882a593Smuzhiyun /*
786*4882a593Smuzhiyun * Add the new data block.
787*4882a593Smuzhiyun */
788*4882a593Smuzhiyun if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
789*4882a593Smuzhiyun &use_block))) {
790*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
791*4882a593Smuzhiyun return error;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun /*
794*4882a593Smuzhiyun * Initialize the block.
795*4882a593Smuzhiyun */
796*4882a593Smuzhiyun if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
797*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
798*4882a593Smuzhiyun return error;
799*4882a593Smuzhiyun }
800*4882a593Smuzhiyun /*
801*4882a593Smuzhiyun * If we're adding a new data block on the end we need to
802*4882a593Smuzhiyun * extend the bests table. Copy it up one entry.
803*4882a593Smuzhiyun */
804*4882a593Smuzhiyun if (use_block >= be32_to_cpu(ltp->bestcount)) {
805*4882a593Smuzhiyun bestsp--;
806*4882a593Smuzhiyun memmove(&bestsp[0], &bestsp[1],
807*4882a593Smuzhiyun be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
808*4882a593Smuzhiyun be32_add_cpu(<p->bestcount, 1);
809*4882a593Smuzhiyun xfs_dir3_leaf_log_tail(args, lbp);
810*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(args, lbp, 0,
811*4882a593Smuzhiyun be32_to_cpu(ltp->bestcount) - 1);
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun /*
814*4882a593Smuzhiyun * If we're filling in a previously empty block just log it.
815*4882a593Smuzhiyun */
816*4882a593Smuzhiyun else
817*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
818*4882a593Smuzhiyun hdr = dbp->b_addr;
819*4882a593Smuzhiyun bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
820*4882a593Smuzhiyun bestsp[use_block] = bf[0].length;
821*4882a593Smuzhiyun grown = 1;
822*4882a593Smuzhiyun } else {
823*4882a593Smuzhiyun /*
824*4882a593Smuzhiyun * Already had space in some data block.
825*4882a593Smuzhiyun * Just read that one in.
826*4882a593Smuzhiyun */
827*4882a593Smuzhiyun error = xfs_dir3_data_read(tp, dp,
828*4882a593Smuzhiyun xfs_dir2_db_to_da(args->geo, use_block),
829*4882a593Smuzhiyun 0, &dbp);
830*4882a593Smuzhiyun if (error) {
831*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
832*4882a593Smuzhiyun return error;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun hdr = dbp->b_addr;
835*4882a593Smuzhiyun bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
836*4882a593Smuzhiyun grown = 0;
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun /*
839*4882a593Smuzhiyun * Point to the biggest freespace in our data block.
840*4882a593Smuzhiyun */
841*4882a593Smuzhiyun dup = (xfs_dir2_data_unused_t *)
842*4882a593Smuzhiyun ((char *)hdr + be16_to_cpu(bf[0].offset));
843*4882a593Smuzhiyun needscan = needlog = 0;
844*4882a593Smuzhiyun /*
845*4882a593Smuzhiyun * Mark the initial part of our freespace in use for the new entry.
846*4882a593Smuzhiyun */
847*4882a593Smuzhiyun error = xfs_dir2_data_use_free(args, dbp, dup,
848*4882a593Smuzhiyun (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
849*4882a593Smuzhiyun length, &needlog, &needscan);
850*4882a593Smuzhiyun if (error) {
851*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
852*4882a593Smuzhiyun return error;
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun /*
855*4882a593Smuzhiyun * Initialize our new entry (at last).
856*4882a593Smuzhiyun */
857*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)dup;
858*4882a593Smuzhiyun dep->inumber = cpu_to_be64(args->inumber);
859*4882a593Smuzhiyun dep->namelen = args->namelen;
860*4882a593Smuzhiyun memcpy(dep->name, args->name, dep->namelen);
861*4882a593Smuzhiyun xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
862*4882a593Smuzhiyun tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
863*4882a593Smuzhiyun *tagp = cpu_to_be16((char *)dep - (char *)hdr);
864*4882a593Smuzhiyun /*
865*4882a593Smuzhiyun * Need to scan fix up the bestfree table.
866*4882a593Smuzhiyun */
867*4882a593Smuzhiyun if (needscan)
868*4882a593Smuzhiyun xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
869*4882a593Smuzhiyun /*
870*4882a593Smuzhiyun * Need to log the data block's header.
871*4882a593Smuzhiyun */
872*4882a593Smuzhiyun if (needlog)
873*4882a593Smuzhiyun xfs_dir2_data_log_header(args, dbp);
874*4882a593Smuzhiyun xfs_dir2_data_log_entry(args, dbp, dep);
875*4882a593Smuzhiyun /*
876*4882a593Smuzhiyun * If the bests table needs to be changed, do it.
877*4882a593Smuzhiyun * Log the change unless we've already done that.
878*4882a593Smuzhiyun */
879*4882a593Smuzhiyun if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
880*4882a593Smuzhiyun bestsp[use_block] = bf[0].length;
881*4882a593Smuzhiyun if (!grown)
882*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
886*4882a593Smuzhiyun highstale, &lfloglow, &lfloghigh);
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun /*
889*4882a593Smuzhiyun * Fill in the new leaf entry.
890*4882a593Smuzhiyun */
891*4882a593Smuzhiyun lep->hashval = cpu_to_be32(args->hashval);
892*4882a593Smuzhiyun lep->address = cpu_to_be32(
893*4882a593Smuzhiyun xfs_dir2_db_off_to_dataptr(args->geo, use_block,
894*4882a593Smuzhiyun be16_to_cpu(*tagp)));
895*4882a593Smuzhiyun /*
896*4882a593Smuzhiyun * Log the leaf fields and give up the buffers.
897*4882a593Smuzhiyun */
898*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
899*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, lbp);
900*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh);
901*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
902*4882a593Smuzhiyun xfs_dir3_data_check(dp, dbp);
903*4882a593Smuzhiyun return 0;
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun /*
907*4882a593Smuzhiyun * Compact out any stale entries in the leaf.
908*4882a593Smuzhiyun * Log the header and changed leaf entries, if any.
909*4882a593Smuzhiyun */
910*4882a593Smuzhiyun void
xfs_dir3_leaf_compact(xfs_da_args_t * args,struct xfs_dir3_icleaf_hdr * leafhdr,struct xfs_buf * bp)911*4882a593Smuzhiyun xfs_dir3_leaf_compact(
912*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
913*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *leafhdr,
914*4882a593Smuzhiyun struct xfs_buf *bp) /* leaf buffer */
915*4882a593Smuzhiyun {
916*4882a593Smuzhiyun int from; /* source leaf index */
917*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
918*4882a593Smuzhiyun int loglow; /* first leaf entry to log */
919*4882a593Smuzhiyun int to; /* target leaf index */
920*4882a593Smuzhiyun struct xfs_inode *dp = args->dp;
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun leaf = bp->b_addr;
923*4882a593Smuzhiyun if (!leafhdr->stale)
924*4882a593Smuzhiyun return;
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun /*
927*4882a593Smuzhiyun * Compress out the stale entries in place.
928*4882a593Smuzhiyun */
929*4882a593Smuzhiyun for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
930*4882a593Smuzhiyun if (leafhdr->ents[from].address ==
931*4882a593Smuzhiyun cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
932*4882a593Smuzhiyun continue;
933*4882a593Smuzhiyun /*
934*4882a593Smuzhiyun * Only actually copy the entries that are different.
935*4882a593Smuzhiyun */
936*4882a593Smuzhiyun if (from > to) {
937*4882a593Smuzhiyun if (loglow == -1)
938*4882a593Smuzhiyun loglow = to;
939*4882a593Smuzhiyun leafhdr->ents[to] = leafhdr->ents[from];
940*4882a593Smuzhiyun }
941*4882a593Smuzhiyun to++;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun /*
944*4882a593Smuzhiyun * Update and log the header, log the leaf entries.
945*4882a593Smuzhiyun */
946*4882a593Smuzhiyun ASSERT(leafhdr->stale == from - to);
947*4882a593Smuzhiyun leafhdr->count -= leafhdr->stale;
948*4882a593Smuzhiyun leafhdr->stale = 0;
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
951*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, bp);
952*4882a593Smuzhiyun if (loglow != -1)
953*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, leafhdr, bp, loglow, to - 1);
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun /*
957*4882a593Smuzhiyun * Compact the leaf entries, removing stale ones.
958*4882a593Smuzhiyun * Leave one stale entry behind - the one closest to our
959*4882a593Smuzhiyun * insertion index - and the caller will shift that one to our insertion
960*4882a593Smuzhiyun * point later.
961*4882a593Smuzhiyun * Return new insertion index, where the remaining stale entry is,
962*4882a593Smuzhiyun * and leaf logging indices.
963*4882a593Smuzhiyun */
964*4882a593Smuzhiyun void
xfs_dir3_leaf_compact_x1(struct xfs_dir3_icleaf_hdr * leafhdr,struct xfs_dir2_leaf_entry * ents,int * indexp,int * lowstalep,int * highstalep,int * lowlogp,int * highlogp)965*4882a593Smuzhiyun xfs_dir3_leaf_compact_x1(
966*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *leafhdr,
967*4882a593Smuzhiyun struct xfs_dir2_leaf_entry *ents,
968*4882a593Smuzhiyun int *indexp, /* insertion index */
969*4882a593Smuzhiyun int *lowstalep, /* out: stale entry before us */
970*4882a593Smuzhiyun int *highstalep, /* out: stale entry after us */
971*4882a593Smuzhiyun int *lowlogp, /* out: low log index */
972*4882a593Smuzhiyun int *highlogp) /* out: high log index */
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun int from; /* source copy index */
975*4882a593Smuzhiyun int highstale; /* stale entry at/after index */
976*4882a593Smuzhiyun int index; /* insertion index */
977*4882a593Smuzhiyun int keepstale; /* source index of kept stale */
978*4882a593Smuzhiyun int lowstale; /* stale entry before index */
979*4882a593Smuzhiyun int newindex=0; /* new insertion index */
980*4882a593Smuzhiyun int to; /* destination copy index */
981*4882a593Smuzhiyun
982*4882a593Smuzhiyun ASSERT(leafhdr->stale > 1);
983*4882a593Smuzhiyun index = *indexp;
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun /*
988*4882a593Smuzhiyun * Pick the better of lowstale and highstale.
989*4882a593Smuzhiyun */
990*4882a593Smuzhiyun if (lowstale >= 0 &&
991*4882a593Smuzhiyun (highstale == leafhdr->count ||
992*4882a593Smuzhiyun index - lowstale <= highstale - index))
993*4882a593Smuzhiyun keepstale = lowstale;
994*4882a593Smuzhiyun else
995*4882a593Smuzhiyun keepstale = highstale;
996*4882a593Smuzhiyun /*
997*4882a593Smuzhiyun * Copy the entries in place, removing all the stale entries
998*4882a593Smuzhiyun * except keepstale.
999*4882a593Smuzhiyun */
1000*4882a593Smuzhiyun for (from = to = 0; from < leafhdr->count; from++) {
1001*4882a593Smuzhiyun /*
1002*4882a593Smuzhiyun * Notice the new value of index.
1003*4882a593Smuzhiyun */
1004*4882a593Smuzhiyun if (index == from)
1005*4882a593Smuzhiyun newindex = to;
1006*4882a593Smuzhiyun if (from != keepstale &&
1007*4882a593Smuzhiyun ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1008*4882a593Smuzhiyun if (from == to)
1009*4882a593Smuzhiyun *lowlogp = to;
1010*4882a593Smuzhiyun continue;
1011*4882a593Smuzhiyun }
1012*4882a593Smuzhiyun /*
1013*4882a593Smuzhiyun * Record the new keepstale value for the insertion.
1014*4882a593Smuzhiyun */
1015*4882a593Smuzhiyun if (from == keepstale)
1016*4882a593Smuzhiyun lowstale = highstale = to;
1017*4882a593Smuzhiyun /*
1018*4882a593Smuzhiyun * Copy only the entries that have moved.
1019*4882a593Smuzhiyun */
1020*4882a593Smuzhiyun if (from > to)
1021*4882a593Smuzhiyun ents[to] = ents[from];
1022*4882a593Smuzhiyun to++;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun ASSERT(from > to);
1025*4882a593Smuzhiyun /*
1026*4882a593Smuzhiyun * If the insertion point was past the last entry,
1027*4882a593Smuzhiyun * set the new insertion point accordingly.
1028*4882a593Smuzhiyun */
1029*4882a593Smuzhiyun if (index == from)
1030*4882a593Smuzhiyun newindex = to;
1031*4882a593Smuzhiyun *indexp = newindex;
1032*4882a593Smuzhiyun /*
1033*4882a593Smuzhiyun * Adjust the leaf header values.
1034*4882a593Smuzhiyun */
1035*4882a593Smuzhiyun leafhdr->count -= from - to;
1036*4882a593Smuzhiyun leafhdr->stale = 1;
1037*4882a593Smuzhiyun /*
1038*4882a593Smuzhiyun * Remember the low/high stale value only in the "right"
1039*4882a593Smuzhiyun * direction.
1040*4882a593Smuzhiyun */
1041*4882a593Smuzhiyun if (lowstale >= newindex)
1042*4882a593Smuzhiyun lowstale = -1;
1043*4882a593Smuzhiyun else
1044*4882a593Smuzhiyun highstale = leafhdr->count;
1045*4882a593Smuzhiyun *highlogp = leafhdr->count - 1;
1046*4882a593Smuzhiyun *lowstalep = lowstale;
1047*4882a593Smuzhiyun *highstalep = highstale;
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun /*
1051*4882a593Smuzhiyun * Log the bests entries indicated from a leaf1 block.
1052*4882a593Smuzhiyun */
1053*4882a593Smuzhiyun static void
xfs_dir3_leaf_log_bests(struct xfs_da_args * args,struct xfs_buf * bp,int first,int last)1054*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(
1055*4882a593Smuzhiyun struct xfs_da_args *args,
1056*4882a593Smuzhiyun struct xfs_buf *bp, /* leaf buffer */
1057*4882a593Smuzhiyun int first, /* first entry to log */
1058*4882a593Smuzhiyun int last) /* last entry to log */
1059*4882a593Smuzhiyun {
1060*4882a593Smuzhiyun __be16 *firstb; /* pointer to first entry */
1061*4882a593Smuzhiyun __be16 *lastb; /* pointer to last entry */
1062*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf = bp->b_addr;
1063*4882a593Smuzhiyun xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1066*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1069*4882a593Smuzhiyun firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1070*4882a593Smuzhiyun lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1071*4882a593Smuzhiyun xfs_trans_log_buf(args->trans, bp,
1072*4882a593Smuzhiyun (uint)((char *)firstb - (char *)leaf),
1073*4882a593Smuzhiyun (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1074*4882a593Smuzhiyun }
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun /*
1077*4882a593Smuzhiyun * Log the leaf entries indicated from a leaf1 or leafn block.
1078*4882a593Smuzhiyun */
1079*4882a593Smuzhiyun void
xfs_dir3_leaf_log_ents(struct xfs_da_args * args,struct xfs_dir3_icleaf_hdr * hdr,struct xfs_buf * bp,int first,int last)1080*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(
1081*4882a593Smuzhiyun struct xfs_da_args *args,
1082*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *hdr,
1083*4882a593Smuzhiyun struct xfs_buf *bp,
1084*4882a593Smuzhiyun int first,
1085*4882a593Smuzhiyun int last)
1086*4882a593Smuzhiyun {
1087*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1088*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1089*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf = bp->b_addr;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1092*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1093*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1094*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun firstlep = &hdr->ents[first];
1097*4882a593Smuzhiyun lastlep = &hdr->ents[last];
1098*4882a593Smuzhiyun xfs_trans_log_buf(args->trans, bp,
1099*4882a593Smuzhiyun (uint)((char *)firstlep - (char *)leaf),
1100*4882a593Smuzhiyun (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun /*
1104*4882a593Smuzhiyun * Log the header of the leaf1 or leafn block.
1105*4882a593Smuzhiyun */
1106*4882a593Smuzhiyun void
xfs_dir3_leaf_log_header(struct xfs_da_args * args,struct xfs_buf * bp)1107*4882a593Smuzhiyun xfs_dir3_leaf_log_header(
1108*4882a593Smuzhiyun struct xfs_da_args *args,
1109*4882a593Smuzhiyun struct xfs_buf *bp)
1110*4882a593Smuzhiyun {
1111*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf = bp->b_addr;
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1114*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1115*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1116*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun xfs_trans_log_buf(args->trans, bp,
1119*4882a593Smuzhiyun (uint)((char *)&leaf->hdr - (char *)leaf),
1120*4882a593Smuzhiyun args->geo->leaf_hdr_size - 1);
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun /*
1124*4882a593Smuzhiyun * Log the tail of the leaf1 block.
1125*4882a593Smuzhiyun */
1126*4882a593Smuzhiyun STATIC void
xfs_dir3_leaf_log_tail(struct xfs_da_args * args,struct xfs_buf * bp)1127*4882a593Smuzhiyun xfs_dir3_leaf_log_tail(
1128*4882a593Smuzhiyun struct xfs_da_args *args,
1129*4882a593Smuzhiyun struct xfs_buf *bp)
1130*4882a593Smuzhiyun {
1131*4882a593Smuzhiyun struct xfs_dir2_leaf *leaf = bp->b_addr;
1132*4882a593Smuzhiyun xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1135*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1136*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1137*4882a593Smuzhiyun leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1138*4882a593Smuzhiyun
1139*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1140*4882a593Smuzhiyun xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1141*4882a593Smuzhiyun (uint)(args->geo->blksize - 1));
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun /*
1145*4882a593Smuzhiyun * Look up the entry referred to by args in the leaf format directory.
1146*4882a593Smuzhiyun * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1147*4882a593Smuzhiyun * is also used by the node-format code.
1148*4882a593Smuzhiyun */
1149*4882a593Smuzhiyun int
xfs_dir2_leaf_lookup(xfs_da_args_t * args)1150*4882a593Smuzhiyun xfs_dir2_leaf_lookup(
1151*4882a593Smuzhiyun xfs_da_args_t *args) /* operation arguments */
1152*4882a593Smuzhiyun {
1153*4882a593Smuzhiyun struct xfs_buf *dbp; /* data block buffer */
1154*4882a593Smuzhiyun xfs_dir2_data_entry_t *dep; /* data block entry */
1155*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
1156*4882a593Smuzhiyun int error; /* error return code */
1157*4882a593Smuzhiyun int index; /* found entry index */
1158*4882a593Smuzhiyun struct xfs_buf *lbp; /* leaf buffer */
1159*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1160*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
1161*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun trace_xfs_dir2_leaf_lookup(args);
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun /*
1166*4882a593Smuzhiyun * Look up name in the leaf block, returning both buffers and index.
1167*4882a593Smuzhiyun */
1168*4882a593Smuzhiyun error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1169*4882a593Smuzhiyun if (error)
1170*4882a593Smuzhiyun return error;
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun tp = args->trans;
1173*4882a593Smuzhiyun dp = args->dp;
1174*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun /*
1177*4882a593Smuzhiyun * Get to the leaf entry and contained data entry address.
1178*4882a593Smuzhiyun */
1179*4882a593Smuzhiyun lep = &leafhdr.ents[index];
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun /*
1182*4882a593Smuzhiyun * Point to the data entry.
1183*4882a593Smuzhiyun */
1184*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)
1185*4882a593Smuzhiyun ((char *)dbp->b_addr +
1186*4882a593Smuzhiyun xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1187*4882a593Smuzhiyun /*
1188*4882a593Smuzhiyun * Return the found inode number & CI name if appropriate
1189*4882a593Smuzhiyun */
1190*4882a593Smuzhiyun args->inumber = be64_to_cpu(dep->inumber);
1191*4882a593Smuzhiyun args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
1192*4882a593Smuzhiyun error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1193*4882a593Smuzhiyun xfs_trans_brelse(tp, dbp);
1194*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
1195*4882a593Smuzhiyun return error;
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun /*
1199*4882a593Smuzhiyun * Look up name/hash in the leaf block.
1200*4882a593Smuzhiyun * Fill in indexp with the found index, and dbpp with the data buffer.
1201*4882a593Smuzhiyun * If not found dbpp will be NULL, and ENOENT comes back.
1202*4882a593Smuzhiyun * lbpp will always be filled in with the leaf buffer unless there's an error.
1203*4882a593Smuzhiyun */
1204*4882a593Smuzhiyun static int /* error */
xfs_dir2_leaf_lookup_int(xfs_da_args_t * args,struct xfs_buf ** lbpp,int * indexp,struct xfs_buf ** dbpp,struct xfs_dir3_icleaf_hdr * leafhdr)1205*4882a593Smuzhiyun xfs_dir2_leaf_lookup_int(
1206*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
1207*4882a593Smuzhiyun struct xfs_buf **lbpp, /* out: leaf buffer */
1208*4882a593Smuzhiyun int *indexp, /* out: index in leaf block */
1209*4882a593Smuzhiyun struct xfs_buf **dbpp, /* out: data buffer */
1210*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *leafhdr)
1211*4882a593Smuzhiyun {
1212*4882a593Smuzhiyun xfs_dir2_db_t curdb = -1; /* current data block number */
1213*4882a593Smuzhiyun struct xfs_buf *dbp = NULL; /* data buffer */
1214*4882a593Smuzhiyun xfs_dir2_data_entry_t *dep; /* data entry */
1215*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
1216*4882a593Smuzhiyun int error; /* error return code */
1217*4882a593Smuzhiyun int index; /* index in leaf block */
1218*4882a593Smuzhiyun struct xfs_buf *lbp; /* leaf buffer */
1219*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1220*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
1221*4882a593Smuzhiyun xfs_mount_t *mp; /* filesystem mount point */
1222*4882a593Smuzhiyun xfs_dir2_db_t newdb; /* new data block number */
1223*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
1224*4882a593Smuzhiyun xfs_dir2_db_t cidb = -1; /* case match data block no. */
1225*4882a593Smuzhiyun enum xfs_dacmp cmp; /* name compare result */
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun dp = args->dp;
1228*4882a593Smuzhiyun tp = args->trans;
1229*4882a593Smuzhiyun mp = dp->i_mount;
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, &lbp);
1232*4882a593Smuzhiyun if (error)
1233*4882a593Smuzhiyun return error;
1234*4882a593Smuzhiyun
1235*4882a593Smuzhiyun *lbpp = lbp;
1236*4882a593Smuzhiyun leaf = lbp->b_addr;
1237*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
1238*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf);
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun /*
1241*4882a593Smuzhiyun * Look for the first leaf entry with our hash value.
1242*4882a593Smuzhiyun */
1243*4882a593Smuzhiyun index = xfs_dir2_leaf_search_hash(args, lbp);
1244*4882a593Smuzhiyun /*
1245*4882a593Smuzhiyun * Loop over all the entries with the right hash value
1246*4882a593Smuzhiyun * looking to match the name.
1247*4882a593Smuzhiyun */
1248*4882a593Smuzhiyun for (lep = &leafhdr->ents[index];
1249*4882a593Smuzhiyun index < leafhdr->count &&
1250*4882a593Smuzhiyun be32_to_cpu(lep->hashval) == args->hashval;
1251*4882a593Smuzhiyun lep++, index++) {
1252*4882a593Smuzhiyun /*
1253*4882a593Smuzhiyun * Skip over stale leaf entries.
1254*4882a593Smuzhiyun */
1255*4882a593Smuzhiyun if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1256*4882a593Smuzhiyun continue;
1257*4882a593Smuzhiyun /*
1258*4882a593Smuzhiyun * Get the new data block number.
1259*4882a593Smuzhiyun */
1260*4882a593Smuzhiyun newdb = xfs_dir2_dataptr_to_db(args->geo,
1261*4882a593Smuzhiyun be32_to_cpu(lep->address));
1262*4882a593Smuzhiyun /*
1263*4882a593Smuzhiyun * If it's not the same as the old data block number,
1264*4882a593Smuzhiyun * need to pitch the old one and read the new one.
1265*4882a593Smuzhiyun */
1266*4882a593Smuzhiyun if (newdb != curdb) {
1267*4882a593Smuzhiyun if (dbp)
1268*4882a593Smuzhiyun xfs_trans_brelse(tp, dbp);
1269*4882a593Smuzhiyun error = xfs_dir3_data_read(tp, dp,
1270*4882a593Smuzhiyun xfs_dir2_db_to_da(args->geo, newdb),
1271*4882a593Smuzhiyun 0, &dbp);
1272*4882a593Smuzhiyun if (error) {
1273*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
1274*4882a593Smuzhiyun return error;
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun curdb = newdb;
1277*4882a593Smuzhiyun }
1278*4882a593Smuzhiyun /*
1279*4882a593Smuzhiyun * Point to the data entry.
1280*4882a593Smuzhiyun */
1281*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1282*4882a593Smuzhiyun xfs_dir2_dataptr_to_off(args->geo,
1283*4882a593Smuzhiyun be32_to_cpu(lep->address)));
1284*4882a593Smuzhiyun /*
1285*4882a593Smuzhiyun * Compare name and if it's an exact match, return the index
1286*4882a593Smuzhiyun * and buffer. If it's the first case-insensitive match, store
1287*4882a593Smuzhiyun * the index and buffer and continue looking for an exact match.
1288*4882a593Smuzhiyun */
1289*4882a593Smuzhiyun cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
1290*4882a593Smuzhiyun if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1291*4882a593Smuzhiyun args->cmpresult = cmp;
1292*4882a593Smuzhiyun *indexp = index;
1293*4882a593Smuzhiyun /* case exact match: return the current buffer. */
1294*4882a593Smuzhiyun if (cmp == XFS_CMP_EXACT) {
1295*4882a593Smuzhiyun *dbpp = dbp;
1296*4882a593Smuzhiyun return 0;
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun cidb = curdb;
1299*4882a593Smuzhiyun }
1300*4882a593Smuzhiyun }
1301*4882a593Smuzhiyun ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1302*4882a593Smuzhiyun /*
1303*4882a593Smuzhiyun * Here, we can only be doing a lookup (not a rename or remove).
1304*4882a593Smuzhiyun * If a case-insensitive match was found earlier, re-read the
1305*4882a593Smuzhiyun * appropriate data block if required and return it.
1306*4882a593Smuzhiyun */
1307*4882a593Smuzhiyun if (args->cmpresult == XFS_CMP_CASE) {
1308*4882a593Smuzhiyun ASSERT(cidb != -1);
1309*4882a593Smuzhiyun if (cidb != curdb) {
1310*4882a593Smuzhiyun xfs_trans_brelse(tp, dbp);
1311*4882a593Smuzhiyun error = xfs_dir3_data_read(tp, dp,
1312*4882a593Smuzhiyun xfs_dir2_db_to_da(args->geo, cidb),
1313*4882a593Smuzhiyun 0, &dbp);
1314*4882a593Smuzhiyun if (error) {
1315*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
1316*4882a593Smuzhiyun return error;
1317*4882a593Smuzhiyun }
1318*4882a593Smuzhiyun }
1319*4882a593Smuzhiyun *dbpp = dbp;
1320*4882a593Smuzhiyun return 0;
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun /*
1323*4882a593Smuzhiyun * No match found, return -ENOENT.
1324*4882a593Smuzhiyun */
1325*4882a593Smuzhiyun ASSERT(cidb == -1);
1326*4882a593Smuzhiyun if (dbp)
1327*4882a593Smuzhiyun xfs_trans_brelse(tp, dbp);
1328*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
1329*4882a593Smuzhiyun return -ENOENT;
1330*4882a593Smuzhiyun }
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun /*
1333*4882a593Smuzhiyun * Remove an entry from a leaf format directory.
1334*4882a593Smuzhiyun */
1335*4882a593Smuzhiyun int /* error */
xfs_dir2_leaf_removename(xfs_da_args_t * args)1336*4882a593Smuzhiyun xfs_dir2_leaf_removename(
1337*4882a593Smuzhiyun xfs_da_args_t *args) /* operation arguments */
1338*4882a593Smuzhiyun {
1339*4882a593Smuzhiyun struct xfs_da_geometry *geo = args->geo;
1340*4882a593Smuzhiyun __be16 *bestsp; /* leaf block best freespace */
1341*4882a593Smuzhiyun xfs_dir2_data_hdr_t *hdr; /* data block header */
1342*4882a593Smuzhiyun xfs_dir2_db_t db; /* data block number */
1343*4882a593Smuzhiyun struct xfs_buf *dbp; /* data block buffer */
1344*4882a593Smuzhiyun xfs_dir2_data_entry_t *dep; /* data entry structure */
1345*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
1346*4882a593Smuzhiyun int error; /* error return code */
1347*4882a593Smuzhiyun xfs_dir2_db_t i; /* temporary data block # */
1348*4882a593Smuzhiyun int index; /* index into leaf entries */
1349*4882a593Smuzhiyun struct xfs_buf *lbp; /* leaf buffer */
1350*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
1351*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1352*4882a593Smuzhiyun xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1353*4882a593Smuzhiyun int needlog; /* need to log data header */
1354*4882a593Smuzhiyun int needscan; /* need to rescan data frees */
1355*4882a593Smuzhiyun xfs_dir2_data_off_t oldbest; /* old value of best free */
1356*4882a593Smuzhiyun struct xfs_dir2_data_free *bf; /* bestfree table */
1357*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
1358*4882a593Smuzhiyun
1359*4882a593Smuzhiyun trace_xfs_dir2_leaf_removename(args);
1360*4882a593Smuzhiyun
1361*4882a593Smuzhiyun /*
1362*4882a593Smuzhiyun * Lookup the leaf entry, get the leaf and data blocks read in.
1363*4882a593Smuzhiyun */
1364*4882a593Smuzhiyun error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1365*4882a593Smuzhiyun if (error)
1366*4882a593Smuzhiyun return error;
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun dp = args->dp;
1369*4882a593Smuzhiyun leaf = lbp->b_addr;
1370*4882a593Smuzhiyun hdr = dbp->b_addr;
1371*4882a593Smuzhiyun xfs_dir3_data_check(dp, dbp);
1372*4882a593Smuzhiyun bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun /*
1375*4882a593Smuzhiyun * Point to the leaf entry, use that to point to the data entry.
1376*4882a593Smuzhiyun */
1377*4882a593Smuzhiyun lep = &leafhdr.ents[index];
1378*4882a593Smuzhiyun db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1379*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1380*4882a593Smuzhiyun xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address)));
1381*4882a593Smuzhiyun needscan = needlog = 0;
1382*4882a593Smuzhiyun oldbest = be16_to_cpu(bf[0].length);
1383*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(geo, leaf);
1384*4882a593Smuzhiyun bestsp = xfs_dir2_leaf_bests_p(ltp);
1385*4882a593Smuzhiyun if (be16_to_cpu(bestsp[db]) != oldbest) {
1386*4882a593Smuzhiyun xfs_buf_mark_corrupt(lbp);
1387*4882a593Smuzhiyun return -EFSCORRUPTED;
1388*4882a593Smuzhiyun }
1389*4882a593Smuzhiyun /*
1390*4882a593Smuzhiyun * Mark the former data entry unused.
1391*4882a593Smuzhiyun */
1392*4882a593Smuzhiyun xfs_dir2_data_make_free(args, dbp,
1393*4882a593Smuzhiyun (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1394*4882a593Smuzhiyun xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
1395*4882a593Smuzhiyun &needscan);
1396*4882a593Smuzhiyun /*
1397*4882a593Smuzhiyun * We just mark the leaf entry stale by putting a null in it.
1398*4882a593Smuzhiyun */
1399*4882a593Smuzhiyun leafhdr.stale++;
1400*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1401*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, lbp);
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1404*4882a593Smuzhiyun xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, index, index);
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun /*
1407*4882a593Smuzhiyun * Scan the freespace in the data block again if necessary,
1408*4882a593Smuzhiyun * log the data block header if necessary.
1409*4882a593Smuzhiyun */
1410*4882a593Smuzhiyun if (needscan)
1411*4882a593Smuzhiyun xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1412*4882a593Smuzhiyun if (needlog)
1413*4882a593Smuzhiyun xfs_dir2_data_log_header(args, dbp);
1414*4882a593Smuzhiyun /*
1415*4882a593Smuzhiyun * If the longest freespace in the data block has changed,
1416*4882a593Smuzhiyun * put the new value in the bests table and log that.
1417*4882a593Smuzhiyun */
1418*4882a593Smuzhiyun if (be16_to_cpu(bf[0].length) != oldbest) {
1419*4882a593Smuzhiyun bestsp[db] = bf[0].length;
1420*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(args, lbp, db, db);
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun xfs_dir3_data_check(dp, dbp);
1423*4882a593Smuzhiyun /*
1424*4882a593Smuzhiyun * If the data block is now empty then get rid of the data block.
1425*4882a593Smuzhiyun */
1426*4882a593Smuzhiyun if (be16_to_cpu(bf[0].length) ==
1427*4882a593Smuzhiyun geo->blksize - geo->data_entry_offset) {
1428*4882a593Smuzhiyun ASSERT(db != geo->datablk);
1429*4882a593Smuzhiyun if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1430*4882a593Smuzhiyun /*
1431*4882a593Smuzhiyun * Nope, can't get rid of it because it caused
1432*4882a593Smuzhiyun * allocation of a bmap btree block to do so.
1433*4882a593Smuzhiyun * Just go on, returning success, leaving the
1434*4882a593Smuzhiyun * empty block in place.
1435*4882a593Smuzhiyun */
1436*4882a593Smuzhiyun if (error == -ENOSPC && args->total == 0)
1437*4882a593Smuzhiyun error = 0;
1438*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
1439*4882a593Smuzhiyun return error;
1440*4882a593Smuzhiyun }
1441*4882a593Smuzhiyun dbp = NULL;
1442*4882a593Smuzhiyun /*
1443*4882a593Smuzhiyun * If this is the last data block then compact the
1444*4882a593Smuzhiyun * bests table by getting rid of entries.
1445*4882a593Smuzhiyun */
1446*4882a593Smuzhiyun if (db == be32_to_cpu(ltp->bestcount) - 1) {
1447*4882a593Smuzhiyun /*
1448*4882a593Smuzhiyun * Look for the last active entry (i).
1449*4882a593Smuzhiyun */
1450*4882a593Smuzhiyun for (i = db - 1; i > 0; i--) {
1451*4882a593Smuzhiyun if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1452*4882a593Smuzhiyun break;
1453*4882a593Smuzhiyun }
1454*4882a593Smuzhiyun /*
1455*4882a593Smuzhiyun * Copy the table down so inactive entries at the
1456*4882a593Smuzhiyun * end are removed.
1457*4882a593Smuzhiyun */
1458*4882a593Smuzhiyun memmove(&bestsp[db - i], bestsp,
1459*4882a593Smuzhiyun (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1460*4882a593Smuzhiyun be32_add_cpu(<p->bestcount, -(db - i));
1461*4882a593Smuzhiyun xfs_dir3_leaf_log_tail(args, lbp);
1462*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(args, lbp, 0,
1463*4882a593Smuzhiyun be32_to_cpu(ltp->bestcount) - 1);
1464*4882a593Smuzhiyun } else
1465*4882a593Smuzhiyun bestsp[db] = cpu_to_be16(NULLDATAOFF);
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun /*
1468*4882a593Smuzhiyun * If the data block was not the first one, drop it.
1469*4882a593Smuzhiyun */
1470*4882a593Smuzhiyun else if (db != geo->datablk)
1471*4882a593Smuzhiyun dbp = NULL;
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
1474*4882a593Smuzhiyun /*
1475*4882a593Smuzhiyun * See if we can convert to block form.
1476*4882a593Smuzhiyun */
1477*4882a593Smuzhiyun return xfs_dir2_leaf_to_block(args, lbp, dbp);
1478*4882a593Smuzhiyun }
1479*4882a593Smuzhiyun
1480*4882a593Smuzhiyun /*
1481*4882a593Smuzhiyun * Replace the inode number in a leaf format directory entry.
1482*4882a593Smuzhiyun */
1483*4882a593Smuzhiyun int /* error */
xfs_dir2_leaf_replace(xfs_da_args_t * args)1484*4882a593Smuzhiyun xfs_dir2_leaf_replace(
1485*4882a593Smuzhiyun xfs_da_args_t *args) /* operation arguments */
1486*4882a593Smuzhiyun {
1487*4882a593Smuzhiyun struct xfs_buf *dbp; /* data block buffer */
1488*4882a593Smuzhiyun xfs_dir2_data_entry_t *dep; /* data block entry */
1489*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
1490*4882a593Smuzhiyun int error; /* error return code */
1491*4882a593Smuzhiyun int index; /* index of leaf entry */
1492*4882a593Smuzhiyun struct xfs_buf *lbp; /* leaf buffer */
1493*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1494*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
1495*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun trace_xfs_dir2_leaf_replace(args);
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun /*
1500*4882a593Smuzhiyun * Look up the entry.
1501*4882a593Smuzhiyun */
1502*4882a593Smuzhiyun error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1503*4882a593Smuzhiyun if (error)
1504*4882a593Smuzhiyun return error;
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun dp = args->dp;
1507*4882a593Smuzhiyun /*
1508*4882a593Smuzhiyun * Point to the leaf entry, get data address from it.
1509*4882a593Smuzhiyun */
1510*4882a593Smuzhiyun lep = &leafhdr.ents[index];
1511*4882a593Smuzhiyun /*
1512*4882a593Smuzhiyun * Point to the data entry.
1513*4882a593Smuzhiyun */
1514*4882a593Smuzhiyun dep = (xfs_dir2_data_entry_t *)
1515*4882a593Smuzhiyun ((char *)dbp->b_addr +
1516*4882a593Smuzhiyun xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1517*4882a593Smuzhiyun ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1518*4882a593Smuzhiyun /*
1519*4882a593Smuzhiyun * Put the new inode number in, log it.
1520*4882a593Smuzhiyun */
1521*4882a593Smuzhiyun dep->inumber = cpu_to_be64(args->inumber);
1522*4882a593Smuzhiyun xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1523*4882a593Smuzhiyun tp = args->trans;
1524*4882a593Smuzhiyun xfs_dir2_data_log_entry(args, dbp, dep);
1525*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
1526*4882a593Smuzhiyun xfs_trans_brelse(tp, lbp);
1527*4882a593Smuzhiyun return 0;
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun /*
1531*4882a593Smuzhiyun * Return index in the leaf block (lbp) which is either the first
1532*4882a593Smuzhiyun * one with this hash value, or if there are none, the insert point
1533*4882a593Smuzhiyun * for that hash value.
1534*4882a593Smuzhiyun */
1535*4882a593Smuzhiyun int /* index value */
xfs_dir2_leaf_search_hash(xfs_da_args_t * args,struct xfs_buf * lbp)1536*4882a593Smuzhiyun xfs_dir2_leaf_search_hash(
1537*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
1538*4882a593Smuzhiyun struct xfs_buf *lbp) /* leaf buffer */
1539*4882a593Smuzhiyun {
1540*4882a593Smuzhiyun xfs_dahash_t hash=0; /* hash from this entry */
1541*4882a593Smuzhiyun xfs_dahash_t hashwant; /* hash value looking for */
1542*4882a593Smuzhiyun int high; /* high leaf index */
1543*4882a593Smuzhiyun int low; /* low leaf index */
1544*4882a593Smuzhiyun xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1545*4882a593Smuzhiyun int mid=0; /* current leaf index */
1546*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
1547*4882a593Smuzhiyun
1548*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, lbp->b_addr);
1549*4882a593Smuzhiyun
1550*4882a593Smuzhiyun /*
1551*4882a593Smuzhiyun * Note, the table cannot be empty, so we have to go through the loop.
1552*4882a593Smuzhiyun * Binary search the leaf entries looking for our hash value.
1553*4882a593Smuzhiyun */
1554*4882a593Smuzhiyun for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1,
1555*4882a593Smuzhiyun hashwant = args->hashval;
1556*4882a593Smuzhiyun low <= high; ) {
1557*4882a593Smuzhiyun mid = (low + high) >> 1;
1558*4882a593Smuzhiyun if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1559*4882a593Smuzhiyun break;
1560*4882a593Smuzhiyun if (hash < hashwant)
1561*4882a593Smuzhiyun low = mid + 1;
1562*4882a593Smuzhiyun else
1563*4882a593Smuzhiyun high = mid - 1;
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun /*
1566*4882a593Smuzhiyun * Found one, back up through all the equal hash values.
1567*4882a593Smuzhiyun */
1568*4882a593Smuzhiyun if (hash == hashwant) {
1569*4882a593Smuzhiyun while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1570*4882a593Smuzhiyun mid--;
1571*4882a593Smuzhiyun }
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun /*
1574*4882a593Smuzhiyun * Need to point to an entry higher than ours.
1575*4882a593Smuzhiyun */
1576*4882a593Smuzhiyun else if (hash < hashwant)
1577*4882a593Smuzhiyun mid++;
1578*4882a593Smuzhiyun return mid;
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun
1581*4882a593Smuzhiyun /*
1582*4882a593Smuzhiyun * Trim off a trailing data block. We know it's empty since the leaf
1583*4882a593Smuzhiyun * freespace table says so.
1584*4882a593Smuzhiyun */
1585*4882a593Smuzhiyun int /* error */
xfs_dir2_leaf_trim_data(xfs_da_args_t * args,struct xfs_buf * lbp,xfs_dir2_db_t db)1586*4882a593Smuzhiyun xfs_dir2_leaf_trim_data(
1587*4882a593Smuzhiyun xfs_da_args_t *args, /* operation arguments */
1588*4882a593Smuzhiyun struct xfs_buf *lbp, /* leaf buffer */
1589*4882a593Smuzhiyun xfs_dir2_db_t db) /* data block number */
1590*4882a593Smuzhiyun {
1591*4882a593Smuzhiyun struct xfs_da_geometry *geo = args->geo;
1592*4882a593Smuzhiyun __be16 *bestsp; /* leaf bests table */
1593*4882a593Smuzhiyun struct xfs_buf *dbp; /* data block buffer */
1594*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
1595*4882a593Smuzhiyun int error; /* error return value */
1596*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
1597*4882a593Smuzhiyun xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1598*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun dp = args->dp;
1601*4882a593Smuzhiyun tp = args->trans;
1602*4882a593Smuzhiyun /*
1603*4882a593Smuzhiyun * Read the offending data block. We need its buffer.
1604*4882a593Smuzhiyun */
1605*4882a593Smuzhiyun error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(geo, db), 0, &dbp);
1606*4882a593Smuzhiyun if (error)
1607*4882a593Smuzhiyun return error;
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun leaf = lbp->b_addr;
1610*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(geo, leaf);
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun #ifdef DEBUG
1613*4882a593Smuzhiyun {
1614*4882a593Smuzhiyun struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1615*4882a593Smuzhiyun struct xfs_dir2_data_free *bf =
1616*4882a593Smuzhiyun xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1617*4882a593Smuzhiyun
1618*4882a593Smuzhiyun ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1619*4882a593Smuzhiyun hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1620*4882a593Smuzhiyun ASSERT(be16_to_cpu(bf[0].length) ==
1621*4882a593Smuzhiyun geo->blksize - geo->data_entry_offset);
1622*4882a593Smuzhiyun ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1623*4882a593Smuzhiyun }
1624*4882a593Smuzhiyun #endif
1625*4882a593Smuzhiyun
1626*4882a593Smuzhiyun /*
1627*4882a593Smuzhiyun * Get rid of the data block.
1628*4882a593Smuzhiyun */
1629*4882a593Smuzhiyun if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1630*4882a593Smuzhiyun ASSERT(error != -ENOSPC);
1631*4882a593Smuzhiyun xfs_trans_brelse(tp, dbp);
1632*4882a593Smuzhiyun return error;
1633*4882a593Smuzhiyun }
1634*4882a593Smuzhiyun /*
1635*4882a593Smuzhiyun * Eliminate the last bests entry from the table.
1636*4882a593Smuzhiyun */
1637*4882a593Smuzhiyun bestsp = xfs_dir2_leaf_bests_p(ltp);
1638*4882a593Smuzhiyun be32_add_cpu(<p->bestcount, -1);
1639*4882a593Smuzhiyun memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1640*4882a593Smuzhiyun xfs_dir3_leaf_log_tail(args, lbp);
1641*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1642*4882a593Smuzhiyun return 0;
1643*4882a593Smuzhiyun }
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun static inline size_t
xfs_dir3_leaf_size(struct xfs_dir3_icleaf_hdr * hdr,int counts)1646*4882a593Smuzhiyun xfs_dir3_leaf_size(
1647*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr *hdr,
1648*4882a593Smuzhiyun int counts)
1649*4882a593Smuzhiyun {
1650*4882a593Smuzhiyun int entries;
1651*4882a593Smuzhiyun int hdrsize;
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun entries = hdr->count - hdr->stale;
1654*4882a593Smuzhiyun if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1655*4882a593Smuzhiyun hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1656*4882a593Smuzhiyun hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1657*4882a593Smuzhiyun else
1658*4882a593Smuzhiyun hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1659*4882a593Smuzhiyun
1660*4882a593Smuzhiyun return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1661*4882a593Smuzhiyun + counts * sizeof(xfs_dir2_data_off_t)
1662*4882a593Smuzhiyun + sizeof(xfs_dir2_leaf_tail_t);
1663*4882a593Smuzhiyun }
1664*4882a593Smuzhiyun
1665*4882a593Smuzhiyun /*
1666*4882a593Smuzhiyun * Convert node form directory to leaf form directory.
1667*4882a593Smuzhiyun * The root of the node form dir needs to already be a LEAFN block.
1668*4882a593Smuzhiyun * Just return if we can't do anything.
1669*4882a593Smuzhiyun */
1670*4882a593Smuzhiyun int /* error */
xfs_dir2_node_to_leaf(xfs_da_state_t * state)1671*4882a593Smuzhiyun xfs_dir2_node_to_leaf(
1672*4882a593Smuzhiyun xfs_da_state_t *state) /* directory operation state */
1673*4882a593Smuzhiyun {
1674*4882a593Smuzhiyun xfs_da_args_t *args; /* operation arguments */
1675*4882a593Smuzhiyun xfs_inode_t *dp; /* incore directory inode */
1676*4882a593Smuzhiyun int error; /* error return code */
1677*4882a593Smuzhiyun struct xfs_buf *fbp; /* buffer for freespace block */
1678*4882a593Smuzhiyun xfs_fileoff_t fo; /* freespace file offset */
1679*4882a593Smuzhiyun struct xfs_buf *lbp; /* buffer for leaf block */
1680*4882a593Smuzhiyun xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1681*4882a593Smuzhiyun xfs_dir2_leaf_t *leaf; /* leaf structure */
1682*4882a593Smuzhiyun xfs_mount_t *mp; /* filesystem mount point */
1683*4882a593Smuzhiyun int rval; /* successful free trim? */
1684*4882a593Smuzhiyun xfs_trans_t *tp; /* transaction pointer */
1685*4882a593Smuzhiyun struct xfs_dir3_icleaf_hdr leafhdr;
1686*4882a593Smuzhiyun struct xfs_dir3_icfree_hdr freehdr;
1687*4882a593Smuzhiyun
1688*4882a593Smuzhiyun /*
1689*4882a593Smuzhiyun * There's more than a leaf level in the btree, so there must
1690*4882a593Smuzhiyun * be multiple leafn blocks. Give up.
1691*4882a593Smuzhiyun */
1692*4882a593Smuzhiyun if (state->path.active > 1)
1693*4882a593Smuzhiyun return 0;
1694*4882a593Smuzhiyun args = state->args;
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun trace_xfs_dir2_node_to_leaf(args);
1697*4882a593Smuzhiyun
1698*4882a593Smuzhiyun mp = state->mp;
1699*4882a593Smuzhiyun dp = args->dp;
1700*4882a593Smuzhiyun tp = args->trans;
1701*4882a593Smuzhiyun /*
1702*4882a593Smuzhiyun * Get the last offset in the file.
1703*4882a593Smuzhiyun */
1704*4882a593Smuzhiyun if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1705*4882a593Smuzhiyun return error;
1706*4882a593Smuzhiyun }
1707*4882a593Smuzhiyun fo -= args->geo->fsbcount;
1708*4882a593Smuzhiyun /*
1709*4882a593Smuzhiyun * If there are freespace blocks other than the first one,
1710*4882a593Smuzhiyun * take this opportunity to remove trailing empty freespace blocks
1711*4882a593Smuzhiyun * that may have been left behind during no-space-reservation
1712*4882a593Smuzhiyun * operations.
1713*4882a593Smuzhiyun */
1714*4882a593Smuzhiyun while (fo > args->geo->freeblk) {
1715*4882a593Smuzhiyun if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1716*4882a593Smuzhiyun return error;
1717*4882a593Smuzhiyun }
1718*4882a593Smuzhiyun if (rval)
1719*4882a593Smuzhiyun fo -= args->geo->fsbcount;
1720*4882a593Smuzhiyun else
1721*4882a593Smuzhiyun return 0;
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun /*
1724*4882a593Smuzhiyun * Now find the block just before the freespace block.
1725*4882a593Smuzhiyun */
1726*4882a593Smuzhiyun if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1727*4882a593Smuzhiyun return error;
1728*4882a593Smuzhiyun }
1729*4882a593Smuzhiyun /*
1730*4882a593Smuzhiyun * If it's not the single leaf block, give up.
1731*4882a593Smuzhiyun */
1732*4882a593Smuzhiyun if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1733*4882a593Smuzhiyun return 0;
1734*4882a593Smuzhiyun lbp = state->path.blk[0].bp;
1735*4882a593Smuzhiyun leaf = lbp->b_addr;
1736*4882a593Smuzhiyun xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
1737*4882a593Smuzhiyun
1738*4882a593Smuzhiyun ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1739*4882a593Smuzhiyun leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun /*
1742*4882a593Smuzhiyun * Read the freespace block.
1743*4882a593Smuzhiyun */
1744*4882a593Smuzhiyun error = xfs_dir2_free_read(tp, dp, args->geo->freeblk, &fbp);
1745*4882a593Smuzhiyun if (error)
1746*4882a593Smuzhiyun return error;
1747*4882a593Smuzhiyun xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr);
1748*4882a593Smuzhiyun
1749*4882a593Smuzhiyun ASSERT(!freehdr.firstdb);
1750*4882a593Smuzhiyun
1751*4882a593Smuzhiyun /*
1752*4882a593Smuzhiyun * Now see if the leafn and free data will fit in a leaf1.
1753*4882a593Smuzhiyun * If not, release the buffer and give up.
1754*4882a593Smuzhiyun */
1755*4882a593Smuzhiyun if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1756*4882a593Smuzhiyun xfs_trans_brelse(tp, fbp);
1757*4882a593Smuzhiyun return 0;
1758*4882a593Smuzhiyun }
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun /*
1761*4882a593Smuzhiyun * If the leaf has any stale entries in it, compress them out.
1762*4882a593Smuzhiyun */
1763*4882a593Smuzhiyun if (leafhdr.stale)
1764*4882a593Smuzhiyun xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1767*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1768*4882a593Smuzhiyun leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1769*4882a593Smuzhiyun ? XFS_DIR2_LEAF1_MAGIC
1770*4882a593Smuzhiyun : XFS_DIR3_LEAF1_MAGIC;
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun /*
1773*4882a593Smuzhiyun * Set up the leaf tail from the freespace block.
1774*4882a593Smuzhiyun */
1775*4882a593Smuzhiyun ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1776*4882a593Smuzhiyun ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1777*4882a593Smuzhiyun
1778*4882a593Smuzhiyun /*
1779*4882a593Smuzhiyun * Set up the leaf bests table.
1780*4882a593Smuzhiyun */
1781*4882a593Smuzhiyun memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests,
1782*4882a593Smuzhiyun freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1783*4882a593Smuzhiyun
1784*4882a593Smuzhiyun xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
1785*4882a593Smuzhiyun xfs_dir3_leaf_log_header(args, lbp);
1786*4882a593Smuzhiyun xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1787*4882a593Smuzhiyun xfs_dir3_leaf_log_tail(args, lbp);
1788*4882a593Smuzhiyun xfs_dir3_leaf_check(dp, lbp);
1789*4882a593Smuzhiyun
1790*4882a593Smuzhiyun /*
1791*4882a593Smuzhiyun * Get rid of the freespace block.
1792*4882a593Smuzhiyun */
1793*4882a593Smuzhiyun error = xfs_dir2_shrink_inode(args,
1794*4882a593Smuzhiyun xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1795*4882a593Smuzhiyun fbp);
1796*4882a593Smuzhiyun if (error) {
1797*4882a593Smuzhiyun /*
1798*4882a593Smuzhiyun * This can't fail here because it can only happen when
1799*4882a593Smuzhiyun * punching out the middle of an extent, and this is an
1800*4882a593Smuzhiyun * isolated block.
1801*4882a593Smuzhiyun */
1802*4882a593Smuzhiyun ASSERT(error != -ENOSPC);
1803*4882a593Smuzhiyun return error;
1804*4882a593Smuzhiyun }
1805*4882a593Smuzhiyun fbp = NULL;
1806*4882a593Smuzhiyun /*
1807*4882a593Smuzhiyun * Now see if we can convert the single-leaf directory
1808*4882a593Smuzhiyun * down to a block form directory.
1809*4882a593Smuzhiyun * This routine always kills the dabuf for the leaf, so
1810*4882a593Smuzhiyun * eliminate it from the path.
1811*4882a593Smuzhiyun */
1812*4882a593Smuzhiyun error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1813*4882a593Smuzhiyun state->path.blk[0].bp = NULL;
1814*4882a593Smuzhiyun return error;
1815*4882a593Smuzhiyun }
1816