Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3a8a9a10 | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
8 | */ |
9 | ||
b3b94faa DT |
10 | #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> | |
5c676f6d | 15 | #include <linux/gfs2_ondisk.h> |
e01580bf | 16 | #include <linux/posix_acl_xattr.h> |
7c0f6ba6 | 17 | #include <linux/uaccess.h> |
b3b94faa DT |
18 | |
19 | #include "gfs2.h" | |
5c676f6d | 20 | #include "incore.h" |
b3b94faa | 21 | #include "acl.h" |
307cf6e6 | 22 | #include "xattr.h" |
b3b94faa DT |
23 | #include "glock.h" |
24 | #include "inode.h" | |
25 | #include "meta_io.h" | |
26 | #include "quota.h" | |
27 | #include "rgrp.h" | |
27c3b415 | 28 | #include "super.h" |
b3b94faa | 29 | #include "trans.h" |
5c676f6d | 30 | #include "util.h" |
b3b94faa DT |
31 | |
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 | ||
40b78a32 | 42 | static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize, |
b3b94faa DT |
43 | unsigned int *size) |
44 | { | |
40b78a32 SW |
45 | 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) | |
b3b94faa DT |
51 | return 1; |
52 | ||
40b78a32 SW |
53 | /* Unstuffed */ |
54 | *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + | |
55 | (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8); | |
b3b94faa DT |
56 | |
57 | return 0; | |
58 | } | |
59 | ||
40b78a32 | 60 | static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize) |
b3b94faa DT |
61 | { |
62 | unsigned int size; | |
63 | ||
40b78a32 | 64 | if (dsize > GFS2_EA_MAX_DATA_LEN) |
b3b94faa DT |
65 | return -ERANGE; |
66 | ||
40b78a32 | 67 | ea_calc_size(sdp, nsize, dsize, &size); |
b3b94faa DT |
68 | |
69 | /* This can only happen with 512 byte blocks */ | |
70 | if (size > sdp->sd_jbsize) | |
71 | return -ERANGE; | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
cca195c5 | 76 | typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh, |
b3b94faa | 77 | struct gfs2_ea_header *ea, |
cca195c5 | 78 | struct gfs2_ea_header *prev, void *private); |
b3b94faa DT |
79 | |
80 | static 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 | ||
feaa7bba | 86 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA)) |
b3b94faa DT |
87 | 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; | |
cca195c5 SW |
92 | if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <= |
93 | bh->b_data + bh->b_size)) | |
b3b94faa DT |
94 | 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 | ||
a91ea69f | 112 | fail: |
b3b94faa DT |
113 | gfs2_consist_inode(ip); |
114 | return -EIO; | |
115 | } | |
116 | ||
117 | static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data) | |
118 | { | |
119 | struct buffer_head *bh, *eabh; | |
b44b84d7 | 120 | __be64 *eablk, *end; |
b3b94faa DT |
121 | int error; |
122 | ||
c8d57703 | 123 | error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &bh); |
b3b94faa DT |
124 | if (error) |
125 | return error; | |
126 | ||
383f01fb | 127 | if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) { |
b3b94faa DT |
128 | error = ea_foreach_i(ip, bh, ea_call, data); |
129 | goto out; | |
130 | } | |
131 | ||
feaa7bba | 132 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) { |
b3b94faa DT |
133 | error = -EIO; |
134 | goto out; | |
135 | } | |
136 | ||
b44b84d7 | 137 | eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header)); |
feaa7bba | 138 | end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs; |
b3b94faa DT |
139 | |
140 | for (; eablk < end; eablk++) { | |
cd915493 | 141 | u64 bn; |
b3b94faa DT |
142 | |
143 | if (!*eablk) | |
144 | break; | |
145 | bn = be64_to_cpu(*eablk); | |
146 | ||
c8d57703 | 147 | error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, 0, &eabh); |
b3b94faa DT |
148 | if (error) |
149 | break; | |
150 | error = ea_foreach_i(ip, eabh, ea_call, data); | |
151 | brelse(eabh); | |
152 | if (error) | |
153 | break; | |
154 | } | |
a91ea69f | 155 | out: |
b3b94faa | 156 | brelse(bh); |
b3b94faa DT |
157 | return error; |
158 | } | |
159 | ||
160 | struct ea_find { | |
40b78a32 SW |
161 | int type; |
162 | const char *name; | |
163 | size_t namel; | |
b3b94faa DT |
164 | struct gfs2_ea_location *ef_el; |
165 | }; | |
166 | ||
167 | static 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; | |
b3b94faa DT |
172 | |
173 | if (ea->ea_type == GFS2_EATYPE_UNUSED) | |
174 | return 0; | |
175 | ||
40b78a32 SW |
176 | 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)) { | |
b3b94faa DT |
179 | 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 | ||
b3b94faa DT |
188 | return 0; |
189 | } | |
190 | ||
479c427d SW |
191 | static int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name, |
192 | struct gfs2_ea_location *el) | |
b3b94faa DT |
193 | { |
194 | struct ea_find ef; | |
195 | int error; | |
196 | ||
40b78a32 SW |
197 | ef.type = type; |
198 | ef.name = name; | |
199 | ef.namel = strlen(name); | |
b3b94faa DT |
200 | 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 | ||
226 | static 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; | |
feaa7bba | 231 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
232 | struct gfs2_rgrpd *rgd; |
233 | struct gfs2_holder rg_gh; | |
b44b84d7 AV |
234 | __be64 *dataptrs; |
235 | u64 bn = 0; | |
cd915493 | 236 | u64 bstart = 0; |
b3b94faa DT |
237 | unsigned int blen = 0; |
238 | unsigned int blks = 0; | |
239 | unsigned int x; | |
240 | int error; | |
241 | ||
5e2f7d61 BP |
242 | error = gfs2_rindex_update(sdp); |
243 | if (error) | |
244 | return error; | |
245 | ||
b3b94faa DT |
246 | if (GFS2_EA_IS_STUFFED(ea)) |
247 | return 0; | |
248 | ||
249 | dataptrs = GFS2_EA2DATAPTRS(ea); | |
cca195c5 | 250 | for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) { |
b3b94faa DT |
251 | if (*dataptrs) { |
252 | blks++; | |
253 | bn = be64_to_cpu(*dataptrs); | |
254 | } | |
cca195c5 | 255 | } |
b3b94faa DT |
256 | if (!blks) |
257 | return 0; | |
258 | ||
66fc061b | 259 | rgd = gfs2_blk2rgrpd(sdp, bn, 1); |
b3b94faa DT |
260 | if (!rgd) { |
261 | gfs2_consist_inode(ip); | |
262 | return -EIO; | |
263 | } | |
264 | ||
265 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh); | |
266 | if (error) | |
267 | return error; | |
268 | ||
bb8d8a6f | 269 | error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE + |
cca195c5 | 270 | RES_EATTR + RES_STATFS + RES_QUOTA, blks); |
b3b94faa DT |
271 | if (error) |
272 | goto out_gunlock; | |
273 | ||
350a9b0a | 274 | gfs2_trans_add_meta(ip->i_gl, bh); |
b3b94faa DT |
275 | |
276 | dataptrs = GFS2_EA2DATAPTRS(ea); | |
277 | for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) { | |
278 | if (!*dataptrs) | |
279 | break; | |
280 | bn = be64_to_cpu(*dataptrs); | |
281 | ||
282 | if (bstart + blen == bn) | |
283 | blen++; | |
284 | else { | |
285 | if (bstart) | |
0ddeded4 | 286 | gfs2_free_meta(ip, rgd, bstart, blen); |
b3b94faa DT |
287 | bstart = bn; |
288 | blen = 1; | |
289 | } | |
290 | ||
291 | *dataptrs = 0; | |
77658aad | 292 | gfs2_add_inode_blocks(&ip->i_inode, -1); |
b3b94faa DT |
293 | } |
294 | if (bstart) | |
0ddeded4 | 295 | gfs2_free_meta(ip, rgd, bstart, blen); |
b3b94faa DT |
296 | |
297 | if (prev && !leave) { | |
cd915493 | 298 | u32 len; |
b3b94faa DT |
299 | |
300 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); | |
301 | prev->ea_rec_len = cpu_to_be32(len); | |
302 | ||
303 | if (GFS2_EA_IS_LAST(ea)) | |
304 | prev->ea_flags |= GFS2_EAFLAG_LAST; | |
305 | } else { | |
306 | ea->ea_type = GFS2_EATYPE_UNUSED; | |
307 | ea->ea_num_ptrs = 0; | |
308 | } | |
309 | ||
6862c44e | 310 | ip->i_inode.i_ctime = current_time(&ip->i_inode); |
937d3305 | 311 | __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC); |
b3b94faa DT |
312 | |
313 | gfs2_trans_end(sdp); | |
314 | ||
a91ea69f | 315 | out_gunlock: |
b3b94faa | 316 | gfs2_glock_dq_uninit(&rg_gh); |
b3b94faa DT |
317 | return error; |
318 | } | |
319 | ||
320 | static 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 | { | |
b3b94faa DT |
324 | int error; |
325 | ||
8e2e0047 BP |
326 | error = gfs2_rindex_update(GFS2_SB(&ip->i_inode)); |
327 | if (error) | |
328 | return error; | |
329 | ||
f4108a60 | 330 | error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); |
b3b94faa DT |
331 | if (error) |
332 | goto out_alloc; | |
333 | ||
cca195c5 | 334 | error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL); |
b3b94faa | 335 | |
b3b94faa | 336 | gfs2_quota_unhold(ip); |
a91ea69f | 337 | out_alloc: |
b3b94faa DT |
338 | return error; |
339 | } | |
340 | ||
b3b94faa DT |
341 | struct ea_list { |
342 | struct gfs2_ea_request *ei_er; | |
343 | unsigned int ei_size; | |
344 | }; | |
345 | ||
346 | static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh, | |
347 | struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, | |
348 | void *private) | |
349 | { | |
350 | struct ea_list *ei = private; | |
351 | struct gfs2_ea_request *er = ei->ei_er; | |
21e2156f AG |
352 | unsigned int ea_size; |
353 | char *prefix; | |
354 | unsigned int l; | |
b3b94faa DT |
355 | |
356 | if (ea->ea_type == GFS2_EATYPE_UNUSED) | |
357 | return 0; | |
358 | ||
21e2156f AG |
359 | switch (ea->ea_type) { |
360 | case GFS2_EATYPE_USR: | |
361 | prefix = "user."; | |
362 | l = 5; | |
363 | break; | |
364 | case GFS2_EATYPE_SYS: | |
365 | prefix = "system."; | |
366 | l = 7; | |
367 | break; | |
368 | case GFS2_EATYPE_SECURITY: | |
369 | prefix = "security."; | |
370 | l = 9; | |
371 | break; | |
372 | default: | |
373 | BUG(); | |
374 | } | |
b3b94faa | 375 | |
21e2156f AG |
376 | ea_size = l + ea->ea_name_len + 1; |
377 | if (er->er_data_len) { | |
b3b94faa DT |
378 | if (ei->ei_size + ea_size > er->er_data_len) |
379 | return -ERANGE; | |
380 | ||
90cdd208 SW |
381 | memcpy(er->er_data + ei->ei_size, prefix, l); |
382 | memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea), | |
b3b94faa | 383 | ea->ea_name_len); |
21e2156f | 384 | er->er_data[ei->ei_size + ea_size - 1] = 0; |
b3b94faa DT |
385 | } |
386 | ||
387 | ei->ei_size += ea_size; | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | /** | |
40b78a32 SW |
393 | * gfs2_listxattr - List gfs2 extended attributes |
394 | * @dentry: The dentry whose inode we are interested in | |
395 | * @buffer: The buffer to write the results | |
396 | * @size: The size of the buffer | |
b3b94faa DT |
397 | * |
398 | * Returns: actual size of data on success, -errno on error | |
399 | */ | |
400 | ||
40b78a32 | 401 | ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size) |
b3b94faa | 402 | { |
2b0143b5 | 403 | struct gfs2_inode *ip = GFS2_I(d_inode(dentry)); |
40b78a32 | 404 | struct gfs2_ea_request er; |
b3b94faa DT |
405 | struct gfs2_holder i_gh; |
406 | int error; | |
407 | ||
40b78a32 SW |
408 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
409 | if (size) { | |
410 | er.er_data = buffer; | |
411 | er.er_data_len = size; | |
b3b94faa DT |
412 | } |
413 | ||
cca195c5 | 414 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
b3b94faa DT |
415 | if (error) |
416 | return error; | |
417 | ||
3767ac21 | 418 | if (ip->i_eattr) { |
40b78a32 | 419 | struct ea_list ei = { .ei_er = &er, .ei_size = 0 }; |
b3b94faa DT |
420 | |
421 | error = ea_foreach(ip, ea_list_i, &ei); | |
422 | if (!error) | |
423 | error = ei.ei_size; | |
424 | } | |
425 | ||
426 | gfs2_glock_dq_uninit(&i_gh); | |
427 | ||
428 | return error; | |
429 | } | |
430 | ||
431 | /** | |
1f981697 SW |
432 | * ea_iter_unstuffed - copies the unstuffed xattr data to/from the |
433 | * request buffer | |
cca195c5 SW |
434 | * @ip: The GFS2 inode |
435 | * @ea: The extended attribute header structure | |
1f981697 SW |
436 | * @din: The data to be copied in |
437 | * @dout: The data to be copied out (one of din,dout will be NULL) | |
b3b94faa DT |
438 | * |
439 | * Returns: errno | |
440 | */ | |
441 | ||
1f981697 SW |
442 | static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea, |
443 | const char *din, char *dout) | |
b3b94faa | 444 | { |
feaa7bba | 445 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
446 | struct buffer_head **bh; |
447 | unsigned int amount = GFS2_EA_DATA_LEN(ea); | |
5c676f6d | 448 | unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize); |
b44b84d7 | 449 | __be64 *dataptrs = GFS2_EA2DATAPTRS(ea); |
b3b94faa DT |
450 | unsigned int x; |
451 | int error = 0; | |
1f981697 SW |
452 | unsigned char *pos; |
453 | unsigned cp_size; | |
b3b94faa | 454 | |
16c5f06f | 455 | bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS); |
b3b94faa DT |
456 | if (!bh) |
457 | return -ENOMEM; | |
458 | ||
459 | for (x = 0; x < nptrs; x++) { | |
c8d57703 | 460 | error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, 0, |
7276b3b0 | 461 | bh + x); |
b3b94faa DT |
462 | if (error) { |
463 | while (x--) | |
464 | brelse(bh[x]); | |
465 | goto out; | |
466 | } | |
467 | dataptrs++; | |
468 | } | |
469 | ||
470 | for (x = 0; x < nptrs; x++) { | |
7276b3b0 | 471 | error = gfs2_meta_wait(sdp, bh[x]); |
b3b94faa DT |
472 | if (error) { |
473 | for (; x < nptrs; x++) | |
474 | brelse(bh[x]); | |
475 | goto out; | |
476 | } | |
477 | if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) { | |
478 | for (; x < nptrs; x++) | |
479 | brelse(bh[x]); | |
480 | error = -EIO; | |
481 | goto out; | |
482 | } | |
483 | ||
1f981697 SW |
484 | pos = bh[x]->b_data + sizeof(struct gfs2_meta_header); |
485 | cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize; | |
b3b94faa | 486 | |
1f981697 SW |
487 | if (dout) { |
488 | memcpy(dout, pos, cp_size); | |
489 | dout += sdp->sd_jbsize; | |
490 | } | |
491 | ||
492 | if (din) { | |
350a9b0a | 493 | gfs2_trans_add_meta(ip->i_gl, bh[x]); |
1f981697 SW |
494 | memcpy(pos, din, cp_size); |
495 | din += sdp->sd_jbsize; | |
496 | } | |
b3b94faa | 497 | |
1f981697 | 498 | amount -= sdp->sd_jbsize; |
b3b94faa DT |
499 | brelse(bh[x]); |
500 | } | |
501 | ||
a91ea69f | 502 | out: |
b3b94faa | 503 | kfree(bh); |
b3b94faa DT |
504 | return error; |
505 | } | |
506 | ||
479c427d SW |
507 | static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el, |
508 | char *data, size_t size) | |
b3b94faa | 509 | { |
40b78a32 SW |
510 | int ret; |
511 | size_t len = GFS2_EA_DATA_LEN(el->el_ea); | |
512 | if (len > size) | |
513 | return -ERANGE; | |
514 | ||
b3b94faa | 515 | if (GFS2_EA_IS_STUFFED(el->el_ea)) { |
40b78a32 SW |
516 | memcpy(data, GFS2_EA2DATA(el->el_ea), len); |
517 | return len; | |
518 | } | |
1f981697 | 519 | ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data); |
40b78a32 SW |
520 | if (ret < 0) |
521 | return ret; | |
522 | return len; | |
b3b94faa DT |
523 | } |
524 | ||
479c427d SW |
525 | int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata) |
526 | { | |
527 | struct gfs2_ea_location el; | |
528 | int error; | |
529 | int len; | |
530 | char *data; | |
531 | ||
532 | error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el); | |
533 | if (error) | |
534 | return error; | |
535 | if (!el.el_ea) | |
536 | goto out; | |
537 | if (!GFS2_EA_DATA_LEN(el.el_ea)) | |
538 | goto out; | |
539 | ||
540 | len = GFS2_EA_DATA_LEN(el.el_ea); | |
541 | data = kmalloc(len, GFP_NOFS); | |
542 | error = -ENOMEM; | |
543 | if (data == NULL) | |
544 | goto out; | |
545 | ||
546 | error = gfs2_ea_get_copy(ip, &el, data, len); | |
114b80ce SW |
547 | if (error < 0) |
548 | kfree(data); | |
549 | else | |
550 | *ppdata = data; | |
479c427d SW |
551 | out: |
552 | brelse(el.el_bh); | |
553 | return error; | |
554 | } | |
555 | ||
b3b94faa | 556 | /** |
40b78a32 SW |
557 | * gfs2_xattr_get - Get a GFS2 extended attribute |
558 | * @inode: The inode | |
40b78a32 SW |
559 | * @name: The name of the extended attribute |
560 | * @buffer: The buffer to write the result into | |
561 | * @size: The size of the buffer | |
431547b3 | 562 | * @type: The type of extended attribute |
b3b94faa DT |
563 | * |
564 | * Returns: actual size of data on success, -errno on error | |
565 | */ | |
1a39ba99 AV |
566 | static int __gfs2_xattr_get(struct inode *inode, const char *name, |
567 | void *buffer, size_t size, int type) | |
b3b94faa | 568 | { |
b296821a | 569 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa DT |
570 | struct gfs2_ea_location el; |
571 | int error; | |
572 | ||
3767ac21 | 573 | if (!ip->i_eattr) |
b3b94faa | 574 | return -ENODATA; |
40b78a32 SW |
575 | if (strlen(name) > GFS2_EA_MAX_NAME_LEN) |
576 | return -EINVAL; | |
b3b94faa | 577 | |
40b78a32 | 578 | error = gfs2_ea_find(ip, type, name, &el); |
b3b94faa DT |
579 | if (error) |
580 | return error; | |
581 | if (!el.el_ea) | |
582 | return -ENODATA; | |
86d00636 | 583 | if (size) |
40b78a32 SW |
584 | error = gfs2_ea_get_copy(ip, &el, buffer, size); |
585 | else | |
b3b94faa | 586 | error = GFS2_EA_DATA_LEN(el.el_ea); |
b3b94faa DT |
587 | brelse(el.el_bh); |
588 | ||
589 | return error; | |
590 | } | |
591 | ||
1a39ba99 AV |
592 | static int gfs2_xattr_get(const struct xattr_handler *handler, |
593 | struct dentry *unused, struct inode *inode, | |
594 | const char *name, void *buffer, size_t size) | |
595 | { | |
596 | struct gfs2_inode *ip = GFS2_I(inode); | |
597 | struct gfs2_holder gh; | |
1a39ba99 AV |
598 | int ret; |
599 | ||
600 | /* During lookup, SELinux calls this function with the glock locked. */ | |
601 | ||
602 | if (!gfs2_glock_is_locked_by_me(ip->i_gl)) { | |
603 | ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); | |
604 | if (ret) | |
605 | return ret; | |
d0920a9c AG |
606 | } else { |
607 | gfs2_holder_mark_uninitialized(&gh); | |
1a39ba99 AV |
608 | } |
609 | ret = __gfs2_xattr_get(inode, name, buffer, size, handler->flags); | |
d0920a9c | 610 | if (gfs2_holder_initialized(&gh)) |
1a39ba99 AV |
611 | gfs2_glock_dq_uninit(&gh); |
612 | return ret; | |
613 | } | |
614 | ||
b3b94faa DT |
615 | /** |
616 | * ea_alloc_blk - allocates a new block for extended attributes. | |
617 | * @ip: A pointer to the inode that's getting extended attributes | |
cca195c5 | 618 | * @bhp: Pointer to pointer to a struct buffer_head |
b3b94faa DT |
619 | * |
620 | * Returns: errno | |
621 | */ | |
622 | ||
623 | static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp) | |
624 | { | |
feaa7bba | 625 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa | 626 | struct gfs2_ea_header *ea; |
b45e41d7 | 627 | unsigned int n = 1; |
cd915493 | 628 | u64 block; |
09010978 | 629 | int error; |
b3b94faa | 630 | |
6e87ed0f | 631 | error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL); |
09010978 SW |
632 | if (error) |
633 | return error; | |
fbb27873 | 634 | gfs2_trans_remove_revoke(sdp, block, 1); |
b3b94faa | 635 | *bhp = gfs2_meta_new(ip->i_gl, block); |
350a9b0a | 636 | gfs2_trans_add_meta(ip->i_gl, *bhp); |
b3b94faa DT |
637 | gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA); |
638 | gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header)); | |
639 | ||
640 | ea = GFS2_EA_BH2FIRST(*bhp); | |
641 | ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize); | |
642 | ea->ea_type = GFS2_EATYPE_UNUSED; | |
643 | ea->ea_flags = GFS2_EAFLAG_LAST; | |
644 | ea->ea_num_ptrs = 0; | |
645 | ||
77658aad | 646 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
b3b94faa DT |
647 | |
648 | return 0; | |
649 | } | |
650 | ||
651 | /** | |
652 | * ea_write - writes the request info to an ea, creating new blocks if | |
653 | * necessary | |
cca195c5 SW |
654 | * @ip: inode that is being modified |
655 | * @ea: the location of the new ea in a block | |
b3b94faa DT |
656 | * @er: the write request |
657 | * | |
658 | * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags | |
659 | * | |
660 | * returns : errno | |
661 | */ | |
662 | ||
663 | static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea, | |
664 | struct gfs2_ea_request *er) | |
665 | { | |
feaa7bba | 666 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
09010978 | 667 | int error; |
b3b94faa DT |
668 | |
669 | ea->ea_data_len = cpu_to_be32(er->er_data_len); | |
670 | ea->ea_name_len = er->er_name_len; | |
671 | ea->ea_type = er->er_type; | |
672 | ea->__pad = 0; | |
673 | ||
674 | memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len); | |
675 | ||
676 | if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) { | |
677 | ea->ea_num_ptrs = 0; | |
678 | memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len); | |
679 | } else { | |
b44b84d7 | 680 | __be64 *dataptr = GFS2_EA2DATAPTRS(ea); |
b3b94faa DT |
681 | const char *data = er->er_data; |
682 | unsigned int data_len = er->er_data_len; | |
683 | unsigned int copy; | |
684 | unsigned int x; | |
685 | ||
5c676f6d | 686 | ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize); |
b3b94faa DT |
687 | for (x = 0; x < ea->ea_num_ptrs; x++) { |
688 | struct buffer_head *bh; | |
cd915493 | 689 | u64 block; |
b3b94faa | 690 | int mh_size = sizeof(struct gfs2_meta_header); |
b45e41d7 | 691 | unsigned int n = 1; |
b3b94faa | 692 | |
6e87ed0f | 693 | error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL); |
09010978 SW |
694 | if (error) |
695 | return error; | |
fbb27873 | 696 | gfs2_trans_remove_revoke(sdp, block, 1); |
b3b94faa | 697 | bh = gfs2_meta_new(ip->i_gl, block); |
350a9b0a | 698 | gfs2_trans_add_meta(ip->i_gl, bh); |
b3b94faa DT |
699 | gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED); |
700 | ||
77658aad | 701 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
b3b94faa | 702 | |
cca195c5 SW |
703 | copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize : |
704 | data_len; | |
b3b94faa DT |
705 | memcpy(bh->b_data + mh_size, data, copy); |
706 | if (copy < sdp->sd_jbsize) | |
707 | memset(bh->b_data + mh_size + copy, 0, | |
708 | sdp->sd_jbsize - copy); | |
709 | ||
cca195c5 | 710 | *dataptr++ = cpu_to_be64(bh->b_blocknr); |
b3b94faa DT |
711 | data += copy; |
712 | data_len -= copy; | |
713 | ||
714 | brelse(bh); | |
715 | } | |
716 | ||
717 | gfs2_assert_withdraw(sdp, !data_len); | |
718 | } | |
719 | ||
720 | return 0; | |
721 | } | |
722 | ||
723 | typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip, | |
cca195c5 | 724 | struct gfs2_ea_request *er, void *private); |
b3b94faa DT |
725 | |
726 | static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er, | |
727 | unsigned int blks, | |
cca195c5 | 728 | ea_skeleton_call_t skeleton_call, void *private) |
b3b94faa | 729 | { |
7b9cff46 | 730 | struct gfs2_alloc_parms ap = { .target = blks }; |
b3b94faa DT |
731 | int error; |
732 | ||
8e2e0047 BP |
733 | error = gfs2_rindex_update(GFS2_SB(&ip->i_inode)); |
734 | if (error) | |
735 | return error; | |
736 | ||
b8fbf471 | 737 | error = gfs2_quota_lock_check(ip, &ap); |
b3b94faa | 738 | if (error) |
5407e242 | 739 | return error; |
b3b94faa | 740 | |
7b9cff46 | 741 | error = gfs2_inplace_reserve(ip, &ap); |
b3b94faa DT |
742 | if (error) |
743 | goto out_gunlock_q; | |
744 | ||
feaa7bba | 745 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), |
71f890f7 | 746 | blks + gfs2_rg_blocks(ip, blks) + |
b3b94faa DT |
747 | RES_DINODE + RES_STATFS + RES_QUOTA, 0); |
748 | if (error) | |
749 | goto out_ipres; | |
750 | ||
751 | error = skeleton_call(ip, er, private); | |
752 | if (error) | |
753 | goto out_end_trans; | |
754 | ||
6862c44e | 755 | ip->i_inode.i_ctime = current_time(&ip->i_inode); |
937d3305 | 756 | __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC); |
b3b94faa | 757 | |
a91ea69f | 758 | out_end_trans: |
feaa7bba | 759 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
a91ea69f | 760 | out_ipres: |
b3b94faa | 761 | gfs2_inplace_release(ip); |
a91ea69f | 762 | out_gunlock_q: |
b3b94faa | 763 | gfs2_quota_unlock(ip); |
b3b94faa DT |
764 | return error; |
765 | } | |
766 | ||
767 | static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er, | |
768 | void *private) | |
769 | { | |
770 | struct buffer_head *bh; | |
771 | int error; | |
772 | ||
773 | error = ea_alloc_blk(ip, &bh); | |
774 | if (error) | |
775 | return error; | |
776 | ||
3767ac21 | 777 | ip->i_eattr = bh->b_blocknr; |
b3b94faa DT |
778 | error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er); |
779 | ||
780 | brelse(bh); | |
781 | ||
782 | return error; | |
783 | } | |
784 | ||
785 | /** | |
786 | * ea_init - initializes a new eattr block | |
787 | * @ip: | |
788 | * @er: | |
789 | * | |
790 | * Returns: errno | |
791 | */ | |
792 | ||
40b78a32 SW |
793 | static int ea_init(struct gfs2_inode *ip, int type, const char *name, |
794 | const void *data, size_t size) | |
b3b94faa | 795 | { |
40b78a32 | 796 | struct gfs2_ea_request er; |
feaa7bba | 797 | unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize; |
b3b94faa DT |
798 | unsigned int blks = 1; |
799 | ||
40b78a32 SW |
800 | er.er_type = type; |
801 | er.er_name = name; | |
802 | er.er_name_len = strlen(name); | |
803 | er.er_data = (void *)data; | |
804 | er.er_data_len = size; | |
805 | ||
806 | if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize) | |
807 | blks += DIV_ROUND_UP(er.er_data_len, jbsize); | |
b3b94faa | 808 | |
40b78a32 | 809 | return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL); |
b3b94faa DT |
810 | } |
811 | ||
812 | static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea) | |
813 | { | |
cd915493 | 814 | u32 ea_size = GFS2_EA_SIZE(ea); |
568f4c96 SW |
815 | struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea + |
816 | ea_size); | |
cd915493 | 817 | u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size; |
b3b94faa DT |
818 | int last = ea->ea_flags & GFS2_EAFLAG_LAST; |
819 | ||
820 | ea->ea_rec_len = cpu_to_be32(ea_size); | |
821 | ea->ea_flags ^= last; | |
822 | ||
823 | new->ea_rec_len = cpu_to_be32(new_size); | |
824 | new->ea_flags = last; | |
825 | ||
826 | return new; | |
827 | } | |
828 | ||
829 | static void ea_set_remove_stuffed(struct gfs2_inode *ip, | |
830 | struct gfs2_ea_location *el) | |
831 | { | |
832 | struct gfs2_ea_header *ea = el->el_ea; | |
833 | struct gfs2_ea_header *prev = el->el_prev; | |
cd915493 | 834 | u32 len; |
b3b94faa | 835 | |
350a9b0a | 836 | gfs2_trans_add_meta(ip->i_gl, el->el_bh); |
b3b94faa DT |
837 | |
838 | if (!prev || !GFS2_EA_IS_STUFFED(ea)) { | |
839 | ea->ea_type = GFS2_EATYPE_UNUSED; | |
840 | return; | |
841 | } else if (GFS2_EA2NEXT(prev) != ea) { | |
842 | prev = GFS2_EA2NEXT(prev); | |
feaa7bba | 843 | gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea); |
b3b94faa DT |
844 | } |
845 | ||
846 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); | |
847 | prev->ea_rec_len = cpu_to_be32(len); | |
848 | ||
849 | if (GFS2_EA_IS_LAST(ea)) | |
850 | prev->ea_flags |= GFS2_EAFLAG_LAST; | |
851 | } | |
852 | ||
853 | struct ea_set { | |
854 | int ea_split; | |
855 | ||
856 | struct gfs2_ea_request *es_er; | |
857 | struct gfs2_ea_location *es_el; | |
858 | ||
859 | struct buffer_head *es_bh; | |
860 | struct gfs2_ea_header *es_ea; | |
861 | }; | |
862 | ||
863 | static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh, | |
864 | struct gfs2_ea_header *ea, struct ea_set *es) | |
865 | { | |
866 | struct gfs2_ea_request *er = es->es_er; | |
b3b94faa DT |
867 | int error; |
868 | ||
feaa7bba | 869 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0); |
b3b94faa DT |
870 | if (error) |
871 | return error; | |
872 | ||
350a9b0a | 873 | gfs2_trans_add_meta(ip->i_gl, bh); |
b3b94faa DT |
874 | |
875 | if (es->ea_split) | |
876 | ea = ea_split_ea(ea); | |
877 | ||
878 | ea_write(ip, ea, er); | |
879 | ||
880 | if (es->es_el) | |
881 | ea_set_remove_stuffed(ip, es->es_el); | |
882 | ||
078cd827 | 883 | ip->i_inode.i_ctime = current_time(&ip->i_inode); |
937d3305 | 884 | __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC); |
6862c44e | 885 | |
feaa7bba | 886 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
b3b94faa DT |
887 | return error; |
888 | } | |
889 | ||
890 | static int ea_set_simple_alloc(struct gfs2_inode *ip, | |
891 | struct gfs2_ea_request *er, void *private) | |
892 | { | |
893 | struct ea_set *es = private; | |
894 | struct gfs2_ea_header *ea = es->es_ea; | |
895 | int error; | |
896 | ||
350a9b0a | 897 | gfs2_trans_add_meta(ip->i_gl, es->es_bh); |
b3b94faa DT |
898 | |
899 | if (es->ea_split) | |
900 | ea = ea_split_ea(ea); | |
901 | ||
902 | error = ea_write(ip, ea, er); | |
903 | if (error) | |
904 | return error; | |
905 | ||
906 | if (es->es_el) | |
907 | ea_set_remove_stuffed(ip, es->es_el); | |
908 | ||
909 | return 0; | |
910 | } | |
911 | ||
912 | static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh, | |
913 | struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, | |
914 | void *private) | |
915 | { | |
916 | struct ea_set *es = private; | |
917 | unsigned int size; | |
918 | int stuffed; | |
919 | int error; | |
920 | ||
40b78a32 SW |
921 | stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len, |
922 | es->es_er->er_data_len, &size); | |
b3b94faa DT |
923 | |
924 | if (ea->ea_type == GFS2_EATYPE_UNUSED) { | |
925 | if (GFS2_EA_REC_LEN(ea) < size) | |
926 | return 0; | |
927 | if (!GFS2_EA_IS_STUFFED(ea)) { | |
928 | error = ea_remove_unstuffed(ip, bh, ea, prev, 1); | |
929 | if (error) | |
930 | return error; | |
931 | } | |
932 | es->ea_split = 0; | |
933 | } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size) | |
934 | es->ea_split = 1; | |
935 | else | |
936 | return 0; | |
937 | ||
938 | if (stuffed) { | |
939 | error = ea_set_simple_noalloc(ip, bh, ea, es); | |
940 | if (error) | |
941 | return error; | |
942 | } else { | |
943 | unsigned int blks; | |
944 | ||
945 | es->es_bh = bh; | |
946 | es->es_ea = ea; | |
5c676f6d | 947 | blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len, |
feaa7bba | 948 | GFS2_SB(&ip->i_inode)->sd_jbsize); |
b3b94faa DT |
949 | |
950 | error = ea_alloc_skeleton(ip, es->es_er, blks, | |
951 | ea_set_simple_alloc, es); | |
952 | if (error) | |
953 | return error; | |
954 | } | |
955 | ||
956 | return 1; | |
957 | } | |
958 | ||
959 | static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er, | |
960 | void *private) | |
961 | { | |
feaa7bba | 962 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa | 963 | struct buffer_head *indbh, *newbh; |
b44b84d7 | 964 | __be64 *eablk; |
b3b94faa DT |
965 | int error; |
966 | int mh_size = sizeof(struct gfs2_meta_header); | |
967 | ||
383f01fb | 968 | if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) { |
b44b84d7 | 969 | __be64 *end; |
b3b94faa | 970 | |
c8d57703 | 971 | error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, |
7276b3b0 | 972 | &indbh); |
b3b94faa DT |
973 | if (error) |
974 | return error; | |
975 | ||
976 | if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) { | |
977 | error = -EIO; | |
978 | goto out; | |
979 | } | |
980 | ||
b44b84d7 | 981 | eablk = (__be64 *)(indbh->b_data + mh_size); |
b3b94faa DT |
982 | end = eablk + sdp->sd_inptrs; |
983 | ||
984 | for (; eablk < end; eablk++) | |
985 | if (!*eablk) | |
986 | break; | |
987 | ||
988 | if (eablk == end) { | |
989 | error = -ENOSPC; | |
990 | goto out; | |
991 | } | |
992 | ||
350a9b0a | 993 | gfs2_trans_add_meta(ip->i_gl, indbh); |
b3b94faa | 994 | } else { |
cd915493 | 995 | u64 blk; |
b45e41d7 | 996 | unsigned int n = 1; |
6e87ed0f | 997 | error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL); |
09010978 SW |
998 | if (error) |
999 | return error; | |
fbb27873 | 1000 | gfs2_trans_remove_revoke(sdp, blk, 1); |
b3b94faa | 1001 | indbh = gfs2_meta_new(ip->i_gl, blk); |
350a9b0a | 1002 | gfs2_trans_add_meta(ip->i_gl, indbh); |
b3b94faa DT |
1003 | gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN); |
1004 | gfs2_buffer_clear_tail(indbh, mh_size); | |
1005 | ||
b44b84d7 | 1006 | eablk = (__be64 *)(indbh->b_data + mh_size); |
3767ac21 SW |
1007 | *eablk = cpu_to_be64(ip->i_eattr); |
1008 | ip->i_eattr = blk; | |
383f01fb | 1009 | ip->i_diskflags |= GFS2_DIF_EA_INDIRECT; |
77658aad | 1010 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
b3b94faa DT |
1011 | |
1012 | eablk++; | |
1013 | } | |
1014 | ||
1015 | error = ea_alloc_blk(ip, &newbh); | |
1016 | if (error) | |
1017 | goto out; | |
1018 | ||
cd915493 | 1019 | *eablk = cpu_to_be64((u64)newbh->b_blocknr); |
b3b94faa DT |
1020 | error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er); |
1021 | brelse(newbh); | |
1022 | if (error) | |
1023 | goto out; | |
1024 | ||
1025 | if (private) | |
cca195c5 | 1026 | ea_set_remove_stuffed(ip, private); |
b3b94faa | 1027 | |
a91ea69f | 1028 | out: |
b3b94faa | 1029 | brelse(indbh); |
b3b94faa DT |
1030 | return error; |
1031 | } | |
1032 | ||
40b78a32 SW |
1033 | static int ea_set_i(struct gfs2_inode *ip, int type, const char *name, |
1034 | const void *value, size_t size, struct gfs2_ea_location *el) | |
b3b94faa | 1035 | { |
40b78a32 | 1036 | struct gfs2_ea_request er; |
b3b94faa DT |
1037 | struct ea_set es; |
1038 | unsigned int blks = 2; | |
1039 | int error; | |
1040 | ||
40b78a32 SW |
1041 | er.er_type = type; |
1042 | er.er_name = name; | |
1043 | er.er_data = (void *)value; | |
1044 | er.er_name_len = strlen(name); | |
1045 | er.er_data_len = size; | |
1046 | ||
b3b94faa | 1047 | memset(&es, 0, sizeof(struct ea_set)); |
40b78a32 | 1048 | es.es_er = &er; |
b3b94faa DT |
1049 | es.es_el = el; |
1050 | ||
1051 | error = ea_foreach(ip, ea_set_simple, &es); | |
1052 | if (error > 0) | |
1053 | return 0; | |
1054 | if (error) | |
1055 | return error; | |
1056 | ||
383f01fb | 1057 | if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) |
b3b94faa | 1058 | blks++; |
40b78a32 SW |
1059 | if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize) |
1060 | blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize); | |
b3b94faa | 1061 | |
40b78a32 | 1062 | return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el); |
b3b94faa DT |
1063 | } |
1064 | ||
1065 | static int ea_set_remove_unstuffed(struct gfs2_inode *ip, | |
1066 | struct gfs2_ea_location *el) | |
1067 | { | |
1068 | if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) { | |
1069 | el->el_prev = GFS2_EA2NEXT(el->el_prev); | |
feaa7bba | 1070 | gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), |
b3b94faa DT |
1071 | GFS2_EA2NEXT(el->el_prev) == el->el_ea); |
1072 | } | |
1073 | ||
86d00636 | 1074 | return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0); |
b3b94faa DT |
1075 | } |
1076 | ||
b3b94faa DT |
1077 | static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el) |
1078 | { | |
1079 | struct gfs2_ea_header *ea = el->el_ea; | |
1080 | struct gfs2_ea_header *prev = el->el_prev; | |
b3b94faa DT |
1081 | int error; |
1082 | ||
feaa7bba | 1083 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0); |
b3b94faa DT |
1084 | if (error) |
1085 | return error; | |
1086 | ||
350a9b0a | 1087 | gfs2_trans_add_meta(ip->i_gl, el->el_bh); |
b3b94faa DT |
1088 | |
1089 | if (prev) { | |
cd915493 | 1090 | u32 len; |
b3b94faa DT |
1091 | |
1092 | len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); | |
1093 | prev->ea_rec_len = cpu_to_be32(len); | |
1094 | ||
1095 | if (GFS2_EA_IS_LAST(ea)) | |
1096 | prev->ea_flags |= GFS2_EAFLAG_LAST; | |
40b78a32 | 1097 | } else { |
b3b94faa | 1098 | ea->ea_type = GFS2_EATYPE_UNUSED; |
40b78a32 | 1099 | } |
b3b94faa | 1100 | |
6862c44e | 1101 | ip->i_inode.i_ctime = current_time(&ip->i_inode); |
937d3305 | 1102 | __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC); |
b3b94faa | 1103 | |
feaa7bba | 1104 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
b3b94faa DT |
1105 | |
1106 | return error; | |
1107 | } | |
1108 | ||
40b78a32 SW |
1109 | /** |
1110 | * gfs2_xattr_remove - Remove a GFS2 extended attribute | |
431547b3 | 1111 | * @ip: The inode |
40b78a32 SW |
1112 | * @type: The type of the extended attribute |
1113 | * @name: The name of the extended attribute | |
1114 | * | |
1115 | * This is not called directly by the VFS since we use the (common) | |
1116 | * scheme of making a "set with NULL data" mean a remove request. Note | |
1117 | * that this is different from a set with zero length data. | |
1118 | * | |
1119 | * Returns: 0, or errno on failure | |
1120 | */ | |
1121 | ||
431547b3 | 1122 | static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name) |
b3b94faa DT |
1123 | { |
1124 | struct gfs2_ea_location el; | |
1125 | int error; | |
1126 | ||
3767ac21 | 1127 | if (!ip->i_eattr) |
b3b94faa DT |
1128 | return -ENODATA; |
1129 | ||
40b78a32 | 1130 | error = gfs2_ea_find(ip, type, name, &el); |
b3b94faa DT |
1131 | if (error) |
1132 | return error; | |
1133 | if (!el.el_ea) | |
1134 | return -ENODATA; | |
1135 | ||
1136 | if (GFS2_EA_IS_STUFFED(el.el_ea)) | |
1137 | error = ea_remove_stuffed(ip, &el); | |
1138 | else | |
40b78a32 | 1139 | error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0); |
b3b94faa DT |
1140 | |
1141 | brelse(el.el_bh); | |
1142 | ||
1143 | return error; | |
1144 | } | |
1145 | ||
1146 | /** | |
431547b3 CH |
1147 | * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute |
1148 | * @ip: The inode | |
40b78a32 SW |
1149 | * @name: The name of the extended attribute |
1150 | * @value: The value of the extended attribute (NULL for remove) | |
1151 | * @size: The size of the @value argument | |
1152 | * @flags: Create or Replace | |
431547b3 | 1153 | * @type: The type of the extended attribute |
b3b94faa | 1154 | * |
40b78a32 SW |
1155 | * See gfs2_xattr_remove() for details of the removal of xattrs. |
1156 | * | |
1157 | * Returns: 0 or errno on failure | |
b3b94faa DT |
1158 | */ |
1159 | ||
431547b3 CH |
1160 | int __gfs2_xattr_set(struct inode *inode, const char *name, |
1161 | const void *value, size_t size, int flags, int type) | |
b3b94faa | 1162 | { |
40b78a32 | 1163 | struct gfs2_inode *ip = GFS2_I(inode); |
431547b3 | 1164 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
40b78a32 SW |
1165 | struct gfs2_ea_location el; |
1166 | unsigned int namel = strlen(name); | |
b3b94faa DT |
1167 | int error; |
1168 | ||
40b78a32 SW |
1169 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
1170 | return -EPERM; | |
1171 | if (namel > GFS2_EA_MAX_NAME_LEN) | |
1172 | return -ERANGE; | |
b3b94faa | 1173 | |
54aae14b EF |
1174 | if (value == NULL) { |
1175 | error = gfs2_xattr_remove(ip, type, name); | |
1176 | if (error == -ENODATA && !(flags & XATTR_REPLACE)) | |
1177 | error = 0; | |
1178 | return error; | |
1179 | } | |
40b78a32 SW |
1180 | |
1181 | if (ea_check_size(sdp, namel, size)) | |
1182 | return -ERANGE; | |
1183 | ||
1184 | if (!ip->i_eattr) { | |
1185 | if (flags & XATTR_REPLACE) | |
1186 | return -ENODATA; | |
1187 | return ea_init(ip, type, name, value, size); | |
1188 | } | |
1189 | ||
1190 | error = gfs2_ea_find(ip, type, name, &el); | |
b3b94faa DT |
1191 | if (error) |
1192 | return error; | |
1193 | ||
40b78a32 SW |
1194 | if (el.el_ea) { |
1195 | if (ip->i_diskflags & GFS2_DIF_APPENDONLY) { | |
1196 | brelse(el.el_bh); | |
1197 | return -EPERM; | |
1198 | } | |
b3b94faa | 1199 | |
40b78a32 SW |
1200 | error = -EEXIST; |
1201 | if (!(flags & XATTR_CREATE)) { | |
1202 | int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea); | |
1203 | error = ea_set_i(ip, type, name, value, size, &el); | |
1204 | if (!error && unstuffed) | |
1205 | ea_set_remove_unstuffed(ip, &el); | |
1206 | } | |
1207 | ||
1208 | brelse(el.el_bh); | |
1209 | return error; | |
1210 | } | |
1211 | ||
1212 | error = -ENODATA; | |
1213 | if (!(flags & XATTR_REPLACE)) | |
1214 | error = ea_set_i(ip, type, name, value, size, NULL); | |
b3b94faa DT |
1215 | |
1216 | return error; | |
1217 | } | |
1218 | ||
d9a82a04 | 1219 | static int gfs2_xattr_set(const struct xattr_handler *handler, |
59301226 AV |
1220 | struct dentry *unused, struct inode *inode, |
1221 | const char *name, const void *value, | |
1222 | size_t size, int flags) | |
431547b3 | 1223 | { |
1a39ba99 AV |
1224 | struct gfs2_inode *ip = GFS2_I(inode); |
1225 | struct gfs2_holder gh; | |
1226 | int ret; | |
1227 | ||
1228 | ret = gfs2_rsqa_alloc(ip); | |
1229 | if (ret) | |
1230 | return ret; | |
1231 | ||
d0920a9c AG |
1232 | /* May be called from gfs_setattr with the glock locked. */ |
1233 | ||
1234 | if (!gfs2_glock_is_locked_by_me(ip->i_gl)) { | |
1235 | ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); | |
1236 | if (ret) | |
1237 | return ret; | |
1238 | } else { | |
1239 | if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE)) | |
1240 | return -EIO; | |
1241 | gfs2_holder_mark_uninitialized(&gh); | |
1242 | } | |
1a39ba99 | 1243 | ret = __gfs2_xattr_set(inode, name, value, size, flags, handler->flags); |
d0920a9c AG |
1244 | if (gfs2_holder_initialized(&gh)) |
1245 | gfs2_glock_dq_uninit(&gh); | |
1a39ba99 | 1246 | return ret; |
431547b3 CH |
1247 | } |
1248 | ||
b3b94faa DT |
1249 | static int ea_dealloc_indirect(struct gfs2_inode *ip) |
1250 | { | |
feaa7bba | 1251 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa | 1252 | struct gfs2_rgrp_list rlist; |
0ddeded4 | 1253 | struct gfs2_rgrpd *rgd; |
b3b94faa | 1254 | struct buffer_head *indbh, *dibh; |
b44b84d7 | 1255 | __be64 *eablk, *end; |
b3b94faa | 1256 | unsigned int rg_blocks = 0; |
cd915493 | 1257 | u64 bstart = 0; |
b3b94faa DT |
1258 | unsigned int blen = 0; |
1259 | unsigned int blks = 0; | |
1260 | unsigned int x; | |
1261 | int error; | |
1262 | ||
5e2f7d61 BP |
1263 | error = gfs2_rindex_update(sdp); |
1264 | if (error) | |
1265 | return error; | |
1266 | ||
b3b94faa DT |
1267 | memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); |
1268 | ||
c8d57703 | 1269 | error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &indbh); |
b3b94faa DT |
1270 | if (error) |
1271 | return error; | |
1272 | ||
1273 | if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) { | |
1274 | error = -EIO; | |
1275 | goto out; | |
1276 | } | |
1277 | ||
b44b84d7 | 1278 | eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header)); |
b3b94faa DT |
1279 | end = eablk + sdp->sd_inptrs; |
1280 | ||
1281 | for (; eablk < end; eablk++) { | |
cd915493 | 1282 | u64 bn; |
b3b94faa DT |
1283 | |
1284 | if (!*eablk) | |
1285 | break; | |
1286 | bn = be64_to_cpu(*eablk); | |
1287 | ||
1288 | if (bstart + blen == bn) | |
1289 | blen++; | |
1290 | else { | |
1291 | if (bstart) | |
70b0c365 | 1292 | gfs2_rlist_add(ip, &rlist, bstart); |
b3b94faa DT |
1293 | bstart = bn; |
1294 | blen = 1; | |
1295 | } | |
1296 | blks++; | |
1297 | } | |
1298 | if (bstart) | |
70b0c365 | 1299 | gfs2_rlist_add(ip, &rlist, bstart); |
b3b94faa DT |
1300 | else |
1301 | goto out; | |
1302 | ||
c3abc29e | 1303 | gfs2_rlist_alloc(&rlist); |
b3b94faa DT |
1304 | |
1305 | for (x = 0; x < rlist.rl_rgrps; x++) { | |
0ddeded4 | 1306 | rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl); |
bb8d8a6f | 1307 | rg_blocks += rgd->rd_length; |
b3b94faa DT |
1308 | } |
1309 | ||
1310 | error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs); | |
1311 | if (error) | |
1312 | goto out_rlist_free; | |
1313 | ||
cca195c5 SW |
1314 | error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT + |
1315 | RES_STATFS + RES_QUOTA, blks); | |
b3b94faa DT |
1316 | if (error) |
1317 | goto out_gunlock; | |
1318 | ||
350a9b0a | 1319 | gfs2_trans_add_meta(ip->i_gl, indbh); |
b3b94faa | 1320 | |
b44b84d7 | 1321 | eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header)); |
b3b94faa | 1322 | bstart = 0; |
0ddeded4 | 1323 | rgd = NULL; |
b3b94faa DT |
1324 | blen = 0; |
1325 | ||
1326 | for (; eablk < end; eablk++) { | |
cd915493 | 1327 | u64 bn; |
b3b94faa DT |
1328 | |
1329 | if (!*eablk) | |
1330 | break; | |
1331 | bn = be64_to_cpu(*eablk); | |
1332 | ||
1333 | if (bstart + blen == bn) | |
1334 | blen++; | |
1335 | else { | |
1336 | if (bstart) | |
0ddeded4 | 1337 | gfs2_free_meta(ip, rgd, bstart, blen); |
b3b94faa | 1338 | bstart = bn; |
0ddeded4 | 1339 | rgd = gfs2_blk2rgrpd(sdp, bstart, true); |
b3b94faa DT |
1340 | blen = 1; |
1341 | } | |
1342 | ||
1343 | *eablk = 0; | |
77658aad | 1344 | gfs2_add_inode_blocks(&ip->i_inode, -1); |
b3b94faa DT |
1345 | } |
1346 | if (bstart) | |
0ddeded4 | 1347 | gfs2_free_meta(ip, rgd, bstart, blen); |
b3b94faa | 1348 | |
383f01fb | 1349 | ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT; |
b3b94faa DT |
1350 | |
1351 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1352 | if (!error) { | |
350a9b0a | 1353 | gfs2_trans_add_meta(ip->i_gl, dibh); |
539e5d6b | 1354 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
1355 | brelse(dibh); |
1356 | } | |
1357 | ||
1358 | gfs2_trans_end(sdp); | |
1359 | ||
a91ea69f | 1360 | out_gunlock: |
b3b94faa | 1361 | gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); |
a91ea69f | 1362 | out_rlist_free: |
b3b94faa | 1363 | gfs2_rlist_free(&rlist); |
a91ea69f | 1364 | out: |
b3b94faa | 1365 | brelse(indbh); |
b3b94faa DT |
1366 | return error; |
1367 | } | |
1368 | ||
1369 | static int ea_dealloc_block(struct gfs2_inode *ip) | |
1370 | { | |
feaa7bba | 1371 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1372 | struct gfs2_rgrpd *rgd; |
1373 | struct buffer_head *dibh; | |
564e12b1 | 1374 | struct gfs2_holder gh; |
b3b94faa DT |
1375 | int error; |
1376 | ||
5e2f7d61 BP |
1377 | error = gfs2_rindex_update(sdp); |
1378 | if (error) | |
1379 | return error; | |
1380 | ||
66fc061b | 1381 | rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1); |
b3b94faa DT |
1382 | if (!rgd) { |
1383 | gfs2_consist_inode(ip); | |
1384 | return -EIO; | |
1385 | } | |
1386 | ||
564e12b1 | 1387 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh); |
b3b94faa DT |
1388 | if (error) |
1389 | return error; | |
1390 | ||
cca195c5 SW |
1391 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS + |
1392 | RES_QUOTA, 1); | |
b3b94faa DT |
1393 | if (error) |
1394 | goto out_gunlock; | |
1395 | ||
0ddeded4 | 1396 | gfs2_free_meta(ip, rgd, ip->i_eattr, 1); |
b3b94faa | 1397 | |
3767ac21 | 1398 | ip->i_eattr = 0; |
77658aad | 1399 | gfs2_add_inode_blocks(&ip->i_inode, -1); |
b3b94faa DT |
1400 | |
1401 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1402 | if (!error) { | |
350a9b0a | 1403 | gfs2_trans_add_meta(ip->i_gl, dibh); |
539e5d6b | 1404 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
1405 | brelse(dibh); |
1406 | } | |
1407 | ||
1408 | gfs2_trans_end(sdp); | |
1409 | ||
a91ea69f | 1410 | out_gunlock: |
564e12b1 | 1411 | gfs2_glock_dq_uninit(&gh); |
b3b94faa DT |
1412 | return error; |
1413 | } | |
1414 | ||
1415 | /** | |
1416 | * gfs2_ea_dealloc - deallocate the extended attribute fork | |
1417 | * @ip: the inode | |
1418 | * | |
1419 | * Returns: errno | |
1420 | */ | |
1421 | ||
1422 | int gfs2_ea_dealloc(struct gfs2_inode *ip) | |
1423 | { | |
b3b94faa DT |
1424 | int error; |
1425 | ||
8e2e0047 BP |
1426 | error = gfs2_rindex_update(GFS2_SB(&ip->i_inode)); |
1427 | if (error) | |
1428 | return error; | |
1429 | ||
f4108a60 | 1430 | error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); |
b3b94faa | 1431 | if (error) |
5407e242 | 1432 | return error; |
b3b94faa | 1433 | |
b3b94faa DT |
1434 | error = ea_foreach(ip, ea_dealloc_unstuffed, NULL); |
1435 | if (error) | |
8339ee54 | 1436 | goto out_quota; |
b3b94faa | 1437 | |
383f01fb | 1438 | if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) { |
b3b94faa DT |
1439 | error = ea_dealloc_indirect(ip); |
1440 | if (error) | |
8339ee54 | 1441 | goto out_quota; |
b3b94faa DT |
1442 | } |
1443 | ||
1444 | error = ea_dealloc_block(ip); | |
1445 | ||
a91ea69f | 1446 | out_quota: |
b3b94faa | 1447 | gfs2_quota_unhold(ip); |
b3b94faa DT |
1448 | return error; |
1449 | } | |
1450 | ||
b7bb0a12 | 1451 | static const struct xattr_handler gfs2_xattr_user_handler = { |
40b78a32 | 1452 | .prefix = XATTR_USER_PREFIX, |
431547b3 CH |
1453 | .flags = GFS2_EATYPE_USR, |
1454 | .get = gfs2_xattr_get, | |
1455 | .set = gfs2_xattr_set, | |
40b78a32 SW |
1456 | }; |
1457 | ||
b7bb0a12 | 1458 | static const struct xattr_handler gfs2_xattr_security_handler = { |
40b78a32 | 1459 | .prefix = XATTR_SECURITY_PREFIX, |
431547b3 CH |
1460 | .flags = GFS2_EATYPE_SECURITY, |
1461 | .get = gfs2_xattr_get, | |
1462 | .set = gfs2_xattr_set, | |
40b78a32 SW |
1463 | }; |
1464 | ||
b7bb0a12 | 1465 | const struct xattr_handler *gfs2_xattr_handlers[] = { |
40b78a32 SW |
1466 | &gfs2_xattr_user_handler, |
1467 | &gfs2_xattr_security_handler, | |
e01580bf CH |
1468 | &posix_acl_access_xattr_handler, |
1469 | &posix_acl_default_xattr_handler, | |
40b78a32 SW |
1470 | NULL, |
1471 | }; | |
1472 |