xfs_dir2_data.c 29.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"
L
Linus Torvalds 已提交
21 22 23 24
#include "xfs_types.h"
#include "xfs_log.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
25
#include "xfs_ag.h"
L
Linus Torvalds 已提交
26
#include "xfs_mount.h"
27
#include "xfs_da_btree.h"
L
Linus Torvalds 已提交
28 29 30
#include "xfs_bmap_btree.h"
#include "xfs_dinode.h"
#include "xfs_inode.h"
C
Christoph Hellwig 已提交
31
#include "xfs_dir2_format.h"
32
#include "xfs_dir2.h"
C
Christoph Hellwig 已提交
33
#include "xfs_dir2_priv.h"
L
Linus Torvalds 已提交
34
#include "xfs_error.h"
35 36
#include "xfs_buf_item.h"
#include "xfs_cksum.h"
L
Linus Torvalds 已提交
37 38 39 40

/*
 * Check the consistency of the data block.
 * The input can also be a block-format directory.
41
 * Return 0 is the buffer is good, otherwise an error.
L
Linus Torvalds 已提交
42
 */
43
int
44
__xfs_dir3_data_check(
45 46
	struct xfs_inode	*dp,		/* incore inode pointer */
	struct xfs_buf		*bp)		/* data block's buffer */
L
Linus Torvalds 已提交
47 48 49 50 51
{
	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 */
52
	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62 63 64
	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 */
65
	struct xfs_name		name;
L
Linus Torvalds 已提交
66

67
	mp = bp->b_target->bt_mount;
68
	hdr = bp->b_addr;
69 70
	bf = xfs_dir3_data_bestfree_p(hdr);
	p = (char *)xfs_dir3_data_entry_p(hdr);
71

72
	switch (hdr->magic) {
73
	case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
74
	case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
75
		btp = xfs_dir2_block_tail_p(mp, hdr);
76
		lep = xfs_dir2_block_leaf_p(btp);
L
Linus Torvalds 已提交
77
		endp = (char *)lep;
78
		break;
79
	case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
80
	case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
81
		endp = (char *)hdr + mp->m_dirblksize;
82 83 84 85
		break;
	default:
		XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
		return EFSCORRUPTED;
86 87
	}

L
Linus Torvalds 已提交
88 89 90 91 92
	count = lastfree = freeseen = 0;
	/*
	 * Account for zero bestfree entries.
	 */
	if (!bf[0].length) {
93
		XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
L
Linus Torvalds 已提交
94 95 96
		freeseen |= 1 << 0;
	}
	if (!bf[1].length) {
97
		XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
L
Linus Torvalds 已提交
98 99 100
		freeseen |= 1 << 1;
	}
	if (!bf[2].length) {
101
		XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
L
Linus Torvalds 已提交
102 103
		freeseen |= 1 << 2;
	}
104 105 106 107 108

	XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
						be16_to_cpu(bf[1].length));
	XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
						be16_to_cpu(bf[2].length));
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118
	/*
	 * 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.
		 */
119
		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
120 121 122 123
			XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
			XFS_WANT_CORRUPTED_RETURN(
				be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
					       (char *)dup - (char *)hdr);
124
			dfp = xfs_dir2_data_freefind(hdr, dup);
L
Linus Torvalds 已提交
125 126
			if (dfp) {
				i = (int)(dfp - bf);
127 128
				XFS_WANT_CORRUPTED_RETURN(
					(freeseen & (1 << i)) == 0);
L
Linus Torvalds 已提交
129
				freeseen |= 1 << i;
130
			} else {
131 132 133
				XFS_WANT_CORRUPTED_RETURN(
					be16_to_cpu(dup->length) <=
						be16_to_cpu(bf[2].length));
134
			}
135
			p += be16_to_cpu(dup->length);
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145
			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;
146 147 148 149
		XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
		XFS_WANT_CORRUPTED_RETURN(
			!xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
		XFS_WANT_CORRUPTED_RETURN(
150
			be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)) ==
151
					       (char *)dep - (char *)hdr);
152 153
		XFS_WANT_CORRUPTED_RETURN(
			xfs_dir3_dirent_get_ftype(mp, dep) < XFS_DIR3_FT_MAX);
L
Linus Torvalds 已提交
154 155
		count++;
		lastfree = 0;
156 157
		if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
		    hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
158
			addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
L
Linus Torvalds 已提交
159
				(xfs_dir2_data_aoff_t)
160
				((char *)dep - (char *)hdr));
161 162 163
			name.name = dep->name;
			name.len = dep->namelen;
			hash = mp->m_dirnameops->hashname(&name);
164
			for (i = 0; i < be32_to_cpu(btp->count); i++) {
165 166
				if (be32_to_cpu(lep[i].address) == addr &&
				    be32_to_cpu(lep[i].hashval) == hash)
L
Linus Torvalds 已提交
167 168
					break;
			}
169
			XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
L
Linus Torvalds 已提交
170
		}
171
		p += xfs_dir3_data_entsize(mp, dep->namelen);
L
Linus Torvalds 已提交
172 173 174 175
	}
	/*
	 * Need to have seen all the entries and all the bestfree slots.
	 */
176
	XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
177 178
	if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	    hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
179
		for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
180 181
			if (lep[i].address ==
			    cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
L
Linus Torvalds 已提交
182 183
				stale++;
			if (i > 0)
184 185 186
				XFS_WANT_CORRUPTED_RETURN(
					be32_to_cpu(lep[i].hashval) >=
						be32_to_cpu(lep[i - 1].hashval));
L
Linus Torvalds 已提交
187
		}
188 189 190
		XFS_WANT_CORRUPTED_RETURN(count ==
			be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
		XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
L
Linus Torvalds 已提交
191
	}
192
	return 0;
L
Linus Torvalds 已提交
193 194
}

195 196
static bool
xfs_dir3_data_verify(
197 198 199
	struct xfs_buf		*bp)
{
	struct xfs_mount	*mp = bp->b_target->bt_mount;
200
	struct xfs_dir3_blk_hdr	*hdr3 = bp->b_addr;
201

202 203 204 205 206 207 208 209 210 211
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
			return false;
		if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
			return false;
		if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
			return false;
	} else {
		if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
			return false;
212
	}
213 214 215
	if (__xfs_dir3_data_check(NULL, bp))
		return false;
	return true;
216 217
}

218 219 220 221 222 223
/*
 * 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
224
xfs_dir3_data_reada_verify(
225 226 227 228 229 230 231
	struct xfs_buf		*bp)
{
	struct xfs_mount	*mp = bp->b_target->bt_mount;
	struct xfs_dir2_data_hdr *hdr = bp->b_addr;

	switch (hdr->magic) {
	case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
232 233
	case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
		bp->b_ops = &xfs_dir3_block_buf_ops;
234 235 236
		bp->b_ops->verify_read(bp);
		return;
	case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
237 238
	case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
		xfs_dir3_data_verify(bp);
239 240 241 242 243 244 245 246 247
		return;
	default:
		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
		xfs_buf_ioerror(bp, EFSCORRUPTED);
		break;
	}
}

static void
248
xfs_dir3_data_read_verify(
249 250
	struct xfs_buf	*bp)
{
251 252 253 254 255 256 257 258 259
	struct xfs_mount	*mp = bp->b_target->bt_mount;

	if ((xfs_sb_version_hascrc(&mp->m_sb) &&
	     !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
					  XFS_DIR3_DATA_CRC_OFF)) ||
	    !xfs_dir3_data_verify(bp)) {
		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
		xfs_buf_ioerror(bp, EFSCORRUPTED);
	}
260
}
261

262
static void
263
xfs_dir3_data_write_verify(
264 265
	struct xfs_buf	*bp)
{
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	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)) {
		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
		xfs_buf_ioerror(bp, EFSCORRUPTED);
		return;
	}

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

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

	xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
283 284
}

285 286 287
const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
	.verify_read = xfs_dir3_data_read_verify,
	.verify_write = xfs_dir3_data_write_verify,
288 289
};

290 291 292
static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
	.verify_read = xfs_dir3_data_reada_verify,
	.verify_write = xfs_dir3_data_write_verify,
293 294
};

295

296
int
297
xfs_dir3_data_read(
298 299 300 301 302 303
	struct xfs_trans	*tp,
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mapped_bno,
	struct xfs_buf		**bpp)
{
304 305 306
	int			err;

	err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
307
				XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
308
	if (!err && tp)
309
		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
310
	return err;
311 312
}

313
int
314
xfs_dir3_data_readahead(
315 316 317 318 319 320
	struct xfs_trans	*tp,
	struct xfs_inode	*dp,
	xfs_dablk_t		bno,
	xfs_daddr_t		mapped_bno)
{
	return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
321
				XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
322 323
}

L
Linus Torvalds 已提交
324 325 326 327
/*
 * Given a data block and an unused entry from that block,
 * return the bestfree entry if any that corresponds to it.
 */
328
xfs_dir2_data_free_t *
L
Linus Torvalds 已提交
329
xfs_dir2_data_freefind(
330
	xfs_dir2_data_hdr_t	*hdr,		/* data block */
L
Linus Torvalds 已提交
331 332 333 334
	xfs_dir2_data_unused_t	*dup)		/* data unused entry */
{
	xfs_dir2_data_free_t	*dfp;		/* bestfree entry */
	xfs_dir2_data_aoff_t	off;		/* offset value needed */
335
	struct xfs_dir2_data_free *bf;
336
#ifdef DEBUG
L
Linus Torvalds 已提交
337 338 339 340
	int			matched;	/* matched the value */
	int			seenzero;	/* saw a 0 bestfree entry */
#endif

341
	off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
342 343
	bf = xfs_dir3_data_bestfree_p(hdr);

344
#ifdef DEBUG
L
Linus Torvalds 已提交
345 346 347 348 349
	/*
	 * 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.
	 */
350
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
351
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
352 353 354 355
	       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 已提交
356 357 358 359 360 361 362
	     dfp++) {
		if (!dfp->offset) {
			ASSERT(!dfp->length);
			seenzero = 1;
			continue;
		}
		ASSERT(seenzero == 0);
363
		if (be16_to_cpu(dfp->offset) == off) {
L
Linus Torvalds 已提交
364
			matched = 1;
365
			ASSERT(dfp->length == dup->length);
366
		} else if (off < be16_to_cpu(dfp->offset))
367
			ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
L
Linus Torvalds 已提交
368
		else
369
			ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
370
		ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
371
		if (dfp > &bf[0])
372
			ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
L
Linus Torvalds 已提交
373 374 375 376 377 378
	}
#endif
	/*
	 * If this is smaller than the smallest bestfree entry,
	 * it can't be there since they're sorted.
	 */
379
	if (be16_to_cpu(dup->length) <
380
	    be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
L
Linus Torvalds 已提交
381 382 383 384
		return NULL;
	/*
	 * Look at the three bestfree entries for our guy.
	 */
385
	for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
L
Linus Torvalds 已提交
386 387
		if (!dfp->offset)
			return NULL;
388
		if (be16_to_cpu(dfp->offset) == off)
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401
			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(
402
	xfs_dir2_data_hdr_t	*hdr,		/* data block pointer */
L
Linus Torvalds 已提交
403 404 405 406 407 408
	xfs_dir2_data_unused_t	*dup,		/* unused space */
	int			*loghead)	/* log the data header (out) */
{
	xfs_dir2_data_free_t	*dfp;		/* bestfree table pointer */
	xfs_dir2_data_free_t	new;		/* new bestfree entry */

409
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
410 411 412 413 414
	       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));

	dfp = xfs_dir3_data_bestfree_p(hdr);
415
	new.length = dup->length;
416 417
	new.offset = cpu_to_be16((char *)dup - (char *)hdr);

L
Linus Torvalds 已提交
418 419 420
	/*
	 * Insert at position 0, 1, or 2; or not at all.
	 */
421
	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
L
Linus Torvalds 已提交
422 423 424 425 426 427
		dfp[2] = dfp[1];
		dfp[1] = dfp[0];
		dfp[0] = new;
		*loghead = 1;
		return &dfp[0];
	}
428
	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
L
Linus Torvalds 已提交
429 430 431 432 433
		dfp[2] = dfp[1];
		dfp[1] = new;
		*loghead = 1;
		return &dfp[1];
	}
434
	if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444
		dfp[2] = new;
		*loghead = 1;
		return &dfp[2];
	}
	return NULL;
}

/*
 * Remove a bestfree entry from the table.
 */
445
STATIC void
L
Linus Torvalds 已提交
446
xfs_dir2_data_freeremove(
447
	xfs_dir2_data_hdr_t	*hdr,		/* data block header */
L
Linus Torvalds 已提交
448 449 450
	xfs_dir2_data_free_t	*dfp,		/* bestfree entry pointer */
	int			*loghead)	/* out: log data header */
{
451 452
	struct xfs_dir2_data_free *bf;

453
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
454 455 456 457
	       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 已提交
458 459 460
	/*
	 * It's the first entry, slide the next 2 up.
	 */
461 462 463 464
	bf = xfs_dir3_data_bestfree_p(hdr);
	if (dfp == &bf[0]) {
		bf[0] = bf[1];
		bf[1] = bf[2];
L
Linus Torvalds 已提交
465 466 467 468
	}
	/*
	 * It's the second entry, slide the 3rd entry up.
	 */
469 470
	else if (dfp == &bf[1])
		bf[1] = bf[2];
L
Linus Torvalds 已提交
471 472 473 474
	/*
	 * Must be the last entry.
	 */
	else
475
		ASSERT(dfp == &bf[2]);
L
Linus Torvalds 已提交
476 477 478
	/*
	 * Clear the 3rd entry, must be zero now.
	 */
479 480
	bf[2].length = 0;
	bf[2].offset = 0;
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488 489
	*loghead = 1;
}

/*
 * Given a data block, reconstruct its bestfree map.
 */
void
xfs_dir2_data_freescan(
	xfs_mount_t		*mp,		/* filesystem mount point */
490
	xfs_dir2_data_hdr_t	*hdr,		/* data block header */
491
	int			*loghead)	/* out: log data header */
L
Linus Torvalds 已提交
492 493 494 495
{
	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 */
496
	struct xfs_dir2_data_free *bf;
L
Linus Torvalds 已提交
497 498 499
	char			*endp;		/* end of block's data */
	char			*p;		/* current entry pointer */

500
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
501
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
502 503 504
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));

L
Linus Torvalds 已提交
505 506 507
	/*
	 * Start by clearing the table.
	 */
508 509
	bf = xfs_dir3_data_bestfree_p(hdr);
	memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
L
Linus Torvalds 已提交
510 511 512 513
	*loghead = 1;
	/*
	 * Set up pointers.
	 */
514 515 516
	p = (char *)xfs_dir3_data_entry_p(hdr);
	if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	    hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
517
		btp = xfs_dir2_block_tail_p(mp, hdr);
518
		endp = (char *)xfs_dir2_block_leaf_p(btp);
L
Linus Torvalds 已提交
519
	} else
520
		endp = (char *)hdr + mp->m_dirblksize;
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528
	/*
	 * Loop over the block's entries.
	 */
	while (p < endp) {
		dup = (xfs_dir2_data_unused_t *)p;
		/*
		 * If it's a free entry, insert it.
		 */
529
		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
530
			ASSERT((char *)dup - (char *)hdr ==
531
			       be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
532
			xfs_dir2_data_freeinsert(hdr, dup, loghead);
533
			p += be16_to_cpu(dup->length);
L
Linus Torvalds 已提交
534 535 536 537 538 539
		}
		/*
		 * For active entries, check their tags and skip them.
		 */
		else {
			dep = (xfs_dir2_data_entry_t *)p;
540
			ASSERT((char *)dep - (char *)hdr ==
541 542
			       be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)));
			p += xfs_dir3_data_entsize(mp, dep->namelen);
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551
		}
	}
}

/*
 * Initialize a data block at the given block number in the directory.
 * Give back the buffer for the created block.
 */
int						/* error */
552
xfs_dir3_data_init(
L
Linus Torvalds 已提交
553 554
	xfs_da_args_t		*args,		/* directory operation args */
	xfs_dir2_db_t		blkno,		/* logical dir block number */
555
	struct xfs_buf		**bpp)		/* output block buffer */
L
Linus Torvalds 已提交
556
{
557
	struct xfs_buf		*bp;		/* block buffer */
558
	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
L
Linus Torvalds 已提交
559 560
	xfs_inode_t		*dp;		/* incore directory inode */
	xfs_dir2_data_unused_t	*dup;		/* unused entry pointer */
561
	struct xfs_dir2_data_free *bf;
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571 572 573
	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.
	 */
574
	error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
L
Linus Torvalds 已提交
575
		XFS_DATA_FORK);
576
	if (error)
L
Linus Torvalds 已提交
577
		return error;
578
	bp->b_ops = &xfs_dir3_data_buf_ops;
579
	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
580

L
Linus Torvalds 已提交
581 582 583
	/*
	 * Initialize the header.
	 */
584
	hdr = bp->b_addr;
585 586 587 588 589 590 591 592 593 594 595 596 597 598
	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);
		uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);

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

	bf = xfs_dir3_data_bestfree_p(hdr);
	bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
L
Linus Torvalds 已提交
599
	for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
600 601
		bf[i].length = 0;
		bf[i].offset = 0;
L
Linus Torvalds 已提交
602
	}
603

L
Linus Torvalds 已提交
604 605 606
	/*
	 * Set up an unused entry for the block's body.
	 */
607
	dup = xfs_dir3_data_unused_p(hdr);
608
	dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
L
Linus Torvalds 已提交
609

610 611
	t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
	bf[0].length = cpu_to_be16(t);
612
	dup->length = cpu_to_be16(t);
613
	*xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
L
Linus Torvalds 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627
	/*
	 * Log it and return it.
	 */
	xfs_dir2_data_log_header(tp, bp);
	xfs_dir2_data_log_unused(tp, bp, dup);
	*bpp = bp;
	return 0;
}

/*
 * Log an active data entry from the block.
 */
void
xfs_dir2_data_log_entry(
628 629
	struct xfs_trans	*tp,
	struct xfs_buf		*bp,
L
Linus Torvalds 已提交
630 631
	xfs_dir2_data_entry_t	*dep)		/* data entry pointer */
{
632 633
	struct xfs_dir2_data_hdr *hdr = bp->b_addr;
	struct xfs_mount	*mp = tp->t_mountp;
L
Linus Torvalds 已提交
634

635
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
636
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
637 638
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
639

640
	xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
641
		(uint)((char *)(xfs_dir3_data_entry_tag_p(mp, dep) + 1) -
642
		       (char *)hdr - 1));
L
Linus Torvalds 已提交
643 644 645 646 647 648 649
}

/*
 * Log a data block header.
 */
void
xfs_dir2_data_log_header(
650 651
	struct xfs_trans	*tp,
	struct xfs_buf		*bp)
L
Linus Torvalds 已提交
652
{
653
	xfs_dir2_data_hdr_t	*hdr = bp->b_addr;
L
Linus Torvalds 已提交
654

655
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
656
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
657 658
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
659

660
	xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
L
Linus Torvalds 已提交
661 662 663 664 665 666 667
}

/*
 * Log a data unused entry.
 */
void
xfs_dir2_data_log_unused(
668 669
	struct xfs_trans	*tp,
	struct xfs_buf		*bp,
L
Linus Torvalds 已提交
670 671
	xfs_dir2_data_unused_t	*dup)		/* data unused pointer */
{
672
	xfs_dir2_data_hdr_t	*hdr = bp->b_addr;
673

674
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
675
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
676 677
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
L
Linus Torvalds 已提交
678 679 680 681

	/*
	 * Log the first part of the unused entry.
	 */
682
	xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
L
Linus Torvalds 已提交
683
		(uint)((char *)&dup->length + sizeof(dup->length) -
684
		       1 - (char *)hdr));
L
Linus Torvalds 已提交
685 686 687
	/*
	 * Log the end (tag) of the unused entry.
	 */
688
	xfs_trans_log_buf(tp, bp,
689 690
		(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 已提交
691 692 693 694 695 696 697 698 699
		       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(
700 701
	struct xfs_trans	*tp,
	struct xfs_buf		*bp,
L
Linus Torvalds 已提交
702 703 704 705 706
	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 */
{
707
	xfs_dir2_data_hdr_t	*hdr;		/* data block pointer */
L
Linus Torvalds 已提交
708 709 710 711 712 713 714
	xfs_dir2_data_free_t	*dfp;		/* bestfree pointer */
	char			*endptr;	/* end of data area */
	xfs_mount_t		*mp;		/* filesystem mount point */
	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 */
715
	struct xfs_dir2_data_free *bf;
L
Linus Torvalds 已提交
716 717

	mp = tp->t_mountp;
718
	hdr = bp->b_addr;
719

L
Linus Torvalds 已提交
720 721 722
	/*
	 * Figure out where the end of the data area is.
	 */
723 724
	if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
	    hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
725
		endptr = (char *)hdr + mp->m_dirblksize;
L
Linus Torvalds 已提交
726 727 728
	else {
		xfs_dir2_block_tail_t	*btp;	/* block tail */

729 730
		ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
			hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
731
		btp = xfs_dir2_block_tail_p(mp, hdr);
732
		endptr = (char *)xfs_dir2_block_leaf_p(btp);
L
Linus Torvalds 已提交
733 734 735 736 737
	}
	/*
	 * If this isn't the start of the block, then back up to
	 * the previous entry and see if it's free.
	 */
738
	if (offset > xfs_dir3_data_entry_offset(hdr)) {
739
		__be16			*tagp;	/* tag just before us */
L
Linus Torvalds 已提交
740

741 742
		tagp = (__be16 *)((char *)hdr + offset) - 1;
		prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
743
		if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
L
Linus Torvalds 已提交
744 745 746 747 748 749 750
			prevdup = NULL;
	} else
		prevdup = NULL;
	/*
	 * If this isn't the end of the block, see if the entry after
	 * us is free.
	 */
751
	if ((char *)hdr + offset + len < endptr) {
L
Linus Torvalds 已提交
752
		postdup =
753
			(xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
754
		if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
L
Linus Torvalds 已提交
755 756 757 758 759 760 761 762 763
			postdup = NULL;
	} else
		postdup = NULL;
	ASSERT(*needscanp == 0);
	needscan = 0;
	/*
	 * Previous and following entries are both free,
	 * merge everything into a single free entry.
	 */
764
	bf = xfs_dir3_data_bestfree_p(hdr);
L
Linus Torvalds 已提交
765 766 767 768 769 770
	if (prevdup && postdup) {
		xfs_dir2_data_free_t	*dfp2;	/* another bestfree pointer */

		/*
		 * See if prevdup and/or postdup are in bestfree table.
		 */
771 772
		dfp = xfs_dir2_data_freefind(hdr, prevdup);
		dfp2 = xfs_dir2_data_freefind(hdr, postdup);
L
Linus Torvalds 已提交
773 774 775 776 777 778
		/*
		 * 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.
		 */
779
		needscan = (bf[2].length != 0);
L
Linus Torvalds 已提交
780 781 782
		/*
		 * Fix up the new big freespace.
		 */
783
		be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
784
		*xfs_dir2_data_unused_tag_p(prevdup) =
785
			cpu_to_be16((char *)prevdup - (char *)hdr);
L
Linus Torvalds 已提交
786 787 788 789 790 791 792 793 794
		xfs_dir2_data_log_unused(tp, bp, prevdup);
		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);
795 796
			if (dfp == &bf[1]) {
				dfp = &bf[0];
L
Linus Torvalds 已提交
797
				ASSERT(dfp2 == dfp);
798
				dfp2 = &bf[1];
L
Linus Torvalds 已提交
799
			}
800 801
			xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
			xfs_dir2_data_freeremove(hdr, dfp, needlogp);
L
Linus Torvalds 已提交
802 803 804
			/*
			 * Now insert the new entry.
			 */
805
			dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
806
			ASSERT(dfp == &bf[0]);
807
			ASSERT(dfp->length == prevdup->length);
L
Linus Torvalds 已提交
808 809 810 811 812 813 814 815
			ASSERT(!dfp[1].length);
			ASSERT(!dfp[2].length);
		}
	}
	/*
	 * The entry before us is free, merge with it.
	 */
	else if (prevdup) {
816
		dfp = xfs_dir2_data_freefind(hdr, prevdup);
817
		be16_add_cpu(&prevdup->length, len);
818
		*xfs_dir2_data_unused_tag_p(prevdup) =
819
			cpu_to_be16((char *)prevdup - (char *)hdr);
L
Linus Torvalds 已提交
820 821 822 823 824 825 826
		xfs_dir2_data_log_unused(tp, bp, prevdup);
		/*
		 * 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) {
827 828
			xfs_dir2_data_freeremove(hdr, dfp, needlogp);
			xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
L
Linus Torvalds 已提交
829 830 831 832
		}
		/*
		 * Otherwise we need a scan if the new entry is big enough.
		 */
833
		else {
834
			needscan = be16_to_cpu(prevdup->length) >
835
				   be16_to_cpu(bf[2].length);
836
		}
L
Linus Torvalds 已提交
837 838 839 840 841
	}
	/*
	 * The following entry is free, merge with it.
	 */
	else if (postdup) {
842 843
		dfp = xfs_dir2_data_freefind(hdr, postdup);
		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
844 845
		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
		newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
846
		*xfs_dir2_data_unused_tag_p(newdup) =
847
			cpu_to_be16((char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
848 849 850 851 852 853 854
		xfs_dir2_data_log_unused(tp, bp, newdup);
		/*
		 * 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) {
855 856
			xfs_dir2_data_freeremove(hdr, dfp, needlogp);
			xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
L
Linus Torvalds 已提交
857 858 859 860
		}
		/*
		 * Otherwise we need a scan if the new entry is big enough.
		 */
861
		else {
862
			needscan = be16_to_cpu(newdup->length) >
863
				   be16_to_cpu(bf[2].length);
864
		}
L
Linus Torvalds 已提交
865 866 867 868 869
	}
	/*
	 * Neither neighbor is free.  Make a new entry.
	 */
	else {
870
		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
871 872
		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
		newdup->length = cpu_to_be16(len);
873
		*xfs_dir2_data_unused_tag_p(newdup) =
874
			cpu_to_be16((char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
875
		xfs_dir2_data_log_unused(tp, bp, newdup);
876
		xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
L
Linus Torvalds 已提交
877 878 879 880 881 882 883 884 885
	}
	*needscanp = needscan;
}

/*
 * Take a byte range out of an existing unused space and make it un-free.
 */
void
xfs_dir2_data_use_free(
886 887
	struct xfs_trans	*tp,
	struct xfs_buf		*bp,
L
Linus Torvalds 已提交
888 889 890 891 892 893
	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 */
{
894
	xfs_dir2_data_hdr_t	*hdr;		/* data block header */
L
Linus Torvalds 已提交
895 896 897 898 899 900 901
	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 */
902
	struct xfs_dir2_data_free *bf;
L
Linus Torvalds 已提交
903

904
	hdr = bp->b_addr;
905
	ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
906
	       hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
907 908
	       hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
	       hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
909
	ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
910 911 912
	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 已提交
913 914 915
	/*
	 * Look up the entry in the bestfree table.
	 */
916
	dfp = xfs_dir2_data_freefind(hdr, dup);
917
	oldlen = be16_to_cpu(dup->length);
918 919
	bf = xfs_dir3_data_bestfree_p(hdr);
	ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
L
Linus Torvalds 已提交
920 921 922
	/*
	 * Check for alignment with front and back of the entry.
	 */
923 924
	matchfront = (char *)dup - (char *)hdr == offset;
	matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
L
Linus Torvalds 已提交
925 926 927 928 929 930 931 932
	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) {
933
			needscan = (bf[2].offset != 0);
L
Linus Torvalds 已提交
934
			if (!needscan)
935
				xfs_dir2_data_freeremove(hdr, dfp, needlogp);
L
Linus Torvalds 已提交
936 937 938 939 940 941 942
		}
	}
	/*
	 * We match the first part of the entry.
	 * Make a new entry with the remaining freespace.
	 */
	else if (matchfront) {
943
		newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
944 945
		newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
		newdup->length = cpu_to_be16(oldlen - len);
946
		*xfs_dir2_data_unused_tag_p(newdup) =
947
			cpu_to_be16((char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
948 949 950 951 952
		xfs_dir2_data_log_unused(tp, bp, newdup);
		/*
		 * If it was in the table, remove it and add the new one.
		 */
		if (dfp) {
953 954
			xfs_dir2_data_freeremove(hdr, dfp, needlogp);
			dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
L
Linus Torvalds 已提交
955
			ASSERT(dfp != NULL);
956
			ASSERT(dfp->length == newdup->length);
957
			ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
958 959 960 961 962
			/*
			 * 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.
			 */
963
			needscan = dfp == &bf[2];
L
Linus Torvalds 已提交
964 965 966 967 968 969 970 971
		}
	}
	/*
	 * We match the last part of the entry.
	 * Trim the allocated space off the tail of the entry.
	 */
	else if (matchback) {
		newdup = dup;
972
		newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
973
		*xfs_dir2_data_unused_tag_p(newdup) =
974
			cpu_to_be16((char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
975 976 977 978 979
		xfs_dir2_data_log_unused(tp, bp, newdup);
		/*
		 * If it was in the table, remove it and add the new one.
		 */
		if (dfp) {
980 981
			xfs_dir2_data_freeremove(hdr, dfp, needlogp);
			dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
L
Linus Torvalds 已提交
982
			ASSERT(dfp != NULL);
983
			ASSERT(dfp->length == newdup->length);
984
			ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
985 986 987 988 989
			/*
			 * 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.
			 */
990
			needscan = dfp == &bf[2];
L
Linus Torvalds 已提交
991 992 993 994 995 996 997 998
		}
	}
	/*
	 * Poking out the middle of an entry.
	 * Make two new entries.
	 */
	else {
		newdup = dup;
999
		newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1000
		*xfs_dir2_data_unused_tag_p(newdup) =
1001
			cpu_to_be16((char *)newdup - (char *)hdr);
L
Linus Torvalds 已提交
1002
		xfs_dir2_data_log_unused(tp, bp, newdup);
1003
		newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1004 1005
		newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
		newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1006
		*xfs_dir2_data_unused_tag_p(newdup2) =
1007
			cpu_to_be16((char *)newdup2 - (char *)hdr);
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
		xfs_dir2_data_log_unused(tp, bp, newdup2);
		/*
		 * 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) {
1018
			needscan = (bf[2].length != 0);
L
Linus Torvalds 已提交
1019
			if (!needscan) {
1020 1021 1022 1023
				xfs_dir2_data_freeremove(hdr, dfp, needlogp);
				xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
				xfs_dir2_data_freeinsert(hdr, newdup2,
							 needlogp);
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028
			}
		}
	}
	*needscanp = needscan;
}