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