2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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
7 * published by the Free Software Foundation.
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.
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
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dir2_sf.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
34 #include "xfs_dir2_data.h"
35 #include "xfs_dir2_leaf.h"
36 #include "xfs_dir2_block.h"
37 #include "xfs_dir2_node.h"
38 #include "xfs_error.h"
39 #include "xfs_trace.h"
42 * Function declarations.
44 static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
45 static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
47 static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
49 #define xfs_dir2_leafn_check(dp, bp)
51 static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
52 int start_s, xfs_dabuf_t *bp_d, int start_d,
54 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
55 xfs_da_state_blk_t *blk1,
56 xfs_da_state_blk_t *blk2);
57 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
58 int index, xfs_da_state_blk_t *dblk,
60 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
61 xfs_da_state_blk_t *fblk);
64 * Log entries from a freespace block.
67 xfs_dir2_free_log_bests(
68 xfs_trans_t *tp, /* transaction pointer */
69 xfs_dabuf_t *bp, /* freespace buffer */
70 int first, /* first entry to log */
71 int last) /* last entry to log */
73 xfs_dir2_free_t *free; /* freespace structure */
76 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
77 xfs_da_log_buf(tp, bp,
78 (uint)((char *)&free->bests[first] - (char *)free),
79 (uint)((char *)&free->bests[last] - (char *)free +
80 sizeof(free->bests[0]) - 1));
84 * Log header from a freespace block.
87 xfs_dir2_free_log_header(
88 xfs_trans_t *tp, /* transaction pointer */
89 xfs_dabuf_t *bp) /* freespace buffer */
91 xfs_dir2_free_t *free; /* freespace structure */
94 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
95 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
96 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
100 * Convert a leaf-format directory to a node-format directory.
101 * We need to change the magic number of the leaf block, and copy
102 * the freespace table out of the leaf block into its own block.
105 xfs_dir2_leaf_to_node(
106 xfs_da_args_t *args, /* operation arguments */
107 xfs_dabuf_t *lbp) /* leaf buffer */
109 xfs_inode_t *dp; /* incore directory inode */
110 int error; /* error return value */
111 xfs_dabuf_t *fbp; /* freespace buffer */
112 xfs_dir2_db_t fdb; /* freespace block number */
113 xfs_dir2_free_t *free; /* freespace structure */
114 __be16 *from; /* pointer to freespace entry */
115 int i; /* leaf freespace index */
116 xfs_dir2_leaf_t *leaf; /* leaf structure */
117 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
118 xfs_mount_t *mp; /* filesystem mount point */
119 int n; /* count of live freespc ents */
120 xfs_dir2_data_off_t off; /* freespace entry value */
121 __be16 *to; /* pointer to freespace entry */
122 xfs_trans_t *tp; /* transaction pointer */
124 trace_xfs_dir2_leaf_to_node(args);
130 * Add a freespace block to the directory.
132 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
135 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
137 * Get the buffer for the new freespace block.
139 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
146 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
148 * Initialize the freespace block header.
150 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
151 free->hdr.firstdb = 0;
152 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
153 free->hdr.nvalid = ltp->bestcount;
155 * Copy freespace entries from the leaf block to the new block.
156 * Count active entries.
158 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
159 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
160 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
162 *to = cpu_to_be16(off);
164 free->hdr.nused = cpu_to_be32(n);
165 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
169 xfs_dir2_leaf_log_header(tp, lbp);
170 xfs_dir2_free_log_header(tp, fbp);
171 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
172 xfs_da_buf_done(fbp);
173 xfs_dir2_leafn_check(dp, lbp);
178 * Add a leaf entry to a leaf block in a node-form directory.
179 * The other work necessary is done from the caller.
181 static int /* error */
183 xfs_dabuf_t *bp, /* leaf buffer */
184 xfs_da_args_t *args, /* operation arguments */
185 int index) /* insertion pt for new entry */
187 int compact; /* compacting stale leaves */
188 xfs_inode_t *dp; /* incore directory inode */
189 int highstale; /* next stale entry */
190 xfs_dir2_leaf_t *leaf; /* leaf structure */
191 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
192 int lfloghigh; /* high leaf entry logging */
193 int lfloglow; /* low leaf entry logging */
194 int lowstale; /* previous stale entry */
195 xfs_mount_t *mp; /* filesystem mount point */
196 xfs_trans_t *tp; /* transaction pointer */
198 trace_xfs_dir2_leafn_add(args, index);
206 * Quick check just to make sure we are not going to index
207 * into other peoples memory
210 return XFS_ERROR(EFSCORRUPTED);
213 * If there are already the maximum number of leaf entries in
214 * the block, if there are no stale entries it won't fit.
215 * Caller will do a split. If there are stale entries we'll do
219 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
220 if (!leaf->hdr.stale)
221 return XFS_ERROR(ENOSPC);
222 compact = be16_to_cpu(leaf->hdr.stale) > 1;
225 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
226 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
227 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
229 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
233 * Compact out all but one stale leaf entry. Leaves behind
234 * the entry closest to index.
237 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
238 &lfloglow, &lfloghigh);
241 * Set impossible logging indices for this case.
243 else if (leaf->hdr.stale) {
244 lfloglow = be16_to_cpu(leaf->hdr.count);
249 * Insert the new entry, log everything.
251 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
252 highstale, &lfloglow, &lfloghigh);
254 lep->hashval = cpu_to_be32(args->hashval);
255 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
256 args->blkno, args->index));
257 xfs_dir2_leaf_log_header(tp, bp);
258 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
259 xfs_dir2_leafn_check(dp, bp);
265 * Check internal consistency of a leafn block.
268 xfs_dir2_leafn_check(
269 xfs_inode_t *dp, /* incore directory inode */
270 xfs_dabuf_t *bp) /* leaf buffer */
272 int i; /* leaf index */
273 xfs_dir2_leaf_t *leaf; /* leaf structure */
274 xfs_mount_t *mp; /* filesystem mount point */
275 int stale; /* count of stale leaves */
279 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
280 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
281 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
282 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
283 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
284 be32_to_cpu(leaf->ents[i + 1].hashval));
286 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
289 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
294 * Return the last hash value in the leaf.
295 * Stale entries are ok.
297 xfs_dahash_t /* hash value */
298 xfs_dir2_leafn_lasthash(
299 xfs_dabuf_t *bp, /* leaf buffer */
300 int *count) /* count of entries in leaf */
302 xfs_dir2_leaf_t *leaf; /* leaf structure */
305 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
307 *count = be16_to_cpu(leaf->hdr.count);
308 if (!leaf->hdr.count)
310 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
314 * Look up a leaf entry for space to add a name in a node-format leaf block.
315 * The extrablk in state is a freespace block.
318 xfs_dir2_leafn_lookup_for_addname(
319 xfs_dabuf_t *bp, /* leaf buffer */
320 xfs_da_args_t *args, /* operation arguments */
321 int *indexp, /* out: leaf entry index */
322 xfs_da_state_t *state) /* state to fill in */
324 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
325 xfs_dir2_db_t curdb = -1; /* current data block number */
326 xfs_dir2_db_t curfdb = -1; /* current free block number */
327 xfs_inode_t *dp; /* incore directory inode */
328 int error; /* error return value */
329 int fi; /* free entry index */
330 xfs_dir2_free_t *free = NULL; /* free block structure */
331 int index; /* leaf entry index */
332 xfs_dir2_leaf_t *leaf; /* leaf structure */
333 int length; /* length of new data entry */
334 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
335 xfs_mount_t *mp; /* filesystem mount point */
336 xfs_dir2_db_t newdb; /* new data block number */
337 xfs_dir2_db_t newfdb; /* new free block number */
338 xfs_trans_t *tp; /* transaction pointer */
344 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
346 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
348 xfs_dir2_leafn_check(dp, bp);
350 * Look up the hash value in the leaf entries.
352 index = xfs_dir2_leaf_search_hash(args, bp);
354 * Do we have a buffer coming in?
356 if (state->extravalid) {
357 /* If so, it's a free block buffer, get the block number. */
358 curbp = state->extrablk.bp;
359 curfdb = state->extrablk.blkno;
361 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
363 length = xfs_dir2_data_entsize(args->namelen);
365 * Loop over leaf entries with the right hash value.
367 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
368 be32_to_cpu(lep->hashval) == args->hashval;
371 * Skip stale leaf entries.
373 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
376 * Pull the data block number from the entry.
378 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
380 * For addname, we're looking for a place to put the new entry.
381 * We want to use a data block with an entry of equal
382 * hash value to ours if there is one with room.
384 * If this block isn't the data block we already have
385 * in hand, take a look at it.
387 if (newdb != curdb) {
390 * Convert the data block to the free block
391 * holding its freespace information.
393 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
395 * If it's not the one we have in hand, read it in.
397 if (newfdb != curfdb) {
399 * If we had one before, drop it.
402 xfs_da_brelse(tp, curbp);
404 * Read the free block.
406 error = xfs_da_read_buf(tp, dp,
407 xfs_dir2_db_to_da(mp, newfdb),
408 -1, &curbp, XFS_DATA_FORK);
412 ASSERT(be32_to_cpu(free->hdr.magic) ==
413 XFS_DIR2_FREE_MAGIC);
414 ASSERT((be32_to_cpu(free->hdr.firstdb) %
415 XFS_DIR2_MAX_FREE_BESTS(mp)) == 0);
416 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
417 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
418 be32_to_cpu(free->hdr.nvalid));
421 * Get the index for our entry.
423 fi = xfs_dir2_db_to_fdindex(mp, curdb);
425 * If it has room, return it.
427 if (unlikely(free->bests[fi] ==
428 cpu_to_be16(NULLDATAOFF))) {
429 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
430 XFS_ERRLEVEL_LOW, mp);
431 if (curfdb != newfdb)
432 xfs_da_brelse(tp, curbp);
433 return XFS_ERROR(EFSCORRUPTED);
436 if (be16_to_cpu(free->bests[fi]) >= length)
440 /* Didn't find any space */
443 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
445 /* Giving back a free block. */
446 state->extravalid = 1;
447 state->extrablk.bp = curbp;
448 state->extrablk.index = fi;
449 state->extrablk.blkno = curfdb;
450 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
452 state->extravalid = 0;
455 * Return the index, that will be the insertion point.
458 return XFS_ERROR(ENOENT);
462 * Look up a leaf entry in a node-format leaf block.
463 * The extrablk in state a data block.
466 xfs_dir2_leafn_lookup_for_entry(
467 xfs_dabuf_t *bp, /* leaf buffer */
468 xfs_da_args_t *args, /* operation arguments */
469 int *indexp, /* out: leaf entry index */
470 xfs_da_state_t *state) /* state to fill in */
472 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
473 xfs_dir2_db_t curdb = -1; /* current data block number */
474 xfs_dir2_data_entry_t *dep; /* data block entry */
475 xfs_inode_t *dp; /* incore directory inode */
476 int error; /* error return value */
477 int index; /* leaf entry index */
478 xfs_dir2_leaf_t *leaf; /* leaf structure */
479 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
480 xfs_mount_t *mp; /* filesystem mount point */
481 xfs_dir2_db_t newdb; /* new data block number */
482 xfs_trans_t *tp; /* transaction pointer */
483 enum xfs_dacmp cmp; /* comparison result */
489 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
491 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
493 xfs_dir2_leafn_check(dp, bp);
495 * Look up the hash value in the leaf entries.
497 index = xfs_dir2_leaf_search_hash(args, bp);
499 * Do we have a buffer coming in?
501 if (state->extravalid) {
502 curbp = state->extrablk.bp;
503 curdb = state->extrablk.blkno;
506 * Loop over leaf entries with the right hash value.
508 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
509 be32_to_cpu(lep->hashval) == args->hashval;
512 * Skip stale leaf entries.
514 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
517 * Pull the data block number from the entry.
519 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
521 * Not adding a new entry, so we really want to find
522 * the name given to us.
524 * If it's a different data block, go get it.
526 if (newdb != curdb) {
528 * If we had a block before that we aren't saving
529 * for a CI name, drop it
531 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
532 curdb != state->extrablk.blkno))
533 xfs_da_brelse(tp, curbp);
535 * If needing the block that is saved with a CI match,
536 * use it otherwise read in the new data block.
538 if (args->cmpresult != XFS_CMP_DIFFERENT &&
539 newdb == state->extrablk.blkno) {
540 ASSERT(state->extravalid);
541 curbp = state->extrablk.bp;
543 error = xfs_da_read_buf(tp, dp,
544 xfs_dir2_db_to_da(mp, newdb),
545 -1, &curbp, XFS_DATA_FORK);
549 xfs_dir2_data_check(dp, curbp);
553 * Point to the data entry.
555 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
556 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
558 * Compare the entry and if it's an exact match, return
559 * EEXIST immediately. If it's the first case-insensitive
560 * match, store the block & inode number and continue looking.
562 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
563 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
564 /* If there is a CI match block, drop it */
565 if (args->cmpresult != XFS_CMP_DIFFERENT &&
566 curdb != state->extrablk.blkno)
567 xfs_da_brelse(tp, state->extrablk.bp);
568 args->cmpresult = cmp;
569 args->inumber = be64_to_cpu(dep->inumber);
571 state->extravalid = 1;
572 state->extrablk.bp = curbp;
573 state->extrablk.blkno = curdb;
574 state->extrablk.index = (int)((char *)dep -
575 (char *)curbp->data);
576 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
577 if (cmp == XFS_CMP_EXACT)
578 return XFS_ERROR(EEXIST);
581 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
582 (args->op_flags & XFS_DA_OP_OKNOENT));
584 if (args->cmpresult == XFS_CMP_DIFFERENT) {
585 /* Giving back last used data block. */
586 state->extravalid = 1;
587 state->extrablk.bp = curbp;
588 state->extrablk.index = -1;
589 state->extrablk.blkno = curdb;
590 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
592 /* If the curbp is not the CI match block, drop it */
593 if (state->extrablk.bp != curbp)
594 xfs_da_brelse(tp, curbp);
597 state->extravalid = 0;
600 return XFS_ERROR(ENOENT);
604 * Look up a leaf entry in a node-format leaf block.
605 * If this is an addname then the extrablk in state is a freespace block,
606 * otherwise it's a data block.
609 xfs_dir2_leafn_lookup_int(
610 xfs_dabuf_t *bp, /* leaf buffer */
611 xfs_da_args_t *args, /* operation arguments */
612 int *indexp, /* out: leaf entry index */
613 xfs_da_state_t *state) /* state to fill in */
615 if (args->op_flags & XFS_DA_OP_ADDNAME)
616 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
618 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
622 * Move count leaf entries from source to destination leaf.
623 * Log entries and headers. Stale entries are preserved.
626 xfs_dir2_leafn_moveents(
627 xfs_da_args_t *args, /* operation arguments */
628 xfs_dabuf_t *bp_s, /* source leaf buffer */
629 int start_s, /* source leaf index */
630 xfs_dabuf_t *bp_d, /* destination leaf buffer */
631 int start_d, /* destination leaf index */
632 int count) /* count of leaves to copy */
634 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
635 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
636 int stale; /* count stale leaves copied */
637 xfs_trans_t *tp; /* transaction pointer */
639 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
642 * Silently return if nothing to do.
651 * If the destination index is not the end of the current
652 * destination leaf entries, open up a hole in the destination
653 * to hold the new entries.
655 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
656 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
657 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
658 sizeof(xfs_dir2_leaf_entry_t));
659 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
660 count + be16_to_cpu(leaf_d->hdr.count) - 1);
663 * If the source has stale leaves, count the ones in the copy range
664 * so we can update the header correctly.
666 if (leaf_s->hdr.stale) {
667 int i; /* temp leaf index */
669 for (i = start_s, stale = 0; i < start_s + count; i++) {
670 if (leaf_s->ents[i].address ==
671 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
677 * Copy the leaf entries from source to destination.
679 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
680 count * sizeof(xfs_dir2_leaf_entry_t));
681 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
683 * If there are source entries after the ones we copied,
684 * delete the ones we copied by sliding the next ones down.
686 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
687 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
688 count * sizeof(xfs_dir2_leaf_entry_t));
689 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
692 * Update the headers and log them.
694 be16_add_cpu(&leaf_s->hdr.count, -(count));
695 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
696 be16_add_cpu(&leaf_d->hdr.count, count);
697 be16_add_cpu(&leaf_d->hdr.stale, stale);
698 xfs_dir2_leaf_log_header(tp, bp_s);
699 xfs_dir2_leaf_log_header(tp, bp_d);
700 xfs_dir2_leafn_check(args->dp, bp_s);
701 xfs_dir2_leafn_check(args->dp, bp_d);
705 * Determine the sort order of two leaf blocks.
706 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
709 xfs_dir2_leafn_order(
710 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
711 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
713 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
714 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
716 leaf1 = leaf1_bp->data;
717 leaf2 = leaf2_bp->data;
718 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
719 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
720 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
721 be16_to_cpu(leaf2->hdr.count) > 0 &&
722 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
723 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
724 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
730 * Rebalance leaf entries between two leaf blocks.
731 * This is actually only called when the second block is new,
732 * though the code deals with the general case.
733 * A new entry will be inserted in one of the blocks, and that
734 * entry is taken into account when balancing.
737 xfs_dir2_leafn_rebalance(
738 xfs_da_state_t *state, /* btree cursor */
739 xfs_da_state_blk_t *blk1, /* first btree block */
740 xfs_da_state_blk_t *blk2) /* second btree block */
742 xfs_da_args_t *args; /* operation arguments */
743 int count; /* count (& direction) leaves */
744 int isleft; /* new goes in left leaf */
745 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
746 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
747 int mid; /* midpoint leaf index */
749 int oldstale; /* old count of stale leaves */
751 int oldsum; /* old total leaf count */
752 int swap; /* swapped leaf blocks */
756 * If the block order is wrong, swap the arguments.
758 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
759 xfs_da_state_blk_t *tmp; /* temp for block swap */
765 leaf1 = blk1->bp->data;
766 leaf2 = blk2->bp->data;
767 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
769 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
773 * If the old leaf count was odd then the new one will be even,
774 * so we need to divide the new count evenly.
777 xfs_dahash_t midhash; /* middle entry hash value */
779 if (mid >= be16_to_cpu(leaf1->hdr.count))
780 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
782 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
783 isleft = args->hashval <= midhash;
786 * If the old count is even then the new count is odd, so there's
787 * no preferred side for the new entry.
793 * Calculate moved entry count. Positive means left-to-right,
794 * negative means right-to-left. Then move the entries.
796 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
798 xfs_dir2_leafn_moveents(args, blk1->bp,
799 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
801 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
802 be16_to_cpu(leaf1->hdr.count), count);
803 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
804 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
806 * Mark whether we're inserting into the old or new leaf.
808 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
809 state->inleaf = swap;
810 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
811 state->inleaf = !swap;
814 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
816 * Adjust the expected index for insertion.
819 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
822 * Finally sanity check just to make sure we are not returning a
825 if(blk2->index < 0) {
828 xfs_alert(args->dp->i_mount,
829 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
830 __func__, blk1->index);
835 * Remove an entry from a node directory.
836 * This removes the leaf entry and the data entry,
837 * and updates the free block if necessary.
839 static int /* error */
840 xfs_dir2_leafn_remove(
841 xfs_da_args_t *args, /* operation arguments */
842 xfs_dabuf_t *bp, /* leaf buffer */
843 int index, /* leaf entry index */
844 xfs_da_state_blk_t *dblk, /* data block */
845 int *rval) /* resulting block needs join */
847 xfs_dir2_data_hdr_t *hdr; /* data block header */
848 xfs_dir2_db_t db; /* data block number */
849 xfs_dabuf_t *dbp; /* data block buffer */
850 xfs_dir2_data_entry_t *dep; /* data block entry */
851 xfs_inode_t *dp; /* incore directory inode */
852 xfs_dir2_leaf_t *leaf; /* leaf structure */
853 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
854 int longest; /* longest data free entry */
855 int off; /* data block entry offset */
856 xfs_mount_t *mp; /* filesystem mount point */
857 int needlog; /* need to log data header */
858 int needscan; /* need to rescan data frees */
859 xfs_trans_t *tp; /* transaction pointer */
861 trace_xfs_dir2_leafn_remove(args, index);
867 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
869 * Point to the entry we're removing.
871 lep = &leaf->ents[index];
873 * Extract the data block and offset from the entry.
875 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
876 ASSERT(dblk->blkno == db);
877 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
878 ASSERT(dblk->index == off);
880 * Kill the leaf entry by marking it stale.
881 * Log the leaf block changes.
883 be16_add_cpu(&leaf->hdr.stale, 1);
884 xfs_dir2_leaf_log_header(tp, bp);
885 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
886 xfs_dir2_leaf_log_ents(tp, bp, index, index);
888 * Make the data entry free. Keep track of the longest freespace
889 * in the data block in case it changes.
893 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
894 longest = be16_to_cpu(hdr->bestfree[0].length);
895 needlog = needscan = 0;
896 xfs_dir2_data_make_free(tp, dbp, off,
897 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
899 * Rescan the data block freespaces for bestfree.
900 * Log the data block header if needed.
903 xfs_dir2_data_freescan(mp, hdr, &needlog);
905 xfs_dir2_data_log_header(tp, dbp);
906 xfs_dir2_data_check(dp, dbp);
908 * If the longest data block freespace changes, need to update
909 * the corresponding freeblock entry.
911 if (longest < be16_to_cpu(hdr->bestfree[0].length)) {
912 int error; /* error return value */
913 xfs_dabuf_t *fbp; /* freeblock buffer */
914 xfs_dir2_db_t fdb; /* freeblock block number */
915 int findex; /* index in freeblock entries */
916 xfs_dir2_free_t *free; /* freeblock structure */
917 int logfree; /* need to log free entry */
920 * Convert the data block number to a free block,
921 * read in the free block.
923 fdb = xfs_dir2_db_to_fdb(mp, db);
924 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
925 -1, &fbp, XFS_DATA_FORK))) {
929 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
930 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
931 XFS_DIR2_MAX_FREE_BESTS(mp) *
932 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
934 * Calculate which entry we need to fix.
936 findex = xfs_dir2_db_to_fdindex(mp, db);
937 longest = be16_to_cpu(hdr->bestfree[0].length);
939 * If the data block is now empty we can get rid of it
942 if (longest == mp->m_dirblksize - (uint)sizeof(*hdr)) {
944 * Try to punch out the data block.
946 error = xfs_dir2_shrink_inode(args, db, dbp);
952 * We can get ENOSPC if there's no space reservation.
953 * In this case just drop the buffer and some one else
954 * will eventually get rid of the empty block.
956 else if (error == ENOSPC && args->total == 0)
957 xfs_da_buf_done(dbp);
962 * If we got rid of the data block, we can eliminate that entry
967 * One less used entry in the free table.
969 be32_add_cpu(&free->hdr.nused, -1);
970 xfs_dir2_free_log_header(tp, fbp);
972 * If this was the last entry in the table, we can
973 * trim the table size back. There might be other
974 * entries at the end referring to non-existent
975 * data blocks, get those too.
977 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
978 int i; /* free entry index */
982 free->bests[i] == cpu_to_be16(NULLDATAOFF);
985 free->hdr.nvalid = cpu_to_be32(i + 1);
989 * Not the last entry, just punch it out.
992 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
996 * If there are no useful entries left in the block,
997 * get rid of the block if we can.
999 if (!free->hdr.nused) {
1000 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1004 } else if (error != ENOSPC || args->total != 0)
1007 * It's possible to get ENOSPC if there is no
1008 * space reservation. In this case some one
1009 * else will eventually get rid of this block.
1014 * Data block is not empty, just set the free entry to
1018 free->bests[findex] = cpu_to_be16(longest);
1022 * Log the free entry that changed, unless we got rid of it.
1025 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1027 * Drop the buffer if we still have it.
1030 xfs_da_buf_done(fbp);
1032 xfs_dir2_leafn_check(dp, bp);
1034 * Return indication of whether this leaf block is empty enough
1035 * to justify trying to join it with a neighbor.
1038 ((uint)sizeof(leaf->hdr) +
1039 (uint)sizeof(leaf->ents[0]) *
1040 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
1046 * Split the leaf entries in the old block into old and new blocks.
1049 xfs_dir2_leafn_split(
1050 xfs_da_state_t *state, /* btree cursor */
1051 xfs_da_state_blk_t *oldblk, /* original block */
1052 xfs_da_state_blk_t *newblk) /* newly created block */
1054 xfs_da_args_t *args; /* operation arguments */
1055 xfs_dablk_t blkno; /* new leaf block number */
1056 int error; /* error return value */
1057 xfs_mount_t *mp; /* filesystem mount point */
1060 * Allocate space for a new leaf node.
1063 mp = args->dp->i_mount;
1064 ASSERT(args != NULL);
1065 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1066 error = xfs_da_grow_inode(args, &blkno);
1071 * Initialize the new leaf block.
1073 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
1074 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1078 newblk->blkno = blkno;
1079 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1081 * Rebalance the entries across the two leaves, link the new
1082 * block into the leaves.
1084 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1085 error = xfs_da_blk_link(state, oldblk, newblk);
1090 * Insert the new entry in the correct block.
1093 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1095 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1097 * Update last hashval in each block since we added the name.
1099 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1100 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1101 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1102 xfs_dir2_leafn_check(args->dp, newblk->bp);
1107 * Check a leaf block and its neighbors to see if the block should be
1108 * collapsed into one or the other neighbor. Always keep the block
1109 * with the smaller block number.
1110 * If the current block is over 50% full, don't try to join it, return 0.
1111 * If the block is empty, fill in the state structure and return 2.
1112 * If it can be collapsed, fill in the state structure and return 1.
1113 * If nothing can be done, return 0.
1116 xfs_dir2_leafn_toosmall(
1117 xfs_da_state_t *state, /* btree cursor */
1118 int *action) /* resulting action to take */
1120 xfs_da_state_blk_t *blk; /* leaf block */
1121 xfs_dablk_t blkno; /* leaf block number */
1122 xfs_dabuf_t *bp; /* leaf buffer */
1123 int bytes; /* bytes in use */
1124 int count; /* leaf live entry count */
1125 int error; /* error return value */
1126 int forward; /* sibling block direction */
1127 int i; /* sibling counter */
1128 xfs_da_blkinfo_t *info; /* leaf block header */
1129 xfs_dir2_leaf_t *leaf; /* leaf structure */
1130 int rval; /* result from path_shift */
1133 * Check for the degenerate case of the block being over 50% full.
1134 * If so, it's not worth even looking to see if we might be able
1135 * to coalesce with a sibling.
1137 blk = &state->path.blk[state->path.active - 1];
1138 info = blk->bp->data;
1139 ASSERT(info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1140 leaf = (xfs_dir2_leaf_t *)info;
1141 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1142 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1143 if (bytes > (state->blocksize >> 1)) {
1145 * Blk over 50%, don't try to join.
1151 * Check for the degenerate case of the block being empty.
1152 * If the block is empty, we'll simply delete it, no need to
1153 * coalesce it with a sibling block. We choose (arbitrarily)
1154 * to merge with the forward block unless it is NULL.
1158 * Make altpath point to the block we want to keep and
1159 * path point to the block we want to drop (this one).
1161 forward = (info->forw != 0);
1162 memcpy(&state->altpath, &state->path, sizeof(state->path));
1163 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1167 *action = rval ? 2 : 0;
1171 * Examine each sibling block to see if we can coalesce with
1172 * at least 25% free space to spare. We need to figure out
1173 * whether to merge with the forward or the backward block.
1174 * We prefer coalescing with the lower numbered sibling so as
1175 * to shrink a directory over time.
1177 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
1178 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1179 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
1183 * Read the sibling leaf block.
1186 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1187 -1, &bp, XFS_DATA_FORK))) {
1192 * Count bytes in the two blocks combined.
1194 leaf = (xfs_dir2_leaf_t *)info;
1195 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1196 bytes = state->blocksize - (state->blocksize >> 2);
1198 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1199 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1200 bytes -= count * (uint)sizeof(leaf->ents[0]);
1202 * Fits with at least 25% to spare.
1206 xfs_da_brelse(state->args->trans, bp);
1209 * Didn't like either block, give up.
1216 * Done with the sibling leaf block here, drop the dabuf
1217 * so path_shift can get it.
1219 xfs_da_buf_done(bp);
1221 * Make altpath point to the block we want to keep (the lower
1222 * numbered block) and path point to the block we want to drop.
1224 memcpy(&state->altpath, &state->path, sizeof(state->path));
1225 if (blkno < blk->blkno)
1226 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1229 error = xfs_da_path_shift(state, &state->path, forward, 0,
1234 *action = rval ? 0 : 1;
1239 * Move all the leaf entries from drop_blk to save_blk.
1240 * This is done as part of a join operation.
1243 xfs_dir2_leafn_unbalance(
1244 xfs_da_state_t *state, /* cursor */
1245 xfs_da_state_blk_t *drop_blk, /* dead block */
1246 xfs_da_state_blk_t *save_blk) /* surviving block */
1248 xfs_da_args_t *args; /* operation arguments */
1249 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1250 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1253 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1254 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1255 drop_leaf = drop_blk->bp->data;
1256 save_leaf = save_blk->bp->data;
1257 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1258 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1260 * If there are any stale leaf entries, take this opportunity
1263 if (drop_leaf->hdr.stale)
1264 xfs_dir2_leaf_compact(args, drop_blk->bp);
1265 if (save_leaf->hdr.stale)
1266 xfs_dir2_leaf_compact(args, save_blk->bp);
1268 * Move the entries from drop to the appropriate end of save.
1270 drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval);
1271 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1272 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
1273 be16_to_cpu(drop_leaf->hdr.count));
1275 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
1276 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
1277 save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval);
1278 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1282 * Top-level node form directory addname routine.
1285 xfs_dir2_node_addname(
1286 xfs_da_args_t *args) /* operation arguments */
1288 xfs_da_state_blk_t *blk; /* leaf block for insert */
1289 int error; /* error return value */
1290 int rval; /* sub-return value */
1291 xfs_da_state_t *state; /* btree cursor */
1293 trace_xfs_dir2_node_addname(args);
1296 * Allocate and initialize the state (btree cursor).
1298 state = xfs_da_state_alloc();
1300 state->mp = args->dp->i_mount;
1301 state->blocksize = state->mp->m_dirblksize;
1302 state->node_ents = state->mp->m_dir_node_ents;
1304 * Look up the name. We're not supposed to find it, but
1305 * this gives us the insertion point.
1307 error = xfs_da_node_lookup_int(state, &rval);
1310 if (rval != ENOENT) {
1314 * Add the data entry to a data block.
1315 * Extravalid is set to a freeblock found by lookup.
1317 rval = xfs_dir2_node_addname_int(args,
1318 state->extravalid ? &state->extrablk : NULL);
1322 blk = &state->path.blk[state->path.active - 1];
1323 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1325 * Add the new leaf entry.
1327 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1330 * It worked, fix the hash values up the btree.
1332 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1333 xfs_da_fixhashpath(state, &state->path);
1336 * It didn't work, we need to split the leaf block.
1338 if (args->total == 0) {
1339 ASSERT(rval == ENOSPC);
1343 * Split the leaf block and insert the new entry.
1345 rval = xfs_da_split(state);
1348 xfs_da_state_free(state);
1353 * Add the data entry for a node-format directory name addition.
1354 * The leaf entry is added in xfs_dir2_leafn_add.
1355 * We may enter with a freespace block that the lookup found.
1357 static int /* error */
1358 xfs_dir2_node_addname_int(
1359 xfs_da_args_t *args, /* operation arguments */
1360 xfs_da_state_blk_t *fblk) /* optional freespace block */
1362 xfs_dir2_data_hdr_t *hdr; /* data block header */
1363 xfs_dir2_db_t dbno; /* data block number */
1364 xfs_dabuf_t *dbp; /* data block buffer */
1365 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1366 xfs_inode_t *dp; /* incore directory inode */
1367 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1368 int error; /* error return value */
1369 xfs_dir2_db_t fbno; /* freespace block number */
1370 xfs_dabuf_t *fbp; /* freespace buffer */
1371 int findex; /* freespace entry index */
1372 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1373 xfs_dir2_db_t ifbno; /* initial freespace block no */
1374 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1375 int length; /* length of the new entry */
1376 int logfree; /* need to log free entry */
1377 xfs_mount_t *mp; /* filesystem mount point */
1378 int needlog; /* need to log data header */
1379 int needscan; /* need to rescan data frees */
1380 __be16 *tagp; /* data entry tag pointer */
1381 xfs_trans_t *tp; /* transaction pointer */
1386 length = xfs_dir2_data_entsize(args->namelen);
1388 * If we came in with a freespace block that means that lookup
1389 * found an entry with our hash value. This is the freespace
1390 * block for that data entry.
1395 * Remember initial freespace block number.
1397 ifbno = fblk->blkno;
1399 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
1400 findex = fblk->index;
1402 * This means the free entry showed that the data block had
1403 * space for our entry, so we remembered it.
1404 * Use that data block.
1407 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1408 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1409 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1410 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1413 * The data block looked at didn't have enough room.
1414 * We'll start at the beginning of the freespace entries.
1422 * Didn't come in with a freespace block, so don't have a data block.
1430 * If we don't have a data block yet, we're going to scan the
1431 * freespace blocks looking for one. Figure out what the
1432 * highest freespace block number is.
1435 xfs_fileoff_t fo; /* freespace block number */
1437 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1439 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
1443 * While we haven't identified a data block, search the freeblock
1444 * data for a good data block. If we find a null freeblock entry,
1445 * indicating a hole in the data blocks, remember that.
1447 while (dbno == -1) {
1449 * If we don't have a freeblock in hand, get the next one.
1453 * Happens the first time through unless lookup gave
1454 * us a freespace block to start with.
1457 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1459 * If it's ifbno we already looked at it.
1464 * If it's off the end we're done.
1466 if (fbno >= lastfbno)
1469 * Read the block. There can be holes in the
1470 * freespace blocks, so this might not succeed.
1471 * This should be really rare, so there's no reason
1474 if ((error = xfs_da_read_buf(tp, dp,
1475 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
1479 if (unlikely(fbp == NULL)) {
1483 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
1487 * Look at the current free entry. Is it good enough?
1489 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1490 be16_to_cpu(free->bests[findex]) >= length)
1491 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1494 * Are we done with the freeblock?
1496 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
1500 xfs_da_brelse(tp, fbp);
1502 if (fblk && fblk->bp)
1508 * If we don't have a data block, we need to allocate one and make
1509 * the freespace entries refer to it.
1511 if (unlikely(dbno == -1)) {
1513 * Not allowed to allocate, return failure.
1515 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1518 * Drop the freespace buffer unless it came from our
1521 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1522 xfs_da_buf_done(fbp);
1523 return XFS_ERROR(ENOSPC);
1526 * Allocate and initialize the new data block.
1528 if (unlikely((error = xfs_dir2_grow_inode(args,
1529 XFS_DIR2_DATA_SPACE,
1531 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1533 * Drop the freespace buffer unless it came from our
1536 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1537 xfs_da_buf_done(fbp);
1541 * If (somehow) we have a freespace block, get rid of it.
1544 xfs_da_brelse(tp, fbp);
1545 if (fblk && fblk->bp)
1549 * Get the freespace block corresponding to the data block
1550 * that was just allocated.
1552 fbno = xfs_dir2_db_to_fdb(mp, dbno);
1553 if (unlikely(error = xfs_da_read_buf(tp, dp,
1554 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
1556 xfs_da_buf_done(dbp);
1560 * If there wasn't a freespace block, the read will
1561 * return a NULL fbp. Allocate and initialize a new one.
1564 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1569 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
1571 "%s: dir ino " "%llu needed freesp block %lld for\n"
1572 " data block %lld, got %lld ifbno %llu lastfbno %d",
1573 __func__, (unsigned long long)dp->i_ino,
1574 (long long)xfs_dir2_db_to_fdb(mp, dbno),
1575 (long long)dbno, (long long)fbno,
1576 (unsigned long long)ifbno, lastfbno);
1579 " fblk 0x%p blkno %llu index %d magic 0x%x",
1581 (unsigned long long)fblk->blkno,
1585 xfs_alert(mp, " ... fblk is NULL");
1587 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1588 XFS_ERRLEVEL_LOW, mp);
1589 return XFS_ERROR(EFSCORRUPTED);
1593 * Get a buffer for the new block.
1595 if ((error = xfs_da_get_buf(tp, dp,
1596 xfs_dir2_db_to_da(mp, fbno),
1597 -1, &fbp, XFS_DATA_FORK))) {
1600 ASSERT(fbp != NULL);
1603 * Initialize the new block to be empty, and remember
1604 * its first slot as our empty slot.
1607 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1608 free->hdr.firstdb = cpu_to_be32(
1609 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1610 XFS_DIR2_MAX_FREE_BESTS(mp));
1611 free->hdr.nvalid = 0;
1612 free->hdr.nused = 0;
1615 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
1619 * Set the freespace block index from the data block number.
1621 findex = xfs_dir2_db_to_fdindex(mp, dbno);
1623 * If it's after the end of the current entries in the
1624 * freespace block, extend that table.
1626 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
1627 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
1628 free->hdr.nvalid = cpu_to_be32(findex + 1);
1630 * Tag new entry so nused will go up.
1632 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1635 * If this entry was for an empty data block
1636 * (this should always be true) then update the header.
1638 if (free->bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1639 be32_add_cpu(&free->hdr.nused, 1);
1640 xfs_dir2_free_log_header(tp, fbp);
1643 * Update the real value in the table.
1644 * We haven't allocated the data entry yet so this will
1648 free->bests[findex] = hdr->bestfree[0].length;
1652 * We had a data block so we don't have to make a new one.
1656 * If just checking, we succeeded.
1658 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1659 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1660 xfs_da_buf_done(fbp);
1664 * Read the data block in.
1667 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
1668 -1, &dbp, XFS_DATA_FORK))) {
1669 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1670 xfs_da_buf_done(fbp);
1676 ASSERT(be16_to_cpu(hdr->bestfree[0].length) >= length);
1678 * Point to the existing unused space.
1680 dup = (xfs_dir2_data_unused_t *)
1681 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
1682 needscan = needlog = 0;
1684 * Mark the first part of the unused space, inuse for us.
1686 xfs_dir2_data_use_free(tp, dbp, dup,
1687 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
1688 &needlog, &needscan);
1690 * Fill in the new entry and log it.
1692 dep = (xfs_dir2_data_entry_t *)dup;
1693 dep->inumber = cpu_to_be64(args->inumber);
1694 dep->namelen = args->namelen;
1695 memcpy(dep->name, args->name, dep->namelen);
1696 tagp = xfs_dir2_data_entry_tag_p(dep);
1697 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1698 xfs_dir2_data_log_entry(tp, dbp, dep);
1700 * Rescan the block for bestfree if needed.
1703 xfs_dir2_data_freescan(mp, hdr, &needlog);
1705 * Log the data block header if needed.
1708 xfs_dir2_data_log_header(tp, dbp);
1710 * If the freespace entry is now wrong, update it.
1712 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(hdr->bestfree[0].length)) {
1713 free->bests[findex] = hdr->bestfree[0].length;
1717 * Log the freespace entry if needed.
1720 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1722 * If the caller didn't hand us the freespace block, drop it.
1724 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1725 xfs_da_buf_done(fbp);
1727 * Return the data block and offset in args, then drop the data block.
1729 args->blkno = (xfs_dablk_t)dbno;
1730 args->index = be16_to_cpu(*tagp);
1731 xfs_da_buf_done(dbp);
1736 * Lookup an entry in a node-format directory.
1737 * All the real work happens in xfs_da_node_lookup_int.
1738 * The only real output is the inode number of the entry.
1741 xfs_dir2_node_lookup(
1742 xfs_da_args_t *args) /* operation arguments */
1744 int error; /* error return value */
1745 int i; /* btree level */
1746 int rval; /* operation return value */
1747 xfs_da_state_t *state; /* btree cursor */
1749 trace_xfs_dir2_node_lookup(args);
1752 * Allocate and initialize the btree cursor.
1754 state = xfs_da_state_alloc();
1756 state->mp = args->dp->i_mount;
1757 state->blocksize = state->mp->m_dirblksize;
1758 state->node_ents = state->mp->m_dir_node_ents;
1760 * Fill in the path to the entry in the cursor.
1762 error = xfs_da_node_lookup_int(state, &rval);
1765 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1766 /* If a CI match, dup the actual name and return EEXIST */
1767 xfs_dir2_data_entry_t *dep;
1769 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1770 data + state->extrablk.index);
1771 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1774 * Release the btree blocks and leaf block.
1776 for (i = 0; i < state->path.active; i++) {
1777 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1778 state->path.blk[i].bp = NULL;
1781 * Release the data block if we have it.
1783 if (state->extravalid && state->extrablk.bp) {
1784 xfs_da_brelse(args->trans, state->extrablk.bp);
1785 state->extrablk.bp = NULL;
1787 xfs_da_state_free(state);
1792 * Remove an entry from a node-format directory.
1795 xfs_dir2_node_removename(
1796 xfs_da_args_t *args) /* operation arguments */
1798 xfs_da_state_blk_t *blk; /* leaf block */
1799 int error; /* error return value */
1800 int rval; /* operation return value */
1801 xfs_da_state_t *state; /* btree cursor */
1803 trace_xfs_dir2_node_removename(args);
1806 * Allocate and initialize the btree cursor.
1808 state = xfs_da_state_alloc();
1810 state->mp = args->dp->i_mount;
1811 state->blocksize = state->mp->m_dirblksize;
1812 state->node_ents = state->mp->m_dir_node_ents;
1814 * Look up the entry we're deleting, set up the cursor.
1816 error = xfs_da_node_lookup_int(state, &rval);
1820 * Didn't find it, upper layer screwed up.
1822 if (rval != EEXIST) {
1823 xfs_da_state_free(state);
1826 blk = &state->path.blk[state->path.active - 1];
1827 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1828 ASSERT(state->extravalid);
1830 * Remove the leaf and data entries.
1831 * Extrablk refers to the data block.
1833 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1834 &state->extrablk, &rval);
1838 * Fix the hash values up the btree.
1840 xfs_da_fixhashpath(state, &state->path);
1842 * If we need to join leaf blocks, do it.
1844 if (rval && state->path.active > 1)
1845 error = xfs_da_join(state);
1847 * If no errors so far, try conversion to leaf format.
1850 error = xfs_dir2_node_to_leaf(state);
1851 xfs_da_state_free(state);
1856 * Replace an entry's inode number in a node-format directory.
1859 xfs_dir2_node_replace(
1860 xfs_da_args_t *args) /* operation arguments */
1862 xfs_da_state_blk_t *blk; /* leaf block */
1863 xfs_dir2_data_hdr_t *hdr; /* data block header */
1864 xfs_dir2_data_entry_t *dep; /* data entry changed */
1865 int error; /* error return value */
1866 int i; /* btree level */
1867 xfs_ino_t inum; /* new inode number */
1868 xfs_dir2_leaf_t *leaf; /* leaf structure */
1869 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1870 int rval; /* internal return value */
1871 xfs_da_state_t *state; /* btree cursor */
1873 trace_xfs_dir2_node_replace(args);
1876 * Allocate and initialize the btree cursor.
1878 state = xfs_da_state_alloc();
1880 state->mp = args->dp->i_mount;
1881 state->blocksize = state->mp->m_dirblksize;
1882 state->node_ents = state->mp->m_dir_node_ents;
1883 inum = args->inumber;
1885 * Lookup the entry to change in the btree.
1887 error = xfs_da_node_lookup_int(state, &rval);
1892 * It should be found, since the vnodeops layer has looked it up
1893 * and locked it. But paranoia is good.
1895 if (rval == EEXIST) {
1897 * Find the leaf entry.
1899 blk = &state->path.blk[state->path.active - 1];
1900 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1901 leaf = blk->bp->data;
1902 lep = &leaf->ents[blk->index];
1903 ASSERT(state->extravalid);
1905 * Point to the data entry.
1907 hdr = state->extrablk.bp->data;
1908 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
1909 dep = (xfs_dir2_data_entry_t *)
1911 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
1912 ASSERT(inum != be64_to_cpu(dep->inumber));
1914 * Fill in the new inode number and log the entry.
1916 dep->inumber = cpu_to_be64(inum);
1917 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1921 * Didn't find it, and we're holding a data block. Drop it.
1923 else if (state->extravalid) {
1924 xfs_da_brelse(args->trans, state->extrablk.bp);
1925 state->extrablk.bp = NULL;
1928 * Release all the buffers in the cursor.
1930 for (i = 0; i < state->path.active; i++) {
1931 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1932 state->path.blk[i].bp = NULL;
1934 xfs_da_state_free(state);
1939 * Trim off a trailing empty freespace block.
1940 * Return (in rvalp) 1 if we did it, 0 if not.
1943 xfs_dir2_node_trim_free(
1944 xfs_da_args_t *args, /* operation arguments */
1945 xfs_fileoff_t fo, /* free block number */
1946 int *rvalp) /* out: did something */
1948 xfs_dabuf_t *bp; /* freespace buffer */
1949 xfs_inode_t *dp; /* incore directory inode */
1950 int error; /* error return code */
1951 xfs_dir2_free_t *free; /* freespace structure */
1952 xfs_mount_t *mp; /* filesystem mount point */
1953 xfs_trans_t *tp; /* transaction pointer */
1959 * Read the freespace block.
1961 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1967 * There can be holes in freespace. If fo is a hole, there's
1974 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
1976 * If there are used entries, there's nothing to do.
1978 if (be32_to_cpu(free->hdr.nused) > 0) {
1979 xfs_da_brelse(tp, bp);
1984 * Blow the block away.
1987 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
1990 * Can't fail with ENOSPC since that only happens with no
1991 * space reservation, when breaking up an extent into two
1992 * pieces. This is the last block of an extent.
1994 ASSERT(error != ENOSPC);
1995 xfs_da_brelse(tp, bp);
1999 * Return that we succeeded.