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