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