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