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 v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/xattr.h>
16 #include <linux/gfs2_ondisk.h>
17 #include <asm/uaccess.h>
20 #include "lm_interface.h"
34 * ea_calc_size - returns the acutal number of bytes the request will take up
35 * (not counting any unstuffed data blocks)
40 * Returns: 1 if the EA should be stuffed
43 static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
46 *size = GFS2_EAREQ_SIZE_STUFFED(er);
47 if (*size <= sdp->sd_jbsize)
50 *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);
55 static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
59 if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
62 ea_calc_size(sdp, er, &size);
64 /* This can only happen with 512 byte blocks */
65 if (size > sdp->sd_jbsize)
71 typedef int (*ea_call_t) (struct gfs2_inode *ip,
72 struct buffer_head *bh,
73 struct gfs2_ea_header *ea,
74 struct gfs2_ea_header *prev,
77 static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
78 ea_call_t ea_call, void *data)
80 struct gfs2_ea_header *ea, *prev = NULL;
83 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
86 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
87 if (!GFS2_EA_REC_LEN(ea))
89 if (!(bh->b_data <= (char *)ea &&
90 (char *)GFS2_EA2NEXT(ea) <=
91 bh->b_data + bh->b_size))
93 if (!GFS2_EATYPE_VALID(ea->ea_type))
96 error = ea_call(ip, bh, ea, prev, data);
100 if (GFS2_EA_IS_LAST(ea)) {
101 if ((char *)GFS2_EA2NEXT(ea) !=
102 bh->b_data + bh->b_size)
111 gfs2_consist_inode(ip);
115 static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
117 struct buffer_head *bh, *eabh;
118 uint64_t *eablk, *end;
121 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
122 DIO_START | DIO_WAIT, &bh);
126 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
127 error = ea_foreach_i(ip, bh, ea_call, data);
131 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
136 eablk = (uint64_t *)(bh->b_data + sizeof(struct gfs2_meta_header));
137 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
139 for (; eablk < end; eablk++) {
144 bn = be64_to_cpu(*eablk);
146 error = gfs2_meta_read(ip->i_gl, bn, DIO_START | DIO_WAIT,
150 error = ea_foreach_i(ip, eabh, ea_call, data);
162 struct gfs2_ea_request *ef_er;
163 struct gfs2_ea_location *ef_el;
166 static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
167 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
170 struct ea_find *ef = private;
171 struct gfs2_ea_request *er = ef->ef_er;
173 if (ea->ea_type == GFS2_EATYPE_UNUSED)
176 if (ea->ea_type == er->er_type) {
177 if (ea->ea_name_len == er->er_name_len &&
178 !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
179 struct gfs2_ea_location *el = ef->ef_el;
189 else if ((ip->i_di.di_flags & GFS2_DIF_EA_PACKED) &&
190 er->er_type == GFS2_EATYPE_SYS)
197 int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
198 struct gfs2_ea_location *el)
206 memset(el, 0, sizeof(struct gfs2_ea_location));
208 error = ea_foreach(ip, ea_find_i, &ef);
216 * ea_dealloc_unstuffed -
223 * Take advantage of the fact that all unstuffed blocks are
224 * allocated from the same RG. But watch, this may not always
230 static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
231 struct gfs2_ea_header *ea,
232 struct gfs2_ea_header *prev, void *private)
234 int *leave = private;
235 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
236 struct gfs2_rgrpd *rgd;
237 struct gfs2_holder rg_gh;
238 struct buffer_head *dibh;
239 uint64_t *dataptrs, bn = 0;
241 unsigned int blen = 0;
242 unsigned int blks = 0;
246 if (GFS2_EA_IS_STUFFED(ea))
249 dataptrs = GFS2_EA2DATAPTRS(ea);
250 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++)
253 bn = be64_to_cpu(*dataptrs);
258 rgd = gfs2_blk2rgrpd(sdp, bn);
260 gfs2_consist_inode(ip);
264 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
268 error = gfs2_trans_begin(sdp, rgd->rd_ri.ri_length +
269 RES_DINODE + RES_EATTR + RES_STATFS +
274 gfs2_trans_add_bh(ip->i_gl, bh, 1);
276 dataptrs = GFS2_EA2DATAPTRS(ea);
277 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
280 bn = be64_to_cpu(*dataptrs);
282 if (bstart + blen == bn)
286 gfs2_free_meta(ip, bstart, blen);
292 if (!ip->i_di.di_blocks)
293 gfs2_consist_inode(ip);
294 ip->i_di.di_blocks--;
297 gfs2_free_meta(ip, bstart, blen);
299 if (prev && !leave) {
302 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
303 prev->ea_rec_len = cpu_to_be32(len);
305 if (GFS2_EA_IS_LAST(ea))
306 prev->ea_flags |= GFS2_EAFLAG_LAST;
308 ea->ea_type = GFS2_EATYPE_UNUSED;
312 error = gfs2_meta_inode_buffer(ip, &dibh);
314 ip->i_di.di_ctime = get_seconds();
315 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
316 gfs2_dinode_out(&ip->i_di, dibh->b_data);
323 gfs2_glock_dq_uninit(&rg_gh);
328 static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
329 struct gfs2_ea_header *ea,
330 struct gfs2_ea_header *prev, int leave)
332 struct gfs2_alloc *al;
335 al = gfs2_alloc_get(ip);
337 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
341 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
345 error = ea_dealloc_unstuffed(ip,
347 (leave) ? &error : NULL);
349 gfs2_glock_dq_uninit(&al->al_ri_gh);
352 gfs2_quota_unhold(ip);
361 struct gfs2_ea_request *ei_er;
362 unsigned int ei_size;
365 static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
366 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
369 struct ea_list *ei = private;
370 struct gfs2_ea_request *er = ei->ei_er;
371 unsigned int ea_size = gfs2_ea_strlen(ea);
373 if (ea->ea_type == GFS2_EATYPE_UNUSED)
376 if (er->er_data_len) {
381 if (ei->ei_size + ea_size > er->er_data_len)
384 switch (ea->ea_type) {
385 case GFS2_EATYPE_USR:
389 case GFS2_EATYPE_SYS:
393 case GFS2_EATYPE_SECURITY:
394 prefix = "security.";
401 memcpy(er->er_data + ei->ei_size, prefix, l);
402 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
404 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
407 ei->ei_size += ea_size;
417 * Returns: actual size of data on success, -errno on error
420 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
422 struct gfs2_holder i_gh;
425 if (!er->er_data || !er->er_data_len) {
430 error = gfs2_glock_nq_init(ip->i_gl,
431 LM_ST_SHARED, LM_FLAG_ANY,
436 if (ip->i_di.di_eattr) {
437 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
439 error = ea_foreach(ip, ea_list_i, &ei);
444 gfs2_glock_dq_uninit(&i_gh);
450 * ea_get_unstuffed - actually copies the unstuffed data into the
459 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
462 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
463 struct buffer_head **bh;
464 unsigned int amount = GFS2_EA_DATA_LEN(ea);
465 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
466 uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
470 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
474 for (x = 0; x < nptrs; x++) {
475 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
485 for (x = 0; x < nptrs; x++) {
486 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
488 for (; x < nptrs; x++)
492 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
493 for (; x < nptrs; x++)
500 bh[x]->b_data + sizeof(struct gfs2_meta_header),
501 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
503 amount -= sdp->sd_jbsize;
504 data += sdp->sd_jbsize;
515 int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
518 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
520 GFS2_EA2DATA(el->el_ea),
521 GFS2_EA_DATA_LEN(el->el_ea));
524 return ea_get_unstuffed(ip, el->el_ea, data);
532 * Returns: actual size of data on success, -errno on error
535 int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
537 struct gfs2_ea_location el;
540 if (!ip->i_di.di_eattr)
543 error = gfs2_ea_find(ip, er, &el);
549 if (er->er_data_len) {
550 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
553 error = gfs2_ea_get_copy(ip, &el, er->er_data);
556 error = GFS2_EA_DATA_LEN(el.el_ea);
568 * Returns: actual size of data on success, -errno on error
571 int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
573 struct gfs2_holder i_gh;
576 if (!er->er_name_len ||
577 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
579 if (!er->er_data || !er->er_data_len) {
584 error = gfs2_glock_nq_init(ip->i_gl,
585 LM_ST_SHARED, LM_FLAG_ANY,
590 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
592 gfs2_glock_dq_uninit(&i_gh);
598 * ea_alloc_blk - allocates a new block for extended attributes.
599 * @ip: A pointer to the inode that's getting extended attributes
605 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
607 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
608 struct gfs2_ea_header *ea;
611 block = gfs2_alloc_meta(ip);
613 *bhp = gfs2_meta_new(ip->i_gl, block);
614 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
615 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
616 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
618 ea = GFS2_EA_BH2FIRST(*bhp);
619 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
620 ea->ea_type = GFS2_EATYPE_UNUSED;
621 ea->ea_flags = GFS2_EAFLAG_LAST;
624 ip->i_di.di_blocks++;
630 * ea_write - writes the request info to an ea, creating new blocks if
632 * @ip: inode that is being modified
633 * @ea: the location of the new ea in a block
634 * @er: the write request
636 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
641 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
642 struct gfs2_ea_request *er)
644 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
646 ea->ea_data_len = cpu_to_be32(er->er_data_len);
647 ea->ea_name_len = er->er_name_len;
648 ea->ea_type = er->er_type;
651 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
653 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
655 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
657 uint64_t *dataptr = GFS2_EA2DATAPTRS(ea);
658 const char *data = er->er_data;
659 unsigned int data_len = er->er_data_len;
663 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
664 for (x = 0; x < ea->ea_num_ptrs; x++) {
665 struct buffer_head *bh;
667 int mh_size = sizeof(struct gfs2_meta_header);
669 block = gfs2_alloc_meta(ip);
671 bh = gfs2_meta_new(ip->i_gl, block);
672 gfs2_trans_add_bh(ip->i_gl, bh, 1);
673 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
675 ip->i_di.di_blocks++;
677 copy = (data_len > sdp->sd_jbsize) ? sdp->sd_jbsize :
679 memcpy(bh->b_data + mh_size, data, copy);
680 if (copy < sdp->sd_jbsize)
681 memset(bh->b_data + mh_size + copy, 0,
682 sdp->sd_jbsize - copy);
684 *dataptr++ = cpu_to_be64((uint64_t)bh->b_blocknr);
691 gfs2_assert_withdraw(sdp, !data_len);
697 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
698 struct gfs2_ea_request *er,
701 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
703 ea_skeleton_call_t skeleton_call,
706 struct gfs2_alloc *al;
707 struct buffer_head *dibh;
710 al = gfs2_alloc_get(ip);
712 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
716 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
720 al->al_requested = blks;
722 error = gfs2_inplace_reserve(ip);
726 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
727 blks + al->al_rgd->rd_ri.ri_length +
728 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
732 error = skeleton_call(ip, er, private);
736 error = gfs2_meta_inode_buffer(ip, &dibh);
738 if (er->er_flags & GFS2_ERF_MODE) {
739 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
740 (ip->i_di.di_mode & S_IFMT) ==
741 (er->er_mode & S_IFMT));
742 ip->i_di.di_mode = er->er_mode;
744 ip->i_di.di_ctime = get_seconds();
745 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
746 gfs2_dinode_out(&ip->i_di, dibh->b_data);
751 gfs2_trans_end(GFS2_SB(&ip->i_inode));
754 gfs2_inplace_release(ip);
757 gfs2_quota_unlock(ip);
765 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
768 struct buffer_head *bh;
771 error = ea_alloc_blk(ip, &bh);
775 ip->i_di.di_eattr = bh->b_blocknr;
776 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
784 * ea_init - initializes a new eattr block
791 static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
793 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
794 unsigned int blks = 1;
796 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
797 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
799 return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
802 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
804 uint32_t ea_size = GFS2_EA_SIZE(ea);
805 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
807 uint32_t new_size = GFS2_EA_REC_LEN(ea) - ea_size;
808 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
810 ea->ea_rec_len = cpu_to_be32(ea_size);
811 ea->ea_flags ^= last;
813 new->ea_rec_len = cpu_to_be32(new_size);
814 new->ea_flags = last;
819 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
820 struct gfs2_ea_location *el)
822 struct gfs2_ea_header *ea = el->el_ea;
823 struct gfs2_ea_header *prev = el->el_prev;
826 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
828 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
829 ea->ea_type = GFS2_EATYPE_UNUSED;
831 } else if (GFS2_EA2NEXT(prev) != ea) {
832 prev = GFS2_EA2NEXT(prev);
833 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
836 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
837 prev->ea_rec_len = cpu_to_be32(len);
839 if (GFS2_EA_IS_LAST(ea))
840 prev->ea_flags |= GFS2_EAFLAG_LAST;
846 struct gfs2_ea_request *es_er;
847 struct gfs2_ea_location *es_el;
849 struct buffer_head *es_bh;
850 struct gfs2_ea_header *es_ea;
853 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
854 struct gfs2_ea_header *ea, struct ea_set *es)
856 struct gfs2_ea_request *er = es->es_er;
857 struct buffer_head *dibh;
860 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
864 gfs2_trans_add_bh(ip->i_gl, bh, 1);
867 ea = ea_split_ea(ea);
869 ea_write(ip, ea, er);
872 ea_set_remove_stuffed(ip, es->es_el);
874 error = gfs2_meta_inode_buffer(ip, &dibh);
878 if (er->er_flags & GFS2_ERF_MODE) {
879 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
880 (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
881 ip->i_di.di_mode = er->er_mode;
883 ip->i_di.di_ctime = get_seconds();
884 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
885 gfs2_dinode_out(&ip->i_di, dibh->b_data);
888 gfs2_trans_end(GFS2_SB(&ip->i_inode));
893 static int ea_set_simple_alloc(struct gfs2_inode *ip,
894 struct gfs2_ea_request *er, void *private)
896 struct ea_set *es = private;
897 struct gfs2_ea_header *ea = es->es_ea;
900 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
903 ea = ea_split_ea(ea);
905 error = ea_write(ip, ea, er);
910 ea_set_remove_stuffed(ip, es->es_el);
915 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
916 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
919 struct ea_set *es = private;
924 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
926 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
927 if (GFS2_EA_REC_LEN(ea) < size)
929 if (!GFS2_EA_IS_STUFFED(ea)) {
930 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
935 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
941 error = ea_set_simple_noalloc(ip, bh, ea, es);
949 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
950 GFS2_SB(&ip->i_inode)->sd_jbsize);
952 error = ea_alloc_skeleton(ip, es->es_er, blks,
953 ea_set_simple_alloc, es);
961 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
964 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
965 struct buffer_head *indbh, *newbh;
968 int mh_size = sizeof(struct gfs2_meta_header);
970 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
973 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
974 DIO_START | DIO_WAIT, &indbh);
978 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
983 eablk = (uint64_t *)(indbh->b_data + mh_size);
984 end = eablk + sdp->sd_inptrs;
986 for (; eablk < end; eablk++)
995 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
999 blk = gfs2_alloc_meta(ip);
1001 indbh = gfs2_meta_new(ip->i_gl, blk);
1002 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1003 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
1004 gfs2_buffer_clear_tail(indbh, mh_size);
1006 eablk = (uint64_t *)(indbh->b_data + mh_size);
1007 *eablk = cpu_to_be64(ip->i_di.di_eattr);
1008 ip->i_di.di_eattr = blk;
1009 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
1010 ip->i_di.di_blocks++;
1015 error = ea_alloc_blk(ip, &newbh);
1019 *eablk = cpu_to_be64((uint64_t)newbh->b_blocknr);
1020 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1026 ea_set_remove_stuffed(ip, (struct gfs2_ea_location *)private);
1034 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1035 struct gfs2_ea_location *el)
1038 unsigned int blks = 2;
1041 memset(&es, 0, sizeof(struct ea_set));
1045 error = ea_foreach(ip, ea_set_simple, &es);
1051 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1053 if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1054 blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
1056 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1059 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1060 struct gfs2_ea_location *el)
1062 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1063 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1064 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
1065 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1068 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1071 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1073 struct gfs2_ea_location el;
1076 if (!ip->i_di.di_eattr) {
1077 if (er->er_flags & XATTR_REPLACE)
1079 return ea_init(ip, er);
1082 error = gfs2_ea_find(ip, er, &el);
1087 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1093 if (!(er->er_flags & XATTR_CREATE)) {
1094 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1095 error = ea_set_i(ip, er, &el);
1096 if (!error && unstuffed)
1097 ea_set_remove_unstuffed(ip, &el);
1103 if (!(er->er_flags & XATTR_REPLACE))
1104 error = ea_set_i(ip, er, NULL);
1110 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1112 struct gfs2_holder i_gh;
1115 if (!er->er_name_len ||
1116 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1118 if (!er->er_data || !er->er_data_len) {
1120 er->er_data_len = 0;
1122 error = ea_check_size(GFS2_SB(&ip->i_inode), er);
1126 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1130 if (IS_IMMUTABLE(&ip->i_inode))
1133 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1135 gfs2_glock_dq_uninit(&i_gh);
1140 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1142 struct gfs2_ea_header *ea = el->el_ea;
1143 struct gfs2_ea_header *prev = el->el_prev;
1144 struct buffer_head *dibh;
1147 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1151 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1156 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1157 prev->ea_rec_len = cpu_to_be32(len);
1159 if (GFS2_EA_IS_LAST(ea))
1160 prev->ea_flags |= GFS2_EAFLAG_LAST;
1162 ea->ea_type = GFS2_EATYPE_UNUSED;
1164 error = gfs2_meta_inode_buffer(ip, &dibh);
1166 ip->i_di.di_ctime = get_seconds();
1167 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1168 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1172 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1177 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1179 struct gfs2_ea_location el;
1182 if (!ip->i_di.di_eattr)
1185 error = gfs2_ea_find(ip, er, &el);
1191 if (GFS2_EA_IS_STUFFED(el.el_ea))
1192 error = ea_remove_stuffed(ip, &el);
1194 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1203 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1204 * @ip: pointer to the inode of the target file
1205 * @er: request information
1210 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1212 struct gfs2_holder i_gh;
1215 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1218 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1222 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1225 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1227 gfs2_glock_dq_uninit(&i_gh);
1232 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1233 struct gfs2_ea_header *ea, char *data)
1235 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1236 struct buffer_head **bh;
1237 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1238 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1239 uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
1243 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1247 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1251 for (x = 0; x < nptrs; x++) {
1252 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
1262 for (x = 0; x < nptrs; x++) {
1263 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
1265 for (; x < nptrs; x++)
1269 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1270 for (; x < nptrs; x++)
1276 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1278 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header),
1280 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1282 amount -= sdp->sd_jbsize;
1283 data += sdp->sd_jbsize;
1294 gfs2_trans_end(sdp);
1300 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1301 struct iattr *attr, char *data)
1303 struct buffer_head *dibh;
1306 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1307 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1311 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1312 memcpy(GFS2_EA2DATA(el->el_ea),
1314 GFS2_EA_DATA_LEN(el->el_ea));
1316 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1321 error = gfs2_meta_inode_buffer(ip, &dibh);
1323 error = inode_setattr(&ip->i_inode, attr);
1324 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1325 gfs2_inode_attr_out(ip);
1326 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1327 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1331 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1336 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1338 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1339 struct gfs2_rgrp_list rlist;
1340 struct buffer_head *indbh, *dibh;
1341 uint64_t *eablk, *end;
1342 unsigned int rg_blocks = 0;
1343 uint64_t bstart = 0;
1344 unsigned int blen = 0;
1345 unsigned int blks = 0;
1349 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1351 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
1352 DIO_START | DIO_WAIT, &indbh);
1356 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1361 eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1362 end = eablk + sdp->sd_inptrs;
1364 for (; eablk < end; eablk++) {
1369 bn = be64_to_cpu(*eablk);
1371 if (bstart + blen == bn)
1375 gfs2_rlist_add(sdp, &rlist, bstart);
1382 gfs2_rlist_add(sdp, &rlist, bstart);
1386 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1388 for (x = 0; x < rlist.rl_rgrps; x++) {
1389 struct gfs2_rgrpd *rgd;
1390 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1391 rg_blocks += rgd->rd_ri.ri_length;
1394 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1396 goto out_rlist_free;
1398 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
1399 RES_INDIRECT + RES_STATFS +
1404 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1406 eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1410 for (; eablk < end; eablk++) {
1415 bn = be64_to_cpu(*eablk);
1417 if (bstart + blen == bn)
1421 gfs2_free_meta(ip, bstart, blen);
1427 if (!ip->i_di.di_blocks)
1428 gfs2_consist_inode(ip);
1429 ip->i_di.di_blocks--;
1432 gfs2_free_meta(ip, bstart, blen);
1434 ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1436 error = gfs2_meta_inode_buffer(ip, &dibh);
1438 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1439 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1443 gfs2_trans_end(sdp);
1446 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1449 gfs2_rlist_free(&rlist);
1457 static int ea_dealloc_block(struct gfs2_inode *ip)
1459 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1460 struct gfs2_alloc *al = &ip->i_alloc;
1461 struct gfs2_rgrpd *rgd;
1462 struct buffer_head *dibh;
1465 rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1467 gfs2_consist_inode(ip);
1471 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1476 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE +
1477 RES_STATFS + RES_QUOTA, 1);
1481 gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1483 ip->i_di.di_eattr = 0;
1484 if (!ip->i_di.di_blocks)
1485 gfs2_consist_inode(ip);
1486 ip->i_di.di_blocks--;
1488 error = gfs2_meta_inode_buffer(ip, &dibh);
1490 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1491 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1495 gfs2_trans_end(sdp);
1498 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1504 * gfs2_ea_dealloc - deallocate the extended attribute fork
1510 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1512 struct gfs2_alloc *al;
1515 al = gfs2_alloc_get(ip);
1517 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1521 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
1525 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1529 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1530 error = ea_dealloc_indirect(ip);
1535 error = ea_dealloc_block(ip);
1538 gfs2_glock_dq_uninit(&al->al_ri_gh);
1541 gfs2_quota_unhold(ip);