xref: /linux/fs/gfs2/xattr.c (revision 910a00830bc151d6fd8e515c834149080c4c4940)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5  */
6 
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
9 #include <linux/completion.h>
10 #include <linux/buffer_head.h>
11 #include <linux/xattr.h>
12 #include <linux/gfs2_ondisk.h>
13 #include <linux/posix_acl_xattr.h>
14 #include <linux/uaccess.h>
15 
16 #include "gfs2.h"
17 #include "incore.h"
18 #include "acl.h"
19 #include "xattr.h"
20 #include "glock.h"
21 #include "inode.h"
22 #include "meta_io.h"
23 #include "quota.h"
24 #include "rgrp.h"
25 #include "super.h"
26 #include "trans.h"
27 #include "util.h"
28 
29 /*
30  * ea_calc_size - returns the actual number of bytes the request will take up
31  *                (not counting any unstuffed data blocks)
32  *
33  * Returns: 1 if the EA should be stuffed
34  */
35 
36 static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize,
37 			unsigned int *size)
38 {
39 	unsigned int jbsize = sdp->sd_jbsize;
40 
41 	/* Stuffed */
42 	*size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + dsize, 8);
43 
44 	if (*size <= jbsize)
45 		return 1;
46 
47 	/* Unstuffed */
48 	*size = ALIGN(sizeof(struct gfs2_ea_header) + nsize +
49 		      (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8);
50 
51 	return 0;
52 }
53 
54 static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize)
55 {
56 	unsigned int size;
57 
58 	if (dsize > GFS2_EA_MAX_DATA_LEN)
59 		return -ERANGE;
60 
61 	ea_calc_size(sdp, nsize, dsize, &size);
62 
63 	/* This can only happen with 512 byte blocks */
64 	if (size > sdp->sd_jbsize)
65 		return -ERANGE;
66 
67 	return 0;
68 }
69 
70 static bool gfs2_eatype_valid(struct gfs2_sbd *sdp, u8 type)
71 {
72 	switch(sdp->sd_sb.sb_fs_format) {
73 	case GFS2_FS_FORMAT_MAX:
74 		return true;
75 
76 	case GFS2_FS_FORMAT_MIN:
77 		return type <= GFS2_EATYPE_SECURITY;
78 
79 	default:
80 		return false;
81 	}
82 }
83 
84 typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
85 			  struct gfs2_ea_header *ea,
86 			  struct gfs2_ea_header *prev, void *private);
87 
88 static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
89 			ea_call_t ea_call, void *data)
90 {
91 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
92 	struct gfs2_ea_header *ea, *prev = NULL;
93 	int error = 0;
94 
95 	if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
96 		return -EIO;
97 
98 	for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
99 		if (!GFS2_EA_REC_LEN(ea)) {
100 			gfs2_consist_inode(ip);
101 			return -EIO;
102 		}
103 		if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
104 						  bh->b_data + bh->b_size)) {
105 			gfs2_consist_inode(ip);
106 			return -EIO;
107 		}
108 		if (!gfs2_eatype_valid(sdp, ea->ea_type)) {
109 			gfs2_consist_inode(ip);
110 			return -EIO;
111 		}
112 		error = ea_call(ip, bh, ea, prev, data);
113 		if (error)
114 			return error;
115 
116 		if (GFS2_EA_IS_LAST(ea)) {
117 			if ((char *)GFS2_EA2NEXT(ea) !=
118 			    bh->b_data + bh->b_size) {
119 				gfs2_consist_inode(ip);
120 				return -EIO;
121 			}
122 			break;
123 		}
124 	}
125 
126 	return error;
127 }
128 
129 static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
130 {
131 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
132 	struct buffer_head *bh, *eabh;
133 	__be64 *eablk, *end;
134 	int error;
135 
136 	error = gfs2_meta_read(gl, ip->i_eattr, DIO_WAIT, 0, &bh);
137 	if (error)
138 		return error;
139 
140 	if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) {
141 		error = ea_foreach_i(ip, bh, ea_call, data);
142 		goto out;
143 	}
144 
145 	if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
146 		error = -EIO;
147 		goto out;
148 	}
149 
150 	eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
151 	end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
152 
153 	for (; eablk < end; eablk++) {
154 		u64 bn;
155 
156 		if (!*eablk)
157 			break;
158 		bn = be64_to_cpu(*eablk);
159 
160 		error = gfs2_meta_read(gl, bn, DIO_WAIT, 0, &eabh);
161 		if (error)
162 			break;
163 		error = ea_foreach_i(ip, eabh, ea_call, data);
164 		brelse(eabh);
165 		if (error)
166 			break;
167 	}
168 out:
169 	brelse(bh);
170 	return error;
171 }
172 
173 struct ea_find {
174 	int type;
175 	const char *name;
176 	size_t namel;
177 	struct gfs2_ea_location *ef_el;
178 };
179 
180 static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
181 		     struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
182 		     void *private)
183 {
184 	struct ea_find *ef = private;
185 
186 	if (ea->ea_type == GFS2_EATYPE_UNUSED)
187 		return 0;
188 
189 	if (ea->ea_type == ef->type) {
190 		if (ea->ea_name_len == ef->namel &&
191 		    !memcmp(GFS2_EA2NAME(ea), ef->name, ea->ea_name_len)) {
192 			struct gfs2_ea_location *el = ef->ef_el;
193 			get_bh(bh);
194 			el->el_bh = bh;
195 			el->el_ea = ea;
196 			el->el_prev = prev;
197 			return 1;
198 		}
199 	}
200 
201 	return 0;
202 }
203 
204 static int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name,
205 			struct gfs2_ea_location *el)
206 {
207 	struct ea_find ef;
208 	int error;
209 
210 	ef.type = type;
211 	ef.name = name;
212 	ef.namel = strlen(name);
213 	ef.ef_el = el;
214 
215 	memset(el, 0, sizeof(struct gfs2_ea_location));
216 
217 	error = ea_foreach(ip, ea_find_i, &ef);
218 	if (error > 0)
219 		return 0;
220 
221 	return error;
222 }
223 
224 /*
225  * ea_dealloc_unstuffed
226  *
227  * Take advantage of the fact that all unstuffed blocks are
228  * allocated from the same RG.  But watch, this may not always
229  * be true.
230  *
231  * Returns: errno
232  */
233 
234 static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
235 				struct gfs2_ea_header *ea,
236 				struct gfs2_ea_header *prev, void *private)
237 {
238 	int *leave = private;
239 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
240 	struct gfs2_rgrpd *rgd;
241 	struct gfs2_holder rg_gh;
242 	__be64 *dataptrs;
243 	u64 bn = 0;
244 	u64 bstart = 0;
245 	unsigned int blen = 0;
246 	unsigned int blks = 0;
247 	unsigned int x;
248 	int error;
249 
250 	error = gfs2_rindex_update(sdp);
251 	if (error)
252 		return error;
253 
254 	if (GFS2_EA_IS_STUFFED(ea))
255 		return 0;
256 
257 	dataptrs = GFS2_EA2DATAPTRS(ea);
258 	for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
259 		if (*dataptrs) {
260 			blks++;
261 			bn = be64_to_cpu(*dataptrs);
262 		}
263 	}
264 	if (!blks)
265 		return 0;
266 
267 	rgd = gfs2_blk2rgrpd(sdp, bn, 1);
268 	if (!rgd) {
269 		gfs2_consist_inode(ip);
270 		return -EIO;
271 	}
272 
273 	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
274 				   LM_FLAG_NODE_SCOPE, &rg_gh);
275 	if (error)
276 		return error;
277 
278 	error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
279 				 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
280 	if (error)
281 		goto out_gunlock;
282 
283 	gfs2_trans_add_meta(gfs2_inode_glock(&ip->i_inode), bh);
284 
285 	dataptrs = GFS2_EA2DATAPTRS(ea);
286 	for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
287 		if (!*dataptrs)
288 			break;
289 		bn = be64_to_cpu(*dataptrs);
290 
291 		if (bstart + blen == bn)
292 			blen++;
293 		else {
294 			if (bstart)
295 				gfs2_free_meta(ip, rgd, bstart, blen);
296 			bstart = bn;
297 			blen = 1;
298 		}
299 
300 		*dataptrs = 0;
301 		gfs2_add_inode_blocks(&ip->i_inode, -1);
302 	}
303 	if (bstart)
304 		gfs2_free_meta(ip, rgd, bstart, blen);
305 
306 	if (prev && !leave) {
307 		u32 len;
308 
309 		len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
310 		prev->ea_rec_len = cpu_to_be32(len);
311 
312 		if (GFS2_EA_IS_LAST(ea))
313 			prev->ea_flags |= GFS2_EAFLAG_LAST;
314 	} else {
315 		ea->ea_type = GFS2_EATYPE_UNUSED;
316 		ea->ea_num_ptrs = 0;
317 	}
318 
319 	inode_set_ctime_current(&ip->i_inode);
320 	__mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
321 
322 	gfs2_trans_end(sdp);
323 
324 out_gunlock:
325 	gfs2_glock_dq_uninit(&rg_gh);
326 	return error;
327 }
328 
329 static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
330 			       struct gfs2_ea_header *ea,
331 			       struct gfs2_ea_header *prev, int leave)
332 {
333 	int error;
334 
335 	error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
336 	if (error)
337 		return error;
338 
339 	error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
340 	if (error)
341 		goto out_alloc;
342 
343 	error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
344 
345 	gfs2_quota_unhold(ip);
346 out_alloc:
347 	return error;
348 }
349 
350 struct ea_list {
351 	struct gfs2_ea_request *ei_er;
352 	unsigned int ei_size;
353 };
354 
355 static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
356 		     struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
357 		     void *private)
358 {
359 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
360 	struct ea_list *ei = private;
361 	struct gfs2_ea_request *er = ei->ei_er;
362 	unsigned int ea_size;
363 	char *prefix;
364 	unsigned int l;
365 
366 	if (ea->ea_type == GFS2_EATYPE_UNUSED)
367 		return 0;
368 
369 	BUG_ON(ea->ea_type > GFS2_EATYPE_SECURITY &&
370 	       sdp->sd_sb.sb_fs_format == GFS2_FS_FORMAT_MIN);
371 	switch (ea->ea_type) {
372 	case GFS2_EATYPE_USR:
373 		prefix = "user.";
374 		l = 5;
375 		break;
376 	case GFS2_EATYPE_SYS:
377 		prefix = "system.";
378 		l = 7;
379 		break;
380 	case GFS2_EATYPE_SECURITY:
381 		prefix = "security.";
382 		l = 9;
383 		break;
384 	case GFS2_EATYPE_TRUSTED:
385 		prefix = "trusted.";
386 		l = 8;
387 		break;
388 	default:
389 		return 0;
390 	}
391 
392 	ea_size = l + ea->ea_name_len + 1;
393 	if (er->er_data_len) {
394 		if (ei->ei_size + ea_size > er->er_data_len)
395 			return -ERANGE;
396 
397 		memcpy(er->er_data + ei->ei_size, prefix, l);
398 		memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
399 		       ea->ea_name_len);
400 		er->er_data[ei->ei_size + ea_size - 1] = 0;
401 	}
402 
403 	ei->ei_size += ea_size;
404 
405 	return 0;
406 }
407 
408 /**
409  * gfs2_listxattr - List gfs2 extended attributes
410  * @dentry: The dentry whose inode we are interested in
411  * @buffer: The buffer to write the results
412  * @size: The size of the buffer
413  *
414  * Returns: actual size of data on success, -errno on error
415  */
416 
417 ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
418 {
419 	struct gfs2_inode *ip = GFS2_I(d_inode(dentry));
420 	struct gfs2_ea_request er;
421 	struct gfs2_holder i_gh;
422 	int error;
423 
424 	memset(&er, 0, sizeof(struct gfs2_ea_request));
425 	if (size) {
426 		er.er_data = buffer;
427 		er.er_data_len = size;
428 	}
429 
430 	error = gfs2_glock_nq_init(gfs2_inode_glock(&ip->i_inode), LM_ST_SHARED,
431 				   LM_FLAG_ANY, &i_gh);
432 	if (error)
433 		return error;
434 
435 	if (ip->i_eattr) {
436 		struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
437 
438 		error = ea_foreach(ip, ea_list_i, &ei);
439 		if (!error)
440 			error = ei.ei_size;
441 	}
442 
443 	gfs2_glock_dq_uninit(&i_gh);
444 
445 	return error;
446 }
447 
448 /**
449  * gfs2_iter_unstuffed - copies the unstuffed xattr data to/from the
450  *                       request buffer
451  * @ip: The GFS2 inode
452  * @ea: The extended attribute header structure
453  * @din: The data to be copied in
454  * @dout: The data to be copied out (one of din,dout will be NULL)
455  *
456  * Returns: errno
457  */
458 
459 static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
460 			       const char *din, char *dout)
461 {
462 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
463 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
464 	struct buffer_head **bh;
465 	unsigned int amount = GFS2_EA_DATA_LEN(ea);
466 	unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
467 	__be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
468 	unsigned int x;
469 	int error = 0;
470 	unsigned char *pos;
471 	unsigned cp_size;
472 
473 	bh = kzalloc_objs(struct buffer_head *, nptrs, GFP_NOFS);
474 	if (!bh)
475 		return -ENOMEM;
476 
477 	for (x = 0; x < nptrs; x++) {
478 		error = gfs2_meta_read(gl, be64_to_cpu(*dataptrs), 0, 0,
479 				       bh + x);
480 		if (error) {
481 			while (x--)
482 				brelse(bh[x]);
483 			goto out;
484 		}
485 		dataptrs++;
486 	}
487 
488 	for (x = 0; x < nptrs; x++) {
489 		error = gfs2_meta_wait(sdp, bh[x]);
490 		if (error) {
491 			for (; x < nptrs; x++)
492 				brelse(bh[x]);
493 			goto out;
494 		}
495 		if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
496 			for (; x < nptrs; x++)
497 				brelse(bh[x]);
498 			error = -EIO;
499 			goto out;
500 		}
501 
502 		pos = bh[x]->b_data + sizeof(struct gfs2_meta_header);
503 		cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize;
504 
505 		if (dout) {
506 			memcpy(dout, pos, cp_size);
507 			dout += sdp->sd_jbsize;
508 		}
509 
510 		if (din) {
511 			gfs2_trans_add_meta(gl, bh[x]);
512 			memcpy(pos, din, cp_size);
513 			din += sdp->sd_jbsize;
514 		}
515 
516 		amount -= sdp->sd_jbsize;
517 		brelse(bh[x]);
518 	}
519 
520 out:
521 	kfree(bh);
522 	return error;
523 }
524 
525 static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
526 			    char *data, size_t size)
527 {
528 	int ret;
529 	size_t len = GFS2_EA_DATA_LEN(el->el_ea);
530 	if (len > size)
531 		return -ERANGE;
532 
533 	if (GFS2_EA_IS_STUFFED(el->el_ea)) {
534 		memcpy(data, GFS2_EA2DATA(el->el_ea), len);
535 		return len;
536 	}
537 	ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data);
538 	if (ret < 0)
539 		return ret;
540 	return len;
541 }
542 
543 int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
544 {
545 	struct gfs2_ea_location el;
546 	int error;
547 	int len;
548 	char *data;
549 
550 	error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
551 	if (error)
552 		return error;
553 	if (!el.el_ea)
554 		goto out;
555 	if (!GFS2_EA_DATA_LEN(el.el_ea))
556 		goto out;
557 
558 	len = GFS2_EA_DATA_LEN(el.el_ea);
559 	data = kmalloc(len, GFP_NOFS);
560 	error = -ENOMEM;
561 	if (data == NULL)
562 		goto out;
563 
564 	error = gfs2_ea_get_copy(ip, &el, data, len);
565 	if (error < 0)
566 		kfree(data);
567 	else
568 		*ppdata = data;
569 out:
570 	brelse(el.el_bh);
571 	return error;
572 }
573 
574 /**
575  * __gfs2_xattr_get - Get a GFS2 extended attribute
576  * @inode: The inode
577  * @name: The name of the extended attribute
578  * @buffer: The buffer to write the result into
579  * @size: The size of the buffer
580  * @type: The type of extended attribute
581  *
582  * Returns: actual size of data on success, -errno on error
583  */
584 static int __gfs2_xattr_get(struct inode *inode, const char *name,
585 			    void *buffer, size_t size, int type)
586 {
587 	struct gfs2_inode *ip = GFS2_I(inode);
588 	struct gfs2_ea_location el;
589 	int error;
590 
591 	if (!ip->i_eattr)
592 		return -ENODATA;
593 	if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
594 		return -EINVAL;
595 
596 	error = gfs2_ea_find(ip, type, name, &el);
597 	if (error)
598 		return error;
599 	if (!el.el_ea)
600 		return -ENODATA;
601 	if (size)
602 		error = gfs2_ea_get_copy(ip, &el, buffer, size);
603 	else
604 		error = GFS2_EA_DATA_LEN(el.el_ea);
605 	brelse(el.el_bh);
606 
607 	return error;
608 }
609 
610 static int gfs2_xattr_get(const struct xattr_handler *handler,
611 			  struct dentry *unused, struct inode *inode,
612 			  const char *name, void *buffer, size_t size)
613 {
614 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
615 	struct gfs2_holder gh;
616 	int ret;
617 
618 	/* During lookup, SELinux calls this function with the glock locked. */
619 
620 	if (!gfs2_glock_is_locked_by_me(gl)) {
621 		ret = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
622 		if (ret)
623 			return ret;
624 	} else {
625 		gfs2_holder_mark_uninitialized(&gh);
626 	}
627 	ret = __gfs2_xattr_get(inode, name, buffer, size, handler->flags);
628 	if (gfs2_holder_initialized(&gh))
629 		gfs2_glock_dq_uninit(&gh);
630 	return ret;
631 }
632 
633 /**
634  * ea_alloc_blk - allocates a new block for extended attributes.
635  * @ip: A pointer to the inode that's getting extended attributes
636  * @bhp: Pointer to pointer to a struct buffer_head
637  *
638  * Returns: errno
639  */
640 
641 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
642 {
643 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
644 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
645 	struct gfs2_ea_header *ea;
646 	unsigned int n = 1;
647 	u64 block;
648 	int error;
649 
650 	error = gfs2_alloc_blocks(ip, &block, &n, 0);
651 	if (error)
652 		return error;
653 	gfs2_trans_remove_revoke(sdp, block, 1);
654 	*bhp = gfs2_meta_new(gl, block);
655 	gfs2_trans_add_meta(gl, *bhp);
656 	gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
657 	gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
658 
659 	ea = GFS2_EA_BH2FIRST(*bhp);
660 	ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
661 	ea->ea_type = GFS2_EATYPE_UNUSED;
662 	ea->ea_flags = GFS2_EAFLAG_LAST;
663 	ea->ea_num_ptrs = 0;
664 
665 	gfs2_add_inode_blocks(&ip->i_inode, 1);
666 
667 	return 0;
668 }
669 
670 /**
671  * ea_write - writes the request info to an ea, creating new blocks if
672  *            necessary
673  * @ip: inode that is being modified
674  * @ea: the location of the new ea in a block
675  * @er: the write request
676  *
677  * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
678  *
679  * returns : errno
680  */
681 
682 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
683 		    struct gfs2_ea_request *er)
684 {
685 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
686 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
687 	int error;
688 
689 	ea->ea_data_len = cpu_to_be32(er->er_data_len);
690 	ea->ea_name_len = er->er_name_len;
691 	ea->ea_type = er->er_type;
692 	ea->__pad = 0;
693 
694 	memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
695 
696 	if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
697 		ea->ea_num_ptrs = 0;
698 		memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
699 	} else {
700 		__be64 *dataptr = GFS2_EA2DATAPTRS(ea);
701 		const char *data = er->er_data;
702 		unsigned int data_len = er->er_data_len;
703 		unsigned int copy;
704 		unsigned int x;
705 
706 		ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
707 		for (x = 0; x < ea->ea_num_ptrs; x++) {
708 			struct buffer_head *bh;
709 			u64 block;
710 			int mh_size = sizeof(struct gfs2_meta_header);
711 			unsigned int n = 1;
712 
713 			error = gfs2_alloc_blocks(ip, &block, &n, 0);
714 			if (error)
715 				return error;
716 			gfs2_trans_remove_revoke(sdp, block, 1);
717 			bh = gfs2_meta_new(gl, block);
718 			gfs2_trans_add_meta(gl, bh);
719 			gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
720 
721 			gfs2_add_inode_blocks(&ip->i_inode, 1);
722 
723 			copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
724 							   data_len;
725 			memcpy(bh->b_data + mh_size, data, copy);
726 			if (copy < sdp->sd_jbsize)
727 				memset(bh->b_data + mh_size + copy, 0,
728 				       sdp->sd_jbsize - copy);
729 
730 			*dataptr++ = cpu_to_be64(bh->b_blocknr);
731 			data += copy;
732 			data_len -= copy;
733 
734 			brelse(bh);
735 		}
736 
737 		gfs2_assert_withdraw(sdp, !data_len);
738 	}
739 
740 	return 0;
741 }
742 
743 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
744 				   struct gfs2_ea_request *er, void *private);
745 
746 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
747 			     unsigned int blks,
748 			     ea_skeleton_call_t skeleton_call, void *private)
749 {
750 	struct gfs2_alloc_parms ap = { .target = blks };
751 	int error;
752 
753 	error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
754 	if (error)
755 		return error;
756 
757 	error = gfs2_quota_lock_check(ip, &ap);
758 	if (error)
759 		return error;
760 
761 	error = gfs2_inplace_reserve(ip, &ap);
762 	if (error)
763 		goto out_gunlock_q;
764 
765 	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
766 				 blks + gfs2_rg_blocks(ip, blks) +
767 				 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
768 	if (error)
769 		goto out_ipres;
770 
771 	error = skeleton_call(ip, er, private);
772 	if (error)
773 		goto out_end_trans;
774 
775 	inode_set_ctime_current(&ip->i_inode);
776 	__mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
777 
778 out_end_trans:
779 	gfs2_trans_end(GFS2_SB(&ip->i_inode));
780 out_ipres:
781 	gfs2_inplace_release(ip);
782 out_gunlock_q:
783 	gfs2_quota_unlock(ip);
784 	return error;
785 }
786 
787 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
788 		     void *private)
789 {
790 	struct buffer_head *bh;
791 	int error;
792 
793 	error = ea_alloc_blk(ip, &bh);
794 	if (error)
795 		return error;
796 
797 	ip->i_eattr = bh->b_blocknr;
798 	error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
799 
800 	brelse(bh);
801 
802 	return error;
803 }
804 
805 /*
806  * ea_init - initializes a new eattr block
807  *
808  * Returns: errno
809  */
810 static int ea_init(struct gfs2_inode *ip, int type, const char *name,
811 		   const void *data, size_t size)
812 {
813 	struct gfs2_ea_request er;
814 	unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
815 	unsigned int blks = 1;
816 
817 	er.er_type = type;
818 	er.er_name = name;
819 	er.er_name_len = strlen(name);
820 	er.er_data = (void *)data;
821 	er.er_data_len = size;
822 
823 	if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
824 		blks += DIV_ROUND_UP(er.er_data_len, jbsize);
825 
826 	return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
827 }
828 
829 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
830 {
831 	u32 ea_size = GFS2_EA_SIZE(ea);
832 	struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
833 				     ea_size);
834 	u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
835 	int last = ea->ea_flags & GFS2_EAFLAG_LAST;
836 
837 	ea->ea_rec_len = cpu_to_be32(ea_size);
838 	ea->ea_flags ^= last;
839 
840 	new->ea_rec_len = cpu_to_be32(new_size);
841 	new->ea_flags = last;
842 
843 	return new;
844 }
845 
846 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
847 				  struct gfs2_ea_location *el)
848 {
849 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
850 	struct gfs2_ea_header *ea = el->el_ea;
851 	struct gfs2_ea_header *prev = el->el_prev;
852 	u32 len;
853 
854 	gfs2_trans_add_meta(gl, el->el_bh);
855 
856 	if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
857 		ea->ea_type = GFS2_EATYPE_UNUSED;
858 		return;
859 	} else if (GFS2_EA2NEXT(prev) != ea) {
860 		prev = GFS2_EA2NEXT(prev);
861 		gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
862 	}
863 
864 	len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
865 	prev->ea_rec_len = cpu_to_be32(len);
866 
867 	if (GFS2_EA_IS_LAST(ea))
868 		prev->ea_flags |= GFS2_EAFLAG_LAST;
869 }
870 
871 struct ea_set {
872 	int ea_split;
873 
874 	struct gfs2_ea_request *es_er;
875 	struct gfs2_ea_location *es_el;
876 
877 	struct buffer_head *es_bh;
878 	struct gfs2_ea_header *es_ea;
879 };
880 
881 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
882 				 struct gfs2_ea_header *ea, struct ea_set *es)
883 {
884 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
885 	struct gfs2_ea_request *er = es->es_er;
886 	int error;
887 
888 	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
889 	if (error)
890 		return error;
891 
892 	gfs2_trans_add_meta(gl, bh);
893 
894 	if (es->ea_split)
895 		ea = ea_split_ea(ea);
896 
897 	ea_write(ip, ea, er);
898 
899 	if (es->es_el)
900 		ea_set_remove_stuffed(ip, es->es_el);
901 
902 	inode_set_ctime_current(&ip->i_inode);
903 	__mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
904 
905 	gfs2_trans_end(GFS2_SB(&ip->i_inode));
906 	return error;
907 }
908 
909 static int ea_set_simple_alloc(struct gfs2_inode *ip,
910 			       struct gfs2_ea_request *er, void *private)
911 {
912 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
913 	struct ea_set *es = private;
914 	struct gfs2_ea_header *ea = es->es_ea;
915 	int error;
916 
917 	gfs2_trans_add_meta(gl, es->es_bh);
918 
919 	if (es->ea_split)
920 		ea = ea_split_ea(ea);
921 
922 	error = ea_write(ip, ea, er);
923 	if (error)
924 		return error;
925 
926 	if (es->es_el)
927 		ea_set_remove_stuffed(ip, es->es_el);
928 
929 	return 0;
930 }
931 
932 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
933 			 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
934 			 void *private)
935 {
936 	struct ea_set *es = private;
937 	unsigned int size;
938 	int stuffed;
939 	int error;
940 
941 	stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
942 			       es->es_er->er_data_len, &size);
943 
944 	if (ea->ea_type == GFS2_EATYPE_UNUSED) {
945 		if (GFS2_EA_REC_LEN(ea) < size)
946 			return 0;
947 		if (!GFS2_EA_IS_STUFFED(ea)) {
948 			error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
949 			if (error)
950 				return error;
951 		}
952 		es->ea_split = 0;
953 	} else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
954 		es->ea_split = 1;
955 	else
956 		return 0;
957 
958 	if (stuffed) {
959 		error = ea_set_simple_noalloc(ip, bh, ea, es);
960 		if (error)
961 			return error;
962 	} else {
963 		unsigned int blks;
964 
965 		es->es_bh = bh;
966 		es->es_ea = ea;
967 		blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
968 					GFS2_SB(&ip->i_inode)->sd_jbsize);
969 
970 		error = ea_alloc_skeleton(ip, es->es_er, blks,
971 					  ea_set_simple_alloc, es);
972 		if (error)
973 			return error;
974 	}
975 
976 	return 1;
977 }
978 
979 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
980 			void *private)
981 {
982 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
983 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
984 	struct buffer_head *indbh, *newbh;
985 	__be64 *eablk;
986 	int error;
987 	int mh_size = sizeof(struct gfs2_meta_header);
988 
989 	if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
990 		__be64 *end;
991 
992 		error = gfs2_meta_read(gl, ip->i_eattr, DIO_WAIT, 0,
993 				       &indbh);
994 		if (error)
995 			return error;
996 
997 		if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
998 			error = -EIO;
999 			goto out;
1000 		}
1001 
1002 		eablk = (__be64 *)(indbh->b_data + mh_size);
1003 		end = eablk + sdp->sd_inptrs;
1004 
1005 		for (; eablk < end; eablk++)
1006 			if (!*eablk)
1007 				break;
1008 
1009 		if (eablk == end) {
1010 			error = -ENOSPC;
1011 			goto out;
1012 		}
1013 
1014 		gfs2_trans_add_meta(gl, indbh);
1015 	} else {
1016 		u64 blk;
1017 		unsigned int n = 1;
1018 		error = gfs2_alloc_blocks(ip, &blk, &n, 0);
1019 		if (error)
1020 			return error;
1021 		gfs2_trans_remove_revoke(sdp, blk, 1);
1022 		indbh = gfs2_meta_new(gl, blk);
1023 		gfs2_trans_add_meta(gl, indbh);
1024 		gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
1025 		gfs2_buffer_clear_tail(indbh, mh_size);
1026 
1027 		eablk = (__be64 *)(indbh->b_data + mh_size);
1028 		*eablk = cpu_to_be64(ip->i_eattr);
1029 		ip->i_eattr = blk;
1030 		ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
1031 		gfs2_add_inode_blocks(&ip->i_inode, 1);
1032 
1033 		eablk++;
1034 	}
1035 
1036 	error = ea_alloc_blk(ip, &newbh);
1037 	if (error)
1038 		goto out;
1039 
1040 	*eablk = cpu_to_be64((u64)newbh->b_blocknr);
1041 	error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1042 	brelse(newbh);
1043 	if (error)
1044 		goto out;
1045 
1046 	if (private)
1047 		ea_set_remove_stuffed(ip, private);
1048 
1049 out:
1050 	brelse(indbh);
1051 	return error;
1052 }
1053 
1054 static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
1055 		    const void *value, size_t size, struct gfs2_ea_location *el)
1056 {
1057 	struct gfs2_ea_request er;
1058 	struct ea_set es;
1059 	unsigned int blks = 2;
1060 	int error;
1061 
1062 	er.er_type = type;
1063 	er.er_name = name;
1064 	er.er_data = (void *)value;
1065 	er.er_name_len = strlen(name);
1066 	er.er_data_len = size;
1067 
1068 	memset(&es, 0, sizeof(struct ea_set));
1069 	es.es_er = &er;
1070 	es.es_el = el;
1071 
1072 	error = ea_foreach(ip, ea_set_simple, &es);
1073 	if (error > 0)
1074 		return 0;
1075 	if (error)
1076 		return error;
1077 
1078 	if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
1079 		blks++;
1080 	if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1081 		blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
1082 
1083 	return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
1084 }
1085 
1086 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1087 				   struct gfs2_ea_location *el)
1088 {
1089 	if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1090 		el->el_prev = GFS2_EA2NEXT(el->el_prev);
1091 		gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
1092 				     GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1093 	}
1094 
1095 	return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
1096 }
1097 
1098 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1099 {
1100 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
1101 	struct gfs2_ea_header *ea = el->el_ea;
1102 	struct gfs2_ea_header *prev = el->el_prev;
1103 	int error;
1104 
1105 	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1106 	if (error)
1107 		return error;
1108 
1109 	gfs2_trans_add_meta(gl, el->el_bh);
1110 
1111 	if (prev) {
1112 		u32 len;
1113 
1114 		len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1115 		prev->ea_rec_len = cpu_to_be32(len);
1116 
1117 		if (GFS2_EA_IS_LAST(ea))
1118 			prev->ea_flags |= GFS2_EAFLAG_LAST;
1119 	} else {
1120 		ea->ea_type = GFS2_EATYPE_UNUSED;
1121 	}
1122 
1123 	inode_set_ctime_current(&ip->i_inode);
1124 	__mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
1125 
1126 	gfs2_trans_end(GFS2_SB(&ip->i_inode));
1127 
1128 	return error;
1129 }
1130 
1131 /**
1132  * gfs2_xattr_remove - Remove a GFS2 extended attribute
1133  * @ip: The inode
1134  * @type: The type of the extended attribute
1135  * @name: The name of the extended attribute
1136  *
1137  * This is not called directly by the VFS since we use the (common)
1138  * scheme of making a "set with NULL data" mean a remove request. Note
1139  * that this is different from a set with zero length data.
1140  *
1141  * Returns: 0, or errno on failure
1142  */
1143 
1144 static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
1145 {
1146 	struct gfs2_ea_location el;
1147 	int error;
1148 
1149 	if (!ip->i_eattr)
1150 		return -ENODATA;
1151 
1152 	error = gfs2_ea_find(ip, type, name, &el);
1153 	if (error)
1154 		return error;
1155 	if (!el.el_ea)
1156 		return -ENODATA;
1157 
1158 	if (GFS2_EA_IS_STUFFED(el.el_ea))
1159 		error = ea_remove_stuffed(ip, &el);
1160 	else
1161 		error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
1162 
1163 	brelse(el.el_bh);
1164 
1165 	return error;
1166 }
1167 
1168 /**
1169  * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
1170  * @inode: The inode
1171  * @name: The name of the extended attribute
1172  * @value: The value of the extended attribute (NULL for remove)
1173  * @size: The size of the @value argument
1174  * @flags: Create or Replace
1175  * @type: The type of the extended attribute
1176  *
1177  * See gfs2_xattr_remove() for details of the removal of xattrs.
1178  *
1179  * Returns: 0 or errno on failure
1180  */
1181 
1182 int __gfs2_xattr_set(struct inode *inode, const char *name,
1183 		   const void *value, size_t size, int flags, int type)
1184 {
1185 	struct gfs2_inode *ip = GFS2_I(inode);
1186 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1187 	struct gfs2_ea_location el;
1188 	unsigned int namel = strlen(name);
1189 	int error;
1190 
1191 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1192 		return -EPERM;
1193 	if (namel > GFS2_EA_MAX_NAME_LEN)
1194 		return -ERANGE;
1195 
1196 	if (value == NULL) {
1197 		error = gfs2_xattr_remove(ip, type, name);
1198 		if (error == -ENODATA && !(flags & XATTR_REPLACE))
1199 			error = 0;
1200 		return error;
1201 	}
1202 
1203 	if (ea_check_size(sdp, namel, size))
1204 		return -ERANGE;
1205 
1206 	if (!ip->i_eattr) {
1207 		if (flags & XATTR_REPLACE)
1208 			return -ENODATA;
1209 		return ea_init(ip, type, name, value, size);
1210 	}
1211 
1212 	error = gfs2_ea_find(ip, type, name, &el);
1213 	if (error)
1214 		return error;
1215 
1216 	if (el.el_ea) {
1217 		if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
1218 			brelse(el.el_bh);
1219 			return -EPERM;
1220 		}
1221 
1222 		error = -EEXIST;
1223 		if (!(flags & XATTR_CREATE)) {
1224 			int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1225 			error = ea_set_i(ip, type, name, value, size, &el);
1226 			if (!error && unstuffed)
1227 				ea_set_remove_unstuffed(ip, &el);
1228 		}
1229 
1230 		brelse(el.el_bh);
1231 		return error;
1232 	}
1233 
1234 	error = -ENODATA;
1235 	if (!(flags & XATTR_REPLACE))
1236 		error = ea_set_i(ip, type, name, value, size, NULL);
1237 
1238 	return error;
1239 }
1240 
1241 static int gfs2_xattr_set(const struct xattr_handler *handler,
1242 			  const struct mnt_idmap *idmap,
1243 			  struct dentry *unused, struct inode *inode,
1244 			  const char *name, const void *value,
1245 			  size_t size, int flags)
1246 {
1247 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
1248 	struct gfs2_inode *ip = GFS2_I(inode);
1249 	struct gfs2_holder gh;
1250 	int ret;
1251 
1252 	ret = gfs2_qa_get(ip);
1253 	if (ret)
1254 		return ret;
1255 
1256 	/* May be called from gfs_setattr with the glock locked. */
1257 
1258 	if (!gfs2_glock_is_locked_by_me(gl)) {
1259 		ret = gfs2_glock_nq_init(gl, LM_ST_EXCLUSIVE, 0, &gh);
1260 		if (ret)
1261 			goto out;
1262 	} else {
1263 		if (WARN_ON_ONCE(gl->gl_state != LM_ST_EXCLUSIVE)) {
1264 			ret = -EIO;
1265 			goto out;
1266 		}
1267 		gfs2_holder_mark_uninitialized(&gh);
1268 	}
1269 	ret = __gfs2_xattr_set(inode, name, value, size, flags, handler->flags);
1270 	if (gfs2_holder_initialized(&gh))
1271 		gfs2_glock_dq_uninit(&gh);
1272 out:
1273 	gfs2_qa_put(ip);
1274 	return ret;
1275 }
1276 
1277 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1278 {
1279 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
1280 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1281 	struct gfs2_rgrp_list rlist;
1282 	struct gfs2_rgrpd *rgd;
1283 	struct buffer_head *indbh, *dibh;
1284 	__be64 *eablk, *end;
1285 	unsigned int rg_blocks = 0;
1286 	u64 bstart = 0;
1287 	unsigned int blen = 0;
1288 	unsigned int blks = 0;
1289 	unsigned int x;
1290 	int error;
1291 
1292 	error = gfs2_rindex_update(sdp);
1293 	if (error)
1294 		return error;
1295 
1296 	memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1297 
1298 	error = gfs2_meta_read(gl, ip->i_eattr, DIO_WAIT, 0, &indbh);
1299 	if (error)
1300 		return error;
1301 
1302 	if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1303 		error = -EIO;
1304 		goto out;
1305 	}
1306 
1307 	eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1308 	end = eablk + sdp->sd_inptrs;
1309 
1310 	for (; eablk < end; eablk++) {
1311 		u64 bn;
1312 
1313 		if (!*eablk)
1314 			break;
1315 		bn = be64_to_cpu(*eablk);
1316 
1317 		if (bstart + blen == bn)
1318 			blen++;
1319 		else {
1320 			if (bstart)
1321 				gfs2_rlist_add(ip, &rlist, bstart);
1322 			bstart = bn;
1323 			blen = 1;
1324 		}
1325 		blks++;
1326 	}
1327 	if (bstart)
1328 		gfs2_rlist_add(ip, &rlist, bstart);
1329 	else
1330 		goto out;
1331 
1332 	gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE);
1333 
1334 	for (x = 0; x < rlist.rl_rgrps; x++) {
1335 		rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
1336 		rg_blocks += rgd->rd_length;
1337 	}
1338 
1339 	error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1340 	if (error)
1341 		goto out_rlist_free;
1342 
1343 	error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1344 				 RES_STATFS + RES_QUOTA, blks);
1345 	if (error)
1346 		goto out_gunlock;
1347 
1348 	gfs2_trans_add_meta(gl, indbh);
1349 
1350 	eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1351 	bstart = 0;
1352 	rgd = NULL;
1353 	blen = 0;
1354 
1355 	for (; eablk < end; eablk++) {
1356 		u64 bn;
1357 
1358 		if (!*eablk)
1359 			break;
1360 		bn = be64_to_cpu(*eablk);
1361 
1362 		if (bstart + blen == bn)
1363 			blen++;
1364 		else {
1365 			if (bstart)
1366 				gfs2_free_meta(ip, rgd, bstart, blen);
1367 			bstart = bn;
1368 			rgd = gfs2_blk2rgrpd(sdp, bstart, true);
1369 			blen = 1;
1370 		}
1371 
1372 		*eablk = 0;
1373 		gfs2_add_inode_blocks(&ip->i_inode, -1);
1374 	}
1375 	if (bstart)
1376 		gfs2_free_meta(ip, rgd, bstart, blen);
1377 
1378 	ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
1379 
1380 	error = gfs2_meta_inode_buffer(ip, &dibh);
1381 	if (!error) {
1382 		gfs2_trans_add_meta(gl, dibh);
1383 		gfs2_dinode_out(ip, dibh->b_data);
1384 		brelse(dibh);
1385 	}
1386 
1387 	gfs2_trans_end(sdp);
1388 
1389 out_gunlock:
1390 	gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1391 out_rlist_free:
1392 	gfs2_rlist_free(&rlist);
1393 out:
1394 	brelse(indbh);
1395 	return error;
1396 }
1397 
1398 static int ea_dealloc_block(struct gfs2_inode *ip, bool initialized)
1399 {
1400 	struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode);
1401 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1402 	struct gfs2_rgrpd *rgd;
1403 	struct buffer_head *dibh;
1404 	struct gfs2_holder gh;
1405 	int error;
1406 
1407 	error = gfs2_rindex_update(sdp);
1408 	if (error)
1409 		return error;
1410 
1411 	rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
1412 	if (!rgd) {
1413 		gfs2_consist_inode(ip);
1414 		return -EIO;
1415 	}
1416 
1417 	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1418 				   LM_FLAG_NODE_SCOPE, &gh);
1419 	if (error)
1420 		return error;
1421 
1422 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1423 				 RES_QUOTA, 1);
1424 	if (error)
1425 		goto out_gunlock;
1426 
1427 	gfs2_free_meta(ip, rgd, ip->i_eattr, 1);
1428 
1429 	ip->i_eattr = 0;
1430 	gfs2_add_inode_blocks(&ip->i_inode, -1);
1431 
1432 	if (initialized) {
1433 		error = gfs2_meta_inode_buffer(ip, &dibh);
1434 		if (!error) {
1435 			gfs2_trans_add_meta(gl, dibh);
1436 			gfs2_dinode_out(ip, dibh->b_data);
1437 			brelse(dibh);
1438 		}
1439 	}
1440 
1441 	gfs2_trans_end(sdp);
1442 
1443 out_gunlock:
1444 	gfs2_glock_dq_uninit(&gh);
1445 	return error;
1446 }
1447 
1448 /**
1449  * gfs2_ea_dealloc - deallocate the extended attribute fork
1450  * @ip: the inode
1451  * @initialized: xattrs have been initialized
1452  *
1453  * Returns: errno
1454  */
1455 
1456 int gfs2_ea_dealloc(struct gfs2_inode *ip, bool initialized)
1457 {
1458 	int error;
1459 
1460 	error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
1461 	if (error)
1462 		return error;
1463 
1464 	error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1465 	if (error)
1466 		return error;
1467 
1468 	if (initialized) {
1469 		error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1470 		if (error)
1471 			goto out_quota;
1472 
1473 		if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
1474 			error = ea_dealloc_indirect(ip);
1475 			if (error)
1476 				goto out_quota;
1477 		}
1478 	}
1479 
1480 	error = ea_dealloc_block(ip, initialized);
1481 
1482 out_quota:
1483 	gfs2_quota_unhold(ip);
1484 	return error;
1485 }
1486 
1487 static const struct xattr_handler gfs2_xattr_user_handler = {
1488 	.prefix = XATTR_USER_PREFIX,
1489 	.flags  = GFS2_EATYPE_USR,
1490 	.get    = gfs2_xattr_get,
1491 	.set    = gfs2_xattr_set,
1492 };
1493 
1494 static const struct xattr_handler gfs2_xattr_security_handler = {
1495 	.prefix = XATTR_SECURITY_PREFIX,
1496 	.flags  = GFS2_EATYPE_SECURITY,
1497 	.get    = gfs2_xattr_get,
1498 	.set    = gfs2_xattr_set,
1499 };
1500 
1501 static bool
1502 gfs2_xattr_trusted_list(struct dentry *dentry)
1503 {
1504 	return capable(CAP_SYS_ADMIN);
1505 }
1506 
1507 static const struct xattr_handler gfs2_xattr_trusted_handler = {
1508 	.prefix = XATTR_TRUSTED_PREFIX,
1509 	.flags  = GFS2_EATYPE_TRUSTED,
1510 	.list	= gfs2_xattr_trusted_list,
1511 	.get    = gfs2_xattr_get,
1512 	.set    = gfs2_xattr_set,
1513 };
1514 
1515 const struct xattr_handler * const gfs2_xattr_handlers_max[] = {
1516 	/* GFS2_FS_FORMAT_MAX */
1517 	&gfs2_xattr_trusted_handler,
1518 
1519 	/* GFS2_FS_FORMAT_MIN */
1520 	&gfs2_xattr_user_handler,
1521 	&gfs2_xattr_security_handler,
1522 	NULL,
1523 };
1524 
1525 const struct xattr_handler * const *gfs2_xattr_handlers_min = gfs2_xattr_handlers_max + 1;
1526