• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ea_calc_size(struct gfs2_sbd * sdp,unsigned int nsize,size_t dsize,unsigned int * size)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 
ea_check_size(struct gfs2_sbd * sdp,unsigned int nsize,size_t dsize)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 
ea_foreach_i(struct gfs2_inode * ip,struct buffer_head * bh,ea_call_t ea_call,void * data)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 
ea_foreach(struct gfs2_inode * ip,ea_call_t ea_call,void * data)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 
ea_find_i(struct gfs2_inode * ip,struct buffer_head * bh,struct gfs2_ea_header * ea,struct gfs2_ea_header * prev,void * private)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 
gfs2_ea_find(struct gfs2_inode * ip,int type,const char * name,struct gfs2_ea_location * el)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 
ea_dealloc_unstuffed(struct gfs2_inode * ip,struct buffer_head * bh,struct gfs2_ea_header * ea,struct gfs2_ea_header * prev,void * private)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, 0, &rg_gh);
263 	if (error)
264 		return error;
265 
266 	error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
267 				 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
268 	if (error)
269 		goto out_gunlock;
270 
271 	gfs2_trans_add_meta(ip->i_gl, bh);
272 
273 	dataptrs = GFS2_EA2DATAPTRS(ea);
274 	for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
275 		if (!*dataptrs)
276 			break;
277 		bn = be64_to_cpu(*dataptrs);
278 
279 		if (bstart + blen == bn)
280 			blen++;
281 		else {
282 			if (bstart)
283 				gfs2_free_meta(ip, rgd, bstart, blen);
284 			bstart = bn;
285 			blen = 1;
286 		}
287 
288 		*dataptrs = 0;
289 		gfs2_add_inode_blocks(&ip->i_inode, -1);
290 	}
291 	if (bstart)
292 		gfs2_free_meta(ip, rgd, bstart, blen);
293 
294 	if (prev && !leave) {
295 		u32 len;
296 
297 		len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
298 		prev->ea_rec_len = cpu_to_be32(len);
299 
300 		if (GFS2_EA_IS_LAST(ea))
301 			prev->ea_flags |= GFS2_EAFLAG_LAST;
302 	} else {
303 		ea->ea_type = GFS2_EATYPE_UNUSED;
304 		ea->ea_num_ptrs = 0;
305 	}
306 
307 	ip->i_inode.i_ctime = current_time(&ip->i_inode);
308 	__mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
309 
310 	gfs2_trans_end(sdp);
311 
312 out_gunlock:
313 	gfs2_glock_dq_uninit(&rg_gh);
314 	return error;
315 }
316 
ea_remove_unstuffed(struct gfs2_inode * ip,struct buffer_head * bh,struct gfs2_ea_header * ea,struct gfs2_ea_header * prev,int leave)317 static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
318 			       struct gfs2_ea_header *ea,
319 			       struct gfs2_ea_header *prev, int leave)
320 {
321 	int error;
322 
323 	error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
324 	if (error)
325 		return error;
326 
327 	error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
328 	if (error)
329 		goto out_alloc;
330 
331 	error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
332 
333 	gfs2_quota_unhold(ip);
334 out_alloc:
335 	return error;
336 }
337 
338 struct ea_list {
339 	struct gfs2_ea_request *ei_er;
340 	unsigned int ei_size;
341 };
342 
ea_list_i(struct gfs2_inode * ip,struct buffer_head * bh,struct gfs2_ea_header * ea,struct gfs2_ea_header * prev,void * private)343 static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
344 		     struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
345 		     void *private)
346 {
347 	struct ea_list *ei = private;
348 	struct gfs2_ea_request *er = ei->ei_er;
349 	unsigned int ea_size;
350 	char *prefix;
351 	unsigned int l;
352 
353 	if (ea->ea_type == GFS2_EATYPE_UNUSED)
354 		return 0;
355 
356 	switch (ea->ea_type) {
357 	case GFS2_EATYPE_USR:
358 		prefix = "user.";
359 		l = 5;
360 		break;
361 	case GFS2_EATYPE_SYS:
362 		prefix = "system.";
363 		l = 7;
364 		break;
365 	case GFS2_EATYPE_SECURITY:
366 		prefix = "security.";
367 		l = 9;
368 		break;
369 	default:
370 		BUG();
371 	}
372 
373 	ea_size = l + ea->ea_name_len + 1;
374 	if (er->er_data_len) {
375 		if (ei->ei_size + ea_size > er->er_data_len)
376 			return -ERANGE;
377 
378 		memcpy(er->er_data + ei->ei_size, prefix, l);
379 		memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
380 		       ea->ea_name_len);
381 		er->er_data[ei->ei_size + ea_size - 1] = 0;
382 	}
383 
384 	ei->ei_size += ea_size;
385 
386 	return 0;
387 }
388 
389 /**
390  * gfs2_listxattr - List gfs2 extended attributes
391  * @dentry: The dentry whose inode we are interested in
392  * @buffer: The buffer to write the results
393  * @size: The size of the buffer
394  *
395  * Returns: actual size of data on success, -errno on error
396  */
397 
gfs2_listxattr(struct dentry * dentry,char * buffer,size_t size)398 ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
399 {
400 	struct gfs2_inode *ip = GFS2_I(d_inode(dentry));
401 	struct gfs2_ea_request er;
402 	struct gfs2_holder i_gh;
403 	int error;
404 
405 	memset(&er, 0, sizeof(struct gfs2_ea_request));
406 	if (size) {
407 		er.er_data = buffer;
408 		er.er_data_len = size;
409 	}
410 
411 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
412 	if (error)
413 		return error;
414 
415 	if (ip->i_eattr) {
416 		struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
417 
418 		error = ea_foreach(ip, ea_list_i, &ei);
419 		if (!error)
420 			error = ei.ei_size;
421 	}
422 
423 	gfs2_glock_dq_uninit(&i_gh);
424 
425 	return error;
426 }
427 
428 /**
429  * ea_iter_unstuffed - copies the unstuffed xattr data to/from the
430  *                     request buffer
431  * @ip: The GFS2 inode
432  * @ea: The extended attribute header structure
433  * @din: The data to be copied in
434  * @dout: The data to be copied out (one of din,dout will be NULL)
435  *
436  * Returns: errno
437  */
438 
gfs2_iter_unstuffed(struct gfs2_inode * ip,struct gfs2_ea_header * ea,const char * din,char * dout)439 static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
440 			       const char *din, char *dout)
441 {
442 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
443 	struct buffer_head **bh;
444 	unsigned int amount = GFS2_EA_DATA_LEN(ea);
445 	unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
446 	__be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
447 	unsigned int x;
448 	int error = 0;
449 	unsigned char *pos;
450 	unsigned cp_size;
451 
452 	bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
453 	if (!bh)
454 		return -ENOMEM;
455 
456 	for (x = 0; x < nptrs; x++) {
457 		error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, 0,
458 				       bh + x);
459 		if (error) {
460 			while (x--)
461 				brelse(bh[x]);
462 			goto out;
463 		}
464 		dataptrs++;
465 	}
466 
467 	for (x = 0; x < nptrs; x++) {
468 		error = gfs2_meta_wait(sdp, bh[x]);
469 		if (error) {
470 			for (; x < nptrs; x++)
471 				brelse(bh[x]);
472 			goto out;
473 		}
474 		if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
475 			for (; x < nptrs; x++)
476 				brelse(bh[x]);
477 			error = -EIO;
478 			goto out;
479 		}
480 
481 		pos = bh[x]->b_data + sizeof(struct gfs2_meta_header);
482 		cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize;
483 
484 		if (dout) {
485 			memcpy(dout, pos, cp_size);
486 			dout += sdp->sd_jbsize;
487 		}
488 
489 		if (din) {
490 			gfs2_trans_add_meta(ip->i_gl, bh[x]);
491 			memcpy(pos, din, cp_size);
492 			din += sdp->sd_jbsize;
493 		}
494 
495 		amount -= sdp->sd_jbsize;
496 		brelse(bh[x]);
497 	}
498 
499 out:
500 	kfree(bh);
501 	return error;
502 }
503 
gfs2_ea_get_copy(struct gfs2_inode * ip,struct gfs2_ea_location * el,char * data,size_t size)504 static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
505 			    char *data, size_t size)
506 {
507 	int ret;
508 	size_t len = GFS2_EA_DATA_LEN(el->el_ea);
509 	if (len > size)
510 		return -ERANGE;
511 
512 	if (GFS2_EA_IS_STUFFED(el->el_ea)) {
513 		memcpy(data, GFS2_EA2DATA(el->el_ea), len);
514 		return len;
515 	}
516 	ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data);
517 	if (ret < 0)
518 		return ret;
519 	return len;
520 }
521 
gfs2_xattr_acl_get(struct gfs2_inode * ip,const char * name,char ** ppdata)522 int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
523 {
524 	struct gfs2_ea_location el;
525 	int error;
526 	int len;
527 	char *data;
528 
529 	error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
530 	if (error)
531 		return error;
532 	if (!el.el_ea)
533 		goto out;
534 	if (!GFS2_EA_DATA_LEN(el.el_ea))
535 		goto out;
536 
537 	len = GFS2_EA_DATA_LEN(el.el_ea);
538 	data = kmalloc(len, GFP_NOFS);
539 	error = -ENOMEM;
540 	if (data == NULL)
541 		goto out;
542 
543 	error = gfs2_ea_get_copy(ip, &el, data, len);
544 	if (error < 0)
545 		kfree(data);
546 	else
547 		*ppdata = data;
548 out:
549 	brelse(el.el_bh);
550 	return error;
551 }
552 
553 /**
554  * gfs2_xattr_get - Get a GFS2 extended attribute
555  * @inode: The inode
556  * @name: The name of the extended attribute
557  * @buffer: The buffer to write the result into
558  * @size: The size of the buffer
559  * @type: The type of extended attribute
560  *
561  * Returns: actual size of data on success, -errno on error
562  */
__gfs2_xattr_get(struct inode * inode,const char * name,void * buffer,size_t size,int type)563 static int __gfs2_xattr_get(struct inode *inode, const char *name,
564 			    void *buffer, size_t size, int type)
565 {
566 	struct gfs2_inode *ip = GFS2_I(inode);
567 	struct gfs2_ea_location el;
568 	int error;
569 
570 	if (!ip->i_eattr)
571 		return -ENODATA;
572 	if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
573 		return -EINVAL;
574 
575 	error = gfs2_ea_find(ip, type, name, &el);
576 	if (error)
577 		return error;
578 	if (!el.el_ea)
579 		return -ENODATA;
580 	if (size)
581 		error = gfs2_ea_get_copy(ip, &el, buffer, size);
582 	else
583 		error = GFS2_EA_DATA_LEN(el.el_ea);
584 	brelse(el.el_bh);
585 
586 	return error;
587 }
588 
gfs2_xattr_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size,int flags)589 static int gfs2_xattr_get(const struct xattr_handler *handler,
590 			  struct dentry *unused, struct inode *inode,
591 			  const char *name, void *buffer, size_t size,
592 			  int flags)
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 
ea_alloc_blk(struct gfs2_inode * ip,struct buffer_head ** bhp)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 
ea_write(struct gfs2_inode * ip,struct gfs2_ea_header * ea,struct gfs2_ea_request * er)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 
ea_alloc_skeleton(struct gfs2_inode * ip,struct gfs2_ea_request * er,unsigned int blks,ea_skeleton_call_t skeleton_call,void * private)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 
ea_init_i(struct gfs2_inode * ip,struct gfs2_ea_request * er,void * private)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 
ea_init(struct gfs2_inode * ip,int type,const char * name,const void * data,size_t size)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 
ea_split_ea(struct gfs2_ea_header * ea)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 
ea_set_remove_stuffed(struct gfs2_inode * ip,struct gfs2_ea_location * el)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 
ea_set_simple_noalloc(struct gfs2_inode * ip,struct buffer_head * bh,struct gfs2_ea_header * ea,struct ea_set * es)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 
ea_set_simple_alloc(struct gfs2_inode * ip,struct gfs2_ea_request * er,void * private)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 
ea_set_simple(struct gfs2_inode * ip,struct buffer_head * bh,struct gfs2_ea_header * ea,struct gfs2_ea_header * prev,void * private)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 
ea_set_block(struct gfs2_inode * ip,struct gfs2_ea_request * er,void * private)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 
ea_set_i(struct gfs2_inode * ip,int type,const char * name,const void * value,size_t size,struct gfs2_ea_location * el)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 
ea_set_remove_unstuffed(struct gfs2_inode * ip,struct gfs2_ea_location * el)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 
ea_remove_stuffed(struct gfs2_inode * ip,struct gfs2_ea_location * el)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 
gfs2_xattr_remove(struct gfs2_inode * ip,int type,const char * name)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 
__gfs2_xattr_set(struct inode * inode,const char * name,const void * value,size_t size,int flags,int type)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 
gfs2_xattr_set(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)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 
ea_dealloc_indirect(struct gfs2_inode * ip)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 
ea_dealloc_block(struct gfs2_inode * ip)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, 0, &gh);
1390 	if (error)
1391 		return error;
1392 
1393 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1394 				 RES_QUOTA, 1);
1395 	if (error)
1396 		goto out_gunlock;
1397 
1398 	gfs2_free_meta(ip, rgd, ip->i_eattr, 1);
1399 
1400 	ip->i_eattr = 0;
1401 	gfs2_add_inode_blocks(&ip->i_inode, -1);
1402 
1403 	error = gfs2_meta_inode_buffer(ip, &dibh);
1404 	if (!error) {
1405 		gfs2_trans_add_meta(ip->i_gl, dibh);
1406 		gfs2_dinode_out(ip, dibh->b_data);
1407 		brelse(dibh);
1408 	}
1409 
1410 	gfs2_trans_end(sdp);
1411 
1412 out_gunlock:
1413 	gfs2_glock_dq_uninit(&gh);
1414 	return error;
1415 }
1416 
1417 /**
1418  * gfs2_ea_dealloc - deallocate the extended attribute fork
1419  * @ip: the inode
1420  *
1421  * Returns: errno
1422  */
1423 
gfs2_ea_dealloc(struct gfs2_inode * ip)1424 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1425 {
1426 	int error;
1427 
1428 	error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
1429 	if (error)
1430 		return error;
1431 
1432 	error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1433 	if (error)
1434 		return error;
1435 
1436 	error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1437 	if (error)
1438 		goto out_quota;
1439 
1440 	if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
1441 		error = ea_dealloc_indirect(ip);
1442 		if (error)
1443 			goto out_quota;
1444 	}
1445 
1446 	error = ea_dealloc_block(ip);
1447 
1448 out_quota:
1449 	gfs2_quota_unhold(ip);
1450 	return error;
1451 }
1452 
1453 static const struct xattr_handler gfs2_xattr_user_handler = {
1454 	.prefix = XATTR_USER_PREFIX,
1455 	.flags  = GFS2_EATYPE_USR,
1456 	.get    = gfs2_xattr_get,
1457 	.set    = gfs2_xattr_set,
1458 };
1459 
1460 static const struct xattr_handler gfs2_xattr_security_handler = {
1461 	.prefix = XATTR_SECURITY_PREFIX,
1462 	.flags  = GFS2_EATYPE_SECURITY,
1463 	.get    = gfs2_xattr_get,
1464 	.set    = gfs2_xattr_set,
1465 };
1466 
1467 const struct xattr_handler *gfs2_xattr_handlers[] = {
1468 	&gfs2_xattr_user_handler,
1469 	&gfs2_xattr_security_handler,
1470 	&posix_acl_access_xattr_handler,
1471 	&posix_acl_default_xattr_handler,
1472 	NULL,
1473 };
1474 
1475