blob: ea09e41dbb49c2079bedd285bdccc3e38101edaa [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>
Christoph Hellwige01580b2013-12-20 05:16:52 -080016#include <linux/posix_acl_xattr.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080017#include <linux/uaccess.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018
19#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "acl.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010022#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include "glock.h"
24#include "inode.h"
25#include "meta_io.h"
26#include "quota.h"
27#include "rgrp.h"
Bob Peterson27c3b412017-08-18 09:15:13 -050028#include "super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050030#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031
32/**
33 * ea_calc_size - returns the acutal number of bytes the request will take up
34 * (not counting any unstuffed data blocks)
35 * @sdp:
36 * @er:
37 * @size:
38 *
39 * Returns: 1 if the EA should be stuffed
40 */
41
Steven Whitehouse40b78a32009-08-26 18:41:32 +010042static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize,
David Teiglandb3b94fa2006-01-16 16:50:04 +000043 unsigned int *size)
44{
Steven Whitehouse40b78a32009-08-26 18:41:32 +010045 unsigned int jbsize = sdp->sd_jbsize;
46
47 /* Stuffed */
48 *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + dsize, 8);
49
50 if (*size <= jbsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 return 1;
52
Steven Whitehouse40b78a32009-08-26 18:41:32 +010053 /* Unstuffed */
54 *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize +
55 (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8);
David Teiglandb3b94fa2006-01-16 16:50:04 +000056
57 return 0;
58}
59
Steven Whitehouse40b78a32009-08-26 18:41:32 +010060static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize)
David Teiglandb3b94fa2006-01-16 16:50:04 +000061{
62 unsigned int size;
63
Steven Whitehouse40b78a32009-08-26 18:41:32 +010064 if (dsize > GFS2_EA_MAX_DATA_LEN)
David Teiglandb3b94fa2006-01-16 16:50:04 +000065 return -ERANGE;
66
Steven Whitehouse40b78a32009-08-26 18:41:32 +010067 ea_calc_size(sdp, nsize, dsize, &size);
David Teiglandb3b94fa2006-01-16 16:50:04 +000068
69 /* This can only happen with 512 byte blocks */
70 if (size > sdp->sd_jbsize)
71 return -ERANGE;
72
73 return 0;
74}
75
Steven Whitehousecca195c2006-09-05 13:15:18 -040076typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
David Teiglandb3b94fa2006-01-16 16:50:04 +000077 struct gfs2_ea_header *ea,
Steven Whitehousecca195c2006-09-05 13:15:18 -040078 struct gfs2_ea_header *prev, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +000079
80static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
81 ea_call_t ea_call, void *data)
82{
83 struct gfs2_ea_header *ea, *prev = NULL;
84 int error = 0;
85
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040086 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
David Teiglandb3b94fa2006-01-16 16:50:04 +000087 return -EIO;
88
89 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
90 if (!GFS2_EA_REC_LEN(ea))
91 goto fail;
Steven Whitehousecca195c2006-09-05 13:15:18 -040092 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
93 bh->b_data + bh->b_size))
David Teiglandb3b94fa2006-01-16 16:50:04 +000094 goto fail;
95 if (!GFS2_EATYPE_VALID(ea->ea_type))
96 goto fail;
97
98 error = ea_call(ip, bh, ea, prev, data);
99 if (error)
100 return error;
101
102 if (GFS2_EA_IS_LAST(ea)) {
103 if ((char *)GFS2_EA2NEXT(ea) !=
104 bh->b_data + bh->b_size)
105 goto fail;
106 break;
107 }
108 }
109
110 return error;
111
Steven Whitehousea91ea692006-09-04 12:04:26 -0400112fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 gfs2_consist_inode(ip);
114 return -EIO;
115}
116
117static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
118{
119 struct buffer_head *bh, *eabh;
Al Virob44b84d2006-10-14 10:46:30 -0400120 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121 int error;
122
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600123 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000124 if (error)
125 return error;
126
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000127 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 error = ea_foreach_i(ip, bh, ea_call, data);
129 goto out;
130 }
131
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400132 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133 error = -EIO;
134 goto out;
135 }
136
Al Virob44b84d2006-10-14 10:46:30 -0400137 eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400138 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139
140 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400141 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142
143 if (!*eablk)
144 break;
145 bn = be64_to_cpu(*eablk);
146
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600147 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, 0, &eabh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 if (error)
149 break;
150 error = ea_foreach_i(ip, eabh, ea_call, data);
151 brelse(eabh);
152 if (error)
153 break;
154 }
Steven Whitehousea91ea692006-09-04 12:04:26 -0400155out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 return error;
158}
159
160struct ea_find {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100161 int type;
162 const char *name;
163 size_t namel;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164 struct gfs2_ea_location *ef_el;
165};
166
167static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
168 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
169 void *private)
170{
171 struct ea_find *ef = private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172
173 if (ea->ea_type == GFS2_EATYPE_UNUSED)
174 return 0;
175
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100176 if (ea->ea_type == ef->type) {
177 if (ea->ea_name_len == ef->namel &&
178 !memcmp(GFS2_EA2NAME(ea), ef->name, ea->ea_name_len)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000179 struct gfs2_ea_location *el = ef->ef_el;
180 get_bh(bh);
181 el->el_bh = bh;
182 el->el_ea = ea;
183 el->el_prev = prev;
184 return 1;
185 }
186 }
187
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188 return 0;
189}
190
Steven Whitehouse479c4272009-10-02 12:00:00 +0100191static int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name,
192 struct gfs2_ea_location *el)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193{
194 struct ea_find ef;
195 int error;
196
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100197 ef.type = type;
198 ef.name = name;
199 ef.namel = strlen(name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200 ef.ef_el = el;
201
202 memset(el, 0, sizeof(struct gfs2_ea_location));
203
204 error = ea_foreach(ip, ea_find_i, &ef);
205 if (error > 0)
206 return 0;
207
208 return error;
209}
210
211/**
212 * ea_dealloc_unstuffed -
213 * @ip:
214 * @bh:
215 * @ea:
216 * @prev:
217 * @private:
218 *
219 * Take advantage of the fact that all unstuffed blocks are
220 * allocated from the same RG. But watch, this may not always
221 * be true.
222 *
223 * Returns: errno
224 */
225
226static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
227 struct gfs2_ea_header *ea,
228 struct gfs2_ea_header *prev, void *private)
229{
230 int *leave = private;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400231 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 struct gfs2_rgrpd *rgd;
233 struct gfs2_holder rg_gh;
234 struct buffer_head *dibh;
Al Virob44b84d2006-10-14 10:46:30 -0400235 __be64 *dataptrs;
236 u64 bn = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -0400237 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238 unsigned int blen = 0;
239 unsigned int blks = 0;
240 unsigned int x;
241 int error;
242
Bob Peterson5e2f7d62012-04-04 22:11:16 -0400243 error = gfs2_rindex_update(sdp);
244 if (error)
245 return error;
246
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247 if (GFS2_EA_IS_STUFFED(ea))
248 return 0;
249
250 dataptrs = GFS2_EA2DATAPTRS(ea);
Steven Whitehousecca195c2006-09-05 13:15:18 -0400251 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252 if (*dataptrs) {
253 blks++;
254 bn = be64_to_cpu(*dataptrs);
255 }
Steven Whitehousecca195c2006-09-05 13:15:18 -0400256 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 if (!blks)
258 return 0;
259
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000260 rgd = gfs2_blk2rgrpd(sdp, bn, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 if (!rgd) {
262 gfs2_consist_inode(ip);
263 return -EIO;
264 }
265
266 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
267 if (error)
268 return error;
269
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100270 error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
Steven Whitehousecca195c2006-09-05 13:15:18 -0400271 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272 if (error)
273 goto out_gunlock;
274
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000275 gfs2_trans_add_meta(ip->i_gl, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276
277 dataptrs = GFS2_EA2DATAPTRS(ea);
278 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
279 if (!*dataptrs)
280 break;
281 bn = be64_to_cpu(*dataptrs);
282
283 if (bstart + blen == bn)
284 blen++;
285 else {
286 if (bstart)
287 gfs2_free_meta(ip, bstart, blen);
288 bstart = bn;
289 blen = 1;
290 }
291
292 *dataptrs = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000293 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000294 }
295 if (bstart)
296 gfs2_free_meta(ip, bstart, blen);
297
298 if (prev && !leave) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400299 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300
301 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
302 prev->ea_rec_len = cpu_to_be32(len);
303
304 if (GFS2_EA_IS_LAST(ea))
305 prev->ea_flags |= GFS2_EAFLAG_LAST;
306 } else {
307 ea->ea_type = GFS2_EATYPE_UNUSED;
308 ea->ea_num_ptrs = 0;
309 }
310
311 error = gfs2_meta_inode_buffer(ip, &dibh);
312 if (!error) {
Deepa Dinamani078cd822016-09-14 07:48:04 -0700313 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000314 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500315 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 brelse(dibh);
317 }
318
319 gfs2_trans_end(sdp);
320
Steven Whitehousea91ea692006-09-04 12:04:26 -0400321out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 gfs2_glock_dq_uninit(&rg_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 return error;
324}
325
326static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
327 struct gfs2_ea_header *ea,
328 struct gfs2_ea_header *prev, int leave)
329{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330 int error;
331
Bob Peterson8e2e0042012-07-19 08:12:40 -0400332 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
333 if (error)
334 return error;
335
Eric W. Biedermanf4108a62013-01-31 17:49:26 -0800336 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 if (error)
338 goto out_alloc;
339
Steven Whitehousecca195c2006-09-05 13:15:18 -0400340 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342 gfs2_quota_unhold(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400343out_alloc:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 return error;
345}
346
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347struct ea_list {
348 struct gfs2_ea_request *ei_er;
349 unsigned int ei_size;
350};
351
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100352static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header *ea)
353{
354 switch (ea->ea_type) {
355 case GFS2_EATYPE_USR:
356 return 5 + ea->ea_name_len + 1;
357 case GFS2_EATYPE_SYS:
358 return 7 + ea->ea_name_len + 1;
359 case GFS2_EATYPE_SECURITY:
360 return 9 + ea->ea_name_len + 1;
361 default:
362 return 0;
363 }
364}
365
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
367 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
368 void *private)
369{
370 struct ea_list *ei = private;
371 struct gfs2_ea_request *er = ei->ei_er;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400372 unsigned int ea_size = gfs2_ea_strlen(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373
374 if (ea->ea_type == GFS2_EATYPE_UNUSED)
375 return 0;
376
377 if (er->er_data_len) {
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400378 char *prefix = NULL;
379 unsigned int l = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000380 char c = 0;
381
382 if (ei->ei_size + ea_size > er->er_data_len)
383 return -ERANGE;
384
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400385 switch (ea->ea_type) {
386 case GFS2_EATYPE_USR:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 prefix = "user.";
388 l = 5;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400389 break;
390 case GFS2_EATYPE_SYS:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391 prefix = "system.";
392 l = 7;
Ryan O'Hara639b6d72006-05-22 10:08:35 -0400393 break;
394 case GFS2_EATYPE_SECURITY:
395 prefix = "security.";
396 l = 9;
397 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398 }
399
Steven Whitehouse01eb7c02006-06-06 17:31:30 -0400400 BUG_ON(l == 0);
401
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400402 memcpy(er->er_data + ei->ei_size, prefix, l);
403 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 ea->ea_name_len);
Steven Whitehouse90cdd202006-05-22 10:36:25 -0400405 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000406 }
407
408 ei->ei_size += ea_size;
409
410 return 0;
411}
412
413/**
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100414 * gfs2_listxattr - List gfs2 extended attributes
415 * @dentry: The dentry whose inode we are interested in
416 * @buffer: The buffer to write the results
417 * @size: The size of the buffer
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 *
419 * Returns: actual size of data on success, -errno on error
420 */
421
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100422ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423{
David Howells2b0143b2015-03-17 22:25:59 +0000424 struct gfs2_inode *ip = GFS2_I(d_inode(dentry));
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100425 struct gfs2_ea_request er;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426 struct gfs2_holder i_gh;
427 int error;
428
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100429 memset(&er, 0, sizeof(struct gfs2_ea_request));
430 if (size) {
431 er.er_data = buffer;
432 er.er_data_len = size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433 }
434
Steven Whitehousecca195c2006-09-05 13:15:18 -0400435 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436 if (error)
437 return error;
438
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000439 if (ip->i_eattr) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100440 struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441
442 error = ea_foreach(ip, ea_list_i, &ei);
443 if (!error)
444 error = ei.ei_size;
445 }
446
447 gfs2_glock_dq_uninit(&i_gh);
448
449 return error;
450}
451
452/**
Steven Whitehouse1f981692012-07-26 11:26:36 +0100453 * ea_iter_unstuffed - copies the unstuffed xattr data to/from the
454 * request buffer
Steven Whitehousecca195c2006-09-05 13:15:18 -0400455 * @ip: The GFS2 inode
456 * @ea: The extended attribute header structure
Steven Whitehouse1f981692012-07-26 11:26:36 +0100457 * @din: The data to be copied in
458 * @dout: The data to be copied out (one of din,dout will be NULL)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 *
460 * Returns: errno
461 */
462
Steven Whitehouse1f981692012-07-26 11:26:36 +0100463static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
464 const char *din, char *dout)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400466 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467 struct buffer_head **bh;
468 unsigned int amount = GFS2_EA_DATA_LEN(ea);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500469 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
Al Virob44b84d2006-10-14 10:46:30 -0400470 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 unsigned int x;
472 int error = 0;
Steven Whitehouse1f981692012-07-26 11:26:36 +0100473 unsigned char *pos;
474 unsigned cp_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475
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++) {
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600481 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, 0,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400482 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 Whitehouse1f981692012-07-26 11:26:36 +0100505 pos = bh[x]->b_data + sizeof(struct gfs2_meta_header);
506 cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize;
507
508 if (dout) {
509 memcpy(dout, pos, cp_size);
510 dout += sdp->sd_jbsize;
511 }
512
513 if (din) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000514 gfs2_trans_add_meta(ip->i_gl, bh[x]);
Steven Whitehouse1f981692012-07-26 11:26:36 +0100515 memcpy(pos, din, cp_size);
516 din += sdp->sd_jbsize;
517 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518
519 amount -= sdp->sd_jbsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 brelse(bh[x]);
521 }
522
Steven Whitehousea91ea692006-09-04 12:04:26 -0400523out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 kfree(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 return error;
526}
527
Steven Whitehouse479c4272009-10-02 12:00:00 +0100528static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
529 char *data, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100531 int ret;
532 size_t len = GFS2_EA_DATA_LEN(el->el_ea);
533 if (len > size)
534 return -ERANGE;
535
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100537 memcpy(data, GFS2_EA2DATA(el->el_ea), len);
538 return len;
539 }
Steven Whitehouse1f981692012-07-26 11:26:36 +0100540 ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100541 if (ret < 0)
542 return ret;
543 return len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544}
545
Steven Whitehouse479c4272009-10-02 12:00:00 +0100546int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
547{
548 struct gfs2_ea_location el;
549 int error;
550 int len;
551 char *data;
552
553 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
554 if (error)
555 return error;
556 if (!el.el_ea)
557 goto out;
558 if (!GFS2_EA_DATA_LEN(el.el_ea))
559 goto out;
560
561 len = GFS2_EA_DATA_LEN(el.el_ea);
562 data = kmalloc(len, GFP_NOFS);
563 error = -ENOMEM;
564 if (data == NULL)
565 goto out;
566
567 error = gfs2_ea_get_copy(ip, &el, data, len);
Steven Whitehouse114b80c2011-11-09 12:54:43 +0000568 if (error < 0)
569 kfree(data);
570 else
571 *ppdata = data;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100572out:
573 brelse(el.el_bh);
574 return error;
575}
576
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577/**
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100578 * gfs2_xattr_get - Get a GFS2 extended attribute
579 * @inode: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100580 * @name: The name of the extended attribute
581 * @buffer: The buffer to write the result into
582 * @size: The size of the buffer
Christoph Hellwig431547b2009-11-13 09:52:56 +0000583 * @type: The type of extended attribute
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 *
585 * Returns: actual size of data on success, -errno on error
586 */
Al Viro1a39ba92016-05-13 03:59:17 +0200587static int __gfs2_xattr_get(struct inode *inode, const char *name,
588 void *buffer, size_t size, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589{
Al Virob2968212016-04-10 20:48:24 -0400590 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 struct gfs2_ea_location el;
592 int error;
593
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000594 if (!ip->i_eattr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595 return -ENODATA;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100596 if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
597 return -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100599 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 if (error)
601 return error;
602 if (!el.el_ea)
603 return -ENODATA;
Steven Whitehouse86d00632009-09-14 09:50:57 +0100604 if (size)
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100605 error = gfs2_ea_get_copy(ip, &el, buffer, size);
606 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 error = GFS2_EA_DATA_LEN(el.el_ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608 brelse(el.el_bh);
609
610 return error;
611}
612
Al Viro1a39ba92016-05-13 03:59:17 +0200613static int gfs2_xattr_get(const struct xattr_handler *handler,
614 struct dentry *unused, struct inode *inode,
615 const char *name, void *buffer, size_t size)
616{
617 struct gfs2_inode *ip = GFS2_I(inode);
618 struct gfs2_holder gh;
619 bool need_unlock = false;
620 int ret;
621
622 /* During lookup, SELinux calls this function with the glock locked. */
623
624 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
625 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
626 if (ret)
627 return ret;
628 need_unlock = true;
629 }
630 ret = __gfs2_xattr_get(inode, name, buffer, size, handler->flags);
631 if (need_unlock)
632 gfs2_glock_dq_uninit(&gh);
633 return ret;
634}
635
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637 * ea_alloc_blk - allocates a new block for extended attributes.
638 * @ip: A pointer to the inode that's getting extended attributes
Steven Whitehousecca195c2006-09-05 13:15:18 -0400639 * @bhp: Pointer to pointer to a struct buffer_head
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 *
641 * Returns: errno
642 */
643
644static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
645{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400646 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647 struct gfs2_ea_header *ea;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000648 unsigned int n = 1;
Steven Whitehousecd915492006-09-04 12:49:07 -0400649 u64 block;
Steven Whitehouse09010972009-05-20 10:48:47 +0100650 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651
Bob Peterson6e87ed02011-11-18 10:58:32 -0500652 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100653 if (error)
654 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000655 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 *bhp = gfs2_meta_new(ip->i_gl, block);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000657 gfs2_trans_add_meta(ip->i_gl, *bhp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
659 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
660
661 ea = GFS2_EA_BH2FIRST(*bhp);
662 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
663 ea->ea_type = GFS2_EATYPE_UNUSED;
664 ea->ea_flags = GFS2_EAFLAG_LAST;
665 ea->ea_num_ptrs = 0;
666
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000667 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668
669 return 0;
670}
671
672/**
673 * ea_write - writes the request info to an ea, creating new blocks if
674 * necessary
Steven Whitehousecca195c2006-09-05 13:15:18 -0400675 * @ip: inode that is being modified
676 * @ea: the location of the new ea in a block
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 * @er: the write request
678 *
679 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
680 *
681 * returns : errno
682 */
683
684static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
685 struct gfs2_ea_request *er)
686{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400687 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse09010972009-05-20 10:48:47 +0100688 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689
690 ea->ea_data_len = cpu_to_be32(er->er_data_len);
691 ea->ea_name_len = er->er_name_len;
692 ea->ea_type = er->er_type;
693 ea->__pad = 0;
694
695 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
696
697 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
698 ea->ea_num_ptrs = 0;
699 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
700 } else {
Al Virob44b84d2006-10-14 10:46:30 -0400701 __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000702 const char *data = er->er_data;
703 unsigned int data_len = er->er_data_len;
704 unsigned int copy;
705 unsigned int x;
706
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500707 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 for (x = 0; x < ea->ea_num_ptrs; x++) {
709 struct buffer_head *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400710 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711 int mh_size = sizeof(struct gfs2_meta_header);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000712 unsigned int n = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713
Bob Peterson6e87ed02011-11-18 10:58:32 -0500714 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100715 if (error)
716 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000717 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 bh = gfs2_meta_new(ip->i_gl, block);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000719 gfs2_trans_add_meta(ip->i_gl, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
721
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000722 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723
Steven Whitehousecca195c2006-09-05 13:15:18 -0400724 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
725 data_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 memcpy(bh->b_data + mh_size, data, copy);
727 if (copy < sdp->sd_jbsize)
728 memset(bh->b_data + mh_size + copy, 0,
729 sdp->sd_jbsize - copy);
730
Steven Whitehousecca195c2006-09-05 13:15:18 -0400731 *dataptr++ = cpu_to_be64(bh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 data += copy;
733 data_len -= copy;
734
735 brelse(bh);
736 }
737
738 gfs2_assert_withdraw(sdp, !data_len);
739 }
740
741 return 0;
742}
743
744typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400745 struct gfs2_ea_request *er, void *private);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746
747static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
748 unsigned int blks,
Steven Whitehousecca195c2006-09-05 13:15:18 -0400749 ea_skeleton_call_t skeleton_call, void *private)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750{
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100751 struct gfs2_alloc_parms ap = { .target = blks };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 struct buffer_head *dibh;
753 int error;
754
Bob Peterson8e2e0042012-07-19 08:12:40 -0400755 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
756 if (error)
757 return error;
758
Abhi Dasb8fbf472015-03-18 12:03:41 -0500759 error = gfs2_quota_lock_check(ip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -0400761 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100763 error = gfs2_inplace_reserve(ip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 if (error)
765 goto out_gunlock_q;
766
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400767 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
Steven Whitehouse71f890f2012-07-30 14:53:19 +0100768 blks + gfs2_rg_blocks(ip, blks) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
770 if (error)
771 goto out_ipres;
772
773 error = skeleton_call(ip, er, private);
774 if (error)
775 goto out_end_trans;
776
777 error = gfs2_meta_inode_buffer(ip, &dibh);
778 if (!error) {
Deepa Dinamani078cd822016-09-14 07:48:04 -0700779 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000780 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500781 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 brelse(dibh);
783 }
784
Steven Whitehousea91ea692006-09-04 12:04:26 -0400785out_end_trans:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400786 gfs2_trans_end(GFS2_SB(&ip->i_inode));
Steven Whitehousea91ea692006-09-04 12:04:26 -0400787out_ipres:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 gfs2_inplace_release(ip);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400789out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 gfs2_quota_unlock(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 return error;
792}
793
794static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
795 void *private)
796{
797 struct buffer_head *bh;
798 int error;
799
800 error = ea_alloc_blk(ip, &bh);
801 if (error)
802 return error;
803
Steven Whitehouse3767ac22008-11-03 14:28:42 +0000804 ip->i_eattr = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
806
807 brelse(bh);
808
809 return error;
810}
811
812/**
813 * ea_init - initializes a new eattr block
814 * @ip:
815 * @er:
816 *
817 * Returns: errno
818 */
819
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100820static int ea_init(struct gfs2_inode *ip, int type, const char *name,
821 const void *data, size_t size)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100823 struct gfs2_ea_request er;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400824 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 unsigned int blks = 1;
826
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100827 er.er_type = type;
828 er.er_name = name;
829 er.er_name_len = strlen(name);
830 er.er_data = (void *)data;
831 er.er_data_len = size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100833 if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
834 blks += DIV_ROUND_UP(er.er_data_len, jbsize);
835
836 return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837}
838
839static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
840{
Steven Whitehousecd915492006-09-04 12:49:07 -0400841 u32 ea_size = GFS2_EA_SIZE(ea);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500842 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
843 ea_size);
Steven Whitehousecd915492006-09-04 12:49:07 -0400844 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
846
847 ea->ea_rec_len = cpu_to_be32(ea_size);
848 ea->ea_flags ^= last;
849
850 new->ea_rec_len = cpu_to_be32(new_size);
851 new->ea_flags = last;
852
853 return new;
854}
855
856static void ea_set_remove_stuffed(struct gfs2_inode *ip,
857 struct gfs2_ea_location *el)
858{
859 struct gfs2_ea_header *ea = el->el_ea;
860 struct gfs2_ea_header *prev = el->el_prev;
Steven Whitehousecd915492006-09-04 12:49:07 -0400861 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000863 gfs2_trans_add_meta(ip->i_gl, el->el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864
865 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
866 ea->ea_type = GFS2_EATYPE_UNUSED;
867 return;
868 } else if (GFS2_EA2NEXT(prev) != ea) {
869 prev = GFS2_EA2NEXT(prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400870 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 }
872
873 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
874 prev->ea_rec_len = cpu_to_be32(len);
875
876 if (GFS2_EA_IS_LAST(ea))
877 prev->ea_flags |= GFS2_EAFLAG_LAST;
878}
879
880struct ea_set {
881 int ea_split;
882
883 struct gfs2_ea_request *es_er;
884 struct gfs2_ea_location *es_el;
885
886 struct buffer_head *es_bh;
887 struct gfs2_ea_header *es_ea;
888};
889
890static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
891 struct gfs2_ea_header *ea, struct ea_set *es)
892{
893 struct gfs2_ea_request *er = es->es_er;
894 struct buffer_head *dibh;
895 int error;
896
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400897 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 if (error)
899 return error;
900
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000901 gfs2_trans_add_meta(ip->i_gl, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902
903 if (es->ea_split)
904 ea = ea_split_ea(ea);
905
906 ea_write(ip, ea, er);
907
908 if (es->es_el)
909 ea_set_remove_stuffed(ip, es->es_el);
910
911 error = gfs2_meta_inode_buffer(ip, &dibh);
912 if (error)
913 goto out;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700914 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000915 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500916 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 brelse(dibh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400918out:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400919 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 return error;
921}
922
923static int ea_set_simple_alloc(struct gfs2_inode *ip,
924 struct gfs2_ea_request *er, void *private)
925{
926 struct ea_set *es = private;
927 struct gfs2_ea_header *ea = es->es_ea;
928 int error;
929
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000930 gfs2_trans_add_meta(ip->i_gl, es->es_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931
932 if (es->ea_split)
933 ea = ea_split_ea(ea);
934
935 error = ea_write(ip, ea, er);
936 if (error)
937 return error;
938
939 if (es->es_el)
940 ea_set_remove_stuffed(ip, es->es_el);
941
942 return 0;
943}
944
945static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
946 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
947 void *private)
948{
949 struct ea_set *es = private;
950 unsigned int size;
951 int stuffed;
952 int error;
953
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100954 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
955 es->es_er->er_data_len, &size);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956
957 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
958 if (GFS2_EA_REC_LEN(ea) < size)
959 return 0;
960 if (!GFS2_EA_IS_STUFFED(ea)) {
961 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
962 if (error)
963 return error;
964 }
965 es->ea_split = 0;
966 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
967 es->ea_split = 1;
968 else
969 return 0;
970
971 if (stuffed) {
972 error = ea_set_simple_noalloc(ip, bh, ea, es);
973 if (error)
974 return error;
975 } else {
976 unsigned int blks;
977
978 es->es_bh = bh;
979 es->es_ea = ea;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500980 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400981 GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982
983 error = ea_alloc_skeleton(ip, es->es_er, blks,
984 ea_set_simple_alloc, es);
985 if (error)
986 return error;
987 }
988
989 return 1;
990}
991
992static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
993 void *private)
994{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400995 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 struct buffer_head *indbh, *newbh;
Al Virob44b84d2006-10-14 10:46:30 -0400997 __be64 *eablk;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998 int error;
999 int mh_size = sizeof(struct gfs2_meta_header);
1000
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001001 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
Al Virob44b84d2006-10-14 10:46:30 -04001002 __be64 *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -06001004 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001005 &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006 if (error)
1007 return error;
1008
1009 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1010 error = -EIO;
1011 goto out;
1012 }
1013
Al Virob44b84d2006-10-14 10:46:30 -04001014 eablk = (__be64 *)(indbh->b_data + mh_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001015 end = eablk + sdp->sd_inptrs;
1016
1017 for (; eablk < end; eablk++)
1018 if (!*eablk)
1019 break;
1020
1021 if (eablk == end) {
1022 error = -ENOSPC;
1023 goto out;
1024 }
1025
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001026 gfs2_trans_add_meta(ip->i_gl, indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 } else {
Steven Whitehousecd915492006-09-04 12:49:07 -04001028 u64 blk;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001029 unsigned int n = 1;
Bob Peterson6e87ed02011-11-18 10:58:32 -05001030 error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +01001031 if (error)
1032 return error;
Steven Whitehouse5731be52008-02-01 13:16:55 +00001033 gfs2_trans_add_unrevoke(sdp, blk, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 indbh = gfs2_meta_new(ip->i_gl, blk);
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001035 gfs2_trans_add_meta(ip->i_gl, indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
1037 gfs2_buffer_clear_tail(indbh, mh_size);
1038
Al Virob44b84d2006-10-14 10:46:30 -04001039 eablk = (__be64 *)(indbh->b_data + mh_size);
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001040 *eablk = cpu_to_be64(ip->i_eattr);
1041 ip->i_eattr = blk;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001042 ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001043 gfs2_add_inode_blocks(&ip->i_inode, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044
1045 eablk++;
1046 }
1047
1048 error = ea_alloc_blk(ip, &newbh);
1049 if (error)
1050 goto out;
1051
Steven Whitehousecd915492006-09-04 12:49:07 -04001052 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1054 brelse(newbh);
1055 if (error)
1056 goto out;
1057
1058 if (private)
Steven Whitehousecca195c2006-09-05 13:15:18 -04001059 ea_set_remove_stuffed(ip, private);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060
Steven Whitehousea91ea692006-09-04 12:04:26 -04001061out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 return error;
1064}
1065
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001066static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
1067 const void *value, size_t size, struct gfs2_ea_location *el)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001069 struct gfs2_ea_request er;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070 struct ea_set es;
1071 unsigned int blks = 2;
1072 int error;
1073
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001074 er.er_type = type;
1075 er.er_name = name;
1076 er.er_data = (void *)value;
1077 er.er_name_len = strlen(name);
1078 er.er_data_len = size;
1079
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080 memset(&es, 0, sizeof(struct ea_set));
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001081 es.es_er = &er;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 es.es_el = el;
1083
1084 error = ea_foreach(ip, ea_set_simple, &es);
1085 if (error > 0)
1086 return 0;
1087 if (error)
1088 return error;
1089
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001090 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001091 blks++;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001092 if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1093 blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001094
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001095 return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096}
1097
1098static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1099 struct gfs2_ea_location *el)
1100{
1101 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1102 el->el_prev = GFS2_EA2NEXT(el->el_prev);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001103 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1105 }
1106
Steven Whitehouse86d00632009-09-14 09:50:57 +01001107 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108}
1109
David Teiglandb3b94fa2006-01-16 16:50:04 +00001110static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1111{
1112 struct gfs2_ea_header *ea = el->el_ea;
1113 struct gfs2_ea_header *prev = el->el_prev;
1114 struct buffer_head *dibh;
1115 int error;
1116
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001117 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 if (error)
1119 return error;
1120
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001121 gfs2_trans_add_meta(ip->i_gl, el->el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122
1123 if (prev) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001124 u32 len;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125
1126 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1127 prev->ea_rec_len = cpu_to_be32(len);
1128
1129 if (GFS2_EA_IS_LAST(ea))
1130 prev->ea_flags |= GFS2_EAFLAG_LAST;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001131 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132 ea->ea_type = GFS2_EATYPE_UNUSED;
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001133 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134
1135 error = gfs2_meta_inode_buffer(ip, &dibh);
1136 if (!error) {
Deepa Dinamani078cd822016-09-14 07:48:04 -07001137 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001138 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001139 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140 brelse(dibh);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001141 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001143 gfs2_trans_end(GFS2_SB(&ip->i_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001144
1145 return error;
1146}
1147
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001148/**
1149 * gfs2_xattr_remove - Remove a GFS2 extended attribute
Christoph Hellwig431547b2009-11-13 09:52:56 +00001150 * @ip: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001151 * @type: The type of the extended attribute
1152 * @name: The name of the extended attribute
1153 *
1154 * This is not called directly by the VFS since we use the (common)
1155 * scheme of making a "set with NULL data" mean a remove request. Note
1156 * that this is different from a set with zero length data.
1157 *
1158 * Returns: 0, or errno on failure
1159 */
1160
Christoph Hellwig431547b2009-11-13 09:52:56 +00001161static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162{
1163 struct gfs2_ea_location el;
1164 int error;
1165
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001166 if (!ip->i_eattr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001167 return -ENODATA;
1168
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001169 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170 if (error)
1171 return error;
1172 if (!el.el_ea)
1173 return -ENODATA;
1174
1175 if (GFS2_EA_IS_STUFFED(el.el_ea))
1176 error = ea_remove_stuffed(ip, &el);
1177 else
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001178 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001179
1180 brelse(el.el_bh);
1181
1182 return error;
1183}
1184
1185/**
Christoph Hellwig431547b2009-11-13 09:52:56 +00001186 * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
1187 * @ip: The inode
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001188 * @name: The name of the extended attribute
1189 * @value: The value of the extended attribute (NULL for remove)
1190 * @size: The size of the @value argument
1191 * @flags: Create or Replace
Christoph Hellwig431547b2009-11-13 09:52:56 +00001192 * @type: The type of the extended attribute
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 *
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001194 * See gfs2_xattr_remove() for details of the removal of xattrs.
1195 *
1196 * Returns: 0 or errno on failure
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197 */
1198
Christoph Hellwig431547b2009-11-13 09:52:56 +00001199int __gfs2_xattr_set(struct inode *inode, const char *name,
1200 const void *value, size_t size, int flags, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201{
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001202 struct gfs2_inode *ip = GFS2_I(inode);
Christoph Hellwig431547b2009-11-13 09:52:56 +00001203 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001204 struct gfs2_ea_location el;
1205 unsigned int namel = strlen(name);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206 int error;
1207
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001208 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1209 return -EPERM;
1210 if (namel > GFS2_EA_MAX_NAME_LEN)
1211 return -ERANGE;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212
Ernesto A. Fernández54aae142017-08-30 07:26:30 -05001213 if (value == NULL) {
1214 error = gfs2_xattr_remove(ip, type, name);
1215 if (error == -ENODATA && !(flags & XATTR_REPLACE))
1216 error = 0;
1217 return error;
1218 }
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001219
1220 if (ea_check_size(sdp, namel, size))
1221 return -ERANGE;
1222
1223 if (!ip->i_eattr) {
1224 if (flags & XATTR_REPLACE)
1225 return -ENODATA;
1226 return ea_init(ip, type, name, value, size);
1227 }
1228
1229 error = gfs2_ea_find(ip, type, name, &el);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001230 if (error)
1231 return error;
1232
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001233 if (el.el_ea) {
1234 if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
1235 brelse(el.el_bh);
1236 return -EPERM;
1237 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001239 error = -EEXIST;
1240 if (!(flags & XATTR_CREATE)) {
1241 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1242 error = ea_set_i(ip, type, name, value, size, &el);
1243 if (!error && unstuffed)
1244 ea_set_remove_unstuffed(ip, &el);
1245 }
1246
1247 brelse(el.el_bh);
1248 return error;
1249 }
1250
1251 error = -ENODATA;
1252 if (!(flags & XATTR_REPLACE))
1253 error = ea_set_i(ip, type, name, value, size, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001254
1255 return error;
1256}
1257
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02001258static int gfs2_xattr_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04001259 struct dentry *unused, struct inode *inode,
1260 const char *name, const void *value,
1261 size_t size, int flags)
Christoph Hellwig431547b2009-11-13 09:52:56 +00001262{
Al Viro1a39ba92016-05-13 03:59:17 +02001263 struct gfs2_inode *ip = GFS2_I(inode);
1264 struct gfs2_holder gh;
1265 int ret;
1266
1267 ret = gfs2_rsqa_alloc(ip);
1268 if (ret)
1269 return ret;
1270
1271 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1272 if (ret)
1273 return ret;
1274 ret = __gfs2_xattr_set(inode, name, value, size, flags, handler->flags);
1275 gfs2_glock_dq_uninit(&gh);
1276 return ret;
Christoph Hellwig431547b2009-11-13 09:52:56 +00001277}
1278
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279static int ea_dealloc_indirect(struct gfs2_inode *ip)
1280{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001281 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001282 struct gfs2_rgrp_list rlist;
1283 struct buffer_head *indbh, *dibh;
Al Virob44b84d2006-10-14 10:46:30 -04001284 __be64 *eablk, *end;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285 unsigned int rg_blocks = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001286 u64 bstart = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287 unsigned int blen = 0;
1288 unsigned int blks = 0;
1289 unsigned int x;
1290 int error;
1291
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001292 error = gfs2_rindex_update(sdp);
1293 if (error)
1294 return error;
1295
David Teiglandb3b94fa2006-01-16 16:50:04 +00001296 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1297
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -06001298 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001299 if (error)
1300 return error;
1301
1302 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1303 error = -EIO;
1304 goto out;
1305 }
1306
Al Virob44b84d2006-10-14 10:46:30 -04001307 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001308 end = eablk + sdp->sd_inptrs;
1309
1310 for (; eablk < end; eablk++) {
Steven Whitehousecd915492006-09-04 12:49:07 -04001311 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312
1313 if (!*eablk)
1314 break;
1315 bn = be64_to_cpu(*eablk);
1316
1317 if (bstart + blen == bn)
1318 blen++;
1319 else {
1320 if (bstart)
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001321 gfs2_rlist_add(ip, &rlist, bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001322 bstart = bn;
1323 blen = 1;
1324 }
1325 blks++;
1326 }
1327 if (bstart)
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001328 gfs2_rlist_add(ip, &rlist, bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001329 else
1330 goto out;
1331
Bob Petersonfe6c9912008-01-28 11:13:02 -06001332 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333
1334 for (x = 0; x < rlist.rl_rgrps; x++) {
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -05001335 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
1336
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001337 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338 }
1339
1340 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1341 if (error)
1342 goto out_rlist_free;
1343
Steven Whitehousecca195c2006-09-05 13:15:18 -04001344 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1345 RES_STATFS + RES_QUOTA, blks);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001346 if (error)
1347 goto out_gunlock;
1348
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001349 gfs2_trans_add_meta(ip->i_gl, indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001350
Al Virob44b84d2006-10-14 10:46:30 -04001351 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352 bstart = 0;
1353 blen = 0;
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_free_meta(ip, bstart, blen);
1367 bstart = bn;
1368 blen = 1;
1369 }
1370
1371 *eablk = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001372 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373 }
1374 if (bstart)
1375 gfs2_free_meta(ip, bstart, blen);
1376
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001377 ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001378
1379 error = gfs2_meta_inode_buffer(ip, &dibh);
1380 if (!error) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001381 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001382 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001383 brelse(dibh);
1384 }
1385
1386 gfs2_trans_end(sdp);
1387
Steven Whitehousea91ea692006-09-04 12:04:26 -04001388out_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001389 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001390out_rlist_free:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 gfs2_rlist_free(&rlist);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001392out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001393 brelse(indbh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394 return error;
1395}
1396
1397static int ea_dealloc_block(struct gfs2_inode *ip)
1398{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001399 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400 struct gfs2_rgrpd *rgd;
1401 struct buffer_head *dibh;
Bob Peterson564e12b2011-11-21 13:36:17 -05001402 struct gfs2_holder gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001403 int error;
1404
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001405 error = gfs2_rindex_update(sdp);
1406 if (error)
1407 return error;
1408
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001409 rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410 if (!rgd) {
1411 gfs2_consist_inode(ip);
1412 return -EIO;
1413 }
1414
Bob Peterson564e12b2011-11-21 13:36:17 -05001415 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001416 if (error)
1417 return error;
1418
Steven Whitehousecca195c2006-09-05 13:15:18 -04001419 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1420 RES_QUOTA, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001421 if (error)
1422 goto out_gunlock;
1423
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001424 gfs2_free_meta(ip, ip->i_eattr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001425
Steven Whitehouse3767ac22008-11-03 14:28:42 +00001426 ip->i_eattr = 0;
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001427 gfs2_add_inode_blocks(&ip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001428
1429 error = gfs2_meta_inode_buffer(ip, &dibh);
1430 if (!error) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001431 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001432 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001433 brelse(dibh);
1434 }
1435
1436 gfs2_trans_end(sdp);
1437
Steven Whitehousea91ea692006-09-04 12:04:26 -04001438out_gunlock:
Bob Peterson564e12b2011-11-21 13:36:17 -05001439 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001440 return error;
1441}
1442
1443/**
1444 * gfs2_ea_dealloc - deallocate the extended attribute fork
1445 * @ip: the inode
1446 *
1447 * Returns: errno
1448 */
1449
1450int gfs2_ea_dealloc(struct gfs2_inode *ip)
1451{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001452 int error;
1453
Bob Peterson8e2e0042012-07-19 08:12:40 -04001454 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
1455 if (error)
1456 return error;
1457
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001458 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001459 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001460 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461
David Teiglandb3b94fa2006-01-16 16:50:04 +00001462 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1463 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001464 goto out_quota;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001465
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001466 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001467 error = ea_dealloc_indirect(ip);
1468 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001469 goto out_quota;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001470 }
1471
1472 error = ea_dealloc_block(ip);
1473
Steven Whitehousea91ea692006-09-04 12:04:26 -04001474out_quota:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001475 gfs2_quota_unhold(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001476 return error;
1477}
1478
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001479static const struct xattr_handler gfs2_xattr_user_handler = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001480 .prefix = XATTR_USER_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +00001481 .flags = GFS2_EATYPE_USR,
1482 .get = gfs2_xattr_get,
1483 .set = gfs2_xattr_set,
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001484};
1485
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001486static const struct xattr_handler gfs2_xattr_security_handler = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001487 .prefix = XATTR_SECURITY_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +00001488 .flags = GFS2_EATYPE_SECURITY,
1489 .get = gfs2_xattr_get,
1490 .set = gfs2_xattr_set,
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001491};
1492
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -07001493const struct xattr_handler *gfs2_xattr_handlers[] = {
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001494 &gfs2_xattr_user_handler,
1495 &gfs2_xattr_security_handler,
Christoph Hellwige01580b2013-12-20 05:16:52 -08001496 &posix_acl_access_xattr_handler,
1497 &posix_acl_default_xattr_handler,
Steven Whitehouse40b78a32009-08-26 18:41:32 +01001498 NULL,
1499};
1500