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