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