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