blob: 523c0de0d805562e2d2dbd0d412af9ba807518db [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016#include <asm/uaccess.h>
17
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "acl.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010021#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "glock.h"
23#include "inode.h"
24#include "meta_io.h"
25#include "quota.h"
26#include "rgrp.h"
27#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
30/**
31 * ea_calc_size - returns the acutal number of bytes the request will take up
32 * (not counting any unstuffed data blocks)
33 * @sdp:
34 * @er:
35 * @size:
36 *
37 * Returns: 1 if the EA should be stuffed
38 */
39
Steven Whitehouse40b78a32009-08-26 18:41:32 +010040static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize,
David Teiglandb3b94fa2006-01-16 16:50:04 +000041 unsigned int *size)
42{
Steven Whitehouse40b78a32009-08-26 18:41:32 +010043 unsigned int jbsize = sdp->sd_jbsize;
44
45 /* Stuffed */
46 *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + dsize, 8);
47
48 if (*size <= jbsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +000049 return 1;
50
Steven Whitehouse40b78a32009-08-26 18:41:32 +010051 /* Unstuffed */
52 *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize +
53 (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8);
David Teiglandb3b94fa2006-01-16 16:50:04 +000054
55 return 0;
56}
57
Steven Whitehouse40b78a32009-08-26 18:41:32 +010058static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +000059{
60 unsigned int size;
61
Steven Whitehouse40b78a32009-08-26 18:41:32 +010062 if (dsize > GFS2_EA_MAX_DATA_LEN)
David Teiglandb3b94fa2006-01-16 16:50:04 +000063 return -ERANGE;
64
Steven Whitehouse40b78a32009-08-26 18:41:32 +010065 ea_calc_size(sdp, nsize, dsize, &size);
David Teiglandb3b94fa2006-01-16 16:50:04 +000066
67 /* This can only happen with 512 byte blocks */
68 if (size > sdp->sd_jbsize)
69 return -ERANGE;
70
71 return 0;
72}
73
Steven Whitehousecca195c2006-09-05 13:15:18 -040074typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 struct gfs2_ea_header *ea,
Steven Whitehousecca195c2006-09-05 13:15:18 -040076 struct gfs2_ea_header *prev, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +000077
78static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
79 ea_call_t ea_call, void *data)
80{
81 struct gfs2_ea_header *ea, *prev = NULL;
82 int error = 0;
83
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040084 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 return -EIO;
86
87 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
88 if (!GFS2_EA_REC_LEN(ea))
89 goto fail;
Steven Whitehousecca195c2006-09-05 13:15:18 -040090 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
91 bh->b_data + bh->b_size))
David Teiglandb3b94fa2006-01-16 16:50:04 +000092 goto fail;
93 if (!GFS2_EATYPE_VALID(ea->ea_type))
94 goto fail;
95
96 error = ea_call(ip, bh, ea, prev, data);
97 if (error)
98 return error;
99
100 if (GFS2_EA_IS_LAST(ea)) {
101 if ((char *)GFS2_EA2NEXT(ea) !=
102 bh->b_data + bh->b_size)
103 goto fail;
104 break;
105 }
106 }
107
108 return error;
109
Steven Whitehousea91ea692006-09-04 12:04:26 -0400110fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111 gfs2_consist_inode(ip);
112 return -EIO;
113}
114
115static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
116{
117 struct buffer_head *bh, *eabh;
Al Virob44b84d2006-10-14 10:46:30 -0400118 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119 int error;
120
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000121 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 if (error)
123 return error;
124
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000125 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 error = ea_foreach_i(ip, bh, ea_call, data);
127 goto out;
128 }
129
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400130 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 error = -EIO;
132 goto out;
133 }
134
Al Virob44b84d2006-10-14 10:46:30 -0400135 eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400136 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137
138 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400139 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140
141 if (!*eablk)
142 break;
143 bn = be64_to_cpu(*eablk);
144
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400145 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 if (error)
147 break;
148 error = ea_foreach_i(ip, eabh, ea_call, data);
149 brelse(eabh);
150 if (error)
151 break;
152 }
Steven Whitehousea91ea692006-09-04 12:04:26 -0400153out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000155 return error;
156}
157
158struct ea_find {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100159 int type;
160 const char *name;
161 size_t namel;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 struct gfs2_ea_location *ef_el;
163};
164
165static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
166 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
167 void *private)
168{
169 struct ea_find *ef = private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170
171 if (ea->ea_type == GFS2_EATYPE_UNUSED)
172 return 0;
173
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100174 if (ea->ea_type == ef->type) {
175 if (ea->ea_name_len == ef->namel &&
176 !memcmp(GFS2_EA2NAME(ea), ef->name, ea->ea_name_len)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 struct gfs2_ea_location *el = ef->ef_el;
178 get_bh(bh);
179 el->el_bh = bh;
180 el->el_ea = ea;
181 el->el_prev = prev;
182 return 1;
183 }
184 }
185
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186 return 0;
187}
188
Steven Whitehouse479c4272009-10-02 12:00:00 +0100189static int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name,
190 struct gfs2_ea_location *el)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191{
192 struct ea_find ef;
193 int error;
194
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100195 ef.type = type;
196 ef.name = name;
197 ef.namel = strlen(name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198 ef.ef_el = el;
199
200 memset(el, 0, sizeof(struct gfs2_ea_location));
201
202 error = ea_foreach(ip, ea_find_i, &ef);
203 if (error > 0)
204 return 0;
205
206 return error;
207}
208
209/**
210 * ea_dealloc_unstuffed -
211 * @ip:
212 * @bh:
213 * @ea:
214 * @prev:
215 * @private:
216 *
217 * Take advantage of the fact that all unstuffed blocks are
218 * allocated from the same RG. But watch, this may not always
219 * be true.
220 *
221 * Returns: errno
222 */
223
224static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
225 struct gfs2_ea_header *ea,
226 struct gfs2_ea_header *prev, void *private)
227{
228 int *leave = private;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400229 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 struct gfs2_rgrpd *rgd;
231 struct gfs2_holder rg_gh;
232 struct buffer_head *dibh;
Al Virob44b84d2006-10-14 10:46:30 -0400233 __be64 *dataptrs;
234 u64 bn = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -0400235 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236 unsigned int blen = 0;
237 unsigned int blks = 0;
238 unsigned int x;
239 int error;
240
Bob Peterson5e2f7d62012-04-04 22:11:16 -0400241 error = gfs2_rindex_update(sdp);
242 if (error)
243 return error;
244
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 if (GFS2_EA_IS_STUFFED(ea))
246 return 0;
247
248 dataptrs = GFS2_EA2DATAPTRS(ea);
Steven Whitehousecca195c2006-09-05 13:15:18 -0400249 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 if (*dataptrs) {
251 blks++;
252 bn = be64_to_cpu(*dataptrs);
253 }
Steven Whitehousecca195c2006-09-05 13:15:18 -0400254 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255 if (!blks)
256 return 0;
257
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000258 rgd = gfs2_blk2rgrpd(sdp, bn, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259 if (!rgd) {
260 gfs2_consist_inode(ip);
261 return -EIO;
262 }
263
264 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
265 if (error)
266 return error;
267
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100268 error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
Steven Whitehousecca195c2006-09-05 13:15:18 -0400269 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270 if (error)
271 goto out_gunlock;
272
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000273 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274
275 dataptrs = GFS2_EA2DATAPTRS(ea);
276 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
277 if (!*dataptrs)
278 break;
279 bn = be64_to_cpu(*dataptrs);
280
281 if (bstart + blen == bn)
282 blen++;
283 else {
284 if (bstart)
285 gfs2_free_meta(ip, bstart, blen);
286 bstart = bn;
287 blen = 1;
288 }
289
290 *dataptrs = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000291 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 }
293 if (bstart)
294 gfs2_free_meta(ip, bstart, blen);
295
296 if (prev && !leave) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400297 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298
299 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
300 prev->ea_rec_len = cpu_to_be32(len);
301
302 if (GFS2_EA_IS_LAST(ea))
303 prev->ea_flags |= GFS2_EAFLAG_LAST;
304 } else {
305 ea->ea_type = GFS2_EATYPE_UNUSED;
306 ea->ea_num_ptrs = 0;
307 }
308
309 error = gfs2_meta_inode_buffer(ip, &dibh);
310 if (!error) {
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100311 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000312 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500313 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314 brelse(dibh);
315 }
316
317 gfs2_trans_end(sdp);
318
Steven Whitehousea91ea692006-09-04 12:04:26 -0400319out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000320 gfs2_glock_dq_uninit(&rg_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 return error;
322}
323
324static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
325 struct gfs2_ea_header *ea,
326 struct gfs2_ea_header *prev, int leave)
327{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 int error;
329
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
331 if (error)
332 goto out_alloc;
333
Steven Whitehousecca195c2006-09-05 13:15:18 -0400334 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336 gfs2_quota_unhold(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400337out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338 return error;
339}
340
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341struct ea_list {
342 struct gfs2_ea_request *ei_er;
343 unsigned int ei_size;
344};
345
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100346static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header *ea)
347{
348 switch (ea->ea_type) {
349 case GFS2_EATYPE_USR:
350 return 5 + ea->ea_name_len + 1;
351 case GFS2_EATYPE_SYS:
352 return 7 + ea->ea_name_len + 1;
353 case GFS2_EATYPE_SECURITY:
354 return 9 + ea->ea_name_len + 1;
355 default:
356 return 0;
357 }
358}
359
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
361 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
362 void *private)
363{
364 struct ea_list *ei = private;
365 struct gfs2_ea_request *er = ei->ei_er;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400366 unsigned int ea_size = gfs2_ea_strlen(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367
368 if (ea->ea_type == GFS2_EATYPE_UNUSED)
369 return 0;
370
371 if (er->er_data_len) {
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400372 char *prefix = NULL;
373 unsigned int l = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 char c = 0;
375
376 if (ei->ei_size + ea_size > er->er_data_len)
377 return -ERANGE;
378
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400379 switch (ea->ea_type) {
380 case GFS2_EATYPE_USR:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381 prefix = "user.";
382 l = 5;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400383 break;
384 case GFS2_EATYPE_SYS:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 prefix = "system.";
386 l = 7;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400387 break;
388 case GFS2_EATYPE_SECURITY:
389 prefix = "security.";
390 l = 9;
391 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392 }
393
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400394 BUG_ON(l == 0);
395
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400396 memcpy(er->er_data + ei->ei_size, prefix, l);
397 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398 ea->ea_name_len);
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400399 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400 }
401
402 ei->ei_size += ea_size;
403
404 return 0;
405}
406
407/**
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100408 * gfs2_listxattr - List gfs2 extended attributes
409 * @dentry: The dentry whose inode we are interested in
410 * @buffer: The buffer to write the results
411 * @size: The size of the buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 *
413 * Returns: actual size of data on success, -errno on error
414 */
415
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100416ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100418 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
419 struct gfs2_ea_request er;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 struct gfs2_holder i_gh;
421 int error;
422
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100423 memset(&er, 0, sizeof(struct gfs2_ea_request));
424 if (size) {
425 er.er_data = buffer;
426 er.er_data_len = size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 }
428
Steven Whitehousecca195c2006-09-05 13:15:18 -0400429 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430 if (error)
431 return error;
432
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000433 if (ip->i_eattr) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100434 struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435
436 error = ea_foreach(ip, ea_list_i, &ei);
437 if (!error)
438 error = ei.ei_size;
439 }
440
441 gfs2_glock_dq_uninit(&i_gh);
442
443 return error;
444}
445
446/**
447 * ea_get_unstuffed - actually copies the unstuffed data into the
448 * request buffer
Steven Whitehousecca195c2006-09-05 13:15:18 -0400449 * @ip: The GFS2 inode
450 * @ea: The extended attribute header structure
451 * @data: The data to be copied
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452 *
453 * Returns: errno
454 */
455
456static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
457 char *data)
458{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400459 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 struct buffer_head **bh;
461 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500462 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -0400463 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464 unsigned int x;
465 int error = 0;
466
Josef Bacik16c5f062008-04-09 09:33:41 -0400467 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468 if (!bh)
469 return -ENOMEM;
470
471 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400472 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
473 bh + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474 if (error) {
475 while (x--)
476 brelse(bh[x]);
477 goto out;
478 }
479 dataptrs++;
480 }
481
482 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400483 error = gfs2_meta_wait(sdp, bh[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484 if (error) {
485 for (; x < nptrs; x++)
486 brelse(bh[x]);
487 goto out;
488 }
489 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
490 for (; x < nptrs; x++)
491 brelse(bh[x]);
492 error = -EIO;
493 goto out;
494 }
495
Steven Whitehousecca195c2006-09-05 13:15:18 -0400496 memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
498
499 amount -= sdp->sd_jbsize;
500 data += sdp->sd_jbsize;
501
502 brelse(bh[x]);
503 }
504
Steven Whitehousea91ea692006-09-04 12:04:26 -0400505out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000507 return error;
508}
509
Steven Whitehouse479c4272009-10-02 12:00:00 +0100510static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
511 char *data, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100513 int ret;
514 size_t len = GFS2_EA_DATA_LEN(el->el_ea);
515 if (len > size)
516 return -ERANGE;
517
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100519 memcpy(data, GFS2_EA2DATA(el->el_ea), len);
520 return len;
521 }
522 ret = ea_get_unstuffed(ip, el->el_ea, data);
523 if (ret < 0)
524 return ret;
525 return len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526}
527
Steven Whitehouse479c4272009-10-02 12:00:00 +0100528int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
529{
530 struct gfs2_ea_location el;
531 int error;
532 int len;
533 char *data;
534
535 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
536 if (error)
537 return error;
538 if (!el.el_ea)
539 goto out;
540 if (!GFS2_EA_DATA_LEN(el.el_ea))
541 goto out;
542
543 len = GFS2_EA_DATA_LEN(el.el_ea);
544 data = kmalloc(len, GFP_NOFS);
545 error = -ENOMEM;
546 if (data == NULL)
547 goto out;
548
549 error = gfs2_ea_get_copy(ip, &el, data, len);
Steven Whitehouse114b80c2011-11-09 12:54:43 +0000550 if (error < 0)
551 kfree(data);
552 else
553 *ppdata = data;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100554out:
555 brelse(el.el_bh);
556 return error;
557}
558
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559/**
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100560 * gfs2_xattr_get - Get a GFS2 extended attribute
561 * @inode: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100562 * @name: The name of the extended attribute
563 * @buffer: The buffer to write the result into
564 * @size: The size of the buffer
Christoph Hellwig431547b2009-11-13 09:52:56 +0000565 * @type: The type of extended attribute
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 *
567 * Returns: actual size of data on success, -errno on error
568 */
Christoph Hellwig431547b2009-11-13 09:52:56 +0000569static int gfs2_xattr_get(struct dentry *dentry, const char *name,
570 void *buffer, size_t size, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000572 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573 struct gfs2_ea_location el;
574 int error;
575
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000576 if (!ip->i_eattr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 return -ENODATA;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100578 if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
579 return -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100581 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 if (error)
583 return error;
584 if (!el.el_ea)
585 return -ENODATA;
Steven Whitehouse86d00632009-09-14 09:50:57 +0100586 if (size)
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100587 error = gfs2_ea_get_copy(ip, &el, buffer, size);
588 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 error = GFS2_EA_DATA_LEN(el.el_ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590 brelse(el.el_bh);
591
592 return error;
593}
594
595/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 * ea_alloc_blk - allocates a new block for extended attributes.
597 * @ip: A pointer to the inode that's getting extended attributes
Steven Whitehousecca195c2006-09-05 13:15:18 -0400598 * @bhp: Pointer to pointer to a struct buffer_head
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599 *
600 * Returns: errno
601 */
602
603static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
604{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400605 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 struct gfs2_ea_header *ea;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000607 unsigned int n = 1;
Steven Whitehousecd915492006-09-04 12:49:07 -0400608 u64 block;
Steven Whitehouse09010972009-05-20 10:48:47 +0100609 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610
Bob Peterson6e87ed02011-11-18 10:58:32 -0500611 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100612 if (error)
613 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000614 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615 *bhp = gfs2_meta_new(ip->i_gl, block);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000616 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
618 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
619
620 ea = GFS2_EA_BH2FIRST(*bhp);
621 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
622 ea->ea_type = GFS2_EATYPE_UNUSED;
623 ea->ea_flags = GFS2_EAFLAG_LAST;
624 ea->ea_num_ptrs = 0;
625
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000626 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627
628 return 0;
629}
630
631/**
632 * ea_write - writes the request info to an ea, creating new blocks if
633 * necessary
Steven Whitehousecca195c2006-09-05 13:15:18 -0400634 * @ip: inode that is being modified
635 * @ea: the location of the new ea in a block
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 * @er: the write request
637 *
638 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
639 *
640 * returns : errno
641 */
642
643static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
644 struct gfs2_ea_request *er)
645{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400646 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse09010972009-05-20 10:48:47 +0100647 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648
649 ea->ea_data_len = cpu_to_be32(er->er_data_len);
650 ea->ea_name_len = er->er_name_len;
651 ea->ea_type = er->er_type;
652 ea->__pad = 0;
653
654 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
655
656 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
657 ea->ea_num_ptrs = 0;
658 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
659 } else {
Al Virob44b84d2006-10-14 10:46:30 -0400660 __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 const char *data = er->er_data;
662 unsigned int data_len = er->er_data_len;
663 unsigned int copy;
664 unsigned int x;
665
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500666 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 for (x = 0; x < ea->ea_num_ptrs; x++) {
668 struct buffer_head *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400669 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 int mh_size = sizeof(struct gfs2_meta_header);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000671 unsigned int n = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672
Bob Peterson6e87ed02011-11-18 10:58:32 -0500673 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100674 if (error)
675 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000676 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 bh = gfs2_meta_new(ip->i_gl, block);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000678 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
680
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000681 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682
Steven Whitehousecca195c2006-09-05 13:15:18 -0400683 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
684 data_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 memcpy(bh->b_data + mh_size, data, copy);
686 if (copy < sdp->sd_jbsize)
687 memset(bh->b_data + mh_size + copy, 0,
688 sdp->sd_jbsize - copy);
689
Steven Whitehousecca195c2006-09-05 13:15:18 -0400690 *dataptr++ = cpu_to_be64(bh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 data += copy;
692 data_len -= copy;
693
694 brelse(bh);
695 }
696
697 gfs2_assert_withdraw(sdp, !data_len);
698 }
699
700 return 0;
701}
702
703typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400704 struct gfs2_ea_request *er, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705
706static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
707 unsigned int blks,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400708 ea_skeleton_call_t skeleton_call, void *private)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 struct buffer_head *dibh;
711 int error;
712
Steven Whitehoused82661d2008-03-10 15:34:50 +0000713 error = gfs2_quota_lock_check(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -0400715 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716
Bob Peterson564e12b2011-11-21 13:36:17 -0500717 error = gfs2_inplace_reserve(ip, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 if (error)
719 goto out_gunlock_q;
720
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400721 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
Steven Whitehouse54335b12011-09-01 13:31:59 +0100722 blks + gfs2_rg_blocks(ip) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
724 if (error)
725 goto out_ipres;
726
727 error = skeleton_call(ip, er, private);
728 if (error)
729 goto out_end_trans;
730
731 error = gfs2_meta_inode_buffer(ip, &dibh);
732 if (!error) {
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100733 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000734 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500735 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 brelse(dibh);
737 }
738
Steven Whitehousea91ea692006-09-04 12:04:26 -0400739out_end_trans:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400740 gfs2_trans_end(GFS2_SB(&ip->i_inode));
Steven Whitehousea91ea692006-09-04 12:04:26 -0400741out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400743out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 gfs2_quota_unlock(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745 return error;
746}
747
748static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
749 void *private)
750{
751 struct buffer_head *bh;
752 int error;
753
754 error = ea_alloc_blk(ip, &bh);
755 if (error)
756 return error;
757
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000758 ip->i_eattr = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
760
761 brelse(bh);
762
763 return error;
764}
765
766/**
767 * ea_init - initializes a new eattr block
768 * @ip:
769 * @er:
770 *
771 * Returns: errno
772 */
773
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100774static int ea_init(struct gfs2_inode *ip, int type, const char *name,
775 const void *data, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100777 struct gfs2_ea_request er;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400778 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 unsigned int blks = 1;
780
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100781 er.er_type = type;
782 er.er_name = name;
783 er.er_name_len = strlen(name);
784 er.er_data = (void *)data;
785 er.er_data_len = size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100787 if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
788 blks += DIV_ROUND_UP(er.er_data_len, jbsize);
789
790 return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791}
792
793static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
794{
Steven Whitehousecd915492006-09-04 12:49:07 -0400795 u32 ea_size = GFS2_EA_SIZE(ea);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500796 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
797 ea_size);
Steven Whitehousecd915492006-09-04 12:49:07 -0400798 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
800
801 ea->ea_rec_len = cpu_to_be32(ea_size);
802 ea->ea_flags ^= last;
803
804 new->ea_rec_len = cpu_to_be32(new_size);
805 new->ea_flags = last;
806
807 return new;
808}
809
810static void ea_set_remove_stuffed(struct gfs2_inode *ip,
811 struct gfs2_ea_location *el)
812{
813 struct gfs2_ea_header *ea = el->el_ea;
814 struct gfs2_ea_header *prev = el->el_prev;
Steven Whitehousecd915492006-09-04 12:49:07 -0400815 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000817 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818
819 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
820 ea->ea_type = GFS2_EATYPE_UNUSED;
821 return;
822 } else if (GFS2_EA2NEXT(prev) != ea) {
823 prev = GFS2_EA2NEXT(prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400824 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 }
826
827 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
828 prev->ea_rec_len = cpu_to_be32(len);
829
830 if (GFS2_EA_IS_LAST(ea))
831 prev->ea_flags |= GFS2_EAFLAG_LAST;
832}
833
834struct ea_set {
835 int ea_split;
836
837 struct gfs2_ea_request *es_er;
838 struct gfs2_ea_location *es_el;
839
840 struct buffer_head *es_bh;
841 struct gfs2_ea_header *es_ea;
842};
843
844static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
845 struct gfs2_ea_header *ea, struct ea_set *es)
846{
847 struct gfs2_ea_request *er = es->es_er;
848 struct buffer_head *dibh;
849 int error;
850
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400851 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 if (error)
853 return error;
854
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000855 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856
857 if (es->ea_split)
858 ea = ea_split_ea(ea);
859
860 ea_write(ip, ea, er);
861
862 if (es->es_el)
863 ea_set_remove_stuffed(ip, es->es_el);
864
865 error = gfs2_meta_inode_buffer(ip, &dibh);
866 if (error)
867 goto out;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100868 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000869 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500870 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400872out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400873 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874 return error;
875}
876
877static int ea_set_simple_alloc(struct gfs2_inode *ip,
878 struct gfs2_ea_request *er, void *private)
879{
880 struct ea_set *es = private;
881 struct gfs2_ea_header *ea = es->es_ea;
882 int error;
883
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000884 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885
886 if (es->ea_split)
887 ea = ea_split_ea(ea);
888
889 error = ea_write(ip, ea, er);
890 if (error)
891 return error;
892
893 if (es->es_el)
894 ea_set_remove_stuffed(ip, es->es_el);
895
896 return 0;
897}
898
899static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
900 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
901 void *private)
902{
903 struct ea_set *es = private;
904 unsigned int size;
905 int stuffed;
906 int error;
907
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100908 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
909 es->es_er->er_data_len, &size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910
911 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
912 if (GFS2_EA_REC_LEN(ea) < size)
913 return 0;
914 if (!GFS2_EA_IS_STUFFED(ea)) {
915 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
916 if (error)
917 return error;
918 }
919 es->ea_split = 0;
920 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
921 es->ea_split = 1;
922 else
923 return 0;
924
925 if (stuffed) {
926 error = ea_set_simple_noalloc(ip, bh, ea, es);
927 if (error)
928 return error;
929 } else {
930 unsigned int blks;
931
932 es->es_bh = bh;
933 es->es_ea = ea;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500934 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400935 GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936
937 error = ea_alloc_skeleton(ip, es->es_er, blks,
938 ea_set_simple_alloc, es);
939 if (error)
940 return error;
941 }
942
943 return 1;
944}
945
946static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
947 void *private)
948{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400949 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950 struct buffer_head *indbh, *newbh;
Al Virob44b84d2006-10-14 10:46:30 -0400951 __be64 *eablk;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 int error;
953 int mh_size = sizeof(struct gfs2_meta_header);
954
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000955 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
Al Virob44b84d2006-10-14 10:46:30 -0400956 __be64 *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000958 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400959 &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 if (error)
961 return error;
962
963 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
964 error = -EIO;
965 goto out;
966 }
967
Al Virob44b84d2006-10-14 10:46:30 -0400968 eablk = (__be64 *)(indbh->b_data + mh_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969 end = eablk + sdp->sd_inptrs;
970
971 for (; eablk < end; eablk++)
972 if (!*eablk)
973 break;
974
975 if (eablk == end) {
976 error = -ENOSPC;
977 goto out;
978 }
979
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000980 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -0400982 u64 blk;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000983 unsigned int n = 1;
Bob Peterson6e87ed02011-11-18 10:58:32 -0500984 error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100985 if (error)
986 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000987 gfs2_trans_add_unrevoke(sdp, blk, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 indbh = gfs2_meta_new(ip->i_gl, blk);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000989 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
991 gfs2_buffer_clear_tail(indbh, mh_size);
992
Al Virob44b84d2006-10-14 10:46:30 -0400993 eablk = (__be64 *)(indbh->b_data + mh_size);
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000994 *eablk = cpu_to_be64(ip->i_eattr);
995 ip->i_eattr = blk;
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000996 ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000997 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998
999 eablk++;
1000 }
1001
1002 error = ea_alloc_blk(ip, &newbh);
1003 if (error)
1004 goto out;
1005
Steven Whitehousecd915492006-09-04 12:49:07 -04001006 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1008 brelse(newbh);
1009 if (error)
1010 goto out;
1011
1012 if (private)
Steven Whitehousecca195c2006-09-05 13:15:18 -04001013 ea_set_remove_stuffed(ip, private);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001014
Steven Whitehousea91ea692006-09-04 12:04:26 -04001015out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 return error;
1018}
1019
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001020static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
1021 const void *value, size_t size, struct gfs2_ea_location *el)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001023 struct gfs2_ea_request er;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 struct ea_set es;
1025 unsigned int blks = 2;
1026 int error;
1027
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001028 er.er_type = type;
1029 er.er_name = name;
1030 er.er_data = (void *)value;
1031 er.er_name_len = strlen(name);
1032 er.er_data_len = size;
1033
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 memset(&es, 0, sizeof(struct ea_set));
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001035 es.es_er = &er;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 es.es_el = el;
1037
1038 error = ea_foreach(ip, ea_set_simple, &es);
1039 if (error > 0)
1040 return 0;
1041 if (error)
1042 return error;
1043
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001044 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045 blks++;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001046 if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1047 blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001049 return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050}
1051
1052static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1053 struct gfs2_ea_location *el)
1054{
1055 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1056 el->el_prev = GFS2_EA2NEXT(el->el_prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001057 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1059 }
1060
Steven Whitehouse86d00632009-09-14 09:50:57 +01001061 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062}
1063
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1065{
1066 struct gfs2_ea_header *ea = el->el_ea;
1067 struct gfs2_ea_header *prev = el->el_prev;
1068 struct buffer_head *dibh;
1069 int error;
1070
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001071 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 if (error)
1073 return error;
1074
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001075 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076
1077 if (prev) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001078 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079
1080 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1081 prev->ea_rec_len = cpu_to_be32(len);
1082
1083 if (GFS2_EA_IS_LAST(ea))
1084 prev->ea_flags |= GFS2_EAFLAG_LAST;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001085 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001086 ea->ea_type = GFS2_EATYPE_UNUSED;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001087 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088
1089 error = gfs2_meta_inode_buffer(ip, &dibh);
1090 if (!error) {
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001091 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001092 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001093 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001094 brelse(dibh);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001095 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001097 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098
1099 return error;
1100}
1101
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001102/**
1103 * gfs2_xattr_remove - Remove a GFS2 extended attribute
Christoph Hellwig431547b2009-11-13 09:52:56 +00001104 * @ip: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001105 * @type: The type of the extended attribute
1106 * @name: The name of the extended attribute
1107 *
1108 * This is not called directly by the VFS since we use the (common)
1109 * scheme of making a "set with NULL data" mean a remove request. Note
1110 * that this is different from a set with zero length data.
1111 *
1112 * Returns: 0, or errno on failure
1113 */
1114
Christoph Hellwig431547b2009-11-13 09:52:56 +00001115static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116{
1117 struct gfs2_ea_location el;
1118 int error;
1119
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001120 if (!ip->i_eattr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001121 return -ENODATA;
1122
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001123 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001124 if (error)
1125 return error;
1126 if (!el.el_ea)
1127 return -ENODATA;
1128
1129 if (GFS2_EA_IS_STUFFED(el.el_ea))
1130 error = ea_remove_stuffed(ip, &el);
1131 else
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001132 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133
1134 brelse(el.el_bh);
1135
1136 return error;
1137}
1138
1139/**
Christoph Hellwig431547b2009-11-13 09:52:56 +00001140 * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
1141 * @ip: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001142 * @name: The name of the extended attribute
1143 * @value: The value of the extended attribute (NULL for remove)
1144 * @size: The size of the @value argument
1145 * @flags: Create or Replace
Christoph Hellwig431547b2009-11-13 09:52:56 +00001146 * @type: The type of the extended attribute
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 *
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001148 * See gfs2_xattr_remove() for details of the removal of xattrs.
1149 *
1150 * Returns: 0 or errno on failure
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151 */
1152
Christoph Hellwig431547b2009-11-13 09:52:56 +00001153int __gfs2_xattr_set(struct inode *inode, const char *name,
1154 const void *value, size_t size, int flags, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001156 struct gfs2_inode *ip = GFS2_I(inode);
Christoph Hellwig431547b2009-11-13 09:52:56 +00001157 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001158 struct gfs2_ea_location el;
1159 unsigned int namel = strlen(name);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 int error;
1161
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001162 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1163 return -EPERM;
1164 if (namel > GFS2_EA_MAX_NAME_LEN)
1165 return -ERANGE;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001167 if (value == NULL)
Christoph Hellwig431547b2009-11-13 09:52:56 +00001168 return gfs2_xattr_remove(ip, type, name);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001169
1170 if (ea_check_size(sdp, namel, size))
1171 return -ERANGE;
1172
1173 if (!ip->i_eattr) {
1174 if (flags & XATTR_REPLACE)
1175 return -ENODATA;
1176 return ea_init(ip, type, name, value, size);
1177 }
1178
1179 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 if (error)
1181 return error;
1182
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001183 if (el.el_ea) {
1184 if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
1185 brelse(el.el_bh);
1186 return -EPERM;
1187 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001189 error = -EEXIST;
1190 if (!(flags & XATTR_CREATE)) {
1191 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1192 error = ea_set_i(ip, type, name, value, size, &el);
1193 if (!error && unstuffed)
1194 ea_set_remove_unstuffed(ip, &el);
1195 }
1196
1197 brelse(el.el_bh);
1198 return error;
1199 }
1200
1201 error = -ENODATA;
1202 if (!(flags & XATTR_REPLACE))
1203 error = ea_set_i(ip, type, name, value, size, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204
1205 return error;
1206}
1207
Christoph Hellwig431547b2009-11-13 09:52:56 +00001208static int gfs2_xattr_set(struct dentry *dentry, const char *name,
1209 const void *value, size_t size, int flags, int type)
1210{
1211 return __gfs2_xattr_set(dentry->d_inode, name, value,
1212 size, flags, type);
1213}
1214
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1216 struct gfs2_ea_header *ea, char *data)
1217{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001218 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001219 struct buffer_head **bh;
1220 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001221 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -04001222 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001223 unsigned int x;
1224 int error;
1225
Josef Bacik16c5f062008-04-09 09:33:41 -04001226 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227 if (!bh)
1228 return -ENOMEM;
1229
1230 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1231 if (error)
1232 goto out;
1233
1234 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001235 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
1236 bh + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237 if (error) {
1238 while (x--)
1239 brelse(bh[x]);
1240 goto fail;
1241 }
1242 dataptrs++;
1243 }
1244
1245 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001246 error = gfs2_meta_wait(sdp, bh[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247 if (error) {
1248 for (; x < nptrs; x++)
1249 brelse(bh[x]);
1250 goto fail;
1251 }
1252 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1253 for (; x < nptrs; x++)
1254 brelse(bh[x]);
1255 error = -EIO;
1256 goto fail;
1257 }
1258
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001259 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260
Steven Whitehousecca195c2006-09-05 13:15:18 -04001261 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001262 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1263
1264 amount -= sdp->sd_jbsize;
1265 data += sdp->sd_jbsize;
1266
1267 brelse(bh[x]);
1268 }
1269
Steven Whitehousea91ea692006-09-04 12:04:26 -04001270out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272 return error;
1273
Steven Whitehousea91ea692006-09-04 12:04:26 -04001274fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001275 gfs2_trans_end(sdp);
1276 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277 return error;
1278}
1279
Steven Whitehouse479c4272009-10-02 12:00:00 +01001280int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281{
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001282 struct inode *inode = &ip->i_inode;
1283 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse479c4272009-10-02 12:00:00 +01001284 struct gfs2_ea_location el;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285 int error;
1286
Steven Whitehouse479c4272009-10-02 12:00:00 +01001287 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, GFS2_POSIX_ACL_ACCESS, &el);
1288 if (error)
1289 return error;
1290
1291 if (GFS2_EA_IS_STUFFED(el.el_ea)) {
Steven Whitehousee412bdb2009-12-21 13:55:28 +00001292 error = gfs2_trans_begin(sdp, RES_DINODE + RES_EATTR, 0);
1293 if (error == 0) {
1294 gfs2_trans_add_bh(ip->i_gl, el.el_bh, 1);
1295 memcpy(GFS2_EA2DATA(el.el_ea), data,
1296 GFS2_EA_DATA_LEN(el.el_ea));
1297 }
1298 } else {
Steven Whitehouse479c4272009-10-02 12:00:00 +01001299 error = ea_acl_chmod_unstuffed(ip, el.el_ea, data);
Steven Whitehousee412bdb2009-12-21 13:55:28 +00001300 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301
Steven Whitehousee412bdb2009-12-21 13:55:28 +00001302 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303 if (error)
1304 return error;
1305
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001306 error = gfs2_setattr_simple(inode, attr);
Steven Whitehousee412bdb2009-12-21 13:55:28 +00001307 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001308 return error;
1309}
1310
1311static int ea_dealloc_indirect(struct gfs2_inode *ip)
1312{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001313 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001314 struct gfs2_rgrp_list rlist;
1315 struct buffer_head *indbh, *dibh;
Al Virob44b84d2006-10-14 10:46:30 -04001316 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317 unsigned int rg_blocks = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001318 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001319 unsigned int blen = 0;
1320 unsigned int blks = 0;
1321 unsigned int x;
1322 int error;
1323
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001324 error = gfs2_rindex_update(sdp);
1325 if (error)
1326 return error;
1327
David Teiglandb3b94fa2006-01-16 16:50:04 +00001328 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1329
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001330 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001331 if (error)
1332 return error;
1333
1334 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1335 error = -EIO;
1336 goto out;
1337 }
1338
Al Virob44b84d2006-10-14 10:46:30 -04001339 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340 end = eablk + sdp->sd_inptrs;
1341
1342 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001343 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344
1345 if (!*eablk)
1346 break;
1347 bn = be64_to_cpu(*eablk);
1348
1349 if (bstart + blen == bn)
1350 blen++;
1351 else {
1352 if (bstart)
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001353 gfs2_rlist_add(ip, &rlist, bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 bstart = bn;
1355 blen = 1;
1356 }
1357 blks++;
1358 }
1359 if (bstart)
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001360 gfs2_rlist_add(ip, &rlist, bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001361 else
1362 goto out;
1363
Bob Petersonfe6c9912008-01-28 11:13:02 -06001364 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001365
1366 for (x = 0; x < rlist.rl_rgrps; x++) {
1367 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001368 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001369 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001370 }
1371
1372 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1373 if (error)
1374 goto out_rlist_free;
1375
Steven Whitehousecca195c2006-09-05 13:15:18 -04001376 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1377 RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001378 if (error)
1379 goto out_gunlock;
1380
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001381 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001382
Al Virob44b84d2006-10-14 10:46:30 -04001383 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384 bstart = 0;
1385 blen = 0;
1386
1387 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001388 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001389
1390 if (!*eablk)
1391 break;
1392 bn = be64_to_cpu(*eablk);
1393
1394 if (bstart + blen == bn)
1395 blen++;
1396 else {
1397 if (bstart)
1398 gfs2_free_meta(ip, bstart, blen);
1399 bstart = bn;
1400 blen = 1;
1401 }
1402
1403 *eablk = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001404 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001405 }
1406 if (bstart)
1407 gfs2_free_meta(ip, bstart, blen);
1408
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001409 ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410
1411 error = gfs2_meta_inode_buffer(ip, &dibh);
1412 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001413 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001414 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001415 brelse(dibh);
1416 }
1417
1418 gfs2_trans_end(sdp);
1419
Steven Whitehousea91ea692006-09-04 12:04:26 -04001420out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001421 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001422out_rlist_free:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001423 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001424out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001425 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001426 return error;
1427}
1428
1429static int ea_dealloc_block(struct gfs2_inode *ip)
1430{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001431 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001432 struct gfs2_rgrpd *rgd;
1433 struct buffer_head *dibh;
Bob Peterson564e12b2011-11-21 13:36:17 -05001434 struct gfs2_holder gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001435 int error;
1436
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001437 error = gfs2_rindex_update(sdp);
1438 if (error)
1439 return error;
1440
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001441 rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001442 if (!rgd) {
1443 gfs2_consist_inode(ip);
1444 return -EIO;
1445 }
1446
Bob Peterson564e12b2011-11-21 13:36:17 -05001447 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448 if (error)
1449 return error;
1450
Steven Whitehousecca195c2006-09-05 13:15:18 -04001451 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1452 RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001453 if (error)
1454 goto out_gunlock;
1455
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001456 gfs2_free_meta(ip, ip->i_eattr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001457
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001458 ip->i_eattr = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001459 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001460
1461 error = gfs2_meta_inode_buffer(ip, &dibh);
1462 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001463 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001464 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001465 brelse(dibh);
1466 }
1467
1468 gfs2_trans_end(sdp);
1469
Steven Whitehousea91ea692006-09-04 12:04:26 -04001470out_gunlock:
Bob Peterson564e12b2011-11-21 13:36:17 -05001471 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001472 return error;
1473}
1474
1475/**
1476 * gfs2_ea_dealloc - deallocate the extended attribute fork
1477 * @ip: the inode
1478 *
1479 * Returns: errno
1480 */
1481
1482int gfs2_ea_dealloc(struct gfs2_inode *ip)
1483{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001484 int error;
1485
David Teiglandb3b94fa2006-01-16 16:50:04 +00001486 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1487 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001488 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489
David Teiglandb3b94fa2006-01-16 16:50:04 +00001490 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1491 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001492 goto out_quota;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001493
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001494 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001495 error = ea_dealloc_indirect(ip);
1496 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001497 goto out_quota;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498 }
1499
1500 error = ea_dealloc_block(ip);
1501
Steven Whitehousea91ea692006-09-04 12:04:26 -04001502out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001503 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001504 return error;
1505}
1506
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001507static const struct xattr_handler gfs2_xattr_user_handler = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001508 .prefix = XATTR_USER_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +00001509 .flags = GFS2_EATYPE_USR,
1510 .get = gfs2_xattr_get,
1511 .set = gfs2_xattr_set,
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001512};
1513
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001514static const struct xattr_handler gfs2_xattr_security_handler = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001515 .prefix = XATTR_SECURITY_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +00001516 .flags = GFS2_EATYPE_SECURITY,
1517 .get = gfs2_xattr_get,
1518 .set = gfs2_xattr_set,
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001519};
1520
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001521const struct xattr_handler *gfs2_xattr_handlers[] = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001522 &gfs2_xattr_user_handler,
1523 &gfs2_xattr_security_handler,
1524 &gfs2_xattr_system_handler,
1525 NULL,
1526};
1527