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