xfs_attr_remote.c 14.2 KB
Newer Older
1 2
/*
 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3
 * Copyright (c) 2013 Red Hat, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * 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.
 *
 * 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
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_types.h"
#include "xfs_bit.h"
#include "xfs_log.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_mount.h"
#include "xfs_error.h"
#include "xfs_da_btree.h"
#include "xfs_bmap_btree.h"
#include "xfs_dinode.h"
#include "xfs_inode.h"
#include "xfs_alloc.h"
#include "xfs_inode_item.h"
#include "xfs_bmap.h"
#include "xfs_attr.h"
#include "xfs_attr_leaf.h"
#include "xfs_attr_remote.h"
#include "xfs_trans_space.h"
#include "xfs_trace.h"
41 42
#include "xfs_cksum.h"
#include "xfs_buf_item.h"
43 44 45

#define ATTR_RMTVALUE_MAPSIZE	1	/* # of map entries at once */

46 47 48 49 50 51 52 53 54
/*
 * Each contiguous block has a header, so it is not just a simple attribute
 * length to FSB conversion.
 */
static int
xfs_attr3_rmt_blocks(
	struct xfs_mount *mp,
	int		attrlen)
{
55 56 57 58 59
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
		return (attrlen + buflen - 1) / buflen;
	}
	return XFS_B_TO_FSB(mp, attrlen);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
}

static bool
xfs_attr3_rmt_verify(
	struct xfs_buf		*bp)
{
	struct xfs_mount	*mp = bp->b_target->bt_mount;
	struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;

	if (!xfs_sb_version_hascrc(&mp->m_sb))
		return false;
	if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
		return false;
	if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
		return false;
	if (bp->b_bn != be64_to_cpu(rmt->rm_blkno))
		return false;
	if (be32_to_cpu(rmt->rm_offset) +
78
				be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		return false;
	if (rmt->rm_owner == 0)
		return false;

	return true;
}

static void
xfs_attr3_rmt_read_verify(
	struct xfs_buf	*bp)
{
	struct xfs_mount *mp = bp->b_target->bt_mount;

	/* no verification of non-crc buffers */
	if (!xfs_sb_version_hascrc(&mp->m_sb))
		return;

	if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
			      XFS_ATTR3_RMT_CRC_OFF) ||
	    !xfs_attr3_rmt_verify(bp)) {
		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
		xfs_buf_ioerror(bp, EFSCORRUPTED);
	}
}

static void
xfs_attr3_rmt_write_verify(
	struct xfs_buf	*bp)
{
	struct xfs_mount *mp = bp->b_target->bt_mount;
	struct xfs_buf_log_item	*bip = bp->b_fspriv;

	/* no verification of non-crc buffers */
	if (!xfs_sb_version_hascrc(&mp->m_sb))
		return;

	if (!xfs_attr3_rmt_verify(bp)) {
		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
		xfs_buf_ioerror(bp, EFSCORRUPTED);
		return;
	}

	if (bip) {
		struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
		rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
	}
	xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
			 XFS_ATTR3_RMT_CRC_OFF);
}

const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
	.verify_read = xfs_attr3_rmt_read_verify,
	.verify_write = xfs_attr3_rmt_write_verify,
};

static int
xfs_attr3_rmt_hdr_set(
	struct xfs_mount	*mp,
	xfs_ino_t		ino,
	uint32_t		offset,
	uint32_t		size,
	struct xfs_buf		*bp)
{
	struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;

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

	rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
	rmt->rm_offset = cpu_to_be32(offset);
	rmt->rm_bytes = cpu_to_be32(size);
	uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
	rmt->rm_owner = cpu_to_be64(ino);
	rmt->rm_blkno = cpu_to_be64(bp->b_bn);
	bp->b_ops = &xfs_attr3_rmt_buf_ops;

	return sizeof(struct xfs_attr3_rmt_hdr);
}

/*
 * Checking of the remote attribute header is split into two parts. the verifier
 * does CRC, location and bounds checking, the unpacking function checks the
 * attribute parameters and owner.
 */
static bool
xfs_attr3_rmt_hdr_ok(
	struct xfs_mount	*mp,
	xfs_ino_t		ino,
	uint32_t		offset,
	uint32_t		size,
	struct xfs_buf		*bp)
{
	struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;

	if (offset != be32_to_cpu(rmt->rm_offset))
		return false;
	if (size != be32_to_cpu(rmt->rm_bytes))
		return false;
	if (ino != be64_to_cpu(rmt->rm_owner))
		return false;

	/* ok */
	return true;
}

184 185 186 187 188
/*
 * Read the value associated with an attribute from the out-of-line buffer
 * that we stored it in.
 */
int
189 190
xfs_attr_rmtval_get(
	struct xfs_da_args	*args)
191
{
192 193 194 195 196 197 198 199 200 201 202 203
	struct xfs_bmbt_irec	map[ATTR_RMTVALUE_MAPSIZE];
	struct xfs_mount	*mp = args->dp->i_mount;
	struct xfs_buf		*bp;
	xfs_daddr_t		dblkno;
	xfs_dablk_t		lblkno = args->rmtblkno;
	void			*dst = args->value;
	int			valuelen = args->valuelen;
	int			nmap;
	int			error;
	int			blkcnt;
	int			i;
	int			offset = 0;
204 205 206 207 208 209 210

	trace_xfs_attr_rmtval_get(args);

	ASSERT(!(args->flags & ATTR_KERNOVAL));

	while (valuelen > 0) {
		nmap = ATTR_RMTVALUE_MAPSIZE;
211
		blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
212
		error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
213
				       blkcnt, map, &nmap,
214 215
				       XFS_BMAPI_ATTRFORK);
		if (error)
216
			return error;
217 218 219
		ASSERT(nmap >= 1);

		for (i = 0; (i < nmap) && (valuelen > 0); i++) {
220 221 222
			int	byte_cnt;
			char	*src;

223 224 225 226 227
			ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
			       (map[i].br_startblock != HOLESTARTBLOCK));
			dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
			blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
			error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
228 229
						   dblkno, blkcnt, 0, &bp,
						   &xfs_attr3_rmt_buf_ops);
230
			if (error)
231 232
				return error;

233 234
			byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
			byte_cnt = min_t(int, valuelen, byte_cnt);
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
			src = bp->b_addr;
			if (xfs_sb_version_hascrc(&mp->m_sb)) {
				if (!xfs_attr3_rmt_hdr_ok(mp, args->dp->i_ino,
							offset, byte_cnt, bp)) {
					xfs_alert(mp,
"remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
						offset, byte_cnt, args->dp->i_ino);
					xfs_buf_relse(bp);
					return EFSCORRUPTED;

				}

				src += sizeof(struct xfs_attr3_rmt_hdr);
			}

			memcpy(dst, src, byte_cnt);
252
			xfs_buf_relse(bp);
253 254 255 256

			offset += byte_cnt;
			dst += byte_cnt;
			valuelen -= byte_cnt;
257 258 259 260 261

			lblkno += map[i].br_blockcount;
		}
	}
	ASSERT(valuelen == 0);
262
	return 0;
263 264 265 266 267 268 269
}

/*
 * Write the value associated with an attribute into the out-of-line buffer
 * that we have defined for it.
 */
int
270 271
xfs_attr_rmtval_set(
	struct xfs_da_args	*args)
272
{
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	struct xfs_inode	*dp = args->dp;
	struct xfs_mount	*mp = dp->i_mount;
	struct xfs_bmbt_irec	map;
	struct xfs_buf		*bp;
	xfs_daddr_t		dblkno;
	xfs_dablk_t		lblkno;
	xfs_fileoff_t		lfileoff = 0;
	void			*src = args->value;
	int			blkcnt;
	int			valuelen;
	int			nmap;
	int			error;
	int			hdrcnt = 0;
	bool			crcs = xfs_sb_version_hascrc(&mp->m_sb);
	int			offset = 0;
288 289 290 291 292

	trace_xfs_attr_rmtval_set(args);

	/*
	 * Find a "hole" in the attribute address space large enough for
293 294 295 296 297
	 * us to drop the new attribute's value into. Because CRC enable
	 * attributes have headers, we can't just do a straight byte to FSB
	 * conversion. We calculate the worst case block count in this case
	 * and we may not need that many, so we have to handle this when
	 * allocating the blocks below. 
298
	 */
299
	blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
300

301 302
	error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
						   XFS_ATTR_FORK);
303 304 305 306 307 308 309
	if (error)
		return error;

	/* Start with the attribute data. We'll allocate the rest afterwards. */
	if (crcs)
		blkcnt = XFS_B_TO_FSB(mp, args->valuelen);

310 311 312 313 314 315 316
	args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
	args->rmtblkcnt = blkcnt;

	/*
	 * Roll through the "value", allocating blocks on disk as required.
	 */
	while (blkcnt > 0) {
317 318
		int	committed;

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
		/*
		 * Allocate a single extent, up to the size of the value.
		 */
		xfs_bmap_init(args->flist, args->firstblock);
		nmap = 1;
		error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
				  blkcnt,
				  XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
				  args->firstblock, args->total, &map, &nmap,
				  args->flist);
		if (!error) {
			error = xfs_bmap_finish(&args->trans, args->flist,
						&committed);
		}
		if (error) {
			ASSERT(committed);
			args->trans = NULL;
			xfs_bmap_cancel(args->flist);
			return(error);
		}

		/*
		 * bmap_finish() may have committed the last trans and started
		 * a new one.  We need the inode to be in all transactions.
		 */
		if (committed)
			xfs_trans_ijoin(args->trans, dp, 0);

		ASSERT(nmap == 1);
		ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
		       (map.br_startblock != HOLESTARTBLOCK));
		lblkno += map.br_blockcount;
		blkcnt -= map.br_blockcount;
352 353 354 355 356 357 358 359 360 361
		hdrcnt++;

		/*
		 * If we have enough blocks for the attribute data, calculate
		 * how many extra blocks we need for headers. We might run
		 * through this multiple times in the case that the additional
		 * headers in the blocks needed for the data fragments spills
		 * into requiring more blocks. e.g. for 512 byte blocks, we'll
		 * spill for another block every 9 headers we require in this
		 * loop.
362 363 364 365 366
		 *
		 * Note that this can result in contiguous allocation of blocks,
		 * so we don't use all the space we allocate for headers as we
		 * have one less header for each contiguous allocation that
		 * occurs in the map/write loop below.
367 368 369 370 371 372 373 374 375 376
		 */
		if (crcs && blkcnt == 0) {
			int total_len;

			total_len = args->valuelen +
				    hdrcnt * sizeof(struct xfs_attr3_rmt_hdr);
			blkcnt = XFS_B_TO_FSB(mp, total_len);
			blkcnt -= args->rmtblkcnt;
			args->rmtblkcnt += blkcnt;
		}
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

		/*
		 * Start the next trans in the chain.
		 */
		error = xfs_trans_roll(&args->trans, dp);
		if (error)
			return (error);
	}

	/*
	 * Roll through the "value", copying the attribute value to the
	 * already-allocated blocks.  Blocks are written synchronously
	 * so that we can know they are all on disk before we turn off
	 * the INCOMPLETE flag.
	 */
	lblkno = args->rmtblkno;
	valuelen = args->valuelen;
394
	blkcnt = args->rmtblkcnt;
395
	while (valuelen > 0) {
396
		int	byte_cnt;
397 398
		int	hdr_size;
		int	dblkcnt;
399
		char	*buf;
400 401 402 403 404 405 406

		/*
		 * Try to remember where we decided to put the value.
		 */
		xfs_bmap_init(args->flist, args->firstblock);
		nmap = 1;
		error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
407
				       blkcnt, &map, &nmap,
408 409 410 411 412 413 414 415
				       XFS_BMAPI_ATTRFORK);
		if (error)
			return(error);
		ASSERT(nmap == 1);
		ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
		       (map.br_startblock != HOLESTARTBLOCK));

		dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
416
		dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
417

418
		bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
419 420
		if (!bp)
			return ENOMEM;
421 422
		bp->b_ops = &xfs_attr3_rmt_buf_ops;
		buf = bp->b_addr;
423 424 425 426

		byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
		byte_cnt = min_t(int, valuelen, byte_cnt);
		hdr_size = xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
427
					     byte_cnt, bp);
428 429 430
		ASSERT(hdr_size + byte_cnt <= BBTOB(bp->b_length));

		memcpy(buf + hdr_size, src, byte_cnt);
431

432 433 434
		if (byte_cnt + hdr_size < BBTOB(bp->b_length))
			xfs_buf_zero(bp, byte_cnt + hdr_size,
				     BBTOB(bp->b_length) - byte_cnt - hdr_size);
435 436 437 438 439

		error = xfs_bwrite(bp);	/* GROT: NOTE: synchronous write */
		xfs_buf_relse(bp);
		if (error)
			return error;
440 441 442 443

		src += byte_cnt;
		valuelen -= byte_cnt;
		offset += byte_cnt;
444 445

		lblkno += map.br_blockcount;
446
		blkcnt -= map.br_blockcount;
447 448
	}
	ASSERT(valuelen == 0);
449
	return 0;
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
}

/*
 * Remove the value associated with an attribute by deleting the
 * out-of-line buffer that it is stored on.
 */
int
xfs_attr_rmtval_remove(xfs_da_args_t *args)
{
	xfs_mount_t *mp;
	xfs_bmbt_irec_t map;
	xfs_buf_t *bp;
	xfs_daddr_t dblkno;
	xfs_dablk_t lblkno;
	int valuelen, blkcnt, nmap, error, done, committed;

	trace_xfs_attr_rmtval_remove(args);

	mp = args->dp->i_mount;

	/*
471 472 473 474 475 476
	 * Roll through the "value", invalidating the attribute value's blocks.
	 * Note that args->rmtblkcnt is the minimum number of data blocks we'll
	 * see for a CRC enabled remote attribute. Each extent will have a
	 * header, and so we may have more blocks than we realise here.  If we
	 * fail to map the blocks correctly, we'll have problems with the buffer
	 * lookups.
477 478
	 */
	lblkno = args->rmtblkno;
479 480
	valuelen = args->valuelen;
	blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
481
	while (valuelen > 0) {
482 483
		int dblkcnt;

484 485 486 487 488
		/*
		 * Try to remember where we decided to put the value.
		 */
		nmap = 1;
		error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
489
				       blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
490 491 492 493 494 495 496
		if (error)
			return(error);
		ASSERT(nmap == 1);
		ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
		       (map.br_startblock != HOLESTARTBLOCK));

		dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
497
		dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
498 499 500 501

		/*
		 * If the "remote" value is in the cache, remove it.
		 */
502
		bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
503 504 505 506 507 508
		if (bp) {
			xfs_buf_stale(bp);
			xfs_buf_relse(bp);
			bp = NULL;
		}

509 510
		valuelen -= XFS_ATTR3_RMT_BUF_SPACE(mp,
					XFS_FSB_TO_B(mp, map.br_blockcount));
511 512

		lblkno += map.br_blockcount;
513 514
		blkcnt -= map.br_blockcount;
		blkcnt = max(blkcnt, xfs_attr3_rmt_blocks(mp, valuelen));
515 516 517 518 519
	}

	/*
	 * Keep de-allocating extents until the remote-value region is gone.
	 */
520
	blkcnt = lblkno - args->rmtblkno;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	lblkno = args->rmtblkno;
	done = 0;
	while (!done) {
		xfs_bmap_init(args->flist, args->firstblock);
		error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
				    XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
				    1, args->firstblock, args->flist,
				    &done);
		if (!error) {
			error = xfs_bmap_finish(&args->trans, args->flist,
						&committed);
		}
		if (error) {
			ASSERT(committed);
			args->trans = NULL;
			xfs_bmap_cancel(args->flist);
537
			return error;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
		}

		/*
		 * bmap_finish() may have committed the last trans and started
		 * a new one.  We need the inode to be in all transactions.
		 */
		if (committed)
			xfs_trans_ijoin(args->trans, args->dp, 0);

		/*
		 * Close out trans and start the next one in the chain.
		 */
		error = xfs_trans_roll(&args->trans, args->dp);
		if (error)
			return (error);
	}
	return(0);
}