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/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 if (ea->ea_type == GFS2_EATYPE_USR) {
392 memcpy(er->er_data + ei->ei_size,
394 memcpy(er->er_data + ei->ei_size + l,
397 memcpy(er->er_data + ei->ei_size +
402 ei->ei_size += ea_size;
412 * Returns: actual size of data on success, -errno on error
415 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
417 struct gfs2_holder i_gh;
420 if (!er->er_data || !er->er_data_len) {
425 error = gfs2_glock_nq_init(ip->i_gl,
426 LM_ST_SHARED, LM_FLAG_ANY,
431 if (ip->i_di.di_eattr) {
432 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
434 error = ea_foreach(ip, ea_list_i, &ei);
439 gfs2_glock_dq_uninit(&i_gh);
445 * ea_get_unstuffed - actually copies the unstuffed data into the
454 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
457 struct gfs2_sbd *sdp = ip->i_sbd;
458 struct buffer_head **bh;
459 unsigned int amount = GFS2_EA_DATA_LEN(ea);
460 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
461 uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
465 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
469 for (x = 0; x < nptrs; x++) {
470 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
480 for (x = 0; x < nptrs; x++) {
481 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
483 for (; x < nptrs; x++)
487 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
488 for (; x < nptrs; x++)
495 bh[x]->b_data + sizeof(struct gfs2_meta_header),
496 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
498 amount -= sdp->sd_jbsize;
499 data += sdp->sd_jbsize;
510 int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
513 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
515 GFS2_EA2DATA(el->el_ea),
516 GFS2_EA_DATA_LEN(el->el_ea));
519 return ea_get_unstuffed(ip, el->el_ea, data);
527 * Returns: actual size of data on success, -errno on error
530 int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
532 struct gfs2_ea_location el;
535 if (!ip->i_di.di_eattr)
538 error = gfs2_ea_find(ip, er, &el);
544 if (er->er_data_len) {
545 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
548 error = gfs2_ea_get_copy(ip, &el, er->er_data);
551 error = GFS2_EA_DATA_LEN(el.el_ea);
563 * Returns: actual size of data on success, -errno on error
566 int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
568 struct gfs2_holder i_gh;
571 if (!er->er_name_len ||
572 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
574 if (!er->er_data || !er->er_data_len) {
579 error = gfs2_glock_nq_init(ip->i_gl,
580 LM_ST_SHARED, LM_FLAG_ANY,
585 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
587 gfs2_glock_dq_uninit(&i_gh);
593 * ea_alloc_blk - allocates a new block for extended attributes.
594 * @ip: A pointer to the inode that's getting extended attributes
600 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
602 struct gfs2_sbd *sdp = ip->i_sbd;
603 struct gfs2_ea_header *ea;
606 block = gfs2_alloc_meta(ip);
608 *bhp = gfs2_meta_new(ip->i_gl, block);
609 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
610 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
611 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
613 ea = GFS2_EA_BH2FIRST(*bhp);
614 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
615 ea->ea_type = GFS2_EATYPE_UNUSED;
616 ea->ea_flags = GFS2_EAFLAG_LAST;
619 ip->i_di.di_blocks++;
625 * ea_write - writes the request info to an ea, creating new blocks if
627 * @ip: inode that is being modified
628 * @ea: the location of the new ea in a block
629 * @er: the write request
631 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
636 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
637 struct gfs2_ea_request *er)
639 struct gfs2_sbd *sdp = ip->i_sbd;
641 ea->ea_data_len = cpu_to_be32(er->er_data_len);
642 ea->ea_name_len = er->er_name_len;
643 ea->ea_type = er->er_type;
646 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
648 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
650 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
652 uint64_t *dataptr = GFS2_EA2DATAPTRS(ea);
653 const char *data = er->er_data;
654 unsigned int data_len = er->er_data_len;
658 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
659 for (x = 0; x < ea->ea_num_ptrs; x++) {
660 struct buffer_head *bh;
662 int mh_size = sizeof(struct gfs2_meta_header);
664 block = gfs2_alloc_meta(ip);
666 bh = gfs2_meta_new(ip->i_gl, block);
667 gfs2_trans_add_bh(ip->i_gl, bh, 1);
668 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
670 ip->i_di.di_blocks++;
672 copy = (data_len > sdp->sd_jbsize) ? sdp->sd_jbsize :
674 memcpy(bh->b_data + mh_size, data, copy);
675 if (copy < sdp->sd_jbsize)
676 memset(bh->b_data + mh_size + copy, 0,
677 sdp->sd_jbsize - copy);
679 *dataptr++ = cpu_to_be64((uint64_t)bh->b_blocknr);
686 gfs2_assert_withdraw(sdp, !data_len);
692 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
693 struct gfs2_ea_request *er,
696 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
698 ea_skeleton_call_t skeleton_call,
701 struct gfs2_alloc *al;
702 struct buffer_head *dibh;
705 al = gfs2_alloc_get(ip);
707 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
711 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
715 al->al_requested = blks;
717 error = gfs2_inplace_reserve(ip);
721 error = gfs2_trans_begin(ip->i_sbd,
722 blks + al->al_rgd->rd_ri.ri_length +
723 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
727 error = skeleton_call(ip, er, private);
731 error = gfs2_meta_inode_buffer(ip, &dibh);
733 if (er->er_flags & GFS2_ERF_MODE) {
734 gfs2_assert_withdraw(ip->i_sbd,
735 (ip->i_di.di_mode & S_IFMT) ==
736 (er->er_mode & S_IFMT));
737 ip->i_di.di_mode = er->er_mode;
739 ip->i_di.di_ctime = get_seconds();
740 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
741 gfs2_dinode_out(&ip->i_di, dibh->b_data);
746 gfs2_trans_end(ip->i_sbd);
749 gfs2_inplace_release(ip);
752 gfs2_quota_unlock(ip);
760 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
763 struct buffer_head *bh;
766 error = ea_alloc_blk(ip, &bh);
770 ip->i_di.di_eattr = bh->b_blocknr;
771 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
779 * ea_init - initializes a new eattr block
786 static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
788 unsigned int jbsize = ip->i_sbd->sd_jbsize;
789 unsigned int blks = 1;
791 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
792 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
794 return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
797 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
799 uint32_t ea_size = GFS2_EA_SIZE(ea);
800 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
802 uint32_t new_size = GFS2_EA_REC_LEN(ea) - ea_size;
803 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
805 ea->ea_rec_len = cpu_to_be32(ea_size);
806 ea->ea_flags ^= last;
808 new->ea_rec_len = cpu_to_be32(new_size);
809 new->ea_flags = last;
814 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
815 struct gfs2_ea_location *el)
817 struct gfs2_ea_header *ea = el->el_ea;
818 struct gfs2_ea_header *prev = el->el_prev;
821 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
823 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
824 ea->ea_type = GFS2_EATYPE_UNUSED;
826 } else if (GFS2_EA2NEXT(prev) != ea) {
827 prev = GFS2_EA2NEXT(prev);
828 gfs2_assert_withdraw(ip->i_sbd, GFS2_EA2NEXT(prev) == ea);
831 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
832 prev->ea_rec_len = cpu_to_be32(len);
834 if (GFS2_EA_IS_LAST(ea))
835 prev->ea_flags |= GFS2_EAFLAG_LAST;
841 struct gfs2_ea_request *es_er;
842 struct gfs2_ea_location *es_el;
844 struct buffer_head *es_bh;
845 struct gfs2_ea_header *es_ea;
848 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
849 struct gfs2_ea_header *ea, struct ea_set *es)
851 struct gfs2_ea_request *er = es->es_er;
852 struct buffer_head *dibh;
855 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + 2 * RES_EATTR, 0);
859 gfs2_trans_add_bh(ip->i_gl, bh, 1);
862 ea = ea_split_ea(ea);
864 ea_write(ip, ea, er);
867 ea_set_remove_stuffed(ip, es->es_el);
869 error = gfs2_meta_inode_buffer(ip, &dibh);
873 if (er->er_flags & GFS2_ERF_MODE) {
874 gfs2_assert_withdraw(ip->i_sbd,
875 (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
876 ip->i_di.di_mode = er->er_mode;
878 ip->i_di.di_ctime = get_seconds();
879 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
880 gfs2_dinode_out(&ip->i_di, dibh->b_data);
883 gfs2_trans_end(ip->i_sbd);
888 static int ea_set_simple_alloc(struct gfs2_inode *ip,
889 struct gfs2_ea_request *er, void *private)
891 struct ea_set *es = private;
892 struct gfs2_ea_header *ea = es->es_ea;
895 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
898 ea = ea_split_ea(ea);
900 error = ea_write(ip, ea, er);
905 ea_set_remove_stuffed(ip, es->es_el);
910 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
911 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
914 struct ea_set *es = private;
919 stuffed = ea_calc_size(ip->i_sbd, es->es_er, &size);
921 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
922 if (GFS2_EA_REC_LEN(ea) < size)
924 if (!GFS2_EA_IS_STUFFED(ea)) {
925 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
930 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
936 error = ea_set_simple_noalloc(ip, bh, ea, es);
944 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
945 ip->i_sbd->sd_jbsize);
947 error = ea_alloc_skeleton(ip, es->es_er, blks,
948 ea_set_simple_alloc, es);
956 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
959 struct gfs2_sbd *sdp = ip->i_sbd;
960 struct buffer_head *indbh, *newbh;
963 int mh_size = sizeof(struct gfs2_meta_header);
965 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
968 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
969 DIO_START | DIO_WAIT, &indbh);
973 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
978 eablk = (uint64_t *)(indbh->b_data + mh_size);
979 end = eablk + sdp->sd_inptrs;
981 for (; eablk < end; eablk++)
990 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
994 blk = gfs2_alloc_meta(ip);
996 indbh = gfs2_meta_new(ip->i_gl, blk);
997 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
998 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
999 gfs2_buffer_clear_tail(indbh, mh_size);
1001 eablk = (uint64_t *)(indbh->b_data + mh_size);
1002 *eablk = cpu_to_be64(ip->i_di.di_eattr);
1003 ip->i_di.di_eattr = blk;
1004 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
1005 ip->i_di.di_blocks++;
1010 error = ea_alloc_blk(ip, &newbh);
1014 *eablk = cpu_to_be64((uint64_t)newbh->b_blocknr);
1015 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1021 ea_set_remove_stuffed(ip, (struct gfs2_ea_location *)private);
1029 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1030 struct gfs2_ea_location *el)
1033 unsigned int blks = 2;
1036 memset(&es, 0, sizeof(struct ea_set));
1040 error = ea_foreach(ip, ea_set_simple, &es);
1046 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1048 if (GFS2_EAREQ_SIZE_STUFFED(er) > ip->i_sbd->sd_jbsize)
1049 blks += DIV_ROUND_UP(er->er_data_len, ip->i_sbd->sd_jbsize);
1051 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1054 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1055 struct gfs2_ea_location *el)
1057 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1058 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1059 gfs2_assert_withdraw(ip->i_sbd,
1060 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1063 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1066 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1068 struct gfs2_ea_location el;
1071 if (!ip->i_di.di_eattr) {
1072 if (er->er_flags & XATTR_REPLACE)
1074 return ea_init(ip, er);
1077 error = gfs2_ea_find(ip, er, &el);
1082 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1088 if (!(er->er_flags & XATTR_CREATE)) {
1089 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1090 error = ea_set_i(ip, er, &el);
1091 if (!error && unstuffed)
1092 ea_set_remove_unstuffed(ip, &el);
1098 if (!(er->er_flags & XATTR_REPLACE))
1099 error = ea_set_i(ip, er, NULL);
1105 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1107 struct gfs2_holder i_gh;
1110 if (!er->er_name_len ||
1111 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1113 if (!er->er_data || !er->er_data_len) {
1115 er->er_data_len = 0;
1117 error = ea_check_size(ip->i_sbd, er);
1121 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1125 if (IS_IMMUTABLE(ip->i_vnode))
1128 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1130 gfs2_glock_dq_uninit(&i_gh);
1135 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1137 struct gfs2_ea_header *ea = el->el_ea;
1138 struct gfs2_ea_header *prev = el->el_prev;
1139 struct buffer_head *dibh;
1142 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
1146 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1151 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1152 prev->ea_rec_len = cpu_to_be32(len);
1154 if (GFS2_EA_IS_LAST(ea))
1155 prev->ea_flags |= GFS2_EAFLAG_LAST;
1157 ea->ea_type = GFS2_EATYPE_UNUSED;
1159 error = gfs2_meta_inode_buffer(ip, &dibh);
1161 ip->i_di.di_ctime = get_seconds();
1162 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1163 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1167 gfs2_trans_end(ip->i_sbd);
1172 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1174 struct gfs2_ea_location el;
1177 if (!ip->i_di.di_eattr)
1180 error = gfs2_ea_find(ip, er, &el);
1186 if (GFS2_EA_IS_STUFFED(el.el_ea))
1187 error = ea_remove_stuffed(ip, &el);
1189 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1198 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1199 * @ip: pointer to the inode of the target file
1200 * @er: request information
1205 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1207 struct gfs2_holder i_gh;
1210 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1213 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1217 if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1220 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1222 gfs2_glock_dq_uninit(&i_gh);
1227 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1228 struct gfs2_ea_header *ea, char *data)
1230 struct gfs2_sbd *sdp = ip->i_sbd;
1231 struct buffer_head **bh;
1232 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1233 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1234 uint64_t *dataptrs = GFS2_EA2DATAPTRS(ea);
1238 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1242 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1246 for (x = 0; x < nptrs; x++) {
1247 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
1257 for (x = 0; x < nptrs; x++) {
1258 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
1260 for (; x < nptrs; x++)
1264 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1265 for (; x < nptrs; x++)
1271 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1273 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header),
1275 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1277 amount -= sdp->sd_jbsize;
1278 data += sdp->sd_jbsize;
1289 gfs2_trans_end(sdp);
1295 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1296 struct iattr *attr, char *data)
1298 struct buffer_head *dibh;
1301 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1302 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE + RES_EATTR, 0);
1306 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1307 memcpy(GFS2_EA2DATA(el->el_ea),
1309 GFS2_EA_DATA_LEN(el->el_ea));
1311 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1316 error = gfs2_meta_inode_buffer(ip, &dibh);
1318 error = inode_setattr(ip->i_vnode, attr);
1319 gfs2_assert_warn(ip->i_sbd, !error);
1320 gfs2_inode_attr_out(ip);
1321 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1322 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1326 gfs2_trans_end(ip->i_sbd);
1331 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1333 struct gfs2_sbd *sdp = ip->i_sbd;
1334 struct gfs2_rgrp_list rlist;
1335 struct buffer_head *indbh, *dibh;
1336 uint64_t *eablk, *end;
1337 unsigned int rg_blocks = 0;
1338 uint64_t bstart = 0;
1339 unsigned int blen = 0;
1340 unsigned int blks = 0;
1344 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1346 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
1347 DIO_START | DIO_WAIT, &indbh);
1351 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1356 eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1357 end = eablk + sdp->sd_inptrs;
1359 for (; eablk < end; eablk++) {
1364 bn = be64_to_cpu(*eablk);
1366 if (bstart + blen == bn)
1370 gfs2_rlist_add(sdp, &rlist, bstart);
1377 gfs2_rlist_add(sdp, &rlist, bstart);
1381 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1383 for (x = 0; x < rlist.rl_rgrps; x++) {
1384 struct gfs2_rgrpd *rgd;
1385 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1386 rg_blocks += rgd->rd_ri.ri_length;
1389 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1391 goto out_rlist_free;
1393 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
1394 RES_INDIRECT + RES_STATFS +
1399 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1401 eablk = (uint64_t *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1405 for (; eablk < end; eablk++) {
1410 bn = be64_to_cpu(*eablk);
1412 if (bstart + blen == bn)
1416 gfs2_free_meta(ip, bstart, blen);
1422 if (!ip->i_di.di_blocks)
1423 gfs2_consist_inode(ip);
1424 ip->i_di.di_blocks--;
1427 gfs2_free_meta(ip, bstart, blen);
1429 ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1431 error = gfs2_meta_inode_buffer(ip, &dibh);
1433 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1434 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1438 gfs2_trans_end(sdp);
1441 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1444 gfs2_rlist_free(&rlist);
1452 static int ea_dealloc_block(struct gfs2_inode *ip)
1454 struct gfs2_sbd *sdp = ip->i_sbd;
1455 struct gfs2_alloc *al = &ip->i_alloc;
1456 struct gfs2_rgrpd *rgd;
1457 struct buffer_head *dibh;
1460 rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1462 gfs2_consist_inode(ip);
1466 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1471 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE +
1472 RES_STATFS + RES_QUOTA, 1);
1476 gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1478 ip->i_di.di_eattr = 0;
1479 if (!ip->i_di.di_blocks)
1480 gfs2_consist_inode(ip);
1481 ip->i_di.di_blocks--;
1483 error = gfs2_meta_inode_buffer(ip, &dibh);
1485 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1486 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1490 gfs2_trans_end(sdp);
1493 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1499 * gfs2_ea_dealloc - deallocate the extended attribute fork
1505 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1507 struct gfs2_alloc *al;
1510 al = gfs2_alloc_get(ip);
1512 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1516 error = gfs2_rindex_hold(ip->i_sbd, &al->al_ri_gh);
1520 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1524 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1525 error = ea_dealloc_indirect(ip);
1530 error = ea_dealloc_block(ip);
1533 gfs2_glock_dq_uninit(&al->al_ri_gh);
1536 gfs2_quota_unhold(ip);