xfs_attr_list.c 13.3 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8
/*
 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
 * Copyright (c) 2013 Red Hat, Inc.
 * All Rights Reserved.
 */
#include "xfs.h"
#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"
13
#include "xfs_mount.h"
14
#include "xfs_da_format.h"
15
#include "xfs_inode.h"
16
#include "xfs_trans.h"
17 18
#include "xfs_bmap.h"
#include "xfs_attr.h"
19
#include "xfs_attr_sf.h"
20 21 22
#include "xfs_attr_leaf.h"
#include "xfs_error.h"
#include "xfs_trace.h"
D
Dave Chinner 已提交
23
#include "xfs_dir2.h"
24 25 26 27 28 29 30 31 32

STATIC int
xfs_attr_shortform_compare(const void *a, const void *b)
{
	xfs_attr_sf_sort_t *sa, *sb;

	sa = (xfs_attr_sf_sort_t *)a;
	sb = (xfs_attr_sf_sort_t *)b;
	if (sa->hash < sb->hash) {
E
Eric Sandeen 已提交
33
		return -1;
34
	} else if (sa->hash > sb->hash) {
E
Eric Sandeen 已提交
35
		return 1;
36
	} else {
E
Eric Sandeen 已提交
37
		return sa->entno - sb->entno;
38 39 40 41 42 43 44 45 46 47 48 49 50
	}
}

#define XFS_ISRESET_CURSOR(cursor) \
	(!((cursor)->initted) && !((cursor)->hashval) && \
	 !((cursor)->blkno) && !((cursor)->offset))
/*
 * Copy out entries of shortform attribute lists for attr_list().
 * Shortform attribute lists are not stored in hashval sorted order.
 * If the output buffer is not large enough to hold them all, then we
 * we have to calculate each entries' hashvalue and sort them before
 * we can begin returning them to the user.
 */
51
static int
52 53
xfs_attr_shortform_list(
	struct xfs_attr_list_context	*context)
54
{
55 56
	struct xfs_attrlist_cursor_kern	*cursor = &context->cursor;
	struct xfs_inode		*dp = context->dp;
57 58 59 60 61
	struct xfs_attr_sf_sort		*sbuf, *sbp;
	struct xfs_attr_shortform	*sf;
	struct xfs_attr_sf_entry	*sfe;
	int				sbsize, nsbuf, count, i;
	int				error = 0;
62 63 64 65 66

	ASSERT(dp->i_afp != NULL);
	sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
	ASSERT(sf != NULL);
	if (!sf->hdr.count)
E
Eric Sandeen 已提交
67
		return 0;
68 69 70 71 72 73 74 75 76 77 78 79 80 81

	trace_xfs_attr_list_sf(context);

	/*
	 * If the buffer is large enough and the cursor is at the start,
	 * do not bother with sorting since we will return everything in
	 * one buffer and another call using the cursor won't need to be
	 * made.
	 * Note the generous fudge factor of 16 overhead bytes per entry.
	 * If bufsize is zero then put_listent must be a search function
	 * and can just scan through what we have.
	 */
	if (context->bufsize == 0 ||
	    (XFS_ISRESET_CURSOR(cursor) &&
82
	     (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
83
		for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
84 85 86
			if (XFS_IS_CORRUPT(context->dp->i_mount,
					   !xfs_attr_namecheck(sfe->nameval,
							       sfe->namelen)))
87
				return -EFSCORRUPTED;
88 89 90 91 92
			context->put_listent(context,
					     sfe->flags,
					     sfe->nameval,
					     (int)sfe->namelen,
					     (int)sfe->valuelen);
93 94 95 96 97 98 99 100 101
			/*
			 * Either search callback finished early or
			 * didn't fit it all in the buffer after all.
			 */
			if (context->seen_enough)
				break;
			sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
		}
		trace_xfs_attr_list_sf_all(context);
E
Eric Sandeen 已提交
102
		return 0;
103 104 105 106 107 108 109 110 111 112
	}

	/* do no more for a search callback */
	if (context->bufsize == 0)
		return 0;

	/*
	 * It didn't all fit, so we have to sort everything on hashval.
	 */
	sbsize = sf->hdr.count * sizeof(*sbuf);
113
	sbp = sbuf = kmem_alloc(sbsize, KM_NOFS);
114 115 116 117 118 119 120 121 122 123 124 125

	/*
	 * Scan the attribute list for the rest of the entries, storing
	 * the relevant info from only those that match into a buffer.
	 */
	nsbuf = 0;
	for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
		if (unlikely(
		    ((char *)sfe < (char *)sf) ||
		    ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
			XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
					     XFS_ERRLEVEL_LOW,
126 127
					     context->dp->i_mount, sfe,
					     sizeof(*sfe));
128
			kmem_free(sbuf);
D
Dave Chinner 已提交
129
			return -EFSCORRUPTED;
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		}

		sbp->entno = i;
		sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
		sbp->name = sfe->nameval;
		sbp->namelen = sfe->namelen;
		/* These are bytes, and both on-disk, don't endian-flip */
		sbp->valuelen = sfe->valuelen;
		sbp->flags = sfe->flags;
		sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
		sbp++;
		nsbuf++;
	}

	/*
	 * Sort the entries on hash then entno.
	 */
	xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);

	/*
	 * Re-find our place IN THE SORTED LIST.
	 */
	count = 0;
	cursor->initted = 1;
	cursor->blkno = 0;
	for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
		if (sbp->hash == cursor->hashval) {
			if (cursor->offset == count) {
				break;
			}
			count++;
		} else if (sbp->hash > cursor->hashval) {
			break;
		}
	}
165 166
	if (i == nsbuf)
		goto out;
167 168 169 170 171 172 173 174 175

	/*
	 * Loop putting entries into the user buffer.
	 */
	for ( ; i < nsbuf; i++, sbp++) {
		if (cursor->hashval != sbp->hash) {
			cursor->hashval = sbp->hash;
			cursor->offset = 0;
		}
176 177 178
		if (XFS_IS_CORRUPT(context->dp->i_mount,
				   !xfs_attr_namecheck(sbp->name,
						       sbp->namelen))) {
179 180 181
			error = -EFSCORRUPTED;
			goto out;
		}
182 183 184 185 186
		context->put_listent(context,
				     sbp->flags,
				     sbp->name,
				     sbp->namelen,
				     sbp->valuelen);
187 188 189 190
		if (context->seen_enough)
			break;
		cursor->offset++;
	}
191
out:
192
	kmem_free(sbuf);
193
	return error;
194 195
}

196 197 198 199
/*
 * We didn't find the block & hash mentioned in the cursor state, so
 * walk down the attr btree looking for the hash.
 */
200
STATIC int
201 202
xfs_attr_node_list_lookup(
	struct xfs_attr_list_context	*context,
203
	struct xfs_attrlist_cursor_kern	*cursor,
204
	struct xfs_buf			**pbp)
205
{
206 207 208 209 210 211 212 213 214
	struct xfs_da3_icnode_hdr	nodehdr;
	struct xfs_da_intnode		*node;
	struct xfs_da_node_entry	*btree;
	struct xfs_inode		*dp = context->dp;
	struct xfs_mount		*mp = dp->i_mount;
	struct xfs_trans		*tp = context->tp;
	struct xfs_buf			*bp;
	int				i;
	int				error = 0;
215
	unsigned int			expected_level = 0;
216 217 218 219 220
	uint16_t			magic;

	ASSERT(*pbp == NULL);
	cursor->blkno = 0;
	for (;;) {
C
Christoph Hellwig 已提交
221
		error = xfs_da3_node_read(tp, dp, cursor->blkno, &bp,
222 223 224 225 226 227 228 229 230 231 232
				XFS_ATTR_FORK);
		if (error)
			return error;
		node = bp->b_addr;
		magic = be16_to_cpu(node->hdr.info.magic);
		if (magic == XFS_ATTR_LEAF_MAGIC ||
		    magic == XFS_ATTR3_LEAF_MAGIC)
			break;
		if (magic != XFS_DA_NODE_MAGIC &&
		    magic != XFS_DA3_NODE_MAGIC) {
			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
233
					node, sizeof(*node));
234 235 236
			goto out_corruptbuf;
		}

237
		xfs_da3_node_hdr_from_disk(mp, &nodehdr, node);
238

239 240 241 242 243 244 245 246 247 248 249 250
		/* Tree taller than we can handle; bail out! */
		if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
			goto out_corruptbuf;

		/* Check the level from the root node. */
		if (cursor->blkno == 0)
			expected_level = nodehdr.level - 1;
		else if (expected_level != nodehdr.level)
			goto out_corruptbuf;
		else
			expected_level--;

251
		btree = nodehdr.btree;
252 253 254 255 256 257 258 259 260 261 262 263
		for (i = 0; i < nodehdr.count; btree++, i++) {
			if (cursor->hashval <= be32_to_cpu(btree->hashval)) {
				cursor->blkno = be32_to_cpu(btree->before);
				trace_xfs_attr_list_node_descend(context,
						btree);
				break;
			}
		}
		xfs_trans_brelse(tp, bp);

		if (i == nodehdr.count)
			return 0;
264 265

		/* We can't point back to the root. */
266
		if (XFS_IS_CORRUPT(mp, cursor->blkno == 0))
267
			return -EFSCORRUPTED;
268 269
	}

270 271 272
	if (expected_level != 0)
		goto out_corruptbuf;

273 274 275 276
	*pbp = bp;
	return 0;

out_corruptbuf:
277
	xfs_buf_corruption_error(bp);
278 279 280 281 282 283 284 285
	xfs_trans_brelse(tp, bp);
	return -EFSCORRUPTED;
}

STATIC int
xfs_attr_node_list(
	struct xfs_attr_list_context	*context)
{
286
	struct xfs_attrlist_cursor_kern	*cursor = &context->cursor;
287 288 289 290 291 292
	struct xfs_attr3_icleaf_hdr	leafhdr;
	struct xfs_attr_leafblock	*leaf;
	struct xfs_da_intnode		*node;
	struct xfs_buf			*bp;
	struct xfs_inode		*dp = context->dp;
	struct xfs_mount		*mp = dp->i_mount;
293
	int				error = 0;
294 295 296 297 298 299 300 301 302 303 304 305

	trace_xfs_attr_node_list(context);

	cursor->initted = 1;

	/*
	 * Do all sorts of validation on the passed-in cursor structure.
	 * If anything is amiss, ignore the cursor and look up the hashval
	 * starting from the btree root.
	 */
	bp = NULL;
	if (cursor->blkno > 0) {
C
Christoph Hellwig 已提交
306 307
		error = xfs_da3_node_read(context->tp, dp, cursor->blkno, &bp,
				XFS_ATTR_FORK);
D
Dave Chinner 已提交
308
		if ((error != 0) && (error != -EFSCORRUPTED))
E
Eric Sandeen 已提交
309
			return error;
310 311 312 313 314 315 316 317
		if (bp) {
			struct xfs_attr_leaf_entry *entries;

			node = bp->b_addr;
			switch (be16_to_cpu(node->hdr.info.magic)) {
			case XFS_DA_NODE_MAGIC:
			case XFS_DA3_NODE_MAGIC:
				trace_xfs_attr_list_wrong_blk(context);
318
				xfs_trans_brelse(context->tp, bp);
319 320 321 322 323
				bp = NULL;
				break;
			case XFS_ATTR_LEAF_MAGIC:
			case XFS_ATTR3_LEAF_MAGIC:
				leaf = bp->b_addr;
324 325
				xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo,
							     &leafhdr, leaf);
326 327 328 329
				entries = xfs_attr3_leaf_entryp(leaf);
				if (cursor->hashval > be32_to_cpu(
						entries[leafhdr.count - 1].hashval)) {
					trace_xfs_attr_list_wrong_blk(context);
330
					xfs_trans_brelse(context->tp, bp);
331 332 333 334
					bp = NULL;
				} else if (cursor->hashval <= be32_to_cpu(
						entries[0].hashval)) {
					trace_xfs_attr_list_wrong_blk(context);
335
					xfs_trans_brelse(context->tp, bp);
336 337 338 339 340
					bp = NULL;
				}
				break;
			default:
				trace_xfs_attr_list_wrong_blk(context);
341
				xfs_trans_brelse(context->tp, bp);
342 343 344 345 346 347 348 349 350 351 352
				bp = NULL;
			}
		}
	}

	/*
	 * We did not find what we expected given the cursor's contents,
	 * so we start from the top and work down based on the hash value.
	 * Note that start of node block is same as start of leaf block.
	 */
	if (bp == NULL) {
353 354 355
		error = xfs_attr_node_list_lookup(context, cursor, &bp);
		if (error || !bp)
			return error;
356 357 358 359 360 361 362 363 364 365
	}
	ASSERT(bp != NULL);

	/*
	 * Roll upward through the blocks, processing each leaf block in
	 * order.  As long as there is space in the result buffer, keep
	 * adding the information.
	 */
	for (;;) {
		leaf = bp->b_addr;
366 367 368
		error = xfs_attr3_leaf_list_int(bp, context);
		if (error)
			break;
369
		xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
370 371 372
		if (context->seen_enough || leafhdr.forw == 0)
			break;
		cursor->blkno = leafhdr.forw;
373
		xfs_trans_brelse(context->tp, bp);
374 375
		error = xfs_attr3_leaf_read(context->tp, dp, cursor->blkno,
					    &bp);
376 377 378
		if (error)
			return error;
	}
379
	xfs_trans_brelse(context->tp, bp);
380
	return error;
381 382 383 384 385
}

/*
 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
 */
386
int
387 388 389 390
xfs_attr3_leaf_list_int(
	struct xfs_buf			*bp,
	struct xfs_attr_list_context	*context)
{
391
	struct xfs_attrlist_cursor_kern	*cursor = &context->cursor;
392 393 394 395 396
	struct xfs_attr_leafblock	*leaf;
	struct xfs_attr3_icleaf_hdr	ichdr;
	struct xfs_attr_leaf_entry	*entries;
	struct xfs_attr_leaf_entry	*entry;
	int				i;
397
	struct xfs_mount		*mp = context->dp->i_mount;
398 399 400 401

	trace_xfs_attr_list_leaf(context);

	leaf = bp->b_addr;
402
	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	entries = xfs_attr3_leaf_entryp(leaf);

	cursor->initted = 1;

	/*
	 * Re-find our place in the leaf block if this is a new syscall.
	 */
	if (context->resynch) {
		entry = &entries[0];
		for (i = 0; i < ichdr.count; entry++, i++) {
			if (be32_to_cpu(entry->hashval) == cursor->hashval) {
				if (cursor->offset == context->dupcnt) {
					context->dupcnt = 0;
					break;
				}
				context->dupcnt++;
			} else if (be32_to_cpu(entry->hashval) >
					cursor->hashval) {
				context->dupcnt = 0;
				break;
			}
		}
		if (i == ichdr.count) {
			trace_xfs_attr_list_notfound(context);
427
			return 0;
428 429 430 431 432 433 434 435 436 437 438
		}
	} else {
		entry = &entries[0];
		i = 0;
	}
	context->resynch = 0;

	/*
	 * We have found our place, start copying out the new attributes.
	 */
	for (; i < ichdr.count; entry++, i++) {
439 440 441
		char *name;
		int namelen, valuelen;

442 443 444 445 446
		if (be32_to_cpu(entry->hashval) != cursor->hashval) {
			cursor->hashval = be32_to_cpu(entry->hashval);
			cursor->offset = 0;
		}

D
Darrick J. Wong 已提交
447
		if ((entry->flags & XFS_ATTR_INCOMPLETE) &&
448 449
		    !context->allow_incomplete)
			continue;
450 451

		if (entry->flags & XFS_ATTR_LOCAL) {
452
			xfs_attr_leaf_name_local_t *name_loc;
453

454 455 456 457 458 459
			name_loc = xfs_attr3_leaf_name_local(leaf, i);
			name = name_loc->nameval;
			namelen = name_loc->namelen;
			valuelen = be16_to_cpu(name_loc->valuelen);
		} else {
			xfs_attr_leaf_name_remote_t *name_rmt;
460

461 462 463 464
			name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
			name = name_rmt->name;
			namelen = name_rmt->namelen;
			valuelen = be32_to_cpu(name_rmt->valuelen);
465
		}
466

467 468
		if (XFS_IS_CORRUPT(context->dp->i_mount,
				   !xfs_attr_namecheck(name, namelen)))
469
			return -EFSCORRUPTED;
470
		context->put_listent(context, entry->flags,
471
					      name, namelen, valuelen);
472 473 474 475 476
		if (context->seen_enough)
			break;
		cursor->offset++;
	}
	trace_xfs_attr_list_leaf_end(context);
477
	return 0;
478 479 480 481 482 483
}

/*
 * Copy out attribute entries for attr_list(), for leaf attribute lists.
 */
STATIC int
484 485
xfs_attr_leaf_list(
	struct xfs_attr_list_context	*context)
486
{
487 488
	struct xfs_buf			*bp;
	int				error;
489 490 491

	trace_xfs_attr_leaf_list(context);

492
	context->cursor.blkno = 0;
493
	error = xfs_attr3_leaf_read(context->tp, context->dp, 0, &bp);
494
	if (error)
E
Eric Sandeen 已提交
495
		return error;
496

497
	error = xfs_attr3_leaf_list_int(bp, context);
498
	xfs_trans_brelse(context->tp, bp);
499
	return error;
500 501
}

502
int
503
xfs_attr_list_ilocked(
504 505 506 507
	struct xfs_attr_list_context	*context)
{
	struct xfs_inode		*dp = context->dp;

508 509
	ASSERT(xfs_isilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));

510 511 512 513 514 515 516 517 518 519 520 521
	/*
	 * Decide on what work routines to call based on the inode size.
	 */
	if (!xfs_inode_hasattr(dp))
		return 0;
	else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
		return xfs_attr_shortform_list(context);
	else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
		return xfs_attr_leaf_list(context);
	return xfs_attr_node_list(context);
}

522
int
523
xfs_attr_list(
524
	struct xfs_attr_list_context	*context)
525
{
526 527 528
	struct xfs_inode		*dp = context->dp;
	uint				lock_mode;
	int				error;
529

530
	XFS_STATS_INC(dp->i_mount, xs_attr_list);
531 532

	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
D
Dave Chinner 已提交
533
		return -EIO;
534

535
	lock_mode = xfs_ilock_attr_map_shared(dp);
536
	error = xfs_attr_list_ilocked(context);
537
	xfs_iunlock(dp, lock_mode);
538 539
	return error;
}