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