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