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