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
= (__be64
*)(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
;
230 unsigned int blen
= 0;
231 unsigned int blks
= 0;
235 if (GFS2_EA_IS_STUFFED(ea
))
238 dataptrs
= GFS2_EA2DATAPTRS(ea
);
239 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++, dataptrs
++) {
242 bn
= be64_to_cpu(*dataptrs
);
248 rgd
= gfs2_blk2rgrpd(sdp
, bn
);
250 gfs2_consist_inode(ip
);
254 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, &rg_gh
);
258 error
= gfs2_trans_begin(sdp
, rgd
->rd_ri
.ri_length
+ RES_DINODE
+
259 RES_EATTR
+ RES_STATFS
+ RES_QUOTA
, blks
);
263 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
265 dataptrs
= GFS2_EA2DATAPTRS(ea
);
266 for (x
= 0; x
< ea
->ea_num_ptrs
; x
++, dataptrs
++) {
269 bn
= be64_to_cpu(*dataptrs
);
271 if (bstart
+ blen
== bn
)
275 gfs2_free_meta(ip
, bstart
, blen
);
281 if (!ip
->i_di
.di_blocks
)
282 gfs2_consist_inode(ip
);
283 ip
->i_di
.di_blocks
--;
284 gfs2_set_inode_blocks(&ip
->i_inode
);
287 gfs2_free_meta(ip
, bstart
, blen
);
289 if (prev
&& !leave
) {
292 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
293 prev
->ea_rec_len
= cpu_to_be32(len
);
295 if (GFS2_EA_IS_LAST(ea
))
296 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
298 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
302 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
304 ip
->i_inode
.i_ctime
= CURRENT_TIME_SEC
;
305 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
306 gfs2_dinode_out(ip
, dibh
->b_data
);
313 gfs2_glock_dq_uninit(&rg_gh
);
317 static int ea_remove_unstuffed(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
318 struct gfs2_ea_header
*ea
,
319 struct gfs2_ea_header
*prev
, int leave
)
321 struct gfs2_alloc
*al
;
324 al
= gfs2_alloc_get(ip
);
326 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
330 error
= gfs2_rindex_hold(GFS2_SB(&ip
->i_inode
), &al
->al_ri_gh
);
334 error
= ea_dealloc_unstuffed(ip
, bh
, ea
, prev
, (leave
) ? &error
: NULL
);
336 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
339 gfs2_quota_unhold(ip
);
346 struct gfs2_ea_request
*ei_er
;
347 unsigned int ei_size
;
350 static int ea_list_i(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
351 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
354 struct ea_list
*ei
= private;
355 struct gfs2_ea_request
*er
= ei
->ei_er
;
356 unsigned int ea_size
= gfs2_ea_strlen(ea
);
358 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
)
361 if (er
->er_data_len
) {
366 if (ei
->ei_size
+ ea_size
> er
->er_data_len
)
369 switch (ea
->ea_type
) {
370 case GFS2_EATYPE_USR
:
374 case GFS2_EATYPE_SYS
:
378 case GFS2_EATYPE_SECURITY
:
379 prefix
= "security.";
386 memcpy(er
->er_data
+ ei
->ei_size
, prefix
, l
);
387 memcpy(er
->er_data
+ ei
->ei_size
+ l
, GFS2_EA2NAME(ea
),
389 memcpy(er
->er_data
+ ei
->ei_size
+ ea_size
- 1, &c
, 1);
392 ei
->ei_size
+= ea_size
;
402 * Returns: actual size of data on success, -errno on error
405 int gfs2_ea_list(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
407 struct gfs2_holder i_gh
;
410 if (!er
->er_data
|| !er
->er_data_len
) {
415 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
419 if (ip
->i_di
.di_eattr
) {
420 struct ea_list ei
= { .ei_er
= er
, .ei_size
= 0 };
422 error
= ea_foreach(ip
, ea_list_i
, &ei
);
427 gfs2_glock_dq_uninit(&i_gh
);
433 * ea_get_unstuffed - actually copies the unstuffed data into the
435 * @ip: The GFS2 inode
436 * @ea: The extended attribute header structure
437 * @data: The data to be copied
442 static int ea_get_unstuffed(struct gfs2_inode
*ip
, struct gfs2_ea_header
*ea
,
445 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
446 struct buffer_head
**bh
;
447 unsigned int amount
= GFS2_EA_DATA_LEN(ea
);
448 unsigned int nptrs
= DIV_ROUND_UP(amount
, sdp
->sd_jbsize
);
449 __be64
*dataptrs
= GFS2_EA2DATAPTRS(ea
);
453 bh
= kcalloc(nptrs
, sizeof(struct buffer_head
*), GFP_KERNEL
);
457 for (x
= 0; x
< nptrs
; x
++) {
458 error
= gfs2_meta_read(ip
->i_gl
, be64_to_cpu(*dataptrs
), 0,
468 for (x
= 0; x
< nptrs
; x
++) {
469 error
= gfs2_meta_wait(sdp
, bh
[x
]);
471 for (; x
< nptrs
; x
++)
475 if (gfs2_metatype_check(sdp
, bh
[x
], GFS2_METATYPE_ED
)) {
476 for (; x
< nptrs
; x
++)
482 memcpy(data
, bh
[x
]->b_data
+ sizeof(struct gfs2_meta_header
),
483 (sdp
->sd_jbsize
> amount
) ? amount
: sdp
->sd_jbsize
);
485 amount
-= sdp
->sd_jbsize
;
486 data
+= sdp
->sd_jbsize
;
496 int gfs2_ea_get_copy(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
,
499 if (GFS2_EA_IS_STUFFED(el
->el_ea
)) {
500 memcpy(data
, GFS2_EA2DATA(el
->el_ea
), GFS2_EA_DATA_LEN(el
->el_ea
));
503 return ea_get_unstuffed(ip
, el
->el_ea
, data
);
508 * @ip: The GFS2 inode
509 * @er: The request structure
511 * Returns: actual size of data on success, -errno on error
514 int gfs2_ea_get_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
516 struct gfs2_ea_location el
;
519 if (!ip
->i_di
.di_eattr
)
522 error
= gfs2_ea_find(ip
, er
, &el
);
528 if (er
->er_data_len
) {
529 if (GFS2_EA_DATA_LEN(el
.el_ea
) > er
->er_data_len
)
532 error
= gfs2_ea_get_copy(ip
, &el
, er
->er_data
);
535 error
= GFS2_EA_DATA_LEN(el
.el_ea
);
544 * @ip: The GFS2 inode
545 * @er: The request structure
547 * Returns: actual size of data on success, -errno on error
550 int gfs2_ea_get(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
552 struct gfs2_holder i_gh
;
555 if (!er
->er_name_len
||
556 er
->er_name_len
> GFS2_EA_MAX_NAME_LEN
)
558 if (!er
->er_data
|| !er
->er_data_len
) {
563 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
567 error
= gfs2_ea_ops
[er
->er_type
]->eo_get(ip
, er
);
569 gfs2_glock_dq_uninit(&i_gh
);
575 * ea_alloc_blk - allocates a new block for extended attributes.
576 * @ip: A pointer to the inode that's getting extended attributes
577 * @bhp: Pointer to pointer to a struct buffer_head
582 static int ea_alloc_blk(struct gfs2_inode
*ip
, struct buffer_head
**bhp
)
584 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
585 struct gfs2_ea_header
*ea
;
588 block
= gfs2_alloc_meta(ip
);
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
);
647 block
= gfs2_alloc_meta(ip
);
649 bh
= gfs2_meta_new(ip
->i_gl
, block
);
650 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
651 gfs2_metatype_set(bh
, GFS2_METATYPE_ED
, GFS2_FORMAT_ED
);
653 ip
->i_di
.di_blocks
++;
654 gfs2_set_inode_blocks(&ip
->i_inode
);
656 copy
= data_len
> sdp
->sd_jbsize
? sdp
->sd_jbsize
:
658 memcpy(bh
->b_data
+ mh_size
, data
, copy
);
659 if (copy
< sdp
->sd_jbsize
)
660 memset(bh
->b_data
+ mh_size
+ copy
, 0,
661 sdp
->sd_jbsize
- copy
);
663 *dataptr
++ = cpu_to_be64(bh
->b_blocknr
);
670 gfs2_assert_withdraw(sdp
, !data_len
);
676 typedef int (*ea_skeleton_call_t
) (struct gfs2_inode
*ip
,
677 struct gfs2_ea_request
*er
, void *private);
679 static int ea_alloc_skeleton(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
681 ea_skeleton_call_t skeleton_call
, void *private)
683 struct gfs2_alloc
*al
;
684 struct buffer_head
*dibh
;
687 al
= gfs2_alloc_get(ip
);
689 error
= gfs2_quota_lock(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
693 error
= gfs2_quota_check(ip
, ip
->i_inode
.i_uid
, ip
->i_inode
.i_gid
);
697 al
->al_requested
= blks
;
699 error
= gfs2_inplace_reserve(ip
);
703 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
),
704 blks
+ al
->al_rgd
->rd_ri
.ri_length
+
705 RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
709 error
= skeleton_call(ip
, er
, private);
713 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
715 if (er
->er_flags
& GFS2_ERF_MODE
) {
716 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
),
717 (ip
->i_inode
.i_mode
& S_IFMT
) ==
718 (er
->er_mode
& S_IFMT
));
719 ip
->i_inode
.i_mode
= er
->er_mode
;
721 ip
->i_inode
.i_ctime
= CURRENT_TIME_SEC
;
722 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
723 gfs2_dinode_out(ip
, dibh
->b_data
);
728 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
730 gfs2_inplace_release(ip
);
732 gfs2_quota_unlock(ip
);
738 static int ea_init_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
741 struct buffer_head
*bh
;
744 error
= ea_alloc_blk(ip
, &bh
);
748 ip
->i_di
.di_eattr
= bh
->b_blocknr
;
749 error
= ea_write(ip
, GFS2_EA_BH2FIRST(bh
), er
);
757 * ea_init - initializes a new eattr block
764 static int ea_init(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
766 unsigned int jbsize
= GFS2_SB(&ip
->i_inode
)->sd_jbsize
;
767 unsigned int blks
= 1;
769 if (GFS2_EAREQ_SIZE_STUFFED(er
) > jbsize
)
770 blks
+= DIV_ROUND_UP(er
->er_data_len
, jbsize
);
772 return ea_alloc_skeleton(ip
, er
, blks
, ea_init_i
, NULL
);
775 static struct gfs2_ea_header
*ea_split_ea(struct gfs2_ea_header
*ea
)
777 u32 ea_size
= GFS2_EA_SIZE(ea
);
778 struct gfs2_ea_header
*new = (struct gfs2_ea_header
*)((char *)ea
+
780 u32 new_size
= GFS2_EA_REC_LEN(ea
) - ea_size
;
781 int last
= ea
->ea_flags
& GFS2_EAFLAG_LAST
;
783 ea
->ea_rec_len
= cpu_to_be32(ea_size
);
784 ea
->ea_flags
^= last
;
786 new->ea_rec_len
= cpu_to_be32(new_size
);
787 new->ea_flags
= last
;
792 static void ea_set_remove_stuffed(struct gfs2_inode
*ip
,
793 struct gfs2_ea_location
*el
)
795 struct gfs2_ea_header
*ea
= el
->el_ea
;
796 struct gfs2_ea_header
*prev
= el
->el_prev
;
799 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
801 if (!prev
|| !GFS2_EA_IS_STUFFED(ea
)) {
802 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
804 } else if (GFS2_EA2NEXT(prev
) != ea
) {
805 prev
= GFS2_EA2NEXT(prev
);
806 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
), GFS2_EA2NEXT(prev
) == ea
);
809 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
810 prev
->ea_rec_len
= cpu_to_be32(len
);
812 if (GFS2_EA_IS_LAST(ea
))
813 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
819 struct gfs2_ea_request
*es_er
;
820 struct gfs2_ea_location
*es_el
;
822 struct buffer_head
*es_bh
;
823 struct gfs2_ea_header
*es_ea
;
826 static int ea_set_simple_noalloc(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
827 struct gfs2_ea_header
*ea
, struct ea_set
*es
)
829 struct gfs2_ea_request
*er
= es
->es_er
;
830 struct buffer_head
*dibh
;
833 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ 2 * RES_EATTR
, 0);
837 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
840 ea
= ea_split_ea(ea
);
842 ea_write(ip
, ea
, er
);
845 ea_set_remove_stuffed(ip
, es
->es_el
);
847 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
851 if (er
->er_flags
& GFS2_ERF_MODE
) {
852 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
),
853 (ip
->i_inode
.i_mode
& S_IFMT
) == (er
->er_mode
& S_IFMT
));
854 ip
->i_inode
.i_mode
= er
->er_mode
;
856 ip
->i_inode
.i_ctime
= CURRENT_TIME_SEC
;
857 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
858 gfs2_dinode_out(ip
, dibh
->b_data
);
861 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
865 static int ea_set_simple_alloc(struct gfs2_inode
*ip
,
866 struct gfs2_ea_request
*er
, void *private)
868 struct ea_set
*es
= private;
869 struct gfs2_ea_header
*ea
= es
->es_ea
;
872 gfs2_trans_add_bh(ip
->i_gl
, es
->es_bh
, 1);
875 ea
= ea_split_ea(ea
);
877 error
= ea_write(ip
, ea
, er
);
882 ea_set_remove_stuffed(ip
, es
->es_el
);
887 static int ea_set_simple(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
888 struct gfs2_ea_header
*ea
, struct gfs2_ea_header
*prev
,
891 struct ea_set
*es
= private;
896 stuffed
= ea_calc_size(GFS2_SB(&ip
->i_inode
), es
->es_er
, &size
);
898 if (ea
->ea_type
== GFS2_EATYPE_UNUSED
) {
899 if (GFS2_EA_REC_LEN(ea
) < size
)
901 if (!GFS2_EA_IS_STUFFED(ea
)) {
902 error
= ea_remove_unstuffed(ip
, bh
, ea
, prev
, 1);
907 } else if (GFS2_EA_REC_LEN(ea
) - GFS2_EA_SIZE(ea
) >= size
)
913 error
= ea_set_simple_noalloc(ip
, bh
, ea
, es
);
921 blks
= 2 + DIV_ROUND_UP(es
->es_er
->er_data_len
,
922 GFS2_SB(&ip
->i_inode
)->sd_jbsize
);
924 error
= ea_alloc_skeleton(ip
, es
->es_er
, blks
,
925 ea_set_simple_alloc
, es
);
933 static int ea_set_block(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
936 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
937 struct buffer_head
*indbh
, *newbh
;
940 int mh_size
= sizeof(struct gfs2_meta_header
);
942 if (ip
->i_di
.di_flags
& GFS2_DIF_EA_INDIRECT
) {
945 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_di
.di_eattr
, DIO_WAIT
,
950 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
955 eablk
= (__be64
*)(indbh
->b_data
+ mh_size
);
956 end
= eablk
+ sdp
->sd_inptrs
;
958 for (; eablk
< end
; eablk
++)
967 gfs2_trans_add_bh(ip
->i_gl
, indbh
, 1);
971 blk
= gfs2_alloc_meta(ip
);
973 indbh
= gfs2_meta_new(ip
->i_gl
, blk
);
974 gfs2_trans_add_bh(ip
->i_gl
, indbh
, 1);
975 gfs2_metatype_set(indbh
, GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
976 gfs2_buffer_clear_tail(indbh
, mh_size
);
978 eablk
= (__be64
*)(indbh
->b_data
+ mh_size
);
979 *eablk
= cpu_to_be64(ip
->i_di
.di_eattr
);
980 ip
->i_di
.di_eattr
= blk
;
981 ip
->i_di
.di_flags
|= GFS2_DIF_EA_INDIRECT
;
982 ip
->i_di
.di_blocks
++;
983 gfs2_set_inode_blocks(&ip
->i_inode
);
988 error
= ea_alloc_blk(ip
, &newbh
);
992 *eablk
= cpu_to_be64((u64
)newbh
->b_blocknr
);
993 error
= ea_write(ip
, GFS2_EA_BH2FIRST(newbh
), er
);
999 ea_set_remove_stuffed(ip
, private);
1006 static int ea_set_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
,
1007 struct gfs2_ea_location
*el
)
1010 unsigned int blks
= 2;
1013 memset(&es
, 0, sizeof(struct ea_set
));
1017 error
= ea_foreach(ip
, ea_set_simple
, &es
);
1023 if (!(ip
->i_di
.di_flags
& GFS2_DIF_EA_INDIRECT
))
1025 if (GFS2_EAREQ_SIZE_STUFFED(er
) > GFS2_SB(&ip
->i_inode
)->sd_jbsize
)
1026 blks
+= DIV_ROUND_UP(er
->er_data_len
, GFS2_SB(&ip
->i_inode
)->sd_jbsize
);
1028 return ea_alloc_skeleton(ip
, er
, blks
, ea_set_block
, el
);
1031 static int ea_set_remove_unstuffed(struct gfs2_inode
*ip
,
1032 struct gfs2_ea_location
*el
)
1034 if (el
->el_prev
&& GFS2_EA2NEXT(el
->el_prev
) != el
->el_ea
) {
1035 el
->el_prev
= GFS2_EA2NEXT(el
->el_prev
);
1036 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
),
1037 GFS2_EA2NEXT(el
->el_prev
) == el
->el_ea
);
1040 return ea_remove_unstuffed(ip
, el
->el_bh
, el
->el_ea
, el
->el_prev
,0);
1043 int gfs2_ea_set_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
1045 struct gfs2_ea_location el
;
1048 if (!ip
->i_di
.di_eattr
) {
1049 if (er
->er_flags
& XATTR_REPLACE
)
1051 return ea_init(ip
, er
);
1054 error
= gfs2_ea_find(ip
, er
, &el
);
1059 if (ip
->i_di
.di_flags
& GFS2_DIF_APPENDONLY
) {
1065 if (!(er
->er_flags
& XATTR_CREATE
)) {
1066 int unstuffed
= !GFS2_EA_IS_STUFFED(el
.el_ea
);
1067 error
= ea_set_i(ip
, er
, &el
);
1068 if (!error
&& unstuffed
)
1069 ea_set_remove_unstuffed(ip
, &el
);
1075 if (!(er
->er_flags
& XATTR_REPLACE
))
1076 error
= ea_set_i(ip
, er
, NULL
);
1082 int gfs2_ea_set(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
1084 struct gfs2_holder i_gh
;
1087 if (!er
->er_name_len
|| er
->er_name_len
> GFS2_EA_MAX_NAME_LEN
)
1089 if (!er
->er_data
|| !er
->er_data_len
) {
1091 er
->er_data_len
= 0;
1093 error
= ea_check_size(GFS2_SB(&ip
->i_inode
), er
);
1097 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1101 if (IS_IMMUTABLE(&ip
->i_inode
))
1104 error
= gfs2_ea_ops
[er
->er_type
]->eo_set(ip
, er
);
1106 gfs2_glock_dq_uninit(&i_gh
);
1111 static int ea_remove_stuffed(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
)
1113 struct gfs2_ea_header
*ea
= el
->el_ea
;
1114 struct gfs2_ea_header
*prev
= el
->el_prev
;
1115 struct buffer_head
*dibh
;
1118 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ RES_EATTR
, 0);
1122 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
1127 len
= GFS2_EA_REC_LEN(prev
) + GFS2_EA_REC_LEN(ea
);
1128 prev
->ea_rec_len
= cpu_to_be32(len
);
1130 if (GFS2_EA_IS_LAST(ea
))
1131 prev
->ea_flags
|= GFS2_EAFLAG_LAST
;
1133 ea
->ea_type
= GFS2_EATYPE_UNUSED
;
1135 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1137 ip
->i_inode
.i_ctime
= CURRENT_TIME_SEC
;
1138 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1139 gfs2_dinode_out(ip
, dibh
->b_data
);
1143 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
1148 int gfs2_ea_remove_i(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
1150 struct gfs2_ea_location el
;
1153 if (!ip
->i_di
.di_eattr
)
1156 error
= gfs2_ea_find(ip
, er
, &el
);
1162 if (GFS2_EA_IS_STUFFED(el
.el_ea
))
1163 error
= ea_remove_stuffed(ip
, &el
);
1165 error
= ea_remove_unstuffed(ip
, el
.el_bh
, el
.el_ea
, el
.el_prev
,
1174 * gfs2_ea_remove - sets (or creates or replaces) an extended attribute
1175 * @ip: pointer to the inode of the target file
1176 * @er: request information
1181 int gfs2_ea_remove(struct gfs2_inode
*ip
, struct gfs2_ea_request
*er
)
1183 struct gfs2_holder i_gh
;
1186 if (!er
->er_name_len
|| er
->er_name_len
> GFS2_EA_MAX_NAME_LEN
)
1189 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1193 if (IS_IMMUTABLE(&ip
->i_inode
) || IS_APPEND(&ip
->i_inode
))
1196 error
= gfs2_ea_ops
[er
->er_type
]->eo_remove(ip
, er
);
1198 gfs2_glock_dq_uninit(&i_gh
);
1203 static int ea_acl_chmod_unstuffed(struct gfs2_inode
*ip
,
1204 struct gfs2_ea_header
*ea
, char *data
)
1206 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1207 struct buffer_head
**bh
;
1208 unsigned int amount
= GFS2_EA_DATA_LEN(ea
);
1209 unsigned int nptrs
= DIV_ROUND_UP(amount
, sdp
->sd_jbsize
);
1210 __be64
*dataptrs
= GFS2_EA2DATAPTRS(ea
);
1214 bh
= kcalloc(nptrs
, sizeof(struct buffer_head
*), GFP_KERNEL
);
1218 error
= gfs2_trans_begin(sdp
, nptrs
+ RES_DINODE
, 0);
1222 for (x
= 0; x
< nptrs
; x
++) {
1223 error
= gfs2_meta_read(ip
->i_gl
, be64_to_cpu(*dataptrs
), 0,
1233 for (x
= 0; x
< nptrs
; x
++) {
1234 error
= gfs2_meta_wait(sdp
, bh
[x
]);
1236 for (; x
< nptrs
; x
++)
1240 if (gfs2_metatype_check(sdp
, bh
[x
], GFS2_METATYPE_ED
)) {
1241 for (; x
< nptrs
; x
++)
1247 gfs2_trans_add_bh(ip
->i_gl
, bh
[x
], 1);
1249 memcpy(bh
[x
]->b_data
+ sizeof(struct gfs2_meta_header
), data
,
1250 (sdp
->sd_jbsize
> amount
) ? amount
: sdp
->sd_jbsize
);
1252 amount
-= sdp
->sd_jbsize
;
1253 data
+= sdp
->sd_jbsize
;
1263 gfs2_trans_end(sdp
);
1268 int gfs2_ea_acl_chmod(struct gfs2_inode
*ip
, struct gfs2_ea_location
*el
,
1269 struct iattr
*attr
, char *data
)
1271 struct buffer_head
*dibh
;
1274 if (GFS2_EA_IS_STUFFED(el
->el_ea
)) {
1275 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
+ RES_EATTR
, 0);
1279 gfs2_trans_add_bh(ip
->i_gl
, el
->el_bh
, 1);
1280 memcpy(GFS2_EA2DATA(el
->el_ea
), data
,
1281 GFS2_EA_DATA_LEN(el
->el_ea
));
1283 error
= ea_acl_chmod_unstuffed(ip
, el
->el_ea
, data
);
1288 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1290 error
= inode_setattr(&ip
->i_inode
, attr
);
1291 gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), !error
);
1292 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1293 gfs2_dinode_out(ip
, dibh
->b_data
);
1297 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
1302 static int ea_dealloc_indirect(struct gfs2_inode
*ip
)
1304 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1305 struct gfs2_rgrp_list rlist
;
1306 struct buffer_head
*indbh
, *dibh
;
1307 __be64
*eablk
, *end
;
1308 unsigned int rg_blocks
= 0;
1310 unsigned int blen
= 0;
1311 unsigned int blks
= 0;
1315 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
1317 error
= gfs2_meta_read(ip
->i_gl
, ip
->i_di
.di_eattr
, DIO_WAIT
, &indbh
);
1321 if (gfs2_metatype_check(sdp
, indbh
, GFS2_METATYPE_IN
)) {
1326 eablk
= (__be64
*)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1327 end
= eablk
+ sdp
->sd_inptrs
;
1329 for (; eablk
< end
; eablk
++) {
1334 bn
= be64_to_cpu(*eablk
);
1336 if (bstart
+ blen
== bn
)
1340 gfs2_rlist_add(sdp
, &rlist
, bstart
);
1347 gfs2_rlist_add(sdp
, &rlist
, bstart
);
1351 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
, 0);
1353 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
1354 struct gfs2_rgrpd
*rgd
;
1355 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
1356 rg_blocks
+= rgd
->rd_ri
.ri_length
;
1359 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1361 goto out_rlist_free
;
1363 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+ RES_INDIRECT
+
1364 RES_STATFS
+ RES_QUOTA
, blks
);
1368 gfs2_trans_add_bh(ip
->i_gl
, indbh
, 1);
1370 eablk
= (__be64
*)(indbh
->b_data
+ sizeof(struct gfs2_meta_header
));
1374 for (; eablk
< end
; eablk
++) {
1379 bn
= be64_to_cpu(*eablk
);
1381 if (bstart
+ blen
== bn
)
1385 gfs2_free_meta(ip
, bstart
, blen
);
1391 if (!ip
->i_di
.di_blocks
)
1392 gfs2_consist_inode(ip
);
1393 ip
->i_di
.di_blocks
--;
1394 gfs2_set_inode_blocks(&ip
->i_inode
);
1397 gfs2_free_meta(ip
, bstart
, blen
);
1399 ip
->i_di
.di_flags
&= ~GFS2_DIF_EA_INDIRECT
;
1401 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1403 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1404 gfs2_dinode_out(ip
, dibh
->b_data
);
1408 gfs2_trans_end(sdp
);
1411 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
1413 gfs2_rlist_free(&rlist
);
1419 static int ea_dealloc_block(struct gfs2_inode
*ip
)
1421 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1422 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1423 struct gfs2_rgrpd
*rgd
;
1424 struct buffer_head
*dibh
;
1427 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_di
.di_eattr
);
1429 gfs2_consist_inode(ip
);
1433 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0,
1438 error
= gfs2_trans_begin(sdp
, RES_RG_BIT
+ RES_DINODE
+ RES_STATFS
+
1443 gfs2_free_meta(ip
, ip
->i_di
.di_eattr
, 1);
1445 ip
->i_di
.di_eattr
= 0;
1446 if (!ip
->i_di
.di_blocks
)
1447 gfs2_consist_inode(ip
);
1448 ip
->i_di
.di_blocks
--;
1449 gfs2_set_inode_blocks(&ip
->i_inode
);
1451 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1453 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1454 gfs2_dinode_out(ip
, dibh
->b_data
);
1458 gfs2_trans_end(sdp
);
1461 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
1466 * gfs2_ea_dealloc - deallocate the extended attribute fork
1472 int gfs2_ea_dealloc(struct gfs2_inode
*ip
)
1474 struct gfs2_alloc
*al
;
1477 al
= gfs2_alloc_get(ip
);
1479 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1483 error
= gfs2_rindex_hold(GFS2_SB(&ip
->i_inode
), &al
->al_ri_gh
);
1487 error
= ea_foreach(ip
, ea_dealloc_unstuffed
, NULL
);
1491 if (ip
->i_di
.di_flags
& GFS2_DIF_EA_INDIRECT
) {
1492 error
= ea_dealloc_indirect(ip
);
1497 error
= ea_dealloc_block(ip
);
1500 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
1502 gfs2_quota_unhold(ip
);