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