xfs_dir2_leaf.c 49.9 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
3
 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4
 * Copyright (c) 2013 Red Hat, Inc.
5
 * All Rights Reserved.
L
Linus Torvalds 已提交
6 7
 */
#include "xfs.h"
8
#include "xfs_fs.h"
9
#include "xfs_shared.h"
10
#include "xfs_format.h"
11 12
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
L
Linus Torvalds 已提交
13 14 15
#include "xfs_mount.h"
#include "xfs_inode.h"
#include "xfs_bmap.h"
16
#include "xfs_dir2.h"
C
Christoph Hellwig 已提交
17
#include "xfs_dir2_priv.h"
L
Linus Torvalds 已提交
18
#include "xfs_error.h"
C
Christoph Hellwig 已提交
19
#include "xfs_trace.h"
20
#include "xfs_trans.h"
21
#include "xfs_buf_item.h"
L
Linus Torvalds 已提交
22 23 24 25

/*
 * Local function declarations.
 */
26
static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
27 28
				    int *indexp, struct xfs_buf **dbpp,
				    struct xfs_dir3_icleaf_hdr *leafhdr);
29 30 31 32
static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
				    struct xfs_buf *bp, int first, int last);
static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
				   struct xfs_buf *bp);
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47
void
xfs_dir2_leaf_hdr_from_disk(
	struct xfs_mount		*mp,
	struct xfs_dir3_icleaf_hdr	*to,
	struct xfs_dir2_leaf		*from)
{
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;

		to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
		to->back = be32_to_cpu(from3->hdr.info.hdr.back);
		to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
		to->count = be16_to_cpu(from3->hdr.count);
		to->stale = be16_to_cpu(from3->hdr.stale);
48
		to->ents = from3->__ents;
49 50 51 52 53 54 55 56 57

		ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
		       to->magic == XFS_DIR3_LEAFN_MAGIC);
	} else {
		to->forw = be32_to_cpu(from->hdr.info.forw);
		to->back = be32_to_cpu(from->hdr.info.back);
		to->magic = be16_to_cpu(from->hdr.info.magic);
		to->count = be16_to_cpu(from->hdr.count);
		to->stale = be16_to_cpu(from->hdr.stale);
58
		to->ents = from->__ents;
59 60 61 62 63 64

		ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
		       to->magic == XFS_DIR2_LEAFN_MAGIC);
	}
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
void
xfs_dir2_leaf_hdr_to_disk(
	struct xfs_mount		*mp,
	struct xfs_dir2_leaf		*to,
	struct xfs_dir3_icleaf_hdr	*from)
{
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;

		ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
		       from->magic == XFS_DIR3_LEAFN_MAGIC);

		to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
		to3->hdr.info.hdr.back = cpu_to_be32(from->back);
		to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
		to3->hdr.count = cpu_to_be16(from->count);
		to3->hdr.stale = cpu_to_be16(from->stale);
	} else {
		ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
		       from->magic == XFS_DIR2_LEAFN_MAGIC);

		to->hdr.info.forw = cpu_to_be32(from->forw);
		to->hdr.info.back = cpu_to_be32(from->back);
		to->hdr.info.magic = cpu_to_be16(from->magic);
		to->hdr.count = cpu_to_be16(from->count);
		to->hdr.stale = cpu_to_be16(from->stale);
	}
}

94 95 96 97 98
/*
 * Check the internal consistency of a leaf1 block.
 * Pop an assert if something is wrong.
 */
#ifdef DEBUG
99
static xfs_failaddr_t
100
xfs_dir3_leaf1_check(
101
	struct xfs_inode	*dp,
102 103 104 105 106
	struct xfs_buf		*bp)
{
	struct xfs_dir2_leaf	*leaf = bp->b_addr;
	struct xfs_dir3_icleaf_hdr leafhdr;

107
	xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
108 109 110 111

	if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
		struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
		if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
112
			return __this_address;
113
	} else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
114
		return __this_address;
115

116
	return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf);
117
}
118 119 120 121 122 123 124 125 126 127 128 129

static inline void
xfs_dir3_leaf_check(
	struct xfs_inode	*dp,
	struct xfs_buf		*bp)
{
	xfs_failaddr_t		fa;

	fa = xfs_dir3_leaf1_check(dp, bp);
	if (!fa)
		return;
	xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
130 131
			bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
			fa);
132 133
	ASSERT(0);
}
134
#else
135
#define	xfs_dir3_leaf_check(dp, bp)
136 137
#endif

138
xfs_failaddr_t
139 140 141 142 143 144 145 146
xfs_dir3_leaf_check_int(
	struct xfs_mount	*mp,
	struct xfs_dir3_icleaf_hdr *hdr,
	struct xfs_dir2_leaf	*leaf)
{
	xfs_dir2_leaf_tail_t	*ltp;
	int			stale;
	int			i;
147
	struct xfs_dir3_icleaf_hdr leafhdr;
148
	struct xfs_da_geometry	*geo = mp->m_dir_geo;
149

150
	if (!hdr) {
151
		xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
152 153 154
		hdr = &leafhdr;
	}

155
	ltp = xfs_dir2_leaf_tail_p(geo, leaf);
156 157 158 159 160 161

	/*
	 * XXX (dgc): This value is not restrictive enough.
	 * Should factor in the size of the bests table as well.
	 * We can deduce a value for that from di_size.
	 */
162
	if (hdr->count > geo->leaf_max_ents)
163
		return __this_address;
164 165 166 167

	/* Leaves and bests don't overlap in leaf format. */
	if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
	     hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
168
	    (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
169
		return __this_address;
170 171 172 173

	/* Check hash value order, count stale entries.  */
	for (i = stale = 0; i < hdr->count; i++) {
		if (i + 1 < hdr->count) {
174 175
			if (be32_to_cpu(hdr->ents[i].hashval) >
					be32_to_cpu(hdr->ents[i + 1].hashval))
176
				return __this_address;
177
		}
178
		if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
179 180 181
			stale++;
	}
	if (hdr->stale != stale)
182 183
		return __this_address;
	return NULL;
184 185
}

186 187 188 189 190
/*
 * We verify the magic numbers before decoding the leaf header so that on debug
 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
 * to incorrect magic numbers.
 */
191
static xfs_failaddr_t
192
xfs_dir3_leaf_verify(
193
	struct xfs_buf		*bp)
194
{
195
	struct xfs_mount	*mp = bp->b_mount;
196
	struct xfs_dir2_leaf	*leaf = bp->b_addr;
197
	xfs_failaddr_t		fa;
198

199 200 201
	fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
	if (fa)
		return fa;
202

203
	return xfs_dir3_leaf_check_int(mp, NULL, leaf);
204 205 206
}

static void
207 208
xfs_dir3_leaf_read_verify(
	struct xfs_buf  *bp)
209
{
210
	struct xfs_mount	*mp = bp->b_mount;
211
	xfs_failaddr_t		fa;
212

213 214
	if (xfs_sb_version_hascrc(&mp->m_sb) &&
	     !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
215 216
		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
	else {
217
		fa = xfs_dir3_leaf_verify(bp);
218 219 220
		if (fa)
			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
	}
221 222 223
}

static void
224 225
xfs_dir3_leaf_write_verify(
	struct xfs_buf  *bp)
D
Dave Chinner 已提交
226
{
227
	struct xfs_mount	*mp = bp->b_mount;
C
Carlos Maiolino 已提交
228
	struct xfs_buf_log_item	*bip = bp->b_log_item;
229
	struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
230
	xfs_failaddr_t		fa;
D
Dave Chinner 已提交
231

232
	fa = xfs_dir3_leaf_verify(bp);
233 234
	if (fa) {
		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
235
		return;
D
Dave Chinner 已提交
236
	}
237 238 239 240 241 242 243

	if (!xfs_sb_version_hascrc(&mp->m_sb))
		return;

	if (bip)
		hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);

244
	xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
245
}
D
Dave Chinner 已提交
246

247
const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
248
	.name = "xfs_dir3_leaf1",
249 250
	.magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC),
		     cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) },
251 252 253
	.verify_read = xfs_dir3_leaf_read_verify,
	.verify_write = xfs_dir3_leaf_write_verify,
	.verify_struct = xfs_dir3_leaf_verify,
254 255
};

256
const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
257
	.name = "xfs_dir3_leafn",
258 259
	.magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC),
		     cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) },
260 261 262
	.verify_read = xfs_dir3_leaf_read_verify,
	.verify_write = xfs_dir3_leaf_write_verify,
	.verify_struct = xfs_dir3_leaf_verify,
263 264
};

265
int
266
xfs_dir3_leaf_read(
D
Dave Chinner 已提交
267 268 269 270 271 272
	struct xfs_trans	*tp,
	struct xfs_inode	*dp,
	xfs_dablk_t		fbno,
	xfs_daddr_t		mappedbno,
	struct xfs_buf		**bpp)
{
273 274 275
	int			err;

	err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
276
				XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
277
	if (!err && tp && *bpp)
278
		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
279
	return err;
D
Dave Chinner 已提交
280 281 282
}

int
283
xfs_dir3_leafn_read(
D
Dave Chinner 已提交
284 285 286 287 288 289
	struct xfs_trans	*tp,
	struct xfs_inode	*dp,
	xfs_dablk_t		fbno,
	xfs_daddr_t		mappedbno,
	struct xfs_buf		**bpp)
{
290 291 292
	int			err;

	err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
293
				XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
294
	if (!err && tp && *bpp)
295
		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
296
	return err;
297 298 299 300 301 302 303 304
}

/*
 * Initialize a new leaf block, leaf1 or leafn magic accepted.
 */
static void
xfs_dir3_leaf_init(
	struct xfs_mount	*mp,
305
	struct xfs_trans	*tp,
306 307
	struct xfs_buf		*bp,
	xfs_ino_t		owner,
308
	uint16_t		type)
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
{
	struct xfs_dir2_leaf	*leaf = bp->b_addr;

	ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);

	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;

		memset(leaf3, 0, sizeof(*leaf3));

		leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
					 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
					 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
		leaf3->info.blkno = cpu_to_be64(bp->b_bn);
		leaf3->info.owner = cpu_to_be64(owner);
324
		uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
325 326 327 328 329 330 331 332 333 334 335 336
	} else {
		memset(leaf, 0, sizeof(*leaf));
		leaf->hdr.info.magic = cpu_to_be16(type);
	}

	/*
	 * If it's a leaf-format directory initialize the tail.
	 * Caller is responsible for initialising the bests table.
	 */
	if (type == XFS_DIR2_LEAF1_MAGIC) {
		struct xfs_dir2_leaf_tail *ltp;

337
		ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
338 339
		ltp->bestcount = 0;
		bp->b_ops = &xfs_dir3_leaf1_buf_ops;
340
		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
341
	} else {
342
		bp->b_ops = &xfs_dir3_leafn_buf_ops;
343
		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
344
	}
345 346 347 348 349 350 351
}

int
xfs_dir3_leaf_get_buf(
	xfs_da_args_t		*args,
	xfs_dir2_db_t		bno,
	struct xfs_buf		**bpp,
352
	uint16_t		magic)
353 354 355 356 357 358 359 360
{
	struct xfs_inode	*dp = args->dp;
	struct xfs_trans	*tp = args->trans;
	struct xfs_mount	*mp = dp->i_mount;
	struct xfs_buf		*bp;
	int			error;

	ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
361 362
	ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
	       bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
363

364 365
	error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
			       -1, &bp, XFS_DATA_FORK);
366 367 368
	if (error)
		return error;

369
	xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
370
	xfs_dir3_leaf_log_header(args, bp);
371
	if (magic == XFS_DIR2_LEAF1_MAGIC)
372
		xfs_dir3_leaf_log_tail(args, bp);
373 374
	*bpp = bp;
	return 0;
D
Dave Chinner 已提交
375
}
L
Linus Torvalds 已提交
376 377 378 379 380 381 382

/*
 * Convert a block form directory to a leaf form directory.
 */
int						/* error */
xfs_dir2_block_to_leaf(
	xfs_da_args_t		*args,		/* operation arguments */
383
	struct xfs_buf		*dbp)		/* input block's buffer */
L
Linus Torvalds 已提交
384
{
385
	__be16			*bestsp;	/* leaf's bestsp entries */
L
Linus Torvalds 已提交
386
	xfs_dablk_t		blkno;		/* leaf block's bno */
387
	xfs_dir2_data_hdr_t	*hdr;		/* block header */
L
Linus Torvalds 已提交
388 389 390 391
	xfs_dir2_leaf_entry_t	*blp;		/* block's leaf entries */
	xfs_dir2_block_tail_t	*btp;		/* block's tail */
	xfs_inode_t		*dp;		/* incore directory inode */
	int			error;		/* error return code */
392
	struct xfs_buf		*lbp;		/* leaf block's buffer */
L
Linus Torvalds 已提交
393 394 395 396 397 398
	xfs_dir2_db_t		ldb;		/* leaf block's bno */
	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
	xfs_dir2_leaf_tail_t	*ltp;		/* leaf's tail */
	int			needlog;	/* need to log block header */
	int			needscan;	/* need to rescan bestfree */
	xfs_trans_t		*tp;		/* transaction pointer */
399
	struct xfs_dir2_data_free *bf;
400
	struct xfs_dir3_icleaf_hdr leafhdr;
L
Linus Torvalds 已提交
401

C
Christoph Hellwig 已提交
402 403
	trace_xfs_dir2_block_to_leaf(args);

L
Linus Torvalds 已提交
404 405 406 407 408 409 410 411 412 413
	dp = args->dp;
	tp = args->trans;
	/*
	 * Add the leaf block to the inode.
	 * This interface will only put blocks in the leaf/node range.
	 * Since that's empty now, we'll get the root (block 0 in range).
	 */
	if ((error = xfs_da_grow_inode(args, &blkno))) {
		return error;
	}
414
	ldb = xfs_dir2_da_to_db(args->geo, blkno);
415
	ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
L
Linus Torvalds 已提交
416 417 418
	/*
	 * Initialize the leaf block, get a buffer for it.
	 */
419 420
	error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
	if (error)
L
Linus Torvalds 已提交
421
		return error;
422

423 424
	leaf = lbp->b_addr;
	hdr = dbp->b_addr;
425
	xfs_dir3_data_check(dp, dbp);
426
	btp = xfs_dir2_block_tail_p(args->geo, hdr);
427
	blp = xfs_dir2_block_leaf_p(btp);
428
	bf = dp->d_ops->data_bestfree_p(hdr);
429

L
Linus Torvalds 已提交
430 431 432
	/*
	 * Set the counts in the leaf header.
	 */
433
	xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
434 435
	leafhdr.count = be32_to_cpu(btp->count);
	leafhdr.stale = be32_to_cpu(btp->stale);
436
	xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
437
	xfs_dir3_leaf_log_header(args, lbp);
438

L
Linus Torvalds 已提交
439 440 441 442
	/*
	 * Could compact these but I think we always do the conversion
	 * after squeezing out stale entries.
	 */
443 444 445
	memcpy(leafhdr.ents, blp,
		be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry));
	xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1);
L
Linus Torvalds 已提交
446 447 448 449 450 451
	needscan = 0;
	needlog = 1;
	/*
	 * Make the space formerly occupied by the leaf entries and block
	 * tail be free.
	 */
452
	xfs_dir2_data_make_free(args, dbp,
453
		(xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
454
		(xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
L
Linus Torvalds 已提交
455 456 457 458 459
				       (char *)blp),
		&needlog, &needscan);
	/*
	 * Fix up the block header, make it a data block.
	 */
460
	dbp->b_ops = &xfs_dir3_data_buf_ops;
461
	xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
462 463 464 465 466
	if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
		hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
	else
		hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);

L
Linus Torvalds 已提交
467
	if (needscan)
468
		xfs_dir2_data_freescan(dp, hdr, &needlog);
L
Linus Torvalds 已提交
469 470 471
	/*
	 * Set up leaf tail and bests table.
	 */
472
	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
473
	ltp->bestcount = cpu_to_be32(1);
474
	bestsp = xfs_dir2_leaf_bests_p(ltp);
475
	bestsp[0] =  bf[0].length;
L
Linus Torvalds 已提交
476 477 478 479
	/*
	 * Log the data header and leaf bests table.
	 */
	if (needlog)
480
		xfs_dir2_data_log_header(args, dbp);
481
	xfs_dir3_leaf_check(dp, lbp);
482
	xfs_dir3_data_check(dp, dbp);
483
	xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
L
Linus Torvalds 已提交
484 485 486
	return 0;
}

487
STATIC void
488 489 490
xfs_dir3_leaf_find_stale(
	struct xfs_dir3_icleaf_hdr *leafhdr,
	struct xfs_dir2_leaf_entry *ents,
491 492 493 494 495 496 497 498
	int			index,
	int			*lowstale,
	int			*highstale)
{
	/*
	 * Find the first stale entry before our index, if any.
	 */
	for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
499
		if (ents[*lowstale].address ==
500 501 502 503 504 505 506 507 508
		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
			break;
	}

	/*
	 * Find the first stale entry at or after our index, if any.
	 * Stop if the result would require moving more entries than using
	 * lowstale.
	 */
509 510
	for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
		if (ents[*highstale].address ==
511 512 513 514 515 516 517
		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
			break;
		if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
			break;
	}
}

518
struct xfs_dir2_leaf_entry *
519 520 521
xfs_dir3_leaf_find_entry(
	struct xfs_dir3_icleaf_hdr *leafhdr,
	struct xfs_dir2_leaf_entry *ents,
522 523 524 525 526 527 528
	int			index,		/* leaf table position */
	int			compact,	/* need to compact leaves */
	int			lowstale,	/* index of prev stale leaf */
	int			highstale,	/* index of next stale leaf */
	int			*lfloglow,	/* low leaf logging index */
	int			*lfloghigh)	/* high leaf logging index */
{
529
	if (!leafhdr->stale) {
530 531 532 533 534 535 536
		xfs_dir2_leaf_entry_t	*lep;	/* leaf entry table pointer */

		/*
		 * Now we need to make room to insert the leaf entry.
		 *
		 * If there are no stale entries, just insert a hole at index.
		 */
537 538
		lep = &ents[index];
		if (index < leafhdr->count)
539
			memmove(lep + 1, lep,
540
				(leafhdr->count - index) * sizeof(*lep));
541 542 543 544 545

		/*
		 * Record low and high logging indices for the leaf.
		 */
		*lfloglow = index;
546
		*lfloghigh = leafhdr->count++;
547 548 549 550 551 552 553 554 555 556 557 558
		return lep;
	}

	/*
	 * There are stale entries.
	 *
	 * We will use one of them for the new entry.  It's probably not at
	 * the right location, so we'll have to shift some up or down first.
	 *
	 * If we didn't compact before, we need to find the nearest stale
	 * entries before and after our insertion point.
	 */
559
	if (compact == 0)
560 561
		xfs_dir3_leaf_find_stale(leafhdr, ents, index,
					 &lowstale, &highstale);
562 563 564 565 566

	/*
	 * If the low one is better, use it.
	 */
	if (lowstale >= 0 &&
567
	    (highstale == leafhdr->count ||
568 569
	     index - lowstale - 1 < highstale - index)) {
		ASSERT(index - lowstale - 1 >= 0);
570
		ASSERT(ents[lowstale].address ==
571
		       cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
572 573 574 575 576 577

		/*
		 * Copy entries up to cover the stale entry and make room
		 * for the new entry.
		 */
		if (index - lowstale - 1 > 0) {
578
			memmove(&ents[lowstale], &ents[lowstale + 1],
579
				(index - lowstale - 1) *
580
					sizeof(xfs_dir2_leaf_entry_t));
581
		}
D
Dave Chinner 已提交
582 583
		*lfloglow = min(lowstale, *lfloglow);
		*lfloghigh = max(index - 1, *lfloghigh);
584 585
		leafhdr->stale--;
		return &ents[index - 1];
586 587 588 589 590 591
	}

	/*
	 * The high one is better, so use that one.
	 */
	ASSERT(highstale - index >= 0);
592
	ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
593 594 595 596 597 598

	/*
	 * Copy entries down to cover the stale entry and make room for the
	 * new entry.
	 */
	if (highstale - index > 0) {
599
		memmove(&ents[index + 1], &ents[index],
600 601
			(highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
	}
D
Dave Chinner 已提交
602 603
	*lfloglow = min(index, *lfloglow);
	*lfloghigh = max(highstale, *lfloghigh);
604 605
	leafhdr->stale--;
	return &ents[index];
606 607
}

L
Linus Torvalds 已提交
608 609 610 611 612
/*
 * Add an entry to a leaf form directory.
 */
int						/* error */
xfs_dir2_leaf_addname(
613
	struct xfs_da_args	*args)		/* operation arguments */
L
Linus Torvalds 已提交
614
{
615 616
	struct xfs_dir3_icleaf_hdr leafhdr;
	struct xfs_trans	*tp = args->trans;
617
	__be16			*bestsp;	/* freespace table in leaf */
618
	__be16			*tagp;		/* end of data entry */
619
	struct xfs_buf		*dbp;		/* data block buffer */
620 621 622 623 624 625 626 627 628 629 630
	struct xfs_buf		*lbp;		/* leaf's buffer */
	struct xfs_dir2_leaf	*leaf;		/* leaf structure */
	struct xfs_inode	*dp = args->dp;	/* incore directory inode */
	struct xfs_dir2_data_hdr *hdr;		/* data block header */
	struct xfs_dir2_data_entry *dep;	/* data block entry */
	struct xfs_dir2_leaf_entry *lep;	/* leaf entry table pointer */
	struct xfs_dir2_leaf_entry *ents;
	struct xfs_dir2_data_unused *dup;	/* data unused entry */
	struct xfs_dir2_leaf_tail *ltp;		/* leaf tail pointer */
	struct xfs_dir2_data_free *bf;		/* bestfree table */
	int			compact;	/* need to compact leaves */
L
Linus Torvalds 已提交
631 632
	int			error;		/* error return value */
	int			grown;		/* allocated new data block */
633
	int			highstale = 0;	/* index of next stale leaf */
L
Linus Torvalds 已提交
634 635 636 637 638
	int			i;		/* temporary, index */
	int			index;		/* leaf table position */
	int			length;		/* length of new entry */
	int			lfloglow;	/* low leaf logging index */
	int			lfloghigh;	/* high leaf logging index */
639
	int			lowstale = 0;	/* index of prev stale leaf */
L
Linus Torvalds 已提交
640 641 642 643 644
	int			needbytes;	/* leaf block bytes needed */
	int			needlog;	/* need to log data header */
	int			needscan;	/* need to rescan data free */
	xfs_dir2_db_t		use_block;	/* data block number */

C
Christoph Hellwig 已提交
645 646
	trace_xfs_dir2_leaf_addname(args);

647
	error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
648
	if (error)
L
Linus Torvalds 已提交
649
		return error;
D
Dave Chinner 已提交
650

L
Linus Torvalds 已提交
651 652 653 654 655 656 657
	/*
	 * Look up the entry by hash value and name.
	 * We know it's not there, our caller has already done a lookup.
	 * So the index is of the entry to insert in front of.
	 * But if there are dup hash values the index is of the first of those.
	 */
	index = xfs_dir2_leaf_search_hash(args, lbp);
658
	leaf = lbp->b_addr;
659
	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
660
	xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
661
	ents = leafhdr.ents;
662
	bestsp = xfs_dir2_leaf_bests_p(ltp);
663
	length = dp->d_ops->data_entsize(args->namelen);
664

L
Linus Torvalds 已提交
665 666 667 668 669 670
	/*
	 * See if there are any entries with the same hash value
	 * and space in their block for the new entry.
	 * This is good because it puts multiple same-hash value entries
	 * in a data block, improving the lookup of those entries.
	 */
671 672
	for (use_block = -1, lep = &ents[index];
	     index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
L
Linus Torvalds 已提交
673
	     index++, lep++) {
674
		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
L
Linus Torvalds 已提交
675
			continue;
676
		i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
677
		ASSERT(i < be32_to_cpu(ltp->bestcount));
678
		ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
679
		if (be16_to_cpu(bestsp[i]) >= length) {
L
Linus Torvalds 已提交
680 681 682 683 684 685 686 687
			use_block = i;
			break;
		}
	}
	/*
	 * Didn't find a block yet, linear search all the data blocks.
	 */
	if (use_block == -1) {
688
		for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
L
Linus Torvalds 已提交
689 690 691
			/*
			 * Remember a block we see that's missing.
			 */
692 693
			if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
			    use_block == -1)
L
Linus Torvalds 已提交
694
				use_block = i;
695
			else if (be16_to_cpu(bestsp[i]) >= length) {
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703
				use_block = i;
				break;
			}
		}
	}
	/*
	 * How many bytes do we need in the leaf block?
	 */
704
	needbytes = 0;
705
	if (!leafhdr.stale)
706 707 708 709
		needbytes += sizeof(xfs_dir2_leaf_entry_t);
	if (use_block == -1)
		needbytes += sizeof(xfs_dir2_data_off_t);

L
Linus Torvalds 已提交
710 711 712 713
	/*
	 * Now kill use_block if it refers to a missing block, so we
	 * can use it as an indication of allocation needed.
	 */
714
	if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
L
Linus Torvalds 已提交
715 716 717 718 719
		use_block = -1;
	/*
	 * If we don't have enough free bytes but we can make enough
	 * by compacting out stale entries, we'll do that.
	 */
720 721
	if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
	    leafhdr.stale > 1)
L
Linus Torvalds 已提交
722
		compact = 1;
723

L
Linus Torvalds 已提交
724 725 726 727
	/*
	 * Otherwise if we don't have enough free bytes we need to
	 * convert to node form.
	 */
728
	else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
L
Linus Torvalds 已提交
729 730 731
		/*
		 * Just checking or no space reservation, give up.
		 */
732 733
		if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
							args->total == 0) {
734
			xfs_trans_brelse(tp, lbp);
D
Dave Chinner 已提交
735
			return -ENOSPC;
L
Linus Torvalds 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
		}
		/*
		 * Convert to node form.
		 */
		error = xfs_dir2_leaf_to_node(args, lbp);
		if (error)
			return error;
		/*
		 * Then add the new entry.
		 */
		return xfs_dir2_node_addname(args);
	}
	/*
	 * Otherwise it will fit without compaction.
	 */
	else
		compact = 0;
	/*
	 * If just checking, then it will fit unless we needed to allocate
	 * a new data block.
	 */
757
	if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
758
		xfs_trans_brelse(tp, lbp);
D
Dave Chinner 已提交
759
		return use_block == -1 ? -ENOSPC : 0;
L
Linus Torvalds 已提交
760 761 762 763 764 765
	}
	/*
	 * If no allocations are allowed, return now before we've
	 * changed anything.
	 */
	if (args->total == 0 && use_block == -1) {
766
		xfs_trans_brelse(tp, lbp);
D
Dave Chinner 已提交
767
		return -ENOSPC;
L
Linus Torvalds 已提交
768 769 770 771 772 773 774 775
	}
	/*
	 * Need to compact the leaf entries, removing stale ones.
	 * Leave one stale entry behind - the one closest to our
	 * insertion index - and we'll shift that one to our insertion
	 * point later.
	 */
	if (compact) {
776 777
		xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
			&highstale, &lfloglow, &lfloghigh);
L
Linus Torvalds 已提交
778 779 780 781 782
	}
	/*
	 * There are stale entries, so we'll need log-low and log-high
	 * impossibly bad values later.
	 */
783 784
	else if (leafhdr.stale) {
		lfloglow = leafhdr.count;
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792 793 794 795 796
		lfloghigh = -1;
	}
	/*
	 * If there was no data block space found, we need to allocate
	 * a new one.
	 */
	if (use_block == -1) {
		/*
		 * Add the new data block.
		 */
		if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
				&use_block))) {
797
			xfs_trans_brelse(tp, lbp);
L
Linus Torvalds 已提交
798 799 800 801 802
			return error;
		}
		/*
		 * Initialize the block.
		 */
803
		if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
804
			xfs_trans_brelse(tp, lbp);
L
Linus Torvalds 已提交
805 806 807 808 809 810
			return error;
		}
		/*
		 * If we're adding a new data block on the end we need to
		 * extend the bests table.  Copy it up one entry.
		 */
811
		if (use_block >= be32_to_cpu(ltp->bestcount)) {
L
Linus Torvalds 已提交
812 813
			bestsp--;
			memmove(&bestsp[0], &bestsp[1],
814
				be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
815
			be32_add_cpu(&ltp->bestcount, 1);
816 817 818
			xfs_dir3_leaf_log_tail(args, lbp);
			xfs_dir3_leaf_log_bests(args, lbp, 0,
						be32_to_cpu(ltp->bestcount) - 1);
L
Linus Torvalds 已提交
819 820 821 822 823
		}
		/*
		 * If we're filling in a previously empty block just log it.
		 */
		else
824
			xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
825
		hdr = dbp->b_addr;
826
		bf = dp->d_ops->data_bestfree_p(hdr);
827
		bestsp[use_block] = bf[0].length;
L
Linus Torvalds 已提交
828
		grown = 1;
829 830 831 832 833
	} else {
		/*
		 * Already had space in some data block.
		 * Just read that one in.
		 */
834
		error = xfs_dir3_data_read(tp, dp,
835 836
				   xfs_dir2_db_to_da(args->geo, use_block),
				   -1, &dbp);
837
		if (error) {
838
			xfs_trans_brelse(tp, lbp);
L
Linus Torvalds 已提交
839 840
			return error;
		}
841
		hdr = dbp->b_addr;
842
		bf = dp->d_ops->data_bestfree_p(hdr);
L
Linus Torvalds 已提交
843 844 845 846 847 848
		grown = 0;
	}
	/*
	 * Point to the biggest freespace in our data block.
	 */
	dup = (xfs_dir2_data_unused_t *)
849
	      ((char *)hdr + be16_to_cpu(bf[0].offset));
L
Linus Torvalds 已提交
850 851 852 853
	needscan = needlog = 0;
	/*
	 * Mark the initial part of our freespace in use for the new entry.
	 */
854 855 856 857 858 859 860
	error = xfs_dir2_data_use_free(args, dbp, dup,
			(xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
			length, &needlog, &needscan);
	if (error) {
		xfs_trans_brelse(tp, lbp);
		return error;
	}
L
Linus Torvalds 已提交
861 862 863 864
	/*
	 * Initialize our new entry (at last).
	 */
	dep = (xfs_dir2_data_entry_t *)dup;
865
	dep->inumber = cpu_to_be64(args->inumber);
L
Linus Torvalds 已提交
866 867
	dep->namelen = args->namelen;
	memcpy(dep->name, args->name, dep->namelen);
868 869
	dp->d_ops->data_put_ftype(dep, args->filetype);
	tagp = dp->d_ops->data_entry_tag_p(dep);
870
	*tagp = cpu_to_be16((char *)dep - (char *)hdr);
L
Linus Torvalds 已提交
871 872 873 874
	/*
	 * Need to scan fix up the bestfree table.
	 */
	if (needscan)
875
		xfs_dir2_data_freescan(dp, hdr, &needlog);
L
Linus Torvalds 已提交
876 877 878 879
	/*
	 * Need to log the data block's header.
	 */
	if (needlog)
880 881
		xfs_dir2_data_log_header(args, dbp);
	xfs_dir2_data_log_entry(args, dbp, dep);
L
Linus Torvalds 已提交
882 883 884 885
	/*
	 * If the bests table needs to be changed, do it.
	 * Log the change unless we've already done that.
	 */
886 887
	if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
		bestsp[use_block] = bf[0].length;
L
Linus Torvalds 已提交
888
		if (!grown)
889
			xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
L
Linus Torvalds 已提交
890
	}
891

892
	lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
893 894
				       highstale, &lfloglow, &lfloghigh);

L
Linus Torvalds 已提交
895 896 897
	/*
	 * Fill in the new leaf entry.
	 */
898
	lep->hashval = cpu_to_be32(args->hashval);
899 900
	lep->address = cpu_to_be32(
				xfs_dir2_db_off_to_dataptr(args->geo, use_block,
901
				be16_to_cpu(*tagp)));
L
Linus Torvalds 已提交
902 903 904
	/*
	 * Log the leaf fields and give up the buffers.
	 */
905
	xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
906
	xfs_dir3_leaf_log_header(args, lbp);
907
	xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh);
908
	xfs_dir3_leaf_check(dp, lbp);
909
	xfs_dir3_data_check(dp, dbp);
L
Linus Torvalds 已提交
910 911 912 913 914 915 916 917
	return 0;
}

/*
 * Compact out any stale entries in the leaf.
 * Log the header and changed leaf entries, if any.
 */
void
918
xfs_dir3_leaf_compact(
L
Linus Torvalds 已提交
919
	xfs_da_args_t	*args,		/* operation arguments */
920
	struct xfs_dir3_icleaf_hdr *leafhdr,
921
	struct xfs_buf	*bp)		/* leaf buffer */
L
Linus Torvalds 已提交
922 923 924 925 926
{
	int		from;		/* source leaf index */
	xfs_dir2_leaf_t	*leaf;		/* leaf structure */
	int		loglow;		/* first leaf entry to log */
	int		to;		/* target leaf index */
927
	struct xfs_inode *dp = args->dp;
L
Linus Torvalds 已提交
928

929
	leaf = bp->b_addr;
930
	if (!leafhdr->stale)
L
Linus Torvalds 已提交
931
		return;
932

L
Linus Torvalds 已提交
933 934 935
	/*
	 * Compress out the stale entries in place.
	 */
936
	for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
937 938
		if (leafhdr->ents[from].address ==
		    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
L
Linus Torvalds 已提交
939 940 941 942 943 944 945
			continue;
		/*
		 * Only actually copy the entries that are different.
		 */
		if (from > to) {
			if (loglow == -1)
				loglow = to;
946
			leafhdr->ents[to] = leafhdr->ents[from];
L
Linus Torvalds 已提交
947 948 949 950 951 952
		}
		to++;
	}
	/*
	 * Update and log the header, log the leaf entries.
	 */
953 954 955 956
	ASSERT(leafhdr->stale == from - to);
	leafhdr->count -= leafhdr->stale;
	leafhdr->stale = 0;

957
	xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
958
	xfs_dir3_leaf_log_header(args, bp);
L
Linus Torvalds 已提交
959
	if (loglow != -1)
960
		xfs_dir3_leaf_log_ents(args, leafhdr, bp, loglow, to - 1);
L
Linus Torvalds 已提交
961 962 963 964 965 966 967 968 969 970 971
}

/*
 * Compact the leaf entries, removing stale ones.
 * Leave one stale entry behind - the one closest to our
 * insertion index - and the caller will shift that one to our insertion
 * point later.
 * Return new insertion index, where the remaining stale entry is,
 * and leaf logging indices.
 */
void
972 973 974
xfs_dir3_leaf_compact_x1(
	struct xfs_dir3_icleaf_hdr *leafhdr,
	struct xfs_dir2_leaf_entry *ents,
L
Linus Torvalds 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987 988
	int		*indexp,	/* insertion index */
	int		*lowstalep,	/* out: stale entry before us */
	int		*highstalep,	/* out: stale entry after us */
	int		*lowlogp,	/* out: low log index */
	int		*highlogp)	/* out: high log index */
{
	int		from;		/* source copy index */
	int		highstale;	/* stale entry at/after index */
	int		index;		/* insertion index */
	int		keepstale;	/* source index of kept stale */
	int		lowstale;	/* stale entry before index */
	int		newindex=0;	/* new insertion index */
	int		to;		/* destination copy index */

989
	ASSERT(leafhdr->stale > 1);
L
Linus Torvalds 已提交
990
	index = *indexp;
991

992
	xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
993

L
Linus Torvalds 已提交
994 995 996 997
	/*
	 * Pick the better of lowstale and highstale.
	 */
	if (lowstale >= 0 &&
998
	    (highstale == leafhdr->count ||
L
Linus Torvalds 已提交
999 1000 1001 1002 1003 1004 1005 1006
	     index - lowstale <= highstale - index))
		keepstale = lowstale;
	else
		keepstale = highstale;
	/*
	 * Copy the entries in place, removing all the stale entries
	 * except keepstale.
	 */
1007
	for (from = to = 0; from < leafhdr->count; from++) {
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013
		/*
		 * Notice the new value of index.
		 */
		if (index == from)
			newindex = to;
		if (from != keepstale &&
1014
		    ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
L
Linus Torvalds 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
			if (from == to)
				*lowlogp = to;
			continue;
		}
		/*
		 * Record the new keepstale value for the insertion.
		 */
		if (from == keepstale)
			lowstale = highstale = to;
		/*
		 * Copy only the entries that have moved.
		 */
		if (from > to)
1028
			ents[to] = ents[from];
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
		to++;
	}
	ASSERT(from > to);
	/*
	 * If the insertion point was past the last entry,
	 * set the new insertion point accordingly.
	 */
	if (index == from)
		newindex = to;
	*indexp = newindex;
	/*
	 * Adjust the leaf header values.
	 */
1042 1043
	leafhdr->count -= from - to;
	leafhdr->stale = 1;
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049 1050
	/*
	 * Remember the low/high stale value only in the "right"
	 * direction.
	 */
	if (lowstale >= newindex)
		lowstale = -1;
	else
1051 1052
		highstale = leafhdr->count;
	*highlogp = leafhdr->count - 1;
L
Linus Torvalds 已提交
1053 1054 1055 1056 1057 1058 1059
	*lowstalep = lowstale;
	*highstalep = highstale;
}

/*
 * Log the bests entries indicated from a leaf1 block.
 */
1060
static void
1061
xfs_dir3_leaf_log_bests(
1062
	struct xfs_da_args	*args,
1063
	struct xfs_buf		*bp,		/* leaf buffer */
L
Linus Torvalds 已提交
1064 1065 1066
	int			first,		/* first entry to log */
	int			last)		/* last entry to log */
{
1067 1068
	__be16			*firstb;	/* pointer to first entry */
	__be16			*lastb;		/* pointer to last entry */
1069
	struct xfs_dir2_leaf	*leaf = bp->b_addr;
L
Linus Torvalds 已提交
1070 1071
	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */

1072 1073 1074
	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));

1075
	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1076 1077
	firstb = xfs_dir2_leaf_bests_p(ltp) + first;
	lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1078 1079
	xfs_trans_log_buf(args->trans, bp,
		(uint)((char *)firstb - (char *)leaf),
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085 1086
		(uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
}

/*
 * Log the leaf entries indicated from a leaf1 or leafn block.
 */
void
1087
xfs_dir3_leaf_log_ents(
1088
	struct xfs_da_args	*args,
1089
	struct xfs_dir3_icleaf_hdr *hdr,
1090 1091 1092
	struct xfs_buf		*bp,
	int			first,
	int			last)
L
Linus Torvalds 已提交
1093 1094 1095
{
	xfs_dir2_leaf_entry_t	*firstlep;	/* pointer to first entry */
	xfs_dir2_leaf_entry_t	*lastlep;	/* pointer to last entry */
1096
	struct xfs_dir2_leaf	*leaf = bp->b_addr;
L
Linus Torvalds 已提交
1097

1098
	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1099 1100 1101 1102
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));

1103 1104
	firstlep = &hdr->ents[first];
	lastlep = &hdr->ents[last];
1105 1106
	xfs_trans_log_buf(args->trans, bp,
		(uint)((char *)firstlep - (char *)leaf),
L
Linus Torvalds 已提交
1107 1108 1109 1110 1111 1112 1113
		(uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
}

/*
 * Log the header of the leaf1 or leafn block.
 */
void
1114
xfs_dir3_leaf_log_header(
1115
	struct xfs_da_args	*args,
1116
	struct xfs_buf		*bp)
L
Linus Torvalds 已提交
1117
{
1118
	struct xfs_dir2_leaf	*leaf = bp->b_addr;
L
Linus Torvalds 已提交
1119

1120
	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1121 1122 1123 1124
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));

1125 1126
	xfs_trans_log_buf(args->trans, bp,
			  (uint)((char *)&leaf->hdr - (char *)leaf),
1127
			  args->geo->leaf_hdr_size - 1);
L
Linus Torvalds 已提交
1128 1129 1130 1131 1132
}

/*
 * Log the tail of the leaf1 block.
 */
1133
STATIC void
1134
xfs_dir3_leaf_log_tail(
1135
	struct xfs_da_args	*args,
1136
	struct xfs_buf		*bp)
L
Linus Torvalds 已提交
1137
{
1138
	struct xfs_dir2_leaf	*leaf = bp->b_addr;
L
Linus Torvalds 已提交
1139
	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
1140 1141 1142 1143 1144

	ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
	       leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
L
Linus Torvalds 已提交
1145

1146 1147 1148
	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
	xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
		(uint)(args->geo->blksize - 1));
L
Linus Torvalds 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
}

/*
 * Look up the entry referred to by args in the leaf format directory.
 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
 * is also used by the node-format code.
 */
int
xfs_dir2_leaf_lookup(
	xfs_da_args_t		*args)		/* operation arguments */
{
1160
	struct xfs_buf		*dbp;		/* data block buffer */
L
Linus Torvalds 已提交
1161 1162 1163 1164
	xfs_dir2_data_entry_t	*dep;		/* data block entry */
	xfs_inode_t		*dp;		/* incore directory inode */
	int			error;		/* error return code */
	int			index;		/* found entry index */
1165
	struct xfs_buf		*lbp;		/* leaf buffer */
L
Linus Torvalds 已提交
1166 1167
	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
	xfs_trans_t		*tp;		/* transaction pointer */
1168
	struct xfs_dir3_icleaf_hdr leafhdr;
L
Linus Torvalds 已提交
1169

C
Christoph Hellwig 已提交
1170 1171
	trace_xfs_dir2_leaf_lookup(args);

L
Linus Torvalds 已提交
1172 1173 1174
	/*
	 * Look up name in the leaf block, returning both buffers and index.
	 */
1175 1176
	error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
	if (error)
L
Linus Torvalds 已提交
1177
		return error;
1178

L
Linus Torvalds 已提交
1179 1180
	tp = args->trans;
	dp = args->dp;
1181
	xfs_dir3_leaf_check(dp, lbp);
1182

L
Linus Torvalds 已提交
1183 1184 1185
	/*
	 * Get to the leaf entry and contained data entry address.
	 */
1186
	lep = &leafhdr.ents[index];
1187

L
Linus Torvalds 已提交
1188 1189 1190 1191
	/*
	 * Point to the data entry.
	 */
	dep = (xfs_dir2_data_entry_t *)
1192
	      ((char *)dbp->b_addr +
1193
	       xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
L
Linus Torvalds 已提交
1194
	/*
1195
	 * Return the found inode number & CI name if appropriate
L
Linus Torvalds 已提交
1196
	 */
1197
	args->inumber = be64_to_cpu(dep->inumber);
1198
	args->filetype = dp->d_ops->data_get_ftype(dep);
1199
	error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1200 1201
	xfs_trans_brelse(tp, dbp);
	xfs_trans_brelse(tp, lbp);
E
Eric Sandeen 已提交
1202
	return error;
L
Linus Torvalds 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
}

/*
 * Look up name/hash in the leaf block.
 * Fill in indexp with the found index, and dbpp with the data buffer.
 * If not found dbpp will be NULL, and ENOENT comes back.
 * lbpp will always be filled in with the leaf buffer unless there's an error.
 */
static int					/* error */
xfs_dir2_leaf_lookup_int(
	xfs_da_args_t		*args,		/* operation arguments */
1214
	struct xfs_buf		**lbpp,		/* out: leaf buffer */
L
Linus Torvalds 已提交
1215
	int			*indexp,	/* out: index in leaf block */
1216 1217
	struct xfs_buf		**dbpp,		/* out: data buffer */
	struct xfs_dir3_icleaf_hdr *leafhdr)
L
Linus Torvalds 已提交
1218
{
1219
	xfs_dir2_db_t		curdb = -1;	/* current data block number */
1220
	struct xfs_buf		*dbp = NULL;	/* data buffer */
L
Linus Torvalds 已提交
1221 1222 1223 1224
	xfs_dir2_data_entry_t	*dep;		/* data entry */
	xfs_inode_t		*dp;		/* incore directory inode */
	int			error;		/* error return code */
	int			index;		/* index in leaf block */
1225
	struct xfs_buf		*lbp;		/* leaf buffer */
L
Linus Torvalds 已提交
1226 1227 1228 1229 1230
	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
	xfs_mount_t		*mp;		/* filesystem mount point */
	xfs_dir2_db_t		newdb;		/* new data block number */
	xfs_trans_t		*tp;		/* transaction pointer */
1231
	xfs_dir2_db_t		cidb = -1;	/* case match data block no. */
1232
	enum xfs_dacmp		cmp;		/* name compare result */
L
Linus Torvalds 已提交
1233 1234 1235 1236

	dp = args->dp;
	tp = args->trans;
	mp = dp->i_mount;
D
Dave Chinner 已提交
1237

1238
	error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
1239
	if (error)
L
Linus Torvalds 已提交
1240
		return error;
D
Dave Chinner 已提交
1241

L
Linus Torvalds 已提交
1242
	*lbpp = lbp;
1243
	leaf = lbp->b_addr;
1244
	xfs_dir3_leaf_check(dp, lbp);
1245
	xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf);
1246

L
Linus Torvalds 已提交
1247 1248 1249 1250 1251 1252 1253 1254
	/*
	 * Look for the first leaf entry with our hash value.
	 */
	index = xfs_dir2_leaf_search_hash(args, lbp);
	/*
	 * Loop over all the entries with the right hash value
	 * looking to match the name.
	 */
1255 1256 1257
	for (lep = &leafhdr->ents[index];
	     index < leafhdr->count &&
			be32_to_cpu(lep->hashval) == args->hashval;
1258
	     lep++, index++) {
L
Linus Torvalds 已提交
1259 1260 1261
		/*
		 * Skip over stale leaf entries.
		 */
1262
		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
L
Linus Torvalds 已提交
1263 1264 1265 1266
			continue;
		/*
		 * Get the new data block number.
		 */
1267 1268
		newdb = xfs_dir2_dataptr_to_db(args->geo,
					       be32_to_cpu(lep->address));
L
Linus Torvalds 已提交
1269 1270 1271 1272 1273
		/*
		 * If it's not the same as the old data block number,
		 * need to pitch the old one and read the new one.
		 */
		if (newdb != curdb) {
1274
			if (dbp)
1275
				xfs_trans_brelse(tp, dbp);
1276
			error = xfs_dir3_data_read(tp, dp,
1277 1278
					   xfs_dir2_db_to_da(args->geo, newdb),
					   -1, &dbp);
1279
			if (error) {
1280
				xfs_trans_brelse(tp, lbp);
L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286 1287
				return error;
			}
			curdb = newdb;
		}
		/*
		 * Point to the data entry.
		 */
1288
		dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1289 1290
			xfs_dir2_dataptr_to_off(args->geo,
						be32_to_cpu(lep->address)));
L
Linus Torvalds 已提交
1291
		/*
1292 1293 1294
		 * Compare name and if it's an exact match, return the index
		 * and buffer. If it's the first case-insensitive match, store
		 * the index and buffer and continue looking for an exact match.
L
Linus Torvalds 已提交
1295
		 */
1296 1297 1298
		cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
		if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
			args->cmpresult = cmp;
L
Linus Torvalds 已提交
1299
			*indexp = index;
1300
			/* case exact match: return the current buffer. */
1301 1302 1303 1304
			if (cmp == XFS_CMP_EXACT) {
				*dbpp = dbp;
				return 0;
			}
1305
			cidb = curdb;
L
Linus Torvalds 已提交
1306 1307
		}
	}
1308
	ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1309
	/*
1310 1311 1312
	 * Here, we can only be doing a lookup (not a rename or remove).
	 * If a case-insensitive match was found earlier, re-read the
	 * appropriate data block if required and return it.
1313 1314
	 */
	if (args->cmpresult == XFS_CMP_CASE) {
1315 1316
		ASSERT(cidb != -1);
		if (cidb != curdb) {
1317
			xfs_trans_brelse(tp, dbp);
1318
			error = xfs_dir3_data_read(tp, dp,
1319 1320
					   xfs_dir2_db_to_da(args->geo, cidb),
					   -1, &dbp);
1321
			if (error) {
1322
				xfs_trans_brelse(tp, lbp);
1323 1324 1325 1326
				return error;
			}
		}
		*dbpp = dbp;
1327 1328
		return 0;
	}
L
Linus Torvalds 已提交
1329
	/*
D
Dave Chinner 已提交
1330
	 * No match found, return -ENOENT.
L
Linus Torvalds 已提交
1331
	 */
1332
	ASSERT(cidb == -1);
L
Linus Torvalds 已提交
1333
	if (dbp)
1334 1335
		xfs_trans_brelse(tp, dbp);
	xfs_trans_brelse(tp, lbp);
D
Dave Chinner 已提交
1336
	return -ENOENT;
L
Linus Torvalds 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345
}

/*
 * Remove an entry from a leaf format directory.
 */
int						/* error */
xfs_dir2_leaf_removename(
	xfs_da_args_t		*args)		/* operation arguments */
{
1346
	__be16			*bestsp;	/* leaf block best freespace */
1347
	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
L
Linus Torvalds 已提交
1348
	xfs_dir2_db_t		db;		/* data block number */
1349
	struct xfs_buf		*dbp;		/* data block buffer */
L
Linus Torvalds 已提交
1350 1351 1352 1353 1354
	xfs_dir2_data_entry_t	*dep;		/* data entry structure */
	xfs_inode_t		*dp;		/* incore directory inode */
	int			error;		/* error return code */
	xfs_dir2_db_t		i;		/* temporary data block # */
	int			index;		/* index into leaf entries */
1355
	struct xfs_buf		*lbp;		/* leaf buffer */
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360 1361
	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
	int			needlog;	/* need to log data header */
	int			needscan;	/* need to rescan data frees */
	xfs_dir2_data_off_t	oldbest;	/* old value of best free */
1362
	struct xfs_dir2_data_free *bf;		/* bestfree table */
1363
	struct xfs_dir3_icleaf_hdr leafhdr;
L
Linus Torvalds 已提交
1364

C
Christoph Hellwig 已提交
1365 1366
	trace_xfs_dir2_leaf_removename(args);

L
Linus Torvalds 已提交
1367 1368 1369
	/*
	 * Lookup the leaf entry, get the leaf and data blocks read in.
	 */
1370 1371
	error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
	if (error)
L
Linus Torvalds 已提交
1372
		return error;
1373

L
Linus Torvalds 已提交
1374
	dp = args->dp;
1375 1376
	leaf = lbp->b_addr;
	hdr = dbp->b_addr;
1377
	xfs_dir3_data_check(dp, dbp);
1378
	bf = dp->d_ops->data_bestfree_p(hdr);
1379

L
Linus Torvalds 已提交
1380 1381 1382
	/*
	 * Point to the leaf entry, use that to point to the data entry.
	 */
1383
	lep = &leafhdr.ents[index];
1384 1385 1386
	db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
	dep = (xfs_dir2_data_entry_t *)((char *)hdr +
		xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
L
Linus Torvalds 已提交
1387
	needscan = needlog = 0;
1388
	oldbest = be16_to_cpu(bf[0].length);
1389
	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1390
	bestsp = xfs_dir2_leaf_bests_p(ltp);
1391 1392
	if (be16_to_cpu(bestsp[db]) != oldbest) {
		xfs_buf_corruption_error(lbp);
1393
		return -EFSCORRUPTED;
1394
	}
L
Linus Torvalds 已提交
1395 1396 1397
	/*
	 * Mark the former data entry unused.
	 */
1398
	xfs_dir2_data_make_free(args, dbp,
1399
		(xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1400
		dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
L
Linus Torvalds 已提交
1401 1402 1403
	/*
	 * We just mark the leaf entry stale by putting a null in it.
	 */
1404
	leafhdr.stale++;
1405
	xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1406
	xfs_dir3_leaf_log_header(args, lbp);
1407

1408
	lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1409
	xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, index, index);
1410

L
Linus Torvalds 已提交
1411 1412 1413 1414 1415
	/*
	 * Scan the freespace in the data block again if necessary,
	 * log the data block header if necessary.
	 */
	if (needscan)
1416
		xfs_dir2_data_freescan(dp, hdr, &needlog);
L
Linus Torvalds 已提交
1417
	if (needlog)
1418
		xfs_dir2_data_log_header(args, dbp);
L
Linus Torvalds 已提交
1419 1420 1421 1422
	/*
	 * If the longest freespace in the data block has changed,
	 * put the new value in the bests table and log that.
	 */
1423 1424
	if (be16_to_cpu(bf[0].length) != oldbest) {
		bestsp[db] = bf[0].length;
1425
		xfs_dir3_leaf_log_bests(args, lbp, db, db);
L
Linus Torvalds 已提交
1426
	}
1427
	xfs_dir3_data_check(dp, dbp);
L
Linus Torvalds 已提交
1428 1429 1430
	/*
	 * If the data block is now empty then get rid of the data block.
	 */
1431
	if (be16_to_cpu(bf[0].length) ==
1432
			args->geo->blksize - dp->d_ops->data_entry_offset) {
1433
		ASSERT(db != args->geo->datablk);
L
Linus Torvalds 已提交
1434 1435 1436 1437 1438 1439 1440
		if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
			/*
			 * Nope, can't get rid of it because it caused
			 * allocation of a bmap btree block to do so.
			 * Just go on, returning success, leaving the
			 * empty block in place.
			 */
D
Dave Chinner 已提交
1441
			if (error == -ENOSPC && args->total == 0)
L
Linus Torvalds 已提交
1442
				error = 0;
1443
			xfs_dir3_leaf_check(dp, lbp);
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449 1450
			return error;
		}
		dbp = NULL;
		/*
		 * If this is the last data block then compact the
		 * bests table by getting rid of entries.
		 */
1451
		if (db == be32_to_cpu(ltp->bestcount) - 1) {
L
Linus Torvalds 已提交
1452 1453 1454 1455
			/*
			 * Look for the last active entry (i).
			 */
			for (i = db - 1; i > 0; i--) {
1456
				if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
L
Linus Torvalds 已提交
1457 1458 1459 1460 1461 1462 1463
					break;
			}
			/*
			 * Copy the table down so inactive entries at the
			 * end are removed.
			 */
			memmove(&bestsp[db - i], bestsp,
1464
				(be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1465
			be32_add_cpu(&ltp->bestcount, -(db - i));
1466 1467 1468
			xfs_dir3_leaf_log_tail(args, lbp);
			xfs_dir3_leaf_log_bests(args, lbp, 0,
						be32_to_cpu(ltp->bestcount) - 1);
L
Linus Torvalds 已提交
1469
		} else
1470
			bestsp[db] = cpu_to_be16(NULLDATAOFF);
L
Linus Torvalds 已提交
1471 1472 1473 1474
	}
	/*
	 * If the data block was not the first one, drop it.
	 */
1475
	else if (db != args->geo->datablk)
L
Linus Torvalds 已提交
1476
		dbp = NULL;
1477

1478
	xfs_dir3_leaf_check(dp, lbp);
L
Linus Torvalds 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
	/*
	 * See if we can convert to block form.
	 */
	return xfs_dir2_leaf_to_block(args, lbp, dbp);
}

/*
 * Replace the inode number in a leaf format directory entry.
 */
int						/* error */
xfs_dir2_leaf_replace(
	xfs_da_args_t		*args)		/* operation arguments */
{
1492
	struct xfs_buf		*dbp;		/* data block buffer */
L
Linus Torvalds 已提交
1493 1494 1495 1496
	xfs_dir2_data_entry_t	*dep;		/* data block entry */
	xfs_inode_t		*dp;		/* incore directory inode */
	int			error;		/* error return code */
	int			index;		/* index of leaf entry */
1497
	struct xfs_buf		*lbp;		/* leaf buffer */
L
Linus Torvalds 已提交
1498 1499
	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
	xfs_trans_t		*tp;		/* transaction pointer */
1500
	struct xfs_dir3_icleaf_hdr leafhdr;
L
Linus Torvalds 已提交
1501

C
Christoph Hellwig 已提交
1502 1503
	trace_xfs_dir2_leaf_replace(args);

L
Linus Torvalds 已提交
1504 1505 1506
	/*
	 * Look up the entry.
	 */
1507 1508
	error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
	if (error)
L
Linus Torvalds 已提交
1509
		return error;
1510

L
Linus Torvalds 已提交
1511 1512 1513 1514
	dp = args->dp;
	/*
	 * Point to the leaf entry, get data address from it.
	 */
1515
	lep = &leafhdr.ents[index];
L
Linus Torvalds 已提交
1516 1517 1518 1519
	/*
	 * Point to the data entry.
	 */
	dep = (xfs_dir2_data_entry_t *)
1520
	      ((char *)dbp->b_addr +
1521
	       xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1522
	ASSERT(args->inumber != be64_to_cpu(dep->inumber));
L
Linus Torvalds 已提交
1523 1524 1525
	/*
	 * Put the new inode number in, log it.
	 */
1526
	dep->inumber = cpu_to_be64(args->inumber);
1527
	dp->d_ops->data_put_ftype(dep, args->filetype);
L
Linus Torvalds 已提交
1528
	tp = args->trans;
1529
	xfs_dir2_data_log_entry(args, dbp, dep);
1530
	xfs_dir3_leaf_check(dp, lbp);
1531
	xfs_trans_brelse(tp, lbp);
L
Linus Torvalds 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
	return 0;
}

/*
 * Return index in the leaf block (lbp) which is either the first
 * one with this hash value, or if there are none, the insert point
 * for that hash value.
 */
int						/* index value */
xfs_dir2_leaf_search_hash(
	xfs_da_args_t		*args,		/* operation arguments */
1543
	struct xfs_buf		*lbp)		/* leaf buffer */
L
Linus Torvalds 已提交
1544 1545 1546 1547 1548 1549 1550
{
	xfs_dahash_t		hash=0;		/* hash from this entry */
	xfs_dahash_t		hashwant;	/* hash value looking for */
	int			high;		/* high leaf index */
	int			low;		/* low leaf index */
	xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */
	int			mid=0;		/* current leaf index */
1551
	struct xfs_dir3_icleaf_hdr leafhdr;
L
Linus Torvalds 已提交
1552

1553
	xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, lbp->b_addr);
1554

L
Linus Torvalds 已提交
1555 1556 1557 1558
	/*
	 * Note, the table cannot be empty, so we have to go through the loop.
	 * Binary search the leaf entries looking for our hash value.
	 */
1559
	for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1,
L
Linus Torvalds 已提交
1560 1561 1562
		hashwant = args->hashval;
	     low <= high; ) {
		mid = (low + high) >> 1;
1563
		if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
L
Linus Torvalds 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
			break;
		if (hash < hashwant)
			low = mid + 1;
		else
			high = mid - 1;
	}
	/*
	 * Found one, back up through all the equal hash values.
	 */
	if (hash == hashwant) {
1574
		while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
L
Linus Torvalds 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
			mid--;
		}
	}
	/*
	 * Need to point to an entry higher than ours.
	 */
	else if (hash < hashwant)
		mid++;
	return mid;
}

/*
 * Trim off a trailing data block.  We know it's empty since the leaf
 * freespace table says so.
 */
int						/* error */
xfs_dir2_leaf_trim_data(
	xfs_da_args_t		*args,		/* operation arguments */
1593
	struct xfs_buf		*lbp,		/* leaf buffer */
L
Linus Torvalds 已提交
1594 1595
	xfs_dir2_db_t		db)		/* data block number */
{
1596
	__be16			*bestsp;	/* leaf bests table */
1597
	struct xfs_buf		*dbp;		/* data block buffer */
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
	xfs_inode_t		*dp;		/* incore directory inode */
	int			error;		/* error return value */
	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
	xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */
	xfs_trans_t		*tp;		/* transaction pointer */

	dp = args->dp;
	tp = args->trans;
	/*
	 * Read the offending data block.  We need its buffer.
	 */
1609 1610
	error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(args->geo, db),
				   -1, &dbp);
1611
	if (error)
L
Linus Torvalds 已提交
1612 1613
		return error;

1614
	leaf = lbp->b_addr;
1615
	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1616 1617 1618

#ifdef DEBUG
{
1619
	struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1620
	struct xfs_dir2_data_free *bf = dp->d_ops->data_bestfree_p(hdr);
1621

1622 1623 1624
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
	ASSERT(be16_to_cpu(bf[0].length) ==
1625
	       args->geo->blksize - dp->d_ops->data_entry_offset);
1626
	ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1627 1628 1629
}
#endif

L
Linus Torvalds 已提交
1630 1631 1632 1633
	/*
	 * Get rid of the data block.
	 */
	if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
D
Dave Chinner 已提交
1634
		ASSERT(error != -ENOSPC);
1635
		xfs_trans_brelse(tp, dbp);
L
Linus Torvalds 已提交
1636 1637 1638 1639 1640
		return error;
	}
	/*
	 * Eliminate the last bests entry from the table.
	 */
1641
	bestsp = xfs_dir2_leaf_bests_p(ltp);
1642
	be32_add_cpu(&ltp->bestcount, -1);
1643
	memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1644 1645
	xfs_dir3_leaf_log_tail(args, lbp);
	xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
L
Linus Torvalds 已提交
1646 1647 1648
	return 0;
}

1649
static inline size_t
1650 1651
xfs_dir3_leaf_size(
	struct xfs_dir3_icleaf_hdr	*hdr,
1652 1653
	int				counts)
{
1654 1655 1656 1657 1658 1659 1660 1661 1662
	int	entries;
	int	hdrsize;

	entries = hdr->count - hdr->stale;
	if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
	    hdr->magic == XFS_DIR2_LEAFN_MAGIC)
		hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
	else
		hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1663

1664 1665 1666
	return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
	               + counts * sizeof(xfs_dir2_data_off_t)
		       + sizeof(xfs_dir2_leaf_tail_t);
1667 1668
}

L
Linus Torvalds 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
/*
 * Convert node form directory to leaf form directory.
 * The root of the node form dir needs to already be a LEAFN block.
 * Just return if we can't do anything.
 */
int						/* error */
xfs_dir2_node_to_leaf(
	xfs_da_state_t		*state)		/* directory operation state */
{
	xfs_da_args_t		*args;		/* operation arguments */
	xfs_inode_t		*dp;		/* incore directory inode */
	int			error;		/* error return code */
1681
	struct xfs_buf		*fbp;		/* buffer for freespace block */
L
Linus Torvalds 已提交
1682 1683
	xfs_fileoff_t		fo;		/* freespace file offset */
	xfs_dir2_free_t		*free;		/* freespace structure */
1684
	struct xfs_buf		*lbp;		/* buffer for leaf block */
L
Linus Torvalds 已提交
1685 1686 1687 1688 1689
	xfs_dir2_leaf_tail_t	*ltp;		/* tail of leaf structure */
	xfs_dir2_leaf_t		*leaf;		/* leaf structure */
	xfs_mount_t		*mp;		/* filesystem mount point */
	int			rval;		/* successful free trim? */
	xfs_trans_t		*tp;		/* transaction pointer */
1690
	struct xfs_dir3_icleaf_hdr leafhdr;
1691
	struct xfs_dir3_icfree_hdr freehdr;
L
Linus Torvalds 已提交
1692 1693 1694 1695 1696 1697 1698 1699

	/*
	 * There's more than a leaf level in the btree, so there must
	 * be multiple leafn blocks.  Give up.
	 */
	if (state->path.active > 1)
		return 0;
	args = state->args;
C
Christoph Hellwig 已提交
1700 1701 1702

	trace_xfs_dir2_node_to_leaf(args);

L
Linus Torvalds 已提交
1703 1704 1705 1706 1707 1708
	mp = state->mp;
	dp = args->dp;
	tp = args->trans;
	/*
	 * Get the last offset in the file.
	 */
1709
	if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
L
Linus Torvalds 已提交
1710 1711
		return error;
	}
1712
	fo -= args->geo->fsbcount;
L
Linus Torvalds 已提交
1713 1714 1715 1716 1717 1718
	/*
	 * If there are freespace blocks other than the first one,
	 * take this opportunity to remove trailing empty freespace blocks
	 * that may have been left behind during no-space-reservation
	 * operations.
	 */
1719
	while (fo > args->geo->freeblk) {
L
Linus Torvalds 已提交
1720 1721 1722 1723
		if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
			return error;
		}
		if (rval)
1724
			fo -= args->geo->fsbcount;
L
Linus Torvalds 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
		else
			return 0;
	}
	/*
	 * Now find the block just before the freespace block.
	 */
	if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
		return error;
	}
	/*
	 * If it's not the single leaf block, give up.
	 */
1737
	if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
L
Linus Torvalds 已提交
1738 1739
		return 0;
	lbp = state->path.blk[0].bp;
1740
	leaf = lbp->b_addr;
1741
	xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
1742 1743 1744 1745

	ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
	       leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);

L
Linus Torvalds 已提交
1746 1747 1748
	/*
	 * Read the freespace block.
	 */
1749
	error = xfs_dir2_free_read(tp, dp,  args->geo->freeblk, &fbp);
1750
	if (error)
L
Linus Torvalds 已提交
1751
		return error;
1752
	free = fbp->b_addr;
1753
	xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
1754 1755

	ASSERT(!freehdr.firstdb);
1756

L
Linus Torvalds 已提交
1757 1758 1759 1760
	/*
	 * Now see if the leafn and free data will fit in a leaf1.
	 * If not, release the buffer and give up.
	 */
1761
	if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1762
		xfs_trans_brelse(tp, fbp);
L
Linus Torvalds 已提交
1763 1764
		return 0;
	}
1765

L
Linus Torvalds 已提交
1766 1767 1768
	/*
	 * If the leaf has any stale entries in it, compress them out.
	 */
1769 1770
	if (leafhdr.stale)
		xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1771

1772
	lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1773
	xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1774 1775 1776
	leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
					? XFS_DIR2_LEAF1_MAGIC
					: XFS_DIR3_LEAF1_MAGIC;
1777

L
Linus Torvalds 已提交
1778 1779 1780
	/*
	 * Set up the leaf tail from the freespace block.
	 */
1781
	ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1782
	ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1783

L
Linus Torvalds 已提交
1784 1785 1786
	/*
	 * Set up the leaf bests table.
	 */
1787
	memcpy(xfs_dir2_leaf_bests_p(ltp), dp->d_ops->free_bests_p(free),
1788
		freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1789

1790
	xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
1791 1792 1793
	xfs_dir3_leaf_log_header(args, lbp);
	xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
	xfs_dir3_leaf_log_tail(args, lbp);
1794
	xfs_dir3_leaf_check(dp, lbp);
1795

L
Linus Torvalds 已提交
1796 1797 1798
	/*
	 * Get rid of the freespace block.
	 */
1799
	error = xfs_dir2_shrink_inode(args,
1800 1801
			xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
			fbp);
L
Linus Torvalds 已提交
1802 1803 1804 1805 1806 1807
	if (error) {
		/*
		 * This can't fail here because it can only happen when
		 * punching out the middle of an extent, and this is an
		 * isolated block.
		 */
D
Dave Chinner 已提交
1808
		ASSERT(error != -ENOSPC);
L
Linus Torvalds 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
		return error;
	}
	fbp = NULL;
	/*
	 * Now see if we can convert the single-leaf directory
	 * down to a block form directory.
	 * This routine always kills the dabuf for the leaf, so
	 * eliminate it from the path.
	 */
	error = xfs_dir2_leaf_to_block(args, lbp, NULL);
	state->path.blk[0].bp = NULL;
	return error;
}