Linux 4.19.133
[linux/fpc-iii.git] / fs / gfs2 / xattr.c
blob38515988aaf746efb426674cc3f431d582ca523f
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
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>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/posix_acl_xattr.h>
17 #include <linux/uaccess.h>
19 #include "gfs2.h"
20 #include "incore.h"
21 #include "acl.h"
22 #include "xattr.h"
23 #include "glock.h"
24 #include "inode.h"
25 #include "meta_io.h"
26 #include "quota.h"
27 #include "rgrp.h"
28 #include "super.h"
29 #include "trans.h"
30 #include "util.h"
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:
39 * Returns: 1 if the EA should be stuffed
42 static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize,
43 unsigned int *size)
45 unsigned int jbsize = sdp->sd_jbsize;
47 /* Stuffed */
48 *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + dsize, 8);
50 if (*size <= jbsize)
51 return 1;
53 /* Unstuffed */
54 *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize +
55 (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8);
57 return 0;
60 static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize)
62 unsigned int size;
64 if (dsize > GFS2_EA_MAX_DATA_LEN)
65 return -ERANGE;
67 ea_calc_size(sdp, nsize, dsize, &size);
69 /* This can only happen with 512 byte blocks */
70 if (size > sdp->sd_jbsize)
71 return -ERANGE;
73 return 0;
76 typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
77 struct gfs2_ea_header *ea,
78 struct gfs2_ea_header *prev, void *private);
80 static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
81 ea_call_t ea_call, void *data)
83 struct gfs2_ea_header *ea, *prev = NULL;
84 int error = 0;
86 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
87 return -EIO;
89 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
90 if (!GFS2_EA_REC_LEN(ea))
91 goto fail;
92 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
93 bh->b_data + bh->b_size))
94 goto fail;
95 if (!GFS2_EATYPE_VALID(ea->ea_type))
96 goto fail;
98 error = ea_call(ip, bh, ea, prev, data);
99 if (error)
100 return error;
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;
110 return error;
112 fail:
113 gfs2_consist_inode(ip);
114 return -EIO;
117 static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
119 struct buffer_head *bh, *eabh;
120 __be64 *eablk, *end;
121 int error;
123 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &bh);
124 if (error)
125 return error;
127 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) {
128 error = ea_foreach_i(ip, bh, ea_call, data);
129 goto out;
132 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
133 error = -EIO;
134 goto out;
137 eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
138 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
140 for (; eablk < end; eablk++) {
141 u64 bn;
143 if (!*eablk)
144 break;
145 bn = be64_to_cpu(*eablk);
147 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, 0, &eabh);
148 if (error)
149 break;
150 error = ea_foreach_i(ip, eabh, ea_call, data);
151 brelse(eabh);
152 if (error)
153 break;
155 out:
156 brelse(bh);
157 return error;
160 struct ea_find {
161 int type;
162 const char *name;
163 size_t namel;
164 struct gfs2_ea_location *ef_el;
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)
171 struct ea_find *ef = private;
173 if (ea->ea_type == GFS2_EATYPE_UNUSED)
174 return 0;
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)) {
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;
188 return 0;
191 static int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name,
192 struct gfs2_ea_location *el)
194 struct ea_find ef;
195 int error;
197 ef.type = type;
198 ef.name = name;
199 ef.namel = strlen(name);
200 ef.ef_el = el;
202 memset(el, 0, sizeof(struct gfs2_ea_location));
204 error = ea_foreach(ip, ea_find_i, &ef);
205 if (error > 0)
206 return 0;
208 return error;
212 * ea_dealloc_unstuffed -
213 * @ip:
214 * @bh:
215 * @ea:
216 * @prev:
217 * @private:
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.
223 * Returns: errno
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)
230 int *leave = private;
231 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
232 struct gfs2_rgrpd *rgd;
233 struct gfs2_holder rg_gh;
234 __be64 *dataptrs;
235 u64 bn = 0;
236 u64 bstart = 0;
237 unsigned int blen = 0;
238 unsigned int blks = 0;
239 unsigned int x;
240 int error;
242 error = gfs2_rindex_update(sdp);
243 if (error)
244 return error;
246 if (GFS2_EA_IS_STUFFED(ea))
247 return 0;
249 dataptrs = GFS2_EA2DATAPTRS(ea);
250 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
251 if (*dataptrs) {
252 blks++;
253 bn = be64_to_cpu(*dataptrs);
256 if (!blks)
257 return 0;
259 rgd = gfs2_blk2rgrpd(sdp, bn, 1);
260 if (!rgd) {
261 gfs2_consist_inode(ip);
262 return -EIO;
265 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
266 if (error)
267 return error;
269 error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
270 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
271 if (error)
272 goto out_gunlock;
274 gfs2_trans_add_meta(ip->i_gl, bh);
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);
282 if (bstart + blen == bn)
283 blen++;
284 else {
285 if (bstart)
286 gfs2_free_meta(ip, bstart, blen);
287 bstart = bn;
288 blen = 1;
291 *dataptrs = 0;
292 gfs2_add_inode_blocks(&ip->i_inode, -1);
294 if (bstart)
295 gfs2_free_meta(ip, bstart, blen);
297 if (prev && !leave) {
298 u32 len;
300 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
301 prev->ea_rec_len = cpu_to_be32(len);
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;
310 ip->i_inode.i_ctime = current_time(&ip->i_inode);
311 __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
313 gfs2_trans_end(sdp);
315 out_gunlock:
316 gfs2_glock_dq_uninit(&rg_gh);
317 return error;
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)
324 int error;
326 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
327 if (error)
328 return error;
330 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
331 if (error)
332 goto out_alloc;
334 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
336 gfs2_quota_unhold(ip);
337 out_alloc:
338 return error;
341 struct ea_list {
342 struct gfs2_ea_request *ei_er;
343 unsigned int ei_size;
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)
350 struct ea_list *ei = private;
351 struct gfs2_ea_request *er = ei->ei_er;
352 unsigned int ea_size;
353 char *prefix;
354 unsigned int l;
356 if (ea->ea_type == GFS2_EATYPE_UNUSED)
357 return 0;
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();
376 ea_size = l + ea->ea_name_len + 1;
377 if (er->er_data_len) {
378 if (ei->ei_size + ea_size > er->er_data_len)
379 return -ERANGE;
381 memcpy(er->er_data + ei->ei_size, prefix, l);
382 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
383 ea->ea_name_len);
384 er->er_data[ei->ei_size + ea_size - 1] = 0;
387 ei->ei_size += ea_size;
389 return 0;
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
398 * Returns: actual size of data on success, -errno on error
401 ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
403 struct gfs2_inode *ip = GFS2_I(d_inode(dentry));
404 struct gfs2_ea_request er;
405 struct gfs2_holder i_gh;
406 int error;
408 memset(&er, 0, sizeof(struct gfs2_ea_request));
409 if (size) {
410 er.er_data = buffer;
411 er.er_data_len = size;
414 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
415 if (error)
416 return error;
418 if (ip->i_eattr) {
419 struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
421 error = ea_foreach(ip, ea_list_i, &ei);
422 if (!error)
423 error = ei.ei_size;
426 gfs2_glock_dq_uninit(&i_gh);
428 return error;
432 * ea_iter_unstuffed - copies the unstuffed xattr data to/from the
433 * request buffer
434 * @ip: The GFS2 inode
435 * @ea: The extended attribute header structure
436 * @din: The data to be copied in
437 * @dout: The data to be copied out (one of din,dout will be NULL)
439 * Returns: errno
442 static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
443 const char *din, char *dout)
445 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
446 struct buffer_head **bh;
447 unsigned int amount = GFS2_EA_DATA_LEN(ea);
448 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
449 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
450 unsigned int x;
451 int error = 0;
452 unsigned char *pos;
453 unsigned cp_size;
455 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
456 if (!bh)
457 return -ENOMEM;
459 for (x = 0; x < nptrs; x++) {
460 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, 0,
461 bh + x);
462 if (error) {
463 while (x--)
464 brelse(bh[x]);
465 goto out;
467 dataptrs++;
470 for (x = 0; x < nptrs; x++) {
471 error = gfs2_meta_wait(sdp, bh[x]);
472 if (error) {
473 for (; x < nptrs; x++)
474 brelse(bh[x]);
475 goto out;
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;
484 pos = bh[x]->b_data + sizeof(struct gfs2_meta_header);
485 cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize;
487 if (dout) {
488 memcpy(dout, pos, cp_size);
489 dout += sdp->sd_jbsize;
492 if (din) {
493 gfs2_trans_add_meta(ip->i_gl, bh[x]);
494 memcpy(pos, din, cp_size);
495 din += sdp->sd_jbsize;
498 amount -= sdp->sd_jbsize;
499 brelse(bh[x]);
502 out:
503 kfree(bh);
504 return error;
507 static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
508 char *data, size_t size)
510 int ret;
511 size_t len = GFS2_EA_DATA_LEN(el->el_ea);
512 if (len > size)
513 return -ERANGE;
515 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
516 memcpy(data, GFS2_EA2DATA(el->el_ea), len);
517 return len;
519 ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data);
520 if (ret < 0)
521 return ret;
522 return len;
525 int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
527 struct gfs2_ea_location el;
528 int error;
529 int len;
530 char *data;
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;
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;
546 error = gfs2_ea_get_copy(ip, &el, data, len);
547 if (error < 0)
548 kfree(data);
549 else
550 *ppdata = data;
551 out:
552 brelse(el.el_bh);
553 return error;
557 * gfs2_xattr_get - Get a GFS2 extended attribute
558 * @inode: The inode
559 * @name: The name of the extended attribute
560 * @buffer: The buffer to write the result into
561 * @size: The size of the buffer
562 * @type: The type of extended attribute
564 * Returns: actual size of data on success, -errno on error
566 static int __gfs2_xattr_get(struct inode *inode, const char *name,
567 void *buffer, size_t size, int type)
569 struct gfs2_inode *ip = GFS2_I(inode);
570 struct gfs2_ea_location el;
571 int error;
573 if (!ip->i_eattr)
574 return -ENODATA;
575 if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
576 return -EINVAL;
578 error = gfs2_ea_find(ip, type, name, &el);
579 if (error)
580 return error;
581 if (!el.el_ea)
582 return -ENODATA;
583 if (size)
584 error = gfs2_ea_get_copy(ip, &el, buffer, size);
585 else
586 error = GFS2_EA_DATA_LEN(el.el_ea);
587 brelse(el.el_bh);
589 return error;
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)
596 struct gfs2_inode *ip = GFS2_I(inode);
597 struct gfs2_holder gh;
598 int ret;
600 /* During lookup, SELinux calls this function with the glock locked. */
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;
606 } else {
607 gfs2_holder_mark_uninitialized(&gh);
609 ret = __gfs2_xattr_get(inode, name, buffer, size, handler->flags);
610 if (gfs2_holder_initialized(&gh))
611 gfs2_glock_dq_uninit(&gh);
612 return ret;
616 * ea_alloc_blk - allocates a new block for extended attributes.
617 * @ip: A pointer to the inode that's getting extended attributes
618 * @bhp: Pointer to pointer to a struct buffer_head
620 * Returns: errno
623 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
625 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
626 struct gfs2_ea_header *ea;
627 unsigned int n = 1;
628 u64 block;
629 int error;
631 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
632 if (error)
633 return error;
634 gfs2_trans_add_unrevoke(sdp, block, 1);
635 *bhp = gfs2_meta_new(ip->i_gl, block);
636 gfs2_trans_add_meta(ip->i_gl, *bhp);
637 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
638 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
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;
646 gfs2_add_inode_blocks(&ip->i_inode, 1);
648 return 0;
652 * ea_write - writes the request info to an ea, creating new blocks if
653 * necessary
654 * @ip: inode that is being modified
655 * @ea: the location of the new ea in a block
656 * @er: the write request
658 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
660 * returns : errno
663 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
664 struct gfs2_ea_request *er)
666 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
667 int error;
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;
674 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
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 {
680 __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
681 const char *data = er->er_data;
682 unsigned int data_len = er->er_data_len;
683 unsigned int copy;
684 unsigned int x;
686 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
687 for (x = 0; x < ea->ea_num_ptrs; x++) {
688 struct buffer_head *bh;
689 u64 block;
690 int mh_size = sizeof(struct gfs2_meta_header);
691 unsigned int n = 1;
693 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
694 if (error)
695 return error;
696 gfs2_trans_add_unrevoke(sdp, block, 1);
697 bh = gfs2_meta_new(ip->i_gl, block);
698 gfs2_trans_add_meta(ip->i_gl, bh);
699 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
701 gfs2_add_inode_blocks(&ip->i_inode, 1);
703 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
704 data_len;
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);
710 *dataptr++ = cpu_to_be64(bh->b_blocknr);
711 data += copy;
712 data_len -= copy;
714 brelse(bh);
717 gfs2_assert_withdraw(sdp, !data_len);
720 return 0;
723 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
724 struct gfs2_ea_request *er, void *private);
726 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
727 unsigned int blks,
728 ea_skeleton_call_t skeleton_call, void *private)
730 struct gfs2_alloc_parms ap = { .target = blks };
731 int error;
733 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
734 if (error)
735 return error;
737 error = gfs2_quota_lock_check(ip, &ap);
738 if (error)
739 return error;
741 error = gfs2_inplace_reserve(ip, &ap);
742 if (error)
743 goto out_gunlock_q;
745 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
746 blks + gfs2_rg_blocks(ip, blks) +
747 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
748 if (error)
749 goto out_ipres;
751 error = skeleton_call(ip, er, private);
752 if (error)
753 goto out_end_trans;
755 ip->i_inode.i_ctime = current_time(&ip->i_inode);
756 __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
758 out_end_trans:
759 gfs2_trans_end(GFS2_SB(&ip->i_inode));
760 out_ipres:
761 gfs2_inplace_release(ip);
762 out_gunlock_q:
763 gfs2_quota_unlock(ip);
764 return error;
767 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
768 void *private)
770 struct buffer_head *bh;
771 int error;
773 error = ea_alloc_blk(ip, &bh);
774 if (error)
775 return error;
777 ip->i_eattr = bh->b_blocknr;
778 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
780 brelse(bh);
782 return error;
786 * ea_init - initializes a new eattr block
787 * @ip:
788 * @er:
790 * Returns: errno
793 static int ea_init(struct gfs2_inode *ip, int type, const char *name,
794 const void *data, size_t size)
796 struct gfs2_ea_request er;
797 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
798 unsigned int blks = 1;
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;
806 if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
807 blks += DIV_ROUND_UP(er.er_data_len, jbsize);
809 return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
812 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
814 u32 ea_size = GFS2_EA_SIZE(ea);
815 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
816 ea_size);
817 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
818 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
820 ea->ea_rec_len = cpu_to_be32(ea_size);
821 ea->ea_flags ^= last;
823 new->ea_rec_len = cpu_to_be32(new_size);
824 new->ea_flags = last;
826 return new;
829 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
830 struct gfs2_ea_location *el)
832 struct gfs2_ea_header *ea = el->el_ea;
833 struct gfs2_ea_header *prev = el->el_prev;
834 u32 len;
836 gfs2_trans_add_meta(ip->i_gl, el->el_bh);
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);
843 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
846 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
847 prev->ea_rec_len = cpu_to_be32(len);
849 if (GFS2_EA_IS_LAST(ea))
850 prev->ea_flags |= GFS2_EAFLAG_LAST;
853 struct ea_set {
854 int ea_split;
856 struct gfs2_ea_request *es_er;
857 struct gfs2_ea_location *es_el;
859 struct buffer_head *es_bh;
860 struct gfs2_ea_header *es_ea;
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)
866 struct gfs2_ea_request *er = es->es_er;
867 int error;
869 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
870 if (error)
871 return error;
873 gfs2_trans_add_meta(ip->i_gl, bh);
875 if (es->ea_split)
876 ea = ea_split_ea(ea);
878 ea_write(ip, ea, er);
880 if (es->es_el)
881 ea_set_remove_stuffed(ip, es->es_el);
883 ip->i_inode.i_ctime = current_time(&ip->i_inode);
884 __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
886 gfs2_trans_end(GFS2_SB(&ip->i_inode));
887 return error;
890 static int ea_set_simple_alloc(struct gfs2_inode *ip,
891 struct gfs2_ea_request *er, void *private)
893 struct ea_set *es = private;
894 struct gfs2_ea_header *ea = es->es_ea;
895 int error;
897 gfs2_trans_add_meta(ip->i_gl, es->es_bh);
899 if (es->ea_split)
900 ea = ea_split_ea(ea);
902 error = ea_write(ip, ea, er);
903 if (error)
904 return error;
906 if (es->es_el)
907 ea_set_remove_stuffed(ip, es->es_el);
909 return 0;
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)
916 struct ea_set *es = private;
917 unsigned int size;
918 int stuffed;
919 int error;
921 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
922 es->es_er->er_data_len, &size);
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;
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;
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;
945 es->es_bh = bh;
946 es->es_ea = ea;
947 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
948 GFS2_SB(&ip->i_inode)->sd_jbsize);
950 error = ea_alloc_skeleton(ip, es->es_er, blks,
951 ea_set_simple_alloc, es);
952 if (error)
953 return error;
956 return 1;
959 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
960 void *private)
962 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
963 struct buffer_head *indbh, *newbh;
964 __be64 *eablk;
965 int error;
966 int mh_size = sizeof(struct gfs2_meta_header);
968 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
969 __be64 *end;
971 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0,
972 &indbh);
973 if (error)
974 return error;
976 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
977 error = -EIO;
978 goto out;
981 eablk = (__be64 *)(indbh->b_data + mh_size);
982 end = eablk + sdp->sd_inptrs;
984 for (; eablk < end; eablk++)
985 if (!*eablk)
986 break;
988 if (eablk == end) {
989 error = -ENOSPC;
990 goto out;
993 gfs2_trans_add_meta(ip->i_gl, indbh);
994 } else {
995 u64 blk;
996 unsigned int n = 1;
997 error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL);
998 if (error)
999 return error;
1000 gfs2_trans_add_unrevoke(sdp, blk, 1);
1001 indbh = gfs2_meta_new(ip->i_gl, blk);
1002 gfs2_trans_add_meta(ip->i_gl, indbh);
1003 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
1004 gfs2_buffer_clear_tail(indbh, mh_size);
1006 eablk = (__be64 *)(indbh->b_data + mh_size);
1007 *eablk = cpu_to_be64(ip->i_eattr);
1008 ip->i_eattr = blk;
1009 ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
1010 gfs2_add_inode_blocks(&ip->i_inode, 1);
1012 eablk++;
1015 error = ea_alloc_blk(ip, &newbh);
1016 if (error)
1017 goto out;
1019 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
1020 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1021 brelse(newbh);
1022 if (error)
1023 goto out;
1025 if (private)
1026 ea_set_remove_stuffed(ip, private);
1028 out:
1029 brelse(indbh);
1030 return error;
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)
1036 struct gfs2_ea_request er;
1037 struct ea_set es;
1038 unsigned int blks = 2;
1039 int error;
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;
1047 memset(&es, 0, sizeof(struct ea_set));
1048 es.es_er = &er;
1049 es.es_el = el;
1051 error = ea_foreach(ip, ea_set_simple, &es);
1052 if (error > 0)
1053 return 0;
1054 if (error)
1055 return error;
1057 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
1058 blks++;
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);
1062 return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
1065 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1066 struct gfs2_ea_location *el)
1068 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1069 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1070 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
1071 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1074 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
1077 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1079 struct gfs2_ea_header *ea = el->el_ea;
1080 struct gfs2_ea_header *prev = el->el_prev;
1081 int error;
1083 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1084 if (error)
1085 return error;
1087 gfs2_trans_add_meta(ip->i_gl, el->el_bh);
1089 if (prev) {
1090 u32 len;
1092 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1093 prev->ea_rec_len = cpu_to_be32(len);
1095 if (GFS2_EA_IS_LAST(ea))
1096 prev->ea_flags |= GFS2_EAFLAG_LAST;
1097 } else {
1098 ea->ea_type = GFS2_EATYPE_UNUSED;
1101 ip->i_inode.i_ctime = current_time(&ip->i_inode);
1102 __mark_inode_dirty(&ip->i_inode, I_DIRTY_DATASYNC);
1104 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1106 return error;
1110 * gfs2_xattr_remove - Remove a GFS2 extended attribute
1111 * @ip: The inode
1112 * @type: The type of the extended attribute
1113 * @name: The name of the extended attribute
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.
1119 * Returns: 0, or errno on failure
1122 static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
1124 struct gfs2_ea_location el;
1125 int error;
1127 if (!ip->i_eattr)
1128 return -ENODATA;
1130 error = gfs2_ea_find(ip, type, name, &el);
1131 if (error)
1132 return error;
1133 if (!el.el_ea)
1134 return -ENODATA;
1136 if (GFS2_EA_IS_STUFFED(el.el_ea))
1137 error = ea_remove_stuffed(ip, &el);
1138 else
1139 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
1141 brelse(el.el_bh);
1143 return error;
1147 * __gfs2_xattr_set - Set (or remove) a GFS2 extended attribute
1148 * @ip: The inode
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
1153 * @type: The type of the extended attribute
1155 * See gfs2_xattr_remove() for details of the removal of xattrs.
1157 * Returns: 0 or errno on failure
1160 int __gfs2_xattr_set(struct inode *inode, const char *name,
1161 const void *value, size_t size, int flags, int type)
1163 struct gfs2_inode *ip = GFS2_I(inode);
1164 struct gfs2_sbd *sdp = GFS2_SB(inode);
1165 struct gfs2_ea_location el;
1166 unsigned int namel = strlen(name);
1167 int error;
1169 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1170 return -EPERM;
1171 if (namel > GFS2_EA_MAX_NAME_LEN)
1172 return -ERANGE;
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;
1181 if (ea_check_size(sdp, namel, size))
1182 return -ERANGE;
1184 if (!ip->i_eattr) {
1185 if (flags & XATTR_REPLACE)
1186 return -ENODATA;
1187 return ea_init(ip, type, name, value, size);
1190 error = gfs2_ea_find(ip, type, name, &el);
1191 if (error)
1192 return error;
1194 if (el.el_ea) {
1195 if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
1196 brelse(el.el_bh);
1197 return -EPERM;
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);
1208 brelse(el.el_bh);
1209 return error;
1212 error = -ENODATA;
1213 if (!(flags & XATTR_REPLACE))
1214 error = ea_set_i(ip, type, name, value, size, NULL);
1216 return error;
1219 static int gfs2_xattr_set(const struct xattr_handler *handler,
1220 struct dentry *unused, struct inode *inode,
1221 const char *name, const void *value,
1222 size_t size, int flags)
1224 struct gfs2_inode *ip = GFS2_I(inode);
1225 struct gfs2_holder gh;
1226 int ret;
1228 ret = gfs2_rsqa_alloc(ip);
1229 if (ret)
1230 return ret;
1232 /* May be called from gfs_setattr with the glock locked. */
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);
1243 ret = __gfs2_xattr_set(inode, name, value, size, flags, handler->flags);
1244 if (gfs2_holder_initialized(&gh))
1245 gfs2_glock_dq_uninit(&gh);
1246 return ret;
1249 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1251 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1252 struct gfs2_rgrp_list rlist;
1253 struct buffer_head *indbh, *dibh;
1254 __be64 *eablk, *end;
1255 unsigned int rg_blocks = 0;
1256 u64 bstart = 0;
1257 unsigned int blen = 0;
1258 unsigned int blks = 0;
1259 unsigned int x;
1260 int error;
1262 error = gfs2_rindex_update(sdp);
1263 if (error)
1264 return error;
1266 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1268 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &indbh);
1269 if (error)
1270 return error;
1272 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1273 error = -EIO;
1274 goto out;
1277 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1278 end = eablk + sdp->sd_inptrs;
1280 for (; eablk < end; eablk++) {
1281 u64 bn;
1283 if (!*eablk)
1284 break;
1285 bn = be64_to_cpu(*eablk);
1287 if (bstart + blen == bn)
1288 blen++;
1289 else {
1290 if (bstart)
1291 gfs2_rlist_add(ip, &rlist, bstart);
1292 bstart = bn;
1293 blen = 1;
1295 blks++;
1297 if (bstart)
1298 gfs2_rlist_add(ip, &rlist, bstart);
1299 else
1300 goto out;
1302 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
1304 for (x = 0; x < rlist.rl_rgrps; x++) {
1305 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
1307 rg_blocks += rgd->rd_length;
1310 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1311 if (error)
1312 goto out_rlist_free;
1314 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1315 RES_STATFS + RES_QUOTA, blks);
1316 if (error)
1317 goto out_gunlock;
1319 gfs2_trans_add_meta(ip->i_gl, indbh);
1321 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1322 bstart = 0;
1323 blen = 0;
1325 for (; eablk < end; eablk++) {
1326 u64 bn;
1328 if (!*eablk)
1329 break;
1330 bn = be64_to_cpu(*eablk);
1332 if (bstart + blen == bn)
1333 blen++;
1334 else {
1335 if (bstart)
1336 gfs2_free_meta(ip, bstart, blen);
1337 bstart = bn;
1338 blen = 1;
1341 *eablk = 0;
1342 gfs2_add_inode_blocks(&ip->i_inode, -1);
1344 if (bstart)
1345 gfs2_free_meta(ip, bstart, blen);
1347 ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
1349 error = gfs2_meta_inode_buffer(ip, &dibh);
1350 if (!error) {
1351 gfs2_trans_add_meta(ip->i_gl, dibh);
1352 gfs2_dinode_out(ip, dibh->b_data);
1353 brelse(dibh);
1356 gfs2_trans_end(sdp);
1358 out_gunlock:
1359 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1360 out_rlist_free:
1361 gfs2_rlist_free(&rlist);
1362 out:
1363 brelse(indbh);
1364 return error;
1367 static int ea_dealloc_block(struct gfs2_inode *ip)
1369 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1370 struct gfs2_rgrpd *rgd;
1371 struct buffer_head *dibh;
1372 struct gfs2_holder gh;
1373 int error;
1375 error = gfs2_rindex_update(sdp);
1376 if (error)
1377 return error;
1379 rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
1380 if (!rgd) {
1381 gfs2_consist_inode(ip);
1382 return -EIO;
1385 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1386 if (error)
1387 return error;
1389 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1390 RES_QUOTA, 1);
1391 if (error)
1392 goto out_gunlock;
1394 gfs2_free_meta(ip, ip->i_eattr, 1);
1396 ip->i_eattr = 0;
1397 gfs2_add_inode_blocks(&ip->i_inode, -1);
1399 error = gfs2_meta_inode_buffer(ip, &dibh);
1400 if (!error) {
1401 gfs2_trans_add_meta(ip->i_gl, dibh);
1402 gfs2_dinode_out(ip, dibh->b_data);
1403 brelse(dibh);
1406 gfs2_trans_end(sdp);
1408 out_gunlock:
1409 gfs2_glock_dq_uninit(&gh);
1410 return error;
1414 * gfs2_ea_dealloc - deallocate the extended attribute fork
1415 * @ip: the inode
1417 * Returns: errno
1420 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1422 int error;
1424 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
1425 if (error)
1426 return error;
1428 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1429 if (error)
1430 return error;
1432 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1433 if (error)
1434 goto out_quota;
1436 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
1437 error = ea_dealloc_indirect(ip);
1438 if (error)
1439 goto out_quota;
1442 error = ea_dealloc_block(ip);
1444 out_quota:
1445 gfs2_quota_unhold(ip);
1446 return error;
1449 static const struct xattr_handler gfs2_xattr_user_handler = {
1450 .prefix = XATTR_USER_PREFIX,
1451 .flags = GFS2_EATYPE_USR,
1452 .get = gfs2_xattr_get,
1453 .set = gfs2_xattr_set,
1456 static const struct xattr_handler gfs2_xattr_security_handler = {
1457 .prefix = XATTR_SECURITY_PREFIX,
1458 .flags = GFS2_EATYPE_SECURITY,
1459 .get = gfs2_xattr_get,
1460 .set = gfs2_xattr_set,
1463 const struct xattr_handler *gfs2_xattr_handlers[] = {
1464 &gfs2_xattr_user_handler,
1465 &gfs2_xattr_security_handler,
1466 &posix_acl_access_xattr_handler,
1467 &posix_acl_default_xattr_handler,
1468 NULL,