2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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/semaphore.h>
18 #include <asm/uaccess.h>
21 #include "lm_interface.h"
35 * ea_calc_size - returns the acutal number of bytes the request will take up
36 * (not counting any unstuffed data blocks)
41 * Returns: 1 if the EA should be stuffed
44 static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
47 *size = GFS2_EAREQ_SIZE_STUFFED(er);
48 if (*size <= sdp->sd_jbsize)
51 *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);
56 static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
60 if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
63 ea_calc_size(sdp, er, &size);
65 /* This can only happen with 512 byte blocks */
66 if (size > sdp->sd_jbsize)
72 typedef int (*ea_call_t) (struct gfs2_inode *ip,
73 struct buffer_head *bh,
74 struct gfs2_ea_header *ea,
75 struct gfs2_ea_header *prev,
78 static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
79 ea_call_t ea_call, void *data)
81 struct gfs2_ea_header *ea, *prev = NULL;
84 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_EA))
87 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
88 if (!GFS2_EA_REC_LEN(ea))
90 if (!(bh->b_data <= (char *)ea &&
91 (char *)GFS2_EA2NEXT(ea) <=
92 bh->b_data + bh->b_size))
94 if (!GFS2_EATYPE_VALID(ea->ea_type))
97 error = ea_call(ip, bh, ea, prev, data);
101 if (GFS2_EA_IS_LAST(ea)) {
102 if ((char *)GFS2_EA2NEXT(ea) !=
103 bh->b_data + bh->b_size)
112 gfs2_consist_inode(ip);
116 static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
118 struct buffer_head *bh, *eabh;
119 uint64_t *eablk, *end;
122 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
123 DIO_START | DIO_WAIT, &bh);
127 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
128 error = ea_foreach_i(ip, bh, ea_call, data);
132 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_IN)) {
137 eablk = (uint64_t *)(bh->b_data + sizeof(struct gfs2_meta_header));
138 end = eablk + ip->i_sbd->sd_inptrs;
140 for (; eablk < end; eablk++) {
145 bn = be64_to_cpu(*eablk);
147 error = gfs2_meta_read(ip->i_gl, bn, DIO_START | DIO_WAIT,
151 error = ea_foreach_i(ip, eabh, ea_call, data);
163 struct gfs2_ea_request *ef_er;
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,
171 struct ea_find *ef = private;
172 struct gfs2_ea_request *er = ef->ef_er;
174 if (ea->ea_type == GFS2_EATYPE_UNUSED)
177 if (ea->ea_type == er->er_type) {
178 if (ea->ea_name_len == er->er_name_len &&
179 !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
180 struct gfs2_ea_location *el = ef->ef_el;
190 else if ((ip->i_di.di_flags & GFS2_DIF_EA_PACKED) &&
191 er->er_type == GFS2_EATYPE_SYS)
198 int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
199 struct gfs2_ea_location *el)
207 memset(el, 0, sizeof(struct gfs2_ea_location));
209 error = ea_foreach(ip, ea_find_i, &ef);
217 * ea_dealloc_unstuffed -
224 * Take advantage of the fact that all unstuffed blocks are
225 * allocated from the same RG. But watch, this may not always
231 static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
232 struct gfs2_ea_header *ea,
233 struct gfs2_ea_header *prev, void *private)
235 int *leave = private;
236 struct gfs2_sbd *sdp = ip->i_sbd;
237 struct gfs2_rgrpd *rgd;
238 struct gfs2_holder rg_gh;
239 struct buffer_head *dibh;
240 uint64_t *dataptrs, bn = 0;
242 unsigned int blen = 0;
243 unsigned int blks = 0;
247 if (GFS2_EA_IS_STUFFED(ea))
250 dataptrs = GFS2_EA2DATAPTRS(ea);
251 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++)
254 bn = be64_to_cpu(*dataptrs);
259 rgd = gfs2_blk2rgrpd(sdp, bn);
261 gfs2_consist_inode(ip);
265 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
269 error = gfs2_trans_begin(sdp, rgd->rd_ri.ri_length +
270 RES_DINODE + RES_EATTR + RES_STATFS +
275 gfs2_trans_add_bh(ip->i_gl, bh, 1);
277 dataptrs = GFS2_EA2DATAPTRS(ea);
278 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
281 bn = be64_to_cpu(*dataptrs);
283 if (bstart + blen == bn)
287 gfs2_free_meta(ip, bstart, blen);
293 if (!ip->i_di.di_blocks)
294 gfs2_consist_inode(ip);
295 ip->i_di.di_blocks--;
298 gfs2_free_meta(ip, bstart, blen);
300 if (prev && !leave) {
303 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
304 prev->ea_rec_len = cpu_to_be32(len);
306 if (GFS2_EA_IS_LAST(ea))
307 prev->ea_flags |= GFS2_EAFLAG_LAST;
309 ea->ea_type = GFS2_EATYPE_UNUSED;
313 error = gfs2_meta_inode_buffer(ip, &dibh);
315 ip->i_di.di_ctime = get_seconds();
316 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
317 gfs2_dinode_out(&ip->i_di, dibh->b_data);
324 gfs2_glock_dq_uninit(&rg_gh);
329 static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
330 struct gfs2_ea_header *ea,
331 struct gfs2_ea_header *prev, int leave)
333 struct gfs2_alloc *al;
336 al = gfs2_alloc_get(ip);
338 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
342 error = gfs2_rindex_hold(ip->i_sbd, &al->al_ri_gh);
346 error = ea_dealloc_unstuffed(ip,
348 (leave) ? &error : NULL);
350 gfs2_glock_dq_uninit(&al->al_ri_gh);
353 gfs2_quota_unhold(ip);
362 static int gfs2_ea_repack_i(struct gfs2_inode *ip)
367 int gfs2_ea_repack(struct gfs2_inode *ip)
369 struct gfs2_holder gh;
372 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
376 /* Some sort of permissions checking would be nice */
378 error = gfs2_ea_repack_i(ip);
380 gfs2_glock_dq_uninit(&gh);
386 struct gfs2_ea_request *ei_er;
387 unsigned int ei_size;
390 static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
391 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
394 struct ea_list *ei = private;
395 struct gfs2_ea_request *er = ei->ei_er;
396 unsigned int ea_size = GFS2_EA_STRLEN(ea);
398 if (ea->ea_type == GFS2_EATYPE_UNUSED)
401 if (er->er_data_len) {
406 if (ei->ei_size + ea_size > er->er_data_len)
409 if (ea->ea_type == GFS2_EATYPE_USR) {
417 memcpy(er->er_data + ei->ei_size,
419 memcpy(er->er_data + ei->ei_size + l,
422 memcpy(er->er_data + ei->ei_size +
427 ei->ei_size += ea_size;
437 * Returns: actual size of data on success, -errno on error
440 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
442 struct gfs2_holder i_gh;
445 if (!er->er_data || !er->er_data_len) {
450 error = gfs2_glock_nq_init(ip->i_gl,
451 LM_ST_SHARED, LM_FLAG_ANY,
456 if (ip->i_di.di_eattr) {
457 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
459 error = ea_foreach(ip, ea_list_i, &ei);
464 gfs2_glock_dq_uninit(&i_gh);
470 * ea_get_unstuffed - actually copies the unstuffed data into the
479 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
482 struct gfs2_sbd *sdp = ip->i_sbd;
483 struct buffer_head **bh;
484 unsigned int amount = GFS2_EA_DATA_LEN(ea);
485 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
486 uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
490 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
494 for (x = 0; x < nptrs; x++) {
495 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
505 for (x = 0; x < nptrs; x++) {
506 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
508 for (; x < nptrs; x++)
512 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
513 for (; x < nptrs; x++)
520 bh[x]->b_data + sizeof(struct gfs2_meta_header),
521 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
523 amount -= sdp->sd_jbsize;
524 data += sdp->sd_jbsize;
535 int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
538 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
540 GFS2_EA2DATA(el->el_ea),
541 GFS2_EA_DATA_LEN(el->el_ea));
544 return ea_get_unstuffed(ip, el->el_ea, data);
552 * Returns: actual size of data on success, -errno on error
555 int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
557 struct gfs2_ea_location el;
560 if (!ip->i_di.di_eattr)
563 error = gfs2_ea_find(ip, er, &el);
569 if (er->er_data_len) {
570 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
573 error = gfs2_ea_get_copy(ip, &el, er->er_data);
576 error = GFS2_EA_DATA_LEN(el.el_ea);
588 * Returns: actual size of data on success, -errno on error
591 int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
593 struct gfs2_holder i_gh;
596 if (!er->er_name_len ||
597 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
599 if (!er->er_data || !er->er_data_len) {
604 error = gfs2_glock_nq_init(ip->i_gl,
605 LM_ST_SHARED, LM_FLAG_ANY,
610 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
612 gfs2_glock_dq_uninit(&i_gh);
618 * ea_alloc_blk - allocates a new block for extended attributes.
619 * @ip: A pointer to the inode that's getting extended attributes
625 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
627 struct gfs2_sbd *sdp = ip->i_sbd;
628 struct gfs2_ea_header *ea;
631 block = gfs2_alloc_meta(ip);
633 *bhp = gfs2_meta_new(ip->i_gl, block);
634 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
635 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
636 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
638 ea = GFS2_EA_BH2FIRST(*bhp);
639 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
640 ea->ea_type = GFS2_EATYPE_UNUSED;
641 ea->ea_flags = GFS2_EAFLAG_LAST;
644 ip->i_di.di_blocks++;
650 * ea_write - writes the request info to an ea, creating new blocks if
652 * @ip: inode that is being modified
653 * @ea: the location of the new ea in a block
654 * @er: the write request
656 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
661 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
662 struct gfs2_ea_request *er)
664 struct gfs2_sbd *sdp = ip->i_sbd;
666 ea->ea_data_len = cpu_to_be32(er->er_data_len);
667 ea->ea_name_len = er->er_name_len;
668 ea->ea_type = er->er_type;
671 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
673 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
675 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
677 uint64_t *dataptr = GFS2_EA2DATAPTRS(ea);
678 const char *data = er->er_data;
679 unsigned int data_len = er->er_data_len;
683 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
684 for (x = 0; x < ea->ea_num_ptrs; x++) {
685 struct buffer_head *bh;
687 int mh_size = sizeof(struct gfs2_meta_header);
689 block = gfs2_alloc_meta(ip);
691 bh = gfs2_meta_new(ip->i_gl, block);
692 gfs2_trans_add_bh(ip->i_gl, bh, 1);
693 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
695 ip->i_di.di_blocks++;
697 copy = (data_len > sdp->sd_jbsize) ? sdp->sd_jbsize :
699 memcpy(bh->b_data + mh_size, data, copy);
700 if (copy < sdp->sd_jbsize)
701 memset(bh->b_data + mh_size + copy, 0,
702 sdp->sd_jbsize - copy);
704 *dataptr++ = cpu_to_be64((uint64_t)bh->b_blocknr);
711 gfs2_assert_withdraw(sdp, !data_len);
717 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
718 struct gfs2_ea_request *er,
721 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
723 ea_skeleton_call_t skeleton_call,
726 struct gfs2_alloc *al;
727 struct buffer_head *dibh;
730 al = gfs2_alloc_get(ip);
732 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
736 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
740 al->al_requested = blks;
742 error = gfs2_inplace_reserve(ip);
746 error = gfs2_trans_begin(ip->i_sbd,
747 blks + al->al_rgd->rd_ri.ri_length +
748 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
752 error = skeleton_call(ip, er, private);
756 error = gfs2_meta_inode_buffer(ip, &dibh);
758 if (er->er_flags & GFS2_ERF_MODE) {
759 gfs2_assert_withdraw(ip->i_sbd,
760 (ip->i_di.di_mode & S_IFMT) ==
761 (er->er_mode & S_IFMT));
762 ip->i_di.di_mode = er->er_mode;
764 ip->i_di.di_ctime = get_seconds();
765 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
766 gfs2_dinode_out(&ip->i_di, dibh->b_data);
771 gfs2_trans_end(ip->i_sbd);
774 gfs2_inplace_release(ip);
777 gfs2_quota_unlock(ip);
785 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
788 struct buffer_head *bh;
791 error = ea_alloc_blk(ip, &bh);
795 ip->i_di.di_eattr = bh->b_blocknr;
796 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
804 * ea_init - initializes a new eattr block
811 static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
813 unsigned int jbsize = ip->i_sbd->sd_jbsize;
814 unsigned int blks = 1;
816 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
817 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
819 return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
822 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
824 uint32_t ea_size = GFS2_EA_SIZE(ea);
825 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
827 uint32_t new_size = GFS2_EA_REC_LEN(ea) - ea_size;
828 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
830 ea->ea_rec_len = cpu_to_be32(ea_size);
831 ea->ea_flags ^= last;
833 new->ea_rec_len = cpu_to_be32(new_size);
834 new->ea_flags = last;
839 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
840 struct gfs2_ea_location *el)
842 struct gfs2_ea_header *ea = el->el_ea;
843 struct gfs2_ea_header *prev = el->el_prev;
846 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
848 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
849 ea->ea_type = GFS2_EATYPE_UNUSED;
851 } else if (GFS2_EA2NEXT(prev) != ea) {
852 prev = GFS2_EA2NEXT(prev);
853 gfs2_assert_withdraw(ip->i_sbd, GFS2_EA2NEXT(prev) == ea);
856 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
857 prev->ea_rec_len = cpu_to_be32(len);
859 if (GFS2_EA_IS_LAST(ea))
860 prev->ea_flags |= GFS2_EAFLAG_LAST;
866 struct gfs2_ea_request *es_er;
867 struct gfs2_ea_location *es_el;
869 struct buffer_head *es_bh;
870 struct gfs2_ea_header *es_ea;
873 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
874 struct gfs2_ea_header *ea, struct ea_set *es)
876 struct gfs2_ea_request *er = es->es_er;
877 struct buffer_head *dibh;
880 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + 2 * RES_EATTR, 0);
884 gfs2_trans_add_bh(ip->i_gl, bh, 1);
887 ea = ea_split_ea(ea);
889 ea_write(ip, ea, er);
892 ea_set_remove_stuffed(ip, es->es_el);
894 error = gfs2_meta_inode_buffer(ip, &dibh);
898 if (er->er_flags & GFS2_ERF_MODE) {
899 gfs2_assert_withdraw(ip->i_sbd,
900 (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
901 ip->i_di.di_mode = er->er_mode;
903 ip->i_di.di_ctime = get_seconds();
904 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
905 gfs2_dinode_out(&ip->i_di, dibh->b_data);
908 gfs2_trans_end(ip->i_sbd);
913 static int ea_set_simple_alloc(struct gfs2_inode *ip,
914 struct gfs2_ea_request *er, void *private)
916 struct ea_set *es = private;
917 struct gfs2_ea_header *ea = es->es_ea;
920 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
923 ea = ea_split_ea(ea);
925 error = ea_write(ip, ea, er);
930 ea_set_remove_stuffed(ip, es->es_el);
935 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
936 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
939 struct ea_set *es = private;
944 stuffed = ea_calc_size(ip->i_sbd, es->es_er, &size);
946 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
947 if (GFS2_EA_REC_LEN(ea) < size)
949 if (!GFS2_EA_IS_STUFFED(ea)) {
950 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
955 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
961 error = ea_set_simple_noalloc(ip, bh, ea, es);
969 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
970 ip->i_sbd->sd_jbsize);
972 error = ea_alloc_skeleton(ip, es->es_er, blks,
973 ea_set_simple_alloc, es);
981 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
984 struct gfs2_sbd *sdp = ip->i_sbd;
985 struct buffer_head *indbh, *newbh;
988 int mh_size = sizeof(struct gfs2_meta_header);
990 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
993 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
994 DIO_START | DIO_WAIT, &indbh);
998 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1003 eablk = (uint64_t *)(indbh->b_data + mh_size);
1004 end = eablk + sdp->sd_inptrs;
1006 for (; eablk < end; eablk++)
1015 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1019 blk = gfs2_alloc_meta(ip);
1021 indbh = gfs2_meta_new(ip->i_gl, blk);
1022 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1023 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
1024 gfs2_buffer_clear_tail(indbh, mh_size);
1026 eablk = (uint64_t *)(indbh->b_data + mh_size);
1027 *eablk = cpu_to_be64(ip->i_di.di_eattr);
1028 ip->i_di.di_eattr = blk;
1029 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
1030 ip->i_di.di_blocks++;
1035 error = ea_alloc_blk(ip, &newbh);
1039 *eablk = cpu_to_be64((uint64_t)newbh->b_blocknr);
1040 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1046 ea_set_remove_stuffed(ip, (struct gfs2_ea_location *)private);
1054 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1055 struct gfs2_ea_location *el)
1058 unsigned int blks = 2;
1061 memset(&es, 0, sizeof(struct ea_set));
1065 error = ea_foreach(ip, ea_set_simple, &es);
1071 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1073 if (GFS2_EAREQ_SIZE_STUFFED(er) > ip->i_sbd->sd_jbsize)
1074 blks += DIV_ROUND_UP(er->er_data_len, ip->i_sbd->sd_jbsize);
1076 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1079 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1080 struct gfs2_ea_location *el)
1082 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1083 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1084 gfs2_assert_withdraw(ip->i_sbd,
1085 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1088 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1091 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1093 struct gfs2_ea_location el;
1096 if (!ip->i_di.di_eattr) {
1097 if (er->er_flags & XATTR_REPLACE)
1099 return ea_init(ip, er);
1102 error = gfs2_ea_find(ip, er, &el);
1107 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1113 if (!(er->er_flags & XATTR_CREATE)) {
1114 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1115 error = ea_set_i(ip, er, &el);
1116 if (!error && unstuffed)
1117 ea_set_remove_unstuffed(ip, &el);
1123 if (!(er->er_flags & XATTR_REPLACE))
1124 error = ea_set_i(ip, er, NULL);
1130 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1132 struct gfs2_holder i_gh;
1135 if (!er->er_name_len ||
1136 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1138 if (!er->er_data || !er->er_data_len) {
1140 er->er_data_len = 0;
1142 error = ea_check_size(ip->i_sbd, er);
1146 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1150 if (IS_IMMUTABLE(ip->i_vnode))
1153 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1155 gfs2_glock_dq_uninit(&i_gh);
1160 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1162 struct gfs2_ea_header *ea = el->el_ea;
1163 struct gfs2_ea_header *prev = el->el_prev;
1164 struct buffer_head *dibh;
1167 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
1171 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1176 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1177 prev->ea_rec_len = cpu_to_be32(len);
1179 if (GFS2_EA_IS_LAST(ea))
1180 prev->ea_flags |= GFS2_EAFLAG_LAST;
1182 ea->ea_type = GFS2_EATYPE_UNUSED;
1184 error = gfs2_meta_inode_buffer(ip, &dibh);
1186 ip->i_di.di_ctime = get_seconds();
1187 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1188 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1192 gfs2_trans_end(ip->i_sbd);
1197 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1199 struct gfs2_ea_location el;
1202 if (!ip->i_di.di_eattr)
1205 error = gfs2_ea_find(ip, er, &el);
1211 if (GFS2_EA_IS_STUFFED(el.el_ea))
1212 error = ea_remove_stuffed(ip, &el);
1214 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1223 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1224 * @ip: pointer to the inode of the target file
1225 * @er: request information
1230 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1232 struct gfs2_holder i_gh;
1235 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1238 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1242 if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1245 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1247 gfs2_glock_dq_uninit(&i_gh);
1252 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1253 struct gfs2_ea_header *ea, char *data)
1255 struct gfs2_sbd *sdp = ip->i_sbd;
1256 struct buffer_head **bh;
1257 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1258 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1259 uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
1263 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1267 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1271 for (x = 0; x < nptrs; x++) {
1272 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
1282 for (x = 0; x < nptrs; x++) {
1283 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
1285 for (; x < nptrs; x++)
1289 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1290 for (; x < nptrs; x++)
1296 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1298 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header),
1300 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1302 amount -= sdp->sd_jbsize;
1303 data += sdp->sd_jbsize;
1314 gfs2_trans_end(sdp);
1320 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1321 struct iattr *attr, char *data)
1323 struct buffer_head *dibh;
1326 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1327 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
1331 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1332 memcpy(GFS2_EA2DATA(el->el_ea),
1334 GFS2_EA_DATA_LEN(el->el_ea));
1336 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1341 error = gfs2_meta_inode_buffer(ip, &dibh);
1343 error = inode_setattr(ip->i_vnode, attr);
1344 gfs2_assert_warn(ip->i_sbd, !error);
1345 gfs2_inode_attr_out(ip);
1346 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1347 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1351 gfs2_trans_end(ip->i_sbd);
1356 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1358 struct gfs2_sbd *sdp = ip->i_sbd;
1359 struct gfs2_rgrp_list rlist;
1360 struct buffer_head *indbh, *dibh;
1361 uint64_t *eablk, *end;
1362 unsigned int rg_blocks = 0;
1363 uint64_t bstart = 0;
1364 unsigned int blen = 0;
1365 unsigned int blks = 0;
1369 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1371 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
1372 DIO_START | DIO_WAIT, &indbh);
1376 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1381 eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1382 end = eablk + sdp->sd_inptrs;
1384 for (; eablk < end; eablk++) {
1389 bn = be64_to_cpu(*eablk);
1391 if (bstart + blen == bn)
1395 gfs2_rlist_add(sdp, &rlist, bstart);
1402 gfs2_rlist_add(sdp, &rlist, bstart);
1406 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1408 for (x = 0; x < rlist.rl_rgrps; x++) {
1409 struct gfs2_rgrpd *rgd;
1410 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1411 rg_blocks += rgd->rd_ri.ri_length;
1414 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1416 goto out_rlist_free;
1418 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
1419 RES_INDIRECT + RES_STATFS +
1424 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1426 eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1430 for (; eablk < end; eablk++) {
1435 bn = be64_to_cpu(*eablk);
1437 if (bstart + blen == bn)
1441 gfs2_free_meta(ip, bstart, blen);
1447 if (!ip->i_di.di_blocks)
1448 gfs2_consist_inode(ip);
1449 ip->i_di.di_blocks--;
1452 gfs2_free_meta(ip, bstart, blen);
1454 ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1456 error = gfs2_meta_inode_buffer(ip, &dibh);
1458 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1459 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1463 gfs2_trans_end(sdp);
1466 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1469 gfs2_rlist_free(&rlist);
1477 static int ea_dealloc_block(struct gfs2_inode *ip)
1479 struct gfs2_sbd *sdp = ip->i_sbd;
1480 struct gfs2_alloc *al = &ip->i_alloc;
1481 struct gfs2_rgrpd *rgd;
1482 struct buffer_head *dibh;
1485 rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1487 gfs2_consist_inode(ip);
1491 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1496 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE +
1497 RES_STATFS + RES_QUOTA, 1);
1501 gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1503 ip->i_di.di_eattr = 0;
1504 if (!ip->i_di.di_blocks)
1505 gfs2_consist_inode(ip);
1506 ip->i_di.di_blocks--;
1508 error = gfs2_meta_inode_buffer(ip, &dibh);
1510 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1511 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1515 gfs2_trans_end(sdp);
1518 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1524 * gfs2_ea_dealloc - deallocate the extended attribute fork
1530 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1532 struct gfs2_alloc *al;
1535 al = gfs2_alloc_get(ip);
1537 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1541 error = gfs2_rindex_hold(ip->i_sbd, &al->al_ri_gh);
1545 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1549 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1550 error = ea_dealloc_indirect(ip);
1555 error = ea_dealloc_block(ip);
1558 gfs2_glock_dq_uninit(&al->al_ri_gh);
1561 gfs2_quota_unhold(ip);