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_rsqa_alloc(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 return ret;
1236 } else {
1237 if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
1238 return -EIO;
1239 gfs2_holder_mark_uninitialized(&gh);
1240 }
1241 ret = __gfs2_xattr_set(inode, name, value, size, flags, handler->flags);
1242 if (gfs2_holder_initialized(&gh))
1243 gfs2_glock_dq_uninit(&gh);
1244 return ret;
1245 }
1246
ea_dealloc_indirect(struct gfs2_inode * ip)1247 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1248 {
1249 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1250 struct gfs2_rgrp_list rlist;
1251 struct gfs2_rgrpd *rgd;
1252 struct buffer_head *indbh, *dibh;
1253 __be64 *eablk, *end;
1254 unsigned int rg_blocks = 0;
1255 u64 bstart = 0;
1256 unsigned int blen = 0;
1257 unsigned int blks = 0;
1258 unsigned int x;
1259 int error;
1260
1261 error = gfs2_rindex_update(sdp);
1262 if (error)
1263 return error;
1264
1265 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1266
1267 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &indbh);
1268 if (error)
1269 return error;
1270
1271 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1272 error = -EIO;
1273 goto out;
1274 }
1275
1276 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1277 end = eablk + sdp->sd_inptrs;
1278
1279 for (; eablk < end; eablk++) {
1280 u64 bn;
1281
1282 if (!*eablk)
1283 break;
1284 bn = be64_to_cpu(*eablk);
1285
1286 if (bstart + blen == bn)
1287 blen++;
1288 else {
1289 if (bstart)
1290 gfs2_rlist_add(ip, &rlist, bstart);
1291 bstart = bn;
1292 blen = 1;
1293 }
1294 blks++;
1295 }
1296 if (bstart)
1297 gfs2_rlist_add(ip, &rlist, bstart);
1298 else
1299 goto out;
1300
1301 gfs2_rlist_alloc(&rlist);
1302
1303 for (x = 0; x < rlist.rl_rgrps; x++) {
1304 rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
1305 rg_blocks += rgd->rd_length;
1306 }
1307
1308 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1309 if (error)
1310 goto out_rlist_free;
1311
1312 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1313 RES_STATFS + RES_QUOTA, blks);
1314 if (error)
1315 goto out_gunlock;
1316
1317 gfs2_trans_add_meta(ip->i_gl, indbh);
1318
1319 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1320 bstart = 0;
1321 rgd = NULL;
1322 blen = 0;
1323
1324 for (; eablk < end; eablk++) {
1325 u64 bn;
1326
1327 if (!*eablk)
1328 break;
1329 bn = be64_to_cpu(*eablk);
1330
1331 if (bstart + blen == bn)
1332 blen++;
1333 else {
1334 if (bstart)
1335 gfs2_free_meta(ip, rgd, bstart, blen);
1336 bstart = bn;
1337 rgd = gfs2_blk2rgrpd(sdp, bstart, true);
1338 blen = 1;
1339 }
1340
1341 *eablk = 0;
1342 gfs2_add_inode_blocks(&ip->i_inode, -1);
1343 }
1344 if (bstart)
1345 gfs2_free_meta(ip, rgd, bstart, blen);
1346
1347 ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
1348
1349 error = gfs2_meta_inode_buffer(ip, &dibh);
1350 if (!error) {
1351 gfs2_trans_add_meta(ip->i_gl, dibh);
1352 gfs2_dinode_out(ip, dibh->b_data);
1353 brelse(dibh);
1354 }
1355
1356 gfs2_trans_end(sdp);
1357
1358 out_gunlock:
1359 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1360 out_rlist_free:
1361 gfs2_rlist_free(&rlist);
1362 out:
1363 brelse(indbh);
1364 return error;
1365 }
1366
ea_dealloc_block(struct gfs2_inode * ip)1367 static int ea_dealloc_block(struct gfs2_inode *ip)
1368 {
1369 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1370 struct gfs2_rgrpd *rgd;
1371 struct buffer_head *dibh;
1372 struct gfs2_holder gh;
1373 int error;
1374
1375 error = gfs2_rindex_update(sdp);
1376 if (error)
1377 return error;
1378
1379 rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
1380 if (!rgd) {
1381 gfs2_consist_inode(ip);
1382 return -EIO;
1383 }
1384
1385 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1386 if (error)
1387 return error;
1388
1389 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1390 RES_QUOTA, 1);
1391 if (error)
1392 goto out_gunlock;
1393
1394 gfs2_free_meta(ip, rgd, ip->i_eattr, 1);
1395
1396 ip->i_eattr = 0;
1397 gfs2_add_inode_blocks(&ip->i_inode, -1);
1398
1399 error = gfs2_meta_inode_buffer(ip, &dibh);
1400 if (!error) {
1401 gfs2_trans_add_meta(ip->i_gl, dibh);
1402 gfs2_dinode_out(ip, dibh->b_data);
1403 brelse(dibh);
1404 }
1405
1406 gfs2_trans_end(sdp);
1407
1408 out_gunlock:
1409 gfs2_glock_dq_uninit(&gh);
1410 return error;
1411 }
1412
1413 /**
1414 * gfs2_ea_dealloc - deallocate the extended attribute fork
1415 * @ip: the inode
1416 *
1417 * Returns: errno
1418 */
1419
gfs2_ea_dealloc(struct gfs2_inode * ip)1420 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1421 {
1422 int error;
1423
1424 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
1425 if (error)
1426 return error;
1427
1428 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1429 if (error)
1430 return error;
1431
1432 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1433 if (error)
1434 goto out_quota;
1435
1436 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
1437 error = ea_dealloc_indirect(ip);
1438 if (error)
1439 goto out_quota;
1440 }
1441
1442 error = ea_dealloc_block(ip);
1443
1444 out_quota:
1445 gfs2_quota_unhold(ip);
1446 return error;
1447 }
1448
1449 static const struct xattr_handler gfs2_xattr_user_handler = {
1450 .prefix = XATTR_USER_PREFIX,
1451 .flags = GFS2_EATYPE_USR,
1452 .get = gfs2_xattr_get,
1453 .set = gfs2_xattr_set,
1454 };
1455
1456 static const struct xattr_handler gfs2_xattr_security_handler = {
1457 .prefix = XATTR_SECURITY_PREFIX,
1458 .flags = GFS2_EATYPE_SECURITY,
1459 .get = gfs2_xattr_get,
1460 .set = gfs2_xattr_set,
1461 };
1462
1463 const struct xattr_handler *gfs2_xattr_handlers[] = {
1464 &gfs2_xattr_user_handler,
1465 &gfs2_xattr_security_handler,
1466 &posix_acl_access_xattr_handler,
1467 &posix_acl_default_xattr_handler,
1468 NULL,
1469 };
1470
1471