xfs_dir2_data.c 30.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3
 * Copyright (c) 2013 Red Hat, Inc.
4
 * All Rights Reserved.
L
Linus Torvalds 已提交
5
 *
6 7
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
8 9
 * published by the Free Software Foundation.
 *
10 11 12 13
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
14
 *
15 16 17
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
18 19
 */
#include "xfs.h"
20
#include "xfs_fs.h"
21
#include "xfs_format.h"
22 23
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
L
Linus Torvalds 已提交
24
#include "xfs_mount.h"
25
#include "xfs_da_format.h"
26
#include "xfs_da_btree.h"
L
Linus Torvalds 已提交
27
#include "xfs_inode.h"
28
#include "xfs_dir2.h"
C
Christoph Hellwig 已提交
29
#include "xfs_dir2_priv.h"
L
Linus Torvalds 已提交
30
#include "xfs_error.h"
31
#include "xfs_trans.h"
32 33
#include "xfs_buf_item.h"
#include "xfs_cksum.h"
34
#include "xfs_log.h"
L
Linus Torvalds 已提交
35 36 37 38

/*
 * Check the consistency of the data block.
 * The input can also be a block-format directory.
39
 * Return 0 is the buffer is good, otherwise an error.
L
Linus Torvalds 已提交
40
 */
41
int
42
__xfs_dir3_data_check(
43 44
	struct xfs_inode	*dp,		/* incore inode pointer */
	struct xfs_buf		*bp)		/* data block's buffer */
L
Linus Torvalds 已提交
45 46 47 48 49
{
	xfs_dir2_dataptr_t	addr;		/* addr for leaf lookup */
	xfs_dir2_data_free_t	*bf;		/* bestfree table */
	xfs_dir2_block_tail_t	*btp=NULL;	/* block tail */
	int			count;		/* count of entries found */
50
	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62
	xfs_dir2_data_entry_t	*dep;		/* data entry */
	xfs_dir2_data_free_t	*dfp;		/* bestfree entry */
	xfs_dir2_data_unused_t	*dup;		/* unused entry */
	char			*endp;		/* end of useful data */
	int			freeseen;	/* mask of bestfrees seen */
	xfs_dahash_t		hash;		/* hash of current name */
	int			i;		/* leaf index */
	int			lastfree;	/* last entry was unused */
	xfs_dir2_leaf_entry_t	*lep=NULL;	/* block leaf entries */
	xfs_mount_t		*mp;		/* filesystem mount point */
	char			*p;		/* current data position */
	int			stale;		/* count of stale leaves */
63
	struct xfs_name		name;
64
	const struct xfs_dir_ops *ops;
65
	struct xfs_da_geometry	*geo;
L
Linus Torvalds 已提交
66

67
	mp = bp->b_target->bt_mount;
68
	geo = mp->m_dir_geo;
69

70
	/*
71 72
	 * We can be passed a null dp here from a verifier, so we need to go the
	 * hard way to get them.
73
	 */
74
	ops = xfs_dir_get_ops(mp, dp);
75

76 77 78
	hdr = bp->b_addr;
	p = (char *)ops->data_entry_p(hdr);

79
	switch (hdr->magic) {
80
	case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
81
	case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
82
		btp = xfs_dir2_block_tail_p(geo, hdr);
83
		lep = xfs_dir2_block_leaf_p(btp);
L
Linus Torvalds 已提交
84
		endp = (char *)lep;
85 86 87 88 89 90 91 92

		/*
		 * The number of leaf entries is limited by the size of the
		 * block and the amount of space used by the data entries.
		 * We don't know how much space is used by the data entries yet,
		 * so just ensure that the count falls somewhere inside the
		 * block right now.
		 */
93
		XFS_WANT_CORRUPTED_RETURN(mp, be32_to_cpu(btp->count) <
94
			((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry));
95
		break;
96
	case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
97
	case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
98
		endp = (char *)hdr + geo->blksize;
99 100 101
		break;
	default:
		XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
D
Dave Chinner 已提交
102
		return -EFSCORRUPTED;
103 104
	}

L
Linus Torvalds 已提交
105 106 107
	/*
	 * Account for zero bestfree entries.
	 */
108 109
	bf = ops->data_bestfree_p(hdr);
	count = lastfree = freeseen = 0;
L
Linus Torvalds 已提交
110
	if (!bf[0].length) {
111
		XFS_WANT_CORRUPTED_RETURN(mp, !bf[0].offset);
L
Linus Torvalds 已提交
112 113 114
		freeseen |= 1 << 0;
	}
	if (!bf[1].length) {
115
		XFS_WANT_CORRUPTED_RETURN(mp, !bf[1].offset);
L
Linus Torvalds 已提交
116 117 118
		freeseen |= 1 << 1;
	}
	if (!bf[2].length) {
119
		XFS_WANT_CORRUPTED_RETURN(mp, !bf[2].offset);
L
Linus Torvalds 已提交
120 121
		freeseen |= 1 << 2;
	}
122

123
	XFS_WANT_CORRUPTED_RETURN(mp, be16_to_cpu(bf[0].length) >=
124
						be16_to_cpu(bf[1].length));
125
	XFS_WANT_CORRUPTED_RETURN(mp, be16_to_cpu(bf[1].length) >=
126
						be16_to_cpu(bf[2].length));
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135 136
	/*
	 * Loop over the data/unused entries.
	 */
	while (p < endp) {
		dup = (xfs_dir2_data_unused_t *)p;
		/*
		 * If it's unused, look for the space in the bestfree table.
		 * If we find it, account for that, else make sure it
		 * doesn't need to be there.
		 */
137
		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
138
			XFS_WANT_CORRUPTED_RETURN(mp, lastfree == 0);
139 140
			XFS_WANT_CORRUPTED_RETURN(mp, endp >=
					p + be16_to_cpu(dup->length));
141
			XFS_WANT_CORRUPTED_RETURN(mp,
142 143
				be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
					       (char *)dup - (char *)hdr);
144
			dfp = xfs_dir2_data_freefind(hdr, bf, dup);
L
Linus Torvalds 已提交
145 146
			if (dfp) {
				i = (int)(dfp - bf);
147
				XFS_WANT_CORRUPTED_RETURN(mp,
148
					(freeseen & (1 << i)) == 0);
L
Linus Torvalds 已提交
149
				freeseen |= 1 << i;
150
			} else {
151
				XFS_WANT_CORRUPTED_RETURN(mp,
152 153
					be16_to_cpu(dup->length) <=
						be16_to_cpu(bf[2].length));
154
			}
155
			p += be16_to_cpu(dup->length);
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165
			lastfree = 1;
			continue;
		}
		/*
		 * It's a real entry.  Validate the fields.
		 * If this is a block directory then make sure it's
		 * in the leaf section of the block.
		 * The linear search is crude but this is DEBUG code.
		 */
		dep = (xfs_dir2_data_entry_t *)p;
166 167
		XFS_WANT_CORRUPTED_RETURN(mp, dep->namelen != 0);
		XFS_WANT_CORRUPTED_RETURN(mp,
168
			!xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
169 170
		XFS_WANT_CORRUPTED_RETURN(mp, endp >=
				p + ops->data_entsize(dep->namelen));
171
		XFS_WANT_CORRUPTED_RETURN(mp,
172
			be16_to_cpu(*ops->data_entry_tag_p(dep)) ==
173
					       (char *)dep - (char *)hdr);
174
		XFS_WANT_CORRUPTED_RETURN(mp,
175
				ops->data_get_ftype(dep) < XFS_DIR3_FT_MAX);
L
Linus Torvalds 已提交
176 177
		count++;
		lastfree = 0;
178 179
		if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
		    hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
180 181 182
			addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
						(xfs_dir2_data_aoff_t)
						((char *)dep - (char *)hdr));
183 184 185
			name.name = dep->name;
			name.len = dep->namelen;
			hash = mp->m_dirnameops->hashname(&name);
186
			for (i = 0; i < be32_to_cpu(btp->count); i++) {
187 188
				if (be32_to_cpu(lep[i].address) == addr &&
				    be32_to_cpu(lep[i].hashval) == hash)
L
Linus Torvalds 已提交
189 190
					break;
			}
191 192
			XFS_WANT_CORRUPTED_RETURN(mp,
						  i < be32_to_cpu(btp->count));
L
Linus Torvalds 已提交
193
		}
194
		p += ops->data_entsize(dep->namelen);
L
Linus Torvalds 已提交
195 196 197 198
	}
	/*
	 * Need to have seen all the entries and all the bestfree slots.
	 */
199
	XFS_WANT_CORRUPTED_RETURN(mp, freeseen == 7);
200 201
	if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	    hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
202
		for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
203 204
			if (lep[i].address ==
			    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
L
Linus Torvalds 已提交
205 206
				stale++;
			if (i > 0)
207
				XFS_WANT_CORRUPTED_RETURN(mp,
208 209
					be32_to_cpu(lep[i].hashval) >=
						be32_to_cpu(lep[i - 1].hashval));
L
Linus Torvalds 已提交
210
		}
211
		XFS_WANT_CORRUPTED_RETURN(mp, count ==
212
			be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
213
		XFS_WANT_CORRUPTED_RETURN(mp, stale == be32_to_cpu(btp->stale));
L
Linus Torvalds 已提交
214
	}
215
	return 0;
L
Linus Torvalds 已提交
216 217
}

218 219
static bool
xfs_dir3_data_verify(
220 221 222
	struct xfs_buf		*bp)
{
	struct xfs_mount	*mp = bp->b_target->bt_mount;
223
	struct xfs_dir3_blk_hdr	*hdr3 = bp->b_addr;
224

225 226 227
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
			return false;
228
		if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
229 230 231
			return false;
		if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
			return false;
232 233
		if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
			return false;
234 235 236
	} else {
		if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
			return false;
237
	}
238 239 240
	if (__xfs_dir3_data_check(NULL, bp))
		return false;
	return true;
241 242
}

243 244 245 246 247 248
/*
 * Readahead of the first block of the directory when it is opened is completely
 * oblivious to the format of the directory. Hence we can either get a block
 * format buffer or a data format buffer on readahead.
 */
static void
249
xfs_dir3_data_reada_verify(
250 251 252 253 254 255
	struct xfs_buf		*bp)
{
	struct xfs_dir2_data_hdr *hdr = bp->b_addr;

	switch (hdr->magic) {
	case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
256 257
	case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
		bp->b_ops = &xfs_dir3_block_buf_ops;
258 259 260
		bp->b_ops->verify_read(bp);
		return;
	case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
261
	case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
262 263
		bp->b_ops = &xfs_dir3_data_buf_ops;
		bp->b_ops->verify_read(bp);
264 265
		return;
	default:
D
Dave Chinner 已提交
266
		xfs_buf_ioerror(bp, -EFSCORRUPTED);
267
		xfs_verifier_error(bp);
268 269 270 271 272
		break;
	}
}

static void
273
xfs_dir3_data_read_verify(
274 275
	struct xfs_buf	*bp)
{
276 277
	struct xfs_mount	*mp = bp->b_target->bt_mount;

278 279
	if (xfs_sb_version_hascrc(&mp->m_sb) &&
	     !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
D
Dave Chinner 已提交
280
		 xfs_buf_ioerror(bp, -EFSBADCRC);
281
	else if (!xfs_dir3_data_verify(bp))
D
Dave Chinner 已提交
282
		xfs_buf_ioerror(bp, -EFSCORRUPTED);
283 284 285

	if (bp->b_error)
		xfs_verifier_error(bp);
286
}
287

288
static void
289
xfs_dir3_data_write_verify(
290 291
	struct xfs_buf	*bp)
{
292 293 294 295 296
	struct xfs_mount	*mp = bp->b_target->bt_mount;
	struct xfs_buf_log_item	*bip = bp->b_fspriv;
	struct xfs_dir3_blk_hdr	*hdr3 = bp->b_addr;

	if (!xfs_dir3_data_verify(bp)) {
D
Dave Chinner 已提交
297
		xfs_buf_ioerror(bp, -EFSCORRUPTED);
298
		xfs_verifier_error(bp);
299 300 301 302 303 304 305 306 307
		return;
	}

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

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

308
	xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
309 310
}

311
const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
312
	.name = "xfs_dir3_data",
313 314
	.verify_read = xfs_dir3_data_read_verify,
	.verify_write = xfs_dir3_data_write_verify,
315 316
};

317
static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
318
	.name = "xfs_dir3_data_reada",
319 320
	.verify_read = xfs_dir3_data_reada_verify,
	.verify_write = xfs_dir3_data_write_verify,
321 322
};

323

324
int
325
xfs_dir3_data_read(
326 327 328 329 330 331
	struct xfs_trans	*tp,
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mapped_bno,
	struct xfs_buf		**bpp)
{
332 333 334
	int			err;

	err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
335
				XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
336
	if (!err && tp && *bpp)
337
		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
338
	return err;
339 340
}

341
int
342
xfs_dir3_data_readahead(
343 344 345 346
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mapped_bno)
{
347
	return xfs_da_reada_buf(dp, bno, mapped_bno,
348
				XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
349 350
}

L
Linus Torvalds 已提交
351 352 353 354
/*
 * Given a data block and an unused entry from that block,
 * return the bestfree entry if any that corresponds to it.
 */
355
xfs_dir2_data_free_t *
L
Linus Torvalds 已提交
356
xfs_dir2_data_freefind(
357 358 359
	struct xfs_dir2_data_hdr *hdr,		/* data block header */
	struct xfs_dir2_data_free *bf,		/* bestfree table pointer */
	struct xfs_dir2_data_unused *dup)	/* unused space */
L
Linus Torvalds 已提交
360 361 362
{
	xfs_dir2_data_free_t	*dfp;		/* bestfree entry */
	xfs_dir2_data_aoff_t	off;		/* offset value needed */
363
#ifdef DEBUG
L
Linus Torvalds 已提交
364 365 366 367
	int			matched;	/* matched the value */
	int			seenzero;	/* saw a 0 bestfree entry */
#endif

368
	off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
369

370
#ifdef DEBUG
L
Linus Torvalds 已提交
371 372 373 374 375
	/*
	 * Validate some consistency in the bestfree table.
	 * Check order, non-overlapping entries, and if we find the
	 * one we're looking for it has to be exact.
	 */
376
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
377
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
378 379 380 381
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
	for (dfp = &bf[0], seenzero = matched = 0;
	     dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
L
Linus Torvalds 已提交
382 383 384 385 386 387 388
	     dfp++) {
		if (!dfp->offset) {
			ASSERT(!dfp->length);
			seenzero = 1;
			continue;
		}
		ASSERT(seenzero == 0);
389
		if (be16_to_cpu(dfp->offset) == off) {
L
Linus Torvalds 已提交
390
			matched = 1;
391
			ASSERT(dfp->length == dup->length);
392
		} else if (off < be16_to_cpu(dfp->offset))
393
			ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
L
Linus Torvalds 已提交
394
		else
395
			ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
396
		ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
397
		if (dfp > &bf[0])
398
			ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
L
Linus Torvalds 已提交
399 400 401 402 403 404
	}
#endif
	/*
	 * If this is smaller than the smallest bestfree entry,
	 * it can't be there since they're sorted.
	 */
405
	if (be16_to_cpu(dup->length) <
406
	    be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
L
Linus Torvalds 已提交
407 408 409 410
		return NULL;
	/*
	 * Look at the three bestfree entries for our guy.
	 */
411
	for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
L
Linus Torvalds 已提交
412 413
		if (!dfp->offset)
			return NULL;
414
		if (be16_to_cpu(dfp->offset) == off)
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427
			return dfp;
	}
	/*
	 * Didn't find it.  This only happens if there are duplicate lengths.
	 */
	return NULL;
}

/*
 * Insert an unused-space entry into the bestfree table.
 */
xfs_dir2_data_free_t *				/* entry inserted */
xfs_dir2_data_freeinsert(
428 429 430
	struct xfs_dir2_data_hdr *hdr,		/* data block pointer */
	struct xfs_dir2_data_free *dfp,		/* bestfree table pointer */
	struct xfs_dir2_data_unused *dup,	/* unused space */
L
Linus Torvalds 已提交
431 432 433 434
	int			*loghead)	/* log the data header (out) */
{
	xfs_dir2_data_free_t	new;		/* new bestfree entry */

435
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
436 437 438 439
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));

440
	new.length = dup->length;
441 442
	new.offset = cpu_to_be16((char *)dup - (char *)hdr);

L
Linus Torvalds 已提交
443 444 445
	/*
	 * Insert at position 0, 1, or 2; or not at all.
	 */
446
	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
L
Linus Torvalds 已提交
447 448 449 450 451 452
		dfp[2] = dfp[1];
		dfp[1] = dfp[0];
		dfp[0] = new;
		*loghead = 1;
		return &dfp[0];
	}
453
	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
L
Linus Torvalds 已提交
454 455 456 457 458
		dfp[2] = dfp[1];
		dfp[1] = new;
		*loghead = 1;
		return &dfp[1];
	}
459
	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
L
Linus Torvalds 已提交
460 461 462 463 464 465 466 467 468 469
		dfp[2] = new;
		*loghead = 1;
		return &dfp[2];
	}
	return NULL;
}

/*
 * Remove a bestfree entry from the table.
 */
470
STATIC void
L
Linus Torvalds 已提交
471
xfs_dir2_data_freeremove(
472 473 474
	struct xfs_dir2_data_hdr *hdr,		/* data block header */
	struct xfs_dir2_data_free *bf,		/* bestfree table pointer */
	struct xfs_dir2_data_free *dfp,		/* bestfree entry pointer */
L
Linus Torvalds 已提交
475 476
	int			*loghead)	/* out: log data header */
{
477

478
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
479 480 481 482
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));

L
Linus Torvalds 已提交
483 484 485
	/*
	 * It's the first entry, slide the next 2 up.
	 */
486 487 488
	if (dfp == &bf[0]) {
		bf[0] = bf[1];
		bf[1] = bf[2];
L
Linus Torvalds 已提交
489 490 491 492
	}
	/*
	 * It's the second entry, slide the 3rd entry up.
	 */
493 494
	else if (dfp == &bf[1])
		bf[1] = bf[2];
L
Linus Torvalds 已提交
495 496 497 498
	/*
	 * Must be the last entry.
	 */
	else
499
		ASSERT(dfp == &bf[2]);
L
Linus Torvalds 已提交
500 501 502
	/*
	 * Clear the 3rd entry, must be zero now.
	 */
503 504
	bf[2].length = 0;
	bf[2].offset = 0;
L
Linus Torvalds 已提交
505 506 507 508 509 510 511
	*loghead = 1;
}

/*
 * Given a data block, reconstruct its bestfree map.
 */
void
512 513 514
xfs_dir2_data_freescan_int(
	struct xfs_da_geometry	*geo,
	const struct xfs_dir_ops *ops,
515 516
	struct xfs_dir2_data_hdr *hdr,
	int			*loghead)
L
Linus Torvalds 已提交
517 518 519 520
{
	xfs_dir2_block_tail_t	*btp;		/* block tail */
	xfs_dir2_data_entry_t	*dep;		/* active data entry */
	xfs_dir2_data_unused_t	*dup;		/* unused data entry */
521
	struct xfs_dir2_data_free *bf;
L
Linus Torvalds 已提交
522 523 524
	char			*endp;		/* end of block's data */
	char			*p;		/* current entry pointer */

525
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
526
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
527 528 529
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));

L
Linus Torvalds 已提交
530 531 532
	/*
	 * Start by clearing the table.
	 */
533
	bf = ops->data_bestfree_p(hdr);
534
	memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
L
Linus Torvalds 已提交
535 536 537 538
	*loghead = 1;
	/*
	 * Set up pointers.
	 */
539
	p = (char *)ops->data_entry_p(hdr);
540 541
	if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	    hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
542
		btp = xfs_dir2_block_tail_p(geo, hdr);
543
		endp = (char *)xfs_dir2_block_leaf_p(btp);
L
Linus Torvalds 已提交
544
	} else
545
		endp = (char *)hdr + geo->blksize;
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553
	/*
	 * Loop over the block's entries.
	 */
	while (p < endp) {
		dup = (xfs_dir2_data_unused_t *)p;
		/*
		 * If it's a free entry, insert it.
		 */
554
		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
555
			ASSERT((char *)dup - (char *)hdr ==
556
			       be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
557
			xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
558
			p += be16_to_cpu(dup->length);
L
Linus Torvalds 已提交
559 560 561 562 563 564
		}
		/*
		 * For active entries, check their tags and skip them.
		 */
		else {
			dep = (xfs_dir2_data_entry_t *)p;
565
			ASSERT((char *)dep - (char *)hdr ==
566 567
			       be16_to_cpu(*ops->data_entry_tag_p(dep)));
			p += ops->data_entsize(dep->namelen);
L
Linus Torvalds 已提交
568 569 570 571
		}
	}
}

572 573 574 575 576 577 578 579 580 581
void
xfs_dir2_data_freescan(
	struct xfs_inode	*dp,
	struct xfs_dir2_data_hdr *hdr,
	int			*loghead)
{
	return xfs_dir2_data_freescan_int(dp->i_mount->m_dir_geo, dp->d_ops,
			hdr, loghead);
}

L
Linus Torvalds 已提交
582 583 584 585 586
/*
 * Initialize a data block at the given block number in the directory.
 * Give back the buffer for the created block.
 */
int						/* error */
587
xfs_dir3_data_init(
L
Linus Torvalds 已提交
588 589
	xfs_da_args_t		*args,		/* directory operation args */
	xfs_dir2_db_t		blkno,		/* logical dir block number */
590
	struct xfs_buf		**bpp)		/* output block buffer */
L
Linus Torvalds 已提交
591
{
592
	struct xfs_buf		*bp;		/* block buffer */
593
	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
L
Linus Torvalds 已提交
594 595
	xfs_inode_t		*dp;		/* incore directory inode */
	xfs_dir2_data_unused_t	*dup;		/* unused entry pointer */
596
	struct xfs_dir2_data_free *bf;
L
Linus Torvalds 已提交
597 598 599 600 601 602 603 604 605 606 607 608
	int			error;		/* error return value */
	int			i;		/* bestfree index */
	xfs_mount_t		*mp;		/* filesystem mount point */
	xfs_trans_t		*tp;		/* transaction pointer */
	int                     t;              /* temp */

	dp = args->dp;
	mp = dp->i_mount;
	tp = args->trans;
	/*
	 * Get the buffer set up for the block.
	 */
609 610
	error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
			       -1, &bp, XFS_DATA_FORK);
611
	if (error)
L
Linus Torvalds 已提交
612
		return error;
613
	bp->b_ops = &xfs_dir3_data_buf_ops;
614
	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
615

L
Linus Torvalds 已提交
616 617 618
	/*
	 * Initialize the header.
	 */
619
	hdr = bp->b_addr;
620 621 622 623 624 625 626
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;

		memset(hdr3, 0, sizeof(*hdr3));
		hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
		hdr3->blkno = cpu_to_be64(bp->b_bn);
		hdr3->owner = cpu_to_be64(dp->i_ino);
627
		uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
628 629 630 631

	} else
		hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);

632
	bf = dp->d_ops->data_bestfree_p(hdr);
633
	bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
L
Linus Torvalds 已提交
634
	for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
635 636
		bf[i].length = 0;
		bf[i].offset = 0;
L
Linus Torvalds 已提交
637
	}
638

L
Linus Torvalds 已提交
639 640 641
	/*
	 * Set up an unused entry for the block's body.
	 */
642
	dup = dp->d_ops->data_unused_p(hdr);
643
	dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
L
Linus Torvalds 已提交
644

645
	t = args->geo->blksize - (uint)dp->d_ops->data_entry_offset;
646
	bf[0].length = cpu_to_be16(t);
647
	dup->length = cpu_to_be16(t);
648
	*xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
L
Linus Torvalds 已提交
649 650 651
	/*
	 * Log it and return it.
	 */
652 653
	xfs_dir2_data_log_header(args, bp);
	xfs_dir2_data_log_unused(args, bp, dup);
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662
	*bpp = bp;
	return 0;
}

/*
 * Log an active data entry from the block.
 */
void
xfs_dir2_data_log_entry(
663
	struct xfs_da_args	*args,
664
	struct xfs_buf		*bp,
L
Linus Torvalds 已提交
665 666
	xfs_dir2_data_entry_t	*dep)		/* data entry pointer */
{
667
	struct xfs_dir2_data_hdr *hdr = bp->b_addr;
L
Linus Torvalds 已提交
668

669
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
670
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
671 672
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
673

674 675
	xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
		(uint)((char *)(args->dp->d_ops->data_entry_tag_p(dep) + 1) -
676
		       (char *)hdr - 1));
L
Linus Torvalds 已提交
677 678 679 680 681 682 683
}

/*
 * Log a data block header.
 */
void
xfs_dir2_data_log_header(
684
	struct xfs_da_args	*args,
685
	struct xfs_buf		*bp)
L
Linus Torvalds 已提交
686
{
687 688
#ifdef DEBUG
	struct xfs_dir2_data_hdr *hdr = bp->b_addr;
L
Linus Torvalds 已提交
689

690
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
691
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
692 693
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
694
#endif
695

696 697
	xfs_trans_log_buf(args->trans, bp, 0,
			  args->dp->d_ops->data_entry_offset - 1);
L
Linus Torvalds 已提交
698 699 700 701 702 703 704
}

/*
 * Log a data unused entry.
 */
void
xfs_dir2_data_log_unused(
705
	struct xfs_da_args	*args,
706
	struct xfs_buf		*bp,
L
Linus Torvalds 已提交
707 708
	xfs_dir2_data_unused_t	*dup)		/* data unused pointer */
{
709
	xfs_dir2_data_hdr_t	*hdr = bp->b_addr;
710

711
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
712
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
713 714
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
L
Linus Torvalds 已提交
715 716 717 718

	/*
	 * Log the first part of the unused entry.
	 */
719
	xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
L
Linus Torvalds 已提交
720
		(uint)((char *)&dup->length + sizeof(dup->length) -
721
		       1 - (char *)hdr));
L
Linus Torvalds 已提交
722 723 724
	/*
	 * Log the end (tag) of the unused entry.
	 */
725
	xfs_trans_log_buf(args->trans, bp,
726 727
		(uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
		(uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
L
Linus Torvalds 已提交
728 729 730 731 732 733 734 735 736
		       sizeof(xfs_dir2_data_off_t) - 1));
}

/*
 * Make a byte range in the data block unused.
 * Its current contents are unimportant.
 */
void
xfs_dir2_data_make_free(
737
	struct xfs_da_args	*args,
738
	struct xfs_buf		*bp,
L
Linus Torvalds 已提交
739 740 741 742 743
	xfs_dir2_data_aoff_t	offset,		/* starting byte offset */
	xfs_dir2_data_aoff_t	len,		/* length in bytes */
	int			*needlogp,	/* out: log header */
	int			*needscanp)	/* out: regen bestfree */
{
744
	xfs_dir2_data_hdr_t	*hdr;		/* data block pointer */
L
Linus Torvalds 已提交
745 746 747 748 749 750
	xfs_dir2_data_free_t	*dfp;		/* bestfree pointer */
	char			*endptr;	/* end of data area */
	int			needscan;	/* need to regen bestfree */
	xfs_dir2_data_unused_t	*newdup;	/* new unused entry */
	xfs_dir2_data_unused_t	*postdup;	/* unused entry after us */
	xfs_dir2_data_unused_t	*prevdup;	/* unused entry before us */
751
	struct xfs_dir2_data_free *bf;
L
Linus Torvalds 已提交
752

753
	hdr = bp->b_addr;
754

L
Linus Torvalds 已提交
755 756 757
	/*
	 * Figure out where the end of the data area is.
	 */
758 759
	if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
	    hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
760
		endptr = (char *)hdr + args->geo->blksize;
L
Linus Torvalds 已提交
761 762 763
	else {
		xfs_dir2_block_tail_t	*btp;	/* block tail */

764 765
		ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
			hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
766
		btp = xfs_dir2_block_tail_p(args->geo, hdr);
767
		endptr = (char *)xfs_dir2_block_leaf_p(btp);
L
Linus Torvalds 已提交
768 769 770 771 772
	}
	/*
	 * If this isn't the start of the block, then back up to
	 * the previous entry and see if it's free.
	 */
773
	if (offset > args->dp->d_ops->data_entry_offset) {
774
		__be16			*tagp;	/* tag just before us */
L
Linus Torvalds 已提交
775

776 777
		tagp = (__be16 *)((char *)hdr + offset) - 1;
		prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
778
		if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
L
Linus Torvalds 已提交
779 780 781 782 783 784 785
			prevdup = NULL;
	} else
		prevdup = NULL;
	/*
	 * If this isn't the end of the block, see if the entry after
	 * us is free.
	 */
786
	if ((char *)hdr + offset + len < endptr) {
L
Linus Torvalds 已提交
787
		postdup =
788
			(xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
789
		if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
L
Linus Torvalds 已提交
790 791 792 793 794 795 796 797 798
			postdup = NULL;
	} else
		postdup = NULL;
	ASSERT(*needscanp == 0);
	needscan = 0;
	/*
	 * Previous and following entries are both free,
	 * merge everything into a single free entry.
	 */
799
	bf = args->dp->d_ops->data_bestfree_p(hdr);
L
Linus Torvalds 已提交
800 801 802 803 804 805
	if (prevdup && postdup) {
		xfs_dir2_data_free_t	*dfp2;	/* another bestfree pointer */

		/*
		 * See if prevdup and/or postdup are in bestfree table.
		 */
806 807
		dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
		dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
L
Linus Torvalds 已提交
808 809 810 811 812 813
		/*
		 * We need a rescan unless there are exactly 2 free entries
		 * namely our two.  Then we know what's happening, otherwise
		 * since the third bestfree is there, there might be more
		 * entries.
		 */
814
		needscan = (bf[2].length != 0);
L
Linus Torvalds 已提交
815 816 817
		/*
		 * Fix up the new big freespace.
		 */
818
		be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
819
		*xfs_dir2_data_unused_tag_p(prevdup) =
820
			cpu_to_be16((char *)prevdup - (char *)hdr);
821
		xfs_dir2_data_log_unused(args, bp, prevdup);
L
Linus Torvalds 已提交
822 823 824 825 826 827 828 829
		if (!needscan) {
			/*
			 * Has to be the case that entries 0 and 1 are
			 * dfp and dfp2 (don't know which is which), and
			 * entry 2 is empty.
			 * Remove entry 1 first then entry 0.
			 */
			ASSERT(dfp && dfp2);
830 831
			if (dfp == &bf[1]) {
				dfp = &bf[0];
L
Linus Torvalds 已提交
832
				ASSERT(dfp2 == dfp);
833
				dfp2 = &bf[1];
L
Linus Torvalds 已提交
834
			}
835 836
			xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
L
Linus Torvalds 已提交
837 838 839
			/*
			 * Now insert the new entry.
			 */
840 841
			dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
						       needlogp);
842
			ASSERT(dfp == &bf[0]);
843
			ASSERT(dfp->length == prevdup->length);
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851
			ASSERT(!dfp[1].length);
			ASSERT(!dfp[2].length);
		}
	}
	/*
	 * The entry before us is free, merge with it.
	 */
	else if (prevdup) {
852
		dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
853
		be16_add_cpu(&prevdup->length, len);
854
		*xfs_dir2_data_unused_tag_p(prevdup) =
855
			cpu_to_be16((char *)prevdup - (char *)hdr);
856
		xfs_dir2_data_log_unused(args, bp, prevdup);
L
Linus Torvalds 已提交
857 858 859 860 861 862
		/*
		 * If the previous entry was in the table, the new entry
		 * is longer, so it will be in the table too.  Remove
		 * the old one and add the new one.
		 */
		if (dfp) {
863 864
			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
			xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
L
Linus Torvalds 已提交
865 866 867 868
		}
		/*
		 * Otherwise we need a scan if the new entry is big enough.
		 */
869
		else {
870
			needscan = be16_to_cpu(prevdup->length) >
871
				   be16_to_cpu(bf[2].length);
872
		}
L
Linus Torvalds 已提交
873 874 875 876 877
	}
	/*
	 * The following entry is free, merge with it.
	 */
	else if (postdup) {
878
		dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
879
		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
880 881
		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
		newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
882
		*xfs_dir2_data_unused_tag_p(newdup) =
883
			cpu_to_be16((char *)newdup - (char *)hdr);
884
		xfs_dir2_data_log_unused(args, bp, newdup);
L
Linus Torvalds 已提交
885 886 887 888 889 890
		/*
		 * If the following entry was in the table, the new entry
		 * is longer, so it will be in the table too.  Remove
		 * the old one and add the new one.
		 */
		if (dfp) {
891 892
			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
			xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
L
Linus Torvalds 已提交
893 894 895 896
		}
		/*
		 * Otherwise we need a scan if the new entry is big enough.
		 */
897
		else {
898
			needscan = be16_to_cpu(newdup->length) >
899
				   be16_to_cpu(bf[2].length);
900
		}
L
Linus Torvalds 已提交
901 902 903 904 905
	}
	/*
	 * Neither neighbor is free.  Make a new entry.
	 */
	else {
906
		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
907 908
		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
		newdup->length = cpu_to_be16(len);
909
		*xfs_dir2_data_unused_tag_p(newdup) =
910
			cpu_to_be16((char *)newdup - (char *)hdr);
911
		xfs_dir2_data_log_unused(args, bp, newdup);
912
		xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
L
Linus Torvalds 已提交
913 914 915 916 917 918 919 920 921
	}
	*needscanp = needscan;
}

/*
 * Take a byte range out of an existing unused space and make it un-free.
 */
void
xfs_dir2_data_use_free(
922
	struct xfs_da_args	*args,
923
	struct xfs_buf		*bp,
L
Linus Torvalds 已提交
924 925 926 927 928 929
	xfs_dir2_data_unused_t	*dup,		/* unused entry */
	xfs_dir2_data_aoff_t	offset,		/* starting offset to use */
	xfs_dir2_data_aoff_t	len,		/* length to use */
	int			*needlogp,	/* out: need to log header */
	int			*needscanp)	/* out: need regen bestfree */
{
930
	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
L
Linus Torvalds 已提交
931 932 933 934 935 936 937
	xfs_dir2_data_free_t	*dfp;		/* bestfree pointer */
	int			matchback;	/* matches end of freespace */
	int			matchfront;	/* matches start of freespace */
	int			needscan;	/* need to regen bestfree */
	xfs_dir2_data_unused_t	*newdup;	/* new unused entry */
	xfs_dir2_data_unused_t	*newdup2;	/* another new unused entry */
	int			oldlen;		/* old unused entry's length */
938
	struct xfs_dir2_data_free *bf;
L
Linus Torvalds 已提交
939

940
	hdr = bp->b_addr;
941
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
942
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
943 944
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
945
	ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
946 947 948
	ASSERT(offset >= (char *)dup - (char *)hdr);
	ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
	ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
L
Linus Torvalds 已提交
949 950 951
	/*
	 * Look up the entry in the bestfree table.
	 */
952
	oldlen = be16_to_cpu(dup->length);
953
	bf = args->dp->d_ops->data_bestfree_p(hdr);
954
	dfp = xfs_dir2_data_freefind(hdr, bf, dup);
955
	ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
L
Linus Torvalds 已提交
956 957 958
	/*
	 * Check for alignment with front and back of the entry.
	 */
959 960
	matchfront = (char *)dup - (char *)hdr == offset;
	matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
L
Linus Torvalds 已提交
961 962 963 964 965 966 967 968
	ASSERT(*needscanp == 0);
	needscan = 0;
	/*
	 * If we matched it exactly we just need to get rid of it from
	 * the bestfree table.
	 */
	if (matchfront && matchback) {
		if (dfp) {
969
			needscan = (bf[2].offset != 0);
L
Linus Torvalds 已提交
970
			if (!needscan)
971 972
				xfs_dir2_data_freeremove(hdr, bf, dfp,
							 needlogp);
L
Linus Torvalds 已提交
973 974 975 976 977 978 979
		}
	}
	/*
	 * We match the first part of the entry.
	 * Make a new entry with the remaining freespace.
	 */
	else if (matchfront) {
980
		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
981 982
		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
		newdup->length = cpu_to_be16(oldlen - len);
983
		*xfs_dir2_data_unused_tag_p(newdup) =
984
			cpu_to_be16((char *)newdup - (char *)hdr);
985
		xfs_dir2_data_log_unused(args, bp, newdup);
L
Linus Torvalds 已提交
986 987 988 989
		/*
		 * If it was in the table, remove it and add the new one.
		 */
		if (dfp) {
990 991 992
			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
			dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
						       needlogp);
L
Linus Torvalds 已提交
993
			ASSERT(dfp != NULL);
994
			ASSERT(dfp->length == newdup->length);
995
			ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
996 997 998 999 1000
			/*
			 * If we got inserted at the last slot,
			 * that means we don't know if there was a better
			 * choice for the last slot, or not.  Rescan.
			 */
1001
			needscan = dfp == &bf[2];
L
Linus Torvalds 已提交
1002 1003 1004 1005 1006 1007 1008 1009
		}
	}
	/*
	 * We match the last part of the entry.
	 * Trim the allocated space off the tail of the entry.
	 */
	else if (matchback) {
		newdup = dup;
1010
		newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1011
		*xfs_dir2_data_unused_tag_p(newdup) =
1012
			cpu_to_be16((char *)newdup - (char *)hdr);
1013
		xfs_dir2_data_log_unused(args, bp, newdup);
L
Linus Torvalds 已提交
1014 1015 1016 1017
		/*
		 * If it was in the table, remove it and add the new one.
		 */
		if (dfp) {
1018 1019 1020
			xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
			dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
						       needlogp);
L
Linus Torvalds 已提交
1021
			ASSERT(dfp != NULL);
1022
			ASSERT(dfp->length == newdup->length);
1023
			ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028
			/*
			 * If we got inserted at the last slot,
			 * that means we don't know if there was a better
			 * choice for the last slot, or not.  Rescan.
			 */
1029
			needscan = dfp == &bf[2];
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037
		}
	}
	/*
	 * Poking out the middle of an entry.
	 * Make two new entries.
	 */
	else {
		newdup = dup;
1038
		newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1039
		*xfs_dir2_data_unused_tag_p(newdup) =
1040
			cpu_to_be16((char *)newdup - (char *)hdr);
1041
		xfs_dir2_data_log_unused(args, bp, newdup);
1042
		newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1043 1044
		newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
		newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1045
		*xfs_dir2_data_unused_tag_p(newdup2) =
1046
			cpu_to_be16((char *)newdup2 - (char *)hdr);
1047
		xfs_dir2_data_log_unused(args, bp, newdup2);
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056
		/*
		 * If the old entry was in the table, we need to scan
		 * if the 3rd entry was valid, since these entries
		 * are smaller than the old one.
		 * If we don't need to scan that means there were 1 or 2
		 * entries in the table, and removing the old and adding
		 * the 2 new will work.
		 */
		if (dfp) {
1057
			needscan = (bf[2].length != 0);
L
Linus Torvalds 已提交
1058
			if (!needscan) {
1059 1060 1061 1062 1063
				xfs_dir2_data_freeremove(hdr, bf, dfp,
							 needlogp);
				xfs_dir2_data_freeinsert(hdr, bf, newdup,
							 needlogp);
				xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1064
							 needlogp);
L
Linus Torvalds 已提交
1065 1066 1067 1068 1069
			}
		}
	}
	*needscanp = needscan;
}