blob: 288d5e6ad93ac1b3637c6c40920dd93ea723ba46 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020016#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017#include <asm/uaccess.h>
18
19#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "acl.h"
22#include "eaops.h"
23#include "eattr.h"
24#include "glock.h"
25#include "inode.h"
26#include "meta_io.h"
27#include "quota.h"
28#include "rgrp.h"
29#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050030#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031
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
42static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
43 unsigned int *size)
44{
45 *size = GFS2_EAREQ_SIZE_STUFFED(er);
46 if (*size <= sdp->sd_jbsize)
47 return 1;
48
49 *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);
50
51 return 0;
52}
53
54static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
55{
56 unsigned int size;
57
58 if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
59 return -ERANGE;
60
61 ea_calc_size(sdp, er, &size);
62
63 /* This can only happen with 512 byte blocks */
64 if (size > sdp->sd_jbsize)
65 return -ERANGE;
66
67 return 0;
68}
69
Steven Whitehousecca195c2006-09-05 13:15:18 -040070typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
David Teiglandb3b94fa2006-01-16 16:50:04 +000071 struct gfs2_ea_header *ea,
Steven Whitehousecca195c2006-09-05 13:15:18 -040072 struct gfs2_ea_header *prev, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +000073
74static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
75 ea_call_t ea_call, void *data)
76{
77 struct gfs2_ea_header *ea, *prev = NULL;
78 int error = 0;
79
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040080 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
David Teiglandb3b94fa2006-01-16 16:50:04 +000081 return -EIO;
82
83 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
84 if (!GFS2_EA_REC_LEN(ea))
85 goto fail;
Steven Whitehousecca195c2006-09-05 13:15:18 -040086 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
87 bh->b_data + bh->b_size))
David Teiglandb3b94fa2006-01-16 16:50:04 +000088 goto fail;
89 if (!GFS2_EATYPE_VALID(ea->ea_type))
90 goto fail;
91
92 error = ea_call(ip, bh, ea, prev, data);
93 if (error)
94 return error;
95
96 if (GFS2_EA_IS_LAST(ea)) {
97 if ((char *)GFS2_EA2NEXT(ea) !=
98 bh->b_data + bh->b_size)
99 goto fail;
100 break;
101 }
102 }
103
104 return error;
105
Steven Whitehousea91ea692006-09-04 12:04:26 -0400106fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107 gfs2_consist_inode(ip);
108 return -EIO;
109}
110
111static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
112{
113 struct buffer_head *bh, *eabh;
Al Virob44b84d2006-10-14 10:46:30 -0400114 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115 int error;
116
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400117 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118 if (error)
119 return error;
120
121 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
122 error = ea_foreach_i(ip, bh, ea_call, data);
123 goto out;
124 }
125
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400126 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 error = -EIO;
128 goto out;
129 }
130
Al Virob44b84d2006-10-14 10:46:30 -0400131 eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400132 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133
134 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400135 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136
137 if (!*eablk)
138 break;
139 bn = be64_to_cpu(*eablk);
140
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400141 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142 if (error)
143 break;
144 error = ea_foreach_i(ip, eabh, ea_call, data);
145 brelse(eabh);
146 if (error)
147 break;
148 }
Steven Whitehousea91ea692006-09-04 12:04:26 -0400149out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 return error;
152}
153
154struct ea_find {
155 struct gfs2_ea_request *ef_er;
156 struct gfs2_ea_location *ef_el;
157};
158
159static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
160 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
161 void *private)
162{
163 struct ea_find *ef = private;
164 struct gfs2_ea_request *er = ef->ef_er;
165
166 if (ea->ea_type == GFS2_EATYPE_UNUSED)
167 return 0;
168
169 if (ea->ea_type == er->er_type) {
170 if (ea->ea_name_len == er->er_name_len &&
171 !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
172 struct gfs2_ea_location *el = ef->ef_el;
173 get_bh(bh);
174 el->el_bh = bh;
175 el->el_ea = ea;
176 el->el_prev = prev;
177 return 1;
178 }
179 }
180
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 return 0;
182}
183
184int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
185 struct gfs2_ea_location *el)
186{
187 struct ea_find ef;
188 int error;
189
190 ef.ef_er = er;
191 ef.ef_el = el;
192
193 memset(el, 0, sizeof(struct gfs2_ea_location));
194
195 error = ea_foreach(ip, ea_find_i, &ef);
196 if (error > 0)
197 return 0;
198
199 return error;
200}
201
202/**
203 * ea_dealloc_unstuffed -
204 * @ip:
205 * @bh:
206 * @ea:
207 * @prev:
208 * @private:
209 *
210 * Take advantage of the fact that all unstuffed blocks are
211 * allocated from the same RG. But watch, this may not always
212 * be true.
213 *
214 * Returns: errno
215 */
216
217static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
218 struct gfs2_ea_header *ea,
219 struct gfs2_ea_header *prev, void *private)
220{
221 int *leave = private;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400222 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 struct gfs2_rgrpd *rgd;
224 struct gfs2_holder rg_gh;
225 struct buffer_head *dibh;
Al Virob44b84d2006-10-14 10:46:30 -0400226 __be64 *dataptrs;
227 u64 bn = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -0400228 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229 unsigned int blen = 0;
230 unsigned int blks = 0;
231 unsigned int x;
232 int error;
233
234 if (GFS2_EA_IS_STUFFED(ea))
235 return 0;
236
237 dataptrs = GFS2_EA2DATAPTRS(ea);
Steven Whitehousecca195c2006-09-05 13:15:18 -0400238 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239 if (*dataptrs) {
240 blks++;
241 bn = be64_to_cpu(*dataptrs);
242 }
Steven Whitehousecca195c2006-09-05 13:15:18 -0400243 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 if (!blks)
245 return 0;
246
247 rgd = gfs2_blk2rgrpd(sdp, bn);
248 if (!rgd) {
249 gfs2_consist_inode(ip);
250 return -EIO;
251 }
252
253 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
254 if (error)
255 return error;
256
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100257 error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
Steven Whitehousecca195c2006-09-05 13:15:18 -0400258 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259 if (error)
260 goto out_gunlock;
261
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000262 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263
264 dataptrs = GFS2_EA2DATAPTRS(ea);
265 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
266 if (!*dataptrs)
267 break;
268 bn = be64_to_cpu(*dataptrs);
269
270 if (bstart + blen == bn)
271 blen++;
272 else {
273 if (bstart)
274 gfs2_free_meta(ip, bstart, blen);
275 bstart = bn;
276 blen = 1;
277 }
278
279 *dataptrs = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000280 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 }
282 if (bstart)
283 gfs2_free_meta(ip, bstart, blen);
284
285 if (prev && !leave) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400286 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287
288 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
289 prev->ea_rec_len = cpu_to_be32(len);
290
291 if (GFS2_EA_IS_LAST(ea))
292 prev->ea_flags |= GFS2_EAFLAG_LAST;
293 } else {
294 ea->ea_type = GFS2_EATYPE_UNUSED;
295 ea->ea_num_ptrs = 0;
296 }
297
298 error = gfs2_meta_inode_buffer(ip, &dibh);
299 if (!error) {
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100300 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000301 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500302 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303 brelse(dibh);
304 }
305
306 gfs2_trans_end(sdp);
307
Steven Whitehousea91ea692006-09-04 12:04:26 -0400308out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 gfs2_glock_dq_uninit(&rg_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 return error;
311}
312
313static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
314 struct gfs2_ea_header *ea,
315 struct gfs2_ea_header *prev, int leave)
316{
317 struct gfs2_alloc *al;
318 int error;
319
320 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300321 if (!al)
322 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323
324 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
325 if (error)
326 goto out_alloc;
327
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400328 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329 if (error)
330 goto out_quota;
331
Steven Whitehousecca195c2006-09-05 13:15:18 -0400332 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333
334 gfs2_glock_dq_uninit(&al->al_ri_gh);
335
Steven Whitehousea91ea692006-09-04 12:04:26 -0400336out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 gfs2_quota_unhold(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400338out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340 return error;
341}
342
David Teiglandb3b94fa2006-01-16 16:50:04 +0000343struct ea_list {
344 struct gfs2_ea_request *ei_er;
345 unsigned int ei_size;
346};
347
348static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
349 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
350 void *private)
351{
352 struct ea_list *ei = private;
353 struct gfs2_ea_request *er = ei->ei_er;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400354 unsigned int ea_size = gfs2_ea_strlen(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355
356 if (ea->ea_type == GFS2_EATYPE_UNUSED)
357 return 0;
358
359 if (er->er_data_len) {
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400360 char *prefix = NULL;
361 unsigned int l = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 char c = 0;
363
364 if (ei->ei_size + ea_size > er->er_data_len)
365 return -ERANGE;
366
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400367 switch (ea->ea_type) {
368 case GFS2_EATYPE_USR:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 prefix = "user.";
370 l = 5;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400371 break;
372 case GFS2_EATYPE_SYS:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 prefix = "system.";
374 l = 7;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400375 break;
376 case GFS2_EATYPE_SECURITY:
377 prefix = "security.";
378 l = 9;
379 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380 }
381
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400382 BUG_ON(l == 0);
383
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400384 memcpy(er->er_data + ei->ei_size, prefix, l);
385 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386 ea->ea_name_len);
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400387 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388 }
389
390 ei->ei_size += ea_size;
391
392 return 0;
393}
394
395/**
396 * gfs2_ea_list -
397 * @ip:
398 * @er:
399 *
400 * Returns: actual size of data on success, -errno on error
401 */
402
403int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
404{
405 struct gfs2_holder i_gh;
406 int error;
407
408 if (!er->er_data || !er->er_data_len) {
409 er->er_data = NULL;
410 er->er_data_len = 0;
411 }
412
Steven Whitehousecca195c2006-09-05 13:15:18 -0400413 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 if (error)
415 return error;
416
417 if (ip->i_di.di_eattr) {
418 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
419
420 error = ea_foreach(ip, ea_list_i, &ei);
421 if (!error)
422 error = ei.ei_size;
423 }
424
425 gfs2_glock_dq_uninit(&i_gh);
426
427 return error;
428}
429
430/**
431 * ea_get_unstuffed - actually copies the unstuffed data into the
432 * request buffer
Steven Whitehousecca195c2006-09-05 13:15:18 -0400433 * @ip: The GFS2 inode
434 * @ea: The extended attribute header structure
435 * @data: The data to be copied
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436 *
437 * Returns: errno
438 */
439
440static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
441 char *data)
442{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400443 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 struct buffer_head **bh;
445 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500446 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -0400447 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448 unsigned int x;
449 int error = 0;
450
451 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
452 if (!bh)
453 return -ENOMEM;
454
455 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400456 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
457 bh + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 if (error) {
459 while (x--)
460 brelse(bh[x]);
461 goto out;
462 }
463 dataptrs++;
464 }
465
466 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400467 error = gfs2_meta_wait(sdp, bh[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 if (error) {
469 for (; x < nptrs; x++)
470 brelse(bh[x]);
471 goto out;
472 }
473 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
474 for (; x < nptrs; x++)
475 brelse(bh[x]);
476 error = -EIO;
477 goto out;
478 }
479
Steven Whitehousecca195c2006-09-05 13:15:18 -0400480 memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000481 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
482
483 amount -= sdp->sd_jbsize;
484 data += sdp->sd_jbsize;
485
486 brelse(bh[x]);
487 }
488
Steven Whitehousea91ea692006-09-04 12:04:26 -0400489out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491 return error;
492}
493
494int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
495 char *data)
496{
497 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
Steven Whitehousecca195c2006-09-05 13:15:18 -0400498 memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 return 0;
500 } else
501 return ea_get_unstuffed(ip, el->el_ea, data);
502}
503
504/**
505 * gfs2_ea_get_i -
Steven Whitehousecca195c2006-09-05 13:15:18 -0400506 * @ip: The GFS2 inode
507 * @er: The request structure
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 *
509 * Returns: actual size of data on success, -errno on error
510 */
511
512int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
513{
514 struct gfs2_ea_location el;
515 int error;
516
517 if (!ip->i_di.di_eattr)
518 return -ENODATA;
519
520 error = gfs2_ea_find(ip, er, &el);
521 if (error)
522 return error;
523 if (!el.el_ea)
524 return -ENODATA;
525
526 if (er->er_data_len) {
527 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
528 error = -ERANGE;
529 else
530 error = gfs2_ea_get_copy(ip, &el, er->er_data);
531 }
532 if (!error)
533 error = GFS2_EA_DATA_LEN(el.el_ea);
534
535 brelse(el.el_bh);
536
537 return error;
538}
539
540/**
541 * gfs2_ea_get -
Steven Whitehousecca195c2006-09-05 13:15:18 -0400542 * @ip: The GFS2 inode
543 * @er: The request structure
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 *
545 * Returns: actual size of data on success, -errno on error
546 */
547
548int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
549{
550 struct gfs2_holder i_gh;
551 int error;
552
553 if (!er->er_name_len ||
554 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
555 return -EINVAL;
556 if (!er->er_data || !er->er_data_len) {
557 er->er_data = NULL;
558 er->er_data_len = 0;
559 }
560
Steven Whitehousecca195c2006-09-05 13:15:18 -0400561 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 if (error)
563 return error;
564
565 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
566
567 gfs2_glock_dq_uninit(&i_gh);
568
569 return error;
570}
571
572/**
573 * ea_alloc_blk - allocates a new block for extended attributes.
574 * @ip: A pointer to the inode that's getting extended attributes
Steven Whitehousecca195c2006-09-05 13:15:18 -0400575 * @bhp: Pointer to pointer to a struct buffer_head
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576 *
577 * Returns: errno
578 */
579
580static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
581{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400582 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583 struct gfs2_ea_header *ea;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000584 unsigned int n = 1;
Steven Whitehousecd915492006-09-04 12:49:07 -0400585 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000587 block = gfs2_alloc_block(ip, &n);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000588 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 *bhp = gfs2_meta_new(ip->i_gl, block);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000590 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
592 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
593
594 ea = GFS2_EA_BH2FIRST(*bhp);
595 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
596 ea->ea_type = GFS2_EATYPE_UNUSED;
597 ea->ea_flags = GFS2_EAFLAG_LAST;
598 ea->ea_num_ptrs = 0;
599
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000600 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601
602 return 0;
603}
604
605/**
606 * ea_write - writes the request info to an ea, creating new blocks if
607 * necessary
Steven Whitehousecca195c2006-09-05 13:15:18 -0400608 * @ip: inode that is being modified
609 * @ea: the location of the new ea in a block
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 * @er: the write request
611 *
612 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
613 *
614 * returns : errno
615 */
616
617static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
618 struct gfs2_ea_request *er)
619{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400620 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621
622 ea->ea_data_len = cpu_to_be32(er->er_data_len);
623 ea->ea_name_len = er->er_name_len;
624 ea->ea_type = er->er_type;
625 ea->__pad = 0;
626
627 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
628
629 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
630 ea->ea_num_ptrs = 0;
631 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
632 } else {
Al Virob44b84d2006-10-14 10:46:30 -0400633 __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 const char *data = er->er_data;
635 unsigned int data_len = er->er_data_len;
636 unsigned int copy;
637 unsigned int x;
638
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500639 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 for (x = 0; x < ea->ea_num_ptrs; x++) {
641 struct buffer_head *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400642 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643 int mh_size = sizeof(struct gfs2_meta_header);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000644 unsigned int n = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000645
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000646 block = gfs2_alloc_block(ip, &n);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000647 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648 bh = gfs2_meta_new(ip->i_gl, block);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000649 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
651
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000652 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653
Steven Whitehousecca195c2006-09-05 13:15:18 -0400654 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
655 data_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 memcpy(bh->b_data + mh_size, data, copy);
657 if (copy < sdp->sd_jbsize)
658 memset(bh->b_data + mh_size + copy, 0,
659 sdp->sd_jbsize - copy);
660
Steven Whitehousecca195c2006-09-05 13:15:18 -0400661 *dataptr++ = cpu_to_be64(bh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662 data += copy;
663 data_len -= copy;
664
665 brelse(bh);
666 }
667
668 gfs2_assert_withdraw(sdp, !data_len);
669 }
670
671 return 0;
672}
673
674typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400675 struct gfs2_ea_request *er, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676
677static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
678 unsigned int blks,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400679 ea_skeleton_call_t skeleton_call, void *private)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680{
681 struct gfs2_alloc *al;
682 struct buffer_head *dibh;
683 int error;
684
685 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300686 if (!al)
687 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688
689 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
690 if (error)
691 goto out;
692
Steven Whitehouse2933f922006-11-01 13:23:29 -0500693 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 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
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400703 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100704 blks + al->al_rgd->rd_length +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 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) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400716 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500717 (ip->i_inode.i_mode & S_IFMT) ==
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 (er->er_mode & S_IFMT));
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500719 ip->i_inode.i_mode = er->er_mode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720 }
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100721 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000722 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500723 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 brelse(dibh);
725 }
726
Steven Whitehousea91ea692006-09-04 12:04:26 -0400727out_end_trans:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400728 gfs2_trans_end(GFS2_SB(&ip->i_inode));
Steven Whitehousea91ea692006-09-04 12:04:26 -0400729out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400731out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400733out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400766 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 unsigned int blks = 1;
768
769 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500770 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771
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{
Steven Whitehousecd915492006-09-04 12:49:07 -0400777 u32 ea_size = GFS2_EA_SIZE(ea);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500778 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
779 ea_size);
Steven Whitehousecd915492006-09-04 12:49:07 -0400780 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 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;
Steven Whitehousecd915492006-09-04 12:49:07 -0400797 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000799 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800
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);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400806 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807 }
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
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400833 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 if (error)
835 return error;
836
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000837 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838
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) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400852 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500853 (ip->i_inode.i_mode & S_IFMT) == (er->er_mode & S_IFMT));
854 ip->i_inode.i_mode = er->er_mode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 }
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100856 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000857 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500858 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400860out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400861 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 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
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000872 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873
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
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400896 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897
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;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500921 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400922 GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923
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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400936 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 struct buffer_head *indbh, *newbh;
Al Virob44b84d2006-10-14 10:46:30 -0400938 __be64 *eablk;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939 int error;
940 int mh_size = sizeof(struct gfs2_meta_header);
941
942 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
Al Virob44b84d2006-10-14 10:46:30 -0400943 __be64 *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400945 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT,
946 &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947 if (error)
948 return error;
949
950 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
951 error = -EIO;
952 goto out;
953 }
954
Al Virob44b84d2006-10-14 10:46:30 -0400955 eablk = (__be64 *)(indbh->b_data + mh_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 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
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000967 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -0400969 u64 blk;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000970 unsigned int n = 1;
971 blk = gfs2_alloc_block(ip, &n);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000972 gfs2_trans_add_unrevoke(sdp, blk, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973 indbh = gfs2_meta_new(ip->i_gl, blk);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000974 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000975 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
976 gfs2_buffer_clear_tail(indbh, mh_size);
977
Al Virob44b84d2006-10-14 10:46:30 -0400978 eablk = (__be64 *)(indbh->b_data + mh_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 *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;
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000982 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983
984 eablk++;
985 }
986
987 error = ea_alloc_blk(ip, &newbh);
988 if (error)
989 goto out;
990
Steven Whitehousecd915492006-09-04 12:49:07 -0400991 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
993 brelse(newbh);
994 if (error)
995 goto out;
996
997 if (private)
Steven Whitehousecca195c2006-09-05 13:15:18 -0400998 ea_set_remove_stuffed(ip, private);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999
Steven Whitehousea91ea692006-09-04 12:04:26 -04001000out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 return error;
1003}
1004
1005static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1006 struct gfs2_ea_location *el)
1007{
1008 struct ea_set es;
1009 unsigned int blks = 2;
1010 int error;
1011
1012 memset(&es, 0, sizeof(struct ea_set));
1013 es.es_er = er;
1014 es.es_el = el;
1015
1016 error = ea_foreach(ip, ea_set_simple, &es);
1017 if (error > 0)
1018 return 0;
1019 if (error)
1020 return error;
1021
1022 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1023 blks++;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001024 if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1025 blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026
1027 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1028}
1029
1030static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1031 struct gfs2_ea_location *el)
1032{
1033 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1034 el->el_prev = GFS2_EA2NEXT(el->el_prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001035 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1037 }
1038
1039 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1040}
1041
1042int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1043{
1044 struct gfs2_ea_location el;
1045 int error;
1046
1047 if (!ip->i_di.di_eattr) {
1048 if (er->er_flags & XATTR_REPLACE)
1049 return -ENODATA;
1050 return ea_init(ip, er);
1051 }
1052
1053 error = gfs2_ea_find(ip, er, &el);
1054 if (error)
1055 return error;
1056
1057 if (el.el_ea) {
1058 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1059 brelse(el.el_bh);
1060 return -EPERM;
1061 }
1062
1063 error = -EEXIST;
1064 if (!(er->er_flags & XATTR_CREATE)) {
1065 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1066 error = ea_set_i(ip, er, &el);
1067 if (!error && unstuffed)
1068 ea_set_remove_unstuffed(ip, &el);
1069 }
1070
1071 brelse(el.el_bh);
1072 } else {
1073 error = -ENODATA;
1074 if (!(er->er_flags & XATTR_REPLACE))
1075 error = ea_set_i(ip, er, NULL);
1076 }
1077
1078 return error;
1079}
1080
1081int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1082{
1083 struct gfs2_holder i_gh;
1084 int error;
1085
Steven Whitehousecca195c2006-09-05 13:15:18 -04001086 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087 return -EINVAL;
1088 if (!er->er_data || !er->er_data_len) {
1089 er->er_data = NULL;
1090 er->er_data_len = 0;
1091 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001092 error = ea_check_size(GFS2_SB(&ip->i_inode), er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093 if (error)
1094 return error;
1095
1096 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1097 if (error)
1098 return error;
1099
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001100 if (IS_IMMUTABLE(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 error = -EPERM;
1102 else
1103 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1104
1105 gfs2_glock_dq_uninit(&i_gh);
1106
1107 return error;
1108}
1109
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
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001117 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 if (error)
1119 return error;
1120
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001121 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122
1123 if (prev) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001124 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125
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;
1131 } else
1132 ea->ea_type = GFS2_EATYPE_UNUSED;
1133
1134 error = gfs2_meta_inode_buffer(ip, &dibh);
1135 if (!error) {
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001136 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001137 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001138 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 brelse(dibh);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001140 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001142 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143
1144 return error;
1145}
1146
1147int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1148{
1149 struct gfs2_ea_location el;
1150 int error;
1151
1152 if (!ip->i_di.di_eattr)
1153 return -ENODATA;
1154
1155 error = gfs2_ea_find(ip, er, &el);
1156 if (error)
1157 return error;
1158 if (!el.el_ea)
1159 return -ENODATA;
1160
1161 if (GFS2_EA_IS_STUFFED(el.el_ea))
1162 error = ea_remove_stuffed(ip, &el);
1163 else
1164 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1165 0);
1166
1167 brelse(el.el_bh);
1168
1169 return error;
1170}
1171
1172/**
1173 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1174 * @ip: pointer to the inode of the target file
1175 * @er: request information
1176 *
1177 * Returns: errno
1178 */
1179
1180int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1181{
1182 struct gfs2_holder i_gh;
1183 int error;
1184
1185 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1186 return -EINVAL;
1187
1188 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1189 if (error)
1190 return error;
1191
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001192 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 error = -EPERM;
1194 else
1195 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1196
1197 gfs2_glock_dq_uninit(&i_gh);
1198
1199 return error;
1200}
1201
1202static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1203 struct gfs2_ea_header *ea, char *data)
1204{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001205 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206 struct buffer_head **bh;
1207 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001208 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -04001209 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001210 unsigned int x;
1211 int error;
1212
1213 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1214 if (!bh)
1215 return -ENOMEM;
1216
1217 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1218 if (error)
1219 goto out;
1220
1221 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001222 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
1223 bh + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224 if (error) {
1225 while (x--)
1226 brelse(bh[x]);
1227 goto fail;
1228 }
1229 dataptrs++;
1230 }
1231
1232 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001233 error = gfs2_meta_wait(sdp, bh[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001234 if (error) {
1235 for (; x < nptrs; x++)
1236 brelse(bh[x]);
1237 goto fail;
1238 }
1239 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1240 for (; x < nptrs; x++)
1241 brelse(bh[x]);
1242 error = -EIO;
1243 goto fail;
1244 }
1245
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001246 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247
Steven Whitehousecca195c2006-09-05 13:15:18 -04001248 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001249 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1250
1251 amount -= sdp->sd_jbsize;
1252 data += sdp->sd_jbsize;
1253
1254 brelse(bh[x]);
1255 }
1256
Steven Whitehousea91ea692006-09-04 12:04:26 -04001257out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001259 return error;
1260
Steven Whitehousea91ea692006-09-04 12:04:26 -04001261fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001262 gfs2_trans_end(sdp);
1263 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264 return error;
1265}
1266
1267int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1268 struct iattr *attr, char *data)
1269{
1270 struct buffer_head *dibh;
1271 int error;
1272
1273 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001274 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001275 if (error)
1276 return error;
1277
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001278 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
Steven Whitehousecca195c2006-09-05 13:15:18 -04001279 memcpy(GFS2_EA2DATA(el->el_ea), data,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280 GFS2_EA_DATA_LEN(el->el_ea));
1281 } else
1282 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1283
1284 if (error)
1285 return error;
1286
1287 error = gfs2_meta_inode_buffer(ip, &dibh);
1288 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001289 error = inode_setattr(&ip->i_inode, attr);
1290 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001291 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001292 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001293 brelse(dibh);
1294 }
1295
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001296 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001297
1298 return error;
1299}
1300
1301static int ea_dealloc_indirect(struct gfs2_inode *ip)
1302{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001303 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001304 struct gfs2_rgrp_list rlist;
1305 struct buffer_head *indbh, *dibh;
Al Virob44b84d2006-10-14 10:46:30 -04001306 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307 unsigned int rg_blocks = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001308 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 unsigned int blen = 0;
1310 unsigned int blks = 0;
1311 unsigned int x;
1312 int error;
1313
1314 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1315
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001316 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317 if (error)
1318 return error;
1319
1320 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1321 error = -EIO;
1322 goto out;
1323 }
1324
Al Virob44b84d2006-10-14 10:46:30 -04001325 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001326 end = eablk + sdp->sd_inptrs;
1327
1328 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001329 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001330
1331 if (!*eablk)
1332 break;
1333 bn = be64_to_cpu(*eablk);
1334
1335 if (bstart + blen == bn)
1336 blen++;
1337 else {
1338 if (bstart)
1339 gfs2_rlist_add(sdp, &rlist, bstart);
1340 bstart = bn;
1341 blen = 1;
1342 }
1343 blks++;
1344 }
1345 if (bstart)
1346 gfs2_rlist_add(sdp, &rlist, bstart);
1347 else
1348 goto out;
1349
Bob Petersonfe6c9912008-01-28 11:13:02 -06001350 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001351
1352 for (x = 0; x < rlist.rl_rgrps; x++) {
1353 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001354 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001355 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001356 }
1357
1358 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1359 if (error)
1360 goto out_rlist_free;
1361
Steven Whitehousecca195c2006-09-05 13:15:18 -04001362 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1363 RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001364 if (error)
1365 goto out_gunlock;
1366
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001367 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001368
Al Virob44b84d2006-10-14 10:46:30 -04001369 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001370 bstart = 0;
1371 blen = 0;
1372
1373 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001374 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001375
1376 if (!*eablk)
1377 break;
1378 bn = be64_to_cpu(*eablk);
1379
1380 if (bstart + blen == bn)
1381 blen++;
1382 else {
1383 if (bstart)
1384 gfs2_free_meta(ip, bstart, blen);
1385 bstart = bn;
1386 blen = 1;
1387 }
1388
1389 *eablk = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001390 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 }
1392 if (bstart)
1393 gfs2_free_meta(ip, bstart, blen);
1394
1395 ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1396
1397 error = gfs2_meta_inode_buffer(ip, &dibh);
1398 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001399 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001400 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001401 brelse(dibh);
1402 }
1403
1404 gfs2_trans_end(sdp);
1405
Steven Whitehousea91ea692006-09-04 12:04:26 -04001406out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001407 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001408out_rlist_free:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001409 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001410out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001411 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001412 return error;
1413}
1414
1415static int ea_dealloc_block(struct gfs2_inode *ip)
1416{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001417 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001418 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001419 struct gfs2_rgrpd *rgd;
1420 struct buffer_head *dibh;
1421 int error;
1422
1423 rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1424 if (!rgd) {
1425 gfs2_consist_inode(ip);
1426 return -EIO;
1427 }
1428
1429 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1430 &al->al_rgd_gh);
1431 if (error)
1432 return error;
1433
Steven Whitehousecca195c2006-09-05 13:15:18 -04001434 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1435 RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001436 if (error)
1437 goto out_gunlock;
1438
1439 gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1440
1441 ip->i_di.di_eattr = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001442 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001443
1444 error = gfs2_meta_inode_buffer(ip, &dibh);
1445 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001446 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001447 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448 brelse(dibh);
1449 }
1450
1451 gfs2_trans_end(sdp);
1452
Steven Whitehousea91ea692006-09-04 12:04:26 -04001453out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001454 gfs2_glock_dq_uninit(&al->al_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455 return error;
1456}
1457
1458/**
1459 * gfs2_ea_dealloc - deallocate the extended attribute fork
1460 * @ip: the inode
1461 *
1462 * Returns: errno
1463 */
1464
1465int gfs2_ea_dealloc(struct gfs2_inode *ip)
1466{
1467 struct gfs2_alloc *al;
1468 int error;
1469
1470 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001471 if (!al)
1472 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001473
1474 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1475 if (error)
1476 goto out_alloc;
1477
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001478 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001479 if (error)
1480 goto out_quota;
1481
1482 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1483 if (error)
1484 goto out_rindex;
1485
1486 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1487 error = ea_dealloc_indirect(ip);
1488 if (error)
1489 goto out_rindex;
1490 }
1491
1492 error = ea_dealloc_block(ip);
1493
Steven Whitehousea91ea692006-09-04 12:04:26 -04001494out_rindex:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001495 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001496out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001497 gfs2_quota_unhold(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001498out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001499 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001500 return error;
1501}
1502