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