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