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