eattr.c 31.6 KB
Newer Older
D
David Teigland 已提交
1 2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3
 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
D
David Teigland 已提交
4 5 6
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
7
 * of the GNU General Public License version 2.
D
David Teigland 已提交
8 9 10 11 12 13 14
 */

#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
#include <linux/xattr.h>
15
#include <linux/gfs2_ondisk.h>
16
#include <linux/lm_interface.h>
D
David Teigland 已提交
17 18 19
#include <asm/uaccess.h>

#include "gfs2.h"
20
#include "incore.h"
D
David Teigland 已提交
21 22 23 24 25 26 27 28 29
#include "acl.h"
#include "eaops.h"
#include "eattr.h"
#include "glock.h"
#include "inode.h"
#include "meta_io.h"
#include "quota.h"
#include "rgrp.h"
#include "trans.h"
30
#include "util.h"
D
David Teigland 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

/**
 * ea_calc_size - returns the acutal number of bytes the request will take up
 *                (not counting any unstuffed data blocks)
 * @sdp:
 * @er:
 * @size:
 *
 * Returns: 1 if the EA should be stuffed
 */

static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
			unsigned int *size)
{
	*size = GFS2_EAREQ_SIZE_STUFFED(er);
	if (*size <= sdp->sd_jbsize)
		return 1;

	*size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);

	return 0;
}

static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
{
	unsigned int size;

	if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
		return -ERANGE;

	ea_calc_size(sdp, er, &size);

	/* This can only happen with 512 byte blocks */
	if (size > sdp->sd_jbsize)
		return -ERANGE;

	return 0;
}

70
typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
D
David Teigland 已提交
71
			  struct gfs2_ea_header *ea,
72
			  struct gfs2_ea_header *prev, void *private);
D
David Teigland 已提交
73 74 75 76 77 78 79

static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
			ea_call_t ea_call, void *data)
{
	struct gfs2_ea_header *ea, *prev = NULL;
	int error = 0;

80
	if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
D
David Teigland 已提交
81 82 83 84 85
		return -EIO;

	for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
		if (!GFS2_EA_REC_LEN(ea))
			goto fail;
86 87
		if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
						  bh->b_data + bh->b_size))
D
David Teigland 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
			goto fail;
		if (!GFS2_EATYPE_VALID(ea->ea_type))
			goto fail;

		error = ea_call(ip, bh, ea, prev, data);
		if (error)
			return error;

		if (GFS2_EA_IS_LAST(ea)) {
			if ((char *)GFS2_EA2NEXT(ea) !=
			    bh->b_data + bh->b_size)
				goto fail;
			break;
		}
	}

	return error;

106
fail:
D
David Teigland 已提交
107 108 109 110 111 112 113
	gfs2_consist_inode(ip);
	return -EIO;
}

static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
{
	struct buffer_head *bh, *eabh;
A
Al Viro 已提交
114
	__be64 *eablk, *end;
D
David Teigland 已提交
115 116
	int error;

S
Steven Whitehouse 已提交
117
	error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &bh);
D
David Teigland 已提交
118 119 120 121 122 123 124 125
	if (error)
		return error;

	if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
		error = ea_foreach_i(ip, bh, ea_call, data);
		goto out;
	}

126
	if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
D
David Teigland 已提交
127 128 129 130
		error = -EIO;
		goto out;
	}

A
Al Viro 已提交
131
	eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
132
	end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
D
David Teigland 已提交
133 134

	for (; eablk < end; eablk++) {
135
		u64 bn;
D
David Teigland 已提交
136 137 138 139 140

		if (!*eablk)
			break;
		bn = be64_to_cpu(*eablk);

S
Steven Whitehouse 已提交
141
		error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh);
D
David Teigland 已提交
142 143 144 145 146 147 148
		if (error)
			break;
		error = ea_foreach_i(ip, eabh, ea_call, data);
		brelse(eabh);
		if (error)
			break;
	}
149
out:
D
David Teigland 已提交
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	brelse(bh);
	return error;
}

struct ea_find {
	struct gfs2_ea_request *ef_er;
	struct gfs2_ea_location *ef_el;
};

static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
		     struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
		     void *private)
{
	struct ea_find *ef = private;
	struct gfs2_ea_request *er = ef->ef_er;

	if (ea->ea_type == GFS2_EATYPE_UNUSED)
		return 0;

	if (ea->ea_type == er->er_type) {
		if (ea->ea_name_len == er->er_name_len &&
		    !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
			struct gfs2_ea_location *el = ef->ef_el;
			get_bh(bh);
			el->el_bh = bh;
			el->el_ea = ea;
			el->el_prev = prev;
			return 1;
		}
	}

	return 0;
}

int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
		 struct gfs2_ea_location *el)
{
	struct ea_find ef;
	int error;

	ef.ef_er = er;
	ef.ef_el = el;

	memset(el, 0, sizeof(struct gfs2_ea_location));

	error = ea_foreach(ip, ea_find_i, &ef);
	if (error > 0)
		return 0;

	return error;
}

/**
 * ea_dealloc_unstuffed -
 * @ip:
 * @bh:
 * @ea:
 * @prev:
 * @private:
 *
 * Take advantage of the fact that all unstuffed blocks are
 * allocated from the same RG.  But watch, this may not always
 * be true.
 *
 * Returns: errno
 */

static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
				struct gfs2_ea_header *ea,
				struct gfs2_ea_header *prev, void *private)
{
	int *leave = private;
222
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
223 224 225
	struct gfs2_rgrpd *rgd;
	struct gfs2_holder rg_gh;
	struct buffer_head *dibh;
A
Al Viro 已提交
226 227
	__be64 *dataptrs;
	u64 bn = 0;
228
	u64 bstart = 0;
D
David Teigland 已提交
229 230 231 232 233 234 235 236 237
	unsigned int blen = 0;
	unsigned int blks = 0;
	unsigned int x;
	int error;

	if (GFS2_EA_IS_STUFFED(ea))
		return 0;

	dataptrs = GFS2_EA2DATAPTRS(ea);
238
	for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
D
David Teigland 已提交
239 240 241 242
		if (*dataptrs) {
			blks++;
			bn = be64_to_cpu(*dataptrs);
		}
243
	}
D
David Teigland 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256
	if (!blks)
		return 0;

	rgd = gfs2_blk2rgrpd(sdp, bn);
	if (!rgd) {
		gfs2_consist_inode(ip);
		return -EIO;
	}

	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
	if (error)
		return error;

257
	error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
258
				 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
D
David Teigland 已提交
259 260 261
	if (error)
		goto out_gunlock;

262
	gfs2_trans_add_bh(ip->i_gl, bh, 1);
D
David Teigland 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

	dataptrs = GFS2_EA2DATAPTRS(ea);
	for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
		if (!*dataptrs)
			break;
		bn = be64_to_cpu(*dataptrs);

		if (bstart + blen == bn)
			blen++;
		else {
			if (bstart)
				gfs2_free_meta(ip, bstart, blen);
			bstart = bn;
			blen = 1;
		}

		*dataptrs = 0;
		if (!ip->i_di.di_blocks)
			gfs2_consist_inode(ip);
		ip->i_di.di_blocks--;
283
		gfs2_set_inode_blocks(&ip->i_inode);
D
David Teigland 已提交
284 285 286 287 288
	}
	if (bstart)
		gfs2_free_meta(ip, bstart, blen);

	if (prev && !leave) {
289
		u32 len;
D
David Teigland 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302

		len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
		prev->ea_rec_len = cpu_to_be32(len);

		if (GFS2_EA_IS_LAST(ea))
			prev->ea_flags |= GFS2_EAFLAG_LAST;
	} else {
		ea->ea_type = GFS2_EATYPE_UNUSED;
		ea->ea_num_ptrs = 0;
	}

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (!error) {
303
		ip->i_inode.i_ctime = CURRENT_TIME;
304
		gfs2_trans_add_bh(ip->i_gl, dibh, 1);
305
		gfs2_dinode_out(ip, dibh->b_data);
D
David Teigland 已提交
306 307 308 309 310
		brelse(dibh);
	}

	gfs2_trans_end(sdp);

311
out_gunlock:
D
David Teigland 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	gfs2_glock_dq_uninit(&rg_gh);
	return error;
}

static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
			       struct gfs2_ea_header *ea,
			       struct gfs2_ea_header *prev, int leave)
{
	struct gfs2_alloc *al;
	int error;

	al = gfs2_alloc_get(ip);

	error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
	if (error)
		goto out_alloc;

329
	error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
D
David Teigland 已提交
330 331 332
	if (error)
		goto out_quota;

333
	error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
D
David Teigland 已提交
334 335 336

	gfs2_glock_dq_uninit(&al->al_ri_gh);

337
out_quota:
D
David Teigland 已提交
338
	gfs2_quota_unhold(ip);
339
out_alloc:
D
David Teigland 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	gfs2_alloc_put(ip);
	return error;
}

struct ea_list {
	struct gfs2_ea_request *ei_er;
	unsigned int ei_size;
};

static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
		     struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
		     void *private)
{
	struct ea_list *ei = private;
	struct gfs2_ea_request *er = ei->ei_er;
R
Ryan O'Hara 已提交
355
	unsigned int ea_size = gfs2_ea_strlen(ea);
D
David Teigland 已提交
356 357 358 359 360

	if (ea->ea_type == GFS2_EATYPE_UNUSED)
		return 0;

	if (er->er_data_len) {
361 362
		char *prefix = NULL;
		unsigned int l = 0;
D
David Teigland 已提交
363 364 365 366 367
		char c = 0;

		if (ei->ei_size + ea_size > er->er_data_len)
			return -ERANGE;

R
Ryan O'Hara 已提交
368 369
		switch (ea->ea_type) {
		case GFS2_EATYPE_USR:
D
David Teigland 已提交
370 371
			prefix = "user.";
			l = 5;
R
Ryan O'Hara 已提交
372 373
			break;
		case GFS2_EATYPE_SYS:
D
David Teigland 已提交
374 375
			prefix = "system.";
			l = 7;
R
Ryan O'Hara 已提交
376 377 378 379 380
			break;
		case GFS2_EATYPE_SECURITY:
			prefix = "security.";
			l = 9;
			break;
D
David Teigland 已提交
381 382
		}

383 384
		BUG_ON(l == 0);

385 386
		memcpy(er->er_data + ei->ei_size, prefix, l);
		memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
D
David Teigland 已提交
387
		       ea->ea_name_len);
388
		memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
D
David Teigland 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	}

	ei->ei_size += ea_size;

	return 0;
}

/**
 * gfs2_ea_list -
 * @ip:
 * @er:
 *
 * Returns: actual size of data on success, -errno on error
 */

int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
{
	struct gfs2_holder i_gh;
	int error;

	if (!er->er_data || !er->er_data_len) {
		er->er_data = NULL;
		er->er_data_len = 0;
	}

414
	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
D
David Teigland 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	if (error)
		return error;

	if (ip->i_di.di_eattr) {
		struct ea_list ei = { .ei_er = er, .ei_size = 0 };

		error = ea_foreach(ip, ea_list_i, &ei);
		if (!error)
			error = ei.ei_size;
	}

	gfs2_glock_dq_uninit(&i_gh);

	return error;
}

/**
 * ea_get_unstuffed - actually copies the unstuffed data into the
 *                    request buffer
434 435 436
 * @ip: The GFS2 inode
 * @ea: The extended attribute header structure
 * @data: The data to be copied
D
David Teigland 已提交
437 438 439 440 441 442 443
 *
 * Returns: errno
 */

static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
			    char *data)
{
444
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
445 446
	struct buffer_head **bh;
	unsigned int amount = GFS2_EA_DATA_LEN(ea);
447
	unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
A
Al Viro 已提交
448
	__be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
D
David Teigland 已提交
449 450 451 452 453 454 455 456
	unsigned int x;
	int error = 0;

	bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
	if (!bh)
		return -ENOMEM;

	for (x = 0; x < nptrs; x++) {
S
Steven Whitehouse 已提交
457 458
		error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
				       bh + x);
D
David Teigland 已提交
459 460 461 462 463 464 465 466 467
		if (error) {
			while (x--)
				brelse(bh[x]);
			goto out;
		}
		dataptrs++;
	}

	for (x = 0; x < nptrs; x++) {
S
Steven Whitehouse 已提交
468
		error = gfs2_meta_wait(sdp, bh[x]);
D
David Teigland 已提交
469 470 471 472 473 474 475 476 477 478 479 480
		if (error) {
			for (; x < nptrs; x++)
				brelse(bh[x]);
			goto out;
		}
		if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
			for (; x < nptrs; x++)
				brelse(bh[x]);
			error = -EIO;
			goto out;
		}

481
		memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
D
David Teigland 已提交
482 483 484 485 486 487 488 489
		       (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);

		amount -= sdp->sd_jbsize;
		data += sdp->sd_jbsize;

		brelse(bh[x]);
	}

490
out:
D
David Teigland 已提交
491 492 493 494 495 496 497 498
	kfree(bh);
	return error;
}

int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
		     char *data)
{
	if (GFS2_EA_IS_STUFFED(el->el_ea)) {
499
		memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea));
D
David Teigland 已提交
500 501 502 503 504 505 506
		return 0;
	} else
		return ea_get_unstuffed(ip, el->el_ea, data);
}

/**
 * gfs2_ea_get_i -
507 508
 * @ip: The GFS2 inode
 * @er: The request structure
D
David Teigland 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
 *
 * Returns: actual size of data on success, -errno on error
 */

int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
{
	struct gfs2_ea_location el;
	int error;

	if (!ip->i_di.di_eattr)
		return -ENODATA;

	error = gfs2_ea_find(ip, er, &el);
	if (error)
		return error;
	if (!el.el_ea)
		return -ENODATA;

	if (er->er_data_len) {
		if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
			error =  -ERANGE;
		else
			error = gfs2_ea_get_copy(ip, &el, er->er_data);
	}
	if (!error)
		error = GFS2_EA_DATA_LEN(el.el_ea);

	brelse(el.el_bh);

	return error;
}

/**
 * gfs2_ea_get -
543 544
 * @ip: The GFS2 inode
 * @er: The request structure
D
David Teigland 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
 *
 * Returns: actual size of data on success, -errno on error
 */

int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
{
	struct gfs2_holder i_gh;
	int error;

	if (!er->er_name_len ||
	    er->er_name_len > GFS2_EA_MAX_NAME_LEN)
		return -EINVAL;
	if (!er->er_data || !er->er_data_len) {
		er->er_data = NULL;
		er->er_data_len = 0;
	}

562
	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
D
David Teigland 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575
	if (error)
		return error;

	error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);

	gfs2_glock_dq_uninit(&i_gh);

	return error;
}

/**
 * ea_alloc_blk - allocates a new block for extended attributes.
 * @ip: A pointer to the inode that's getting extended attributes
576
 * @bhp: Pointer to pointer to a struct buffer_head
D
David Teigland 已提交
577 578 579 580 581 582
 *
 * Returns: errno
 */

static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
{
583
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
584
	struct gfs2_ea_header *ea;
585
	u64 block;
D
David Teigland 已提交
586 587 588 589

	block = gfs2_alloc_meta(ip);

	*bhp = gfs2_meta_new(ip->i_gl, block);
590
	gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
D
David Teigland 已提交
591 592 593 594 595 596 597 598 599 600
	gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
	gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));

	ea = GFS2_EA_BH2FIRST(*bhp);
	ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
	ea->ea_type = GFS2_EATYPE_UNUSED;
	ea->ea_flags = GFS2_EAFLAG_LAST;
	ea->ea_num_ptrs = 0;

	ip->i_di.di_blocks++;
601
	gfs2_set_inode_blocks(&ip->i_inode);
D
David Teigland 已提交
602 603 604 605 606 607 608

	return 0;
}

/**
 * ea_write - writes the request info to an ea, creating new blocks if
 *            necessary
609 610
 * @ip: inode that is being modified
 * @ea: the location of the new ea in a block
D
David Teigland 已提交
611 612 613 614 615 616 617 618 619 620
 * @er: the write request
 *
 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
 *
 * returns : errno
 */

static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
		    struct gfs2_ea_request *er)
{
621
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
622 623 624 625 626 627 628 629 630 631 632 633

	ea->ea_data_len = cpu_to_be32(er->er_data_len);
	ea->ea_name_len = er->er_name_len;
	ea->ea_type = er->er_type;
	ea->__pad = 0;

	memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);

	if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
		ea->ea_num_ptrs = 0;
		memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
	} else {
A
Al Viro 已提交
634
		__be64 *dataptr = GFS2_EA2DATAPTRS(ea);
D
David Teigland 已提交
635 636 637 638 639
		const char *data = er->er_data;
		unsigned int data_len = er->er_data_len;
		unsigned int copy;
		unsigned int x;

640
		ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
D
David Teigland 已提交
641 642
		for (x = 0; x < ea->ea_num_ptrs; x++) {
			struct buffer_head *bh;
643
			u64 block;
D
David Teigland 已提交
644 645 646 647 648
			int mh_size = sizeof(struct gfs2_meta_header);

			block = gfs2_alloc_meta(ip);

			bh = gfs2_meta_new(ip->i_gl, block);
649
			gfs2_trans_add_bh(ip->i_gl, bh, 1);
D
David Teigland 已提交
650 651 652
			gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);

			ip->i_di.di_blocks++;
653
			gfs2_set_inode_blocks(&ip->i_inode);
D
David Teigland 已提交
654

655 656
			copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
							   data_len;
D
David Teigland 已提交
657 658 659 660 661
			memcpy(bh->b_data + mh_size, data, copy);
			if (copy < sdp->sd_jbsize)
				memset(bh->b_data + mh_size + copy, 0,
				       sdp->sd_jbsize - copy);

662
			*dataptr++ = cpu_to_be64(bh->b_blocknr);
D
David Teigland 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675
			data += copy;
			data_len -= copy;

			brelse(bh);
		}

		gfs2_assert_withdraw(sdp, !data_len);
	}

	return 0;
}

typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
676
				   struct gfs2_ea_request *er, void *private);
D
David Teigland 已提交
677 678 679

static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
			     unsigned int blks,
680
			     ea_skeleton_call_t skeleton_call, void *private)
D
David Teigland 已提交
681 682 683 684 685 686 687 688 689 690 691
{
	struct gfs2_alloc *al;
	struct buffer_head *dibh;
	int error;

	al = gfs2_alloc_get(ip);

	error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
	if (error)
		goto out;

692
	error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
D
David Teigland 已提交
693 694 695 696 697 698 699 700 701
	if (error)
		goto out_gunlock_q;

	al->al_requested = blks;

	error = gfs2_inplace_reserve(ip);
	if (error)
		goto out_gunlock_q;

702
	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
703
				 blks + al->al_rgd->rd_length +
D
David Teigland 已提交
704 705 706 707 708 709 710 711 712 713 714
				 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
	if (error)
		goto out_ipres;

	error = skeleton_call(ip, er, private);
	if (error)
		goto out_end_trans;

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (!error) {
		if (er->er_flags & GFS2_ERF_MODE) {
715
			gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
716
					    (ip->i_inode.i_mode & S_IFMT) ==
D
David Teigland 已提交
717
					    (er->er_mode & S_IFMT));
718
			ip->i_inode.i_mode = er->er_mode;
D
David Teigland 已提交
719
		}
720
		ip->i_inode.i_ctime = CURRENT_TIME;
721
		gfs2_trans_add_bh(ip->i_gl, dibh, 1);
722
		gfs2_dinode_out(ip, dibh->b_data);
D
David Teigland 已提交
723 724 725
		brelse(dibh);
	}

726
out_end_trans:
727
	gfs2_trans_end(GFS2_SB(&ip->i_inode));
728
out_ipres:
D
David Teigland 已提交
729
	gfs2_inplace_release(ip);
730
out_gunlock_q:
D
David Teigland 已提交
731
	gfs2_quota_unlock(ip);
732
out:
D
David Teigland 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	gfs2_alloc_put(ip);
	return error;
}

static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
		     void *private)
{
	struct buffer_head *bh;
	int error;

	error = ea_alloc_blk(ip, &bh);
	if (error)
		return error;

	ip->i_di.di_eattr = bh->b_blocknr;
	error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);

	brelse(bh);

	return error;
}

/**
 * ea_init - initializes a new eattr block
 * @ip:
 * @er:
 *
 * Returns: errno
 */

static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
{
765
	unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
D
David Teigland 已提交
766 767 768
	unsigned int blks = 1;

	if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
769
		blks += DIV_ROUND_UP(er->er_data_len, jbsize);
D
David Teigland 已提交
770 771 772 773 774 775

	return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
}

static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
{
776
	u32 ea_size = GFS2_EA_SIZE(ea);
777 778
	struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
				     ea_size);
779
	u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
D
David Teigland 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	int last = ea->ea_flags & GFS2_EAFLAG_LAST;

	ea->ea_rec_len = cpu_to_be32(ea_size);
	ea->ea_flags ^= last;

	new->ea_rec_len = cpu_to_be32(new_size);
	new->ea_flags = last;

	return new;
}

static void ea_set_remove_stuffed(struct gfs2_inode *ip,
				  struct gfs2_ea_location *el)
{
	struct gfs2_ea_header *ea = el->el_ea;
	struct gfs2_ea_header *prev = el->el_prev;
796
	u32 len;
D
David Teigland 已提交
797

798
	gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
D
David Teigland 已提交
799 800 801 802 803 804

	if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
		ea->ea_type = GFS2_EATYPE_UNUSED;
		return;
	} else if (GFS2_EA2NEXT(prev) != ea) {
		prev = GFS2_EA2NEXT(prev);
805
		gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
D
David Teigland 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
	}

	len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
	prev->ea_rec_len = cpu_to_be32(len);

	if (GFS2_EA_IS_LAST(ea))
		prev->ea_flags |= GFS2_EAFLAG_LAST;
}

struct ea_set {
	int ea_split;

	struct gfs2_ea_request *es_er;
	struct gfs2_ea_location *es_el;

	struct buffer_head *es_bh;
	struct gfs2_ea_header *es_ea;
};

static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
				 struct gfs2_ea_header *ea, struct ea_set *es)
{
	struct gfs2_ea_request *er = es->es_er;
	struct buffer_head *dibh;
	int error;

832
	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
D
David Teigland 已提交
833 834 835
	if (error)
		return error;

836
	gfs2_trans_add_bh(ip->i_gl, bh, 1);
D
David Teigland 已提交
837 838 839 840 841 842 843 844 845 846 847 848 849 850

	if (es->ea_split)
		ea = ea_split_ea(ea);

	ea_write(ip, ea, er);

	if (es->es_el)
		ea_set_remove_stuffed(ip, es->es_el);

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (error)
		goto out;

	if (er->er_flags & GFS2_ERF_MODE) {
851
		gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
852 853
			(ip->i_inode.i_mode & S_IFMT) == (er->er_mode & S_IFMT));
		ip->i_inode.i_mode = er->er_mode;
D
David Teigland 已提交
854
	}
855
	ip->i_inode.i_ctime = CURRENT_TIME;
856
	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
857
	gfs2_dinode_out(ip, dibh->b_data);
D
David Teigland 已提交
858
	brelse(dibh);
859
out:
860
	gfs2_trans_end(GFS2_SB(&ip->i_inode));
D
David Teigland 已提交
861 862 863 864 865 866 867 868 869 870
	return error;
}

static int ea_set_simple_alloc(struct gfs2_inode *ip,
			       struct gfs2_ea_request *er, void *private)
{
	struct ea_set *es = private;
	struct gfs2_ea_header *ea = es->es_ea;
	int error;

871
	gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
D
David Teigland 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

	if (es->ea_split)
		ea = ea_split_ea(ea);

	error = ea_write(ip, ea, er);
	if (error)
		return error;

	if (es->es_el)
		ea_set_remove_stuffed(ip, es->es_el);

	return 0;
}

static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
			 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
			 void *private)
{
	struct ea_set *es = private;
	unsigned int size;
	int stuffed;
	int error;

895
	stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
D
David Teigland 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

	if (ea->ea_type == GFS2_EATYPE_UNUSED) {
		if (GFS2_EA_REC_LEN(ea) < size)
			return 0;
		if (!GFS2_EA_IS_STUFFED(ea)) {
			error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
			if (error)
				return error;
		}
		es->ea_split = 0;
	} else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
		es->ea_split = 1;
	else
		return 0;

	if (stuffed) {
		error = ea_set_simple_noalloc(ip, bh, ea, es);
		if (error)
			return error;
	} else {
		unsigned int blks;

		es->es_bh = bh;
		es->es_ea = ea;
920
		blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
921
					GFS2_SB(&ip->i_inode)->sd_jbsize);
D
David Teigland 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934

		error = ea_alloc_skeleton(ip, es->es_er, blks,
					  ea_set_simple_alloc, es);
		if (error)
			return error;
	}

	return 1;
}

static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
			void *private)
{
935
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
936
	struct buffer_head *indbh, *newbh;
A
Al Viro 已提交
937
	__be64 *eablk;
D
David Teigland 已提交
938 939 940 941
	int error;
	int mh_size = sizeof(struct gfs2_meta_header);

	if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
A
Al Viro 已提交
942
		__be64 *end;
D
David Teigland 已提交
943

S
Steven Whitehouse 已提交
944 945
		error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT,
				       &indbh);
D
David Teigland 已提交
946 947 948 949 950 951 952 953
		if (error)
			return error;

		if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
			error = -EIO;
			goto out;
		}

A
Al Viro 已提交
954
		eablk = (__be64 *)(indbh->b_data + mh_size);
D
David Teigland 已提交
955 956 957 958 959 960 961 962 963 964 965
		end = eablk + sdp->sd_inptrs;

		for (; eablk < end; eablk++)
			if (!*eablk)
				break;

		if (eablk == end) {
			error = -ENOSPC;
			goto out;
		}

966
		gfs2_trans_add_bh(ip->i_gl, indbh, 1);
D
David Teigland 已提交
967
	} else {
968
		u64 blk;
D
David Teigland 已提交
969 970 971 972

		blk = gfs2_alloc_meta(ip);

		indbh = gfs2_meta_new(ip->i_gl, blk);
973
		gfs2_trans_add_bh(ip->i_gl, indbh, 1);
D
David Teigland 已提交
974 975 976
		gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
		gfs2_buffer_clear_tail(indbh, mh_size);

A
Al Viro 已提交
977
		eablk = (__be64 *)(indbh->b_data + mh_size);
D
David Teigland 已提交
978 979 980 981
		*eablk = cpu_to_be64(ip->i_di.di_eattr);
		ip->i_di.di_eattr = blk;
		ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
		ip->i_di.di_blocks++;
982
		gfs2_set_inode_blocks(&ip->i_inode);
D
David Teigland 已提交
983 984 985 986 987 988 989 990

		eablk++;
	}

	error = ea_alloc_blk(ip, &newbh);
	if (error)
		goto out;

991
	*eablk = cpu_to_be64((u64)newbh->b_blocknr);
D
David Teigland 已提交
992 993 994 995 996 997
	error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
	brelse(newbh);
	if (error)
		goto out;

	if (private)
998
		ea_set_remove_stuffed(ip, private);
D
David Teigland 已提交
999

1000
out:
D
David Teigland 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
	brelse(indbh);
	return error;
}

static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
		    struct gfs2_ea_location *el)
{
	struct ea_set es;
	unsigned int blks = 2;
	int error;

	memset(&es, 0, sizeof(struct ea_set));
	es.es_er = er;
	es.es_el = el;

	error = ea_foreach(ip, ea_set_simple, &es);
	if (error > 0)
		return 0;
	if (error)
		return error;

	if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
		blks++;
1024 1025
	if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
		blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
D
David Teigland 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034

	return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
}

static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
				   struct gfs2_ea_location *el)
{
	if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
		el->el_prev = GFS2_EA2NEXT(el->el_prev);
1035
		gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
D
David Teigland 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
				     GFS2_EA2NEXT(el->el_prev) == el->el_ea);
	}

	return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
}

int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
{
	struct gfs2_ea_location el;
	int error;

	if (!ip->i_di.di_eattr) {
		if (er->er_flags & XATTR_REPLACE)
			return -ENODATA;
		return ea_init(ip, er);
	}

	error = gfs2_ea_find(ip, er, &el);
	if (error)
		return error;

	if (el.el_ea) {
		if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
			brelse(el.el_bh);
			return -EPERM;
		}

		error = -EEXIST;
		if (!(er->er_flags & XATTR_CREATE)) {
			int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
			error = ea_set_i(ip, er, &el);
			if (!error && unstuffed)
				ea_set_remove_unstuffed(ip, &el);
		}

		brelse(el.el_bh);
	} else {
		error = -ENODATA;
		if (!(er->er_flags & XATTR_REPLACE))
			error = ea_set_i(ip, er, NULL);
	}

	return error;
}

int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
{
	struct gfs2_holder i_gh;
	int error;

1086
	if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
D
David Teigland 已提交
1087 1088 1089 1090 1091
		return -EINVAL;
	if (!er->er_data || !er->er_data_len) {
		er->er_data = NULL;
		er->er_data_len = 0;
	}
1092
	error = ea_check_size(GFS2_SB(&ip->i_inode), er);
D
David Teigland 已提交
1093 1094 1095 1096 1097 1098 1099
	if (error)
		return error;

	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
	if (error)
		return error;

1100
	if (IS_IMMUTABLE(&ip->i_inode))
D
David Teigland 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
		error = -EPERM;
	else
		error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);

	gfs2_glock_dq_uninit(&i_gh);

	return error;
}

static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
{
	struct gfs2_ea_header *ea = el->el_ea;
	struct gfs2_ea_header *prev = el->el_prev;
	struct buffer_head *dibh;
	int error;

1117
	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
D
David Teigland 已提交
1118 1119 1120
	if (error)
		return error;

1121
	gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
D
David Teigland 已提交
1122 1123

	if (prev) {
1124
		u32 len;
D
David Teigland 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135

		len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
		prev->ea_rec_len = cpu_to_be32(len);

		if (GFS2_EA_IS_LAST(ea))
			prev->ea_flags |= GFS2_EAFLAG_LAST;
	} else
		ea->ea_type = GFS2_EATYPE_UNUSED;

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (!error) {
1136
		ip->i_inode.i_ctime = CURRENT_TIME;
1137
		gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1138
		gfs2_dinode_out(ip, dibh->b_data);
D
David Teigland 已提交
1139
		brelse(dibh);
1140
	}
D
David Teigland 已提交
1141

1142
	gfs2_trans_end(GFS2_SB(&ip->i_inode));
D
David Teigland 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

	return error;
}

int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
{
	struct gfs2_ea_location el;
	int error;

	if (!ip->i_di.di_eattr)
		return -ENODATA;

	error = gfs2_ea_find(ip, er, &el);
	if (error)
		return error;
	if (!el.el_ea)
		return -ENODATA;

	if (GFS2_EA_IS_STUFFED(el.el_ea))
		error = ea_remove_stuffed(ip, &el);
	else
		error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
					    0);

	brelse(el.el_bh);

	return error;
}

/**
 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
 * @ip: pointer to the inode of the target file
 * @er: request information
 *
 * Returns: errno
 */

int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
{
	struct gfs2_holder i_gh;
	int error;

	if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
		return -EINVAL;

	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
	if (error)
		return error;

1192
	if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
D
David Teigland 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
		error = -EPERM;
	else
		error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);

	gfs2_glock_dq_uninit(&i_gh);

	return error;
}

static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
				  struct gfs2_ea_header *ea, char *data)
{
1205
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
1206 1207
	struct buffer_head **bh;
	unsigned int amount = GFS2_EA_DATA_LEN(ea);
1208
	unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
A
Al Viro 已提交
1209
	__be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
D
David Teigland 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	unsigned int x;
	int error;

	bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
	if (!bh)
		return -ENOMEM;

	error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
	if (error)
		goto out;

	for (x = 0; x < nptrs; x++) {
S
Steven Whitehouse 已提交
1222 1223
		error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
				       bh + x);
D
David Teigland 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232
		if (error) {
			while (x--)
				brelse(bh[x]);
			goto fail;
		}
		dataptrs++;
	}

	for (x = 0; x < nptrs; x++) {
S
Steven Whitehouse 已提交
1233
		error = gfs2_meta_wait(sdp, bh[x]);
D
David Teigland 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
		if (error) {
			for (; x < nptrs; x++)
				brelse(bh[x]);
			goto fail;
		}
		if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
			for (; x < nptrs; x++)
				brelse(bh[x]);
			error = -EIO;
			goto fail;
		}

1246
		gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
D
David Teigland 已提交
1247

1248
		memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
D
David Teigland 已提交
1249 1250 1251 1252 1253 1254 1255 1256
		       (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);

		amount -= sdp->sd_jbsize;
		data += sdp->sd_jbsize;

		brelse(bh[x]);
	}

1257
out:
D
David Teigland 已提交
1258 1259 1260
	kfree(bh);
	return error;

1261
fail:
D
David Teigland 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	gfs2_trans_end(sdp);
	kfree(bh);
	return error;
}

int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
		      struct iattr *attr, char *data)
{
	struct buffer_head *dibh;
	int error;

	if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1274
		error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
D
David Teigland 已提交
1275 1276 1277
		if (error)
			return error;

1278
		gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1279
		memcpy(GFS2_EA2DATA(el->el_ea), data,
D
David Teigland 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288
		       GFS2_EA_DATA_LEN(el->el_ea));
	} else
		error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);

	if (error)
		return error;

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (!error) {
1289 1290
		error = inode_setattr(&ip->i_inode, attr);
		gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1291
		gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1292
		gfs2_dinode_out(ip, dibh->b_data);
D
David Teigland 已提交
1293 1294 1295
		brelse(dibh);
	}

1296
	gfs2_trans_end(GFS2_SB(&ip->i_inode));
D
David Teigland 已提交
1297 1298 1299 1300 1301 1302

	return error;
}

static int ea_dealloc_indirect(struct gfs2_inode *ip)
{
1303
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
1304 1305
	struct gfs2_rgrp_list rlist;
	struct buffer_head *indbh, *dibh;
A
Al Viro 已提交
1306
	__be64 *eablk, *end;
D
David Teigland 已提交
1307
	unsigned int rg_blocks = 0;
1308
	u64 bstart = 0;
D
David Teigland 已提交
1309 1310 1311 1312 1313 1314 1315
	unsigned int blen = 0;
	unsigned int blks = 0;
	unsigned int x;
	int error;

	memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));

S
Steven Whitehouse 已提交
1316
	error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &indbh);
D
David Teigland 已提交
1317 1318 1319 1320 1321 1322 1323 1324
	if (error)
		return error;

	if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
		error = -EIO;
		goto out;
	}

A
Al Viro 已提交
1325
	eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
D
David Teigland 已提交
1326 1327 1328
	end = eablk + sdp->sd_inptrs;

	for (; eablk < end; eablk++) {
1329
		u64 bn;
D
David Teigland 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353

		if (!*eablk)
			break;
		bn = be64_to_cpu(*eablk);

		if (bstart + blen == bn)
			blen++;
		else {
			if (bstart)
				gfs2_rlist_add(sdp, &rlist, bstart);
			bstart = bn;
			blen = 1;
		}
		blks++;
	}
	if (bstart)
		gfs2_rlist_add(sdp, &rlist, bstart);
	else
		goto out;

	gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);

	for (x = 0; x < rlist.rl_rgrps; x++) {
		struct gfs2_rgrpd *rgd;
1354
		rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1355
		rg_blocks += rgd->rd_length;
D
David Teigland 已提交
1356 1357 1358 1359 1360 1361
	}

	error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
	if (error)
		goto out_rlist_free;

1362 1363
	error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
				 RES_STATFS + RES_QUOTA, blks);
D
David Teigland 已提交
1364 1365 1366
	if (error)
		goto out_gunlock;

1367
	gfs2_trans_add_bh(ip->i_gl, indbh, 1);
D
David Teigland 已提交
1368

A
Al Viro 已提交
1369
	eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
D
David Teigland 已提交
1370 1371 1372 1373
	bstart = 0;
	blen = 0;

	for (; eablk < end; eablk++) {
1374
		u64 bn;
D
David Teigland 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392

		if (!*eablk)
			break;
		bn = be64_to_cpu(*eablk);

		if (bstart + blen == bn)
			blen++;
		else {
			if (bstart)
				gfs2_free_meta(ip, bstart, blen);
			bstart = bn;
			blen = 1;
		}

		*eablk = 0;
		if (!ip->i_di.di_blocks)
			gfs2_consist_inode(ip);
		ip->i_di.di_blocks--;
1393
		gfs2_set_inode_blocks(&ip->i_inode);
D
David Teigland 已提交
1394 1395 1396 1397 1398 1399 1400 1401
	}
	if (bstart)
		gfs2_free_meta(ip, bstart, blen);

	ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (!error) {
1402
		gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1403
		gfs2_dinode_out(ip, dibh->b_data);
D
David Teigland 已提交
1404 1405 1406 1407 1408
		brelse(dibh);
	}

	gfs2_trans_end(sdp);

1409
out_gunlock:
D
David Teigland 已提交
1410
	gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1411
out_rlist_free:
D
David Teigland 已提交
1412
	gfs2_rlist_free(&rlist);
1413
out:
D
David Teigland 已提交
1414 1415 1416 1417 1418 1419
	brelse(indbh);
	return error;
}

static int ea_dealloc_block(struct gfs2_inode *ip)
{
1420
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1421
	struct gfs2_alloc *al = ip->i_alloc;
D
David Teigland 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	struct gfs2_rgrpd *rgd;
	struct buffer_head *dibh;
	int error;

	rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
	if (!rgd) {
		gfs2_consist_inode(ip);
		return -EIO;
	}

	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
				   &al->al_rgd_gh);
	if (error)
		return error;

1437 1438
	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
				 RES_QUOTA, 1);
D
David Teigland 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447
	if (error)
		goto out_gunlock;

	gfs2_free_meta(ip, ip->i_di.di_eattr, 1);

	ip->i_di.di_eattr = 0;
	if (!ip->i_di.di_blocks)
		gfs2_consist_inode(ip);
	ip->i_di.di_blocks--;
1448
	gfs2_set_inode_blocks(&ip->i_inode);
D
David Teigland 已提交
1449 1450 1451

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (!error) {
1452
		gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1453
		gfs2_dinode_out(ip, dibh->b_data);
D
David Teigland 已提交
1454 1455 1456 1457 1458
		brelse(dibh);
	}

	gfs2_trans_end(sdp);

1459
out_gunlock:
D
David Teigland 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	gfs2_glock_dq_uninit(&al->al_rgd_gh);
	return error;
}

/**
 * gfs2_ea_dealloc - deallocate the extended attribute fork
 * @ip: the inode
 *
 * Returns: errno
 */

int gfs2_ea_dealloc(struct gfs2_inode *ip)
{
	struct gfs2_alloc *al;
	int error;

	al = gfs2_alloc_get(ip);

	error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
	if (error)
		goto out_alloc;

1482
	error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
D
David Teigland 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
	if (error)
		goto out_quota;

	error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
	if (error)
		goto out_rindex;

	if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
		error = ea_dealloc_indirect(ip);
		if (error)
			goto out_rindex;
	}

	error = ea_dealloc_block(ip);

1498
out_rindex:
D
David Teigland 已提交
1499
	gfs2_glock_dq_uninit(&al->al_ri_gh);
1500
out_quota:
D
David Teigland 已提交
1501
	gfs2_quota_unhold(ip);
1502
out_alloc:
D
David Teigland 已提交
1503 1504 1505 1506
	gfs2_alloc_put(ip);
	return error;
}