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