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