2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/xattr.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <asm/uaccess.h>
32 * ea_calc_size - returns the acutal number of bytes the request will take up
33 * (not counting any unstuffed data blocks)
38 * Returns: 1 if the EA should be stuffed
41 static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
44 *size = GFS2_EAREQ_SIZE_STUFFED(er);
45 if (*size <= sdp->sd_jbsize)
48 *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);
53 static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
57 if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
60 ea_calc_size(sdp, er, &size);
62 /* This can only happen with 512 byte blocks */
63 if (size > sdp->sd_jbsize)
69 typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
70 struct gfs2_ea_header *ea,
71 struct gfs2_ea_header *prev, void *private);
73 static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
74 ea_call_t ea_call, void *data)
76 struct gfs2_ea_header *ea, *prev = NULL;
79 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
82 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
83 if (!GFS2_EA_REC_LEN(ea))
85 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
86 bh->b_data + bh->b_size))
88 if (!GFS2_EATYPE_VALID(ea->ea_type))
91 error = ea_call(ip, bh, ea, prev, data);
95 if (GFS2_EA_IS_LAST(ea)) {
96 if ((char *)GFS2_EA2NEXT(ea) !=
97 bh->b_data + bh->b_size)
106 gfs2_consist_inode(ip);
110 static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
112 struct buffer_head *bh, *eabh;
116 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &bh);
120 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) {
121 error = ea_foreach_i(ip, bh, ea_call, data);
125 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
130 eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
131 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
133 for (; eablk < end; eablk++) {
138 bn = be64_to_cpu(*eablk);
140 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh);
143 error = ea_foreach_i(ip, eabh, ea_call, data);
154 struct gfs2_ea_request *ef_er;
155 struct gfs2_ea_location *ef_el;
158 static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
159 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
162 struct ea_find *ef = private;
163 struct gfs2_ea_request *er = ef->ef_er;
165 if (ea->ea_type == GFS2_EATYPE_UNUSED)
168 if (ea->ea_type == er->er_type) {
169 if (ea->ea_name_len == er->er_name_len &&
170 !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
171 struct gfs2_ea_location *el = ef->ef_el;
183 int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
184 struct gfs2_ea_location *el)
192 memset(el, 0, sizeof(struct gfs2_ea_location));
194 error = ea_foreach(ip, ea_find_i, &ef);
202 * ea_dealloc_unstuffed -
209 * Take advantage of the fact that all unstuffed blocks are
210 * allocated from the same RG. But watch, this may not always
216 static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
217 struct gfs2_ea_header *ea,
218 struct gfs2_ea_header *prev, void *private)
220 int *leave = private;
221 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
222 struct gfs2_rgrpd *rgd;
223 struct gfs2_holder rg_gh;
224 struct buffer_head *dibh;
228 unsigned int blen = 0;
229 unsigned int blks = 0;
233 if (GFS2_EA_IS_STUFFED(ea))
236 dataptrs = GFS2_EA2DATAPTRS(ea);
237 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
240 bn = be64_to_cpu(*dataptrs);
246 rgd = gfs2_blk2rgrpd(sdp, bn);
248 gfs2_consist_inode(ip);
252 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
256 error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
257 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
261 gfs2_trans_add_bh(ip->i_gl, bh, 1);
263 dataptrs = GFS2_EA2DATAPTRS(ea);
264 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
267 bn = be64_to_cpu(*dataptrs);
269 if (bstart + blen == bn)
273 gfs2_free_meta(ip, bstart, blen);
279 gfs2_add_inode_blocks(&ip->i_inode, -1);
282 gfs2_free_meta(ip, bstart, blen);
284 if (prev && !leave) {
287 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
288 prev->ea_rec_len = cpu_to_be32(len);
290 if (GFS2_EA_IS_LAST(ea))
291 prev->ea_flags |= GFS2_EAFLAG_LAST;
293 ea->ea_type = GFS2_EATYPE_UNUSED;
297 error = gfs2_meta_inode_buffer(ip, &dibh);
299 ip->i_inode.i_ctime = CURRENT_TIME;
300 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
301 gfs2_dinode_out(ip, dibh->b_data);
308 gfs2_glock_dq_uninit(&rg_gh);
312 static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
313 struct gfs2_ea_header *ea,
314 struct gfs2_ea_header *prev, int leave)
316 struct gfs2_alloc *al;
319 al = gfs2_alloc_get(ip);
323 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
327 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
331 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
333 gfs2_glock_dq_uninit(&al->al_ri_gh);
336 gfs2_quota_unhold(ip);
343 struct gfs2_ea_request *ei_er;
344 unsigned int ei_size;
347 static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
348 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
351 struct ea_list *ei = private;
352 struct gfs2_ea_request *er = ei->ei_er;
353 unsigned int ea_size = gfs2_ea_strlen(ea);
355 if (ea->ea_type == GFS2_EATYPE_UNUSED)
358 if (er->er_data_len) {
363 if (ei->ei_size + ea_size > er->er_data_len)
366 switch (ea->ea_type) {
367 case GFS2_EATYPE_USR:
371 case GFS2_EATYPE_SYS:
375 case GFS2_EATYPE_SECURITY:
376 prefix = "security.";
383 memcpy(er->er_data + ei->ei_size, prefix, l);
384 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
386 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
389 ei->ei_size += ea_size;
399 * Returns: actual size of data on success, -errno on error
402 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
404 struct gfs2_holder i_gh;
407 if (!er->er_data || !er->er_data_len) {
412 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
417 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
419 error = ea_foreach(ip, ea_list_i, &ei);
424 gfs2_glock_dq_uninit(&i_gh);
430 * ea_get_unstuffed - actually copies the unstuffed data into the
432 * @ip: The GFS2 inode
433 * @ea: The extended attribute header structure
434 * @data: The data to be copied
439 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
442 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
443 struct buffer_head **bh;
444 unsigned int amount = GFS2_EA_DATA_LEN(ea);
445 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
446 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
450 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
454 for (x = 0; x < nptrs; x++) {
455 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
465 for (x = 0; x < nptrs; x++) {
466 error = gfs2_meta_wait(sdp, bh[x]);
468 for (; x < nptrs; x++)
472 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
473 for (; x < nptrs; x++)
479 memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
480 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
482 amount -= sdp->sd_jbsize;
483 data += sdp->sd_jbsize;
493 int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
496 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
497 memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea));
500 return ea_get_unstuffed(ip, el->el_ea, data);
505 * @ip: The GFS2 inode
506 * @er: The request structure
508 * Returns: actual size of data on success, -errno on error
511 int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
513 struct gfs2_ea_location el;
519 error = gfs2_ea_find(ip, er, &el);
525 if (er->er_data_len) {
526 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
529 error = gfs2_ea_get_copy(ip, &el, er->er_data);
532 error = GFS2_EA_DATA_LEN(el.el_ea);
541 * @ip: The GFS2 inode
542 * @er: The request structure
544 * Returns: actual size of data on success, -errno on error
547 int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
549 struct gfs2_holder i_gh;
552 if (!er->er_name_len ||
553 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
555 if (!er->er_data || !er->er_data_len) {
560 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
564 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
566 gfs2_glock_dq_uninit(&i_gh);
572 * ea_alloc_blk - allocates a new block for extended attributes.
573 * @ip: A pointer to the inode that's getting extended attributes
574 * @bhp: Pointer to pointer to a struct buffer_head
579 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
581 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
582 struct gfs2_ea_header *ea;
587 error = gfs2_alloc_block(ip, &block, &n);
590 gfs2_trans_add_unrevoke(sdp, block, 1);
591 *bhp = gfs2_meta_new(ip->i_gl, block);
592 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
593 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
594 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
596 ea = GFS2_EA_BH2FIRST(*bhp);
597 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
598 ea->ea_type = GFS2_EATYPE_UNUSED;
599 ea->ea_flags = GFS2_EAFLAG_LAST;
602 gfs2_add_inode_blocks(&ip->i_inode, 1);
608 * ea_write - writes the request info to an ea, creating new blocks if
610 * @ip: inode that is being modified
611 * @ea: the location of the new ea in a block
612 * @er: the write request
614 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
619 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
620 struct gfs2_ea_request *er)
622 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
625 ea->ea_data_len = cpu_to_be32(er->er_data_len);
626 ea->ea_name_len = er->er_name_len;
627 ea->ea_type = er->er_type;
630 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
632 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
634 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
636 __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
637 const char *data = er->er_data;
638 unsigned int data_len = er->er_data_len;
642 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
643 for (x = 0; x < ea->ea_num_ptrs; x++) {
644 struct buffer_head *bh;
646 int mh_size = sizeof(struct gfs2_meta_header);
649 error = gfs2_alloc_block(ip, &block, &n);
652 gfs2_trans_add_unrevoke(sdp, block, 1);
653 bh = gfs2_meta_new(ip->i_gl, block);
654 gfs2_trans_add_bh(ip->i_gl, bh, 1);
655 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
657 gfs2_add_inode_blocks(&ip->i_inode, 1);
659 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
661 memcpy(bh->b_data + mh_size, data, copy);
662 if (copy < sdp->sd_jbsize)
663 memset(bh->b_data + mh_size + copy, 0,
664 sdp->sd_jbsize - copy);
666 *dataptr++ = cpu_to_be64(bh->b_blocknr);
673 gfs2_assert_withdraw(sdp, !data_len);
679 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
680 struct gfs2_ea_request *er, void *private);
682 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
684 ea_skeleton_call_t skeleton_call, void *private)
686 struct gfs2_alloc *al;
687 struct buffer_head *dibh;
690 al = gfs2_alloc_get(ip);
694 error = gfs2_quota_lock_check(ip);
698 al->al_requested = blks;
700 error = gfs2_inplace_reserve(ip);
704 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
705 blks + al->al_rgd->rd_length +
706 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
710 error = skeleton_call(ip, er, private);
714 error = gfs2_meta_inode_buffer(ip, &dibh);
716 if (er->er_flags & GFS2_ERF_MODE) {
717 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
718 (ip->i_inode.i_mode & S_IFMT) ==
719 (er->er_mode & S_IFMT));
720 ip->i_inode.i_mode = er->er_mode;
722 ip->i_inode.i_ctime = CURRENT_TIME;
723 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
724 gfs2_dinode_out(ip, dibh->b_data);
729 gfs2_trans_end(GFS2_SB(&ip->i_inode));
731 gfs2_inplace_release(ip);
733 gfs2_quota_unlock(ip);
739 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
742 struct buffer_head *bh;
745 error = ea_alloc_blk(ip, &bh);
749 ip->i_eattr = bh->b_blocknr;
750 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
758 * ea_init - initializes a new eattr block
765 static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
767 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
768 unsigned int blks = 1;
770 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
771 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
773 return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
776 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
778 u32 ea_size = GFS2_EA_SIZE(ea);
779 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
781 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
782 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
784 ea->ea_rec_len = cpu_to_be32(ea_size);
785 ea->ea_flags ^= last;
787 new->ea_rec_len = cpu_to_be32(new_size);
788 new->ea_flags = last;
793 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
794 struct gfs2_ea_location *el)
796 struct gfs2_ea_header *ea = el->el_ea;
797 struct gfs2_ea_header *prev = el->el_prev;
800 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
802 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
803 ea->ea_type = GFS2_EATYPE_UNUSED;
805 } else if (GFS2_EA2NEXT(prev) != ea) {
806 prev = GFS2_EA2NEXT(prev);
807 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
810 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
811 prev->ea_rec_len = cpu_to_be32(len);
813 if (GFS2_EA_IS_LAST(ea))
814 prev->ea_flags |= GFS2_EAFLAG_LAST;
820 struct gfs2_ea_request *es_er;
821 struct gfs2_ea_location *es_el;
823 struct buffer_head *es_bh;
824 struct gfs2_ea_header *es_ea;
827 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
828 struct gfs2_ea_header *ea, struct ea_set *es)
830 struct gfs2_ea_request *er = es->es_er;
831 struct buffer_head *dibh;
834 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
838 gfs2_trans_add_bh(ip->i_gl, bh, 1);
841 ea = ea_split_ea(ea);
843 ea_write(ip, ea, er);
846 ea_set_remove_stuffed(ip, es->es_el);
848 error = gfs2_meta_inode_buffer(ip, &dibh);
852 if (er->er_flags & GFS2_ERF_MODE) {
853 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
854 (ip->i_inode.i_mode & S_IFMT) == (er->er_mode & S_IFMT));
855 ip->i_inode.i_mode = er->er_mode;
857 ip->i_inode.i_ctime = CURRENT_TIME;
858 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
859 gfs2_dinode_out(ip, dibh->b_data);
862 gfs2_trans_end(GFS2_SB(&ip->i_inode));
866 static int ea_set_simple_alloc(struct gfs2_inode *ip,
867 struct gfs2_ea_request *er, void *private)
869 struct ea_set *es = private;
870 struct gfs2_ea_header *ea = es->es_ea;
873 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
876 ea = ea_split_ea(ea);
878 error = ea_write(ip, ea, er);
883 ea_set_remove_stuffed(ip, es->es_el);
888 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
889 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
892 struct ea_set *es = private;
897 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
899 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
900 if (GFS2_EA_REC_LEN(ea) < size)
902 if (!GFS2_EA_IS_STUFFED(ea)) {
903 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
908 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
914 error = ea_set_simple_noalloc(ip, bh, ea, es);
922 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
923 GFS2_SB(&ip->i_inode)->sd_jbsize);
925 error = ea_alloc_skeleton(ip, es->es_er, blks,
926 ea_set_simple_alloc, es);
934 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
937 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
938 struct buffer_head *indbh, *newbh;
941 int mh_size = sizeof(struct gfs2_meta_header);
943 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
946 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT,
951 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
956 eablk = (__be64 *)(indbh->b_data + mh_size);
957 end = eablk + sdp->sd_inptrs;
959 for (; eablk < end; eablk++)
968 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
972 error = gfs2_alloc_block(ip, &blk, &n);
975 gfs2_trans_add_unrevoke(sdp, blk, 1);
976 indbh = gfs2_meta_new(ip->i_gl, blk);
977 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
978 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
979 gfs2_buffer_clear_tail(indbh, mh_size);
981 eablk = (__be64 *)(indbh->b_data + mh_size);
982 *eablk = cpu_to_be64(ip->i_eattr);
984 ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
985 gfs2_add_inode_blocks(&ip->i_inode, 1);
990 error = ea_alloc_blk(ip, &newbh);
994 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
995 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1001 ea_set_remove_stuffed(ip, private);
1008 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1009 struct gfs2_ea_location *el)
1012 unsigned int blks = 2;
1015 memset(&es, 0, sizeof(struct ea_set));
1019 error = ea_foreach(ip, ea_set_simple, &es);
1025 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
1027 if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1028 blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
1030 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1033 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1034 struct gfs2_ea_location *el)
1036 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1037 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1038 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
1039 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1042 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1045 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1047 struct gfs2_ea_location el;
1051 if (er->er_flags & XATTR_REPLACE)
1053 return ea_init(ip, er);
1056 error = gfs2_ea_find(ip, er, &el);
1061 if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
1067 if (!(er->er_flags & XATTR_CREATE)) {
1068 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1069 error = ea_set_i(ip, er, &el);
1070 if (!error && unstuffed)
1071 ea_set_remove_unstuffed(ip, &el);
1077 if (!(er->er_flags & XATTR_REPLACE))
1078 error = ea_set_i(ip, er, NULL);
1084 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1086 struct gfs2_holder i_gh;
1089 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1091 if (!er->er_data || !er->er_data_len) {
1093 er->er_data_len = 0;
1095 error = ea_check_size(GFS2_SB(&ip->i_inode), er);
1099 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1103 if (IS_IMMUTABLE(&ip->i_inode))
1106 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1108 gfs2_glock_dq_uninit(&i_gh);
1113 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1115 struct gfs2_ea_header *ea = el->el_ea;
1116 struct gfs2_ea_header *prev = el->el_prev;
1117 struct buffer_head *dibh;
1120 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1124 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1129 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1130 prev->ea_rec_len = cpu_to_be32(len);
1132 if (GFS2_EA_IS_LAST(ea))
1133 prev->ea_flags |= GFS2_EAFLAG_LAST;
1135 ea->ea_type = GFS2_EATYPE_UNUSED;
1137 error = gfs2_meta_inode_buffer(ip, &dibh);
1139 ip->i_inode.i_ctime = CURRENT_TIME;
1140 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1141 gfs2_dinode_out(ip, dibh->b_data);
1145 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1150 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1152 struct gfs2_ea_location el;
1158 error = gfs2_ea_find(ip, er, &el);
1164 if (GFS2_EA_IS_STUFFED(el.el_ea))
1165 error = ea_remove_stuffed(ip, &el);
1167 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1176 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1177 * @ip: pointer to the inode of the target file
1178 * @er: request information
1183 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1185 struct gfs2_holder i_gh;
1188 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1191 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1195 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1198 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1200 gfs2_glock_dq_uninit(&i_gh);
1205 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1206 struct gfs2_ea_header *ea, char *data)
1208 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1209 struct buffer_head **bh;
1210 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1211 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1212 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
1216 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
1220 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1224 for (x = 0; x < nptrs; x++) {
1225 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
1235 for (x = 0; x < nptrs; x++) {
1236 error = gfs2_meta_wait(sdp, bh[x]);
1238 for (; x < nptrs; x++)
1242 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1243 for (; x < nptrs; x++)
1249 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1251 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
1252 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1254 amount -= sdp->sd_jbsize;
1255 data += sdp->sd_jbsize;
1265 gfs2_trans_end(sdp);
1270 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1271 struct iattr *attr, char *data)
1273 struct buffer_head *dibh;
1276 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1277 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1281 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1282 memcpy(GFS2_EA2DATA(el->el_ea), data,
1283 GFS2_EA_DATA_LEN(el->el_ea));
1285 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1290 error = gfs2_meta_inode_buffer(ip, &dibh);
1292 error = inode_setattr(&ip->i_inode, attr);
1293 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1294 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1295 gfs2_dinode_out(ip, dibh->b_data);
1299 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1304 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1306 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1307 struct gfs2_rgrp_list rlist;
1308 struct buffer_head *indbh, *dibh;
1309 __be64 *eablk, *end;
1310 unsigned int rg_blocks = 0;
1312 unsigned int blen = 0;
1313 unsigned int blks = 0;
1317 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1319 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, &indbh);
1323 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1328 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1329 end = eablk + sdp->sd_inptrs;
1331 for (; eablk < end; eablk++) {
1336 bn = be64_to_cpu(*eablk);
1338 if (bstart + blen == bn)
1342 gfs2_rlist_add(sdp, &rlist, bstart);
1349 gfs2_rlist_add(sdp, &rlist, bstart);
1353 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
1355 for (x = 0; x < rlist.rl_rgrps; x++) {
1356 struct gfs2_rgrpd *rgd;
1357 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1358 rg_blocks += rgd->rd_length;
1361 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1363 goto out_rlist_free;
1365 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1366 RES_STATFS + RES_QUOTA, blks);
1370 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1372 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1376 for (; eablk < end; eablk++) {
1381 bn = be64_to_cpu(*eablk);
1383 if (bstart + blen == bn)
1387 gfs2_free_meta(ip, bstart, blen);
1393 gfs2_add_inode_blocks(&ip->i_inode, -1);
1396 gfs2_free_meta(ip, bstart, blen);
1398 ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
1400 error = gfs2_meta_inode_buffer(ip, &dibh);
1402 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1403 gfs2_dinode_out(ip, dibh->b_data);
1407 gfs2_trans_end(sdp);
1410 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1412 gfs2_rlist_free(&rlist);
1418 static int ea_dealloc_block(struct gfs2_inode *ip)
1420 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1421 struct gfs2_alloc *al = ip->i_alloc;
1422 struct gfs2_rgrpd *rgd;
1423 struct buffer_head *dibh;
1426 rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr);
1428 gfs2_consist_inode(ip);
1432 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1437 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1442 gfs2_free_meta(ip, ip->i_eattr, 1);
1445 gfs2_add_inode_blocks(&ip->i_inode, -1);
1447 error = gfs2_meta_inode_buffer(ip, &dibh);
1449 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1450 gfs2_dinode_out(ip, dibh->b_data);
1454 gfs2_trans_end(sdp);
1457 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1462 * gfs2_ea_dealloc - deallocate the extended attribute fork
1468 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1470 struct gfs2_alloc *al;
1473 al = gfs2_alloc_get(ip);
1477 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1481 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
1485 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1489 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
1490 error = ea_dealloc_indirect(ip);
1495 error = ea_dealloc_block(ip);
1498 gfs2_glock_dq_uninit(&al->al_ri_gh);
1500 gfs2_quota_unhold(ip);