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