Linux Audio

Check our new training course

Loading...
Note: File does not exist in v5.14.15.
   1/*
   2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
   3 * All Rights Reserved.
   4 *
   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.
   8 *
   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.
  13 *
  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
  17 */
  18#include "xfs.h"
  19#include "xfs_fs.h"
  20#include "xfs_types.h"
  21#include "xfs_bit.h"
  22#include "xfs_log.h"
  23#include "xfs_inum.h"
  24#include "xfs_trans.h"
  25#include "xfs_sb.h"
  26#include "xfs_ag.h"
  27#include "xfs_mount.h"
  28#include "xfs_da_btree.h"
  29#include "xfs_bmap_btree.h"
  30#include "xfs_dinode.h"
  31#include "xfs_inode.h"
  32#include "xfs_bmap.h"
  33#include "xfs_dir2_format.h"
  34#include "xfs_dir2_priv.h"
  35#include "xfs_error.h"
  36#include "xfs_trace.h"
  37
  38/*
  39 * Local function declarations.
  40 */
  41#ifdef DEBUG
  42static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
  43#else
  44#define	xfs_dir2_leaf_check(dp, bp)
  45#endif
  46static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
  47				    int *indexp, xfs_dabuf_t **dbpp);
  48static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
  49				    int first, int last);
  50static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
  51
  52
  53/*
  54 * Convert a block form directory to a leaf form directory.
  55 */
  56int						/* error */
  57xfs_dir2_block_to_leaf(
  58	xfs_da_args_t		*args,		/* operation arguments */
  59	xfs_dabuf_t		*dbp)		/* input block's buffer */
  60{
  61	__be16			*bestsp;	/* leaf's bestsp entries */
  62	xfs_dablk_t		blkno;		/* leaf block's bno */
  63	xfs_dir2_data_hdr_t	*hdr;		/* block header */
  64	xfs_dir2_leaf_entry_t	*blp;		/* block's leaf entries */
  65	xfs_dir2_block_tail_t	*btp;		/* block's tail */
  66	xfs_inode_t		*dp;		/* incore directory inode */
  67	int			error;		/* error return code */
  68	xfs_dabuf_t		*lbp;		/* leaf block's buffer */
  69	xfs_dir2_db_t		ldb;		/* leaf block's bno */
  70	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
  71	xfs_dir2_leaf_tail_t	*ltp;		/* leaf's tail */
  72	xfs_mount_t		*mp;		/* filesystem mount point */
  73	int			needlog;	/* need to log block header */
  74	int			needscan;	/* need to rescan bestfree */
  75	xfs_trans_t		*tp;		/* transaction pointer */
  76
  77	trace_xfs_dir2_block_to_leaf(args);
  78
  79	dp = args->dp;
  80	mp = dp->i_mount;
  81	tp = args->trans;
  82	/*
  83	 * Add the leaf block to the inode.
  84	 * This interface will only put blocks in the leaf/node range.
  85	 * Since that's empty now, we'll get the root (block 0 in range).
  86	 */
  87	if ((error = xfs_da_grow_inode(args, &blkno))) {
  88		return error;
  89	}
  90	ldb = xfs_dir2_da_to_db(mp, blkno);
  91	ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
  92	/*
  93	 * Initialize the leaf block, get a buffer for it.
  94	 */
  95	if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
  96		return error;
  97	}
  98	ASSERT(lbp != NULL);
  99	leaf = lbp->data;
 100	hdr = dbp->data;
 101	xfs_dir2_data_check(dp, dbp);
 102	btp = xfs_dir2_block_tail_p(mp, hdr);
 103	blp = xfs_dir2_block_leaf_p(btp);
 104	/*
 105	 * Set the counts in the leaf header.
 106	 */
 107	leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
 108	leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
 109	/*
 110	 * Could compact these but I think we always do the conversion
 111	 * after squeezing out stale entries.
 112	 */
 113	memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
 114	xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
 115	needscan = 0;
 116	needlog = 1;
 117	/*
 118	 * Make the space formerly occupied by the leaf entries and block
 119	 * tail be free.
 120	 */
 121	xfs_dir2_data_make_free(tp, dbp,
 122		(xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
 123		(xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
 124				       (char *)blp),
 125		&needlog, &needscan);
 126	/*
 127	 * Fix up the block header, make it a data block.
 128	 */
 129	hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
 130	if (needscan)
 131		xfs_dir2_data_freescan(mp, hdr, &needlog);
 132	/*
 133	 * Set up leaf tail and bests table.
 134	 */
 135	ltp = xfs_dir2_leaf_tail_p(mp, leaf);
 136	ltp->bestcount = cpu_to_be32(1);
 137	bestsp = xfs_dir2_leaf_bests_p(ltp);
 138	bestsp[0] =  hdr->bestfree[0].length;
 139	/*
 140	 * Log the data header and leaf bests table.
 141	 */
 142	if (needlog)
 143		xfs_dir2_data_log_header(tp, dbp);
 144	xfs_dir2_leaf_check(dp, lbp);
 145	xfs_dir2_data_check(dp, dbp);
 146	xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
 147	xfs_da_buf_done(lbp);
 148	return 0;
 149}
 150
 151STATIC void
 152xfs_dir2_leaf_find_stale(
 153	struct xfs_dir2_leaf	*leaf,
 154	int			index,
 155	int			*lowstale,
 156	int			*highstale)
 157{
 158	/*
 159	 * Find the first stale entry before our index, if any.
 160	 */
 161	for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
 162		if (leaf->ents[*lowstale].address ==
 163		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
 164			break;
 165	}
 166
 167	/*
 168	 * Find the first stale entry at or after our index, if any.
 169	 * Stop if the result would require moving more entries than using
 170	 * lowstale.
 171	 */
 172	for (*highstale = index;
 173	     *highstale < be16_to_cpu(leaf->hdr.count);
 174	     ++*highstale) {
 175		if (leaf->ents[*highstale].address ==
 176		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
 177			break;
 178		if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
 179			break;
 180	}
 181}
 182
 183struct xfs_dir2_leaf_entry *
 184xfs_dir2_leaf_find_entry(
 185	xfs_dir2_leaf_t		*leaf,		/* leaf structure */
 186	int			index,		/* leaf table position */
 187	int			compact,	/* need to compact leaves */
 188	int			lowstale,	/* index of prev stale leaf */
 189	int			highstale,	/* index of next stale leaf */
 190	int			*lfloglow,	/* low leaf logging index */
 191	int			*lfloghigh)	/* high leaf logging index */
 192{
 193	if (!leaf->hdr.stale) {
 194		xfs_dir2_leaf_entry_t	*lep;	/* leaf entry table pointer */
 195
 196		/*
 197		 * Now we need to make room to insert the leaf entry.
 198		 *
 199		 * If there are no stale entries, just insert a hole at index.
 200		 */
 201		lep = &leaf->ents[index];
 202		if (index < be16_to_cpu(leaf->hdr.count))
 203			memmove(lep + 1, lep,
 204				(be16_to_cpu(leaf->hdr.count) - index) *
 205				 sizeof(*lep));
 206
 207		/*
 208		 * Record low and high logging indices for the leaf.
 209		 */
 210		*lfloglow = index;
 211		*lfloghigh = be16_to_cpu(leaf->hdr.count);
 212		be16_add_cpu(&leaf->hdr.count, 1);
 213		return lep;
 214	}
 215
 216	/*
 217	 * There are stale entries.
 218	 *
 219	 * We will use one of them for the new entry.  It's probably not at
 220	 * the right location, so we'll have to shift some up or down first.
 221	 *
 222	 * If we didn't compact before, we need to find the nearest stale
 223	 * entries before and after our insertion point.
 224	 */
 225	if (compact == 0)
 226		xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
 227
 228	/*
 229	 * If the low one is better, use it.
 230	 */
 231	if (lowstale >= 0 &&
 232	    (highstale == be16_to_cpu(leaf->hdr.count) ||
 233	     index - lowstale - 1 < highstale - index)) {
 234		ASSERT(index - lowstale - 1 >= 0);
 235		ASSERT(leaf->ents[lowstale].address ==
 236		       cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
 237
 238		/*
 239		 * Copy entries up to cover the stale entry and make room
 240		 * for the new entry.
 241		 */
 242		if (index - lowstale - 1 > 0) {
 243			memmove(&leaf->ents[lowstale],
 244				&leaf->ents[lowstale + 1],
 245				(index - lowstale - 1) *
 246				sizeof(xfs_dir2_leaf_entry_t));
 247		}
 248		*lfloglow = MIN(lowstale, *lfloglow);
 249		*lfloghigh = MAX(index - 1, *lfloghigh);
 250		be16_add_cpu(&leaf->hdr.stale, -1);
 251		return &leaf->ents[index - 1];
 252	}
 253
 254	/*
 255	 * The high one is better, so use that one.
 256	 */
 257	ASSERT(highstale - index >= 0);
 258	ASSERT(leaf->ents[highstale].address ==
 259	       cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
 260
 261	/*
 262	 * Copy entries down to cover the stale entry and make room for the
 263	 * new entry.
 264	 */
 265	if (highstale - index > 0) {
 266		memmove(&leaf->ents[index + 1],
 267			&leaf->ents[index],
 268			(highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
 269	}
 270	*lfloglow = MIN(index, *lfloglow);
 271	*lfloghigh = MAX(highstale, *lfloghigh);
 272	be16_add_cpu(&leaf->hdr.stale, -1);
 273	return &leaf->ents[index];
 274}
 275
 276/*
 277 * Add an entry to a leaf form directory.
 278 */
 279int						/* error */
 280xfs_dir2_leaf_addname(
 281	xfs_da_args_t		*args)		/* operation arguments */
 282{
 283	__be16			*bestsp;	/* freespace table in leaf */
 284	int			compact;	/* need to compact leaves */
 285	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
 286	xfs_dabuf_t		*dbp;		/* data block buffer */
 287	xfs_dir2_data_entry_t	*dep;		/* data block entry */
 288	xfs_inode_t		*dp;		/* incore directory inode */
 289	xfs_dir2_data_unused_t	*dup;		/* data unused entry */
 290	int			error;		/* error return value */
 291	int			grown;		/* allocated new data block */
 292	int			highstale;	/* index of next stale leaf */
 293	int			i;		/* temporary, index */
 294	int			index;		/* leaf table position */
 295	xfs_dabuf_t		*lbp;		/* leaf's buffer */
 296	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
 297	int			length;		/* length of new entry */
 298	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry table pointer */
 299	int			lfloglow;	/* low leaf logging index */
 300	int			lfloghigh;	/* high leaf logging index */
 301	int			lowstale;	/* index of prev stale leaf */
 302	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail pointer */
 303	xfs_mount_t		*mp;		/* filesystem mount point */
 304	int			needbytes;	/* leaf block bytes needed */
 305	int			needlog;	/* need to log data header */
 306	int			needscan;	/* need to rescan data free */
 307	__be16			*tagp;		/* end of data entry */
 308	xfs_trans_t		*tp;		/* transaction pointer */
 309	xfs_dir2_db_t		use_block;	/* data block number */
 310
 311	trace_xfs_dir2_leaf_addname(args);
 312
 313	dp = args->dp;
 314	tp = args->trans;
 315	mp = dp->i_mount;
 316	/*
 317	 * Read the leaf block.
 318	 */
 319	error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
 320		XFS_DATA_FORK);
 321	if (error) {
 322		return error;
 323	}
 324	ASSERT(lbp != NULL);
 325	/*
 326	 * Look up the entry by hash value and name.
 327	 * We know it's not there, our caller has already done a lookup.
 328	 * So the index is of the entry to insert in front of.
 329	 * But if there are dup hash values the index is of the first of those.
 330	 */
 331	index = xfs_dir2_leaf_search_hash(args, lbp);
 332	leaf = lbp->data;
 333	ltp = xfs_dir2_leaf_tail_p(mp, leaf);
 334	bestsp = xfs_dir2_leaf_bests_p(ltp);
 335	length = xfs_dir2_data_entsize(args->namelen);
 336	/*
 337	 * See if there are any entries with the same hash value
 338	 * and space in their block for the new entry.
 339	 * This is good because it puts multiple same-hash value entries
 340	 * in a data block, improving the lookup of those entries.
 341	 */
 342	for (use_block = -1, lep = &leaf->ents[index];
 343	     index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
 344	     index++, lep++) {
 345		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
 346			continue;
 347		i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
 348		ASSERT(i < be32_to_cpu(ltp->bestcount));
 349		ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
 350		if (be16_to_cpu(bestsp[i]) >= length) {
 351			use_block = i;
 352			break;
 353		}
 354	}
 355	/*
 356	 * Didn't find a block yet, linear search all the data blocks.
 357	 */
 358	if (use_block == -1) {
 359		for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
 360			/*
 361			 * Remember a block we see that's missing.
 362			 */
 363			if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
 364			    use_block == -1)
 365				use_block = i;
 366			else if (be16_to_cpu(bestsp[i]) >= length) {
 367				use_block = i;
 368				break;
 369			}
 370		}
 371	}
 372	/*
 373	 * How many bytes do we need in the leaf block?
 374	 */
 375	needbytes = 0;
 376	if (!leaf->hdr.stale)
 377		needbytes += sizeof(xfs_dir2_leaf_entry_t);
 378	if (use_block == -1)
 379		needbytes += sizeof(xfs_dir2_data_off_t);
 380
 381	/*
 382	 * Now kill use_block if it refers to a missing block, so we
 383	 * can use it as an indication of allocation needed.
 384	 */
 385	if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
 386		use_block = -1;
 387	/*
 388	 * If we don't have enough free bytes but we can make enough
 389	 * by compacting out stale entries, we'll do that.
 390	 */
 391	if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
 392				needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
 393		compact = 1;
 394	}
 395	/*
 396	 * Otherwise if we don't have enough free bytes we need to
 397	 * convert to node form.
 398	 */
 399	else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
 400						leaf->hdr.count)] < needbytes) {
 401		/*
 402		 * Just checking or no space reservation, give up.
 403		 */
 404		if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
 405							args->total == 0) {
 406			xfs_da_brelse(tp, lbp);
 407			return XFS_ERROR(ENOSPC);
 408		}
 409		/*
 410		 * Convert to node form.
 411		 */
 412		error = xfs_dir2_leaf_to_node(args, lbp);
 413		xfs_da_buf_done(lbp);
 414		if (error)
 415			return error;
 416		/*
 417		 * Then add the new entry.
 418		 */
 419		return xfs_dir2_node_addname(args);
 420	}
 421	/*
 422	 * Otherwise it will fit without compaction.
 423	 */
 424	else
 425		compact = 0;
 426	/*
 427	 * If just checking, then it will fit unless we needed to allocate
 428	 * a new data block.
 429	 */
 430	if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
 431		xfs_da_brelse(tp, lbp);
 432		return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
 433	}
 434	/*
 435	 * If no allocations are allowed, return now before we've
 436	 * changed anything.
 437	 */
 438	if (args->total == 0 && use_block == -1) {
 439		xfs_da_brelse(tp, lbp);
 440		return XFS_ERROR(ENOSPC);
 441	}
 442	/*
 443	 * Need to compact the leaf entries, removing stale ones.
 444	 * Leave one stale entry behind - the one closest to our
 445	 * insertion index - and we'll shift that one to our insertion
 446	 * point later.
 447	 */
 448	if (compact) {
 449		xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
 450			&lfloglow, &lfloghigh);
 451	}
 452	/*
 453	 * There are stale entries, so we'll need log-low and log-high
 454	 * impossibly bad values later.
 455	 */
 456	else if (be16_to_cpu(leaf->hdr.stale)) {
 457		lfloglow = be16_to_cpu(leaf->hdr.count);
 458		lfloghigh = -1;
 459	}
 460	/*
 461	 * If there was no data block space found, we need to allocate
 462	 * a new one.
 463	 */
 464	if (use_block == -1) {
 465		/*
 466		 * Add the new data block.
 467		 */
 468		if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
 469				&use_block))) {
 470			xfs_da_brelse(tp, lbp);
 471			return error;
 472		}
 473		/*
 474		 * Initialize the block.
 475		 */
 476		if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
 477			xfs_da_brelse(tp, lbp);
 478			return error;
 479		}
 480		/*
 481		 * If we're adding a new data block on the end we need to
 482		 * extend the bests table.  Copy it up one entry.
 483		 */
 484		if (use_block >= be32_to_cpu(ltp->bestcount)) {
 485			bestsp--;
 486			memmove(&bestsp[0], &bestsp[1],
 487				be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
 488			be32_add_cpu(&ltp->bestcount, 1);
 489			xfs_dir2_leaf_log_tail(tp, lbp);
 490			xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
 491		}
 492		/*
 493		 * If we're filling in a previously empty block just log it.
 494		 */
 495		else
 496			xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
 497		hdr = dbp->data;
 498		bestsp[use_block] = hdr->bestfree[0].length;
 499		grown = 1;
 500	}
 501	/*
 502	 * Already had space in some data block.
 503	 * Just read that one in.
 504	 */
 505	else {
 506		if ((error =
 507		    xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
 508			    -1, &dbp, XFS_DATA_FORK))) {
 509			xfs_da_brelse(tp, lbp);
 510			return error;
 511		}
 512		hdr = dbp->data;
 513		grown = 0;
 514	}
 515	xfs_dir2_data_check(dp, dbp);
 516	/*
 517	 * Point to the biggest freespace in our data block.
 518	 */
 519	dup = (xfs_dir2_data_unused_t *)
 520	      ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
 521	ASSERT(be16_to_cpu(dup->length) >= length);
 522	needscan = needlog = 0;
 523	/*
 524	 * Mark the initial part of our freespace in use for the new entry.
 525	 */
 526	xfs_dir2_data_use_free(tp, dbp, dup,
 527		(xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
 528		&needlog, &needscan);
 529	/*
 530	 * Initialize our new entry (at last).
 531	 */
 532	dep = (xfs_dir2_data_entry_t *)dup;
 533	dep->inumber = cpu_to_be64(args->inumber);
 534	dep->namelen = args->namelen;
 535	memcpy(dep->name, args->name, dep->namelen);
 536	tagp = xfs_dir2_data_entry_tag_p(dep);
 537	*tagp = cpu_to_be16((char *)dep - (char *)hdr);
 538	/*
 539	 * Need to scan fix up the bestfree table.
 540	 */
 541	if (needscan)
 542		xfs_dir2_data_freescan(mp, hdr, &needlog);
 543	/*
 544	 * Need to log the data block's header.
 545	 */
 546	if (needlog)
 547		xfs_dir2_data_log_header(tp, dbp);
 548	xfs_dir2_data_log_entry(tp, dbp, dep);
 549	/*
 550	 * If the bests table needs to be changed, do it.
 551	 * Log the change unless we've already done that.
 552	 */
 553	if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
 554		bestsp[use_block] = hdr->bestfree[0].length;
 555		if (!grown)
 556			xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
 557	}
 558
 559	lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
 560				       highstale, &lfloglow, &lfloghigh);
 561
 562	/*
 563	 * Fill in the new leaf entry.
 564	 */
 565	lep->hashval = cpu_to_be32(args->hashval);
 566	lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
 567				be16_to_cpu(*tagp)));
 568	/*
 569	 * Log the leaf fields and give up the buffers.
 570	 */
 571	xfs_dir2_leaf_log_header(tp, lbp);
 572	xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
 573	xfs_dir2_leaf_check(dp, lbp);
 574	xfs_da_buf_done(lbp);
 575	xfs_dir2_data_check(dp, dbp);
 576	xfs_da_buf_done(dbp);
 577	return 0;
 578}
 579
 580#ifdef DEBUG
 581/*
 582 * Check the internal consistency of a leaf1 block.
 583 * Pop an assert if something is wrong.
 584 */
 585STATIC void
 586xfs_dir2_leaf_check(
 587	xfs_inode_t		*dp,		/* incore directory inode */
 588	xfs_dabuf_t		*bp)		/* leaf's buffer */
 589{
 590	int			i;		/* leaf index */
 591	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
 592	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail pointer */
 593	xfs_mount_t		*mp;		/* filesystem mount point */
 594	int			stale;		/* count of stale leaves */
 595
 596	leaf = bp->data;
 597	mp = dp->i_mount;
 598	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
 599	/*
 600	 * This value is not restrictive enough.
 601	 * Should factor in the size of the bests table as well.
 602	 * We can deduce a value for that from di_size.
 603	 */
 604	ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
 605	ltp = xfs_dir2_leaf_tail_p(mp, leaf);
 606	/*
 607	 * Leaves and bests don't overlap.
 608	 */
 609	ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
 610	       (char *)xfs_dir2_leaf_bests_p(ltp));
 611	/*
 612	 * Check hash value order, count stale entries.
 613	 */
 614	for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
 615		if (i + 1 < be16_to_cpu(leaf->hdr.count))
 616			ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
 617			       be32_to_cpu(leaf->ents[i + 1].hashval));
 618		if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
 619			stale++;
 620	}
 621	ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
 622}
 623#endif	/* DEBUG */
 624
 625/*
 626 * Compact out any stale entries in the leaf.
 627 * Log the header and changed leaf entries, if any.
 628 */
 629void
 630xfs_dir2_leaf_compact(
 631	xfs_da_args_t	*args,		/* operation arguments */
 632	xfs_dabuf_t	*bp)		/* leaf buffer */
 633{
 634	int		from;		/* source leaf index */
 635	xfs_dir2_leaf_t	*leaf;		/* leaf structure */
 636	int		loglow;		/* first leaf entry to log */
 637	int		to;		/* target leaf index */
 638
 639	leaf = bp->data;
 640	if (!leaf->hdr.stale) {
 641		return;
 642	}
 643	/*
 644	 * Compress out the stale entries in place.
 645	 */
 646	for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
 647		if (leaf->ents[from].address ==
 648		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
 649			continue;
 650		/*
 651		 * Only actually copy the entries that are different.
 652		 */
 653		if (from > to) {
 654			if (loglow == -1)
 655				loglow = to;
 656			leaf->ents[to] = leaf->ents[from];
 657		}
 658		to++;
 659	}
 660	/*
 661	 * Update and log the header, log the leaf entries.
 662	 */
 663	ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
 664	be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
 665	leaf->hdr.stale = 0;
 666	xfs_dir2_leaf_log_header(args->trans, bp);
 667	if (loglow != -1)
 668		xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
 669}
 670
 671/*
 672 * Compact the leaf entries, removing stale ones.
 673 * Leave one stale entry behind - the one closest to our
 674 * insertion index - and the caller will shift that one to our insertion
 675 * point later.
 676 * Return new insertion index, where the remaining stale entry is,
 677 * and leaf logging indices.
 678 */
 679void
 680xfs_dir2_leaf_compact_x1(
 681	xfs_dabuf_t	*bp,		/* leaf buffer */
 682	int		*indexp,	/* insertion index */
 683	int		*lowstalep,	/* out: stale entry before us */
 684	int		*highstalep,	/* out: stale entry after us */
 685	int		*lowlogp,	/* out: low log index */
 686	int		*highlogp)	/* out: high log index */
 687{
 688	int		from;		/* source copy index */
 689	int		highstale;	/* stale entry at/after index */
 690	int		index;		/* insertion index */
 691	int		keepstale;	/* source index of kept stale */
 692	xfs_dir2_leaf_t	*leaf;		/* leaf structure */
 693	int		lowstale;	/* stale entry before index */
 694	int		newindex=0;	/* new insertion index */
 695	int		to;		/* destination copy index */
 696
 697	leaf = bp->data;
 698	ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
 699	index = *indexp;
 700
 701	xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
 702
 703	/*
 704	 * Pick the better of lowstale and highstale.
 705	 */
 706	if (lowstale >= 0 &&
 707	    (highstale == be16_to_cpu(leaf->hdr.count) ||
 708	     index - lowstale <= highstale - index))
 709		keepstale = lowstale;
 710	else
 711		keepstale = highstale;
 712	/*
 713	 * Copy the entries in place, removing all the stale entries
 714	 * except keepstale.
 715	 */
 716	for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
 717		/*
 718		 * Notice the new value of index.
 719		 */
 720		if (index == from)
 721			newindex = to;
 722		if (from != keepstale &&
 723		    leaf->ents[from].address ==
 724		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
 725			if (from == to)
 726				*lowlogp = to;
 727			continue;
 728		}
 729		/*
 730		 * Record the new keepstale value for the insertion.
 731		 */
 732		if (from == keepstale)
 733			lowstale = highstale = to;
 734		/*
 735		 * Copy only the entries that have moved.
 736		 */
 737		if (from > to)
 738			leaf->ents[to] = leaf->ents[from];
 739		to++;
 740	}
 741	ASSERT(from > to);
 742	/*
 743	 * If the insertion point was past the last entry,
 744	 * set the new insertion point accordingly.
 745	 */
 746	if (index == from)
 747		newindex = to;
 748	*indexp = newindex;
 749	/*
 750	 * Adjust the leaf header values.
 751	 */
 752	be16_add_cpu(&leaf->hdr.count, -(from - to));
 753	leaf->hdr.stale = cpu_to_be16(1);
 754	/*
 755	 * Remember the low/high stale value only in the "right"
 756	 * direction.
 757	 */
 758	if (lowstale >= newindex)
 759		lowstale = -1;
 760	else
 761		highstale = be16_to_cpu(leaf->hdr.count);
 762	*highlogp = be16_to_cpu(leaf->hdr.count) - 1;
 763	*lowstalep = lowstale;
 764	*highstalep = highstale;
 765}
 766
 767/*
 768 * Getdents (readdir) for leaf and node directories.
 769 * This reads the data blocks only, so is the same for both forms.
 770 */
 771int						/* error */
 772xfs_dir2_leaf_getdents(
 773	xfs_inode_t		*dp,		/* incore directory inode */
 774	void			*dirent,
 775	size_t			bufsize,
 776	xfs_off_t		*offset,
 777	filldir_t		filldir)
 778{
 779	xfs_dabuf_t		*bp;		/* data block buffer */
 780	int			byteoff;	/* offset in current block */
 781	xfs_dir2_db_t		curdb;		/* db for current block */
 782	xfs_dir2_off_t		curoff;		/* current overall offset */
 783	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
 784	xfs_dir2_data_entry_t	*dep;		/* data entry */
 785	xfs_dir2_data_unused_t	*dup;		/* unused entry */
 786	int			error = 0;	/* error return value */
 787	int			i;		/* temporary loop index */
 788	int			j;		/* temporary loop index */
 789	int			length;		/* temporary length value */
 790	xfs_bmbt_irec_t		*map;		/* map vector for blocks */
 791	xfs_extlen_t		map_blocks;	/* number of fsbs in map */
 792	xfs_dablk_t		map_off;	/* last mapped file offset */
 793	int			map_size;	/* total entries in *map */
 794	int			map_valid;	/* valid entries in *map */
 795	xfs_mount_t		*mp;		/* filesystem mount point */
 796	xfs_dir2_off_t		newoff;		/* new curoff after new blk */
 797	int			nmap;		/* mappings to ask xfs_bmapi */
 798	char			*ptr = NULL;	/* pointer to current data */
 799	int			ra_current;	/* number of read-ahead blks */
 800	int			ra_index;	/* *map index for read-ahead */
 801	int			ra_offset;	/* map entry offset for ra */
 802	int			ra_want;	/* readahead count wanted */
 803
 804	/*
 805	 * If the offset is at or past the largest allowed value,
 806	 * give up right away.
 807	 */
 808	if (*offset >= XFS_DIR2_MAX_DATAPTR)
 809		return 0;
 810
 811	mp = dp->i_mount;
 812
 813	/*
 814	 * Set up to bmap a number of blocks based on the caller's
 815	 * buffer size, the directory block size, and the filesystem
 816	 * block size.
 817	 */
 818	map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
 819	map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
 820	map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
 821	bp = NULL;
 822
 823	/*
 824	 * Inside the loop we keep the main offset value as a byte offset
 825	 * in the directory file.
 826	 */
 827	curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
 828
 829	/*
 830	 * Force this conversion through db so we truncate the offset
 831	 * down to get the start of the data block.
 832	 */
 833	map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
 834	/*
 835	 * Loop over directory entries until we reach the end offset.
 836	 * Get more blocks and readahead as necessary.
 837	 */
 838	while (curoff < XFS_DIR2_LEAF_OFFSET) {
 839		/*
 840		 * If we have no buffer, or we're off the end of the
 841		 * current buffer, need to get another one.
 842		 */
 843		if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
 844			/*
 845			 * If we have a buffer, we need to release it and
 846			 * take it out of the mapping.
 847			 */
 848			if (bp) {
 849				xfs_da_brelse(NULL, bp);
 850				bp = NULL;
 851				map_blocks -= mp->m_dirblkfsbs;
 852				/*
 853				 * Loop to get rid of the extents for the
 854				 * directory block.
 855				 */
 856				for (i = mp->m_dirblkfsbs; i > 0; ) {
 857					j = MIN((int)map->br_blockcount, i);
 858					map->br_blockcount -= j;
 859					map->br_startblock += j;
 860					map->br_startoff += j;
 861					/*
 862					 * If mapping is done, pitch it from
 863					 * the table.
 864					 */
 865					if (!map->br_blockcount && --map_valid)
 866						memmove(&map[0], &map[1],
 867							sizeof(map[0]) *
 868							map_valid);
 869					i -= j;
 870				}
 871			}
 872			/*
 873			 * Recalculate the readahead blocks wanted.
 874			 */
 875			ra_want = howmany(bufsize + mp->m_dirblksize,
 876					  mp->m_sb.sb_blocksize) - 1;
 877			ASSERT(ra_want >= 0);
 878
 879			/*
 880			 * If we don't have as many as we want, and we haven't
 881			 * run out of data blocks, get some more mappings.
 882			 */
 883			if (1 + ra_want > map_blocks &&
 884			    map_off <
 885			    xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
 886				/*
 887				 * Get more bmaps, fill in after the ones
 888				 * we already have in the table.
 889				 */
 890				nmap = map_size - map_valid;
 891				error = xfs_bmapi(NULL, dp,
 892					map_off,
 893					xfs_dir2_byte_to_da(mp,
 894						XFS_DIR2_LEAF_OFFSET) - map_off,
 895					XFS_BMAPI_METADATA, NULL, 0,
 896					&map[map_valid], &nmap, NULL);
 897				/*
 898				 * Don't know if we should ignore this or
 899				 * try to return an error.
 900				 * The trouble with returning errors
 901				 * is that readdir will just stop without
 902				 * actually passing the error through.
 903				 */
 904				if (error)
 905					break;	/* XXX */
 906				/*
 907				 * If we got all the mappings we asked for,
 908				 * set the final map offset based on the
 909				 * last bmap value received.
 910				 * Otherwise, we've reached the end.
 911				 */
 912				if (nmap == map_size - map_valid)
 913					map_off =
 914					map[map_valid + nmap - 1].br_startoff +
 915					map[map_valid + nmap - 1].br_blockcount;
 916				else
 917					map_off =
 918						xfs_dir2_byte_to_da(mp,
 919							XFS_DIR2_LEAF_OFFSET);
 920				/*
 921				 * Look for holes in the mapping, and
 922				 * eliminate them.  Count up the valid blocks.
 923				 */
 924				for (i = map_valid; i < map_valid + nmap; ) {
 925					if (map[i].br_startblock ==
 926					    HOLESTARTBLOCK) {
 927						nmap--;
 928						length = map_valid + nmap - i;
 929						if (length)
 930							memmove(&map[i],
 931								&map[i + 1],
 932								sizeof(map[i]) *
 933								length);
 934					} else {
 935						map_blocks +=
 936							map[i].br_blockcount;
 937						i++;
 938					}
 939				}
 940				map_valid += nmap;
 941			}
 942			/*
 943			 * No valid mappings, so no more data blocks.
 944			 */
 945			if (!map_valid) {
 946				curoff = xfs_dir2_da_to_byte(mp, map_off);
 947				break;
 948			}
 949			/*
 950			 * Read the directory block starting at the first
 951			 * mapping.
 952			 */
 953			curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
 954			error = xfs_da_read_buf(NULL, dp, map->br_startoff,
 955				map->br_blockcount >= mp->m_dirblkfsbs ?
 956				    XFS_FSB_TO_DADDR(mp, map->br_startblock) :
 957				    -1,
 958				&bp, XFS_DATA_FORK);
 959			/*
 960			 * Should just skip over the data block instead
 961			 * of giving up.
 962			 */
 963			if (error)
 964				break;	/* XXX */
 965			/*
 966			 * Adjust the current amount of read-ahead: we just
 967			 * read a block that was previously ra.
 968			 */
 969			if (ra_current)
 970				ra_current -= mp->m_dirblkfsbs;
 971			/*
 972			 * Do we need more readahead?
 973			 */
 974			for (ra_index = ra_offset = i = 0;
 975			     ra_want > ra_current && i < map_blocks;
 976			     i += mp->m_dirblkfsbs) {
 977				ASSERT(ra_index < map_valid);
 978				/*
 979				 * Read-ahead a contiguous directory block.
 980				 */
 981				if (i > ra_current &&
 982				    map[ra_index].br_blockcount >=
 983				    mp->m_dirblkfsbs) {
 984					xfs_buf_readahead(mp->m_ddev_targp,
 985						XFS_FSB_TO_DADDR(mp,
 986						   map[ra_index].br_startblock +
 987						   ra_offset),
 988						(int)BTOBB(mp->m_dirblksize));
 989					ra_current = i;
 990				}
 991				/*
 992				 * Read-ahead a non-contiguous directory block.
 993				 * This doesn't use our mapping, but this
 994				 * is a very rare case.
 995				 */
 996				else if (i > ra_current) {
 997					(void)xfs_da_reada_buf(NULL, dp,
 998						map[ra_index].br_startoff +
 999						ra_offset, XFS_DATA_FORK);
1000					ra_current = i;
1001				}
1002				/*
1003				 * Advance offset through the mapping table.
1004				 */
1005				for (j = 0; j < mp->m_dirblkfsbs; j++) {
1006					/*
1007					 * The rest of this extent but not
1008					 * more than a dir block.
1009					 */
1010					length = MIN(mp->m_dirblkfsbs,
1011						(int)(map[ra_index].br_blockcount -
1012						ra_offset));
1013					j += length;
1014					ra_offset += length;
1015					/*
1016					 * Advance to the next mapping if
1017					 * this one is used up.
1018					 */
1019					if (ra_offset ==
1020					    map[ra_index].br_blockcount) {
1021						ra_offset = 0;
1022						ra_index++;
1023					}
1024				}
1025			}
1026			/*
1027			 * Having done a read, we need to set a new offset.
1028			 */
1029			newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
1030			/*
1031			 * Start of the current block.
1032			 */
1033			if (curoff < newoff)
1034				curoff = newoff;
1035			/*
1036			 * Make sure we're in the right block.
1037			 */
1038			else if (curoff > newoff)
1039				ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1040				       curdb);
1041			hdr = bp->data;
1042			xfs_dir2_data_check(dp, bp);
1043			/*
1044			 * Find our position in the block.
1045			 */
1046			ptr = (char *)(hdr + 1);
1047			byteoff = xfs_dir2_byte_to_off(mp, curoff);
1048			/*
1049			 * Skip past the header.
1050			 */
1051			if (byteoff == 0)
1052				curoff += (uint)sizeof(*hdr);
1053			/*
1054			 * Skip past entries until we reach our offset.
1055			 */
1056			else {
1057				while ((char *)ptr - (char *)hdr < byteoff) {
1058					dup = (xfs_dir2_data_unused_t *)ptr;
1059
1060					if (be16_to_cpu(dup->freetag)
1061						  == XFS_DIR2_DATA_FREE_TAG) {
1062
1063						length = be16_to_cpu(dup->length);
1064						ptr += length;
1065						continue;
1066					}
1067					dep = (xfs_dir2_data_entry_t *)ptr;
1068					length =
1069					   xfs_dir2_data_entsize(dep->namelen);
1070					ptr += length;
1071				}
1072				/*
1073				 * Now set our real offset.
1074				 */
1075				curoff =
1076					xfs_dir2_db_off_to_byte(mp,
1077					    xfs_dir2_byte_to_db(mp, curoff),
1078					    (char *)ptr - (char *)hdr);
1079				if (ptr >= (char *)hdr + mp->m_dirblksize) {
1080					continue;
1081				}
1082			}
1083		}
1084		/*
1085		 * We have a pointer to an entry.
1086		 * Is it a live one?
1087		 */
1088		dup = (xfs_dir2_data_unused_t *)ptr;
1089		/*
1090		 * No, it's unused, skip over it.
1091		 */
1092		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1093			length = be16_to_cpu(dup->length);
1094			ptr += length;
1095			curoff += length;
1096			continue;
1097		}
1098
1099		dep = (xfs_dir2_data_entry_t *)ptr;
1100		length = xfs_dir2_data_entsize(dep->namelen);
1101
1102		if (filldir(dirent, (char *)dep->name, dep->namelen,
1103			    xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
1104			    be64_to_cpu(dep->inumber), DT_UNKNOWN))
1105			break;
1106
1107		/*
1108		 * Advance to next entry in the block.
1109		 */
1110		ptr += length;
1111		curoff += length;
1112		/* bufsize may have just been a guess; don't go negative */
1113		bufsize = bufsize > length ? bufsize - length : 0;
1114	}
1115
1116	/*
1117	 * All done.  Set output offset value to current offset.
1118	 */
1119	if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
1120		*offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
1121	else
1122		*offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
1123	kmem_free(map);
1124	if (bp)
1125		xfs_da_brelse(NULL, bp);
1126	return error;
1127}
1128
1129/*
1130 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1131 */
1132int
1133xfs_dir2_leaf_init(
1134	xfs_da_args_t		*args,		/* operation arguments */
1135	xfs_dir2_db_t		bno,		/* directory block number */
1136	xfs_dabuf_t		**bpp,		/* out: leaf buffer */
1137	int			magic)		/* magic number for block */
1138{
1139	xfs_dabuf_t		*bp;		/* leaf buffer */
1140	xfs_inode_t		*dp;		/* incore directory inode */
1141	int			error;		/* error return code */
1142	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1143	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1144	xfs_mount_t		*mp;		/* filesystem mount point */
1145	xfs_trans_t		*tp;		/* transaction pointer */
1146
1147	dp = args->dp;
1148	ASSERT(dp != NULL);
1149	tp = args->trans;
1150	mp = dp->i_mount;
1151	ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1152	       bno < XFS_DIR2_FREE_FIRSTDB(mp));
1153	/*
1154	 * Get the buffer for the block.
1155	 */
1156	error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1157		XFS_DATA_FORK);
1158	if (error) {
1159		return error;
1160	}
1161	ASSERT(bp != NULL);
1162	leaf = bp->data;
1163	/*
1164	 * Initialize the header.
1165	 */
1166	leaf->hdr.info.magic = cpu_to_be16(magic);
1167	leaf->hdr.info.forw = 0;
1168	leaf->hdr.info.back = 0;
1169	leaf->hdr.count = 0;
1170	leaf->hdr.stale = 0;
1171	xfs_dir2_leaf_log_header(tp, bp);
1172	/*
1173	 * If it's a leaf-format directory initialize the tail.
1174	 * In this case our caller has the real bests table to copy into
1175	 * the block.
1176	 */
1177	if (magic == XFS_DIR2_LEAF1_MAGIC) {
1178		ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1179		ltp->bestcount = 0;
1180		xfs_dir2_leaf_log_tail(tp, bp);
1181	}
1182	*bpp = bp;
1183	return 0;
1184}
1185
1186/*
1187 * Log the bests entries indicated from a leaf1 block.
1188 */
1189static void
1190xfs_dir2_leaf_log_bests(
1191	xfs_trans_t		*tp,		/* transaction pointer */
1192	xfs_dabuf_t		*bp,		/* leaf buffer */
1193	int			first,		/* first entry to log */
1194	int			last)		/* last entry to log */
1195{
1196	__be16			*firstb;	/* pointer to first entry */
1197	__be16			*lastb;		/* pointer to last entry */
1198	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1199	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1200
1201	leaf = bp->data;
1202	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
1203	ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1204	firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1205	lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1206	xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1207		(uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1208}
1209
1210/*
1211 * Log the leaf entries indicated from a leaf1 or leafn block.
1212 */
1213void
1214xfs_dir2_leaf_log_ents(
1215	xfs_trans_t		*tp,		/* transaction pointer */
1216	xfs_dabuf_t		*bp,		/* leaf buffer */
1217	int			first,		/* first entry to log */
1218	int			last)		/* last entry to log */
1219{
1220	xfs_dir2_leaf_entry_t	*firstlep;	/* pointer to first entry */
1221	xfs_dir2_leaf_entry_t	*lastlep;	/* pointer to last entry */
1222	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1223
1224	leaf = bp->data;
1225	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1226	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1227	firstlep = &leaf->ents[first];
1228	lastlep = &leaf->ents[last];
1229	xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1230		(uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1231}
1232
1233/*
1234 * Log the header of the leaf1 or leafn block.
1235 */
1236void
1237xfs_dir2_leaf_log_header(
1238	xfs_trans_t		*tp,		/* transaction pointer */
1239	xfs_dabuf_t		*bp)		/* leaf buffer */
1240{
1241	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1242
1243	leaf = bp->data;
1244	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1245	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1246	xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1247		(uint)(sizeof(leaf->hdr) - 1));
1248}
1249
1250/*
1251 * Log the tail of the leaf1 block.
1252 */
1253STATIC void
1254xfs_dir2_leaf_log_tail(
1255	xfs_trans_t		*tp,		/* transaction pointer */
1256	xfs_dabuf_t		*bp)		/* leaf buffer */
1257{
1258	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1259	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1260	xfs_mount_t		*mp;		/* filesystem mount point */
1261
1262	mp = tp->t_mountp;
1263	leaf = bp->data;
1264	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
1265	ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1266	xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1267		(uint)(mp->m_dirblksize - 1));
1268}
1269
1270/*
1271 * Look up the entry referred to by args in the leaf format directory.
1272 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1273 * is also used by the node-format code.
1274 */
1275int
1276xfs_dir2_leaf_lookup(
1277	xfs_da_args_t		*args)		/* operation arguments */
1278{
1279	xfs_dabuf_t		*dbp;		/* data block buffer */
1280	xfs_dir2_data_entry_t	*dep;		/* data block entry */
1281	xfs_inode_t		*dp;		/* incore directory inode */
1282	int			error;		/* error return code */
1283	int			index;		/* found entry index */
1284	xfs_dabuf_t		*lbp;		/* leaf buffer */
1285	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1286	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1287	xfs_trans_t		*tp;		/* transaction pointer */
1288
1289	trace_xfs_dir2_leaf_lookup(args);
1290
1291	/*
1292	 * Look up name in the leaf block, returning both buffers and index.
1293	 */
1294	if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1295		return error;
1296	}
1297	tp = args->trans;
1298	dp = args->dp;
1299	xfs_dir2_leaf_check(dp, lbp);
1300	leaf = lbp->data;
1301	/*
1302	 * Get to the leaf entry and contained data entry address.
1303	 */
1304	lep = &leaf->ents[index];
1305	/*
1306	 * Point to the data entry.
1307	 */
1308	dep = (xfs_dir2_data_entry_t *)
1309	      ((char *)dbp->data +
1310	       xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1311	/*
1312	 * Return the found inode number & CI name if appropriate
1313	 */
1314	args->inumber = be64_to_cpu(dep->inumber);
1315	error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1316	xfs_da_brelse(tp, dbp);
1317	xfs_da_brelse(tp, lbp);
1318	return XFS_ERROR(error);
1319}
1320
1321/*
1322 * Look up name/hash in the leaf block.
1323 * Fill in indexp with the found index, and dbpp with the data buffer.
1324 * If not found dbpp will be NULL, and ENOENT comes back.
1325 * lbpp will always be filled in with the leaf buffer unless there's an error.
1326 */
1327static int					/* error */
1328xfs_dir2_leaf_lookup_int(
1329	xfs_da_args_t		*args,		/* operation arguments */
1330	xfs_dabuf_t		**lbpp,		/* out: leaf buffer */
1331	int			*indexp,	/* out: index in leaf block */
1332	xfs_dabuf_t		**dbpp)		/* out: data buffer */
1333{
1334	xfs_dir2_db_t		curdb = -1;	/* current data block number */
1335	xfs_dabuf_t		*dbp = NULL;	/* data buffer */
1336	xfs_dir2_data_entry_t	*dep;		/* data entry */
1337	xfs_inode_t		*dp;		/* incore directory inode */
1338	int			error;		/* error return code */
1339	int			index;		/* index in leaf block */
1340	xfs_dabuf_t		*lbp;		/* leaf buffer */
1341	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1342	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1343	xfs_mount_t		*mp;		/* filesystem mount point */
1344	xfs_dir2_db_t		newdb;		/* new data block number */
1345	xfs_trans_t		*tp;		/* transaction pointer */
1346	xfs_dir2_db_t		cidb = -1;	/* case match data block no. */
1347	enum xfs_dacmp		cmp;		/* name compare result */
1348
1349	dp = args->dp;
1350	tp = args->trans;
1351	mp = dp->i_mount;
1352	/*
1353	 * Read the leaf block into the buffer.
1354	 */
1355	error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1356							XFS_DATA_FORK);
1357	if (error)
1358		return error;
1359	*lbpp = lbp;
1360	leaf = lbp->data;
1361	xfs_dir2_leaf_check(dp, lbp);
1362	/*
1363	 * Look for the first leaf entry with our hash value.
1364	 */
1365	index = xfs_dir2_leaf_search_hash(args, lbp);
1366	/*
1367	 * Loop over all the entries with the right hash value
1368	 * looking to match the name.
1369	 */
1370	for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
1371				be32_to_cpu(lep->hashval) == args->hashval;
1372				lep++, index++) {
1373		/*
1374		 * Skip over stale leaf entries.
1375		 */
1376		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1377			continue;
1378		/*
1379		 * Get the new data block number.
1380		 */
1381		newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1382		/*
1383		 * If it's not the same as the old data block number,
1384		 * need to pitch the old one and read the new one.
1385		 */
1386		if (newdb != curdb) {
1387			if (dbp)
1388				xfs_da_brelse(tp, dbp);
1389			error = xfs_da_read_buf(tp, dp,
1390						xfs_dir2_db_to_da(mp, newdb),
1391						-1, &dbp, XFS_DATA_FORK);
1392			if (error) {
1393				xfs_da_brelse(tp, lbp);
1394				return error;
1395			}
1396			xfs_dir2_data_check(dp, dbp);
1397			curdb = newdb;
1398		}
1399		/*
1400		 * Point to the data entry.
1401		 */
1402		dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1403			xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1404		/*
1405		 * Compare name and if it's an exact match, return the index
1406		 * and buffer. If it's the first case-insensitive match, store
1407		 * the index and buffer and continue looking for an exact match.
1408		 */
1409		cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1410		if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1411			args->cmpresult = cmp;
1412			*indexp = index;
1413			/* case exact match: return the current buffer. */
1414			if (cmp == XFS_CMP_EXACT) {
1415				*dbpp = dbp;
1416				return 0;
1417			}
1418			cidb = curdb;
1419		}
1420	}
1421	ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1422	/*
1423	 * Here, we can only be doing a lookup (not a rename or remove).
1424	 * If a case-insensitive match was found earlier, re-read the
1425	 * appropriate data block if required and return it.
1426	 */
1427	if (args->cmpresult == XFS_CMP_CASE) {
1428		ASSERT(cidb != -1);
1429		if (cidb != curdb) {
1430			xfs_da_brelse(tp, dbp);
1431			error = xfs_da_read_buf(tp, dp,
1432						xfs_dir2_db_to_da(mp, cidb),
1433						-1, &dbp, XFS_DATA_FORK);
1434			if (error) {
1435				xfs_da_brelse(tp, lbp);
1436				return error;
1437			}
1438		}
1439		*dbpp = dbp;
1440		return 0;
1441	}
1442	/*
1443	 * No match found, return ENOENT.
1444	 */
1445	ASSERT(cidb == -1);
1446	if (dbp)
1447		xfs_da_brelse(tp, dbp);
1448	xfs_da_brelse(tp, lbp);
1449	return XFS_ERROR(ENOENT);
1450}
1451
1452/*
1453 * Remove an entry from a leaf format directory.
1454 */
1455int						/* error */
1456xfs_dir2_leaf_removename(
1457	xfs_da_args_t		*args)		/* operation arguments */
1458{
1459	__be16			*bestsp;	/* leaf block best freespace */
1460	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
1461	xfs_dir2_db_t		db;		/* data block number */
1462	xfs_dabuf_t		*dbp;		/* data block buffer */
1463	xfs_dir2_data_entry_t	*dep;		/* data entry structure */
1464	xfs_inode_t		*dp;		/* incore directory inode */
1465	int			error;		/* error return code */
1466	xfs_dir2_db_t		i;		/* temporary data block # */
1467	int			index;		/* index into leaf entries */
1468	xfs_dabuf_t		*lbp;		/* leaf buffer */
1469	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1470	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1471	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1472	xfs_mount_t		*mp;		/* filesystem mount point */
1473	int			needlog;	/* need to log data header */
1474	int			needscan;	/* need to rescan data frees */
1475	xfs_dir2_data_off_t	oldbest;	/* old value of best free */
1476	xfs_trans_t		*tp;		/* transaction pointer */
1477
1478	trace_xfs_dir2_leaf_removename(args);
1479
1480	/*
1481	 * Lookup the leaf entry, get the leaf and data blocks read in.
1482	 */
1483	if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1484		return error;
1485	}
1486	dp = args->dp;
1487	tp = args->trans;
1488	mp = dp->i_mount;
1489	leaf = lbp->data;
1490	hdr = dbp->data;
1491	xfs_dir2_data_check(dp, dbp);
1492	/*
1493	 * Point to the leaf entry, use that to point to the data entry.
1494	 */
1495	lep = &leaf->ents[index];
1496	db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1497	dep = (xfs_dir2_data_entry_t *)
1498	      ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1499	needscan = needlog = 0;
1500	oldbest = be16_to_cpu(hdr->bestfree[0].length);
1501	ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1502	bestsp = xfs_dir2_leaf_bests_p(ltp);
1503	ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1504	/*
1505	 * Mark the former data entry unused.
1506	 */
1507	xfs_dir2_data_make_free(tp, dbp,
1508		(xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1509		xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1510	/*
1511	 * We just mark the leaf entry stale by putting a null in it.
1512	 */
1513	be16_add_cpu(&leaf->hdr.stale, 1);
1514	xfs_dir2_leaf_log_header(tp, lbp);
1515	lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1516	xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1517	/*
1518	 * Scan the freespace in the data block again if necessary,
1519	 * log the data block header if necessary.
1520	 */
1521	if (needscan)
1522		xfs_dir2_data_freescan(mp, hdr, &needlog);
1523	if (needlog)
1524		xfs_dir2_data_log_header(tp, dbp);
1525	/*
1526	 * If the longest freespace in the data block has changed,
1527	 * put the new value in the bests table and log that.
1528	 */
1529	if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1530		bestsp[db] = hdr->bestfree[0].length;
1531		xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1532	}
1533	xfs_dir2_data_check(dp, dbp);
1534	/*
1535	 * If the data block is now empty then get rid of the data block.
1536	 */
1537	if (be16_to_cpu(hdr->bestfree[0].length) ==
1538	    mp->m_dirblksize - (uint)sizeof(*hdr)) {
1539		ASSERT(db != mp->m_dirdatablk);
1540		if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1541			/*
1542			 * Nope, can't get rid of it because it caused
1543			 * allocation of a bmap btree block to do so.
1544			 * Just go on, returning success, leaving the
1545			 * empty block in place.
1546			 */
1547			if (error == ENOSPC && args->total == 0) {
1548				xfs_da_buf_done(dbp);
1549				error = 0;
1550			}
1551			xfs_dir2_leaf_check(dp, lbp);
1552			xfs_da_buf_done(lbp);
1553			return error;
1554		}
1555		dbp = NULL;
1556		/*
1557		 * If this is the last data block then compact the
1558		 * bests table by getting rid of entries.
1559		 */
1560		if (db == be32_to_cpu(ltp->bestcount) - 1) {
1561			/*
1562			 * Look for the last active entry (i).
1563			 */
1564			for (i = db - 1; i > 0; i--) {
1565				if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1566					break;
1567			}
1568			/*
1569			 * Copy the table down so inactive entries at the
1570			 * end are removed.
1571			 */
1572			memmove(&bestsp[db - i], bestsp,
1573				(be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1574			be32_add_cpu(&ltp->bestcount, -(db - i));
1575			xfs_dir2_leaf_log_tail(tp, lbp);
1576			xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1577		} else
1578			bestsp[db] = cpu_to_be16(NULLDATAOFF);
1579	}
1580	/*
1581	 * If the data block was not the first one, drop it.
1582	 */
1583	else if (db != mp->m_dirdatablk && dbp != NULL) {
1584		xfs_da_buf_done(dbp);
1585		dbp = NULL;
1586	}
1587	xfs_dir2_leaf_check(dp, lbp);
1588	/*
1589	 * See if we can convert to block form.
1590	 */
1591	return xfs_dir2_leaf_to_block(args, lbp, dbp);
1592}
1593
1594/*
1595 * Replace the inode number in a leaf format directory entry.
1596 */
1597int						/* error */
1598xfs_dir2_leaf_replace(
1599	xfs_da_args_t		*args)		/* operation arguments */
1600{
1601	xfs_dabuf_t		*dbp;		/* data block buffer */
1602	xfs_dir2_data_entry_t	*dep;		/* data block entry */
1603	xfs_inode_t		*dp;		/* incore directory inode */
1604	int			error;		/* error return code */
1605	int			index;		/* index of leaf entry */
1606	xfs_dabuf_t		*lbp;		/* leaf buffer */
1607	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1608	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1609	xfs_trans_t		*tp;		/* transaction pointer */
1610
1611	trace_xfs_dir2_leaf_replace(args);
1612
1613	/*
1614	 * Look up the entry.
1615	 */
1616	if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1617		return error;
1618	}
1619	dp = args->dp;
1620	leaf = lbp->data;
1621	/*
1622	 * Point to the leaf entry, get data address from it.
1623	 */
1624	lep = &leaf->ents[index];
1625	/*
1626	 * Point to the data entry.
1627	 */
1628	dep = (xfs_dir2_data_entry_t *)
1629	      ((char *)dbp->data +
1630	       xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1631	ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1632	/*
1633	 * Put the new inode number in, log it.
1634	 */
1635	dep->inumber = cpu_to_be64(args->inumber);
1636	tp = args->trans;
1637	xfs_dir2_data_log_entry(tp, dbp, dep);
1638	xfs_da_buf_done(dbp);
1639	xfs_dir2_leaf_check(dp, lbp);
1640	xfs_da_brelse(tp, lbp);
1641	return 0;
1642}
1643
1644/*
1645 * Return index in the leaf block (lbp) which is either the first
1646 * one with this hash value, or if there are none, the insert point
1647 * for that hash value.
1648 */
1649int						/* index value */
1650xfs_dir2_leaf_search_hash(
1651	xfs_da_args_t		*args,		/* operation arguments */
1652	xfs_dabuf_t		*lbp)		/* leaf buffer */
1653{
1654	xfs_dahash_t		hash=0;		/* hash from this entry */
1655	xfs_dahash_t		hashwant;	/* hash value looking for */
1656	int			high;		/* high leaf index */
1657	int			low;		/* low leaf index */
1658	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1659	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
1660	int			mid=0;		/* current leaf index */
1661
1662	leaf = lbp->data;
1663#ifndef __KERNEL__
1664	if (!leaf->hdr.count)
1665		return 0;
1666#endif
1667	/*
1668	 * Note, the table cannot be empty, so we have to go through the loop.
1669	 * Binary search the leaf entries looking for our hash value.
1670	 */
1671	for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1672		hashwant = args->hashval;
1673	     low <= high; ) {
1674		mid = (low + high) >> 1;
1675		if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1676			break;
1677		if (hash < hashwant)
1678			low = mid + 1;
1679		else
1680			high = mid - 1;
1681	}
1682	/*
1683	 * Found one, back up through all the equal hash values.
1684	 */
1685	if (hash == hashwant) {
1686		while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1687			mid--;
1688		}
1689	}
1690	/*
1691	 * Need to point to an entry higher than ours.
1692	 */
1693	else if (hash < hashwant)
1694		mid++;
1695	return mid;
1696}
1697
1698/*
1699 * Trim off a trailing data block.  We know it's empty since the leaf
1700 * freespace table says so.
1701 */
1702int						/* error */
1703xfs_dir2_leaf_trim_data(
1704	xfs_da_args_t		*args,		/* operation arguments */
1705	xfs_dabuf_t		*lbp,		/* leaf buffer */
1706	xfs_dir2_db_t		db)		/* data block number */
1707{
1708	__be16			*bestsp;	/* leaf bests table */
1709	xfs_dabuf_t		*dbp;		/* data block buffer */
1710	xfs_inode_t		*dp;		/* incore directory inode */
1711	int			error;		/* error return value */
1712	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1713	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1714	xfs_mount_t		*mp;		/* filesystem mount point */
1715	xfs_trans_t		*tp;		/* transaction pointer */
1716
1717	dp = args->dp;
1718	mp = dp->i_mount;
1719	tp = args->trans;
1720	/*
1721	 * Read the offending data block.  We need its buffer.
1722	 */
1723	if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1724			XFS_DATA_FORK))) {
1725		return error;
1726	}
1727
1728	leaf = lbp->data;
1729	ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1730
1731#ifdef DEBUG
1732{
1733	struct xfs_dir2_data_hdr *hdr = dbp->data;
1734
1735	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
1736	ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1737	       mp->m_dirblksize - (uint)sizeof(*hdr));
1738	ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1739}
1740#endif
1741
1742	/*
1743	 * Get rid of the data block.
1744	 */
1745	if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1746		ASSERT(error != ENOSPC);
1747		xfs_da_brelse(tp, dbp);
1748		return error;
1749	}
1750	/*
1751	 * Eliminate the last bests entry from the table.
1752	 */
1753	bestsp = xfs_dir2_leaf_bests_p(ltp);
1754	be32_add_cpu(&ltp->bestcount, -1);
1755	memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1756	xfs_dir2_leaf_log_tail(tp, lbp);
1757	xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1758	return 0;
1759}
1760
1761static inline size_t
1762xfs_dir2_leaf_size(
1763	struct xfs_dir2_leaf_hdr	*hdr,
1764	int				counts)
1765{
1766	int			entries;
1767
1768	entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1769	return sizeof(xfs_dir2_leaf_hdr_t) +
1770	    entries * sizeof(xfs_dir2_leaf_entry_t) +
1771	    counts * sizeof(xfs_dir2_data_off_t) +
1772	    sizeof(xfs_dir2_leaf_tail_t);
1773}
1774
1775/*
1776 * Convert node form directory to leaf form directory.
1777 * The root of the node form dir needs to already be a LEAFN block.
1778 * Just return if we can't do anything.
1779 */
1780int						/* error */
1781xfs_dir2_node_to_leaf(
1782	xfs_da_state_t		*state)		/* directory operation state */
1783{
1784	xfs_da_args_t		*args;		/* operation arguments */
1785	xfs_inode_t		*dp;		/* incore directory inode */
1786	int			error;		/* error return code */
1787	xfs_dabuf_t		*fbp;		/* buffer for freespace block */
1788	xfs_fileoff_t		fo;		/* freespace file offset */
1789	xfs_dir2_free_t		*free;		/* freespace structure */
1790	xfs_dabuf_t		*lbp;		/* buffer for leaf block */
1791	xfs_dir2_leaf_tail_t	*ltp;		/* tail of leaf structure */
1792	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
1793	xfs_mount_t		*mp;		/* filesystem mount point */
1794	int			rval;		/* successful free trim? */
1795	xfs_trans_t		*tp;		/* transaction pointer */
1796
1797	/*
1798	 * There's more than a leaf level in the btree, so there must
1799	 * be multiple leafn blocks.  Give up.
1800	 */
1801	if (state->path.active > 1)
1802		return 0;
1803	args = state->args;
1804
1805	trace_xfs_dir2_node_to_leaf(args);
1806
1807	mp = state->mp;
1808	dp = args->dp;
1809	tp = args->trans;
1810	/*
1811	 * Get the last offset in the file.
1812	 */
1813	if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1814		return error;
1815	}
1816	fo -= mp->m_dirblkfsbs;
1817	/*
1818	 * If there are freespace blocks other than the first one,
1819	 * take this opportunity to remove trailing empty freespace blocks
1820	 * that may have been left behind during no-space-reservation
1821	 * operations.
1822	 */
1823	while (fo > mp->m_dirfreeblk) {
1824		if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1825			return error;
1826		}
1827		if (rval)
1828			fo -= mp->m_dirblkfsbs;
1829		else
1830			return 0;
1831	}
1832	/*
1833	 * Now find the block just before the freespace block.
1834	 */
1835	if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1836		return error;
1837	}
1838	/*
1839	 * If it's not the single leaf block, give up.
1840	 */
1841	if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1842		return 0;
1843	lbp = state->path.blk[0].bp;
1844	leaf = lbp->data;
1845	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1846	/*
1847	 * Read the freespace block.
1848	 */
1849	if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1850			XFS_DATA_FORK))) {
1851		return error;
1852	}
1853	free = fbp->data;
1854	ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
1855	ASSERT(!free->hdr.firstdb);
1856
1857	/*
1858	 * Now see if the leafn and free data will fit in a leaf1.
1859	 * If not, release the buffer and give up.
1860	 */
1861	if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1862			mp->m_dirblksize) {
1863		xfs_da_brelse(tp, fbp);
1864		return 0;
1865	}
1866
1867	/*
1868	 * If the leaf has any stale entries in it, compress them out.
1869	 * The compact routine will log the header.
1870	 */
1871	if (be16_to_cpu(leaf->hdr.stale))
1872		xfs_dir2_leaf_compact(args, lbp);
1873	else
1874		xfs_dir2_leaf_log_header(tp, lbp);
1875	leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1876	/*
1877	 * Set up the leaf tail from the freespace block.
1878	 */
1879	ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1880	ltp->bestcount = free->hdr.nvalid;
1881	/*
1882	 * Set up the leaf bests table.
1883	 */
1884	memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
1885		be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
1886	xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1887	xfs_dir2_leaf_log_tail(tp, lbp);
1888	xfs_dir2_leaf_check(dp, lbp);
1889	/*
1890	 * Get rid of the freespace block.
1891	 */
1892	error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1893	if (error) {
1894		/*
1895		 * This can't fail here because it can only happen when
1896		 * punching out the middle of an extent, and this is an
1897		 * isolated block.
1898		 */
1899		ASSERT(error != ENOSPC);
1900		return error;
1901	}
1902	fbp = NULL;
1903	/*
1904	 * Now see if we can convert the single-leaf directory
1905	 * down to a block form directory.
1906	 * This routine always kills the dabuf for the leaf, so
1907	 * eliminate it from the path.
1908	 */
1909	error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1910	state->path.blk[0].bp = NULL;
1911	return error;
1912}