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