blob: 695304cf01cc672e7a1ce9d5036a1345a5a4f8ae [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
241 if (GFS2_EA_IS_STUFFED(ea))
242 return 0;
243
244 dataptrs = GFS2_EA2DATAPTRS(ea);
Steven Whitehousecca195c2006-09-05 13:15:18 -0400245 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 if (*dataptrs) {
247 blks++;
248 bn = be64_to_cpu(*dataptrs);
249 }
Steven Whitehousecca195c2006-09-05 13:15:18 -0400250 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251 if (!blks)
252 return 0;
253
254 rgd = gfs2_blk2rgrpd(sdp, bn);
255 if (!rgd) {
256 gfs2_consist_inode(ip);
257 return -EIO;
258 }
259
260 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
261 if (error)
262 return error;
263
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100264 error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
Steven Whitehousecca195c2006-09-05 13:15:18 -0400265 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266 if (error)
267 goto out_gunlock;
268
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000269 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270
271 dataptrs = GFS2_EA2DATAPTRS(ea);
272 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
273 if (!*dataptrs)
274 break;
275 bn = be64_to_cpu(*dataptrs);
276
277 if (bstart + blen == bn)
278 blen++;
279 else {
280 if (bstart)
281 gfs2_free_meta(ip, bstart, blen);
282 bstart = bn;
283 blen = 1;
284 }
285
286 *dataptrs = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000287 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 }
289 if (bstart)
290 gfs2_free_meta(ip, bstart, blen);
291
292 if (prev && !leave) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400293 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000294
295 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
296 prev->ea_rec_len = cpu_to_be32(len);
297
298 if (GFS2_EA_IS_LAST(ea))
299 prev->ea_flags |= GFS2_EAFLAG_LAST;
300 } else {
301 ea->ea_type = GFS2_EATYPE_UNUSED;
302 ea->ea_num_ptrs = 0;
303 }
304
305 error = gfs2_meta_inode_buffer(ip, &dibh);
306 if (!error) {
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100307 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000308 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500309 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 brelse(dibh);
311 }
312
313 gfs2_trans_end(sdp);
314
Steven Whitehousea91ea692006-09-04 12:04:26 -0400315out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 gfs2_glock_dq_uninit(&rg_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317 return error;
318}
319
320static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
321 struct gfs2_ea_header *ea,
322 struct gfs2_ea_header *prev, int leave)
323{
324 struct gfs2_alloc *al;
325 int error;
326
327 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300328 if (!al)
329 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330
331 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
332 if (error)
333 goto out_alloc;
334
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400335 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336 if (error)
337 goto out_quota;
338
Steven Whitehousecca195c2006-09-05 13:15:18 -0400339 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340
341 gfs2_glock_dq_uninit(&al->al_ri_gh);
342
Steven Whitehousea91ea692006-09-04 12:04:26 -0400343out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 gfs2_quota_unhold(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400345out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347 return error;
348}
349
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350struct ea_list {
351 struct gfs2_ea_request *ei_er;
352 unsigned int ei_size;
353};
354
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100355static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header *ea)
356{
357 switch (ea->ea_type) {
358 case GFS2_EATYPE_USR:
359 return 5 + ea->ea_name_len + 1;
360 case GFS2_EATYPE_SYS:
361 return 7 + ea->ea_name_len + 1;
362 case GFS2_EATYPE_SECURITY:
363 return 9 + ea->ea_name_len + 1;
364 default:
365 return 0;
366 }
367}
368
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
370 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
371 void *private)
372{
373 struct ea_list *ei = private;
374 struct gfs2_ea_request *er = ei->ei_er;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400375 unsigned int ea_size = gfs2_ea_strlen(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000376
377 if (ea->ea_type == GFS2_EATYPE_UNUSED)
378 return 0;
379
380 if (er->er_data_len) {
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400381 char *prefix = NULL;
382 unsigned int l = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000383 char c = 0;
384
385 if (ei->ei_size + ea_size > er->er_data_len)
386 return -ERANGE;
387
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400388 switch (ea->ea_type) {
389 case GFS2_EATYPE_USR:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 prefix = "user.";
391 l = 5;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400392 break;
393 case GFS2_EATYPE_SYS:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 prefix = "system.";
395 l = 7;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400396 break;
397 case GFS2_EATYPE_SECURITY:
398 prefix = "security.";
399 l = 9;
400 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401 }
402
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400403 BUG_ON(l == 0);
404
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400405 memcpy(er->er_data + ei->ei_size, prefix, l);
406 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 ea->ea_name_len);
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400408 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409 }
410
411 ei->ei_size += ea_size;
412
413 return 0;
414}
415
416/**
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100417 * gfs2_listxattr - List gfs2 extended attributes
418 * @dentry: The dentry whose inode we are interested in
419 * @buffer: The buffer to write the results
420 * @size: The size of the buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 *
422 * Returns: actual size of data on success, -errno on error
423 */
424
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100425ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100427 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
428 struct gfs2_ea_request er;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429 struct gfs2_holder i_gh;
430 int error;
431
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100432 memset(&er, 0, sizeof(struct gfs2_ea_request));
433 if (size) {
434 er.er_data = buffer;
435 er.er_data_len = size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436 }
437
Steven Whitehousecca195c2006-09-05 13:15:18 -0400438 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 if (error)
440 return error;
441
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000442 if (ip->i_eattr) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100443 struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444
445 error = ea_foreach(ip, ea_list_i, &ei);
446 if (!error)
447 error = ei.ei_size;
448 }
449
450 gfs2_glock_dq_uninit(&i_gh);
451
452 return error;
453}
454
455/**
456 * ea_get_unstuffed - actually copies the unstuffed data into the
457 * request buffer
Steven Whitehousecca195c2006-09-05 13:15:18 -0400458 * @ip: The GFS2 inode
459 * @ea: The extended attribute header structure
460 * @data: The data to be copied
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461 *
462 * Returns: errno
463 */
464
465static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
466 char *data)
467{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400468 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 struct buffer_head **bh;
470 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500471 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -0400472 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473 unsigned int x;
474 int error = 0;
475
Josef Bacik16c5f062008-04-09 09:33:41 -0400476 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477 if (!bh)
478 return -ENOMEM;
479
480 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400481 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
482 bh + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483 if (error) {
484 while (x--)
485 brelse(bh[x]);
486 goto out;
487 }
488 dataptrs++;
489 }
490
491 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400492 error = gfs2_meta_wait(sdp, bh[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 if (error) {
494 for (; x < nptrs; x++)
495 brelse(bh[x]);
496 goto out;
497 }
498 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
499 for (; x < nptrs; x++)
500 brelse(bh[x]);
501 error = -EIO;
502 goto out;
503 }
504
Steven Whitehousecca195c2006-09-05 13:15:18 -0400505 memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000506 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
507
508 amount -= sdp->sd_jbsize;
509 data += sdp->sd_jbsize;
510
511 brelse(bh[x]);
512 }
513
Steven Whitehousea91ea692006-09-04 12:04:26 -0400514out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 return error;
517}
518
Steven Whitehouse479c4272009-10-02 12:00:00 +0100519static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
520 char *data, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100522 int ret;
523 size_t len = GFS2_EA_DATA_LEN(el->el_ea);
524 if (len > size)
525 return -ERANGE;
526
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100528 memcpy(data, GFS2_EA2DATA(el->el_ea), len);
529 return len;
530 }
531 ret = ea_get_unstuffed(ip, el->el_ea, data);
532 if (ret < 0)
533 return ret;
534 return len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535}
536
Steven Whitehouse479c4272009-10-02 12:00:00 +0100537int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
538{
539 struct gfs2_ea_location el;
540 int error;
541 int len;
542 char *data;
543
544 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
545 if (error)
546 return error;
547 if (!el.el_ea)
548 goto out;
549 if (!GFS2_EA_DATA_LEN(el.el_ea))
550 goto out;
551
552 len = GFS2_EA_DATA_LEN(el.el_ea);
553 data = kmalloc(len, GFP_NOFS);
554 error = -ENOMEM;
555 if (data == NULL)
556 goto out;
557
558 error = gfs2_ea_get_copy(ip, &el, data, len);
559 if (error == 0)
560 error = len;
561 *ppdata = data;
562out:
563 brelse(el.el_bh);
564 return error;
565}
566
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567/**
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100568 * gfs2_xattr_get - Get a GFS2 extended attribute
569 * @inode: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100570 * @name: The name of the extended attribute
571 * @buffer: The buffer to write the result into
572 * @size: The size of the buffer
Christoph Hellwig431547b2009-11-13 09:52:56 +0000573 * @type: The type of extended attribute
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 *
575 * Returns: actual size of data on success, -errno on error
576 */
Christoph Hellwig431547b2009-11-13 09:52:56 +0000577static int gfs2_xattr_get(struct dentry *dentry, const char *name,
578 void *buffer, size_t size, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000580 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 struct gfs2_ea_location el;
582 int error;
583
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000584 if (!ip->i_eattr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 return -ENODATA;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100586 if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
587 return -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100589 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590 if (error)
591 return error;
592 if (!el.el_ea)
593 return -ENODATA;
Steven Whitehouse86d00632009-09-14 09:50:57 +0100594 if (size)
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100595 error = gfs2_ea_get_copy(ip, &el, buffer, size);
596 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 error = GFS2_EA_DATA_LEN(el.el_ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 brelse(el.el_bh);
599
600 return error;
601}
602
603/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 * ea_alloc_blk - allocates a new block for extended attributes.
605 * @ip: A pointer to the inode that's getting extended attributes
Steven Whitehousecca195c2006-09-05 13:15:18 -0400606 * @bhp: Pointer to pointer to a struct buffer_head
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 *
608 * Returns: errno
609 */
610
611static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
612{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400613 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614 struct gfs2_ea_header *ea;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000615 unsigned int n = 1;
Steven Whitehousecd915492006-09-04 12:49:07 -0400616 u64 block;
Steven Whitehouse09010972009-05-20 10:48:47 +0100617 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618
Steven Whitehouse09010972009-05-20 10:48:47 +0100619 error = gfs2_alloc_block(ip, &block, &n);
620 if (error)
621 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000622 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623 *bhp = gfs2_meta_new(ip->i_gl, block);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000624 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
626 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
627
628 ea = GFS2_EA_BH2FIRST(*bhp);
629 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
630 ea->ea_type = GFS2_EATYPE_UNUSED;
631 ea->ea_flags = GFS2_EAFLAG_LAST;
632 ea->ea_num_ptrs = 0;
633
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000634 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635
636 return 0;
637}
638
639/**
640 * ea_write - writes the request info to an ea, creating new blocks if
641 * necessary
Steven Whitehousecca195c2006-09-05 13:15:18 -0400642 * @ip: inode that is being modified
643 * @ea: the location of the new ea in a block
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644 * @er: the write request
645 *
646 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
647 *
648 * returns : errno
649 */
650
651static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
652 struct gfs2_ea_request *er)
653{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400654 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse09010972009-05-20 10:48:47 +0100655 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656
657 ea->ea_data_len = cpu_to_be32(er->er_data_len);
658 ea->ea_name_len = er->er_name_len;
659 ea->ea_type = er->er_type;
660 ea->__pad = 0;
661
662 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
663
664 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
665 ea->ea_num_ptrs = 0;
666 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
667 } else {
Al Virob44b84d2006-10-14 10:46:30 -0400668 __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 const char *data = er->er_data;
670 unsigned int data_len = er->er_data_len;
671 unsigned int copy;
672 unsigned int x;
673
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500674 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675 for (x = 0; x < ea->ea_num_ptrs; x++) {
676 struct buffer_head *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400677 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 int mh_size = sizeof(struct gfs2_meta_header);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000679 unsigned int n = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680
Steven Whitehouse09010972009-05-20 10:48:47 +0100681 error = gfs2_alloc_block(ip, &block, &n);
682 if (error)
683 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000684 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 bh = gfs2_meta_new(ip->i_gl, block);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000686 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
688
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000689 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690
Steven Whitehousecca195c2006-09-05 13:15:18 -0400691 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
692 data_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 memcpy(bh->b_data + mh_size, data, copy);
694 if (copy < sdp->sd_jbsize)
695 memset(bh->b_data + mh_size + copy, 0,
696 sdp->sd_jbsize - copy);
697
Steven Whitehousecca195c2006-09-05 13:15:18 -0400698 *dataptr++ = cpu_to_be64(bh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699 data += copy;
700 data_len -= copy;
701
702 brelse(bh);
703 }
704
705 gfs2_assert_withdraw(sdp, !data_len);
706 }
707
708 return 0;
709}
710
711typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400712 struct gfs2_ea_request *er, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713
714static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
715 unsigned int blks,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400716 ea_skeleton_call_t skeleton_call, void *private)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717{
718 struct gfs2_alloc *al;
719 struct buffer_head *dibh;
720 int error;
721
722 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +0300723 if (!al)
724 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725
Steven Whitehoused82661d2008-03-10 15:34:50 +0000726 error = gfs2_quota_lock_check(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 if (error)
728 goto out;
729
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 al->al_requested = blks;
731
732 error = gfs2_inplace_reserve(ip);
733 if (error)
734 goto out_gunlock_q;
735
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400736 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
Benjamin Marzinskibf97b672010-09-27 16:00:04 -0500737 blks + gfs2_rg_blocks(al) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
739 if (error)
740 goto out_ipres;
741
742 error = skeleton_call(ip, er, private);
743 if (error)
744 goto out_end_trans;
745
746 error = gfs2_meta_inode_buffer(ip, &dibh);
747 if (!error) {
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100748 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000749 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500750 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 brelse(dibh);
752 }
753
Steven Whitehousea91ea692006-09-04 12:04:26 -0400754out_end_trans:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400755 gfs2_trans_end(GFS2_SB(&ip->i_inode));
Steven Whitehousea91ea692006-09-04 12:04:26 -0400756out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400758out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 gfs2_quota_unlock(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400760out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 return error;
763}
764
765static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
766 void *private)
767{
768 struct buffer_head *bh;
769 int error;
770
771 error = ea_alloc_blk(ip, &bh);
772 if (error)
773 return error;
774
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000775 ip->i_eattr = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
777
778 brelse(bh);
779
780 return error;
781}
782
783/**
784 * ea_init - initializes a new eattr block
785 * @ip:
786 * @er:
787 *
788 * Returns: errno
789 */
790
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100791static int ea_init(struct gfs2_inode *ip, int type, const char *name,
792 const void *data, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100794 struct gfs2_ea_request er;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400795 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 unsigned int blks = 1;
797
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100798 er.er_type = type;
799 er.er_name = name;
800 er.er_name_len = strlen(name);
801 er.er_data = (void *)data;
802 er.er_data_len = size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100804 if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
805 blks += DIV_ROUND_UP(er.er_data_len, jbsize);
806
807 return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808}
809
810static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
811{
Steven Whitehousecd915492006-09-04 12:49:07 -0400812 u32 ea_size = GFS2_EA_SIZE(ea);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500813 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
814 ea_size);
Steven Whitehousecd915492006-09-04 12:49:07 -0400815 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
817
818 ea->ea_rec_len = cpu_to_be32(ea_size);
819 ea->ea_flags ^= last;
820
821 new->ea_rec_len = cpu_to_be32(new_size);
822 new->ea_flags = last;
823
824 return new;
825}
826
827static void ea_set_remove_stuffed(struct gfs2_inode *ip,
828 struct gfs2_ea_location *el)
829{
830 struct gfs2_ea_header *ea = el->el_ea;
831 struct gfs2_ea_header *prev = el->el_prev;
Steven Whitehousecd915492006-09-04 12:49:07 -0400832 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000834 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835
836 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
837 ea->ea_type = GFS2_EATYPE_UNUSED;
838 return;
839 } else if (GFS2_EA2NEXT(prev) != ea) {
840 prev = GFS2_EA2NEXT(prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400841 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 }
843
844 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
845 prev->ea_rec_len = cpu_to_be32(len);
846
847 if (GFS2_EA_IS_LAST(ea))
848 prev->ea_flags |= GFS2_EAFLAG_LAST;
849}
850
851struct ea_set {
852 int ea_split;
853
854 struct gfs2_ea_request *es_er;
855 struct gfs2_ea_location *es_el;
856
857 struct buffer_head *es_bh;
858 struct gfs2_ea_header *es_ea;
859};
860
861static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
862 struct gfs2_ea_header *ea, struct ea_set *es)
863{
864 struct gfs2_ea_request *er = es->es_er;
865 struct buffer_head *dibh;
866 int error;
867
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400868 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 if (error)
870 return error;
871
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000872 gfs2_trans_add_bh(ip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873
874 if (es->ea_split)
875 ea = ea_split_ea(ea);
876
877 ea_write(ip, ea, er);
878
879 if (es->es_el)
880 ea_set_remove_stuffed(ip, es->es_el);
881
882 error = gfs2_meta_inode_buffer(ip, &dibh);
883 if (error)
884 goto out;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100885 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000886 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500887 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400889out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400890 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 return error;
892}
893
894static int ea_set_simple_alloc(struct gfs2_inode *ip,
895 struct gfs2_ea_request *er, void *private)
896{
897 struct ea_set *es = private;
898 struct gfs2_ea_header *ea = es->es_ea;
899 int error;
900
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000901 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902
903 if (es->ea_split)
904 ea = ea_split_ea(ea);
905
906 error = ea_write(ip, ea, er);
907 if (error)
908 return error;
909
910 if (es->es_el)
911 ea_set_remove_stuffed(ip, es->es_el);
912
913 return 0;
914}
915
916static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
917 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
918 void *private)
919{
920 struct ea_set *es = private;
921 unsigned int size;
922 int stuffed;
923 int error;
924
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100925 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
926 es->es_er->er_data_len, &size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927
928 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
929 if (GFS2_EA_REC_LEN(ea) < size)
930 return 0;
931 if (!GFS2_EA_IS_STUFFED(ea)) {
932 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
933 if (error)
934 return error;
935 }
936 es->ea_split = 0;
937 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
938 es->ea_split = 1;
939 else
940 return 0;
941
942 if (stuffed) {
943 error = ea_set_simple_noalloc(ip, bh, ea, es);
944 if (error)
945 return error;
946 } else {
947 unsigned int blks;
948
949 es->es_bh = bh;
950 es->es_ea = ea;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500951 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400952 GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953
954 error = ea_alloc_skeleton(ip, es->es_er, blks,
955 ea_set_simple_alloc, es);
956 if (error)
957 return error;
958 }
959
960 return 1;
961}
962
963static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
964 void *private)
965{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400966 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 struct buffer_head *indbh, *newbh;
Al Virob44b84d2006-10-14 10:46:30 -0400968 __be64 *eablk;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969 int error;
970 int mh_size = sizeof(struct gfs2_meta_header);
971
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000972 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
Al Virob44b84d2006-10-14 10:46:30 -0400973 __be64 *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000975 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400976 &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977 if (error)
978 return error;
979
980 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
981 error = -EIO;
982 goto out;
983 }
984
Al Virob44b84d2006-10-14 10:46:30 -0400985 eablk = (__be64 *)(indbh->b_data + mh_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 end = eablk + sdp->sd_inptrs;
987
988 for (; eablk < end; eablk++)
989 if (!*eablk)
990 break;
991
992 if (eablk == end) {
993 error = -ENOSPC;
994 goto out;
995 }
996
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000997 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -0400999 u64 blk;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001000 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +01001001 error = gfs2_alloc_block(ip, &blk, &n);
1002 if (error)
1003 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +00001004 gfs2_trans_add_unrevoke(sdp, blk, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 indbh = gfs2_meta_new(ip->i_gl, blk);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001006 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
1008 gfs2_buffer_clear_tail(indbh, mh_size);
1009
Al Virob44b84d2006-10-14 10:46:30 -04001010 eablk = (__be64 *)(indbh->b_data + mh_size);
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001011 *eablk = cpu_to_be64(ip->i_eattr);
1012 ip->i_eattr = blk;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001013 ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001014 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001015
1016 eablk++;
1017 }
1018
1019 error = ea_alloc_blk(ip, &newbh);
1020 if (error)
1021 goto out;
1022
Steven Whitehousecd915492006-09-04 12:49:07 -04001023 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1025 brelse(newbh);
1026 if (error)
1027 goto out;
1028
1029 if (private)
Steven Whitehousecca195c2006-09-05 13:15:18 -04001030 ea_set_remove_stuffed(ip, private);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031
Steven Whitehousea91ea692006-09-04 12:04:26 -04001032out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 return error;
1035}
1036
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001037static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
1038 const void *value, size_t size, struct gfs2_ea_location *el)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001040 struct gfs2_ea_request er;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 struct ea_set es;
1042 unsigned int blks = 2;
1043 int error;
1044
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001045 er.er_type = type;
1046 er.er_name = name;
1047 er.er_data = (void *)value;
1048 er.er_name_len = strlen(name);
1049 er.er_data_len = size;
1050
David Teiglandb3b94fa2006-01-16 16:50:04 +00001051 memset(&es, 0, sizeof(struct ea_set));
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001052 es.es_er = &er;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 es.es_el = el;
1054
1055 error = ea_foreach(ip, ea_set_simple, &es);
1056 if (error > 0)
1057 return 0;
1058 if (error)
1059 return error;
1060
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001061 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 blks++;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001063 if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1064 blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001066 return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067}
1068
1069static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1070 struct gfs2_ea_location *el)
1071{
1072 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1073 el->el_prev = GFS2_EA2NEXT(el->el_prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001074 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1076 }
1077
Steven Whitehouse86d00632009-09-14 09:50:57 +01001078 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079}
1080
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1082{
1083 struct gfs2_ea_header *ea = el->el_ea;
1084 struct gfs2_ea_header *prev = el->el_prev;
1085 struct buffer_head *dibh;
1086 int error;
1087
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001088 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089 if (error)
1090 return error;
1091
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001092 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093
1094 if (prev) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001095 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096
1097 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1098 prev->ea_rec_len = cpu_to_be32(len);
1099
1100 if (GFS2_EA_IS_LAST(ea))
1101 prev->ea_flags |= GFS2_EAFLAG_LAST;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001102 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 ea->ea_type = GFS2_EATYPE_UNUSED;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001104 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105
1106 error = gfs2_meta_inode_buffer(ip, &dibh);
1107 if (!error) {
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001108 ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001109 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001110 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 brelse(dibh);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001112 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001114 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115
1116 return error;
1117}
1118
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001119/**
1120 * gfs2_xattr_remove - Remove a GFS2 extended attribute
Christoph Hellwig431547b2009-11-13 09:52:56 +00001121 * @ip: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001122 * @type: The type of the extended attribute
1123 * @name: The name of the extended attribute
1124 *
1125 * This is not called directly by the VFS since we use the (common)
1126 * scheme of making a "set with NULL data" mean a remove request. Note
1127 * that this is different from a set with zero length data.
1128 *
1129 * Returns: 0, or errno on failure
1130 */
1131
Christoph Hellwig431547b2009-11-13 09:52:56 +00001132static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133{
1134 struct gfs2_ea_location el;
1135 int error;
1136
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001137 if (!ip->i_eattr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138 return -ENODATA;
1139
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001140 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141 if (error)
1142 return error;
1143 if (!el.el_ea)
1144 return -ENODATA;
1145
1146 if (GFS2_EA_IS_STUFFED(el.el_ea))
1147 error = ea_remove_stuffed(ip, &el);
1148 else
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001149 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150
1151 brelse(el.el_bh);
1152
1153 return error;
1154}
1155
1156/**
Christoph Hellwig431547b2009-11-13 09:52:56 +00001157 * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
1158 * @ip: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001159 * @name: The name of the extended attribute
1160 * @value: The value of the extended attribute (NULL for remove)
1161 * @size: The size of the @value argument
1162 * @flags: Create or Replace
Christoph Hellwig431547b2009-11-13 09:52:56 +00001163 * @type: The type of the extended attribute
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164 *
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001165 * See gfs2_xattr_remove() for details of the removal of xattrs.
1166 *
1167 * Returns: 0 or errno on failure
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168 */
1169
Christoph Hellwig431547b2009-11-13 09:52:56 +00001170int __gfs2_xattr_set(struct inode *inode, const char *name,
1171 const void *value, size_t size, int flags, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001173 struct gfs2_inode *ip = GFS2_I(inode);
Christoph Hellwig431547b2009-11-13 09:52:56 +00001174 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001175 struct gfs2_ea_location el;
1176 unsigned int namel = strlen(name);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001177 int error;
1178
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001179 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1180 return -EPERM;
1181 if (namel > GFS2_EA_MAX_NAME_LEN)
1182 return -ERANGE;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001183
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001184 if (value == NULL)
Christoph Hellwig431547b2009-11-13 09:52:56 +00001185 return gfs2_xattr_remove(ip, type, name);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001186
1187 if (ea_check_size(sdp, namel, size))
1188 return -ERANGE;
1189
1190 if (!ip->i_eattr) {
1191 if (flags & XATTR_REPLACE)
1192 return -ENODATA;
1193 return ea_init(ip, type, name, value, size);
1194 }
1195
1196 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197 if (error)
1198 return error;
1199
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001200 if (el.el_ea) {
1201 if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
1202 brelse(el.el_bh);
1203 return -EPERM;
1204 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001205
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001206 error = -EEXIST;
1207 if (!(flags & XATTR_CREATE)) {
1208 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1209 error = ea_set_i(ip, type, name, value, size, &el);
1210 if (!error && unstuffed)
1211 ea_set_remove_unstuffed(ip, &el);
1212 }
1213
1214 brelse(el.el_bh);
1215 return error;
1216 }
1217
1218 error = -ENODATA;
1219 if (!(flags & XATTR_REPLACE))
1220 error = ea_set_i(ip, type, name, value, size, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001221
1222 return error;
1223}
1224
Christoph Hellwig431547b2009-11-13 09:52:56 +00001225static int gfs2_xattr_set(struct dentry *dentry, const char *name,
1226 const void *value, size_t size, int flags, int type)
1227{
1228 return __gfs2_xattr_set(dentry->d_inode, name, value,
1229 size, flags, type);
1230}
1231
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1233 struct gfs2_ea_header *ea, char *data)
1234{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001235 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001236 struct buffer_head **bh;
1237 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001238 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -04001239 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001240 unsigned int x;
1241 int error;
1242
Josef Bacik16c5f062008-04-09 09:33:41 -04001243 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 if (!bh)
1245 return -ENOMEM;
1246
1247 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1248 if (error)
1249 goto out;
1250
1251 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001252 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
1253 bh + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001254 if (error) {
1255 while (x--)
1256 brelse(bh[x]);
1257 goto fail;
1258 }
1259 dataptrs++;
1260 }
1261
1262 for (x = 0; x < nptrs; x++) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001263 error = gfs2_meta_wait(sdp, bh[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264 if (error) {
1265 for (; x < nptrs; x++)
1266 brelse(bh[x]);
1267 goto fail;
1268 }
1269 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1270 for (; x < nptrs; x++)
1271 brelse(bh[x]);
1272 error = -EIO;
1273 goto fail;
1274 }
1275
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001276 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277
Steven Whitehousecca195c2006-09-05 13:15:18 -04001278 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1280
1281 amount -= sdp->sd_jbsize;
1282 data += sdp->sd_jbsize;
1283
1284 brelse(bh[x]);
1285 }
1286
Steven Whitehousea91ea692006-09-04 12:04:26 -04001287out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001288 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001289 return error;
1290
Steven Whitehousea91ea692006-09-04 12:04:26 -04001291fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292 gfs2_trans_end(sdp);
1293 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001294 return error;
1295}
1296
Steven Whitehouse479c4272009-10-02 12:00:00 +01001297int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001298{
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001299 struct inode *inode = &ip->i_inode;
1300 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse479c4272009-10-02 12:00:00 +01001301 struct gfs2_ea_location el;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001302 int error;
1303
Steven Whitehouse479c4272009-10-02 12:00:00 +01001304 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, GFS2_POSIX_ACL_ACCESS, &el);
1305 if (error)
1306 return error;
1307
1308 if (GFS2_EA_IS_STUFFED(el.el_ea)) {
Steven Whitehousee412bdb2009-12-21 13:55:28 +00001309 error = gfs2_trans_begin(sdp, RES_DINODE + RES_EATTR, 0);
1310 if (error == 0) {
1311 gfs2_trans_add_bh(ip->i_gl, el.el_bh, 1);
1312 memcpy(GFS2_EA2DATA(el.el_ea), data,
1313 GFS2_EA_DATA_LEN(el.el_ea));
1314 }
1315 } else {
Steven Whitehouse479c4272009-10-02 12:00:00 +01001316 error = ea_acl_chmod_unstuffed(ip, el.el_ea, data);
Steven Whitehousee412bdb2009-12-21 13:55:28 +00001317 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318
Steven Whitehousee412bdb2009-12-21 13:55:28 +00001319 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001320 if (error)
1321 return error;
1322
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001323 error = gfs2_setattr_simple(inode, attr);
Steven Whitehousee412bdb2009-12-21 13:55:28 +00001324 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 return error;
1326}
1327
1328static int ea_dealloc_indirect(struct gfs2_inode *ip)
1329{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001330 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001331 struct gfs2_rgrp_list rlist;
1332 struct buffer_head *indbh, *dibh;
Al Virob44b84d2006-10-14 10:46:30 -04001333 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334 unsigned int rg_blocks = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001335 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001336 unsigned int blen = 0;
1337 unsigned int blks = 0;
1338 unsigned int x;
1339 int error;
1340
1341 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1342
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001343 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344 if (error)
1345 return error;
1346
1347 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1348 error = -EIO;
1349 goto out;
1350 }
1351
Al Virob44b84d2006-10-14 10:46:30 -04001352 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353 end = eablk + sdp->sd_inptrs;
1354
1355 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001356 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357
1358 if (!*eablk)
1359 break;
1360 bn = be64_to_cpu(*eablk);
1361
1362 if (bstart + blen == bn)
1363 blen++;
1364 else {
1365 if (bstart)
1366 gfs2_rlist_add(sdp, &rlist, bstart);
1367 bstart = bn;
1368 blen = 1;
1369 }
1370 blks++;
1371 }
1372 if (bstart)
1373 gfs2_rlist_add(sdp, &rlist, bstart);
1374 else
1375 goto out;
1376
Bob Petersonfe6c9912008-01-28 11:13:02 -06001377 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001378
1379 for (x = 0; x < rlist.rl_rgrps; x++) {
1380 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001381 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001382 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001383 }
1384
1385 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1386 if (error)
1387 goto out_rlist_free;
1388
Steven Whitehousecca195c2006-09-05 13:15:18 -04001389 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1390 RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 if (error)
1392 goto out_gunlock;
1393
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001394 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395
Al Virob44b84d2006-10-14 10:46:30 -04001396 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397 bstart = 0;
1398 blen = 0;
1399
1400 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001401 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402
1403 if (!*eablk)
1404 break;
1405 bn = be64_to_cpu(*eablk);
1406
1407 if (bstart + blen == bn)
1408 blen++;
1409 else {
1410 if (bstart)
1411 gfs2_free_meta(ip, bstart, blen);
1412 bstart = bn;
1413 blen = 1;
1414 }
1415
1416 *eablk = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001417 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001418 }
1419 if (bstart)
1420 gfs2_free_meta(ip, bstart, blen);
1421
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001422 ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001423
1424 error = gfs2_meta_inode_buffer(ip, &dibh);
1425 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001426 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001427 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001428 brelse(dibh);
1429 }
1430
1431 gfs2_trans_end(sdp);
1432
Steven Whitehousea91ea692006-09-04 12:04:26 -04001433out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001434 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001435out_rlist_free:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001436 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001437out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001438 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001439 return error;
1440}
1441
1442static int ea_dealloc_block(struct gfs2_inode *ip)
1443{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001444 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001445 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001446 struct gfs2_rgrpd *rgd;
1447 struct buffer_head *dibh;
1448 int error;
1449
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001450 rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001451 if (!rgd) {
1452 gfs2_consist_inode(ip);
1453 return -EIO;
1454 }
1455
1456 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1457 &al->al_rgd_gh);
1458 if (error)
1459 return error;
1460
Steven Whitehousecca195c2006-09-05 13:15:18 -04001461 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1462 RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001463 if (error)
1464 goto out_gunlock;
1465
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001466 gfs2_free_meta(ip, ip->i_eattr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001467
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001468 ip->i_eattr = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001469 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001470
1471 error = gfs2_meta_inode_buffer(ip, &dibh);
1472 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001473 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001474 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001475 brelse(dibh);
1476 }
1477
1478 gfs2_trans_end(sdp);
1479
Steven Whitehousea91ea692006-09-04 12:04:26 -04001480out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001481 gfs2_glock_dq_uninit(&al->al_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001482 return error;
1483}
1484
1485/**
1486 * gfs2_ea_dealloc - deallocate the extended attribute fork
1487 * @ip: the inode
1488 *
1489 * Returns: errno
1490 */
1491
1492int gfs2_ea_dealloc(struct gfs2_inode *ip)
1493{
1494 struct gfs2_alloc *al;
1495 int error;
1496
1497 al = gfs2_alloc_get(ip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001498 if (!al)
1499 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001500
1501 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1502 if (error)
1503 goto out_alloc;
1504
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001505 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001506 if (error)
1507 goto out_quota;
1508
1509 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1510 if (error)
1511 goto out_rindex;
1512
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001513 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001514 error = ea_dealloc_indirect(ip);
1515 if (error)
1516 goto out_rindex;
1517 }
1518
1519 error = ea_dealloc_block(ip);
1520
Steven Whitehousea91ea692006-09-04 12:04:26 -04001521out_rindex:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001522 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001523out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001524 gfs2_quota_unhold(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001525out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001526 gfs2_alloc_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001527 return error;
1528}
1529
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001530static const struct xattr_handler gfs2_xattr_user_handler = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001531 .prefix = XATTR_USER_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +00001532 .flags = GFS2_EATYPE_USR,
1533 .get = gfs2_xattr_get,
1534 .set = gfs2_xattr_set,
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001535};
1536
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001537static const struct xattr_handler gfs2_xattr_security_handler = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001538 .prefix = XATTR_SECURITY_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +00001539 .flags = GFS2_EATYPE_SECURITY,
1540 .get = gfs2_xattr_get,
1541 .set = gfs2_xattr_set,
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001542};
1543
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001544const struct xattr_handler *gfs2_xattr_handlers[] = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001545 &gfs2_xattr_user_handler,
1546 &gfs2_xattr_security_handler,
1547 &gfs2_xattr_system_handler,
1548 NULL,
1549};
1550