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