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