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