blob: 935cc9a571688128fb8d0fd40dad675f3f3af2d4 [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
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/xattr.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050016#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020017#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018#include <asm/uaccess.h>
19
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "acl.h"
23#include "eaops.h"
24#include "eattr.h"
25#include "glock.h"
26#include "inode.h"
27#include "meta_io.h"
28#include "quota.h"
29#include "rgrp.h"
30#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050031#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
33/**
34 * ea_calc_size - returns the acutal number of bytes the request will take up
35 * (not counting any unstuffed data blocks)
36 * @sdp:
37 * @er:
38 * @size:
39 *
40 * Returns: 1 if the EA should be stuffed
41 */
42
43static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
44 unsigned int *size)
45{
46 *size = GFS2_EAREQ_SIZE_STUFFED(er);
47 if (*size <= sdp->sd_jbsize)
48 return 1;
49
50 *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);
51
52 return 0;
53}
54
55static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
56{
57 unsigned int size;
58
59 if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
60 return -ERANGE;
61
62 ea_calc_size(sdp, er, &size);
63
64 /* This can only happen with 512 byte blocks */
65 if (size > sdp->sd_jbsize)
66 return -ERANGE;
67
68 return 0;
69}
70
Steven Whitehousecca195c2006-09-05 13:15:18 -040071typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
David Teiglandb3b94fa2006-01-16 16:50:04 +000072 struct gfs2_ea_header *ea,
Steven Whitehousecca195c2006-09-05 13:15:18 -040073 struct gfs2_ea_header *prev, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +000074
75static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
76 ea_call_t ea_call, void *data)
77{
78 struct gfs2_ea_header *ea, *prev = NULL;
79 int error = 0;
80
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040081 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
David Teiglandb3b94fa2006-01-16 16:50:04 +000082 return -EIO;
83
84 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
85 if (!GFS2_EA_REC_LEN(ea))
86 goto fail;
Steven Whitehousecca195c2006-09-05 13:15:18 -040087 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
88 bh->b_data + bh->b_size))
David Teiglandb3b94fa2006-01-16 16:50:04 +000089 goto fail;
90 if (!GFS2_EATYPE_VALID(ea->ea_type))
91 goto fail;
92
93 error = ea_call(ip, bh, ea, prev, data);
94 if (error)
95 return error;
96
97 if (GFS2_EA_IS_LAST(ea)) {
98 if ((char *)GFS2_EA2NEXT(ea) !=
99 bh->b_data + bh->b_size)
100 goto fail;
101 break;
102 }
103 }
104
105 return error;
106
Steven Whitehousea91ea692006-09-04 12:04:26 -0400107fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108 gfs2_consist_inode(ip);
109 return -EIO;
110}
111
112static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
113{
114 struct buffer_head *bh, *eabh;
Al Virob44b84d2006-10-14 10:46:30 -0400115 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116 int error;
117
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400118 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119 if (error)
120 return error;
121
122 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
123 error = ea_foreach_i(ip, bh, ea_call, data);
124 goto out;
125 }
126
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400127 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 error = -EIO;
129 goto out;
130 }
131
Al Virob44b84d2006-10-14 10:46:30 -0400132 eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400133 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134
135 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400136 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137
138 if (!*eablk)
139 break;
140 bn = be64_to_cpu(*eablk);
141
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400142 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 if (error)
144 break;
145 error = ea_foreach_i(ip, eabh, ea_call, data);
146 brelse(eabh);
147 if (error)
148 break;
149 }
Steven Whitehousea91ea692006-09-04 12:04:26 -0400150out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 return error;
153}
154
155struct ea_find {
156 struct gfs2_ea_request *ef_er;
157 struct gfs2_ea_location *ef_el;
158};
159
160static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
161 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
162 void *private)
163{
164 struct ea_find *ef = private;
165 struct gfs2_ea_request *er = ef->ef_er;
166
167 if (ea->ea_type == GFS2_EATYPE_UNUSED)
168 return 0;
169
170 if (ea->ea_type == er->er_type) {
171 if (ea->ea_name_len == er->er_name_len &&
172 !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
173 struct gfs2_ea_location *el = ef->ef_el;
174 get_bh(bh);
175 el->el_bh = bh;
176 el->el_ea = ea;
177 el->el_prev = prev;
178 return 1;
179 }
180 }
181
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 return 0;
183}
184
185int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
186 struct gfs2_ea_location *el)
187{
188 struct ea_find ef;
189 int error;
190
191 ef.ef_er = er;
192 ef.ef_el = el;
193
194 memset(el, 0, sizeof(struct gfs2_ea_location));
195
196 error = ea_foreach(ip, ea_find_i, &ef);
197 if (error > 0)
198 return 0;
199
200 return error;
201}
202
203/**
204 * ea_dealloc_unstuffed -
205 * @ip:
206 * @bh:
207 * @ea:
208 * @prev:
209 * @private:
210 *
211 * Take advantage of the fact that all unstuffed blocks are
212 * allocated from the same RG. But watch, this may not always
213 * be true.
214 *
215 * Returns: errno
216 */
217
218static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
219 struct gfs2_ea_header *ea,
220 struct gfs2_ea_header *prev, void *private)
221{
222 int *leave = private;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400223 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224 struct gfs2_rgrpd *rgd;
225 struct gfs2_holder rg_gh;
226 struct buffer_head *dibh;
Al Virob44b84d2006-10-14 10:46:30 -0400227 __be64 *dataptrs;
228 u64 bn = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -0400229 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 unsigned int blen = 0;
231 unsigned int blks = 0;
232 unsigned int x;
233 int error;
234
235 if (GFS2_EA_IS_STUFFED(ea))
236 return 0;
237
238 dataptrs = GFS2_EA2DATAPTRS(ea);
Steven Whitehousecca195c2006-09-05 13:15:18 -0400239 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 if (*dataptrs) {
241 blks++;
242 bn = be64_to_cpu(*dataptrs);
243 }
Steven Whitehousecca195c2006-09-05 13:15:18 -0400244 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 if (!blks)
246 return 0;
247
248 rgd = gfs2_blk2rgrpd(sdp, bn);
249 if (!rgd) {
250 gfs2_consist_inode(ip);
251 return -EIO;
252 }
253
254 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
255 if (error)
256 return error;
257
Steven Whitehousecca195c2006-09-05 13:15:18 -0400258 error = gfs2_trans_begin(sdp, rgd->rd_ri.ri_length + RES_DINODE +
259 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260 if (error)
261 goto out_gunlock;
262
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000263 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000264
265 dataptrs = GFS2_EA2DATAPTRS(ea);
266 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
267 if (!*dataptrs)
268 break;
269 bn = be64_to_cpu(*dataptrs);
270
271 if (bstart + blen == bn)
272 blen++;
273 else {
274 if (bstart)
275 gfs2_free_meta(ip, bstart, blen);
276 bstart = bn;
277 blen = 1;
278 }
279
280 *dataptrs = 0;
281 if (!ip->i_di.di_blocks)
282 gfs2_consist_inode(ip);
283 ip->i_di.di_blocks--;
284 }
285 if (bstart)
286 gfs2_free_meta(ip, bstart, blen);
287
288 if (prev && !leave) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400289 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290
291 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
292 prev->ea_rec_len = cpu_to_be32(len);
293
294 if (GFS2_EA_IS_LAST(ea))
295 prev->ea_flags |= GFS2_EAFLAG_LAST;
296 } else {
297 ea->ea_type = GFS2_EATYPE_UNUSED;
298 ea->ea_num_ptrs = 0;
299 }
300
301 error = gfs2_meta_inode_buffer(ip, &dibh);
302 if (!error) {
303 ip->i_di.di_ctime = get_seconds();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000304 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500305 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306 brelse(dibh);
307 }
308
309 gfs2_trans_end(sdp);
310
Steven Whitehousea91ea692006-09-04 12:04:26 -0400311out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312 gfs2_glock_dq_uninit(&rg_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313 return error;
314}
315
316static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
317 struct gfs2_ea_header *ea,
318 struct gfs2_ea_header *prev, int leave)
319{
320 struct gfs2_alloc *al;
321 int error;
322
323 al = gfs2_alloc_get(ip);
324
325 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
326 if (error)
327 goto out_alloc;
328
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400329 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330 if (error)
331 goto out_quota;
332
Steven Whitehousecca195c2006-09-05 13:15:18 -0400333 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
335 gfs2_glock_dq_uninit(&al->al_ri_gh);
336
Steven Whitehousea91ea692006-09-04 12:04:26 -0400337out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338 gfs2_quota_unhold(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400339out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 return error;
342}
343
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344struct ea_list {
345 struct gfs2_ea_request *ei_er;
346 unsigned int ei_size;
347};
348
349static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
350 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
351 void *private)
352{
353 struct ea_list *ei = private;
354 struct gfs2_ea_request *er = ei->ei_er;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400355 unsigned int ea_size = gfs2_ea_strlen(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356
357 if (ea->ea_type == GFS2_EATYPE_UNUSED)
358 return 0;
359
360 if (er->er_data_len) {
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400361 char *prefix = NULL;
362 unsigned int l = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000363 char c = 0;
364
365 if (ei->ei_size + ea_size > er->er_data_len)
366 return -ERANGE;
367
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400368 switch (ea->ea_type) {
369 case GFS2_EATYPE_USR:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370 prefix = "user.";
371 l = 5;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400372 break;
373 case GFS2_EATYPE_SYS:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 prefix = "system.";
375 l = 7;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400376 break;
377 case GFS2_EATYPE_SECURITY:
378 prefix = "security.";
379 l = 9;
380 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381 }
382
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400383 BUG_ON(l == 0);
384
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400385 memcpy(er->er_data + ei->ei_size, prefix, l);
386 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 ea->ea_name_len);
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400388 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 }
390
391 ei->ei_size += ea_size;
392
393 return 0;
394}
395
396/**
397 * gfs2_ea_list -
398 * @ip:
399 * @er:
400 *
401 * Returns: actual size of data on success, -errno on error
402 */
403
404int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
405{
406 struct gfs2_holder i_gh;
407 int error;
408
409 if (!er->er_data || !er->er_data_len) {
410 er->er_data = NULL;
411 er->er_data_len = 0;
412 }
413
Steven Whitehousecca195c2006-09-05 13:15:18 -0400414 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 if (error)
416 return error;
417
418 if (ip->i_di.di_eattr) {
419 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
420
421 error = ea_foreach(ip, ea_list_i, &ei);
422 if (!error)
423 error = ei.ei_size;
424 }
425
426 gfs2_glock_dq_uninit(&i_gh);
427
428 return error;
429}
430
431/**
432 * ea_get_unstuffed - actually copies the unstuffed data into the
433 * request buffer
Steven Whitehousecca195c2006-09-05 13:15:18 -0400434 * @ip: The GFS2 inode
435 * @ea: The extended attribute header structure
436 * @data: The data to be copied
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437 *
438 * Returns: errno
439 */
440
441static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
442 char *data)
443{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400444 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 struct buffer_head **bh;
446 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500447 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -0400448 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449 unsigned int x;
450 int error = 0;
451
452 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
453 if (!bh)
454 return -ENOMEM;
455
456 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400457 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
458 bh + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 if (error) {
460 while (x--)
461 brelse(bh[x]);
462 goto out;
463 }
464 dataptrs++;
465 }
466
467 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400468 error = gfs2_meta_wait(sdp, bh[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 if (error) {
470 for (; x < nptrs; x++)
471 brelse(bh[x]);
472 goto out;
473 }
474 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
475 for (; x < nptrs; x++)
476 brelse(bh[x]);
477 error = -EIO;
478 goto out;
479 }
480
Steven Whitehousecca195c2006-09-05 13:15:18 -0400481 memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
483
484 amount -= sdp->sd_jbsize;
485 data += sdp->sd_jbsize;
486
487 brelse(bh[x]);
488 }
489
Steven Whitehousea91ea692006-09-04 12:04:26 -0400490out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 return error;
493}
494
495int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
496 char *data)
497{
498 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
Steven Whitehousecca195c2006-09-05 13:15:18 -0400499 memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500 return 0;
501 } else
502 return ea_get_unstuffed(ip, el->el_ea, data);
503}
504
505/**
506 * gfs2_ea_get_i -
Steven Whitehousecca195c2006-09-05 13:15:18 -0400507 * @ip: The GFS2 inode
508 * @er: The request structure
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 *
510 * Returns: actual size of data on success, -errno on error
511 */
512
513int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
514{
515 struct gfs2_ea_location el;
516 int error;
517
518 if (!ip->i_di.di_eattr)
519 return -ENODATA;
520
521 error = gfs2_ea_find(ip, er, &el);
522 if (error)
523 return error;
524 if (!el.el_ea)
525 return -ENODATA;
526
527 if (er->er_data_len) {
528 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
529 error = -ERANGE;
530 else
531 error = gfs2_ea_get_copy(ip, &el, er->er_data);
532 }
533 if (!error)
534 error = GFS2_EA_DATA_LEN(el.el_ea);
535
536 brelse(el.el_bh);
537
538 return error;
539}
540
541/**
542 * gfs2_ea_get -
Steven Whitehousecca195c2006-09-05 13:15:18 -0400543 * @ip: The GFS2 inode
544 * @er: The request structure
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545 *
546 * Returns: actual size of data on success, -errno on error
547 */
548
549int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
550{
551 struct gfs2_holder i_gh;
552 int error;
553
554 if (!er->er_name_len ||
555 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
556 return -EINVAL;
557 if (!er->er_data || !er->er_data_len) {
558 er->er_data = NULL;
559 er->er_data_len = 0;
560 }
561
Steven Whitehousecca195c2006-09-05 13:15:18 -0400562 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 if (error)
564 return error;
565
566 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
567
568 gfs2_glock_dq_uninit(&i_gh);
569
570 return error;
571}
572
573/**
574 * ea_alloc_blk - allocates a new block for extended attributes.
575 * @ip: A pointer to the inode that's getting extended attributes
Steven Whitehousecca195c2006-09-05 13:15:18 -0400576 * @bhp: Pointer to pointer to a struct buffer_head
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 *
578 * Returns: errno
579 */
580
581static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
582{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400583 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 struct gfs2_ea_header *ea;
Steven Whitehousecd915492006-09-04 12:49:07 -0400585 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586
587 block = gfs2_alloc_meta(ip);
588
589 *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
600 ip->i_di.di_blocks++;
601
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);
644
645 block = gfs2_alloc_meta(ip);
646
647 bh = gfs2_meta_new(ip->i_gl, block);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000648 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
650
651 ip->i_di.di_blocks++;
652
Steven Whitehousecca195c2006-09-05 13:15:18 -0400653 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
654 data_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000655 memcpy(bh->b_data + mh_size, data, copy);
656 if (copy < sdp->sd_jbsize)
657 memset(bh->b_data + mh_size + copy, 0,
658 sdp->sd_jbsize - copy);
659
Steven Whitehousecca195c2006-09-05 13:15:18 -0400660 *dataptr++ = cpu_to_be64(bh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 data += copy;
662 data_len -= copy;
663
664 brelse(bh);
665 }
666
667 gfs2_assert_withdraw(sdp, !data_len);
668 }
669
670 return 0;
671}
672
673typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400674 struct gfs2_ea_request *er, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675
676static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
677 unsigned int blks,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400678 ea_skeleton_call_t skeleton_call, void *private)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679{
680 struct gfs2_alloc *al;
681 struct buffer_head *dibh;
682 int error;
683
684 al = gfs2_alloc_get(ip);
685
686 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
687 if (error)
688 goto out;
689
Steven Whitehouse2933f922006-11-01 13:23:29 -0500690 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 if (error)
692 goto out_gunlock_q;
693
694 al->al_requested = blks;
695
696 error = gfs2_inplace_reserve(ip);
697 if (error)
698 goto out_gunlock_q;
699
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400700 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701 blks + al->al_rgd->rd_ri.ri_length +
702 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
703 if (error)
704 goto out_ipres;
705
706 error = skeleton_call(ip, er, private);
707 if (error)
708 goto out_end_trans;
709
710 error = gfs2_meta_inode_buffer(ip, &dibh);
711 if (!error) {
712 if (er->er_flags & GFS2_ERF_MODE) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400713 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500714 (ip->i_inode.i_mode & S_IFMT) ==
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 (er->er_mode & S_IFMT));
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500716 ip->i_inode.i_mode = er->er_mode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 }
718 ip->i_di.di_ctime = get_seconds();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000719 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500720 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721 brelse(dibh);
722 }
723
Steven Whitehousea91ea692006-09-04 12:04:26 -0400724out_end_trans:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400725 gfs2_trans_end(GFS2_SB(&ip->i_inode));
Steven Whitehousea91ea692006-09-04 12:04:26 -0400726out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400728out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400730out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 return error;
733}
734
735static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
736 void *private)
737{
738 struct buffer_head *bh;
739 int error;
740
741 error = ea_alloc_blk(ip, &bh);
742 if (error)
743 return error;
744
745 ip->i_di.di_eattr = bh->b_blocknr;
746 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
747
748 brelse(bh);
749
750 return error;
751}
752
753/**
754 * ea_init - initializes a new eattr block
755 * @ip:
756 * @er:
757 *
758 * Returns: errno
759 */
760
761static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
762{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400763 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 unsigned int blks = 1;
765
766 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500767 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768
769 return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
770}
771
772static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
773{
Steven Whitehousecd915492006-09-04 12:49:07 -0400774 u32 ea_size = GFS2_EA_SIZE(ea);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500775 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
776 ea_size);
Steven Whitehousecd915492006-09-04 12:49:07 -0400777 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
779
780 ea->ea_rec_len = cpu_to_be32(ea_size);
781 ea->ea_flags ^= last;
782
783 new->ea_rec_len = cpu_to_be32(new_size);
784 new->ea_flags = last;
785
786 return new;
787}
788
789static void ea_set_remove_stuffed(struct gfs2_inode *ip,
790 struct gfs2_ea_location *el)
791{
792 struct gfs2_ea_header *ea = el->el_ea;
793 struct gfs2_ea_header *prev = el->el_prev;
Steven Whitehousecd915492006-09-04 12:49:07 -0400794 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000796 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797
798 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
799 ea->ea_type = GFS2_EATYPE_UNUSED;
800 return;
801 } else if (GFS2_EA2NEXT(prev) != ea) {
802 prev = GFS2_EA2NEXT(prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400803 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 }
805
806 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
807 prev->ea_rec_len = cpu_to_be32(len);
808
809 if (GFS2_EA_IS_LAST(ea))
810 prev->ea_flags |= GFS2_EAFLAG_LAST;
811}
812
813struct ea_set {
814 int ea_split;
815
816 struct gfs2_ea_request *es_er;
817 struct gfs2_ea_location *es_el;
818
819 struct buffer_head *es_bh;
820 struct gfs2_ea_header *es_ea;
821};
822
823static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
824 struct gfs2_ea_header *ea, struct ea_set *es)
825{
826 struct gfs2_ea_request *er = es->es_er;
827 struct buffer_head *dibh;
828 int error;
829
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400830 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 if (error)
832 return error;
833
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000834 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835
836 if (es->ea_split)
837 ea = ea_split_ea(ea);
838
839 ea_write(ip, ea, er);
840
841 if (es->es_el)
842 ea_set_remove_stuffed(ip, es->es_el);
843
844 error = gfs2_meta_inode_buffer(ip, &dibh);
845 if (error)
846 goto out;
847
848 if (er->er_flags & GFS2_ERF_MODE) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400849 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500850 (ip->i_inode.i_mode & S_IFMT) == (er->er_mode & S_IFMT));
851 ip->i_inode.i_mode = er->er_mode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 }
853 ip->i_di.di_ctime = get_seconds();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000854 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500855 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400857out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400858 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 return error;
860}
861
862static int ea_set_simple_alloc(struct gfs2_inode *ip,
863 struct gfs2_ea_request *er, void *private)
864{
865 struct ea_set *es = private;
866 struct gfs2_ea_header *ea = es->es_ea;
867 int error;
868
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000869 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870
871 if (es->ea_split)
872 ea = ea_split_ea(ea);
873
874 error = ea_write(ip, ea, er);
875 if (error)
876 return error;
877
878 if (es->es_el)
879 ea_set_remove_stuffed(ip, es->es_el);
880
881 return 0;
882}
883
884static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
885 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
886 void *private)
887{
888 struct ea_set *es = private;
889 unsigned int size;
890 int stuffed;
891 int error;
892
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400893 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894
895 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
896 if (GFS2_EA_REC_LEN(ea) < size)
897 return 0;
898 if (!GFS2_EA_IS_STUFFED(ea)) {
899 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
900 if (error)
901 return error;
902 }
903 es->ea_split = 0;
904 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
905 es->ea_split = 1;
906 else
907 return 0;
908
909 if (stuffed) {
910 error = ea_set_simple_noalloc(ip, bh, ea, es);
911 if (error)
912 return error;
913 } else {
914 unsigned int blks;
915
916 es->es_bh = bh;
917 es->es_ea = ea;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500918 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400919 GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920
921 error = ea_alloc_skeleton(ip, es->es_er, blks,
922 ea_set_simple_alloc, es);
923 if (error)
924 return error;
925 }
926
927 return 1;
928}
929
930static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
931 void *private)
932{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400933 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 struct buffer_head *indbh, *newbh;
Al Virob44b84d2006-10-14 10:46:30 -0400935 __be64 *eablk;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936 int error;
937 int mh_size = sizeof(struct gfs2_meta_header);
938
939 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
Al Virob44b84d2006-10-14 10:46:30 -0400940 __be64 *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400942 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT,
943 &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 if (error)
945 return error;
946
947 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
948 error = -EIO;
949 goto out;
950 }
951
Al Virob44b84d2006-10-14 10:46:30 -0400952 eablk = (__be64 *)(indbh->b_data + mh_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 end = eablk + sdp->sd_inptrs;
954
955 for (; eablk < end; eablk++)
956 if (!*eablk)
957 break;
958
959 if (eablk == end) {
960 error = -ENOSPC;
961 goto out;
962 }
963
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000964 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -0400966 u64 blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967
968 blk = gfs2_alloc_meta(ip);
969
970 indbh = gfs2_meta_new(ip->i_gl, blk);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000971 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
973 gfs2_buffer_clear_tail(indbh, mh_size);
974
Al Virob44b84d2006-10-14 10:46:30 -0400975 eablk = (__be64 *)(indbh->b_data + mh_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 *eablk = cpu_to_be64(ip->i_di.di_eattr);
977 ip->i_di.di_eattr = blk;
978 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
979 ip->i_di.di_blocks++;
980
981 eablk++;
982 }
983
984 error = ea_alloc_blk(ip, &newbh);
985 if (error)
986 goto out;
987
Steven Whitehousecd915492006-09-04 12:49:07 -0400988 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
990 brelse(newbh);
991 if (error)
992 goto out;
993
994 if (private)
Steven Whitehousecca195c2006-09-05 13:15:18 -0400995 ea_set_remove_stuffed(ip, private);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996
Steven Whitehousea91ea692006-09-04 12:04:26 -0400997out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 return error;
1000}
1001
1002static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1003 struct gfs2_ea_location *el)
1004{
1005 struct ea_set es;
1006 unsigned int blks = 2;
1007 int error;
1008
1009 memset(&es, 0, sizeof(struct ea_set));
1010 es.es_er = er;
1011 es.es_el = el;
1012
1013 error = ea_foreach(ip, ea_set_simple, &es);
1014 if (error > 0)
1015 return 0;
1016 if (error)
1017 return error;
1018
1019 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1020 blks++;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001021 if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1022 blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023
1024 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1025}
1026
1027static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1028 struct gfs2_ea_location *el)
1029{
1030 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1031 el->el_prev = GFS2_EA2NEXT(el->el_prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001032 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1034 }
1035
1036 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1037}
1038
1039int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1040{
1041 struct gfs2_ea_location el;
1042 int error;
1043
1044 if (!ip->i_di.di_eattr) {
1045 if (er->er_flags & XATTR_REPLACE)
1046 return -ENODATA;
1047 return ea_init(ip, er);
1048 }
1049
1050 error = gfs2_ea_find(ip, er, &el);
1051 if (error)
1052 return error;
1053
1054 if (el.el_ea) {
1055 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1056 brelse(el.el_bh);
1057 return -EPERM;
1058 }
1059
1060 error = -EEXIST;
1061 if (!(er->er_flags & XATTR_CREATE)) {
1062 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1063 error = ea_set_i(ip, er, &el);
1064 if (!error && unstuffed)
1065 ea_set_remove_unstuffed(ip, &el);
1066 }
1067
1068 brelse(el.el_bh);
1069 } else {
1070 error = -ENODATA;
1071 if (!(er->er_flags & XATTR_REPLACE))
1072 error = ea_set_i(ip, er, NULL);
1073 }
1074
1075 return error;
1076}
1077
1078int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1079{
1080 struct gfs2_holder i_gh;
1081 int error;
1082
Steven Whitehousecca195c2006-09-05 13:15:18 -04001083 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 return -EINVAL;
1085 if (!er->er_data || !er->er_data_len) {
1086 er->er_data = NULL;
1087 er->er_data_len = 0;
1088 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001089 error = ea_check_size(GFS2_SB(&ip->i_inode), er);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 if (error)
1091 return error;
1092
1093 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1094 if (error)
1095 return error;
1096
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001097 if (IS_IMMUTABLE(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098 error = -EPERM;
1099 else
1100 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1101
1102 gfs2_glock_dq_uninit(&i_gh);
1103
1104 return error;
1105}
1106
1107static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1108{
1109 struct gfs2_ea_header *ea = el->el_ea;
1110 struct gfs2_ea_header *prev = el->el_prev;
1111 struct buffer_head *dibh;
1112 int error;
1113
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001114 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 if (error)
1116 return error;
1117
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001118 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119
1120 if (prev) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001121 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122
1123 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1124 prev->ea_rec_len = cpu_to_be32(len);
1125
1126 if (GFS2_EA_IS_LAST(ea))
1127 prev->ea_flags |= GFS2_EAFLAG_LAST;
1128 } else
1129 ea->ea_type = GFS2_EATYPE_UNUSED;
1130
1131 error = gfs2_meta_inode_buffer(ip, &dibh);
1132 if (!error) {
1133 ip->i_di.di_ctime = get_seconds();
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001134 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001135 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 brelse(dibh);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001137 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001139 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140
1141 return error;
1142}
1143
1144int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1145{
1146 struct gfs2_ea_location el;
1147 int error;
1148
1149 if (!ip->i_di.di_eattr)
1150 return -ENODATA;
1151
1152 error = gfs2_ea_find(ip, er, &el);
1153 if (error)
1154 return error;
1155 if (!el.el_ea)
1156 return -ENODATA;
1157
1158 if (GFS2_EA_IS_STUFFED(el.el_ea))
1159 error = ea_remove_stuffed(ip, &el);
1160 else
1161 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1162 0);
1163
1164 brelse(el.el_bh);
1165
1166 return error;
1167}
1168
1169/**
1170 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1171 * @ip: pointer to the inode of the target file
1172 * @er: request information
1173 *
1174 * Returns: errno
1175 */
1176
1177int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1178{
1179 struct gfs2_holder i_gh;
1180 int error;
1181
1182 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1183 return -EINVAL;
1184
1185 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1186 if (error)
1187 return error;
1188
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001189 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190 error = -EPERM;
1191 else
1192 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1193
1194 gfs2_glock_dq_uninit(&i_gh);
1195
1196 return error;
1197}
1198
1199static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1200 struct gfs2_ea_header *ea, char *data)
1201{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001202 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 struct buffer_head **bh;
1204 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001205 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -04001206 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001207 unsigned int x;
1208 int error;
1209
1210 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1211 if (!bh)
1212 return -ENOMEM;
1213
1214 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1215 if (error)
1216 goto out;
1217
1218 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001219 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
1220 bh + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001221 if (error) {
1222 while (x--)
1223 brelse(bh[x]);
1224 goto fail;
1225 }
1226 dataptrs++;
1227 }
1228
1229 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001230 error = gfs2_meta_wait(sdp, bh[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001231 if (error) {
1232 for (; x < nptrs; x++)
1233 brelse(bh[x]);
1234 goto fail;
1235 }
1236 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1237 for (; x < nptrs; x++)
1238 brelse(bh[x]);
1239 error = -EIO;
1240 goto fail;
1241 }
1242
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001243 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244
Steven Whitehousecca195c2006-09-05 13:15:18 -04001245 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001246 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1247
1248 amount -= sdp->sd_jbsize;
1249 data += sdp->sd_jbsize;
1250
1251 brelse(bh[x]);
1252 }
1253
Steven Whitehousea91ea692006-09-04 12:04:26 -04001254out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256 return error;
1257
Steven Whitehousea91ea692006-09-04 12:04:26 -04001258fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001259 gfs2_trans_end(sdp);
1260 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001261 return error;
1262}
1263
1264int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1265 struct iattr *attr, char *data)
1266{
1267 struct buffer_head *dibh;
1268 int error;
1269
1270 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001271 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272 if (error)
1273 return error;
1274
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001275 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
Steven Whitehousecca195c2006-09-05 13:15:18 -04001276 memcpy(GFS2_EA2DATA(el->el_ea), data,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277 GFS2_EA_DATA_LEN(el->el_ea));
1278 } else
1279 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1280
1281 if (error)
1282 return error;
1283
1284 error = gfs2_meta_inode_buffer(ip, &dibh);
1285 if (!error) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001286 error = inode_setattr(&ip->i_inode, attr);
1287 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001288 gfs2_inode_attr_out(ip);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001289 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001290 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001291 brelse(dibh);
1292 }
1293
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001294 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295
1296 return error;
1297}
1298
1299static int ea_dealloc_indirect(struct gfs2_inode *ip)
1300{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001301 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001302 struct gfs2_rgrp_list rlist;
1303 struct buffer_head *indbh, *dibh;
Al Virob44b84d2006-10-14 10:46:30 -04001304 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001305 unsigned int rg_blocks = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001306 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307 unsigned int blen = 0;
1308 unsigned int blks = 0;
1309 unsigned int x;
1310 int error;
1311
1312 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1313
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001314 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315 if (error)
1316 return error;
1317
1318 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1319 error = -EIO;
1320 goto out;
1321 }
1322
Al Virob44b84d2006-10-14 10:46:30 -04001323 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001324 end = eablk + sdp->sd_inptrs;
1325
1326 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001327 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001328
1329 if (!*eablk)
1330 break;
1331 bn = be64_to_cpu(*eablk);
1332
1333 if (bstart + blen == bn)
1334 blen++;
1335 else {
1336 if (bstart)
1337 gfs2_rlist_add(sdp, &rlist, bstart);
1338 bstart = bn;
1339 blen = 1;
1340 }
1341 blks++;
1342 }
1343 if (bstart)
1344 gfs2_rlist_add(sdp, &rlist, bstart);
1345 else
1346 goto out;
1347
1348 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1349
1350 for (x = 0; x < rlist.rl_rgrps; x++) {
1351 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001352 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353 rg_blocks += rgd->rd_ri.ri_length;
1354 }
1355
1356 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1357 if (error)
1358 goto out_rlist_free;
1359
Steven Whitehousecca195c2006-09-05 13:15:18 -04001360 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1361 RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001362 if (error)
1363 goto out_gunlock;
1364
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001365 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001366
Al Virob44b84d2006-10-14 10:46:30 -04001367 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001368 bstart = 0;
1369 blen = 0;
1370
1371 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001372 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373
1374 if (!*eablk)
1375 break;
1376 bn = be64_to_cpu(*eablk);
1377
1378 if (bstart + blen == bn)
1379 blen++;
1380 else {
1381 if (bstart)
1382 gfs2_free_meta(ip, bstart, blen);
1383 bstart = bn;
1384 blen = 1;
1385 }
1386
1387 *eablk = 0;
1388 if (!ip->i_di.di_blocks)
1389 gfs2_consist_inode(ip);
1390 ip->i_di.di_blocks--;
1391 }
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);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001418 struct gfs2_alloc *al = &ip->i_alloc;
1419 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;
1442 if (!ip->i_di.di_blocks)
1443 gfs2_consist_inode(ip);
1444 ip->i_di.di_blocks--;
1445
1446 error = gfs2_meta_inode_buffer(ip, &dibh);
1447 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001448 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001449 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001450 brelse(dibh);
1451 }
1452
1453 gfs2_trans_end(sdp);
1454
Steven Whitehousea91ea692006-09-04 12:04:26 -04001455out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456 gfs2_glock_dq_uninit(&al->al_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001457 return error;
1458}
1459
1460/**
1461 * gfs2_ea_dealloc - deallocate the extended attribute fork
1462 * @ip: the inode
1463 *
1464 * Returns: errno
1465 */
1466
1467int gfs2_ea_dealloc(struct gfs2_inode *ip)
1468{
1469 struct gfs2_alloc *al;
1470 int error;
1471
1472 al = gfs2_alloc_get(ip);
1473
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