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_log.h"
22#include "xfs_inum.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_dir2.h"
26#include "xfs_dmapi.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_attr_sf.h"
32#include "xfs_dinode.h"
33#include "xfs_inode.h"
34#include "xfs_inode_item.h"
35#include "xfs_dir2_data.h"
36#include "xfs_dir2_leaf.h"
37#include "xfs_dir2_block.h"
38#include "xfs_dir2_trace.h"
39#include "xfs_error.h"
40
41/*
42 * Local function prototypes.
43 */
44static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
45				    int last);
46static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
47static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
48				     int *entno);
49static int xfs_dir2_block_sort(const void *a, const void *b);
50
51static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
52
53/*
54 * One-time startup routine called from xfs_init().
55 */
56void
57xfs_dir_startup(void)
58{
59	xfs_dir_hash_dot = xfs_da_hashname(".", 1);
60	xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
61}
62
63/*
64 * Add an entry to a block directory.
65 */
66int						/* error */
67xfs_dir2_block_addname(
68	xfs_da_args_t		*args)		/* directory op arguments */
69{
70	xfs_dir2_data_free_t	*bf;		/* bestfree table in block */
71	xfs_dir2_block_t	*block;		/* directory block structure */
72	xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */
73	xfs_dabuf_t		*bp;		/* buffer for block */
74	xfs_dir2_block_tail_t	*btp;		/* block tail */
75	int			compact;	/* need to compact leaf ents */
76	xfs_dir2_data_entry_t	*dep;		/* block data entry */
77	xfs_inode_t		*dp;		/* directory inode */
78	xfs_dir2_data_unused_t	*dup;		/* block unused entry */
79	int			error;		/* error return value */
80	xfs_dir2_data_unused_t	*enddup=NULL;	/* unused at end of data */
81	xfs_dahash_t		hash;		/* hash value of found entry */
82	int			high;		/* high index for binary srch */
83	int			highstale;	/* high stale index */
84	int			lfloghigh=0;	/* last final leaf to log */
85	int			lfloglow=0;	/* first final leaf to log */
86	int			len;		/* length of the new entry */
87	int			low;		/* low index for binary srch */
88	int			lowstale;	/* low stale index */
89	int			mid=0;		/* midpoint for binary srch */
90	xfs_mount_t		*mp;		/* filesystem mount point */
91	int			needlog;	/* need to log header */
92	int			needscan;	/* need to rescan freespace */
93	__be16			*tagp;		/* pointer to tag value */
94	xfs_trans_t		*tp;		/* transaction structure */
95
96	xfs_dir2_trace_args("block_addname", args);
97	dp = args->dp;
98	tp = args->trans;
99	mp = dp->i_mount;
100	/*
101	 * Read the (one and only) directory block into dabuf bp.
102	 */
103	if ((error =
104	    xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
105		return error;
106	}
107	ASSERT(bp != NULL);
108	block = bp->data;
109	/*
110	 * Check the magic number, corrupted if wrong.
111	 */
112	if (unlikely(be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)) {
113		XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
114				     XFS_ERRLEVEL_LOW, mp, block);
115		xfs_da_brelse(tp, bp);
116		return XFS_ERROR(EFSCORRUPTED);
117	}
118	len = XFS_DIR2_DATA_ENTSIZE(args->namelen);
119	/*
120	 * Set up pointers to parts of the block.
121	 */
122	bf = block->hdr.bestfree;
123	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
124	blp = XFS_DIR2_BLOCK_LEAF_P(btp);
125	/*
126	 * No stale entries?  Need space for entry and new leaf.
127	 */
128	if (!btp->stale) {
129		/*
130		 * Tag just before the first leaf entry.
131		 */
132		tagp = (__be16 *)blp - 1;
133		/*
134		 * Data object just before the first leaf entry.
135		 */
136		enddup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
137		/*
138		 * If it's not free then can't do this add without cleaning up:
139		 * the space before the first leaf entry needs to be free so it
140		 * can be expanded to hold the pointer to the new entry.
141		 */
142		if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
143			dup = enddup = NULL;
144		/*
145		 * Check out the biggest freespace and see if it's the same one.
146		 */
147		else {
148			dup = (xfs_dir2_data_unused_t *)
149			      ((char *)block + be16_to_cpu(bf[0].offset));
150			if (dup == enddup) {
151				/*
152				 * It is the biggest freespace, is it too small
153				 * to hold the new leaf too?
154				 */
155				if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
156					/*
157					 * Yes, we use the second-largest
158					 * entry instead if it works.
159					 */
160					if (be16_to_cpu(bf[1].length) >= len)
161						dup = (xfs_dir2_data_unused_t *)
162						      ((char *)block +
163						       be16_to_cpu(bf[1].offset));
164					else
165						dup = NULL;
166				}
167			} else {
168				/*
169				 * Not the same free entry,
170				 * just check its length.
171				 */
172				if (be16_to_cpu(dup->length) < len) {
173					dup = NULL;
174				}
175			}
176		}
177		compact = 0;
178	}
179	/*
180	 * If there are stale entries we'll use one for the leaf.
181	 * Is the biggest entry enough to avoid compaction?
182	 */
183	else if (be16_to_cpu(bf[0].length) >= len) {
184		dup = (xfs_dir2_data_unused_t *)
185		      ((char *)block + be16_to_cpu(bf[0].offset));
186		compact = 0;
187	}
188	/*
189	 * Will need to compact to make this work.
190	 */
191	else {
192		/*
193		 * Tag just before the first leaf entry.
194		 */
195		tagp = (__be16 *)blp - 1;
196		/*
197		 * Data object just before the first leaf entry.
198		 */
199		dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
200		/*
201		 * If it's not free then the data will go where the
202		 * leaf data starts now, if it works at all.
203		 */
204		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
205			if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
206			    (uint)sizeof(*blp) < len)
207				dup = NULL;
208		} else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
209			dup = NULL;
210		else
211			dup = (xfs_dir2_data_unused_t *)blp;
212		compact = 1;
213	}
214	/*
215	 * If this isn't a real add, we're done with the buffer.
216	 */
217	if (args->justcheck)
218		xfs_da_brelse(tp, bp);
219	/*
220	 * If we don't have space for the new entry & leaf ...
221	 */
222	if (!dup) {
223		/*
224		 * Not trying to actually do anything, or don't have
225		 * a space reservation: return no-space.
226		 */
227		if (args->justcheck || args->total == 0)
228			return XFS_ERROR(ENOSPC);
229		/*
230		 * Convert to the next larger format.
231		 * Then add the new entry in that format.
232		 */
233		error = xfs_dir2_block_to_leaf(args, bp);
234		xfs_da_buf_done(bp);
235		if (error)
236			return error;
237		return xfs_dir2_leaf_addname(args);
238	}
239	/*
240	 * Just checking, and it would work, so say so.
241	 */
242	if (args->justcheck)
243		return 0;
244	needlog = needscan = 0;
245	if (compact) {
246		int	fromidx;		/* source leaf index */
247		int	toidx;			/* target leaf index */
248
249		for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
250			highstale = lfloghigh = -1;
251		     fromidx >= 0;
252		     fromidx--) {
253			if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
254				if (highstale == -1)
255					highstale = toidx;
256				else {
257					if (lfloghigh == -1)
258						lfloghigh = toidx;
259					continue;
260				}
261			}
262			if (fromidx < toidx)
263				blp[toidx] = blp[fromidx];
264			toidx--;
265		}
266		lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
267		lfloghigh -= be32_to_cpu(btp->stale) - 1;
268		be32_add(&btp->count, -(be32_to_cpu(btp->stale) - 1));
269		xfs_dir2_data_make_free(tp, bp,
270			(xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
271			(xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
272			&needlog, &needscan);
273		blp += be32_to_cpu(btp->stale) - 1;
274		btp->stale = cpu_to_be32(1);
275		/*
276		 * If we now need to rebuild the bestfree map, do so.
277		 * This needs to happen before the next call to use_free.
278		 */
279		if (needscan) {
280			xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
281			needscan = 0;
282		}
283	}
284	/*
285	 * Set leaf logging boundaries to impossible state.
286	 * For the no-stale case they're set explicitly.
287	 */
288	else if (btp->stale) {
289		lfloglow = be32_to_cpu(btp->count);
290		lfloghigh = -1;
291	}
292	/*
293	 * Find the slot that's first lower than our hash value, -1 if none.
294	 */
295	for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
296		mid = (low + high) >> 1;
297		if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
298			break;
299		if (hash < args->hashval)
300			low = mid + 1;
301		else
302			high = mid - 1;
303	}
304	while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
305		mid--;
306	}
307	/*
308	 * No stale entries, will use enddup space to hold new leaf.
309	 */
310	if (!btp->stale) {
311		/*
312		 * Mark the space needed for the new leaf entry, now in use.
313		 */
314		xfs_dir2_data_use_free(tp, bp, enddup,
315			(xfs_dir2_data_aoff_t)
316			((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
317			 sizeof(*blp)),
318			(xfs_dir2_data_aoff_t)sizeof(*blp),
319			&needlog, &needscan);
320		/*
321		 * Update the tail (entry count).
322		 */
323		be32_add(&btp->count, 1);
324		/*
325		 * If we now need to rebuild the bestfree map, do so.
326		 * This needs to happen before the next call to use_free.
327		 */
328		if (needscan) {
329			xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
330				&needlog);
331			needscan = 0;
332		}
333		/*
334		 * Adjust pointer to the first leaf entry, we're about to move
335		 * the table up one to open up space for the new leaf entry.
336		 * Then adjust our index to match.
337		 */
338		blp--;
339		mid++;
340		if (mid)
341			memmove(blp, &blp[1], mid * sizeof(*blp));
342		lfloglow = 0;
343		lfloghigh = mid;
344	}
345	/*
346	 * Use a stale leaf for our new entry.
347	 */
348	else {
349		for (lowstale = mid;
350		     lowstale >= 0 &&
351			be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
352		     lowstale--)
353			continue;
354		for (highstale = mid + 1;
355		     highstale < be32_to_cpu(btp->count) &&
356			be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
357			(lowstale < 0 || mid - lowstale > highstale - mid);
358		     highstale++)
359			continue;
360		/*
361		 * Move entries toward the low-numbered stale entry.
362		 */
363		if (lowstale >= 0 &&
364		    (highstale == be32_to_cpu(btp->count) ||
365		     mid - lowstale <= highstale - mid)) {
366			if (mid - lowstale)
367				memmove(&blp[lowstale], &blp[lowstale + 1],
368					(mid - lowstale) * sizeof(*blp));
369			lfloglow = MIN(lowstale, lfloglow);
370			lfloghigh = MAX(mid, lfloghigh);
371		}
372		/*
373		 * Move entries toward the high-numbered stale entry.
374		 */
375		else {
376			ASSERT(highstale < be32_to_cpu(btp->count));
377			mid++;
378			if (highstale - mid)
379				memmove(&blp[mid + 1], &blp[mid],
380					(highstale - mid) * sizeof(*blp));
381			lfloglow = MIN(mid, lfloglow);
382			lfloghigh = MAX(highstale, lfloghigh);
383		}
384		be32_add(&btp->stale, -1);
385	}
386	/*
387	 * Point to the new data entry.
388	 */
389	dep = (xfs_dir2_data_entry_t *)dup;
390	/*
391	 * Fill in the leaf entry.
392	 */
393	blp[mid].hashval = cpu_to_be32(args->hashval);
394	blp[mid].address = cpu_to_be32(XFS_DIR2_BYTE_TO_DATAPTR(mp,
395				(char *)dep - (char *)block));
396	xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
397	/*
398	 * Mark space for the data entry used.
399	 */
400	xfs_dir2_data_use_free(tp, bp, dup,
401		(xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
402		(xfs_dir2_data_aoff_t)len, &needlog, &needscan);
403	/*
404	 * Create the new data entry.
405	 */
406	dep->inumber = cpu_to_be64(args->inumber);
407	dep->namelen = args->namelen;
408	memcpy(dep->name, args->name, args->namelen);
409	tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
410	*tagp = cpu_to_be16((char *)dep - (char *)block);
411	/*
412	 * Clean up the bestfree array and log the header, tail, and entry.
413	 */
414	if (needscan)
415		xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
416	if (needlog)
417		xfs_dir2_data_log_header(tp, bp);
418	xfs_dir2_block_log_tail(tp, bp);
419	xfs_dir2_data_log_entry(tp, bp, dep);
420	xfs_dir2_data_check(dp, bp);
421	xfs_da_buf_done(bp);
422	return 0;
423}
424
425/*
426 * Readdir for block directories.
427 */
428int						/* error */
429xfs_dir2_block_getdents(
430	xfs_trans_t		*tp,		/* transaction (NULL) */
431	xfs_inode_t		*dp,		/* incore inode */
432	uio_t			*uio,		/* caller's buffer control */
433	int			*eofp,		/* eof reached? (out) */
434	xfs_dirent_t		*dbp,		/* caller's buffer */
435	xfs_dir2_put_t		put)		/* abi's formatting function */
436{
437	xfs_dir2_block_t	*block;		/* directory block structure */
438	xfs_dabuf_t		*bp;		/* buffer for block */
439	xfs_dir2_block_tail_t	*btp;		/* block tail */
440	xfs_dir2_data_entry_t	*dep;		/* block data entry */
441	xfs_dir2_data_unused_t	*dup;		/* block unused entry */
442	char			*endptr;	/* end of the data entries */
443	int			error;		/* error return value */
444	xfs_mount_t		*mp;		/* filesystem mount point */
445	xfs_dir2_put_args_t	p;		/* arg package for put rtn */
446	char			*ptr;		/* current data entry */
447	int			wantoff;	/* starting block offset */
448
449	mp = dp->i_mount;
450	/*
451	 * If the block number in the offset is out of range, we're done.
452	 */
453	if (XFS_DIR2_DATAPTR_TO_DB(mp, uio->uio_offset) > mp->m_dirdatablk) {
454		*eofp = 1;
455		return 0;
456	}
457	/*
458	 * Can't read the block, give up, else get dabuf in bp.
459	 */
460	if ((error =
461	    xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
462		return error;
463	}
464	ASSERT(bp != NULL);
465	/*
466	 * Extract the byte offset we start at from the seek pointer.
467	 * We'll skip entries before this.
468	 */
469	wantoff = XFS_DIR2_DATAPTR_TO_OFF(mp, uio->uio_offset);
470	block = bp->data;
471	xfs_dir2_data_check(dp, bp);
472	/*
473	 * Set up values for the loop.
474	 */
475	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
476	ptr = (char *)block->u;
477	endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
478	p.dbp = dbp;
479	p.put = put;
480	p.uio = uio;
481	/*
482	 * Loop over the data portion of the block.
483	 * Each object is a real entry (dep) or an unused one (dup).
484	 */
485	while (ptr < endptr) {
486		dup = (xfs_dir2_data_unused_t *)ptr;
487		/*
488		 * Unused, skip it.
489		 */
490		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
491			ptr += be16_to_cpu(dup->length);
492			continue;
493		}
494
495		dep = (xfs_dir2_data_entry_t *)ptr;
496
497		/*
498		 * Bump pointer for the next iteration.
499		 */
500		ptr += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
501		/*
502		 * The entry is before the desired starting point, skip it.
503		 */
504		if ((char *)dep - (char *)block < wantoff)
505			continue;
506		/*
507		 * Set up argument structure for put routine.
508		 */
509		p.namelen = dep->namelen;
510
511		p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
512						    ptr - (char *)block);
513		p.ino = be64_to_cpu(dep->inumber);
514#if XFS_BIG_INUMS
515		p.ino += mp->m_inoadd;
516#endif
517		p.name = (char *)dep->name;
518
519		/*
520		 * Put the entry in the caller's buffer.
521		 */
522		error = p.put(&p);
523
524		/*
525		 * If it didn't fit, set the final offset to here & return.
526		 */
527		if (!p.done) {
528			uio->uio_offset =
529				XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
530					(char *)dep - (char *)block);
531			xfs_da_brelse(tp, bp);
532			return error;
533		}
534	}
535
536	/*
537	 * Reached the end of the block.
538	 * Set the offset to a non-existent block 1 and return.
539	 */
540	*eofp = 1;
541
542	uio->uio_offset =
543		XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk + 1, 0);
544
545	xfs_da_brelse(tp, bp);
546
547	return 0;
548}
549
550/*
551 * Log leaf entries from the block.
552 */
553static void
554xfs_dir2_block_log_leaf(
555	xfs_trans_t		*tp,		/* transaction structure */
556	xfs_dabuf_t		*bp,		/* block buffer */
557	int			first,		/* index of first logged leaf */
558	int			last)		/* index of last logged leaf */
559{
560	xfs_dir2_block_t	*block;		/* directory block structure */
561	xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */
562	xfs_dir2_block_tail_t	*btp;		/* block tail */
563	xfs_mount_t		*mp;		/* filesystem mount point */
564
565	mp = tp->t_mountp;
566	block = bp->data;
567	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
568	blp = XFS_DIR2_BLOCK_LEAF_P(btp);
569	xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
570		(uint)((char *)&blp[last + 1] - (char *)block - 1));
571}
572
573/*
574 * Log the block tail.
575 */
576static void
577xfs_dir2_block_log_tail(
578	xfs_trans_t		*tp,		/* transaction structure */
579	xfs_dabuf_t		*bp)		/* block buffer */
580{
581	xfs_dir2_block_t	*block;		/* directory block structure */
582	xfs_dir2_block_tail_t	*btp;		/* block tail */
583	xfs_mount_t		*mp;		/* filesystem mount point */
584
585	mp = tp->t_mountp;
586	block = bp->data;
587	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
588	xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
589		(uint)((char *)(btp + 1) - (char *)block - 1));
590}
591
592/*
593 * Look up an entry in the block.  This is the external routine,
594 * xfs_dir2_block_lookup_int does the real work.
595 */
596int						/* error */
597xfs_dir2_block_lookup(
598	xfs_da_args_t		*args)		/* dir lookup arguments */
599{
600	xfs_dir2_block_t	*block;		/* block structure */
601	xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */
602	xfs_dabuf_t		*bp;		/* block buffer */
603	xfs_dir2_block_tail_t	*btp;		/* block tail */
604	xfs_dir2_data_entry_t	*dep;		/* block data entry */
605	xfs_inode_t		*dp;		/* incore inode */
606	int			ent;		/* entry index */
607	int			error;		/* error return value */
608	xfs_mount_t		*mp;		/* filesystem mount point */
609
610	xfs_dir2_trace_args("block_lookup", args);
611	/*
612	 * Get the buffer, look up the entry.
613	 * If not found (ENOENT) then return, have no buffer.
614	 */
615	if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
616		return error;
617	dp = args->dp;
618	mp = dp->i_mount;
619	block = bp->data;
620	xfs_dir2_data_check(dp, bp);
621	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
622	blp = XFS_DIR2_BLOCK_LEAF_P(btp);
623	/*
624	 * Get the offset from the leaf entry, to point to the data.
625	 */
626	dep = (xfs_dir2_data_entry_t *)
627	      ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(blp[ent].address)));
628	/*
629	 * Fill in inode number, release the block.
630	 */
631	args->inumber = be64_to_cpu(dep->inumber);
632	xfs_da_brelse(args->trans, bp);
633	return XFS_ERROR(EEXIST);
634}
635
636/*
637 * Internal block lookup routine.
638 */
639static int					/* error */
640xfs_dir2_block_lookup_int(
641	xfs_da_args_t		*args,		/* dir lookup arguments */
642	xfs_dabuf_t		**bpp,		/* returned block buffer */
643	int			*entno)		/* returned entry number */
644{
645	xfs_dir2_dataptr_t	addr;		/* data entry address */
646	xfs_dir2_block_t	*block;		/* block structure */
647	xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */
648	xfs_dabuf_t		*bp;		/* block buffer */
649	xfs_dir2_block_tail_t	*btp;		/* block tail */
650	xfs_dir2_data_entry_t	*dep;		/* block data entry */
651	xfs_inode_t		*dp;		/* incore inode */
652	int			error;		/* error return value */
653	xfs_dahash_t		hash;		/* found hash value */
654	int			high;		/* binary search high index */
655	int			low;		/* binary search low index */
656	int			mid;		/* binary search current idx */
657	xfs_mount_t		*mp;		/* filesystem mount point */
658	xfs_trans_t		*tp;		/* transaction pointer */
659
660	dp = args->dp;
661	tp = args->trans;
662	mp = dp->i_mount;
663	/*
664	 * Read the buffer, return error if we can't get it.
665	 */
666	if ((error =
667	    xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
668		return error;
669	}
670	ASSERT(bp != NULL);
671	block = bp->data;
672	xfs_dir2_data_check(dp, bp);
673	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
674	blp = XFS_DIR2_BLOCK_LEAF_P(btp);
675	/*
676	 * Loop doing a binary search for our hash value.
677	 * Find our entry, ENOENT if it's not there.
678	 */
679	for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
680		ASSERT(low <= high);
681		mid = (low + high) >> 1;
682		if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
683			break;
684		if (hash < args->hashval)
685			low = mid + 1;
686		else
687			high = mid - 1;
688		if (low > high) {
689			ASSERT(args->oknoent);
690			xfs_da_brelse(tp, bp);
691			return XFS_ERROR(ENOENT);
692		}
693	}
694	/*
695	 * Back up to the first one with the right hash value.
696	 */
697	while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
698		mid--;
699	}
700	/*
701	 * Now loop forward through all the entries with the
702	 * right hash value looking for our name.
703	 */
704	do {
705		if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
706			continue;
707		/*
708		 * Get pointer to the entry from the leaf.
709		 */
710		dep = (xfs_dir2_data_entry_t *)
711			((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, addr));
712		/*
713		 * Compare, if it's right give back buffer & entry number.
714		 */
715		if (dep->namelen == args->namelen &&
716		    dep->name[0] == args->name[0] &&
717		    memcmp(dep->name, args->name, args->namelen) == 0) {
718			*bpp = bp;
719			*entno = mid;
720			return 0;
721		}
722	} while (++mid < be32_to_cpu(btp->count) && be32_to_cpu(blp[mid].hashval) == hash);
723	/*
724	 * No match, release the buffer and return ENOENT.
725	 */
726	ASSERT(args->oknoent);
727	xfs_da_brelse(tp, bp);
728	return XFS_ERROR(ENOENT);
729}
730
731/*
732 * Remove an entry from a block format directory.
733 * If that makes the block small enough to fit in shortform, transform it.
734 */
735int						/* error */
736xfs_dir2_block_removename(
737	xfs_da_args_t		*args)		/* directory operation args */
738{
739	xfs_dir2_block_t	*block;		/* block structure */
740	xfs_dir2_leaf_entry_t	*blp;		/* block leaf pointer */
741	xfs_dabuf_t		*bp;		/* block buffer */
742	xfs_dir2_block_tail_t	*btp;		/* block tail */
743	xfs_dir2_data_entry_t	*dep;		/* block data entry */
744	xfs_inode_t		*dp;		/* incore inode */
745	int			ent;		/* block leaf entry index */
746	int			error;		/* error return value */
747	xfs_mount_t		*mp;		/* filesystem mount point */
748	int			needlog;	/* need to log block header */
749	int			needscan;	/* need to fixup bestfree */
750	xfs_dir2_sf_hdr_t	sfh;		/* shortform header */
751	int			size;		/* shortform size */
752	xfs_trans_t		*tp;		/* transaction pointer */
753
754	xfs_dir2_trace_args("block_removename", args);
755	/*
756	 * Look up the entry in the block.  Gets the buffer and entry index.
757	 * It will always be there, the vnodeops level does a lookup first.
758	 */
759	if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
760		return error;
761	}
762	dp = args->dp;
763	tp = args->trans;
764	mp = dp->i_mount;
765	block = bp->data;
766	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
767	blp = XFS_DIR2_BLOCK_LEAF_P(btp);
768	/*
769	 * Point to the data entry using the leaf entry.
770	 */
771	dep = (xfs_dir2_data_entry_t *)
772	      ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(blp[ent].address)));
773	/*
774	 * Mark the data entry's space free.
775	 */
776	needlog = needscan = 0;
777	xfs_dir2_data_make_free(tp, bp,
778		(xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
779		XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
780	/*
781	 * Fix up the block tail.
782	 */
783	be32_add(&btp->stale, 1);
784	xfs_dir2_block_log_tail(tp, bp);
785	/*
786	 * Remove the leaf entry by marking it stale.
787	 */
788	blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
789	xfs_dir2_block_log_leaf(tp, bp, ent, ent);
790	/*
791	 * Fix up bestfree, log the header if necessary.
792	 */
793	if (needscan)
794		xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
795	if (needlog)
796		xfs_dir2_data_log_header(tp, bp);
797	xfs_dir2_data_check(dp, bp);
798	/*
799	 * See if the size as a shortform is good enough.
800	 */
801	if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
802	    XFS_IFORK_DSIZE(dp)) {
803		xfs_da_buf_done(bp);
804		return 0;
805	}
806	/*
807	 * If it works, do the conversion.
808	 */
809	return xfs_dir2_block_to_sf(args, bp, size, &sfh);
810}
811
812/*
813 * Replace an entry in a V2 block directory.
814 * Change the inode number to the new value.
815 */
816int						/* error */
817xfs_dir2_block_replace(
818	xfs_da_args_t		*args)		/* directory operation args */
819{
820	xfs_dir2_block_t	*block;		/* block structure */
821	xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */
822	xfs_dabuf_t		*bp;		/* block buffer */
823	xfs_dir2_block_tail_t	*btp;		/* block tail */
824	xfs_dir2_data_entry_t	*dep;		/* block data entry */
825	xfs_inode_t		*dp;		/* incore inode */
826	int			ent;		/* leaf entry index */
827	int			error;		/* error return value */
828	xfs_mount_t		*mp;		/* filesystem mount point */
829
830	xfs_dir2_trace_args("block_replace", args);
831	/*
832	 * Lookup the entry in the directory.  Get buffer and entry index.
833	 * This will always succeed since the caller has already done a lookup.
834	 */
835	if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
836		return error;
837	}
838	dp = args->dp;
839	mp = dp->i_mount;
840	block = bp->data;
841	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
842	blp = XFS_DIR2_BLOCK_LEAF_P(btp);
843	/*
844	 * Point to the data entry we need to change.
845	 */
846	dep = (xfs_dir2_data_entry_t *)
847	      ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(blp[ent].address)));
848	ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
849	/*
850	 * Change the inode number to the new value.
851	 */
852	dep->inumber = cpu_to_be64(args->inumber);
853	xfs_dir2_data_log_entry(args->trans, bp, dep);
854	xfs_dir2_data_check(dp, bp);
855	xfs_da_buf_done(bp);
856	return 0;
857}
858
859/*
860 * Qsort comparison routine for the block leaf entries.
861 */
862static int					/* sort order */
863xfs_dir2_block_sort(
864	const void			*a,	/* first leaf entry */
865	const void			*b)	/* second leaf entry */
866{
867	const xfs_dir2_leaf_entry_t	*la;	/* first leaf entry */
868	const xfs_dir2_leaf_entry_t	*lb;	/* second leaf entry */
869
870	la = a;
871	lb = b;
872	return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
873		(be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
874}
875
876/*
877 * Convert a V2 leaf directory to a V2 block directory if possible.
878 */
879int						/* error */
880xfs_dir2_leaf_to_block(
881	xfs_da_args_t		*args,		/* operation arguments */
882	xfs_dabuf_t		*lbp,		/* leaf buffer */
883	xfs_dabuf_t		*dbp)		/* data buffer */
884{
885	__be16			*bestsp;	/* leaf bests table */
886	xfs_dir2_block_t	*block;		/* block structure */
887	xfs_dir2_block_tail_t	*btp;		/* block tail */
888	xfs_inode_t		*dp;		/* incore directory inode */
889	xfs_dir2_data_unused_t	*dup;		/* unused data entry */
890	int			error;		/* error return value */
891	int			from;		/* leaf from index */
892	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
893	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
894	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
895	xfs_mount_t		*mp;		/* file system mount point */
896	int			needlog;	/* need to log data header */
897	int			needscan;	/* need to scan for bestfree */
898	xfs_dir2_sf_hdr_t	sfh;		/* shortform header */
899	int			size;		/* bytes used */
900	__be16			*tagp;		/* end of entry (tag) */
901	int			to;		/* block/leaf to index */
902	xfs_trans_t		*tp;		/* transaction pointer */
903
904	xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
905	dp = args->dp;
906	tp = args->trans;
907	mp = dp->i_mount;
908	leaf = lbp->data;
909	ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
910	ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
911	/*
912	 * If there are data blocks other than the first one, take this
913	 * opportunity to remove trailing empty data blocks that may have
914	 * been left behind during no-space-reservation operations.
915	 * These will show up in the leaf bests table.
916	 */
917	while (dp->i_d.di_size > mp->m_dirblksize) {
918		bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
919		if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
920		    mp->m_dirblksize - (uint)sizeof(block->hdr)) {
921			if ((error =
922			    xfs_dir2_leaf_trim_data(args, lbp,
923				    (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
924				goto out;
925		} else {
926			error = 0;
927			goto out;
928		}
929	}
930	/*
931	 * Read the data block if we don't already have it, give up if it fails.
932	 */
933	if (dbp == NULL &&
934	    (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
935		    XFS_DATA_FORK))) {
936		goto out;
937	}
938	block = dbp->data;
939	ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
940	/*
941	 * Size of the "leaf" area in the block.
942	 */
943	size = (uint)sizeof(block->tail) +
944	       (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
945	/*
946	 * Look at the last data entry.
947	 */
948	tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
949	dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
950	/*
951	 * If it's not free or is too short we can't do it.
952	 */
953	if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
954	    be16_to_cpu(dup->length) < size) {
955		error = 0;
956		goto out;
957	}
958	/*
959	 * Start converting it to block form.
960	 */
961	block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
962	needlog = 1;
963	needscan = 0;
964	/*
965	 * Use up the space at the end of the block (blp/btp).
966	 */
967	xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
968		&needlog, &needscan);
969	/*
970	 * Initialize the block tail.
971	 */
972	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
973	btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
974	btp->stale = 0;
975	xfs_dir2_block_log_tail(tp, dbp);
976	/*
977	 * Initialize the block leaf area.  We compact out stale entries.
978	 */
979	lep = XFS_DIR2_BLOCK_LEAF_P(btp);
980	for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
981		if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
982			continue;
983		lep[to++] = leaf->ents[from];
984	}
985	ASSERT(to == be32_to_cpu(btp->count));
986	xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
987	/*
988	 * Scan the bestfree if we need it and log the data block header.
989	 */
990	if (needscan)
991		xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
992	if (needlog)
993		xfs_dir2_data_log_header(tp, dbp);
994	/*
995	 * Pitch the old leaf block.
996	 */
997	error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
998	lbp = NULL;
999	if (error) {
1000		goto out;
1001	}
1002	/*
1003	 * Now see if the resulting block can be shrunken to shortform.
1004	 */
1005	if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
1006	    XFS_IFORK_DSIZE(dp)) {
1007		error = 0;
1008		goto out;
1009	}
1010	return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1011out:
1012	if (lbp)
1013		xfs_da_buf_done(lbp);
1014	if (dbp)
1015		xfs_da_buf_done(dbp);
1016	return error;
1017}
1018
1019/*
1020 * Convert the shortform directory to block form.
1021 */
1022int						/* error */
1023xfs_dir2_sf_to_block(
1024	xfs_da_args_t		*args)		/* operation arguments */
1025{
1026	xfs_dir2_db_t		blkno;		/* dir-relative block # (0) */
1027	xfs_dir2_block_t	*block;		/* block structure */
1028	xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */
1029	xfs_dabuf_t		*bp;		/* block buffer */
1030	xfs_dir2_block_tail_t	*btp;		/* block tail pointer */
1031	char			*buf;		/* sf buffer */
1032	int			buf_len;
1033	xfs_dir2_data_entry_t	*dep;		/* data entry pointer */
1034	xfs_inode_t		*dp;		/* incore directory inode */
1035	int			dummy;		/* trash */
1036	xfs_dir2_data_unused_t	*dup;		/* unused entry pointer */
1037	int			endoffset;	/* end of data objects */
1038	int			error;		/* error return value */
1039	int			i;		/* index */
1040	xfs_mount_t		*mp;		/* filesystem mount point */
1041	int			needlog;	/* need to log block header */
1042	int			needscan;	/* need to scan block freespc */
1043	int			newoffset;	/* offset from current entry */
1044	int			offset;		/* target block offset */
1045	xfs_dir2_sf_entry_t	*sfep;		/* sf entry pointer */
1046	xfs_dir2_sf_t		*sfp;		/* shortform structure */
1047	__be16			*tagp;		/* end of data entry */
1048	xfs_trans_t		*tp;		/* transaction pointer */
1049
1050	xfs_dir2_trace_args("sf_to_block", args);
1051	dp = args->dp;
1052	tp = args->trans;
1053	mp = dp->i_mount;
1054	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1055	/*
1056	 * Bomb out if the shortform directory is way too short.
1057	 */
1058	if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1059		ASSERT(XFS_FORCED_SHUTDOWN(mp));
1060		return XFS_ERROR(EIO);
1061	}
1062	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1063	ASSERT(dp->i_df.if_u1.if_data != NULL);
1064	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1065	ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
1066	/*
1067	 * Copy the directory into the stack buffer.
1068	 * Then pitch the incore inode data so we can make extents.
1069	 */
1070
1071	buf_len = dp->i_df.if_bytes;
1072	buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1073
1074	memcpy(buf, sfp, dp->i_df.if_bytes);
1075	xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1076	dp->i_d.di_size = 0;
1077	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1078	/*
1079	 * Reset pointer - old sfp is gone.
1080	 */
1081	sfp = (xfs_dir2_sf_t *)buf;
1082	/*
1083	 * Add block 0 to the inode.
1084	 */
1085	error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1086	if (error) {
1087		kmem_free(buf, buf_len);
1088		return error;
1089	}
1090	/*
1091	 * Initialize the data block.
1092	 */
1093	error = xfs_dir2_data_init(args, blkno, &bp);
1094	if (error) {
1095		kmem_free(buf, buf_len);
1096		return error;
1097	}
1098	block = bp->data;
1099	block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
1100	/*
1101	 * Compute size of block "tail" area.
1102	 */
1103	i = (uint)sizeof(*btp) +
1104	    (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1105	/*
1106	 * The whole thing is initialized to free by the init routine.
1107	 * Say we're using the leaf and tail area.
1108	 */
1109	dup = (xfs_dir2_data_unused_t *)block->u;
1110	needlog = needscan = 0;
1111	xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1112		&needscan);
1113	ASSERT(needscan == 0);
1114	/*
1115	 * Fill in the tail.
1116	 */
1117	btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
1118	btp->count = cpu_to_be32(sfp->hdr.count + 2);	/* ., .. */
1119	btp->stale = 0;
1120	blp = XFS_DIR2_BLOCK_LEAF_P(btp);
1121	endoffset = (uint)((char *)blp - (char *)block);
1122	/*
1123	 * Remove the freespace, we'll manage it.
1124	 */
1125	xfs_dir2_data_use_free(tp, bp, dup,
1126		(xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
1127		be16_to_cpu(dup->length), &needlog, &needscan);
1128	/*
1129	 * Create entry for .
1130	 */
1131	dep = (xfs_dir2_data_entry_t *)
1132	      ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
1133	dep->inumber = cpu_to_be64(dp->i_ino);
1134	dep->namelen = 1;
1135	dep->name[0] = '.';
1136	tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1137	*tagp = cpu_to_be16((char *)dep - (char *)block);
1138	xfs_dir2_data_log_entry(tp, bp, dep);
1139	blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1140	blp[0].address = cpu_to_be32(XFS_DIR2_BYTE_TO_DATAPTR(mp,
1141				(char *)dep - (char *)block));
1142	/*
1143	 * Create entry for ..
1144	 */
1145	dep = (xfs_dir2_data_entry_t *)
1146		((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
1147	dep->inumber = cpu_to_be64(XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent));
1148	dep->namelen = 2;
1149	dep->name[0] = dep->name[1] = '.';
1150	tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1151	*tagp = cpu_to_be16((char *)dep - (char *)block);
1152	xfs_dir2_data_log_entry(tp, bp, dep);
1153	blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1154	blp[1].address = cpu_to_be32(XFS_DIR2_BYTE_TO_DATAPTR(mp,
1155				(char *)dep - (char *)block));
1156	offset = XFS_DIR2_DATA_FIRST_OFFSET;
1157	/*
1158	 * Loop over existing entries, stuff them in.
1159	 */
1160	if ((i = 0) == sfp->hdr.count)
1161		sfep = NULL;
1162	else
1163		sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
1164	/*
1165	 * Need to preserve the existing offset values in the sf directory.
1166	 * Insert holes (unused entries) where necessary.
1167	 */
1168	while (offset < endoffset) {
1169		/*
1170		 * sfep is null when we reach the end of the list.
1171		 */
1172		if (sfep == NULL)
1173			newoffset = endoffset;
1174		else
1175			newoffset = XFS_DIR2_SF_GET_OFFSET(sfep);
1176		/*
1177		 * There should be a hole here, make one.
1178		 */
1179		if (offset < newoffset) {
1180			dup = (xfs_dir2_data_unused_t *)
1181			      ((char *)block + offset);
1182			dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1183			dup->length = cpu_to_be16(newoffset - offset);
1184			*XFS_DIR2_DATA_UNUSED_TAG_P(dup) = cpu_to_be16(
1185				((char *)dup - (char *)block));
1186			xfs_dir2_data_log_unused(tp, bp, dup);
1187			(void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1188				dup, &dummy);
1189			offset += be16_to_cpu(dup->length);
1190			continue;
1191		}
1192		/*
1193		 * Copy a real entry.
1194		 */
1195		dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
1196		dep->inumber = cpu_to_be64(XFS_DIR2_SF_GET_INUMBER(sfp,
1197				XFS_DIR2_SF_INUMBERP(sfep)));
1198		dep->namelen = sfep->namelen;
1199		memcpy(dep->name, sfep->name, dep->namelen);
1200		tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1201		*tagp = cpu_to_be16((char *)dep - (char *)block);
1202		xfs_dir2_data_log_entry(tp, bp, dep);
1203		blp[2 + i].hashval = cpu_to_be32(xfs_da_hashname(
1204					(char *)sfep->name, sfep->namelen));
1205		blp[2 + i].address = cpu_to_be32(XFS_DIR2_BYTE_TO_DATAPTR(mp,
1206						 (char *)dep - (char *)block));
1207		offset = (int)((char *)(tagp + 1) - (char *)block);
1208		if (++i == sfp->hdr.count)
1209			sfep = NULL;
1210		else
1211			sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
1212	}
1213	/* Done with the temporary buffer */
1214	kmem_free(buf, buf_len);
1215	/*
1216	 * Sort the leaf entries by hash value.
1217	 */
1218	xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1219	/*
1220	 * Log the leaf entry area and tail.
1221	 * Already logged the header in data_init, ignore needlog.
1222	 */
1223	ASSERT(needscan == 0);
1224	xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1225	xfs_dir2_block_log_tail(tp, bp);
1226	xfs_dir2_data_check(dp, bp);
1227	xfs_da_buf_done(bp);
1228	return 0;
1229}
1230