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 <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(GFS2_SB(&ip->i_inode), 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;
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(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
136 eablk = (u64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
137 end = eablk + GFS2_SB(&ip->i_inode)->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);
161 struct gfs2_ea_request *ef_er;
162 struct gfs2_ea_location *ef_el;
165 static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
166 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
169 struct ea_find *ef = private;
170 struct gfs2_ea_request *er = ef->ef_er;
172 if (ea->ea_type == GFS2_EATYPE_UNUSED)
175 if (ea->ea_type == er->er_type) {
176 if (ea->ea_name_len == er->er_name_len &&
177 !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
178 struct gfs2_ea_location *el = ef->ef_el;
188 else if ((ip->i_di.di_flags & GFS2_DIF_EA_PACKED) &&
189 er->er_type == GFS2_EATYPE_SYS)
196 int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
197 struct gfs2_ea_location *el)
205 memset(el, 0, sizeof(struct gfs2_ea_location));
207 error = ea_foreach(ip, ea_find_i, &ef);
215 * ea_dealloc_unstuffed -
222 * Take advantage of the fact that all unstuffed blocks are
223 * allocated from the same RG. But watch, this may not always
229 static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
230 struct gfs2_ea_header *ea,
231 struct gfs2_ea_header *prev, void *private)
233 int *leave = private;
234 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
235 struct gfs2_rgrpd *rgd;
236 struct gfs2_holder rg_gh;
237 struct buffer_head *dibh;
238 u64 *dataptrs, bn = 0;
240 unsigned int blen = 0;
241 unsigned int blks = 0;
245 if (GFS2_EA_IS_STUFFED(ea))
248 dataptrs = GFS2_EA2DATAPTRS(ea);
249 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++)
252 bn = be64_to_cpu(*dataptrs);
257 rgd = gfs2_blk2rgrpd(sdp, bn);
259 gfs2_consist_inode(ip);
263 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
267 error = gfs2_trans_begin(sdp, rgd->rd_ri.ri_length +
268 RES_DINODE + RES_EATTR + RES_STATFS +
273 gfs2_trans_add_bh(ip->i_gl, bh, 1);
275 dataptrs = GFS2_EA2DATAPTRS(ea);
276 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
279 bn = be64_to_cpu(*dataptrs);
281 if (bstart + blen == bn)
285 gfs2_free_meta(ip, bstart, blen);
291 if (!ip->i_di.di_blocks)
292 gfs2_consist_inode(ip);
293 ip->i_di.di_blocks--;
296 gfs2_free_meta(ip, bstart, blen);
298 if (prev && !leave) {
301 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
302 prev->ea_rec_len = cpu_to_be32(len);
304 if (GFS2_EA_IS_LAST(ea))
305 prev->ea_flags |= GFS2_EAFLAG_LAST;
307 ea->ea_type = GFS2_EATYPE_UNUSED;
311 error = gfs2_meta_inode_buffer(ip, &dibh);
313 ip->i_di.di_ctime = get_seconds();
314 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
315 gfs2_dinode_out(&ip->i_di, dibh->b_data);
322 gfs2_glock_dq_uninit(&rg_gh);
326 static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
327 struct gfs2_ea_header *ea,
328 struct gfs2_ea_header *prev, int leave)
330 struct gfs2_alloc *al;
333 al = gfs2_alloc_get(ip);
335 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
339 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
343 error = ea_dealloc_unstuffed(ip,
345 (leave) ? &error : NULL);
347 gfs2_glock_dq_uninit(&al->al_ri_gh);
350 gfs2_quota_unhold(ip);
357 struct gfs2_ea_request *ei_er;
358 unsigned int ei_size;
361 static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
362 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
365 struct ea_list *ei = private;
366 struct gfs2_ea_request *er = ei->ei_er;
367 unsigned int ea_size = gfs2_ea_strlen(ea);
369 if (ea->ea_type == GFS2_EATYPE_UNUSED)
372 if (er->er_data_len) {
377 if (ei->ei_size + ea_size > er->er_data_len)
380 switch (ea->ea_type) {
381 case GFS2_EATYPE_USR:
385 case GFS2_EATYPE_SYS:
389 case GFS2_EATYPE_SECURITY:
390 prefix = "security.";
397 memcpy(er->er_data + ei->ei_size, prefix, l);
398 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
400 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
403 ei->ei_size += ea_size;
413 * Returns: actual size of data on success, -errno on error
416 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
418 struct gfs2_holder i_gh;
421 if (!er->er_data || !er->er_data_len) {
426 error = gfs2_glock_nq_init(ip->i_gl,
427 LM_ST_SHARED, LM_FLAG_ANY,
432 if (ip->i_di.di_eattr) {
433 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
435 error = ea_foreach(ip, ea_list_i, &ei);
440 gfs2_glock_dq_uninit(&i_gh);
446 * ea_get_unstuffed - actually copies the unstuffed data into the
455 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
458 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
459 struct buffer_head **bh;
460 unsigned int amount = GFS2_EA_DATA_LEN(ea);
461 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
462 u64 *dataptrs = GFS2_EA2DATAPTRS(ea);
466 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
470 for (x = 0; x < nptrs; x++) {
471 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
481 for (x = 0; x < nptrs; x++) {
482 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
484 for (; x < nptrs; x++)
488 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
489 for (; x < nptrs; x++)
496 bh[x]->b_data + sizeof(struct gfs2_meta_header),
497 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
499 amount -= sdp->sd_jbsize;
500 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 = GFS2_SB(&ip->i_inode);
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 = GFS2_SB(&ip->i_inode);
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 u64 *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((u64)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(GFS2_SB(&ip->i_inode),
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(GFS2_SB(&ip->i_inode),
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(GFS2_SB(&ip->i_inode));
748 gfs2_inplace_release(ip);
750 gfs2_quota_unlock(ip);
756 static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
759 struct buffer_head *bh;
762 error = ea_alloc_blk(ip, &bh);
766 ip->i_di.di_eattr = bh->b_blocknr;
767 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
775 * ea_init - initializes a new eattr block
782 static int ea_init(struct gfs2_inode *ip, struct gfs2_ea_request *er)
784 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
785 unsigned int blks = 1;
787 if (GFS2_EAREQ_SIZE_STUFFED(er) > jbsize)
788 blks += DIV_ROUND_UP(er->er_data_len, jbsize);
790 return ea_alloc_skeleton(ip, er, blks, ea_init_i, NULL);
793 static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
795 u32 ea_size = GFS2_EA_SIZE(ea);
796 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
798 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
799 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
801 ea->ea_rec_len = cpu_to_be32(ea_size);
802 ea->ea_flags ^= last;
804 new->ea_rec_len = cpu_to_be32(new_size);
805 new->ea_flags = last;
810 static void ea_set_remove_stuffed(struct gfs2_inode *ip,
811 struct gfs2_ea_location *el)
813 struct gfs2_ea_header *ea = el->el_ea;
814 struct gfs2_ea_header *prev = el->el_prev;
817 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
819 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
820 ea->ea_type = GFS2_EATYPE_UNUSED;
822 } else if (GFS2_EA2NEXT(prev) != ea) {
823 prev = GFS2_EA2NEXT(prev);
824 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
827 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
828 prev->ea_rec_len = cpu_to_be32(len);
830 if (GFS2_EA_IS_LAST(ea))
831 prev->ea_flags |= GFS2_EAFLAG_LAST;
837 struct gfs2_ea_request *es_er;
838 struct gfs2_ea_location *es_el;
840 struct buffer_head *es_bh;
841 struct gfs2_ea_header *es_ea;
844 static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
845 struct gfs2_ea_header *ea, struct ea_set *es)
847 struct gfs2_ea_request *er = es->es_er;
848 struct buffer_head *dibh;
851 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
855 gfs2_trans_add_bh(ip->i_gl, bh, 1);
858 ea = ea_split_ea(ea);
860 ea_write(ip, ea, er);
863 ea_set_remove_stuffed(ip, es->es_el);
865 error = gfs2_meta_inode_buffer(ip, &dibh);
869 if (er->er_flags & GFS2_ERF_MODE) {
870 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
871 (ip->i_di.di_mode & S_IFMT) == (er->er_mode & S_IFMT));
872 ip->i_di.di_mode = er->er_mode;
874 ip->i_di.di_ctime = get_seconds();
875 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
876 gfs2_dinode_out(&ip->i_di, dibh->b_data);
879 gfs2_trans_end(GFS2_SB(&ip->i_inode));
883 static int ea_set_simple_alloc(struct gfs2_inode *ip,
884 struct gfs2_ea_request *er, void *private)
886 struct ea_set *es = private;
887 struct gfs2_ea_header *ea = es->es_ea;
890 gfs2_trans_add_bh(ip->i_gl, es->es_bh, 1);
893 ea = ea_split_ea(ea);
895 error = ea_write(ip, ea, er);
900 ea_set_remove_stuffed(ip, es->es_el);
905 static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
906 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
909 struct ea_set *es = private;
914 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er, &size);
916 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
917 if (GFS2_EA_REC_LEN(ea) < size)
919 if (!GFS2_EA_IS_STUFFED(ea)) {
920 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
925 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
931 error = ea_set_simple_noalloc(ip, bh, ea, es);
939 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
940 GFS2_SB(&ip->i_inode)->sd_jbsize);
942 error = ea_alloc_skeleton(ip, es->es_er, blks,
943 ea_set_simple_alloc, es);
951 static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
954 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
955 struct buffer_head *indbh, *newbh;
958 int mh_size = sizeof(struct gfs2_meta_header);
960 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
963 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
964 DIO_START | DIO_WAIT, &indbh);
968 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
973 eablk = (u64 *)(indbh->b_data + mh_size);
974 end = eablk + sdp->sd_inptrs;
976 for (; eablk < end; eablk++)
985 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
989 blk = gfs2_alloc_meta(ip);
991 indbh = gfs2_meta_new(ip->i_gl, blk);
992 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
993 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
994 gfs2_buffer_clear_tail(indbh, mh_size);
996 eablk = (u64 *)(indbh->b_data + mh_size);
997 *eablk = cpu_to_be64(ip->i_di.di_eattr);
998 ip->i_di.di_eattr = blk;
999 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
1000 ip->i_di.di_blocks++;
1005 error = ea_alloc_blk(ip, &newbh);
1009 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
1010 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1016 ea_set_remove_stuffed(ip, (struct gfs2_ea_location *)private);
1023 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1024 struct gfs2_ea_location *el)
1027 unsigned int blks = 2;
1030 memset(&es, 0, sizeof(struct ea_set));
1034 error = ea_foreach(ip, ea_set_simple, &es);
1040 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1042 if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1043 blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
1045 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1048 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1049 struct gfs2_ea_location *el)
1051 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1052 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1053 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
1054 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1057 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1060 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1062 struct gfs2_ea_location el;
1065 if (!ip->i_di.di_eattr) {
1066 if (er->er_flags & XATTR_REPLACE)
1068 return ea_init(ip, er);
1071 error = gfs2_ea_find(ip, er, &el);
1076 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1082 if (!(er->er_flags & XATTR_CREATE)) {
1083 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1084 error = ea_set_i(ip, er, &el);
1085 if (!error && unstuffed)
1086 ea_set_remove_unstuffed(ip, &el);
1092 if (!(er->er_flags & XATTR_REPLACE))
1093 error = ea_set_i(ip, er, NULL);
1099 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1101 struct gfs2_holder i_gh;
1104 if (!er->er_name_len ||
1105 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1107 if (!er->er_data || !er->er_data_len) {
1109 er->er_data_len = 0;
1111 error = ea_check_size(GFS2_SB(&ip->i_inode), er);
1115 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1119 if (IS_IMMUTABLE(&ip->i_inode))
1122 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1124 gfs2_glock_dq_uninit(&i_gh);
1129 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1131 struct gfs2_ea_header *ea = el->el_ea;
1132 struct gfs2_ea_header *prev = el->el_prev;
1133 struct buffer_head *dibh;
1136 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1140 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1145 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1146 prev->ea_rec_len = cpu_to_be32(len);
1148 if (GFS2_EA_IS_LAST(ea))
1149 prev->ea_flags |= GFS2_EAFLAG_LAST;
1151 ea->ea_type = GFS2_EATYPE_UNUSED;
1153 error = gfs2_meta_inode_buffer(ip, &dibh);
1155 ip->i_di.di_ctime = get_seconds();
1156 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1157 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1161 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1166 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1168 struct gfs2_ea_location el;
1171 if (!ip->i_di.di_eattr)
1174 error = gfs2_ea_find(ip, er, &el);
1180 if (GFS2_EA_IS_STUFFED(el.el_ea))
1181 error = ea_remove_stuffed(ip, &el);
1183 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1192 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1193 * @ip: pointer to the inode of the target file
1194 * @er: request information
1199 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1201 struct gfs2_holder i_gh;
1204 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1207 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1211 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1214 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1216 gfs2_glock_dq_uninit(&i_gh);
1221 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1222 struct gfs2_ea_header *ea, char *data)
1224 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1225 struct buffer_head **bh;
1226 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1227 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1228 u64 *dataptrs = GFS2_EA2DATAPTRS(ea);
1232 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1236 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1240 for (x = 0; x < nptrs; x++) {
1241 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs),
1251 for (x = 0; x < nptrs; x++) {
1252 error = gfs2_meta_reread(sdp, bh[x], DIO_WAIT);
1254 for (; x < nptrs; x++)
1258 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1259 for (; x < nptrs; x++)
1265 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1267 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header),
1269 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1271 amount -= sdp->sd_jbsize;
1272 data += sdp->sd_jbsize;
1282 gfs2_trans_end(sdp);
1287 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1288 struct iattr *attr, char *data)
1290 struct buffer_head *dibh;
1293 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1294 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1298 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1299 memcpy(GFS2_EA2DATA(el->el_ea),
1301 GFS2_EA_DATA_LEN(el->el_ea));
1303 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1308 error = gfs2_meta_inode_buffer(ip, &dibh);
1310 error = inode_setattr(&ip->i_inode, attr);
1311 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1312 gfs2_inode_attr_out(ip);
1313 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1314 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1318 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1323 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1325 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1326 struct gfs2_rgrp_list rlist;
1327 struct buffer_head *indbh, *dibh;
1329 unsigned int rg_blocks = 0;
1331 unsigned int blen = 0;
1332 unsigned int blks = 0;
1336 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1338 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr,
1339 DIO_START | DIO_WAIT, &indbh);
1343 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1348 eablk = (u64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1349 end = eablk + sdp->sd_inptrs;
1351 for (; eablk < end; eablk++) {
1356 bn = be64_to_cpu(*eablk);
1358 if (bstart + blen == bn)
1362 gfs2_rlist_add(sdp, &rlist, bstart);
1369 gfs2_rlist_add(sdp, &rlist, bstart);
1373 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1375 for (x = 0; x < rlist.rl_rgrps; x++) {
1376 struct gfs2_rgrpd *rgd;
1377 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1378 rg_blocks += rgd->rd_ri.ri_length;
1381 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1383 goto out_rlist_free;
1385 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
1386 RES_INDIRECT + RES_STATFS +
1391 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1393 eablk = (u64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1397 for (; eablk < end; eablk++) {
1402 bn = be64_to_cpu(*eablk);
1404 if (bstart + blen == bn)
1408 gfs2_free_meta(ip, bstart, blen);
1414 if (!ip->i_di.di_blocks)
1415 gfs2_consist_inode(ip);
1416 ip->i_di.di_blocks--;
1419 gfs2_free_meta(ip, bstart, blen);
1421 ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1423 error = gfs2_meta_inode_buffer(ip, &dibh);
1425 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1426 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1430 gfs2_trans_end(sdp);
1433 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1435 gfs2_rlist_free(&rlist);
1441 static int ea_dealloc_block(struct gfs2_inode *ip)
1443 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1444 struct gfs2_alloc *al = &ip->i_alloc;
1445 struct gfs2_rgrpd *rgd;
1446 struct buffer_head *dibh;
1449 rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1451 gfs2_consist_inode(ip);
1455 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1460 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE +
1461 RES_STATFS + RES_QUOTA, 1);
1465 gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1467 ip->i_di.di_eattr = 0;
1468 if (!ip->i_di.di_blocks)
1469 gfs2_consist_inode(ip);
1470 ip->i_di.di_blocks--;
1472 error = gfs2_meta_inode_buffer(ip, &dibh);
1474 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1475 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1479 gfs2_trans_end(sdp);
1482 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1487 * gfs2_ea_dealloc - deallocate the extended attribute fork
1493 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1495 struct gfs2_alloc *al;
1498 al = gfs2_alloc_get(ip);
1500 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1504 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
1508 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1512 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1513 error = ea_dealloc_indirect(ip);
1518 error = ea_dealloc_block(ip);
1521 gfs2_glock_dq_uninit(&al->al_ri_gh);
1523 gfs2_quota_unhold(ip);