Merge branch 'master'
[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(ip->i_sbd, 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(ip->i_sbd, 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 + ip->i_sbd->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 = ip->i_sbd;
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(ip->i_sbd, &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;
378                 unsigned int l;
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                 default:
398                         /* FIXME: Needs looking at again */
399                         break;
400                 }
401
402                 memcpy(er->er_data + ei->ei_size, prefix, l);
403                 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
404                        ea->ea_name_len);
405                 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
406         }
407
408         ei->ei_size += ea_size;
409
410         return 0;
411 }
412
413 /**
414  * gfs2_ea_list -
415  * @ip:
416  * @er:
417  *
418  * Returns: actual size of data on success, -errno on error
419  */
420
421 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
422 {
423         struct gfs2_holder i_gh;
424         int error;
425
426         if (!er->er_data || !er->er_data_len) {
427                 er->er_data = NULL;
428                 er->er_data_len = 0;
429         }
430
431         error = gfs2_glock_nq_init(ip->i_gl,
432                                   LM_ST_SHARED, LM_FLAG_ANY,
433                                   &i_gh);
434         if (error)
435                 return error;
436
437         if (ip->i_di.di_eattr) {
438                 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
439
440                 error = ea_foreach(ip, ea_list_i, &ei);
441                 if (!error)
442                         error = ei.ei_size;
443         }
444
445         gfs2_glock_dq_uninit(&i_gh);
446
447         return error;
448 }
449
450 /**
451  * ea_get_unstuffed - actually copies the unstuffed data into the
452  *                    request buffer
453  * @ip:
454  * @ea:
455  * @data:
456  *
457  * Returns: errno
458  */
459
460 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
461                             char *data)
462 {
463         struct gfs2_sbd *sdp = ip->i_sbd;
464         struct buffer_head **bh;
465         unsigned int amount = GFS2_EA_DATA_LEN(ea);
466         unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
467         uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
468         unsigned int x;
469         int error = 0;
470
471         bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
472         if (!bh)
473                 return -ENOMEM;
474
475         for (x = 0; x < nptrs; x++) {
476                 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
477                                        DIO_START, bh + x);
478                 if (error) {
479                         while (x--)
480                                 brelse(bh[x]);
481                         goto out;
482                 }
483                 dataptrs++;
484         }
485
486         for (x = 0; x < nptrs; x++) {
487                 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
488                 if (error) {
489                         for (; x < nptrs; x++)
490                                 brelse(bh[x]);
491                         goto out;
492                 }
493                 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
494                         for (; x < nptrs; x++)
495                                 brelse(bh[x]);
496                         error = -EIO;
497                         goto out;
498                 }
499
500                 memcpy(data,
501                        bh[x]->b_data + sizeof(struct gfs2_meta_header),
502                        (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
503
504                 amount -= sdp->sd_jbsize;
505                 data += sdp->sd_jbsize;
506
507                 brelse(bh[x]);
508         }
509
510  out:
511         kfree(bh);
512
513         return error;
514 }
515
516 int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
517                      char *data)
518 {
519         if (GFS2_EA_IS_STUFFED(el->el_ea)) {
520                 memcpy(data,
521                        GFS2_EA2DATA(el->el_ea),
522                        GFS2_EA_DATA_LEN(el->el_ea));
523                 return 0;
524         } else
525                 return ea_get_unstuffed(ip, el->el_ea, data);
526 }
527
528 /**
529  * gfs2_ea_get_i -
530  * @ip:
531  * @er:
532  *
533  * Returns: actual size of data on success, -errno on error
534  */
535
536 int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
537 {
538         struct gfs2_ea_location el;
539         int error;
540
541         if (!ip->i_di.di_eattr)
542                 return -ENODATA;
543
544         error = gfs2_ea_find(ip, er, &el);
545         if (error)
546                 return error;
547         if (!el.el_ea)
548                 return -ENODATA;
549
550         if (er->er_data_len) {
551                 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
552                         error =  -ERANGE;
553                 else
554                         error = gfs2_ea_get_copy(ip, &el, er->er_data);
555         }
556         if (!error)
557                 error = GFS2_EA_DATA_LEN(el.el_ea);
558
559         brelse(el.el_bh);
560
561         return error;
562 }
563
564 /**
565  * gfs2_ea_get -
566  * @ip:
567  * @er:
568  *
569  * Returns: actual size of data on success, -errno on error
570  */
571
572 int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
573 {
574         struct gfs2_holder i_gh;
575         int error;
576
577         if (!er->er_name_len ||
578             er->er_name_len > GFS2_EA_MAX_NAME_LEN)
579                 return -EINVAL;
580         if (!er->er_data || !er->er_data_len) {
581                 er->er_data = NULL;
582                 er->er_data_len = 0;
583         }
584
585         error = gfs2_glock_nq_init(ip->i_gl,
586                                   LM_ST_SHARED, LM_FLAG_ANY,
587                                   &i_gh);
588         if (error)
589                 return error;
590
591         error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
592
593         gfs2_glock_dq_uninit(&i_gh);
594
595         return error;
596 }
597
598 /**
599  * ea_alloc_blk - allocates a new block for extended attributes.
600  * @ip: A pointer to the inode that's getting extended attributes
601  * @bhp:
602  *
603  * Returns: errno
604  */
605
606 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
607 {
608         struct gfs2_sbd *sdp = ip->i_sbd;
609         struct gfs2_ea_header *ea;
610         uint64_t block;
611
612         block = gfs2_alloc_meta(ip);
613
614         *bhp = gfs2_meta_new(ip->i_gl, block);
615         gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
616         gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
617         gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
618
619         ea = GFS2_EA_BH2FIRST(*bhp);
620         ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
621         ea->ea_type = GFS2_EATYPE_UNUSED;
622         ea->ea_flags = GFS2_EAFLAG_LAST;
623         ea->ea_num_ptrs = 0;
624
625         ip->i_di.di_blocks++;
626
627         return 0;
628 }
629
630 /**
631  * ea_write - writes the request info to an ea, creating new blocks if
632  *            necessary
633  * @ip:  inode that is being modified
634  * @ea:  the location of the new ea in a block
635  * @er: the write request
636  *
637  * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
638  *
639  * returns : errno
640  */
641
642 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
643                     struct gfs2_ea_request *er)
644 {
645         struct gfs2_sbd *sdp = ip->i_sbd;
646
647         ea->ea_data_len = cpu_to_be32(er->er_data_len);
648         ea->ea_name_len = er->er_name_len;
649         ea->ea_type = er->er_type;
650         ea->__pad = 0;
651
652         memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
653
654         if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
655                 ea->ea_num_ptrs = 0;
656                 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
657         } else {
658                 uint64_t *dataptr = GFS2_EA2DATAPTRS(ea);
659                 const char *data = er->er_data;
660                 unsigned int data_len = er->er_data_len;
661                 unsigned int copy;
662                 unsigned int x;
663
664                 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
665                 for (x = 0; x < ea->ea_num_ptrs; x++) {
666                         struct buffer_head *bh;
667                         uint64_t block;
668                         int mh_size = sizeof(struct gfs2_meta_header);
669
670                         block = gfs2_alloc_meta(ip);
671
672                         bh = gfs2_meta_new(ip->i_gl, block);
673                         gfs2_trans_add_bh(ip->i_gl, bh, 1);
674                         gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
675
676                         ip->i_di.di_blocks++;
677
678                         copy = (data_len > sdp->sd_jbsize) ? sdp->sd_jbsize :
679                                                              data_len;
680                         memcpy(bh->b_data + mh_size, data, copy);
681                         if (copy < sdp->sd_jbsize)
682                                 memset(bh->b_data + mh_size + copy, 0,
683                                        sdp->sd_jbsize - copy);
684
685                         *dataptr++ = cpu_to_be64((uint64_t)bh->b_blocknr);
686                         data += copy;
687                         data_len -= copy;
688
689                         brelse(bh);
690                 }
691
692                 gfs2_assert_withdraw(sdp, !data_len);
693         }
694
695         return 0;
696 }
697
698 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
699                                    struct gfs2_ea_request *er,
700                                    void *private);
701
702 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
703                              unsigned int blks,
704                              ea_skeleton_call_t skeleton_call,
705                              void *private)
706 {
707         struct gfs2_alloc *al;
708         struct buffer_head *dibh;
709         int error;
710
711         al = gfs2_alloc_get(ip);
712
713         error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
714         if (error)
715                 goto out;
716
717         error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
718         if (error)
719                 goto out_gunlock_q;
720
721         al->al_requested = blks;
722
723         error = gfs2_inplace_reserve(ip);
724         if (error)
725                 goto out_gunlock_q;
726
727         error = gfs2_trans_begin(ip->i_sbd,
728                                  blks + al->al_rgd->rd_ri.ri_length +
729                                  RES_DINODE + RES_STATFS + RES_QUOTA, 0);
730         if (error)
731                 goto out_ipres;
732
733         error = skeleton_call(ip, er, private);
734         if (error)
735                 goto out_end_trans;
736
737         error = gfs2_meta_inode_buffer(ip, &dibh);
738         if (!error) {
739                 if (er->er_flags & GFS2_ERF_MODE) {
740                         gfs2_assert_withdraw(ip->i_sbd,
741                                             (ip->i_di.di_mode & S_IFMT) ==
742                                             (er->er_mode & S_IFMT));
743                         ip->i_di.di_mode = er->er_mode;
744                 }
745                 ip->i_di.di_ctime = get_seconds();
746                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
747                 gfs2_dinode_out(&ip->i_di, dibh->b_data);
748                 brelse(dibh);
749         }
750
751  out_end_trans:
752         gfs2_trans_end(ip->i_sbd);
753
754  out_ipres:
755         gfs2_inplace_release(ip);
756
757  out_gunlock_q:
758         gfs2_quota_unlock(ip);
759
760  out:
761         gfs2_alloc_put(ip);
762
763         return error;
764 }
765
766 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
767                      void *private)
768 {
769         struct buffer_head *bh;
770         int error;
771
772         error = ea_alloc_blk(ip, &bh);
773         if (error)
774                 return error;
775
776         ip->i_di.di_eattr = bh->b_blocknr;
777         error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
778
779         brelse(bh);
780
781         return error;
782 }
783
784 /**
785  * ea_init - initializes a new eattr block
786  * @ip:
787  * @er:
788  *
789  * Returns: errno
790  */
791
792 static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
793 {
794         unsigned int jbsize = ip->i_sbd->sd_jbsize;
795         unsigned int blks = 1;
796
797         if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
798                 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
799
800         return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
801 }
802
803 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
804 {
805         uint32_t ea_size = GFS2_EA_SIZE(ea);
806         struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
807                                      ea_size);
808         uint32_t new_size = GFS2_EA_REC_LEN(ea) - ea_size;
809         int last = ea->ea_flags & GFS2_EAFLAG_LAST;
810
811         ea->ea_rec_len = cpu_to_be32(ea_size);
812         ea->ea_flags ^= last;
813
814         new->ea_rec_len = cpu_to_be32(new_size);
815         new->ea_flags = last;
816
817         return new;
818 }
819
820 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
821                                   struct gfs2_ea_location *el)
822 {
823         struct gfs2_ea_header *ea = el->el_ea;
824         struct gfs2_ea_header *prev = el->el_prev;
825         uint32_t len;
826
827         gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
828
829         if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
830                 ea->ea_type = GFS2_EATYPE_UNUSED;
831                 return;
832         } else if (GFS2_EA2NEXT(prev) != ea) {
833                 prev = GFS2_EA2NEXT(prev);
834                 gfs2_assert_withdraw(ip->i_sbd, GFS2_EA2NEXT(prev) == ea);
835         }
836
837         len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
838         prev->ea_rec_len = cpu_to_be32(len);
839
840         if (GFS2_EA_IS_LAST(ea))
841                 prev->ea_flags |= GFS2_EAFLAG_LAST;
842 }
843
844 struct ea_set {
845         int ea_split;
846
847         struct gfs2_ea_request *es_er;
848         struct gfs2_ea_location *es_el;
849
850         struct buffer_head *es_bh;
851         struct gfs2_ea_header *es_ea;
852 };
853
854 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
855                                  struct gfs2_ea_header *ea, struct ea_set *es)
856 {
857         struct gfs2_ea_request *er = es->es_er;
858         struct buffer_head *dibh;
859         int error;
860
861         error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + 2 * RES_EATTR, 0);
862         if (error)
863                 return error;
864
865         gfs2_trans_add_bh(ip->i_gl, bh, 1);
866
867         if (es->ea_split)
868                 ea = ea_split_ea(ea);
869
870         ea_write(ip, ea, er);
871
872         if (es->es_el)
873                 ea_set_remove_stuffed(ip, es->es_el);
874
875         error = gfs2_meta_inode_buffer(ip, &dibh);
876         if (error)
877                 goto out;
878
879         if (er->er_flags & GFS2_ERF_MODE) {
880                 gfs2_assert_withdraw(ip->i_sbd,
881                         (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
882                 ip->i_di.di_mode = er->er_mode;
883         }
884         ip->i_di.di_ctime = get_seconds();
885         gfs2_trans_add_bh(ip->i_gl, dibh, 1);
886         gfs2_dinode_out(&ip->i_di, dibh->b_data);
887         brelse(dibh);
888  out:
889         gfs2_trans_end(ip->i_sbd);
890
891         return error;
892 }
893
894 static int ea_set_simple_alloc(struct gfs2_inode *ip,
895                                struct gfs2_ea_request *er, void *private)
896 {
897         struct ea_set *es = private;
898         struct gfs2_ea_header *ea = es->es_ea;
899         int error;
900
901         gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
902
903         if (es->ea_split)
904                 ea = ea_split_ea(ea);
905
906         error = ea_write(ip, ea, er);
907         if (error)
908                 return error;
909
910         if (es->es_el)
911                 ea_set_remove_stuffed(ip, es->es_el);
912
913         return 0;
914 }
915
916 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
917                          struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
918                          void *private)
919 {
920         struct ea_set *es = private;
921         unsigned int size;
922         int stuffed;
923         int error;
924
925         stuffed = ea_calc_size(ip->i_sbd, es->es_er, &size);
926
927         if (ea->ea_type == GFS2_EATYPE_UNUSED) {
928                 if (GFS2_EA_REC_LEN(ea) < size)
929                         return 0;
930                 if (!GFS2_EA_IS_STUFFED(ea)) {
931                         error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
932                         if (error)
933                                 return error;
934                 }
935                 es->ea_split = 0;
936         } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
937                 es->ea_split = 1;
938         else
939                 return 0;
940
941         if (stuffed) {
942                 error = ea_set_simple_noalloc(ip, bh, ea, es);
943                 if (error)
944                         return error;
945         } else {
946                 unsigned int blks;
947
948                 es->es_bh = bh;
949                 es->es_ea = ea;
950                 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
951                                         ip->i_sbd->sd_jbsize);
952
953                 error = ea_alloc_skeleton(ip, es->es_er, blks,
954                                           ea_set_simple_alloc, es);
955                 if (error)
956                         return error;
957         }
958
959         return 1;
960 }
961
962 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
963                         void *private)
964 {
965         struct gfs2_sbd *sdp = ip->i_sbd;
966         struct buffer_head *indbh, *newbh;
967         uint64_t *eablk;
968         int error;
969         int mh_size = sizeof(struct gfs2_meta_header);
970
971         if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
972                 uint64_t *end;
973
974                 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
975                                        DIO_START | DIO_WAIT, &indbh);
976                 if (error)
977                         return error;
978
979                 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
980                         error = -EIO;
981                         goto out;
982                 }
983
984                 eablk = (uint64_t *)(indbh->b_data + mh_size);
985                 end = eablk + sdp->sd_inptrs;
986
987                 for (; eablk < end; eablk++)
988                         if (!*eablk)
989                                 break;
990
991                 if (eablk == end) {
992                         error = -ENOSPC;
993                         goto out;
994                 }
995
996                 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
997         } else {
998                 uint64_t blk;
999
1000                 blk = gfs2_alloc_meta(ip);
1001
1002                 indbh = gfs2_meta_new(ip->i_gl, blk);
1003                 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1004                 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
1005                 gfs2_buffer_clear_tail(indbh, mh_size);
1006
1007                 eablk = (uint64_t *)(indbh->b_data + mh_size);
1008                 *eablk = cpu_to_be64(ip->i_di.di_eattr);
1009                 ip->i_di.di_eattr = blk;
1010                 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
1011                 ip->i_di.di_blocks++;
1012
1013                 eablk++;
1014         }
1015
1016         error = ea_alloc_blk(ip, &newbh);
1017         if (error)
1018                 goto out;
1019
1020         *eablk = cpu_to_be64((uint64_t)newbh->b_blocknr);
1021         error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1022         brelse(newbh);
1023         if (error)
1024                 goto out;
1025
1026         if (private)
1027                 ea_set_remove_stuffed(ip, (struct gfs2_ea_location *)private);
1028
1029  out:
1030         brelse(indbh);
1031
1032         return error;
1033 }
1034
1035 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1036                     struct gfs2_ea_location *el)
1037 {
1038         struct ea_set es;
1039         unsigned int blks = 2;
1040         int error;
1041
1042         memset(&es, 0, sizeof(struct ea_set));
1043         es.es_er = er;
1044         es.es_el = el;
1045
1046         error = ea_foreach(ip, ea_set_simple, &es);
1047         if (error > 0)
1048                 return 0;
1049         if (error)
1050                 return error;
1051
1052         if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1053                 blks++;
1054         if (GFS2_EAREQ_SIZE_STUFFED(er) > ip->i_sbd->sd_jbsize)
1055                 blks += DIV_ROUND_UP(er->er_data_len, ip->i_sbd->sd_jbsize);
1056
1057         return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1058 }
1059
1060 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1061                                    struct gfs2_ea_location *el)
1062 {
1063         if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1064                 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1065                 gfs2_assert_withdraw(ip->i_sbd,
1066                                      GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1067         }
1068
1069         return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1070 }
1071
1072 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1073 {
1074         struct gfs2_ea_location el;
1075         int error;
1076
1077         if (!ip->i_di.di_eattr) {
1078                 if (er->er_flags & XATTR_REPLACE)
1079                         return -ENODATA;
1080                 return ea_init(ip, er);
1081         }
1082
1083         error = gfs2_ea_find(ip, er, &el);
1084         if (error)
1085                 return error;
1086
1087         if (el.el_ea) {
1088                 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1089                         brelse(el.el_bh);
1090                         return -EPERM;
1091                 }
1092
1093                 error = -EEXIST;
1094                 if (!(er->er_flags & XATTR_CREATE)) {
1095                         int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1096                         error = ea_set_i(ip, er, &el);
1097                         if (!error && unstuffed)
1098                                 ea_set_remove_unstuffed(ip, &el);
1099                 }
1100
1101                 brelse(el.el_bh);
1102         } else {
1103                 error = -ENODATA;
1104                 if (!(er->er_flags & XATTR_REPLACE))
1105                         error = ea_set_i(ip, er, NULL);
1106         }
1107
1108         return error;
1109 }
1110
1111 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1112 {
1113         struct gfs2_holder i_gh;
1114         int error;
1115
1116         if (!er->er_name_len ||
1117             er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1118                 return -EINVAL;
1119         if (!er->er_data || !er->er_data_len) {
1120                 er->er_data = NULL;
1121                 er->er_data_len = 0;
1122         }
1123         error = ea_check_size(ip->i_sbd, er);
1124         if (error)
1125                 return error;
1126
1127         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1128         if (error)
1129                 return error;
1130
1131         if (IS_IMMUTABLE(ip->i_vnode))
1132                 error = -EPERM;
1133         else
1134                 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1135
1136         gfs2_glock_dq_uninit(&i_gh);
1137
1138         return error;
1139 }
1140
1141 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1142 {
1143         struct gfs2_ea_header *ea = el->el_ea;
1144         struct gfs2_ea_header *prev = el->el_prev;
1145         struct buffer_head *dibh;
1146         int error;
1147
1148         error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
1149         if (error)
1150                 return error;
1151
1152         gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1153
1154         if (prev) {
1155                 uint32_t len;
1156
1157                 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1158                 prev->ea_rec_len = cpu_to_be32(len);
1159
1160                 if (GFS2_EA_IS_LAST(ea))
1161                         prev->ea_flags |= GFS2_EAFLAG_LAST;
1162         } else
1163                 ea->ea_type = GFS2_EATYPE_UNUSED;
1164
1165         error = gfs2_meta_inode_buffer(ip, &dibh);
1166         if (!error) {
1167                 ip->i_di.di_ctime = get_seconds();
1168                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1169                 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1170                 brelse(dibh);
1171         }       
1172
1173         gfs2_trans_end(ip->i_sbd);
1174
1175         return error;
1176 }
1177
1178 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1179 {
1180         struct gfs2_ea_location el;
1181         int error;
1182
1183         if (!ip->i_di.di_eattr)
1184                 return -ENODATA;
1185
1186         error = gfs2_ea_find(ip, er, &el);
1187         if (error)
1188                 return error;
1189         if (!el.el_ea)
1190                 return -ENODATA;
1191
1192         if (GFS2_EA_IS_STUFFED(el.el_ea))
1193                 error = ea_remove_stuffed(ip, &el);
1194         else
1195                 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1196                                             0);
1197
1198         brelse(el.el_bh);
1199
1200         return error;
1201 }
1202
1203 /**
1204  * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1205  * @ip: pointer to the inode of the target file
1206  * @er: request information
1207  *
1208  * Returns: errno
1209  */
1210
1211 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1212 {
1213         struct gfs2_holder i_gh;
1214         int error;
1215
1216         if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1217                 return -EINVAL;
1218
1219         error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1220         if (error)
1221                 return error;
1222
1223         if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1224                 error = -EPERM;
1225         else
1226                 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1227
1228         gfs2_glock_dq_uninit(&i_gh);
1229
1230         return error;
1231 }
1232
1233 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1234                                   struct gfs2_ea_header *ea, char *data)
1235 {
1236         struct gfs2_sbd *sdp = ip->i_sbd;
1237         struct buffer_head **bh;
1238         unsigned int amount = GFS2_EA_DATA_LEN(ea);
1239         unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1240         uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
1241         unsigned int x;
1242         int error;
1243
1244         bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1245         if (!bh)
1246                 return -ENOMEM;
1247
1248         error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1249         if (error)
1250                 goto out;
1251
1252         for (x = 0; x < nptrs; x++) {
1253                 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
1254                                        DIO_START, bh + x);
1255                 if (error) {
1256                         while (x--)
1257                                 brelse(bh[x]);
1258                         goto fail;
1259                 }
1260                 dataptrs++;
1261         }
1262
1263         for (x = 0; x < nptrs; x++) {
1264                 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
1265                 if (error) {
1266                         for (; x < nptrs; x++)
1267                                 brelse(bh[x]);
1268                         goto fail;
1269                 }
1270                 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1271                         for (; x < nptrs; x++)
1272                                 brelse(bh[x]);
1273                         error = -EIO;
1274                         goto fail;
1275                 }
1276
1277                 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1278
1279                 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header),
1280                        data,
1281                        (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1282
1283                 amount -= sdp->sd_jbsize;
1284                 data += sdp->sd_jbsize;
1285
1286                 brelse(bh[x]);
1287         }
1288
1289  out:
1290         kfree(bh);
1291
1292         return error;
1293
1294  fail:
1295         gfs2_trans_end(sdp);
1296         kfree(bh);
1297
1298         return error;
1299 }
1300
1301 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1302                       struct iattr *attr, char *data)
1303 {
1304         struct buffer_head *dibh;
1305         int error;
1306
1307         if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1308                 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
1309                 if (error)
1310                         return error;
1311
1312                 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1313                 memcpy(GFS2_EA2DATA(el->el_ea),
1314                        data,
1315                        GFS2_EA_DATA_LEN(el->el_ea));
1316         } else
1317                 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1318
1319         if (error)
1320                 return error;
1321
1322         error = gfs2_meta_inode_buffer(ip, &dibh);
1323         if (!error) {
1324                 error = inode_setattr(ip->i_vnode, attr);
1325                 gfs2_assert_warn(ip->i_sbd, !error);
1326                 gfs2_inode_attr_out(ip);
1327                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1328                 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1329                 brelse(dibh);
1330         }
1331
1332         gfs2_trans_end(ip->i_sbd);
1333
1334         return error;
1335 }
1336
1337 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1338 {
1339         struct gfs2_sbd *sdp = ip->i_sbd;
1340         struct gfs2_rgrp_list rlist;
1341         struct buffer_head *indbh, *dibh;
1342         uint64_t *eablk, *end;
1343         unsigned int rg_blocks = 0;
1344         uint64_t bstart = 0;
1345         unsigned int blen = 0;
1346         unsigned int blks = 0;
1347         unsigned int x;
1348         int error;
1349
1350         memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1351
1352         error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
1353                                DIO_START | DIO_WAIT, &indbh);
1354         if (error)
1355                 return error;
1356
1357         if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1358                 error = -EIO;
1359                 goto out;
1360         }
1361
1362         eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1363         end = eablk + sdp->sd_inptrs;
1364
1365         for (; eablk < end; eablk++) {
1366                 uint64_t bn;
1367
1368                 if (!*eablk)
1369                         break;
1370                 bn = be64_to_cpu(*eablk);
1371
1372                 if (bstart + blen == bn)
1373                         blen++;
1374                 else {
1375                         if (bstart)
1376                                 gfs2_rlist_add(sdp, &rlist, bstart);
1377                         bstart = bn;
1378                         blen = 1;
1379                 }
1380                 blks++;
1381         }
1382         if (bstart)
1383                 gfs2_rlist_add(sdp, &rlist, bstart);
1384         else
1385                 goto out;
1386
1387         gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1388
1389         for (x = 0; x < rlist.rl_rgrps; x++) {
1390                 struct gfs2_rgrpd *rgd;
1391                 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1392                 rg_blocks += rgd->rd_ri.ri_length;
1393         }
1394
1395         error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1396         if (error)
1397                 goto out_rlist_free;
1398
1399         error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
1400                                  RES_INDIRECT + RES_STATFS +
1401                                  RES_QUOTA, blks);
1402         if (error)
1403                 goto out_gunlock;
1404
1405         gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1406
1407         eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1408         bstart = 0;
1409         blen = 0;
1410
1411         for (; eablk < end; eablk++) {
1412                 uint64_t bn;
1413
1414                 if (!*eablk)
1415                         break;
1416                 bn = be64_to_cpu(*eablk);
1417
1418                 if (bstart + blen == bn)
1419                         blen++;
1420                 else {
1421                         if (bstart)
1422                                 gfs2_free_meta(ip, bstart, blen);
1423                         bstart = bn;
1424                         blen = 1;
1425                 }
1426
1427                 *eablk = 0;
1428                 if (!ip->i_di.di_blocks)
1429                         gfs2_consist_inode(ip);
1430                 ip->i_di.di_blocks--;
1431         }
1432         if (bstart)
1433                 gfs2_free_meta(ip, bstart, blen);
1434
1435         ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1436
1437         error = gfs2_meta_inode_buffer(ip, &dibh);
1438         if (!error) {
1439                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1440                 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1441                 brelse(dibh);
1442         }
1443
1444         gfs2_trans_end(sdp);
1445
1446  out_gunlock:
1447         gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1448
1449  out_rlist_free:
1450         gfs2_rlist_free(&rlist);
1451
1452  out:
1453         brelse(indbh);
1454
1455         return error;
1456 }
1457
1458 static int ea_dealloc_block(struct gfs2_inode *ip)
1459 {
1460         struct gfs2_sbd *sdp = ip->i_sbd;
1461         struct gfs2_alloc *al = &ip->i_alloc;
1462         struct gfs2_rgrpd *rgd;
1463         struct buffer_head *dibh;
1464         int error;
1465
1466         rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1467         if (!rgd) {
1468                 gfs2_consist_inode(ip);
1469                 return -EIO;
1470         }
1471
1472         error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1473                                    &al->al_rgd_gh);
1474         if (error)
1475                 return error;
1476
1477         error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE +
1478                                  RES_STATFS + RES_QUOTA, 1);
1479         if (error)
1480                 goto out_gunlock;
1481
1482         gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1483
1484         ip->i_di.di_eattr = 0;
1485         if (!ip->i_di.di_blocks)
1486                 gfs2_consist_inode(ip);
1487         ip->i_di.di_blocks--;
1488
1489         error = gfs2_meta_inode_buffer(ip, &dibh);
1490         if (!error) {
1491                 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1492                 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1493                 brelse(dibh);
1494         }
1495
1496         gfs2_trans_end(sdp);
1497
1498  out_gunlock:
1499         gfs2_glock_dq_uninit(&al->al_rgd_gh);
1500
1501         return error;
1502 }
1503
1504 /**
1505  * gfs2_ea_dealloc - deallocate the extended attribute fork
1506  * @ip: the inode
1507  *
1508  * Returns: errno
1509  */
1510
1511 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1512 {
1513         struct gfs2_alloc *al;
1514         int error;
1515
1516         al = gfs2_alloc_get(ip);
1517
1518         error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1519         if (error)
1520                 goto out_alloc;
1521
1522         error = gfs2_rindex_hold(ip->i_sbd, &al->al_ri_gh);
1523         if (error)
1524                 goto out_quota;
1525
1526         error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1527         if (error)
1528                 goto out_rindex;
1529
1530         if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1531                 error = ea_dealloc_indirect(ip);
1532                 if (error)
1533                         goto out_rindex;
1534         }
1535
1536         error = ea_dealloc_block(ip);
1537
1538  out_rindex:
1539         gfs2_glock_dq_uninit(&al->al_ri_gh);
1540
1541  out_quota:
1542         gfs2_quota_unhold(ip);
1543
1544  out_alloc:
1545         gfs2_alloc_put(ip);
1546
1547         return error;
1548 }
1549