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/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 <linux/lm_interface.h>
18 #include <asm/uaccess.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, struct buffer_head *bh,
72 struct gfs2_ea_header *ea,
73 struct gfs2_ea_header *prev, void *private);
75 static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
76 ea_call_t ea_call, void *data)
78 struct gfs2_ea_header *ea, *prev = NULL;
81 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
84 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
85 if (!GFS2_EA_REC_LEN(ea))
87 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
88 bh->b_data + bh->b_size))
90 if (!GFS2_EATYPE_VALID(ea->ea_type))
93 error = ea_call(ip, bh, ea, prev, data);
97 if (GFS2_EA_IS_LAST(ea)) {
98 if ((char *)GFS2_EA2NEXT(ea) !=
99 bh->b_data + bh->b_size)
108 gfs2_consist_inode(ip);
112 static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
114 struct buffer_head *bh, *eabh;
118 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &bh);
122 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
123 error = ea_foreach_i(ip, bh, ea_call, data);
127 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
132 eablk = (u64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
133 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
135 for (; eablk < end; eablk++) {
140 bn = be64_to_cpu(*eablk);
142 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh);
145 error = ea_foreach_i(ip, eabh, ea_call, data);
156 struct gfs2_ea_request *ef_er;
157 struct gfs2_ea_location *ef_el;
160 static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
161 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
164 struct ea_find *ef = private;
165 struct gfs2_ea_request *er = ef->ef_er;
167 if (ea->ea_type == GFS2_EATYPE_UNUSED)
170 if (ea->ea_type == er->er_type) {
171 if (ea->ea_name_len == er->er_name_len &&
172 !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
173 struct gfs2_ea_location *el = ef->ef_el;
185 int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
186 struct gfs2_ea_location *el)
194 memset(el, 0, sizeof(struct gfs2_ea_location));
196 error = ea_foreach(ip, ea_find_i, &ef);
204 * ea_dealloc_unstuffed -
211 * Take advantage of the fact that all unstuffed blocks are
212 * allocated from the same RG. But watch, this may not always
218 static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
219 struct gfs2_ea_header *ea,
220 struct gfs2_ea_header *prev, void *private)
222 int *leave = private;
223 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
224 struct gfs2_rgrpd *rgd;
225 struct gfs2_holder rg_gh;
226 struct buffer_head *dibh;
227 u64 *dataptrs, bn = 0;
229 unsigned int blen = 0;
230 unsigned int blks = 0;
234 if (GFS2_EA_IS_STUFFED(ea))
237 dataptrs = GFS2_EA2DATAPTRS(ea);
238 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
241 bn = be64_to_cpu(*dataptrs);
247 rgd = gfs2_blk2rgrpd(sdp, bn);
249 gfs2_consist_inode(ip);
253 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
257 error = gfs2_trans_begin(sdp, rgd->rd_ri.ri_length + RES_DINODE +
258 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
262 gfs2_trans_add_bh(ip->i_gl, bh, 1);
264 dataptrs = GFS2_EA2DATAPTRS(ea);
265 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
268 bn = be64_to_cpu(*dataptrs);
270 if (bstart + blen == bn)
274 gfs2_free_meta(ip, bstart, blen);
280 if (!ip->i_di.di_blocks)
281 gfs2_consist_inode(ip);
282 ip->i_di.di_blocks--;
285 gfs2_free_meta(ip, bstart, blen);
287 if (prev && !leave) {
290 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
291 prev->ea_rec_len = cpu_to_be32(len);
293 if (GFS2_EA_IS_LAST(ea))
294 prev->ea_flags |= GFS2_EAFLAG_LAST;
296 ea->ea_type = GFS2_EATYPE_UNUSED;
300 error = gfs2_meta_inode_buffer(ip, &dibh);
302 ip->i_di.di_ctime = get_seconds();
303 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
304 gfs2_dinode_out(&ip->i_di, dibh->b_data);
311 gfs2_glock_dq_uninit(&rg_gh);
315 static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
316 struct gfs2_ea_header *ea,
317 struct gfs2_ea_header *prev, int leave)
319 struct gfs2_alloc *al;
322 al = gfs2_alloc_get(ip);
324 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
328 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
332 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
334 gfs2_glock_dq_uninit(&al->al_ri_gh);
337 gfs2_quota_unhold(ip);
344 struct gfs2_ea_request *ei_er;
345 unsigned int ei_size;
348 static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
349 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
352 struct ea_list *ei = private;
353 struct gfs2_ea_request *er = ei->ei_er;
354 unsigned int ea_size = gfs2_ea_strlen(ea);
356 if (ea->ea_type == GFS2_EATYPE_UNUSED)
359 if (er->er_data_len) {
364 if (ei->ei_size + ea_size > er->er_data_len)
367 switch (ea->ea_type) {
368 case GFS2_EATYPE_USR:
372 case GFS2_EATYPE_SYS:
376 case GFS2_EATYPE_SECURITY:
377 prefix = "security.";
384 memcpy(er->er_data + ei->ei_size, prefix, l);
385 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
387 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
390 ei->ei_size += ea_size;
400 * Returns: actual size of data on success, -errno on error
403 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
405 struct gfs2_holder i_gh;
408 if (!er->er_data || !er->er_data_len) {
413 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
417 if (ip->i_di.di_eattr) {
418 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
420 error = ea_foreach(ip, ea_list_i, &ei);
425 gfs2_glock_dq_uninit(&i_gh);
431 * ea_get_unstuffed - actually copies the unstuffed data into the
433 * @ip: The GFS2 inode
434 * @ea: The extended attribute header structure
435 * @data: The data to be copied
440 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
443 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
444 struct buffer_head **bh;
445 unsigned int amount = GFS2_EA_DATA_LEN(ea);
446 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
447 u64 *dataptrs = GFS2_EA2DATAPTRS(ea);
451 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
455 for (x = 0; x < nptrs; x++) {
456 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
466 for (x = 0; x < nptrs; x++) {
467 error = gfs2_meta_wait(sdp, bh[x]);
469 for (; x < nptrs; x++)
473 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
474 for (; x < nptrs; x++)
480 memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
481 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
483 amount -= sdp->sd_jbsize;
484 data += sdp->sd_jbsize;
494 int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
497 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
498 memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea));
501 return ea_get_unstuffed(ip, el->el_ea, data);
506 * @ip: The GFS2 inode
507 * @er: The request structure
509 * Returns: actual size of data on success, -errno on error
512 int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
514 struct gfs2_ea_location el;
517 if (!ip->i_di.di_eattr)
520 error = gfs2_ea_find(ip, er, &el);
526 if (er->er_data_len) {
527 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
530 error = gfs2_ea_get_copy(ip, &el, er->er_data);
533 error = GFS2_EA_DATA_LEN(el.el_ea);
542 * @ip: The GFS2 inode
543 * @er: The request structure
545 * Returns: actual size of data on success, -errno on error
548 int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
550 struct gfs2_holder i_gh;
553 if (!er->er_name_len ||
554 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
556 if (!er->er_data || !er->er_data_len) {
561 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
565 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
567 gfs2_glock_dq_uninit(&i_gh);
573 * ea_alloc_blk - allocates a new block for extended attributes.
574 * @ip: A pointer to the inode that's getting extended attributes
575 * @bhp: Pointer to pointer to a struct buffer_head
580 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
582 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
583 struct gfs2_ea_header *ea;
586 block = gfs2_alloc_meta(ip);
588 *bhp = gfs2_meta_new(ip->i_gl, block);
589 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
590 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
591 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
593 ea = GFS2_EA_BH2FIRST(*bhp);
594 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
595 ea->ea_type = GFS2_EATYPE_UNUSED;
596 ea->ea_flags = GFS2_EAFLAG_LAST;
599 ip->i_di.di_blocks++;
605 * ea_write - writes the request info to an ea, creating new blocks if
607 * @ip: inode that is being modified
608 * @ea: the location of the new ea in a block
609 * @er: the write request
611 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
616 static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
617 struct gfs2_ea_request *er)
619 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
621 ea->ea_data_len = cpu_to_be32(er->er_data_len);
622 ea->ea_name_len = er->er_name_len;
623 ea->ea_type = er->er_type;
626 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
628 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
630 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
632 u64 *dataptr = GFS2_EA2DATAPTRS(ea);
633 const char *data = er->er_data;
634 unsigned int data_len = er->er_data_len;
638 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
639 for (x = 0; x < ea->ea_num_ptrs; x++) {
640 struct buffer_head *bh;
642 int mh_size = sizeof(struct gfs2_meta_header);
644 block = gfs2_alloc_meta(ip);
646 bh = gfs2_meta_new(ip->i_gl, block);
647 gfs2_trans_add_bh(ip->i_gl, bh, 1);
648 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
650 ip->i_di.di_blocks++;
652 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
654 memcpy(bh->b_data + mh_size, data, copy);
655 if (copy < sdp->sd_jbsize)
656 memset(bh->b_data + mh_size + copy, 0,
657 sdp->sd_jbsize - copy);
659 *dataptr++ = cpu_to_be64(bh->b_blocknr);
666 gfs2_assert_withdraw(sdp, !data_len);
672 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
673 struct gfs2_ea_request *er, void *private);
675 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
677 ea_skeleton_call_t skeleton_call, void *private)
679 struct gfs2_alloc *al;
680 struct buffer_head *dibh;
683 al = gfs2_alloc_get(ip);
685 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
689 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
693 al->al_requested = blks;
695 error = gfs2_inplace_reserve(ip);
699 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
700 blks + al->al_rgd->rd_ri.ri_length +
701 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
705 error = skeleton_call(ip, er, private);
709 error = gfs2_meta_inode_buffer(ip, &dibh);
711 if (er->er_flags & GFS2_ERF_MODE) {
712 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
713 (ip->i_di.di_mode & S_IFMT) ==
714 (er->er_mode & S_IFMT));
715 ip->i_di.di_mode = er->er_mode;
717 ip->i_di.di_ctime = get_seconds();
718 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
719 gfs2_dinode_out(&ip->i_di, dibh->b_data);
724 gfs2_trans_end(GFS2_SB(&ip->i_inode));
726 gfs2_inplace_release(ip);
728 gfs2_quota_unlock(ip);
734 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
737 struct buffer_head *bh;
740 error = ea_alloc_blk(ip, &bh);
744 ip->i_di.di_eattr = bh->b_blocknr;
745 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
753 * ea_init - initializes a new eattr block
760 static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
762 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
763 unsigned int blks = 1;
765 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
766 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
768 return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
771 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
773 u32 ea_size = GFS2_EA_SIZE(ea);
774 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
776 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
777 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
779 ea->ea_rec_len = cpu_to_be32(ea_size);
780 ea->ea_flags ^= last;
782 new->ea_rec_len = cpu_to_be32(new_size);
783 new->ea_flags = last;
788 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
789 struct gfs2_ea_location *el)
791 struct gfs2_ea_header *ea = el->el_ea;
792 struct gfs2_ea_header *prev = el->el_prev;
795 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
797 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
798 ea->ea_type = GFS2_EATYPE_UNUSED;
800 } else if (GFS2_EA2NEXT(prev) != ea) {
801 prev = GFS2_EA2NEXT(prev);
802 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
805 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
806 prev->ea_rec_len = cpu_to_be32(len);
808 if (GFS2_EA_IS_LAST(ea))
809 prev->ea_flags |= GFS2_EAFLAG_LAST;
815 struct gfs2_ea_request *es_er;
816 struct gfs2_ea_location *es_el;
818 struct buffer_head *es_bh;
819 struct gfs2_ea_header *es_ea;
822 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
823 struct gfs2_ea_header *ea, struct ea_set *es)
825 struct gfs2_ea_request *er = es->es_er;
826 struct buffer_head *dibh;
829 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
833 gfs2_trans_add_bh(ip->i_gl, bh, 1);
836 ea = ea_split_ea(ea);
838 ea_write(ip, ea, er);
841 ea_set_remove_stuffed(ip, es->es_el);
843 error = gfs2_meta_inode_buffer(ip, &dibh);
847 if (er->er_flags & GFS2_ERF_MODE) {
848 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
849 (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
850 ip->i_di.di_mode = er->er_mode;
852 ip->i_di.di_ctime = get_seconds();
853 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
854 gfs2_dinode_out(&ip->i_di, dibh->b_data);
857 gfs2_trans_end(GFS2_SB(&ip->i_inode));
861 static int ea_set_simple_alloc(struct gfs2_inode *ip,
862 struct gfs2_ea_request *er, void *private)
864 struct ea_set *es = private;
865 struct gfs2_ea_header *ea = es->es_ea;
868 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
871 ea = ea_split_ea(ea);
873 error = ea_write(ip, ea, er);
878 ea_set_remove_stuffed(ip, es->es_el);
883 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
884 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
887 struct ea_set *es = private;
892 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
894 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
895 if (GFS2_EA_REC_LEN(ea) < size)
897 if (!GFS2_EA_IS_STUFFED(ea)) {
898 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
903 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
909 error = ea_set_simple_noalloc(ip, bh, ea, es);
917 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
918 GFS2_SB(&ip->i_inode)->sd_jbsize);
920 error = ea_alloc_skeleton(ip, es->es_er, blks,
921 ea_set_simple_alloc, es);
929 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
932 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
933 struct buffer_head *indbh, *newbh;
936 int mh_size = sizeof(struct gfs2_meta_header);
938 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
941 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT,
946 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
951 eablk = (u64 *)(indbh->b_data + mh_size);
952 end = eablk + sdp->sd_inptrs;
954 for (; eablk < end; eablk++)
963 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
967 blk = gfs2_alloc_meta(ip);
969 indbh = gfs2_meta_new(ip->i_gl, blk);
970 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
971 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
972 gfs2_buffer_clear_tail(indbh, mh_size);
974 eablk = (u64 *)(indbh->b_data + mh_size);
975 *eablk = cpu_to_be64(ip->i_di.di_eattr);
976 ip->i_di.di_eattr = blk;
977 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
978 ip->i_di.di_blocks++;
983 error = ea_alloc_blk(ip, &newbh);
987 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
988 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
994 ea_set_remove_stuffed(ip, private);
1001 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1002 struct gfs2_ea_location *el)
1005 unsigned int blks = 2;
1008 memset(&es, 0, sizeof(struct ea_set));
1012 error = ea_foreach(ip, ea_set_simple, &es);
1018 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1020 if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1021 blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
1023 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1026 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1027 struct gfs2_ea_location *el)
1029 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1030 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1031 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
1032 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1035 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1038 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1040 struct gfs2_ea_location el;
1043 if (!ip->i_di.di_eattr) {
1044 if (er->er_flags & XATTR_REPLACE)
1046 return ea_init(ip, er);
1049 error = gfs2_ea_find(ip, er, &el);
1054 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1060 if (!(er->er_flags & XATTR_CREATE)) {
1061 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1062 error = ea_set_i(ip, er, &el);
1063 if (!error && unstuffed)
1064 ea_set_remove_unstuffed(ip, &el);
1070 if (!(er->er_flags & XATTR_REPLACE))
1071 error = ea_set_i(ip, er, NULL);
1077 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1079 struct gfs2_holder i_gh;
1082 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1084 if (!er->er_data || !er->er_data_len) {
1086 er->er_data_len = 0;
1088 error = ea_check_size(GFS2_SB(&ip->i_inode), er);
1092 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1096 if (IS_IMMUTABLE(&ip->i_inode))
1099 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1101 gfs2_glock_dq_uninit(&i_gh);
1106 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1108 struct gfs2_ea_header *ea = el->el_ea;
1109 struct gfs2_ea_header *prev = el->el_prev;
1110 struct buffer_head *dibh;
1113 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1117 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1122 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1123 prev->ea_rec_len = cpu_to_be32(len);
1125 if (GFS2_EA_IS_LAST(ea))
1126 prev->ea_flags |= GFS2_EAFLAG_LAST;
1128 ea->ea_type = GFS2_EATYPE_UNUSED;
1130 error = gfs2_meta_inode_buffer(ip, &dibh);
1132 ip->i_di.di_ctime = get_seconds();
1133 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1134 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1138 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1143 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1145 struct gfs2_ea_location el;
1148 if (!ip->i_di.di_eattr)
1151 error = gfs2_ea_find(ip, er, &el);
1157 if (GFS2_EA_IS_STUFFED(el.el_ea))
1158 error = ea_remove_stuffed(ip, &el);
1160 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1169 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1170 * @ip: pointer to the inode of the target file
1171 * @er: request information
1176 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1178 struct gfs2_holder i_gh;
1181 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1184 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1188 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1191 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1193 gfs2_glock_dq_uninit(&i_gh);
1198 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1199 struct gfs2_ea_header *ea, char *data)
1201 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1202 struct buffer_head **bh;
1203 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1204 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1205 u64 *dataptrs = GFS2_EA2DATAPTRS(ea);
1209 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1213 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1217 for (x = 0; x < nptrs; x++) {
1218 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
1228 for (x = 0; x < nptrs; x++) {
1229 error = gfs2_meta_wait(sdp, bh[x]);
1231 for (; x < nptrs; x++)
1235 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1236 for (; x < nptrs; x++)
1242 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1244 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
1245 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1247 amount -= sdp->sd_jbsize;
1248 data += sdp->sd_jbsize;
1258 gfs2_trans_end(sdp);
1263 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1264 struct iattr *attr, char *data)
1266 struct buffer_head *dibh;
1269 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1270 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1274 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1275 memcpy(GFS2_EA2DATA(el->el_ea), data,
1276 GFS2_EA_DATA_LEN(el->el_ea));
1278 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1283 error = gfs2_meta_inode_buffer(ip, &dibh);
1285 error = inode_setattr(&ip->i_inode, attr);
1286 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1287 gfs2_inode_attr_out(ip);
1288 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1289 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1293 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1298 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1300 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1301 struct gfs2_rgrp_list rlist;
1302 struct buffer_head *indbh, *dibh;
1304 unsigned int rg_blocks = 0;
1306 unsigned int blen = 0;
1307 unsigned int blks = 0;
1311 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1313 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &indbh);
1317 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1322 eablk = (u64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1323 end = eablk + sdp->sd_inptrs;
1325 for (; eablk < end; eablk++) {
1330 bn = be64_to_cpu(*eablk);
1332 if (bstart + blen == bn)
1336 gfs2_rlist_add(sdp, &rlist, bstart);
1343 gfs2_rlist_add(sdp, &rlist, bstart);
1347 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1349 for (x = 0; x < rlist.rl_rgrps; x++) {
1350 struct gfs2_rgrpd *rgd;
1351 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1352 rg_blocks += rgd->rd_ri.ri_length;
1355 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1357 goto out_rlist_free;
1359 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1360 RES_STATFS + RES_QUOTA, blks);
1364 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1366 eablk = (u64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1370 for (; eablk < end; eablk++) {
1375 bn = be64_to_cpu(*eablk);
1377 if (bstart + blen == bn)
1381 gfs2_free_meta(ip, bstart, blen);
1387 if (!ip->i_di.di_blocks)
1388 gfs2_consist_inode(ip);
1389 ip->i_di.di_blocks--;
1392 gfs2_free_meta(ip, bstart, blen);
1394 ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1396 error = gfs2_meta_inode_buffer(ip, &dibh);
1398 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1399 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1403 gfs2_trans_end(sdp);
1406 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1408 gfs2_rlist_free(&rlist);
1414 static int ea_dealloc_block(struct gfs2_inode *ip)
1416 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1417 struct gfs2_alloc *al = &ip->i_alloc;
1418 struct gfs2_rgrpd *rgd;
1419 struct buffer_head *dibh;
1422 rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1424 gfs2_consist_inode(ip);
1428 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1433 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1438 gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1440 ip->i_di.di_eattr = 0;
1441 if (!ip->i_di.di_blocks)
1442 gfs2_consist_inode(ip);
1443 ip->i_di.di_blocks--;
1445 error = gfs2_meta_inode_buffer(ip, &dibh);
1447 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1448 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1452 gfs2_trans_end(sdp);
1455 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1460 * gfs2_ea_dealloc - deallocate the extended attribute fork
1466 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1468 struct gfs2_alloc *al;
1471 al = gfs2_alloc_get(ip);
1473 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1477 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
1481 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1485 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1486 error = ea_dealloc_indirect(ip);
1491 error = ea_dealloc_block(ip);
1494 gfs2_glock_dq_uninit(&al->al_ri_gh);
1496 gfs2_quota_unhold(ip);