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(ip->i_sbd, 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(ip->i_sbd, bh, GFS2_METATYPE_IN)) {
136 eablk = (uint64_t *)(bh->b_data + sizeof(struct gfs2_meta_header));
137 end = eablk + ip->i_sbd->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 = ip->i_sbd;
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(ip->i_sbd, &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,
403 memcpy(er->er_data + ei->ei_size + l,
406 memcpy(er->er_data + ei->ei_size +
411 ei->ei_size += ea_size;
421 * Returns: actual size of data on success, -errno on error
424 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
426 struct gfs2_holder i_gh;
429 if (!er->er_data || !er->er_data_len) {
434 error = gfs2_glock_nq_init(ip->i_gl,
435 LM_ST_SHARED, LM_FLAG_ANY,
440 if (ip->i_di.di_eattr) {
441 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
443 error = ea_foreach(ip, ea_list_i, &ei);
448 gfs2_glock_dq_uninit(&i_gh);
454 * ea_get_unstuffed - actually copies the unstuffed data into the
463 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
466 struct gfs2_sbd *sdp = ip->i_sbd;
467 struct buffer_head **bh;
468 unsigned int amount = GFS2_EA_DATA_LEN(ea);
469 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
470 uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
474 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
478 for (x = 0; x < nptrs; x++) {
479 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
489 for (x = 0; x < nptrs; x++) {
490 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
492 for (; x < nptrs; x++)
496 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
497 for (; x < nptrs; x++)
504 bh[x]->b_data + sizeof(struct gfs2_meta_header),
505 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
507 amount -= sdp->sd_jbsize;
508 data += sdp->sd_jbsize;
519 int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
522 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
524 GFS2_EA2DATA(el->el_ea),
525 GFS2_EA_DATA_LEN(el->el_ea));
528 return ea_get_unstuffed(ip, el->el_ea, data);
536 * Returns: actual size of data on success, -errno on error
539 int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
541 struct gfs2_ea_location el;
544 if (!ip->i_di.di_eattr)
547 error = gfs2_ea_find(ip, er, &el);
553 if (er->er_data_len) {
554 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
557 error = gfs2_ea_get_copy(ip, &el, er->er_data);
560 error = GFS2_EA_DATA_LEN(el.el_ea);
572 * Returns: actual size of data on success, -errno on error
575 int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
577 struct gfs2_holder i_gh;
580 if (!er->er_name_len ||
581 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
583 if (!er->er_data || !er->er_data_len) {
588 error = gfs2_glock_nq_init(ip->i_gl,
589 LM_ST_SHARED, LM_FLAG_ANY,
594 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
596 gfs2_glock_dq_uninit(&i_gh);
602 * ea_alloc_blk - allocates a new block for extended attributes.
603 * @ip: A pointer to the inode that's getting extended attributes
609 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
611 struct gfs2_sbd *sdp = ip->i_sbd;
612 struct gfs2_ea_header *ea;
615 block = gfs2_alloc_meta(ip);
617 *bhp = gfs2_meta_new(ip->i_gl, block);
618 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
619 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
620 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
622 ea = GFS2_EA_BH2FIRST(*bhp);
623 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
624 ea->ea_type = GFS2_EATYPE_UNUSED;
625 ea->ea_flags = GFS2_EAFLAG_LAST;
628 ip->i_di.di_blocks++;
634 * ea_write - writes the request info to an ea, creating new blocks if
636 * @ip: inode that is being modified
637 * @ea: the location of the new ea in a block
638 * @er: the write request
640 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
645 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
646 struct gfs2_ea_request *er)
648 struct gfs2_sbd *sdp = ip->i_sbd;
650 ea->ea_data_len = cpu_to_be32(er->er_data_len);
651 ea->ea_name_len = er->er_name_len;
652 ea->ea_type = er->er_type;
655 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
657 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
659 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
661 uint64_t *dataptr = GFS2_EA2DATAPTRS(ea);
662 const char *data = er->er_data;
663 unsigned int data_len = er->er_data_len;
667 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
668 for (x = 0; x < ea->ea_num_ptrs; x++) {
669 struct buffer_head *bh;
671 int mh_size = sizeof(struct gfs2_meta_header);
673 block = gfs2_alloc_meta(ip);
675 bh = gfs2_meta_new(ip->i_gl, block);
676 gfs2_trans_add_bh(ip->i_gl, bh, 1);
677 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
679 ip->i_di.di_blocks++;
681 copy = (data_len > sdp->sd_jbsize) ? sdp->sd_jbsize :
683 memcpy(bh->b_data + mh_size, data, copy);
684 if (copy < sdp->sd_jbsize)
685 memset(bh->b_data + mh_size + copy, 0,
686 sdp->sd_jbsize - copy);
688 *dataptr++ = cpu_to_be64((uint64_t)bh->b_blocknr);
695 gfs2_assert_withdraw(sdp, !data_len);
701 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
702 struct gfs2_ea_request *er,
705 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
707 ea_skeleton_call_t skeleton_call,
710 struct gfs2_alloc *al;
711 struct buffer_head *dibh;
714 al = gfs2_alloc_get(ip);
716 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
720 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
724 al->al_requested = blks;
726 error = gfs2_inplace_reserve(ip);
730 error = gfs2_trans_begin(ip->i_sbd,
731 blks + al->al_rgd->rd_ri.ri_length +
732 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
736 error = skeleton_call(ip, er, private);
740 error = gfs2_meta_inode_buffer(ip, &dibh);
742 if (er->er_flags & GFS2_ERF_MODE) {
743 gfs2_assert_withdraw(ip->i_sbd,
744 (ip->i_di.di_mode & S_IFMT) ==
745 (er->er_mode & S_IFMT));
746 ip->i_di.di_mode = er->er_mode;
748 ip->i_di.di_ctime = get_seconds();
749 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
750 gfs2_dinode_out(&ip->i_di, dibh->b_data);
755 gfs2_trans_end(ip->i_sbd);
758 gfs2_inplace_release(ip);
761 gfs2_quota_unlock(ip);
769 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
772 struct buffer_head *bh;
775 error = ea_alloc_blk(ip, &bh);
779 ip->i_di.di_eattr = bh->b_blocknr;
780 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
788 * ea_init - initializes a new eattr block
795 static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
797 unsigned int jbsize = ip->i_sbd->sd_jbsize;
798 unsigned int blks = 1;
800 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
801 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
803 return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
806 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
808 uint32_t ea_size = GFS2_EA_SIZE(ea);
809 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
811 uint32_t new_size = GFS2_EA_REC_LEN(ea) - ea_size;
812 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
814 ea->ea_rec_len = cpu_to_be32(ea_size);
815 ea->ea_flags ^= last;
817 new->ea_rec_len = cpu_to_be32(new_size);
818 new->ea_flags = last;
823 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
824 struct gfs2_ea_location *el)
826 struct gfs2_ea_header *ea = el->el_ea;
827 struct gfs2_ea_header *prev = el->el_prev;
830 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
832 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
833 ea->ea_type = GFS2_EATYPE_UNUSED;
835 } else if (GFS2_EA2NEXT(prev) != ea) {
836 prev = GFS2_EA2NEXT(prev);
837 gfs2_assert_withdraw(ip->i_sbd, GFS2_EA2NEXT(prev) == ea);
840 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
841 prev->ea_rec_len = cpu_to_be32(len);
843 if (GFS2_EA_IS_LAST(ea))
844 prev->ea_flags |= GFS2_EAFLAG_LAST;
850 struct gfs2_ea_request *es_er;
851 struct gfs2_ea_location *es_el;
853 struct buffer_head *es_bh;
854 struct gfs2_ea_header *es_ea;
857 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
858 struct gfs2_ea_header *ea, struct ea_set *es)
860 struct gfs2_ea_request *er = es->es_er;
861 struct buffer_head *dibh;
864 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + 2 * RES_EATTR, 0);
868 gfs2_trans_add_bh(ip->i_gl, bh, 1);
871 ea = ea_split_ea(ea);
873 ea_write(ip, ea, er);
876 ea_set_remove_stuffed(ip, es->es_el);
878 error = gfs2_meta_inode_buffer(ip, &dibh);
882 if (er->er_flags & GFS2_ERF_MODE) {
883 gfs2_assert_withdraw(ip->i_sbd,
884 (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
885 ip->i_di.di_mode = er->er_mode;
887 ip->i_di.di_ctime = get_seconds();
888 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
889 gfs2_dinode_out(&ip->i_di, dibh->b_data);
892 gfs2_trans_end(ip->i_sbd);
897 static int ea_set_simple_alloc(struct gfs2_inode *ip,
898 struct gfs2_ea_request *er, void *private)
900 struct ea_set *es = private;
901 struct gfs2_ea_header *ea = es->es_ea;
904 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
907 ea = ea_split_ea(ea);
909 error = ea_write(ip, ea, er);
914 ea_set_remove_stuffed(ip, es->es_el);
919 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
920 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
923 struct ea_set *es = private;
928 stuffed = ea_calc_size(ip->i_sbd, es->es_er, &size);
930 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
931 if (GFS2_EA_REC_LEN(ea) < size)
933 if (!GFS2_EA_IS_STUFFED(ea)) {
934 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
939 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
945 error = ea_set_simple_noalloc(ip, bh, ea, es);
953 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
954 ip->i_sbd->sd_jbsize);
956 error = ea_alloc_skeleton(ip, es->es_er, blks,
957 ea_set_simple_alloc, es);
965 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
968 struct gfs2_sbd *sdp = ip->i_sbd;
969 struct buffer_head *indbh, *newbh;
972 int mh_size = sizeof(struct gfs2_meta_header);
974 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
977 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
978 DIO_START | DIO_WAIT, &indbh);
982 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
987 eablk = (uint64_t *)(indbh->b_data + mh_size);
988 end = eablk + sdp->sd_inptrs;
990 for (; eablk < end; eablk++)
999 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1003 blk = gfs2_alloc_meta(ip);
1005 indbh = gfs2_meta_new(ip->i_gl, blk);
1006 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1007 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
1008 gfs2_buffer_clear_tail(indbh, mh_size);
1010 eablk = (uint64_t *)(indbh->b_data + mh_size);
1011 *eablk = cpu_to_be64(ip->i_di.di_eattr);
1012 ip->i_di.di_eattr = blk;
1013 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
1014 ip->i_di.di_blocks++;
1019 error = ea_alloc_blk(ip, &newbh);
1023 *eablk = cpu_to_be64((uint64_t)newbh->b_blocknr);
1024 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1030 ea_set_remove_stuffed(ip, (struct gfs2_ea_location *)private);
1038 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1039 struct gfs2_ea_location *el)
1042 unsigned int blks = 2;
1045 memset(&es, 0, sizeof(struct ea_set));
1049 error = ea_foreach(ip, ea_set_simple, &es);
1055 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1057 if (GFS2_EAREQ_SIZE_STUFFED(er) > ip->i_sbd->sd_jbsize)
1058 blks += DIV_ROUND_UP(er->er_data_len, ip->i_sbd->sd_jbsize);
1060 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1063 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1064 struct gfs2_ea_location *el)
1066 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1067 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1068 gfs2_assert_withdraw(ip->i_sbd,
1069 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1072 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1075 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1077 struct gfs2_ea_location el;
1080 if (!ip->i_di.di_eattr) {
1081 if (er->er_flags & XATTR_REPLACE)
1083 return ea_init(ip, er);
1086 error = gfs2_ea_find(ip, er, &el);
1091 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1097 if (!(er->er_flags & XATTR_CREATE)) {
1098 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1099 error = ea_set_i(ip, er, &el);
1100 if (!error && unstuffed)
1101 ea_set_remove_unstuffed(ip, &el);
1107 if (!(er->er_flags & XATTR_REPLACE))
1108 error = ea_set_i(ip, er, NULL);
1114 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1116 struct gfs2_holder i_gh;
1119 if (!er->er_name_len ||
1120 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1122 if (!er->er_data || !er->er_data_len) {
1124 er->er_data_len = 0;
1126 error = ea_check_size(ip->i_sbd, er);
1130 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1134 if (IS_IMMUTABLE(ip->i_vnode))
1137 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1139 gfs2_glock_dq_uninit(&i_gh);
1144 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1146 struct gfs2_ea_header *ea = el->el_ea;
1147 struct gfs2_ea_header *prev = el->el_prev;
1148 struct buffer_head *dibh;
1151 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
1155 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1160 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1161 prev->ea_rec_len = cpu_to_be32(len);
1163 if (GFS2_EA_IS_LAST(ea))
1164 prev->ea_flags |= GFS2_EAFLAG_LAST;
1166 ea->ea_type = GFS2_EATYPE_UNUSED;
1168 error = gfs2_meta_inode_buffer(ip, &dibh);
1170 ip->i_di.di_ctime = get_seconds();
1171 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1172 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1176 gfs2_trans_end(ip->i_sbd);
1181 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1183 struct gfs2_ea_location el;
1186 if (!ip->i_di.di_eattr)
1189 error = gfs2_ea_find(ip, er, &el);
1195 if (GFS2_EA_IS_STUFFED(el.el_ea))
1196 error = ea_remove_stuffed(ip, &el);
1198 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1207 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1208 * @ip: pointer to the inode of the target file
1209 * @er: request information
1214 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1216 struct gfs2_holder i_gh;
1219 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1222 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1226 if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1229 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1231 gfs2_glock_dq_uninit(&i_gh);
1236 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1237 struct gfs2_ea_header *ea, char *data)
1239 struct gfs2_sbd *sdp = ip->i_sbd;
1240 struct buffer_head **bh;
1241 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1242 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1243 uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
1247 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1251 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1255 for (x = 0; x < nptrs; x++) {
1256 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
1266 for (x = 0; x < nptrs; x++) {
1267 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
1269 for (; x < nptrs; x++)
1273 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1274 for (; x < nptrs; x++)
1280 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1282 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header),
1284 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1286 amount -= sdp->sd_jbsize;
1287 data += sdp->sd_jbsize;
1298 gfs2_trans_end(sdp);
1304 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1305 struct iattr *attr, char *data)
1307 struct buffer_head *dibh;
1310 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1311 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
1315 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1316 memcpy(GFS2_EA2DATA(el->el_ea),
1318 GFS2_EA_DATA_LEN(el->el_ea));
1320 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1325 error = gfs2_meta_inode_buffer(ip, &dibh);
1327 error = inode_setattr(ip->i_vnode, attr);
1328 gfs2_assert_warn(ip->i_sbd, !error);
1329 gfs2_inode_attr_out(ip);
1330 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1331 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1335 gfs2_trans_end(ip->i_sbd);
1340 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1342 struct gfs2_sbd *sdp = ip->i_sbd;
1343 struct gfs2_rgrp_list rlist;
1344 struct buffer_head *indbh, *dibh;
1345 uint64_t *eablk, *end;
1346 unsigned int rg_blocks = 0;
1347 uint64_t bstart = 0;
1348 unsigned int blen = 0;
1349 unsigned int blks = 0;
1353 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1355 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
1356 DIO_START | DIO_WAIT, &indbh);
1360 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1365 eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1366 end = eablk + sdp->sd_inptrs;
1368 for (; eablk < end; eablk++) {
1373 bn = be64_to_cpu(*eablk);
1375 if (bstart + blen == bn)
1379 gfs2_rlist_add(sdp, &rlist, bstart);
1386 gfs2_rlist_add(sdp, &rlist, bstart);
1390 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1392 for (x = 0; x < rlist.rl_rgrps; x++) {
1393 struct gfs2_rgrpd *rgd;
1394 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1395 rg_blocks += rgd->rd_ri.ri_length;
1398 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1400 goto out_rlist_free;
1402 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
1403 RES_INDIRECT + RES_STATFS +
1408 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1410 eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1414 for (; eablk < end; eablk++) {
1419 bn = be64_to_cpu(*eablk);
1421 if (bstart + blen == bn)
1425 gfs2_free_meta(ip, bstart, blen);
1431 if (!ip->i_di.di_blocks)
1432 gfs2_consist_inode(ip);
1433 ip->i_di.di_blocks--;
1436 gfs2_free_meta(ip, bstart, blen);
1438 ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1440 error = gfs2_meta_inode_buffer(ip, &dibh);
1442 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1443 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1447 gfs2_trans_end(sdp);
1450 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1453 gfs2_rlist_free(&rlist);
1461 static int ea_dealloc_block(struct gfs2_inode *ip)
1463 struct gfs2_sbd *sdp = ip->i_sbd;
1464 struct gfs2_alloc *al = &ip->i_alloc;
1465 struct gfs2_rgrpd *rgd;
1466 struct buffer_head *dibh;
1469 rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1471 gfs2_consist_inode(ip);
1475 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1480 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE +
1481 RES_STATFS + RES_QUOTA, 1);
1485 gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1487 ip->i_di.di_eattr = 0;
1488 if (!ip->i_di.di_blocks)
1489 gfs2_consist_inode(ip);
1490 ip->i_di.di_blocks--;
1492 error = gfs2_meta_inode_buffer(ip, &dibh);
1494 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1495 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1499 gfs2_trans_end(sdp);
1502 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1508 * gfs2_ea_dealloc - deallocate the extended attribute fork
1514 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1516 struct gfs2_alloc *al;
1519 al = gfs2_alloc_get(ip);
1521 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1525 error = gfs2_rindex_hold(ip->i_sbd, &al->al_ri_gh);
1529 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1533 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1534 error = ea_dealloc_indirect(ip);
1539 error = ea_dealloc_block(ip);
1542 gfs2_glock_dq_uninit(&al->al_ri_gh);
1545 gfs2_quota_unhold(ip);