2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/xattr.h>
16 #include <linux/gfs2_ondisk.h>
17 #include <asm/semaphore.h>
18 #include <asm/uaccess.h>
21 #include "lm_interface.h"
35 * ea_calc_size - returns the acutal number of bytes the request will take up
36 * (not counting any unstuffed data blocks)
41 * Returns: 1 if the EA should be stuffed
44 static int ea_calc_size(struct gfs2_sbd
*sdp
, struct gfs2_ea_request
*er
,
47 *size
= GFS2_EAREQ_SIZE_STUFFED(er
);
48 if (*size
<= sdp
->sd_jbsize
)
51 *size
= GFS2_EAREQ_SIZE_UNSTUFFED(sdp
, er
);
56 static int ea_check_size(struct gfs2_sbd
*sdp
, struct gfs2_ea_request
*er
)
60 if (er
->er_data_len
> GFS2_EA_MAX_DATA_LEN
)
63 ea_calc_size(sdp
, er
, &size
);
65 /* This can only happen with 512 byte blocks */
66 if (size
> sdp
->sd_jbsize
)
72 typedef int (*ea_call_t
) (struct gfs2_inode
*ip
,
73 struct buffer_head
*bh
,
74 struct gfs2_ea_header
*ea
,
75 struct gfs2_ea_header
*prev
,
78 static int ea_foreach_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
79 ea_call_t ea_call
, void *data
)
81 struct gfs2_ea_header
*ea
, *prev
= NULL
;
84 if (gfs2_metatype_check(ip
->i_sbd
, bh
, GFS2_METATYPE_EA
))
87 for (ea
= GFS2_EA_BH2FIRST(bh
);; prev
= ea
, ea
= GFS2_EA2NEXT(ea
)) {
88 if (!GFS2_EA_REC_LEN(ea
))
90 if (!(bh
->b_data
<= (char *)ea
&&
91 (char *)GFS2_EA2NEXT(ea
) <=
92 bh
->b_data
+ bh
->b_size
))
94 if (!GFS2_EATYPE_VALID(ea
->ea_type
))
97 error
= ea_call(ip
, bh
, ea
, prev
, data
);
101 if (GFS2_EA_IS_LAST(ea
)) {
102 if ((char *)GFS2_EA2NEXT(ea
) !=
103 bh
->b_data
+ bh
->b_size
)
112 gfs2_consist_inode(ip
);
116 static int ea_foreach(struct gfs2_inode
*ip
, ea_call_t ea_call
, void *data
)
118 struct buffer_head
*bh
, *eabh
;
119 uint64_t *eablk
, *end
;
122 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_di
.di_eattr
,
123 DIO_START
| DIO_WAIT
, &bh
);
127 if (!(ip
->i_di
.di_flags
& GFS2_DIF_EA_INDIRECT
)) {
128 error
= ea_foreach_i(ip
, bh
, ea_call
, data
);
132 if (gfs2_metatype_check(ip
->i_sbd
, bh
, GFS2_METATYPE_IN
)) {
137 eablk
= (uint64_t *)(bh
->b_data
+ sizeof(struct gfs2_meta_header
));
138 end
= eablk
+ ip
->i_sbd
->sd_inptrs
;
140 for (; eablk
< end
; eablk
++) {
145 bn
= be64_to_cpu(*eablk
);
147 error
= gfs2_meta_read(ip
->i_gl
, bn
, DIO_START
| DIO_WAIT
,
151 error
= ea_foreach_i(ip
, eabh
, ea_call
, data
);
163 struct gfs2_ea_request
*ef_er
;
164 struct gfs2_ea_location
*ef_el
;
167 static int ea_find_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
168 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
171 struct ea_find
*ef
= private;
172 struct gfs2_ea_request
*er
= ef
->ef_er
;
174 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
)
177 if (ea
->ea_type
== er
->er_type
) {
178 if (ea
->ea_name_len
== er
->er_name_len
&&
179 !memcmp(GFS2_EA2NAME(ea
), er
->er_name
, ea
->ea_name_len
)) {
180 struct gfs2_ea_location
*el
= ef
->ef_el
;
190 else if ((ip
->i_di
.di_flags
& GFS2_DIF_EA_PACKED
) &&
191 er
->er_type
== GFS2_EATYPE_SYS
)
198 int gfs2_ea_find(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
199 struct gfs2_ea_location
*el
)
207 memset(el
, 0, sizeof(struct gfs2_ea_location
));
209 error
= ea_foreach(ip
, ea_find_i
, &ef
);
217 * ea_dealloc_unstuffed -
224 * Take advantage of the fact that all unstuffed blocks are
225 * allocated from the same RG. But watch, this may not always
231 static int ea_dealloc_unstuffed(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
232 struct gfs2_ea_header
*ea
,
233 struct gfs2_ea_header
*prev
, void *private)
235 int *leave
= private;
236 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
237 struct gfs2_rgrpd
*rgd
;
238 struct gfs2_holder rg_gh
;
239 struct buffer_head
*dibh
;
240 uint64_t *dataptrs
, bn
= 0;
242 unsigned int blen
= 0;
243 unsigned int blks
= 0;
247 if (GFS2_EA_IS_STUFFED(ea
))
250 dataptrs
= GFS2_EA2DATAPTRS(ea
);
251 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++, dataptrs
++)
254 bn
= be64_to_cpu(*dataptrs
);
259 rgd
= gfs2_blk2rgrpd(sdp
, bn
);
261 gfs2_consist_inode(ip
);
265 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, &rg_gh
);
269 error
= gfs2_trans_begin(sdp
, rgd
->rd_ri
.ri_length
+
270 RES_DINODE
+ RES_EATTR
+ RES_STATFS
+
275 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
277 dataptrs
= GFS2_EA2DATAPTRS(ea
);
278 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++, dataptrs
++) {
281 bn
= be64_to_cpu(*dataptrs
);
283 if (bstart
+ blen
== bn
)
287 gfs2_free_meta(ip
, bstart
, blen
);
293 if (!ip
->i_di
.di_blocks
)
294 gfs2_consist_inode(ip
);
295 ip
->i_di
.di_blocks
--;
298 gfs2_free_meta(ip
, bstart
, blen
);
300 if (prev
&& !leave
) {
303 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
304 prev
->ea_rec_len
= cpu_to_be32(len
);
306 if (GFS2_EA_IS_LAST(ea
))
307 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
309 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
313 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
315 ip
->i_di
.di_ctime
= get_seconds();
316 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
317 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
324 gfs2_glock_dq_uninit(&rg_gh
);
329 static int ea_remove_unstuffed(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
330 struct gfs2_ea_header
*ea
,
331 struct gfs2_ea_header
*prev
, int leave
)
333 struct gfs2_alloc
*al
;
336 al
= gfs2_alloc_get(ip
);
338 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
342 error
= gfs2_rindex_hold(ip
->i_sbd
, &al
->al_ri_gh
);
346 error
= ea_dealloc_unstuffed(ip
,
348 (leave
) ? &error
: NULL
);
350 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
353 gfs2_quota_unhold(ip
);
362 struct gfs2_ea_request
*ei_er
;
363 unsigned int ei_size
;
366 static int ea_list_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
367 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
370 struct ea_list
*ei
= private;
371 struct gfs2_ea_request
*er
= ei
->ei_er
;
372 unsigned int ea_size
= GFS2_EA_STRLEN(ea
);
374 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
)
377 if (er
->er_data_len
) {
382 if (ei
->ei_size
+ ea_size
> er
->er_data_len
)
385 if (ea
->ea_type
== GFS2_EATYPE_USR
) {
393 memcpy(er
->er_data
+ ei
->ei_size
,
395 memcpy(er
->er_data
+ ei
->ei_size
+ l
,
398 memcpy(er
->er_data
+ ei
->ei_size
+
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
= ip
->i_sbd
;
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 uint64_t *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
;
511 int gfs2_ea_get_copy(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
,
514 if (GFS2_EA_IS_STUFFED(el
->el_ea
)) {
516 GFS2_EA2DATA(el
->el_ea
),
517 GFS2_EA_DATA_LEN(el
->el_ea
));
520 return ea_get_unstuffed(ip
, el
->el_ea
, data
);
528 * Returns: actual size of data on success, -errno on error
531 int gfs2_ea_get_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
533 struct gfs2_ea_location el
;
536 if (!ip
->i_di
.di_eattr
)
539 error
= gfs2_ea_find(ip
, er
, &el
);
545 if (er
->er_data_len
) {
546 if (GFS2_EA_DATA_LEN(el
.el_ea
) > er
->er_data_len
)
549 error
= gfs2_ea_get_copy(ip
, &el
, er
->er_data
);
552 error
= GFS2_EA_DATA_LEN(el
.el_ea
);
564 * Returns: actual size of data on success, -errno on error
567 int gfs2_ea_get(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
569 struct gfs2_holder i_gh
;
572 if (!er
->er_name_len
||
573 er
->er_name_len
> GFS2_EA_MAX_NAME_LEN
)
575 if (!er
->er_data
|| !er
->er_data_len
) {
580 error
= gfs2_glock_nq_init(ip
->i_gl
,
581 LM_ST_SHARED
, LM_FLAG_ANY
,
586 error
= gfs2_ea_ops
[er
->er_type
]->eo_get(ip
, er
);
588 gfs2_glock_dq_uninit(&i_gh
);
594 * ea_alloc_blk - allocates a new block for extended attributes.
595 * @ip: A pointer to the inode that's getting extended attributes
601 static int ea_alloc_blk(struct gfs2_inode
*ip
, struct buffer_head
**bhp
)
603 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
604 struct gfs2_ea_header
*ea
;
607 block
= gfs2_alloc_meta(ip
);
609 *bhp
= gfs2_meta_new(ip
->i_gl
, block
);
610 gfs2_trans_add_bh(ip
->i_gl
, *bhp
, 1);
611 gfs2_metatype_set(*bhp
, GFS2_METATYPE_EA
, GFS2_FORMAT_EA
);
612 gfs2_buffer_clear_tail(*bhp
, sizeof(struct gfs2_meta_header
));
614 ea
= GFS2_EA_BH2FIRST(*bhp
);
615 ea
->ea_rec_len
= cpu_to_be32(sdp
->sd_jbsize
);
616 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
617 ea
->ea_flags
= GFS2_EAFLAG_LAST
;
620 ip
->i_di
.di_blocks
++;
626 * ea_write - writes the request info to an ea, creating new blocks if
628 * @ip: inode that is being modified
629 * @ea: the location of the new ea in a block
630 * @er: the write request
632 * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags
637 static int ea_write(struct gfs2_inode
*ip
, struct gfs2_ea_header
*ea
,
638 struct gfs2_ea_request
*er
)
640 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
642 ea
->ea_data_len
= cpu_to_be32(er
->er_data_len
);
643 ea
->ea_name_len
= er
->er_name_len
;
644 ea
->ea_type
= er
->er_type
;
647 memcpy(GFS2_EA2NAME(ea
), er
->er_name
, er
->er_name_len
);
649 if (GFS2_EAREQ_SIZE_STUFFED(er
) <= sdp
->sd_jbsize
) {
651 memcpy(GFS2_EA2DATA(ea
), er
->er_data
, er
->er_data_len
);
653 uint64_t *dataptr
= GFS2_EA2DATAPTRS(ea
);
654 const char *data
= er
->er_data
;
655 unsigned int data_len
= er
->er_data_len
;
659 ea
->ea_num_ptrs
= DIV_ROUND_UP(er
->er_data_len
, sdp
->sd_jbsize
);
660 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++) {
661 struct buffer_head
*bh
;
663 int mh_size
= sizeof(struct gfs2_meta_header
);
665 block
= gfs2_alloc_meta(ip
);
667 bh
= gfs2_meta_new(ip
->i_gl
, block
);
668 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
669 gfs2_metatype_set(bh
, GFS2_METATYPE_ED
, GFS2_FORMAT_ED
);
671 ip
->i_di
.di_blocks
++;
673 copy
= (data_len
> sdp
->sd_jbsize
) ? sdp
->sd_jbsize
:
675 memcpy(bh
->b_data
+ mh_size
, data
, copy
);
676 if (copy
< sdp
->sd_jbsize
)
677 memset(bh
->b_data
+ mh_size
+ copy
, 0,
678 sdp
->sd_jbsize
- copy
);
680 *dataptr
++ = cpu_to_be64((uint64_t)bh
->b_blocknr
);
687 gfs2_assert_withdraw(sdp
, !data_len
);
693 typedef int (*ea_skeleton_call_t
) (struct gfs2_inode
*ip
,
694 struct gfs2_ea_request
*er
,
697 static int ea_alloc_skeleton(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
699 ea_skeleton_call_t skeleton_call
,
702 struct gfs2_alloc
*al
;
703 struct buffer_head
*dibh
;
706 al
= gfs2_alloc_get(ip
);
708 error
= gfs2_quota_lock(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
712 error
= gfs2_quota_check(ip
, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
716 al
->al_requested
= blks
;
718 error
= gfs2_inplace_reserve(ip
);
722 error
= gfs2_trans_begin(ip
->i_sbd
,
723 blks
+ al
->al_rgd
->rd_ri
.ri_length
+
724 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
728 error
= skeleton_call(ip
, er
, private);
732 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
734 if (er
->er_flags
& GFS2_ERF_MODE
) {
735 gfs2_assert_withdraw(ip
->i_sbd
,
736 (ip
->i_di
.di_mode
& S_IFMT
) ==
737 (er
->er_mode
& S_IFMT
));
738 ip
->i_di
.di_mode
= er
->er_mode
;
740 ip
->i_di
.di_ctime
= get_seconds();
741 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
742 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
747 gfs2_trans_end(ip
->i_sbd
);
750 gfs2_inplace_release(ip
);
753 gfs2_quota_unlock(ip
);
761 static int ea_init_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
764 struct buffer_head
*bh
;
767 error
= ea_alloc_blk(ip
, &bh
);
771 ip
->i_di
.di_eattr
= bh
->b_blocknr
;
772 error
= ea_write(ip
, GFS2_EA_BH2FIRST(bh
), er
);
780 * ea_init - initializes a new eattr block
787 static int ea_init(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
789 unsigned int jbsize
= ip
->i_sbd
->sd_jbsize
;
790 unsigned int blks
= 1;
792 if (GFS2_EAREQ_SIZE_STUFFED(er
) > jbsize
)
793 blks
+= DIV_ROUND_UP(er
->er_data_len
, jbsize
);
795 return ea_alloc_skeleton(ip
, er
, blks
, ea_init_i
, NULL
);
798 static struct gfs2_ea_header
*ea_split_ea(struct gfs2_ea_header
*ea
)
800 uint32_t ea_size
= GFS2_EA_SIZE(ea
);
801 struct gfs2_ea_header
*new = (struct gfs2_ea_header
*)((char *)ea
+
803 uint32_t new_size
= GFS2_EA_REC_LEN(ea
) - ea_size
;
804 int last
= ea
->ea_flags
& GFS2_EAFLAG_LAST
;
806 ea
->ea_rec_len
= cpu_to_be32(ea_size
);
807 ea
->ea_flags
^= last
;
809 new->ea_rec_len
= cpu_to_be32(new_size
);
810 new->ea_flags
= last
;
815 static void ea_set_remove_stuffed(struct gfs2_inode
*ip
,
816 struct gfs2_ea_location
*el
)
818 struct gfs2_ea_header
*ea
= el
->el_ea
;
819 struct gfs2_ea_header
*prev
= el
->el_prev
;
822 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
824 if (!prev
|| !GFS2_EA_IS_STUFFED(ea
)) {
825 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
827 } else if (GFS2_EA2NEXT(prev
) != ea
) {
828 prev
= GFS2_EA2NEXT(prev
);
829 gfs2_assert_withdraw(ip
->i_sbd
, GFS2_EA2NEXT(prev
) == ea
);
832 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
833 prev
->ea_rec_len
= cpu_to_be32(len
);
835 if (GFS2_EA_IS_LAST(ea
))
836 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
842 struct gfs2_ea_request
*es_er
;
843 struct gfs2_ea_location
*es_el
;
845 struct buffer_head
*es_bh
;
846 struct gfs2_ea_header
*es_ea
;
849 static int ea_set_simple_noalloc(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
850 struct gfs2_ea_header
*ea
, struct ea_set
*es
)
852 struct gfs2_ea_request
*er
= es
->es_er
;
853 struct buffer_head
*dibh
;
856 error
= gfs2_trans_begin(ip
->i_sbd
, RES_DINODE
+ 2 * RES_EATTR
, 0);
860 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
863 ea
= ea_split_ea(ea
);
865 ea_write(ip
, ea
, er
);
868 ea_set_remove_stuffed(ip
, es
->es_el
);
870 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
874 if (er
->er_flags
& GFS2_ERF_MODE
) {
875 gfs2_assert_withdraw(ip
->i_sbd
,
876 (ip
->i_di
.di_mode
& S_IFMT
) == (er
->er_mode
& S_IFMT
));
877 ip
->i_di
.di_mode
= er
->er_mode
;
879 ip
->i_di
.di_ctime
= get_seconds();
880 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
881 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
884 gfs2_trans_end(ip
->i_sbd
);
889 static int ea_set_simple_alloc(struct gfs2_inode
*ip
,
890 struct gfs2_ea_request
*er
, void *private)
892 struct ea_set
*es
= private;
893 struct gfs2_ea_header
*ea
= es
->es_ea
;
896 gfs2_trans_add_bh(ip
->i_gl
, es
->es_bh
, 1);
899 ea
= ea_split_ea(ea
);
901 error
= ea_write(ip
, ea
, er
);
906 ea_set_remove_stuffed(ip
, es
->es_el
);
911 static int ea_set_simple(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
912 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
915 struct ea_set
*es
= private;
920 stuffed
= ea_calc_size(ip
->i_sbd
, es
->es_er
, &size
);
922 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
) {
923 if (GFS2_EA_REC_LEN(ea
) < size
)
925 if (!GFS2_EA_IS_STUFFED(ea
)) {
926 error
= ea_remove_unstuffed(ip
, bh
, ea
, prev
, 1);
931 } else if (GFS2_EA_REC_LEN(ea
) - GFS2_EA_SIZE(ea
) >= size
)
937 error
= ea_set_simple_noalloc(ip
, bh
, ea
, es
);
945 blks
= 2 + DIV_ROUND_UP(es
->es_er
->er_data_len
,
946 ip
->i_sbd
->sd_jbsize
);
948 error
= ea_alloc_skeleton(ip
, es
->es_er
, blks
,
949 ea_set_simple_alloc
, es
);
957 static int ea_set_block(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
960 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
961 struct buffer_head
*indbh
, *newbh
;
964 int mh_size
= sizeof(struct gfs2_meta_header
);
966 if (ip
->i_di
.di_flags
& GFS2_DIF_EA_INDIRECT
) {
969 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_di
.di_eattr
,
970 DIO_START
| DIO_WAIT
, &indbh
);
974 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
979 eablk
= (uint64_t *)(indbh
->b_data
+ mh_size
);
980 end
= eablk
+ sdp
->sd_inptrs
;
982 for (; eablk
< end
; eablk
++)
991 gfs2_trans_add_bh(ip
->i_gl
, indbh
, 1);
995 blk
= gfs2_alloc_meta(ip
);
997 indbh
= gfs2_meta_new(ip
->i_gl
, blk
);
998 gfs2_trans_add_bh(ip
->i_gl
, indbh
, 1);
999 gfs2_metatype_set(indbh
, GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
1000 gfs2_buffer_clear_tail(indbh
, mh_size
);
1002 eablk
= (uint64_t *)(indbh
->b_data
+ mh_size
);
1003 *eablk
= cpu_to_be64(ip
->i_di
.di_eattr
);
1004 ip
->i_di
.di_eattr
= blk
;
1005 ip
->i_di
.di_flags
|= GFS2_DIF_EA_INDIRECT
;
1006 ip
->i_di
.di_blocks
++;
1011 error
= ea_alloc_blk(ip
, &newbh
);
1015 *eablk
= cpu_to_be64((uint64_t)newbh
->b_blocknr
);
1016 error
= ea_write(ip
, GFS2_EA_BH2FIRST(newbh
), er
);
1022 ea_set_remove_stuffed(ip
, (struct gfs2_ea_location
*)private);
1030 static int ea_set_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
1031 struct gfs2_ea_location
*el
)
1034 unsigned int blks
= 2;
1037 memset(&es
, 0, sizeof(struct ea_set
));
1041 error
= ea_foreach(ip
, ea_set_simple
, &es
);
1047 if (!(ip
->i_di
.di_flags
& GFS2_DIF_EA_INDIRECT
))
1049 if (GFS2_EAREQ_SIZE_STUFFED(er
) > ip
->i_sbd
->sd_jbsize
)
1050 blks
+= DIV_ROUND_UP(er
->er_data_len
, ip
->i_sbd
->sd_jbsize
);
1052 return ea_alloc_skeleton(ip
, er
, blks
, ea_set_block
, el
);
1055 static int ea_set_remove_unstuffed(struct gfs2_inode
*ip
,
1056 struct gfs2_ea_location
*el
)
1058 if (el
->el_prev
&& GFS2_EA2NEXT(el
->el_prev
) != el
->el_ea
) {
1059 el
->el_prev
= GFS2_EA2NEXT(el
->el_prev
);
1060 gfs2_assert_withdraw(ip
->i_sbd
,
1061 GFS2_EA2NEXT(el
->el_prev
) == el
->el_ea
);
1064 return ea_remove_unstuffed(ip
, el
->el_bh
, el
->el_ea
, el
->el_prev
,0);
1067 int gfs2_ea_set_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
1069 struct gfs2_ea_location el
;
1072 if (!ip
->i_di
.di_eattr
) {
1073 if (er
->er_flags
& XATTR_REPLACE
)
1075 return ea_init(ip
, er
);
1078 error
= gfs2_ea_find(ip
, er
, &el
);
1083 if (ip
->i_di
.di_flags
& GFS2_DIF_APPENDONLY
) {
1089 if (!(er
->er_flags
& XATTR_CREATE
)) {
1090 int unstuffed
= !GFS2_EA_IS_STUFFED(el
.el_ea
);
1091 error
= ea_set_i(ip
, er
, &el
);
1092 if (!error
&& unstuffed
)
1093 ea_set_remove_unstuffed(ip
, &el
);
1099 if (!(er
->er_flags
& XATTR_REPLACE
))
1100 error
= ea_set_i(ip
, er
, NULL
);
1106 int gfs2_ea_set(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
1108 struct gfs2_holder i_gh
;
1111 if (!er
->er_name_len
||
1112 er
->er_name_len
> GFS2_EA_MAX_NAME_LEN
)
1114 if (!er
->er_data
|| !er
->er_data_len
) {
1116 er
->er_data_len
= 0;
1118 error
= ea_check_size(ip
->i_sbd
, er
);
1122 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1126 if (IS_IMMUTABLE(ip
->i_vnode
))
1129 error
= gfs2_ea_ops
[er
->er_type
]->eo_set(ip
, er
);
1131 gfs2_glock_dq_uninit(&i_gh
);
1136 static int ea_remove_stuffed(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
)
1138 struct gfs2_ea_header
*ea
= el
->el_ea
;
1139 struct gfs2_ea_header
*prev
= el
->el_prev
;
1140 struct buffer_head
*dibh
;
1143 error
= gfs2_trans_begin(ip
->i_sbd
, RES_DINODE
+ RES_EATTR
, 0);
1147 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
1152 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
1153 prev
->ea_rec_len
= cpu_to_be32(len
);
1155 if (GFS2_EA_IS_LAST(ea
))
1156 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
1158 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
1160 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1162 ip
->i_di
.di_ctime
= get_seconds();
1163 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1164 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
1168 gfs2_trans_end(ip
->i_sbd
);
1173 int gfs2_ea_remove_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
1175 struct gfs2_ea_location el
;
1178 if (!ip
->i_di
.di_eattr
)
1181 error
= gfs2_ea_find(ip
, er
, &el
);
1187 if (GFS2_EA_IS_STUFFED(el
.el_ea
))
1188 error
= ea_remove_stuffed(ip
, &el
);
1190 error
= ea_remove_unstuffed(ip
, el
.el_bh
, el
.el_ea
, el
.el_prev
,
1199 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1200 * @ip: pointer to the inode of the target file
1201 * @er: request information
1206 int gfs2_ea_remove(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
1208 struct gfs2_holder i_gh
;
1211 if (!er
->er_name_len
|| er
->er_name_len
> GFS2_EA_MAX_NAME_LEN
)
1214 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1218 if (IS_IMMUTABLE(ip
->i_vnode
) || IS_APPEND(ip
->i_vnode
))
1221 error
= gfs2_ea_ops
[er
->er_type
]->eo_remove(ip
, er
);
1223 gfs2_glock_dq_uninit(&i_gh
);
1228 static int ea_acl_chmod_unstuffed(struct gfs2_inode
*ip
,
1229 struct gfs2_ea_header
*ea
, char *data
)
1231 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1232 struct buffer_head
**bh
;
1233 unsigned int amount
= GFS2_EA_DATA_LEN(ea
);
1234 unsigned int nptrs
= DIV_ROUND_UP(amount
, sdp
->sd_jbsize
);
1235 uint64_t *dataptrs
= GFS2_EA2DATAPTRS(ea
);
1239 bh
= kcalloc(nptrs
, sizeof(struct buffer_head
*), GFP_KERNEL
);
1243 error
= gfs2_trans_begin(sdp
, nptrs
+ RES_DINODE
, 0);
1247 for (x
= 0; x
< nptrs
; x
++) {
1248 error
= gfs2_meta_read(ip
->i_gl
, be64_to_cpu(*dataptrs
),
1258 for (x
= 0; x
< nptrs
; x
++) {
1259 error
= gfs2_meta_reread(sdp
, bh
[x
], DIO_WAIT
);
1261 for (; x
< nptrs
; x
++)
1265 if (gfs2_metatype_check(sdp
, bh
[x
], GFS2_METATYPE_ED
)) {
1266 for (; x
< nptrs
; x
++)
1272 gfs2_trans_add_bh(ip
->i_gl
, bh
[x
], 1);
1274 memcpy(bh
[x
]->b_data
+ sizeof(struct gfs2_meta_header
),
1276 (sdp
->sd_jbsize
> amount
) ? amount
: sdp
->sd_jbsize
);
1278 amount
-= sdp
->sd_jbsize
;
1279 data
+= sdp
->sd_jbsize
;
1290 gfs2_trans_end(sdp
);
1296 int gfs2_ea_acl_chmod(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
,
1297 struct iattr
*attr
, char *data
)
1299 struct buffer_head
*dibh
;
1302 if (GFS2_EA_IS_STUFFED(el
->el_ea
)) {
1303 error
= gfs2_trans_begin(ip
->i_sbd
, RES_DINODE
+ RES_EATTR
, 0);
1307 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
1308 memcpy(GFS2_EA2DATA(el
->el_ea
),
1310 GFS2_EA_DATA_LEN(el
->el_ea
));
1312 error
= ea_acl_chmod_unstuffed(ip
, el
->el_ea
, data
);
1317 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1319 error
= inode_setattr(ip
->i_vnode
, attr
);
1320 gfs2_assert_warn(ip
->i_sbd
, !error
);
1321 gfs2_inode_attr_out(ip
);
1322 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1323 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
1327 gfs2_trans_end(ip
->i_sbd
);
1332 static int ea_dealloc_indirect(struct gfs2_inode
*ip
)
1334 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1335 struct gfs2_rgrp_list rlist
;
1336 struct buffer_head
*indbh
, *dibh
;
1337 uint64_t *eablk
, *end
;
1338 unsigned int rg_blocks
= 0;
1339 uint64_t bstart
= 0;
1340 unsigned int blen
= 0;
1341 unsigned int blks
= 0;
1345 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1347 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_di
.di_eattr
,
1348 DIO_START
| DIO_WAIT
, &indbh
);
1352 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
1357 eablk
= (uint64_t *)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1358 end
= eablk
+ sdp
->sd_inptrs
;
1360 for (; eablk
< end
; eablk
++) {
1365 bn
= be64_to_cpu(*eablk
);
1367 if (bstart
+ blen
== bn
)
1371 gfs2_rlist_add(sdp
, &rlist
, bstart
);
1378 gfs2_rlist_add(sdp
, &rlist
, bstart
);
1382 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
, 0);
1384 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1385 struct gfs2_rgrpd
*rgd
;
1386 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
1387 rg_blocks
+= rgd
->rd_ri
.ri_length
;
1390 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1392 goto out_rlist_free
;
1394 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+
1395 RES_INDIRECT
+ RES_STATFS
+
1400 gfs2_trans_add_bh(ip
->i_gl
, indbh
, 1);
1402 eablk
= (uint64_t *)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1406 for (; eablk
< end
; eablk
++) {
1411 bn
= be64_to_cpu(*eablk
);
1413 if (bstart
+ blen
== bn
)
1417 gfs2_free_meta(ip
, bstart
, blen
);
1423 if (!ip
->i_di
.di_blocks
)
1424 gfs2_consist_inode(ip
);
1425 ip
->i_di
.di_blocks
--;
1428 gfs2_free_meta(ip
, bstart
, blen
);
1430 ip
->i_di
.di_flags
&= ~GFS2_DIF_EA_INDIRECT
;
1432 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1434 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1435 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
1439 gfs2_trans_end(sdp
);
1442 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1445 gfs2_rlist_free(&rlist
);
1453 static int ea_dealloc_block(struct gfs2_inode
*ip
)
1455 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1456 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1457 struct gfs2_rgrpd
*rgd
;
1458 struct buffer_head
*dibh
;
1461 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_di
.di_eattr
);
1463 gfs2_consist_inode(ip
);
1467 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0,
1472 error
= gfs2_trans_begin(sdp
, RES_RG_BIT
+ RES_DINODE
+
1473 RES_STATFS
+ RES_QUOTA
, 1);
1477 gfs2_free_meta(ip
, ip
->i_di
.di_eattr
, 1);
1479 ip
->i_di
.di_eattr
= 0;
1480 if (!ip
->i_di
.di_blocks
)
1481 gfs2_consist_inode(ip
);
1482 ip
->i_di
.di_blocks
--;
1484 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1486 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1487 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
1491 gfs2_trans_end(sdp
);
1494 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
1500 * gfs2_ea_dealloc - deallocate the extended attribute fork
1506 int gfs2_ea_dealloc(struct gfs2_inode
*ip
)
1508 struct gfs2_alloc
*al
;
1511 al
= gfs2_alloc_get(ip
);
1513 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1517 error
= gfs2_rindex_hold(ip
->i_sbd
, &al
->al_ri_gh
);
1521 error
= ea_foreach(ip
, ea_dealloc_unstuffed
, NULL
);
1525 if (ip
->i_di
.di_flags
& GFS2_DIF_EA_INDIRECT
) {
1526 error
= ea_dealloc_indirect(ip
);
1531 error
= ea_dealloc_block(ip
);
1534 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
1537 gfs2_quota_unhold(ip
);