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 <linux/lm_interface.h>
17 #include <asm/uaccess.h>
33 * ea_calc_size - returns the acutal number of bytes the request will take up
34 * (not counting any unstuffed data blocks)
39 * Returns: 1 if the EA should be stuffed
42 static int ea_calc_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er,
45 *size = GFS2_EAREQ_SIZE_STUFFED(er);
46 if (*size <= sdp->sd_jbsize)
49 *size = GFS2_EAREQ_SIZE_UNSTUFFED(sdp, er);
54 static int ea_check_size(struct gfs2_sbd *sdp, struct gfs2_ea_request *er)
58 if (er->er_data_len > GFS2_EA_MAX_DATA_LEN)
61 ea_calc_size(sdp, er, &size);
63 /* This can only happen with 512 byte blocks */
64 if (size > sdp->sd_jbsize)
70 typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
71 struct gfs2_ea_header *ea,
72 struct gfs2_ea_header *prev, void *private);
74 static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
75 ea_call_t ea_call, void *data)
77 struct gfs2_ea_header *ea, *prev = NULL;
80 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
83 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
84 if (!GFS2_EA_REC_LEN(ea))
86 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
87 bh->b_data + bh->b_size))
89 if (!GFS2_EATYPE_VALID(ea->ea_type))
92 error = ea_call(ip, bh, ea, prev, data);
96 if (GFS2_EA_IS_LAST(ea)) {
97 if ((char *)GFS2_EA2NEXT(ea) !=
98 bh->b_data + bh->b_size)
107 gfs2_consist_inode(ip);
111 static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
113 struct buffer_head *bh, *eabh;
117 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &bh);
121 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT)) {
122 error = ea_foreach_i(ip, bh, ea_call, data);
126 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
131 eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
132 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
134 for (; eablk < end; eablk++) {
139 bn = be64_to_cpu(*eablk);
141 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, &eabh);
144 error = ea_foreach_i(ip, eabh, ea_call, data);
155 struct gfs2_ea_request *ef_er;
156 struct gfs2_ea_location *ef_el;
159 static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
160 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
163 struct ea_find *ef = private;
164 struct gfs2_ea_request *er = ef->ef_er;
166 if (ea->ea_type == GFS2_EATYPE_UNUSED)
169 if (ea->ea_type == er->er_type) {
170 if (ea->ea_name_len == er->er_name_len &&
171 !memcmp(GFS2_EA2NAME(ea), er->er_name, ea->ea_name_len)) {
172 struct gfs2_ea_location *el = ef->ef_el;
184 int gfs2_ea_find(struct gfs2_inode *ip, struct gfs2_ea_request *er,
185 struct gfs2_ea_location *el)
193 memset(el, 0, sizeof(struct gfs2_ea_location));
195 error = ea_foreach(ip, ea_find_i, &ef);
203 * ea_dealloc_unstuffed -
210 * Take advantage of the fact that all unstuffed blocks are
211 * allocated from the same RG. But watch, this may not always
217 static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
218 struct gfs2_ea_header *ea,
219 struct gfs2_ea_header *prev, void *private)
221 int *leave = private;
222 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
223 struct gfs2_rgrpd *rgd;
224 struct gfs2_holder rg_gh;
225 struct buffer_head *dibh;
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_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--;
283 gfs2_set_inode_blocks(&ip->i_inode);
286 gfs2_free_meta(ip, bstart, blen);
288 if (prev && !leave) {
291 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
292 prev->ea_rec_len = cpu_to_be32(len);
294 if (GFS2_EA_IS_LAST(ea))
295 prev->ea_flags |= GFS2_EAFLAG_LAST;
297 ea->ea_type = GFS2_EATYPE_UNUSED;
301 error = gfs2_meta_inode_buffer(ip, &dibh);
303 ip->i_inode.i_ctime = CURRENT_TIME;
304 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
305 gfs2_dinode_out(ip, dibh->b_data);
312 gfs2_glock_dq_uninit(&rg_gh);
316 static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
317 struct gfs2_ea_header *ea,
318 struct gfs2_ea_header *prev, int leave)
320 struct gfs2_alloc *al;
323 al = gfs2_alloc_get(ip);
325 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
329 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
333 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
335 gfs2_glock_dq_uninit(&al->al_ri_gh);
338 gfs2_quota_unhold(ip);
345 struct gfs2_ea_request *ei_er;
346 unsigned int ei_size;
349 static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
350 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
353 struct ea_list *ei = private;
354 struct gfs2_ea_request *er = ei->ei_er;
355 unsigned int ea_size = gfs2_ea_strlen(ea);
357 if (ea->ea_type == GFS2_EATYPE_UNUSED)
360 if (er->er_data_len) {
365 if (ei->ei_size + ea_size > er->er_data_len)
368 switch (ea->ea_type) {
369 case GFS2_EATYPE_USR:
373 case GFS2_EATYPE_SYS:
377 case GFS2_EATYPE_SECURITY:
378 prefix = "security.";
385 memcpy(er->er_data + ei->ei_size, prefix, l);
386 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
388 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
391 ei->ei_size += ea_size;
401 * Returns: actual size of data on success, -errno on error
404 int gfs2_ea_list(struct gfs2_inode *ip, struct gfs2_ea_request *er)
406 struct gfs2_holder i_gh;
409 if (!er->er_data || !er->er_data_len) {
414 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
418 if (ip->i_di.di_eattr) {
419 struct ea_list ei = { .ei_er = er, .ei_size = 0 };
421 error = ea_foreach(ip, ea_list_i, &ei);
426 gfs2_glock_dq_uninit(&i_gh);
432 * ea_get_unstuffed - actually copies the unstuffed data into the
434 * @ip: The GFS2 inode
435 * @ea: The extended attribute header structure
436 * @data: The data to be copied
441 static int ea_get_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
444 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
445 struct buffer_head **bh;
446 unsigned int amount = GFS2_EA_DATA_LEN(ea);
447 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
448 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
452 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
456 for (x = 0; x < nptrs; x++) {
457 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
467 for (x = 0; x < nptrs; x++) {
468 error = gfs2_meta_wait(sdp, bh[x]);
470 for (; x < nptrs; x++)
474 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
475 for (; x < nptrs; x++)
481 memcpy(data, bh[x]->b_data + sizeof(struct gfs2_meta_header),
482 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
484 amount -= sdp->sd_jbsize;
485 data += sdp->sd_jbsize;
495 int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
498 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
499 memcpy(data, GFS2_EA2DATA(el->el_ea), GFS2_EA_DATA_LEN(el->el_ea));
502 return ea_get_unstuffed(ip, el->el_ea, data);
507 * @ip: The GFS2 inode
508 * @er: The request structure
510 * Returns: actual size of data on success, -errno on error
513 int gfs2_ea_get_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
515 struct gfs2_ea_location el;
518 if (!ip->i_di.di_eattr)
521 error = gfs2_ea_find(ip, er, &el);
527 if (er->er_data_len) {
528 if (GFS2_EA_DATA_LEN(el.el_ea) > er->er_data_len)
531 error = gfs2_ea_get_copy(ip, &el, er->er_data);
534 error = GFS2_EA_DATA_LEN(el.el_ea);
543 * @ip: The GFS2 inode
544 * @er: The request structure
546 * Returns: actual size of data on success, -errno on error
549 int gfs2_ea_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
551 struct gfs2_holder i_gh;
554 if (!er->er_name_len ||
555 er->er_name_len > GFS2_EA_MAX_NAME_LEN)
557 if (!er->er_data || !er->er_data_len) {
562 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
566 error = gfs2_ea_ops[er->er_type]->eo_get(ip, er);
568 gfs2_glock_dq_uninit(&i_gh);
574 * ea_alloc_blk - allocates a new block for extended attributes.
575 * @ip: A pointer to the inode that's getting extended attributes
576 * @bhp: Pointer to pointer to a struct buffer_head
581 static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
583 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
584 struct gfs2_ea_header *ea;
588 block = gfs2_alloc_block(ip, &n);
589 gfs2_trans_add_unrevoke(sdp, block, 1);
590 *bhp = gfs2_meta_new(ip->i_gl, block);
591 gfs2_trans_add_bh(ip->i_gl, *bhp, 1);
592 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
593 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
595 ea = GFS2_EA_BH2FIRST(*bhp);
596 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
597 ea->ea_type = GFS2_EATYPE_UNUSED;
598 ea->ea_flags = GFS2_EAFLAG_LAST;
601 ip->i_di.di_blocks++;
602 gfs2_set_inode_blocks(&ip->i_inode);
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);
624 ea->ea_data_len = cpu_to_be32(er->er_data_len);
625 ea->ea_name_len = er->er_name_len;
626 ea->ea_type = er->er_type;
629 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
631 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
633 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
635 __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
636 const char *data = er->er_data;
637 unsigned int data_len = er->er_data_len;
641 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
642 for (x = 0; x < ea->ea_num_ptrs; x++) {
643 struct buffer_head *bh;
645 int mh_size = sizeof(struct gfs2_meta_header);
648 block = gfs2_alloc_block(ip, &n);
649 gfs2_trans_add_unrevoke(sdp, block, 1);
650 bh = gfs2_meta_new(ip->i_gl, block);
651 gfs2_trans_add_bh(ip->i_gl, bh, 1);
652 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
654 ip->i_di.di_blocks++;
655 gfs2_set_inode_blocks(&ip->i_inode);
657 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
659 memcpy(bh->b_data + mh_size, data, copy);
660 if (copy < sdp->sd_jbsize)
661 memset(bh->b_data + mh_size + copy, 0,
662 sdp->sd_jbsize - copy);
664 *dataptr++ = cpu_to_be64(bh->b_blocknr);
671 gfs2_assert_withdraw(sdp, !data_len);
677 typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
678 struct gfs2_ea_request *er, void *private);
680 static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
682 ea_skeleton_call_t skeleton_call, void *private)
684 struct gfs2_alloc *al;
685 struct buffer_head *dibh;
688 al = gfs2_alloc_get(ip);
690 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
694 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
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_di.di_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_di.di_flags & GFS2_DIF_EA_INDIRECT) {
946 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_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 blk = gfs2_alloc_block(ip, &n);
973 gfs2_trans_add_unrevoke(sdp, blk, 1);
974 indbh = gfs2_meta_new(ip->i_gl, blk);
975 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
976 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
977 gfs2_buffer_clear_tail(indbh, mh_size);
979 eablk = (__be64 *)(indbh->b_data + mh_size);
980 *eablk = cpu_to_be64(ip->i_di.di_eattr);
981 ip->i_di.di_eattr = blk;
982 ip->i_di.di_flags |= GFS2_DIF_EA_INDIRECT;
983 ip->i_di.di_blocks++;
984 gfs2_set_inode_blocks(&ip->i_inode);
989 error = ea_alloc_blk(ip, &newbh);
993 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
994 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1000 ea_set_remove_stuffed(ip, private);
1007 static int ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
1008 struct gfs2_ea_location *el)
1011 unsigned int blks = 2;
1014 memset(&es, 0, sizeof(struct ea_set));
1018 error = ea_foreach(ip, ea_set_simple, &es);
1024 if (!(ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT))
1026 if (GFS2_EAREQ_SIZE_STUFFED(er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1027 blks += DIV_ROUND_UP(er->er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
1029 return ea_alloc_skeleton(ip, er, blks, ea_set_block, el);
1032 static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1033 struct gfs2_ea_location *el)
1035 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1036 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1037 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
1038 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1041 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev,0);
1044 int gfs2_ea_set_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1046 struct gfs2_ea_location el;
1049 if (!ip->i_di.di_eattr) {
1050 if (er->er_flags & XATTR_REPLACE)
1052 return ea_init(ip, er);
1055 error = gfs2_ea_find(ip, er, &el);
1060 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) {
1066 if (!(er->er_flags & XATTR_CREATE)) {
1067 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1068 error = ea_set_i(ip, er, &el);
1069 if (!error && unstuffed)
1070 ea_set_remove_unstuffed(ip, &el);
1076 if (!(er->er_flags & XATTR_REPLACE))
1077 error = ea_set_i(ip, er, NULL);
1083 int gfs2_ea_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1085 struct gfs2_holder i_gh;
1088 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1090 if (!er->er_data || !er->er_data_len) {
1092 er->er_data_len = 0;
1094 error = ea_check_size(GFS2_SB(&ip->i_inode), er);
1098 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1102 if (IS_IMMUTABLE(&ip->i_inode))
1105 error = gfs2_ea_ops[er->er_type]->eo_set(ip, er);
1107 gfs2_glock_dq_uninit(&i_gh);
1112 static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1114 struct gfs2_ea_header *ea = el->el_ea;
1115 struct gfs2_ea_header *prev = el->el_prev;
1116 struct buffer_head *dibh;
1119 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1123 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1128 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1129 prev->ea_rec_len = cpu_to_be32(len);
1131 if (GFS2_EA_IS_LAST(ea))
1132 prev->ea_flags |= GFS2_EAFLAG_LAST;
1134 ea->ea_type = GFS2_EATYPE_UNUSED;
1136 error = gfs2_meta_inode_buffer(ip, &dibh);
1138 ip->i_inode.i_ctime = CURRENT_TIME;
1139 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1140 gfs2_dinode_out(ip, dibh->b_data);
1144 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1149 int gfs2_ea_remove_i(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1151 struct gfs2_ea_location el;
1154 if (!ip->i_di.di_eattr)
1157 error = gfs2_ea_find(ip, er, &el);
1163 if (GFS2_EA_IS_STUFFED(el.el_ea))
1164 error = ea_remove_stuffed(ip, &el);
1166 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev,
1175 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1176 * @ip: pointer to the inode of the target file
1177 * @er: request information
1182 int gfs2_ea_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
1184 struct gfs2_holder i_gh;
1187 if (!er->er_name_len || er->er_name_len > GFS2_EA_MAX_NAME_LEN)
1190 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1194 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1197 error = gfs2_ea_ops[er->er_type]->eo_remove(ip, er);
1199 gfs2_glock_dq_uninit(&i_gh);
1204 static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1205 struct gfs2_ea_header *ea, char *data)
1207 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1208 struct buffer_head **bh;
1209 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1210 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1211 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
1215 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_KERNEL);
1219 error = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1223 for (x = 0; x < nptrs; x++) {
1224 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0,
1234 for (x = 0; x < nptrs; x++) {
1235 error = gfs2_meta_wait(sdp, bh[x]);
1237 for (; x < nptrs; x++)
1241 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
1242 for (; x < nptrs; x++)
1248 gfs2_trans_add_bh(ip->i_gl, bh[x], 1);
1250 memcpy(bh[x]->b_data + sizeof(struct gfs2_meta_header), data,
1251 (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize);
1253 amount -= sdp->sd_jbsize;
1254 data += sdp->sd_jbsize;
1264 gfs2_trans_end(sdp);
1269 int gfs2_ea_acl_chmod(struct gfs2_inode *ip, struct gfs2_ea_location *el,
1270 struct iattr *attr, char *data)
1272 struct buffer_head *dibh;
1275 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
1276 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1280 gfs2_trans_add_bh(ip->i_gl, el->el_bh, 1);
1281 memcpy(GFS2_EA2DATA(el->el_ea), data,
1282 GFS2_EA_DATA_LEN(el->el_ea));
1284 error = ea_acl_chmod_unstuffed(ip, el->el_ea, data);
1289 error = gfs2_meta_inode_buffer(ip, &dibh);
1291 error = inode_setattr(&ip->i_inode, attr);
1292 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1293 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1294 gfs2_dinode_out(ip, dibh->b_data);
1298 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1303 static int ea_dealloc_indirect(struct gfs2_inode *ip)
1305 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1306 struct gfs2_rgrp_list rlist;
1307 struct buffer_head *indbh, *dibh;
1308 __be64 *eablk, *end;
1309 unsigned int rg_blocks = 0;
1311 unsigned int blen = 0;
1312 unsigned int blks = 0;
1316 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1318 error = gfs2_meta_read(ip->i_gl, ip->i_di.di_eattr, DIO_WAIT, &indbh);
1322 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1327 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1328 end = eablk + sdp->sd_inptrs;
1330 for (; eablk < end; eablk++) {
1335 bn = be64_to_cpu(*eablk);
1337 if (bstart + blen == bn)
1341 gfs2_rlist_add(sdp, &rlist, bstart);
1348 gfs2_rlist_add(sdp, &rlist, bstart);
1352 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
1354 for (x = 0; x < rlist.rl_rgrps; x++) {
1355 struct gfs2_rgrpd *rgd;
1356 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1357 rg_blocks += rgd->rd_length;
1360 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1362 goto out_rlist_free;
1364 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1365 RES_STATFS + RES_QUOTA, blks);
1369 gfs2_trans_add_bh(ip->i_gl, indbh, 1);
1371 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1375 for (; eablk < end; eablk++) {
1380 bn = be64_to_cpu(*eablk);
1382 if (bstart + blen == bn)
1386 gfs2_free_meta(ip, bstart, blen);
1392 if (!ip->i_di.di_blocks)
1393 gfs2_consist_inode(ip);
1394 ip->i_di.di_blocks--;
1395 gfs2_set_inode_blocks(&ip->i_inode);
1398 gfs2_free_meta(ip, bstart, blen);
1400 ip->i_di.di_flags &= ~GFS2_DIF_EA_INDIRECT;
1402 error = gfs2_meta_inode_buffer(ip, &dibh);
1404 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1405 gfs2_dinode_out(ip, dibh->b_data);
1409 gfs2_trans_end(sdp);
1412 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1414 gfs2_rlist_free(&rlist);
1420 static int ea_dealloc_block(struct gfs2_inode *ip)
1422 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1423 struct gfs2_alloc *al = ip->i_alloc;
1424 struct gfs2_rgrpd *rgd;
1425 struct buffer_head *dibh;
1428 rgd = gfs2_blk2rgrpd(sdp, ip->i_di.di_eattr);
1430 gfs2_consist_inode(ip);
1434 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1439 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1444 gfs2_free_meta(ip, ip->i_di.di_eattr, 1);
1446 ip->i_di.di_eattr = 0;
1447 if (!ip->i_di.di_blocks)
1448 gfs2_consist_inode(ip);
1449 ip->i_di.di_blocks--;
1450 gfs2_set_inode_blocks(&ip->i_inode);
1452 error = gfs2_meta_inode_buffer(ip, &dibh);
1454 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1455 gfs2_dinode_out(ip, dibh->b_data);
1459 gfs2_trans_end(sdp);
1462 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1467 * gfs2_ea_dealloc - deallocate the extended attribute fork
1473 int gfs2_ea_dealloc(struct gfs2_inode *ip)
1475 struct gfs2_alloc *al;
1478 al = gfs2_alloc_get(ip);
1480 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1484 error = gfs2_rindex_hold(GFS2_SB(&ip->i_inode), &al->al_ri_gh);
1488 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1492 if (ip->i_di.di_flags & GFS2_DIF_EA_INDIRECT) {
1493 error = ea_dealloc_indirect(ip);
1498 error = ea_dealloc_block(ip);
1501 gfs2_glock_dq_uninit(&al->al_ri_gh);
1503 gfs2_quota_unhold(ip);